Skip to content

Commit e9f74dd

Browse files
committed
Building packages for Alpine 3.21+
1 parent 593aa56 commit e9f74dd

5 files changed

Lines changed: 188 additions & 7 deletions

File tree

agent/php/ElasticApm/Impl/Log/LoggableTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ protected function toLogLoggableTraitImpl(LogStreamInterface $stream, array $cus
8383
continue;
8484
}
8585

86-
$reflectionProperty->setAccessible(true);
86+
if (PHP_VERSION_ID < 80100) {
87+
$reflectionProperty->setAccessible(true);
88+
}
8789
$propName = $reflectionProperty->name;
8890
if (array_key_exists($propName, $customPropValues)) {
8991
continue;

packaging/Dockerfile.apk-v3

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM alpine:3.21
2+
3+
RUN apk add --no-cache alpine-sdk sudo
4+
5+
RUN adduser -D builder \
6+
&& addgroup builder abuild \
7+
&& echo "builder ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
8+
9+
RUN su builder -c "abuild-keygen -a -n" \
10+
&& cp /home/builder/.abuild/*.pub /etc/apk/keys/
11+
12+
COPY create-apk-v3.sh /bin/
13+
RUN chmod +x /bin/create-apk-v3.sh
14+
15+
WORKDIR /app
16+
ENTRYPOINT ["/bin/create-apk-v3.sh"]

packaging/Makefile

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SHELL = /bin/bash
22
MAKEFLAGS += --no-print-directory
33
IMAGE:=php-packaging
4+
IMAGE_APK_V3:=php-packaging-apk-v3
45
IMAGE_TAG:=latest
56
NAME:=apm-agent-php
67
VERSION?=$(shell grep 'VERSION' ../agent/php/ElasticApm/ElasticApm.php | cut -d= -f2 | tr -d " " | sed "s/'\(.*\)'.*/\1/g")
@@ -13,6 +14,11 @@ RELEASE_VERSION?=
1314
GITHUB_RELEASES_URL=https://github.com/elastic/apm-agent-php/releases/download
1415
BUILD_ARCH := $(if $(BUILD_ARCH),$(BUILD_ARCH),"x86-64")
1516

17+
DOCKER_PLATFORM := linux/amd64
18+
ifeq ($(BUILD_ARCH), arm64)
19+
DOCKER_PLATFORM := linux/arm64
20+
endif
21+
1622
ifeq ($(shell [ $(PHP_VERSION) \< 8.4 ] && echo true), true)
1723
RPM_IMAGE_VERSION = 0.0.3 # PHP < 8.4 - image using centos
1824
else
@@ -43,7 +49,7 @@ prepare: ## Build docker image for the packaging
4349
docker build --build-arg IMAGE_TAG=$(IMAGE_TAG) -t $(IMAGE) . || exit 1
4450

4551
.PHONY: build-docker-images
46-
build-docker-images: prepare prepare-apk prepare-deb prepare-rpm prepare-tar prepare-deb-apache prepare-deb-fpm ## Build all the docker images
52+
build-docker-images: prepare prepare-apk-v3-image prepare-apk prepare-deb prepare-rpm prepare-tar prepare-deb-apache prepare-deb-fpm ## Build all the docker images
4753
@echo 'Build docker images'
4854

4955
create-%: prepare ## Create the specific package
@@ -64,7 +70,27 @@ create-%: prepare ## Create the specific package
6470

6571
.PHONY: apk
6672
apk: FPM_FLAGS="--depends=bash"
67-
apk: create-apk ## Create the apk installer
73+
apk: create-apk ## Create the apk v2 installer (Alpine <=3.20)
74+
75+
.PHONY: prepare-apk-v3-image
76+
prepare-apk-v3-image: ## Build docker image for APK v3 packaging
77+
docker build -t $(IMAGE_APK_V3) -f Dockerfile.apk-v3 . || exit 1
78+
79+
.PHONY: apk-v3
80+
apk-v3: prepare-apk-v3-image ## Create the apk v3 installer (Alpine 3.21+)
81+
@echo "::group::$@"
82+
@echo "Creating APK v3 package..."
83+
mkdir -p $(PWD)/$(OUTPUT)
84+
docker run --rm \
85+
-v $(PWD):/app \
86+
-e NAME=$(NAME) \
87+
-e VERSION=$(VERSION) \
88+
-e OUTPUT=$(OUTPUT) \
89+
-e PHP_AGENT_DIR=$(PHP_AGENT_DIR) \
90+
-e BUILD_ARCH=$(BUILD_ARCH) \
91+
-e GIT_SHA=$(GIT_SHA) \
92+
-w /app $(IMAGE_APK_V3)
93+
@echo "::endgroup::"
6894

6995
.PHONY: deb
7096
deb: create-deb ## Create the deb installer
@@ -80,7 +106,7 @@ version: ## Show the fpm version
80106
docker run --rm $(IMAGE) --version || exit 1
81107

82108
.PHONY: package
83-
package: apk deb rpm tar ## Create all the installers
109+
package: apk apk-v3 deb rpm tar ## Create all the installers
84110

85111
.PHONY: info
86112
info: apk-info deb-info rpm-info tar-info ## Show the package metadata for all the installers

packaging/create-apk-v3.sh

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env sh
2+
set -x
3+
4+
## Create APK v3 package using abuild on Alpine 3.21+
5+
## This produces packages compatible with apk-tools 3.x
6+
7+
PACKAGE_ARCH=""
8+
if [ "${BUILD_ARCH}" = "x86-64" ]; then
9+
PACKAGE_ARCH="x86_64"
10+
elif [ "${BUILD_ARCH}" = "arm64" ]; then
11+
PACKAGE_ARCH="aarch64"
12+
else
13+
echo "Architecture not supported"
14+
exit 1
15+
fi
16+
17+
BUILD_TARGET="linuxmusl-${BUILD_ARCH}"
18+
BUILD_EXT_DIR=agent/native/_build/${BUILD_TARGET}-release/ext
19+
BUILD_LOADER_DIR=agent/native/_build/${BUILD_TARGET}-release/loader/code
20+
21+
echo "Fetching agent libraries from ${BUILD_EXT_DIR}"
22+
echo "Package architecture ${PACKAGE_ARCH}"
23+
24+
if [ ! -d "${BUILD_EXT_DIR}" ]; then
25+
echo "Agent libraries not built! Missing folder ${BUILD_EXT_DIR}"
26+
exit 1
27+
fi
28+
29+
touch build/elastic-apm.ini
30+
31+
# Prepare extensions in temp dir
32+
EXTENSIONS_DIR=/tmp/extensions
33+
mkdir -p ${EXTENSIONS_DIR}
34+
cp ${BUILD_EXT_DIR}/*.so ${EXTENSIONS_DIR}/
35+
cp ${BUILD_LOADER_DIR}/*.so ${EXTENSIONS_DIR}/
36+
37+
# Alpine pkgver does not allow dashes
38+
PKGVER=$(echo ${VERSION} | tr '-' '_')
39+
40+
# Setup APKBUILD workspace
41+
WORK_DIR=/tmp/apk-v3-build
42+
ABUILD_DIR=${WORK_DIR}/${NAME}
43+
rm -rf ${WORK_DIR}
44+
mkdir -p ${ABUILD_DIR}
45+
46+
# Post-install trigger
47+
cat > ${ABUILD_DIR}/${NAME}.post-install << 'EOF'
48+
#!/bin/sh
49+
/opt/elastic/apm-agent-php/bin/post-install.sh
50+
EOF
51+
52+
# Pre-deinstall trigger
53+
cat > ${ABUILD_DIR}/${NAME}.pre-deinstall << 'EOF'
54+
#!/bin/sh
55+
/opt/elastic/apm-agent-php/bin/before-uninstall.sh
56+
EOF
57+
58+
# Create APKBUILD
59+
cat > ${ABUILD_DIR}/APKBUILD << BUILDEOF
60+
# Maintainer: APM Team <info@elastic.co>
61+
pkgname=${NAME}
62+
pkgver=${PKGVER}
63+
pkgrel=0
64+
pkgdesc="PHP agent for Elastic APM (Git Commit: ${GIT_SHA})"
65+
url="https://github.com/elastic/apm-agent-php"
66+
arch="all"
67+
license="Apache-2.0"
68+
depends="bash"
69+
install="\$pkgname.post-install \$pkgname.pre-deinstall"
70+
options="!check !fhs !strip !tracedeps"
71+
72+
package() {
73+
local dest="\$pkgdir${PHP_AGENT_DIR}"
74+
75+
install -d "\$dest/extensions"
76+
for f in ${EXTENSIONS_DIR}/*.so; do
77+
install -m755 "\$f" "\$dest/extensions/"
78+
done
79+
80+
mkdir -p "\$dest/src"
81+
cp -r /app/agent/php/* "\$dest/src/"
82+
83+
install -d "\$dest/etc"
84+
install -m644 /app/build/elastic-apm.ini "\$dest/etc/"
85+
install -m644 /app/packaging/elastic-apm-custom-template.ini "\$dest/etc/elastic-apm-custom.ini"
86+
87+
install -d "\$dest/bin"
88+
install -m755 /app/packaging/post-install.sh "\$dest/bin/post-install.sh"
89+
install -m755 /app/packaging/before-uninstall.sh "\$dest/bin/before-uninstall.sh"
90+
91+
install -d "\$dest/docs"
92+
install -m644 /app/README.md "\$dest/docs/README.md"
93+
}
94+
BUILDEOF
95+
96+
#print debug info
97+
echo "APKBUILD content:"
98+
cat ${ABUILD_DIR}/APKBUILD || true
99+
100+
101+
# Build as non-root user (abuild requirement)
102+
chown -R builder:builder ${WORK_DIR} ${EXTENSIONS_DIR}
103+
OUTPUT_DIR=${WORK_DIR}/output
104+
su builder -c "cd ${ABUILD_DIR} && abuild -F -d -P ${OUTPUT_DIR}"
105+
106+
# Find the built package
107+
APK_FILE=$(find ${OUTPUT_DIR} -name "*.apk" ! -name "APKINDEX*" | head -1)
108+
if [ -z "${APK_FILE}" ]; then
109+
echo "ERROR: No APK file found in build output"
110+
ls -laR ${OUTPUT_DIR} || true
111+
exit 1
112+
fi
113+
114+
# Copy to output with architecture in filename
115+
NEW_NAME="${NAME}-${PKGVER}-r0.${PACKAGE_ARCH}.apk"
116+
mkdir -p ${OUTPUT}
117+
cp ${APK_FILE} ${OUTPUT}/${NEW_NAME}
118+
119+
# Create sha512 checksum
120+
cd ${OUTPUT}
121+
sha512sum "${NEW_NAME}" > "${NEW_NAME}.sha512"
122+
123+
echo "APK v3 package created: ${OUTPUT}/${NEW_NAME}"

packaging/test/alpine/entrypoint.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,27 @@ validate_agent_installation() {
5151
#### MAIN ####
5252
##############
5353
if [ "${TYPE}" = "release-github" ] ; then
54-
PACKAGE=apm-agent-php_${VERSION}_all.apk
54+
if apk --version 2>&1 | grep -q "apk-tools 3"; then
55+
echo "Detected apk-tools v3, downloading APK v3 format package"
56+
APKARCH=$(apk --print-arch)
57+
PKGVER=$(echo ${VERSION} | tr '-' '_')
58+
PACKAGE=apm-agent-php-${PKGVER}-r0.${APKARCH}.apk
59+
else
60+
echo "Detected apk-tools v2, downloading APK v2 format package"
61+
PACKAGE=apm-agent-php_${VERSION}_all.apk
62+
fi
5563
download "${PACKAGE}" "${BUILD_RELEASES_FOLDER}" "${GITHUB_RELEASES_URL}/v${VERSION}"
5664
apk add --allow-untrusted --verbose --no-cache "${BUILD_RELEASES_FOLDER}/${PACKAGE}"
5765
else
5866
ls -l $BUILD_PACKAGES
59-
## Install apk package and configure the agent accordingly
60-
apk add --allow-untrusted --verbose --no-cache $BUILD_PACKAGES/*.apk
67+
## Install apk package - select v2 or v3 format based on apk-tools version
68+
if apk --version 2>&1 | grep -q "apk-tools 3"; then
69+
echo "Detected apk-tools v3, installing APK v3 format package"
70+
apk add --allow-untrusted --verbose --no-cache $BUILD_PACKAGES/apm-agent-php-*-r0.*.apk
71+
else
72+
echo "Detected apk-tools v2, installing APK v2 format package"
73+
apk add --allow-untrusted --verbose --no-cache $BUILD_PACKAGES/apm-agent-php_*.apk
74+
fi
6175
fi
6276

6377
validate_if_agent_is_enabled

0 commit comments

Comments
 (0)