aws-runtime-bridge 1.3.2 → 1.3.5
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/README.md
CHANGED
|
@@ -23,7 +23,7 @@ npm install -g .
|
|
|
23
23
|
|
|
24
24
|
## 启动
|
|
25
25
|
|
|
26
|
-
首次运行 `awsb` / `aws-bridge` 时,如果不存在 `~/.aws-bridge/config.json`,CLI 会进入交互式配置引导;也可以在提示中选择跳过。跳过时仍会创建配置文件并自动生成随机 `connectionKey`,终端会输出该密钥,请保存后在 server/面板连接此 Bridge 时使用。非交互环境(如 systemd、CI、Docker
|
|
26
|
+
首次运行 `awsb` / `aws-bridge` 时,如果不存在 `~/.aws-bridge/config.json`,CLI 会进入交互式配置引导;也可以在提示中选择跳过。跳过时仍会创建配置文件并自动生成随机 `connectionKey`,终端会输出该密钥,请保存后在 server/面板连接此 Bridge 时使用。非交互环境(如 systemd、CI、Docker 后台启动)不会阻塞等待输入,也会自动生成随机 `connectionKey` 并跳过引导。
|
|
27
27
|
|
|
28
28
|
引导会生成类似下面的配置:
|
|
29
29
|
|
|
@@ -63,6 +63,22 @@ AWS_CLIENT_AGENT_MCP_ARGS='[]' \
|
|
|
63
63
|
aws-bridge
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
+
## systemd 服务管理
|
|
67
|
+
|
|
68
|
+
Linux systemd 环境中可使用 CLI 安装或卸载 `awsb.service`。安装命令会写入 systemd unit、执行 `systemctl daemon-reload`、启用开机自启,并在服务尚未运行时立即启动:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
sudo awsb service install
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
卸载命令会在服务运行时先停止服务,禁用开机自启,删除 unit 文件,并重新执行 `systemctl daemon-reload`:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
sudo awsb service uninstall
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`service` 命令仅支持 Linux systemd;在其他平台会直接返回清晰错误。生成的 unit 会设置 `AWS_BRIDGE_SKIP_SETUP=true`,避免后台服务启动时阻塞在交互式配置引导。
|
|
81
|
+
|
|
66
82
|
## 关键环境变量
|
|
67
83
|
|
|
68
84
|
| 变量名 | 说明 | 默认值 |
|
|
@@ -14,7 +14,14 @@ export interface CliCommandOptions {
|
|
|
14
14
|
stderr?: (message: string) => void;
|
|
15
15
|
runCommand?: (command: string, args: string[]) => CommandRunResult;
|
|
16
16
|
configure?: () => Promise<StartupConfigWizardResult>;
|
|
17
|
+
platform?: NodeJS.Platform;
|
|
18
|
+
serviceUnitPath?: string;
|
|
19
|
+
executablePath?: string;
|
|
17
20
|
}
|
|
18
21
|
export declare function readPackageVersion(packageRoot?: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* 生成 systemd unit 内容,主流程是用当前 Node 可执行文件启动当前 CLI 入口。
|
|
24
|
+
*/
|
|
25
|
+
export declare function createSystemdUnitContent(executablePath: string): string;
|
|
19
26
|
export declare function handleCliCommand(options?: CliCommandOptions): Promise<CliCommandResult>;
|
|
20
27
|
//# sourceMappingURL=cli-commands.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli-commands.d.ts","sourceRoot":"","sources":["../../src/services/cli-commands.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cli-commands.d.ts","sourceRoot":"","sources":["../../src/services/cli-commands.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAM5E,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,KAAK,CAAC,EAAE,KAAK,CAAC;CACf;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,gBAAgB,CAAC;IACnE,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC,yBAAyB,CAAC,CAAC;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC;IAC3B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAYD,wBAAgB,kBAAkB,CAAC,WAAW,SAA0B,GAAG,MAAM,CAMhF;AAgCD;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAsBvE;AAwOD,wBAAsB,gBAAgB,CACpC,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,gBAAgB,CAAC,CA+C3B"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import { configureStartupConfig } from "./startup-config-wizard.js";
|
|
6
|
+
const SYSTEMD_SERVICE_NAME = "awsb.service";
|
|
7
|
+
const DEFAULT_SYSTEMD_UNIT_PATH = `/etc/systemd/system/${SYSTEMD_SERVICE_NAME}`;
|
|
6
8
|
function getDefaultPackageRoot() {
|
|
7
9
|
const currentFile = fileURLToPath(import.meta.url);
|
|
8
10
|
return path.resolve(path.dirname(currentFile), "..", "..");
|
|
@@ -19,14 +21,215 @@ function defaultRunCommand(command, args) {
|
|
|
19
21
|
});
|
|
20
22
|
return { status: result.status, error: result.error };
|
|
21
23
|
}
|
|
24
|
+
function defaultStdout(message) {
|
|
25
|
+
process.stdout.write(`${message}\n`);
|
|
26
|
+
}
|
|
27
|
+
function defaultStderr(message) {
|
|
28
|
+
process.stderr.write(`${message}\n`);
|
|
29
|
+
}
|
|
22
30
|
function isVersionCommand(command) {
|
|
23
31
|
return command === "-v" || command === "--version" || command === "version";
|
|
24
32
|
}
|
|
33
|
+
function quoteSystemdExecArg(value) {
|
|
34
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
35
|
+
}
|
|
36
|
+
function getDefaultExecutablePath() {
|
|
37
|
+
return process.argv[1]
|
|
38
|
+
? path.resolve(process.argv[1])
|
|
39
|
+
: fileURLToPath(import.meta.url);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 生成 systemd unit 内容,主流程是用当前 Node 可执行文件启动当前 CLI 入口。
|
|
43
|
+
*/
|
|
44
|
+
export function createSystemdUnitContent(executablePath) {
|
|
45
|
+
const execStart = [process.execPath, executablePath]
|
|
46
|
+
.map((part) => quoteSystemdExecArg(part))
|
|
47
|
+
.join(" ");
|
|
48
|
+
return [
|
|
49
|
+
"[Unit]",
|
|
50
|
+
"Description=AgentsWorkStudio Runtime Bridge",
|
|
51
|
+
"After=network-online.target",
|
|
52
|
+
"Wants=network-online.target",
|
|
53
|
+
"",
|
|
54
|
+
"[Service]",
|
|
55
|
+
"Type=simple",
|
|
56
|
+
"Environment=AWS_BRIDGE_SKIP_SETUP=true",
|
|
57
|
+
`ExecStart=${execStart}`,
|
|
58
|
+
"Restart=on-failure",
|
|
59
|
+
"RestartSec=5s",
|
|
60
|
+
"",
|
|
61
|
+
"[Install]",
|
|
62
|
+
"WantedBy=multi-user.target",
|
|
63
|
+
"",
|
|
64
|
+
].join("\n");
|
|
65
|
+
}
|
|
66
|
+
function runSystemctl(runCommand, args) {
|
|
67
|
+
return runCommand("systemctl", args);
|
|
68
|
+
}
|
|
69
|
+
function systemctlFailureMessage(action, result) {
|
|
70
|
+
if (result.error) {
|
|
71
|
+
return `[runtime-bridge] ${action}失败: ${result.error.message}`;
|
|
72
|
+
}
|
|
73
|
+
return `[runtime-bridge] ${action}失败,systemctl 退出码: ${result.status ?? "unknown"}`;
|
|
74
|
+
}
|
|
75
|
+
function commandSucceeded(result) {
|
|
76
|
+
return !result.error && result.status === 0;
|
|
77
|
+
}
|
|
78
|
+
function ensureLinuxSystemd(platform, stderr) {
|
|
79
|
+
if (platform === "linux") {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
stderr("[runtime-bridge] service 命令仅支持 Linux systemd 环境。");
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
function printServiceUsage(stdout) {
|
|
86
|
+
stdout("Usage: awsb service <install|uninstall>");
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 安装 systemd 服务,主干流程是写入 unit、重载 systemd、启用开机自启,并在未运行时启动服务。
|
|
90
|
+
*/
|
|
91
|
+
function installSystemdService(options) {
|
|
92
|
+
if (!ensureLinuxSystemd(options.platform, options.stderr)) {
|
|
93
|
+
return { handled: true, exitCode: 1 };
|
|
94
|
+
}
|
|
95
|
+
const unitContent = createSystemdUnitContent(options.executablePath);
|
|
96
|
+
try {
|
|
97
|
+
mkdirSync(path.dirname(options.serviceUnitPath), { recursive: true });
|
|
98
|
+
writeFileSync(options.serviceUnitPath, unitContent, {
|
|
99
|
+
encoding: "utf-8",
|
|
100
|
+
mode: 0o644,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
105
|
+
options.stderr(`[runtime-bridge] 写入 systemd unit 失败: ${err.message}`);
|
|
106
|
+
return { handled: true, exitCode: 1 };
|
|
107
|
+
}
|
|
108
|
+
const daemonReloadResult = runSystemctl(options.runCommand, ["daemon-reload"]);
|
|
109
|
+
if (!commandSucceeded(daemonReloadResult)) {
|
|
110
|
+
options.stderr(systemctlFailureMessage("重载 systemd", daemonReloadResult));
|
|
111
|
+
return { handled: true, exitCode: daemonReloadResult.status ?? 1 };
|
|
112
|
+
}
|
|
113
|
+
const enableResult = runSystemctl(options.runCommand, [
|
|
114
|
+
"enable",
|
|
115
|
+
SYSTEMD_SERVICE_NAME,
|
|
116
|
+
]);
|
|
117
|
+
if (!commandSucceeded(enableResult)) {
|
|
118
|
+
options.stderr(systemctlFailureMessage("启用 awsb 开机自启", enableResult));
|
|
119
|
+
return { handled: true, exitCode: enableResult.status ?? 1 };
|
|
120
|
+
}
|
|
121
|
+
const activeResult = runSystemctl(options.runCommand, [
|
|
122
|
+
"is-active",
|
|
123
|
+
"--quiet",
|
|
124
|
+
SYSTEMD_SERVICE_NAME,
|
|
125
|
+
]);
|
|
126
|
+
if (activeResult.error) {
|
|
127
|
+
options.stderr(systemctlFailureMessage("检查 awsb 服务状态", activeResult));
|
|
128
|
+
return { handled: true, exitCode: 1 };
|
|
129
|
+
}
|
|
130
|
+
if (activeResult.status !== 0) {
|
|
131
|
+
const startResult = runSystemctl(options.runCommand, [
|
|
132
|
+
"start",
|
|
133
|
+
SYSTEMD_SERVICE_NAME,
|
|
134
|
+
]);
|
|
135
|
+
if (!commandSucceeded(startResult)) {
|
|
136
|
+
options.stderr(systemctlFailureMessage("启动 awsb 服务", startResult));
|
|
137
|
+
return { handled: true, exitCode: startResult.status ?? 1 };
|
|
138
|
+
}
|
|
139
|
+
options.stdout("[runtime-bridge] awsb systemd 服务已安装、已启用开机自启并已启动。");
|
|
140
|
+
return { handled: true, exitCode: 0 };
|
|
141
|
+
}
|
|
142
|
+
options.stdout("[runtime-bridge] awsb systemd 服务已安装并已启用开机自启;服务已在运行。");
|
|
143
|
+
return { handled: true, exitCode: 0 };
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 卸载 systemd 服务,主干流程是按需停止、禁用自启、删除 unit,并重载 systemd。
|
|
147
|
+
*/
|
|
148
|
+
function uninstallSystemdService(options) {
|
|
149
|
+
if (!ensureLinuxSystemd(options.platform, options.stderr)) {
|
|
150
|
+
return { handled: true, exitCode: 1 };
|
|
151
|
+
}
|
|
152
|
+
const unitExists = existsSync(options.serviceUnitPath);
|
|
153
|
+
const activeResult = runSystemctl(options.runCommand, [
|
|
154
|
+
"is-active",
|
|
155
|
+
"--quiet",
|
|
156
|
+
SYSTEMD_SERVICE_NAME,
|
|
157
|
+
]);
|
|
158
|
+
if (activeResult.error) {
|
|
159
|
+
options.stderr(systemctlFailureMessage("检查 awsb 服务状态", activeResult));
|
|
160
|
+
return { handled: true, exitCode: 1 };
|
|
161
|
+
}
|
|
162
|
+
if (activeResult.status === 0) {
|
|
163
|
+
const stopResult = runSystemctl(options.runCommand, [
|
|
164
|
+
"stop",
|
|
165
|
+
SYSTEMD_SERVICE_NAME,
|
|
166
|
+
]);
|
|
167
|
+
if (!commandSucceeded(stopResult)) {
|
|
168
|
+
options.stderr(systemctlFailureMessage("停止 awsb 服务", stopResult));
|
|
169
|
+
return { handled: true, exitCode: stopResult.status ?? 1 };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const disableResult = runSystemctl(options.runCommand, [
|
|
173
|
+
"disable",
|
|
174
|
+
SYSTEMD_SERVICE_NAME,
|
|
175
|
+
]);
|
|
176
|
+
if (disableResult.error || (disableResult.status !== 0 && unitExists)) {
|
|
177
|
+
options.stderr(systemctlFailureMessage("禁用 awsb 开机自启", disableResult));
|
|
178
|
+
return { handled: true, exitCode: disableResult.status ?? 1 };
|
|
179
|
+
}
|
|
180
|
+
if (unitExists) {
|
|
181
|
+
try {
|
|
182
|
+
unlinkSync(options.serviceUnitPath);
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
186
|
+
options.stderr(`[runtime-bridge] 删除 systemd unit 失败: ${err.message}`);
|
|
187
|
+
return { handled: true, exitCode: 1 };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
options.stdout("[runtime-bridge] awsb systemd unit 不存在,跳过删除。");
|
|
192
|
+
}
|
|
193
|
+
const daemonReloadResult = runSystemctl(options.runCommand, ["daemon-reload"]);
|
|
194
|
+
if (!commandSucceeded(daemonReloadResult)) {
|
|
195
|
+
options.stderr(systemctlFailureMessage("重载 systemd", daemonReloadResult));
|
|
196
|
+
return { handled: true, exitCode: daemonReloadResult.status ?? 1 };
|
|
197
|
+
}
|
|
198
|
+
options.stdout("[runtime-bridge] awsb systemd 服务已卸载。");
|
|
199
|
+
return { handled: true, exitCode: 0 };
|
|
200
|
+
}
|
|
201
|
+
function handleServiceCommand(argv, options, stdout, stderr) {
|
|
202
|
+
const subcommand = argv[1];
|
|
203
|
+
if (!subcommand ||
|
|
204
|
+
subcommand === "-h" ||
|
|
205
|
+
subcommand === "--help" ||
|
|
206
|
+
subcommand === "help") {
|
|
207
|
+
printServiceUsage(stdout);
|
|
208
|
+
return { handled: true, exitCode: subcommand ? 0 : 1 };
|
|
209
|
+
}
|
|
210
|
+
const serviceOptions = {
|
|
211
|
+
stdout,
|
|
212
|
+
stderr,
|
|
213
|
+
runCommand: options.runCommand || defaultRunCommand,
|
|
214
|
+
platform: options.platform || process.platform,
|
|
215
|
+
serviceUnitPath: options.serviceUnitPath || DEFAULT_SYSTEMD_UNIT_PATH,
|
|
216
|
+
executablePath: options.executablePath || getDefaultExecutablePath(),
|
|
217
|
+
};
|
|
218
|
+
if (subcommand === "install") {
|
|
219
|
+
return installSystemdService(serviceOptions);
|
|
220
|
+
}
|
|
221
|
+
if (subcommand === "uninstall") {
|
|
222
|
+
return uninstallSystemdService(serviceOptions);
|
|
223
|
+
}
|
|
224
|
+
stderr(`[runtime-bridge] 未知 service 子命令: ${subcommand}`);
|
|
225
|
+
printServiceUsage(stdout);
|
|
226
|
+
return { handled: true, exitCode: 1 };
|
|
227
|
+
}
|
|
25
228
|
export async function handleCliCommand(options = {}) {
|
|
26
229
|
const argv = options.argv || process.argv.slice(2);
|
|
27
230
|
const command = argv[0];
|
|
28
|
-
const stdout = options.stdout ||
|
|
29
|
-
const stderr = options.stderr ||
|
|
231
|
+
const stdout = options.stdout || defaultStdout;
|
|
232
|
+
const stderr = options.stderr || defaultStderr;
|
|
30
233
|
if (isVersionCommand(command)) {
|
|
31
234
|
stdout(`aws-runtime-bridge ${readPackageVersion(options.packageRoot)}`);
|
|
32
235
|
return { handled: true, exitCode: 0 };
|
|
@@ -34,7 +237,11 @@ export async function handleCliCommand(options = {}) {
|
|
|
34
237
|
if (command === "update") {
|
|
35
238
|
stdout("[runtime-bridge] 正在更新 aws-runtime-bridge 到最新版本...");
|
|
36
239
|
const runCommand = options.runCommand || defaultRunCommand;
|
|
37
|
-
const result = runCommand("npm", [
|
|
240
|
+
const result = runCommand("npm", [
|
|
241
|
+
"install",
|
|
242
|
+
"-g",
|
|
243
|
+
"aws-runtime-bridge@latest",
|
|
244
|
+
]);
|
|
38
245
|
if (result.error) {
|
|
39
246
|
stderr(`[runtime-bridge] 更新失败: ${result.error.message}`);
|
|
40
247
|
return { handled: true, exitCode: 1 };
|
|
@@ -51,5 +258,8 @@ export async function handleCliCommand(options = {}) {
|
|
|
51
258
|
const result = await configure();
|
|
52
259
|
return { handled: true, exitCode: result === "configured" ? 0 : 1 };
|
|
53
260
|
}
|
|
261
|
+
if (command === "service") {
|
|
262
|
+
return handleServiceCommand(argv, options, stdout, stderr);
|
|
263
|
+
}
|
|
54
264
|
return { handled: false, exitCode: 0 };
|
|
55
265
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { handleCliCommand, readPackageVersion } from "./cli-commands.js";
|
|
5
|
+
import { createSystemdUnitContent, handleCliCommand, readPackageVersion, } from "./cli-commands.js";
|
|
6
6
|
const tempRoots = [];
|
|
7
7
|
function createPackageRoot(version = "9.8.7") {
|
|
8
8
|
const root = mkdtempSync(path.join(os.tmpdir(), "aws-bridge-cli-"));
|
|
@@ -11,6 +11,14 @@ function createPackageRoot(version = "9.8.7") {
|
|
|
11
11
|
writeFileSync(path.join(root, "package.json"), JSON.stringify({ name: "aws-runtime-bridge", version }), "utf-8");
|
|
12
12
|
return root;
|
|
13
13
|
}
|
|
14
|
+
function createServiceUnitPath() {
|
|
15
|
+
const root = mkdtempSync(path.join(os.tmpdir(), "aws-bridge-service-"));
|
|
16
|
+
tempRoots.push(root);
|
|
17
|
+
return path.join(root, "systemd", "awsb.service");
|
|
18
|
+
}
|
|
19
|
+
function quotedSystemdArg(value) {
|
|
20
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
21
|
+
}
|
|
14
22
|
afterEach(() => {
|
|
15
23
|
for (const root of tempRoots.splice(0)) {
|
|
16
24
|
rmSync(root, { recursive: true, force: true });
|
|
@@ -85,6 +93,136 @@ describe("cli commands", () => {
|
|
|
85
93
|
});
|
|
86
94
|
expect(result).toEqual({ handled: true, exitCode: 1 });
|
|
87
95
|
});
|
|
96
|
+
it("creates a systemd unit that starts the current executable with setup skipped", () => {
|
|
97
|
+
const executablePath = "/opt/aws-runtime-bridge/dist/index.js";
|
|
98
|
+
const unit = createSystemdUnitContent(executablePath);
|
|
99
|
+
expect(unit).toContain("Description=AgentsWorkStudio Runtime Bridge");
|
|
100
|
+
expect(unit).toContain("Environment=AWS_BRIDGE_SKIP_SETUP=true");
|
|
101
|
+
expect(unit).toContain(`ExecStart=${quotedSystemdArg(process.execPath)} ${quotedSystemdArg(executablePath)}`);
|
|
102
|
+
expect(unit).toContain("Restart=on-failure");
|
|
103
|
+
expect(unit).toContain("WantedBy=multi-user.target");
|
|
104
|
+
});
|
|
105
|
+
it("installs service, enables autostart, and starts when inactive", async () => {
|
|
106
|
+
const output = [];
|
|
107
|
+
const commands = [];
|
|
108
|
+
const serviceUnitPath = createServiceUnitPath();
|
|
109
|
+
const executablePath = "/usr/local/bin/awsb";
|
|
110
|
+
const result = await handleCliCommand({
|
|
111
|
+
argv: ["service", "install"],
|
|
112
|
+
platform: "linux",
|
|
113
|
+
serviceUnitPath,
|
|
114
|
+
executablePath,
|
|
115
|
+
stdout: (message) => output.push(message),
|
|
116
|
+
runCommand: (command, args) => {
|
|
117
|
+
commands.push({ command, args });
|
|
118
|
+
if (args[0] === "is-active") {
|
|
119
|
+
return { status: 3 };
|
|
120
|
+
}
|
|
121
|
+
return { status: 0 };
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
expect(result).toEqual({ handled: true, exitCode: 0 });
|
|
125
|
+
expect(readFileSync(serviceUnitPath, "utf-8")).toContain(`ExecStart=${quotedSystemdArg(process.execPath)} ${quotedSystemdArg(executablePath)}`);
|
|
126
|
+
expect(commands).toEqual([
|
|
127
|
+
{ command: "systemctl", args: ["daemon-reload"] },
|
|
128
|
+
{ command: "systemctl", args: ["enable", "awsb.service"] },
|
|
129
|
+
{ command: "systemctl", args: ["is-active", "--quiet", "awsb.service"] },
|
|
130
|
+
{ command: "systemctl", args: ["start", "awsb.service"] },
|
|
131
|
+
]);
|
|
132
|
+
expect(output.at(-1)).toBe("[runtime-bridge] awsb systemd 服务已安装、已启用开机自启并已启动。");
|
|
133
|
+
});
|
|
134
|
+
it("does not start service during install when it is already active", async () => {
|
|
135
|
+
const commands = [];
|
|
136
|
+
const serviceUnitPath = createServiceUnitPath();
|
|
137
|
+
const result = await handleCliCommand({
|
|
138
|
+
argv: ["service", "install"],
|
|
139
|
+
platform: "linux",
|
|
140
|
+
serviceUnitPath,
|
|
141
|
+
executablePath: "/usr/local/bin/awsb",
|
|
142
|
+
runCommand: (command, args) => {
|
|
143
|
+
commands.push({ command, args });
|
|
144
|
+
return { status: 0 };
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
expect(result).toEqual({ handled: true, exitCode: 0 });
|
|
148
|
+
expect(commands).toEqual([
|
|
149
|
+
{ command: "systemctl", args: ["daemon-reload"] },
|
|
150
|
+
{ command: "systemctl", args: ["enable", "awsb.service"] },
|
|
151
|
+
{ command: "systemctl", args: ["is-active", "--quiet", "awsb.service"] },
|
|
152
|
+
]);
|
|
153
|
+
});
|
|
154
|
+
it("uninstalls service by stopping, disabling, removing unit, and reloading systemd", async () => {
|
|
155
|
+
const output = [];
|
|
156
|
+
const commands = [];
|
|
157
|
+
const serviceUnitPath = createServiceUnitPath();
|
|
158
|
+
mkdirSync(path.dirname(serviceUnitPath), { recursive: true });
|
|
159
|
+
writeFileSync(serviceUnitPath, "unit", "utf-8");
|
|
160
|
+
const result = await handleCliCommand({
|
|
161
|
+
argv: ["service", "uninstall"],
|
|
162
|
+
platform: "linux",
|
|
163
|
+
serviceUnitPath,
|
|
164
|
+
stdout: (message) => output.push(message),
|
|
165
|
+
runCommand: (command, args) => {
|
|
166
|
+
commands.push({ command, args });
|
|
167
|
+
return { status: 0 };
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
expect(result).toEqual({ handled: true, exitCode: 0 });
|
|
171
|
+
expect(existsSync(serviceUnitPath)).toBe(false);
|
|
172
|
+
expect(commands).toEqual([
|
|
173
|
+
{ command: "systemctl", args: ["is-active", "--quiet", "awsb.service"] },
|
|
174
|
+
{ command: "systemctl", args: ["stop", "awsb.service"] },
|
|
175
|
+
{ command: "systemctl", args: ["disable", "awsb.service"] },
|
|
176
|
+
{ command: "systemctl", args: ["daemon-reload"] },
|
|
177
|
+
]);
|
|
178
|
+
expect(output.at(-1)).toBe("[runtime-bridge] awsb systemd 服务已卸载。");
|
|
179
|
+
});
|
|
180
|
+
it("tolerates uninstall when service is inactive and unit is absent", async () => {
|
|
181
|
+
const output = [];
|
|
182
|
+
const commands = [];
|
|
183
|
+
const serviceUnitPath = createServiceUnitPath();
|
|
184
|
+
const result = await handleCliCommand({
|
|
185
|
+
argv: ["service", "uninstall"],
|
|
186
|
+
platform: "linux",
|
|
187
|
+
serviceUnitPath,
|
|
188
|
+
stdout: (message) => output.push(message),
|
|
189
|
+
runCommand: (command, args) => {
|
|
190
|
+
commands.push({ command, args });
|
|
191
|
+
if (args[0] === "is-active" || args[0] === "disable") {
|
|
192
|
+
return { status: 1 };
|
|
193
|
+
}
|
|
194
|
+
return { status: 0 };
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
expect(result).toEqual({ handled: true, exitCode: 0 });
|
|
198
|
+
expect(commands).toEqual([
|
|
199
|
+
{ command: "systemctl", args: ["is-active", "--quiet", "awsb.service"] },
|
|
200
|
+
{ command: "systemctl", args: ["disable", "awsb.service"] },
|
|
201
|
+
{ command: "systemctl", args: ["daemon-reload"] },
|
|
202
|
+
]);
|
|
203
|
+
expect(output).toContain("[runtime-bridge] awsb systemd unit 不存在,跳过删除。");
|
|
204
|
+
});
|
|
205
|
+
it("fails service commands clearly on non-linux platforms", async () => {
|
|
206
|
+
const errors = [];
|
|
207
|
+
const result = await handleCliCommand({
|
|
208
|
+
argv: ["service", "install"],
|
|
209
|
+
platform: "win32",
|
|
210
|
+
stderr: (message) => errors.push(message),
|
|
211
|
+
});
|
|
212
|
+
expect(result).toEqual({ handled: true, exitCode: 1 });
|
|
213
|
+
expect(errors).toEqual([
|
|
214
|
+
"[runtime-bridge] service 命令仅支持 Linux systemd 环境。",
|
|
215
|
+
]);
|
|
216
|
+
});
|
|
217
|
+
it("prints service help for missing subcommand", async () => {
|
|
218
|
+
const output = [];
|
|
219
|
+
const result = await handleCliCommand({
|
|
220
|
+
argv: ["service"],
|
|
221
|
+
stdout: (message) => output.push(message),
|
|
222
|
+
});
|
|
223
|
+
expect(result).toEqual({ handled: true, exitCode: 1 });
|
|
224
|
+
expect(output).toEqual(["Usage: awsb service <install|uninstall>"]);
|
|
225
|
+
});
|
|
88
226
|
it("does not handle normal startup arguments", async () => {
|
|
89
227
|
const result = await handleCliCommand({ argv: [] });
|
|
90
228
|
expect(result).toEqual({ handled: false, exitCode: 0 });
|