package release import ( "context" "crypto/ed25519" "fmt" "net/http" "os" "os/exec" "runtime" "syscall" "time" ) // Options is what an operator configures once. type Options struct { // ManifestURL is the signed manifest's URL. ManifestURL string // PublicKey verifies the manifest. PublicKey []byte // Path is the running binary; empty means os.Executable(). Path string // Current is the running version, compared against the manifest. Current string // Client is the HTTP client; nil means a two-minute default. Client *http.Client } // Result says what happened, so an operator or an admin channel can report it. type Result struct { Previous string `json:"previous"` Installed string `json:"installed"` Version string `json:"version"` Path string `json:"path"` Notes string `json:"notes,omitempty"` NoOp bool `json:"no_op,omitempty"` } // Check fetches and verifies a manifest without installing anything. func Check(ctx context.Context, opts Options) (Manifest, error) { if opts.ManifestURL == "" { return Manifest{}, fmt.Errorf("release: no manifest URL configured") } if len(opts.PublicKey) != ed25519.PublicKeySize { return Manifest{}, fmt.Errorf("release: no update public key configured") } body, err := Fetch(ctx, opts.Client, opts.ManifestURL) if err != nil { return Manifest{}, err } manifest, _, err := Verify(body, opts.PublicKey) if err != nil { return Manifest{}, err } return manifest, nil } // Apply downloads, verifies and installs a newer release. It does not restart anything: the // caller decides when to exec the new binary, because a gateway must answer before it replaces // itself. func Apply(ctx context.Context, opts Options) (Result, error) { manifest, err := Check(ctx, opts) if err != nil { return Result{}, err } if err := Newer(opts.Current, manifest); err != nil { return Result{NoOp: true, Version: opts.Current}, err } artifact, err := manifest.Choose(runtime.GOOS, runtime.GOARCH) if err != nil { return Result{}, err } body, err := Download(ctx, opts.Client, artifact) if err != nil { return Result{}, err } path := opts.Path if path == "" { if path, err = os.Executable(); err != nil { return Result{}, err } } previous, err := Install(path, body) if err != nil { return Result{}, err } return Result{ Previous: previous, Installed: artifact.URL, Version: manifest.Version, Path: path, Notes: manifest.Notes, }, nil } // Exec replaces the running process with the installed binary. It only returns on failure, which // is the point: a successful update has no old process left to run the rest of the code. func Exec(path string) error { argv := append([]string{path}, os.Args[1:]...) return syscall.Exec(path, argv, os.Environ()) } // ExecAfter runs the new binary as a child and waits for it to stay up, for callers that cannot // replace themselves. The child owns the gateway afterwards; the parent returns. func ExecAfter(path string, settle time.Duration) error { command := exec.Command(path, os.Args[1:]...) command.Stdout = os.Stdout command.Stderr = os.Stderr command.Env = os.Environ() if err := command.Start(); err != nil { return err } // If the child dies immediately the release did not work; the caller still has the previous // binary and can roll back. time.Sleep(settle) if command.ProcessState != nil && !command.ProcessState.Success() { return fmt.Errorf("release: the new binary exited: %v", command.ProcessState) } return command.Process.Release() }