fix: 修复 runner 迁移后的部署状态检查
This commit is contained in:
1 parent
ca6d1712bd
commit
12a3bf2f3b
6 files changed
+102
-38
No files matched your search
+65
-36
@@ -1,9 +1,10 @@
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
||||
const target = process.env.DEPLOY_TARGET ?? "oracle_arm";
|
||||
const deployRoot = process.env.DEPLOY_ROOT ?? "/home/ubuntu/persional-brand";
|
||||
const publicBaseUrl = (process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top").replace(
|
||||
/\/$/,
|
||||
"",
|
||||
@@ -21,41 +22,69 @@ function shellQuote(value) {
|
||||
return "'" + value.replace(/'/g, "'\\''") + "'";
|
||||
}
|
||||
|
||||
const remoteScript = [
|
||||
"set -euo pipefail",
|
||||
'source "$HOME/.nvm/nvm.sh"',
|
||||
"export DEPLOY_ROOT=/home/ubuntu/persional-brand",
|
||||
'printf "current=%s\\n" "$(readlink /home/ubuntu/persional-brand/current)"',
|
||||
'printf "api="; test "$(pm2 pid personal-brand-api)" != "0"; echo online',
|
||||
'printf "pm2-autostart="; systemctl --user is-enabled personal-brand-pm2.service',
|
||||
'printf "admin="; systemctl --user is-active personal-brand-admin-static.service',
|
||||
'printf "database="; docker inspect -f "{{.State.Status}}" personal-brand-postgres-1',
|
||||
'printf "ci-runner="; systemctl --user is-active gitea-runner.service',
|
||||
'printf "backups="; find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -name "*.dump" | wc -l',
|
||||
'printf "latest-backup="; newest=$(find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -type f -name "*.dump" -printf "%T@ %p\\n" | sort -nr | head -1 | cut -d" " -f2-); test -n "$newest"; stat -c "%y %s bytes" "$newest"; docker exec -i personal-brand-postgres-1 pg_restore -l < "$newest" >/dev/null; echo readable',
|
||||
'printf "backup-temp-files="; find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -type f -name "*.tmp" | wc -l',
|
||||
'printf "disk="; df -h / | awk \'NR==2 {print $5 " used, " $4 " free"}\'',
|
||||
'printf "release-count="; find /home/ubuntu/persional-brand/releases -mindepth 1 -maxdepth 1 -type d | wc -l',
|
||||
].join("; ");
|
||||
/**
|
||||
* 生产状态检查只覆盖 VPS 上的服务和公网路由。
|
||||
* CI runner 已迁到本地工作站,不能再把远端 gitea-runner.service 当成生产健康条件。
|
||||
*/
|
||||
export function buildRemoteStatusScript({ deployRoot: rootPath = deployRoot } = {}) {
|
||||
if (!/^\/[a-zA-Z0-9_./-]+$/.test(rootPath)) throw new Error("Invalid deployment root");
|
||||
|
||||
run("ssh", [target, "bash -lc " + shellQuote(remoteScript)]);
|
||||
return [
|
||||
"set -euo pipefail",
|
||||
'source "$HOME/.nvm/nvm.sh"',
|
||||
`export DEPLOY_ROOT=${shellQuote(rootPath)}`,
|
||||
'printf "current=%s\\n" "$(readlink "$DEPLOY_ROOT/current")"',
|
||||
'printf "api="; test "$(pm2 pid personal-brand-api)" != "0"; echo online',
|
||||
'printf "pm2-autostart="; systemctl --user is-enabled personal-brand-pm2.service',
|
||||
'printf "admin="; systemctl --user is-active personal-brand-admin-static.service',
|
||||
'printf "database="; docker inspect -f "{{.State.Status}}" personal-brand-postgres-1',
|
||||
'printf "backups="; find "$DEPLOY_ROOT/shared/backups" -maxdepth 1 -name "*.dump" | wc -l',
|
||||
'printf "latest-backup="; newest=$(find "$DEPLOY_ROOT/shared/backups" -maxdepth 1 -type f -name "*.dump" -printf "%T@ %p\\n" | sort -nr | head -1 | cut -d" " -f2-); test -n "$newest"; stat -c "%y %s bytes" "$newest"; docker exec -i personal-brand-postgres-1 pg_restore -l < "$newest" >/dev/null; echo readable',
|
||||
'printf "backup-temp-files="; find "$DEPLOY_ROOT/shared/backups" -maxdepth 1 -type f -name "*.tmp" | wc -l',
|
||||
'printf "disk="; df -h / | awk \'NR==2 {print $5 " used, " $4 " free"}\'',
|
||||
'printf "release-count="; find "$DEPLOY_ROOT/releases" -mindepth 1 -maxdepth 1 -type d | wc -l',
|
||||
].join("; ");
|
||||
}
|
||||
|
||||
for (const route of [
|
||||
"/admin/",
|
||||
"/template-preview/",
|
||||
"/api/health",
|
||||
"/media/seed/food-package-02.jpg",
|
||||
]) {
|
||||
run("curl", [
|
||||
"--fail",
|
||||
"--silent",
|
||||
"--show-error",
|
||||
"--max-time",
|
||||
"10",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
"-w",
|
||||
route + " %{http_code}\\n",
|
||||
publicBaseUrl + route,
|
||||
]);
|
||||
/**
|
||||
* CI runner 与生产状态是两件事。这里只做本地提示,runner 不活跃不应阻断部署状态检查。
|
||||
*/
|
||||
function printLocalRunnerStatus() {
|
||||
const result = spawnSync("systemctl", ["--user", "is-active", "gitea-runner.service"], {
|
||||
encoding: "utf8",
|
||||
});
|
||||
const state = (result.stdout || "").trim();
|
||||
process.stdout.write(`ci-runner-local=${state || "unknown"}\n`);
|
||||
}
|
||||
|
||||
function main() {
|
||||
if (!/^[a-zA-Z0-9_.@:-]+$/.test(target)) throw new Error("Invalid deployment SSH target");
|
||||
if (!/^\/[a-zA-Z0-9_./-]+$/.test(deployRoot)) throw new Error("Invalid deployment root");
|
||||
|
||||
run("ssh", [target, "bash -lc " + shellQuote(buildRemoteStatusScript())]);
|
||||
printLocalRunnerStatus();
|
||||
|
||||
for (const route of [
|
||||
"/admin/",
|
||||
"/template-preview/",
|
||||
"/api/health",
|
||||
"/media/seed/food-package-02.jpg",
|
||||
]) {
|
||||
run("curl", [
|
||||
"--fail",
|
||||
"--silent",
|
||||
"--show-error",
|
||||
"--max-time",
|
||||
"10",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
"-w",
|
||||
route + " %{http_code}\\n",
|
||||
publicBaseUrl + route,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
main();
|
||||
}
|
||||
Reference in new issue
Block a user