| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/provider/opencode" |
| 5 | "amadan.net/oficina/equipo/internal/runner/provider/pi" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/equipo" |
| 17 | "amadan.net/oficina/equipo/internal/release" |
| 18 | "amadan.net/oficina/equipo/internal/runner" |
| 19 | runnersetup "amadan.net/oficina/equipo/internal/runner/setup" |
| 20 | "amadan.net/rastrillo/rastrillo" |
| 21 | ) |
| 22 | |
| 23 | // linuxInstallHost is what installing on Linux touches; tests stand in. |
| 24 | type linuxInstallHost struct { |
| 25 | home func() (string, error) |
| 26 | chdir func(string) error |
| 27 | installable func() bool |
| 28 | install func(context.Context, runnersetup.InstallOptions) (runnersetup.InstallResult, error) |
| 29 | config func(root, origin, instance, version string, getenv func(string) string) (runner.Config, error) |
| 30 | verify func(runner.Config) error |
| 31 | enrol func(context.Context, runner.Config, io.Writer) error |
| 32 | checkEnv func(root, origin, state string) error |
| 33 | // owned is whether the installer may replace a file it wrote before |
| 34 | // (runnersetup.InstallerOwns): never one edited by hand. |
| 35 | owned func(path string) bool |
| 36 | } |
| 37 | |
| 38 | func installRunner(ctx context.Context, args []string, input io.Reader, out, errout io.Writer, getenv func(string) string) int { |
| 39 | return installLinux(ctx, args, input, out, errout, getenv, linuxInstallHost{ |
| 40 | home: os.UserHomeDir, chdir: os.Chdir, |
| 41 | installable: func() bool { return release.BuildManifest().Installable() }, |
| 42 | install: runnersetup.InstallUser, config: installConfig, |
| 43 | verify: func(cfg runner.Config) error { return verifyInstalled(release.BuildManifest(), cfg) }, |
| 44 | enrol: runner.Enrol, |
| 45 | checkEnv: runnersetup.CheckServiceEnvironment, |
| 46 | owned: runnersetup.InstallerOwns, |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | // installLinux installs a runner under --root (default |
| 51 | // ~/.local/share/equipo-runner), from the user's home. A root whose |
| 52 | // runner is enrolled with another Equipo is refused before anything is |
| 53 | // installed. The unit and the environment file a runner installed before |
| 54 | // uses are kept aside until the enrolment is done, and put back if the |
| 55 | // install stops before then; the running service is never stopped. |
| 56 | func installLinux(ctx context.Context, args []string, input io.Reader, out, errout io.Writer, getenv func(string) string, h linuxInstallHost) int { |
| 57 | fail := func(key string) int { fmt.Fprintln(errout, equipo.RunnerSetupCopy(key)); return 1 } |
| 58 | flags := flag.NewFlagSet("install", flag.ContinueOnError) |
| 59 | flags.SetOutput(io.Discard) |
| 60 | home, e := h.home() |
| 61 | if e != nil { |
| 62 | return fail("runner.install.destination") |
| 63 | } |
| 64 | instance := flags.String("instance", "", "") |
| 65 | version := flags.String("release", "", "") |
| 66 | origin := flags.String("origin", "", "") |
| 67 | root := flags.String("root", filepath.Join(home, ".local/share/equipo-runner"), "") |
| 68 | if flags.Parse(args) != nil || flags.NArg() != 0 || *instance == "" || *version != rastrillo.BuildVersion || !h.installable() { |
| 69 | return fail("runner.install.release") |
| 70 | } |
| 71 | // Everything runs from the user's home, never the folder the |
| 72 | // installer was started from (which this user may not read). |
| 73 | if h.chdir(home) != nil { |
| 74 | return fail("runner.install.destination") |
| 75 | } |
| 76 | // One install at a time, from this check until it is enrolled or put |
| 77 | // back: a second one is refused, not let put back over the first. |
| 78 | lock, e := installLock(filepath.Join(*root, "state")) |
| 79 | if e != nil { |
| 80 | return fail(lockKey(e)) |
| 81 | } |
| 82 | defer lock.Close() |
| 83 | if key, a := enrolledElsewhere(filepath.Join(*root, "state"), *origin, *instance, "runner.install.other_instance"); key != "" { |
| 84 | fmt.Fprintf(errout, equipo.RunnerSetupCopy(key)+"\n", a...) |
| 85 | return 1 |
| 86 | } |
| 87 | // The unit and environment file are kept aside until the enrolment |
| 88 | // is done; on a new root they do not exist yet, and a failed install |
| 89 | // removes what it wrote there. One edited by hand is never replaced. |
| 90 | envFile, unit := filepath.Join(*root, "runner.env"), filepath.Join(*root, "equipo-runner.service") |
| 91 | if !h.owned(envFile) || !h.owned(unit) { |
| 92 | return fail("runner.install.destination") |
| 93 | } |
| 94 | backup, e := newInstallBackup(*root) |
| 95 | if e != nil { |
| 96 | return fail("runner.install.destination") |
| 97 | } |
| 98 | if e = backup.keep(envFile, unit); e != nil { |
| 99 | backup.restore() |
| 100 | return fail("runner.install.destination") |
| 101 | } |
| 102 | putBack := func() int { |
| 103 | backup.restore() |
| 104 | return 1 |
| 105 | } |
| 106 | abort := func(key string) int { fail(key); return putBack() } |
| 107 | result, e := h.install(ctx, runnersetup.InstallOptions{Release: *version, Root: *root, InstanceOrigin: *origin}) |
| 108 | if e != nil { |
| 109 | for _, key := range result.Missing { |
| 110 | fmt.Fprintln(errout, equipo.RunnerSetupCopy(key)) |
| 111 | } |
| 112 | return putBack() |
| 113 | } |
| 114 | // Ctrl-C at either prompt ends the install and puts back what was |
| 115 | // kept aside, as it does while waiting for the link. |
| 116 | lines := newPromptLines(input) |
| 117 | label, e := promptMachineLabel(ctx, lines, out, *root, *origin, *instance) |
| 118 | if e != nil { |
| 119 | return abort("runner.install.unavailable") |
| 120 | } |
| 121 | fmt.Fprint(out, equipo.RunnerSetupCopy("runner.install.enrol")) |
| 122 | confirm, e := lines.read(ctx) |
| 123 | if e != nil || strings.TrimSpace(confirm) != "yes" { |
| 124 | return abort("runner.install.unavailable") |
| 125 | } |
| 126 | // Runtime configuration stays explicit. The installer never guesses credential |
| 127 | // paths or creates a provider home. Task 17 supplies the local account setup. |
| 128 | cfg, e := h.config(*root, *origin, *instance, *version, getenv) |
| 129 | if e != nil { |
| 130 | return abort("runner.install.config") |
| 131 | } |
| 132 | if cfg.Origin != *origin || cfg.StateDir != filepath.Join(*root, "state") { |
| 133 | return abort("runner.install.config") |
| 134 | } |
| 135 | // The service starts the installed runner, not this one, and that |
| 136 | // runner looks for the scanner beside itself. |
| 137 | installed := cfg |
| 138 | installed.Executable = result.BinaryPath |
| 139 | if result.BinaryPath == "" || h.verify(installed) != nil { |
| 140 | return abort("runner.install.release") |
| 141 | } |
| 142 | cfg.MachineLabel = label |
| 143 | report := enrolReport{errout: errout, log: filepath.Join(*root, "install.log")} |
| 144 | cfg.EnrolNotify = report.notify |
| 145 | // An error after the credential was published (its directory sync) |
| 146 | // is still an enrolment: what was kept aside is not put back beside it. |
| 147 | before := credentialBefore(cfg.StateDir) |
| 148 | if e = h.enrol(ctx, cfg, out); e != nil { |
| 149 | enrolled, unknown := enrolOutcome(cfg.StateDir, *origin, *instance, before) |
| 150 | switch { |
| 151 | case unknown: |
| 152 | // Whether it was published cannot be read: nothing is put back |
| 153 | // beside what may be a new credential, and what was kept aside |
| 154 | // stays where it is. |
| 155 | report.failed(e) |
| 156 | return 1 |
| 157 | case !enrolled: |
| 158 | report.failed(e) |
| 159 | return putBack() |
| 160 | } |
| 161 | } |
| 162 | // Enrolled: the state now holds the credential this configuration |
| 163 | // goes with, so the earlier one is not put back any more. |
| 164 | backup.drop() |
| 165 | // Use the already configured environment file. Never copy the invoking |
| 166 | // process's environment (which may contain credentials) into service metadata. |
| 167 | if e = h.checkEnv(*root, cfg.Origin, cfg.StateDir); e != nil { |
| 168 | return fail("runner.install.config") |
| 169 | } |
| 170 | result, e = h.install(ctx, runnersetup.InstallOptions{Release: *version, Root: *root, InstanceOrigin: *origin, EnableService: true}) |
| 171 | if e != nil { |
| 172 | for _, key := range result.Missing { |
| 173 | fmt.Fprintln(errout, equipo.RunnerSetupCopy(key)) |
| 174 | } |
| 175 | return 1 |
| 176 | } |
| 177 | fmt.Fprintln(out, equipo.RunnerSetupCopy("runner.install."+result.State)) |
| 178 | return 0 |
| 179 | } |
| 180 | |
| 181 | // scannerPin is the gitleaks hash linked into this build; tests replace it. |
| 182 | var scannerPin = release.GitleaksSHA256 |
| 183 | |
| 184 | var errScanner = errors.New("preview_scanner_unverified") |
| 185 | |
| 186 | // verifyInstalled holds every program this runner starts, at the paths in |
| 187 | // its configuration, to the digests of this release: bubblewrap, claude, |
| 188 | // codex, its code mode host and the tool proxy built with this runner, |
| 189 | // and the preview scanner (gitleaks) beside the runner's executable, where |
| 190 | // the runner looks for it, to the hash linked into this build. Without |
| 191 | // that scanner every preview fails (preview_scanner). On Linux amd64 the |
| 192 | // page renderer beside it, too (verifyRenderer). |
| 193 | // The installer runs it before enrolling, and staging and the production |
| 194 | // cutover run it (equipo-runner verify-installed) before starting a unit. |
| 195 | func verifyInstalled(m release.Manifest, cfg runner.Config) error { |
| 196 | if !release.VerifyGitleaks(filepath.Join(filepath.Dir(cfg.Executable), "gitleaks"), scannerPin()) { |
| 197 | return errScanner |
| 198 | } |
| 199 | // The pinned Pi and OpenCode, where this runner has them, are held |
| 200 | // to their pins too. |
| 201 | if cfg.PiRoot != "" { |
| 202 | if err := pi.Verify(cfg.PiRoot); err != nil { |
| 203 | return err |
| 204 | } |
| 205 | } |
| 206 | if cfg.OpenCodeRoot != "" { |
| 207 | if err := opencode.Verify(cfg.OpenCodeRoot); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | } |
| 211 | if err := m.VerifyInstalled(map[string]string{"bwrap": cfg.Bwrap(), "claude": cfg.ClaudePath, "codex": cfg.CodexPath, "codex-code-mode-host": cfg.CodexCodeModeHostPath, "equipo-tool-proxy": cfg.ToolProxy()}); err != nil { |
| 212 | return err |
| 213 | } |
| 214 | // Last, once bubblewrap is the pinned one: on Linux amd64 the page |
| 215 | // renderer beside the runner, to its linked pin, and a fixture render. |
| 216 | return verifyRenderer(cfg, goos(), goarch()) |
| 217 | } |
| 218 | func installManifest(args []string, out io.Writer) int { |
| 219 | if len(args) != 1 { |
| 220 | return 1 |
| 221 | } |
| 222 | b, e := os.ReadFile(args[0]) |
| 223 | if e != nil { |
| 224 | return 1 |
| 225 | } |
| 226 | var m release.InstallManifest |
| 227 | if json.Unmarshal(b, &m) != nil || m.Validate(rastrillo.BuildVersion) != nil { |
| 228 | return 1 |
| 229 | } |
| 230 | if json.NewEncoder(out).Encode(m) != nil { |
| 231 | return 1 |
| 232 | } |
| 233 | return 0 |
| 234 | } |
| 235 | |
| 236 | func promptMachineLabel(ctx context.Context, lines *promptLines, out io.Writer, root, origin, instance string) (string, error) { |
| 237 | label, e := runner.DefaultMachineLabel(filepath.Join(root, "state"), origin, instance) |
| 238 | if e != nil { |
| 239 | return "", e |
| 240 | } |
| 241 | fmt.Fprintf(out, equipo.RunnerSetupCopy("runner.install.label"), label) |
| 242 | edited, e := lines.read(ctx) |
| 243 | if e != nil { |
| 244 | return "", e |
| 245 | } |
| 246 | if strings.TrimSpace(edited) != "" { |
| 247 | label = strings.TrimSpace(edited) |
| 248 | } |
| 249 | return label, nil |
| 250 | } |
| 251 | |