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
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package launcher
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"runtime/debug"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
)
|
|
|
|
type process struct {
|
|
cmd *exec.Cmd
|
|
done chan struct{}
|
|
err error
|
|
}
|
|
|
|
// StartProcess runs path with the launcher's environment and output.
|
|
func StartProcess(path string) (Process, error) {
|
|
cmd := exec.Command(path)
|
|
cmd.Stdout, cmd.Stderr, cmd.Env = os.Stdout, os.Stderr, os.Environ()
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
p := &process{cmd: cmd, done: make(chan struct{})}
|
|
go func() {
|
|
p.err = cmd.Wait()
|
|
close(p.done)
|
|
}()
|
|
return p, nil
|
|
}
|
|
|
|
// Done is closed when the agent has exited.
|
|
func (p *process) Done() <-chan struct{} { return p.done }
|
|
|
|
// Err is the agent's exit error.
|
|
func (p *process) Err() error { return p.err }
|
|
|
|
// Stop sends SIGTERM and kills the agent after timeout.
|
|
func (p *process) Stop(timeout time.Duration) {
|
|
_ = p.cmd.Process.Signal(syscall.SIGTERM)
|
|
select {
|
|
case <-p.done:
|
|
case <-time.After(timeout):
|
|
_ = p.cmd.Process.Kill()
|
|
<-p.done
|
|
}
|
|
}
|
|
|
|
// BinaryVersion asks an agent binary which release it is.
|
|
func BinaryVersion(path string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
out, err := exec.CommandContext(ctx, path, "version").Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
v := strings.TrimSpace(string(out))
|
|
if v == "" || strings.ContainsAny(v, " \n") {
|
|
return "", fmt.Errorf("launcher: %s reports no release", path)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// Platform is the platform this launcher was built for, and so the agent
|
|
// binaries it runs.
|
|
func Platform() v1.Platform {
|
|
p := v1.Platform{OS: runtime.GOOS, Architecture: runtime.GOARCH}
|
|
if runtime.GOARCH != "arm" {
|
|
return p
|
|
}
|
|
p.Variant = "v7" // Go's default for linux/arm
|
|
if info, ok := debug.ReadBuildInfo(); ok {
|
|
for _, s := range info.Settings {
|
|
if s.Key == "GOARM" && s.Value != "" {
|
|
p.Variant = "v" + s.Value[:1]
|
|
}
|
|
}
|
|
}
|
|
return p
|
|
}
|