function _defunc removed (#2199)

* function _defunc removed

* _shutdown is better than just notify in that cases

* PANIC_TYPE 'fail-init' introduced

Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com>
This commit is contained in:
Casper 2021-09-23 19:49:07 +02:00 committed by GitHub
parent 6715e0bba9
commit c7e9dd2bad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 44 deletions

View File

@ -21,13 +21,17 @@ function errex
# PANIC_SCOPE => Optionally provide a string for debugging to better identify/locate the source of the panic. # PANIC_SCOPE => Optionally provide a string for debugging to better identify/locate the source of the panic.
function dms_panic function dms_panic
{ {
local PANIC_TYPE=$1 local PANIC_TYPE=${1}
local PANIC_INFO=$2 local PANIC_INFO=${2}
local PANIC_SCOPE=$3 #optional local PANIC_SCOPE=${3} #optional
local SHUTDOWN_MESSAGE local SHUTDOWN_MESSAGE
case "${PANIC_TYPE}" in case "${PANIC_TYPE}" in
( 'fail-init' ) # PANIC_INFO == <name of service or process that failed to start / initialize>
SHUTDOWN_MESSAGE="Failed to start ${PANIC_INFO}!"
;;
( 'no-env' ) # PANIC_INFO == <ENV VAR name> ( 'no-env' ) # PANIC_INFO == <ENV VAR name>
SHUTDOWN_MESSAGE="Environment Variable: ${PANIC_INFO} is not set!" SHUTDOWN_MESSAGE="Environment Variable: ${PANIC_INFO} is not set!"
;; ;;
@ -58,6 +62,7 @@ function dms_panic
} }
# Convenience wrappers based on type: # Convenience wrappers based on type:
function dms_panic__fail_init { dms_panic 'fail-init' "${1}" "${2}"; }
function dms_panic__no_env { dms_panic 'no-env' "${1}" "${2}"; } function dms_panic__no_env { dms_panic 'no-env' "${1}" "${2}"; }
function dms_panic__no_file { dms_panic 'no-file' "${1}" "${2}"; } function dms_panic__no_file { dms_panic 'no-file' "${1}" "${2}"; }
function dms_panic__misconfigured { dms_panic 'misconfigured' "${1}" "${2}"; } function dms_panic__misconfigured { dms_panic 'misconfigured' "${1}" "${2}"; }

View File

@ -217,12 +217,6 @@ function _register_misc_function
_notify 'inf' "${1}() registered" _notify 'inf' "${1}() registered"
} }
function _defunc
{
_notify 'fatal' 'Please fix your configuration. Exiting...'
exit 1
}
# ------------------------------------------------------------ # ------------------------------------------------------------
# ? << Registering functions # ? << Registering functions
# -- # --

View File

@ -5,7 +5,7 @@ function check
_notify 'tasklog' 'Checking configuration' _notify 'tasklog' 'Checking configuration'
for FUNC in "${FUNCS_CHECK[@]}" for FUNC in "${FUNCS_CHECK[@]}"
do do
${FUNC} || _defunc ${FUNC}
done done
} }
@ -20,6 +20,5 @@ function _check_hostname
if ! grep -q -E '^(\S+[.]\S+)$' <<< "${HOSTNAME}" if ! grep -q -E '^(\S+[.]\S+)$' <<< "${HOSTNAME}"
then then
_shutdown 'Setting hostname/domainname is required' _shutdown 'Setting hostname/domainname is required'
return 1
fi fi
} }

View File

