kernelwatch-apt-update.service failed hourly on most hosts for weeks without anything showing it. The agent only Wants= its helper units, so the report goes out regardless, with stale lists or (for needrestart) results that quietly turn "unknown". Each helper (apt-update, needrestart) now runs kernelwatch-unit-result as its ExecStopPost=, which systemd runs after failed runs too. It writes "RESULT EXIT_CODE EXIT_STATUS FAILURES" to /run/kernelwatch/<name>.result, FAILURES counting failed runs in a row. The agent (0.10.0) reports results from the last two hours as helper_units. On the server, one or two failures are a note (an admin's apt can hold the lock now and then); three in a row put the host in update, naming the unit and its exit status and pointing at journalctl. The host page gets a "kernelwatch helpers" row. The Livepatch unit isn't tracked: it fails by design where the snap is installed but Livepatch isn't enabled, and Livepatch has its own check. The apt-update unit now has RuntimeDirectory=kernelwatch (preserved, like the others) for its result file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
239 lines
7.9 KiB
Bash
Executable File
239 lines
7.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Install or upgrade kernelwatch-agent on fleet hosts over SSH, from a checkout
|
|
# on your workstation. Hosts need no git checkout or Gitea access.
|
|
#
|
|
# agent/deploy.sh upgrade every host in agent/hosts
|
|
# agent/deploy.sh web01 db01 upgrade just these
|
|
# agent/deploy.sh --register new01 register on the dashboard, then install
|
|
# agent/deploy.sh --rotate web01 issue a new token, then reinstall
|
|
# agent/deploy.sh --dry-run show what would happen
|
|
# agent/deploy.sh -- --with-needrestart pass the rest to install.sh
|
|
# agent/deploy.sh --allow-outdated deploy even if this checkout is behind
|
|
#
|
|
# Settings: agent/deploy.conf (see deploy.conf.example). Hosts: agent/hosts,
|
|
# one per line: `<ssh target> [kernelwatch name]`; the name defaults to the
|
|
# host's `hostname -s`. Prompts for sudo passwords on each host (and on the
|
|
# dashboard when registering) unless you have passwordless sudo.
|
|
set -euo pipefail
|
|
|
|
here=$(cd "$(dirname "$0")" && pwd)
|
|
CONF=${KERNELWATCH_DEPLOY_CONF:-$here/deploy.conf}
|
|
HOSTS_FILE=${KERNELWATCH_HOSTS:-$here/hosts}
|
|
FILES=(kernelwatch-agent kernelwatch-agent.service kernelwatch-agent.timer
|
|
kernelwatch-needrestart.service kernelwatch-livepatch kernelwatch-livepatch.service
|
|
kernelwatch-apt-update.service kernelwatch-unit-result install.sh)
|
|
NAME_RE='^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$'
|
|
|
|
SERVER_URL=
|
|
DASHBOARD=
|
|
|
|
die() {
|
|
echo "deploy: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
say() {
|
|
if [[ -t 2 ]]; then printf '\033[1m%s\033[0m\n' "$*" >&2; else printf '%s\n' "$*" >&2; fi
|
|
}
|
|
|
|
load_config() {
|
|
[[ -r $CONF ]] || die "no $CONF; copy deploy.conf.example and fill it in"
|
|
local key value
|
|
while IFS='=' read -r key value; do
|
|
key=${key//[[:space:]]/}
|
|
[[ -z $key || $key == \#* ]] && continue
|
|
value=${value#"${value%%[![:space:]]*}"}
|
|
value=${value%"${value##*[![:space:]]}"}
|
|
value=${value#\"}
|
|
value=${value%\"}
|
|
case $key in
|
|
SERVER_URL) SERVER_URL=$value ;;
|
|
DASHBOARD) DASHBOARD=$value ;;
|
|
*) die "unknown key '$key' in $CONF" ;;
|
|
esac
|
|
done <"$CONF"
|
|
}
|
|
|
|
# Prints "<target> <name>" lines from the hosts file (name may be empty).
|
|
read_hosts() {
|
|
[[ -r $HOSTS_FILE ]] || return 0
|
|
local line target name _
|
|
while IFS= read -r line; do
|
|
line=${line%%#*}
|
|
read -r target name _ <<<"$line" || true
|
|
[[ -z $target ]] && continue
|
|
printf '%s %s\n' "$target" "$name"
|
|
done <"$HOSTS_FILE"
|
|
}
|
|
|
|
in_hosts() {
|
|
local t _
|
|
while read -r t _; do
|
|
[[ $t == "$1" ]] && return 0
|
|
done < <(read_hosts)
|
|
return 1
|
|
}
|
|
|
|
# kernelwatch name for a target from the hosts file, or empty.
|
|
name_for() {
|
|
local t n
|
|
while read -r t n; do
|
|
[[ $t == "$1" ]] && { printf '%s' "$n"; return 0; }
|
|
done < <(read_hosts)
|
|
}
|
|
|
|
# Registers (or rotates) a host on the dashboard and prints its token. The token
|
|
# goes through a 0600 file in your dashboard home directory for a moment,
|
|
# because an interactive sudo prompt and captured output can't share one SSH
|
|
# session.
|
|
fetch_token() {
|
|
local name=$1 cmd=add-host f=".kernelwatch-token.$1"
|
|
[[ $mode == rotate ]] && cmd=rotate-token
|
|
if ! ssh -t "$DASHBOARD" "umask 077 && sudo kernelwatch $cmd --token-only $name > $f" >&2; then
|
|
ssh -T "$DASHBOARD" "rm -f $f" || true
|
|
return 1
|
|
fi
|
|
ssh -T "$DASHBOARD" "cat $f && rm -f $f"
|
|
}
|
|
|
|
deploy_one() {
|
|
local target=$1 name=$2 out dir remote_name token remote
|
|
|
|
if ((dry_run)); then
|
|
if [[ $mode == upgrade ]]; then
|
|
echo " copy agent to $target, run install.sh ${install_args[*]} (keeps config and token)" >&2
|
|
else
|
|
echo " on $DASHBOARD: kernelwatch ${mode/register/add-host} ${name:-<its hostname -s>}" >&2
|
|
echo " copy agent and token to $target, run install.sh --server $SERVER_URL" >&2
|
|
fi
|
|
return 0
|
|
fi
|
|
|
|
# 1. Copy the agent into a private temp dir; learn the host's short name.
|
|
out=$(tar -C "$here" -cf - "${FILES[@]}" |
|
|
ssh -T "$target" 'd=$(mktemp -d) && tar -C "$d" -xf - && printf "%s %s\n" "$d" "$(hostname -s)"') ||
|
|
{ echo " copy failed" >&2; return 1; }
|
|
dir=${out%% *}
|
|
remote_name=${out#* }
|
|
name=${name:-$remote_name}
|
|
cleanup() { ssh -T "$target" "rm -rf '$dir'" || true; }
|
|
|
|
if ! [[ $name =~ $NAME_RE ]]; then
|
|
echo " invalid kernelwatch name '$name'; set one in $HOSTS_FILE" >&2
|
|
cleanup
|
|
return 1
|
|
fi
|
|
|
|
local -a install=(sudo "$dir/install.sh" "${install_args[@]}")
|
|
if [[ $mode != upgrade ]]; then
|
|
# 2. Get a token from the dashboard and hand it over on stdin, never argv.
|
|
say " $mode $name on $DASHBOARD"
|
|
if ! token=$(fetch_token "$name" | tr -d '[:space:]') || [[ -z $token ]]; then
|
|
echo " could not get a token for $name" >&2
|
|
cleanup
|
|
return 1
|
|
fi
|
|
printf '%s\n' "$token" | ssh -T "$target" "umask 077 && cat > '$dir/token'" ||
|
|
{ cleanup; return 1; }
|
|
install+=(--server "$SERVER_URL" --token-file "$dir/token")
|
|
fi
|
|
|
|
# 3. Install (or upgrade) and send a first report; always remove the temp dir.
|
|
say " installing as $name"
|
|
printf -v remote '%q ' "${install[@]}"
|
|
ssh -t "$target" "$remote; rc=\$?; rm -rf '$dir'; exit \$rc" || return 1
|
|
|
|
# Remember newly registered hosts so plain `deploy.sh` upgrades them later.
|
|
if [[ $mode == register ]] && ! in_hosts "$target"; then
|
|
printf '%s %s\n' "$target" "$name" >>"$HOSTS_FILE"
|
|
echo " added '$target $name' to $HOSTS_FILE" >&2
|
|
fi
|
|
}
|
|
|
|
usage() { sed -n '2,17s/^# \{0,1\}//p' "$0"; }
|
|
|
|
# Under sudo, ssh would read root's ~/.ssh/config and keys instead of yours (a
|
|
# custom Port or IdentityFile silently disappears, so it tries port 22). Nothing
|
|
# here needs root locally: install.sh runs under sudo on each host.
|
|
refuse_sudo() {
|
|
[[ -n ${SUDO_USER:-} && $(id -u) == 0 ]] || return 0
|
|
die "don't run this with sudo: it uses your own SSH config and keys, and runs sudo on each host itself. Run it as $SUDO_USER."
|
|
}
|
|
|
|
# Deploying from a checkout that's behind its upstream ships an old agent: the
|
|
# dashboard keeps showing "agent is older" after a deploy that looked fine.
|
|
# Fetches with a time limit; a failed fetch only warns. Behind: asks on a
|
|
# terminal, otherwise needs --allow-outdated. Skipped outside a git checkout or
|
|
# without an upstream branch.
|
|
check_checkout() {
|
|
local repo upstream behind
|
|
repo=$(git -C "$here" rev-parse --show-toplevel 2>/dev/null) || return 0
|
|
upstream=$(git -C "$repo" rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null) || return 0
|
|
local -a fetch=(git -C "$repo" fetch --quiet)
|
|
command -v timeout >/dev/null && fetch=(timeout 20 "${fetch[@]}")
|
|
if ! GIT_TERMINAL_PROMPT=0 "${fetch[@]}" >/dev/null 2>&1; then
|
|
echo "deploy: warning: couldn't fetch $upstream to check this checkout is up to date" >&2
|
|
return 0
|
|
fi
|
|
behind=$(git -C "$repo" rev-list --count "HEAD..$upstream" 2>/dev/null) || return 0
|
|
((behind > 0)) || return 0
|
|
local msg="this checkout is $behind commit(s) behind $upstream, so it would deploy an older agent (git pull first)"
|
|
if ((allow_outdated || dry_run)); then
|
|
echo "deploy: warning: $msg" >&2
|
|
return 0
|
|
fi
|
|
[[ -t 0 && -t 2 ]] || die "$msg; pass --allow-outdated to deploy it anyway"
|
|
local answer
|
|
read -r -p "deploy: $msg. Deploy anyway? [y/N] " answer
|
|
[[ $answer == [yY]* ]] || die "stopped; nothing was deployed"
|
|
}
|
|
|
|
mode=upgrade
|
|
dry_run=0
|
|
allow_outdated=0
|
|
targets=()
|
|
install_args=()
|
|
while (($#)); do
|
|
case $1 in
|
|
--)
|
|
shift
|
|
install_args=("$@")
|
|
break
|
|
;;
|
|
--register) mode=register ;;
|
|
--rotate) mode=rotate ;;
|
|
--dry-run) dry_run=1 ;;
|
|
--allow-outdated) allow_outdated=1 ;;
|
|
-h | --help) usage; exit 0 ;;
|
|
-*) usage >&2; exit 2 ;;
|
|
*) targets+=("$1") ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
refuse_sudo
|
|
load_config
|
|
[[ -n $SERVER_URL ]] || die "SERVER_URL is not set in $CONF"
|
|
[[ $mode == upgrade || -n $DASHBOARD ]] || die "DASHBOARD is not set in $CONF"
|
|
|
|
if ((${#targets[@]} == 0)); then
|
|
[[ $mode == upgrade ]] || die "name the hosts to $mode"
|
|
while read -r t _; do targets+=("$t"); done < <(read_hosts)
|
|
((${#targets[@]})) || die "no hosts given and none in $HOSTS_FILE"
|
|
fi
|
|
|
|
check_checkout
|
|
|
|
failed=()
|
|
for t in "${targets[@]}"; do
|
|
say "==> $t"
|
|
deploy_one "$t" "$(name_for "$t")" || failed+=("$t")
|
|
done
|
|
|
|
echo >&2
|
|
if ((${#failed[@]})); then
|
|
say "Failed (${#failed[@]}/${#targets[@]}): ${failed[*]}"
|
|
exit 1
|
|
fi
|
|
say "Done: ${#targets[@]} host(s)."
|