Skip to content

Commit ca69257

Browse files
committed
refactor vm.sh build/start/stop actions
1 parent cd539c3 commit ca69257

3 files changed

Lines changed: 87 additions & 64 deletions

File tree

tests/ci/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ARG UBUNTU_VERSION=focal
33
FROM ubuntu:${UBUNTU_VERSION}
44

55
ARG PHP_VERSION=default
6+
ARG UBUNTU_VERSION=focal
67

78
COPY setup/*.sh /root/setup/
89
COPY config/* /root/config/
@@ -18,7 +19,9 @@ RUN if [ ! -f /.dockerenv ]; then touch /.dockerenv; fi && \
1819
./setup_apache.sh && \
1920
./setup_privoxy.sh && \
2021
./setup_php.sh "${PHP_VERSION}" && \
21-
./setup_composer.sh
22+
./setup_composer.sh && \
23+
echo "PHP_VERSION=${PHP_VERSION}" > /etc/build-info && \
24+
echo "UBUNTU_VERSION=${UBUNTU_VERSION}" >> /etc/build-info
2225

2326
COPY docker/entrypoint.sh /root/entrypoint.sh
2427
RUN chmod 755 /root/entrypoint.sh

tests/ci/docker/entrypoint.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ USERNAME="${1:-docker}"
44

55
echo "[$(date)] Bootstrapping the Test container..."
66

7-
UBUNTU_VERSION="$(fgrep DISTRIB_CODENAME /etc/lsb-release | sed 's/DISTRIB_CODENAME=//')"
8-
PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')"
7+
# load values for UBUNTU_VERSION, PHP_VERSION
8+
. /etc/build-info
9+
# NB: the following line does not account for 'default'
10+
#PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')"
11+
#UBUNTU_VERSION="$(fgrep DISTRIB_CODENAME /etc/lsb-release | sed 's/DISTRIB_CODENAME=//')"
912
BOOTSTRAP_OK_FILE="${TESTS_ROOT_DIR}/tests/ci/var/bootstrap_ok_${UBUNTU_VERSION}_${PHP_VERSION}"
1013

1114
if [ -f "${BOOTSTRAP_OK_FILE}" ]; then
@@ -74,8 +77,9 @@ if [ -f "${TESTS_ROOT_DIR}/composer.json" ]; then
7477
echo "[$(date)] Running Composer..."
7578

7679
# @todo if there is a composer.lock file present, there are chances it might be a leftover from when running the
77-
# container using a different php version. We should then back it up / do some symlink magic to make sure that
78-
# it matches the current php version and a hash of composer.json...
80+
# container using a different os/php version. We should then back it up / do some symlink magic to make sure that
81+
# it matches the current php version and a hash of composer.json... (also symlink the vendor folder).
82+
# Make it at least optional to run composer at container start
7983
su "${USERNAME}" -c "cd ${TESTS_ROOT_DIR} && composer install"
8084
else
8185
# @todo should we exit?

tests/ci/vm.sh

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ Environment variables:
5656
to be set before the 'build' action
5757
PHP_VERSION default value: 'default', ie. the stock php version from the Ubuntu version in use. Other possible values: 5.6, 7.0 .. 7.4, 8.0 .. 8.4
5858
UBUNTU_VERSION default value: jammy. Other possible values: xenial, bionic, focal, noble
59-
default value: 8080. Set to 'no' not to publish the container's proxy http port to the host
60-
can also be set before the 'runtests' and 'runcoverage' actions:
59+
to be set before the 'start' action
60+
HOST_HTTPPORT default value: 80. Set to 'no' not to publish the container's http port to the host
61+
HOST_HTTPSPORT default value: 443. Set to 'no' not to publish the container's https port to the host
62+
HOST_PROXYPORT default value: 8080. Set to 'no' not to publish the container's proxy http port to the host
63+
to be set before the 'runtests' and 'runcoverage' actions:
6164
HTTPSVERIFYHOST 0, 1 or 2. Default and recommended: 0
6265
HTTPSIGNOREPEER 0 or 1. Default and recommended: 1
6366
SSLVERSION 0 (auto), 2 (SSLv2) to 7 (tls 1.3). Default: 0
64-
can be used only for 'runtests' and 'runcoverage' actions:
6567
DEBUG
6668
"
6769
}
@@ -77,76 +79,91 @@ wait_for_bootstrap() {
7779
sleep 1
7880
I=$((I+1))
7981
done
80-
if [ $I -eq 60 ]; then
82+
if [ $I -gt 60 ]; then
83+
echo ''
8184
echo "ERROR: Container did not finish bootstrapping within 60 seconds..." >&2
8285
return 1
8386
fi
8487
return 0
8588
}
8689

8790
build() {
88-
stop
89-
docker build --build-arg PHP_VERSION --build-arg UBUNTU_VERSION -t "${IMAGE_NAME}" .
90-
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
91-
docker rm "${CONTAINER_NAME}"
92-
fi
93-
PORTMAPPING=''
94-
# @todo improve error message and abort in case any port is not an integer or negative
95-
if [ "$HOST_HTTPPORT" != no ] && [ "$HOST_HTTPPORT" != '' ]; then
96-
PORTMAPPING="-p $((HOST_HTTPPORT-0)):80 "
97-
fi
98-
if [ "$HOST_HTTPSPORT" != no ] && [ "$HOST_HTTPSPORT" != '' ]; then
99-
PORTMAPPING="${PORTMAPPING}-p $((HOST_HTTPSPORT)):443 "
100-
fi
101-
if [ "$HOST_PROXYPORT" != no ] && [ "$HOST_PROXYPORT" != '' ]; then
102-
PORTMAPPING="-p $((HOST_PROXYPORT-0)):8080 "
103-
fi
104-
if docker run -d \
105-
$PORTMAPPING \
106-
--name "${CONTAINER_NAME}" \
107-
--env "CONTAINER_USER_UID=$(id -u)" --env "CONTAINER_USER_GID=$(id -g)" \
108-
--env "TESTS_ROOT_DIR=${CONTAINER_WORKSPACE_DIR}" \
109-
--env HTTPSERVER=localhost \
110-
--env HTTPURI=/tests/index.php?demo=server/server.php \
111-
--env HTTPSSERVER=localhost \
112-
--env HTTPSURI=/tests/index.php?demo=server/server.php \
113-
--env PROXYSERVER=localhost:8080 \
114-
--env "HTTPSVERIFYHOST=${HTTPSVERIFYHOST}" \
115-
--env "HTTPSIGNOREPEER=${HTTPSIGNOREPEER}" \
116-
--env "SSLVERSION=${SSLVERSION}" \
117-
--env DEBUG="${DEBUG}" \
118-
-v "${ROOT_DIR}":"${CONTAINER_WORKSPACE_DIR}" \
119-
"${IMAGE_NAME}"; then
120-
wait_for_bootstrap
91+
if docker build --build-arg PHP_VERSION --build-arg UBUNTU_VERSION -t "${IMAGE_NAME}" .; then
92+
if [ "$1" = '-r' ]; then
93+
# stop and remove existing containers built from a previous version of this image
94+
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
95+
stop
96+
docker rm "${CONTAINER_NAME}"
97+
fi
98+
fi
12199
fi
122100
}
123101

124102
start() {
125-
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
126-
docker start "${CONTAINER_NAME}"
127-
if [ $? -eq 0 ]; then
128-
wait_for_bootstrap
129-
fi
103+
if [ "$(docker inspect --format '{{.State.Status}}' ${CONTAINER_NAME} 2>/dev/null)" = running ]; then
104+
# @todo we should check that the env vars have not changed, and give a warning if so
105+
echo "${CONTAINER_NAME} already started..."
130106
else
131-
build
107+
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
108+
echo "starting existing container ${CONTAINER_NAME}..."
109+
# @todo we should check that the env vars have not changed, and give a warning if so. Doable using `inspect`...
110+
if docker start "${CONTAINER_NAME}"; then
111+
wait_for_bootstrap
112+
fi
113+
else
114+
build
115+
116+
PORTMAPPING=''
117+
# @todo improve error message and abort in case any port is not an integer or negative
118+
if [ "$HOST_HTTPPORT" != no ] && [ "$HOST_HTTPPORT" != '' ]; then
119+
PORTMAPPING="-p $((HOST_HTTPPORT-0)):80 "
120+
fi
121+
if [ "$HOST_HTTPSPORT" != no ] && [ "$HOST_HTTPSPORT" != '' ]; then
122+
PORTMAPPING="${PORTMAPPING}-p $((HOST_HTTPSPORT)):443 "
123+
fi
124+
if [ "$HOST_PROXYPORT" != no ] && [ "$HOST_PROXYPORT" != '' ]; then
125+
PORTMAPPING="-p $((HOST_PROXYPORT-0)):8080 "
126+
fi
127+
if docker run -d \
128+
$PORTMAPPING \
129+
--name "${CONTAINER_NAME}" \
130+
--env "CONTAINER_USER_UID=$(id -u)" --env "CONTAINER_USER_GID=$(id -g)" \
131+
--env "TESTS_ROOT_DIR=${CONTAINER_WORKSPACE_DIR}" \
132+
--env HTTPSERVER=localhost \
133+
--env HTTPURI=/tests/index.php?demo=server/server.php \
134+
--env HTTPSSERVER=localhost \
135+
--env HTTPSURI=/tests/index.php?demo=server/server.php \
136+
--env PROXYSERVER=localhost:8080 \
137+
-v "${ROOT_DIR}":"${CONTAINER_WORKSPACE_DIR}" \
138+
"${IMAGE_NAME}"; then
139+
wait_for_bootstrap
140+
fi
141+
fi
132142
fi
133143
}
134144

135145
stop() {
136-
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
137-
docker stop "${CONTAINER_NAME}"
146+
if [ "$(docker inspect --format '{{.State.Status}}' ${CONTAINER_NAME} 2>/dev/null)" = exited ]; then
147+
echo "${CONTAINER_NAME} already stopped"
148+
else
149+
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
150+
echo "stopping ${CONTAINER_NAME}..."
151+
docker stop "${CONTAINER_NAME}"
152+
fi
138153
fi
139154
}
140155

141156
case "${ACTION}" in
142157

143158
build)
144-
build
145-
stop
159+
build -r
146160
;;
147161

148162
cleanup)
149-
docker rm "${CONTAINER_NAME}"
163+
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
164+
docker stop "${CONTAINER_NAME}"
165+
docker rm "${CONTAINER_NAME}"
166+
fi
150167
docker rmi "${IMAGE_NAME}"
151168
;;
152169

@@ -164,13 +181,13 @@ case "${ACTION}" in
164181
shift
165182
test -t 1 && USE_TTY="-t"
166183
docker exec -i $USE_TTY \
167-
--env "HTTPSVERIFYHOST=${HTTPSVERIFYHOST}" \
168-
--env "HTTPSIGNOREPEER=${HTTPSIGNOREPEER}" \
169-
--env "SSLVERSION=${SSLVERSION}" \
170-
--env DEBUG="${DEBUG}" \
171-
"${CONTAINER_NAME}" su "${CONTAINER_USER}" -c '"$0" "$@"' -- "$@"
172-
# q: which one is better? test with a command with spaces in options values, and with a composite command such as cd here && do that
173-
#"${CONTAINER_NAME}" sudo -iu "${CONTAINER_USER}" -- "$@"
184+
--env "HTTPSVERIFYHOST=${HTTPSVERIFYHOST}" \
185+
--env "HTTPSIGNOREPEER=${HTTPSIGNOREPEER}" \
186+
--env "SSLVERSION=${SSLVERSION}" \
187+
--env DEBUG="${DEBUG}" \
188+
"${CONTAINER_NAME}" su "${CONTAINER_USER}" -c '"$0" "$@"' -- "$@"
189+
# @todo which one is better? test with a command with spaces in options values, and with a composite command such as cd here && do that
190+
#"${CONTAINER_NAME}" sudo -iu "${CONTAINER_USER}" -- "$@"
174191
;;
175192

176193
restart)
@@ -181,6 +198,7 @@ case "${ACTION}" in
181198
runcoverage)
182199
test -t 1 && USE_TTY="-t"
183200
# @todo clean up /tmp/phpxmlrpc and .phpunit.result.cache
201+
# @todo run composer install if it was not yet run or we are asked to
184202
if [ ! -d build ]; then mkdir build; fi
185203
docker exec -t "${CONTAINER_NAME}" "${CONTAINER_WORKSPACE_DIR}/tests/ci/setup/setup_code_coverage.sh" enable
186204
docker exec -i $USE_TTY \
@@ -194,6 +212,8 @@ case "${ACTION}" in
194212

195213
runtests)
196214
test -t 1 && USE_TTY="-t"
215+
# @todo clean up /tmp/phpxmlrpc and .phpunit.result.cache
216+
# @todo run composer install if it was not yet run or we are asked to
197217
docker exec -i $USE_TTY \
198218
--env "HTTPSVERIFYHOST=${HTTPSVERIFYHOST}" \
199219
--env "HTTPSIGNOREPEER=${HTTPSIGNOREPEER}" \
@@ -212,10 +232,6 @@ case "${ACTION}" in
212232
start
213233
;;
214234

215-
#status)
216-
# :
217-
# ;;
218-
219235
stop)
220236
stop
221237
;;

0 commit comments

Comments
 (0)