@ -5,31 +5,31 @@ function start_daemons
_notify 'tasklog' 'Starting daemons & mail server' _notify 'tasklog' 'Starting daemons & mail server'
for FUNC in "${DAEMONS_START[@]}" for FUNC in "${DAEMONS_START[@]}"
do do
${FUNC} || _defunc ${FUNC}
done done
} }
function _start_daemons_cron function _start_daemons_cron
{ {
_notify 'task' 'Starting cron' _notify 'task' 'Starting cron'
supervisorctl start cron supervisorctl start cron || dms_panic__fail_init 'cron'
} }
function _start_daemons_rsyslog function _start_daemons_rsyslog
{ {
_notify 'task' 'Starting rsyslog' _notify 'task' 'Starting rsyslog'
supervisorctl start rsyslog supervisorctl start rsyslog || dms_panic__fail_init 'rsyslog'
} }
function _start_daemons_saslauthd function _start_daemons_saslauthd
{ {
_notify 'task' 'Starting saslauthd' _notify 'task' 'Starting saslauthd'
supervisorctl start "saslauthd_${SASLAUTHD_MECHANISMS}" supervisorctl start "saslauthd_${SASLAUTHD_MECHANISMS}" || dms_panic__fail_init 'saslauthd'
} }
function _start_daemons_fail2ban function _start_daemons_fail2ban
{ {
_notify 'task' 'Starting fail2ban' _notify 'task' 'Starting Fail2ban'
touch /var/log/auth.log touch /var/log/auth.log
# delete fail2ban.sock that probably was left here after container restart # delete fail2ban.sock that probably was left here after container restart
@ -38,31 +38,31 @@ function _start_daemons_fail2ban
rm /var/run/fail2ban/fail2ban.sock rm /var/run/fail2ban/fail2ban.sock
fi fi
supervisorctl start fail2ban supervisorctl start fail2ban || dms_panic__fail_init 'Fail2ban'
} }
function _start_daemons_opendkim function _start_daemons_opendkim
{ {
_notify 'task' 'Starting opendkim' _notify 'task' 'Starting opendkim'
supervisorctl start opendkim supervisorctl start opendkim || dms_panic__fail_init 'opendkim'
} }
function _start_daemons_opendmarc function _start_daemons_opendmarc
{ {
_notify 'task' 'Starting opendmarc' _notify 'task' 'Starting opendmarc'
supervisorctl start opendmarc supervisorctl start opendmarc || dms_panic__fail_init 'opendmarc'
} }
function _start_daemons_postsrsd function _start_daemons_postsrsd
{ {
_notify 'task' 'Starting postsrsd' _notify 'task' 'Starting postsrsd'
supervisorctl start postsrsd supervisorctl start postsrsd || dms_panic__fail_init 'postsrsd'
} }
function _start_daemons_postfix function _start_daemons_postfix
{ {
_notify 'task' 'Starting postfix' _notify 'task' 'Starting postfix'
supervisorctl start postfix supervisorctl start postfix || dms_panic__fail_init 'postfix'
} }
function _start_daemons_dovecot function _start_daemons_dovecot
@ -81,7 +81,7 @@ function _start_daemons_dovecot
cp /tmp/docker-mailserver/dovecot.cf /etc/dovecot/local.conf cp /tmp/docker-mailserver/dovecot.cf /etc/dovecot/local.conf
fi fi
supervisorctl start dovecot supervisorctl start dovecot || dms_panic__fail_init 'dovecot'
} }
function _start_daemons_fetchmail function _start_daemons_fetchmail
@ -120,41 +120,41 @@ EOF
do do
COUNTER=$(( COUNTER + 1 )) COUNTER=$(( COUNTER + 1 ))
_notify 'task' "Starting fetchmail instance ${COUNTER}" _notify 'task' "Starting fetchmail instance ${COUNTER}"
supervisorctl start "fetchmail-${COUNTER}" supervisorctl start "fetchmail-${COUNTER}" || _panic__fail_init "fetchmail-${COUNTER}"
done done
else else
_notify 'task' 'Starting fetchmail' _notify 'task' 'Starting fetchmail'
supervisorctl start fetchmail supervisorctl start fetchmail || dms_panic__fail_init 'fetchmail'
fi fi
} }
function _start_daemons_clamav function _start_daemons_clamav
{ {
_notify 'task' 'Starting clamav' _notify 'task' 'Starting clamav'
supervisorctl start clamav supervisorctl start clamav || dms_panic__fail_init 'ClamAV'
} }
function _start_daemons_postgrey function _start_daemons_postgrey
{ {
_notify 'task' 'Starting postgrey' _notify 'task' 'Starting postgrey'
rm -f /var/run/postgrey/postgrey.pid rm -f /var/run/postgrey/postgrey.pid
supervisorctl start postgrey supervisorctl start postgrey || dms_panic__fail_init 'postgrey'
} }
function _start_daemons_amavis function _start_daemons_amavis
{ {
_notify 'task' 'Starting amavis' _notify 'task' 'Starting amavis'
supervisorctl start amavis supervisorctl start amavis || dms_panic__fail_init 'amavis'
} }
function _start_changedetector function _start_changedetector
{ {
_notify 'task' 'Starting changedetector' _notify 'task' 'Starting changedetector'
supervisorctl start changedetector supervisorctl start changedetector || dms_panic__fail_init 'changedetector'
} }
function _start_daemons_update_check function _start_daemons_update_check
{ {
_notify 'task' 'Starting update-check' _notify 'task' 'Starting update-check'
supervisorctl start update-check supervisorctl start update-check || dms_panic__fail_init 'update-check'
} }

