refactor: 重构 OpenClaw config.yaml 更新逻辑
- _configureOpenClaw 改名为 _updateOpenClawOrigin(targetId) - active 时传真实 claw_id,inactive 时传 '0000' - 每次 _applyStatus(含 status_update)都调用,文件无变化则幂等跳过 - 有变化时 pkill -9 -x openclaw-gateway,不再依赖 openclaw CLI 或 systemctl --user - 移除 _openClawConfigured 标志位 Made-with: Cursor
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
const { execFileSync } = require('child_process');
|
const { execFileSync, execSync } = require('child_process');
|
||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
const log = require('./logger');
|
const log = require('./logger');
|
||||||
const { getBoxId } = require('./fingerprint');
|
const { getBoxId } = require('./fingerprint');
|
||||||
@@ -52,8 +52,6 @@ class ClawClient {
|
|||||||
// 最近一次 WS 错误是否是证书时间问题(NTP 未同步)
|
// 最近一次 WS 错误是否是证书时间问题(NTP 未同步)
|
||||||
this._certTimeError = false;
|
this._certTimeError = false;
|
||||||
|
|
||||||
// 记录已为哪个 clawId 配置过 openclaw(防止 WS 重连时重复执行)
|
|
||||||
this._openClawConfigured = null;
|
|
||||||
|
|
||||||
// systemd watchdog
|
// systemd watchdog
|
||||||
this._sdTimer = null;
|
this._sdTimer = null;
|
||||||
@@ -390,65 +388,54 @@ class ClawClient {
|
|||||||
log.info('clawd', '╚════════════════════════════════════╝');
|
log.info('clawd', '╚════════════════════════════════════╝');
|
||||||
log.info('clawd', '');
|
log.info('clawd', '');
|
||||||
log.info('clawd', '等待激活,心跳正常运行...');
|
log.info('clawd', '等待激活,心跳正常运行...');
|
||||||
|
this._updateOpenClawOrigin('0000');
|
||||||
} else {
|
} else {
|
||||||
led.status.setApps();
|
led.status.setApps();
|
||||||
led.display.showTime();
|
led.display.showTime();
|
||||||
log.info('clawd', `已激活 claw_id = ${this._cfg.claw_id}`);
|
log.info('clawd', `已激活 claw_id = ${this._cfg.claw_id}`);
|
||||||
if (this._cfg.claw_id && this._openClawConfigured !== this._cfg.claw_id) {
|
this._updateOpenClawOrigin(String(this._cfg.claw_id));
|
||||||
this._openClawConfigured = this._cfg.claw_id;
|
|
||||||
this._configureOpenClaw(this._cfg.claw_id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── OpenClaw 配置 ────────────────────────────────────────────────────────────
|
// ── OpenClaw 配置 ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
_configureOpenClaw(clawId) {
|
_updateOpenClawOrigin(targetId) {
|
||||||
const { existsSync, readFileSync, writeFileSync } = require('fs');
|
const { existsSync, readFileSync, writeFileSync } = require('fs');
|
||||||
|
|
||||||
// 1. 直接修改 openclaw 配置文件(避免用 root 执行 openclaw CLI 超时问题)
|
|
||||||
const configFile = '/home/sts/.openclaw/config/config.yaml';
|
const configFile = '/home/sts/.openclaw/config/config.yaml';
|
||||||
|
|
||||||
if (!existsSync(configFile)) {
|
if (!existsSync(configFile)) {
|
||||||
log.warn('clawd', `openclaw 配置文件不存在: ${configFile}`);
|
log.warn('clawd', `openclaw 配置文件不存在: ${configFile}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let content = readFileSync(configFile, 'utf8');
|
const content = readFileSync(configFile, 'utf8');
|
||||||
const newOrigin = `https://${clawId}.claw.cutos.ai`;
|
const newOrigin = `https://${targetId}.claw.cutos.ai`;
|
||||||
|
|
||||||
// 替换 https://xxxx.claw.cutos.ai 为当前 clawId 对应的域名
|
// 替换 https://任意子域.claw.cutos.ai 为目标域名
|
||||||
const updated = content.replace(
|
const updated = content.replace(
|
||||||
/https:\/\/\d+\.claw\.cutos\.ai/g,
|
/https:\/\/[^"'\s]+\.claw\.cutos\.ai/g,
|
||||||
newOrigin
|
newOrigin
|
||||||
);
|
);
|
||||||
|
|
||||||
if (updated === content) {
|
if (updated === content) {
|
||||||
log.info('clawd', `openclaw allowedOrigins 已是最新,无需修改`);
|
log.info('clawd', `openclaw origin 已是 ${newOrigin},无需变更`);
|
||||||
} else {
|
return;
|
||||||
writeFileSync(configFile, updated, 'utf8');
|
}
|
||||||
log.info('clawd', `openclaw config 已更新: ${newOrigin}`);
|
|
||||||
|
writeFileSync(configFile, updated, 'utf8');
|
||||||
|
log.info('clawd', `openclaw config 已更新: ${newOrigin}`);
|
||||||
|
|
||||||
|
// 文件有变化,kill -9 openclaw-gateway,让它被 systemd --user 自动拉起
|
||||||
|
try {
|
||||||
|
execSync('pkill -9 -x openclaw-gateway', { timeout: 3000 });
|
||||||
|
log.info('clawd', 'openclaw-gateway 已终止,等待自动重启');
|
||||||
|
} catch (_) {
|
||||||
|
// pkill 找不到进程时返回非 0,属于正常情况(进程未运行)
|
||||||
|
log.info('clawd', 'openclaw-gateway 进程不存在,无需终止');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.warn('clawd', `openclaw config 修改失败: ${e.message}`);
|
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 gateway restart 完成');
|
|
||||||
} catch (e) {
|
|
||||||
log.warn('clawd', `openclaw gateway restart 失败: ${e.message}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user