diff --git a/CLAUDE.md b/CLAUDE.md index 036e05e..42470ad 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -40,6 +40,7 @@ DATABASE_URL=postgres://... go test -tags integration ./internal/master/store/ - `internal/master/poller` — every `POLL_INTERVAL`: one lookup per followed `repo:tag`, sets the available version, starts auto jobs - `internal/master/jobs` — decides whether a service may be updated and starts a job on the right agent - `internal/master/webhook` — `POST /update`: records the available version, starts jobs for auto services, waits for them +- `internal/master/web/icons` — embedded Simple Icons path data; slug derived from the image ref; regenerate with `go generate ./internal/master/web/icons` - `internal/master/web`, `webauth` — HTMX UI behind OIDC login, SSE signals - `internal/master/events` — in-process publish/subscribe for UI refresh signals - `internal/master/model` — domain types shared by the above @@ -162,6 +163,8 @@ DATABASE_URL=postgres://... go test -tags integration ./internal/master/store/ - **Merge lock order is agent, then service rows by id** (as in inventory writes), and the duplicate row is deleted before the old row is updated, because `location_key` is unique and not deferrable. A removal racing a merge can be lost; the next snapshot fixes it. +- **The SSE stream sends a named `ping` event every 25 s**; the sidebar Live indicator relies on it. +- **Agents persist `swarm_cluster_id`.** Only managers report it, so workers show no role. ## References diff --git a/cmd/master/main.go b/cmd/master/main.go index 821c495..36bfe90 100644 --- a/cmd/master/main.go +++ b/cmd/master/main.go @@ -117,7 +117,7 @@ func run() error { ui, err := web.New(st, agentHub, jobService, broker, web.Config{ AgentURL: cfg.AgentURL, MasterKey: fingerprint, AgentImage: cfg.AgentImage, - PollInterval: cfg.PollInterval, + PollInterval: cfg.PollInterval, Version: version, }, logger) if err != nil { return err diff --git a/internal/imageref/imageref.go b/internal/imageref/imageref.go index 43f98fd..6efee0f 100644 --- a/internal/imageref/imageref.go +++ b/internal/imageref/imageref.go @@ -33,6 +33,12 @@ func Parse(ref string) Ref { return r } +// IsRegistryHost reports whether the first path segment of a repository names +// a registry: it contains a "." or ":" or is "localhost". +func IsRegistryHost(segment string) bool { + return strings.ContainsAny(segment, ".:") || segment == "localhost" +} + // String reassembles the reference as repo[:tag][@digest]. func (r Ref) String() string { s := r.Repo diff --git a/internal/imageref/imageref_test.go b/internal/imageref/imageref_test.go index 7ce6685..1c7e33b 100644 --- a/internal/imageref/imageref_test.go +++ b/internal/imageref/imageref_test.go @@ -29,6 +29,26 @@ func TestParse(t *testing.T) { } } +func TestIsRegistryHost(t *testing.T) { + cases := []struct { + in string + want bool + }{ + {"lscr.io", true}, + {"localhost:5000", true}, + {"localhost", true}, + {"registry:5000", true}, + {"gitea", false}, + {"library", false}, + {"", false}, + } + for _, c := range cases { + t.Run(c.in, func(t *testing.T) { + assert.Equal(t, c.want, IsRegistryHost(c.in)) + }) + } +} + func TestIsDigest(t *testing.T) { cases := []struct { in string diff --git a/internal/master/model/model.go b/internal/master/model/model.go index cee251f..c105314 100644 --- a/internal/master/model/model.go +++ b/internal/master/model/model.go @@ -29,9 +29,12 @@ type Agent struct { Hostname string AgentVersion string SwarmManager bool - LastSeenAt *time.Time - RevokedAt *time.Time - CreatedAt time.Time + // SwarmClusterID is the Swarm cluster the agent's daemon was in when it last + // connected; "" outside a swarm. + SwarmClusterID string + LastSeenAt *time.Time + RevokedAt *time.Time + CreatedAt time.Time } // Service is one service as last reported by an agent. diff --git a/internal/master/store/agents.go b/internal/master/store/agents.go index 79ff91f..4f9708f 100644 --- a/internal/master/store/agents.go +++ b/internal/master/store/agents.go @@ -13,11 +13,11 @@ import ( "github.com/shcizo/package-updater/internal/protocol" ) -const agentColumns = `id, name, hostname, agent_version, swarm_manager, last_seen_at, revoked_at, created_at` +const agentColumns = `id, name, hostname, agent_version, swarm_manager, swarm_cluster_id, last_seen_at, revoked_at, created_at` func scanAgent(row pgx.Row) (model.Agent, error) { var a model.Agent - err := row.Scan(&a.ID, &a.Name, &a.Hostname, &a.AgentVersion, &a.SwarmManager, &a.LastSeenAt, &a.RevokedAt, &a.CreatedAt) + err := row.Scan(&a.ID, &a.Name, &a.Hostname, &a.AgentVersion, &a.SwarmManager, &a.SwarmClusterID, &a.LastSeenAt, &a.RevokedAt, &a.CreatedAt) if errors.Is(err, pgx.ErrNoRows) { return model.Agent{}, model.ErrNotFound } @@ -101,8 +101,8 @@ func (s *Store) RevokeAgent(ctx context.Context, id int64, at time.Time) ([]int6 // RecordHello stores what the agent reported about itself on connect. func (s *Store) RecordHello(ctx context.Context, id int64, h protocol.Hello, at time.Time) error { if _, err := s.pool.Exec(ctx, - `UPDATE agents SET hostname = $2, agent_version = $3, swarm_manager = $4, last_seen_at = $5 WHERE id = $1`, - id, h.Hostname, h.AgentVersion, h.SwarmManager, at); err != nil { + `UPDATE agents SET hostname = $2, agent_version = $3, swarm_manager = $4, swarm_cluster_id = $6, last_seen_at = $5 WHERE id = $1`, + id, h.Hostname, h.AgentVersion, h.SwarmManager, at, h.SwarmClusterID); err != nil { return fmt.Errorf("record hello: %w", err) } return nil diff --git a/internal/master/store/agents_integration_test.go b/internal/master/store/agents_integration_test.go index 9cc22b9..93f4383 100644 --- a/internal/master/store/agents_integration_test.go +++ b/internal/master/store/agents_integration_test.go @@ -80,6 +80,7 @@ func TestAgents_RecordHelloAndMarkSeen(t *testing.T) { assert.Equal(t, "swarm-mgr", agents[0].Hostname) assert.Equal(t, "v1", agents[0].AgentVersion) assert.True(t, agents[0].SwarmManager) + assert.Equal(t, "c1", agents[0].SwarmClusterID) require.NotNil(t, agents[0].LastSeenAt) assert.True(t, agents[0].LastSeenAt.Equal(at.Add(time.Minute))) } diff --git a/internal/master/store/migrations/0006_agent_cluster.sql b/internal/master/store/migrations/0006_agent_cluster.sql new file mode 100644 index 0000000..14dba3b --- /dev/null +++ b/internal/master/store/migrations/0006_agent_cluster.sql @@ -0,0 +1 @@ +ALTER TABLE agents ADD COLUMN swarm_cluster_id text NOT NULL DEFAULT ''; diff --git a/internal/master/web/icons/LICENSE-simple-icons.md b/internal/master/web/icons/LICENSE-simple-icons.md new file mode 100644 index 0000000..70d4a7b --- /dev/null +++ b/internal/master/web/icons/LICENSE-simple-icons.md @@ -0,0 +1,30 @@ +# CC0 1.0 Universal + +## Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an “owner”) of an original work of authorship and/or a database (each, a “Work”). + +Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works (“Commons”) that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the “Affirmer”), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights (“Copyright and Related Rights”). Copyright and Related Rights include, but are not limited to, the following: + 1. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; + 2. moral rights retained by the original author(s) and/or performer(s); + 3. publicity and privacy rights pertaining to a person’s image or likeness depicted in a Work; + 4. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(i), below; + 5. rights protecting the extraction, dissemination, use and reuse of data in a Work; + 6. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and + 7. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the “Waiver”). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer’s heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer’s express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer’s express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer’s Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the “License”). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer’s express Statement of Purpose. + +4. Limitations and Disclaimers. + 1. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. + 2. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. + 3. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person’s Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. + 4. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. + +For more information, please see . diff --git a/internal/master/web/icons/gen/main.go b/internal/master/web/icons/gen/main.go new file mode 100644 index 0000000..d8468e4 --- /dev/null +++ b/internal/master/web/icons/gen/main.go @@ -0,0 +1,128 @@ +// Command gen builds simple-icons.json.gz, the icon data the icons package +// embeds. It downloads the pinned simple-icons npm tarball, checks its +// sha256 and writes a gzipped JSON object {slug: svg path data}. The output +// is deterministic. It writes into the current directory; run it with +// +// go generate ./internal/master/web/icons +package main + +import ( + "archive/tar" + "bytes" + "compress/gzip" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "path" + "regexp" + "strings" +) + +const ( + tarballURL = "https://registry.npmjs.org/simple-icons/-/simple-icons-16.35.0.tgz" + tarballSHA256 = "20ebead4f6bc93464812a5e260d19fc4d84772e5eeff69a6fbea263ca2441c5f" +) + +var pathData = regexp.MustCompile(`, found %d", h.Name, len(m)) + } + icons[strings.TrimSuffix(path.Base(h.Name), ".svg")] = string(m[0][1]) + } + } + if license == nil || len(icons) == 0 { + return nil, nil, fmt.Errorf("tarball has no LICENSE.md or no icons") + } + return icons, license, nil +} diff --git a/internal/master/web/icons/icons.go b/internal/master/web/icons/icons.go new file mode 100644 index 0000000..f6cec43 --- /dev/null +++ b/internal/master/web/icons/icons.go @@ -0,0 +1,109 @@ +// Package icons maps container image references to brand icons from the +// Simple Icons set (CC0, see LICENSE-simple-icons.md). Only the master's UI +// imports it, so the agent binary does not carry the data. +package icons + +import ( + "bytes" + "compress/gzip" + _ "embed" + "encoding/json" + "strings" + "sync" + + "github.com/shcizo/package-updater/internal/imageref" +) + +//go:generate go run ./gen + +//go:embed simple-icons.json.gz +var data []byte + +var ( + once sync.Once + paths map[string]string +) + +// aliases maps a normalised image name to the Simple Icons slug that differs +// from it. +var aliases = map[string]string{ + "postgres": "postgresql", + "goauthentik": "authentik", + "traefik": "traefikproxy", + "node": "nodedotjs", + "mongo": "mongodb", +} + +// skipNamespaces are registry namespaces that name a publisher, not a product. +var skipNamespaces = map[string]bool{"library": true, "linuxserver": true, "bitnami": true, "hotio": true} + +// suffixes are stripped from the last path segment to find the product name +// ("authentik-server" is authentik). +var suffixes = []string{"-server", "-web", "-api", "-worker", "-agent"} + +func load() { + once.Do(func() { + paths = map[string]string{} + zr, err := gzip.NewReader(bytes.NewReader(data)) + if err != nil { + return + } + _ = json.NewDecoder(zr).Decode(&paths) + }) +} + +// Path returns the SVG path data (24x24 viewBox) of the icon with this Simple +// Icons slug. +func Path(slug string) (string, bool) { + load() + p, ok := paths[slug] + return p, ok +} + +// Count returns how many icons are embedded. +func Count() int { + load() + return len(paths) +} + +// ForImage returns the slug of the icon that best matches the image +// reference, or "" when there is none. It tries the last path segment, that +// segment without a role suffix, and the namespace, in that order. +func ForImage(ref string) string { + segs := strings.Split(imageref.Parse(ref).Repo, "/") + if len(segs) > 1 && imageref.IsRegistryHost(segs[0]) { + segs = segs[1:] + } + last := segs[len(segs)-1] + candidates := []string{last} + for _, s := range suffixes { + if base, ok := strings.CutSuffix(last, s); ok && base != "" { + candidates = append(candidates, base) + break + } + } + if len(segs) > 1 && !skipNamespaces[segs[len(segs)-2]] { + candidates = append(candidates, segs[len(segs)-2]) + } + for _, c := range candidates { + slug := normalise(c) + if a, ok := aliases[slug]; ok { + slug = a + } + if _, ok := Path(slug); ok { + return slug + } + } + return "" +} + +// normalise lowercases s and keeps only a-z and 0-9, the form of Simple Icons slugs. +func normalise(s string) string { + var b strings.Builder + for _, r := range strings.ToLower(s) { + if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' { + b.WriteRune(r) + } + } + return b.String() +} diff --git a/internal/master/web/icons/icons_test.go b/internal/master/web/icons/icons_test.go new file mode 100644 index 0000000..a02cbd8 --- /dev/null +++ b/internal/master/web/icons/icons_test.go @@ -0,0 +1,46 @@ +package icons + +import ( + "regexp" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestForImage(t *testing.T) { + cases := []struct { + ref, want string + }{ + {"gitea/gitea:1.27.3", "gitea"}, + {"ghcr.io/goauthentik/server:2026.8.2", "authentik"}, + {"postgres:17.10", "postgresql"}, + {"grafana/loki:3.4.2", "grafana"}, + {"docker.n8n.io/n8nio/n8n:1.112.4", "n8n"}, + {"lscr.io/linuxserver/prowlarr:latest", ""}, + {"localhost:5000/foo:v1", ""}, + {"gitea.shcizo.se/shcizo/slusk:v0.8.1", ""}, + } + for _, c := range cases { + t.Run(c.ref, func(t *testing.T) { + assert.Equal(t, c.want, ForImage(c.ref)) + }) + } +} + +// safePath is a strict allowlist for what an embedded path may contain, since +// the paths are written into the page's HTML. +var safePath = regexp.MustCompile(`^[MmLlHhVvCcSsQqTtAaZz0-9.,\- e]+$`) + +func TestEmbeddedSet(t *testing.T) { + assert.Greater(t, Count(), 3000) + for slug, p := range paths { + if !safePath.MatchString(p) { + t.Errorf("icon %q has unexpected characters in its path", slug) + } + } + p, ok := Path("gitea") + assert.True(t, ok) + assert.NotEmpty(t, p) + _, ok = Path("no-such-icon") + assert.False(t, ok) +} diff --git a/internal/master/web/icons/simple-icons.json.gz b/internal/master/web/icons/simple-icons.json.gz new file mode 100644 index 0000000..c7a5c20 Binary files /dev/null and b/internal/master/web/icons/simple-icons.json.gz differ diff --git a/internal/master/web/ledger_test.go b/internal/master/web/ledger_test.go new file mode 100644 index 0000000..404eb89 --- /dev/null +++ b/internal/master/web/ledger_test.go @@ -0,0 +1,127 @@ +package web + +import ( + "bufio" + "context" + "io" + "net/http" + "net/url" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestEvents_SendsPingWhenIdle(t *testing.T) { + old := pingInterval + pingInterval = 20 * time.Millisecond + t.Cleanup(func() { pingInterval = old }) + srv, _, _, _ := newTestServer(t) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/events", nil) + require.NoError(t, err) + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + + reader := bufio.NewReader(resp.Body) + for { + line, err := reader.ReadString('\n') + require.NoError(t, err) + if strings.HasPrefix(line, "event:") { + assert.Equal(t, "event: ping\n", line, "the Live box counts a named event; a comment would not reach the browser's listeners") + return + } + } +} + +func TestCreateAgent_ValidationShowsInsideTheAddPanel(t *testing.T) { + srv, _, _, _ := newTestServer(t) + resp, err := http.PostForm(srv.URL+"/agents", url.Values{"name": {"node-2"}}) + require.NoError(t, err) + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + assert.Equal(t, http.StatusConflict, resp.StatusCode) + assert.Contains(t, string(body), `class="drawer"`) + assert.Contains(t, string(body), `value="node-2"`, "the typed name is kept") + assert.NotContains(t, string(body), "pua_", "no token is shown") +} + +func TestNavFragment(t *testing.T) { + srv, _, _, _ := newTestServer(t) + cases := []struct { + query, want, notWant string + }{ + {"?section=agents", `Agents 1/3`, `Services 4`, `Jobs`, `class="cur"`}, + } + for _, c := range cases { + t.Run(c.query, func(t *testing.T) { + code, body := get(t, srv, "/fragments/nav"+c.query) + assert.Equal(t, http.StatusOK, code) + assert.NotContains(t, body, "" on the service page + Version string // the master's build version; agents on another version are marked old } // Server renders the UI. @@ -110,9 +114,9 @@ type Server struct { // New parses the templates. Each page is its own template set so every page // can define its own "content" block. func New(store Store, hub Hub, jobs Jobs, broker Broker, cfg Config, log *slog.Logger) (*Server, error) { - funcs := template.FuncMap{"shortRef": shortRef, "duration": jobDuration, "followsNoTag": followsNoTag} + funcs := template.FuncMap{"shortRef": shortRef, "duration": jobDuration, "followsNoTag": followsNoTag, "middle": middle} pages := map[string]*template.Template{} - for _, page := range []string{"overview.html", "agents.html", "agent_created.html", "service.html", "jobs.html", "job.html"} { + for _, page := range []string{"overview.html", "agents.html", "service.html", "jobs.html", "job.html"} { t, err := template.New(page).Funcs(funcs).ParseFS(assets, "templates/layout.html", "templates/"+page) if err != nil { return nil, fmt.Errorf("parse %s: %w", page, err) @@ -142,20 +146,57 @@ func (s *Server) Register(mux *http.ServeMux, protect func(http.Handler) http.Ha route("GET /fragments/jobs/{id}", s.jobFragment) route("GET /agents", s.agents) route("GET /fragments/agents", s.agentsFragment) + route("GET /fragments/agents/{id}/connect", s.agentConnectFragment) + route("GET /fragments/nav", s.navFragment) route("POST /agents", s.createAgent) route("POST /agents/{id}/revoke", s.revokeAgent) route("GET /events", s.events) } +// navData is what the sidebar shows on every page. +type navData struct { + Section string // services, jobs or agents; "" for none + Services int + AgentsOnline int + AgentsTotal int // agents that are not revoked +} + +// spriteIcon is one icon of the sprite the services fragment carries. +type spriteIcon struct { + Slug string + Path string +} + +// sections maps a page to the sidebar entry that is current on it. +var sections = map[string]string{ + "overview.html": "services", "service.html": "services", + "jobs.html": "jobs", "job.html": "jobs", + "agents.html": "agents", +} + type pageData struct { - Title string - CSRF string - Groups []serviceGroup + Title string + CSRF string + Nav navData + Groups []serviceGroup + // ServiceCount is how many services the groups hold. + ServiceCount int + // Sprite holds the icons the groups use, so the page carries only those. + Sprite []spriteIcon Agents []agentRow Error string Created model.Agent Snippet string + // AddStep is the step of the "add agent" panel: 0 closed, 1 name, + // 2 deploy (Created, Token, MasterKey and Snippet are set). + AddStep int + FormName string // what was typed in step 1 + Token string + MasterKey string + Connected bool // the agent Created has connected + MasterVersion string // the master's build version; "" if unknown + Service model.Service ConfigFiles []string Reason string // why Service cannot be updated; "" when it can @@ -178,6 +219,58 @@ func (s *Server) base(r *http.Request, title string) pageData { return pageData{Title: title, CSRF: sess.CSRFToken} } +// page renders a full page: the sidebar numbers are looked up and put in data. +func (s *Server) page(w http.ResponseWriter, r *http.Request, status int, page string, data pageData) { + nav, err := s.navData(r.Context(), sections[page]) + if err != nil { + s.fail(w, err) + return + } + data.Nav = nav + s.render(w, status, page, "layout", data) +} + +// navData counts what the sidebar shows next to its entries. +func (s *Server) navData(ctx context.Context, section string) (navData, error) { + nav := navData{Section: section} + services, err := s.store.ListServices(ctx) + if err != nil { + return nav, err + } + nav.Services = len(services) + agents, err := s.store.ListAgents(ctx) + if err != nil { + return nav, err + } + for _, a := range agents { + if a.RevokedAt != nil { + continue + } + nav.AgentsTotal++ + if s.hub.Online(a.ID) { + nav.AgentsOnline++ + } + } + return nav, nil +} + +// navFragment renders the sidebar entries. The page asks for it with its own +// section so the current entry stays marked. +func (s *Server) navFragment(w http.ResponseWriter, r *http.Request) { + section := r.URL.Query().Get("section") + if section != "services" && section != "jobs" && section != "agents" { + section = "" + } + nav, err := s.navData(r.Context(), section) + if err != nil { + s.fail(w, err) + return + } + data := s.base(r, "") + data.Nav = nav + s.render(w, http.StatusOK, "overview.html", "nav", data) +} + func (s *Server) render(w http.ResponseWriter, status int, page, block string, data pageData) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(status) @@ -192,17 +285,41 @@ func (s *Server) serviceData(r *http.Request) (pageData, error) { if err != nil { return data, err } - data.Groups = groupServices(services, s.autoBlocked) + agents, err := s.store.ListAgents(r.Context()) + if err != nil { + return data, err + } + data.Groups = groupServices(services, agents, s.hub.Online, s.autoBlocked) + data.ServiceCount = len(services) + data.Sprite = spriteFor(data.Groups) return data, nil } +// spriteFor collects the icons the groups' rows use, each once. +func spriteFor(groups []serviceGroup) []spriteIcon { + var sprite []spriteIcon + seen := map[string]bool{} + for _, g := range groups { + for _, row := range g.Services { + if row.Icon == "" || seen[row.Icon] { + continue + } + seen[row.Icon] = true + if path, ok := icons.Path(row.Icon); ok { + sprite = append(sprite, spriteIcon{Slug: row.Icon, Path: path}) + } + } + } + return sprite +} + func (s *Server) overview(w http.ResponseWriter, r *http.Request) { data, err := s.serviceData(r) if err != nil { s.fail(w, err) return } - s.render(w, http.StatusOK, "overview.html", "layout", data) + s.page(w, r, http.StatusOK, "overview.html", data) } func (s *Server) servicesFragment(w http.ResponseWriter, r *http.Request) { @@ -211,7 +328,7 @@ func (s *Server) servicesFragment(w http.ResponseWriter, r *http.Request) { s.fail(w, err) return } - s.render(w, http.StatusOK, "overview.html", "services", data) + s.render(w, http.StatusOK, "overview.html", "services-fragment", data) } // autoBlocked returns the reason svc cannot be set to auto mode, or "". @@ -308,7 +425,7 @@ func (s *Server) servicePage(w http.ResponseWriter, r *http.Request) { s.failLookup(w, err) return } - s.render(w, http.StatusOK, "service.html", "layout", data) + s.page(w, r, http.StatusOK, "service.html", data) } func (s *Server) serviceJobsFragment(w http.ResponseWriter, r *http.Request) { @@ -470,7 +587,7 @@ func (s *Server) renderServiceError(w http.ResponseWriter, r *http.Request, id i return } data.Error = msg - s.render(w, status, "service.html", "layout", data) + s.page(w, r, status, "service.html", data) } // setMode switches a service between manual and auto. Manual is always @@ -528,7 +645,7 @@ func (s *Server) jobsPage(w http.ResponseWriter, r *http.Request) { s.fail(w, err) return } - s.render(w, http.StatusOK, "jobs.html", "layout", data) + s.page(w, r, http.StatusOK, "jobs.html", data) } func (s *Server) jobsFragment(w http.ResponseWriter, r *http.Request) { @@ -559,7 +676,7 @@ func (s *Server) jobData(w http.ResponseWriter, r *http.Request) (pageData, bool func (s *Server) jobPage(w http.ResponseWriter, r *http.Request) { if data, ok := s.jobData(w, r); ok { - s.render(w, http.StatusOK, "job.html", "layout", data) + s.page(w, r, http.StatusOK, "job.html", data) } } @@ -571,23 +688,28 @@ func (s *Server) jobFragment(w http.ResponseWriter, r *http.Request) { func (s *Server) agentData(r *http.Request) (pageData, error) { data := s.base(r, "Agents") + data.MasterVersion = s.cfg.Version agents, err := s.store.ListAgents(r.Context()) if err != nil { return data, err } for _, a := range agents { - data.Agents = append(data.Agents, agentRow{Agent: a, Online: s.hub.Online(a.ID)}) + data.Agents = append(data.Agents, newAgentRow(a, s.hub.Online(a.ID), s.cfg.Version)) } return data, nil } +// agents shows the agent list; with ?add=1 the "add agent" panel is open. func (s *Server) agents(w http.ResponseWriter, r *http.Request) { data, err := s.agentData(r) if err != nil { s.fail(w, err) return } - s.render(w, http.StatusOK, "agents.html", "layout", data) + if r.URL.Query().Get("add") == "1" { + data.AddStep = 1 + } + s.page(w, r, http.StatusOK, "agents.html", data) } func (s *Server) agentsFragment(w http.ResponseWriter, r *http.Request) { @@ -596,13 +718,41 @@ func (s *Server) agentsFragment(w http.ResponseWriter, r *http.Request) { s.fail(w, err) return } - s.render(w, http.StatusOK, "agents.html", "agents", data) + if data.Nav, err = s.navData(r.Context(), "agents"); err != nil { + s.fail(w, err) + return + } + s.render(w, http.StatusOK, "agents.html", "agents-fragment", data) +} + +// agentConnectFragment is the last step of the "add agent" panel: it says +// whether the agent that was just added has connected. It never repeats the +// token. +func (s *Server) agentConnectFragment(w http.ResponseWriter, r *http.Request) { + id, ok := pathID(w, r) + if !ok { + return + } + agents, err := s.store.ListAgents(r.Context()) + if err != nil { + s.fail(w, err) + return + } + i := slices.IndexFunc(agents, func(a model.Agent) bool { return a.ID == id && a.RevokedAt == nil }) + if i < 0 { + http.Error(w, "not found", http.StatusNotFound) + return + } + data := s.base(r, "Agents") + data.Created = agents[i] + data.Connected = s.hub.Online(id) || agents[i].LastSeenAt != nil + s.render(w, http.StatusOK, "agents.html", "connect", data) } func (s *Server) createAgent(w http.ResponseWriter, r *http.Request) { name := strings.TrimSpace(r.FormValue("name")) if !agentNamePattern.MatchString(name) { - s.agentsWithError(w, r, http.StatusBadRequest, "Agent names use lowercase letters, digits and dashes.") + s.agentsWithError(w, r, http.StatusBadRequest, name, "Agent names use lowercase letters, digits and dashes.") return } token, err := agenttoken.Generate() @@ -612,28 +762,39 @@ func (s *Server) createAgent(w http.ResponseWriter, r *http.Request) { } agent, err := s.store.CreateAgent(r.Context(), name, agenttoken.Hash(token)) if errors.Is(err, model.ErrConflict) { - s.agentsWithError(w, r, http.StatusConflict, fmt.Sprintf("An agent named %q already exists.", name)) + s.agentsWithError(w, r, http.StatusConflict, name, fmt.Sprintf("An agent named %q already exists.", name)) return } if err != nil { s.fail(w, err) return } - data := s.base(r, "Agent created") - data.Created = agent - data.Snippet = agentSnippet(s.cfg, token) - w.Header().Set("Cache-Control", "no-store") // the page shows the one-time token - s.render(w, http.StatusOK, "agent_created.html", "layout", data) -} - -func (s *Server) agentsWithError(w http.ResponseWriter, r *http.Request, status int, msg string) { data, err := s.agentData(r) if err != nil { s.fail(w, err) return } + data.AddStep = 2 + data.Created = agent + data.Token = token + data.MasterKey = s.cfg.MasterKey + data.Snippet = agentSnippet(s.cfg, token) + w.Header().Set("Cache-Control", "no-store") // the page shows the one-time token + s.page(w, r, http.StatusOK, "agents.html", data) +} + +// agentsWithError shows the agent list with the "add agent" panel open at +// its first step, name kept and msg under the field. +func (s *Server) agentsWithError(w http.ResponseWriter, r *http.Request, status int, name, msg string) { + data, err := s.agentData(r) + if err != nil { + s.fail(w, err) + return + } + data.AddStep = 1 + data.FormName = name data.Error = msg - s.render(w, status, "agents.html", "layout", data) + s.page(w, r, status, "agents.html", data) } func (s *Server) revokeAgent(w http.ResponseWriter, r *http.Request) { @@ -648,6 +809,7 @@ func (s *Server) revokeAgent(w http.ResponseWriter, r *http.Request) { return } s.hub.Disconnect(id) + s.broker.Publish(events.Agents) // the agent list and the sidebar change s.broker.Publish(events.Services) // the agent's services were marked removed s.broker.Publish(events.Jobs) // the agent's active jobs were failed for _, jobID := range failedJobs { @@ -656,6 +818,9 @@ func (s *Server) revokeAgent(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/agents", http.StatusSeeOther) } +// pingInterval is how often an idle event stream sends "event: ping". +var pingInterval = 25 * time.Second + // events streams change signals as Server-Sent Events. htmx's SSE extension // turns "event: services" into a refetch of the services fragment. The event // carries no state, so a dropped signal is recovered by the next one. @@ -675,7 +840,7 @@ func (s *Server) events(w http.ResponseWriter, r *http.Request) { s.log.Error("sse_flush_unsupported", "error", err) return } - keepalive := time.NewTicker(25 * time.Second) + keepalive := time.NewTicker(pingInterval) defer keepalive.Stop() for { var msg string @@ -685,7 +850,7 @@ func (s *Server) events(w http.ResponseWriter, r *http.Request) { case t := <-ch: msg = fmt.Sprintf("event: %s\ndata: changed\n\n", t) case <-keepalive.C: - msg = ": keepalive\n\n" + msg = "event: ping\ndata: ok\n\n" // keeps proxies open; the Live box in the sidebar counts it } if _, err := fmt.Fprint(w, msg); err != nil { return diff --git a/internal/master/web/server_test.go b/internal/master/web/server_test.go index 6b6e31e..11b05f1 100644 --- a/internal/master/web/server_test.go +++ b/internal/master/web/server_test.go @@ -221,30 +221,46 @@ func newTestEnv(t *testing.T) (*httptest.Server, *fakeStore, *fakeHub, *events.B // the service page shows. func newTestEnvPolling(t *testing.T, pollInterval time.Duration) (*httptest.Server, *fakeStore, *fakeHub, *events.Broker, *fakeJobs) { t.Helper() - agentID := int64(2) + return newTestEnvConfig(t, func(c *Config) { c.PollInterval = pollInterval }) +} + +// newTestEnvVersion is newTestEnv with version as the master's build version. +func newTestEnvVersion(t *testing.T, version string) (*httptest.Server, *fakeStore, *fakeHub, *events.Broker, *fakeJobs) { + t.Helper() + return newTestEnvConfig(t, func(c *Config) { c.Version = version }) +} + +// newTestEnvConfig is newTestEnv with edit applied to the UI's Config. +func newTestEnvConfig(t *testing.T, edit func(*Config)) (*httptest.Server, *fakeStore, *fakeHub, *events.Broker, *fakeJobs) { + t.Helper() + agentID, offlineAgentID := int64(2), int64(3) store := &fakeStore{ created: map[string]string{"node-2": "x"}, services: []model.Service{ {ID: 1, Kind: protocol.KindCompose, ComposeProject: "slusk", ComposeService: "web", AgentID: &agentID, AgentName: "node-2", ActualRef: "gitea.shcizo.se/shcizo/slusk:1.4.2", Status: "running", UpdateMode: "manual"}, - {ID: 2, Kind: protocol.KindSwarm, SwarmService: "jellyfin_jellyfin", + {ID: 2, Kind: protocol.KindSwarm, SwarmService: "jellyfin_jellyfin", SwarmClusterID: "c1abcdefgh", ActualRef: "lscr.io/linuxserver/jellyfin@sha256:0f42497a69fa0441bfd5f9d6bba8694f2a656ec984e571d0ac04c6dd91250039", Status: "degraded", UpdateMode: "manual"}, {ID: 3, Kind: protocol.KindContainer, ContainerName: "updater-agent", AgentID: &agentID, AgentName: "node-2", ActualRef: "pu:1", Status: "running", UpdateMode: "manual", Self: true}, + {ID: 4, Kind: protocol.KindCompose, ComposeProject: "media", ComposeService: "radarr", AgentID: &offlineAgentID, AgentName: "node-3", + ActualRef: "lscr.io/linuxserver/radarr:latest", Status: "running", UpdateMode: "manual"}, }, agents: []model.Agent{ - {ID: 2, Name: "node-2", Hostname: "node-2"}, - {ID: 3, Name: "node-3"}, + {ID: 2, Name: "node-2", Hostname: "node-2.lan", AgentVersion: "v1.4.0", SwarmManager: true, SwarmClusterID: "c1abcdefgh", LastSeenAt: ptr(time.Now().UTC())}, + {ID: 3, Name: "node-3", Hostname: "node-3.lan", AgentVersion: "v1.3.2", LastSeenAt: ptr(time.Date(2026, 10, 12, 13, 51, 0, 0, time.UTC))}, {ID: 4, Name: "old", RevokedAt: ptr(time.Now())}, + {ID: 5, Name: "fresh"}, }, } hub := &fakeHub{online: map[int64]bool{2: true}} broker := events.NewBroker() jobs := &fakeJobs{} - s, err := New(store, hub, jobs, broker, Config{ + cfg := Config{ AgentURL: "wss://10.0.0.5:8090/agent", MasterKey: "sha256:abc", AgentImage: "gitea.shcizo.se/shcizo/package-updater:latest", - PollInterval: pollInterval, - }, slog.New(slog.NewTextHandler(io.Discard, nil))) + } + edit(&cfg) + s, err := New(store, hub, jobs, broker, cfg, slog.New(slog.NewTextHandler(io.Discard, nil))) require.NoError(t, err) mux := http.NewServeMux() s.Register(mux, func(h http.Handler) http.Handler { @@ -279,13 +295,44 @@ func TestOverview_GroupsServicesPerMachineAndSwarm(t *testing.T) { code, body := get(t, srv, "/") assert.Equal(t, http.StatusOK, code) assert.Contains(t, body, "Swarm", "one cluster is just called Swarm") + assert.Contains(t, body, `title="c1abcdefgh"`, "the full cluster id is the hover text") + assert.Contains(t, body, "managers node-2") + assert.Contains(t, body, ">node-2") + assert.Contains(t, body, `web`) + assert.Contains(t, body, `slusk`) + assert.Contains(t, body, "jellyfin_jellyfin", "Swarm service names are shown as they are") assert.Contains(t, body, "sha256:0f42497a69fa…", "digests are shortened") - assert.Contains(t, body, "managed outside") - assert.Less(t, strings.Index(body, "node-2"), strings.Index(body, "Swarm"), "machines before Swarm") + assert.Contains(t, body, "Managed outside") + assert.Less(t, strings.Index(body, ">Swarm"), strings.Index(body, ">node-2"), "a cluster is followed by its managers' machines") + assert.Contains(t, body, `class="sub" data-machine="agent-2"`, "the machine filter lists the manager under the cluster") +} + +func TestOverview_OfflineGroupShowsSince(t *testing.T) { + srv, store, hub, _, _ := newTestEnv(t) + _, body := get(t, srv, "/") + assert.Regexp(t, `Agent offline since · showing last known state`, body) + assert.NotContains(t, body, "No manager online", "the cluster has a connected manager") + + hub.mu.Lock() + hub.online[2] = false + hub.mu.Unlock() + _, body = get(t, srv, "/") + since := store.agents[0].LastSeenAt.Format("2006-01-02T15:04:05Z07:00") + assert.Contains(t, body, `No manager online since