Skip to content
Closed
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
57 changes: 14 additions & 43 deletions scripts/build_layers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,49 +91,29 @@ trap cleanup EXIT
# Helper: replace the multi-line ddtrace dependency in pyproject.toml.
# Uses perl instead of sed -z for macOS/Linux portability.
replace_ddtrace_dep() {
echo "Replacing dep with $1"
perl -i -0777 -pe "s|ddtrace = \[[^\]]*\]|$1|gs" pyproject.toml
}

function make_path_absolute {
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
}

function search_wheel {
# Args: [wheel base name] [index]

WHEEL_BASENAME=$1
INDEX=$2

SEARCH_PATTERN="${WHEEL_BASENAME}-[^\"]*${PY_TAG}[^\"]*${PLATFORM}[^\"]*\.whl"
INDEX_URL="${S3_BASE}/index-${INDEX}.html"
echo "Searching for wheel ${SEARCH_PATTERN}"
export WHEEL_FILE=$(curl -sSfL ${INDEX_URL} | grep -o "$SEARCH_PATTERN" | head -n 1)
if [ ! -z "${WHEEL_FILE}" ]; then
curl -sSfL "${S3_BASE}/${WHEEL_FILE}" -o "${WHEEL_FILE}"
echo "Using S3 wheel: ${WHEEL_FILE}"
replace_ddtrace_dep "${WHEEL_BASENAME} = { file = \"${WHEEL_FILE}\" }"
fi
}

function find_and_spec_wheel {
# Args: [python version] [wheel base name] [index]
function docker_build_zip {
# Args: [python version] [zip destination]

arch=$2
wheel_basename=$3
index=$4
destination=$(make_path_absolute $2)
arch=$3

# Restore pyproject.toml to a clean state for each build iteration
cp pyproject.toml.bak pyproject.toml

# Replace ddtrace source if necessary
if [ -n "$DD_TRACE_COMMIT" ]; then
replace_ddtrace_dep "${wheel_basename} = { git = \"https://github.com/DataDog/dd-trace-py.git\", rev = \"$DD_TRACE_COMMIT\" }"
replace_ddtrace_dep "ddtrace = { git = \"https://github.com/DataDog/dd-trace-py.git\", rev = \"$DD_TRACE_COMMIT\" }"
elif [ -n "$DD_TRACE_COMMIT_BRANCH" ]; then
replace_ddtrace_dep "${wheel_basename} = { git = \"https://github.com/DataDog/dd-trace-py.git\", branch = \"$DD_TRACE_COMMIT_BRANCH\" }"
replace_ddtrace_dep "ddtrace = { git = \"https://github.com/DataDog/dd-trace-py.git\", branch = \"$DD_TRACE_COMMIT_BRANCH\" }"
elif [ -n "$DD_TRACE_WHEEL" ]; then
wheel_basename=$(sed 's/^\.\///' <<< ${DD_TRACE_WHEEL%%-*})
replace_ddtrace_dep "${wheel_basename} = { file = \"$DD_TRACE_WHEEL\" }"
replace_ddtrace_dep "ddtrace = { file = \"$DD_TRACE_WHEEL\" }"
elif [ -n "$UPSTREAM_PIPELINE_ID" ]; then
S3_BASE="https://dd-trace-py-builds.s3.amazonaws.com/${UPSTREAM_PIPELINE_ID}"
if [ "${arch}" = "amd64" ]; then
Expand All @@ -142,19 +122,18 @@ function find_and_spec_wheel {
PLATFORM="manylinux2014_aarch64"
fi
PY_TAG="cp$(echo "$1" | tr -d '.')"
search_wheel ${wheel_basename} ${index}
WHEEL_FILE=$(curl -sSfL "${S3_BASE}/index-manylinux2014.html" \
| grep -o "ddtrace-[^\"]*${PY_TAG}[^\"]*${PLATFORM}[^\"]*\.whl" \
| head -n 1)
if [ -z "${WHEEL_FILE}" ]; then
echo "No S3 wheel found for ${PY_TAG} ${PLATFORM}, using default pyproject.toml version"
return 1
else
curl -sSfL "${S3_BASE}/${WHEEL_FILE}" -o "${WHEEL_FILE}"
echo "Using S3 wheel: ${WHEEL_FILE}"
replace_ddtrace_dep "ddtrace = { file = \"${WHEEL_FILE}\" }"
fi
fi
}

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)
Expand All @@ -180,14 +159,6 @@ do
for architecture in "${ARCHS[@]}"
do
echo "Building layer for Python ${python_version} arch=${architecture}"
set +e
find_and_spec_wheel ${python_version} ${architecture} "ddtrace_serverless" "serverless"
FAILURE=$?
if [ $FAILURE != 0 ]; then
echo "Attempting layer build again with package ddtrace"
find_and_spec_wheel ${python_version} ${architecture} "ddtrace" "manylinux2014"
fi
set -e
docker_build_zip ${python_version} $LAYER_DIR/${LAYER_FILES_PREFIX}-${architecture}-${python_version}.zip ${architecture}
done
done
Expand Down
Loading