huaweicloud-devkit 2.0.1-next.2 → 2.0.1-next.4
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 +26 -3
- package/README.zh-CN.md +24 -1
- package/package.json +1 -1
- package/plugins/huaweicloud-core/.claude-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.codex-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.hermes-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/.mcp.json +3 -0
- package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +1 -1
- package/plugins/huaweicloud-core/dist/approval-roster.js +2 -0
- package/plugins/huaweicloud-core/dist/lib/hdkit/hdkit-gate.js +7 -0
- package/plugins/huaweicloud-core/dist/lib/hdkit/hdkitservice-api.js +30 -1
- package/plugins/huaweicloud-core/dist/lib/hdkit/sandbox-pool.js +21 -0
- package/plugins/huaweicloud-core/dist/mcp-server.js +4227 -3049
- package/plugins/huaweicloud-core/dist/mcp-server.js.map +4 -4
- package/plugins/huaweicloud-core/dist/packs/registry.js +1 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/approval.js +13 -2
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-account.js +198 -10
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-credentials.js +11 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-session.js +22 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-tunnel.js +46 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-upload.js +11 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/schemas.js +20 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/tools.js +26 -18
- package/plugins/huaweicloud-core/dist/sandbox/deploy-check.js +97 -0
- package/plugins/huaweicloud-core/dist/sandbox/deploy-nginx.js +30 -14
- package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-backend.js +37 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-connections.js +84 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-envd-client.js +229 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-envd-protocol.js +151 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-envd-transport.js +121 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-service-api.js +80 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-upload.js +106 -0
- package/plugins/huaweicloud-core/dist/sandbox/hwlink-connection.js +12 -0
- package/plugins/huaweicloud-core/dist/sandbox/upload-project.js +5 -2
- package/plugins/huaweicloud-core/dist/server-install.js +363 -0
- package/plugins/huaweicloud-core/dist/setup-cli.js +16 -0
- package/plugins/huaweicloud-core/dist/tool-definitions.js +4 -0
- package/plugins/huaweicloud-core/dist/tools.js +1 -0
- package/plugins/huaweicloud-core/openclaw.plugin.json +1 -1
- package/plugins/huaweicloud-core/safety/policy.json +2 -1
- package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +67 -16
- package/plugins/huaweicloud-core/skills/huawei-sandbox/references/devbox-backend.md +78 -0
- package/plugins/huaweicloud-core/skills/huawei-sandbox/references/resource-pool.md +14 -0
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +2 -1
- package/plugins/huaweicloud-core/src/approval-roster.ts +2 -0
- package/plugins/huaweicloud-core/src/lib/hdkit/hdkit-gate.ts +7 -0
- package/plugins/huaweicloud-core/src/lib/hdkit/hdkitservice-api.ts +30 -1
- package/plugins/huaweicloud-core/src/lib/hdkit/sandbox-pool.ts +22 -0
- package/plugins/huaweicloud-core/src/mcp-server-remote.ts +68 -5
- package/plugins/huaweicloud-core/src/mcp-server.ts +4 -1
- package/plugins/huaweicloud-core/src/packs/registry.ts +1 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/approval.ts +15 -2
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-account.ts +206 -11
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-credentials.ts +12 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-session.ts +23 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-tunnel.ts +49 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-upload.ts +11 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/schemas.ts +23 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/tools.ts +27 -17
- package/plugins/huaweicloud-core/src/sandbox/deploy-check.ts +105 -0
- package/plugins/huaweicloud-core/src/sandbox/deploy-nginx.ts +36 -18
- package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-backend.ts +44 -0
- package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-connections.ts +115 -0
- package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-envd-client.ts +281 -0
- package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-envd-protocol.ts +180 -0
- package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-envd-transport.ts +142 -0
- package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-service-api.ts +102 -0
- package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-upload.ts +167 -0
- package/plugins/huaweicloud-core/src/sandbox/hwlink-connection.ts +12 -0
- package/plugins/huaweicloud-core/src/sandbox/upload-project.ts +5 -2
- package/plugins/huaweicloud-core/src/server-install.ts +442 -0
- package/plugins/huaweicloud-core/src/setup-cli.ts +16 -0
- package/plugins/huaweicloud-core/src/tool-definitions.ts +4 -0
- package/plugins/huaweicloud-core/src/tools.ts +3 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// DevBox upload implementations: EnvD /files binary writes, md5 verify, and
|
|
2
|
+
// tar extraction for projects. No session, no HTTP tunnel, no file server.
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { readFileSync, rmSync, statSync } from 'node:fs';
|
|
5
|
+
import { dirname as posixDirname, posix } from 'node:path';
|
|
6
|
+
import { classifyLocalUploadPath } from "../upload-file.js";
|
|
7
|
+
import { computeMd5, createTarGz } from "../upload-project.js";
|
|
8
|
+
import { execDevboxOneShot, writeDevboxFile } from "./devbox-envd-client.js";
|
|
9
|
+
import { withDevboxConnection } from "./devbox-service-api.js";
|
|
10
|
+
function shellQuote(value) {
|
|
11
|
+
return `'${value.replaceAll("'", `'\\''`)}'`;
|
|
12
|
+
}
|
|
13
|
+
function remoteParent(remotePath) {
|
|
14
|
+
const parent = posixDirname(remotePath);
|
|
15
|
+
return parent && parent !== '.' ? parent : '/';
|
|
16
|
+
}
|
|
17
|
+
async function verifiedRemoteMd5(workspaceId, remotePath, expectedMd5, timeoutMs) {
|
|
18
|
+
return withDevboxConnection(workspaceId, async (connection) => {
|
|
19
|
+
const result = await execDevboxOneShot(connection, `md5sum ${shellQuote(remotePath)} | awk '{print $1}'`, Math.min(timeoutMs, 60000));
|
|
20
|
+
return result.stdout.trim() === expectedMd5;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export async function uploadFileDevbox(workspaceId, localPath, remotePath, timeoutMs = 120000) {
|
|
24
|
+
const localError = classifyLocalUploadPath(localPath, 'file');
|
|
25
|
+
if (localError)
|
|
26
|
+
throw localError;
|
|
27
|
+
if (!remotePath.startsWith('/'))
|
|
28
|
+
throw new Error('sandbox upload: remote_path must be an absolute path.');
|
|
29
|
+
const data = readFileSync(localPath);
|
|
30
|
+
const md5 = createHash('md5').update(data).digest('hex');
|
|
31
|
+
await withDevboxConnection(workspaceId, async (connection) => {
|
|
32
|
+
await execDevboxOneShot(connection, `mkdir -p ${shellQuote(remoteParent(remotePath))}`, Math.min(timeoutMs, 30000));
|
|
33
|
+
await writeDevboxFile(connection, remotePath, data, timeoutMs);
|
|
34
|
+
});
|
|
35
|
+
const md5Verified = await verifiedRemoteMd5(workspaceId, remotePath, md5, timeoutMs);
|
|
36
|
+
if (!md5Verified) {
|
|
37
|
+
throw new Error(`sandbox upload: md5 mismatch after writing ${remotePath}.`);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
ok: true,
|
|
41
|
+
localPath,
|
|
42
|
+
remotePath,
|
|
43
|
+
bytes: data.length,
|
|
44
|
+
chunks: 1,
|
|
45
|
+
md5,
|
|
46
|
+
md5Verified,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export async function uploadProjectDevbox(workspaceId, localDir, remoteDir, timeoutMs = 300000, options = {}) {
|
|
50
|
+
const localError = classifyLocalUploadPath(localDir, 'directory');
|
|
51
|
+
if (localError)
|
|
52
|
+
throw localError;
|
|
53
|
+
const projectName = posix.basename(localDir);
|
|
54
|
+
const targetParentDir = remoteDir || '/workspace';
|
|
55
|
+
const archiveRemotePath = `${targetParentDir}/${projectName}.tar.gz`;
|
|
56
|
+
const archivePath = await createTarGz(localDir, options.exclude);
|
|
57
|
+
try {
|
|
58
|
+
const archiveSize = statSync(archivePath).size;
|
|
59
|
+
const expectedMd5 = await computeMd5(archivePath);
|
|
60
|
+
const archiveBytes = readFileSync(archivePath);
|
|
61
|
+
await withDevboxConnection(workspaceId, async (connection) => {
|
|
62
|
+
await execDevboxOneShot(connection, `mkdir -p ${shellQuote(targetParentDir)}`, Math.min(timeoutMs, 30000));
|
|
63
|
+
await writeDevboxFile(connection, archiveRemotePath, archiveBytes, timeoutMs);
|
|
64
|
+
});
|
|
65
|
+
const md5Verified = await verifiedRemoteMd5(workspaceId, archiveRemotePath, expectedMd5, timeoutMs);
|
|
66
|
+
if (options.verify !== false && !md5Verified) {
|
|
67
|
+
throw new Error(`sandbox upload project: md5 mismatch after writing ${archiveRemotePath}.`);
|
|
68
|
+
}
|
|
69
|
+
if (options.extract !== false) {
|
|
70
|
+
await withDevboxConnection(workspaceId, async (connection) => {
|
|
71
|
+
const extract = await execDevboxOneShot(connection, `mkdir -p ${shellQuote(targetParentDir)} && tar -xzf ${shellQuote(archiveRemotePath)} -C ` +
|
|
72
|
+
`${shellQuote(targetParentDir)} && rm -f ${shellQuote(archiveRemotePath)}`, timeoutMs);
|
|
73
|
+
if (extract.exitCode !== 0) {
|
|
74
|
+
throw new Error(`sandbox upload project: tar extraction failed (exit ${extract.exitCode}): ${extract.stderr.trim()}`);
|
|
75
|
+
}
|
|
76
|
+
// Best-effort traverse-permission hardening for later nginx serving.
|
|
77
|
+
await execDevboxOneShot(connection, [
|
|
78
|
+
`REAL_PATH=$(readlink -f ${shellQuote(`${targetParentDir}/${projectName}`)} 2>/dev/null || echo ${shellQuote(`${targetParentDir}/${projectName}`)})`,
|
|
79
|
+
`if [ ! -d "$REAL_PATH" ] && [ -d ${shellQuote(targetParentDir)} ]; then`,
|
|
80
|
+
` REAL_PATH=${shellQuote(`${targetParentDir}/${projectName}`)}`,
|
|
81
|
+
`fi`,
|
|
82
|
+
`chmod -R o+rX "$REAL_PATH" 2>/dev/null || true`,
|
|
83
|
+
`find "$REAL_PATH" -type d -exec chmod o+x {} \\; 2>/dev/null || true`,
|
|
84
|
+
`find "$REAL_PATH" -type f -path "*/node_modules/.bin/*" -exec chmod +x {} \\; 2>/dev/null || true`,
|
|
85
|
+
].join('\n'), Math.min(timeoutMs, 15000)).catch(() => undefined);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
ok: true,
|
|
90
|
+
localDir,
|
|
91
|
+
remotePath: options.extract !== false ? `${targetParentDir}/${projectName}` : archiveRemotePath,
|
|
92
|
+
bytes: archiveSize,
|
|
93
|
+
md5: expectedMd5,
|
|
94
|
+
md5Verified,
|
|
95
|
+
extracted: options.extract !== false,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
try {
|
|
100
|
+
rmSync(posixDirname(archivePath), { recursive: true, force: true });
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// best-effort local temp cleanup
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
import { createConnection, getCredentials } from "../lib/hdkit/hwlink-api.js";
|
|
11
11
|
import { getWebSocketImpl } from "../proxy/proxy-agent.js";
|
|
12
12
|
import { trackSandboxConnect, trackSandboxDisconnect } from "../telemetry/telemetry.js";
|
|
13
|
+
import { backendForWorkspace } from "./devbox/devbox-backend.js";
|
|
14
|
+
import { execDevboxOneShot } from "./devbox/devbox-envd-client.js";
|
|
15
|
+
import { withDevboxConnection } from "./devbox/devbox-service-api.js";
|
|
13
16
|
import { loadWsExec, } from "./ws-exec-loader.js";
|
|
14
17
|
let currentWorkspaceId = process.env.HW_WORKSPACE_ID || null;
|
|
15
18
|
export function getCurrentWorkspaceId() {
|
|
@@ -103,6 +106,9 @@ export async function createTunnelSession(workspaceId, username, timeoutMs = 300
|
|
|
103
106
|
return { mux, close: () => mux.close() };
|
|
104
107
|
}
|
|
105
108
|
export async function execOneShot(workspaceId, command, username, timeoutMs) {
|
|
109
|
+
if (backendForWorkspace(workspaceId) === 'devbox') {
|
|
110
|
+
return withDevboxConnection(workspaceId, (connection) => execDevboxOneShot(connection, command, timeoutMs));
|
|
111
|
+
}
|
|
106
112
|
const { wsUrl, source, WebSocketImpl, wsExec } = await openHwlinkConnection(workspaceId);
|
|
107
113
|
return await wsExec.executeHwlinkCommand({
|
|
108
114
|
url: wsUrl,
|
|
@@ -114,6 +120,12 @@ export async function execOneShot(workspaceId, command, username, timeoutMs) {
|
|
|
114
120
|
});
|
|
115
121
|
}
|
|
116
122
|
export async function execWithSession(workspaceId, command, username, timeoutMs) {
|
|
123
|
+
// Devbox v1 has no persistent PTY: the tool face degrades to one-shot and
|
|
124
|
+
// marks the response (degraded/statePersisted:false). Commands must be
|
|
125
|
+
// self-contained; no shell state survives between calls.
|
|
126
|
+
if (backendForWorkspace(workspaceId) === 'devbox') {
|
|
127
|
+
return execOneShot(workspaceId, command, username, timeoutMs ?? 60000);
|
|
128
|
+
}
|
|
117
129
|
const session = await getSession(workspaceId, username, timeoutMs);
|
|
118
130
|
return await session.exec(command, { timeoutMs });
|
|
119
131
|
}
|
|
@@ -31,7 +31,10 @@ function errorLabel(error) {
|
|
|
31
31
|
return error.name;
|
|
32
32
|
return 'unknown';
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
// Exported for the devbox upload path (src/sandbox/devbox/devbox-upload.ts):
|
|
35
|
+
// the archive recipe (git archive vs tar + exclude expansion) is shared so
|
|
36
|
+
// the two backends produce identical payloads.
|
|
37
|
+
export async function createTarGz(localDir, exclude = []) {
|
|
35
38
|
const archiveName = `${basename(localDir)}.tar.gz`;
|
|
36
39
|
const archiveDir = join(tmpdir(), `sandbox-upload-${Date.now()}`);
|
|
37
40
|
mkdirSync(archiveDir, { recursive: true });
|
|
@@ -67,7 +70,7 @@ async function createTarGz(localDir, exclude = []) {
|
|
|
67
70
|
}
|
|
68
71
|
return archivePath;
|
|
69
72
|
}
|
|
70
|
-
async function computeMd5(filePath) {
|
|
73
|
+
export async function computeMd5(filePath) {
|
|
71
74
|
return new Promise((resolve, reject) => {
|
|
72
75
|
const hash = createHash('md5');
|
|
73
76
|
createReadStream(filePath)
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeSync } from 'node:fs';
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { randomInt } from 'node:crypto';
|
|
4
|
+
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Deterministic defaults (contest/ops alignment: fixed service names, fixed
|
|
8
|
+
// user, fixed port pair so graders and scripts can discover them). These
|
|
9
|
+
// mirror the v1 installer's values one-for-one so one grading script covers
|
|
10
|
+
// both release lines.
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
export const DEFAULT_SERVER_INSTALL_PATH = '/opt/kunpeng';
|
|
13
|
+
export const DEFAULT_BUSINESS_PORT = 8088;
|
|
14
|
+
export const DEFAULT_TOOL_PORT = 7088;
|
|
15
|
+
export const DEFAULT_SERVER_USER = 'devkit';
|
|
16
|
+
export const BUSINESS_SERVICE_NAME = 'huaweicloud-devkit.service';
|
|
17
|
+
export const TOOL_SERVICE_NAME = 'huaweicloud-devkit-tool.service';
|
|
18
|
+
export const NODE_VERSION = '22.19.0';
|
|
19
|
+
const NODE_MIRROR = 'https://mirrors.huaweicloud.com/nodejs';
|
|
20
|
+
// The running module may be an individually compiled dist file (setup-cli
|
|
21
|
+
// loads it as dist/server-install.js) or inlined inside the bundled server
|
|
22
|
+
// entry. Walking up to the nearest package.json *named like this package*
|
|
23
|
+
// is robust against both layouts; the v1 installer's fixed three-levels-up
|
|
24
|
+
// resolve shipped a one-level-off copy bug before it was patched, so the
|
|
25
|
+
// walk is also the validation.
|
|
26
|
+
function resolvePackageRoot(startDir) {
|
|
27
|
+
let current = startDir;
|
|
28
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
29
|
+
const manifest = join(current, 'package.json');
|
|
30
|
+
if (existsSync(manifest)) {
|
|
31
|
+
let name;
|
|
32
|
+
try {
|
|
33
|
+
({ name } = JSON.parse(readFileSync(manifest, 'utf8')));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
throw new Error(`unreadable package manifest while locating the install root: ${manifest}`);
|
|
37
|
+
}
|
|
38
|
+
if (name === 'huaweicloud-devkit')
|
|
39
|
+
return current;
|
|
40
|
+
}
|
|
41
|
+
const parent = dirname(current);
|
|
42
|
+
if (parent === current)
|
|
43
|
+
break;
|
|
44
|
+
current = parent;
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`could not locate the huaweicloud-devkit package root above ${startDir}`);
|
|
47
|
+
}
|
|
48
|
+
const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
|
|
49
|
+
const PACKAGE_ROOT = resolvePackageRoot(MODULE_DIR);
|
|
50
|
+
export function parseServerInstallArgs(argv = []) {
|
|
51
|
+
const opts = {
|
|
52
|
+
installPath: DEFAULT_SERVER_INSTALL_PATH,
|
|
53
|
+
port: DEFAULT_BUSINESS_PORT,
|
|
54
|
+
toolPort: DEFAULT_TOOL_PORT,
|
|
55
|
+
user: DEFAULT_SERVER_USER,
|
|
56
|
+
rotatePassword: false,
|
|
57
|
+
removeUser: false,
|
|
58
|
+
};
|
|
59
|
+
for (let i = 0; i < argv.length; i++) {
|
|
60
|
+
const arg = argv[i];
|
|
61
|
+
if (arg === undefined)
|
|
62
|
+
break;
|
|
63
|
+
const value = () => {
|
|
64
|
+
const next = argv[i + 1];
|
|
65
|
+
if (!next || next.startsWith('--')) {
|
|
66
|
+
throw new Error(`Missing value for ${arg}`);
|
|
67
|
+
}
|
|
68
|
+
i += 1;
|
|
69
|
+
return next;
|
|
70
|
+
};
|
|
71
|
+
if (arg === '--install-path')
|
|
72
|
+
opts.installPath = value();
|
|
73
|
+
else if (arg === '--port')
|
|
74
|
+
opts.port = Number(value());
|
|
75
|
+
else if (arg === '--tool-port')
|
|
76
|
+
opts.toolPort = Number(value());
|
|
77
|
+
else if (arg === '--user')
|
|
78
|
+
opts.user = value();
|
|
79
|
+
else if (arg === '--rotate-password')
|
|
80
|
+
opts.rotatePassword = true;
|
|
81
|
+
else if (arg === '--remove-user')
|
|
82
|
+
opts.removeUser = true;
|
|
83
|
+
else
|
|
84
|
+
throw new Error(`Unknown option for install-server: ${arg}`);
|
|
85
|
+
}
|
|
86
|
+
if (!Number.isSafeInteger(opts.port) || opts.port < 1024 || opts.port > 65535) {
|
|
87
|
+
throw new Error(`Invalid --port: ${opts.port}`);
|
|
88
|
+
}
|
|
89
|
+
if (!Number.isSafeInteger(opts.toolPort) || opts.toolPort < 1024 || opts.toolPort > 65535) {
|
|
90
|
+
throw new Error(`Invalid --tool-port: ${opts.toolPort}`);
|
|
91
|
+
}
|
|
92
|
+
if (opts.port === opts.toolPort) {
|
|
93
|
+
throw new Error('--port and --tool-port must differ');
|
|
94
|
+
}
|
|
95
|
+
if (!/^[a-z_][a-z0-9_-]{0,31}$/.test(opts.user)) {
|
|
96
|
+
throw new Error(`Invalid --user: ${opts.user}`);
|
|
97
|
+
}
|
|
98
|
+
return opts;
|
|
99
|
+
}
|
|
100
|
+
// Deconfusable alphabet (no 0/O/1/I/l): the password is transcribed by
|
|
101
|
+
// humans from a headless server terminal exactly once, at install time.
|
|
102
|
+
// randomInt samples each character uniformly — a modulo of randomBytes
|
|
103
|
+
// would bias the first two alphabet entries (256 % 56 != 0).
|
|
104
|
+
export function randomPassword(length = 24) {
|
|
105
|
+
const alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';
|
|
106
|
+
let out = '';
|
|
107
|
+
for (let i = 0; i < length; i++)
|
|
108
|
+
out += alphabet[randomInt(0, alphabet.length)];
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
export function renderSystemdUnit({ serviceName, description, nodePath, appEntry, port, user, authPasswordFile, unitDir = '/etc/systemd/system', }) {
|
|
112
|
+
const unit = [
|
|
113
|
+
'[Unit]',
|
|
114
|
+
`Description=${description}`,
|
|
115
|
+
'After=network.target',
|
|
116
|
+
'',
|
|
117
|
+
'[Service]',
|
|
118
|
+
'Type=simple',
|
|
119
|
+
`ExecStart=${nodePath} ${appEntry} --transport remote --port ${port} --host 0.0.0.0 --auth-password-file ${authPasswordFile}`,
|
|
120
|
+
'Restart=on-failure',
|
|
121
|
+
'RestartSec=3',
|
|
122
|
+
`User=${user}`,
|
|
123
|
+
'',
|
|
124
|
+
'[Install]',
|
|
125
|
+
'WantedBy=multi-user.target',
|
|
126
|
+
'',
|
|
127
|
+
].join('\n');
|
|
128
|
+
return {
|
|
129
|
+
unitDir: unitDir.endsWith('/') ? unitDir : `${unitDir}/`,
|
|
130
|
+
unitPath: `${unitDir}/${serviceName}`,
|
|
131
|
+
unit,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
export function nodeArchiveUrl(version, arch = process.arch) {
|
|
135
|
+
const flat = arch === 'arm64' || arch === 'aarch64' ? 'arm64' : 'x64';
|
|
136
|
+
return `${NODE_MIRROR}/v${version}/node-v${version}-linux-${flat}.tar.xz`;
|
|
137
|
+
}
|
|
138
|
+
export function isSupportedPlatform(platform = process.platform) {
|
|
139
|
+
return platform === 'linux';
|
|
140
|
+
}
|
|
141
|
+
function run(cmd, args) {
|
|
142
|
+
const res = spawnSync(cmd, args, { encoding: 'utf8' });
|
|
143
|
+
const out = (res.stdout ?? '').trim();
|
|
144
|
+
const err = (res.stderr ?? '').trim();
|
|
145
|
+
return { code: res.status, out, err, ok: res.status === 0 };
|
|
146
|
+
}
|
|
147
|
+
function runOrDie(cmd, args, what) {
|
|
148
|
+
const res = run(cmd, args);
|
|
149
|
+
if (!res.ok) {
|
|
150
|
+
throw new Error(`${what} failed: ${cmd} ${args.join(' ')}\n${res.err || res.out}`);
|
|
151
|
+
}
|
|
152
|
+
return res;
|
|
153
|
+
}
|
|
154
|
+
function writeFileOwner(path, content, owner, mode = 0o644) {
|
|
155
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
156
|
+
const fd = openSync(path, 'w', mode);
|
|
157
|
+
writeSync(fd, content);
|
|
158
|
+
closeSync(fd);
|
|
159
|
+
chmodSync(path, mode);
|
|
160
|
+
if (owner)
|
|
161
|
+
runOrDie('chown', [owner, path], `chown ${path}`);
|
|
162
|
+
}
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
// Steps
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
function ensureRoot() {
|
|
167
|
+
if (typeof process.getuid === 'function' && process.getuid() !== 0) {
|
|
168
|
+
throw new Error('install-server must run as root (needs user/group creation and systemd)');
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function ensureLinux() {
|
|
172
|
+
if (!isSupportedPlatform()) {
|
|
173
|
+
throw new Error('install-server is Linux-only today (systemd required). Use `install` to configure coding agents.');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function ensureUser(user) {
|
|
177
|
+
const check = run('id', ['-u', user]);
|
|
178
|
+
if (check.ok)
|
|
179
|
+
return;
|
|
180
|
+
const nologin = ['/usr/sbin/nologin', '/sbin/nologin'].find((p) => existsSync(p)) || '/bin/false';
|
|
181
|
+
runOrDie('useradd', ['--system', '--no-create-home', '--shell', nologin, user], `create system user ${user}`);
|
|
182
|
+
}
|
|
183
|
+
function resolveSystemNode() {
|
|
184
|
+
const res = run('node', ['-v']);
|
|
185
|
+
if (res.ok) {
|
|
186
|
+
const major = Number.parseInt(res.out.replace(/^v/, ''), 10);
|
|
187
|
+
if (Number.isSafeInteger(major) && major >= 22) {
|
|
188
|
+
const where = run('bash', ['-c', 'command -v node']);
|
|
189
|
+
// An npx-shim node dies with the cache; the service must not pin it.
|
|
190
|
+
if (where.ok && where.out && !where.out.includes('_npx'))
|
|
191
|
+
return where.out.trim();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
function provisionNode(installPath) {
|
|
197
|
+
const target = join(installPath, 'node');
|
|
198
|
+
const bin = join(target, 'bin', 'node');
|
|
199
|
+
if (existsSync(bin))
|
|
200
|
+
return bin;
|
|
201
|
+
const url = nodeArchiveUrl(NODE_VERSION);
|
|
202
|
+
mkdirSync(installPath, { recursive: true });
|
|
203
|
+
console.log(` downloading Node.js v${NODE_VERSION} from ${url}`);
|
|
204
|
+
runOrDie('curl', ['-sL', '--retry', '3', '-o', join(installPath, 'node.tar.xz'), url], 'download Node.js');
|
|
205
|
+
runOrDie('mkdir', ['-p', target], 'mkdir node dir');
|
|
206
|
+
runOrDie('tar', ['-xJf', join(installPath, 'node.tar.xz'), '-C', target, '--strip-components', '1'], 'extract Node.js');
|
|
207
|
+
rmSync(join(installPath, 'node.tar.xz'), { force: true });
|
|
208
|
+
return bin;
|
|
209
|
+
}
|
|
210
|
+
function installAppDir(installPath, user) {
|
|
211
|
+
const app = join(installPath, 'devkit', 'app');
|
|
212
|
+
rmSync(app, { recursive: true, force: true });
|
|
213
|
+
mkdirSync(app, { recursive: true });
|
|
214
|
+
// Copy the running package (bin/ + plugins/ + package.json + node_modules
|
|
215
|
+
// when present) so the service is self-contained. Direct argv, no shell:
|
|
216
|
+
// every path component stays a literal argument.
|
|
217
|
+
const res = run('cp', ['-a', `${PACKAGE_ROOT}/.`, `${app}/`]);
|
|
218
|
+
if (!res.ok) {
|
|
219
|
+
throw new Error(`copy package to ${app} failed: ${res.err || res.out}`);
|
|
220
|
+
}
|
|
221
|
+
rmSync(join(app, '.git'), { recursive: true, force: true });
|
|
222
|
+
runOrDie('chown', ['-R', `${user}:${user}`, join(installPath, 'devkit')], 'chown app dir');
|
|
223
|
+
chmodSync(app, 0o750);
|
|
224
|
+
return app;
|
|
225
|
+
}
|
|
226
|
+
function writePasswordFile(installPath, user, rotate) {
|
|
227
|
+
const pwFile = join(installPath, 'devkit', 'server', '.auth-password');
|
|
228
|
+
if (existsSync(pwFile) && !rotate) {
|
|
229
|
+
return pwFile;
|
|
230
|
+
}
|
|
231
|
+
const password = randomPassword(24);
|
|
232
|
+
writeFileOwner(pwFile, `${password}\n`, user, 0o600);
|
|
233
|
+
return pwFile;
|
|
234
|
+
}
|
|
235
|
+
function writeUnits(installPath, params) {
|
|
236
|
+
// The dist bundle, not the TypeScript sources: the remote transport ships
|
|
237
|
+
// only inside the bundled entry (the individual mcp-server-remote.js is
|
|
238
|
+
// removed from dist by the build), so this is the one runnable path — the
|
|
239
|
+
// same file the `huaweicloud-devkit-mcp` bin resolves to.
|
|
240
|
+
const entry = join(params.app, 'plugins', 'huaweicloud-core', 'dist', 'mcp-server.js');
|
|
241
|
+
const business = renderSystemdUnit({
|
|
242
|
+
serviceName: BUSINESS_SERVICE_NAME,
|
|
243
|
+
description: 'HuaweiCloud DevKit MCP server (business port)',
|
|
244
|
+
nodePath: params.nodePath,
|
|
245
|
+
appEntry: entry,
|
|
246
|
+
port: params.port,
|
|
247
|
+
user: params.user,
|
|
248
|
+
authPasswordFile: params.pwFile,
|
|
249
|
+
});
|
|
250
|
+
const tool = renderSystemdUnit({
|
|
251
|
+
serviceName: TOOL_SERVICE_NAME,
|
|
252
|
+
description: 'HuaweiCloud DevKit MCP server (tool port)',
|
|
253
|
+
nodePath: params.nodePath,
|
|
254
|
+
appEntry: entry,
|
|
255
|
+
port: params.toolPort,
|
|
256
|
+
user: params.user,
|
|
257
|
+
authPasswordFile: params.pwFile,
|
|
258
|
+
});
|
|
259
|
+
writeFileOwner(business.unitPath, business.unit, 'root', 0o644);
|
|
260
|
+
writeFileOwner(tool.unitPath, tool.unit, 'root', 0o644);
|
|
261
|
+
runOrDie('systemctl', ['daemon-reload'], 'systemd daemon-reload');
|
|
262
|
+
}
|
|
263
|
+
export function planServiceCommands() {
|
|
264
|
+
return [
|
|
265
|
+
{
|
|
266
|
+
cmd: 'systemctl',
|
|
267
|
+
args: ['enable', BUSINESS_SERVICE_NAME, TOOL_SERVICE_NAME],
|
|
268
|
+
label: 'enable services',
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
cmd: 'systemctl',
|
|
272
|
+
args: ['restart', BUSINESS_SERVICE_NAME, TOOL_SERVICE_NAME],
|
|
273
|
+
label: 'restart services',
|
|
274
|
+
},
|
|
275
|
+
];
|
|
276
|
+
}
|
|
277
|
+
// One joint restart call removes the serial-restart failure window (the
|
|
278
|
+
// first restart succeeding and the second erroring, leaving one unit
|
|
279
|
+
// restarted and the other untouched). It is not a transaction: systemd
|
|
280
|
+
// enqueues the two units' jobs independently, so a mid-call failure can
|
|
281
|
+
// still land between them — but any failure aborts the install at
|
|
282
|
+
// runOrDie before "install complete" is ever printed.
|
|
283
|
+
function startServices() {
|
|
284
|
+
for (const { cmd, args, label } of planServiceCommands()) {
|
|
285
|
+
runOrDie(cmd, args, label);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function waitHealthy(port, timeoutMs = 15000) {
|
|
289
|
+
const deadline = Date.now() + timeoutMs;
|
|
290
|
+
while (Date.now() < deadline) {
|
|
291
|
+
const res = run('curl', ['-fsS', `http://127.0.0.1:${port}/health`]);
|
|
292
|
+
if (res.ok && res.out.includes('"ok"'))
|
|
293
|
+
return true;
|
|
294
|
+
const sleep = run('sleep', ['1']);
|
|
295
|
+
if (!sleep.ok)
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
// The installer never mutates firewall state — punching network holes is a
|
|
301
|
+
// security decision the operator makes knowingly. When firewalld is active
|
|
302
|
+
// we print the exact commands so the step is copy-paste instead of research.
|
|
303
|
+
function printFirewallHint(port, toolPort) {
|
|
304
|
+
const state = run('firewall-cmd', ['--state']);
|
|
305
|
+
if (state.ok && state.out === 'running') {
|
|
306
|
+
console.log('\nFirewalld is active on this host; the installer did not change firewall rules.');
|
|
307
|
+
console.log('Open the service ports yourself if external access is intended:');
|
|
308
|
+
console.log(` firewall-cmd --permanent --add-port=${port}/tcp`);
|
|
309
|
+
console.log(` firewall-cmd --permanent --add-port=${toolPort}/tcp`);
|
|
310
|
+
console.log(' firewall-cmd --reload');
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
console.log('\nNote: firewall rules were not changed. If external clients will connect, open the service ports per your distro (firewalld/ufw/iptables).');
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// ---------------------------------------------------------------------------
|
|
317
|
+
// CLI entry points
|
|
318
|
+
// ---------------------------------------------------------------------------
|
|
319
|
+
export async function installServerCli(argv = []) {
|
|
320
|
+
ensureLinux();
|
|
321
|
+
ensureRoot();
|
|
322
|
+
const opts = parseServerInstallArgs(argv);
|
|
323
|
+
const installPath = resolve(opts.installPath);
|
|
324
|
+
console.log(`Installing HuaweiCloud DevKit server under ${installPath}`);
|
|
325
|
+
console.log(` business port: ${opts.port} tool port: ${opts.toolPort} service user: ${opts.user}`);
|
|
326
|
+
ensureUser(opts.user);
|
|
327
|
+
const nodePath = resolveSystemNode() || provisionNode(installPath);
|
|
328
|
+
const app = installAppDir(installPath, opts.user);
|
|
329
|
+
const pwFile = writePasswordFile(installPath, opts.user, opts.rotatePassword);
|
|
330
|
+
writeUnits(installPath, { nodePath, app, port: opts.port, toolPort: opts.toolPort, user: opts.user, pwFile });
|
|
331
|
+
startServices();
|
|
332
|
+
const okBusiness = waitHealthy(opts.port);
|
|
333
|
+
const okTool = waitHealthy(opts.toolPort);
|
|
334
|
+
if (!okBusiness || !okTool) {
|
|
335
|
+
const failed = !okBusiness ? BUSINESS_SERVICE_NAME : TOOL_SERVICE_NAME;
|
|
336
|
+
console.log(` warning: ${failed} did not report healthy yet; inspect with: journalctl -u ${failed} -n 50`);
|
|
337
|
+
}
|
|
338
|
+
console.log('\nServer install complete:');
|
|
339
|
+
console.log(` install path : ${installPath}/devkit/app`);
|
|
340
|
+
console.log(` service user : ${opts.user}`);
|
|
341
|
+
console.log(` business svc : ${BUSINESS_SERVICE_NAME} -> 0.0.0.0:${opts.port}`);
|
|
342
|
+
console.log(` tool svc : ${TOOL_SERVICE_NAME} -> 0.0.0.0:${opts.toolPort}`);
|
|
343
|
+
console.log(` auth password: ${pwFile} (0600; header: Authorization: Bearer <password> or x-devkit-password)`);
|
|
344
|
+
console.log(' health check : GET http://127.0.0.1:<port>/health');
|
|
345
|
+
printFirewallHint(opts.port, opts.toolPort);
|
|
346
|
+
}
|
|
347
|
+
export async function uninstallServerCli(argv = []) {
|
|
348
|
+
ensureLinux();
|
|
349
|
+
ensureRoot();
|
|
350
|
+
const opts = parseServerInstallArgs(argv);
|
|
351
|
+
run('systemctl', ['disable', '--now', BUSINESS_SERVICE_NAME]);
|
|
352
|
+
run('systemctl', ['disable', '--now', TOOL_SERVICE_NAME]);
|
|
353
|
+
for (const unit of [BUSINESS_SERVICE_NAME, TOOL_SERVICE_NAME]) {
|
|
354
|
+
rmSync(`/etc/systemd/system/${unit}`, { force: true });
|
|
355
|
+
}
|
|
356
|
+
run('systemctl', ['daemon-reload']);
|
|
357
|
+
rmSync(join(opts.installPath, 'devkit'), { recursive: true, force: true });
|
|
358
|
+
if (opts.removeUser) {
|
|
359
|
+
run('userdel', [opts.user]);
|
|
360
|
+
console.log(`removed service user: ${opts.user}`);
|
|
361
|
+
}
|
|
362
|
+
console.log('HuaweiCloud DevKit server uninstalled.');
|
|
363
|
+
}
|
|
@@ -15,6 +15,7 @@ import { removeKooCli, removeObsConfig } from "./sandbox/uninstall-cleanup.js";
|
|
|
15
15
|
import { readReceipt, removeReceipt } from "./dep/receipts.js";
|
|
16
16
|
import { runSecretCommand } from "./lib/secret-vault/cli.js";
|
|
17
17
|
import { createProcessApproveIo, runApproveCommand, runPinCommand } from "./approvals-cli.js";
|
|
18
|
+
import { installServerCli, uninstallServerCli } from "./server-install.js";
|
|
18
19
|
import { readConsentEvents } from "./approval-ledger.js";
|
|
19
20
|
import { custodyReport } from "./lib/secret-vault/custody.js";
|
|
20
21
|
import { vaultList } from "./lib/secret-vault/store.js";
|
|
@@ -1323,6 +1324,12 @@ async function main() {
|
|
|
1323
1324
|
case 'install-hcloud':
|
|
1324
1325
|
await cmdInstallHcloud();
|
|
1325
1326
|
break;
|
|
1327
|
+
case 'install-server':
|
|
1328
|
+
await installServerCli(process.argv.slice(3));
|
|
1329
|
+
break;
|
|
1330
|
+
case 'uninstall-server':
|
|
1331
|
+
await uninstallServerCli(process.argv.slice(3));
|
|
1332
|
+
break;
|
|
1326
1333
|
case 'dep':
|
|
1327
1334
|
await cmdDep();
|
|
1328
1335
|
break;
|
|
@@ -1364,6 +1371,8 @@ async function main() {
|
|
|
1364
1371
|
console.log(' status Show installation status');
|
|
1365
1372
|
console.log(' doctor Self-check: hcloud, MCP, skills, auth');
|
|
1366
1373
|
console.log(' install-hcloud Show KooCLI install commands for your OS');
|
|
1374
|
+
console.log(' install-server Server mode: systemd services + dedicated user + auth password (Linux root)');
|
|
1375
|
+
console.log(' uninstall-server Remove server-mode services and files');
|
|
1367
1376
|
console.log(' dep Dependency tooling: status | install <id> | uninstall <id>');
|
|
1368
1377
|
console.log(' packs Post-install pack surface: status | enable <ids> | disable <ids> | all');
|
|
1369
1378
|
console.log(' auth Manage unified auth: init | sync | status | reconcile');
|
|
@@ -1380,6 +1389,13 @@ async function main() {
|
|
|
1380
1389
|
console.log(' --clean-obs (with: uninstall --target all) also remove OBS config');
|
|
1381
1390
|
console.log(' --clean-gateway (with: uninstall --target all) also remove the devkit-installed open-mcp-gateway');
|
|
1382
1391
|
console.log(' --clean-global (with: uninstall --target all) also remove KooCLI + OBS config + open-mcp-gateway');
|
|
1392
|
+
console.log('\ninstall-server options:');
|
|
1393
|
+
console.log(' --install-path <dir> Base install path (default /opt/kunpeng)');
|
|
1394
|
+
console.log(' --port <n> Business port (default 8088)');
|
|
1395
|
+
console.log(' --tool-port <n> Tool port (default 7088)');
|
|
1396
|
+
console.log(' --user <name> Dedicated service user (default devkit)');
|
|
1397
|
+
console.log(' --rotate-password Regenerate the server auth password');
|
|
1398
|
+
console.log(' --remove-user (with: uninstall-server) also delete the service user');
|
|
1383
1399
|
console.log('\nExamples:');
|
|
1384
1400
|
console.log(' npx huaweicloud-devkit install');
|
|
1385
1401
|
console.log(' npx huaweicloud-devkit install --target codex');
|
|
@@ -204,6 +204,10 @@ export const TOOL_DEFINITIONS_LITERALS = [
|
|
|
204
204
|
name: 'huaweicloud_sandbox_shutdown',
|
|
205
205
|
description: packDescription('huaweicloud_sandbox_shutdown'),
|
|
206
206
|
},
|
|
207
|
+
{
|
|
208
|
+
name: 'huaweicloud_sandbox_kill',
|
|
209
|
+
description: packDescription('huaweicloud_sandbox_kill'),
|
|
210
|
+
},
|
|
207
211
|
{
|
|
208
212
|
name: 'huaweicloud_voucher_status',
|
|
209
213
|
description: packDescription('huaweicloud_voucher_status'),
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "huaweicloud-devkit",
|
|
3
3
|
"id": "huaweicloud-devkit",
|
|
4
4
|
"displayName": "HuaweiCloud DevKit",
|
|
5
|
-
"version": "2.0.1-next.
|
|
5
|
+
"version": "2.0.1-next.4",
|
|
6
6
|
"family": "bundle-plugin",
|
|
7
7
|
"bundleFormat": "codex",
|
|
8
8
|
"description": "Guide coding agents to use Huawei Cloud safely �?KooCLI, APIs, SDKs, 40 MCP tools, skills, and safety guardrails.",
|