299 lines
10 KiB
JavaScript
299 lines
10 KiB
JavaScript
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);
|
|
if (!["127.0.0.1", "localhost"].includes(databaseUrl.hostname)) {
|
|
throw new Error("DATABASE_URL must use the loopback-published PostgreSQL port");
|
|
}
|
|
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");
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
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);
|
|
}
|
|
|
|
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 < 12; attempt++) {
|
|
const result = spawnSync(process.execPath, [verifier, release], {
|
|
cwd: release,
|
|
encoding: "utf8",
|
|
env: process.env,
|
|
timeout: 30000,
|
|
});
|
|
if (result.status === 0) {
|
|
process.stdout.write(result.stdout);
|
|
return;
|
|
}
|
|
if (attempt === 11) {
|
|
process.stderr.write(result.stderr ?? "");
|
|
throw new Error("Public release verification failed");
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
}
|
|
}
|
|
|
|
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(
|
|
"pnpm",
|
|
[
|
|
"--filter",
|
|
"@personal-brand/api...",
|
|
"install",
|
|
"--prod",
|
|
"--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"));
|
|
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;
|
|
run("pm2", ["startOrRestart", path.join(release, "deploy/ecosystem.config.cjs"), "--update-env"]);
|
|
await waitForHealth();
|
|
const staticResponse = await fetch(
|
|
"http://127.0.0.1:" + (process.env.ADMIN_STATIC_PORT ?? "3010") + "/admin/",
|
|
{ signal: AbortSignal.timeout(5000) },
|
|
);
|
|
if (!staticResponse.ok) throw new Error("Admin static health check failed");
|
|
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"]);
|
|
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);
|
|
run("pm2", [
|
|
"startOrRestart",
|
|
path.join(source, "deploy/ecosystem.config.cjs"),
|
|
"--update-env",
|
|
]);
|
|
await waitForHealth();
|
|
console.error(
|
|
"application and route rollback completed; database migrations were not reversed",
|
|
);
|
|
}
|
|
throw error;
|
|
} finally {
|
|
await lock.close();
|
|
await unlink(lockPath);
|
|
}
|