import { createWriteStream } from "node:fs"; import { copyFile, cp, mkdir, open, readFile, readlink, readdir, rename, rm, symlink, unlink, writeFile, } from "node:fs/promises"; import path from "node:path"; import { spawn, spawnSync } from "node:child_process"; import { checkPackage } from "../scripts/deploy/check-package.mjs"; const root = process.env.DEPLOY_ROOT; if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute"); const source = path.resolve(process.argv[2] ?? "."); const environmentFile = path.join(root, "shared/.env.production"); process.loadEnvFile(environmentFile); const current = path.join(root, "current"); const postgresContainer = process.env.POSTGRES_CONTAINER; if (!postgresContainer || !/^[a-zA-Z0-9_.-]+$/.test(postgresContainer)) { throw new Error("POSTGRES_CONTAINER is required for the database backup"); } if (Number(process.versions.node.split(".")[0]) !== 24) throw new Error("Node.js 24 is required"); if ( process.env.NODE_ENV !== "production" || !process.env.DATABASE_URL || !process.env.SESSION_SECRET || process.env.SESSION_SECRET.length < 32 || !process.env.DEFAULT_BRAND_ID || !process.env.LOCAL_MEDIA_ROOT ) { throw new Error("Production environment is missing required values"); } if (!path.isAbsolute(process.env.LOCAL_MEDIA_ROOT)) throw new Error("LOCAL_MEDIA_ROOT must be absolute"); if (process.env.API_PREFIX) throw new Error("API_PREFIX must be empty when Traefik strips /api"); const databaseUrl = new URL(process.env.DATABASE_URL); const privateDatabaseHost = /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[0-1])\.)/.test( databaseUrl.hostname, ); if ( !["127.0.0.1", "localhost"].includes(databaseUrl.hostname) && !(privateDatabaseHost && process.env.ALLOW_PRIVATE_DATABASE_HOST === "true") ) { throw new Error( "DATABASE_URL must use loopback or explicitly approved private Docker networking", ); } const traefikDirectory = process.env.TRAEFIK_DYNAMIC_DIR ?? "/home/ubuntu/traefik/dynamic"; const traefikGateway = process.env.TRAEFIK_DOCKER_GATEWAY ?? "172.21.0.1"; const traefikHost = process.env.TRAEFIK_HOST ?? "wechat.app.que01.top"; if (!/^[a-zA-Z0-9.-]+$/.test(traefikHost) || !/^[a-zA-Z0-9.:-]+$/.test(traefikGateway)) { throw new Error("Invalid Traefik host or gateway setting"); } const routeFile = path.join(traefikDirectory, "personal-brand-managed.yml"); const adminServiceName = "personal-brand-admin-static.service"; const pm2ServiceName = "personal-brand-pm2.service"; const adminServiceDirectory = path.join( process.env.XDG_CONFIG_HOME ?? path.join(process.env.HOME ?? "/home/ubuntu", ".config"), "systemd/user", ); const adminServiceFile = path.join(adminServiceDirectory, adminServiceName); function run(command, args, cwd = root) { const result = spawnSync(command, args, { cwd, stdio: "inherit", env: process.env }); if (result.status !== 0) throw new Error(command + " failed with exit " + result.status); } function restartApi(release) { run("pm2", ["delete", "personal-brand-api"]); run("pm2", ["start", path.join(release, "deploy/ecosystem.config.cjs"), "--update-env"]); } async function installAdminService(release) { const stopped = spawnSync("systemctl", ["--user", "stop", adminServiceName], { stdio: "ignore", env: process.env, }); if (stopped.status !== 0 && stopped.status !== 5) { throw new Error("Could not stop the previous Admin systemd unit"); } await mkdir(adminServiceDirectory, { recursive: true }); await writeFile( adminServiceFile, `[Unit]\nDescription=Personal Brand Admin static server\nAfter=network-online.target\n\n[Service]\nType=simple\nWorkingDirectory=${release}\nEnvironment=NODE_ENV=production\nEnvironment=ADMIN_STATIC_PORT=${process.env.ADMIN_STATIC_PORT ?? "3010"}\nEnvironment=ADMIN_STATIC_ROOT=${release}/apps/admin/dist\nExecStart=${process.execPath} ${release}/deploy/static-server.mjs\nRestart=always\nRestartSec=2\n\n[Install]\nWantedBy=default.target\n`, { mode: 0o644 }, ); run("systemctl", ["--user", "daemon-reload"]); run("systemctl", ["--user", "enable", "--now", adminServiceName]); run("systemctl", ["--user", "restart", adminServiceName]); } async function installPm2Service() { const directory = path.dirname(adminServiceFile); const file = path.join(directory, pm2ServiceName); await mkdir(directory, { recursive: true }); await writeFile( file, `[Unit]\nDescription=Personal Brand API PM2 processes\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=oneshot\nRemainAfterExit=yes\nEnvironment=HOME=${process.env.HOME ?? "/home/ubuntu"}\nEnvironment=PATH=${process.env.PATH}\nExecStart=${process.env.HOME ?? "/home/ubuntu"}/.nvm/versions/node/v${process.versions.node}/bin/pm2 resurrect\nExecStop=${process.env.HOME ?? "/home/ubuntu"}/.nvm/versions/node/v${process.versions.node}/bin/pm2 save\n\n[Install]\nWantedBy=default.target\n`, { mode: 0o644 }, ); run("systemctl", ["--user", "daemon-reload"]); run("systemctl", ["--user", "enable", pm2ServiceName]); } async function waitForAdminHealth() { const url = "http://127.0.0.1:" + (process.env.ADMIN_STATIC_PORT ?? "3010") + "/admin/"; for (let attempt = 0; attempt < 20; attempt++) { try { const response = await fetch(url, { signal: AbortSignal.timeout(2000) }); if (response.ok) return; } catch {} await new Promise((resolve) => setTimeout(resolve, 500)); } throw new Error("Admin static health check failed"); } function verifyTraefikAdmin() { const result = spawnSync( "docker", [ "exec", "traefik", "wget", "-qO", "/dev/null", "-T", "5", `http://${traefikGateway}:${process.env.ADMIN_STATIC_PORT ?? "3010"}/admin/`, ], { stdio: "inherit", env: process.env }, ); if (result.status !== 0) throw new Error("Traefik cannot reach Admin static service"); } async function switchCurrent(target) { const temporary = current + ".next-" + process.pid; await symlink(target, temporary); await rename(temporary, current); } async function backupDatabase(destination) { const temporary = destination + ".tmp"; const child = spawn( "docker", [ "exec", postgresContainer, "sh", "-c", 'exec pg_dump -Fc -U "$POSTGRES_USER" -d "$POSTGRES_DB"', ], { stdio: ["ignore", "pipe", "inherit"] }, ); const output = createWriteStream(temporary, { flags: "wx", mode: 0o600 }); child.stdout.pipe(output); try { await Promise.all([ new Promise((resolve, reject) => { child.on("error", reject); child.on("exit", (code) => code === 0 ? resolve() : reject(new Error("Database backup failed")), ); }), new Promise((resolve, reject) => { output.on("finish", resolve); output.on("error", reject); }), ]); await rename(temporary, destination); } catch (error) { await rm(temporary, { force: true }); throw error; } } async function pruneBackups(directory) { const backups = (await readdir(directory, { withFileTypes: true })) .filter((entry) => entry.isFile() && entry.name.endsWith(".dump")) .map((entry) => entry.name) .sort() .reverse(); for (const backup of backups.slice(7)) await rm(path.join(directory, backup)); for (const entry of await readdir(directory)) { if (entry.endsWith(".dump.tmp")) await rm(path.join(directory, entry), { force: true }); } } async function pruneReleases(directory, currentRelease, previousRelease) { const releases = (await readdir(directory, { withFileTypes: true })) .filter((entry) => entry.isDirectory()) .map((entry) => entry.name) .sort() .reverse(); const keep = new Set([ path.basename(currentRelease), path.basename(previousRelease), ...releases.slice(0, 5), ]); for (const release of releases) { if (!keep.has(release)) await rm(path.join(directory, release), { recursive: true, force: true }); } } async function copyMissingFiles(sourceDirectory, destinationDirectory) { await mkdir(destinationDirectory, { recursive: true }); for (const entry of await readdir(sourceDirectory, { withFileTypes: true })) { if (!entry.isFile()) throw new Error("Unexpected seed media entry: " + entry.name); try { await copyFile( path.join(sourceDirectory, entry.name), path.join(destinationDirectory, entry.name), 1, ); } catch (error) { if (error.code !== "EEXIST") throw error; } } } async function writeTraefikRoute() { await mkdir(traefikDirectory, { recursive: true }); const existing = await readFile(routeFile, "utf8").catch((error) => { if (error.code === "ENOENT") return undefined; throw error; }); if (existing && !existing.includes("managed-by-personal-brand-deploy")) { throw new Error("Refusing to overwrite an unmanaged Traefik route file"); } const hostRule = "Host(`" + traefikHost + "`)"; const configuration = [ "# managed-by-personal-brand-deploy", "http:", " routers:", " personal-brand-managed-admin:", " rule: " + JSON.stringify(hostRule + " && !PathPrefix(`/api`) && !PathPrefix(`/media`)"), " entryPoints:", " - websecure", " service: personal-brand-managed-admin", " priority: 1000", " tls:", " certResolver: le", " personal-brand-managed-api:", " rule: " + JSON.stringify(hostRule + " && PathPrefix(`/api`)"), " entryPoints:", " - websecure", " middlewares:", " - personal-brand-managed-strip-api", " service: personal-brand-managed-api", " priority: 1000", " tls:", " certResolver: le", " personal-brand-managed-media:", " rule: " + JSON.stringify(hostRule + " && PathPrefix(`/media`)"), " entryPoints:", " - websecure", " service: personal-brand-managed-api", " priority: 1000", " tls:", " certResolver: le", " middlewares:", " personal-brand-managed-strip-api:", " stripPrefix:", " prefixes:", " - /api", " services:", " personal-brand-managed-admin:", " loadBalancer:", " servers:", " - url: http://" + traefikGateway + ":3010", " personal-brand-managed-api:", " loadBalancer:", " servers:", " - url: http://" + traefikGateway + ":" + (process.env.API_PORT ?? "3001"), "", ].join("\n"); const temporary = routeFile + ".tmp-" + process.pid; await writeFile(temporary, configuration, { mode: 0o644 }); await rename(temporary, routeFile); } async function waitForPublicRelease(release) { const verifier = path.join(release, "scripts/deploy/verify-public-release.mjs"); for (let attempt = 0; attempt < 4; attempt++) { const result = spawnSync(process.execPath, [verifier, release], { cwd: release, encoding: "utf8", env: process.env, timeout: 45000, }); if (result.status === 0) { process.stdout.write(result.stdout); return; } if (attempt === 3) { process.stderr.write(result.stderr ?? ""); process.stdout.write(result.stdout ?? ""); throw new Error("Public release verification failed"); } await new Promise((resolve) => setTimeout(resolve, 3000)); } } async function waitForHealth() { const url = "http://127.0.0.1:" + (process.env.API_PORT ?? "3001") + "/health"; for (let attempt = 0; attempt < 20; attempt++) { try { const response = await fetch(url, { signal: AbortSignal.timeout(2000) }); const body = await response.json(); if (response.ok && body.status === "ok") return; } catch {} await new Promise((resolve) => setTimeout(resolve, 1000)); } throw new Error("API health check failed"); } await mkdir(path.join(root, "shared/backups"), { recursive: true }); await mkdir(path.join(root, "releases"), { recursive: true }); const lockPath = path.join(root, "shared/deploy.lock"); const lock = await open(lockPath, "wx", 0o600); await lock.writeFile(String(process.pid)); let previous; let switched = false; let routeUpdated = false; let previousRoute; try { const manifest = await checkPackage(source); if (manifest.nodeMajor !== 24 || manifest.packageManager !== "pnpm@9.15.0") { throw new Error("Unsupported release toolchain"); } const pnpmVersion = spawnSync("pnpm", ["--version"], { encoding: "utf8" }); if (pnpmVersion.status !== 0 || pnpmVersion.stdout.trim() !== "9.15.0") { throw new Error("pnpm 9.15.0 is required"); } previous = await readlink(current).catch((error) => { if (error.code === "ENOENT") { throw new Error( "Initialize DEPLOY_ROOT/current with the working deployment before the first managed release", ); } throw error; }); if (!previous) throw new Error("DEPLOY_ROOT/current must point to the working deployment"); previousRoute = await readFile(routeFile).catch((error) => { if (error.code === "ENOENT") return undefined; throw error; }); const release = path.join(root, "releases", manifest.version); await mkdir(release, { recursive: false }); await cp(source, release, { recursive: true }); run( "env", [ "NODE_ENV=development", "pnpm", "--filter", "@personal-brand/api", "--filter", "@personal-brand/taro", "--filter", "@personal-brand/template-acheng-campus-food", "--filter", "@personal-brand/template-linjing-editorial", "--filter", "@personal-brand/template-atelier-solopreneur", "install", "--frozen-lockfile", "--prefer-offline", "--ignore-scripts", ], release, ); run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "generate"], release); await backupDatabase(path.join(root, "shared/backups", manifest.version + ".dump")); await pruneBackups(path.join(root, "shared/backups")); run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "migrate", "deploy"], release); const seedDirectory = path.join(process.env.LOCAL_MEDIA_ROOT, "seed"); await copyMissingFiles(path.join(release, "seed-media"), seedDirectory); await switchCurrent(release); switched = true; restartApi(release); await installPm2Service(); await installAdminService(release); await waitForHealth(); await waitForAdminHealth(); verifyTraefikAdmin(); await writeTraefikRoute(); routeUpdated = true; if (!process.env.PUBLIC_BASE_URL) throw new Error("PUBLIC_BASE_URL is required for cutover verification"); await waitForPublicRelease(release); await writeFile(path.join(root, "shared/previous-release"), previous + "\n"); run("pm2", ["save"]); await pruneReleases(path.join(root, "releases"), release, previous); console.log("release installed: " + manifest.version); } catch (error) { if (routeUpdated) { if (previousRoute) await writeFile(routeFile, previousRoute); else await rm(routeFile, { force: true }); } if (switched) { await switchCurrent(previous); await installAdminService(previous); restartApi(path.resolve(root, previous)); await waitForHealth(); await waitForAdminHealth(); console.error( "application and route rollback completed; database migrations were not reversed", ); } throw error; } finally { await lock.close(); await unlink(lockPath); }