| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #!/usr/bin/env node
- /**
- * 到期/回收扫描(2026-12-03)—— **默认只读**,输出清单供人工确认。
- *
- * 用法:
- * node scripts/trial-sweep.mjs # 干跑:列出到期与待回收清单
- * node scripts/trial-sweep.mjs --apply # 真正执行(到期标记 + 7 天回收)
- * node scripts/trial-sweep.mjs --recycle-days=7 # 回收门槛(默认 7 天)
- *
- * 判定:
- * 到期 —— accessExpiresAt <= now 且 accessState != 'expired'(旧数据用 trialEndsAt 兜底)
- * 回收 —— accessState='expired' 且 过期已满 N 天(默认 7)
- *
- * 回收动作:停容器由运维执行(本脚本只输出 docker 命令);记录置 isDeleted、绑定 revoked。
- * 🔴 不自动删容器 —— 删除不可逆,必须人工确认后执行脚本给出的命令。
- */
- import fs from 'node:fs';
- const cfg = JSON.parse(fs.readFileSync('.claude/dev-login.json', 'utf8'));
- const H = { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster', 'X-Parse-Master-Key': cfg.masterKey };
- const B = 'https://server.fmode.cn/parse/classes';
- const arg = (n, d) => process.argv.find((a) => a.startsWith(`--${n}=`))?.split('=')[1] ?? d;
- const APPLY = process.argv.includes('--apply');
- const RECYCLE_DAYS = Number(arg('recycle-days', '7'));
- const now = Date.now();
- const q = async (where) => (await (await fetch(`${B}/FmodeAgent?where=${encodeURIComponent(JSON.stringify(where))}&limit=1000&keys=objectId,name,agentId,planCode,trialEndsAt,accessExpiresAt,accessState,accessTokenVer,user`, { headers: H })).json()).results ?? [];
- const put = async (id, body) => (await fetch(`${B}/FmodeAgent/${id}`, { method: 'PUT', headers: H, body: JSON.stringify(body) })).status;
- /* 1. 已到期但状态还没标记的 */
- const all = await q({ isDeleted: { $ne: true } });
- const expired = [];
- const recycle = [];
- const healthy = [];
- for (const a of all) {
- const expRaw = a.accessExpiresAt?.iso || a.trialEndsAt?.iso;
- const expMs = expRaw ? Date.parse(expRaw) : 0;
- const isExpired = expMs > 0 && expMs <= now;
- if (!isExpired) { healthy.push(a); continue; }
- const overDays = Math.floor((now - expMs) / 86400000);
- if (a.accessState !== 'expired') expired.push({ ...a, overDays, expRaw });
- if (overDays >= RECYCLE_DAYS) recycle.push({ ...a, overDays, expRaw });
- }
- console.log(`扫描 ${all.length} 条活跃记录(回收门槛 ${RECYCLE_DAYS} 天)\n`);
- console.log(`【待标记到期】${expired.length} 条`);
- for (const a of expired) console.log(` ${a.name} (${a.agentId || a.objectId}) planCode=${a.planCode} 到期于 ${a.expRaw?.slice(0, 10)},已超 ${a.overDays} 天`);
- console.log(`\n【待回收】${recycle.length} 条(超过 ${RECYCLE_DAYS} 天)`);
- for (const a of recycle) console.log(` ${a.name} (${a.agentId || a.objectId}) 已超 ${a.overDays} 天`);
- console.log(`\n【健康】${healthy.length} 条`);
- if (recycle.length) {
- console.log('\n回收需人工执行(脚本不自动删容器):');
- for (const a of recycle) console.log(` docker stop ${a.agentId} && docker rm ${a.agentId} # 数据目录 /opt/fmode-containers/${a.agentId}/data 保留 30 天后人工清理`);
- }
- if (!APPLY) {
- console.log('\n[干跑] 未加 --apply,未修改任何数据。');
- process.exit(0);
- }
- let n = 0;
- for (const a of expired) {
- await put(a.objectId, { accessState: 'expired' });
- n++;
- }
- for (const a of recycle) {
- await put(a.objectId, { isDeleted: true, accessState: 'revoked' });
- const bw = encodeURIComponent(JSON.stringify({ agentId: a.agentId }));
- const bs = (await (await fetch(`${B}/FmodeMatrixBinding?where=${bw}`, { headers: H })).json()).results ?? [];
- for (const b of bs) await fetch(`${B}/FmodeMatrixBinding/${b.objectId}`, { method: 'PUT', headers: H, body: JSON.stringify({ status: 'revoked' }) });
- }
- console.log(`\n[已执行] 标记到期 ${n} 条,回收记录 ${recycle.length} 条(容器需按上面命令人工停删)。`);
|