| 1 | #!/usr/bin/env python3 |
| 2 | """Stage project tools on Snow Globe; activation is a separate atomic switch.""" |
| 3 | |
| 4 | import argparse |
| 5 | import hashlib |
| 6 | import io |
| 7 | import json |
| 8 | import os |
| 9 | from pathlib import Path |
| 10 | import shutil |
| 11 | import socket |
| 12 | import subprocess |
| 13 | import tarfile |
| 14 | import tempfile |
| 15 | import urllib.request |
| 16 | |
| 17 | |
| 18 | HERE = Path(__file__).resolve().parent |
| 19 | BASE = Path("/opt/studio-project-store") |
| 20 | PROJECTS = "/srv/mac-studio-vm/sandbox/project" |
| 21 | GUEST_PROJECTS = "/Volumes/Code/project" |
| 22 | |
| 23 | |
| 24 | def download(url, digest): |
| 25 | with urllib.request.urlopen(url, timeout=60) as response: |
| 26 | data = response.read() |
| 27 | if hashlib.sha256(data).hexdigest() != digest: |
| 28 | raise ValueError("Downloaded tool failed checksum verification.") |
| 29 | return tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") |
| 30 | |
| 31 | |
| 32 | def stage(name): |
| 33 | destination = BASE / "releases" / name |
| 34 | if destination.exists(): |
| 35 | raise ValueError("Release already exists. Choose a new name.") |
| 36 | current = (BASE / "current").resolve(strict=True) |
| 37 | binaries = {} |
| 38 | for name, pattern, executable in (("python", "project-store-*-python3-*", "python3"), |
| 39 | ("node", "project-store-*-nodejs-*", "node"), |
| 40 | ("bwrap", "project-store-*-bubblewrap-*", "bwrap"), |
| 41 | ("coreutils", "project-store-coreutils", "true")): |
| 42 | roots = list(Path("/nix/var/nix/gcroots").glob(pattern)) |
| 43 | if len(roots) != 1: |
| 44 | raise ValueError(f"Expected one pinned {name} runtime, found {len(roots)}.") |
| 45 | binaries[name] = str(roots[0].resolve(strict=True) / "bin" / executable) |
| 46 | with tempfile.TemporaryDirectory(prefix=".prepare-", dir=destination.parent) as temporary: |
| 47 | release = Path(temporary) |
| 48 | shutil.copytree(HERE / "project-tools", release / "project-tools", |
| 49 | ignore=shutil.ignore_patterns("__pycache__", "*.pyc")) |
| 50 | for filename in ("project-store.py", "project-store-test.py"): |
| 51 | shutil.copy2(HERE / filename, release / filename) |
| 52 | for filename in ("restore.cjs", "restore-test.cjs"): |
| 53 | shutil.copy2(HERE / "project-pnpm" / filename, release / filename) |
| 54 | profiles = json.loads((current / "profiles.json").read_text()) |
| 55 | for profile in profiles.values(): |
| 56 | profile.pop("workspace_config", None) |
| 57 | (release / "profiles.json").write_text(json.dumps(profiles)) |
| 58 | shutil.copy2(current / "jj.toml", release / "jj.toml") |
| 59 | (release / "bin").mkdir() |
| 60 | with download("https://github.com/jj-vcs/jj/releases/download/v0.42.0/" |
| 61 | "jj-v0.42.0-x86_64-unknown-linux-musl.tar.gz", |
| 62 | "2d91e81d649e617a81608e7401ad1106029c15ece01ac928c4a351abef42be6a") as archive: |
| 63 | member, = [m for m in archive if m.isfile() and Path(m.name).name == "jj"] |
| 64 | (release / "bin/jj-real").write_bytes(archive.extractfile(member).read()) |
| 65 | (release / "bin/jj-real").chmod(0o755) |
| 66 | (release / "bin/jj").symlink_to("../project-tools/jj") |
| 67 | with download("https://github.com/BurntSushi/ripgrep/releases/download/15.2.0/" |
| 68 | "ripgrep-15.2.0-x86_64-unknown-linux-musl.tar.gz", |
| 69 | "33e15bcf1624b25cdd2a55813a47a2f95dbe126268203e76aa6a585d1e7b149c") as archive: |
| 70 | member, = [m for m in archive if m.isfile() and Path(m.name).name == "rg"] |
| 71 | (release / "bin/rg").write_bytes(archive.extractfile(member).read()) |
| 72 | (release / "bin/rg").chmod(0o755) |
| 73 | for name in ("jj", "pnpm", "project"): |
| 74 | path = release / "project-tools" / name |
| 75 | content = path.read_text().split("\n", 1)[1] |
| 76 | path.write_text("#!" + binaries["python"] + "\n" + content) |
| 77 | path.chmod(0o755) |
| 78 | for version, digest, directory, original, patched in ( |
| 79 | ("10.32.1", "9b943b94bc8f55efb993aad8e44b538e6b091e60a9e4a944dcde869855f233e3", |
| 80 | "pnpm10", "pnpm.cjs", "pnpm-darwin-rebuild.cjs"), |
| 81 | ("11.10.0", "620b6605ea4f62fc56a6d0a98733f479071d0321e03e486b522c7e9a74617431", |
| 82 | "pnpm11", "pnpm.mjs", "pnpm-target-cache.mjs"), |
| 83 | ): |
| 84 | with download(f"https://registry.npmjs.org/pnpm/-/pnpm-{version}.tgz", digest) as archive: |
| 85 | archive.extractall(release / directory, filter="data") |
| 86 | package = release / directory / "package" |
| 87 | for path in (package / "dist").iterdir(): |
| 88 | path.rename(release / directory / path.name) |
| 89 | shutil.rmtree(package) |
| 90 | shutil.copy2(release / directory / original, release / directory / patched) |
| 91 | subprocess.run(["patch", "--batch", str(release / directory / patched), |
| 92 | str(HERE / "project-pnpm" / f"pnpm-{version}.patch")], check=True) |
| 93 | (release / "pnpm12").mkdir() |
| 94 | with download("https://github.com/pnpm/pnpm/releases/download/v12.14.0/pnpm-linux-x64-musl.tar.gz", |
| 95 | "d7115e2f2791d79bf9e7e448609537595bb7787de1ef5292ce91ff3d1be2629e") as archive: |
| 96 | member, = [m for m in archive if m.isfile() and Path(m.name).name == "pnpm"] |
| 97 | (release / "pnpm12/pnpm").write_bytes(archive.extractfile(member).read()) |
| 98 | (release / "pnpm12/pnpm").chmod(0o755) |
| 99 | command = [binaries["bwrap"], "--die-with-parent", "--unshare-all", "--cap-drop", "ALL", |
| 100 | "--ro-bind", "/nix/store", "/nix/store", "--proc", "/proc", "--dev", "/dev", |
| 101 | "--tmpfs", "/tmp", "--dir", "/home/clo", |
| 102 | "--ro-bind", str(destination), "/opt/project-store", |
| 103 | "--bind", PROJECTS, GUEST_PROJECTS, "--bind", PROJECTS, PROJECTS] |
| 104 | stores = "/srv/mac-studio-vm/sandbox/cache/project-pnpm" |
| 105 | for target in (stores, "/Volumes/Code/cache/project-pnpm", "/Users/admin/Library/Caches/project"): |
| 106 | command.extend(("--bind", stores, target)) |
| 107 | command.extend(("--bind", "/srv/mac-studio-vm/sandbox/cache/pnpm12", "/Volumes/Code/cache/pnpm12")) |
| 108 | command.extend(("--bind", "/home/clo/.cache/pnpm", "/home/clo/.cache/pnpm", |
| 109 | "--clearenv", "--setenv", "HOME", "/home/clo", "--setenv", "PATH", |
| 110 | "/opt/project-store/bin:" + ":".join(str(Path(binaries[key]).parent) |
| 111 | for key in ("node", "python", "coreutils")), |
| 112 | "--setenv", "JJ_CONFIG", "/opt/project-store/jj.toml", |
| 113 | "--setenv", "PROJECT_STORE_SSH", "", "--setenv", "LC_ALL", "C.UTF-8", |
| 114 | "--chdir", GUEST_PROJECTS, binaries["python"], |
| 115 | "/opt/project-store/project-store.py", GUEST_PROJECTS)) |
| 116 | network = ["--share-net", "--ro-bind", "/etc/resolv.conf", "/etc/resolv.conf", |
| 117 | "--ro-bind", "/etc/ssl/certs/ca-bundle.crt", "/etc/ssl/certs/ca-bundle.crt", |
| 118 | "--setenv", "SSL_CERT_FILE", "/etc/ssl/certs/ca-bundle.crt", |
| 119 | "--setenv", "NODE_EXTRA_CA_CERTS", "/etc/ssl/certs/ca-bundle.crt"] |
| 120 | (release / "ssh-command").write_text( |
| 121 | "#!" + binaries["python"] + "\nimport json, subprocess, sys\n" |
| 122 | "request = sys.stdin.buffer.readline(4 * 1024 * 1024 + 1)\n" |
| 123 | "if len(request) > 4 * 1024 * 1024: sys.exit('Project request exceeds 4 MiB.')\n" |
| 124 | "command = " + repr(command) + "\n" |
| 125 | "if json.loads(request).get('command') == 'read':\n" |
| 126 | " command = ['--ro-bind' if arg == '--bind' else arg for arg in command]\n" |
| 127 | "if json.loads(request).get('command') == 'pnpm':\n" |
| 128 | " command[command.index('--chdir'):command.index('--chdir')] = " + repr(network) + "\n" |
| 129 | "result = subprocess.run(command, input=request)\n" |
| 130 | "sys.exit(result.returncode if result.returncode >= 0 else 128 - result.returncode)\n") |
| 131 | (release / "ssh-command").chmod(0o755) |
| 132 | release.chmod(0o755) |
| 133 | release.rename(destination) |
| 134 | release.mkdir() |
| 135 | print(destination) |
| 136 | |
| 137 | |
| 138 | def main(): |
| 139 | parser = argparse.ArgumentParser(description=__doc__) |
| 140 | parser.add_argument("action", choices=("stage", "activate")) |
| 141 | parser.add_argument("release") |
| 142 | args = parser.parse_args() |
| 143 | if socket.gethostname() != "snowglobe" or os.geteuid() != 0: |
| 144 | parser.error("Run this installer as root on Snow Globe.") |
| 145 | if not args.release or any(c not in "abcdefghijklmnopqrstuvwxyz0123456789-." for c in args.release): |
| 146 | parser.error("Use a release name containing lowercase letters, numbers, dots, or hyphens.") |
| 147 | if args.action == "stage": |
| 148 | os.umask(0o022) |
| 149 | stage(args.release) |
| 150 | else: |
| 151 | release = BASE / "releases" / args.release |
| 152 | if not (release / "ssh-command").is_file(): |
| 153 | parser.error("Stage this release before activating it.") |
| 154 | link = BASE / (".current-" + args.release) |
| 155 | link.symlink_to(Path("releases") / args.release) |
| 156 | link.replace(BASE / "current") |
| 157 | print(release) |
| 158 | |
| 159 | |
| 160 | if __name__ == "__main__": |
| 161 | main() |