diff --git a/target/scripts/helpers/dns.sh b/target/scripts/helpers/dns.sh index 4301fa7d..0a600959 100644 --- a/target/scripts/helpers/dns.sh +++ b/target/scripts/helpers/dns.sh @@ -41,7 +41,7 @@ function _obtain_hostname_and_domainname # of this variable or if a more deterministic approach with `cut` should be relied on. if [[ $(_get_label_count "${HOSTNAME}") -gt 2 ]] then - if [[ -n ${OVERRIDE_HOSTNAME} ]] + if [[ -n ${OVERRIDE_HOSTNAME:-} ]] then # Emulates the intended behaviour of `hostname -d`: # Assign the HOSTNAME value minus everything up to and including the first `.` diff --git a/target/scripts/helpers/error.sh b/target/scripts/helpers/error.sh index e5662b22..d28d5341 100644 --- a/target/scripts/helpers/error.sh +++ b/target/scripts/helpers/error.sh @@ -87,3 +87,26 @@ function _shutdown kill -SIGTERM 1 # Trigger graceful DMS shutdown. kill -SIGUSR1 "${SCRIPT_PID}" # Stop start-mailserver.sh execution, even when _shutdown() is called from a subshell. } + +# Calling this function sets up a handler for the `ERR` signal, that occurs when +# an error is not properly checked (e.g., in an `if`-clause or in an `&&` block). +# +# This is mostly useful for debugging. It also helps when using something like `set -eE`, +# as it shows where the script aborts. +function _trap_err_signal +{ + trap '__log_unexpected_error "${FUNCNAME[0]:-}" "${BASH_COMMAND:-}" "${LINENO:-}" "${?:-}"' ERR + + # shellcheck disable=SC2317 + function __log_unexpected_error + { + local MESSAGE="Unexpected error occured :: script = ${SCRIPT:-${0}} " + MESSAGE+=" | function = ${1:-none (global)}" + MESSAGE+=" | command = ${2:-?}" + MESSAGE+=" | line = ${3:-?}" + MESSAGE+=" | exit code = ${4:-?}" + + _log 'error' "${MESSAGE}" + return 0 + } +} diff --git a/target/scripts/helpers/log.sh b/target/scripts/helpers/log.sh index 80d8c4b0..925df2e6 100644 --- a/target/scripts/helpers/log.sh +++ b/target/scripts/helpers/log.sh @@ -123,9 +123,9 @@ function _get_log_level_or_default if [[ -n ${LOG_LEVEL+set} ]] then echo "${LOG_LEVEL}" - elif [[ -e /etc/dms-settings ]] + elif [[ -e /etc/dms-settings ]] && grep -q -E "^LOG_LEVEL='[a-z]+'" /etc/dms-settings then - grep "^LOG_LEVEL=" /etc/dms-settings | cut -d "'" -f 2 + grep '^LOG_LEVEL=' /etc/dms-settings | cut -d "'" -f 2 else echo 'info' fi diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index b9b36f64..e06ff837 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -16,7 +16,13 @@ function _get_valid_lines_from_file # and it will return its value stored in /etc/dms-settings function _get_dms_env_value { - grep "^${1}=" /etc/dms-settings | cut -d "'" -f 2 + if [[ -f /etc/dms-settings ]] + then + grep "^${1}=" /etc/dms-settings | cut -d "'" -f 2 + else + _log 'warn' "Call to '_get_dms_env_value' but '/etc/dms-settings' is not present" + return 1 + fi } # TODO: `chown -R 5000:5000 /var/mail` has existed since the projects first commit. @@ -33,6 +39,7 @@ function _chown_var_mail_if_necessary _log 'trace' 'Fixing /var/mail permissions' chown -R 5000:5000 /var/mail || return 1 fi + return 0 } function _require_n_parameters_or_print_usage @@ -43,6 +50,7 @@ function _require_n_parameters_or_print_usage [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } [[ ${#} -lt ${COUNT} ]] && { __usage ; exit 1 ; } + return 0 } # NOTE: Postfix commands that read `main.cf` will stall execution, @@ -56,6 +64,7 @@ function _adjust_mtime_for_postfix_maincf then touch -d '2 seconds ago' /etc/postfix/main.cf fi + return 0 } function _reload_postfix