ssh-net 26.10.53 → 26.10.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +63 -0
- package/README.md +157 -105
- package/libexec/cli.mjs +51 -16
- package/libexec/commands/doctor.mjs +16 -13
- package/libexec/commands/install.mjs +12 -9
- package/libexec/commands/register.mjs +23 -18
- package/libexec/commands/restart.mjs +5 -5
- package/libexec/commands/start.mjs +8 -5
- package/libexec/commands/status.mjs +62 -45
- package/libexec/commands/stop.mjs +56 -20
- package/libexec/commands/uninstall.mjs +76 -21
- package/libexec/commands/update.mjs +44 -65
- package/libexec/lib/cliargs.mjs +10 -4
- package/libexec/lib/config.mjs +10 -0
- package/libexec/lib/jobctl.mjs +5 -4
- package/libexec/lib/launchd.mjs +29 -12
- package/libexec/lib/manage.mjs +2 -2
- package/libexec/lib/manifest.mjs +3 -0
- package/libexec/lib/paths.mjs +2 -1
- package/libexec/lib/render.mjs +2 -2
- package/libexec/lib/runtime.mjs +45 -8
- package/libexec/lib/systemd.mjs +35 -6
- package/libexec/lib/version.mjs +12 -0
- package/libexec/sn-route +142 -91
- package/libexec/ssh-net-uplink-edge +3 -3
- package/package.json +3 -3
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// update:一键升级——npm i -g 后重跑 install 刷新运行时。
|
|
2
2
|
//
|
|
3
3
|
// 版本选择一条直线:显式版号(update 26.10.50,版号唯一地址,传播窗口内
|
|
4
|
-
// 通常立刻可装,也用于回滚);无参先 `npm view`
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// 旧版号(请求打到更慢的边缘节点)才退回 @latest 标签,npm 不会把已装的
|
|
8
|
-
// 新版降级。版号地址安装 npm 退出码 0 即保证装上的是该版,不必再 ls 核对。
|
|
4
|
+
// 通常立刻可装,也用于回滚);无参先 `npm view` 问最新版号,问到且不旧于
|
|
5
|
+
// 当前版才装这个 exact 版本。查询失败或问回旧版都直接停止,不退回 @latest:
|
|
6
|
+
// 标签与 view 看的是同一份 registry 元数据,先装再 ls 无法提前阻止降级。
|
|
9
7
|
// 只做一轮、不轮询等待:操作者即发版者,知道版号直接 update <版号> 最稳,
|
|
10
8
|
// 真被「显式版号也装不上」实锤后再加等待机制。
|
|
11
9
|
//
|
|
@@ -18,22 +16,21 @@
|
|
|
18
16
|
// 边界:node 角色需要 root(install 写 /etc/systemd/system),先检查再动
|
|
19
17
|
// npm,避免「包装上了、install 被拒」的半截态;core 侧**已注册** edge 的
|
|
20
18
|
// uplink-edge 实体随 install 刷新,job 参数仍等各 edge 下次报名对账。
|
|
21
|
-
import { readRuntimeVersion, RUNTIME_VERSION } from '../lib/runtime.mjs';
|
|
19
|
+
import { readRuntimeVersion, RUNTIME_VERSION, residentRuntimeFingerprint } from '../lib/runtime.mjs';
|
|
20
|
+
import { cmpVersion, VERSION_RE } from '../lib/version.mjs';
|
|
22
21
|
|
|
23
22
|
const NPM_TIMEOUT = 180_000; // 慢网络下全局装包的余量
|
|
24
|
-
const QUERY_TIMEOUT = 60_000; // view
|
|
23
|
+
const QUERY_TIMEOUT = 60_000; // view 只读查询
|
|
25
24
|
const INSTALL_TIMEOUT = 120_000;
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const VERSION_RE = /^\d+\.\d+\.\d+$/;
|
|
30
|
-
const ARG_HINT = 'update 只接受一个可选版本号,如 ssh-net update 26.10.50';
|
|
26
|
+
const ARG_HINT = 'update 只接受一个可选的三段数字版本号';
|
|
27
|
+
const NPM_NOT_FOUND = 'PATH 上找不到 npm——检查 ~/.zshenv(mac)或 .bashrc(Linux)里 node 的 PATH 配置后重试';
|
|
31
28
|
|
|
32
29
|
/** 失败详情:stderr 优先;被信号杀(code null,典型是超时)别说成 exit null */
|
|
33
30
|
const failWhy = (r) => r.stderr.trim()
|
|
34
31
|
|| (r.code !== null ? `exit ${r.code}` : `被信号杀死(${r.signal ?? '未知'},多半是超时)`);
|
|
35
32
|
|
|
36
|
-
/** 参数:零个或一个版号;返回 null
|
|
33
|
+
/** 参数:零个或一个版号;返回 null(无参)或版号字符串 */
|
|
37
34
|
function parseArgs(args) {
|
|
38
35
|
let version = null;
|
|
39
36
|
for (const a of args) {
|
|
@@ -48,100 +45,82 @@ function parseArgs(args) {
|
|
|
48
45
|
return version;
|
|
49
46
|
}
|
|
50
47
|
|
|
51
|
-
/**
|
|
52
|
-
function cmpVersion(a, b) {
|
|
53
|
-
const pa = a.split('.').map(Number);
|
|
54
|
-
const pb = b.split('.').map(Number);
|
|
55
|
-
for (let i = 0; i < 3; i += 1)
|
|
56
|
-
if (pa[i] !== pb[i]) return pa[i] < pb[i] ? -1 : 1;
|
|
57
|
-
return 0;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** ref 为 latest 或具体版号;统一带 --prefer-online 跳过缓存元数据 */
|
|
48
|
+
/** ref 为具体版号;统一带 --prefer-online 跳过缓存元数据 */
|
|
61
49
|
const npmInstall = (runner, ref) =>
|
|
62
50
|
runner.run('npm', ['install', '-g', `ssh-net@${ref}`, '--prefer-online'], { timeout: NPM_TIMEOUT });
|
|
63
51
|
|
|
64
|
-
/** 问 registry 当前最新版号;查不到或输出不合法返回 null
|
|
52
|
+
/** 问 registry 当前最新版号;查不到或输出不合法返回 null(调用方停止升级) */
|
|
65
53
|
const npmLatest = (runner) => {
|
|
66
54
|
const r = runner.run('npm', ['view', 'ssh-net', 'version', '--prefer-online'], { timeout: QUERY_TIMEOUT });
|
|
55
|
+
if (r.error && r.error.code === 'ENOENT')
|
|
56
|
+
throw new Error(NPM_NOT_FOUND);
|
|
67
57
|
const v = r.code === 0 ? r.stdout.trim() : '';
|
|
68
58
|
return VERSION_RE.test(v) ? v : null;
|
|
69
59
|
};
|
|
70
60
|
|
|
71
|
-
/** npm 实际装上的版本(不解析 npm 人类文案);ls 输出非 JSON 返回 null */
|
|
72
|
-
const npmInstalledVersion = (runner) => {
|
|
73
|
-
const ls = runner.run('npm', ['ls', '-g', 'ssh-net', '--depth=0', '--json'], { timeout: QUERY_TIMEOUT });
|
|
74
|
-
try {
|
|
75
|
-
return JSON.parse(ls.stdout).dependencies?.['ssh-net']?.version ?? null;
|
|
76
|
-
} catch { return null; }
|
|
77
|
-
};
|
|
78
|
-
|
|
79
61
|
export async function run(ctx, args = []) {
|
|
80
62
|
const target = parseArgs(args);
|
|
81
63
|
if (ctx.role === 'node' && ctx.uid !== 0)
|
|
82
|
-
throw new Error('node
|
|
64
|
+
throw new Error('node 上升级需要 root(install 要写 /etc/systemd/system),请运行 sudo -i ssh-net update');
|
|
83
65
|
|
|
84
66
|
const { runner } = ctx;
|
|
67
|
+
const residentBefore = residentRuntimeFingerprint(ctx.paths);
|
|
85
68
|
|
|
86
|
-
//
|
|
87
|
-
// 装的是什么要事后 ls 才知道(resolved = null)
|
|
69
|
+
// 无参只装 view 确认的 exact 版号;查询失败/旧版都不能拿 @latest 再赌一次
|
|
88
70
|
let resolved = target;
|
|
89
71
|
if (!resolved) {
|
|
90
72
|
const latest = npmLatest(runner);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
73
|
+
if (latest === null)
|
|
74
|
+
throw new Error('没能从 npm view 确认最新版,未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>');
|
|
75
|
+
if (cmpVersion(latest, RUNTIME_VERSION) < 0)
|
|
76
|
+
throw new Error(`npm 返回的最新版 ${latest} 比当前版本 ${RUNTIME_VERSION} 更旧,已停止且未执行安装——稍后重试;知道目标版号可直接 ssh-net update <版本号>`);
|
|
77
|
+
resolved = latest;
|
|
94
78
|
}
|
|
95
79
|
|
|
96
|
-
const r = npmInstall(runner, resolved
|
|
80
|
+
const r = npmInstall(runner, resolved);
|
|
97
81
|
if (r.error && r.error.code === 'ENOENT')
|
|
98
|
-
throw new Error(
|
|
82
|
+
throw new Error(NPM_NOT_FOUND);
|
|
99
83
|
if (r.code !== 0) {
|
|
100
84
|
if (/ETARGET/.test(r.stderr)) {
|
|
101
85
|
const why = '还没传播到本机打到的 registry 节点(常见于刚发布几分钟内)';
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
: `最新版${why}——等几分钟重跑 ssh-net update;知道版本号可直接 ssh-net update <版本号>(版号唯一地址通常立刻可装)。`;
|
|
86
|
+
const kind = target !== null ? '指定版本' : '最新版';
|
|
87
|
+
const tail = `${kind} ${resolved} ${why}——等几分钟重跑 ssh-net update ${resolved},不用换命令。`;
|
|
105
88
|
throw new Error(`${tail} npm 输出:${r.stderr.trim()}`);
|
|
106
89
|
}
|
|
107
|
-
throw new Error(`npm
|
|
90
|
+
throw new Error(`npm 安装 ${resolved} 失败:${failWhy(r)}(网络或 registry 问题;已装的版本不受影响,稍后重试)`);
|
|
108
91
|
}
|
|
109
92
|
|
|
110
|
-
// 版号地址 code 0
|
|
111
|
-
const installed = resolved ?? npmInstalledVersion(runner);
|
|
112
|
-
|
|
93
|
+
// 版号地址 code 0 即装上该版,无需再解析 npm 人类文案或 ls 猜版本
|
|
113
94
|
// 换装后用新 bin 跑 install:对账运行时实体/软链/plist/unit 并落
|
|
114
|
-
// runtime-version
|
|
95
|
+
// runtime-version 戳(内容没变不重写或重启;服务配置变化仍可能断连)。PATH 上找 ssh-net:
|
|
115
96
|
// 非交互环境由 dotfiles 保证(同报名 RPC 的前提,design §7 第 5 条)
|
|
116
97
|
const inst = runner.run('ssh-net', ['install'], { timeout: INSTALL_TIMEOUT });
|
|
117
98
|
if (inst.error && inst.error.code === 'ENOENT')
|
|
118
99
|
throw new Error('npm 换装后 PATH 上找不到 ssh-net(全局 bin 未生效)——新开一个 shell 或手动跑一次 ssh-net install;若反复出现,检查 fnm 的 global 包路径');
|
|
119
100
|
if (inst.code !== 0)
|
|
120
|
-
throw new Error(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
// 它唯一能到达的场景里误报 stale;如实说没确认到,不碰戳校验
|
|
130
|
-
if (installed === null)
|
|
131
|
-
return `升级完成。${inst.stdout.trim()}(没能确认实际装上的版本——ssh-net status 可核对版本差)${rollover}`;
|
|
101
|
+
throw new Error(`npm 包已切换,但 install 刷新没成功:${failWhy(inst)}——手动跑一次 ssh-net install 修复`);
|
|
102
|
+
|
|
103
|
+
const rel = cmpVersion(resolved, RUNTIME_VERSION);
|
|
104
|
+
const residentChanged = residentRuntimeFingerprint(ctx.paths) !== residentBefore;
|
|
105
|
+
// 提示依据 install 前后的常驻脚本,而不是包版本号或整个 bin/:同版修复也会
|
|
106
|
+
// 换脚本,按需工具与文档-only 更新不需要后台重连提示
|
|
107
|
+
const rollover = residentChanged
|
|
108
|
+
? '提示:常驻脚本已更新;已在运行的后台进程到下次重连才使用新版,服务配置变化或手动 restart 可能断开当前连接'
|
|
109
|
+
: '';
|
|
132
110
|
|
|
133
111
|
// 后置校验:真换装后新 bin 的 install 必把版本戳刷到刚装上的版本
|
|
134
112
|
// (ensureRuntime 无条件 refreshStamp);戳没跟上说明 PATH 解析到了旧
|
|
135
113
|
// ssh-net,install 跑在旧代码上、升级静默没生效(status 的 runtime-version
|
|
136
114
|
// 对账能查到,但当场报比事后对账快)。no-op 时新旧版本恒等,校验无意义,跳过
|
|
137
115
|
const stamp = readRuntimeVersion(ctx.paths);
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
? `\n⚠️ 但后台脚本还是旧版 ${stamp}——刚才的 install 跑的不是新装的 ssh-net(PATH 可能指向旧安装,如 fnm 多版本残留):which -a ssh-net 排查,再手动跑一次 ssh-net install`
|
|
116
|
+
const stampWarning = rel !== 0 && stamp !== null && stamp !== resolved
|
|
117
|
+
? `⚠️ 后台脚本版本仍为 ${stamp}——刚才的 install 可能跑了旧 ssh-net;用 which -a ssh-net 检查 PATH,再手动运行 ssh-net install`
|
|
141
118
|
: '';
|
|
142
119
|
|
|
143
120
|
const head = target !== null && rel < 0
|
|
144
|
-
? `已切换到指定版本 ${
|
|
145
|
-
:
|
|
146
|
-
|
|
121
|
+
? `已切换到指定版本 ${resolved}(原版本 ${RUNTIME_VERSION},这是降级)`
|
|
122
|
+
: rel === 0
|
|
123
|
+
? (target !== null ? `当前已是指定版本 ${resolved}` : '已是最新版本')
|
|
124
|
+
: `已升级到 ${resolved}`;
|
|
125
|
+
return [head, inst.stdout.trim(), stampWarning, rollover].filter(Boolean).join('\n');
|
|
147
126
|
}
|
package/libexec/lib/cliargs.mjs
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
1
|
+
// 命令行参数与配置恢复输出的共用助手。
|
|
2
|
+
|
|
3
|
+
/** 恢复结果同时交代配置前提与实际处理范围,避免看起来像正常全量体检。 */
|
|
4
|
+
export function recoveryResult(ctx, action) {
|
|
5
|
+
const reason = ctx.configError?.message ?? '未知配置错误';
|
|
6
|
+
return `配置不可用,已进入恢复模式:${action}\n原因:${reason}`;
|
|
7
|
+
}
|
|
3
8
|
|
|
4
9
|
/**
|
|
5
10
|
* core/node 是单总开关:无参或显式 -a/--all 收下(等同缺省),其余参数拒绝。
|
|
6
11
|
* @param {string[]} args
|
|
7
12
|
* @param {'start'|'stop'} verb
|
|
13
|
+
* @param {string} [message] 恢复模式等场景可覆盖错误说明,参数判断仍只有这一份
|
|
8
14
|
*/
|
|
9
|
-
export function assertUnitSwitchArgs(args, verb) {
|
|
15
|
+
export function assertUnitSwitchArgs(args, verb, message) {
|
|
10
16
|
if (args.length && !(args.length === 1 && (args[0] === '-a' || args[0] === '--all')))
|
|
11
|
-
throw new Error(`本机是单总开关,${verb} 不带参数(-a/--all 亦可,等同缺省)`);
|
|
17
|
+
throw new Error(message ?? `本机是单总开关,${verb} 不带参数(-a/--all 亦可,等同缺省)`);
|
|
12
18
|
}
|
package/libexec/lib/config.mjs
CHANGED
|
@@ -190,6 +190,16 @@ export function parseTopology(text) {
|
|
|
190
190
|
if (core.length !== 1)
|
|
191
191
|
throw new ConfigError(0, `core 必须恰好一台,当前 ${core.length} 台`);
|
|
192
192
|
|
|
193
|
+
// 所有注册口都绑在 relay 的 127.0.0.1 上,跨 core/node 必须唯一
|
|
194
|
+
const relayPorts = new Map();
|
|
195
|
+
for (const h of hosts) {
|
|
196
|
+
if (h.relayPort === null) continue;
|
|
197
|
+
const previous = relayPorts.get(h.relayPort);
|
|
198
|
+
if (previous)
|
|
199
|
+
throw new ConfigError(0, `relay 注册端口重复:${h.relayPort}(${previous} 与 ${h.alias})`);
|
|
200
|
+
relayPorts.set(h.relayPort, h.alias);
|
|
201
|
+
}
|
|
202
|
+
|
|
193
203
|
/** @type {Forward[]} */
|
|
194
204
|
const forwards = [];
|
|
195
205
|
/** @type {Set<number>} */
|
package/libexec/lib/jobctl.mjs
CHANGED
|
@@ -14,17 +14,18 @@ function sleepMs(ms) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
function isGone(ctx, label) {
|
|
17
|
-
|
|
17
|
+
// 停止路径不能把 launchd 查询失败当成已退出,否则会继续谎报关闭成功
|
|
18
|
+
const st = launchd.printState(ctx.runner, ctx.uid, label, true);
|
|
18
19
|
return !st.registered || (!st.running && st.pid === null);
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* @param {object} ctx
|
|
24
|
+
* @param {string} [label] 配置恢复模式按磁盘 label 停止;正常路径取当前 core
|
|
23
25
|
* @returns {{gone:boolean, label:string}}
|
|
24
26
|
*/
|
|
25
|
-
export function reapEdgeLink(ctx) {
|
|
26
|
-
|
|
27
|
-
if (!launchd.printState(ctx.runner, ctx.uid, label).registered)
|
|
27
|
+
export function reapEdgeLink(ctx, label = linkLabel(ctx.topo.core.alias)) {
|
|
28
|
+
if (!launchd.printState(ctx.runner, ctx.uid, label, true).registered)
|
|
28
29
|
return { gone: true, label };
|
|
29
30
|
|
|
30
31
|
launchd.killJob(ctx.runner, ctx.uid, label, 'TERM');
|
package/libexec/lib/launchd.mjs
CHANGED
|
@@ -19,23 +19,35 @@ const NOT_REGISTERED_PRINT = Object.freeze(
|
|
|
19
19
|
const NOT_REGISTERED = Object.freeze({ disabled: false, ...NOT_REGISTERED_PRINT });
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* 磁盘 LaunchAgents
|
|
23
|
-
* uninstall 共用;topology 已删的机器只能靠这个 glob 兜底发现,目录不存在
|
|
24
|
-
* 时空)。
|
|
22
|
+
* 磁盘 LaunchAgents 里指定前缀的 job label(卸载用;目录不存在时空)。
|
|
25
23
|
* @param {string} launchAgentsDir
|
|
24
|
+
* @param {string} prefix
|
|
26
25
|
* @returns {string[]}
|
|
27
26
|
*/
|
|
28
|
-
export function
|
|
27
|
+
export function labelsOnDisk(launchAgentsDir, prefix) {
|
|
29
28
|
try {
|
|
30
29
|
return fs
|
|
31
30
|
.readdirSync(launchAgentsDir)
|
|
32
|
-
.filter((f) => f.startsWith(
|
|
33
|
-
.map((f) => f.slice(
|
|
34
|
-
} catch {
|
|
35
|
-
|
|
31
|
+
.filter((f) => f.startsWith(prefix) && f.endsWith('.plist'))
|
|
32
|
+
.map((f) => f.slice(0, -'.plist'.length));
|
|
33
|
+
} catch (err) {
|
|
34
|
+
// 只有目录不存在才是空清单;权限/IO 错误若吞掉,恢复卸载会漏掉在册任务
|
|
35
|
+
if (err.code === 'ENOENT') return [];
|
|
36
|
+
throw err;
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
|
|
40
|
+
/**
|
|
41
|
+
* 磁盘 LaunchAgents 里 uplink-edge job plist 的别名清单(install/status/
|
|
42
|
+
* uninstall 共用;topology 已删的机器只能靠这个 glob 兜底发现)。
|
|
43
|
+
* @param {string} launchAgentsDir
|
|
44
|
+
* @returns {string[]}
|
|
45
|
+
*/
|
|
46
|
+
export function edgeAliasesOnDisk(launchAgentsDir) {
|
|
47
|
+
return labelsOnDisk(launchAgentsDir, EDGE_PLIST_PREFIX)
|
|
48
|
+
.map((label) => label.slice(EDGE_PLIST_PREFIX.length));
|
|
49
|
+
}
|
|
50
|
+
|
|
39
51
|
/**
|
|
40
52
|
* print 正文解析。
|
|
41
53
|
* @param {{code:number|null, stderr:string, stdout:string}} result
|
|
@@ -135,15 +147,20 @@ export function inspect(runner, uid, label) {
|
|
|
135
147
|
|
|
136
148
|
/**
|
|
137
149
|
* 只要注册/运行/pid 事实(stop 轮询专用):不跑全域 print-disabled。
|
|
150
|
+
* 删除路径必须传 strict:除明确的 113/not-found 外,查询失败都当场报错,
|
|
151
|
+
* 不能把「不知道是否注册」当成「未注册」后继续删 plist。
|
|
138
152
|
* @param {import('./exec.mjs').Runner} runner
|
|
139
153
|
* @param {number} uid
|
|
140
154
|
* @param {string} label
|
|
155
|
+
* @param {boolean} [strict=false]
|
|
141
156
|
* @returns {ReturnType<typeof parsePrint>}
|
|
142
157
|
*/
|
|
143
|
-
export function printState(runner, uid, label) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
158
|
+
export function printState(runner, uid, label, strict = false) {
|
|
159
|
+
const result = runner.run('launchctl', ['print', `${guiDomain(uid)}/${label}`]);
|
|
160
|
+
if (strict && result.code !== 0 && result.code !== 113
|
|
161
|
+
&& !/Could not find service/.test(result.stderr ?? ''))
|
|
162
|
+
throw new Error(`launchctl print 失败(${label}):${result.stderr.trim() || `exit ${result.code}`}`);
|
|
163
|
+
return parsePrint(result);
|
|
147
164
|
}
|
|
148
165
|
|
|
149
166
|
/** 以下均返回原始 RunResult,由命令层判 code(编排顺序是冻结行为,勿在此吞错)。 */
|
package/libexec/lib/manage.mjs
CHANGED
|
@@ -105,8 +105,8 @@ export function syncPlist(ctx, { label, plistPath, content }) {
|
|
|
105
105
|
*/
|
|
106
106
|
export function removeJob(ctx, label, plistPath) {
|
|
107
107
|
const { runner, uid } = ctx;
|
|
108
|
-
//
|
|
109
|
-
const st = launchd.printState(runner, uid, label);
|
|
108
|
+
// 删除路径按查询失败中止,只有明确的未注册才允许直接删 plist
|
|
109
|
+
const st = launchd.printState(runner, uid, label, true);
|
|
110
110
|
if (st.registered) {
|
|
111
111
|
const bo = launchd.bootout(runner, uid, label);
|
|
112
112
|
// 113 = 未注册(竞态下消失),其余失败必须报:job 还在注册表里,删了
|
package/libexec/lib/manifest.mjs
CHANGED
|
@@ -14,6 +14,9 @@ export const SN_REGISTER_LAN = 'sn-register-lan';
|
|
|
14
14
|
const LINK_CORE = 'ssh-net-link-core';
|
|
15
15
|
const UPLINK_RELAY = 'ssh-net-uplink-relay';
|
|
16
16
|
export const UPLINK_EDGE = 'ssh-net-uplink-edge';
|
|
17
|
+
// 可能被后台 job 长期持有的实体;软链按目标名归入同一组。sn-* 按需工具
|
|
18
|
+
// 不影响 update 的重连提示
|
|
19
|
+
export const RESIDENT_SCRIPTS = Object.freeze([LINK_CORE, UPLINK_RELAY, UPLINK_EDGE]);
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
22
|
* link 附加件专属产物:第 4 实体 + 带机器名软链。edgeRuntimePlan 装了 link
|
package/libexec/lib/paths.mjs
CHANGED
|
@@ -85,6 +85,7 @@ export function machinePaths(env = process.env) {
|
|
|
85
85
|
* @param {string} label local.<...> job label */
|
|
86
86
|
plistFile: (label) => path.join(launchAgentsDir, plistFileName(label)),
|
|
87
87
|
logsDir: path.join(home, 'Library', 'Logs'),
|
|
88
|
-
|
|
88
|
+
// 测试 CLI 的 Linux 恢复路径时重定向到临时目录;生产缺省系统目录
|
|
89
|
+
systemdUnitDir: env.SSH_NET_SYSTEMD_UNIT_DIR ?? '/etc/systemd/system',
|
|
89
90
|
});
|
|
90
91
|
}
|
package/libexec/lib/render.mjs
CHANGED
|
@@ -247,7 +247,7 @@ export function probeSshOpts(connectTimeout) {
|
|
|
247
247
|
* @param {import('./config.mjs').Topology} topo
|
|
248
248
|
* @param {object} [opts]
|
|
249
249
|
* @param {number} [opts.connectTimeout=5]
|
|
250
|
-
* @param {boolean} [opts.acceptHost=false]
|
|
250
|
+
* @param {boolean} [opts.acceptHost=false] true 允许首连 accept-new;false 固定 yes
|
|
251
251
|
* @param {string} [opts.remoteCommand] 给了就追加为远端命令
|
|
252
252
|
* @returns {string[]}
|
|
253
253
|
*/
|
|
@@ -262,7 +262,7 @@ export function coreViaRelayArgs(topo, {
|
|
|
262
262
|
'-o', `HostKeyAlias=${topo.core.alias}`,
|
|
263
263
|
...probeSshOpts(connectTimeout),
|
|
264
264
|
];
|
|
265
|
-
|
|
265
|
+
args.push('-o', `StrictHostKeyChecking=${acceptHost ? 'accept-new' : 'yes'}`);
|
|
266
266
|
args.push(`${topo.core.user}@127.0.0.1`);
|
|
267
267
|
if (remoteCommand !== undefined) args.push(remoteCommand);
|
|
268
268
|
return args;
|
package/libexec/lib/runtime.mjs
CHANGED
|
@@ -8,12 +8,47 @@
|
|
|
8
8
|
// 版本差即报
|
|
9
9
|
import fs from 'node:fs';
|
|
10
10
|
import path from 'node:path';
|
|
11
|
+
import { createHash } from 'node:crypto';
|
|
11
12
|
import { packageScriptPath } from './paths.mjs';
|
|
13
|
+
import { RESIDENT_SCRIPTS } from './manifest.mjs';
|
|
12
14
|
import { readFileOrNull, readTrimmedOrNull, writeFileAtomic } from './fsutil.mjs';
|
|
13
15
|
import pkg from '../../package.json' with { type: 'json' };
|
|
14
16
|
|
|
15
17
|
export const RUNTIME_VERSION = pkg.version;
|
|
16
18
|
|
|
19
|
+
/**
|
|
20
|
+
* 常驻服务脚本及其服务软链的实际内容指纹。update 用它比较 install 前后是否
|
|
21
|
+
* 换了后台进程需要重连的脚本;sn-* 按需工具更新不参与 rollover 判断。
|
|
22
|
+
* @param {{binDir:string}} paths
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
export function residentRuntimeFingerprint(paths) {
|
|
26
|
+
const hash = createHash('sha256');
|
|
27
|
+
let entries = [];
|
|
28
|
+
try { entries = fs.readdirSync(paths.binDir, { withFileTypes: true }); } catch (err) {
|
|
29
|
+
if (err.code === 'ENOENT') return hash.digest('hex');
|
|
30
|
+
throw err;
|
|
31
|
+
}
|
|
32
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
33
|
+
const file = path.join(paths.binDir, entry.name);
|
|
34
|
+
const st = fs.lstatSync(file);
|
|
35
|
+
const target = st.isSymbolicLink() ? fs.readlinkSync(file) : entry.name;
|
|
36
|
+
if (!RESIDENT_SCRIPTS.includes(target)) continue;
|
|
37
|
+
hash.update(`${entry.name}\0${st.mode & 0o777}\0`);
|
|
38
|
+
if (st.isSymbolicLink()) {
|
|
39
|
+
hash.update(`link\0${target}\0`);
|
|
40
|
+
} else if (st.isFile()) {
|
|
41
|
+
hash.update('file\0');
|
|
42
|
+
hash.update(fs.readFileSync(file));
|
|
43
|
+
hash.update('\0');
|
|
44
|
+
} else {
|
|
45
|
+
// bin/ 契约只有实体和软链;异常类型也进入指纹,不能被当成无变化
|
|
46
|
+
hash.update(`other\0${st.mode}\0`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return hash.digest('hex');
|
|
50
|
+
}
|
|
51
|
+
|
|
17
52
|
/**
|
|
18
53
|
* 拷贝单脚本到 bin/(实体)。
|
|
19
54
|
* @param {string} dstPath
|
|
@@ -23,13 +58,14 @@ export const RUNTIME_VERSION = pkg.version;
|
|
|
23
58
|
export function ensureScript(dstPath, srcPath) {
|
|
24
59
|
const src = fs.readFileSync(srcPath);
|
|
25
60
|
const cur = readFileOrNull(dstPath, null);
|
|
26
|
-
|
|
61
|
+
const st = cur === null ? null : fs.lstatSync(dstPath);
|
|
62
|
+
if (st !== null && st.isFile() && cur.equals(src)) {
|
|
27
63
|
// 内容对但可执行位丢了(cp/同步工具吃掉 mode):补上
|
|
28
|
-
if (
|
|
64
|
+
if (st.mode & 0o111) return 'unchanged';
|
|
29
65
|
fs.chmodSync(dstPath, 0o755);
|
|
30
66
|
return 'repaired';
|
|
31
67
|
}
|
|
32
|
-
//
|
|
68
|
+
// 缺失、旧内容或软链:tmp+rename 原子换(rename 替换目录项本身,不跟随软链)
|
|
33
69
|
writeFileAtomic(dstPath, src, { mode: 0o755 });
|
|
34
70
|
return cur !== null ? 'updated' : 'created';
|
|
35
71
|
}
|
|
@@ -89,9 +125,10 @@ function refreshStamp(ctx) {
|
|
|
89
125
|
}
|
|
90
126
|
|
|
91
127
|
/**
|
|
92
|
-
* 运行时脚本体检(status
|
|
93
|
-
*
|
|
94
|
-
*
|
|
128
|
+
* 运行时脚本体检(status 用):实体必须是普通文件,与包内逐字节比对,并查
|
|
129
|
+
* 可执行位。软链即使同内容也判 stale,ensureScript 会替成实体,避免运行时
|
|
130
|
+
* 继续依赖包路径;位丢了时 Match exec 直接 EACCES,只报字节一致会让 status
|
|
131
|
+
* 在选路静默落 relay 时误判健康。
|
|
95
132
|
* @param {string} dstPath
|
|
96
133
|
* @param {string} srcPath
|
|
97
134
|
* @returns {'ok'|'missing'|'stale'}
|
|
@@ -100,12 +137,12 @@ export function auditScript(dstPath, srcPath) {
|
|
|
100
137
|
let cur;
|
|
101
138
|
let st;
|
|
102
139
|
try {
|
|
103
|
-
[cur, st] = [fs.readFileSync(dstPath), fs.
|
|
140
|
+
[cur, st] = [fs.readFileSync(dstPath), fs.lstatSync(dstPath)];
|
|
104
141
|
} catch (err) {
|
|
105
142
|
if (err.code !== 'ENOENT') throw err;
|
|
106
143
|
return 'missing';
|
|
107
144
|
}
|
|
108
|
-
if (!cur.equals(fs.readFileSync(srcPath)) || !(st.mode & 0o111))
|
|
145
|
+
if (!st.isFile() || !cur.equals(fs.readFileSync(srcPath)) || !(st.mode & 0o111))
|
|
109
146
|
return 'stale';
|
|
110
147
|
return 'ok';
|
|
111
148
|
}
|
package/libexec/lib/systemd.mjs
CHANGED
|
@@ -16,6 +16,23 @@ export const startUnit = (runner, unit) => sc(runner, ['start', unit]);
|
|
|
16
16
|
const stopUnit = (runner, unit) => sc(runner, ['stop', unit]);
|
|
17
17
|
export const restartUnit = (runner, unit) => sc(runner, ['restart', unit]);
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* 磁盘上指定前缀的 unit 文件名(卸载清理改名前的残留;目录不存在时空)。
|
|
21
|
+
* @param {string} unitDir
|
|
22
|
+
* @param {string} prefix
|
|
23
|
+
* @returns {string[]}
|
|
24
|
+
*/
|
|
25
|
+
export function unitNamesOnDisk(unitDir, prefix) {
|
|
26
|
+
try {
|
|
27
|
+
return fs.readdirSync(unitDir)
|
|
28
|
+
.filter((f) => f.startsWith(prefix) && f.endsWith('.service'));
|
|
29
|
+
} catch (err) {
|
|
30
|
+
// 恢复卸载依赖完整清单;目录不存在才是空,其余错误不能静默吞掉
|
|
31
|
+
if (err.code === 'ENOENT') return [];
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
19
36
|
/** 没装/已卸的 unit 放行(stop 的 not-loaded 与 disable 的 does not exist
|
|
20
37
|
* 两种真机文案都认),其余失败抛出——stop/uninstall 对不存在的 unit 幂等,
|
|
21
38
|
* 但真失败不能谎报成功。start/restart 不经这里:unit 缺失是「先 install」
|
|
@@ -91,7 +108,7 @@ export function stopIfLoaded(runner, unit) {
|
|
|
91
108
|
* 对账一个 unit 到磁盘并确保服务在跑(与 mac 侧 syncPlist 同构,install 幂等
|
|
92
109
|
* 不断连):字节相同只 enable+start,不重写不 restart;内容变了先在临时目录
|
|
93
110
|
* verify → 通过才原子落盘 → daemon-reload → enable → restart;编排失败恢复
|
|
94
|
-
*
|
|
111
|
+
* 旧文件与 manager,首装撤掉 enable/进程,更新用旧 unit 恢复启动。
|
|
95
112
|
* @param {import('./exec.mjs').Runner} runner
|
|
96
113
|
* @param {{unitPath:string, unitName:string, content:string}} job
|
|
97
114
|
* @returns {'unchanged'|'deployed'}
|
|
@@ -115,12 +132,24 @@ export function syncUnit(runner, { unitPath, unitName, content }) {
|
|
|
115
132
|
throw new Error(`systemctl daemon-reload 失败:${reload.stderr.trim()}`);
|
|
116
133
|
enableRestart(runner, unitName);
|
|
117
134
|
} catch (err) {
|
|
118
|
-
//
|
|
135
|
+
// 文件相同只能表示部署完成:失败时恢复旧文件并重载 manager;首装撤掉
|
|
136
|
+
// enable/进程,更新用旧 unit 恢复启动,下一次 install 仍能重新对账。
|
|
119
137
|
try {
|
|
120
|
-
if (previous === null)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
138
|
+
if (previous === null) {
|
|
139
|
+
// disable 必须在删 unit 前:unit 缺失时 systemctl 会拒绝 disable,
|
|
140
|
+
// 而幂等规则会放行 does not exist,遗留 wants 软链。
|
|
141
|
+
stopIfLoaded(runner, unitName);
|
|
142
|
+
disableIfInstalled(runner, unitName);
|
|
143
|
+
fs.rmSync(unitPath);
|
|
144
|
+
} else {
|
|
145
|
+
writeFileAtomic(unitPath, previous, { mode: 0o644 });
|
|
146
|
+
}
|
|
147
|
+
const reload = daemonReload(runner);
|
|
148
|
+
if (reload.code !== 0)
|
|
149
|
+
throw new Error(`恢复后 systemctl daemon-reload 失败:${reload.stderr.trim()}`);
|
|
150
|
+
if (previous !== null) enableRestart(runner, unitName);
|
|
151
|
+
} catch (rollbackError) {
|
|
152
|
+
throw new AggregateError([err, rollbackError], `${err.message};恢复 systemd 状态失败:${rollbackError.message}`);
|
|
124
153
|
}
|
|
125
154
|
throw err;
|
|
126
155
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// 本仓只发数字三段 CalVer,没有预发布;update 与 publish 共用同一份判定,
|
|
2
|
+
// 防止 CLI 与发布闸门对版本格式和新旧关系逐渐分叉。
|
|
3
|
+
export const VERSION_RE = /^\d+\.\d+\.\d+$/;
|
|
4
|
+
|
|
5
|
+
/** 三段数字比较:-1/0/1;调用方先用 VERSION_RE 校验。 */
|
|
6
|
+
export function cmpVersion(a, b) {
|
|
7
|
+
const pa = a.split('.').map(Number);
|
|
8
|
+
const pb = b.split('.').map(Number);
|
|
9
|
+
for (let i = 0; i < 3; i += 1)
|
|
10
|
+
if (pa[i] !== pb[i]) return pa[i] < pb[i] ? -1 : 1;
|
|
11
|
+
return 0;
|
|
12
|
+
}
|