CI / build (push) Successful in 30s
Apps opt in with sema.dashboard.* labels in their own compose file. The primary reads its host's containers through a read-only socket proxy, follows Docker events, and merges in what the agent on the second host reports. Icons come from the icon label, the dashboard-icons set or the app's favicon, with a letter as the fallback. Go with only the standard library, a distroless image, and a Gitea Actions workflow copied from FiMa's: checks in the Dockerfile's check stage, a Trivy scan, and a push to git.mariosem.es/msemes/hoda. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
3.0 KiB
Go
117 lines
3.0 KiB
Go
// Command hoda runs the HODA homelab dashboard. MODE=primary serves the page
|
|
// and merges apps from agents; MODE=agent only reports its own host's apps.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.mariosem.es/msemes/hoda/internal/apps"
|
|
"git.mariosem.es/msemes/hoda/internal/config"
|
|
"git.mariosem.es/msemes/hoda/internal/discovery"
|
|
"git.mariosem.es/msemes/hoda/internal/docker"
|
|
"git.mariosem.es/msemes/hoda/internal/icons"
|
|
"git.mariosem.es/msemes/hoda/internal/peer"
|
|
"git.mariosem.es/msemes/hoda/internal/server"
|
|
)
|
|
|
|
// version is set at build time with -ldflags "-X main.version=...".
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
if len(os.Args) > 1 {
|
|
switch os.Args[1] {
|
|
case "healthcheck":
|
|
os.Exit(healthcheck())
|
|
case "version":
|
|
fmt.Println(version)
|
|
return
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "unknown command %q; the commands are healthcheck and version\n", os.Args[1])
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "hoda:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: cfg.LogLevel}))
|
|
dc, err := docker.New(cfg.DockerHost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
store := apps.NewStore()
|
|
resolver := icons.New(filepath.Join(cfg.DataDir, "icons"), cfg.IconCDN, cfg.IconRefresh, log)
|
|
go resolver.Run(ctx)
|
|
go discovery.New(dc, store, resolver, cfg.LabelPrefix, cfg.NodeName, cfg.ResyncInterval, log).Run(ctx)
|
|
|
|
deps := server.Deps{Store: store, Icons: resolver}
|
|
if len(cfg.Peers) > 0 {
|
|
puller := peer.New(cfg.Peers, cfg.PeerToken, cfg.PeerInterval, store, log)
|
|
go puller.Run(ctx)
|
|
deps.Peers = puller
|
|
}
|
|
|
|
srv := &http.Server{
|
|
Addr: ":" + cfg.Port,
|
|
Handler: server.New(cfg, deps, version, log),
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
IdleTimeout: 2 * time.Minute,
|
|
}
|
|
errc := make(chan error, 1)
|
|
go func() { errc <- srv.ListenAndServe() }()
|
|
log.Info("HODA started", "version", version, "mode", cfg.Mode, "node", cfg.NodeName, "port", cfg.Port, "agents", len(cfg.Peers))
|
|
|
|
select {
|
|
case err := <-errc:
|
|
return err
|
|
case <-ctx.Done():
|
|
}
|
|
log.Info("shutting down")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := srv.Shutdown(shutdownCtx); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// healthcheck backs the image's HEALTHCHECK: distroless has no curl or wget.
|
|
func healthcheck() int {
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
client := http.Client{Timeout: 3 * time.Second}
|
|
resp, err := client.Get("http://127.0.0.1:" + port + "/healthz")
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return 1
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
fmt.Fprintln(os.Stderr, "healthz:", resp.Status)
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|