docker-mailserver.docker-ma.../target/scripts/helpers/error.sh

106 lines
3.8 KiB
Bash
Raw Normal View History

#!/bin/bash
2023-05-26 01:01:41 +02:00
function _exit_with_error() {
refactor: setup CLI `open-dkim` (#4375) Refactoring this `setup` CLI command as part of the effort to unify our DKIM feature support between OpenDKIM + Rspamd: - Adds a `main()` method similar to other setup CLI commands. - Help text more aligned with equivalent rspamd DKIM setup CLI command. - DRY some repetition such as hard-coded paths to use variables. - OpenDKIM config files are created / initialized early on now with `_create_opendkim_configs()`. `while` loop only needs to append entries, so is easier to grok. - `_create_dkim_key()` to scope just the logic (_and additional notes_) to key generation via `opendkim-genkey` - Now overall logic with the `while` loop of the script occurs in `_generate_dkim_keys()`: - Ownership fixes are now applied after the `while` loop as that seems more appropriate than per iteration. - Temporary VHOST config is now removed since it's no longer useful after running. - Tests adjusted for one new log for adding of default trusted hosts content. Overall this should be nicer to grok/maintain. Some of this logic will be reused for the unified DKIM generation command in future, which is more likely to shift towards all domains using the same keypair by default with rspamd/opendkim config generated at runtime rather than reliant upon DMS config volume to provide that (_still expected for private key_). --------- Co-authored-by: Casper <casperklein@users.noreply.github.com> Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com>
2025-03-31 22:27:28 +13:00
if [[ -n ${1:-} ]]; then
_log 'error' "${1}"
else
_log 'error' "Call to '_exit_with_error' is missing a message to log"
fi
_log 'error' 'Aborting'
exit "${2:-1}"
}
# `dms_panic` methods are appropriate when the type of error is a not recoverable,
# or needs to be very clear to the user about misconfiguration.
#
# Method is called with args:
# PANIC_TYPE => (Internal value for matching). You should use the convenience methods below based on your panic type.
# PANIC_INFO => Provide your own message string to insert into the error message for that PANIC_TYPE.
# PANIC_SCOPE => Optionally provide a string for debugging to better identify/locate the source of the panic.
2023-05-26 01:01:41 +02:00
function dms_panic() {
2023-03-06 10:06:50 +01:00
local PANIC_TYPE=${1:-}
local PANIC_INFO=${2:-}
local PANIC_SCOPE=${3:-}
local SHUTDOWN_MESSAGE
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>
SHUTDOWN_MESSAGE="Environment Variable: ${PANIC_INFO} is not set!"
;;
( 'no-file' ) # PANIC_INFO == <invalid filepath>
SHUTDOWN_MESSAGE="File ${PANIC_INFO} does not exist!"
;;
( 'misconfigured' ) # PANIC_INFO == <something possibly misconfigured, eg an ENV var>
SHUTDOWN_MESSAGE="${PANIC_INFO} appears to be misconfigured, please verify."
;;
( 'invalid-value' ) # PANIC_INFO == <an unsupported or invalid value, eg in a case match>
SHUTDOWN_MESSAGE="Invalid value for ${PANIC_INFO}!"
;;
2023-03-06 10:06:50 +01:00
( 'general' )
SHUTDOWN_MESSAGE=${PANIC_INFO}
;;
( * ) # `dms_panic` was called directly without a valid PANIC_TYPE
SHUTDOWN_MESSAGE='Something broke :('
;;
esac
2023-05-24 09:06:59 +02:00
if [[ -n ${PANIC_SCOPE:-} ]]; then
_shutdown "${PANIC_SCOPE} | ${SHUTDOWN_MESSAGE}"
else
_shutdown "${SHUTDOWN_MESSAGE}"
fi
}
# Convenience wrappers based on type:
2023-03-06 10:06:50 +01:00
function _dms_panic__fail_init { dms_panic 'fail-init' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__no_env { dms_panic 'no-env' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__no_file { dms_panic 'no-file' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__misconfigured { dms_panic 'misconfigured' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__invalid_value { dms_panic 'invalid-value' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__general { dms_panic 'general' "${1:-}" "${2:-}" "${3:-}" ; }
# 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.
trap "exit 1" SIGUSR1
SCRIPT_PID=${$}
2023-05-26 01:01:41 +02:00
function _shutdown() {
2023-03-06 10:06:50 +01:00
_log 'error' "${1:-_shutdown called without message}"
_log 'error' 'Shutting down'
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 10:17:08 +01:00
sleep 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.
}
# 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.
2023-05-26 01:01:41 +02:00
function _trap_err_signal() {
trap '__log_unexpected_error "${FUNCNAME[0]:-}" "${BASH_COMMAND:-}" "${LINENO:-}" "${?:-}"' ERR
# shellcheck disable=SC2317
2023-05-26 01:01:41 +02:00
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
}
}