-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathbuild_layers.sh
executable file
·90 lines (73 loc) · 2.8 KB
/
build_layers.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2019 Datadog, Inc.
# Builds datadog-lambda-python layers for Lambda functions
# Usage: PYTHON_VERSION=3.11 ./build_layers.sh
# If PYTHON_VERSION is not specified, all versions will be built.
set -e
LAYER_DIR=".layers"
LAYER_FILES_PREFIX="datadog_lambda_py"
AVAILABLE_PYTHON_VERSIONS=("3.8" "3.9" "3.10" "3.11" "3.12" "3.13")
AVAILABLE_ARCHS=("arm64" "amd64")
if [ -z "$ARCH" ]; then
echo "No architectures specified, building layers for all architectures."
ARCHS=("${AVAILABLE_ARCHS[@]}")
else
echo "Architecture specified: $ARCH"
if [[ ! " ${AVAILABLE_ARCHS[@]} " =~ " ${ARCH} " ]]; then
echo "Architecture $ARCH is not a valid option. Choose from: ${AVAILABLE_ARCHS[@]}"
echo ""
echo "EXITING SCRIPT."
exit 1
fi
ARCHS=$ARCH
fi
# Determine which Python versions to build layers for
if [ -z "$PYTHON_VERSION" ]; then
echo "Python version not specified, building layers for all versions."
PYTHON_VERSIONS=("${AVAILABLE_PYTHON_VERSIONS[@]}")
else
echo "Python version specified: $PYTHON_VERSION"
if [[ ! " ${AVAILABLE_PYTHON_VERSIONS[@]} " =~ " ${PYTHON_VERSION} " ]]; then
echo "Python version $PYTHON_VERSION is not a valid option. Choose from: ${AVAILABLE_PYTHON_VERSIONS[@]}"
echo ""
echo "EXITING SCRIPT."
exit 1
fi
PYTHON_VERSIONS=$PYTHON_VERSION
fi
function make_path_absolute {
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
}
function docker_build_zip {
# Args: [python version] [zip destination]
destination=$(make_path_absolute $2)
arch=$3
# Install datadogpy in a docker container to avoid the mess from switching
# between different python runtimes.
temp_dir=$(mktemp -d)
docker buildx build -t datadog-lambda-python-${arch}:$1 . --no-cache \
--build-arg image=public.ecr.aws/sam/build-python$1:1 \
--build-arg runtime=python$1 \
--platform linux/${arch} \
--progress=plain \
-o $temp_dir/python
# Zip to destination, and keep directory structure as based in $temp_dir
(cd $temp_dir && zip -q -r $destination ./)
rm -rf $temp_dir
echo "Done creating archive $destination"
}
rm -rf $LAYER_DIR
mkdir $LAYER_DIR
for python_version in "${PYTHON_VERSIONS[@]}"
do
for architecture in "${ARCHS[@]}"
do
echo "Building layer for Python ${python_version} arch=${architecture}"
docker_build_zip ${python_version} $LAYER_DIR/${LAYER_FILES_PREFIX}-${architecture}-${python_version}.zip ${architecture}
done
done
echo "Done creating layers:"
ls $LAYER_DIR | xargs -I _ echo "$LAYER_DIR/_"