apply.go raw

   1  package release
   2  
   3  import (
   4  	"context"
   5  	"crypto/ed25519"
   6  	"fmt"
   7  	"net/http"
   8  	"os"
   9  	"os/exec"
  10  	"runtime"
  11  	"syscall"
  12  	"time"
  13  )
  14  
  15  // Options is what an operator configures once.
  16  type Options struct {
  17  	// ManifestURL is the signed manifest's URL.
  18  	ManifestURL string
  19  	// PublicKey verifies the manifest.
  20  	PublicKey []byte
  21  	// Path is the running binary; empty means os.Executable().
  22  	Path string
  23  	// Current is the running version, compared against the manifest.
  24  	Current string
  25  	// Client is the HTTP client; nil means a two-minute default.
  26  	Client *http.Client
  27  }
  28  
  29  // Result says what happened, so an operator or an admin channel can report it.
  30  type Result struct {
  31  	Previous  string `json:"previous"`
  32  	Installed string `json:"installed"`
  33  	Version   string `json:"version"`
  34  	Path      string `json:"path"`
  35  	Notes     string `json:"notes,omitempty"`
  36  	NoOp      bool   `json:"no_op,omitempty"`
  37  }
  38  
  39  // Check fetches and verifies a manifest without installing anything.
  40  func Check(ctx context.Context, opts Options) (Manifest, error) {
  41  	if opts.ManifestURL == "" {
  42  		return Manifest{}, fmt.Errorf("release: no manifest URL configured")
  43  	}
  44  
  45  	if len(opts.PublicKey) != ed25519.PublicKeySize {
  46  		return Manifest{}, fmt.Errorf("release: no update public key configured")
  47  	}
  48  
  49  	body, err := Fetch(ctx, opts.Client, opts.ManifestURL)
  50  
  51  	if err != nil {
  52  		return Manifest{}, err
  53  	}
  54  
  55  	manifest, _, err := Verify(body, opts.PublicKey)
  56  
  57  	if err != nil {
  58  		return Manifest{}, err
  59  	}
  60  
  61  	return manifest, nil
  62  }
  63  
  64  // Apply downloads, verifies and installs a newer release. It does not restart anything: the
  65  // caller decides when to exec the new binary, because a gateway must answer before it replaces
  66  // itself.
  67  func Apply(ctx context.Context, opts Options) (Result, error) {
  68  	manifest, err := Check(ctx, opts)
  69  
  70  	if err != nil {
  71  		return Result{}, err
  72  	}
  73  
  74  	if err := Newer(opts.Current, manifest); err != nil {
  75  		return Result{NoOp: true, Version: opts.Current}, err
  76  	}
  77  
  78  	artifact, err := manifest.Choose(runtime.GOOS, runtime.GOARCH)
  79  
  80  	if err != nil {
  81  		return Result{}, err
  82  	}
  83  
  84  	body, err := Download(ctx, opts.Client, artifact)
  85  
  86  	if err != nil {
  87  		return Result{}, err
  88  	}
  89  
  90  	path := opts.Path
  91  
  92  	if path == "" {
  93  		if path, err = os.Executable(); err != nil {
  94  			return Result{}, err
  95  		}
  96  	}
  97  
  98  	previous, err := Install(path, body)
  99  
 100  	if err != nil {
 101  		return Result{}, err
 102  	}
 103  
 104  	return Result{
 105  		Previous: previous, Installed: artifact.URL, Version: manifest.Version,
 106  		Path: path, Notes: manifest.Notes,
 107  	}, nil
 108  }
 109  
 110  // Exec replaces the running process with the installed binary. It only returns on failure, which
 111  // is the point: a successful update has no old process left to run the rest of the code.
 112  func Exec(path string) error {
 113  	argv := append([]string{path}, os.Args[1:]...)
 114  	return syscall.Exec(path, argv, os.Environ())
 115  }
 116  
 117  // ExecAfter runs the new binary as a child and waits for it to stay up, for callers that cannot
 118  // replace themselves. The child owns the gateway afterwards; the parent returns.
 119  func ExecAfter(path string, settle time.Duration) error {
 120  	command := exec.Command(path, os.Args[1:]...)
 121  	command.Stdout = os.Stdout
 122  	command.Stderr = os.Stderr
 123  	command.Env = os.Environ()
 124  
 125  	if err := command.Start(); err != nil {
 126  		return err
 127  	}
 128  
 129  	// If the child dies immediately the release did not work; the caller still has the previous
 130  	// binary and can roll back.
 131  	time.Sleep(settle)
 132  
 133  	if command.ProcessState != nil && !command.ProcessState.Success() {
 134  		return fmt.Errorf("release: the new binary exited: %v", command.ProcessState)
 135  	}
 136  
 137  	return command.Process.Release()
 138  }
 139