REL-0.1 Initial Release

This commit is contained in:
cytopia
2018-02-23 08:53:30 +01:00
parent c594390bb3
commit f9782dbab6
47 changed files with 5952 additions and 31 deletions

View File

@@ -0,0 +1,60 @@
#!/bin/sh
#
# Available global variables:
# + MY_USER
# + MY_GROUP
# + DEBUG_LEVEL
set -e
set -u
############################################################
# Functions
############################################################
###
### Change Timezone
###
set_timezone() {
tz_env_varname="${1}"
tz_php_ini="${2}"
if ! env_set "${tz_env_varname}"; then
log "info" "\$${tz_env_varname} not set."
log "info" "Setting PHP: timezone=UTC"
run "sed -i'' 's|^[[:space:]]*;*[[:space:]]*date\.timezone[[:space:]]*=.*$|date.timezone = UTF|g' ${tz_php_ini}"
else
tz_timezone="$( env_get "${tz_env_varname}" )"
if [ -f "/usr/share/zoneinfo/${tz_timezone}" ]; then
# Unix Time
log "info" "Setting container timezone to: ${tz_timezone}"
run "rm /etc/localtime"
run "ln -s /usr/share/zoneinfo/${tz_timezone} /etc/localtime"
# PHP Time
log "info" "Setting PHP: timezone=${tz_timezone}"
run "sed -i'' 's|^[[:space:]]*;*[[:space:]]*date\.timezone[[:space:]]*=.*$|date.timezone = ${tz_timezone}|g' ${tz_php_ini}"
else
log "err" "Invalid timezone for \$${tz_env_varname}."
log "err" "\$TIMEZONE: '${tz_timezone}' does not exist."
exit 1
fi
fi
log "info" "Docker date set to: $(date)"
unset -v tz_env_varname
unset -v tz_php_ini
unset -v tz_timezone
}
############################################################
# Sanity Checks
############################################################
if ! command -v sed >/dev/null 2>&1; then
echo "sed not found, but required."
exit 1
fi

View File

@@ -0,0 +1,71 @@
#!/bin/sh
#
# Available global variables:
# + MY_USER
# + MY_GROUP
# + DEBUG_LEVEL
set -e
set -u
############################################################
# Functions
############################################################
###
### Setup Postfix for catch-all
###
set_postfix() {
postfix_env_varname="${1}"
if ! env_set "${postfix_env_varname}"; then
log "info" "\$${postfix_env_varname} not set."
log "info" "Disabling sending of emails"
else
postfix_env_value="$( env_get "${postfix_env_varname}" )"
if [ "${postfix_env_value}" = "1" ]; then
log "info" "Enabling sending of emails"
# Add Mail file if it does not exist
if [ ! -f "/var/mail/${MY_USER}" ]; then
run "touch /var/mail/${MY_USER}"
fi
# Fix mail user permissions after mount
run "chmod 0644 /var/mail/${MY_USER}"
run "chown ${MY_USER}:${MY_GROUP} /var/mail"
run "chown ${MY_USER}:${MY_GROUP} /var/mail/${MY_USER}"
# Postfix configuration
run "postconf -e 'inet_protocols=ipv4'"
run "postconf -e 'virtual_alias_maps=pcre:/etc/postfix/virtual'"
run "echo '/.*@.*/ ${MY_USER}' >> /etc/postfix/virtual"
run "newaliases"
elif [ "${postfix_env_value}" = "0" ]; then
log "info" "Disabling sending of emails."
else
log "err" "Invalid value for \$${postfix_env_varname}"
log "err" "Only 1 (for on) or 0 (for off) are allowed"
exit 1
fi
fi
unset -v postfix_env_varname
unset -v postfix_env_value
}
############################################################
# Sanity Checks
############################################################
if ! command -v postconf >/dev/null 2>&1; then
echo "postconf not found, but required."
exit 1
fi

View File

