fix: edit openclaw config.yaml directly and restart as sts user
Made-with: Cursor
This commit is contained in:
@@ -398,31 +398,51 @@ class ClawClient {
|
||||
// ── OpenClaw 配置 ────────────────────────────────────────────────────────────
|
||||
|
||||
_configureOpenClaw(clawId) {
|
||||
// 查找 openclaw 二进制(可能在用户目录下,不在 root PATH 中)
|
||||
const candidates = [
|
||||
'/home/sts/.npm-global/bin/openclaw',
|
||||
'/usr/local/bin/openclaw',
|
||||
'/usr/bin/openclaw',
|
||||
];
|
||||
const { existsSync } = require('fs');
|
||||
const bin = candidates.find(p => existsSync(p));
|
||||
if (!bin) {
|
||||
log.warn('clawd', 'openclaw 未找到,跳过 allowedOrigins 配置');
|
||||
const { existsSync, readFileSync, writeFileSync } = require('fs');
|
||||
|
||||
// 1. 直接修改 openclaw 配置文件(避免用 root 执行 openclaw CLI 超时问题)
|
||||
const configFile = '/home/sts/.openclaw/config/config.yaml';
|
||||
if (!existsSync(configFile)) {
|
||||
log.warn('clawd', `openclaw 配置文件不存在: ${configFile}`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const origins = JSON.stringify([
|
||||
'http://0.0.0.0:18789',
|
||||
`https://${clawId}.claw.cutos.ai`,
|
||||
]);
|
||||
execFileSync(bin, ['config', 'set', 'gateway.controlUi.allowedOrigins', origins], {
|
||||
timeout: 5000,
|
||||
let content = readFileSync(configFile, 'utf8');
|
||||
const newOrigin = `https://${clawId}.claw.cutos.ai`;
|
||||
|
||||
// 替换 https://xxxx.claw.cutos.ai 为当前 clawId 对应的域名
|
||||
const updated = content.replace(
|
||||
/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',
|
||||
});
|
||||
log.info('clawd', `openclaw allowedOrigins 已配置: ${clawId}.claw.cutos.ai`);
|
||||
log.info('clawd', 'openclaw gateway restart 完成');
|
||||
} catch (e) {
|
||||
log.warn('clawd', `openclaw 配置失败: ${e.message}`);
|
||||
log.warn('clawd', `openclaw gateway restart 失败: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user