Files
Sebastian UnterschützandClaude Opus 5.5 a35dadd8f6
ci / test (pull_request) Successful in 9m12s
ci / lint (pull_request) Successful in 12m17s
ci / build (pull_request) Successful in 16m25s
ci / image (pull_request) Successful in 4m40s
feat(agents): self-updating agent image variant
The -autoupdate variant of the checker and status page images adds a
launcher layer. The launcher runs the agent as a child process and keeps
it on the platform's release: GET /agent/v1/update names the API's own
release and the agent image for it, the launcher downloads the binary
for its platform from the registry, accepts it only when the index is
signed with a release key for exactly that version, swaps the agent
gracefully and rolls back when the new release does not become ready or
crashes right after starting. Agents never get ahead of the API, and
installations on unreleased builds are not followed.

CI pushes both variants and signs each index with RELEASE_SIGNING_KEY
(minisign signature in a sha256-<digest>.minisig image next to it). The
image builder moves to internal/agentimage; the API image job takes the
build job's version so the API reports the release its agents are tagged
with. Agents gain a version command.

Closes #129
Closes #128

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-23 18:57:48 +01:00

112 lines
3.7 KiB
Go

// Command image builds and publishes the multi-arch images of an agent
// (checker or status page server) from cross-compiled binaries, without
// Docker.
//
// image -name checker -dist dist -repo registry/owner/uptimesebi-checker \
// -version v1.2.3 -commit abc123 -ref refs/tags/v1.2.3 [-autoupdate] [-push]
//
// dist holds one directory per platform: linux_amd64, linux_arm64,
// linux_arm_v7 and linux_arm_v6, each with the binaries. Without -push
// the index is only built. The registry login comes from REGISTRY_USER and
// REGISTRY_PASSWORD; when RELEASE_SIGNING_KEY is set, pushed images are
// signed so self-updating agents accept them.
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/google/go-containerregistry/pkg/name"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentimage"
"git.zb-server.de/ZB-Server/uptimesebi/internal/signing"
)
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type listFlag []string
func (l *listFlag) String() string { return strings.Join(*l, ",") }
func (l *listFlag) Set(v string) error { *l = append(*l, v); return nil }
func run(args []string) error {
fs := flag.NewFlagSet("image", flag.ContinueOnError)
var o agentimage.Options
fs.StringVar(&o.Name, "name", "", "binary name: checker or statuspage")
fs.StringVar(&o.Dist, "dist", "dist", "directory with one subdirectory per platform")
fs.StringVar(&o.Version, "version", "dev", "release version, as the binaries report it")
fs.StringVar(&o.Commit, "commit", "", "git revision")
fs.StringVar(&o.Source, "source", "", "repository URL for the image labels")
fs.BoolVar(&o.Launcher, "autoupdate", false, "build the self-updating variant (tags get -autoupdate)")
fs.Var((*listFlag)(&o.Env), "env", "NAME=value baked into the image (repeatable)")
fs.Var((*listFlag)(&o.Ports), "port", "exposed port, e.g. 8081/tcp (repeatable)")
repo := fs.String("repo", "", "image repository, e.g. git.example.com/owner/uptimesebi-checker")
ref := fs.String("ref", "", "git ref being built, e.g. refs/heads/main or refs/tags/v1.2.3")
created := fs.Int64("created", 0, "creation time as Unix seconds, for reproducible images")
push := fs.Bool("push", false, "push to the registry")
insecure := fs.Bool("insecure", false, "talk plain HTTP to the registry")
if err := fs.Parse(args); err != nil {
return err
}
if o.Name == "" || *repo == "" {
return errors.New("image: -name and -repo are required")
}
o.Created = time.Unix(*created, 0).UTC()
idx, err := agentimage.Build(o)
if err != nil {
return err
}
digest, err := idx.Digest()
if err != nil {
return err
}
fmt.Printf("built %s@%s\n", *repo, digest)
if !*push {
return nil
}
suffix := ""
if o.Launcher {
suffix = "autoupdate"
}
tags := agentimage.ChannelTags(*ref, o.Version, suffix)
if len(tags) == 0 {
return fmt.Errorf("image: %q is not a ref that publishes images", *ref)
}
var nameOpts []name.Option
if *insecure {
nameOpts = append(nameOpts, name.Insecure)
}
ctx := context.Background()
login := agentimage.Credentials{User: os.Getenv("REGISTRY_USER"), Password: os.Getenv("REGISTRY_PASSWORD")}
if err := agentimage.Publish(ctx, idx, *repo, tags, login, nameOpts...); err != nil {
return err
}
fmt.Printf("pushed %s:%s\n", *repo, strings.Join(tags, ", :"))
raw := os.Getenv("RELEASE_SIGNING_KEY")
if raw == "" {
fmt.Println("RELEASE_SIGNING_KEY is not set; self-updating agents will not accept this image.")
return nil
}
key, err := signing.ParsePrivateKey(raw)
if err != nil {
return err
}
if err := agentimage.Sign(ctx, idx, *repo, o.Name, o.Version, key, login, nameOpts...); err != nil {
return err
}
fmt.Printf("signed %s@%s as %s\n", *repo, digest, o.Version)
return nil
}