scripts: improve shutdown function by making PANIC_STRATEGY obsolete (#3265)

This commit is contained in:
Casper 2023-04-18 23:38:46 +02:00 committed by GitHub
parent a735dddc52
commit ea07bcdb4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 19 additions and 28 deletions

View File

@ -144,7 +144,7 @@ function _create_dovecot_alias_dummy_accounts
if [[ -z ${REAL_ACC[1]} ]] if [[ -z ${REAL_ACC[1]} ]]
then then
_dms_panic__misconfigured 'postfix-accounts.cf' 'alias configuration' 'immediate' _dms_panic__misconfigured 'postfix-accounts.cf' 'alias configuration'
fi fi
# test if user has a defined quota # test if user has a defined quota

View File

@ -24,8 +24,7 @@ function dms_panic
{ {
local PANIC_TYPE=${1:-} local PANIC_TYPE=${1:-}
local PANIC_INFO=${2:-} local PANIC_INFO=${2:-}
local PANIC_SCOPE=${3-} # optional, must not be :- but just - local PANIC_SCOPE=${3:-}
local PANIC_STRATEGY=${4:-} # optional
local SHUTDOWN_MESSAGE local SHUTDOWN_MESSAGE
@ -61,9 +60,9 @@ function dms_panic
if [[ -n ${PANIC_SCOPE:-} ]] if [[ -n ${PANIC_SCOPE:-} ]]
then then
_shutdown "${PANIC_SCOPE} | ${SHUTDOWN_MESSAGE}" "${PANIC_STRATEGY}" _shutdown "${PANIC_SCOPE} | ${SHUTDOWN_MESSAGE}"
else else
_shutdown "${SHUTDOWN_MESSAGE}" "${PANIC_STRATEGY}" _shutdown "${SHUTDOWN_MESSAGE}"
fi fi
} }
@ -77,22 +76,14 @@ function _dms_panic__general { dms_panic 'general' "${1:-}" "${2:-}"
# Call this method when you want to panic (i.e. emit an 'ERROR' log, and exit uncleanly). # Call this method when you want to panic (i.e. emit an 'ERROR' log, and exit uncleanly).
# `dms_panic` methods should be preferred if your failure type is supported. # `dms_panic` methods should be preferred if your failure type is supported.
trap "exit 1" SIGUSR1
SCRIPT_PID=${$}
function _shutdown function _shutdown
{ {
_log 'error' "${1:-_shutdown called without message}" _log 'error' "${1:-_shutdown called without message}"
_log 'error' 'Shutting down' _log 'error' 'Shutting down'
sleep 1 sleep 1
kill 1 kill -SIGTERM 1 # Trigger graceful DMS shutdown.
kill -SIGUSR1 "${SCRIPT_PID}" # Stop start-mailserver.sh execution, even when _shutdown() is called from a subshell.
if [[ ${2:-wait} == 'immediate' ]]
then
# In case the user requested an immediate exit, he ensure he is not in a subshell
# call and exiting the whole script is safe. This way, we make the shutdown quicker.
exit 1
else
# We can simply wait until Supervisord has terminated all processes; this way,
# we do not return from a subshell call and continue as if nothing happened.
sleep 1000
fi
} }

View File

@ -92,13 +92,13 @@ function _replace_by_env_in_file
{ {
if [[ -z ${1+set} ]] if [[ -z ${1+set} ]]
then then
_dms_panic__invalid_value 'first argument unset' 'utils.sh:_replace_by_env_in_file' 'immediate' _dms_panic__invalid_value 'first argument unset' 'utils.sh:_replace_by_env_in_file'
elif [[ -z ${2+set} ]] elif [[ -z ${2+set} ]]
then then
_dms_panic__invalid_value 'second argument unset' 'utils.sh:_replace_by_env_in_file' 'immediate' _dms_panic__invalid_value 'second argument unset' 'utils.sh:_replace_by_env_in_file'
elif [[ ! -f ${2} ]] elif [[ ! -f ${2} ]]
then then
_dms_panic__invalid_value "file '${2}' does not exist" 'utils.sh:_replace_by_env_in_file' 'immediate' _dms_panic__invalid_value "file '${2}' does not exist" 'utils.sh:_replace_by_env_in_file'
fi fi
local ENV_PREFIX=${1} CONFIG_FILE=${2} local ENV_PREFIX=${1} CONFIG_FILE=${2}

View File

@ -61,11 +61,11 @@ function _register_functions
;; ;;
( 'OIDC' ) ( 'OIDC' )
_dms_panic__fail_init 'OIDC user account provisioning - it is not yet implemented' '' 'immediate' _dms_panic__fail_init 'OIDC user account provisioning - it is not yet implemented'
;; ;;
( * ) ( * )
_dms_panic__invalid_value "'${ACCOUNT_PROVISIONER}' is not a valid value for ACCOUNT_PROVISIONER" '' 'immediate' _dms_panic__invalid_value "'${ACCOUNT_PROVISIONER}' is not a valid value for ACCOUNT_PROVISIONER"
;; ;;
esac esac

View File

@ -38,7 +38,7 @@ function _check_hostname
# HOSTNAME should be an FQDN (eg: hostname.domain) # HOSTNAME should be an FQDN (eg: hostname.domain)
if ! grep -q -E '^(\S+[.]\S+)$' <<< "${HOSTNAME}" if ! grep -q -E '^(\S+[.]\S+)$' <<< "${HOSTNAME}"
then then
_dms_panic__general 'Setting hostname/domainname is required' '' 'immediate' _dms_panic__general 'Setting hostname/domainname is required'
fi fi
} }

View File

@ -29,7 +29,7 @@ function _default_start_daemon
if [[ ${?} -ne 0 ]] if [[ ${?} -ne 0 ]]
then then
_log 'error' "${RESULT}" _log 'error' "${RESULT}"
_dms_panic__fail_init "${1}" '' 'immediate' _dms_panic__fail_init "${1}"
fi fi
} }

