| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/mac" |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider/pi" |
| 14 | ) |
| 15 | |
| 16 | // installPi installs the pinned Pi under root, for a Linux runner's |
| 17 | // metered connections (connectors spec §3, "Pinning"): Node from its |
| 18 | // official archive, checked against its digest before anything is |
| 19 | // unpacked; the npm tree from the committed lockfile with `npm ci |
| 20 | // --ignore-scripts`; the tree sealed read-only; then the Node program |
| 21 | // and the whole tree held to the committed digests (pi.Verify). It |
| 22 | // prints the line for the runner's environment file. An install that |
| 23 | // does not verify is removed. |
| 24 | func installPi(ctx context.Context, args []string, output, errout io.Writer) int { |
| 25 | if len(args) != 1 || !filepath.IsAbs(args[0]) || filepath.Clean(args[0]) != args[0] { |
| 26 | fmt.Fprintln(errout, "usage: equipo-runner install-pi <absolute root>") |
| 27 | return 2 |
| 28 | } |
| 29 | root := args[0] |
| 30 | if err := installPiAt(ctx, root, runtime.GOARCH, fetchTool, npmCIPi); err != nil { |
| 31 | fmt.Fprintln(errout, "equipo-runner: the pinned Pi could not be installed:", err) |
| 32 | return 1 |
| 33 | } |
| 34 | fmt.Fprintf(output, "EQUIPO_PI_ROOT=%q\n", root) |
| 35 | return 0 |
| 36 | } |
| 37 | |
| 38 | func npmCIPi(ctx context.Context, node, npmCLI, prefix, cache string) error { |
| 39 | cmd := exec.CommandContext(ctx, node, npmCLI, "ci", "--ignore-scripts", "--no-audit", "--no-fund") |
| 40 | cmd.Dir = prefix |
| 41 | cmd.Env = []string{"PATH=" + filepath.Dir(node) + ":/usr/bin:/bin", "HOME=" + cache, "npm_config_cache=" + cache, "npm_config_update_notifier=false"} |
| 42 | // The host's own way out to the registry, if it has one. |
| 43 | for _, k := range []string{"HTTPS_PROXY", "HTTP_PROXY", "NO_PROXY", "https_proxy", "http_proxy", "no_proxy"} { |
| 44 | if v := os.Getenv(k); v != "" { |
| 45 | cmd.Env = append(cmd.Env, k+"="+v) |
| 46 | } |
| 47 | } |
| 48 | cmd.Stderr = os.Stderr |
| 49 | return cmd.Run() |
| 50 | } |
| 51 | |
| 52 | func installPiAt(ctx context.Context, root, goarch string, fetch func(context.Context, string, int64) ([]byte, error), npmCI func(ctx context.Context, node, npmCLI, prefix, cache string) error) error { |
| 53 | if pi.Verify(root) == nil { |
| 54 | return nil |
| 55 | } |
| 56 | _, archive, ok := pi.Pins(goarch) |
| 57 | if !ok { |
| 58 | return pi.ErrUnpinned |
| 59 | } |
| 60 | if err := os.MkdirAll(root, 0700); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | scratch, err := os.MkdirTemp(root, ".install-") |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | defer removeTree(scratch) |
| 68 | tool := mac.Tool{Name: "node", Version: archive.Version, URL: archive.URL, SHA256: archive.SHA256, Root: archive.Root, Binary: "bin/node", |
| 69 | Links: map[string]string{"bin/npm": "../lib/node_modules/npm/bin/npm-cli.js", "bin/npx": "../lib/node_modules/npm/bin/npx-cli.js"}, |
| 70 | SkipLinks: []string{"bin/corepack"}} |
| 71 | node, err := mac.Fetch(ctx, tool, filepath.Join(scratch, "tools"), fetch) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | prefix := filepath.Join(scratch, "pi") |
| 76 | if err := os.MkdirAll(prefix, 0700); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | pkg, lock := pi.Package() |
| 80 | if os.WriteFile(filepath.Join(prefix, "package.json"), pkg, 0600) != nil || os.WriteFile(filepath.Join(prefix, "package-lock.json"), lock, 0600) != nil { |
| 81 | return pi.ErrUnpinned |
| 82 | } |
| 83 | cache := filepath.Join(scratch, "npm") |
| 84 | if err := os.MkdirAll(cache, 0700); err != nil { |
| 85 | return err |
| 86 | } |
| 87 | if err := npmCI(ctx, node, filepath.Join(filepath.Dir(filepath.Dir(node)), "lib", "node_modules", "npm", "bin", "npm-cli.js"), prefix, cache); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | tree := filepath.Join(prefix, "node_modules") |
| 91 | if err := pi.Seal(tree); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | // Into place: the old install (if any) goes first. |
| 95 | nodeDst, treeDst := pi.Paths(root) |
| 96 | for _, old := range []string{filepath.Dir(filepath.Dir(nodeDst)), filepath.Dir(treeDst)} { |
| 97 | removeTree(old) |
| 98 | } |
| 99 | if err := os.MkdirAll(filepath.Dir(nodeDst), 0700); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | raw, err := os.ReadFile(node) |
| 103 | if err != nil || os.WriteFile(nodeDst, raw, 0555) != nil { |
| 104 | return pi.ErrUnpinned |
| 105 | } |
| 106 | if err := os.MkdirAll(filepath.Dir(treeDst), 0700); err != nil { |
| 107 | return err |
| 108 | } |
| 109 | // A directory moves to another parent only while it is writable. |
| 110 | if err := os.Chmod(tree, 0755); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | if err := os.Rename(tree, treeDst); err != nil { |
| 114 | return err |
| 115 | } |
| 116 | if err := os.Chmod(treeDst, 0555); err != nil { |
| 117 | return err |
| 118 | } |
| 119 | if err := pi.Verify(root); err != nil { |
| 120 | removeTree(filepath.Dir(treeDst)) |
| 121 | removeTree(filepath.Dir(filepath.Dir(nodeDst))) |
| 122 | return err |
| 123 | } |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | // removeTree removes a tree a seal made read-only. |
| 128 | func removeTree(dir string) { |
| 129 | filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { |
| 130 | if err == nil && info.IsDir() { |
| 131 | os.Chmod(p, 0700) |
| 132 | } |
| 133 | return nil |
| 134 | }) |
| 135 | os.RemoveAll(dir) |
| 136 | } |
| 137 | |