docker-mailserver/target/scripts/startup/setup-stack.sh

101 lines
2.5 KiB
Bash
Raw Normal View History

#!/bin/bash
declare -a FUNCS_SETUP
2023-05-25 23:01:41 +00:00
function _register_setup_function() {
FUNCS_SETUP+=("${1}")
_log 'trace' "${1}() registered"
}
2023-05-25 23:01:41 +00:00
function _setup() {
2023-03-04 09:57:42 +00:00
# Requires `shopt -s globstar` because of `**` which in
# turn is required as we're decending through directories
for FILE in /usr/local/bin/setup.d/**/*.sh
do
# shellcheck source=/dev/null
source "${FILE}"
done
_log 'info' 'Configuring mail server'
for FUNC in "${FUNCS_SETUP[@]}"
do
${FUNC}
done
# All startup modifications to configs should have taken place before calling this:
_prepare_for_change_detection
}
2023-05-25 23:01:41 +00:00
function _early_supervisor_setup() {
SUPERVISOR_LOGLEVEL="${SUPERVISOR_LOGLEVEL:-warn}"
2023-05-24 07:06:59 +00:00
if ! grep -q "loglevel = ${SUPERVISOR_LOGLEVEL}" /etc/supervisor/supervisord.conf; then
case "${SUPERVISOR_LOGLEVEL}" in
( 'critical' | 'error' | 'info' | 'debug' )
sed -i -E \
"s|(loglevel).*|\1 = ${SUPERVISOR_LOGLEVEL}|g" \
/etc/supervisor/supervisord.conf
supervisorctl reload
exit
;;
( 'warn' ) ;;
( * )
_log 'warn' \
"SUPERVISOR_LOGLEVEL '${SUPERVISOR_LOGLEVEL}' unknown. Using default 'warn'"
;;
esac
fi
return 0
}
2023-05-25 23:01:41 +00:00
function _setup_timezone() {
[[ -n ${TZ} ]] || return 0
_log 'debug' "Setting timezone to '${TZ}'"
local ZONEINFO_FILE="/usr/share/zoneinfo/${TZ}"
2023-05-24 07:06:59 +00:00
if [[ ! -e ${ZONEINFO_FILE} ]]; then
_log 'warn' "Cannot find timezone '${TZ}'"
return 1
fi
if ln -fs "${ZONEINFO_FILE}" /etc/localtime \
&& dpkg-reconfigure -f noninteractive tzdata &>/dev/null
then
_log 'trace' "Set time zone to '${TZ}'"
else
_log 'warn' "Setting timezone to '${TZ}' failed"
return 1
fi
}
2023-05-25 23:01:41 +00:00
function _setup_apply_fixes_after_configuration() {
_log 'trace' 'Removing leftover PID files from a stop/start'
find /var/run/ -not -name 'supervisord.pid' -name '*.pid' -delete
touch /dev/shm/supervisor.sock
_log 'debug' 'Checking /var/mail permissions'
2023-05-24 07:06:59 +00:00
if ! _chown_var_mail_if_necessary; then
_dms_panic__general 'Failed to fix /var/mail permissions'
2023-03-06 09:06:50 +00:00
fi
2023-03-04 09:57:42 +00:00
_log 'debug' 'Removing files and directories from older versions'
rm -rf /var/mail-state/spool-postfix/{dev,etc,lib,pid,usr,private/auth}
}
2023-05-25 23:01:41 +00:00
function _run_user_patches() {
local USER_PATCHES='/tmp/docker-mailserver/user-patches.sh'
2023-05-24 07:06:59 +00:00
if [[ -f ${USER_PATCHES} ]]; then
_log 'debug' 'Applying user patches'
/bin/bash "${USER_PATCHES}"
else
_log 'trace' "No optional '${USER_PATCHES}' provided"
fi
}