|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import json |
| 5 | +import shutil |
| 6 | +import time |
| 7 | +import distutils.spawn |
| 8 | +from subprocess import call, check_output |
| 9 | + |
| 10 | +# Disable aws CLI pagination |
| 11 | +os.environ["AWS_PAGER"] = "" |
| 12 | + |
| 13 | +# Check requirements first |
| 14 | +for cmd in ["pip", "zip"]: |
| 15 | + if distutils.spawn.find_executable(cmd) is None: |
| 16 | + print("Can't find required tool: %s" % cmd) |
| 17 | + exit(1) |
| 18 | + |
| 19 | +# Determine where this script is running from |
| 20 | +this_file_path = os.path.dirname(os.path.realpath(__file__)) |
| 21 | + |
| 22 | +# Change directory to the base of the Python sensor repository |
| 23 | +os.chdir(this_file_path + "/../") |
| 24 | + |
| 25 | +cwd = os.getcwd() |
| 26 | +print("===> Working directory is: %s" % cwd) |
| 27 | + |
| 28 | +# For development, respect or set PYTHONPATH to this repository |
| 29 | +local_env = os.environ.copy() |
| 30 | +if "PYTHONPATH" not in os.environ: |
| 31 | + local_env["PYTHONPATH"] = os.getcwd() |
| 32 | + |
| 33 | +build_directory = os.getcwd() + '/build/lambda/python' |
| 34 | + |
| 35 | +if os.path.isdir(build_directory): |
| 36 | + print("===> Cleaning build pre-existing directory: %s" % build_directory) |
| 37 | + shutil.rmtree(build_directory) |
| 38 | + |
| 39 | +print("===> Creating new build directory: %s" % build_directory) |
| 40 | +os.makedirs(build_directory, exist_ok=True) |
| 41 | + |
| 42 | +print("===> Installing Instana and dependencies into build directory") |
| 43 | +call(["pip", "install", "-q", "-U", "-t", os.getcwd() + '/build/lambda/python', "instana"], env=local_env) |
| 44 | + |
| 45 | +print("===> Manually copying in local dev code") |
| 46 | +shutil.rmtree(build_directory + "/instana") |
| 47 | +shutil.copytree(os.getcwd() + '/instana', build_directory + "/instana") |
| 48 | + |
| 49 | +print("===> Creating Lambda ZIP file") |
| 50 | +timestamp = time.strftime("%Y-%m-%d_%H:%M:%S") |
| 51 | +zip_filename = "instana-py-layer-%s.zip" % timestamp |
| 52 | + |
| 53 | +os.chdir(os.getcwd() + "/build/lambda/") |
| 54 | +call(["zip", "-q", "-r", zip_filename, "./python", "-x", "*.pyc", "./python/pip*", "./python/setuptools*", "./python/wheel*"]) |
| 55 | + |
| 56 | +fq_zip_filename = os.getcwd() + '/%s' % zip_filename |
| 57 | +aws_zip_filename = "fileb://%s" % fq_zip_filename |
| 58 | +print("Zipfile should be at: ", fq_zip_filename) |
| 59 | + |
| 60 | +regions = ['ap-northeast-1', 'ap-northeast-2', 'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', |
| 61 | + 'eu-central-1', 'eu-north-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'sa-east-1', 'us-east-1', |
| 62 | + 'us-east-2', 'us-west-1', 'us-west-2'] |
| 63 | + |
| 64 | +# regions = ['us-west-1'] |
| 65 | + |
| 66 | +# LAYER_NAME = "instana-py-test" |
| 67 | +LAYER_NAME = "instana-python" |
| 68 | + |
| 69 | +published = dict() |
| 70 | + |
| 71 | +for region in regions: |
| 72 | + print("===> Uploading layer to AWS %s " % region) |
| 73 | + response = check_output(["aws", "--region", region, "lambda", "publish-layer-version", |
| 74 | + "--description", |
| 75 | + "Provides Instana tracing and monitoring of AWS Lambda functions built with Python", |
| 76 | + "--license-info", "MIT", "--output", "json", |
| 77 | + "--layer-name", LAYER_NAME, "--zip-file", aws_zip_filename, |
| 78 | + "--compatible-runtimes", "python2.7", "python3.6", "python3.7", "python3.8"]) |
| 79 | + |
| 80 | + json_data = json.loads(response) |
| 81 | + version = json_data['Version'] |
| 82 | + print("===> Uploaded version is %s" % version) |
| 83 | + |
| 84 | + print("===> Making layer public...") |
| 85 | + response = check_output(["aws", "--region", region, "lambda", "add-layer-version-permission", |
| 86 | + "--layer-name", LAYER_NAME, "--version-number", str(version), |
| 87 | + "--statement-id", "public-permission-all-accounts", |
| 88 | + "--principal", "*", |
| 89 | + "--action", "lambda:GetLayerVersion", |
| 90 | + "--output", "text"]) |
| 91 | + |
| 92 | + published[region] = json_data['LayerVersionArn'] |
| 93 | + |
| 94 | + |
| 95 | +print("===> Published list:") |
| 96 | +for key in published.keys(): |
| 97 | + print("%s\t%s" % (key, published[key])) |
0 commit comments