Files
dawnline/backend/updater/updater.go
T
Aemeath ac90575b0b
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
Dawnline 0.20.0
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode.
- **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V).
- **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request.
- **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds.
- **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person.
- **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router.
- **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch.
- **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away.
- **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once.
- **Undo.** An action done from a card or a request is undone from its toast.
- **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own.
- **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name.
- **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking.
- **Check access and Health know the networks behind routers.**
- **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st.
- Extra data lasts until that person's month ends; «Reset usage» starts the month anew.
- A limit is for a month or for the whole term; the admin and the portal show when it resets.
- Charts that said «this month» show the last 30 days.
- After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st.
- **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped.
- **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone.
- **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence.
- **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
2026-11-13 09:43:50 +03:00

674 lines
19 KiB
Go

package updater
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/github"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/pkgmgr"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
const usage = `Usage:
updater check [app|amneziawg|packages]
updater install app | app@<tag> | app:reinstall | app:rollback
updater install amneziawg[:auto|slava|grey[:ru]] | amneziawg:rollback
updater install pkgs:<name,…>
updater auto
`
// Env is where a job works.
type Env struct {
RunDir, DataDir string
// Version is the installed Dawnline; Policy the settings' (Settings → Updates).
Version string
Policy model.Updates
Pkg *pkgmgr.Manager
GH *github.Client
Fetch github.Fetch
// T is the job's language (the phases and errors of a job the admin started; nil: English).
T *i18n.Catalog
// Run runs a shell command: what it said (both streams) and its exit code.
Run func(cmd string) (string, int)
// Board is the firmware as `ubus call system board` tells it (release version, target, arch).
Board func() (version, target, arch string)
// Langs is the languages of the translations installed here.
Langs func() []string
Log *servicelog.Log
Now func() time.Time
Pid int
// Sleep waits (a test does not).
Sleep func(time.Duration)
}
type failure struct{ msg string }
// Job is update-job.json.
type Job struct {
ID string `json:"id"`
Action string `json:"action"`
Component any `json:"component"`
State string `json:"state"`
Started int64 `json:"started"`
Finished any `json:"finished"`
Pid int `json:"pid"`
Auto bool `json:"auto"`
Phase string `json:"phase"`
From any `json:"from"`
To any `json:"to"`
Log []string `json:"log"`
Error any `json:"error"`
Result map[string]any `json:"result"`
Current string `json:"current,omitempty"`
}
type worker struct {
e *Env
// quiet: an automatic run only shows up as a job once it installs something
quiet bool
job Job
// what to restart once the job is over (never in the middle of it)
after map[string]bool
work string
}
func (w *worker) now() int64 { return w.e.Now().Unix() }
func (w *worker) t(msg string) string { return w.e.T.T(msg) }
func (w *worker) die(format string, args ...any) {
panic(failure{fmt.Sprintf(w.t(format), args...)})
}
func (w *worker) save() {
if !w.quiet {
files.WriteJSON(w.e.RunDir+"/update-job.json", w.job, 0o644)
}
}
func (w *worker) log(msg string) {
t := w.e.Now().In(zone.Local())
w.job.Log = append(w.job.Log, fmt.Sprintf("%02d:%02d:%02d %s", t.Hour(), t.Minute(), t.Second(), msg))
if n := len(w.job.Log); n > 400 {
w.job.Log = w.job.Log[n-400:]
}
w.save()
}
func (w *worker) logf(format string, args ...any) { w.log(fmt.Sprintf(w.t(format), args...)) }
func (w *worker) phase(format string, args ...any) {
w.job.Phase = fmt.Sprintf(w.t(format), args...)
w.log(w.job.Phase)
}
// run is a command with what it said in the log.
func (w *worker) run(cmd string) (string, int) {
out, code := w.e.Run(cmd)
for _, line := range strings.Split(strings.Trim(out, " \t\r\n\v\f"), "\n") {
if strings.Trim(line, " \t\r\n\v\f") != "" {
w.log(" " + line)
}
}
return out, code
}
func (w *worker) versions(from, to string) {
w.job.From, w.job.To = orNil(from), orNil(to)
w.save()
}
func orNil(s string) any {
if s == "" {
return nil
}
return s
}
func (w *worker) slog(src, level, msg, detail, who string) {
if w.e.Log != nil {
w.e.Log.Write(src, level, msg, detail, who, servicelog.Extra{})
}
}
func (w *worker) history(e map[string]any) { historyAdd(w.e.DataDir, w.now(), e) }
/* ---------- check results ---------- */
type checkFile struct {
Components map[string]map[string]any `json:"components"`
Time int64 `json:"time"`
}
func (w *worker) checks() map[string]map[string]any {
var c checkFile
files.ReadJSON(w.e.RunDir+"/update-check.json", &c)
if c.Components == nil {
c.Components = map[string]map[string]any{}
}
return c.Components
}
// saveCheck puts components' results into the check file (the others stay as they were).
func (w *worker) saveCheck(res map[string]map[string]any) {
c := checkFile{Components: w.checks()}
for k, v := range res {
v["time"] = w.now()
c.Components[k] = v
}
c.Time = w.now()
files.WriteJSON(w.e.RunDir+"/update-check.json", c, 0o644)
}
func str(v any) string { s, _ := v.(string); return s }
func num(v any) int {
switch x := v.(type) {
case int:
return x
case int64:
return int(x)
case float64:
return int(x)
}
return 0
}
// foundIn is what a check found, as short names ("Dawnline 0.3.3", "2 system packages"): kept in the
// history and shown later, so English whatever the job's language.
func foundIn(res map[string]map[string]any) []string {
var found []string
if a := res["app"]; a != nil && a["update"] == true {
found = append(found, "Dawnline "+str(a["latest"]))
}
if a := res["amneziawg"]; a != nil && a["update"] == true && a["installed"] != nil {
found = append(found, "AmneziaWG "+str(a["latest"]))
}
if p := res["packages"]; p != nil && num(p["updates"]) > 0 {
if n := num(p["updates"]); n == 1 {
found = append(found, "1 system package")
} else {
found = append(found, fmt.Sprintf("%d system packages", n))
}
}
return found
}
func (w *worker) checking(name string) string {
switch name {
case "app":
return "Checking Dawnline"
case "amneziawg":
return "Checking AmneziaWG"
}
return "Checking system packages"
}
func (w *worker) updating(name string) string {
switch name {
case "app":
return "Updating Dawnline"
case "amneziawg":
return "Updating AmneziaWG"
}
return "Updating system packages"
}
func (w *worker) check(name string) map[string]any {
switch name {
case "app":
return w.checkApp()
case "amneziawg":
return w.checkAwg()
}
return w.checkPkgs(true)
}
func (w *worker) runChecks(which []string) map[string]map[string]any {
res := map[string]map[string]any{}
for _, name := range which {
w.phase(w.checking(name))
func() {
defer func() {
if r := recover(); r != nil {
msg := fmt.Sprint(r)
if f, is := r.(failure); is {
msg = f.msg
}
w.logf("Error: %s", msg)
res[name] = map[string]any{"error": msg, "status": ""}
}
}()
res[name] = w.check(name)
}()
}
w.saveCheck(res)
return res
}
type autoState struct {
InstallDay string `json:"install_day,omitempty"`
Check int64 `json:"check,omitempty"`
Announced string `json:"announced"`
}
func (w *worker) autoState() autoState {
var st autoState
files.ReadJSON(w.e.DataDir+"/update-auto.json", &st)
return st
}
func (w *worker) autoSave(st autoState) {
os.MkdirAll(w.e.DataDir, 0o700)
files.WriteJSON(w.e.DataDir+"/update-auto.json", st, 0o600)
}
// noteFound: one line in Activity a new finding (the same finding is not repeated).
func (w *worker) noteFound(res map[string]map[string]any, st *autoState, auto bool) {
found := foundIn(res)
sig := strings.Join(found, ", ")
if len(found) > 0 && sig != st.Announced {
if auto {
w.slog("dawnline", "info", "updates available · "+sig, "", "Components")
}
w.history(map[string]any{"kind": "found", "items": found, "auto": auto})
}
st.Announced = sig
}
/* ---------- installs ---------- */
// splitTarget: "app@v1" → app, "@v1"; "amneziawg:slava:ru" → amneziawg, "slava:ru"; "app" → app, "".
func splitTarget(s string) (name, arg string) {
i, at := strings.Index(s, ":"), strings.Index(s, "@")
cut := i
if i < 0 || (at >= 0 && at < i) {
cut = at
}
if cut < 0 {
return s, ""
}
if s[cut] == ':' {
return s[:cut], s[cut+1:]
}
return s[:cut], s[cut:]
}
func appStatus(installed, latest string) string {
if latest == "" {
return "latest"
}
switch c := VersionCmp(installed, latest); {
case c < 0:
return "outdated"
case c > 0:
return "dev"
}
return "latest"
}
// logUpdate is a line in the service log for each finished update (Dawnline's own is told when it starts again).
func (w *worker) logUpdate(name string, r map[string]any) {
switch name {
case "amneziawg":
before, _ := r["before"].(map[string]any)
after, _ := r["after"].(map[string]any)
b, a := str(before["kmod-amneziawg"]), str(after["kmod-amneziawg"])
msg := fmt.Sprintf("installed the %s build", str(r["build"]))
switch {
case b != "" && a != "" && b != a && w.job.Auto:
msg = fmt.Sprintf("updated automatically · %s → %s", b, a)
case b != "" && a != "" && b != a:
msg = fmt.Sprintf("updated · %s → %s", b, a)
case a != "":
msg = fmt.Sprintf("installed the %s build · %s", str(r["build"]), a)
}
w.slog("awg", "up", msg, "", "AmneziaWG")
case "pkgs":
updated, _ := r["updated"].([]string)
installed, _ := r["installed"].([]string)
if all := append(append([]string{}, updated...), installed...); len(all) > 0 {
msg := "updated · "
if w.job.Auto {
msg = "updated automatically · "
}
w.slog("dawnline", "up", msg+strings.Join(all, ", "), "", "System packages")
}
}
}
func (w *worker) install(what []string) {
if w.job.Result == nil {
w.job.Result = map[string]any{}
}
for _, target := range what {
name, arg := splitTarget(target)
var r map[string]any
w.job.Current = name
switch name {
case "app":
w.phase(w.updating(name))
r = w.installApp(arg)
case "amneziawg":
w.phase(w.updating(name))
r = w.installAwg(arg)
case "pkgs":
w.phase(w.updating(name))
r = w.installPkgs(arg)
default:
w.die("Unknown component %s", name)
}
w.job.Result[name] = r
w.logUpdate(name, r)
// installed components are current now: no stale "Update available"
c := w.checks()
switch {
case name == "pkgs" && r["check"] != nil:
w.saveCheck(map[string]map[string]any{"packages": r["check"].(map[string]any)})
case name == "app" && str(r["to"]) != "" && c["app"] != nil:
s := appStatus(str(r["to"]), str(c["app"]["latest"]))
a := c["app"]
a["installed"], a["update"], a["status"] = r["to"], s == "outdated", s
w.saveCheck(map[string]map[string]any{"app": a})
case name == "amneziawg" && r["after"] != nil:
a := c["amneziawg"]
if a == nil {
a = map[string]any{}
}
after := r["after"].(map[string]any)
a["installed"], a["packages"], a["update"], a["status"] = after["amneziawg-tools"], after, false, "latest"
if r["build"] != nil {
a["current"] = r["build"]
}
w.saveCheck(map[string]map[string]any{"amneziawg": a})
}
w.history(map[string]any{"kind": "install", "component": target, "from": r["from"], "to": r["to"], "state": "done", "auto": w.job.Auto,
"took": w.now() - w.job.Started})
w.job.Current = ""
}
}
var clock = regexp.MustCompile(`^([0-9]{1,2}):([0-9]{2})$`)
var intervals = map[string]int64{"6h": 6 * 3600, "12h": 12 * 3600, "1d": 86400, "7d": 7 * 86400}
// slot is the scheduled slot now is in (up to an hour after the set time, also across midnight).
func slot(pol model.Updates, now time.Time) (time.Time, bool) {
hm := clock.FindStringSubmatch(pol.Time)
if hm == nil {
return time.Time{}, false
}
var h, mi int
fmt.Sscan(hm[1], &h)
fmt.Sscan(hm[2], &mi)
for _, day := range []time.Time{now, now.Add(-24 * time.Hour)} {
d := day.In(zone.Local())
at := time.Unix(day.Unix()-int64(d.Hour()*3600+d.Minute()*60+d.Second())+int64(h*3600+mi*60), 0)
if !now.Before(at) && now.Sub(at) < time.Hour {
return at, true
}
}
return time.Time{}, false
}
func dayOf(t time.Time) string { return t.In(zone.Local()).Format("2006-01-02") }
// due is what an automatic run should do now: "install" (the scheduled window, once a slot), "check"
// (the interval has passed), or "".
func due(pol model.Updates, st autoState, now time.Time) string {
if pol.Auto && pol.Mode == "scheduled" && len(pol.Targets) > 0 {
if at, in := slot(pol, now); in && st.InstallDay != dayOf(at) {
return "install"
}
}
every := intervals[pol.Interval]
if every == 0 {
every = 86400
}
if pol.Check && now.Unix()-st.Check >= every {
return "check"
}
return ""
}
func has(list []string, v string) bool {
for _, x := range list {
if x == v {
return true
}
}
return false
}
// auto is Settings → Updates: check when due, install what is allowed (scheduled or right away).
func (w *worker) auto() {
pol := w.e.Policy
st := w.autoState()
what := due(pol, st, w.e.Now())
if what == "" {
return
}
now := w.now()
var res map[string]map[string]any
if what == "check" || now-st.Check >= 3600 {
res = w.runChecks(Targets)
st.Check = now
w.noteFound(res, &st, true)
} else {
res = w.checks()
}
if what == "install" {
at, in := slot(pol, w.e.Now())
if !in {
at = w.e.Now()
}
st.InstallDay = dayOf(at)
}
w.autoSave(st)
if !pol.Auto || (what != "install" && pol.Mode != "immediate") {
return
}
var todo []string
// packages first, Dawnline last: it restarts the service and itself
if p := res["packages"]; has(pol.Targets, "packages") && p != nil && num(p["updates"]) > 0 {
var names []string
pkgs, _ := p["pkgs"].(map[string]any)
for name, v := range pkgs {
one, _ := v.(map[string]any)
with, _ := one["with"].([]any)
withS, _ := one["with"].([]string)
if one["latest"] != nil || len(with) > 0 || len(withS) > 0 {
names = append(names, name)
}
}
sort.Strings(names)
if len(names) > 0 {
todo = append(todo, "pkgs:"+strings.Join(names, ","))
}
}
if a := res["amneziawg"]; has(pol.Targets, "amneziawg") && a != nil && a["update"] == true && a["installed"] != nil {
// only the same build, same generation: anything else needs a person to decide
if cur := str(a["current"]); cur != "" && a["gen_change"] != true {
ru := ""
if a["ru"] == true {
ru = ":ru"
}
todo = append(todo, "amneziawg:"+cur+ru)
} else if latest := str(a["latest"]); latest != "" {
w.slog("awg", "info", latest+" needs a manual switch in Components", "", "AmneziaWG")
} else {
w.slog("awg", "info", "a new build needs a manual switch in Components", "", "AmneziaWG")
}
}
if a := res["app"]; has(pol.Targets, "app") && a != nil && a["update"] == true {
todo = append(todo, "app")
}
if len(todo) == 0 {
return
}
w.quiet = false
w.job.Action, w.job.Component, w.job.Auto, w.job.Log = "install", strings.Join(todo, " "), true, nil
w.logf("Automatic update: %s", strings.Join(todo, ", "))
w.install(todo)
}
var plainVersion = regexp.MustCompile(`[^A-Za-z0-9.-]`)
// restarts is what the job asked for, in one go, after its final state is saved. Dawnline's own package is
// restarted by postupdate, a shell that also checks the new version and rolls back to the kept one when
// it does not come up.
func (w *worker) restarts() {
if w.after["app"] {
w.e.Run("/usr/libexec/dawnline/postupdate '" + plainVersion.ReplaceAllString(str(w.job.To), "") + "' </dev/null >/dev/null 2>&1 &")
return
}
var cmds []string
if w.after["apply"] {
cmds = append(cmds, "/usr/bin/dawnline apply")
}
// LuCI's own web server reads a language's catalogs once: a new translation needs a fresh one
if w.after["uhttpd"] || w.after["luci"] {
cmds = append(cmds, "/etc/init.d/uhttpd restart")
}
if w.after["apply"] || w.after["uhttpd"] {
cmds = append(cmds, "/etc/init.d/dawnline restart")
}
if len(cmds) > 0 {
w.e.Run("( sleep 2; " + strings.Join(cmds, "; ") + " ) </dev/null >/dev/null 2>&1 &")
}
}
var who = map[string]string{"app": "Dawnline", "amneziawg": "AmneziaWG", "pkgs": "System packages", "packages": "System packages"}
// main is the job the command line asks for, to its final state: done, or failed with the error in the
// log and the history.
func (w *worker) main() {
defer func() {
r := recover()
if r == nil {
return
}
msg := fmt.Sprint(r)
if f, is := r.(failure); is {
msg = f.msg
} else if f, is := r.(pkgmgr.Failure); is {
msg = f.Msg
}
w.quiet = false
w.job.State, w.job.Error = "failed", msg
w.logf("Error: %s", msg)
name := w.job.Current
if name == "" {
name, _ = splitTarget(strings.SplitN(str(w.job.Component), " ", 2)[0])
}
if w.job.Action != "check" {
src, said, subject := "dawnline", "update failed", who[name]
if name == "amneziawg" {
src = "awg"
}
if w.job.Auto {
said = "automatic update failed"
}
if subject == "" {
subject = "Dawnline"
}
w.slog(src, "error", said, msg, subject)
w.history(map[string]any{"kind": "install", "component": name, "from": w.job.From, "to": w.job.To, "state": "failed", "auto": w.job.Auto, "error": msg,
"took": w.now() - w.job.Started})
} else {
component := str(w.job.Component)
if component == "" {
component = "all"
}
w.history(map[string]any{"kind": "error", "component": component, "state": "failed", "error": msg})
}
}()
switch w.job.Action {
case "check":
which := Targets
if c := str(w.job.Component); c != "" {
which = []string{c}
}
for _, x := range which {
if !has(Targets, x) {
w.die("Unknown component %s", x)
}
}
res := w.runChecks(which)
w.job.Result = map[string]any{}
for k, v := range res {
w.job.Result[k] = v
}
st := w.autoState()
w.noteFound(res, &st, false)
if len(which) == len(Targets) {
st.Check = w.now()
}
w.autoSave(st)
case "install":
w.install([]string{str(w.job.Component)})
case "auto":
w.auto()
}
w.job.State = "done"
w.phase("Finished")
}
// Main runs the updater's command line and returns its exit code. out and errs take the usage.
func Main(e *Env, args []string, out, errs interface{ Write([]byte) (int, error) }) int {
first := ""
if len(args) > 0 {
first = args[0]
}
// No command, one it does not know, or an install of nothing: the usage and out, before anything is
// read or written. Asked for help: the same, on the other stream.
if first == "-h" || first == "--help" || first == "help" {
out.Write([]byte(usage))
return 0
}
if (first != "check" && first != "install" && first != "auto") || (first == "install" && (len(args) < 2 || args[1] == "")) {
errs.Write([]byte(usage))
return 2
}
if e.Now == nil {
e.Now = time.Now
}
if e.Sleep == nil {
e.Sleep = time.Sleep
}
w := &worker{e: e, quiet: first == "auto", after: map[string]bool{}, work: fmt.Sprintf("/tmp/dawnline-update.%d", e.Pid)}
now := w.now()
w.job = Job{ID: fmt.Sprintf("%d-%d", now, e.Pid), Action: first, State: "running", Started: now, Pid: e.Pid, Log: []string{}}
if len(args) > 1 {
w.job.Component = args[1]
}
os.MkdirAll(e.RunDir, 0o755)
// one job at a time (the page refuses a second one; an automatic run just skips its turn)
var held *os.File
for i := 0; i < 10 && held == nil; i++ {
if held = lock(e.RunDir); held == nil {
e.Sleep(100 * time.Millisecond)
}
}
if held == nil {
return 0
}
defer held.Close()
os.MkdirAll(w.work, 0o700)
w.save()
w.main()
os.RemoveAll(w.work)
w.job.Finished, w.job.Current = w.now(), ""
w.save()
w.restarts()
return 0
}