| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner" |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | ) |
| 16 | |
| 17 | // The carlos CLI the runner runs for previews and sites: one lookup |
| 18 | // (EQUIPO_CARLOS_PATH, else PATH, else ~/.local/bin), an absolute regular |
| 19 | // executable file or none. A stale carlos earlier on PATH broke every |
| 20 | // preview on the production runner (2026-11-26), so its version is read |
| 21 | // at start and with every capability probe and said in the hello; a |
| 22 | // preview or a publish refuses carlos_outdated below wire.MinCarlos. |
| 23 | |
| 24 | // carlosLookup is the carlos the configuration names, "" when none is a |
| 25 | // usable file. |
| 26 | func carlosLookup(c runner.Config) string { |
| 27 | carlos := c.CarlosPath |
| 28 | if carlos == "" { |
| 29 | if p, err := exec.LookPath("carlos"); err == nil { |
| 30 | carlos = p |
| 31 | } else if home, err := os.UserHomeDir(); err == nil { |
| 32 | carlos = filepath.Join(home, ".local", "bin", "carlos") |
| 33 | } |
| 34 | } |
| 35 | if !filepath.IsAbs(carlos) { |
| 36 | return "" |
| 37 | } |
| 38 | if info, err := os.Stat(carlos); err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0o111 == 0 { |
| 39 | return "" |
| 40 | } |
| 41 | return carlos |
| 42 | } |
| 43 | |
| 44 | // carlosRelease is the release `carlos version` names on its first line. |
| 45 | var carlosRelease = regexp.MustCompile(`^carlos (v[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4})\b`) |
| 46 | |
| 47 | // carlosVersionOf runs `carlos version` (5 seconds, an empty environment) |
| 48 | // and returns the release it names, "" when it names none. |
| 49 | func carlosVersionOf(ctx context.Context, bin string) string { |
| 50 | if bin == "" { |
| 51 | return "" |
| 52 | } |
| 53 | cctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 54 | defer cancel() |
| 55 | cmd := exec.CommandContext(cctx, bin, "version") |
| 56 | dir, err := os.MkdirTemp("", "carlos-version-") |
| 57 | if err != nil { |
| 58 | return "" |
| 59 | } |
| 60 | defer os.RemoveAll(dir) |
| 61 | cmd.Dir, cmd.Env = dir, []string{"HOME=" + dir, "PATH=/usr/bin:/bin", "CARLOS_NO_UPDATE_CHECK=1"} |
| 62 | out, _, err := boundedOutput(cmd, 4096) |
| 63 | if err != nil { |
| 64 | return "" |
| 65 | } |
| 66 | line, _, _ := strings.Cut(ansi.ReplaceAllString(string(out), ""), "\n") |
| 67 | m := carlosRelease.FindStringSubmatch(strings.TrimSpace(line)) |
| 68 | if m == nil { |
| 69 | return "" |
| 70 | } |
| 71 | return m[1] |
| 72 | } |
| 73 | |
| 74 | // readCarlosVersion reads a carlos's version (a seam: the package's tests |
| 75 | // stand in for the many fake carlos binaries that have no version verb). |
| 76 | var readCarlosVersion = carlosVersionOf |
| 77 | |
| 78 | // carlosCLI is the runner's carlos and its version, read at most once a |
| 79 | // capability period. |
| 80 | type carlosCLI struct { |
| 81 | Bin string |
| 82 | // Read is the seam for the version read (nil: carlosVersionOf). |
| 83 | Read func(ctx context.Context, bin string) string |
| 84 | |
| 85 | mu sync.Mutex |
| 86 | version string |
| 87 | at time.Time |
| 88 | } |
| 89 | |
| 90 | // Version is the carlos release, re-read when older than an hour. |
| 91 | func (c *carlosCLI) Version(ctx context.Context) string { |
| 92 | if c == nil { |
| 93 | return "" |
| 94 | } |
| 95 | c.mu.Lock() |
| 96 | defer c.mu.Unlock() |
| 97 | if c.at.IsZero() || time.Since(c.at) > capabilityEvery { |
| 98 | read := c.Read |
| 99 | if read == nil { |
| 100 | read = readCarlosVersion |
| 101 | } |
| 102 | c.version, c.at = read(ctx, c.Bin), time.Now() |
| 103 | } |
| 104 | return c.version |
| 105 | } |
| 106 | |
| 107 | // Health is the carlos health a hello says: missing with no binary, |
| 108 | // outdated when its version is unreadable or below wire.MinCarlos, else |
| 109 | // ok, with the version when one was read. |
| 110 | func (c *carlosCLI) Health(ctx context.Context) (string, string) { |
| 111 | if c == nil || c.Bin == "" { |
| 112 | return wire.CarlosMissing, "" |
| 113 | } |
| 114 | v := c.Version(ctx) |
| 115 | if !wire.ValidCarlosVersion(v) { |
| 116 | return wire.CarlosOutdated, "" |
| 117 | } |
| 118 | if !wire.CarlosAtLeast(v, wire.MinCarlos) { |
| 119 | return wire.CarlosOutdated, v |
| 120 | } |
| 121 | return wire.CarlosOK, v |
| 122 | } |
| 123 | |
| 124 | // OK reports whether previews and sites may run carlos now. |
| 125 | func (c *carlosCLI) OK(ctx context.Context) bool { |
| 126 | state, _ := c.Health(ctx) |
| 127 | return state == wire.CarlosOK |
| 128 | } |
| 129 | |
| 130 | // carlosCLIs are the carlos binaries previews run, each read once a |
| 131 | // capability period. |
| 132 | var carlosCLIs sync.Map |
| 133 | |
| 134 | // carlosOK reports whether the carlos at bin is at least wire.MinCarlos. |
| 135 | func carlosOK(ctx context.Context, bin string) bool { |
| 136 | v, _ := carlosCLIs.LoadOrStore(bin, &carlosCLI{Bin: bin}) |
| 137 | return v.(*carlosCLI).OK(ctx) |
| 138 | } |
| 139 | |
| 140 | // previewCarlosOK reports whether the preview host's carlos may run: the |
| 141 | // devbox's, else the temporary sites' credential's. |
| 142 | func (e *Engineer) previewCarlosOK(ctx context.Context) bool { |
| 143 | switch { |
| 144 | case e.Devbox != nil: |
| 145 | return carlosOK(ctx, e.Devbox.Carlos) |
| 146 | case e.Preview != nil: |
| 147 | return carlosOK(ctx, e.Preview.Carlos) |
| 148 | } |
| 149 | return false |
| 150 | } |
| 151 | |