@@ -0,0 +1,116 @@
#!/bin/sh
#
# Available global variables:
# + MY_USER
# + MY_GROUP
# + DEBUG_LEVEL
set -e
set -u
############################################################
# 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
}
############################################################
# Functions
############################################################
###
### Change UID
###
set_docker_logs() {
dl_env_varname="${1}"
dl_log_dir="${2}"
dl_fpm_error_log_conf="${3}"
dl_fpm_access_log_conf="${4}"
if ! env_set "${dl_env_varname}"; then
log "info" "\$${dl_env_varname} not set."
log "info" "Logging to docker logs stdout and stderr"
else
dl_docker_logs="$( env_get "${dl_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}"
# Keep docker logs
elif [ "${dl_docker_logs}" = "1" ]; then
log "info" "\$${dl_env_varname} set to 1. Logging to docker logs stdout and stderr."
else
log "err" "Invalid value for \$${dl_env_varname}. Can only be 0 or 1. Provided: ${dl_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

View File

@@ -0,0 +1,204 @@
#!/bin/sh
#
# Available global variables:
# + MY_USER
# + MY_GROUP
# + DEBUG_LEVEL
set -e
set -u
############################################################
# Helper Functions
############################################################
###
### Helper functions
###
_isip() {
# IP is not in correct format
if ! echo "${1}" | grep -Eq '^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$'; then
return 1
fi
# Get each octet
isip_o1="$( echo "${1}" | awk -F'.' '{print $1}' )"
isip_o2="$( echo "${1}" | awk -F'.' '{print $2}' )"
isip_o3="$( echo "${1}" | awk -F'.' '{print $3}' )"
isip_o4="$( echo "${1}" | awk -F'.' '{print $4}' )"
# Cannot start with 0 and all must be below 256
if [ "${isip_o1}" -lt "1" ] || \
[ "${isip_o1}" -gt "255" ] || \
[ "${isip_o2}" -gt "255" ] || \
[ "${isip_o3}" -gt "255" ] || \
[ "${isip_o4}" -gt "255" ]; then
unset -v isip_o1
unset -v isip_o2
unset -v isip_o3
unset -v isip_o4
# Error
return 1
fi
unset -v isip_o1
unset -v isip_o2
unset -v isip_o3
unset -v isip_o4
# Success
return 0
}
_ishostname() {
# Does not have correct character class
if ! echo "${1}" | grep -Eq '^[-.0-9a-zA-Z]+$'; then
return 1
fi
# first and last character
ishostname_f_char="$( echo "${1}" | cut -c1-1 )"
ishostname_l_char="$( echo "${1}" | sed -e 's/.*\(.\)$/\1/' )"
# Dot at beginning or end
if [ "${ishostname_f_char}" = "." ] || [ "${ishostname_l_char}" = "." ]; then
unset -v ishostname_f_char
unset -v ishostname_l_char
# Error
return 1
fi
# Dash at beginning or end
if [ "${ishostname_f_char}" = "-" ] || [ "${ishostname_l_char}" = "-" ]; then
unset -v ishostname_f_char
unset -v ishostname_l_char
# Error
return 1
fi
unset -v ishostname_f_char
unset -v ishostname_l_char
# Multiple dots next to each other
if echo "${1}" | grep -Eq '[.]{2,}'; then
# Error
return 1
fi
# Dash next to dot
if echo "${1}" | grep -Eq '(\.-)|(-\.)'; then
# Error
return 1
fi
# Success
return 0
}
############################################################
# Functions
############################################################
###
###
###
port_forward_get_lines() {
if env_set "${1}"; then
# Transform into newline separated forwards:
# local-port:host:remote-port\n
# local-port:host:remote-port\n
pfl_forwards="$( env_get "${1}" | sed 's/[[:space:]]*//g' | sed 's/,/\n/g' )"
# loop over them line by line
IFS='
'
for pfl_line in ${pfl_forwards}; do
echo "${pfl_line}"
done
unset -v pfl_forwards
unset -v pfl_line
fi
}
port_forward_get_lport() {
# local-port:host:remote-port\n
echo "${1}" | awk -F':' '{print $1}'
}
port_forward_get_rhost() {
# local-port:host:remote-port\n
echo "${1}" | awk -F':' '{print $2}'
}
port_forward_get_rport() {
# local-port:host:remote-port\n
echo "${1}" | awk -F':' '{print $3}'
}
port_forward_validate() {
pfv_env_varname="${1}"
if ! env_set "${pfv_env_varname}"; then
log "info" "\$${pfv_env_varname} not set."
log "info" "Not ports from other machines will be forwarded to 127.0.0.1 inside this docker"
else
# Loop over forwards in order to validate them
for pfv_line in $( port_forward_get_lines "${pfv_env_varname}" ); do
pfv_lport="$( port_forward_get_lport "${pfv_line}" )"
pfv_rhost="$( port_forward_get_rhost "${pfv_line}" )"
pfv_rport="$( port_forward_get_rport "${pfv_line}" )"
if ! isint "${pfv_lport}"; then
log "err" "Port forwarding error: local port is not an integer: ${pfv_lport}"
log "err" "Line: ${pfv_line}"
exit 1
fi
if ! _isip "${pfv_rhost}" && ! _ishostname "${pfv_rhost}"; then
log "err" "Port forwarding error: remote host is not a valid IP and not a valid hostname: ${pfv_rhost}"
log "err" "Line: ${pfv_line}"
log "err" ""
exit 1
fi
if ! isint "${pfv_rport}"; then
log "err" "Port forwarding error: remote port is not an integer: ${pfv_rport}"
log "err" "Line: ${pfv_line}"
log "err" ""
exit 1
fi
log "info" "Forwarding ${pfv_rhost}:${pfv_rport} to 127.0.0.1:${pfv_lport} inside this docker."
done
unset -v pfv_line
unset -v pfv_lport
unset -v pfv_rhost
unset -v pfv_rport
fi
unset -v pfv_env_varname
}
############################################################
# Sanity Checks
############################################################
if ! command -v awk >/dev/null 2>&1; then
echo "awk not found, but required."
exit 1
fi
if ! command -v cut >/dev/null 2>&1; then
echo "cut not found, but required."
exit 1
fi
if ! command -v sed >/dev/null 2>&1; then
echo "sed not found, but required."
exit 1
fi

View File

@@ -0,0 +1,58 @@
#!/bin/sh
#
# Available global variables:
# + MY_USER
# + MY_GROUP
# + DEBUG_LEVEL
set -e
set -u
############################################################
# Functions
############################################################
###
### Copy *.ini files from source to destination with prefix
###
copy_ini_files() {
ini_src="${1}"
ini_dst="${2}"
if [ ! -d "${ini_src}" ]; then
run "mkdir -p ${ini_src}"
fi
ini_files="$( find "${ini_src}" -type f -iname '*.ini' )"
# loop over them line by line
IFS='
'
for ini_f in ${ini_files}; do
ini_name="$( basename "${ini_f}" )"
log "info" "PHP.ini: ${ini_name} -> ${ini_dst}/zzz-devilbox-${ini_name}"
run "cp ${ini_f} ${ini_dst}/devilbox-${ini_name}"
done
run "find ${ini_dst} -type f -iname '*.ini' -exec chmod 0644 \"{}\" \;"
unset -v ini_src
unset -v ini_dst
unset -v ini_files
unset -v ini_f
unset -v ini_name
}
############################################################
# Sanity Checks
############################################################
if ! command -v find >/dev/null 2>&1; then
echo "find not found, but required."
exit 1
fi
if ! command -v basename >/dev/null 2>&1; then
echo "basename not found, but required."
exit 1
fi

View File

@@ -0,0 +1,60 @@
#!/bin/sh
#
# Available global variables:
# + MY_USER
# + MY_GROUP
# + DEBUG_LEVEL
set -e
set -u
############################################################
# Functions
############################################################
###
### Add service to supervisord
###
supervisor_add_service() {
supervisor_name="${1}"
supervisor_command="${2}"
supervisor_confd="${3}"
supervisor_priority=
if [ "${#}" -gt "3" ]; then
supervisor_priority="${4}"
fi
if [ ! -d "${supervisor_confd}" ]; then
run "mkdir -p ${supervisor_confd}"
fi
# Add services
{
echo "[program:${supervisor_name}]";
echo "command = ${supervisor_command}";
if [ -n "${supervisor_priority}" ]; then
echo "priority = ${supervisor_priority}";
fi
echo "autostart = true";
echo "autorestart = true";
echo "stdout_logfile = /dev/stdout";
echo "stdout_logfile_maxbytes = 0";
echo "stdout_events_enabled = true";
echo "stderr_logfile = /dev/stderr";
echo "stderr_logfile_maxbytes = 0";
echo "stderr_events_enabled = true";
} > "${supervisor_confd}/${supervisor_name}.conf"
unset -v supervisor_name
unset -v supervisor_command
unset -v supervisor_confd
unset -v supervisor_priority
}

View File

@@ -0,0 +1,132 @@
#!/bin/sh
#
# Available global variables:
# + MY_USER
# + MY_GROUP
set -e
set -u
###
### Variables
###
PHP_INI_PATH="/usr/local/etc/php.ini"
FPM_ERROR_LOG_CFG="/usr/local/etc/php-fpm.conf"
FPM_ACCESS_LOG_CFG="/usr/local/etc/php-fpm.d/zzz-docker.conf"
FPM_LOG_DIR="/var/log/php"
#PHP_CUST_MODULE_DIR="/etc/php-modules.d"
PHP_CUST_INI_DIR="/etc/php-custom.d"
PHP_REAL_INI_DIR="/usr/local/etc/php.d"
SUPERVISOR_CONFD="/etc/supervisor/conf.d"
###
### Source libs
###
init="$( find /docker-entrypoint.d -name '*.sh' -type f | sort -u )"
for f in ${init}; do
# shellcheck disable=SC1090
. "${f}"
done
###
### Set Debug level
###
DEBUG_LEVEL="$( get_debug_level "DEBUG_ENTRYPOINT" "0" )"
log "info" "Debug level: ${DEBUG_LEVEL}" "${DEBUG_LEVEL}"
#############################################################
## Sanity checks
#############################################################
if ! command -v socat >/dev/null 2>&1; then
log "err" "socat not found, but required." "${DEBUG_LEVEL}"
exit 1
fi
#############################################################
## Entry Point
#############################################################
###
### Change uid/gid
###
set_uid "NEW_UID"
set_gid "NEW_GID"
###
### Set timezone
###
set_timezone "TIMEZONE" "${PHP_INI_PATH}"
###
### Set Logging
###
set_docker_logs \
"DOCKER_LOGS" \
"${FPM_LOG_DIR}" \
"${FPM_ERROR_LOG_CFG}" \
"${FPM_ACCESS_LOG_CFG}"
###
### Setup postfix
###
set_postfix "ENABLE_MAIL"
###
### Validate socat port forwards
###
if ! port_forward_validate "FORWARD_PORTS_TO_LOCALHOST"; then
exit 1
fi
###
### Supervisor: socat
###
for line in $( port_forward_get_lines "FORWARD_PORTS_TO_LOCALHOST" ); do
lport="$( port_forward_get_lport "${line}" )"
rhost="$( port_forward_get_rhost "${line}" )"
rport="$( port_forward_get_rport "${line}" )"
supervisor_add_service "socat-${lport}-${rhost}-${rport}" "/usr/bin/socat tcp-listen:${lport},reuseaddr,fork tcp:${rhost}:${rport}" "${SUPERVISOR_CONFD}"
done
###
### Supervisor: rsyslogd & postfix
###
if [ "$( env_get "ENABLE_MAIL" )" = "1" ]; then
supervisor_add_service "rsyslogd" "/usr/sbin/rsyslogd -n" "${SUPERVISOR_CONFD}" "1"
supervisor_add_service "postfix" "/usr/local/sbin/postfix.sh" "${SUPERVISOR_CONFD}"
fi
###
### Supervisor: php-fpm
###
supervisor_add_service "php-fpm" "/usr/local/sbin/php-fpm" "${SUPERVISOR_CONFD}"
###
### Copy custom *.ini files
###
copy_ini_files "${PHP_CUST_INI_DIR}" "${PHP_REAL_INI_DIR}"
###
### Start
###
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

View File

@@ -0,0 +1,95 @@
#!/usr/bin/env bash
#
# Wrapper to have postfix run in foreground
# mode in order to be called by supvervisord
#
#
# CREDITS:
# This script is mostly based on the following Gist:
# https://gist.github.com/chrisnew/b0c1b8d310fc5ceaeac4
#
###
### Be strict
###
set -e
set -u
set -o pipefail
###
### Variables
###
if [ -f "/etc/alpine-release" ]; then
MAILLOG="/var/log/maillog"
elif [ -f "/etc/debian_version" ]; then
MAILLOG="/var/log/mail.log"
else
MAILLOG="/var/log/maillog"
fi
MAILPID="/var/spool/postfix/pid/master.pid"
###
### Sanity checks
###
if ! command -v pidof >/dev/null 2>&1; then
echo "pidof is required for cleaning up tail command."
exit 1
fi
# Give rsyslogd some time to start up
sleep 2
if ! pidof rsyslogd >/dev/null 2>&1; then
echo "rsyslogd is not running, but required for mail logging."
exit 1
fi
# force new copy of hosts there (otherwise links could be outdated)
# TODO: check if required
#cp /etc/hosts /var/spool/postfix/etc/hosts
###
### Trap signals
###
trap "postfix stop" SIGINT
trap "postfix stop" SIGTERM
trap "postfix reload" SIGHUP
###
### Startup
###
# start postfix
postfix start
# Capture output
tail -qF -n 0 "${MAILLOG}" &
tail_pid="${?}"
###
### Warm-up time
###
sleep 3
###
### Wait for kill signales
###
while kill -0 "$(cat "${MAILPID}")" >/dev/null 2>&1; do
# Check every second
sleep 1
done
###
### Clean-up
###
kill "${tail_pid}"

View File

@@ -0,0 +1,9 @@
[supervisord]
user = root
nodaemon = true
logfile = /var/log/supervisor/supervisord.log
pidfile = /var/run/supervisord.pid
childlogdir = /var/log/supervisor
[include]
files = /etc/supervisor/conf.d/*.conf