Add agent/deploy.sh: install and upgrade agents over SSH #1
No files matched your search
@@ -3,3 +3,5 @@ __pycache__/
|
|||||||
*.db
|
*.db
|
||||||
*.db-wal
|
*.db-wal
|
||||||
*.db-shm
|
*.db-shm
|
||||||
|
agent/hosts
|
||||||
|
agent/deploy.conf
|
||||||
@@ -194,10 +194,49 @@ systemctl list-timers kernelwatch-agent.timer
|
|||||||
journalctl -u kernelwatch-agent
|
journalctl -u kernelwatch-agent
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Deploying to many hosts
|
||||||
|
|
||||||
|
`agent/deploy.sh` does all of the above over SSH from your workstation's
|
||||||
|
checkout, so hosts need neither git nor Gitea access. Set it up once:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cp agent/deploy.conf.example agent/deploy.conf # SERVER_URL, DASHBOARD (ssh target)
|
||||||
|
cp agent/hosts.example agent/hosts # optional; --register fills it in
|
||||||
|
```
|
||||||
|
|
||||||
|
Then:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
agent/deploy.sh --register web01 db01 # register on the dashboard + install
|
||||||
|
agent/deploy.sh # upgrade every host in agent/hosts
|
||||||
|
agent/deploy.sh web01 # upgrade one
|
||||||
|
agent/deploy.sh --rotate web01 # new token, reinstall
|
||||||
|
agent/deploy.sh --dry-run # show the plan
|
||||||
|
```
|
||||||
|
|
||||||
|
For each host it copies the agent into a private temp directory and runs
|
||||||
|
`install.sh`, which sends a first report, then deletes the temp directory.
|
||||||
|
With `--register`/`--rotate`, it also runs
|
||||||
|
`sudo kernelwatch add-host --token-only` on the dashboard host and passes the
|
||||||
|
token to the host on SSH stdin. The token never appears on a command line or
|
||||||
|
in your terminal. A plain run is an upgrade: config and token stay as they
|
||||||
|
are. You'll get sudo prompts on each host, and on the dashboard when
|
||||||
|
registering, unless you have passwordless sudo.
|
||||||
|
|
||||||
|
Both files are git-ignored. Host names default to each host's `hostname -s`;
|
||||||
|
put `<ssh target> <name>` in `agent/hosts` to override. For a host your
|
||||||
|
workstation can't reach directly (e.g. IPv6-only while you're on IPv4), give it
|
||||||
|
a jump host in `~/.ssh/config` and `deploy.sh` works unchanged:
|
||||||
|
|
||||||
|
```
|
||||||
|
Host zeus
|
||||||
|
ProxyJump eurus
|
||||||
|
```
|
||||||
|
|
||||||
## Server admin commands
|
## Server admin commands
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo kernelwatch add-host NAME register a host, print its token
|
sudo kernelwatch add-host NAME register a host, print its token (--token-only for scripts)
|
||||||
sudo kernelwatch rotate-token NAME issue a new token (old one stops working)
|
sudo kernelwatch rotate-token NAME issue a new token (old one stops working)
|
||||||
sudo kernelwatch remove-host NAME delete a host and its history
|
sudo kernelwatch remove-host NAME delete a host and its history
|
||||||
sudo kernelwatch list-hosts hosts and when they last reported
|
sudo kernelwatch list-hosts hosts and when they last reported
|
||||||
|
|||||||
@@ -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
|
||||||
Executable
+184
@@ -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)."
|
||||||
@@ -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
@@ -1,22 +1,25 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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]
|
# sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem]
|
||||||
#
|
#
|
||||||
# The host token (from `kernelwatch add-host <name>` on the server) is read
|
# 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
|
# from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt,
|
||||||
# lands in shell history. Re-running without --server keeps the existing config.
|
# 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
|
set -euo pipefail
|
||||||
|
|
||||||
here=$(cd "$(dirname "$0")" && pwd)
|
here=$(cd "$(dirname "$0")" && pwd)
|
||||||
server=
|
server=
|
||||||
ca=
|
ca=
|
||||||
|
token_file=
|
||||||
|
|
||||||
while (($#)); do
|
while (($#)); do
|
||||||
case $1 in
|
case $1 in
|
||||||
--server) server=${2:?--server needs a URL}; shift 2 ;;
|
--server) server=${2:?--server needs a URL}; shift 2 ;;
|
||||||
--ca) ca=${2:?--ca needs a file}; 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 ;;
|
*) echo "unknown argument: $1" >&2; exit 2 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
@@ -54,13 +57,18 @@ elif [[ ! -e /etc/kernelwatch/agent.conf ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -s /etc/kernelwatch/agent.token || -n ${KERNELWATCH_TOKEN:-} ]]; then
|
token=
|
||||||
token=${KERNELWATCH_TOKEN:-}
|
if [[ -n $token_file ]]; then
|
||||||
if [[ -z $token ]]; then
|
token=$(tr -d '[:space:]' <"$token_file")
|
||||||
read -r -s -p "Host token: " token
|
[[ -n $token ]] || { echo "empty token in $token_file" >&2; exit 1; }
|
||||||
echo
|
elif [[ -n ${KERNELWATCH_TOKEN:-} ]]; then
|
||||||
fi
|
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; }
|
[[ -n $token ]] || { echo "empty token" >&2; exit 1; }
|
||||||
|
fi
|
||||||
|
if [[ -n $token ]]; then
|
||||||
(umask 077 && printf '%s\n' "$token" >/etc/kernelwatch/agent.token)
|
(umask 077 && printf '%s\n' "$token" >/etc/kernelwatch/agent.token)
|
||||||
fi
|
fi
|
||||||
chown root:root /etc/kernelwatch/agent.token
|
chown root:root /etc/kernelwatch/agent.token
|
||||||
|
|||||||
@@ -38,6 +38,9 @@ def cmd_add_host(cfg, args) -> int:
|
|||||||
return 1
|
return 1
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
if args.token_only:
|
||||||
|
print(token)
|
||||||
|
return 0
|
||||||
print(f"Added {args.name}. Its token (shown once):\n\n {token}\n")
|
print(f"Added {args.name}. Its token (shown once):\n\n {token}\n")
|
||||||
print("On the host, run: sudo ./agent/install.sh --server <URL> and paste the token.")
|
print("On the host, run: sudo ./agent/install.sh --server <URL> and paste the token.")
|
||||||
return 0
|
return 0
|
||||||
@@ -52,6 +55,9 @@ def cmd_rotate_token(cfg, args) -> int:
|
|||||||
return 1
|
return 1
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
if args.token_only:
|
||||||
|
print(token)
|
||||||
|
return 0
|
||||||
print(f"New token for {args.name} (the old one no longer works):\n\n {token}\n")
|
print(f"New token for {args.name} (the old one no longer works):\n\n {token}\n")
|
||||||
print("On the host, run: sudo KERNELWATCH_TOKEN=... ./agent/install.sh or edit /etc/kernelwatch/agent.token")
|
print("On the host, run: sudo KERNELWATCH_TOKEN=... ./agent/install.sh or edit /etc/kernelwatch/agent.token")
|
||||||
return 0
|
return 0
|
||||||
@@ -136,6 +142,9 @@ def main(argv: list[str] | None = None) -> int:
|
|||||||
):
|
):
|
||||||
p = sub.add_parser(name, help=help_)
|
p = sub.add_parser(name, help=help_)
|
||||||
p.add_argument("name", type=_valid_host)
|
p.add_argument("name", type=_valid_host)
|
||||||
|
if name != "remove-host":
|
||||||
|
p.add_argument("--token-only", action="store_true",
|
||||||
|
help="print only the token (for scripts such as agent/deploy.sh)")
|
||||||
p.set_defaults(func=func)
|
p.set_defaults(func=func)
|
||||||
sub.add_parser("list-hosts", help="list hosts and when they last reported").set_defaults(
|
sub.add_parser("list-hosts", help="list hosts and when they last reported").set_defaults(
|
||||||
func=cmd_list_hosts)
|
func=cmd_list_hosts)
|
||||||
|
|||||||
@@ -0,0 +1,186 @@
|
|||||||
|
"""Runs agent/deploy.sh with ssh/sudo stubbed to run "remote" commands locally.
|
||||||
|
|
||||||
|
The dashboard side is the real kernelwatch CLI on a temp database; install.sh is
|
||||||
|
replaced by a recorder so we can check what each host received.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import stat
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from helpers import REPO, tempdir
|
||||||
|
from kernelwatch import auth, db
|
||||||
|
|
||||||
|
AGENT_DIR = os.path.join(REPO, "agent")
|
||||||
|
|
||||||
|
SSH_STUB = r"""#!/usr/bin/env bash
|
||||||
|
while [[ $1 == -* ]]; do
|
||||||
|
case $1 in -o) shift 2 ;; *) shift ;; esac
|
||||||
|
done
|
||||||
|
target=$1; shift
|
||||||
|
echo "$target" >> "$KW_LOG/ssh.log"
|
||||||
|
export KW_TARGET=$target
|
||||||
|
mkdir -p "$KW_HOMES/$target" && cd "$KW_HOMES/$target"
|
||||||
|
exec bash -c "$*"
|
||||||
|
"""
|
||||||
|
|
||||||
|
FAKE_INSTALL = r"""#!/usr/bin/env bash
|
||||||
|
out=$KW_LOG/install-$KW_TARGET
|
||||||
|
printf '%s\n' "$@" > "$out.args"
|
||||||
|
while (($#)); do
|
||||||
|
[[ $1 == --token-file ]] && cp "$2" "$out.token"
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def write_exec(path, text):
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(text)
|
||||||
|
os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(shutil.which("bash") and shutil.which("tar"), "needs bash and tar")
|
||||||
|
class DeployTest(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self._tmp = tempdir()
|
||||||
|
t = self.tmp = self._tmp.name
|
||||||
|
self.agent = os.path.join(t, "agent")
|
||||||
|
self.log = os.path.join(t, "log")
|
||||||
|
self.homes = os.path.join(t, "homes")
|
||||||
|
self.remote_tmp = os.path.join(t, "remote-tmp")
|
||||||
|
bindir = os.path.join(t, "bin")
|
||||||
|
for d in (self.agent, self.log, self.homes, self.remote_tmp, bindir):
|
||||||
|
os.makedirs(d)
|
||||||
|
|
||||||
|
for name in ("deploy.sh", "kernelwatch-agent", "kernelwatch-agent.service",
|
||||||
|
"kernelwatch-agent.timer"):
|
||||||
|
shutil.copy2(os.path.join(AGENT_DIR, name), self.agent)
|
||||||
|
write_exec(os.path.join(self.agent, "install.sh"), FAKE_INSTALL)
|
||||||
|
|
||||||
|
self.db_path = os.path.join(t, "kw.db")
|
||||||
|
server_conf = os.path.join(t, "server.toml")
|
||||||
|
with open(server_conf, "w") as f:
|
||||||
|
f.write(f'db_path = "{self.db_path}"\n')
|
||||||
|
|
||||||
|
write_exec(os.path.join(bindir, "ssh"), SSH_STUB)
|
||||||
|
write_exec(os.path.join(bindir, "sudo"), '#!/bin/sh\nexec "$@"\n')
|
||||||
|
write_exec(os.path.join(bindir, "hostname"),
|
||||||
|
'#!/bin/sh\nt=${KW_TARGET#*@}; echo "${t%%.*}"\n')
|
||||||
|
write_exec(os.path.join(bindir, "kernelwatch"),
|
||||||
|
f'#!/bin/sh\nexec {sys.executable} {REPO}/server/kernelwatch-server "$@"\n')
|
||||||
|
|
||||||
|
with open(os.path.join(self.agent, "deploy.conf"), "w") as f:
|
||||||
|
f.write("SERVER_URL=https://kernels.example.test\nDASHBOARD=admin@dash\n")
|
||||||
|
self.hosts_file = os.path.join(self.agent, "hosts")
|
||||||
|
with open(self.hosts_file, "w") as f:
|
||||||
|
f.write("# fleet\nweb01.example.test\npatrick@10.0.0.31 db01 # explicit name\n")
|
||||||
|
|
||||||
|
self.env = {
|
||||||
|
"PATH": f"{bindir}:{os.environ['PATH']}",
|
||||||
|
"KW_LOG": self.log,
|
||||||
|
"KW_HOMES": self.homes,
|
||||||
|
"TMPDIR": self.remote_tmp,
|
||||||
|
"KERNELWATCH_CONFIG": server_conf,
|
||||||
|
}
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self._tmp.cleanup()
|
||||||
|
|
||||||
|
def deploy(self, *args):
|
||||||
|
return subprocess.run(["bash", os.path.join(self.agent, "deploy.sh"), *args],
|
||||||
|
env=self.env, capture_output=True, text=True, timeout=60)
|
||||||
|
|
||||||
|
def installed(self, target):
|
||||||
|
base = os.path.join(self.log, f"install-{target}")
|
||||||
|
with open(base + ".args") as f:
|
||||||
|
args = f.read().split()
|
||||||
|
token = None
|
||||||
|
if os.path.exists(base + ".token"):
|
||||||
|
with open(base + ".token") as f:
|
||||||
|
token = f.read().strip()
|
||||||
|
return args, token
|
||||||
|
|
||||||
|
def host_for(self, token):
|
||||||
|
conn = db.connect(self.db_path)
|
||||||
|
try:
|
||||||
|
return db.host_for_token(conn, auth.token_hash(token))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def assert_clean(self):
|
||||||
|
self.assertEqual(os.listdir(self.remote_tmp), [], "remote temp dirs removed")
|
||||||
|
dash = os.path.join(self.homes, "admin@dash")
|
||||||
|
if os.path.isdir(dash):
|
||||||
|
self.assertEqual(os.listdir(dash), [], "no token left on the dashboard")
|
||||||
|
|
||||||
|
def test_register_hosts_from_file_and_cli(self):
|
||||||
|
proc = self.deploy("--register", "web01.example.test", "patrick@10.0.0.31", "new01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
|
||||||
|
for target, name in (("web01.example.test", "web01"), ("patrick@10.0.0.31", "db01"),
|
||||||
|
("new01.example.test", "new01")):
|
||||||
|
with self.subTest(target=target):
|
||||||
|
args, token = self.installed(target)
|
||||||
|
self.assertIn("--server", args)
|
||||||
|
self.assertIn("https://kernels.example.test", args)
|
||||||
|
self.assertEqual(self.host_for(token), name, "token authenticates as that host")
|
||||||
|
self.assertNotIn(token, proc.stdout + proc.stderr, "token never printed")
|
||||||
|
|
||||||
|
with open(self.hosts_file) as f:
|
||||||
|
self.assertIn("new01.example.test new01\n", f.read(), "new host remembered")
|
||||||
|
self.assert_clean()
|
||||||
|
|
||||||
|
def test_upgrade_all_keeps_config(self):
|
||||||
|
proc = self.deploy()
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
for target in ("web01.example.test", "patrick@10.0.0.31"):
|
||||||
|
args, token = self.installed(target)
|
||||||
|
self.assertEqual(len(args), 0, "plain install.sh: keeps existing config and token")
|
||||||
|
self.assertIsNone(token)
|
||||||
|
with open(os.path.join(self.log, "ssh.log")) as f:
|
||||||
|
self.assertNotIn("admin@dash", f.read(), "upgrades don't touch the dashboard")
|
||||||
|
self.assert_clean()
|
||||||
|
|
||||||
|
def test_register_existing_fails_but_others_continue(self):
|
||||||
|
self.assertEqual(self.deploy("--register", "web01.example.test").returncode, 0)
|
||||||
|
proc = self.deploy("--register", "web01.example.test", "new01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 1)
|
||||||
|
self.assertIn("already exists", proc.stderr)
|
||||||
|
self.assertIn("Failed (1/2): web01.example.test", proc.stderr)
|
||||||
|
self.assertEqual(self.host_for(self.installed("new01.example.test")[1]), "new01")
|
||||||
|
self.assert_clean()
|
||||||
|
|
||||||
|
def test_rotate(self):
|
||||||
|
self.deploy("--register", "web01.example.test")
|
||||||
|
old = self.installed("web01.example.test")[1]
|
||||||
|
proc = self.deploy("--rotate", "web01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
new = self.installed("web01.example.test")[1]
|
||||||
|
self.assertNotEqual(old, new)
|
||||||
|
self.assertIsNone(self.host_for(old))
|
||||||
|
self.assertEqual(self.host_for(new), "web01")
|
||||||
|
|
||||||
|
def test_register_needs_explicit_hosts(self):
|
||||||
|
proc = self.deploy("--register")
|
||||||
|
self.assertNotEqual(proc.returncode, 0)
|
||||||
|
self.assertIn("name the hosts", proc.stderr)
|
||||||
|
|
||||||
|
def test_dry_run_touches_nothing(self):
|
||||||
|
proc = self.deploy("--dry-run", "--register", "new01.example.test")
|
||||||
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||||
|
self.assertIn("kernelwatch add-host", proc.stderr)
|
||||||
|
self.assertFalse(os.path.exists(os.path.join(self.log, "ssh.log")))
|
||||||
|
|
||||||
|
def test_missing_config(self):
|
||||||
|
os.remove(os.path.join(self.agent, "deploy.conf"))
|
||||||
|
proc = self.deploy()
|
||||||
|
self.assertNotEqual(proc.returncode, 0)
|
||||||
|
self.assertIn("deploy.conf.example", proc.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user