Export CSV on every table, edit recurring maintenance, runs-on nothing, and run package checks outside the agent sandbox
- Export CSV for the small tables that had none (reads the table as shown) - Servers page shows a skeleton while loading instead of "No servers registered yet" - Recurring maintenance schedules can be edited (PUT /schedules/:id) - A server can be set to run on nothing (servers.no_parent, migration 0028) - Linux agent v3: package-manager question runs in ExecStartPre=-+ outside the sandbox, with --prepare-updates and --check-updates modes - Docs: README, DATABASE, AGENT_UPDATE Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c17e9aa140
commit
540ab704b7
32 files changed
+3003
-66
No files matched your search
@@ -88,6 +88,13 @@ Description=Report scheduled tasks to Homelab Manager
|
||||
Type=oneshot
|
||||
EnvironmentFile=$CONFIG_DIR/homelab-manager-agent.env
|
||||
ExecStart=$INSTALL_DIR/homelab-manager-agent.sh
|
||||
# Asking dnf or zypper what is waiting still writes to /var and /run, which the sandbox below makes read-only, so this one step runs
|
||||
# first and outside it (the + does that; the - lets the report go ahead whatever happens). It only reads what the package manager
|
||||
# knows and leaves the answer in the unit's private directory, which goes away when the run ends.
|
||||
ExecStartPre=-+$INSTALL_DIR/homelab-manager-agent.sh --prepare-updates
|
||||
RuntimeDirectory=homelab-manager-agent
|
||||
RuntimeDirectoryMode=0700
|
||||
TimeoutStartSec=420
|
||||
# The agent only reads (crontabs, timers, /proc, listening sockets) and makes one HTTPS request, but it runs as root. These
|
||||
# keep a bug in it, or in anything it parses, from being able to change the system. It still sees every process, which is
|
||||
# how it names the program behind each listening port. Directives an older systemd doesn't know are ignored.
|
||||
|
||||
@@ -13,7 +13,8 @@ set -euo pipefail
|
||||
# Which version of this script this is, sent with every report so Homelab Manager can tell which agents are behind. Raise it whenever a
|
||||
# change here is worth installing the agent again for (and add it to the list at the top of server/src/services/agentVersions.ts).
|
||||
# 1 = before the number was reported 2 = reports pending updates, and its own version
|
||||
AGENT_VERSION=2
|
||||
# 3 = the package manager is asked outside the sandbox (see --prepare-updates), so dnf and zypper can answer there too
|
||||
AGENT_VERSION=3
|
||||
|
||||
ENV_FILE="${ENV_FILE:-/etc/homelab-manager-agent.env}"
|
||||
if [[ -f "$ENV_FILE" ]]; then
|
||||
@@ -27,7 +28,22 @@ API_INSECURE="${API_INSECURE:-false}"
|
||||
DRY_RUN=0
|
||||
[[ "${1:-}" == "--dry-run" ]] && DRY_RUN=1
|
||||
|
||||
if [[ -z "$API_URL" || -z "$API_TOKEN" ]]; then
|
||||
# Two extra ways to run it, neither of which reports anything:
|
||||
# --prepare-updates asks the package manager what is waiting and leaves the answer in $UPDATES_FILE for the report to pick up. The
|
||||
# systemd unit runs this step outside its sandbox (ExecStartPre=+), because dnf and zypper write to /var and /run
|
||||
# even when only asked a question, and the sandbox makes those read-only. A failure here only means no figure.
|
||||
# --check-updates for a person at a terminal: runs the same check, shows what the package manager said and the result.
|
||||
MODE=report
|
||||
case "${1:-}" in
|
||||
--prepare-updates) MODE=prepare ;;
|
||||
--check-updates) MODE=check ;;
|
||||
esac
|
||||
UPDATES_FILE="${UPDATES_FILE:-/run/homelab-manager-agent/updates.json}"
|
||||
# Where the package managers' own error output goes: nowhere, unless a person asked to see it.
|
||||
PM_ERR=/dev/null
|
||||
[[ "$MODE" == "check" ]] && PM_ERR=/dev/stderr
|
||||
|
||||
if [[ "$MODE" == "report" && ( -z "$API_URL" || -z "$API_TOKEN" ) ]]; then
|
||||
echo "API_URL and API_TOKEN must be set (env vars or $ENV_FILE)" >&2
|
||||
exit 1
|
||||
fi
|
||||
@@ -196,7 +212,7 @@ collect_updates() {
|
||||
if command -v apt-get >/dev/null 2>&1; then
|
||||
manager="apt"
|
||||
# -s simulates: "Inst" lines are what an upgrade would install. Security ones come from a "-security" or "Security" archive.
|
||||
if out=$(LC_ALL=C timeout 60 apt-get -s -o Debug::NoLocking=true upgrade 2>/dev/null); then
|
||||
if out=$(LC_ALL=C timeout 60 apt-get -s -o Debug::NoLocking=true upgrade 2>"$PM_ERR"); then
|
||||
count=$(grep -c '^Inst ' <<<"$out" || true)
|
||||
security=$(grep '^Inst ' <<<"$out" | grep -ci 'security' || true)
|
||||
fi
|
||||
@@ -204,17 +220,17 @@ collect_updates() {
|
||||
elif command -v dnf >/dev/null 2>&1; then
|
||||
manager="dnf"
|
||||
# check-update exits 100 when there are updates, 0 when none, 1 on an error. --cacheonly: never goes to the network from here.
|
||||
out=$(LC_ALL=C timeout 120 dnf -q check-update --cacheonly 2>/dev/null) || status=$?
|
||||
out=$(LC_ALL=C timeout 120 dnf -q check-update --cacheonly 2>"$PM_ERR") || status=$?
|
||||
if [[ "$status" -eq 0 || "$status" -eq 100 ]]; then
|
||||
count=$(awk 'NF >= 3 && $1 ~ /^[A-Za-z0-9_.+-]+\.[A-Za-z0-9_]+$/ && /Obsoleting/ == 0 { n++ } /^Obsoleting/ { exit } END { print n + 0 }' <<<"$out")
|
||||
security=$(LC_ALL=C timeout 120 dnf -q updateinfo list --security --cacheonly 2>/dev/null | grep -c '/Sec\.' || true)
|
||||
security=$(LC_ALL=C timeout 120 dnf -q updateinfo list --security --cacheonly 2>"$PM_ERR" | grep -c '/Sec\.' || true)
|
||||
fi
|
||||
if command -v needs-restarting >/dev/null 2>&1; then
|
||||
needs-restarting -r >/dev/null 2>&1 || reboot="true"
|
||||
fi
|
||||
elif command -v zypper >/dev/null 2>&1; then
|
||||
manager="zypper"
|
||||
if out=$(LC_ALL=C timeout 120 zypper -q --no-refresh list-updates 2>/dev/null); then
|
||||
if out=$(LC_ALL=C timeout 120 zypper -q --no-refresh list-updates 2>"$PM_ERR"); then
|
||||
count=$(grep -c '^v |' <<<"$out" || true)
|
||||
fi
|
||||
[[ -f /run/reboot-needed ]] && reboot="true"
|
||||
@@ -232,6 +248,18 @@ collect_updates() {
|
||||
fi
|
||||
}
|
||||
|
||||
# The answer --prepare-updates left for this report, if it is there, recent and looks like what it should. Read back through jq rather than
|
||||
# trusted: the report only ever carries these four fields, whatever the file holds.
|
||||
use_prepared_updates() {
|
||||
[[ -r "$UPDATES_FILE" ]] || return 1
|
||||
[[ -n "$(find "$UPDATES_FILE" -mmin -10 -print 2>/dev/null)" ]] || return 1
|
||||
local json
|
||||
json=$(jq -ce 'select(type == "object" and (.manager | type) == "string" and (.count | type) == "number")
|
||||
| {manager: .manager, count: .count, security: (if (.security | type) == "number" then .security else null end), reboot_required: (.reboot_required == true)}' "$UPDATES_FILE" 2>/dev/null) || return 1
|
||||
[[ -n "$json" ]] || return 1
|
||||
UPDATES_JSON=$json
|
||||
}
|
||||
|
||||
collect_system_info() {
|
||||
local ip_json="[]"
|
||||
if command -v ip >/dev/null 2>&1; then
|
||||
@@ -295,7 +323,7 @@ collect_system_info() {
|
||||
|
||||
# Best-effort like the rest: a package manager that misbehaves must not cost the report.
|
||||
UPDATES_JSON="null"
|
||||
collect_updates || UPDATES_JSON="null"
|
||||
use_prepared_updates || collect_updates || UPDATES_JSON="null"
|
||||
|
||||
SYSTEM_JSON=$(jq -n \
|
||||
--argjson updates "$UPDATES_JSON" \
|
||||
@@ -317,6 +345,22 @@ collect_system_info() {
|
||||
}')
|
||||
}
|
||||
|
||||
if [[ "$MODE" != "report" ]]; then
|
||||
collect_updates
|
||||
if [[ "$MODE" == "check" ]]; then
|
||||
echo "Pending updates as they would be reported:"
|
||||
jq . <<<"$UPDATES_JSON"
|
||||
exit 0
|
||||
fi
|
||||
# prepare: leave the answer (or no file at all) for the report that follows. Written whole and moved into place, so a report never reads half of it.
|
||||
rm -f "$UPDATES_FILE"
|
||||
if [[ "$UPDATES_JSON" != "null" ]]; then
|
||||
mkdir -p "$(dirname "$UPDATES_FILE")" 2>/dev/null || exit 0
|
||||
printf '%s\n' "$UPDATES_JSON" > "$UPDATES_FILE.tmp" 2>/dev/null && mv -f "$UPDATES_FILE.tmp" "$UPDATES_FILE" 2>/dev/null || true
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
collect_cron
|
||||
collect_systemd_timers
|
||||
|
||||
|
||||
Reference in new issue
Block a user