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

Support APK with FPM #92

Merged
merged 17 commits into from
Aug 19, 2020
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
25 changes: 16 additions & 9 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pipeline {
name 'PHP_VERSION'
values '7.2', '7.3', '7.4'
}
axis {
name 'DOCKERFILE'
values 'Dockerfile', 'Dockerfile.alpine'
}
}
stages {
stage('Build') {
Expand All @@ -64,9 +68,9 @@ pipeline {
// When running in the CI with multiple parallel stages
// the access could be considered as a DDOS attack.
retryWithSleep(retries: 3, seconds: 5, backoff: true) {
sh script: "PHP_VERSION=${PHP_VERSION} make -f .ci/Makefile prepare", label: 'prepare docker image'
sh script: "PHP_VERSION=${PHP_VERSION} DOCKERFILE=${DOCKERFILE} make -f .ci/Makefile prepare", label: 'prepare docker image'
}
sh script: "PHP_VERSION=${PHP_VERSION} make -f .ci/Makefile build", label: 'build'
sh script: "PHP_VERSION=${PHP_VERSION} DOCKERFILE=${DOCKERFILE} make -f .ci/Makefile build", label: 'build'
}
}
}
Expand All @@ -75,7 +79,7 @@ pipeline {
steps {
withGithubNotify(context: "PHPT-Tests-${PHP_VERSION}", tab: 'tests') {
dir("${BASE_DIR}"){
sh script: "PHP_VERSION=${PHP_VERSION} make -f .ci/Makefile test", label: 'test'
sh script: "PHP_VERSION=${PHP_VERSION} DOCKERFILE=${DOCKERFILE} make -f .ci/Makefile test", label: 'test'
}
}
}
Expand All @@ -89,8 +93,8 @@ pipeline {
steps {
withGithubNotify(context: "Generate-For-Package-${PHP_VERSION}") {
dir("${BASE_DIR}"){
sh script: "PHP_VERSION=${PHP_VERSION} make -f .ci/Makefile generate-for-package", label: 'generate-for-package'
stash includes: 'src/ext/modules/*.so', name: "generate-for-package-${PHP_VERSION}"
sh script: "PHP_VERSION=${PHP_VERSION} DOCKERFILE=${DOCKERFILE} make -f .ci/Makefile generate-for-package", label: 'generate-for-package'
stash includes: 'src/ext/modules/*.so', name: "generate-for-package-${PHP_VERSION}-${DOCKERFILE}"
}
}
}
Expand All @@ -99,7 +103,7 @@ pipeline {
steps {
withGithubNotify(context: "PHPUnit-Tests-${PHP_VERSION}", tab: 'tests') {
dir("${BASE_DIR}"){
sh script: "PHP_VERSION=${PHP_VERSION} make -f .ci/Makefile composer", label: 'composer'
sh script: "PHP_VERSION=${PHP_VERSION} DOCKERFILE=${DOCKERFILE} make -f .ci/Makefile composer", label: 'composer'
}
}
}
Expand All @@ -118,9 +122,12 @@ pipeline {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
unstash 'generate-for-package-7.2'
unstash 'generate-for-package-7.3'
unstash 'generate-for-package-7.4'
unstash 'generate-for-package-7.2-Dockerfile'
unstash 'generate-for-package-7.3-Dockerfile'
unstash 'generate-for-package-7.4-Dockerfile'
unstash 'generate-for-package-7.2-Dockerfile.alpine'
unstash 'generate-for-package-7.3-Dockerfile.alpine'
unstash 'generate-for-package-7.4-Dockerfile.alpine'
Comment on lines +125 to +130
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the smartest approach but it's native to the CI. So we might need to find a better way in some follow ups

sh script: "make -C packaging package", label: 'package'
sh script: "make -C packaging info", label: 'package info'
stash includes: 'build/packages/*', name: 'package'
Expand Down
17 changes: 12 additions & 5 deletions .ci/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
IMAGE := test-php
PHP_VERSION ?= 7.2
DOCKERFILE ?= Dockerfile
SUFFIX :=
ifeq ($(DOCKERFILE), Dockerfile.alpine)
## This is only required to tag the docker images used for building/testing this project
SUFFIX := -alpine
endif

CURRENT_UID := $(shell id -u)
CURRENT_GID := $(shell id -g)
Expand All @@ -13,39 +19,40 @@ help: ## Display this help text
prepare: ## Build docker image for building and testing the project
docker build \
--build-arg PHP_VERSION=${PHP_VERSION} \
--tag $(IMAGE):${PHP_VERSION} .
--tag $(IMAGE):${PHP_VERSION}$(SUFFIX) \
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
-f ${DOCKERFILE} .

.PHONY: build
build: prepare ## Build the project
# docker as the current user
docker run --rm -t \
-u $(CURRENT_UID):$(CURRENT_GID) \
-v $(PWD):/app \
$(IMAGE):${PHP_VERSION}
$(IMAGE):${PHP_VERSION}$(SUFFIX)

.PHONY: test
test: prepare ## Test the project
# docker as the current user
docker run --rm -t \
-u $(CURRENT_UID):$(CURRENT_GID) \
-v $(PWD):/app \
$(IMAGE):${PHP_VERSION} \
$(IMAGE):${PHP_VERSION}$(SUFFIX) \
make test

.PHONY: generate-for-package
generate-for-package: prepare ## Generate the agent extension for the package
# docker as root to install the extension
docker run --rm -t \
-v $(PWD):/app \
$(IMAGE):${PHP_VERSION} \
$(IMAGE):${PHP_VERSION}$(SUFFIX) \
/app/.ci/generate-for-package.sh

.PHONY: composer
composer: prepare ## Run composer
# docker as root to install the extension
docker run -t --rm \
-v $(PWD):/app \
$(IMAGE):${PHP_VERSION} \
$(IMAGE):${PHP_VERSION}$(SUFFIX) \
sh -c 'make install \
&& echo 'extension=elastic_apm.so' > /usr/local/etc/php/php.ini \
&& echo 'elastic_apm.bootstrap_php_part_file=/app/src/bootstrap_php_part.php' >> /usr/local/etc/php/php.ini \
Expand Down
17 changes: 9 additions & 8 deletions .ci/generate-for-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ HYPHEN="-"
MODULES_DIR=/app/src/ext/modules
NAME=elastic_apm
## Prepare context where to copy the previous generated so files
GENERATED=$(mktemp -d /tmp/dir-XXXX)
SEARCH="${MODULES_DIR}/*${HYPHEN}*.so"
if ls "${SEARCH}" 1> /dev/null 2>&1; then
cp -f "${SEARCH}" "${GENERATED}" || true
GENERATED_DIR=$(mktemp -d /tmp/dirXXXXXX)
find "${MODULES_DIR}" -name "*${HYPHEN}*.so" -exec cp {} ${GENERATED_DIR} \;
ls -l ${GENERATED_DIR}

## If alpine then add another suffix
if grep -q -i alpine /etc/os-release; then
SUFFIX=${HYPHEN}alpine
fi

## Generate so file
Expand All @@ -20,12 +23,10 @@ make
PHP_API=$(php -i | grep -i 'PHP API' | sed -e 's#.* =>##g' | awk '{print $1}')

## Rename so file with the PHP api
mv "${MODULES_DIR}/${NAME}.so" "${MODULES_DIR}/${NAME}${HYPHEN}${PHP_API}.so"
mv "${MODULES_DIR}/${NAME}.so" "${MODULES_DIR}/${NAME}${HYPHEN}${PHP_API}${SUFFIX}.so"

## Remove la files
find "${MODULES_DIR}" -name "*.la" -print0 | xargs -0 rm -f

## Restore previous so files.
if ls "${SEARCH}" 1> /dev/null 2>&1; then
cp -f "${SEARCH}" ${MODULES_DIR}/
fi
find "${GENERATED_DIR}" -name "*${HYPHEN}*.so" -exec cp -f {} ${MODULES_DIR}/ \;
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ phpunit.xml
html_docs

# Packaging artifacts
build/packages
build/
8 changes: 7 additions & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ PHP_VERSION=7.2 make -f .ci/Makefile generate-for-package
## To install with composer
PHP_VERSION=7.2 make -f .ci/Makefile composer

## To generate the agent extension with the existing PHP API for alpine
PHP_VERSION=7.2 DOCKERFILE=Dockerfile.alpine make -f .ci/Makefile generate-for-package

## Help goal will provide further details
make -f .ci/Makefile help
```

_NOTE_: `PHP_VERSION` can be set to a different PHP version.
_NOTE_:

* `PHP_VERSION` can be set to a different PHP version.
* Alpine specific binaries can be generated if using `DOCKERFILE=Dockerfile.alpine`

To generate the packages then you can use the `packaging/Dockerfile`, see the below commands:

Expand Down
30 changes: 30 additions & 0 deletions Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ARG PHP_VERSION=7.2
FROM php:${PHP_VERSION}-fpm-alpine

RUN apk update \
&& apk add \
autoconf \
bash \
build-base \
curl \
curl-dev \
git \
procps \
unzip

RUN php -r "copy('https://raw.githubusercontent.com/composer/getcomposer.org/baecae060ee7602a9908f2259f7460b737839972/web/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('sha384', 'composer-setup.php') === '572cb359b56ad9ae52f9c23d29d4b19a040af10d6635642e646a7caa7b96de717ce683bd797a92ce99e5929cc51e7d5f') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php --install-dir=/usr/bin --filename=composer --version=1.10.10 \
&& php -r "unlink('composer-setup.php');"

WORKDIR /app/src/ext

ENV REPORT_EXIT_STATUS=1
ENV TEST_PHP_DETAILED=1
ENV NO_INTERACTION=1
ENV TEST_PHP_JUNIT=/app/junit.xml

CMD phpize \
&& ./configure --enable-elastic_apm \
&& make clean \
&& make
13 changes: 10 additions & 3 deletions packaging/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
FROM ruby:2.7.1-alpine3.12

ENV FPM_VERSION 1.11.0
RUN apk add --no-cache \
alpine-sdk make cpio curl libarchive-tools make php-pear \
python3 py3-virtualenv py3-setuptools py3-pip \
rpm unzip xz git tar dpkg \
&& ln -sf python3 /usr/bin/python \
&& gem install --no-document fpm
&& gem install --no-document fpm -v ${FPM_VERSION}

ENTRYPOINT ["fpm"]
WORKDIR /src
## Fix fpm issue, see https://github.com/jordansissel/fpm/issues/1227
ADD fpm_apm.patch /tmp
RUN (cd /usr/local/bundle/gems/fpm-${FPM_VERSION}/ ; patch -p 1 < /tmp/fpm_apm.patch ) \
&& rm -f /tmp/fpm_apk.patch

COPY create-package.sh /bin
WORKDIR /src
ENTRYPOINT ["/bin/create-package.sh"]
95 changes: 49 additions & 46 deletions packaging/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ VERSION?=$(shell grep 'VERSION' ../src/ElasticApm/ElasticApm.php | cut -d= -f2 |
OUTPUT:=build/packages
PHP_AGENT_DIR:=/opt/elastic/apm-agent-php
PHP_VERSION?=7.2
GIT_SHA ?= $(shell git rev-parse HEAD || echo "unknown")
GIT_SHA?=$(shell git rev-parse HEAD || echo "unknown")

export FPM_FLAGS=

.PHONY: help
.DEFAULT_GOAL := help
Expand All @@ -20,41 +22,29 @@ prepare: ## Build docker image for the packaging
@docker build -t $(IMAGE) .

create-%: prepare ## Create the specific package
@echo 'Creating package ...'
@echo "Creating package $* ..."
@mkdir -p $(PWD)/$(OUTPUT)
@docker run --rm \
-v $(PWD):/app \
-w /app $(IMAGE) \
--input-type dir \
--output-type $* \
--name $(NAME) \
--version $(VERSION) \
--architecture all \
--url 'https://github.com/elastic/apm-agent-php' \
--maintainer 'APM Team <[email protected]>' \
--license 'ASL 2.0' \
--vendor 'Elasticsearch, Inc.' \
--description "PHP agent for Elastic APM\nGit Commit: ${GIT_SHA}" \
--package $(OUTPUT) \
--chdir /app \
--after-install=packaging/post-install.sh \
packaging/post-install.sh=$(PHP_AGENT_DIR)/bin/post-install.sh \
src/ext/modules/=$(PHP_AGENT_DIR)/extensions \
README.md=$(PHP_AGENT_DIR)/docs/README.md \
src/ElasticApm=$(PHP_AGENT_DIR)/src \
src/bootstrap_php_part.php=$(PHP_AGENT_DIR)/src/bootstrap_php_part.php
@echo 'Creating sha512sum ...'
@BINARY=$$(ls -1 $(PWD)/$(OUTPUT)/*.$*) ;\
sha512sum $$BINARY > $$BINARY.sha512 ;\
sed -i.bck "s#$(PWD)/$(OUTPUT)/##g" $$BINARY.sha512 ;\
rm $(PWD)/$(OUTPUT)/*.bck

.PHONY: rpm
rpm: create-rpm ## Create the rpm installer
-e TYPE=$* \
-e NAME=$(NAME) \
-e VERSION=$(VERSION) \
-e OUTPUT=$(OUTPUT) \
-e FPM_FLAGS=${FPM_FLAGS} \
-e PHP_AGENT_DIR=${PHP_AGENT_DIR} \
-w /app $(IMAGE)

.PHONY: apk
apk: FPM_FLAGS="--depends=bash"
apk: create-apk ## Create the apk installer

.PHONY: deb
deb: create-deb ## Create the deb installer

.PHONY: rpm
rpm: create-rpm ## Create the rpm installer
## TODO: fpm replaces - with _ in the version

.PHONY: tar
tar: create-tar ## Create the tar.gz

Expand All @@ -63,17 +53,16 @@ version: ## Show the fpm version
@docker run --rm $(IMAGE) --version

.PHONY: package
package: rpm deb tar ## Create all the installers
package: apk deb rpm tar ## Create all the installers

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

.PHONY: rpm-info
rpm-info: ## Show the rpm package metadata
.PHONY: apk-info
apk-info: ## Show the apk package metadata
@cd $(PWD) ;\
BINARY=$$(ls -1 $(OUTPUT)/*.rpm) ;\
docker run --rm -v $(PWD):/app -w /app --entrypoint /usr/bin/rpm $(IMAGE) -qip $$BINARY ;\
docker run --rm -v $(PWD):/app -w /app --entrypoint /usr/bin/rpm $(IMAGE) -qlp $$BINARY
BINARY=$$(ls -1 $(OUTPUT)/*.apk) ;\
docker run --rm -v $(PWD):/app -w /app --entrypoint /sbin/apk $(IMAGE) manifest $$BINARY

.PHONY: deb-info
deb-info: ## Show the deb package metadata
Expand All @@ -82,32 +71,46 @@ deb-info: ## Show the deb package metadata
docker run --rm -v $(PWD):/app -w /app --entrypoint /usr/bin/dpkg $(IMAGE) --info $$BINARY ;\
docker run --rm -v $(PWD):/app -w /app --entrypoint /usr/bin/dpkg $(IMAGE) -c $$BINARY

.PHONY: rpm-info
rpm-info: ## Show the rpm package metadata
@cd $(PWD) ;\
BINARY=$$(ls -1 $(OUTPUT)/*.rpm) ;\
docker run --rm -v $(PWD):/app -w /app --entrypoint /usr/bin/rpm $(IMAGE) -qip $$BINARY ;\
docker run --rm -v $(PWD):/app -w /app --entrypoint /usr/bin/rpm $(IMAGE) -qlp $$BINARY

.PHONY: tar-info
tar-info: ## Show the tar package metadata
@cd $(PWD) ;\
BINARY=$$(ls -1 $(OUTPUT)/*.tar) ;\
docker run --rm -v $(PWD):/app -w /app --entrypoint /usr/bin/tar $(IMAGE) -tvf $$BINARY

.PHONY: rpm-install
rpm-install: ## Install the rpm installer to run some smoke tests
@cd $(PWD)/packaging/test/centos ;\
docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t rpm-install . ;\
.PHONY: apk-install
apk-install: ## Install the apk installer to run some smoke tests
@cd $(PWD)/packaging/test/alpine ;\
docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t $@ . ;\
cd -
docker run --rm -v $(PWD):/src -w /src -e TYPE=rpm rpm-install
docker run --rm -v $(PWD):/src -w /src $@

.PHONY: deb-install
deb-install: ## Install the deb installer to run some smoke tests
@cd $(PWD)/packaging/test/ubuntu ;\
docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t deb-install . ;\
docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t $@ . ;\
cd -
@docker run --rm -v $(PWD):/src -w /src -e TYPE=deb deb-install
@docker run --rm -v $(PWD):/src -w /src -e TYPE=deb $@

.PHONY: tar-install
tar-install: ## Install the tar installer to run some smoke tests
@cd $(PWD)/packaging/test/ubuntu ;\
docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t tar-install . ;\
docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t $@ . ;\
cd -
@docker run --rm -v $(PWD):/src -w /src -e TYPE=tar $@

.PHONY: rpm-install
rpm-install: ## Install the rpm installer to run some smoke tests
@cd $(PWD)/packaging/test/centos ;\
docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t $@ . ;\
cd -
docker run --rm -v $(PWD):/src -w /src -e TYPE=tar tar-install
@docker run --rm -v $(PWD):/src -w /src -e TYPE=rpm $@

.PHONY: install
install: deb-install rpm-install tar-install ## Install all the distributions
install: apk-install deb-install rpm-install tar-install ## Install all the distributions
Loading