View File

@ -89,7 +89,7 @@ function _setup_apply_fixes_after_configuration
_log 'debug' 'Checking /var/mail permissions' _log 'debug' 'Checking /var/mail permissions'
if ! _chown_var_mail_if_necessary if ! _chown_var_mail_if_necessary
then then
_dms_panic__general 'Failed to fix /var/mail permissions' '' 'immediate' _dms_panic__general 'Failed to fix /var/mail permissions'
fi fi
_log 'debug' 'Removing files and directories from older versions' _log 'debug' 'Removing files and directories from older versions'

View File

@ -175,7 +175,7 @@ function _setup_dovecot_local_user
fi fi
done done
_dms_panic__fail_init 'accounts provisioning because no accounts were provided - Dovecot could not be started' '' 'immediate' _dms_panic__fail_init 'accounts provisioning because no accounts were provided - Dovecot could not be started'
} }
__wait_until_an_account_is_added_or_shutdown __wait_until_an_account_is_added_or_shutdown
@ -199,7 +199,7 @@ function _setup_dovecot_inet_protocols
PROTOCOL='[::]' # IPv6 only PROTOCOL='[::]' # IPv6 only
else else
# Unknown value, panic. # Unknown value, panic.
_dms_panic__invalid_value 'DOVECOT_INET_PROTOCOLS' "${DOVECOT_INET_PROTOCOLS}" 'immediate' _dms_panic__invalid_value 'DOVECOT_INET_PROTOCOLS' "${DOVECOT_INET_PROTOCOLS}"
fi fi
sedfile -i "s|^#listen =.*|listen = ${PROTOCOL}|g" /etc/dovecot/dovecot.conf sedfile -i "s|^#listen =.*|listen = ${PROTOCOL}|g" /etc/dovecot/dovecot.conf

View File

@ -22,7 +22,7 @@ function _setup_docker_permit
if [[ -z ${CONTAINER_IP} ]] if [[ -z ${CONTAINER_IP} ]]
then then
_log 'error' 'Detecting the container IP address failed' _log 'error' 'Detecting the container IP address failed'
_dms_panic__misconfigured 'NETWORK_INTERFACE' 'Network Setup [docker_permit]' 'immediate' _dms_panic__misconfigured 'NETWORK_INTERFACE' 'Network Setup [docker_permit]'
fi fi
while read -r IP while read -r IP