| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #!/usr/bin/env node
- /**
- * 按 container-skill-manifest.json 刷新 R730 上容器的技能(2026-12-03)。
- *
- * 与开通链路共用同一份清单:provision.sh 给新容器装技能,本脚本给存量容器补/替换。
- * 新增或替换技能只改清单,不改这里。
- *
- * 用法:
- * node scripts/refresh-container-skills.mjs --list # 只列清单与计划
- * node scripts/refresh-container-skills.mjs --dry-run # 对全部容器演算
- * node scripts/refresh-container-skills.mjs --containers a,b # 指定容器
- * node scripts/refresh-container-skills.mjs --all --apply # 真正下发(需显式 --apply)
- * node scripts/refresh-container-skills.mjs --containers x --apply --restart-gateway
- *
- * 约束(容器扩展铁律):只往 /opt/data/skills/<scope>/<name>/ 写文件,不碰 /opt/hermes 源码。
- */
- import { readFileSync, mkdirSync, writeFileSync } from 'node:fs';
- import { execFileSync } from 'node:child_process';
- import { resolve, basename } from 'node:path';
- const ROOT = resolve(new URL('..', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1'));
- const MANIFEST = resolve(ROOT, 'cloud/skills/container-skill-manifest.json');
- const R730 = resolve(ROOT, '.local-backup/skill-lock-20261201/r730.py');
- const argv = process.argv.slice(2);
- const has = (f) => argv.includes(f);
- const val = (f) => {
- const i = argv.indexOf(f);
- return i >= 0 ? argv[i + 1] : undefined;
- };
- const manifest = JSON.parse(readFileSync(MANIFEST, 'utf8'));
- const enabled = (manifest.skills ?? []).filter((s) => !s.disabled);
- if (enabled.length === 0) {
- console.error('清单里没有可下发的技能');
- process.exit(1);
- }
- /** 读取技能文件内容(base64,避免 shell 转义问题) */
- function readSkillFiles(skill) {
- const srcDir = resolve(ROOT, skill.source);
- const out = {};
- for (const f of skill.files) {
- const p = resolve(srcDir, f);
- try {
- out[f] = readFileSync(p).toString('base64');
- } catch {
- throw new Error(`技能 ${skill.name} 缺少文件: ${p}`);
- }
- }
- return out;
- }
- /** 生成在 R730 上执行的 shell:把技能写进指定容器 */
- function buildScript(containers, opts) {
- const lines = ['set -e'];
- for (const c of containers) {
- lines.push(`echo "[CONTAINER] ${c}"`);
- for (const skill of enabled) {
- const files = readSkillFiles(skill);
- const dir = `/opt/data/skills/${skill.scope || 'custom'}/${skill.name}`;
- lines.push(`docker exec ${c} mkdir -p ${dir}`);
- for (const [name, b64] of Object.entries(files)) {
- // base64 -d 写入后 chown 回 hermes(容器内 uid 10000)
- lines.push(
- `echo "${b64}" | base64 -d | docker exec -i ${c} sh -c "cat > ${dir}/${name}"`,
- );
- }
- lines.push(`docker exec ${c} chmod 0755 ${dir}/*.py 2>/dev/null || true`);
- lines.push(`docker exec ${c} chown -R 10000:10000 ${dir} 2>/dev/null || true`);
- lines.push(`echo "[SKILL] ${skill.name} -> ${dir}"`);
- }
- if (opts.restartGateway) {
- lines.push(`docker exec ${c} /command/s6-svc -r /run/service/gateway-default || echo "[WARN] 重启网关失败"`);
- lines.push(`echo "[RESTART] ${c} 网关已重启"`);
- }
- }
- return lines.join('\n');
- }
- function listContainers() {
- const script = `docker ps --format '{{.Names}}' | grep '^agent-node-app-' || true`;
- const out = execFileSync('python', [R730, writeTmp(script), '60'], {
- encoding: 'utf8',
- cwd: ROOT,
- });
- return out.split('\n').map((s) => s.trim()).filter((s) => /^agent-node-app-/.test(s));
- }
- function writeTmp(content) {
- const dir = resolve(ROOT, '.local-backup/tmp-wecom');
- const f = resolve(dir, `refresh-${Date.now()}.sh`);
- // 目录在仓库内,r730.py 只接受仓库内路径
- mkdirSync(dir, { recursive: true });
- writeFileSync(f, content, 'utf8');
- return f;
- }
- // ---------- 入口 ----------
- if (has('--list')) {
- console.log(`清单: ${basename(MANIFEST)}(${enabled.length} 个启用)`);
- for (const s of enabled) {
- console.log(` · ${s.name} → /opt/data/skills/${s.scope || 'custom'}/${s.name}/ ${s.required ? '[必需]' : '[可选]'}`);
- }
- const off = (manifest.skills ?? []).filter((s) => s.disabled);
- if (off.length) {
- console.log('已禁用:');
- for (const s of off) console.log(` ✗ ${s.name} — ${s.reason ?? ''}`);
- }
- process.exit(0);
- }
- let targets = [];
- if (val('--containers')) {
- targets = val('--containers').split(',').map((s) => s.trim()).filter(Boolean);
- } else if (has('--all') || has('--dry-run')) {
- targets = listContainers();
- }
- if (targets.length === 0) {
- console.error('没有目标容器。用 --containers a,b 或 --all');
- process.exit(1);
- }
- console.log(`目标容器 ${targets.length} 个: ${targets.join(', ')}`);
- console.log(`技能 ${enabled.length} 个: ${enabled.map((s) => s.name).join(', ')}`);
- if (has('--dry-run') || !has('--apply')) {
- console.log('\n[DRY-RUN] 未加 --apply,不执行任何写入。');
- for (const c of targets) {
- for (const s of enabled) {
- console.log(` 将写入 ${c}:/opt/data/skills/${s.scope || 'custom'}/${s.name}/`);
- }
- }
- process.exit(0);
- }
- const script = buildScript(targets, { restartGateway: has('--restart-gateway') });
- const scriptPath = writeTmp(script);
- console.log('\n执行中...');
- const out = execFileSync('python', [R730, scriptPath, '600'], { encoding: 'utf8', cwd: ROOT });
- console.log(out);
- console.log('完成。若改了网关相关配置,记得加 --restart-gateway。');
|