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
This commit is contained in:
1 parent
431048e244
commit
a35dadd8f6
20 files changed
+1577
-443
No files matched your search
+46
-20
@@ -1,14 +1,15 @@
|
||||
// Command image builds the multi-arch images of an agent (checker or status
|
||||
// page server) from cross-compiled binaries, without Docker: one layer on
|
||||
// an empty base per platform, combined into an OCI index.
|
||||
// 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 [-push]
|
||||
// -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 binary. Without -push the
|
||||
// index is only built and checked. The registry login comes from
|
||||
// REGISTRY_USER and REGISTRY_PASSWORD.
|
||||
// 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 (
|
||||
@@ -19,6 +20,11 @@ import (
|
||||
"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() {
|
||||
@@ -35,16 +41,16 @@ 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 options
|
||||
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.Repo, "repo", "", "image repository, e.g. git.example.com/owner/uptimesebi-checker")
|
||||
fs.StringVar(&o.Version, "version", "dev", "release version")
|
||||
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.StringVar(&o.Suffix, "suffix", "", "tag suffix of an image variant, e.g. autoupdate")
|
||||
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")
|
||||
@@ -52,12 +58,12 @@ func run(args []string) error {
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
if o.Name == "" || o.Repo == "" {
|
||||
if o.Name == "" || *repo == "" {
|
||||
return errors.New("image: -name and -repo are required")
|
||||
}
|
||||
o.Created = time.Unix(*created, 0).UTC()
|
||||
|
||||
idx, err := build(o)
|
||||
idx, err := agentimage.Build(o)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -65,21 +71,41 @@ func run(args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tags := channelTags(*ref, o.Version, o.Commit, o.Created, o.Suffix)
|
||||
fmt.Printf("built %s@%s for %s\n", o.Repo, digest, strings.Join(platformNames(), ", "))
|
||||
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)
|
||||
}
|
||||
if *ref == "refs/heads/main" && (o.Commit == "" || *created == 0) {
|
||||
return errors.New("image: builds of main need -commit and -created for their tag")
|
||||
var nameOpts []name.Option
|
||||
if *insecure {
|
||||
nameOpts = append(nameOpts, name.Insecure)
|
||||
}
|
||||
login := credentials{os.Getenv("REGISTRY_USER"), os.Getenv("REGISTRY_PASSWORD")}
|
||||
if err := publish(context.Background(), idx, o.Repo, tags, login, *insecure); err != nil {
|
||||
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", o.Repo, strings.Join(tags, ", :"))
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user