@hua-ri/dsh-ops 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +60 -2
- package/client.js +183 -0
- package/cordis.patch.yml +3 -0
- package/index.js +1079 -0
- package/lib/argocd/instances.mjs +50 -0
- package/lib/argocd/mod.mjs +221 -0
- package/lib/argowf/instances.mjs +52 -0
- package/lib/argowf/mod.mjs +219 -0
- package/lib/cli.mjs +95 -0
- package/lib/client.js +56 -0
- package/lib/helm/mod.mjs +281 -0
- package/lib/k8s/context.mjs +179 -0
- package/lib/k8s/mod.mjs +347 -0
- package/lib/ssh/config.mjs +53 -0
- package/lib/ssh/hosts.mjs +94 -0
- package/lib/ssh/keys.mjs +76 -0
- package/lib/ssh/mod.mjs +476 -0
- package/lib/ssh/snippet-vars.mjs +22 -0
- package/lib/ssh/tunnels.mjs +97 -0
- package/package.json +60 -4
- package/scripts/doctor.mjs +35 -0
package/lib/ssh/mod.mjs
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import { exec, trimOutput } from '../cli.mjs'
|
|
2
|
+
import { loadHosts, findHost, addHost, removeHost, updateHost, buildSshArgs } from './hosts.mjs'
|
|
3
|
+
import { readSshConfig } from './config.mjs'
|
|
4
|
+
import { createTunnel, listTunnels, closeTunnel } from './tunnels.mjs'
|
|
5
|
+
import { listKeys, generateKey } from './keys.mjs'
|
|
6
|
+
import { substituteVars, extractVars } from './snippet-vars.mjs'
|
|
7
|
+
|
|
8
|
+
export function registerSSHTools(ctx, defineTool, toolsSvc, config) {
|
|
9
|
+
const textOutput = {
|
|
10
|
+
schema: { type: 'object', properties: {}, additionalProperties: true },
|
|
11
|
+
render: (_args, value) => [{ type: 'text', text: String(value?.text ?? '') }],
|
|
12
|
+
}
|
|
13
|
+
const register = (tool) => toolsSvc.register(defineTool(tool))
|
|
14
|
+
|
|
15
|
+
register({
|
|
16
|
+
name: 'ops_ssh_exec',
|
|
17
|
+
description: '在远程主机上执行一条 shell 命令。需要先用 ops_ssh_hosts_add 添加主机。',
|
|
18
|
+
parameters: {
|
|
19
|
+
host: { type: 'string', description: '主机名(在 ops_ssh_hosts_list 中注册的名称)', required: true },
|
|
20
|
+
command: { type: 'string', description: '要执行的 shell 命令', required: true },
|
|
21
|
+
timeout: { type: 'number', description: '超时秒数,默认 60' },
|
|
22
|
+
},
|
|
23
|
+
output: textOutput,
|
|
24
|
+
async execute(args) {
|
|
25
|
+
const h = findHost(args.host)
|
|
26
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host},请先用 ops_ssh_hosts_add 添加` }
|
|
27
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
28
|
+
sshArgs.push(target, args.command)
|
|
29
|
+
const timeout = (args.timeout || 60) * 1000
|
|
30
|
+
const r = await exec('ssh', sshArgs, { timeout })
|
|
31
|
+
return {
|
|
32
|
+
ok: r.code === 0,
|
|
33
|
+
text: trimOutput(
|
|
34
|
+
`[exit ${r.code}] ${h.name} (${h.hostname})\n` +
|
|
35
|
+
(r.stdout ? `stdout:\n${r.stdout}\n` : '') +
|
|
36
|
+
(r.stderr ? `stderr:\n${r.stderr}` : '')
|
|
37
|
+
),
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
register({
|
|
43
|
+
name: 'ops_ssh_batch',
|
|
44
|
+
description: '在多台主机上批量执行同一条命令。第一台作为 canary 先执行,失败则不继续。',
|
|
45
|
+
parameters: {
|
|
46
|
+
hosts: { type: 'array', items: { type: 'string' }, description: '主机名列表', required: true },
|
|
47
|
+
command: { type: 'string', description: '要执行的 shell 命令', required: true },
|
|
48
|
+
timeout: { type: 'number', description: '每台超时秒数,默认 60' },
|
|
49
|
+
stop_on_failure: { type: 'boolean', description: '某台失败后是否停止,默认 true' },
|
|
50
|
+
},
|
|
51
|
+
output: textOutput,
|
|
52
|
+
async execute(args) {
|
|
53
|
+
const timeout = (args.timeout || 60) * 1000
|
|
54
|
+
const stopOnFailure = args.stop_on_failure !== false
|
|
55
|
+
const results = []
|
|
56
|
+
|
|
57
|
+
for (let i = 0; i < args.hosts.length; i++) {
|
|
58
|
+
const name = args.hosts[i]
|
|
59
|
+
const h = findHost(name)
|
|
60
|
+
if (!h) {
|
|
61
|
+
results.push({ host: name, code: -1, error: `未找到主机 ${name}` })
|
|
62
|
+
if (stopOnFailure) break
|
|
63
|
+
continue
|
|
64
|
+
}
|
|
65
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
66
|
+
sshArgs.push(target, args.command)
|
|
67
|
+
const r = await exec('ssh', sshArgs, { timeout })
|
|
68
|
+
results.push({
|
|
69
|
+
host: name,
|
|
70
|
+
code: r.code,
|
|
71
|
+
stdout: r.stdout?.slice(0, 500) || '',
|
|
72
|
+
stderr: r.stderr?.slice(0, 300) || '',
|
|
73
|
+
})
|
|
74
|
+
if (r.code !== 0 && stopOnFailure) {
|
|
75
|
+
if (i === 0) {
|
|
76
|
+
results.push({ note: `canary ${name} 失败,后续 ${args.hosts.length - 1} 台已跳过` })
|
|
77
|
+
}
|
|
78
|
+
break
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const ok = results.every((r) => r.code === 0 || r.note)
|
|
83
|
+
const lines = results.map((r) =>
|
|
84
|
+
r.note || `[${r.host}] exit ${r.code}${r.stdout ? ': ' + r.stdout.split('\n')[0] : ''}`
|
|
85
|
+
)
|
|
86
|
+
return { ok, text: trimOutput(lines.join('\n')) }
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
register({
|
|
91
|
+
name: 'ops_ssh_hosts_list',
|
|
92
|
+
description: '列出已保存的 SSH 主机。',
|
|
93
|
+
parameters: {},
|
|
94
|
+
output: textOutput,
|
|
95
|
+
async execute() {
|
|
96
|
+
const hosts = loadHosts()
|
|
97
|
+
if (!hosts.length) return { ok: true, text: '没有已保存的主机。用 ops_ssh_hosts_add 添加。' }
|
|
98
|
+
const lines = hosts.map((h) =>
|
|
99
|
+
`${h.name} ${h.user || ''}@${h.hostname}:${h.port || 22}` +
|
|
100
|
+
(h.tags?.length ? ` [${h.tags.join(', ')}]` : '') +
|
|
101
|
+
(h.note ? ` — ${h.note}` : '')
|
|
102
|
+
)
|
|
103
|
+
return { ok: true, text: lines.join('\n') }
|
|
104
|
+
},
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
register({
|
|
108
|
+
name: 'ops_ssh_hosts_add',
|
|
109
|
+
description: '保存一台 SSH 主机,之后可以用名称引用它。',
|
|
110
|
+
parameters: {
|
|
111
|
+
name: { type: 'string', description: '主机的短名称(字母数字和 - _ .)', required: true },
|
|
112
|
+
hostname: { type: 'string', description: 'IP 地址或域名', required: true },
|
|
113
|
+
port: { type: 'number', description: 'SSH 端口,默认 22' },
|
|
114
|
+
user: { type: 'string', description: '登录用户名' },
|
|
115
|
+
identity_file: { type: 'string', description: '私钥文件路径(可选)' },
|
|
116
|
+
tags: { type: 'array', items: { type: 'string' }, description: '标签,如 env=prod' },
|
|
117
|
+
note: { type: 'string', description: '备注' },
|
|
118
|
+
},
|
|
119
|
+
output: textOutput,
|
|
120
|
+
async execute(args) {
|
|
121
|
+
const r = addHost({
|
|
122
|
+
name: args.name,
|
|
123
|
+
hostname: args.hostname,
|
|
124
|
+
port: args.port,
|
|
125
|
+
user: args.user,
|
|
126
|
+
identityFile: args.identity_file,
|
|
127
|
+
tags: args.tags,
|
|
128
|
+
note: args.note,
|
|
129
|
+
})
|
|
130
|
+
return { ok: r.ok, text: r.ok ? `已添加主机 ${args.name}` : r.why }
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
register({
|
|
135
|
+
name: 'ops_ssh_hosts_remove',
|
|
136
|
+
description: '删除一台已保存的 SSH 主机。',
|
|
137
|
+
parameters: {
|
|
138
|
+
name: { type: 'string', description: '要删除的主机名', required: true },
|
|
139
|
+
},
|
|
140
|
+
output: textOutput,
|
|
141
|
+
async execute(args) {
|
|
142
|
+
const r = removeHost(args.name)
|
|
143
|
+
return { ok: r.ok, text: r.ok ? `已删除主机 ${args.name}` : r.why }
|
|
144
|
+
},
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
register({
|
|
148
|
+
name: 'ops_ssh_file_read',
|
|
149
|
+
description: '读取远程主机上的文本文件内容。',
|
|
150
|
+
parameters: {
|
|
151
|
+
host: { type: 'string', description: '主机名', required: true },
|
|
152
|
+
path: { type: 'string', description: '远程文件路径', required: true },
|
|
153
|
+
tail: { type: 'number', description: '只读取最后 N 行(可选)' },
|
|
154
|
+
},
|
|
155
|
+
output: textOutput,
|
|
156
|
+
async execute(args) {
|
|
157
|
+
const h = findHost(args.host)
|
|
158
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host}` }
|
|
159
|
+
const cmd = args.tail ? `tail -n ${args.tail} ${args.path}` : `cat ${args.path}`
|
|
160
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
161
|
+
sshArgs.push(target, cmd)
|
|
162
|
+
const r = await exec('ssh', sshArgs, { timeout: 30000 })
|
|
163
|
+
if (r.code !== 0) return { ok: false, text: `读取失败:${r.stderr || `exit ${r.code}`}` }
|
|
164
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
register({
|
|
169
|
+
name: 'ops_ssh_file_write',
|
|
170
|
+
description: '将内容写入远程主机上的文件(覆盖)。',
|
|
171
|
+
parameters: {
|
|
172
|
+
host: { type: 'string', description: '主机名', required: true },
|
|
173
|
+
path: { type: 'string', description: '远程文件路径', required: true },
|
|
174
|
+
content: { type: 'string', description: '文件内容', required: true },
|
|
175
|
+
},
|
|
176
|
+
output: textOutput,
|
|
177
|
+
async execute(args) {
|
|
178
|
+
const h = findHost(args.host)
|
|
179
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host}` }
|
|
180
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
181
|
+
sshArgs.push(target, `cat > ${args.path}`)
|
|
182
|
+
const r = await exec('ssh', sshArgs, { timeout: 30000, stdin: args.content })
|
|
183
|
+
if (r.code !== 0) return { ok: false, text: `写入失败:${r.stderr || `exit ${r.code}`}` }
|
|
184
|
+
return { ok: true, text: `已写入 ${args.path}(${args.content.length} 字节)` }
|
|
185
|
+
},
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
register({
|
|
189
|
+
name: 'ops_ssh_file_list',
|
|
190
|
+
description: '列出远程主机上某个目录的内容。',
|
|
191
|
+
parameters: {
|
|
192
|
+
host: { type: 'string', description: '主机名', required: true },
|
|
193
|
+
path: { type: 'string', description: '远程目录路径', required: true },
|
|
194
|
+
all: { type: 'boolean', description: '是否显示隐藏文件' },
|
|
195
|
+
},
|
|
196
|
+
output: textOutput,
|
|
197
|
+
async execute(args) {
|
|
198
|
+
const h = findHost(args.host)
|
|
199
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host}` }
|
|
200
|
+
const lsFlags = args.all ? '-la' : '-l'
|
|
201
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
202
|
+
sshArgs.push(target, `ls ${lsFlags} ${args.path}`)
|
|
203
|
+
const r = await exec('ssh', sshArgs, { timeout: 15000 })
|
|
204
|
+
if (r.code !== 0) return { ok: false, text: `列目录失败:${r.stderr || `exit ${r.code}`}` }
|
|
205
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
206
|
+
},
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
register({
|
|
210
|
+
name: 'ops_ssh_hosts_update',
|
|
211
|
+
description: '修改已保存的 SSH 主机的配置(主机名、端口、用户、密钥、标签、备注等)。',
|
|
212
|
+
parameters: {
|
|
213
|
+
name: { type: 'string', description: '要修改的主机名', required: true },
|
|
214
|
+
hostname: { type: 'string', description: '新的 IP 地址或域名' },
|
|
215
|
+
port: { type: 'number', description: '新的 SSH 端口' },
|
|
216
|
+
user: { type: 'string', description: '新的登录用户名' },
|
|
217
|
+
identity_file: { type: 'string', description: '新的私钥文件路径' },
|
|
218
|
+
proxy_jump: { type: 'string', description: '跳板机名称' },
|
|
219
|
+
tags: { type: 'array', items: { type: 'string' }, description: '新的标签列表(替换全部)' },
|
|
220
|
+
note: { type: 'string', description: '新的备注' },
|
|
221
|
+
},
|
|
222
|
+
output: textOutput,
|
|
223
|
+
async execute(args) {
|
|
224
|
+
const patch = {}
|
|
225
|
+
if (args.hostname !== undefined) patch.hostname = args.hostname
|
|
226
|
+
if (args.port !== undefined) patch.port = args.port
|
|
227
|
+
if (args.user !== undefined) patch.user = args.user
|
|
228
|
+
if (args.identity_file !== undefined) patch.identityFile = args.identity_file
|
|
229
|
+
if (args.proxy_jump !== undefined) patch.proxyJump = args.proxy_jump
|
|
230
|
+
if (args.tags !== undefined) patch.tags = args.tags
|
|
231
|
+
if (args.note !== undefined) patch.note = args.note
|
|
232
|
+
const r = updateHost(args.name, patch)
|
|
233
|
+
return { ok: r.ok, text: r.ok ? `已更新主机 ${args.name}` : r.why }
|
|
234
|
+
},
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
register({
|
|
238
|
+
name: 'ops_ssh_config_import',
|
|
239
|
+
description: '从本机 ~/.ssh/config 解析主机列表并导入(已存在的会跳过)。',
|
|
240
|
+
parameters: {},
|
|
241
|
+
output: textOutput,
|
|
242
|
+
async execute() {
|
|
243
|
+
const entries = readSshConfig()
|
|
244
|
+
if (!entries.length) return { ok: true, text: '未在 ~/.ssh/config 中找到 Host 条目' }
|
|
245
|
+
const existing = loadHosts()
|
|
246
|
+
let added = 0, skipped = 0
|
|
247
|
+
for (const e of entries) {
|
|
248
|
+
if (!e.host || e.host.includes('*')) { skipped++; continue }
|
|
249
|
+
if (existing.find(h => h.name === e.host)) { skipped++; continue }
|
|
250
|
+
const r = addHost({
|
|
251
|
+
name: e.host,
|
|
252
|
+
hostname: e.hostname || e.host,
|
|
253
|
+
port: e.port ? Number(e.port) : undefined,
|
|
254
|
+
user: e.user,
|
|
255
|
+
identityFile: e.identityFile,
|
|
256
|
+
proxyJump: e.proxyJump,
|
|
257
|
+
})
|
|
258
|
+
if (r.ok) added++
|
|
259
|
+
else skipped++
|
|
260
|
+
}
|
|
261
|
+
return { ok: true, text: `导入完成:新增 ${added} 台,跳过 ${skipped} 条` }
|
|
262
|
+
},
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
register({
|
|
266
|
+
name: 'ops_ssh_snippets',
|
|
267
|
+
description: '管理命令片段:list 列出所有、get 获取单个、add 添加、remove 删除。',
|
|
268
|
+
parameters: {
|
|
269
|
+
action: { type: 'string', enum: ['list', 'get', 'add', 'remove'], description: '操作(默认 list)' },
|
|
270
|
+
name: { type: 'string', description: '片段名称(add/get/remove 必填)' },
|
|
271
|
+
command: { type: 'string', description: '命令模板(add 时必填,支持 ${var} 变量)' },
|
|
272
|
+
description: { type: 'string', description: '描述(add 时可选)' },
|
|
273
|
+
tags: { type: 'array', items: { type: 'string' }, description: '标签(add 时可选)' },
|
|
274
|
+
},
|
|
275
|
+
output: textOutput,
|
|
276
|
+
async execute(args) {
|
|
277
|
+
const { readJson, writeJson, dataDir } = await import('../cli.mjs')
|
|
278
|
+
const { join } = await import('node:path')
|
|
279
|
+
const { mkdirSync } = await import('node:fs')
|
|
280
|
+
const snippetsPath = join(dataDir(), 'ssh', 'snippets.json')
|
|
281
|
+
const load = () => readJson(snippetsPath, [])
|
|
282
|
+
const save = (list) => { mkdirSync(join(dataDir(), 'ssh'), { recursive: true }); writeJson(snippetsPath, list) }
|
|
283
|
+
|
|
284
|
+
const action = args.action || 'list'
|
|
285
|
+
if (action === 'list') {
|
|
286
|
+
const list = load()
|
|
287
|
+
if (!list.length) return { ok: true, text: '没有已保存的命令片段' }
|
|
288
|
+
return { ok: true, text: list.map(s => `${s.name} ${s.description || ''}\n $ ${s.command}`).join('\n\n') }
|
|
289
|
+
}
|
|
290
|
+
if (action === 'get') {
|
|
291
|
+
if (!args.name) return { ok: false, text: '需要 name 参数' }
|
|
292
|
+
const list = load()
|
|
293
|
+
const s = list.find(s => s.name === args.name)
|
|
294
|
+
if (!s) return { ok: false, text: `片段 ${args.name} 不存在` }
|
|
295
|
+
const vars = extractVars(s.command)
|
|
296
|
+
return { ok: true, text: `名称: ${s.name}\n描述: ${s.description || '-'}\n命令: ${s.command}\n变量: ${vars.length ? vars.join(', ') : '无'}` }
|
|
297
|
+
}
|
|
298
|
+
if (action === 'add') {
|
|
299
|
+
if (!args.name || !args.command) return { ok: false, text: '需要 name 和 command 参数' }
|
|
300
|
+
const list = load()
|
|
301
|
+
if (list.find(s => s.name === args.name)) return { ok: false, text: `片段 ${args.name} 已存在` }
|
|
302
|
+
list.push({ name: args.name, command: args.command, description: args.description || '', tags: args.tags || [], addedAt: new Date().toISOString() })
|
|
303
|
+
save(list)
|
|
304
|
+
return { ok: true, text: `已添加片段 ${args.name}` }
|
|
305
|
+
}
|
|
306
|
+
if (action === 'remove') {
|
|
307
|
+
if (!args.name) return { ok: false, text: '需要 name 参数' }
|
|
308
|
+
const list = load()
|
|
309
|
+
const idx = list.findIndex(s => s.name === args.name)
|
|
310
|
+
if (idx === -1) return { ok: false, text: `片段 ${args.name} 不存在` }
|
|
311
|
+
list.splice(idx, 1)
|
|
312
|
+
save(list)
|
|
313
|
+
return { ok: true, text: `已删除片段 ${args.name}` }
|
|
314
|
+
}
|
|
315
|
+
return { ok: false, text: `未知操作 ${action}` }
|
|
316
|
+
},
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
register({
|
|
320
|
+
name: 'ops_ssh_snippet_exec',
|
|
321
|
+
description: '在主机上执行命令片段,可传入变量替换模板中的 ${var}。',
|
|
322
|
+
parameters: {
|
|
323
|
+
snippet: { type: 'string', description: '片段名称', required: true },
|
|
324
|
+
host: { type: 'string', description: '目标主机名', required: true },
|
|
325
|
+
vars: { type: 'object', description: '变量键值对,如 { "port": "8080" }' },
|
|
326
|
+
timeout: { type: 'number', description: '超时秒数,默认 60' },
|
|
327
|
+
},
|
|
328
|
+
output: textOutput,
|
|
329
|
+
async execute(args) {
|
|
330
|
+
const { readJson, dataDir } = await import('../cli.mjs')
|
|
331
|
+
const { join } = await import('node:path')
|
|
332
|
+
const list = readJson(join(dataDir(), 'ssh', 'snippets.json'), [])
|
|
333
|
+
const s = list.find(s => s.name === args.snippet)
|
|
334
|
+
if (!s) return { ok: false, text: `片段 ${args.snippet} 不存在` }
|
|
335
|
+
const h = findHost(args.host)
|
|
336
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host}` }
|
|
337
|
+
const command = substituteVars(s.command, {
|
|
338
|
+
hostName: h.name, hostname: h.hostname,
|
|
339
|
+
user: h.user, port: h.port,
|
|
340
|
+
vars: args.vars || {},
|
|
341
|
+
})
|
|
342
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
343
|
+
sshArgs.push(target, command)
|
|
344
|
+
const r = await exec('ssh', sshArgs, { timeout: (args.timeout || 60) * 1000 })
|
|
345
|
+
return {
|
|
346
|
+
ok: r.code === 0,
|
|
347
|
+
text: trimOutput(`[exit ${r.code}] $ ${command}\n${r.stdout}${r.stderr ? '\nstderr: ' + r.stderr : ''}`),
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
register({
|
|
353
|
+
name: 'ops_ssh_keys',
|
|
354
|
+
description: '管理本机 SSH 密钥:list 列出、generate 生成新密钥。',
|
|
355
|
+
parameters: {
|
|
356
|
+
action: { type: 'string', enum: ['list', 'generate'], description: '操作(默认 list)' },
|
|
357
|
+
type: { type: 'string', description: 'generate 时密钥类型:ed25519(推荐)、rsa' },
|
|
358
|
+
comment: { type: 'string', description: 'generate 时的注释' },
|
|
359
|
+
path: { type: 'string', description: 'generate 时密钥保存路径(可选)' },
|
|
360
|
+
},
|
|
361
|
+
output: textOutput,
|
|
362
|
+
async execute(args) {
|
|
363
|
+
const action = args.action || 'list'
|
|
364
|
+
if (action === 'list') {
|
|
365
|
+
const keys = await listKeys()
|
|
366
|
+
if (!keys.length) return { ok: true, text: '未找到 SSH 密钥' }
|
|
367
|
+
return { ok: true, text: keys.map(k => `${k.name} ${k.type} ${k.fingerprint || ''}`).join('\n') }
|
|
368
|
+
}
|
|
369
|
+
if (action === 'generate') {
|
|
370
|
+
const r = await generateKey({ type: args.type, comment: args.comment, path: args.path })
|
|
371
|
+
return { ok: r.ok, text: r.ok ? `已生成密钥:${r.path}\n公钥:${r.pubKey}` : (r.error || '生成失败') }
|
|
372
|
+
}
|
|
373
|
+
return { ok: false, text: `未知操作 ${action}` }
|
|
374
|
+
},
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
register({
|
|
378
|
+
name: 'ops_ssh_tunnels',
|
|
379
|
+
description: '管理 SSH 隧道:list 列出、open 打开新隧道、close 关闭。',
|
|
380
|
+
parameters: {
|
|
381
|
+
action: { type: 'string', enum: ['list', 'open', 'close'], description: '操作(默认 list)' },
|
|
382
|
+
host: { type: 'string', description: 'open 时目标主机名' },
|
|
383
|
+
type: { type: 'string', enum: ['local', 'remote', 'dynamic'], description: 'open 时隧道类型(默认 local)' },
|
|
384
|
+
local_port: { type: 'number', description: 'open 时本地端口' },
|
|
385
|
+
remote_host: { type: 'string', description: 'open 时远程主机(默认 127.0.0.1)' },
|
|
386
|
+
remote_port: { type: 'number', description: 'open 时远程端口' },
|
|
387
|
+
id: { type: 'string', description: 'close 时隧道 ID' },
|
|
388
|
+
},
|
|
389
|
+
output: textOutput,
|
|
390
|
+
async execute(args) {
|
|
391
|
+
const action = args.action || 'list'
|
|
392
|
+
if (action === 'list') {
|
|
393
|
+
const tunnels = listTunnels()
|
|
394
|
+
if (!tunnels.length) return { ok: true, text: '没有活跃的隧道' }
|
|
395
|
+
return { ok: true, text: tunnels.map(t => `[${t.id}] ${t.hostName} ${t.type} ${t.localPort} → ${t.remoteHost || ''}:${t.remotePort || '*'} ${t.alive ? '✓' : '✗'}`).join('\n') }
|
|
396
|
+
}
|
|
397
|
+
if (action === 'open') {
|
|
398
|
+
if (!args.host) return { ok: false, text: '需要 host 参数' }
|
|
399
|
+
if (!args.local_port) return { ok: false, text: '需要 local_port 参数' }
|
|
400
|
+
const r = createTunnel({
|
|
401
|
+
hostName: args.host,
|
|
402
|
+
type: args.type || 'local',
|
|
403
|
+
localPort: args.local_port,
|
|
404
|
+
remoteHost: args.remote_host || '127.0.0.1',
|
|
405
|
+
remotePort: args.remote_port || args.local_port,
|
|
406
|
+
})
|
|
407
|
+
return { ok: r.ok, text: r.ok ? `隧道已打开 [${r.id}] localhost:${args.local_port} → ${args.remote_host || '127.0.0.1'}:${args.remote_port || args.local_port}` : (r.error || '打开失败') }
|
|
408
|
+
}
|
|
409
|
+
if (action === 'close') {
|
|
410
|
+
if (!args.id) return { ok: false, text: '需要 id 参数' }
|
|
411
|
+
const r = closeTunnel(args.id)
|
|
412
|
+
return { ok: r.ok, text: r.ok ? `隧道 ${args.id} 已关闭` : (r.error || '关闭失败') }
|
|
413
|
+
}
|
|
414
|
+
return { ok: false, text: `未知操作 ${action}` }
|
|
415
|
+
},
|
|
416
|
+
})
|
|
417
|
+
|
|
418
|
+
register({
|
|
419
|
+
name: 'ops_ssh_file_mkdir',
|
|
420
|
+
description: '在远程主机上创建目录(支持递归创建)。',
|
|
421
|
+
parameters: {
|
|
422
|
+
host: { type: 'string', description: '主机名', required: true },
|
|
423
|
+
path: { type: 'string', description: '目录路径', required: true },
|
|
424
|
+
},
|
|
425
|
+
output: textOutput,
|
|
426
|
+
async execute(args) {
|
|
427
|
+
const h = findHost(args.host)
|
|
428
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host}` }
|
|
429
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
430
|
+
sshArgs.push(target, `mkdir -p ${args.path}`)
|
|
431
|
+
const r = await exec('ssh', sshArgs, { timeout: 15000 })
|
|
432
|
+
if (r.code !== 0) return { ok: false, text: `创建目录失败:${r.stderr || `exit ${r.code}`}` }
|
|
433
|
+
return { ok: true, text: `已创建目录 ${args.path}` }
|
|
434
|
+
},
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
register({
|
|
438
|
+
name: 'ops_ssh_file_delete',
|
|
439
|
+
description: '删除远程主机上的文件或空目录。',
|
|
440
|
+
parameters: {
|
|
441
|
+
host: { type: 'string', description: '主机名', required: true },
|
|
442
|
+
path: { type: 'string', description: '文件或目录路径', required: true },
|
|
443
|
+
recursive: { type: 'boolean', description: '是否递归删除目录(危险)' },
|
|
444
|
+
},
|
|
445
|
+
output: textOutput,
|
|
446
|
+
async execute(args) {
|
|
447
|
+
const h = findHost(args.host)
|
|
448
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host}` }
|
|
449
|
+
const cmd = args.recursive ? `rm -rf ${args.path}` : `rm -f ${args.path}`
|
|
450
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
451
|
+
sshArgs.push(target, cmd)
|
|
452
|
+
const r = await exec('ssh', sshArgs, { timeout: 15000 })
|
|
453
|
+
if (r.code !== 0) return { ok: false, text: `删除失败:${r.stderr || `exit ${r.code}`}` }
|
|
454
|
+
return { ok: true, text: `已删除 ${args.path}` }
|
|
455
|
+
},
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
register({
|
|
459
|
+
name: 'ops_ssh_perf',
|
|
460
|
+
description: '获取远程主机的性能概况(CPU、内存、磁盘、负载)。',
|
|
461
|
+
parameters: {
|
|
462
|
+
host: { type: 'string', description: '主机名', required: true },
|
|
463
|
+
},
|
|
464
|
+
output: textOutput,
|
|
465
|
+
async execute(args) {
|
|
466
|
+
const h = findHost(args.host)
|
|
467
|
+
if (!h) return { ok: false, text: `未找到主机 ${args.host}` }
|
|
468
|
+
const cmd = `echo "=== LOAD ===" && uptime && echo "=== MEMORY ===" && free -h 2>/dev/null || vm_stat 2>/dev/null && echo "=== DISK ===" && df -h / && echo "=== CPU ===" && (nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null) && echo "cores"`
|
|
469
|
+
const { args: sshArgs, target } = buildSshArgs(h)
|
|
470
|
+
sshArgs.push(target, cmd)
|
|
471
|
+
const r = await exec('ssh', sshArgs, { timeout: 15000 })
|
|
472
|
+
if (r.code !== 0) return { ok: false, text: `获取性能信息失败:${r.stderr || `exit ${r.code}`}` }
|
|
473
|
+
return { ok: true, text: trimOutput(`[${h.name}] 性能概况\n${r.stdout}`) }
|
|
474
|
+
},
|
|
475
|
+
})
|
|
476
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function substituteVars(template, context = {}) {
|
|
2
|
+
const now = new Date()
|
|
3
|
+
const builtins = {
|
|
4
|
+
host: context.hostName || '',
|
|
5
|
+
hostname: context.hostname || '',
|
|
6
|
+
user: context.user || process.env.USER || '',
|
|
7
|
+
port: String(context.port || 22),
|
|
8
|
+
date: now.toISOString().slice(0, 10),
|
|
9
|
+
time: now.toTimeString().slice(0, 8),
|
|
10
|
+
timestamp: String(Math.floor(now.getTime() / 1000)),
|
|
11
|
+
home: process.env.HOME || '~',
|
|
12
|
+
}
|
|
13
|
+
const vars = { ...builtins, ...context.vars }
|
|
14
|
+
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
|
15
|
+
return vars[key] !== undefined ? vars[key] : match
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function extractVars(template) {
|
|
20
|
+
const matches = template.match(/\{\{(\w+)\}\}/g) || []
|
|
21
|
+
return [...new Set(matches.map(m => m.slice(2, -2)))]
|
|
22
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { buildSshArgs, findHost } from './hosts.mjs'
|
|
3
|
+
|
|
4
|
+
const tunnels = new Map()
|
|
5
|
+
let nextId = 1
|
|
6
|
+
|
|
7
|
+
export function createTunnel({ hostName, type, localPort, remoteHost, remotePort }) {
|
|
8
|
+
const host = findHost(hostName)
|
|
9
|
+
if (!host) return { ok: false, error: `未找到主机 ${hostName}` }
|
|
10
|
+
|
|
11
|
+
let sshArgs, target
|
|
12
|
+
|
|
13
|
+
if (host.relay) {
|
|
14
|
+
const relayHost = findHost(host.relay)
|
|
15
|
+
if (!relayHost) return { ok: false, error: `未找到中继主机 ${host.relay}` }
|
|
16
|
+
const relay = buildSshArgs(relayHost)
|
|
17
|
+
sshArgs = relay.args
|
|
18
|
+
target = relay.target
|
|
19
|
+
const fwdTarget = host.hostname
|
|
20
|
+
sshArgs.push('-N')
|
|
21
|
+
if (type === 'local') {
|
|
22
|
+
sshArgs.push('-L', `${localPort}:${fwdTarget}:${remotePort}`)
|
|
23
|
+
} else if (type === 'remote') {
|
|
24
|
+
sshArgs.push('-R', `${remotePort}:${remoteHost || '127.0.0.1'}:${localPort}`)
|
|
25
|
+
} else if (type === 'dynamic') {
|
|
26
|
+
sshArgs.push('-D', String(localPort))
|
|
27
|
+
} else {
|
|
28
|
+
return { ok: false, error: `未知隧道类型 ${type}` }
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
const built = buildSshArgs(host)
|
|
32
|
+
sshArgs = built.args
|
|
33
|
+
target = built.target
|
|
34
|
+
sshArgs.push('-N')
|
|
35
|
+
if (type === 'local') {
|
|
36
|
+
sshArgs.push('-L', `${localPort}:${remoteHost || '127.0.0.1'}:${remotePort}`)
|
|
37
|
+
} else if (type === 'remote') {
|
|
38
|
+
sshArgs.push('-R', `${remotePort}:${remoteHost || '127.0.0.1'}:${localPort}`)
|
|
39
|
+
} else if (type === 'dynamic') {
|
|
40
|
+
sshArgs.push('-D', String(localPort))
|
|
41
|
+
} else {
|
|
42
|
+
return { ok: false, error: `未知隧道类型 ${type}` }
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
sshArgs.push(target)
|
|
47
|
+
|
|
48
|
+
const proc = spawn('ssh', sshArgs, { stdio: 'pipe', detached: false })
|
|
49
|
+
const id = 't' + (nextId++)
|
|
50
|
+
const tunnel = {
|
|
51
|
+
id, host: hostName, type,
|
|
52
|
+
localPort: Number(localPort),
|
|
53
|
+
remoteHost: remoteHost || '127.0.0.1',
|
|
54
|
+
remotePort: Number(remotePort) || 0,
|
|
55
|
+
pid: proc.pid,
|
|
56
|
+
startedAt: new Date().toISOString(),
|
|
57
|
+
alive: true,
|
|
58
|
+
error: null,
|
|
59
|
+
}
|
|
60
|
+
tunnels.set(id, { ...tunnel, proc })
|
|
61
|
+
|
|
62
|
+
proc.on('exit', (code) => {
|
|
63
|
+
const t = tunnels.get(id)
|
|
64
|
+
if (t) { t.alive = false; t.error = code !== 0 ? `exit ${code}` : null }
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
proc.on('error', (e) => {
|
|
68
|
+
const t = tunnels.get(id)
|
|
69
|
+
if (t) { t.alive = false; t.error = e.message }
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
let stderr = ''
|
|
73
|
+
proc.stderr?.on('data', (d) => { stderr += d.toString(); if (stderr.length > 2000) stderr = stderr.slice(-1000) })
|
|
74
|
+
|
|
75
|
+
return { ok: true, tunnel: { ...tunnel } }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function listTunnels() {
|
|
79
|
+
const result = []
|
|
80
|
+
for (const [id, t] of tunnels) {
|
|
81
|
+
try { process.kill(t.proc.pid, 0); t.alive = true } catch { t.alive = false }
|
|
82
|
+
result.push({
|
|
83
|
+
id, host: t.host, type: t.type,
|
|
84
|
+
localPort: t.localPort, remoteHost: t.remoteHost, remotePort: t.remotePort,
|
|
85
|
+
pid: t.pid, startedAt: t.startedAt, alive: t.alive, error: t.error,
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
return result
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function closeTunnel(id) {
|
|
92
|
+
const t = tunnels.get(id)
|
|
93
|
+
if (!t) return { ok: false, error: `隧道 ${id} 不存在` }
|
|
94
|
+
try { process.kill(t.proc.pid, 'SIGTERM') } catch {}
|
|
95
|
+
tunnels.delete(id)
|
|
96
|
+
return { ok: true }
|
|
97
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hua-ri/dsh-ops",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "DSH 一体化运维插件:SSH 远程运维 / Kubernetes 集群管理 / Helm Chart 管理 / ArgoCD 部署管理 / Argo Workflows 工作流管理。",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js",
|
|
10
|
+
"./client": "./lib/client.js",
|
|
11
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "node scripts/build-ui.mjs",
|
|
16
|
+
"prepublishOnly": "node scripts/build-ui.mjs",
|
|
17
|
+
"doctor": "node scripts/doctor.mjs"
|
|
18
|
+
},
|
|
19
|
+
"dsh": {
|
|
20
|
+
"bundle": {
|
|
21
|
+
"patch": "./cordis.patch.yml"
|
|
22
|
+
},
|
|
23
|
+
"client": {
|
|
24
|
+
"platform": "web",
|
|
25
|
+
"immediately": true,
|
|
26
|
+
"inject": [
|
|
27
|
+
"@deepseek-ai/dsh-client-runtime",
|
|
28
|
+
"@deepseek-ai/dsh-client-ui-renderer",
|
|
29
|
+
"@deepseek-ai/dsh-client-ui-layout",
|
|
30
|
+
"@deepseek-ai/dsh-client-ui-sidebar"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"index.js",
|
|
36
|
+
"lib/",
|
|
37
|
+
"client.js",
|
|
38
|
+
"cordis.patch.yml",
|
|
39
|
+
"scripts/doctor.mjs"
|
|
40
|
+
],
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/hua-ri/dsh-ops.git"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/hua-ri/dsh-ops",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@deepseek-ai/dsh": ">=0.2.0-rc.2 <0.3.0-0",
|
|
52
|
+
"@deepseek-ai/dsh-tools": ">=0.2.0-rc.2 <0.3.0-0"
|
|
53
|
+
},
|
|
54
|
+
"peerDependenciesMeta": {
|
|
55
|
+
"@deepseek-ai/dsh": {
|
|
56
|
+
"optional": true
|
|
57
|
+
},
|
|
58
|
+
"@deepseek-ai/dsh-tools": {
|
|
59
|
+
"optional": true
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|