Dawnline 0.20.0
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

- **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.
This commit is contained in:
Aemeath committed 2026-11-13 09:43:50 +03:00
1 parent f8130e39a8
commit ac90575b0b
788 files changed
+139244 -41720

No files matched your search

+224
View File
@@ -0,0 +1,224 @@
package updater
import (
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/files"
)
// Where AmneziaWG comes from (dawnline.awg_builds): the awg-openwrt projects publish builds per OpenWrt
// release and target (the kernel module must match the firmware), and the record of what was installed on
// which kernel and boot (an update whose module could not be swapped starts with the next boot).
// Source is a project that publishes builds.
type Source struct{ Repo, Label string }
var (
Sources = map[string]Source{"slava": {"Slava-Shchipunov/awg-openwrt", "Slava-Shchipunov"}, "grey": {"2Grey/awg-openwrt", "2Grey"}}
// Order is the order they are tried in when no build is chosen.
Order = []string{"slava", "grey"}
// AwgPackages is a build's packages; AwgRU its Russian LuCI translation.
AwgPackages = []string{"kmod-amneziawg", "amneziawg-tools", "luci-proto-amneziawg"}
)
const AwgRU = "luci-i18n-amneziawg-ru"
// Firmware is this firmware as the builds name it: { Tag: "v24.10.6", Postfix:
// "_v24.10.6_aarch64_cortex-a53_mediatek_filogic", Ext }.
type Firmware struct {
Tag, Postfix, Ext string
Snapshot bool
}
var distribArch = regexp.MustCompile(`DISTRIB_ARCH='([^']*)'`)
// FirmwareOf makes it of what `ubus call system board` says of the release (version, target, arch; the
// arch from /etc/openwrt_release when the board does not say); ok is false when it cannot be told.
func FirmwareOf(version, target, arch, release, ext string) (Firmware, bool) {
parts := strings.Split(target, "/")
if arch == "" {
if m := distribArch.FindStringSubmatch(release); m != nil {
arch = m[1]
}
}
if version == "" || len(parts) != 2 || arch == "" {
return Firmware{}, false
}
return Firmware{Tag: "v" + version, Postfix: "_v" + version + "_" + arch + "_" + parts[0] + "_" + parts[1], Ext: ext,
Snapshot: strings.Contains(version, "SNAPSHOT")}, true
}
// Asset is the file name of a package in a build.
func (fw Firmware) Asset(name string) string { return name + fw.Postfix + "." + fw.Ext }
// ForTag is the same firmware, another release of its series (v24.10.6 → v24.10.8).
func (fw Firmware) ForTag(tag string) Firmware {
out := fw
out.Tag, out.Postfix = tag, strings.Replace(fw.Postfix, fw.Tag, tag, 1)
return out
}
var awgPart = regexp.MustCompile(`([0-9]+\.[0-9]+\.[0-9]{8})(-r?[0-9]+)?$`)
// Gen is the AmneziaWG generation of a package version ("" for none): 3.x packages carry 3.1; 1.0.x ones
// are 2.0 from July 2025 on, and the first generation before that. A kernel module's version carries the
// kernel release in front: the AmneziaWG part is at the end.
func Gen(version string) string {
if version == "" {
return ""
}
v := version
if m := awgPart.FindStringSubmatch(version); m != nil {
v = m[1]
}
switch {
case strings.HasPrefix(v, "3."):
return "3.1"
case strings.HasPrefix(v, "1.0.2024"):
return "1.0"
case len(v) >= 10 && strings.HasPrefix(v, "1.0.20250") && v[9] >= '1' && v[9] <= '6':
return "1.0"
}
return "2.0"
}
// Record is awg.json: which build was installed on which kernel and boot.
type Record struct {
OSRelease string `json:"osrelease"`
Source any `json:"source"`
Tag any `json:"tag"`
Packages map[string]any `json:"packages"`
Installed int64 `json:"installed"`
Boot any `json:"boot"`
PrevGen any `json:"prev_gen"`
PrevCore any `json:"prev_core"`
Reloaded any `json:"reloaded"`
}
func readTrim(path string) string {
raw, _ := os.ReadFile(path)
return strings.Trim(string(raw), " \t\r\n\v\f")
}
// bootID is this boot ("" when the kernel does not say); moduleCore the size in memory of the loaded
// kernel module (0: none is loaded). sysfs has no version for it, but builds differ in size.
func bootID() string { return readTrim("/proc/sys/kernel/random/boot_id") }
func moduleCore() int64 {
n, _ := strconv.ParseInt(readTrim("/sys/module/amneziawg/coresize"), 10, 64)
if n < 0 {
return 0
}
return n
}
// Pending tells an installed update that waits for a reboot: the install could not swap the kernel module
// (the old one stayed loaded), this is still the boot it ran in, and the module loaded is still the one
// from before it. Pure.
func Pending(rec *Record, boot string, core int64) bool {
if rec == nil || rec.Reloaded != false || boot == "" || rec.Boot != boot || core == 0 {
return false
}
prev, _ := rec.PrevCore.(float64)
return int64(prev) == core
}
// Module is the kernel module loaded now, taken before an install changes packages.
type Module struct {
Core int64
Gen string
Pending bool
}
func record(data string) *Record {
var r Record
if !files.ReadJSON(data+"/awg.json", &r) {
return nil
}
return &r
}
// moduleNow is what runs: the generation from before an install while it waits for a reboot, else the
// installed package's (version: the kmod-amneziawg package installed now).
func moduleNow(data, version string) Module {
rec, boot, core := record(data), bootID(), moduleCore()
m := Module{Core: core, Pending: Pending(rec, boot, core)}
switch {
case core == 0:
case m.Pending:
m.Gen, _ = rec.PrevGen.(string)
default:
m.Gen = Gen(version)
}
return m
}
// writeRecord remembers which build was installed on which kernel. After a firmware upgrade the module is
// gone (it was built for that kernel): Health can then say so and offer the build for the new one.
func writeRecord(data string, source, tag any, packages map[string]any, was Module, reloaded bool) {
orNil := func(s string) any {
if s == "" {
return nil
}
return s
}
var core any
if was.Core != 0 {
core = was.Core
}
os.MkdirAll(data, 0o700)
files.WriteJSON(data+"/awg.json", map[string]any{"osrelease": readTrim("/proc/sys/kernel/osrelease"), "source": source, "tag": tag, "packages": packages,
"installed": time.Now().Unix(), "boot": orNil(bootID()), "prev_gen": orNil(was.Gen), "prev_core": core, "reloaded": reloaded}, 0o600)
}
// GoArch is the name the backend's binary carries in a release (dawnlined-<version>-<goarch>) for a
// router's architecture as OpenWrt names it (DISTRIB_ARCH: aarch64_cortex-a53, arm_cortex-a7_neon-vfpv4,
// mipsel_24kc, x86_64), or "" where none is built: such a router runs without the backend. ARM older than
// v7 has none (the binary is built for ARMv7's own instructions).
func GoArch(arch string) string {
cpu := strings.SplitN(arch, "_", 2)
sub := ""
if len(cpu) > 1 {
sub = cpu[1]
}
switch cpu[0] {
case "aarch64":
return "arm64"
case "x86":
if sub == "64" {
return "amd64"
}
case "mips":
return "mips"
case "mipsel":
return "mipsle"
case "arm":
// Cortex-A cores are ARMv7 or newer; arm926ej-s, arm1176jzf-s, xscale, fa526 and their like are not
if strings.HasPrefix(sub, "cortex-a") {
return "arm"
}
}
return ""
}
// Adopt makes a record for a module Dawnline did not install (install.sh, by hand, or from before records
// were kept), so the next firmware upgrade is noticed: the kernel it is for, no source. Only when there is
// no record and the module is loaded or on disk for the running kernel. The backend calls it when it starts.
func Adopt(data string) {
rel := readTrim("/proc/sys/kernel/osrelease")
if _, err := os.Stat(data + "/awg.json"); err == nil {
return
}
_, loaded := os.Stat("/sys/module/amneziawg")
_, onDisk := os.Stat("/lib/modules/" + rel + "/amneziawg.ko")
if loaded != nil && (rel == "" || onDisk != nil) {
return
}
os.MkdirAll(data, 0o700)
files.WriteJSON(data+"/awg.json", map[string]any{"osrelease": rel, "source": nil, "tag": nil, "packages": nil, "installed": time.Now().Unix(),
"boot": bootID(), "prev_gen": nil, "prev_core": nil, "reloaded": nil}, 0o600)
}
+721
View File
@@ -0,0 +1,721 @@
package updater
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/github"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
func (w *worker) must(ok bool, err error, format string, args ...any) {
if err != nil {
panic(err)
}
if !ok {
w.die(format, args...)
}
}
func (w *worker) download(url, dest, want string) bool {
return github.Download(w.e.Fetch, url, dest, want, w.e.Policy.Mirrors, w.log, w.t) != ""
}
/* ---------- Dawnline itself ---------- */
func (w *worker) checkApp() map[string]any {
v := w.e.Version
r := w.e.GH.Latest(Repo)
if r == nil {
return map[string]any{"installed": v, "latest": nil, "update": false, "repo": Repo, "status": "", "note": fmt.Sprintf(w.t("Cannot reach github.com/%s"), Repo)}
}
tag := str(r["tag"])
latest := strings.TrimPrefix(tag, "v")
st := appStatus(v, latest)
switch st {
case "outdated":
w.logf("Dawnline update is available: %s → %s", v, latest)
case "dev":
w.logf("Installed Dawnline %s is newer than the release %s", v, latest)
default:
w.logf("Dawnline is up to date (%s)", v)
}
return map[string]any{"installed": v, "latest": latest, "tag": tag, "repo": Repo, "url": r["url"], "published": r["published"], "update": st == "outdated", "status": st}
}
var (
unsafeTag = regexp.MustCompile(`[^A-Za-z0-9.-]`)
sumLine = regexp.MustCompile(`^([0-9a-f]{64})[ \t]+\*?(\S+)$`)
)
// wantSHA is the sha256 a release publishes for a file: GitHub's own (API), else the release's
// sha256sums.txt fetched straight from GitHub (never a mirror).
func (w *worker) wantSHA(tag, asset, dir string) string {
if want := w.e.GH.AssetSHA256(Repo, tag, asset); want != "" {
return want
}
sums := dir + "/sha256sums-" + unsafeTag.ReplaceAllString(tag, "_") + ".txt"
if github.Download(w.e.Fetch, "https://github.com/"+Repo+"/releases/download/"+tag+"/sha256sums.txt", sums, "", false, w.log, w.t) == "" {
return ""
}
raw, _ := os.ReadFile(sums)
for _, line := range strings.Split(string(raw), "\n") {
if m := sumLine.FindStringSubmatch(strings.Trim(line, " \t\r\n\v\f")); m != nil && m[2] == asset {
return m[1]
}
}
return ""
}
// fetchFile downloads one file of a release, verified against its sha256. optional: a release without it
// (one from before the translations) gives "".
func (w *worker) fetchFile(tag, asset, dir string, optional bool) string {
w.phase("Downloading %s", asset)
want := w.wantSHA(tag, asset, dir)
if want == "" && optional {
w.logf("Release %s has no %s: it stays as it is", tag, asset)
return ""
}
if want == "" {
w.die("Release %s publishes no checksum for %s: not installing an unverified package", tag, asset)
}
f := dir + "/" + asset
if !w.download("https://github.com/"+Repo+"/releases/download/"+tag+"/"+asset, f, want) {
w.die("Download failed: %s", asset)
}
w.logf("sha256 matches the release (%s…)", want[:12])
return f
}
func (w *worker) langs() []string {
if w.e.Langs == nil {
return nil
}
return w.e.Langs()
}
// fetchRelease is a release's package and the translations installed here, verified.
func (w *worker) fetchRelease(tag, dir string) []string {
ver := strings.TrimPrefix(tag, "v")
out := []string{w.fetchFile(tag, "luci-app-dawnline-"+ver+"."+w.e.Pkg.Ext(), dir, false)}
for _, l := range w.langs() {
if f := w.fetchFile(tag, "luci-i18n-dawnline-"+l+"-"+ver+"."+w.e.Pkg.Ext(), dir, true); f != "" {
out = append(out, f)
}
}
return out
}
// tagOf is the release tag of a version: tags may or may not carry the v.
func (w *worker) tagOf(ver string) string {
for _, t := range []string{"v" + ver, ver} {
if r, _ := w.e.GH.Release(Repo, t); r != nil {
return t
}
}
return "v" + ver
}
func (w *worker) backup() {
w.phase("Backing up configuration")
w.e.Run("/usr/bin/dawnline backup --reason pre-update >/dev/null 2>&1")
}
// setLang adds the translation `lang` of the installed version from its release, or (lang "") removes the
// installed ones. LuCI reads its catalogs once per web server process: it restarts after the job.
func (w *worker) setLang(lang string) map[string]any {
v := w.e.Version
if lang == "" {
var have []string
for _, l := range w.langs() {
have = append(have, "luci-i18n-dawnline-"+l)
}
if len(have) > 0 {
w.phase("Removing the translation")
if !w.e.Pkg.Remove(have, w.log) {
w.die("The translation could not be removed")
}
}
} else {
tag := w.tagOf(v)
f := w.fetchFile(tag, "luci-i18n-dawnline-"+lang+"-"+v+"."+w.e.Pkg.Ext(), w.work, true)
if f == "" {
w.die("Release %s has no translation into %s: install a release that has one", tag, lang)
}
w.phase("Installing the translation")
ok, err := w.e.Pkg.InstallFiles([]string{f}, false, w.log)
w.must(ok, err, "Installation failed")
}
w.after["luci"] = true
return map[string]any{"from": v, "to": v, "lang": orNil(lang)}
}
// installApp: arg "" (latest) | @<tag> | reinstall | rollback | lang:<code> | nolang
func (w *worker) installApp(arg string) map[string]any {
v := w.e.Version
if arg == "nolang" {
return w.setLang("")
}
if l, is := strings.CutPrefix(arg, "lang:"); is {
return w.setLang(l)
}
out := map[string]any{"from": v, "to": nil}
if arg == "rollback" {
k := kept(w.e.DataDir, "app")
if k == nil {
w.die("No previous Dawnline version is kept on the router")
}
out["to"] = k.Version
w.versions(v, k.Version)
w.backup()
w.phase("Rolling back to %s", k.Version)
ok, err := w.e.Pkg.InstallFiles(k.Files, true, w.log)
w.must(ok, err, "Installation failed")
forget(w.e.DataDir, "app")
w.after["app"] = true
return out
}
tag, force := "", false
switch {
case arg == "reinstall":
tag, force = w.tagOf(v), true
case strings.HasPrefix(arg, "@"):
tag = arg[1:]
force = VersionCmp(strings.TrimPrefix(tag, "v"), v) <= 0
default:
c := w.checkApp()
if c["latest"] == nil {
panic(failure{str(c["note"])})
}
if c["update"] != true {
out["to"] = v
return out
}
tag = str(c["tag"])
}
to := strings.TrimPrefix(tag, "v")
out["to"] = to
w.versions(v, to)
paths := w.fetchRelease(tag, w.work)
// The version being replaced, for Roll back (downloaded again: the package manager keeps no package
// files). Always for an automatic run: nobody is there to recover a router whose new version does not
// come up (postupdate rolls back to it on its own).
if k := kept(w.e.DataDir, "app"); (w.e.Policy.Keep || w.job.Auto) && to != v && (k == nil || k.Version != v) {
w.phase("Keeping Dawnline %s for rollback", v)
func() {
defer func() {
if r := recover(); r != nil {
msg := fmt.Sprint(r)
if f, is := r.(failure); is {
msg = f.msg
}
w.logf("Warning: could not keep %s: %s", v, msg)
}
}()
os.MkdirAll(w.work+"/keep", 0o700)
keep(w.e.DataDir, "app", v, w.fetchRelease(w.tagOf(v), w.work+"/keep"), nil, nil)
}()
}
w.backup()
w.phase("Installing package")
ok, err := w.e.Pkg.InstallFiles(paths, force, w.log)
w.must(ok, err, "Installation failed")
w.after["app"] = true
return out
}
/* ---------- system packages ---------- */
// PkgNames is what Dawnline itself adds to OpenWrt (the rest of LuCI, rpcd and uhttpd is the stock system).
var PkgNames = []string{"ip-full", "kmod-veth", "libustream-openssl", "openssl-util", "acme", "acme-acmesh-dnsapi", "luci-app-acme"}
// Packages released together with each of the above, updated as a set so OpenSSL never ends up half old
// and half new.
var families = [][2]*regexp.Regexp{
{regexp.MustCompile(`^ip-full$`), regexp.MustCompile(`^ip-`)},
{regexp.MustCompile(`^(libustream-openssl|openssl-util)$`), regexp.MustCompile(`^(libustream-openssl|libopenssl|openssl-util$)`)},
{regexp.MustCompile(`^(acme|luci-app-acme$)`), regexp.MustCompile(`^acme`)},
}
// WithFamily is the packages to update for these names: each one plus its upgradable family. Pure.
func WithFamily(names []string, up map[string]string) []string {
set := map[string]bool{}
for _, n := range names {
if _, has := up[n]; has {
set[n] = true
}
for _, f := range families {
if f[0].MatchString(n) {
for u := range up {
if f[1].MatchString(u) {
set[u] = true
}
}
break
}
}
}
out := make([]string, 0, len(set))
for k := range set {
out = append(out, k)
}
sort.Strings(out)
return out
}
// checkPkgs; refresh: update the package lists first (a check by hand or on schedule).
func (w *worker) checkPkgs(refresh bool) map[string]any {
if refresh {
w.phase("Refreshing package lists")
w.e.Pkg.Refresh(nil)
}
up := w.e.Pkg.Upgradable()
res := map[string]any{}
all := map[string]bool{}
for _, p := range PkgNames {
v := w.e.Pkg.Version(p)
set := []string{}
if v != "" {
set = WithFamily([]string{p}, up)
}
with := []string{}
for _, x := range set {
all[x] = true
if x != p {
with = append(with, x)
}
}
var latest any
if l, has := up[p]; has && v != "" {
latest = l
}
res[p] = map[string]any{"installed": orNil(v), "latest": latest, "with": with}
}
names := make([]string, 0, len(all))
for k := range all {
names = append(names, k)
}
sort.Strings(names)
n := len(names)
status := "latest"
if n > 0 {
status = "outdated"
w.log(fmt.Sprintf(w.e.T.N(int64(n), "%d package update in the feeds: %s", "%d package updates in the feeds: %s"), n, strings.Join(names, ", ")))
} else {
w.logf("System packages are up to date")
}
return map[string]any{"pkgs": res, "updates": n, "status": status}
}
var (
needsApply = regexp.MustCompile(`^(openssl-util|libustream|ip-full|kmod-veth)`)
needsUhttpd = regexp.MustCompile(`^(libustream|libopenssl)`)
)
// installPkgs: arg name,name,… (only names Dawnline manages): missing ones are installed, present ones
// updated with their family.
func (w *worker) installPkgs(arg string) map[string]any {
var names []string
for _, n := range strings.Split(arg, ",") {
if n != "" {
names = append(names, n)
}
}
for _, n := range names {
if !has(PkgNames, n) {
w.die("%s is not a package Dawnline manages", n)
}
}
if len(names) == 0 {
w.die("Nothing to install")
}
w.phase("Refreshing package lists")
w.e.Pkg.Refresh(w.log)
var missing, present []string
for _, n := range names {
if w.e.Pkg.Version(n) == "" {
missing = append(missing, n)
} else {
present = append(present, n)
}
}
have := WithFamily(present, w.e.Pkg.Upgradable())
if len(have) > 0 {
w.phase("Updating %s", strings.Join(have, ", "))
ok, err := w.e.Pkg.Upgrade(have, w.log)
w.must(ok, err, "Update failed")
}
if len(missing) > 0 {
w.phase("Installing %s", strings.Join(missing, ", "))
ok, err := w.e.Pkg.Add(missing, w.log)
w.must(ok, err, "Installation failed")
}
// the portal's HTTPS and the home-like link: a piece that just arrived lets them start
arrived, touched := false, false
for _, n := range missing {
arrived = arrived || needsApply.MatchString(n)
}
for _, n := range append(append([]string{}, missing...), have...) {
touched = touched || needsUhttpd.MatchString(n)
}
if arrived {
w.after["apply"], w.after["uhttpd"] = true, true
} else if touched {
w.after["uhttpd"] = true
}
if have == nil {
have = []string{}
}
if missing == nil {
missing = []string{}
}
return map[string]any{"updated": have, "installed": missing, "check": w.checkPkgs(false)}
}
/* ---------- AmneziaWG ---------- */
func (w *worker) firmware() (Firmware, bool) {
version, target, arch := "", "", ""
if w.e.Board != nil {
version, target, arch = w.e.Board()
}
release, _ := os.ReadFile("/etc/openwrt_release")
return FirmwareOf(version, target, arch, string(release), w.e.Pkg.Ext())
}
func (w *worker) awgInstalled() map[string]any {
out := map[string]any{}
for _, p := range AwgPackages {
out[p] = orNil(w.e.Pkg.Version(p))
}
return out
}
// fetchBuild downloads one package of a build to dir, verified when GitHub publishes its sha256 (then
// mirrors may serve it); without a sha256 only github.com itself. The path, or "".
func (w *worker) fetchBuild(fw Firmware, name, src, dir string) string {
file := fw.Asset(name)
dest := dir + "/" + src + "_" + file
repo := Sources[src].Repo
if w.download("https://github.com/"+repo+"/releases/download/"+fw.Tag+"/"+file, dest, w.e.GH.AssetSHA256(repo, fw.Tag, file)) {
return dest
}
return ""
}
var series = regexp.MustCompile(`^v([0-9]+\.[0-9]+)\.`)
// newerSeriesTag is the newest tag of the same OpenWrt series (24.10.x) in a repository, if newer than ours.
func (w *worker) newerSeriesTag(repo, tag string) string {
m := series.FindStringSubmatch(tag)
if m == nil {
return ""
}
best := ""
list, _ := w.e.GH.Releases(repo, 1)
for _, x := range list {
r, _ := x.(map[string]any)
t := str(r["tag"])
if !strings.HasPrefix(t, "v"+m[1]+".") {
continue
}
if c, ok := w.e.Pkg.Cmp(t[1:], tag[1:]); !ok || c <= 0 {
continue
}
if best != "" {
if c, ok := w.e.Pkg.Cmp(t[1:], best[1:]); !ok || c <= 0 {
continue
}
}
best = t
}
return best
}
func (w *worker) checkAwg() map[string]any {
installed := w.awgInstalled()
ru := w.e.Pkg.Version(AwgRU) != ""
cur := str(installed["amneziawg-tools"])
base := func(note string) map[string]any {
return map[string]any{"installed": orNil(cur), "gen": orNil(Gen(cur)), "packages": installed, "current": nil, "ru": ru, "sources": map[string]any{},
"update": false, "status": "", "note": w.t(note)}
}
fw, ok := w.firmware()
if !ok {
return base("Cannot detect the firmware target")
}
if fw.Snapshot {
return base("This is a SNAPSHOT firmware: the AmneziaWG projects publish builds for releases only")
}
dir := w.work + "/check"
os.MkdirAll(dir, 0o700)
sources := map[string]any{}
versions := map[string]string{}
found := false
for _, id := range Order {
src := Sources[id]
w.logf("Looking for the %s build for OpenWrt %s", src.Label, fw.Tag[1:])
v := ""
var date any
if f := w.fetchBuild(fw, "amneziawg-tools", id, dir); f != "" {
v = w.e.Pkg.FileVersion(f)
if r, _ := w.e.GH.Release(src.Repo, fw.Tag); r != nil {
date = r["published"]
}
}
s := map[string]any{"label": src.Label, "repo": src.Repo, "tag": fw.Tag, "version": orNil(v), "gen": orNil(Gen(v)), "date": date}
// a newer firmware may already get 3.1 from this source
if Gen(v) != "3.1" {
if nt := w.newerSeriesTag(src.Repo, fw.Tag); nt != "" {
if nf := w.fetchBuild(fw.ForTag(nt), "amneziawg-tools", id, dir); nf != "" && Gen(w.e.Pkg.FileVersion(nf)) == "3.1" {
s["newer"] = map[string]any{"tag": nt, "gen": "3.1"}
}
}
}
sources[id], versions[id] = s, v
found = found || v != ""
}
// The installed build: the source whose package matches. Unknown stays unknown (an automatic update
// never switches a build it could not identify).
current := ""
for _, id := range Order {
if current == "" && cur != "" && versions[id] == cur {
current = id
}
}
mine := versions[current]
st := ""
if cur != "" && mine != "" {
if c, known := w.e.Pkg.Cmp(cur, mine); known {
st = map[int]string{-1: "outdated", 0: "latest", 1: "dev"}[c]
}
}
// an update that waits for a reboot: Components says so after a check
pending := Pending(record(w.e.DataDir), bootID(), moduleCore())
switch {
case cur == "":
w.logf("AmneziaWG is not installed")
case current == "":
w.logf("AmneziaWG %s: no source publishes this build for this firmware any more", cur)
case st == "outdated":
w.logf("AmneziaWG update is available: %s → %s", cur, mine)
default:
w.logf("AmneziaWG is up to date (%s)", cur)
}
var note any
switch {
case !found:
note = fmt.Sprintf(w.t("No AmneziaWG build for OpenWrt %s yet"), fw.Tag[1:])
case pending:
note = w.t("The AmneziaWG update is installed and starts after a reboot.")
}
return map[string]any{"installed": orNil(cur), "gen": orNil(Gen(cur)), "packages": installed, "current": orNil(current), "ru": ru, "release": fw.Tag,
"sources": sources, "latest": orNil(mine), "status": st, "update": cur == "" || st == "outdated",
// an update that changes the generation (2.0 → 3.1) breaks older apps: never automatic
"gen_change": cur != "" && mine != "" && Gen(cur) != Gen(mine), "pending": pending, "note": note}
}
func exists(path string) bool { _, err := os.Stat(path); return err == nil }
// reloadModule: every AmneziaWG interface is brought down, the module swapped and the interfaces brought
// up again. False when the old module stays loaded: the tunnels come back on it and the installed one
// starts with the next boot.
func (w *worker) reloadModule() bool {
var ifaces []string
names := map[string]string{}
if raw, err := os.ReadFile("/etc/config/network"); err == nil {
if c, err := uci.Parse(string(raw)); err == nil {
for _, s := range c.OfType("interface") {
proto, _ := s.Get("proto")
if off, _ := s.Get("disabled"); proto == "amneziawg" && off != "1" && s.Name != "" {
ifaces = append(ifaces, s.Name)
names[s.Name], _ = s.Get("_dawnline_name")
}
}
}
}
n := len(ifaces)
w.job.Phase = fmt.Sprintf(w.e.T.N(int64(n), "Reloading the kernel module (%d tunnel restarts)", "Reloading the kernel module (%d tunnels restart)"), n)
w.log(w.job.Phase)
// why each goes down, so the log does not call it unexpected
path := w.e.RunDir + "/iface-intent.json"
all := map[string]map[string]any{}
files.ReadJSON(path, &all)
for _, i := range ifaces {
name := names[i]
if name == "" {
name = i
}
all[i] = map[string]any{"reason": "update", "name": name, "ts": w.now()}
}
files.WriteJSON(path, all, 0o600)
for _, i := range ifaces {
w.e.Run("ifdown '" + i + "' >/dev/null 2>&1")
}
if exists("/sys/module/amneziawg") {
w.run("rmmod amneziawg")
}
swapped := !exists("/sys/module/amneziawg")
if swapped {
w.run("modprobe amneziawg")
if !exists("/sys/module/amneziawg") {
w.die("The new AmneziaWG kernel module doesn't load on this firmware: the tunnels stay down until it does (see the log above)")
}
}
for _, i := range ifaces {
w.e.Run("ifup '" + i + "' >/dev/null 2>&1")
}
return swapped
}
// settle, after packages changed: the module loaded, netifd knowing the protocol, Dawnline set up again.
// It returns whether the module that runs is the installed one.
func (w *worker) settle(before, after map[string]any, was Module) bool {
b, a := str(before["kmod-amneziawg"]), str(after["kmod-amneziawg"])
fresh := true
switch {
// a new module package, one over a module loaded by hand, or an earlier update that still waits for a boot
case a != "" && (a != b || was.Pending) && (b != "" || was.Core != 0):
fresh = w.reloadModule()
case a != "" && b == "":
w.phase("Loading the kernel module")
if _, code := w.run("modprobe amneziawg"); code != 0 && !exists("/sys/module/amneziawg") {
w.die("AmneziaWG is installed, but its kernel module doesn't load on this firmware")
}
}
// netifd reads its protocol handlers when it starts
if out, _ := w.run("ubus call network get_proto_handlers"); !strings.Contains(out, `"amneziawg"`) {
w.phase("Restarting the network so it knows AmneziaWG (connections drop for a few seconds)")
w.run("/etc/init.d/network restart")
}
w.after["apply"] = true
return fresh
}
func (w *worker) finishAwg(out map[string]any, source, tag any, was Module) map[string]any {
after := w.awgInstalled()
out["after"] = after
reloaded := w.settle(out["before"].(map[string]any), after, was)
out["reloaded"] = reloaded
writeRecord(w.e.DataDir, source, tag, after, was, reloaded)
if !reloaded {
w.logf("The old kernel module stays loaded (another AmneziaWG interface may use it): the AmneziaWG update is installed and starts after a reboot.")
}
return out
}
// pick is the first source that publishes every package for this firmware.
func (w *worker) pick(fw Firmware) string {
for _, id := range Order {
assets := w.e.GH.Assets(Sources[id].Repo, fw.Tag)
all := assets != nil
for _, p := range AwgPackages {
all = all && has(assets, fw.Asset(p))
}
if all {
return id
}
}
return ""
}
// installAwg: arg [auto|slava|grey][:ru] | rollback
func (w *worker) installAwg(arg string) map[string]any {
before := w.awgInstalled()
was := moduleNow(w.e.DataDir, str(before["kmod-amneziawg"]))
from := str(before["amneziawg-tools"])
out := map[string]any{"from": orNil(from), "to": nil, "before": before}
fw, ok := w.firmware()
if !ok {
w.die("Cannot detect the firmware target")
}
if arg == "rollback" {
k := kept(w.e.DataDir, "amneziawg")
if k == nil {
w.die("No previous AmneziaWG build is kept on the router")
}
if f := str(k.Firmware); f != "" && f != fw.Postfix {
w.die("The kept AmneziaWG build was made for another firmware: it would not load on this one")
}
out["to"], out["build"] = k.Version, k.Build
w.versions(from, k.Version)
w.phase("Rolling back to %s", k.Version)
ok, err := w.e.Pkg.InstallFiles(k.Files, true, w.log)
w.must(ok, err, "Package installation failed")
forget(w.e.DataDir, "amneziawg")
return w.finishAwg(out, k.Build, fw.Tag, was)
}
parts := strings.Split(arg, ":")
id := parts[0]
if id == "" {
id = "auto"
}
wantRU := len(parts) > 1 && parts[1] == "ru"
if id == "auto" {
if id = w.pick(fw); id == "" {
w.die("No AmneziaWG build for OpenWrt %s (%s) yet", fw.Tag[1:], fw.Postfix)
}
}
src, known := Sources[id]
if !known {
w.die("Unknown AmneziaWG build %s", id)
}
out["build"] = id
names := append([]string{}, AwgPackages...)
if wantRU {
names = append(names, AwgRU)
}
var paths []string
for _, p := range names {
w.phase("Downloading %s from %s", p, src.Repo)
f := w.fetchBuild(fw, p, id, w.work)
if f == "" {
w.die("No %s build for OpenWrt %s in %s, or it could not be verified", p, fw.Tag[1:], src.Repo)
}
paths = append(paths, f)
}
to := w.e.Pkg.FileVersion(paths[1])
out["to"] = orNil(to)
w.versions(from, to)
// the build being replaced, for Roll back: from another source that still publishes it
if w.e.Policy.Keep && from != "" && to != from {
dir := w.work + "/keep"
os.MkdirAll(dir, 0o700)
other := ""
for _, sid := range Order {
if sid == id || other != "" {
continue
}
if f := w.fetchBuild(fw, "amneziawg-tools", sid, dir); f != "" && w.e.Pkg.FileVersion(f) == from {
other = sid
}
}
if other != "" {
w.phase("Keeping AmneziaWG %s for rollback", from)
var keepf []string
for _, p := range AwgPackages {
if f := w.fetchBuild(fw, p, other, dir); f != "" {
keepf = append(keepf, f)
}
}
if len(keepf) == len(AwgPackages) {
keep(w.e.DataDir, "amneziawg", from, keepf, other, fw.Postfix)
}
} else {
w.logf("Warning: the installed build is no longer published, nothing to keep for rollback")
}
}
w.phase("Refreshing package lists")
w.e.Pkg.Refresh(w.log)
w.phase("Installing the %s build", src.Label)
ok2, err := w.e.Pkg.InstallFiles(paths, true, w.log)
w.must(ok2, err, "Package installation failed")
if !wantRU && w.e.Pkg.Version(AwgRU) != "" {
w.e.Pkg.Remove([]string{AwgRU}, w.log)
}
return w.finishAwg(out, id, fw.Tag, was)
}
+171
View File
@@ -0,0 +1,171 @@
// Package updater is Dawnline's background update worker (usr/libexec/dawnline/updater and the modules it
// ran: dawnline.updates, upd_app, upd_awg, upd_pkgs, awg_builds), started detached by the backend and by
// the minute's chores:
//
// 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 (Settings → Updates: check, then install)
//
// One job at a time: the job holds the update lock until it exits. Progress: update-job.json; check
// results: update-check.json. The Components tab reads both.
package updater
import (
"os"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/tickcount/luci-app-dawnline/backend/files"
)
// Repo is where Dawnline's own releases are.
const Repo = "tickcount/luci-app-dawnline"
// Targets is what a check looks at.
var Targets = []string{"app", "amneziawg", "packages"}
// lock takes the update lock without waiting: the open file (kept for the whole job; the kernel releases
// it when the process ends), or nil when another job holds it.
func lock(run string) *os.File {
os.MkdirAll(run, 0o755)
f, err := os.OpenFile(run+"/update.lock", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
return nil
}
if syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) != nil {
f.Close()
return nil
}
return f
}
// historyAdd puts an entry at the head of the history (Components → Activity): newest first, the last 30
// kept on flash.
func historyAdd(data string, now int64, e map[string]any) {
var h []map[string]any
files.ReadJSON(data+"/update-history.json", &h)
e["ts"] = now
h = append([]map[string]any{e}, h...)
if len(h) > 30 {
h = h[:30]
}
os.MkdirAll(data, 0o700)
files.WriteJSON(data+"/update-history.json", h, 0o600)
}
var (
versionShape = regexp.MustCompile(`^([0-9.]+)([-~]([0-9A-Za-z.]+))?`)
preShape = regexp.MustCompile(`^([^0-9]*)([0-9]*)`)
)
// VersionCmp compares Dawnline release versions (0.3.2, v0.4.0-rc1): -1, 0 or 1. Numbers compare as
// numbers; a pre-release (-rc1, -beta) comes before its release.
func VersionCmp(a, b string) int {
type parsed struct {
nums []int
pre string
hasPre bool
}
parse := func(v string) parsed {
m := versionShape.FindStringSubmatch(strings.TrimPrefix(v, "v"))
if m == nil {
return parsed{[]int{0}, v, true}
}
var nums []int
for _, x := range strings.Split(m[1], ".") {
n, _ := strconv.Atoi(x)
nums = append(nums, n)
}
return parsed{nums, m[3], m[2] != ""}
}
x, y := parse(a), parse(b)
for i := 0; i < len(x.nums) || i < len(y.nums); i++ {
p, r := 0, 0
if i < len(x.nums) {
p = x.nums[i]
}
if i < len(y.nums) {
r = y.nums[i]
}
if p != r {
if p < r {
return -1
}
return 1
}
}
switch {
case x.hasPre == y.hasPre && x.pre == y.pre:
return 0
case !x.hasPre:
return 1
case !y.hasPre:
return -1
}
// rc1 < rc2 < rc10: the number runs compare as numbers
px, py := preShape.FindStringSubmatch(x.pre), preShape.FindStringSubmatch(y.pre)
if px[1] != py[1] {
if px[1] < py[1] {
return -1
}
return 1
}
nx, _ := strconv.Atoi(px[2])
ny, _ := strconv.Atoi(py[2])
switch {
case nx < ny:
return -1
case nx > ny:
return 1
}
return 0
}
// Kept is the version a component had before its last update (Keep the previous version).
type Kept struct {
Version string `json:"version"`
Build any `json:"build"`
Firmware any `json:"firmware"`
Files []string `json:"files"`
Saved int64 `json:"saved"`
}
// kept is the kept version of a component, or nil (none, or its files are gone).
func kept(data, component string) *Kept {
var k Kept
if !files.ReadJSON(data+"/keep/"+component+"/meta.json", &k) || k.Version == "" || len(k.Files) == 0 {
return nil
}
for _, f := range k.Files {
if _, err := os.Stat(f); err != nil {
return nil
}
}
return &k
}
// keep replaces the kept version of a component with these files (copied in); all of them, or nothing.
func keep(data, component, version string, paths []string, build, firmware any) {
dir := data + "/keep/" + component
os.RemoveAll(dir)
os.MkdirAll(dir, 0o700)
os.Chmod(data+"/keep", 0o700)
var saved []string
for _, f := range paths {
raw, err := os.ReadFile(f)
to := dir + "/" + f[strings.LastIndex(f, "/")+1:]
if err != nil || os.WriteFile(to, raw, 0o600) != nil {
os.RemoveAll(dir)
return
}
saved = append(saved, to)
}
files.WriteJSON(dir+"/meta.json", Kept{Version: version, Build: build, Firmware: firmware, Files: saved, Saved: time.Now().Unix()}, 0o600)
}
func forget(data, component string) { os.RemoveAll(data + "/keep/" + component) }
+673
View File
@@ -0,0 +1,673 @@
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
}
+370
View File
@@ -0,0 +1,370 @@
package updater
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"os"
"strings"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/github"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/pkgmgr"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
)
// The cases tests/ucode holds the ucode modules to (updates, awg_builds, pkg).
func TestPureParts(t *testing.T) {
for _, c := range []struct {
a, b string
want int
}{{"0.5.1", "0.5.1", 0}, {"v0.5.1", "0.5.1", 0}, {"0.5.1", "0.5.2", -1}, {"0.10.0", "0.9.9", 1}, {"0.6", "0.6.0", 0}, {"0.6.0-rc1", "0.6.0", -1},
{"0.6.0-rc2", "0.6.0-rc10", -1}, {"0.6.0-beta", "0.6.0-rc1", -1}, {"garbage", "0.1", -1}} {
if got := VersionCmp(c.a, c.b); got != c.want {
t.Errorf("%s against %s: %d, not %d", c.a, c.b, got, c.want)
}
}
for v, want := range map[string]string{"": "", "3.1.20260812-r1": "3.1", "3.1.0-r1": "3.1", "1.0.20250722-r1": "2.0", "1.0.20250901": "2.0", "1.0.20250610-r1": "1.0",
"1.0.20241112-r2": "1.0", "6.12.94.3.1.20260906-r1": "3.1", "6.12.94.3.1.20260906": "3.1", "6.6.73.1.0.20250722-r1": "2.0", "6.6.73.1.0.20250612-r1": "1.0",
"5.15.150.1.0.20240711-r3": "1.0", "3.1.20260812": "3.1"} {
if got := Gen(v); got != want {
t.Errorf("the generation of %q: %q, not %q", v, got, want)
}
}
waiting := func() *Record { return &Record{Reloaded: false, Boot: "boot-1", PrevGen: "2.0", PrevCore: 98304.0} }
swapped, unknown, noBoot := waiting(), waiting(), waiting()
swapped.Reloaded, unknown.Reloaded, noBoot.Boot = true, nil, nil
for _, c := range []struct {
what string
rec *Record
boot string
core int64
want bool
}{{"the old module still loaded, same boot", waiting(), "boot-1", 98304, true}, {"rebooted", waiting(), "boot-2", 98304, false},
{"no module loaded", waiting(), "boot-1", 0, false}, {"another module loaded", waiting(), "boot-1", 110592, false},
{"the install swapped it", swapped, "boot-1", 98304, false}, {"a module Dawnline did not install", unknown, "boot-1", 98304, false},
{"a record from before boots were kept", &Record{OSRelease: "6.12.94", Source: "slava"}, "boot-1", 98304, false},
{"the kernel gave no boot id", noBoot, "", 98304, false}, {"no record", nil, "boot-1", 98304, false}} {
if got := Pending(c.rec, c.boot, c.core); got != c.want {
t.Errorf("pending, %s: %v", c.what, got)
}
}
if n, v := pkgmgr.ControlInfo("Package: amneziawg-tools\nVersion: 1.0.20250722-r1\nDepends: libc\n", true); n != "amneziawg-tools" || v != "1.0.20250722-r1" {
t.Errorf("an ipk's control file: %q %q", n, v)
}
dump := "#%SCHEMA: 00504B47\ninfo:\n name: kmod-amneziawg\n version: 6.12.94.3.1.20260906-r1\n depends: # 2 items\n - kernel=6.12.94\npaths: # 2 items\n - name: lib/modules\n"
if n, v := pkgmgr.ControlInfo(dump, false); n != "kmod-amneziawg" || v != "6.12.94.3.1.20260906-r1" {
t.Errorf("an apk's dump: %q %q", n, v)
}
if n, v := pkgmgr.ControlInfo("", false); n != "" || v != "" {
t.Error("something read from nothing")
}
want := []pkgmgr.Wanted{{Name: "a", Version: "2"}, {Name: "b", Version: "1"}}
if len(pkgmgr.Mismatched(want, map[string]string{"a": "2", "b": "1"})) != 0 || len(pkgmgr.Mismatched(want, map[string]string{"a": "1", "b": "1"})) != 1 ||
len(pkgmgr.Mismatched([]pkgmgr.Wanted{{File: "x.apk"}}, nil)) != 1 {
t.Error("which packages did not take")
}
up := pkgmgr.ParseUpgradable("libopenssl3-3.5.1-r1 < 3.5.2-r1\nip-full-6.11.0-r1 < 6.12.0-r1\nnoise\n", true)
if up["libopenssl3"] != "3.5.2-r1" || up["ip-full"] != "6.12.0-r1" || len(up) != 2 {
t.Errorf("apk's list: %v", up)
}
if up := pkgmgr.ParseUpgradable("acme - 4.0.0-r1 - 4.0.1-r1\n", false); up["acme"] != "4.0.1-r1" {
t.Errorf("opkg's list: %v", up)
}
fam := WithFamily([]string{"openssl-util", "ip-full", "kmod-veth"}, map[string]string{"libopenssl3": "1", "libustream-openssl20201210": "1", "ip-tiny": "1", "acme": "1"})
if strings.Join(fam, " ") != "ip-tiny libopenssl3 libustream-openssl20201210" {
t.Errorf("the families: %v", fam)
}
for in, want := range map[string][2]string{"app": {"app", ""}, "app@v1.2": {"app", "@v1.2"}, "app:reinstall": {"app", "reinstall"}, "amneziawg:slava:ru": {"amneziawg", "slava:ru"},
"pkgs:a,b": {"pkgs", "a,b"}, "app:lang:ru": {"app", "lang:ru"}} {
if n, a := splitTarget(in); n != want[0] || a != want[1] {
t.Errorf("the target %q: %q %q", in, n, a)
}
}
if fw, ok := FirmwareOf("24.10.6", "mediatek/filogic", "aarch64_cortex-a53", "", "ipk"); !ok || fw.Asset("kmod-amneziawg") != "kmod-amneziawg_v24.10.6_aarch64_cortex-a53_mediatek_filogic.ipk" ||
fw.ForTag("v24.10.8").Postfix != "_v24.10.8_aarch64_cortex-a53_mediatek_filogic" {
t.Errorf("the firmware: %+v", fw)
}
if fw, ok := FirmwareOf("25.12.3", "x86/64", "", "DISTRIB_ARCH='x86_64'\n", "apk"); !ok || fw.Postfix != "_v25.12.3_x86_64_x86_64" {
t.Errorf("the arch from the release file: %+v", fw)
}
if fw, _ := FirmwareOf("SNAPSHOT", "x86/64", "x86_64", "", "apk"); !fw.Snapshot {
t.Error("a snapshot is not told")
}
for arch, want := range map[string]string{"aarch64_cortex-a53": "arm64", "aarch64_generic": "arm64", "x86_64": "amd64", "mips_24kc": "mips", "mipsel_24kc": "mipsle",
"mipsel_74kc": "mipsle", "arm_cortex-a7_neon-vfpv4": "arm", "arm_cortex-a9": "arm", "arm_arm926ej-s": "", "arm_xscale": "", "i386_pentium4": "", "riscv64_riscv64": "", "": ""} {
if got := GoArch(arch); got != want {
t.Errorf("the binary for %q: %q, not %q", arch, got, want)
}
}
if _, ok := FirmwareOf("", "x86/64", "x86_64", "", "apk"); ok {
t.Error("a firmware made of nothing")
}
}
// router is a made-up router: a package manager that keeps versions, GitHub with one newer release.
type router struct {
t *testing.T
dir string
installed map[string]string
ran []string
sums map[string]string
releases map[string]map[string]string // tag -> asset -> body
env *Env
said []string
}
func newRouter(t *testing.T) *router {
r := &router{t: t, dir: t.TempDir(), installed: map[string]string{"luci-app-dawnline": "0.18.0", "ip-full": "6.11.0-r1"}, releases: map[string]map[string]string{}}
r.release("v0.18.0", "0.18.0")
r.release("v0.19.0", "0.19.0")
clock := time.Unix(1_800_000_000, 0)
gh := &github.Client{RunDir: r.dir + "/run", Now: func() time.Time { return clock }, Get: func(url string) (int, []byte) {
rel := func(tag string) map[string]any {
var assets []any
for name, body := range r.releases[tag] {
sum := sha256.Sum256([]byte(body))
a := map[string]any{"name": name, "id": 1.0}
if !strings.Contains(name, "nosum") {
a["digest"] = "sha256:" + hex.EncodeToString(sum[:])
}
assets = append(assets, a)
}
return map[string]any{"tag_name": tag, "html_url": "https://github.com/" + Repo + "/releases/tag/" + tag, "published_at": "2026-11-01T00:00:00Z", "assets": assets}
}
var out any
switch {
case strings.HasSuffix(url, "/releases/latest"):
out = rel("v0.19.0")
case strings.Contains(url, "/releases/tags/"):
tag := url[strings.LastIndex(url, "/")+1:]
if r.releases[tag] == nil {
return 404, []byte(`{"message":"Not Found"}`)
}
out = rel(tag)
default:
return 404, []byte(`{"message":"Not Found"}`)
}
raw, _ := json.Marshal(out)
return 200, raw
}}
pkg := &pkgmgr.Manager{Apk: true, Run: func(cmd string) (string, int) {
switch {
case strings.HasPrefix(cmd, "apk list --installed "):
name := strings.Trim(strings.TrimPrefix(cmd, "apk list --installed "), "'")
if v := r.installed[name]; v != "" {
return name + "-" + v + " x86_64 {feed} [installed]\n", 0
}
return "", 0
case strings.HasPrefix(cmd, "apk adbdump "):
raw, _ := os.ReadFile(strings.Trim(strings.TrimPrefix(cmd, "apk adbdump "), "'"))
return string(raw), 0
case strings.HasPrefix(cmd, "df "):
return "Filesystem 1024-blocks Used Available Capacity Mounted on\noverlay 100000 1000 99000 1% /overlay\n", 0
case strings.HasPrefix(cmd, "apk add --allow-untrusted "):
r.ran = append(r.ran, "apk add files")
for _, f := range strings.Fields(strings.TrimPrefix(cmd, "apk add --allow-untrusted ")) {
raw, _ := os.ReadFile(strings.Trim(f, "'"))
if n, v := pkgmgr.ControlInfo(string(raw), false); n != "" {
r.installed[n] = v
}
}
return "OK: 10 MiB in 100 packages\n", 0
case cmd == `apk version -l "<"`:
return "ip-full-6.11.0-r1 < 6.12.0-r1\nip-tiny-6.11.0-r1 < 6.12.0-r1\n", 0
}
r.ran = append(r.ran, cmd)
return "", 0
}}
r.env = &Env{RunDir: r.dir + "/run", DataDir: r.dir + "/data", Version: "0.18.0", Pkg: pkg, GH: gh, Pid: 4242, Now: func() time.Time { return clock }, Sleep: func(time.Duration) {},
Policy: model.Updates{Check: true, Interval: "1d", Auto: true, Mode: "immediate", Time: "04:00", Targets: []string{"app", "packages"}, Mirrors: true},
Fetch: func(url, dest string) bool {
parts := strings.Split(url, "/releases/download/")
if len(parts) != 2 || strings.HasPrefix(url, "https://gh") {
return false
}
ta := strings.SplitN(parts[1], "/", 2)
body, has := r.releases[ta[0]][ta[1]]
if !has {
return false
}
return os.WriteFile(dest, []byte(body), 0o600) == nil
},
Run: func(cmd string) (string, int) { r.ran = append(r.ran, cmd); return "", 0 },
Log: &servicelog.Log{RunDir: r.dir + "/run", Level: func() string { return "info" }, Syslog: func(_, line string) { r.said = append(r.said, line) }}}
return r
}
func (r *router) release(tag, ver string) {
r.releases[tag] = map[string]string{"luci-app-dawnline-" + ver + ".apk": "info:\n name: luci-app-dawnline\n version: " + ver + "\n"}
}
func (r *router) job() Job {
var j Job
raw, _ := os.ReadFile(r.dir + "/run/update-job.json")
json.Unmarshal(raw, &j)
return j
}
func (r *router) run(args ...string) int {
var out, errs bytes.Buffer
return Main(r.env, args, &out, &errs)
}
func TestUsage(t *testing.T) {
r := newRouter(t)
var out, errs bytes.Buffer
if Main(r.env, nil, &out, &errs) != 2 || Main(r.env, []string{"dance"}, &out, &errs) != 2 || Main(r.env, []string{"install"}, &out, &errs) != 2 || !strings.HasPrefix(errs.String(), "Usage:") {
t.Error("a command it does not know")
}
if Main(r.env, []string{"help"}, &out, &errs) != 0 || !strings.HasPrefix(out.String(), "Usage:") {
t.Error("asked for help")
}
if _, err := os.Stat(r.dir + "/run"); err == nil {
t.Error("something was written for no command")
}
}
func TestCheckAndInstall(t *testing.T) {
r := newRouter(t)
r.run("check", "app")
var c struct {
Components map[string]map[string]any `json:"components"`
}
raw, _ := os.ReadFile(r.dir + "/run/update-check.json")
json.Unmarshal(raw, &c)
if a := c.Components["app"]; a["update"] != true || a["latest"] != "0.19.0" || a["status"] != "outdated" || a["installed"] != "0.18.0" {
t.Fatalf("the check: %s", raw)
}
if j := r.job(); j.State != "done" || j.Phase != "Finished" || j.Finished == nil {
t.Fatalf("the check's job: %+v", j)
}
r.run("check", "packages")
raw, _ = os.ReadFile(r.dir + "/run/update-check.json")
json.Unmarshal(raw, &c)
if p := c.Components["packages"]; p["updates"] != 2.0 || p["status"] != "outdated" || c.Components["app"]["latest"] != "0.19.0" {
t.Fatalf("the packages' check (the app's must stay): %s", raw)
}
// the install: verified, the old version kept, a backup first, the package in, postupdate last
r.env.Policy.Keep = true
r.ran = nil
r.run("install", "app")
j := r.job()
if j.State != "done" || j.From != "0.18.0" || j.To != "0.19.0" || r.installed["luci-app-dawnline"] != "0.19.0" {
t.Fatalf("the install: %+v\n%s", j, strings.Join(j.Log, "\n"))
}
did := strings.Join(r.ran, "; ")
if !strings.Contains(did, "dawnline backup --reason pre-update") || !strings.HasSuffix(did, "postupdate '0.19.0' </dev/null >/dev/null 2>&1 &") ||
strings.Index(did, "backup") > strings.Index(did, "apk add files") {
t.Errorf("what ran: %s", did)
}
if k := kept(r.dir+"/data", "app"); k == nil || k.Version != "0.18.0" || len(k.Files) != 1 {
t.Errorf("the version kept for a rollback: %+v", k)
}
if !strings.Contains(strings.Join(j.Log, "\n"), "sha256 matches the release") {
t.Errorf("nothing says the file was verified:\n%s", strings.Join(j.Log, "\n"))
}
raw, _ = os.ReadFile(r.dir + "/data/update-history.json")
if !strings.Contains(string(raw), `"kind":"install"`) || !strings.Contains(string(raw), `"to":"0.19.0"`) {
t.Errorf("the history: %s", raw)
}
raw, _ = os.ReadFile(r.dir + "/run/update-check.json")
json.Unmarshal(raw, &c)
if a := c.Components["app"]; a["update"] != false || a["installed"] != "0.19.0" {
t.Errorf("the check after the install still offers it: %s", raw)
}
if _, err := os.Stat("/tmp/dawnline-update.4242"); err == nil {
t.Error("the work directory stays")
}
// and back
r.env.Version = "0.19.0"
r.ran = nil
r.run("install", "app:rollback")
if j := r.job(); j.State != "done" || r.installed["luci-app-dawnline"] != "0.18.0" || kept(r.dir+"/data", "app") != nil {
t.Errorf("the rollback: %+v", j)
}
}
func TestInstallRefusals(t *testing.T) {
r := newRouter(t)
// a release that publishes no checksum is not installed
r.releases["v0.20.0"] = map[string]string{"luci-app-dawnline-0.20.0.apk.nosum": "x"}
r.releases["v0.20.0"]["luci-app-dawnline-0.20.0.apk"] = "info:\n name: luci-app-dawnline\n version: 0.20.0\n"
delete(r.releases["v0.20.0"], "luci-app-dawnline-0.20.0.apk.nosum")
r.run("install", "app@v9.9.9")
j := r.job()
if j.State != "failed" || !strings.Contains(j.Error.(string), "publishes no checksum") || r.installed["luci-app-dawnline"] != "0.18.0" {
t.Errorf("a release that is not there: %+v", j)
}
found := false
for _, l := range r.said {
found = found || strings.Contains(l, "Dawnline update failed")
}
raw, _ := os.ReadFile(r.dir + "/data/update-history.json")
if !found || !strings.Contains(string(raw), `"state":"failed"`) {
t.Errorf("a failed update is not told: %v / %s", r.said, raw)
}
r.run("install", "pkgs:busybox")
if j := r.job(); j.State != "failed" || j.Error != "busybox is not a package Dawnline manages" {
t.Errorf("a package that is not ours: %+v", j)
}
r.run("install", "nonsense")
if j := r.job(); j.State != "failed" || j.Error != "Unknown component nonsense" {
t.Errorf("a component there is none of: %+v", j)
}
// one job at a time: a second one leaves without a trace
held := lock(r.dir + "/run")
before, _ := os.ReadFile(r.dir + "/run/update-job.json")
if r.run("check", "app") != 0 {
t.Error("a busy updater is an error")
}
if after, _ := os.ReadFile(r.dir + "/run/update-job.json"); string(after) != string(before) {
t.Error("a second job wrote over the first")
}
held.Close()
}
func TestAuto(t *testing.T) {
r := newRouter(t)
// nothing due: no check, no job
os.MkdirAll(r.dir+"/data", 0o700)
os.WriteFile(r.dir+"/data/update-auto.json", []byte(`{"check":1799999000}`), 0o600)
r.run("auto")
if _, err := os.Stat(r.dir + "/run/update-check.json"); err == nil {
t.Fatal("checked before its time")
}
// due, and updates right away: packages first, Dawnline last
os.WriteFile(r.dir+"/data/update-auto.json", []byte(`{"check":1799000000}`), 0o600)
r.ran = nil
r.run("auto")
j := r.job()
if j.State != "done" || !j.Auto || j.Component != "pkgs:ip-full app" || r.installed["luci-app-dawnline"] != "0.19.0" {
t.Fatalf("the automatic run: %+v\n%s", j, strings.Join(j.Log, "\n"))
}
did := strings.Join(r.ran, "; ")
if !strings.Contains(did, "apk upgrade 'ip-full' 'ip-tiny'") || strings.Index(did, "apk upgrade") > strings.Index(did, "apk add files") {
t.Errorf("the order: %s", did)
}
// an automatic run always keeps the version it replaces
if k := kept(r.dir+"/data", "app"); k == nil || k.Version != "0.18.0" {
t.Errorf("nothing kept by an automatic run: %+v", k)
}
said := strings.Join(r.said, "\n")
if !strings.Contains(said, "updates available · Dawnline 0.19.0, 2 system packages") || !strings.Contains(said, "updated automatically · ip-full, ip-tiny") {
t.Errorf("the service log: %s", said)
}
// checks only: found, told once, nothing installed
r2 := newRouter(t)
r2.env.Policy.Auto = false
r2.run("auto")
r2.run("auto")
if r2.installed["luci-app-dawnline"] != "0.18.0" || strings.Count(strings.Join(r2.said, "\n"), "updates available") != 1 {
t.Errorf("a check-only policy: %v", r2.said)
}
if _, err := os.Stat(r2.dir + "/run/update-job.json"); err == nil {
t.Error("an automatic check shows up as a job")
}
}