Files
package-updater/internal/master/web/icons/gen/main.go
T
shcizo ec28d1e027
Docker Build / docker (push) Successful in 41s
feat(master): Ledger UI for Services and Agents (#18) (#29)
## Summary

- Rebuilds the master's **Services** and **Agents** pages per the "Ledger" design: sidebar shell with live nav counts, machine filter and Live indicator; dark/light theme (follows the OS, toggle overrides); embedded Schibsted Grotesk and JetBrains Mono (fontsource 5.3.0, OFL).
- Services: Swarm grouped per cluster with its machines under it, offline banner ("Agent offline since HH:MM", browser-local), Simple Icons derived from the image ref (letter circle fallback), existing badges and mode switch restyled, client-side search, machine filter and Icon / Icon + text / Text toggle.
- Agents: Online / Offline / Never connected / Revoked, master version in the header with outdated agent versions marked, revoke via popover instead of `confirm()`, "Add agent" as a three-step panel that completes when the agent says hello (SSE `agents`).
- Store: migration `0005_agent_cluster.sql` persists each agent's `swarm_cluster_id` (needed to list a cluster's managers and its offline state after a master restart).
- SSE keepalive is now a named `ping` event so the Live indicator also shows the connection is alive. `revokeAgent` now publishes `agents`.

Closes #18

## Notes

- Workers never report a cluster id (Docker only sets `Swarm.Cluster` on managers), so their Swarm role shows "—". Fixing that needs a new hello field.
- Master binary grows by about 2.16 MB, almost all Simple Icons 16.35.0 path data (gzipped JSON, regenerated with `go generate ./internal/master/web/icons`). The agent binary does not import it.
- Jobs, Job and Service detail pages only get the new shell; their own artboards are out of scope, as are Update buttons, Update all, header stats and the drift badge.
- Unchanged: CSRF on every POST, `Cache-Control: no-store` on the token response, 401 + `HX-Redirect` without a session, no `template.HTML` on agent- or user-controlled values, no Node build step.

## Test plan

- [x] `go test ./...`, `go test -race ./internal/master/...`
- [x] `go test -tags integration ./internal/master/store/` against Postgres 17
- [x] `docker build`
- [x] Browser check on fake data: both themes, 1440 px and 390 px, add-agent flow through connect
- [ ] Services grouping and icons against real stacks and a real Swarm cluster
- [ ] "Agent offline since" after stopping an agent
- [ ] Theme toggle, search and machine filter across reloads and mode changes
- [ ] Add agent end to end with a real agent, Copy buttons
- [ ] Phone at 390 px

https://claude.ai/code/session_01GAZsp3P7Qto6JnhipxWUJQ
Reviewed-on: #29
2026-10-13 09:20:18 +00:00

129 lines
3.2 KiB
Go

// 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(`<path d="([^"]+)"`)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "gen:", err)
os.Exit(1)
}
}
func run() error {
tgz, err := download(tarballURL)
if err != nil {
return err
}
if sum := sha256.Sum256(tgz); hex.EncodeToString(sum[:]) != tarballSHA256 {
return fmt.Errorf("sha256 mismatch for %s: got %x, want %s", tarballURL, sum, tarballSHA256)
}
icons, license, err := extract(tgz)
if err != nil {
return err
}
var js bytes.Buffer
enc := json.NewEncoder(&js)
enc.SetEscapeHTML(false)
if err := enc.Encode(icons); err != nil { // map keys are written sorted
return err
}
var gz bytes.Buffer
zw, err := gzip.NewWriterLevel(&gz, gzip.BestCompression)
if err != nil {
return err
}
if _, err := zw.Write(js.Bytes()); err != nil { // the header has no name and a zero mtime
return err
}
if err := zw.Close(); err != nil {
return err
}
if err := os.WriteFile("simple-icons.json.gz", gz.Bytes(), 0o644); err != nil {
return err
}
if err := os.WriteFile("LICENSE-simple-icons.md", license, 0o644); err != nil {
return err
}
fmt.Printf("wrote %d icons\n", len(icons))
return nil
}
func download(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GET %s: %s", url, resp.Status)
}
return io.ReadAll(resp.Body)
}
// extract returns the path data of every package/icons/*.svg by slug, and
// package/LICENSE.md.
func extract(tgz []byte) (icons map[string]string, license []byte, err error) {
zr, err := gzip.NewReader(bytes.NewReader(tgz))
if err != nil {
return nil, nil, err
}
tr := tar.NewReader(zr)
icons = map[string]string{}
for {
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, nil, err
}
switch {
case h.Name == "package/LICENSE.md":
if license, err = io.ReadAll(tr); err != nil {
return nil, nil, err
}
case path.Dir(h.Name) == "package/icons" && strings.HasSuffix(h.Name, ".svg"):
svg, err := io.ReadAll(tr)
if err != nil {
return nil, nil, err
}
m := pathData.FindAllSubmatch(svg, -1)
if len(m) != 1 {
return nil, nil, fmt.Errorf("%s: want exactly one <path>, 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
}