mirror of
https://github.com/devilbox/docker-php-fpm.git
synced 2026-01-10 01:51:14 +00:00
Rebuild docker-entrypoint scripts
This commit is contained in:
@@ -1,47 +1,48 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# Available global variables:
|
||||
# + MY_USER
|
||||
# + MY_GROUP
|
||||
# + DEBUG_LEVEL
|
||||
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
set -u
|
||||
|
||||
set -o pipefail
|
||||
|
||||
|
||||
############################################################
|
||||
# Helper Functions
|
||||
############################################################
|
||||
|
||||
# Check if PHP-FPM config files contain valid logging directives
|
||||
_validate_docker_logs() {
|
||||
vdl_fpm_error_log_conf="${1}"
|
||||
vdl_fpm_access_log_conf="${2}"
|
||||
|
||||
if [ ! -f "${vdl_fpm_error_log_conf}" ]; then
|
||||
log "err" "PHP-FPM Error log config file does not exist in: ${vdl_fpm_error_log_conf}"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! -f "${vdl_fpm_access_log_conf}" ]; then
|
||||
log "err" "PHP-FPM Access log config file does not exist in: ${dl_fpm_access_log_conf}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Eq '^error_log.*$' "${vdl_fpm_error_log_conf}"; then
|
||||
log "err" "PHP-FPM Error log config file has no error logging directive"
|
||||
exit 1
|
||||
fi
|
||||
if ! grep -Eq '^access\.log.*$' "${vdl_fpm_access_log_conf}"; then
|
||||
log "err" "PHP-FPM Access log config file has no access logging directive"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
unset -v vdl_fpm_error_log_conf
|
||||
unset -v vdl_fpm_access_log_conf
|
||||
_log_to_dockerlogs() {
|
||||
local conf_logfile="${1}"
|
||||
{
|
||||
echo "[global]"
|
||||
echo "error_log = /proc/self/fd/2"
|
||||
echo "[www]"
|
||||
echo "access.log = /proc/self/fd/2"
|
||||
} > "${conf_logfile}"
|
||||
}
|
||||
_log_to_files() {
|
||||
local conf_logfile="${1}"
|
||||
local log_dir="${2}"
|
||||
local user="${3}"
|
||||
local group="${4}"
|
||||
local debug="${5}"
|
||||
|
||||
# Create Log directory and files
|
||||
if [ ! -d "${log_dir}" ]; then
|
||||
run "mkdir -p ${log_dir}" "${debug}"
|
||||
fi
|
||||
if [ ! -f "${log_dir}/php-fpm.access" ]; then
|
||||
run "touch ${log_dir}/php-fpm.access" "${debug}"
|
||||
fi
|
||||
if [ ! -f "${log_dir}/php-fpm.error" ]; then
|
||||
run "touch ${log_dir}/php-fpm.error" "${debug}"
|
||||
fi
|
||||
run "chown -R ${user}:${group} ${log_dir}" "${debug}"
|
||||
{
|
||||
echo "[global]"
|
||||
echo "error_log = ${log_dir}/php-fpm.error"
|
||||
echo "[www]"
|
||||
echo "access.log = ${log_dir}/php-fpm.access"
|
||||
} > "${conf_logfile}"
|
||||
}
|
||||
|
||||
|
||||
############################################################
|
||||
@@ -49,68 +50,38 @@ _validate_docker_logs() {
|
||||
############################################################
|
||||
|
||||
###
|
||||
### Change UID
|
||||
### Change PHP-FPM logging (file or docker logs)
|
||||
###
|
||||
set_docker_logs() {
|
||||
dl_env_varname="${1}"
|
||||
dl_log_dir="${2}"
|
||||
dl_fpm_error_log_conf="${3}"
|
||||
dl_fpm_access_log_conf="${4}"
|
||||
local env_varname="${1}"
|
||||
local log_dir="${2}"
|
||||
local conf_logfile="${3}"
|
||||
local user="${4}"
|
||||
local group="${5}"
|
||||
local debug="${6}"
|
||||
|
||||
if ! env_set "${dl_env_varname}"; then
|
||||
log "info" "\$${dl_env_varname} not set."
|
||||
log "info" "Logging to docker logs stdout and stderr"
|
||||
local docker_logs=
|
||||
|
||||
if ! env_set "${env_varname}"; then
|
||||
log "info" "\$${env_varname} not set." "${debug}"
|
||||
log "info" "Logging to docker logs (stdout and stderr)." "${debug}"
|
||||
_log_to_dockerlogs "${conf_logfile}"
|
||||
else
|
||||
dl_docker_logs="$( env_get "${dl_env_varname}" )"
|
||||
docker_logs="$( env_get "${env_varname}" )"
|
||||
|
||||
# Disable docker logs and log to files
|
||||
if [ "${dl_docker_logs}" = "0" ]; then
|
||||
log "info" "\$${dl_env_varname} set to 0. Logging to files under: ${dl_log_dir}"
|
||||
log "info" "Make sure to mount this directory in order to view logs"
|
||||
|
||||
# Validation
|
||||
_validate_docker_logs "${dl_fpm_error_log_conf}" "${dl_fpm_access_log_conf}"
|
||||
|
||||
# Create Log directory
|
||||
if [ ! -d "${dl_log_dir}" ]; then
|
||||
run "mkdir -p ${dl_log_dir}"
|
||||
fi
|
||||
|
||||
# Fix permissions (in case uid/gid has changed)
|
||||
if [ ! -f "${dl_log_dir}/php-fpm.access" ]; then
|
||||
touch "${dl_log_dir}/php-fpm.access"
|
||||
fi
|
||||
if [ ! -f "${dl_log_dir}/php-fpm.error" ]; then
|
||||
touch "${dl_log_dir}/php-fpm.error"
|
||||
fi
|
||||
run "chown -R ${MY_USER}:${MY_GROUP} ${dl_log_dir}"
|
||||
|
||||
# Adjust PHP-FPM config to log to file
|
||||
run "sed -i'' 's|^error_log.*$|error_log = ${dl_log_dir}/php-fpm.error|g' ${dl_fpm_error_log_conf}"
|
||||
run "sed -i'' 's|^access\.log.*$|access.log = ${dl_log_dir}/php-fpm.access|g' ${dl_fpm_access_log_conf}"
|
||||
if [ "${docker_logs}" = "0" ]; then
|
||||
log "info" "\$${env_varname} set to 0. Logging to files under: ${log_dir}" "${debug}"
|
||||
log "info" "Make sure to mount this directory in order to view logs" "${debug}"
|
||||
_log_to_files "${conf_logfile}" "${log_dir}" "${user}" "${group}" "${debug}"
|
||||
|
||||
# Keep docker logs
|
||||
elif [ "${dl_docker_logs}" = "1" ]; then
|
||||
log "info" "\$${dl_env_varname} set to 1. Logging to docker logs stdout and stderr."
|
||||
elif [ "${docker_logs}" = "1" ]; then
|
||||
log "info" "\$${env_varname} set to 1. Logging to docker logs (stdout and stderr)."
|
||||
_log_to_dockerlogs "${conf_logfile}"
|
||||
else
|
||||
log "err" "Invalid value for \$${dl_env_varname}. Can only be 0 or 1. Provided: ${dl_docker_logs}"
|
||||
log "err" "Invalid value for \$${env_varname}. Can only be 0 or 1. Provided: ${docker_logs}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
unset -v dl_env_varname
|
||||
unset -v dl_log_dir
|
||||
unset -v dl_fpm_error_log_conf
|
||||
unset -v dl_fpm_access_log_conf
|
||||
unset -v dl_docker_logs
|
||||
}
|
||||
|
||||
|
||||
############################################################
|
||||
# Sanity Checks
|
||||
############################################################
|
||||
|
||||
if ! command -v sed >/dev/null 2>&1; then
|
||||
echo "sed not found, but required."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user