Files
kernel-automation/agent/deploy.sh
T
patrickandClaude Opus 5.5 834c1459c0 Report services needing a restart via a separate root needrestart unit
needrestart has to inspect every process, which an unprivileged agent
can't (and unprivileged it gave false kernel results). Add
kernelwatch-needrestart.service: root with only CAP_SYS_PTRACE and
CAP_DAC_READ_SEARCH, PrivateNetwork=yes, list-only (-r l). It writes
`needrestart -b` output to /run/kernelwatch/needrestart; the agent unit
pulls it in before each report and reads the file (ignored if older than
2h). The agent no longer runs needrestart itself. Skipped cleanly when
needrestart isn't installed: the dashboard shows "?" and status is
unaffected.

Server: services_need_restart / sessions_need_restart fields, an
"update"-level reason, a Services column, and a note (not an escalation)
for hosts running an agent older than the one shipped with the server.

install.sh --with-needrestart installs needrestart and sets it to list
services after apt upgrades instead of prompting; deploy.sh passes
anything after `--` to install.sh. Agent 0.2.0, server 0.2.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 11:54:39 -04:00

193 lines
5.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
#
# 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 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"
token=$(fetch_token "$name" | tr -d '[:space:]') && [[ -n $token ]] ||
{ echo " could not get a token for $name" >&2; cleanup; return 1; }
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,16s/^# \{0,1\}//p' "$0"; }
mode=upgrade
dry_run=0
targets=()
install_args=()
while (($#)); do
case $1 in
--)
shift
install_args=("$@")
break
;;
--register) mode=register ;;
--rotate) mode=rotate ;;
--dry-run) dry_run=1 ;;
-h | --help) usage; exit 0 ;;
-*) usage >&2; exit 2 ;;
*) targets+=("$1") ;;
esac
shift
done
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
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)."