| ab61b9d | | | 1 | import { readdirSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from "fs"; |
| 966d71f | | | 2 | import { join } from "path"; |
| 966d71f | | | 3 | import { spawn } from "child_process"; |
| 966d71f | | | 4 | import type { FastifyBaseLogger } from "fastify"; |
| 966d71f | | | 5 | |
| 966d71f | | | 6 | const REPO_CONFIG_TOML = `storage_config = "default" |
| 966d71f | | | 7 | |
| 0d60b20 | | | 8 | [hook_manager_params] |
| 0d60b20 | | | 9 | disable_acl_checker = true |
| 0d60b20 | | | 10 | |
| 966d71f | | | 11 | [push] |
| 966d71f | | | 12 | pure_push_allowed = true |
| 966d71f | | | 13 | |
| 966d71f | | | 14 | [pushrebase] |
| 966d71f | | | 15 | rewritedates = false |
| 966d71f | | | 16 | |
| 966d71f | | | 17 | [source_control_service] |
| 966d71f | | | 18 | permit_writes = true |
| 966d71f | | | 19 | permit_service_writes = true |
| 6dbc795 | | | 20 | permit_commits_without_parents = true |
| 966d71f | | | 21 | |
| 6c9fcae | | | 22 | [git_configs.git_bundle_uri_config.uri_generator_type.local_fs] |
| 6c9fcae | | | 23 | |
| 0d60b20 | | | 24 | [infinitepush] |
| 0d60b20 | | | 25 | allow_writes = true |
| 0d60b20 | | | 26 | |
| 0d60b20 | | | 27 | [commit_cloud_config] |
| 0d60b20 | | | 28 | |
| 966d71f | | | 29 | [derived_data_config] |
| 966d71f | | | 30 | enabled_config_name = "default" |
| 966d71f | | | 31 | |
| 966d71f | | | 32 | [derived_data_config.available_configs.default] |
| 966d71f | | | 33 | types = [ |
| 966d71f | | | 34 | "blame", |
| 966d71f | | | 35 | "changeset_info", |
| 966d71f | | | 36 | "fastlog", |
| 966d71f | | | 37 | "filenodes", |
| 966d71f | | | 38 | "fsnodes", |
| 966d71f | | | 39 | "git_commits", |
| 966d71f | | | 40 | "git_delta_manifests_v2", |
| 6c9fcae | | | 41 | "unodes", |
| 966d71f | | | 42 | "hgchangesets", |
| 966d71f | | | 43 | "skeleton_manifests", |
| 1688ad1 | | | 44 | "skeleton_manifests_v2", |
| 6c9fcae | | | 45 | "ccsm", |
| 966d71f | | | 46 | ] |
| 0d60b20 | | | 47 | |
| 0d60b20 | | | 48 | [derived_data_config.available_configs.default.git_delta_manifest_v2_config] |
| 0d60b20 | | | 49 | max_inlined_object_size = 2000 |
| 0d60b20 | | | 50 | max_inlined_delta_size = 2000 |
| 0d60b20 | | | 51 | delta_chunk_size = 1000000 |
| 966d71f | | | 52 | `; |
| 966d71f | | | 53 | |
| b5baf6d | | | 54 | /** Flatten org/repo names to a single directory component (e.g. "letterpress-labs/site" → "letterpress-labs__site") */ |
| b5baf6d | | | 55 | function repoDirName(repoName: string): string { |
| b5baf6d | | | 56 | return repoName.replace(/\//g, "__"); |
| b5baf6d | | | 57 | } |
| b5baf6d | | | 58 | |
| 966d71f | | | 59 | export class MononokeProvisioner { |
| 966d71f | | | 60 | constructor( |
| 966d71f | | | 61 | private configPath: string, |
| 6c9fcae | | | 62 | private log?: FastifyBaseLogger, |
| 6c9fcae | | | 63 | private bridgeUrl?: string |
| 966d71f | | | 64 | ) {} |
| 966d71f | | | 65 | |
| 966d71f | | | 66 | /** |
| baf0847 | | | 67 | * Find the next available repo_id. |
| baf0847 | | | 68 | * Uses a monotonic counter file to avoid reusing IDs from deleted repos |
| baf0847 | | | 69 | * (whose blobstore data may still exist). |
| 966d71f | | | 70 | */ |
| 966d71f | | | 71 | getNextRepoId(): number { |
| 966d71f | | | 72 | const repoDefsDir = join(this.configPath, "repo_definitions"); |
| 966d71f | | | 73 | |
| baf0847 | | | 74 | // Scan existing configs to find the current max |
| 966d71f | | | 75 | let maxId = -1; |
| baf0847 | | | 76 | if (existsSync(repoDefsDir)) { |
| baf0847 | | | 77 | for (const dir of readdirSync(repoDefsDir)) { |
| baf0847 | | | 78 | const tomlPath = join(repoDefsDir, dir, "server.toml"); |
| baf0847 | | | 79 | if (!existsSync(tomlPath)) continue; |
| baf0847 | | | 80 | const content = readFileSync(tomlPath, "utf-8"); |
| baf0847 | | | 81 | const match = content.match(/repo_id\s*=\s*(\d+)/); |
| baf0847 | | | 82 | if (match) { |
| baf0847 | | | 83 | const id = parseInt(match[1]); |
| baf0847 | | | 84 | if (id > maxId) maxId = id; |
| baf0847 | | | 85 | } |
| 966d71f | | | 86 | } |
| 966d71f | | | 87 | } |
| baf0847 | | | 88 | |
| baf0847 | | | 89 | // Also check the high-water mark file (persists across repo deletions) |
| baf0847 | | | 90 | const hwmPath = join(this.configPath, ".max_repo_id"); |
| baf0847 | | | 91 | if (existsSync(hwmPath)) { |
| baf0847 | | | 92 | const hwm = parseInt(readFileSync(hwmPath, "utf-8").trim()); |
| baf0847 | | | 93 | if (!isNaN(hwm) && hwm > maxId) maxId = hwm; |
| baf0847 | | | 94 | } |
| baf0847 | | | 95 | |
| baf0847 | | | 96 | const nextId = maxId + 1; |
| baf0847 | | | 97 | |
| baf0847 | | | 98 | // Persist the new high-water mark |
| baf0847 | | | 99 | mkdirSync(this.configPath, { recursive: true }); |
| baf0847 | | | 100 | writeFileSync(hwmPath, String(nextId)); |
| baf0847 | | | 101 | |
| baf0847 | | | 102 | return nextId; |
| 966d71f | | | 103 | } |
| 966d71f | | | 104 | |
| 966d71f | | | 105 | /** |
| 966d71f | | | 106 | * Write TOML config files for a new Mononoke repo. |
| 966d71f | | | 107 | * Creates repo_definitions/<name>/server.toml and repos/<name>/server.toml. |
| 966d71f | | | 108 | * Returns the assigned repo_id. |
| 966d71f | | | 109 | */ |
| 966d71f | | | 110 | provisionRepo(repoName: string): number { |
| 966d71f | | | 111 | const repoId = this.getNextRepoId(); |
| b5baf6d | | | 112 | const dirName = repoDirName(repoName); |
| 966d71f | | | 113 | |
| b5baf6d | | | 114 | const repoDefDir = join(this.configPath, "repo_definitions", dirName); |
| b5baf6d | | | 115 | const repoCfgDir = join(this.configPath, "repos", dirName); |
| 966d71f | | | 116 | |
| 966d71f | | | 117 | mkdirSync(repoDefDir, { recursive: true }); |
| 966d71f | | | 118 | mkdirSync(repoCfgDir, { recursive: true }); |
| 966d71f | | | 119 | |
| 966d71f | | | 120 | writeFileSync( |
| 966d71f | | | 121 | join(repoDefDir, "server.toml"), |
| 966d71f | | | 122 | [ |
| 966d71f | | | 123 | `repo_id = ${repoId}`, |
| 966d71f | | | 124 | `repo_name = "${repoName}"`, |
| b5baf6d | | | 125 | `repo_config = "${dirName}"`, |
| 966d71f | | | 126 | `enabled = true`, |
| 6c9fcae | | | 127 | `hipster_acl = "default"`, |
| 966d71f | | | 128 | "", |
| 966d71f | | | 129 | ].join("\n") |
| 966d71f | | | 130 | ); |
| 966d71f | | | 131 | |
| 966d71f | | | 132 | writeFileSync(join(repoCfgDir, "server.toml"), REPO_CONFIG_TOML); |
| 966d71f | | | 133 | |
| 966d71f | | | 134 | this.log?.info({ repoName, repoId }, "Provisioned Mononoke repo config"); |
| 966d71f | | | 135 | return repoId; |
| 966d71f | | | 136 | } |
| 966d71f | | | 137 | |
| ab61b9d | | | 138 | /** |
| ab61b9d | | | 139 | * Remove Mononoke config for a repo so it won't be loaded on next restart. |
| ab61b9d | | | 140 | * Does not clean up blobstore data (would need Mononoke internals). |
| ab61b9d | | | 141 | */ |
| ab61b9d | | | 142 | deprovisionRepo(repoName: string): void { |
| b5baf6d | | | 143 | const dirName = repoDirName(repoName); |
| b5baf6d | | | 144 | const repoDefDir = join(this.configPath, "repo_definitions", dirName); |
| b5baf6d | | | 145 | const repoCfgDir = join(this.configPath, "repos", dirName); |
| ab61b9d | | | 146 | |
| ab61b9d | | | 147 | rmSync(repoDefDir, { recursive: true, force: true }); |
| ab61b9d | | | 148 | rmSync(repoCfgDir, { recursive: true, force: true }); |
| ab61b9d | | | 149 | |
| ab61b9d | | | 150 | this.log?.info({ repoName }, "Deprovisioned Mononoke repo config"); |
| ab61b9d | | | 151 | } |
| ab61b9d | | | 152 | |
| 966d71f | | | 153 | /** |
| 966d71f | | | 154 | * Restart Mononoke containers so they pick up new repo config. |
| 966d71f | | | 155 | * Uses Docker CLI (available via docker-cli in the container image). |
| 6c9fcae | | | 156 | * After restarting, waits for EdenAPI to become healthy. |
| 966d71f | | | 157 | */ |
| 966d71f | | | 158 | async restartMononoke(): Promise<void> { |
| 966d71f | | | 159 | const services = ["mononoke-slapi", "grove-bridge", "mononoke-git"]; |
| 6c9fcae | | | 160 | const errors: string[] = []; |
| 966d71f | | | 161 | |
| 966d71f | | | 162 | for (const name of services) { |
| 966d71f | | | 163 | try { |
| 966d71f | | | 164 | await this.restartContainer(name); |
| 966d71f | | | 165 | this.log?.info({ service: name }, "Restarted Mononoke container"); |
| 966d71f | | | 166 | } catch (err) { |
| 6c9fcae | | | 167 | const msg = err instanceof Error ? err.message : String(err); |
| 6c9fcae | | | 168 | errors.push(`${name}: ${msg}`); |
| 966d71f | | | 169 | this.log?.error({ err, service: name }, "Failed to restart container"); |
| 966d71f | | | 170 | } |
| 966d71f | | | 171 | } |
| 6c9fcae | | | 172 | |
| 6c9fcae | | | 173 | if (errors.length === services.length) { |
| 6c9fcae | | | 174 | throw new Error(`All Mononoke restarts failed: ${errors.join("; ")}`); |
| 6c9fcae | | | 175 | } |
| 6c9fcae | | | 176 | |
| 6c9fcae | | | 177 | // Wait for EdenAPI to become healthy |
| 6c9fcae | | | 178 | await this.waitForHealthy(); |
| 6c9fcae | | | 179 | } |
| 6c9fcae | | | 180 | |
| 6c9fcae | | | 181 | /** |
| 6c9fcae | | | 182 | * Poll the grove-bridge until it responds, or timeout after 30s. |
| 6c9fcae | | | 183 | * The bridge depends on Mononoke, so if it's healthy, Mononoke is too. |
| 6c9fcae | | | 184 | */ |
| 6c9fcae | | | 185 | private async waitForHealthy(): Promise<void> { |
| 6c9fcae | | | 186 | const bridgeUrl = this.bridgeUrl; |
| 6c9fcae | | | 187 | if (!bridgeUrl) { |
| 6c9fcae | | | 188 | this.log?.warn("No bridge URL configured, skipping health check"); |
| 6c9fcae | | | 189 | return; |
| 6c9fcae | | | 190 | } |
| 6c9fcae | | | 191 | |
| 6c9fcae | | | 192 | const maxAttempts = 15; |
| 6c9fcae | | | 193 | const delayMs = 2000; |
| 6c9fcae | | | 194 | |
| 6c9fcae | | | 195 | for (let i = 0; i < maxAttempts; i++) { |
| 6c9fcae | | | 196 | try { |
| 6c9fcae | | | 197 | const res = await fetch(`${bridgeUrl}/repos/grove/bookmarks`, { |
| 6c9fcae | | | 198 | signal: AbortSignal.timeout(3000), |
| 6c9fcae | | | 199 | }); |
| 6c9fcae | | | 200 | if (res.ok) { |
| 6c9fcae | | | 201 | this.log?.info("Mononoke is healthy after restart"); |
| 6c9fcae | | | 202 | return; |
| 6c9fcae | | | 203 | } |
| 6c9fcae | | | 204 | } catch { |
| 6c9fcae | | | 205 | // not ready yet |
| 6c9fcae | | | 206 | } |
| 6c9fcae | | | 207 | await new Promise((r) => setTimeout(r, delayMs)); |
| 6c9fcae | | | 208 | } |
| 6c9fcae | | | 209 | |
| 6c9fcae | | | 210 | this.log?.warn("Mononoke did not become healthy within 30s after restart"); |
| 966d71f | | | 211 | } |
| 966d71f | | | 212 | |
| 966d71f | | | 213 | private restartContainer(nameFilter: string): Promise<void> { |
| 966d71f | | | 214 | return new Promise((resolve, reject) => { |
| 966d71f | | | 215 | // Find container ID by name filter |
| 966d71f | | | 216 | const ps = spawn("docker", [ |
| 966d71f | | | 217 | "ps", |
| 966d71f | | | 218 | "--filter", |
| 966d71f | | | 219 | `name=${nameFilter}`, |
| 966d71f | | | 220 | "--format", |
| 966d71f | | | 221 | "{{.ID}}", |
| 966d71f | | | 222 | ]); |
| 966d71f | | | 223 | |
| 966d71f | | | 224 | let containerId = ""; |
| 966d71f | | | 225 | ps.stdout.on("data", (data: Buffer) => { |
| 966d71f | | | 226 | containerId += data.toString().trim(); |
| 966d71f | | | 227 | }); |
| 966d71f | | | 228 | |
| 966d71f | | | 229 | ps.on("close", (code) => { |
| 966d71f | | | 230 | if (!containerId) { |
| 6c9fcae | | | 231 | this.log?.warn({ service: nameFilter }, "Container not found, skipping restart"); |
| 6c9fcae | | | 232 | resolve(); |
| 966d71f | | | 233 | return; |
| 966d71f | | | 234 | } |
| 966d71f | | | 235 | // Take first ID if multiple lines |
| 966d71f | | | 236 | const id = containerId.split("\n")[0].trim(); |
| 966d71f | | | 237 | const restart = spawn("docker", ["restart", id]); |
| 966d71f | | | 238 | restart.on("close", (rc) => { |
| 966d71f | | | 239 | if (rc === 0) resolve(); |
| 966d71f | | | 240 | else reject(new Error(`docker restart ${nameFilter} exited with ${rc}`)); |
| 966d71f | | | 241 | }); |
| 966d71f | | | 242 | }); |
| 966d71f | | | 243 | }); |
| 966d71f | | | 244 | } |
| 966d71f | | | 245 | } |