Files
docker-fence/lib/envcheck.sh
T
2026-12-05 15:32:27 +00:00

82 lines
2.4 KiB
Bash

# shellcheck shell=bash
# Environment checks. Nothing is assumed about the exact output of the
# tools: only simple patterns are looked for.
# First line of the output of a command, or "?".
fence_first_line() {
local out
out="$("$@" 2>/dev/null | head -n 1)" || true
printf '%s' "${out:-?}"
}
fence_env_versions() {
local docker_v="?" ipt_v="?" ufw_v="?"
if fence_have bin_docker; then
docker_v="$(fence_first_line "${CFG[bin_docker]}" version --format '{{.Server.Version}}')"
fi
if fence_have bin_iptables; then
ipt_v="$(fence_first_line "${CFG[bin_iptables]}" --version)"
fi
if fence_have bin_ufw; then
ufw_v="$(fence_first_line "${CFG[bin_ufw]}" version)"
fi
fence_info i_versions "${BASH_VERSION}" "$docker_v" "$ipt_v" "$ufw_v"
}
# Reads daemon.json without a JSON parser: "key: value" patterns.
# Returns 2 if the environment is not supported.
fence_env_daemon_json() {
local f content
f="$(fence_path "${CFG[docker_daemon_json]}")"
[[ -r "$f" ]] || return 0
content="$(tr -d '\n\r\t ' <"$f")"
if [[ "$content" =~ \"iptables\":false ]]; then
fence_error e_docker_no_iptables "$f"
return 2
fi
if [[ "$content" =~ \"firewall-backend\":\"nftables\" ]]; then
fence_error e_docker_nftables "$f"
return 2
fi
if [[ "$content" =~ \"ipv6\":true ]]; then
fence_warn w_ipv6 "$f"
fi
return 0
}
fence_env_networks() {
fence_have bin_docker || return 0
local drv nets
for drv in macvlan ipvlan; do
nets="$("${CFG[bin_docker]}" network ls --filter "driver=$drv" --format '{{.Name}}' 2>/dev/null | tr '\n' ' ')" || true
nets="${nets% }"
if [[ -n "$nets" ]]; then
fence_warn w_macvlan "$drv" "$nets"
fi
done
}
# Binaries required to change the firewall (apply, rollback...).
fence_env_require_bins() {
local k missing=0
for k in "$@"; do
if ! fence_have "$k"; then
fence_error e_missing_bin "${CFG[$k]}"
missing=1
fi
done
(( missing == 0 ))
}
# Full checks. Returns 2 if the environment is not supported.
fence_env_check() {
fence_env_versions
local k
for k in bin_iptables bin_ufw bin_docker; do
fence_have "$k" || fence_warn w_missing_bin "${CFG[$k]}"
done
fence_env_daemon_json || return 2
fence_env_networks
return 0
}