Files
nixos-router/pkgs/router-webui/gitupdate.go
T
nilsandClaude Sonnet 5.5 3fc54e678e Update from a git repository
router.update.repository names a public repository whose tags vX.Y.Z are
releases. router-update check (daily, and from the Updates page) looks for a
newer one and the Overview says so; router-update --git builds the router
from a tag like a bundle update: the settings are migrated, the new version
must be confirmed, the nixpkgs pin stays. The flake of such a router comes
from the release's config-flake.nix. The global flake registry is off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 08:57:00 +01:00

116 lines
3.5 KiB
Go

package main
import (
"net/http"
"net/url"
"path/filepath"
"regexp"
"strings"
)
// Updates from a git repository (router.update.repository): router-update
// check looks at the repository's release tags (vX.Y.Z) once a day and when the
// page asks, and writes what it found to update/check.json; installing builds
// the router from a tag, like an update from a bundle.
var (
// The same shape the option accepts: no credentials, no query.
repositoryRe = regexp.MustCompile(`^https?://[A-Za-z0-9.-]+(:[0-9]+)?(/[A-Za-z0-9._~/-]*)?$`)
releaseTagRe = regexp.MustCompile(`^v[0-9]+\.[0-9]+\.[0-9]+$`)
)
// GitCheck is update/check.json: the newest release tag of the repository.
type GitCheck struct {
Repository string `json:"repository"`
Time string `json:"time"`
Installed string `json:"installed"`
Newest struct {
Tag string `json:"tag"`
Version string `json:"version"`
Rev string `json:"rev"`
} `json:"newest"`
Newer bool `json:"newer"`
}
func (s Settings) repository() string { return str(s, "router", "update", "repository") }
const gitCheckUnit = "router-update-check.service"
// gitCheck is what the last check found, if it is still about this repository
// and this installed version.
func (a *app) gitCheck(repository string) *GitCheck {
var c GitCheck
if repository == "" || !readUpgradeFile(filepath.Join(a.sys.updateDir(), "check.json"), &c) {
return nil
}
if c.Repository != repository || c.Installed != a.sys.installedRelease().Version || c.Newest.Tag == "" {
return nil
}
return &c
}
// newRouterVersion is the version of a release that is out and not installed,
// for the Overview ("" if there is none or nothing was checked).
func (a *app) newRouterVersion() string {
s, err := loadSettings(a.sys.settingsPath())
if err != nil {
return ""
}
if c := a.gitCheck(s.repository()); c != nil && c.Newer {
return c.Newest.Version
}
return ""
}
func (a *app) updateSaveRepository(w http.ResponseWriter, r *http.Request, _ session) {
v := strings.TrimRight(strings.TrimSpace(r.PostFormValue("repository")), "/")
if !r.PostForm.Has("repository") {
a.updateBack(w, r, "Not saved: no address was sent.")
return
}
if v != "" && !repositoryRe.MatchString(v) {
a.updateBack(w, r, "Not saved: an http:// or https:// address of a public repository, without credentials or a query.")
return
}
if err := a.sys.setUpdateField("repository", v); err != nil {
a.updateBack(w, r, "Not saved: "+err.Error())
return
}
a.updateBack(w, r, "Saved.")
}
func (a *app) updateGitCheck(w http.ResponseWriter, r *http.Request, _ session) {
s, err := loadSettings(a.sys.settingsPath())
if err != nil || s.repository() == "" {
a.updateBack(w, r, "Set the repository first.")
return
}
if err := startUnit(gitCheckUnit); err != nil {
a.updateBack(w, r, "Could not look: "+err.Error())
return
}
a.updateBack(w, r, "Looking at the repository.")
}
func (a *app) updateGitInstall(w http.ResponseWriter, r *http.Request, _ session) {
if a.busy() {
a.updateBack(w, r, "Another change is in progress or waiting for confirmation.")
return
}
tag := r.PostFormValue("tag")
if tag != "" && !releaseTagRe.MatchString(tag) {
a.updateBack(w, r, "Not a release tag: "+url.QueryEscape(tag))
return
}
s, err := loadSettings(a.sys.settingsPath())
if err != nil || s.repository() == "" {
a.updateBack(w, r, "Set the repository first.")
return
}
if err := a.sys.requestUpdate("git", tag, r.PostFormValue("force") == "on"); err != nil {
a.updateBack(w, r, "Could not start the update: "+err.Error())
return
}
a.updateBack(w, r, "Updating.")
}