@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
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { readJson, writeJson, dataDir } from '../cli.mjs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { mkdirSync } from 'node:fs'
|
|
4
|
+
|
|
5
|
+
const instancesFile = () => join(dataDir(), 'argocd', 'instances.json')
|
|
6
|
+
|
|
7
|
+
export function loadInstances() {
|
|
8
|
+
return readJson(instancesFile(), [])
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function saveInstances(instances) {
|
|
12
|
+
mkdirSync(join(dataDir(), 'argocd'), { recursive: true })
|
|
13
|
+
writeJson(instancesFile(), instances)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function findInstance(name) {
|
|
17
|
+
const instances = loadInstances()
|
|
18
|
+
return instances.find((i) => i.name === name) || null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function addInstance({ name, server, tags, note }) {
|
|
22
|
+
const instances = loadInstances()
|
|
23
|
+
if (instances.find((i) => i.name === name)) {
|
|
24
|
+
return { ok: false, why: `实例 ${name} 已存在` }
|
|
25
|
+
}
|
|
26
|
+
instances.push({
|
|
27
|
+
name,
|
|
28
|
+
server,
|
|
29
|
+
tags: tags || [],
|
|
30
|
+
note: note || '',
|
|
31
|
+
addedAt: new Date().toISOString(),
|
|
32
|
+
})
|
|
33
|
+
saveInstances(instances)
|
|
34
|
+
return { ok: true }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function removeInstance(name) {
|
|
38
|
+
const instances = loadInstances()
|
|
39
|
+
const idx = instances.findIndex((i) => i.name === name)
|
|
40
|
+
if (idx === -1) return { ok: false, why: `实例 ${name} 不存在` }
|
|
41
|
+
instances.splice(idx, 1)
|
|
42
|
+
saveInstances(instances)
|
|
43
|
+
return { ok: true }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function buildArgocdArgs(instance, extraArgs = []) {
|
|
47
|
+
const args = ['--server', instance.server, '--grpc-web']
|
|
48
|
+
args.push(...extraArgs)
|
|
49
|
+
return args
|
|
50
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { exec, trimOutput } from '../cli.mjs'
|
|
2
|
+
import { loadInstances, findInstance, addInstance, removeInstance, buildArgocdArgs } from './instances.mjs'
|
|
3
|
+
|
|
4
|
+
export function registerArgocdTools(ctx, defineTool, toolsSvc, config) {
|
|
5
|
+
const textOutput = {
|
|
6
|
+
schema: { type: 'object', properties: {}, additionalProperties: true },
|
|
7
|
+
render: (_args, value) => [{ type: 'text', text: String(value?.text ?? '') }],
|
|
8
|
+
}
|
|
9
|
+
const register = (tool) => toolsSvc.register(defineTool(tool))
|
|
10
|
+
|
|
11
|
+
register({
|
|
12
|
+
name: 'ops_argocd_apps',
|
|
13
|
+
description: '列出 Argo CD 应用(名称、项目、同步状态、健康状态)。',
|
|
14
|
+
parameters: {
|
|
15
|
+
instance: { type: 'string', description: '实例名称(ops_argocd_instances 中注册的)', required: true },
|
|
16
|
+
project: { type: 'string', description: '按项目过滤' },
|
|
17
|
+
selector: { type: 'string', description: '标签选择器,如 team=payments' },
|
|
18
|
+
output: { type: 'string', description: '输出格式:wide / json / yaml(默认表格)' },
|
|
19
|
+
},
|
|
20
|
+
output: textOutput,
|
|
21
|
+
async execute(args) {
|
|
22
|
+
const inst = findInstance(args.instance)
|
|
23
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
24
|
+
const a = buildArgocdArgs(inst, ['app', 'list'])
|
|
25
|
+
if (args.project) a.push('-p', args.project)
|
|
26
|
+
if (args.selector) a.push('-l', args.selector)
|
|
27
|
+
if (args.output) a.push('-o', args.output)
|
|
28
|
+
const r = await exec('argocd', a, { timeout: 30000 })
|
|
29
|
+
if (r.code !== 0) return { ok: false, text: `argocd app list 失败:${r.stderr}` }
|
|
30
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
register({
|
|
35
|
+
name: 'ops_argocd_app',
|
|
36
|
+
description: '查看一个 Argo CD 应用的详情(源、目标、同步策略、最近部署历史)。',
|
|
37
|
+
parameters: {
|
|
38
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
39
|
+
app: { type: 'string', description: '应用名称', required: true },
|
|
40
|
+
},
|
|
41
|
+
output: textOutput,
|
|
42
|
+
async execute(args) {
|
|
43
|
+
const inst = findInstance(args.instance)
|
|
44
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
45
|
+
const a = buildArgocdArgs(inst, ['app', 'get', args.app])
|
|
46
|
+
const r = await exec('argocd', a, { timeout: 30000 })
|
|
47
|
+
if (r.code !== 0) return { ok: false, text: `argocd app get 失败:${r.stderr}` }
|
|
48
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
register({
|
|
53
|
+
name: 'ops_argocd_diagnose',
|
|
54
|
+
description: '诊断应用问题:查看 conditions、失败资源、out-of-sync 资源。',
|
|
55
|
+
parameters: {
|
|
56
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
57
|
+
app: { type: 'string', description: '应用名称', required: true },
|
|
58
|
+
},
|
|
59
|
+
output: textOutput,
|
|
60
|
+
async execute(args) {
|
|
61
|
+
const inst = findInstance(args.instance)
|
|
62
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
63
|
+
const sections = []
|
|
64
|
+
|
|
65
|
+
const getR = await exec('argocd', buildArgocdArgs(inst, ['app', 'get', args.app]), { timeout: 30000 })
|
|
66
|
+
if (getR.code === 0) sections.push('=== 应用状态 ===\n' + getR.stdout)
|
|
67
|
+
|
|
68
|
+
const diffR = await exec('argocd', buildArgocdArgs(inst, ['app', 'diff', args.app]), { timeout: 30000 })
|
|
69
|
+
if (diffR.stdout) sections.push('=== Diff(OutOfSync 资源)===\n' + diffR.stdout)
|
|
70
|
+
|
|
71
|
+
const treeR = await exec('argocd', buildArgocdArgs(inst, ['app', 'resources', args.app]), { timeout: 30000 })
|
|
72
|
+
if (treeR.code === 0) sections.push('=== 资源树 ===\n' + treeR.stdout)
|
|
73
|
+
|
|
74
|
+
return { ok: true, text: trimOutput(sections.join('\n\n')) }
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
register({
|
|
79
|
+
name: 'ops_argocd_diff',
|
|
80
|
+
description: '显示应用 OutOfSync 资源的 diff(live → desired)。',
|
|
81
|
+
parameters: {
|
|
82
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
83
|
+
app: { type: 'string', description: '应用名称', required: true },
|
|
84
|
+
},
|
|
85
|
+
output: textOutput,
|
|
86
|
+
async execute(args) {
|
|
87
|
+
const inst = findInstance(args.instance)
|
|
88
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
89
|
+
const r = await exec('argocd', buildArgocdArgs(inst, ['app', 'diff', args.app]), { timeout: 30000 })
|
|
90
|
+
if (!r.stdout && r.code === 0) return { ok: true, text: '应用已同步,没有差异。' }
|
|
91
|
+
return { ok: true, text: trimOutput(r.stdout || r.stderr) }
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
register({
|
|
96
|
+
name: 'ops_argocd_sync',
|
|
97
|
+
description: '同步应用(默认 dry-run,需显式设 dry_run=false 才真正执行)。',
|
|
98
|
+
parameters: {
|
|
99
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
100
|
+
app: { type: 'string', description: '应用名称', required: true },
|
|
101
|
+
dry_run: { type: 'boolean', description: '是否只做 dry-run(默认 true)' },
|
|
102
|
+
prune: { type: 'boolean', description: '是否删除 Git 中已不存在的资源' },
|
|
103
|
+
revision: { type: 'string', description: '同步到的 Git 版本' },
|
|
104
|
+
},
|
|
105
|
+
output: textOutput,
|
|
106
|
+
async execute(args) {
|
|
107
|
+
const inst = findInstance(args.instance)
|
|
108
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
109
|
+
const a = buildArgocdArgs(inst, ['app', 'sync', args.app])
|
|
110
|
+
if (args.dry_run !== false) a.push('--dry-run')
|
|
111
|
+
if (args.prune) a.push('--prune')
|
|
112
|
+
if (args.revision) a.push('--revision', args.revision)
|
|
113
|
+
const r = await exec('argocd', a, { timeout: 120000 })
|
|
114
|
+
const prefix = args.dry_run !== false ? '[dry-run] ' : ''
|
|
115
|
+
if (r.code !== 0) return { ok: false, text: `${prefix}argocd sync 失败:${r.stderr}` }
|
|
116
|
+
return { ok: true, text: trimOutput(`${prefix}${r.stdout}`) }
|
|
117
|
+
},
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
register({
|
|
121
|
+
name: 'ops_argocd_rollback',
|
|
122
|
+
description: '回滚应用到某个历史版本。需先关闭 auto-sync。',
|
|
123
|
+
parameters: {
|
|
124
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
125
|
+
app: { type: 'string', description: '应用名称', required: true },
|
|
126
|
+
id: { type: 'number', description: '历史版本 ID(从 ops_argocd_app 的历史中获取)', required: true },
|
|
127
|
+
},
|
|
128
|
+
output: textOutput,
|
|
129
|
+
async execute(args) {
|
|
130
|
+
const inst = findInstance(args.instance)
|
|
131
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
132
|
+
const a = buildArgocdArgs(inst, ['app', 'rollback', args.app, String(args.id)])
|
|
133
|
+
const r = await exec('argocd', a, { timeout: 60000 })
|
|
134
|
+
if (r.code !== 0) return { ok: false, text: `argocd rollback 失败:${r.stderr}` }
|
|
135
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
register({
|
|
140
|
+
name: 'ops_argocd_refresh',
|
|
141
|
+
description: '刷新应用:重新比较 Git 清单与集群状态(hard 时重新生成清单)。',
|
|
142
|
+
parameters: {
|
|
143
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
144
|
+
app: { type: 'string', description: '应用名称', required: true },
|
|
145
|
+
hard: { type: 'boolean', description: '是否 hard refresh(重新渲染 Helm/Kustomize)' },
|
|
146
|
+
},
|
|
147
|
+
output: textOutput,
|
|
148
|
+
async execute(args) {
|
|
149
|
+
const inst = findInstance(args.instance)
|
|
150
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
151
|
+
const a = buildArgocdArgs(inst, ['app', 'get', args.app, '--refresh'])
|
|
152
|
+
if (args.hard) a.push('--hard-refresh')
|
|
153
|
+
const r = await exec('argocd', a, { timeout: 30000 })
|
|
154
|
+
if (r.code !== 0) return { ok: false, text: `argocd refresh 失败:${r.stderr}` }
|
|
155
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
register({
|
|
160
|
+
name: 'ops_argocd_login',
|
|
161
|
+
description: '登录 Argo CD 实例(使用 argocd login 命令)。',
|
|
162
|
+
parameters: {
|
|
163
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
164
|
+
username: { type: 'string', description: '用户名(如 admin)' },
|
|
165
|
+
password: { type: 'string', description: '密码' },
|
|
166
|
+
sso: { type: 'boolean', description: '是否使用 SSO(浏览器登录)' },
|
|
167
|
+
},
|
|
168
|
+
output: textOutput,
|
|
169
|
+
async execute(args) {
|
|
170
|
+
const inst = findInstance(args.instance)
|
|
171
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
172
|
+
const a = ['login', inst.server, '--grpc-web']
|
|
173
|
+
if (args.sso) {
|
|
174
|
+
a.push('--sso')
|
|
175
|
+
} else if (args.username && args.password) {
|
|
176
|
+
a.push('--username', args.username, '--password', args.password)
|
|
177
|
+
} else {
|
|
178
|
+
return { ok: false, text: '请提供 username+password 或设置 sso=true' }
|
|
179
|
+
}
|
|
180
|
+
a.push('--insecure')
|
|
181
|
+
const r = await exec('argocd', a, { timeout: 60000 })
|
|
182
|
+
if (r.code !== 0) return { ok: false, text: `argocd login 失败:${r.stderr}` }
|
|
183
|
+
return { ok: true, text: '登录成功' }
|
|
184
|
+
},
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
register({
|
|
188
|
+
name: 'ops_argocd_instances',
|
|
189
|
+
description: '管理 Argo CD 实例(列出/添加/删除)。',
|
|
190
|
+
parameters: {
|
|
191
|
+
action: { type: 'string', description: '操作(默认 list):list / add / remove' },
|
|
192
|
+
name: { type: 'string', description: 'add/remove 时的实例名称' },
|
|
193
|
+
server: { type: 'string', description: 'add 时的服务器地址(如 argocd.example.com)' },
|
|
194
|
+
},
|
|
195
|
+
output: textOutput,
|
|
196
|
+
async execute(args) {
|
|
197
|
+
const action = args.action || 'list'
|
|
198
|
+
if (action === 'list') {
|
|
199
|
+
const instances = loadInstances()
|
|
200
|
+
if (!instances.length) return { ok: true, text: '没有已保存的 ArgoCD 实例。用 ops_argocd_instances action=add 添加。' }
|
|
201
|
+
const lines = instances.map((i) =>
|
|
202
|
+
`${i.name} server=${i.server}` +
|
|
203
|
+
(i.tags?.length ? ` [${i.tags.join(', ')}]` : '') +
|
|
204
|
+
(i.note ? ` — ${i.note}` : '')
|
|
205
|
+
)
|
|
206
|
+
return { ok: true, text: lines.join('\n') }
|
|
207
|
+
}
|
|
208
|
+
if (action === 'add') {
|
|
209
|
+
if (!args.name || !args.server) return { ok: false, text: '需要 name 和 server 参数' }
|
|
210
|
+
const r = addInstance({ name: args.name, server: args.server })
|
|
211
|
+
return { ok: r.ok, text: r.ok ? `已添加实例 ${args.name}` : r.why }
|
|
212
|
+
}
|
|
213
|
+
if (action === 'remove') {
|
|
214
|
+
if (!args.name) return { ok: false, text: '需要 name 参数' }
|
|
215
|
+
const r = removeInstance(args.name)
|
|
216
|
+
return { ok: r.ok, text: r.ok ? `已删除实例 ${args.name}` : r.why }
|
|
217
|
+
}
|
|
218
|
+
return { ok: false, text: `未知操作 ${action}` }
|
|
219
|
+
},
|
|
220
|
+
})
|
|
221
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { readJson, writeJson, dataDir } from '../cli.mjs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { mkdirSync } from 'node:fs'
|
|
4
|
+
|
|
5
|
+
const instancesFile = () => join(dataDir(), 'argowf', 'instances.json')
|
|
6
|
+
|
|
7
|
+
export function loadInstances() {
|
|
8
|
+
return readJson(instancesFile(), [])
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function saveInstances(instances) {
|
|
12
|
+
mkdirSync(join(dataDir(), 'argowf'), { recursive: true })
|
|
13
|
+
writeJson(instancesFile(), instances)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function findInstance(name) {
|
|
17
|
+
const instances = loadInstances()
|
|
18
|
+
return instances.find((i) => i.name === name) || null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function addInstance({ name, server, namespace, tags, note }) {
|
|
22
|
+
const instances = loadInstances()
|
|
23
|
+
if (instances.find((i) => i.name === name)) {
|
|
24
|
+
return { ok: false, why: `实例 ${name} 已存在` }
|
|
25
|
+
}
|
|
26
|
+
instances.push({
|
|
27
|
+
name,
|
|
28
|
+
server,
|
|
29
|
+
namespace: namespace || 'argo',
|
|
30
|
+
tags: tags || [],
|
|
31
|
+
note: note || '',
|
|
32
|
+
addedAt: new Date().toISOString(),
|
|
33
|
+
})
|
|
34
|
+
saveInstances(instances)
|
|
35
|
+
return { ok: true }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function removeInstance(name) {
|
|
39
|
+
const instances = loadInstances()
|
|
40
|
+
const idx = instances.findIndex((i) => i.name === name)
|
|
41
|
+
if (idx === -1) return { ok: false, why: `实例 ${name} 不存在` }
|
|
42
|
+
instances.splice(idx, 1)
|
|
43
|
+
saveInstances(instances)
|
|
44
|
+
return { ok: true }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function buildArgoArgs(instance, extraArgs = []) {
|
|
48
|
+
const args = ['--server', instance.server]
|
|
49
|
+
if (instance.namespace) args.push('-n', instance.namespace)
|
|
50
|
+
args.push(...extraArgs)
|
|
51
|
+
return args
|
|
52
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { exec, trimOutput } from '../cli.mjs'
|
|
2
|
+
import { loadInstances, findInstance, addInstance, removeInstance, buildArgoArgs } from './instances.mjs'
|
|
3
|
+
|
|
4
|
+
export function registerArgowfTools(ctx, defineTool, toolsSvc, config) {
|
|
5
|
+
const textOutput = {
|
|
6
|
+
schema: { type: 'object', properties: {}, additionalProperties: true },
|
|
7
|
+
render: (_args, value) => [{ type: 'text', text: String(value?.text ?? '') }],
|
|
8
|
+
}
|
|
9
|
+
const register = (tool) => toolsSvc.register(defineTool(tool))
|
|
10
|
+
|
|
11
|
+
register({
|
|
12
|
+
name: 'ops_argowf_list',
|
|
13
|
+
description: '列出 Argo Workflows 工作流(最新在前)。',
|
|
14
|
+
parameters: {
|
|
15
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
16
|
+
namespace: { type: 'string', description: '命名空间(覆盖实例默认值)' },
|
|
17
|
+
status: { type: 'string', enum: ['Running', 'Succeeded', 'Failed', 'Error', 'Pending'], description: '按状态过滤' },
|
|
18
|
+
label_selector: { type: 'string', description: '标签选择器' },
|
|
19
|
+
limit: { type: 'number', description: '返回数量限制,默认 50' },
|
|
20
|
+
},
|
|
21
|
+
output: textOutput,
|
|
22
|
+
async execute(args) {
|
|
23
|
+
const inst = findInstance(args.instance)
|
|
24
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
25
|
+
const a = buildArgoArgs(inst, ['list'])
|
|
26
|
+
if (args.namespace) { a.splice(a.indexOf('-n'), 2); a.push('-n', args.namespace) }
|
|
27
|
+
if (args.status) a.push('--status', args.status)
|
|
28
|
+
if (args.label_selector) a.push('-l', args.label_selector)
|
|
29
|
+
a.push('--chunk-size', String(args.limit || 50))
|
|
30
|
+
const r = await exec('argo', a, { timeout: 30000 })
|
|
31
|
+
if (r.code !== 0) return { ok: false, text: `argo list 失败:${r.stderr}` }
|
|
32
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
register({
|
|
37
|
+
name: 'ops_argowf_get',
|
|
38
|
+
description: '查看工作流的详情(阶段、参数、节点树)。',
|
|
39
|
+
parameters: {
|
|
40
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
41
|
+
name: { type: 'string', description: '工作流名称', required: true },
|
|
42
|
+
namespace: { type: 'string', description: '命名空间' },
|
|
43
|
+
},
|
|
44
|
+
output: textOutput,
|
|
45
|
+
async execute(args) {
|
|
46
|
+
const inst = findInstance(args.instance)
|
|
47
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
48
|
+
const a = buildArgoArgs(inst, ['get', args.name])
|
|
49
|
+
if (args.namespace) { a.splice(a.indexOf('-n'), 2); a.push('-n', args.namespace) }
|
|
50
|
+
const r = await exec('argo', a, { timeout: 30000 })
|
|
51
|
+
if (r.code !== 0) return { ok: false, text: `argo get 失败:${r.stderr}` }
|
|
52
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
register({
|
|
57
|
+
name: 'ops_argowf_diagnose',
|
|
58
|
+
description: '诊断失败的工作流:找出失败步骤、分类原因、获取日志。',
|
|
59
|
+
parameters: {
|
|
60
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
61
|
+
name: { type: 'string', description: '工作流名称', required: true },
|
|
62
|
+
namespace: { type: 'string', description: '命名空间' },
|
|
63
|
+
},
|
|
64
|
+
output: textOutput,
|
|
65
|
+
async execute(args) {
|
|
66
|
+
const inst = findInstance(args.instance)
|
|
67
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
68
|
+
const sections = []
|
|
69
|
+
const nsArgs = args.namespace ? ['-n', args.namespace] : (inst.namespace ? ['-n', inst.namespace] : [])
|
|
70
|
+
|
|
71
|
+
const getR = await exec('argo', ['get', args.name, '--server', inst.server, ...nsArgs], { timeout: 30000 })
|
|
72
|
+
if (getR.code === 0) sections.push('=== 工作流状态 ===\n' + getR.stdout)
|
|
73
|
+
|
|
74
|
+
const logsR = await exec('argo', ['logs', args.name, '--server', inst.server, ...nsArgs, '--no-color'], { timeout: 30000 })
|
|
75
|
+
if (logsR.code === 0 && logsR.stdout) {
|
|
76
|
+
const lines = logsR.stdout.split('\n')
|
|
77
|
+
const tail = lines.slice(-100).join('\n')
|
|
78
|
+
sections.push('=== 最近日志(尾部100行)===\n' + tail)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { ok: true, text: trimOutput(sections.join('\n\n')) }
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
register({
|
|
86
|
+
name: 'ops_argowf_logs',
|
|
87
|
+
description: '查看工作流某个步骤的日志。',
|
|
88
|
+
parameters: {
|
|
89
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
90
|
+
name: { type: 'string', description: '工作流名称', required: true },
|
|
91
|
+
node: { type: 'string', description: '步骤名称或 Pod 名称' },
|
|
92
|
+
namespace: { type: 'string', description: '命名空间' },
|
|
93
|
+
container: { type: 'string', description: '容器名,默认 main' },
|
|
94
|
+
grep: { type: 'string', description: '过滤日志中包含此文本的行' },
|
|
95
|
+
},
|
|
96
|
+
output: textOutput,
|
|
97
|
+
async execute(args) {
|
|
98
|
+
const inst = findInstance(args.instance)
|
|
99
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
100
|
+
const a = buildArgoArgs(inst, ['logs', args.name, '--no-color'])
|
|
101
|
+
if (args.namespace) { a.splice(a.indexOf('-n'), 2); a.push('-n', args.namespace) }
|
|
102
|
+
if (args.node) a.push('--node-id', args.node)
|
|
103
|
+
if (args.container) a.push('-c', args.container)
|
|
104
|
+
if (args.grep) a.push('--grep', args.grep)
|
|
105
|
+
const r = await exec('argo', a, { timeout: 30000 })
|
|
106
|
+
if (r.code !== 0) return { ok: false, text: `argo logs 失败:${r.stderr}` }
|
|
107
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
register({
|
|
112
|
+
name: 'ops_argowf_submit',
|
|
113
|
+
description: '提交 WorkflowTemplate 运行。parameters 传 JSON 字符串,如 \'{"key":"value"}\'。',
|
|
114
|
+
parameters: {
|
|
115
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
116
|
+
template: { type: 'string', description: 'WorkflowTemplate 名称', required: true },
|
|
117
|
+
namespace: { type: 'string', description: '命名空间' },
|
|
118
|
+
parameters: { type: 'string', description: '参数 JSON 对象字符串,如 {"key":"value"}' },
|
|
119
|
+
dry_run: { type: 'boolean', description: '仅验证,不创建' },
|
|
120
|
+
},
|
|
121
|
+
output: textOutput,
|
|
122
|
+
async execute(args) {
|
|
123
|
+
const inst = findInstance(args.instance)
|
|
124
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
125
|
+
const a = buildArgoArgs(inst, ['submit', '--from', `wftmpl/${args.template}`])
|
|
126
|
+
if (args.namespace) { a.splice(a.indexOf('-n'), 2); a.push('-n', args.namespace) }
|
|
127
|
+
if (args.parameters) {
|
|
128
|
+
try {
|
|
129
|
+
const params = typeof args.parameters === 'string' ? JSON.parse(args.parameters) : args.parameters
|
|
130
|
+
for (const [k, v] of Object.entries(params)) {
|
|
131
|
+
a.push('-p', `${k}=${v}`)
|
|
132
|
+
}
|
|
133
|
+
} catch (e) {
|
|
134
|
+
return { ok: false, text: `参数 JSON 解析失败:${e.message}` }
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (args.dry_run) a.push('--dry-run')
|
|
138
|
+
const r = await exec('argo', a, { timeout: 30000 })
|
|
139
|
+
if (r.code !== 0) return { ok: false, text: `argo submit 失败:${r.stderr}` }
|
|
140
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
141
|
+
},
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
register({
|
|
145
|
+
name: 'ops_argowf_retry',
|
|
146
|
+
description: '重试失败的工作流(重跑失败和错误的步骤)。先用 ops_argowf_diagnose 确认原因。',
|
|
147
|
+
parameters: {
|
|
148
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
149
|
+
name: { type: 'string', description: '工作流名称', required: true },
|
|
150
|
+
namespace: { type: 'string', description: '命名空间' },
|
|
151
|
+
},
|
|
152
|
+
output: textOutput,
|
|
153
|
+
async execute(args) {
|
|
154
|
+
const inst = findInstance(args.instance)
|
|
155
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
156
|
+
const a = buildArgoArgs(inst, ['retry', args.name])
|
|
157
|
+
if (args.namespace) { a.splice(a.indexOf('-n'), 2); a.push('-n', args.namespace) }
|
|
158
|
+
const r = await exec('argo', a, { timeout: 30000 })
|
|
159
|
+
if (r.code !== 0) return { ok: false, text: `argo retry 失败:${r.stderr}` }
|
|
160
|
+
return { ok: true, text: trimOutput(r.stdout) }
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
register({
|
|
165
|
+
name: 'ops_argowf_stop',
|
|
166
|
+
description: '停止运行中的工作流(会运行 onExit 处理器)。',
|
|
167
|
+
parameters: {
|
|
168
|
+
instance: { type: 'string', description: '实例名称', required: true },
|
|
169
|
+
name: { type: 'string', description: '工作流名称', required: true },
|
|
170
|
+
namespace: { type: 'string', description: '命名空间' },
|
|
171
|
+
},
|
|
172
|
+
output: textOutput,
|
|
173
|
+
async execute(args) {
|
|
174
|
+
const inst = findInstance(args.instance)
|
|
175
|
+
if (!inst) return { ok: false, text: `未找到实例 ${args.instance}` }
|
|
176
|
+
const a = buildArgoArgs(inst, ['stop', args.name])
|
|
177
|
+
if (args.namespace) { a.splice(a.indexOf('-n'), 2); a.push('-n', args.namespace) }
|
|
178
|
+
const r = await exec('argo', a, { timeout: 30000 })
|
|
179
|
+
if (r.code !== 0) return { ok: false, text: `argo stop 失败:${r.stderr}` }
|
|
180
|
+
return { ok: true, text: r.stdout || `已停止 ${args.name}` }
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
register({
|
|
185
|
+
name: 'ops_argowf_instances',
|
|
186
|
+
description: '管理 Argo Workflows 实例(列出/添加/删除)。',
|
|
187
|
+
parameters: {
|
|
188
|
+
action: { type: 'string', enum: ['list', 'add', 'remove'], description: '操作(默认 list)' },
|
|
189
|
+
name: { type: 'string', description: 'add/remove 时的实例名称' },
|
|
190
|
+
server: { type: 'string', description: 'add 时的服务器地址' },
|
|
191
|
+
namespace: { type: 'string', description: 'add 时的默认命名空间' },
|
|
192
|
+
},
|
|
193
|
+
output: textOutput,
|
|
194
|
+
async execute(args) {
|
|
195
|
+
const action = args.action || 'list'
|
|
196
|
+
if (action === 'list') {
|
|
197
|
+
const instances = loadInstances()
|
|
198
|
+
if (!instances.length) return { ok: true, text: '没有已保存的 Argo Workflows 实例。用 ops_argowf_instances action=add 添加。' }
|
|
199
|
+
const lines = instances.map((i) =>
|
|
200
|
+
`${i.name} server=${i.server} ns=${i.namespace}` +
|
|
201
|
+
(i.tags?.length ? ` [${i.tags.join(', ')}]` : '') +
|
|
202
|
+
(i.note ? ` — ${i.note}` : '')
|
|
203
|
+
)
|
|
204
|
+
return { ok: true, text: lines.join('\n') }
|
|
205
|
+
}
|
|
206
|
+
if (action === 'add') {
|
|
207
|
+
if (!args.name || !args.server) return { ok: false, text: '需要 name 和 server 参数' }
|
|
208
|
+
const r = addInstance({ name: args.name, server: args.server, namespace: args.namespace })
|
|
209
|
+
return { ok: r.ok, text: r.ok ? `已添加实例 ${args.name}` : r.why }
|
|
210
|
+
}
|
|
211
|
+
if (action === 'remove') {
|
|
212
|
+
if (!args.name) return { ok: false, text: '需要 name 参数' }
|
|
213
|
+
const r = removeInstance(args.name)
|
|
214
|
+
return { ok: r.ok, text: r.ok ? `已删除实例 ${args.name}` : r.why }
|
|
215
|
+
}
|
|
216
|
+
return { ok: false, text: `未知操作 ${action}` }
|
|
217
|
+
},
|
|
218
|
+
})
|
|
219
|
+
}
|
package/lib/cli.mjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { homedir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
const DATA_DIR = join(homedir(), '.dsh', 'dsh-ops')
|
|
7
|
+
|
|
8
|
+
export function ensureDataDir() {
|
|
9
|
+
mkdirSync(DATA_DIR, { recursive: true })
|
|
10
|
+
return DATA_DIR
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function dataDir() {
|
|
14
|
+
return DATA_DIR
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function readJson(path, fallback = null) {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(readFileSync(path, 'utf8'))
|
|
20
|
+
} catch {
|
|
21
|
+
return fallback
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function writeJson(path, data) {
|
|
26
|
+
mkdirSync(join(path, '..'), { recursive: true })
|
|
27
|
+
writeFileSync(path, JSON.stringify(data, null, 2), 'utf8')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function exec(bin, args, { timeout = 30000, stdin, env } = {}) {
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
const chunks = []
|
|
33
|
+
const errChunks = []
|
|
34
|
+
let done = false
|
|
35
|
+
let child
|
|
36
|
+
let timer
|
|
37
|
+
|
|
38
|
+
const finish = (res) => {
|
|
39
|
+
if (done) return
|
|
40
|
+
done = true
|
|
41
|
+
try { clearTimeout(timer) } catch {}
|
|
42
|
+
resolve(res)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
child = spawn(bin, args, {
|
|
47
|
+
windowsHide: true,
|
|
48
|
+
env: env ? { ...process.env, ...env } : process.env,
|
|
49
|
+
stdio: [stdin ? 'pipe' : 'ignore', 'pipe', 'pipe'],
|
|
50
|
+
})
|
|
51
|
+
} catch (e) {
|
|
52
|
+
finish({ code: -1, stdout: '', stderr: `启动失败:${e?.message ?? e}` })
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (stdin && child.stdin) {
|
|
57
|
+
child.stdin.write(stdin)
|
|
58
|
+
child.stdin.end()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
timer = setTimeout(() => {
|
|
62
|
+
try { child.kill('SIGKILL') } catch {}
|
|
63
|
+
finish({
|
|
64
|
+
code: -2,
|
|
65
|
+
stdout: Buffer.concat(chunks).toString('utf8').trim(),
|
|
66
|
+
stderr: `超时 ${Math.round(timeout / 1000)}s`,
|
|
67
|
+
})
|
|
68
|
+
}, timeout)
|
|
69
|
+
|
|
70
|
+
child.stdout.on('data', (d) => chunks.push(d))
|
|
71
|
+
child.stderr.on('data', (d) => errChunks.push(d))
|
|
72
|
+
child.on('error', (e) => {
|
|
73
|
+
finish({ code: -1, stdout: '', stderr: String(e?.message ?? e) })
|
|
74
|
+
})
|
|
75
|
+
child.on('close', (code) => {
|
|
76
|
+
finish({
|
|
77
|
+
code: code ?? -1,
|
|
78
|
+
stdout: Buffer.concat(chunks).toString('utf8').trim(),
|
|
79
|
+
stderr: Buffer.concat(errChunks).toString('utf8').trim(),
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function which(bin) {
|
|
86
|
+
const r = await exec('which', [bin], { timeout: 5000 })
|
|
87
|
+
if (r.code === 0 && r.stdout) return r.stdout.split('\n')[0].trim()
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function trimOutput(text, maxLen = 4000) {
|
|
92
|
+
const s = String(text ?? '')
|
|
93
|
+
if (s.length <= maxLen) return s
|
|
94
|
+
return s.slice(0, maxLen) + `\n\n…(全文 ${s.length} 字,已截断前 ${maxLen} 字)`
|
|
95
|
+
}
|