Dashboard: the status play parses again; every play is now syntax-checked

Since v0.5.26 the status play's shell block, which grew the stats
script, tripped Ansible's free-form argument splitter, so the Dashboard
and 'interzone status' failed on every server with only "^ column 7"
to show for it. The script now goes in as cmd:, a test runs
ansible-playbook --syntax-check on every play interzone writes, and an
Ansible error shows its [ERROR] line rather than the last line of its
output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vvvomitandClaude Opus 5.5 committed 2026-10-29 15:41:03 +03:30
1 parent 74ab0b8c3d
commit 00216f72f7
4 files changed
+103 -35

No files matched your search

+5
View File
@@ -133,6 +133,11 @@ func taskMessage(h TaskResult) string {
func lastLine(s string, fallback error) string {
s = strings.TrimSpace(s)
// Ansible's own errors end with where in the file, e.g. "^ column 7".
if _, e, ok := strings.Cut(s, "[ERROR]: "); ok {
s, _, _ = strings.Cut(e, "\n")
return s
}
if i := strings.LastIndex(s, "\n"); i >= 0 {
s = s[i+1:]
}
+59
View File
@@ -0,0 +1,59 @@
package deploy
import (
"errors"
"os"
"os/exec"
"path/filepath"
"testing"
)
// Every play interzone writes must at least parse: Ansible's argument
// splitter rejects some shell quoting that YAML accepts (the Dashboard's
// play did, from v0.5.26 to v0.5.28). Needs ansible-playbook; nothing is
// contacted.
func TestPlaysParse(t *testing.T) {
bin, err := exec.LookPath("ansible-playbook")
if err != nil {
t.Skip("ansible-playbook isn't installed")
}
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "hosts"), []byte("[matrix_servers]\nmatrix.example.org ansible_host=192.0.2.1\n"), 0o644); err != nil {
t.Fatal(err)
}
for name, b := range map[string][]byte{"interzone-mirrors-rewrite.yml": mirrorsRewrite, "interzone-mirrors-metalinks.py": metalinksScript} {
if err := os.WriteFile(filepath.Join(dir, name), b, 0o644); err != nil {
t.Fatal(err)
}
}
plays := map[string]string{
"status": statusPlay, "alerts": alertsPlay, "backup": backupPlay, "restore-prep": restorePrepPlay,
"restore-finish": restoreFinishPlay, "schedule": schedulePlay, "fetch-backups": fetchBackupsPlay,
"uninstall": uninstallPlay, "disk": diskPlay, "tokens": tokensPlay, "mas": masPlay, "signup": signupPlay,
"admin": adminPlay, "mirrors": string(mirrorsPlay),
}
for name, play := range plays {
path := filepath.Join(dir, name+".yml")
if err := os.WriteFile(path, []byte(play), 0o644); err != nil {
t.Fatal(err)
}
cmd := exec.Command(bin, "--syntax-check", "-i", "hosts", path)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "ANSIBLE_LOCALHOST_WARNING=false", "ANSIBLE_HOME="+filepath.Join(dir, ".ansible"))
if out, err := cmd.CombinedOutput(); err != nil {
t.Errorf("the %s play doesn't parse: %v\n%s", name, err, out)
}
}
}
func TestLastLine(t *testing.T) {
for in, want := range map[string]string{
"a\nb\n": "b",
"[ERROR]: Error loading tasks: x\nOrigin: f.yml:6:7\n\n6 - name\n ^ column 7\n": "Error loading tasks: x",
"": "fallback",
} {
if got := lastLine(in, errors.New("fallback")); got != want {
t.Errorf("lastLine(%q) = %q, want %q", in, got, want)
}
}
}
+35 -33
View File
@@ -110,39 +110,41 @@ const statusPlay = `- name: interzone status
gather_facts: false
tasks:
- name: interzone status
ansible.builtin.shell: !unsafe |
echo "load=$(cut -d' ' -f1-3 /proc/loadavg)"
echo "cpus=$(nproc)"
echo "uptime=$(cut -d' ' -f1 /proc/uptime)"
awk '/^MemTotal:/{print "memtotal="$2} /^MemAvailable:/{print "memavail="$2} /^SwapTotal:/{print "swaptotal="$2} /^SwapFree:/{print "swapfree="$2}' /proc/meminfo
(. /etc/os-release 2>/dev/null && echo "os=$PRETTY_NAME")
echo "kernel=$(uname -r)"
echo "hostname=$(hostname)"
echo "docker=$(docker version --format '{{.Server.Version}}' 2>/dev/null)"
df -Pk / /matrix 2>/dev/null | awk 'NR>1{print "disk="$6" "$2" "$3" "$4}'
echo "timer=$(systemctl show interzone-backup.timer -p NextElapseUSecRealtime --value 2>/dev/null)"
docker ps -a --filter name=^matrix- --format 'ps={{json .}}'
ids=$(docker ps -q --filter name=^matrix-)
if [ -n "$ids" ]; then docker stats --no-stream --format 'stats={{json .}}' $ids; fi
all=$(docker ps -aq --filter name=^matrix-)
if [ -n "$all" ]; then docker inspect --format 'inspect={{.Name}} {{.RestartCount}} {{if .State.Health}}{{.State.Health.Status}}{{end}}' $all; fi
docker ps --filter name=^matrix- --filter label=traefik.enable=true --format 'labels={{.Names}} {{.Labels}}'
if [ ! -x /usr/local/bin/interzone-stats ]; then
cat > /usr/local/bin/interzone-stats <<'EOF'
#!/bin/sh
# Written by interzone: notes load, memory and disk for its dashboard's history.
f=/var/lib/interzone-stats
echo "$(date +%s) $(cut -d' ' -f1 /proc/loadavg) $(nproc) $(awk '/^MemTotal:/{t=$2} /^MemAvailable:/{a=$2} END{print t-a, t}' /proc/meminfo) $( (df -Pk /matrix || df -Pk /) 2>/dev/null | awk 'NR==2{print $3, $2}')" >> $f
tail -n 4320 $f > $f.new && mv $f.new $f
EOF
chmod 755 /usr/local/bin/interzone-stats
printf '[Unit]\nDescription=Note load, memory and disk (interzone)\n\n[Service]\nType=oneshot\nExecStart=/usr/local/bin/interzone-stats\n' > /etc/systemd/system/interzone-stats.service
printf '[Unit]\nDescription=Note load, memory and disk (interzone)\n\n[Timer]\nOnCalendar=*:0/10\n\n[Install]\nWantedBy=timers.target\n' > /etc/systemd/system/interzone-stats.timer
systemctl daemon-reload && systemctl enable --now interzone-stats.timer
fi >/dev/null 2>&1
if [ "$IZ_HISTORY" = true ] && [ -f /var/lib/interzone-stats ]; then
awk '{l = $0} NR % 3 == 0 {print "hist=" $0} END {if (NR % 3) print "hist=" l}' /var/lib/interzone-stats
fi
ansible.builtin.shell:
# cmd: Ansible's splitter, used for the free form, rejects this quoting.
cmd: !unsafe |
echo "load=$(cut -d' ' -f1-3 /proc/loadavg)"
echo "cpus=$(nproc)"
echo "uptime=$(cut -d' ' -f1 /proc/uptime)"
awk '/^MemTotal:/{print "memtotal="$2} /^MemAvailable:/{print "memavail="$2} /^SwapTotal:/{print "swaptotal="$2} /^SwapFree:/{print "swapfree="$2}' /proc/meminfo
(. /etc/os-release 2>/dev/null && echo "os=$PRETTY_NAME")
echo "kernel=$(uname -r)"
echo "hostname=$(hostname)"
echo "docker=$(docker version --format '{{.Server.Version}}' 2>/dev/null)"
df -Pk / /matrix 2>/dev/null | awk 'NR>1{print "disk="$6" "$2" "$3" "$4}'
echo "timer=$(systemctl show interzone-backup.timer -p NextElapseUSecRealtime --value 2>/dev/null)"
docker ps -a --filter name=^matrix- --format 'ps={{json .}}'
ids=$(docker ps -q --filter name=^matrix-)
if [ -n "$ids" ]; then docker stats --no-stream --format 'stats={{json .}}' $ids; fi
all=$(docker ps -aq --filter name=^matrix-)
if [ -n "$all" ]; then docker inspect --format 'inspect={{.Name}} {{.RestartCount}} {{if .State.Health}}{{.State.Health.Status}}{{end}}' $all; fi
docker ps --filter name=^matrix- --filter label=traefik.enable=true --format 'labels={{.Names}} {{.Labels}}'
if [ ! -x /usr/local/bin/interzone-stats ]; then
cat > /usr/local/bin/interzone-stats <<'EOF'
#!/bin/sh
# Written by interzone: notes load, memory and disk for its dashboard's history.
f=/var/lib/interzone-stats
echo "$(date +%s) $(cut -d' ' -f1 /proc/loadavg) $(nproc) $(awk '/^MemTotal:/{t=$2} /^MemAvailable:/{a=$2} END{print t-a, t}' /proc/meminfo) $( (df -Pk /matrix || df -Pk /) 2>/dev/null | awk 'NR==2{print $3, $2}')" >> $f
tail -n 4320 $f > $f.new && mv $f.new $f
EOF
chmod 755 /usr/local/bin/interzone-stats
printf '[Unit]\nDescription=Note load, memory and disk (interzone)\n\n[Service]\nType=oneshot\nExecStart=/usr/local/bin/interzone-stats\n' > /etc/systemd/system/interzone-stats.service
printf '[Unit]\nDescription=Note load, memory and disk (interzone)\n\n[Timer]\nOnCalendar=*:0/10\n\n[Install]\nWantedBy=timers.target\n' > /etc/systemd/system/interzone-stats.timer
systemctl daemon-reload && systemctl enable --now interzone-stats.timer
fi >/dev/null 2>&1
if [ "$IZ_HISTORY" = true ] && [ -f /var/lib/interzone-stats ]; then
awk '{l = $0} NR % 3 == 0 {print "hist=" $0} END {if (NR % 3) print "hist=" l}' /var/lib/interzone-stats
fi
environment:
IZ_HISTORY: "{{ interzone_history | default(false) }}"
changed_when: false
+4 -2
View File
@@ -39,13 +39,15 @@ func TestStatusHistory(t *testing.T) {
func TestStatsScript(t *testing.T) {
var play []struct {
Tasks []struct {
Shell string `yaml:"ansible.builtin.shell"`
Shell struct {
Cmd string `yaml:"cmd"`
} `yaml:"ansible.builtin.shell"`
}
}
if err := yaml.Unmarshal([]byte(statusPlay), &play); err != nil {
t.Fatal(err)
}
sh := play[0].Tasks[0].Shell
sh := play[0].Tasks[0].Shell.Cmd
if err := exec.Command("bash", "-n", "-c", sh).Run(); err != nil {
t.Fatal("the status shell doesn't parse:", err)
}