View File

@ -5,7 +5,7 @@ function fix
_notify 'tasklog' 'Post-configuration checks' _notify 'tasklog' 'Post-configuration checks'
for FUNC in "${FUNCS_FIX[@]}" for FUNC in "${FUNCS_FIX[@]}"
do do
${FUNC} || _defunc ${FUNC}
done done
_notify 'inf' 'Removing leftover PID files from a stop/start' _notify 'inf' 'Removing leftover PID files from a stop/start'
@ -21,12 +21,10 @@ function _fix_var_mail_permissions
if find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | read -r if find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | read -r
then then
_notify 'inf' 'Fixing /var/mail permissions' _notify 'inf' 'Fixing /var/mail permissions'
chown -R 5000:5000 /var/mail chown -R 5000:5000 /var/mail || _shutdown 'Failed to fix /var/mail permissions'
else else
_notify 'inf' 'Permissions in /var/mail look OK' _notify 'inf' 'Permissions in /var/mail look OK'
fi fi
return 0
} }
function _fix_var_amavis_permissions function _fix_var_amavis_permissions
@ -35,21 +33,18 @@ function _fix_var_amavis_permissions
[[ ${ONE_DIR} -eq 0 ]] && AMAVIS_STATE_DIR="/var/lib/amavis" [[ ${ONE_DIR} -eq 0 ]] && AMAVIS_STATE_DIR="/var/lib/amavis"
[[ ! -e ${AMAVIS_STATE_DIR} ]] && return 0 [[ ! -e ${AMAVIS_STATE_DIR} ]] && return 0
_notify 'inf' 'Checking and fixing Amavis permissions' _notify 'inf' 'Fixing Amavis permissions'
chown -hR amavis:amavis "${AMAVIS_STATE_DIR}" chown -hR amavis:amavis "${AMAVIS_STATE_DIR}" || _shutdown 'Failed to fix Amavis permissions'
return 0
} }
function _fix_cleanup_clamav function _fix_cleanup_clamav
{ {
_notify 'task' 'Cleaning up disabled Clamav' _notify 'task' 'Cleaning up disabled ClamAV'
rm -f /etc/logrotate.d/clamav-* rm /etc/logrotate.d/clamav-* /etc/cron.d/clamav-freshclam || _shutdown 'Failed to remove ClamAV configuration'
rm -f /etc/cron.d/clamav-freshclam
} }
function _fix_cleanup_spamassassin function _fix_cleanup_spamassassin
{ {
_notify 'task' 'Cleaning up disabled SpamAssassin' _notify 'task' 'Cleaning up disabled SpamAssassin'
rm -f /etc/cron.daily/spamassassin rm /etc/cron.daily/spamassassin || _shutdown 'Failed to remove SpamAssassin configuration'
} }

View File

@ -5,7 +5,7 @@ function start_misc
_notify 'inf' 'Starting miscellaneous tasks' _notify 'inf' 'Starting miscellaneous tasks'
for FUNC in "${FUNCS_MISC[@]}" for FUNC in "${FUNCS_MISC[@]}"
do do
${FUNC} || _defunc ${FUNC}
done done
} }

View File

@ -373,8 +373,7 @@ function _setup_dovecot_local_user
then then
if [[ ${ENABLE_LDAP} -eq 0 ]] if [[ ${ENABLE_LDAP} -eq 0 ]]
then then
_notify 'fatal' 'Unless using LDAP, you need at least 1 email account to start Dovecot.' _shutdown 'Unless using LDAP, you need at least 1 email account to start Dovecot.'
_defunc
fi fi
fi fi
} }