// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved. package main // The console's commands on the Hub: // updates, restart, rollback, logs to support, add-ons, the update channel, // the recovery system, a pushed release, the remote terminal and support in // the panel. Each answers first; a restart comes after the answer is sent. import ( "fmt" "os" "os/exec" "regexp" "strings" "time" ) const channelFile = "/data/cantabile/update-channel" // read by /usr/lib/cantabile/update var versionRE = regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$`) func runCmd(name string, args ...string) (string, error) { out, err := exec.Command(name, args...).CombinedOutput() s := strings.TrimSpace(string(out)) if len(s) > 300 { s = s[len(s)-300:] } return s, err } func reboot() { time.Sleep(5 * time.Second); exec.Command("systemctl", "reboot").Run() } // hubDo: (handled, ok, message, after). Not handled: the agent's command. func (l *link) hubDo(c command) (bool, bool, string, func()) { switch c.Cmd { case "refresh": l.publish("hb", l.hubHeartbeat()) l.publish("status", l.status()) l.sendHardware(true) return true, true, "status sent", nil case "update-check": _, err := runCmd("systemctl", "start", "--no-block", "cantabile-update.service") return true, err == nil, "update check started", nil case "restart": event("restart", "restart requested by Cantabile") return true, true, "restarting in 5 s", reboot case "rollback": out, err := runCmd("rauc", "status", "mark-active", "other") if err != nil { return true, false, "rollback refused: " + out, nil } event("rollback", "rollback requested by Cantabile") return true, true, "switched to the other slot; restarting in 5 s", reboot case "collect-logs": if out, err := runCmd(logsTool, "bundle"); err != nil { return true, false, "bundle: " + out, nil } msg := c.Args["message"] if msg == "" { msg = "Logs requested by Cantabile support." } out, err := runCmd(logsTool, "upload", msg) return true, err == nil, out, nil case "addon": // Through systemd, as the panel does: an add-on merged in this // service's own namespace would not be seen by the rest of the box. args := []string{"--quiet", "--wait", "--pipe", "--collect", "/usr/lib/cantabile/addons", c.Args["op"], c.Args["id"]} what := c.Args["op"] + " " + c.Args["id"] if v := c.Args["version"]; v != "" { if c.Args["op"] != "install" || !versionRE.MatchString(v) { return true, false, "bad version " + v, nil } args, what = append(args, v), what+" "+v } out, err := runCmd("systemd-run", args...) if err != nil { return true, false, out, nil } return true, true, what + ": done", nil case "channel": ch := c.Args["channel"] if ch != "stable" && ch != "beta" && ch != "dev" { return true, false, "unknown channel", nil } if err := os.WriteFile(channelFile, []byte(ch+"\n"), 0o644); err != nil { return true, false, err.Error(), nil } event("channel", "update channel set to "+ch+" by Cantabile") return true, true, "follows " + ch, nil case "terminal-open": ok, msg := l.termOpen(c) return true, ok, msg, nil case "panel-open": ok, msg := l.panelOpen(c) return true, ok, msg, nil case "recovery": ok, msg, after := toRecovery() return true, ok, msg, after case "ota-push": ok, msg := otaPush(c.Args["version"]) return true, ok, msg, nil } return false, false, "", nil } func hasRecovery() bool { for _, d := range []string{espRecovery + "-a", espRecovery + "-b", espRecovery} { if _, err := os.Stat(d + "/vmlinuz"); err == nil { return true } } return false } // toRecovery: the next boot starts the recovery system once (RECOVERY=1, // hub/recovery/init). func toRecovery() (bool, string, func()) { if !hasRecovery() { return false, "this box has no recovery system", nil } if out, err := runCmd("grub-editenv", grubenv, "set", "RECOVERY=1"); err != nil { return false, "grubenv: " + out, nil } exec.Command("sync").Run() event("recovery", "restart into recovery requested by Cantabile") return true, "restarting into recovery in 5 s", reboot } // otaPush: that release now, whatever the channel and quiet hours, in its // own unit (the install outlives this answer and the link). One at a time. func otaPush(v string) (bool, string) { if !versionRE.MatchString(v) { return false, "bad version" } if st, _ := runCmd("systemctl", "show", "cantabile-update-push", "-p", "ActiveState", "-p", "ExecMainStartTimestamp", "-p", "Description"); st != "" { p := map[string]string{} for _, ln := range strings.Split(st, "\n") { if k, val, ok := strings.Cut(ln, "="); ok { p[k] = strings.TrimSpace(val) } } switch p["ActiveState"] { case "active", "activating", "deactivating", "reloading": was := "" if f := strings.Fields(p["Description"]); len(f) > 0 { was = f[len(f)-1] } return false, fmt.Sprintf("a push of %s is already running (since %s): wait for it to end, or restart the box to stop it", was, p["ExecMainStartTimestamp"]) case "failed": runCmd("systemctl", "reset-failed", "cantabile-update-push") } } out, err := runCmd("systemd-run", "--unit=cantabile-update-push", "--description=Cantabile pushed release "+v, "--collect", "--no-block", "/usr/lib/cantabile/update", "push", v) if err != nil { return false, out } return true, "installing " + v + " (restarts when done)" }