package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"amadan.net/oficina/equipo/internal/release"
"amadan.net/oficina/equipo/internal/runner"
"amadan.net/oficina/equipo/internal/runner/render"
)
// rendererPin is the renderer pin linked into this build ("" for none);
// tests replace it.
var rendererPin = func() string {
pin, _ := release.RendererPin()
return pin
}
// fixtureRender renders the fixture page; tests replace it.
var fixtureRender = render.CheckFixture
// rendererPlatform is where a runner has the page renderer: Linux amd64.
// A Mac runner and a Linux arm64 one neither install nor check one.
func rendererPlatform(goos, goarch string) bool { return render.Platform(goos, goarch) }
// verifyRenderer is verify-installed's renderer check, on Linux amd64
// only: the tree beside the runner's executable
// (
/renderer//) is exactly the one the pin linked
// into this build names, and it renders the fixture page, through the
// bubblewrap of this configuration (checked before this runs) and the
// runner's own binary, with its scratch in /renderer/.
func verifyRenderer(cfg runner.Config, goos, goarch string) error {
if !rendererPlatform(goos, goarch) {
return nil
}
launch, scratch := render.Installed(cfg.Bwrap(), cfg.Executable, rendererPin())
if err := release.VerifyRenderer(launch.Tree, launch.Pin); err != nil {
return err
}
return fixtureRender(context.Background(), launch, scratch)
}
// installRendererCommand is `equipo-runner install-renderer `:
// the release's renderer.tar.gz, unpacked beside this runner's own
// executable as the tree for the pin linked into it (release.
// InstallRenderer), and that tree's path printed. The production scripts
// run it from the installed runner, then verify-installed, which renders
// the fixture page with it.
func installRendererCommand(args []string, out, errout io.Writer) int {
if len(args) != 1 || !filepath.IsAbs(args[0]) {
fmt.Fprintln(errout, "usage: equipo-runner install-renderer ")
return 2
}
exe, err := os.Executable()
if err == nil {
var tree string
if tree, err = installRendererOn(goos(), goarch(), filepath.Dir(exe), args[0], rendererPin()); err == nil {
fmt.Fprintln(out, tree)
return 0
}
}
fmt.Fprintln(errout, "equipo-runner: the renderer could not be installed:", err)
return 1
}
func installRendererOn(goos, goarch, runnerDir, archive, pin string) (string, error) {
if !rendererPlatform(goos, goarch) {
return "", errors.New("the renderer runs only on Linux amd64")
}
if !filepath.IsAbs(archive) {
return "", fmt.Errorf("%q is not an absolute path", archive)
}
f, err := os.Open(archive)
if err != nil {
return "", err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return "", err
}
if !info.Mode().IsRegular() || info.Size() > release.RendererArchiveMax {
return "", fmt.Errorf("%s is not a renderer archive", archive)
}
return release.InstallRenderer(runnerDir, f, pin)
}
// linkedRendererAsset is the renderer asset linked into this build;
// tests replace it.
var linkedRendererAsset = release.LinkedRendererAsset
// rendererAssetCommand is `equipo-runner renderer-asset --json`: the
// renderer this runner installs (release.LinkedRendererAsset) as one JSON
// object, its forge tag, tree pin, archive digest and size. A build with
// none linked, or one not whole, prints nothing and exits 1.
func rendererAssetCommand(args []string, out, errout io.Writer) int {
if len(args) != 1 || args[0] != "--json" {
fmt.Fprintln(errout, "usage: equipo-runner renderer-asset --json")
return 2
}
a, ok := linkedRendererAsset()
if !ok {
fmt.Fprintln(errout, "equipo-runner: no renderer asset is linked into this build")
return 1
}
if err := json.NewEncoder(out).Encode(struct {
Tag string `json:"tag"`
Pin string `json:"pin"`
SHA256 string `json:"sha256"`
Size int64 `json:"size"`
}{a.Tag, a.Pin, a.SHA256, a.Size}); err != nil {
return 1
}
return 0
}