package main import ( "context" "fmt" "io" "os" "os/exec" "path/filepath" "runtime" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/runner/provider/pi" ) // installPi installs the pinned Pi under root, for a Linux runner's // metered connections (connectors spec ยง3, "Pinning"): Node from its // official archive, checked against its digest before anything is // unpacked; the npm tree from the committed lockfile with `npm ci // --ignore-scripts`; the tree sealed read-only; then the Node program // and the whole tree held to the committed digests (pi.Verify). It // prints the line for the runner's environment file. An install that // does not verify is removed. func installPi(ctx context.Context, args []string, output, errout io.Writer) int { if len(args) != 1 || !filepath.IsAbs(args[0]) || filepath.Clean(args[0]) != args[0] { fmt.Fprintln(errout, "usage: equipo-runner install-pi ") return 2 } root := args[0] if err := installPiAt(ctx, root, runtime.GOARCH, fetchTool, npmCIPi); err != nil { fmt.Fprintln(errout, "equipo-runner: the pinned Pi could not be installed:", err) return 1 } fmt.Fprintf(output, "EQUIPO_PI_ROOT=%q\n", root) return 0 } func npmCIPi(ctx context.Context, node, npmCLI, prefix, cache string) error { cmd := exec.CommandContext(ctx, node, npmCLI, "ci", "--ignore-scripts", "--no-audit", "--no-fund") cmd.Dir = prefix cmd.Env = []string{"PATH=" + filepath.Dir(node) + ":/usr/bin:/bin", "HOME=" + cache, "npm_config_cache=" + cache, "npm_config_update_notifier=false"} // The host's own way out to the registry, if it has one. for _, k := range []string{"HTTPS_PROXY", "HTTP_PROXY", "NO_PROXY", "https_proxy", "http_proxy", "no_proxy"} { if v := os.Getenv(k); v != "" { cmd.Env = append(cmd.Env, k+"="+v) } } cmd.Stderr = os.Stderr return cmd.Run() } 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 { if pi.Verify(root) == nil { return nil } _, archive, ok := pi.Pins(goarch) if !ok { return pi.ErrUnpinned } if err := os.MkdirAll(root, 0700); err != nil { return err } scratch, err := os.MkdirTemp(root, ".install-") if err != nil { return err } defer removeTree(scratch) tool := mac.Tool{Name: "node", Version: archive.Version, URL: archive.URL, SHA256: archive.SHA256, Root: archive.Root, Binary: "bin/node", Links: map[string]string{"bin/npm": "../lib/node_modules/npm/bin/npm-cli.js", "bin/npx": "../lib/node_modules/npm/bin/npx-cli.js"}, SkipLinks: []string{"bin/corepack"}} node, err := mac.Fetch(ctx, tool, filepath.Join(scratch, "tools"), fetch) if err != nil { return err } prefix := filepath.Join(scratch, "pi") if err := os.MkdirAll(prefix, 0700); err != nil { return err } pkg, lock := pi.Package() if os.WriteFile(filepath.Join(prefix, "package.json"), pkg, 0600) != nil || os.WriteFile(filepath.Join(prefix, "package-lock.json"), lock, 0600) != nil { return pi.ErrUnpinned } cache := filepath.Join(scratch, "npm") if err := os.MkdirAll(cache, 0700); err != nil { return err } if err := npmCI(ctx, node, filepath.Join(filepath.Dir(filepath.Dir(node)), "lib", "node_modules", "npm", "bin", "npm-cli.js"), prefix, cache); err != nil { return err } tree := filepath.Join(prefix, "node_modules") if err := pi.Seal(tree); err != nil { return err } // Into place: the old install (if any) goes first. nodeDst, treeDst := pi.Paths(root) for _, old := range []string{filepath.Dir(filepath.Dir(nodeDst)), filepath.Dir(treeDst)} { removeTree(old) } if err := os.MkdirAll(filepath.Dir(nodeDst), 0700); err != nil { return err } raw, err := os.ReadFile(node) if err != nil || os.WriteFile(nodeDst, raw, 0555) != nil { return pi.ErrUnpinned } if err := os.MkdirAll(filepath.Dir(treeDst), 0700); err != nil { return err } // A directory moves to another parent only while it is writable. if err := os.Chmod(tree, 0755); err != nil { return err } if err := os.Rename(tree, treeDst); err != nil { return err } if err := os.Chmod(treeDst, 0555); err != nil { return err } if err := pi.Verify(root); err != nil { removeTree(filepath.Dir(treeDst)) removeTree(filepath.Dir(filepath.Dir(nodeDst))) return err } return nil } // removeTree removes a tree a seal made read-only. func removeTree(dir string) { filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { if err == nil && info.IsDir() { os.Chmod(p, 0700) } return nil }) os.RemoveAll(dir) }