| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/release" |
| 13 | "amadan.net/oficina/equipo/internal/runner" |
| 14 | "amadan.net/oficina/equipo/internal/runner/render" |
| 15 | ) |
| 16 | |
| 17 | // rendererPin is the renderer pin linked into this build ("" for none); |
| 18 | // tests replace it. |
| 19 | var rendererPin = func() string { |
| 20 | pin, _ := release.RendererPin() |
| 21 | return pin |
| 22 | } |
| 23 | |
| 24 | // fixtureRender renders the fixture page; tests replace it. |
| 25 | var fixtureRender = render.CheckFixture |
| 26 | |
| 27 | // rendererPlatform is where a runner has the page renderer: Linux amd64. |
| 28 | // A Mac runner and a Linux arm64 one neither install nor check one. |
| 29 | func rendererPlatform(goos, goarch string) bool { return render.Platform(goos, goarch) } |
| 30 | |
| 31 | // verifyRenderer is verify-installed's renderer check, on Linux amd64 |
| 32 | // only: the tree beside the runner's executable |
| 33 | // (<dir>/renderer/<first 12 of pin>/) is exactly the one the pin linked |
| 34 | // into this build names, and it renders the fixture page, through the |
| 35 | // bubblewrap of this configuration (checked before this runs) and the |
| 36 | // runner's own binary, with its scratch in <dir>/renderer/. |
| 37 | func verifyRenderer(cfg runner.Config, goos, goarch string) error { |
| 38 | if !rendererPlatform(goos, goarch) { |
| 39 | return nil |
| 40 | } |
| 41 | launch, scratch := render.Installed(cfg.Bwrap(), cfg.Executable, rendererPin()) |
| 42 | if err := release.VerifyRenderer(launch.Tree, launch.Pin); err != nil { |
| 43 | return err |
| 44 | } |
| 45 | return fixtureRender(context.Background(), launch, scratch) |
| 46 | } |
| 47 | |
| 48 | // installRendererCommand is `equipo-runner install-renderer <archive>`: |
| 49 | // the release's renderer.tar.gz, unpacked beside this runner's own |
| 50 | // executable as the tree for the pin linked into it (release. |
| 51 | // InstallRenderer), and that tree's path printed. The production scripts |
| 52 | // run it from the installed runner, then verify-installed, which renders |
| 53 | // the fixture page with it. |
| 54 | func installRendererCommand(args []string, out, errout io.Writer) int { |
| 55 | if len(args) != 1 || !filepath.IsAbs(args[0]) { |
| 56 | fmt.Fprintln(errout, "usage: equipo-runner install-renderer <absolute path of renderer.tar.gz>") |
| 57 | return 2 |
| 58 | } |
| 59 | exe, err := os.Executable() |
| 60 | if err == nil { |
| 61 | var tree string |
| 62 | if tree, err = installRendererOn(goos(), goarch(), filepath.Dir(exe), args[0], rendererPin()); err == nil { |
| 63 | fmt.Fprintln(out, tree) |
| 64 | return 0 |
| 65 | } |
| 66 | } |
| 67 | fmt.Fprintln(errout, "equipo-runner: the renderer could not be installed:", err) |
| 68 | return 1 |
| 69 | } |
| 70 | |
| 71 | func installRendererOn(goos, goarch, runnerDir, archive, pin string) (string, error) { |
| 72 | if !rendererPlatform(goos, goarch) { |
| 73 | return "", errors.New("the renderer runs only on Linux amd64") |
| 74 | } |
| 75 | if !filepath.IsAbs(archive) { |
| 76 | return "", fmt.Errorf("%q is not an absolute path", archive) |
| 77 | } |
| 78 | f, err := os.Open(archive) |
| 79 | if err != nil { |
| 80 | return "", err |
| 81 | } |
| 82 | defer f.Close() |
| 83 | info, err := f.Stat() |
| 84 | if err != nil { |
| 85 | return "", err |
| 86 | } |
| 87 | if !info.Mode().IsRegular() || info.Size() > release.RendererArchiveMax { |
| 88 | return "", fmt.Errorf("%s is not a renderer archive", archive) |
| 89 | } |
| 90 | return release.InstallRenderer(runnerDir, f, pin) |
| 91 | } |
| 92 | |
| 93 | // linkedRendererAsset is the renderer asset linked into this build; |
| 94 | // tests replace it. |
| 95 | var linkedRendererAsset = release.LinkedRendererAsset |
| 96 | |
| 97 | // rendererAssetCommand is `equipo-runner renderer-asset --json`: the |
| 98 | // renderer this runner installs (release.LinkedRendererAsset) as one JSON |
| 99 | // object, its forge tag, tree pin, archive digest and size. A build with |
| 100 | // none linked, or one not whole, prints nothing and exits 1. |
| 101 | func rendererAssetCommand(args []string, out, errout io.Writer) int { |
| 102 | if len(args) != 1 || args[0] != "--json" { |
| 103 | fmt.Fprintln(errout, "usage: equipo-runner renderer-asset --json") |
| 104 | return 2 |
| 105 | } |
| 106 | a, ok := linkedRendererAsset() |
| 107 | if !ok { |
| 108 | fmt.Fprintln(errout, "equipo-runner: no renderer asset is linked into this build") |
| 109 | return 1 |
| 110 | } |
| 111 | if err := json.NewEncoder(out).Encode(struct { |
| 112 | Tag string `json:"tag"` |
| 113 | Pin string `json:"pin"` |
| 114 | SHA256 string `json:"sha256"` |
| 115 | Size int64 `json:"size"` |
| 116 | }{a.Tag, a.Pin, a.SHA256, a.Size}); err != nil { |
| 117 | return 1 |
| 118 | } |
| 119 | return 0 |
| 120 | } |
| 121 | |