diff --git a/Dockerfile b/Dockerfile index b491033..1b88069 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,7 @@ ENV FLOWNATIVE_LIB_PATH="/opt/flownative/lib" \ PATH="/opt/flownative/php/bin:$PATH" \ LOG_DEBUG="true" +USER root COPY --from=docker.pkg.github.com/flownative/bash-library/bash-library:1 /lib $FLOWNATIVE_LIB_PATH COPY root-files / diff --git a/root-files/entrypoint.sh b/root-files/entrypoint.sh index 5732da4..cd4cb6a 100755 --- a/root-files/entrypoint.sh +++ b/root-files/entrypoint.sh @@ -7,21 +7,27 @@ set -o pipefail # Load lib . "${FLOWNATIVE_LIB_PATH}/banner.sh" -. "${FLOWNATIVE_LIB_PATH}/validation.sh" . "${FLOWNATIVE_LIB_PATH}/php-fpm.sh" +. "${FLOWNATIVE_LIB_PATH}/supervisor.sh" + +banner_flownative PHP eval "$(php_fpm_env)" +eval "$(supervisor_env)" -banner_flownative PHP +php_fpm_initialize -if [[ "$*" = *"run"* ]]; then - php_fpm_initialize +supervisor_initialize +supervisor_start - trap 'php_fpm_stop' SIGINT SIGTERM - php_fpm_start +trap 'supervisor_stop' SIGINT SIGTERM - wait "$(php_fpm_get_pid)" - # This line will not be reached, because a trap handles termination +if [[ "$*" = *"run"* ]]; then + supervisor_pid=$(supervisor_get_pid) + info "Entrypoint: Start up complete" + # We can't use "wait" because supervisord is not a direct child of this shell: + while [ -e "/proc/${supervisor_pid}" ]; do sleep 1.1; done + info "Good bye 👋" else "$@" fi diff --git a/root-files/opt/flownative/lib/php-fpm.sh b/root-files/opt/flownative/lib/php-fpm.sh index 2b36f7f..a455fbc 100755 --- a/root-files/opt/flownative/lib/php-fpm.sh +++ b/root-files/opt/flownative/lib/php-fpm.sh @@ -36,72 +36,6 @@ export PHP_FPM_MAX_CHILDREN="${PHP_FPM_MAX_CHILDREN:-20}" EOF } -# --------------------------------------------------------------------------------------- -# php_fpm_get_pid() - Return the php process id -# -# @global PHP_* The PHP_ environment variables -# @return Returns the PHP process id, if it is running, otherwise 0 -# -php_fpm_get_pid() { - local pid - pid=$(process_get_pid_from_file "${PHP_TMP_PATH}/php-fpm.pid") - - if [[ -n "${pid}" ]]; then - echo "${pid}" - else - false - fi -} - -# --------------------------------------------------------------------------------------- -# php_fpm_has_pid() - Checks if a PID file exists -# -# @global PHP_* The PHP_ environment variables -# @return Returns false if no PID file exists -# -php_fpm_has_pid() { - if [[ ! -f "${PHP_TMP_PATH}/php-fpm.pid" ]]; then - false - fi -} - -# --------------------------------------------------------------------------------------- -# php_fpm_start() - Start PHP -# -# @global PHP_* The PHP_ environment variables -# @return void -# -php_fpm_start() { - local pid - - info "PHP-FPM: Starting ..." - "${PHP_BASE_PATH}/sbin/php-fpm" 2>&1 | (sed 's/^/PHP-FPM: /' | output) & - sleep 1 - - with_backoff "php_fpm_has_pid" || (error "PHP-FPM: Could not retrieve PID of the PHP-FPM process, maybe it failed during start-up?"; exit 1) - pid=$(php_fpm_get_pid) - - info "PHP-FPM: Running as process #${pid}" -} - -# --------------------------------------------------------------------------------------- -# php_fpm_stop() - Stop the php process based on the current PID -# -# @global PHP_* The PHP_ evnironment variables -# @return void -# -php_fpm_stop() { - local pid - - pid=$(php_fpm_get_pid) - is_process_running "${pid}" || (info "PHP-FPM: Could not stop, because the process was not running (detected pid: ${pid})" && return); - info "PHP-FPM: Stopping ..." - - process_stop "${pid}" - - info "PHP-FPM: Process stopped, good-bye ... 👋" -} - # --------------------------------------------------------------------------------------- # php_fpm_conf_validate() - Validates configuration options passed as PHP_* env vars # @@ -126,4 +60,7 @@ php_fpm_initialize() { info "PHP-FPM: Initializing configuration ..." envsubst < "${PHP_CONF_PATH}/php-fpm.conf.template" > "${PHP_CONF_PATH}/php-fpm.conf" + + # Create a file descriptor for the PHP-FPM log output and clean up the log lines a bit: + exec 4> >(sed -e "s/^\([0-9\/-]* [0-9:,]*\)/\1 OUTPUT PHP-FPM:/") } diff --git a/root-files/opt/flownative/supervisor/etc/conf.d/php-fpm.conf b/root-files/opt/flownative/supervisor/etc/conf.d/php-fpm.conf new file mode 100644 index 0000000..c56829a --- /dev/null +++ b/root-files/opt/flownative/supervisor/etc/conf.d/php-fpm.conf @@ -0,0 +1,10 @@ +[program:php-fpm] +process_name=%(program_name)s +command="%(ENV_PHP_BASE_PATH)s/sbin/php-fpm" +autostart=true +autorestart=true +numprocs=1 + +stdout_logfile=/proc/self/fd/4 +stdout_logfile_maxbytes=0 +redirect_stderr=true