import path from "node:path"; import { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const target = process.env.DEPLOY_TARGET ?? "oracle_arm"; const publicBaseUrl = (process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top").replace( /\/$/, "", ); function run(command, args) { const result = spawnSync(command, args, { cwd: root, encoding: "utf8" }); if (result.status !== 0) throw new Error(result.stderr || command + " failed"); process.stdout.write(result.stdout); } run("ssh", [ target, "bash", "-lc", [ "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 "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("; "), ]); 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, ]); }