From 485ea659c8b5099f3fc73156ea1d75a993e68f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Mik=C3=B3?= Date: Mon, 2 Dec 2024 14:52:59 +0100 Subject: [PATCH] chore: refactor docker-entrypoint.sh scripts --- .../base/context/rootfs/docker-entrypoint.sh | 216 +++++++++----- .../context/rootfs/docker-entrypoint.sh | 240 +++++++++++----- .../context/rootfs/docker-entrypoint.sh | 237 ++++++++++----- .../context/rootfs/docker-entrypoint.sh | 237 ++++++++++----- .../base/context/rootfs/docker-entrypoint.sh | 263 +++++++++++------ .../context/rootfs/docker-entrypoint.sh | 269 ++++++++++++------ .../context/rootfs/docker-entrypoint.sh | 259 ++++++++++++----- .../context/rootfs/docker-entrypoint.sh | 262 +++++++++++------ .../fpm/context/rootfs/docker-entrypoint.sh | 18 +- 9 files changed, 1391 insertions(+), 610 deletions(-) diff --git a/images/php-fpm-rootless/base/context/rootfs/docker-entrypoint.sh b/images/php-fpm-rootless/base/context/rootfs/docker-entrypoint.sh index 76c1480e..b8ea2635 100755 --- a/images/php-fpm-rootless/base/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm-rootless/base/context/rootfs/docker-entrypoint.sh @@ -4,121 +4,203 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } shopt -s expand_aliases -if [ -f "${HOME}/.bash_alias" ]; then +if [[ -f "${HOME}/.bash_alias" ]]; then source "${HOME}/.bash_alias" fi -# PHP -PHP_PREFIX="$HOME/.local/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" - -mkdir -p "${PHP_PREFIX_LONG}/mods-available" "${PHP_PREFIX_LONG}/cli/conf.d" "${PHP_PREFIX_LONG}/fpm/conf.d" "${PHP_PREFIX_LONG}/fpm/pool.d" +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-$HOME/.local/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + prepare_php_directories + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool + configure_php_xdebug + configure_php_blackfire + configure_php_spx +} + +prepare_php_directories() { + mkdir -p "${PHP_PREFIX_LONG}/mods-available" "${PHP_PREFIX_LONG}/cli/conf.d" "${PHP_PREFIX_LONG}/fpm/conf.d" "${PHP_PREFIX_LONG}/fpm/pool.d" +} + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi -# Configure PHP Global Settings -if [ -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" phpenmod docker -fi +} + +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi -# Configure PHP Opcache -if [ -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" phpenmod opcache -fi +} + +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_php_xdebug() { + if [[ ! -f "${PHP_PREFIX}/mods-available/xdebug.ini.template" ]]; then + return 0 + fi -# Configure PHP XDebug -if [ -f "${PHP_PREFIX}/mods-available/xdebug.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/xdebug.ini.template" >"${PHP_PREFIX_LONG}/mods-available/xdebug.ini" phpenmod xdebug -fi +} + +configure_php_blackfire() { + if [[ ! -f "${PHP_PREFIX}/mods-available/blackfire.ini.template" ]]; then + return 0 + fi -# Configure PHP Blackfire -if [ -f "${PHP_PREFIX}/mods-available/blackfire.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/blackfire.ini.template" >"${PHP_PREFIX_LONG}/mods-available/blackfire.ini" phpenmod blackfire -fi +} + +configure_php_spx() { + if [[ ! -f "${PHP_PREFIX}/mods-available/spx.ini.template" ]]; then + return 0 + fi -# Configure PHP SPX -if [ -f "${PHP_PREFIX}/mods-available/spx.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/spx.ini.template" >"${PHP_PREFIX_LONG}/mods-available/spx.ini" phpenmod spx -fi +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem update-ca-certificates -fi +} + +configure_msmtp() { + if [[ ! -f "${HOME}/msmtprc.template" ]]; then + return 0 + fi -if [ -f "${HOME}/msmtprc.template" ]; then gomplate <"${HOME}/msmtprc.template" >"${HOME}/.msmtprc" chmod 600 "${HOME}/.msmtprc" -fi +} -# Start Cron -cron - -# start socat process in background to connect sockets used for agent access within container environment -# shellcheck disable=SC2039 -if [ -S /run/host-services/ssh-auth.sock ] && [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ]; then - bash -c "nohup socat UNIX-CLIENT:/run/host-services/ssh-auth.sock \ - UNIX-LISTEN:${SSH_AUTH_SOCK},fork,user=www-data,group=www-data 1>/var/log/socat-ssh-auth.log 2>&1 &" -fi +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" composer self-update "${COMPOSER_VERSION:-}" fi -fi +} + +start_socat() { + # start socat process in background to connect sockets used for agent access within container environment + # shellcheck disable=SC2039 + if [[ ! -S /run/host-services/ssh-auth.sock ]] || [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]]; then + return 0 + fi + + bash -c "nohup socat UNIX-CLIENT:/run/host-services/ssh-auth.sock \ + UNIX-LISTEN:${SSH_AUTH_SOCK},fork,user=www-data,group=www-data 1>/var/log/socat-ssh-auth.log 2>&1 &" +} + +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/.composer/vendor/bin:/home/www-data/bin:/home/www-data/.local/bin:/var/www/html/node_modules/.bin:/home/www-data/node_modules/.bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - fi -fi +} -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- php-fpm "$@" -# If the first arg is php-fpm call it normally. -else - set -- "$@" -fi +start_cron() { + if [[ "${CRON_ENABLED:-false}" == "true" ]]; then + cron + fi +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + start_socat + + configure_cron + start_cron + + # If the first arg is `-D` or `--some-option` pass it to php-fpm. + if [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- php-fpm "$@" + # If the first arg is php-fpm call it normally. + else + set -- "$@" + fi -exec "$@" + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 + +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/docker-entrypoint.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/docker-entrypoint.sh index a341ce28..8993e8e4 100755 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/docker-entrypoint.sh @@ -4,127 +4,223 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } shopt -s expand_aliases -if [ -f "${HOME}/.bash_alias" ]; then +if [[ -f "${HOME}/.bash_alias" ]]; then source "${HOME}/.bash_alias" fi -# Supervisor: Fix Permissions -if [ "${FIX_PERMISSIONS:-true}" = "true" ] && [ -f /etc/supervisor/available.d/permission.conf.template ]; then +configure_supervisord() { + configure_supervisord_fix_permissions + configure_supervisord_cron + configure_supervisord_socat + configure_supervisord_nginx + configure_supervisord_php_fpm + configure_supervisord_gotty +} + +configure_supervisord_fix_permissions() { + if [[ "${FIX_PERMISSIONS:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/permission.conf.template ]]; then + return 0 + fi + gomplate /etc/supervisor/conf.d/permission.conf -fi +} + +configure_supervisord_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/cron.conf.template ]]; then + return 0 + fi -# Supervisor: Cron -if [ "${CRON_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/cron.conf.template ]; then gomplate /etc/supervisor/conf.d/cron.conf -fi +} + +configure_supervisord_socat() { + if [[ "${SOCAT_ENABLED:-false}" != "true" ]] || + [[ ! -S /run/host-services/ssh-auth.sock ]] || + [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]] || + [[ ! -f /etc/supervisor/available.d/socat.conf.template ]]; then + return 0 + fi -# Supervisor: Socat -if [ "${SOCAT_ENABLED:-false}" = "true" ] && - [ -S /run/host-services/ssh-auth.sock ] && - [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ] && - [ -f /etc/supervisor/available.d/socat.conf.template ]; then gomplate /etc/supervisor/conf.d/socat.conf -fi +} + +configure_supervisord_nginx() { + if [[ "${NGINX_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/nginx.conf.template ]]; then + return 0 + fi -# Supervisor: Nginx -if [ "${NGINX_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/nginx.conf.template ]; then gomplate /etc/supervisor/conf.d/nginx.conf find /etc/nginx -name '*.template' -exec sh -c 'gomplate <${1} > ${1%.*}' sh {} \; -fi +} + +configure_supervisord_php_fpm() { + if [[ "${PHP_FPM_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/php-fpm.conf.template ]]; then + return 0 + fi -# Supervisor: PHP-FPM -if [ "${PHP_FPM_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/php-fpm.conf.template ]; then gomplate /etc/supervisor/conf.d/php-fpm.conf -fi +} + +configure_supervisord_gotty() { + if [[ "${GOTTY_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/gotty.conf.template ]]; then + return 0 + fi -# Supervisor: Gotty -if [ "${GOTTY_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/gotty.conf.template ]; then gomplate /etc/supervisor/conf.d/gotty.conf -fi +} + +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-$HOME/.local/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + prepare_php_directories + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool +} + +prepare_php_directories() { + mkdir -p "${PHP_PREFIX_LONG}/mods-available" "${PHP_PREFIX_LONG}/cli/conf.d" "${PHP_PREFIX_LONG}/fpm/conf.d" "${PHP_PREFIX_LONG}/fpm/pool.d" +} + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi -# PHP -PHP_PREFIX="/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" + gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" + phpenmod docker +} -# Configure PHP Global Settings -gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" -phpenmod docker +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi -# Configure PHP Opcache -gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" -phpenmod opcache + gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" + phpenmod opcache +} + +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem update-ca-certificates -fi +} + +configure_msmtp() { + if [[ ! -f "${HOME}/msmtprc.template" ]]; then + return 0 + fi -if [ -f "${HOME}/msmtprc.template" ]; then gomplate <"${HOME}/msmtprc.template" >"${HOME}/.msmtprc" chmod 600 "${HOME}/.msmtprc" -fi +} + +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" composer self-update "${COMPOSER_VERSION:-}" fi -fi +} + +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/.composer/vendor/bin:/home/www-data/bin:/home/www-data/.local/bin:/var/www/html/node_modules/.bin:/home/www-data/node_modules/.bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - - else # If CRONJOBS is not set, set default Magento cron + else + # If CRONJOBS is not set, set default Magento cron printf "* * * * * /usr/bin/test ! -e /var/www/html/var/.maintenance.flag -a ! -e /var/www/html/var/.cron-disable && cd /var/www/html && /usr/bin/php /var/www/html/bin/magento cron:run 2>&1 | grep -v 'Ran jobs by schedule' >> /var/www/html/var/log/magento.cron.log\n" | crontab -u www-data - fi -fi +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + configure_cron + + configure_supervisord + + # If the first arg is `-D` or `--some-option` pass it to supervisord. + if [[ $# -eq 0 ]] || [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- supervisord -c /etc/supervisor/supervisord.conf "$@" + # If the first arg is supervisord call it normally. + elif [[ "${1}" == "supervisord" ]]; then + set -- "$@" + # If the first arg is anything else + else + set -- "$@" + fi -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ $# -eq 0 ] || [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- supervisord -c /etc/supervisor/supervisord.conf "$@" -# If the first arg is supervisord call it normally. -elif [ "${1}" = "supervisord" ]; then - set -- "$@" -# If the first arg is anything else -else - set -- "$@" -fi + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 -exec "$@" +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php-fpm-rootless/shopware-web/context/rootfs/docker-entrypoint.sh b/images/php-fpm-rootless/shopware-web/context/rootfs/docker-entrypoint.sh index ee5a3238..125782f8 100755 --- a/images/php-fpm-rootless/shopware-web/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm-rootless/shopware-web/context/rootfs/docker-entrypoint.sh @@ -4,124 +4,219 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } shopt -s expand_aliases -if [ -f "${HOME}/.bash_alias" ]; then +if [[ -f "${HOME}/.bash_alias" ]]; then source "${HOME}/.bash_alias" fi -# Supervisor: Fix Permissions -if [ "${FIX_PERMISSIONS:-true}" = "true" ] && [ -f /etc/supervisor/available.d/permission.conf.template ]; then +configure_supervisord() { + configure_supervisord_fix_permissions + configure_supervisord_cron + configure_supervisord_socat + configure_supervisord_nginx + configure_supervisord_php_fpm + configure_supervisord_gotty +} + +configure_supervisord_fix_permissions() { + if [[ "${FIX_PERMISSIONS:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/permission.conf.template ]]; then + return 0 + fi + gomplate /etc/supervisor/conf.d/permission.conf -fi +} + +configure_supervisord_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/cron.conf.template ]]; then + return 0 + fi -# Supervisor: Cron -if [ "${CRON_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/cron.conf.template ]; then gomplate /etc/supervisor/conf.d/cron.conf -fi +} + +configure_supervisord_socat() { + if [[ "${SOCAT_ENABLED:-false}" != "true" ]] || + [[ ! -S /run/host-services/ssh-auth.sock ]] || + [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]] || + [[ ! -f /etc/supervisor/available.d/socat.conf.template ]]; then + return 0 + fi -# Supervisor: Socat -if [ "${SOCAT_ENABLED:-false}" = "true" ] && - [ -S /run/host-services/ssh-auth.sock ] && - [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ] && - [ -f /etc/supervisor/available.d/socat.conf.template ]; then gomplate /etc/supervisor/conf.d/socat.conf -fi +} + +configure_supervisord_nginx() { + if [[ "${NGINX_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/nginx.conf.template ]]; then + return 0 + fi -# Supervisor: Nginx -if [ "${NGINX_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/nginx.conf.template ]; then gomplate /etc/supervisor/conf.d/nginx.conf find /etc/nginx -name '*.template' -exec sh -c 'gomplate <${1} > ${1%.*}' sh {} \; -fi +} + +configure_supervisord_php_fpm() { + if [[ "${PHP_FPM_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/php-fpm.conf.template ]]; then + return 0 + fi -# Supervisor: PHP-FPM -if [ "${PHP_FPM_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/php-fpm.conf.template ]; then gomplate /etc/supervisor/conf.d/php-fpm.conf -fi +} + +configure_supervisord_gotty() { + if [[ "${GOTTY_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/gotty.conf.template ]]; then + return 0 + fi -# Supervisor: Gotty -if [ "${GOTTY_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/gotty.conf.template ]; then gomplate /etc/supervisor/conf.d/gotty.conf -fi +} + +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-$HOME/.local/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + prepare_php_directories + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool +} + +prepare_php_directories() { + mkdir -p "${PHP_PREFIX_LONG}/mods-available" "${PHP_PREFIX_LONG}/cli/conf.d" "${PHP_PREFIX_LONG}/fpm/conf.d" "${PHP_PREFIX_LONG}/fpm/pool.d" +} + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi -# PHP -PHP_PREFIX="/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" + gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" + phpenmod docker +} -# Configure PHP Global Settings -gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" -phpenmod docker +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi -# Configure PHP Opcache -gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" -phpenmod opcache + gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" + phpenmod opcache +} + +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem update-ca-certificates -fi +} + +configure_msmtp() { + if [[ ! -f "${HOME}/msmtprc.template" ]]; then + return 0 + fi -if [ -f "${HOME}/msmtprc.template" ]; then gomplate <"${HOME}/msmtprc.template" >"${HOME}/.msmtprc" chmod 600 "${HOME}/.msmtprc" -fi +} + +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" composer self-update "${COMPOSER_VERSION:-}" fi -fi +} + +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/.composer/vendor/bin:/home/www-data/bin:/home/www-data/.local/bin:/var/www/html/node_modules/.bin:/home/www-data/node_modules/.bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - fi -fi +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + configure_cron + + configure_supervisord + + # If the first arg is `-D` or `--some-option` pass it to supervisord. + if [[ $# -eq 0 ]] || [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- supervisord -c /etc/supervisor/supervisord.conf "$@" + # If the first arg is supervisord call it normally. + elif [[ "${1}" == "supervisord" ]]; then + set -- "$@" + # If the first arg is anything else + else + set -- "$@" + fi -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ $# -eq 0 ] || [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- supervisord -c /etc/supervisor/supervisord.conf "$@" -# If the first arg is supervisord call it normally. -elif [ "${1}" = "supervisord" ]; then - set -- "$@" -# If the first arg is anything else -else - set -- "$@" -fi + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 -exec "$@" +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php-fpm-rootless/wordpress-web/context/rootfs/docker-entrypoint.sh b/images/php-fpm-rootless/wordpress-web/context/rootfs/docker-entrypoint.sh index ee5a3238..125782f8 100755 --- a/images/php-fpm-rootless/wordpress-web/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm-rootless/wordpress-web/context/rootfs/docker-entrypoint.sh @@ -4,124 +4,219 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } shopt -s expand_aliases -if [ -f "${HOME}/.bash_alias" ]; then +if [[ -f "${HOME}/.bash_alias" ]]; then source "${HOME}/.bash_alias" fi -# Supervisor: Fix Permissions -if [ "${FIX_PERMISSIONS:-true}" = "true" ] && [ -f /etc/supervisor/available.d/permission.conf.template ]; then +configure_supervisord() { + configure_supervisord_fix_permissions + configure_supervisord_cron + configure_supervisord_socat + configure_supervisord_nginx + configure_supervisord_php_fpm + configure_supervisord_gotty +} + +configure_supervisord_fix_permissions() { + if [[ "${FIX_PERMISSIONS:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/permission.conf.template ]]; then + return 0 + fi + gomplate /etc/supervisor/conf.d/permission.conf -fi +} + +configure_supervisord_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/cron.conf.template ]]; then + return 0 + fi -# Supervisor: Cron -if [ "${CRON_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/cron.conf.template ]; then gomplate /etc/supervisor/conf.d/cron.conf -fi +} + +configure_supervisord_socat() { + if [[ "${SOCAT_ENABLED:-false}" != "true" ]] || + [[ ! -S /run/host-services/ssh-auth.sock ]] || + [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]] || + [[ ! -f /etc/supervisor/available.d/socat.conf.template ]]; then + return 0 + fi -# Supervisor: Socat -if [ "${SOCAT_ENABLED:-false}" = "true" ] && - [ -S /run/host-services/ssh-auth.sock ] && - [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ] && - [ -f /etc/supervisor/available.d/socat.conf.template ]; then gomplate /etc/supervisor/conf.d/socat.conf -fi +} + +configure_supervisord_nginx() { + if [[ "${NGINX_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/nginx.conf.template ]]; then + return 0 + fi -# Supervisor: Nginx -if [ "${NGINX_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/nginx.conf.template ]; then gomplate /etc/supervisor/conf.d/nginx.conf find /etc/nginx -name '*.template' -exec sh -c 'gomplate <${1} > ${1%.*}' sh {} \; -fi +} + +configure_supervisord_php_fpm() { + if [[ "${PHP_FPM_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/php-fpm.conf.template ]]; then + return 0 + fi -# Supervisor: PHP-FPM -if [ "${PHP_FPM_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/php-fpm.conf.template ]; then gomplate /etc/supervisor/conf.d/php-fpm.conf -fi +} + +configure_supervisord_gotty() { + if [[ "${GOTTY_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/gotty.conf.template ]]; then + return 0 + fi -# Supervisor: Gotty -if [ "${GOTTY_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/gotty.conf.template ]; then gomplate /etc/supervisor/conf.d/gotty.conf -fi +} + +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-$HOME/.local/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + prepare_php_directories + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool +} + +prepare_php_directories() { + mkdir -p "${PHP_PREFIX_LONG}/mods-available" "${PHP_PREFIX_LONG}/cli/conf.d" "${PHP_PREFIX_LONG}/fpm/conf.d" "${PHP_PREFIX_LONG}/fpm/pool.d" +} + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi -# PHP -PHP_PREFIX="/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" + gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" + phpenmod docker +} -# Configure PHP Global Settings -gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" -phpenmod docker +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi -# Configure PHP Opcache -gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" -phpenmod opcache + gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" + phpenmod opcache +} + +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem update-ca-certificates -fi +} + +configure_msmtp() { + if [[ ! -f "${HOME}/msmtprc.template" ]]; then + return 0 + fi -if [ -f "${HOME}/msmtprc.template" ]; then gomplate <"${HOME}/msmtprc.template" >"${HOME}/.msmtprc" chmod 600 "${HOME}/.msmtprc" -fi +} + +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer1" -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then alternatives --altdir ~/.local/etc/alternatives --admindir ~/.local/var/lib/alternatives --set composer "${HOME}/.local/bin/composer2" composer self-update "${COMPOSER_VERSION:-}" fi -fi +} + +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/.composer/vendor/bin:/home/www-data/bin:/home/www-data/.local/bin:/var/www/html/node_modules/.bin:/home/www-data/node_modules/.bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - fi -fi +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + configure_cron + + configure_supervisord + + # If the first arg is `-D` or `--some-option` pass it to supervisord. + if [[ $# -eq 0 ]] || [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- supervisord -c /etc/supervisor/supervisord.conf "$@" + # If the first arg is supervisord call it normally. + elif [[ "${1}" == "supervisord" ]]; then + set -- "$@" + # If the first arg is anything else + else + set -- "$@" + fi -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ $# -eq 0 ] || [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- supervisord -c /etc/supervisor/supervisord.conf "$@" -# If the first arg is supervisord call it normally. -elif [ "${1}" = "supervisord" ]; then - set -- "$@" -# If the first arg is anything else -else - set -- "$@" -fi + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 -exec "$@" +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php-fpm/base/context/rootfs/docker-entrypoint.sh b/images/php-fpm/base/context/rootfs/docker-entrypoint.sh index a0906ff4..28d06fd3 100755 --- a/images/php-fpm/base/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm/base/context/rootfs/docker-entrypoint.sh @@ -3,137 +3,226 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } -# PHP -PHP_PREFIX="/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" +shopt -s expand_aliases +if [[ -f "${HOME}/.bash_alias" ]]; then + source "${HOME}/.bash_alias" +fi + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi -# Configure PHP Global Settings -if [ -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" phpenmod docker -fi +} + +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi -# Configure PHP Opcache -if [ -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" phpenmod opcache -fi +} + +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_php_xdebug() { + if [[ ! -f "${PHP_PREFIX}/mods-available/xdebug.ini.template" ]]; then + return 0 + fi -# Configure PHP XDebug -if [ -f "${PHP_PREFIX}/mods-available/xdebug.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/xdebug.ini.template" >"${PHP_PREFIX_LONG}/mods-available/xdebug.ini" phpenmod xdebug -fi +} + +configure_php_blackfire() { + if [[ ! -f "${PHP_PREFIX}/mods-available/blackfire.ini.template" ]]; then + return 0 + fi -# Configure PHP Blackfire -if [ -f "${PHP_PREFIX}/mods-available/blackfire.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/blackfire.ini.template" >"${PHP_PREFIX_LONG}/mods-available/blackfire.ini" phpenmod blackfire -fi +} + +configure_php_spx() { + if [[ ! -f "${PHP_PREFIX}/mods-available/spx.ini.template" ]]; then + return 0 + fi -# Configure PHP SPX -if [ -f "${PHP_PREFIX}/mods-available/spx.ini.template" ]; then gomplate <"${PHP_PREFIX}/mods-available/spx.ini.template" >"${PHP_PREFIX_LONG}/mods-available/spx.ini" phpenmod spx -fi +} + +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool + configure_php_xdebug + configure_php_blackfire + configure_php_spx +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then sudo cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem sudo update-ca-certificates -fi +} -if [ -f "/etc/msmtprc.template" ]; then - gomplate /dev/null + sudo chmod 0600 /etc/msmtprc /home/www-data/.msmtprc +} -# start socat process in background to connect sockets used for agent access within container environment -# shellcheck disable=SC2039 -if [ -S /run/host-services/ssh-auth.sock ] && [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ]; then - sudo bash -c "nohup socat UNIX-CLIENT:/run/host-services/ssh-auth.sock \ - UNIX-LISTEN:${SSH_AUTH_SOCK},fork,user=www-data,group=www-data 1>/var/log/socat-ssh-auth.log 2>&1 &" -fi +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then sudo n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + sudo alternatives --set composer /usr/local/bin/composer1 + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + sudo alternatives --set composer /usr/local/bin/composer2 + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - sudo alternatives --set composer /usr/local/bin/composer1 -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - sudo alternatives --set composer /usr/local/bin/composer2 -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then sudo alternatives --set composer /usr/local/bin/composer2 sudo composer self-update "${COMPOSER_VERSION:-}" fi -fi +} + +fix_permissions() { + # Resolve permission issues with directories auto-created by volume mounts; to use set CHOWN_DIR_LIST to + # a list of directories (relative to working directory) to chown, walking up the paths to also chown each + # specified parent directory. Example: "dir1/dir2 dir3" will chown dir1/dir2, then dir1 followed by dir3 + # shellcheck disable=SC2039 + for DIR in ${CHOWN_DIR_LIST:-}; do + if [[ -d "${DIR}" ]]; then + while :; do + sudo chown www-data:www-data "${DIR}" + DIR=$(dirname "${DIR}") + if [[ "${DIR}" == "." ]] || [[ "${DIR}" == "/" ]]; then + break + fi + done + fi + done + + # Resolve permission issue with /var/www/html being owned by root as a result of volume mounted on php-fpm + # and nginx combined with nginx running as a different uid/gid than php-fpm does. This condition, when it + # surfaces would cause mutagen sync failures (on initial startup) on macOS environments. + sudo chown www-data:www-data /var/www/html +} + +start_socat() { + # start socat process in background to connect sockets used for agent access within container environment + # shellcheck disable=SC2039 + if [[ ! -S /run/host-services/ssh-auth.sock ]] || [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]]; then + return 0 + fi + + sudo bash -c "nohup socat UNIX-CLIENT:/run/host-services/ssh-auth.sock \ + UNIX-LISTEN:${SSH_AUTH_SOCK},fork,user=www-data,group=www-data 1>/var/log/socat-ssh-auth.log 2>&1 &" +} + +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -# Resolve permission issues with directories auto-created by volume mounts; to use set CHOWN_DIR_LIST to -# a list of directories (relative to working directory) to chown, walking up the paths to also chown each -# specified parent directory. Example: "dir1/dir2 dir3" will chown dir1/dir2, then dir1 followed by dir3 -# shellcheck disable=SC2039 -for DIR in ${CHOWN_DIR_LIST:-}; do - if [ -d "${DIR}" ]; then - while :; do - sudo chown www-data:www-data "${DIR}" - DIR=$(dirname "${DIR}") - if [ "${DIR}" = "." ] || [ "${DIR}" = "/" ]; then - break - fi - done - fi -done - -# Resolve permission issue with /var/www/html being owned by root as a result of volume mounted on php-fpm -# and nginx combined with nginx running as a different uid/gid than php-fpm does. This condition, when it -# surfaces would cause mutagen sync failures (on initial startup) on macOS environments. -sudo chown www-data:www-data /var/www/html - -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - fi -fi +} -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- php-fpm "$@" -# If the first arg is php-fpm call it normally. -else - set -- "$@" -fi +start_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi + + sudo cron +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + fix_permissions -exec "$@" + start_socat + + configure_cron + start_cron + + # If the first arg is `-D` or `--some-option` pass it to php-fpm. + if [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- php-fpm "$@" + # If the first arg is php-fpm call it normally. + else + set -- "$@" + fi + + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 + +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php-fpm/magento2-web/context/rootfs/docker-entrypoint.sh b/images/php-fpm/magento2-web/context/rootfs/docker-entrypoint.sh index f59048a0..ade8266b 100755 --- a/images/php-fpm/magento2-web/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm/magento2-web/context/rootfs/docker-entrypoint.sh @@ -3,134 +3,241 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } -# Supervisor: Fix Permissions -if [ "${FIX_PERMISSIONS:-true}" = "true" ] && [ -f /etc/supervisor/available.d/permission.conf.template ]; then - gomplate /etc/supervisor/conf.d/permission.conf -fi +shopt -s expand_aliases +if [[ -f "${HOME}/.bash_alias" ]]; then + source "${HOME}/.bash_alias" +fi + +configure_supervisord() { + configure_supervisord_sudo + configure_supervisord_fix_permissions + configure_supervisord_cron + configure_supervisord_socat + configure_supervisord_nginx + configure_supervisord_php_fpm + configure_supervisord_gotty +} + +configure_supervisord_sudo() { + if [[ "${SET_SUDO:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/sudo.conf.template ]]; then + return 0 + fi -# Supervisor: Sudo -if [ "${SET_SUDO:-true}" = "true" ] && [ -f /etc/supervisor/available.d/sudo.conf.template ]; then gomplate /etc/supervisor/conf.d/sudo.conf -fi +} + +configure_supervisord_fix_permissions() { + if [[ "${FIX_PERMISSIONS:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/permission.conf.template ]]; then + return 0 + fi + + gomplate /etc/supervisor/conf.d/permission.conf +} + +configure_supervisord_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/cron.conf.template ]]; then + return 0 + fi -# Supervisor: Cron -if [ "${CRON_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/cron.conf.template ]; then gomplate /etc/supervisor/conf.d/cron.conf -fi +} + +configure_supervisord_socat() { + if [[ "${SOCAT_ENABLED:-false}" != "true" ]] || + [[ ! -S /run/host-services/ssh-auth.sock ]] || + [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]] || + [[ ! -f /etc/supervisor/available.d/socat.conf.template ]]; then + return 0 + fi -# Supervisor: Socat -if [ "${SOCAT_ENABLED:-false}" = "true" ] && - [ -S /run/host-services/ssh-auth.sock ] && - [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ] && - [ -f /etc/supervisor/available.d/socat.conf.template ]; then gomplate /etc/supervisor/conf.d/socat.conf -fi +} + +configure_supervisord_nginx() { + if [[ "${NGINX_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/nginx.conf.template ]]; then + return 0 + fi -# Supervisor: Nginx -if [ "${NGINX_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/nginx.conf.template ]; then gomplate /etc/supervisor/conf.d/nginx.conf find /etc/nginx -name '*.template' -exec sh -c 'gomplate <${1} > ${1%.*}' sh {} \; -fi +} + +configure_supervisord_php_fpm() { + if [[ "${PHP_FPM_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/php-fpm.conf.template ]]; then + return 0 + fi -# Supervisor: PHP-FPM -if [ "${PHP_FPM_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/php-fpm.conf.template ]; then gomplate /etc/supervisor/conf.d/php-fpm.conf -fi +} + +configure_supervisord_gotty() { + if [[ "${GOTTY_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/gotty.conf.template ]]; then + return 0 + fi -# Supervisor: Gotty -if [ "${GOTTY_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/gotty.conf.template ]; then gomplate /etc/supervisor/conf.d/gotty.conf -fi +} + +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool +} + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi -# PHP -PHP_PREFIX="/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" + gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" + phpenmod docker +} -# Configure PHP Global Settings -gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" -phpenmod docker +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi + + gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" + phpenmod opcache +} -# Configure PHP Opcache -gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" -phpenmod opcache +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then sudo cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem sudo update-ca-certificates -fi +} + +configure_msmtp() { + if [[ ! -f "/etc/msmtprc.template" ]]; then + return 0 + fi -if [ -f "/etc/msmtprc.template" ]; then - gomplate /dev/null sudo chmod 0600 /etc/msmtprc /home/www-data/.msmtprc -fi +} + +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then sudo n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ -z "${COMPOSER_VERSION:-}" ]]; then + return 0 + fi + + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + sudo alternatives --set composer /usr/local/bin/composer1 + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + sudo alternatives --set composer /usr/local/bin/composer2 + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - sudo alternatives --set composer /usr/local/bin/composer1 -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - sudo alternatives --set composer /usr/local/bin/composer2 -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then sudo alternatives --set composer /usr/local/bin/composer2 sudo composer self-update "${COMPOSER_VERSION:-}" fi -fi +} -if [ "${WWWDATA_PASSWORD}" != "" ]; then - echo "www-data:${WWWDATA_PASSWORD}" | sudo /usr/sbin/chpasswd - unset WWWDATA_PASSWORD -fi +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - - else # If CRONJOBS is not set, set default Magento cron + else + # If CRONJOBS is not set, set default Magento cron printf "* * * * * /usr/bin/test ! -e /var/www/html/var/.maintenance.flag -a ! -e /var/www/html/var/.cron-disable && cd /var/www/html && /usr/bin/php /var/www/html/bin/magento cron:run 2>&1 | grep -v 'Ran jobs by schedule' >> /var/www/html/var/log/magento.cron.log\n" | crontab -u www-data - fi -fi +} -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ $# -eq 0 ] || [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- sudo supervisord -c /etc/supervisor/supervisord.conf "$@" -# If the first arg is supervisord call it normally. -elif [ "${1}" = "supervisord" ]; then - set -- sudo "$@" -# If the first arg is anything else -else - set -- "$@" -fi +change_wwwdata_password() { + if [[ -n "${WWWDATA_PASSWORD:-}" ]]; then + echo "www-data:${WWWDATA_PASSWORD:-}" | sudo /usr/sbin/chpasswd + unset WWWDATA_PASSWORD + fi +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + configure_cron + + configure_supervisord + + change_wwwdata_password + + # If the first arg is `-D` or `--some-option` pass it to supervisord. + if [[ $# -eq 0 ]] || [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- sudo supervisord -c /etc/supervisor/supervisord.conf "$@" + # If the first arg is supervisord call it normally. + elif [[ "${1}" == "supervisord" ]]; then + set -- sudo "$@" + # If the first arg is anything else + else + set -- "$@" + fi -exec "$@" + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 + +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php-fpm/shopware-web/context/rootfs/docker-entrypoint.sh b/images/php-fpm/shopware-web/context/rootfs/docker-entrypoint.sh index 5057f1d4..a9102564 100755 --- a/images/php-fpm/shopware-web/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm/shopware-web/context/rootfs/docker-entrypoint.sh @@ -3,126 +3,233 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } -# Supervisor: Fix Permissions -if [ "${FIX_PERMISSIONS:-true}" = "true" ] && [ -f /etc/supervisor/available.d/permission.conf.template ]; then - gomplate /etc/supervisor/conf.d/permission.conf -fi +shopt -s expand_aliases +if [[ -f "${HOME}/.bash_alias" ]]; then + source "${HOME}/.bash_alias" +fi + +configure_supervisord() { + configure_supervisord_sudo + configure_supervisord_fix_permissions + configure_supervisord_cron + configure_supervisord_socat + configure_supervisord_nginx + configure_supervisord_php_fpm + configure_supervisord_gotty +} + +configure_supervisord_sudo() { + if [[ "${SET_SUDO:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/sudo.conf.template ]]; then + return 0 + fi -# Supervisor: Sudo -if [ "${SET_SUDO:-true}" = "true" ] && [ -f /etc/supervisor/available.d/sudo.conf.template ]; then gomplate /etc/supervisor/conf.d/sudo.conf -fi +} + +configure_supervisord_fix_permissions() { + if [[ "${FIX_PERMISSIONS:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/permission.conf.template ]]; then + return 0 + fi + + gomplate /etc/supervisor/conf.d/permission.conf +} + +configure_supervisord_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/cron.conf.template ]]; then + return 0 + fi -# Supervisor: Cron -if [ "${CRON_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/cron.conf.template ]; then gomplate /etc/supervisor/conf.d/cron.conf -fi +} + +configure_supervisord_socat() { + if [[ "${SOCAT_ENABLED:-false}" != "true" ]] || + [[ ! -S /run/host-services/ssh-auth.sock ]] || + [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]] || + [[ ! -f /etc/supervisor/available.d/socat.conf.template ]]; then + return 0 + fi -# Supervisor: Socat -if [ "${SOCAT_ENABLED:-false}" = "true" ] && - [ -S /run/host-services/ssh-auth.sock ] && - [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ] && - [ -f /etc/supervisor/available.d/socat.conf.template ]; then gomplate /etc/supervisor/conf.d/socat.conf -fi +} + +configure_supervisord_nginx() { + if [[ "${NGINX_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/nginx.conf.template ]]; then + return 0 + fi -# Supervisor: Nginx -if [ "${NGINX_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/nginx.conf.template ]; then gomplate /etc/supervisor/conf.d/nginx.conf find /etc/nginx -name '*.template' -exec sh -c 'gomplate <${1} > ${1%.*}' sh {} \; -fi +} + +configure_supervisord_php_fpm() { + if [[ "${PHP_FPM_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/php-fpm.conf.template ]]; then + return 0 + fi -# Supervisor: PHP-FPM -if [ "${PHP_FPM_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/php-fpm.conf.template ]; then gomplate /etc/supervisor/conf.d/php-fpm.conf -fi +} + +configure_supervisord_gotty() { + if [[ "${GOTTY_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/gotty.conf.template ]]; then + return 0 + fi -# Supervisor: Gotty -if [ "${GOTTY_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/gotty.conf.template ]; then gomplate /etc/supervisor/conf.d/gotty.conf -fi +} + +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool +} + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi + + gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" + phpenmod docker +} -# PHP -PHP_PREFIX="/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi -# Configure PHP Global Settings -gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" -phpenmod docker + gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" + phpenmod opcache +} -# Configure PHP Opcache -gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" -phpenmod opcache +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then sudo cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem sudo update-ca-certificates -fi +} -if [ -f "/etc/msmtprc.template" ]; then - gomplate /dev/null sudo chmod 0600 /etc/msmtprc /home/www-data/.msmtprc -fi +} + +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then sudo n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + sudo alternatives --set composer /usr/local/bin/composer1 + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + sudo alternatives --set composer /usr/local/bin/composer2 + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - sudo alternatives --set composer /usr/local/bin/composer1 -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - sudo alternatives --set composer /usr/local/bin/composer2 -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then sudo alternatives --set composer /usr/local/bin/composer2 sudo composer self-update "${COMPOSER_VERSION:-}" fi -fi +} + +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - fi -fi +} -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ $# -eq 0 ] || [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- sudo supervisord -c /etc/supervisor/supervisord.conf "$@" -# If the first arg is supervisord call it normally. -elif [ "${1}" = "supervisord" ]; then - set -- sudo "$@" -# If the first arg is anything else -else - set -- "$@" -fi +change_wwwdata_password() { + if [[ -n "${WWWDATA_PASSWORD:-}" ]]; then + echo "www-data:${WWWDATA_PASSWORD:-}" | sudo /usr/sbin/chpasswd + unset WWWDATA_PASSWORD + fi +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + configure_cron + + configure_supervisord + + change_wwwdata_password + + # If the first arg is `-D` or `--some-option` pass it to supervisord. + if [[ $# -eq 0 ]] || [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- sudo supervisord -c /etc/supervisor/supervisord.conf "$@" + # If the first arg is supervisord call it normally. + elif [[ "${1}" == "supervisord" ]]; then + set -- sudo "$@" + # If the first arg is anything else + else + set -- "$@" + fi -exec "$@" + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 + +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php-fpm/wordpress-web/context/rootfs/docker-entrypoint.sh b/images/php-fpm/wordpress-web/context/rootfs/docker-entrypoint.sh index f2c99a8e..a9102564 100755 --- a/images/php-fpm/wordpress-web/context/rootfs/docker-entrypoint.sh +++ b/images/php-fpm/wordpress-web/context/rootfs/docker-entrypoint.sh @@ -3,131 +3,233 @@ set -e version_gt() { test "$(printf "%s\n" "${@#v}" | sort -V | head -n 1)" != "${1#v}"; } -# Supervisor: Fix Permissions -if [ "${FIX_PERMISSIONS:-true}" = "true" ] && [ -f /etc/supervisor/available.d/permission.conf.template ]; then - gomplate /etc/supervisor/conf.d/permission.conf -fi +shopt -s expand_aliases +if [[ -f "${HOME}/.bash_alias" ]]; then + source "${HOME}/.bash_alias" +fi + +configure_supervisord() { + configure_supervisord_sudo + configure_supervisord_fix_permissions + configure_supervisord_cron + configure_supervisord_socat + configure_supervisord_nginx + configure_supervisord_php_fpm + configure_supervisord_gotty +} + +configure_supervisord_sudo() { + if [[ "${SET_SUDO:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/sudo.conf.template ]]; then + return 0 + fi -# Supervisor: Sudo -if [ "${SET_SUDO:-true}" = "true" ] && [ -f /etc/supervisor/available.d/sudo.conf.template ]; then gomplate /etc/supervisor/conf.d/sudo.conf -fi +} + +configure_supervisord_fix_permissions() { + if [[ "${FIX_PERMISSIONS:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/permission.conf.template ]]; then + return 0 + fi + + gomplate /etc/supervisor/conf.d/permission.conf +} + +configure_supervisord_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/cron.conf.template ]]; then + return 0 + fi -# Supervisor: Cron -if [ "${CRON_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/cron.conf.template ]; then gomplate /etc/supervisor/conf.d/cron.conf -fi +} + +configure_supervisord_socat() { + if [[ "${SOCAT_ENABLED:-false}" != "true" ]] || + [[ ! -S /run/host-services/ssh-auth.sock ]] || + [[ "${SSH_AUTH_SOCK}" == "/run/host-services/ssh-auth.sock" ]] || + [[ ! -f /etc/supervisor/available.d/socat.conf.template ]]; then + return 0 + fi -# Supervisor: Socat -if [ "${SOCAT_ENABLED:-false}" = "true" ] && - [ -S /run/host-services/ssh-auth.sock ] && - [ "${SSH_AUTH_SOCK}" != "/run/host-services/ssh-auth.sock" ] && - [ -f /etc/supervisor/available.d/socat.conf.template ]; then gomplate /etc/supervisor/conf.d/socat.conf -fi +} + +configure_supervisord_nginx() { + if [[ "${NGINX_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/nginx.conf.template ]]; then + return 0 + fi -# Supervisor: Nginx -if [ "${NGINX_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/nginx.conf.template ]; then gomplate /etc/supervisor/conf.d/nginx.conf find /etc/nginx -name '*.template' -exec sh -c 'gomplate <${1} > ${1%.*}' sh {} \; -fi +} + +configure_supervisord_php_fpm() { + if [[ "${PHP_FPM_ENABLED:-true}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/php-fpm.conf.template ]]; then + return 0 + fi -# Supervisor: PHP-FPM -if [ "${PHP_FPM_ENABLED:-true}" = "true" ] && [ -f /etc/supervisor/available.d/php-fpm.conf.template ]; then gomplate /etc/supervisor/conf.d/php-fpm.conf -fi +} + +configure_supervisord_gotty() { + if [[ "${GOTTY_ENABLED:-false}" != "true" ]] || [[ ! -f /etc/supervisor/available.d/gotty.conf.template ]]; then + return 0 + fi -# Supervisor: Gotty -if [ "${GOTTY_ENABLED:-false}" = "true" ] && [ -f /etc/supervisor/available.d/gotty.conf.template ]; then gomplate /etc/supervisor/conf.d/gotty.conf -fi +} + +configure_php() { + local PHP_PREFIX="${PHP_PREFIX:-/etc/php}" + local PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION?required}" + + configure_php_settings + configure_php_opcache + configure_php_cli + configure_php_fpm + configure_php_fpm_pool +} + +configure_php_settings() { + if [[ ! -f "${PHP_PREFIX}/mods-available/docker.ini.template" ]]; then + return 0 + fi + + gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" + phpenmod docker +} -# PHP -PHP_PREFIX="/etc/php" -PHP_PREFIX_LONG="${PHP_PREFIX}/${PHP_VERSION}" +configure_php_opcache() { + if [[ ! -f "${PHP_PREFIX}/mods-available/opcache.ini.template" ]]; then + return 0 + fi -# Configure PHP Global Settings -gomplate <"${PHP_PREFIX}/mods-available/docker.ini.template" >"${PHP_PREFIX_LONG}/mods-available/docker.ini" -phpenmod docker + gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" + phpenmod opcache +} -# Configure PHP Opcache -gomplate <"${PHP_PREFIX}/mods-available/opcache.ini.template" >"${PHP_PREFIX_LONG}/mods-available/opcache.ini" -phpenmod opcache +configure_php_cli() { + if [[ ! -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]]; then + return 0 + fi -# Configure PHP Cli -if [ -f "${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" ]; then gomplate <"${PHP_PREFIX}/cli/conf.d/php-cli.ini.template" >"${PHP_PREFIX_LONG}/cli/conf.d/php-cli.ini" -fi +} + +configure_php_fpm() { + if [[ ! -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]]; then + return 0 + fi -# Configure PHP-FPM -if [ -f "${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" ]; then gomplate <"${PHP_PREFIX}/fpm/conf.d/php-fpm.ini.template" >"${PHP_PREFIX_LONG}/fpm/conf.d/php-fpm.ini" -fi +} + +configure_php_fpm_pool() { + if [[ ! -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]]; then + return 0 + fi -# Configure PHP-FPM Pool -if [ -f "${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" ]; then gomplate <"${PHP_PREFIX}/fpm/pool.d/zz-docker.conf.template" >"${PHP_PREFIX_LONG}/fpm/pool.d/zz-docker.conf" -fi +} + +configure_reward_root_certificate() { + if [[ ! -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]]; then + return 0 + fi -# Update Reward Root Certificate if exist -if [ -f /etc/ssl/reward-rootca-cert/ca.cert.pem ]; then sudo cp /etc/ssl/reward-rootca-cert/ca.cert.pem /usr/local/share/ca-certificates/reward-rootca-cert.pem sudo update-ca-certificates -fi +} -if [ -f "/etc/msmtprc.template" ]; then - gomplate /dev/null sudo chmod 0600 /etc/msmtprc /home/www-data/.msmtprc -fi +} + +configure_node_version() { + NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" + if [[ "${NODE_INSTALLED}" == "${NODE_VERSION}" ]]; then + return 0 + fi -# Install requested node version if not already installed -NODE_INSTALLED="$(node -v | perl -pe 's/^v([0-9]+)\..*$/$1/')" -if [ "${NODE_INSTALLED}" -ne "${NODE_VERSION}" ] || [ "${NODE_VERSION}" = "latest" ] || [ "${NODE_VERSION}" = "lts" ]; then sudo n install "${NODE_VERSION}" -fi +} + +configure_composer_version() { + if [[ "${COMPOSER_VERSION:-}" == "1" ]]; then + sudo alternatives --set composer /usr/local/bin/composer1 + return $? + fi + + if [[ "${COMPOSER_VERSION:-}" == "2" ]]; then + sudo alternatives --set composer /usr/local/bin/composer2 + return $? + fi -# Configure composer version -if [ "${COMPOSER_VERSION:-}" = "1" ]; then - sudo alternatives --set composer /usr/local/bin/composer1 -elif [ "${COMPOSER_VERSION:-}" = "2" ]; then - sudo alternatives --set composer /usr/local/bin/composer2 -else if version_gt "${COMPOSER_VERSION:-}" "2.0"; then sudo alternatives --set composer /usr/local/bin/composer2 sudo composer self-update "${COMPOSER_VERSION:-}" fi -fi +} -if [ "${WWWDATA_PASSWORD}" != "" ]; then - echo "www-data:${WWWDATA_PASSWORD}" | sudo /usr/sbin/chpasswd - unset WWWDATA_PASSWORD -fi +configure_cron() { + if [[ "${CRON_ENABLED:-false}" != "true" ]]; then + return 0 + fi -if [ "${CRON_ENABLED:-false}" = "true" ]; then printf "PATH=/home/www-data/bin:/home/www-data/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nSHELL=/bin/bash\n" | crontab -u www-data - # If CRONJOBS is set, write it to the crontab - if [ -n "${CRONJOBS}" ]; then + if [[ -n "${CRONJOBS:-}" ]]; then crontab -l -u www-data | { cat - printf "%s\n" "${CRONJOBS}" + printf "%s\n" "${CRONJOBS:-}" } | crontab -u www-data - fi -fi +} -# If the first arg is `-D` or `--some-option` pass it to php-fpm. -if [ $# -eq 0 ] || [ "${1#-}" != "$1" ] || [ "${1#-}" != "$1" ]; then - set -- sudo supervisord -c /etc/supervisor/supervisord.conf "$@" -# If the first arg is supervisord call it normally. -elif [ "${1}" = "supervisord" ]; then - set -- sudo "$@" -# If the first arg is anything else -else - set -- "$@" -fi +change_wwwdata_password() { + if [[ -n "${WWWDATA_PASSWORD:-}" ]]; then + echo "www-data:${WWWDATA_PASSWORD:-}" | sudo /usr/sbin/chpasswd + unset WWWDATA_PASSWORD + fi +} + +main() { + configure_php + configure_reward_root_certificate + configure_msmtp + configure_node_version + configure_composer_version + + configure_cron + + configure_supervisord + + change_wwwdata_password + + # If the first arg is `-D` or `--some-option` pass it to supervisord. + if [[ $# -eq 0 ]] || [[ "${1#-}" != "$1" ]] || [[ "${1#-}" != "$1" ]]; then + set -- sudo supervisord -c /etc/supervisor/supervisord.conf "$@" + # If the first arg is supervisord call it normally. + elif [[ "${1}" == "supervisord" ]]; then + set -- sudo "$@" + # If the first arg is anything else + else + set -- "$@" + fi -exec "$@" + exec "$@" +} + +(return 0 2>/dev/null) && sourced=1 + +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi diff --git a/images/php/fpm/context/rootfs/docker-entrypoint.sh b/images/php/fpm/context/rootfs/docker-entrypoint.sh index abbb359a..70e4e511 100755 --- a/images/php/fpm/context/rootfs/docker-entrypoint.sh +++ b/images/php/fpm/context/rootfs/docker-entrypoint.sh @@ -1,9 +1,17 @@ #!/bin/bash set -e -# first arg is `-f` or `--some-option` -if [ "${1#-}" != "$1" ]; then - set -- php-fpm "$@" -fi +main() { + # If the first arg is `-D` or `--some-option` pass it to php-fpm. + if [[ "${1#-}" != "$1" ]]; then + set -- php-fpm "$@" + fi + + exec "$@" +} -exec "$@" +(return 0 2>/dev/null) && sourced=1 + +if [[ -z "${sourced:-}" ]]; then + main "$@" +fi