fix: edit openclaw config.yaml directly and restart as sts user

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-24 14:07:22 +08:00
parent 1a63e90055
commit 029f7fe0a9

View File

@@ -398,31 +398,51 @@ class ClawClient {
// ── OpenClaw 配置 ──────────────────────────────────────────────────────────── // ── OpenClaw 配置 ────────────────────────────────────────────────────────────
_configureOpenClaw(clawId) { _configureOpenClaw(clawId) {
// 查找 openclaw 二进制(可能在用户目录下,不在 root PATH 中) const { existsSync, readFileSync, writeFileSync } = require('fs');
const candidates = [
'/home/sts/.npm-global/bin/openclaw', // 1. 直接修改 openclaw 配置文件(避免用 root 执行 openclaw CLI 超时问题)
'/usr/local/bin/openclaw', const configFile = '/home/sts/.openclaw/config/config.yaml';
'/usr/bin/openclaw', if (!existsSync(configFile)) {
]; log.warn('clawd', `openclaw 配置文件不存在: ${configFile}`);
const { existsSync } = require('fs');
const bin = candidates.find(p => existsSync(p));
if (!bin) {
log.warn('clawd', 'openclaw 未找到,跳过 allowedOrigins 配置');
return; return;
} }
try { try {
const origins = JSON.stringify([ let content = readFileSync(configFile, 'utf8');
'http://0.0.0.0:18789', const newOrigin = `https://${clawId}.claw.cutos.ai`;
`https://${clawId}.claw.cutos.ai`,
]); // 替换 https://xxxx.claw.cutos.ai 为当前 clawId 对应的域名
execFileSync(bin, ['config', 'set', 'gateway.controlUi.allowedOrigins', origins], { const updated = content.replace(
timeout: 5000, /https:\/\/\d+\.claw\.cutos\.ai/g,
newOrigin
);
if (updated === content) {
log.info('clawd', `openclaw allowedOrigins 已是最新,无需修改`);
} else {
writeFileSync(configFile, updated, 'utf8');
log.info('clawd', `openclaw config 已更新: ${newOrigin}`);
}
} catch (e) {
log.warn('clawd', `openclaw config 修改失败: ${e.message}`);
return;
}
// 2. 用 sts 身份执行 openclaw gateway restart
const openclaw = '/home/sts/.npm-global/bin/openclaw';
if (!existsSync(openclaw)) {
log.warn('clawd', 'openclaw 未找到,跳过 gateway restart');
return;
}
try {
execFileSync('su', ['-', 'sts', '-c', `${openclaw} gateway restart`], {
timeout: 15000,
stdio: 'ignore', stdio: 'ignore',
}); });
log.info('clawd', `openclaw allowedOrigins 已配置: ${clawId}.claw.cutos.ai`); log.info('clawd', 'openclaw gateway restart 完成');
} catch (e) { } catch (e) {
log.warn('clawd', `openclaw 配置失败: ${e.message}`); log.warn('clawd', `openclaw gateway restart 失败: ${e.message}`);
} }
} }