Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Build dist ZIP for AWS Lambda layers #1001

Merged
merged 7 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

- run: |
pip install virtualenv
make dist
make aws-lambda-layer-build
- uses: actions/upload-artifact@v2
with:
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ help:
@echo "make test: Run basic tests (not testing most integrations)"
@echo "make test-all: Run ALL tests (slow, closest to CI)"
@echo "make format: Run code formatters (destructive)"
@echo "make aws-lambda-layer-build: Build serverless ZIP dist package"
@echo
@echo "Also make sure to read ./CONTRIBUTING.md"
@false
Expand Down Expand Up @@ -58,3 +59,7 @@ apidocs-hotfix: apidocs
@$(VENV_PATH)/bin/pip install ghp-import
@$(VENV_PATH)/bin/ghp-import -pf docs/_build
.PHONY: apidocs-hotfix

aws-lambda-layer-build: dist
$(VENV_PATH)/bin/python -m scripts.build-awslambda-layer
.PHONY: aws-lambda-layer-build
71 changes: 71 additions & 0 deletions scripts/build-awslambda-layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
import subprocess
import tempfile
import shutil
from sentry_sdk.consts import VERSION as SDK_VERSION


DIST_DIRNAME = "dist"
DIST_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", DIST_DIRNAME))
DEST_ZIP_FILENAME = f"sentry-python-serverless-{SDK_VERSION}.zip"
WHEELS_FILEPATH = os.path.join(
DIST_DIRNAME, f"sentry_sdk-{SDK_VERSION}-py2.py3-none-any.whl"
)

# Top directory in the ZIP file. Placing the Sentry package in `/python` avoids
# creating a directory for a specific version. For more information, see
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
PACKAGE_PARENT_DIRECTORY = "python"


class PackageBuilder:
def __init__(self, base_dir) -> None:
self.base_dir = base_dir
self.packages_dir = self.get_relative_path_of(PACKAGE_PARENT_DIRECTORY)

def make_directories(self):
os.makedirs(self.packages_dir)

def install_python_binaries(self):
subprocess.run(
[
"pip",
"install",
"--no-cache-dir", # Disables the cache -> always accesses PyPI
"-q", # Quiet
WHEELS_FILEPATH, # Copied to the target directory before installation
"-t", # Target directory flag
self.packages_dir,
],
check=True,
)

def zip(self, filename):
subprocess.run(
[
"zip",
"-q", # Quiet
"-x", # Exclude files
"**/__pycache__/*", # Files to be excluded
"-r", # Recurse paths
filename, # Output filename
PACKAGE_PARENT_DIRECTORY, # Files to be zipped
],
cwd=self.base_dir,
check=True, # Raises CalledProcessError if exit status is non-zero
)

def get_relative_path_of(self, subfile):
return os.path.join(self.base_dir, subfile)


def build_packaged_zip():
with tempfile.TemporaryDirectory() as tmp_dir:
package_builder = PackageBuilder(tmp_dir)
package_builder.make_directories()
package_builder.install_python_binaries()
package_builder.zip(DEST_ZIP_FILENAME)
shutil.copy(package_builder.get_relative_path_of(DEST_ZIP_FILENAME), DIST_DIR)


build_packaged_zip()