ai-chatbot/scripts/security/auto-enable-perf-guard.mjs
Your Name 4915160d5e
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
Auto-enable perf-guard during install and build on Linux root
2026-02-14 02:38:54 +08:00

64 lines
1.5 KiB
JavaScript

#!/usr/bin/env node
import { execSync } from "node:child_process";
import { existsSync } from "node:fs";
import { platform } from "node:os";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
function log(message) {
console.log(`[security-auto] ${message}`);
}
function commandExists(command) {
try {
execSync(`command -v ${command}`, { stdio: "ignore" });
return true;
} catch {
return false;
}
}
function run() {
if (platform() !== "linux") {
log("skip: only runs on Linux");
return;
}
if (process.env.DISABLE_PERF_GUARD_AUTO_INSTALL === "1") {
log("skip: DISABLE_PERF_GUARD_AUTO_INSTALL=1");
return;
}
if (typeof process.getuid !== "function" || process.getuid() !== 0) {
log("skip: requires root user");
return;
}
if (!commandExists("systemctl")) {
log("skip: systemctl not found");
return;
}
const currentFilePath = fileURLToPath(import.meta.url);
const securityDir = dirname(currentFilePath);
const installerPath = resolve(securityDir, "install-perf-guard.sh");
if (!existsSync(installerPath)) {
log(`skip: installer not found (${installerPath})`);
return;
}
try {
log("ensuring perf-guard is installed and enabled");
execSync(`bash "${installerPath}"`, { stdio: "inherit" });
log("perf-guard ready");
} catch (error) {
console.warn(
`[security-auto] warn: install-perf-guard failed, deployment continues. ${error instanceof Error ? error.message : String(error)}`
);
}
}
run();