Add agent/deploy.sh: install and upgrade agents over SSH

Run from a workstation checkout; hosts need no git or Gitea access.
A plain run upgrades every host in agent/hosts (config and token kept);
--register runs `kernelwatch add-host --token-only` on the dashboard
over SSH and hands the token to the host on stdin, never argv or the
terminal; --rotate does the same with rotate-token. Newly registered
hosts are appended to agent/hosts. Failures are summarised and don't
stop the rest of the fleet.

Supporting changes: install.sh --token-file, and --token-only on
`kernelwatch add-host` / `rotate-token`. Tested end to end with ssh and
sudo stubbed to run locally against the real CLI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-17 11:51:15 -04:00
1 parent a430a13fc8
commit 69d4cf6363
8 files changed
+456 -11

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
# agent/deploy.conf — settings for agent/deploy.sh (this file is git-ignored).
# URL agents report to.
SERVER_URL=https://kernels.example.internal
# SSH target of the dashboard host, used by --register / --rotate to run
# `sudo kernelwatch add-host` there.
DASHBOARD=admin@kernels.example.internal
+184
View File
@@ -0,0 +1,184 @@
#!/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
#
# 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 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 (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")
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,15s/^# \{0,1\}//p' "$0"; }
mode=upgrade
dry_run=0
targets=()
while (($#)); do
case $1 in
--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)."
+9
View File
@@ -0,0 +1,9 @@
# agent/hosts — fleet for agent/deploy.sh (this file is git-ignored).
# One host per line: <ssh target> [kernelwatch name]
# The SSH target can be anything `ssh` accepts, including a Host alias from
# ~/.ssh/config. The name defaults to the host's `hostname -s`.
# `deploy.sh --register` appends new hosts here automatically.
web01.example.internal
patrick@10.0.0.31 db01
zeus # IPv6-only? Give it a ProxyJump in ~/.ssh/config
+18 -10
View File
@@ -1,22 +1,25 @@
#!/usr/bin/env bash
# Install or upgrade kernelwatch-agent on a Debian 12/13 or Ubuntu 24.04 host.
# Install or upgrade kernelwatch-agent on a Debian 12/13 or Ubuntu 22.04/24.04 host.
#
# sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem]
#
# The host token (from `kernelwatch add-host <name>` on the server) is read
# from the KERNELWATCH_TOKEN environment variable or prompted for, so it never
# lands in shell history. Re-running without --server keeps the existing config.
# from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt,
# so it never lands in shell history. Re-running without --server or a token
# keeps the existing config and token (that's an upgrade).
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
server=
ca=
token_file=
while (($#)); do
case $1 in
--server) server=${2:?--server needs a URL}; shift 2 ;;
--ca) ca=${2:?--ca needs a file}; shift 2 ;;
-h | --help) sed -n '2,8s/^# \{0,1\}//p' "$0"; exit 0 ;;
--token-file) token_file=${2:?--token-file needs a file}; shift 2 ;;
-h | --help) sed -n '2,9s/^# \{0,1\}//p' "$0"; exit 0 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
done
@@ -54,13 +57,18 @@ elif [[ ! -e /etc/kernelwatch/agent.conf ]]; then
exit 1
fi
if [[ ! -s /etc/kernelwatch/agent.token || -n ${KERNELWATCH_TOKEN:-} ]]; then
token=${KERNELWATCH_TOKEN:-}
if [[ -z $token ]]; then
read -r -s -p "Host token: " token
echo
fi
token=
if [[ -n $token_file ]]; then
token=$(tr -d '[:space:]' <"$token_file")
[[ -n $token ]] || { echo "empty token in $token_file" >&2; exit 1; }
elif [[ -n ${KERNELWATCH_TOKEN:-} ]]; then
token=$KERNELWATCH_TOKEN
elif [[ ! -s /etc/kernelwatch/agent.token ]]; then
read -r -s -p "Host token: " token
echo
[[ -n $token ]] || { echo "empty token" >&2; exit 1; }
fi
if [[ -n $token ]]; then
(umask 077 && printf '%s\n' "$token" >/etc/kernelwatch/agent.token)
fi
chown root:root /etc/kernelwatch/agent.token