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

124 lines
2.9 KiB
Bash
Raw Normal View History

#!/bin/bash
declare -a FUNCS_SETUP
function _register_setup_function
{
FUNCS_SETUP+=("${1}")
_log 'trace' "${1}() registered"
}
function _setup
{
# 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
}
function _early_setup_supervisor
{
SUPERVISOR_LOGLEVEL="${SUPERVISOR_LOGLEVEL:-warn}"
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
}
# File/folder permissions are fine when using docker volumes, but may be wrong
# when file system folders are mounted into the container.
# Set the expected values and create missing folders/files just in case.
function _setup_file_permissions
{
scripts: refactoring & miscellaneous small changes (#2499) * `update-check.sh` now uses the new log * refactored `setup-stack.sh` The changes are: 1. Replaced `""` wiht `''` where possible (reasoning: Bash is very implicit and I'd like to use `''` where possible to indicate no variables are expanded here) 2. `> /file` -> `>/file` according to our style guide 3. Some log adjustments for messages where I deemed it appropriate 4. Then, an error message from a Dovecot setup was also prevented (by adding a check whether the directory is present before a `: >...` command would create a file in this directory). These are all small, miscellaneous changes that I wanted to combine into one commit and ultimately one PR because I see no point in opening a PR for every small change here. I hope this is fine. * added a small `sleep` to the `_shutdown` function This ensure the last log message is actually logged before Supervisor logs the message that it received a SIGTERM. This makes reading the log easier because now the causal relationship is shown (we are terminating Supervisor, and not someone else and we're just logging it). I forgot to replace `""` with `''` in `update-check.sh`, so I included it here because this is the last commit before PR review. * re-add exit on successful update (only) * re-added date information to update-check log messages * added `_log_with_date` function The new function will log a message with a proper timestamp. This is all handled in `log.sh`, we therefore not need to source other files too. This will be used in the future by `check-for-changes.sh` as well :) Co-authored-by: Casper <casperklein@users.noreply.github.com> Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
2022-03-26 09:17:08 +00:00
_log 'debug' 'Setting file and directory permissions'
mkdir -p /var/log/supervisor
mkdir -p /var/log/mail
chown syslog:root /var/log/mail
touch /var/log/mail/clamav.log
chown clamav:adm /var/log/mail/clamav.log
chmod 640 /var/log/mail/clamav.log
touch /var/log/mail/freshclam.log
chown clamav:adm /var/log/mail/freshclam.log
chmod 640 /var/log/mail/freshclam.log
}
function _setup_run_user_patches
{
scripts: refactoring & miscellaneous small changes (#2499) * `update-check.sh` now uses the new log * refactored `setup-stack.sh` The changes are: 1. Replaced `""` wiht `''` where possible (reasoning: Bash is very implicit and I'd like to use `''` where possible to indicate no variables are expanded here) 2. `> /file` -> `>/file` according to our style guide 3. Some log adjustments for messages where I deemed it appropriate 4. Then, an error message from a Dovecot setup was also prevented (by adding a check whether the directory is present before a `: >...` command would create a file in this directory). These are all small, miscellaneous changes that I wanted to combine into one commit and ultimately one PR because I see no point in opening a PR for every small change here. I hope this is fine. * added a small `sleep` to the `_shutdown` function This ensure the last log message is actually logged before Supervisor logs the message that it received a SIGTERM. This makes reading the log easier because now the causal relationship is shown (we are terminating Supervisor, and not someone else and we're just logging it). I forgot to replace `""` with `''` in `update-check.sh`, so I included it here because this is the last commit before PR review. * re-add exit on successful update (only) * re-added date information to update-check log messages * added `_log_with_date` function The new function will log a message with a proper timestamp. This is all handled in `log.sh`, we therefore not need to source other files too. This will be used in the future by `check-for-changes.sh` as well :) Co-authored-by: Casper <casperklein@users.noreply.github.com> Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
2022-03-26 09:17:08 +00:00
local USER_PATCHES='/tmp/docker-mailserver/user-patches.sh'
if [[ -f ${USER_PATCHES} ]]
then
scripts: refactoring & miscellaneous small changes (#2499) * `update-check.sh` now uses the new log * refactored `setup-stack.sh` The changes are: 1. Replaced `""` wiht `''` where possible (reasoning: Bash is very implicit and I'd like to use `''` where possible to indicate no variables are expanded here) 2. `> /file` -> `>/file` according to our style guide 3. Some log adjustments for messages where I deemed it appropriate 4. Then, an error message from a Dovecot setup was also prevented (by adding a check whether the directory is present before a `: >...` command would create a file in this directory). These are all small, miscellaneous changes that I wanted to combine into one commit and ultimately one PR because I see no point in opening a PR for every small change here. I hope this is fine. * added a small `sleep` to the `_shutdown` function This ensure the last log message is actually logged before Supervisor logs the message that it received a SIGTERM. This makes reading the log easier because now the causal relationship is shown (we are terminating Supervisor, and not someone else and we're just logging it). I forgot to replace `""` with `''` in `update-check.sh`, so I included it here because this is the last commit before PR review. * re-add exit on successful update (only) * re-added date information to update-check log messages * added `_log_with_date` function The new function will log a message with a proper timestamp. This is all handled in `log.sh`, we therefore not need to source other files too. This will be used in the future by `check-for-changes.sh` as well :) Co-authored-by: Casper <casperklein@users.noreply.github.com> Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
2022-03-26 09:17:08 +00:00
_log 'debug' 'Applying user patches'
/bin/bash "${USER_PATCHES}"
else
scripts: refactoring & miscellaneous small changes (#2499) * `update-check.sh` now uses the new log * refactored `setup-stack.sh` The changes are: 1. Replaced `""` wiht `''` where possible (reasoning: Bash is very implicit and I'd like to use `''` where possible to indicate no variables are expanded here) 2. `> /file` -> `>/file` according to our style guide 3. Some log adjustments for messages where I deemed it appropriate 4. Then, an error message from a Dovecot setup was also prevented (by adding a check whether the directory is present before a `: >...` command would create a file in this directory). These are all small, miscellaneous changes that I wanted to combine into one commit and ultimately one PR because I see no point in opening a PR for every small change here. I hope this is fine. * added a small `sleep` to the `_shutdown` function This ensure the last log message is actually logged before Supervisor logs the message that it received a SIGTERM. This makes reading the log easier because now the causal relationship is shown (we are terminating Supervisor, and not someone else and we're just logging it). I forgot to replace `""` with `''` in `update-check.sh`, so I included it here because this is the last commit before PR review. * re-add exit on successful update (only) * re-added date information to update-check log messages * added `_log_with_date` function The new function will log a message with a proper timestamp. This is all handled in `log.sh`, we therefore not need to source other files too. This will be used in the future by `check-for-changes.sh` as well :) Co-authored-by: Casper <casperklein@users.noreply.github.com> Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
2022-03-26 09:17:08 +00:00
_log 'trace' "No optional '${USER_PATCHES}' provided"
fi
}
function _setup_timezone
{
[[ -n ${TZ} ]] || return 0
_log 'debug' "Setting timezone to '${TZ}'"
local ZONEINFO_FILE="/usr/share/zoneinfo/${TZ}"
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
}
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'
_chown_var_mail_if_necessary || _shutdown 'Failed to fix /var/mail permissions'
}