package main import ( "net/http" "net/url" "os" "path/filepath" "regexp" "strings" ) // Updates from a git 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. The repository is part of the // installed version (routerUpdate.repository, written to // /etc/router-update-repository), not something the page sets. var 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"` } // repository is where releases come from ("" if the router has none). func (s System) repository() string { path := s.RepositoryFile if path == "" { path = "/etc/router-update-repository" } data, _ := os.ReadFile(path) return strings.TrimSpace(string(data)) } 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() *GitCheck { var c GitCheck repository := a.sys.repository() 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 { if c := a.gitCheck(); c != nil && c.Newer { return c.Newest.Version } return "" } func (a *app) updateGitCheck(w http.ResponseWriter, r *http.Request, _ session) { if a.sys.repository() == "" { a.updateBack(w, r, "This router has no repository to look at.") 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 } if a.sys.repository() == "" { a.updateBack(w, r, "This router has no repository to update from.") 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.") }