52 lines
1.9 KiB
Bash
Executable File
52 lines
1.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Usage: tools/sync-host-config [-h] [--apply] <ssh-target> <repo>...
|
|
#
|
|
# Compares the inline host config WERKREVISOR_CONFIG_TOML in ~/<repo>/.git/werkator/.werkator.yml
|
|
# of each <repo> on the Werkator host <ssh-target> with config/<repo>.toml.
|
|
# With --apply it replaces each block that differs, after a backup .werkator.yml.bak-config-<time>,
|
|
# and keeps the two newest of these backups.
|
|
# It prints only the numbers of the differing lines of a block, since a machine config holds tokens.
|
|
# Exit code: 0 if every block equals its file or was replaced, 1 if one differs without --apply,
|
|
# 2 if a machine config has no such block.
|
|
# Run it from the repository root; it opens one connection and retries a refused one.
|
|
#
|
|
# Example:
|
|
# tools/sync-host-config mih09-werkator@mih09.hostsharing.net werkregie werkator
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
awk 'NR > 1 && /^#/ { sub(/^# ?/, ""); print; next } NR > 1 { exit }' "$0"
|
|
exit "$1"
|
|
}
|
|
[[ "${1:-}" =~ ^(-h|-\?|--help)$ ]] && usage 0
|
|
mode=check
|
|
[[ "${1:-}" == "--apply" ]] && { mode=apply; shift; }
|
|
[[ $# -ge 2 ]] || usage 2
|
|
target="$1"
|
|
shift
|
|
for repo in "$@"; do
|
|
[[ -f "config/$repo.toml" ]] || { echo "no config/$repo.toml" >&2; exit 2; }
|
|
done
|
|
payload=$(python3 -c 'import json, sys; print(json.dumps({r: open(f"config/{r}.toml").read() for r in sys.argv[1:]}))' "$@")
|
|
|
|
# As in deploy-rootfs: the host refuses connections that follow too quickly.
|
|
retrying() {
|
|
local attempt
|
|
for attempt in 1 2 3 4 5 6; do
|
|
"$@" && return 0
|
|
local status=$?
|
|
[[ $status -eq 255 ]] || return $status
|
|
[[ $attempt -lt 6 ]] || break
|
|
echo "connection refused, retry in $((30 * attempt)) s" >&2
|
|
sleep $((30 * attempt))
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# A function, so that a retry sends the payload again.
|
|
send() {
|
|
ssh -o BatchMode=yes "$target" \
|
|
"python3 -c $(printf '%q' "$(cat "$(dirname "$0")/host_config_block.py")") $mode" <<< "$payload"
|
|
}
|
|
retrying send
|