76 lines
1.7 KiB
Bash
76 lines
1.7 KiB
Bash
# shellcheck shell=bash
|
|
# Common functions: output, log, paths, running binaries.
|
|
|
|
# Output level: 0 quiet, 1 normal, 2 verbose. Messages go to standard
|
|
# error; standard output only carries data (generated block, report,
|
|
# plan, configuration).
|
|
FENCE_VERBOSITY=1
|
|
|
|
# Exit codes (see README).
|
|
readonly FENCE_EXIT_OK=0
|
|
readonly FENCE_EXIT_CONFIG=1
|
|
readonly FENCE_EXIT_ENV=2
|
|
readonly FENCE_EXIT_LEGACY=3
|
|
readonly FENCE_EXIT_APPLY=4
|
|
|
|
# Path prefix reserved for tests (FENCE_ROOT).
|
|
fence_path() {
|
|
printf '%s%s' "${FENCE_ROOT:-}" "$1"
|
|
}
|
|
|
|
fence_info() {
|
|
if (( FENCE_VERBOSITY >= 1 )); then
|
|
# shellcheck disable=SC2059
|
|
printf "$(fence_fmt "$1")\n" "${@:2}" >&2
|
|
fi
|
|
}
|
|
|
|
fence_detail() {
|
|
if (( FENCE_VERBOSITY >= 2 )); then
|
|
# shellcheck disable=SC2059
|
|
printf "$(fence_fmt "$1")\n" "${@:2}" >&2
|
|
fi
|
|
}
|
|
|
|
fence_warn() {
|
|
# shellcheck disable=SC2059
|
|
printf "%s $(fence_fmt "$1")\n" "$(fence_fmt w_prefix)" "${@:2}" >&2
|
|
}
|
|
|
|
fence_error() {
|
|
# shellcheck disable=SC2059
|
|
printf "%s $(fence_fmt "$1")\n" "$(fence_fmt e_prefix)" "${@:2}" >&2
|
|
}
|
|
|
|
# fence_die CODE KEY [ARGS...]
|
|
fence_die() {
|
|
local code="$1"
|
|
shift
|
|
fence_error "$@"
|
|
exit "$code"
|
|
}
|
|
|
|
# Log of the actions of the tool (never a secret: only paths, command
|
|
# names and states are written).
|
|
fence_log() {
|
|
local file
|
|
file="$(fence_path "${CFG[log_file]}")"
|
|
local line
|
|
line="$(date '+%Y-%m-%dT%H:%M:%S%z') $*"
|
|
if ! { printf '%s\n' "$line" >>"$file"; } 2>/dev/null; then
|
|
fence_detail i_log_unwritable "$file"
|
|
fi
|
|
}
|
|
|
|
# Runs a configured binary: fence_run bin_ufw reload
|
|
fence_run() {
|
|
local key="$1"
|
|
shift
|
|
fence_detail i_running "${CFG[$key]} $*"
|
|
"${CFG[$key]}" "$@"
|
|
}
|
|
|
|
fence_have() {
|
|
command -v "${CFG[$1]}" >/dev/null 2>&1
|
|
}
|