comments
[plcapi.git] / tests / ci / docker / entrypoint.sh
1 #!/bin/sh
2
3 USERNAME="${1:-docker}"
4
5 echo "[$(date)] Bootstrapping the Test container..."
6
7 clean_up() {
8     # Perform program exit housekeeping
9
10     echo "[$(date)] Stopping the Web server"
11     service apache2 stop
12
13     echo "[$(date)] Stopping Privoxy"
14     service privoxy stop
15
16     echo "[$(date)] Stopping FPM"
17     service php-fpm stop
18
19     echo "[$(date)] Exiting"
20     exit
21 }
22
23 # Fix UID & GID for user
24
25 echo "[$(date)] Fixing filesystem permissions..."
26
27 ORIGPASSWD=$(cat /etc/passwd | grep "^${USERNAME}:")
28 ORIG_UID=$(echo "$ORIGPASSWD" | cut -f3 -d:)
29 ORIG_GID=$(echo "$ORIGPASSWD" | cut -f4 -d:)
30 CONTAINER_USER_HOME=$(echo "$ORIGPASSWD" | cut -f6 -d:)
31 CONTAINER_USER_UID=${CONTAINER_USER_UID:=$ORIG_UID}
32 CONTAINER_USER_GID=${CONTAINER_USER_GID:=$ORIG_GID}
33
34 if [ "$CONTAINER_USER_UID" != "$ORIG_UID" -o "$CONTAINER_USER_GID" != "$ORIG_GID" ]; then
35     groupmod -g "$CONTAINER_USER_GID" "${USERNAME}"
36     usermod -u "$CONTAINER_USER_UID" -g "$CONTAINER_USER_GID" "${USERNAME}"
37 fi
38 if [ $(stat -c '%u' "${CONTAINER_USER_HOME}") != "${CONTAINER_USER_UID}" -o $(stat -c '%g' "${CONTAINER_USER_HOME}") != "${CONTAINER_USER_GID}" ]; then
39     chown "${CONTAINER_USER_UID}":"${CONTAINER_USER_GID}" "${CONTAINER_USER_HOME}"
40     chown -R "${CONTAINER_USER_UID}":"${CONTAINER_USER_GID}" "${CONTAINER_USER_HOME}"/.*
41 fi
42 # @todo do the same chmod for ${TESTS_ROOT_DIR}, if it's not within CONTAINER_USER_HOME
43
44 echo "[$(date)] Fixing Apache configuration..."
45
46 sed -e "s?^export TESTS_ROOT_DIR=.*?export TESTS_ROOT_DIR=${TESTS_ROOT_DIR}?g" --in-place /etc/apache2/envvars
47 sed -e "s?^export APACHE_RUN_USER=.*?export APACHE_RUN_USER=${USERNAME}?g" --in-place /etc/apache2/envvars
48 sed -e "s?^export APACHE_RUN_GROUP=.*?export APACHE_RUN_GROUP=${USERNAME}?g" --in-place /etc/apache2/envvars
49
50 echo "[$(date)] Fixing FPM configuration..."
51
52 FPMCONF="/etc/php/$(php -r 'echo implode(".",array_slice(explode(".",PHP_VERSION),0,2));' 2>/dev/null)/fpm/pool.d/www.conf"
53 sed -e "s?^user =.*?user = ${USERNAME}?g" --in-place "${FPMCONF}"
54 sed -e "s?^group =.*?group = ${USERNAME}?g" --in-place "${FPMCONF}"
55 sed -e "s?^listen.owner =.*?listen.owner = ${USERNAME}?g" --in-place "${FPMCONF}"
56 sed -e "s?^listen.group =.*?listen.group = ${USERNAME}?g" --in-place "${FPMCONF}"
57
58 echo "[$(date)] Running Composer..."
59
60 # @todo if there is a composer.lock file present, there are chances it might be a leftover from when running the
61 #       container using a different php version. We should then back it up / do some symlink magic to make sure that
62 #       it matches the current php version and a hash of composer.json...
63 sudo "${USERNAME}" -c "cd ${TESTS_ROOT_DIR} && composer install"
64
65 trap clean_up TERM
66
67 echo "[$(date)] Starting FPM..."
68 service php-fpm start
69
70 echo "[$(date)] Starting the Web server..."
71 service apache2 start
72
73 echo "[$(date)] Starting Privoxy..."
74 service privoxy start
75
76 tail -f /dev/null &
77 child=$!
78 wait "$child"