compshare-cli 0.3.2__tar.gz → 0.3.4__tar.gz

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.
Files changed (58) hide show
  1. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/PKG-INFO +167 -22
  2. compshare_cli-0.3.4/README.md +375 -0
  3. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/pyproject.toml +4 -1
  4. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/__init__.py +1 -1
  5. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/actions.py +15 -7
  6. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/api.py +28 -17
  7. compshare_cli-0.3.4/src/compshare_cli/cli.py +556 -0
  8. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/common.py +25 -2
  9. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/doctor.py +5 -0
  10. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/image.py +113 -43
  11. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/instance.py +1178 -82
  12. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/storage.py +12 -36
  13. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/team.py +41 -4
  14. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/config.py +46 -2
  15. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/i18n.py +210 -6
  16. compshare_cli-0.3.4/src/compshare_cli/instance_templates.py +202 -0
  17. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/location.py +38 -21
  18. compshare_cli-0.3.4/src/compshare_cli/output.py +362 -0
  19. compshare_cli-0.3.4/src/compshare_cli/remote_jobs.py +670 -0
  20. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/ssh.py +54 -9
  21. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli.egg-info/PKG-INFO +167 -22
  22. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli.egg-info/SOURCES.txt +5 -0
  23. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_actions.py +10 -5
  24. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_api.py +42 -0
  25. compshare_cli-0.3.4/tests/test_cli.py +3050 -0
  26. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_config.py +29 -0
  27. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_feedback.py +6 -1
  28. compshare_cli-0.3.4/tests/test_instance_templates.py +73 -0
  29. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_location.py +47 -0
  30. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_output.py +50 -2
  31. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_qa.py +7 -2
  32. compshare_cli-0.3.4/tests/test_release_version.py +19 -0
  33. compshare_cli-0.3.4/tests/test_remote_jobs.py +230 -0
  34. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_ssh.py +17 -0
  35. compshare_cli-0.3.2/README.md +0 -230
  36. compshare_cli-0.3.2/src/compshare_cli/cli.py +0 -318
  37. compshare_cli-0.3.2/src/compshare_cli/output.py +0 -187
  38. compshare_cli-0.3.2/tests/test_cli.py +0 -1608
  39. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/LICENSE +0 -0
  40. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/setup.cfg +0 -0
  41. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/__main__.py +0 -0
  42. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/__init__.py +0 -0
  43. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/ask.py +0 -0
  44. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/commands/feedback.py +0 -0
  45. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/errors.py +0 -0
  46. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/insights.py +0 -0
  47. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/parsing.py +0 -0
  48. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/qa.py +0 -0
  49. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/runtime.py +0 -0
  50. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/sdk.py +0 -0
  51. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli/ssh_cache.py +0 -0
  52. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli.egg-info/dependency_links.txt +0 -0
  53. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli.egg-info/entry_points.txt +0 -0
  54. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli.egg-info/requires.txt +0 -0
  55. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/src/compshare_cli.egg-info/top_level.txt +0 -0
  56. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_parsing.py +0 -0
  57. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_sdk.py +0 -0
  58. {compshare_cli-0.3.2 → compshare_cli-0.3.4}/tests/test_ssh_cache.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: compshare-cli
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Command-line client for CompShare GPU compute
5
5
  Author: CompShare
6
6
  License-Expression: Apache-2.0
@@ -29,19 +29,21 @@ Dynamic: license-file
29
29
 
30
30
  - 交互式创建实例,实时查询规格、库存和价格
31
31
  - 支持批量生命周期操作、等待状态和跨地域查询
32
+ - 支持可断线恢复的远程任务、状态查询和增量日志读取
32
33
  - 支持社区/自定义镜像、云盘、团队额度与账单
33
34
  - 默认中文帮助,可切换英文;所有命令支持 JSON 输出
34
35
  - 可通过 `compshare ask` 查询产品使用和计费知识
35
36
  - 可通过 `compshare feedback` 反馈 CLI 问题和建议
36
37
  - 基于官方 [`ucloud-sdk-python3`](https://github.com/ucloud/ucloud-sdk-python3)
37
38
 
38
- 当前版本:`0.3.1`,要求 Python 3.9 或更高版本。
39
+ 当前版本:`0.3.4`,要求 Python 3.9 或更高版本。
39
40
 
40
41
  ## 安装
41
42
 
42
43
  ```bash
43
44
  pip install compshare-cli
44
45
  compshare -h
46
+ compshare --version
45
47
  ```
46
48
 
47
49
  本地开发:
@@ -85,6 +87,9 @@ compshare config use production
85
87
  compshare --profile production instance list
86
88
  ```
87
89
 
90
+ `config list` 会明确标注当前凭证来自配置文件、环境变量或两者混合;JSON 输出还会分别说明
91
+ 公钥和私钥的来源,但不会显示凭证内容。
92
+
88
93
  也可以使用环境变量:
89
94
 
90
95
  ```bash
@@ -101,6 +106,11 @@ compshare instance search --region cn-sh2 --zone cn-sh2-02 \
101
106
  --gpu 4090 --image IMAGE_ID --available
102
107
  compshare instance list --all
103
108
 
109
+ # 聚焦查看实例信息;多个分组可组合
110
+ compshare instance show INSTANCE_ID --ip --softwares
111
+ compshare instance show INSTANCE_ID --spec --disks --billing
112
+ compshare instance show INSTANCE_ID --image --status
113
+
104
114
  # 交互式创建
105
115
  compshare instance create
106
116
 
@@ -109,6 +119,11 @@ compshare instance stop INSTANCE_1 INSTANCE_2
109
119
  compshare instance start INSTANCE_1 INSTANCE_2
110
120
  compshare instance wait INSTANCE_1 INSTANCE_2 --state Running
111
121
 
122
+ # 设置、查询和延期定时关机
123
+ compshare instance schedule set INSTANCE_ID --at 2h
124
+ compshare instance schedule show INSTANCE_ID
125
+ compshare instance schedule extend INSTANCE_ID --by 1h
126
+
112
127
  # SSH
113
128
  compshare instance ssh INSTANCE_ID
114
129
  # 默认自动填写 API 返回的密码;需要手动输入时可关闭
@@ -116,14 +131,21 @@ compshare instance ssh INSTANCE_ID --no-auto-password
116
131
  # 自动登录后执行远程命令;远程参数用 -- 与 CLI 选项分隔
117
132
  compshare instance ssh INSTANCE_ID -- nvidia-smi --query-gpu=name
118
133
  compshare instance ssh INSTANCE_ID -- 'cd /workspace && python train.py'
119
- # Agent/脚本:执行并返回 ok、phase、exit_code、stdout、stderr、error
134
+ # Agent/脚本:在统一 JSON 契约的 data 中返回 phase、exit_code、stdout、stderr
120
135
  compshare --json instance ssh INSTANCE_ID -- nvidia-smi
121
136
  # 强制刷新或禁用本地 SSH 连接缓存
122
137
  compshare instance ssh INSTANCE_ID --refresh
123
138
  compshare instance ssh INSTANCE_ID --no-cache
124
- # 上传本地文件或目录,目录会自动递归复制
125
- compshare instance scp INSTANCE_ID ./model.bin /workspace/model.bin
126
- compshare instance scp INSTANCE_ID ./dataset /workspace/dataset
139
+ # 在本地与实例间双向复制;实例路径以 : 开头,目录会自动递归复制
140
+ compshare instance cp INSTANCE_ID ./model.bin :/workspace/model.bin
141
+ compshare instance cp INSTANCE_ID ./dataset :/workspace/dataset
142
+ compshare instance cp INSTANCE_ID :/workspace/result ./result
143
+
144
+ # 长时间运行的远程任务;提交后立即返回 Job ID
145
+ compshare instance job submit INSTANCE_ID --name build --cwd /workspace/project -- make -j8
146
+ compshare instance job show INSTANCE_ID JOB_ID
147
+ compshare instance job logs INSTANCE_ID JOB_ID --follow
148
+ compshare instance job wait INSTANCE_ID JOB_ID --timeout 300
127
149
 
128
150
  # 产品问答
129
151
  compshare ask "按量实例关机以后,云硬盘还收费吗?"
@@ -148,16 +170,82 @@ compshare instance create \
148
170
 
149
171
  使用 `--dry-run` 只检查库存、价格和请求,不创建资源。
150
172
 
173
+ 本地实例模板可以保存常用创建参数,不依赖控制台或后端模板接口:
174
+
175
+ ```bash
176
+ compshare instance template create training-4090 \
177
+ --description '4090 训练环境' \
178
+ --gpu 4090 \
179
+ --count 1 \
180
+ --cpu 16 \
181
+ --memory 64GiB \
182
+ --image IMAGE_ID \
183
+ --region cn-sh2 \
184
+ --zone cn-sh2-02 \
185
+ --disk 100GiB \
186
+ --charge Postpay
187
+
188
+ compshare instance template list
189
+ compshare instance template show training-4090
190
+ compshare instance create --template training-4090 --yes
191
+
192
+ # 本次显式参数优先于模板值
193
+ compshare instance create --template training-4090 --cpu 32 --memory 128GiB --yes
194
+ ```
195
+
196
+ 模板存放在 `~/.config/compshare/instance-templates.json`;可用
197
+ `compshare instance template path` 查看实际路径。模板只保存实例创建参数,不保存 API
198
+ 凭证、`--yes`、`--show-sensitive` 或其他执行控制选项。
199
+
200
+ ## 远程任务
201
+
202
+ `instance ssh INSTANCE_ID -- COMMAND` 适合需要同步获取结果的短命令。安装、编译和计算等
203
+ 长任务可通过 `instance job` 在实例上脱离 SSH 运行,本地终端关闭或网络中断不会终止任务:
204
+
205
+ ```bash
206
+ # 普通命令及参数会被安全转义
207
+ compshare instance job submit INSTANCE_ID \
208
+ --name training \
209
+ --cwd /workspace/project \
210
+ -- python train.py --epochs 100
211
+
212
+ # 管道、重定向等 Shell 语法应显式使用 sh -lc
213
+ compshare instance job submit INSTANCE_ID --name install -- \
214
+ sh -lc 'apt-get update && apt-get install -y ffmpeg'
215
+
216
+ compshare instance job list INSTANCE_ID
217
+ compshare instance job show INSTANCE_ID JOB_ID
218
+ compshare instance job logs INSTANCE_ID JOB_ID --tail 200
219
+ compshare instance job logs INSTANCE_ID JOB_ID --follow
220
+ compshare instance job wait INSTANCE_ID JOB_ID --timeout 300
221
+ compshare instance job cancel INSTANCE_ID JOB_ID --yes
222
+ compshare instance job prune INSTANCE_ID --older-than 7d --yes
223
+ ```
224
+
225
+ Agent 可按字节偏移增量读取 stdout 和 stderr;响应中的 `StdoutNextOffset`、
226
+ `StderrNextOffset` 可直接用于下一次请求:
227
+
228
+ ```bash
229
+ compshare --json instance job logs INSTANCE_ID JOB_ID \
230
+ --stdout-offset 0 --stderr-offset 0 --limit 65536
231
+ ```
232
+
233
+ 任务记录遵循 XDG Base Directory 规范,保存在实例 SSH 用户的
234
+ `${XDG_STATE_HOME:-$HOME/.local/state}/compshare/jobs/JOB_ID/`。目录权限为 `0700`,文件权限为
235
+ `0600`。任务可以抵抗本地断线,但实例关机或重启会将未完成任务标记为 `Interrupted`;实例重装
236
+ 或删除会移除系统盘上的任务记录和日志,重要产物应写入数据盘或共享存储。
237
+
151
238
  ## 功能入口
152
239
 
153
240
  ```text
241
+ compshare config API 凭证配置
242
+ compshare feedback 反馈 CLI 问题或建议
243
+ compshare doctor 配置、鉴权、网络与 SSH 环境诊断
244
+ compshare ask 产品使用和计费问答
154
245
  compshare instance GPU 实例、规格、库存、价格和生命周期
155
246
  compshare image 平台、自定义、社区及共享镜像
156
247
  compshare storage 云盘和 US3 挂载
157
248
  compshare team 团队、邀请、成员额度、账单和审计
158
- compshare doctor 配置、鉴权、网络与 SSH 环境诊断
159
- compshare ask 产品使用和计费问答
160
- compshare feedback 反馈 CLI 问题或建议
161
249
  ```
162
250
 
163
251
  ## 产品问答
@@ -191,57 +279,114 @@ compshare instance -h
191
279
  compshare instance create -h
192
280
  ```
193
281
 
282
+ ## 命令补全
283
+
284
+ CLI 支持 Bash、Zsh、Fish 和 PowerShell 命令补全。为当前 Shell 自动安装:
285
+
286
+ ```bash
287
+ compshare --install-completion
288
+ ```
289
+
194
290
  ## 脚本调用
195
291
 
196
- `--json`、`--profile` 和 `--show-sensitive` 是全局选项,必须放在子命令前:
292
+ `--json`、`--profile` 和 `--show-sensitive` 可以放在命令前,也可以放在常见的子命令参数位置;
293
+ `--` 之后的同名参数始终原样传给远程命令:
197
294
 
198
295
  ```bash
199
296
  compshare --json instance list --status Running --all
200
- compshare --profile production image list --source community --region cn-sh2 --zone cn-sh2-02
201
- compshare --json --show-sensitive instance show uhost-xxxxxxxx
297
+ compshare image list --source community --profile production
298
+ compshare instance show uhost-xxxxxxxx --json --show-sensitive
202
299
  ```
203
300
 
204
301
  JSON 模式不会启动交互向导。生命周期操作可使用 `--wait`、`--no-wait` 和
205
302
  `--timeout` 控制等待行为;部分批量操作失败时,CLI 返回非零退出码。默认输出会递归隐藏
206
303
  密码、IP、访问 URL 和登录命令;只有显式指定 `--show-sensitive` 才会输出原值。
304
+ `instance show` 可使用 `--ip`、`--softwares`、`--spec`、`--disks`、`--billing`、
305
+ `--image` 和 `--status` 聚焦输出一个或多个信息分组;不传这些选项时保持完整摘要。
306
+
307
+ 所有 JSON 命令只输出一个 UTF-8 文档,并使用相同的顶层契约。成功响应如下:
308
+
309
+ ```json
310
+ {
311
+ "ok": true,
312
+ "schema_version": "1",
313
+ "data": {"version": "0.3.4"}
314
+ }
315
+ ```
316
+
317
+ 错误响应中的 `code` 稳定且不受语言设置影响,适合 Agent 分支判断;`message` 用于显示,
318
+ 接口错误的 Action、RetCode 和请求 ID 放在 `details` 中:
319
+
320
+ ```json
321
+ {
322
+ "ok": false,
323
+ "schema_version": "1",
324
+ "error": {
325
+ "code": "api_error",
326
+ "message": "...",
327
+ "details": {
328
+ "action": "DescribeCompShareInstance",
329
+ "ret_code": 171,
330
+ "request_uuid": "..."
331
+ }
332
+ }
333
+ }
334
+ ```
335
+
336
+ 列表响应使用 `data.items`,总数、过滤数、偏移量和是否返回全部结果位于 `meta`;列表只返回
337
+ 表格对应的摘要字段,完整 README、镜像详情和实例详情应通过相应的 `show` 命令读取。
338
+ API 的 Action、RetCode 和请求 ID 也会移到 `meta`。发生默认脱敏时,
339
+ `meta.redacted_fields` 会列出被替换为 `***` 的 JSON 路径。业务字段本身仍沿用 API 返回的名称、
340
+ 类型和单位,本版本不做语义转换。
341
+
342
+ `--json --version` 和 `--json --help` 同样遵循上述契约;JSON 帮助直接返回命令、参数和子命令
343
+ 结构,不输出 Rich 终端表格。
207
344
 
208
345
  Region 和 Zone 是资源参数,放在对应子命令后:
209
346
 
210
347
  ```bash
211
- compshare instance list --region cn-sh2
348
+ compshare instance list --region cn-sh2 --zone cn-sh2-02
212
349
  compshare instance create --region cn-sh2 --zone cn-sh2-02
213
- compshare image list --source custom --region cn-sh2 --zone cn-sh2-02
350
+ compshare image list --source custom
351
+ compshare storage disk list
214
352
  ```
215
353
 
216
- CLI 不保存或自动注入默认 Region/Zone。非交互式地域资源操作需要显式传入;按实例 ID
354
+ 镜像列表、网络检测和软件端口列表可进行全局查询,Region/Zone 只是可选过滤条件。CLI
355
+ 不保存或自动注入默认 Region/Zone;确实依赖地域的资源操作仍需显式传入。按实例 ID
217
356
  执行的生命周期操作使用 `DescribeCompShareInstance` 响应中的 Region 和 Zone;响应缺少任一
218
357
  字段时会停止操作,不会从 Zone 推导或用请求 Region 补齐。
219
358
 
220
359
  ## 语言
221
360
 
222
361
  ```bash
223
- compshare lang en
224
- compshare lang zh
225
- compshare lang # 查看当前语言
362
+ compshare --lang en --help
363
+ compshare --lang zh --help
226
364
  ```
227
365
 
228
- 也可以通过 `COMPSHARE_LANG=en` 临时覆盖。
366
+ `--lang` 会保存所选语言,后续命令继续使用;也可以通过 `COMPSHARE_LANG=en` 临时覆盖。
229
367
 
230
368
  ## 安全说明
231
369
 
232
370
  - 配置目录和文件分别使用 `0700`、`0600` 权限。
371
+ - 本地实例模板文件同样使用 `0600` 权限,所在目录使用 `0700` 权限。
233
372
  - API 私钥不会出现在命令输出中。
234
373
  - 默认使用 `***` 隐藏密码、私钥、IP、访问 URL、令牌和登录命令,包括嵌套 JSON 字段。
235
374
  - `instance ssh` 在支持的交互式终端中通过伪终端自动填写登录密码,不会打印密码或将其放入进程参数。
236
375
  - 自动登录会接受首次出现的 SSH 主机密钥;已记录主机的密钥发生变化时仍会拒绝连接。
237
376
  - `instance ssh INSTANCE_ID -- COMMAND` 可非交互执行远程命令,透传命令输出和退出码。
238
- - `--json instance ssh INSTANCE_ID -- COMMAND` 会真实执行命令,并结构化返回退出码、标准输出、标准错误及连接/认证错误阶段。
377
+ - `instance job submit` 使用 `nohup` 和独立进程会话在远端运行长任务,任务状态和日志不依赖
378
+ 本地 CLI 进程;取消任务和清理历史记录需要显式确认。
379
+ - `--json instance ssh INSTANCE_ID -- COMMAND` 会真实执行命令;成功信息位于 `data`,连接或认证失败使用统一的 `error` 对象。
239
380
  - `--json` 始终以 UTF-8 字节输出,不依赖 Windows 活动代码页或终端编码。
240
381
  - `instance ssh` 默认将 API 返回的 SSH 命令和密码按 profile/实例缓存 1 小时;Windows 使用当前用户的 DPAPI 加密密码,其他平台将缓存文件权限限制为当前用户。可用 `--refresh` 强制更新、`--no-cache` 禁用;重置密码和重装实例会清除对应缓存。
241
382
  - 实例创建是异步操作;`instance create` 默认等待到 `Running`,显式使用 `--no-wait` 才会在创建接口返回后立即退出。`instance ssh` 也会默认等待实例运行后再连接。
242
- - `instance scp INSTANCE_ID LOCAL_PATH REMOTE_PATH` 可自动认证并上传本地文件或目录。
383
+ - `instance cp INSTANCE_ID SOURCE DESTINATION` 可自动认证并双向复制文件或目录;实例侧路径
384
+ 使用 `:/path` 表示。旧的 `instance scp INSTANCE_ID LOCAL_PATH REMOTE_PATH` 上传语法继续兼容。
385
+ - `--json instance cp ...` 会真实执行复制,并在统一契约中返回 `phase`、`exit_code`、
386
+ `stdout` 和 `stderr`;只有显式使用 `--print` 才仅输出命令。
243
387
  - `--show-sensitive` 会恢复这些字段的原始值;请勿在共享终端、CI 日志或 Agent 会话中使用。
244
- - 删除、关机、重启、重装和改配等操作默认要求确认。
388
+ - 删除、关机、重启、重装和改配等操作默认要求输入 `y` 或 `n` 确认;空输入或无效输入
389
+ 最多重试三次。JSON 模式不启动确认提示,执行这类操作必须显式使用 `--yes`。
245
390
 
246
391
  ## 开发校验
247
392
 
@@ -0,0 +1,375 @@
1
+ # CompShare CLI
2
+
3
+ 在终端管理优云智算 GPU 实例、镜像、云盘、US3 挂载和团队资源。
4
+
5
+ - 交互式创建实例,实时查询规格、库存和价格
6
+ - 支持批量生命周期操作、等待状态和跨地域查询
7
+ - 支持可断线恢复的远程任务、状态查询和增量日志读取
8
+ - 支持社区/自定义镜像、云盘、团队额度与账单
9
+ - 默认中文帮助,可切换英文;所有命令支持 JSON 输出
10
+ - 可通过 `compshare ask` 查询产品使用和计费知识
11
+ - 可通过 `compshare feedback` 反馈 CLI 问题和建议
12
+ - 基于官方 [`ucloud-sdk-python3`](https://github.com/ucloud/ucloud-sdk-python3)
13
+
14
+ 当前版本:`0.3.4`,要求 Python 3.9 或更高版本。
15
+
16
+ ## 安装
17
+
18
+ ```bash
19
+ pip install compshare-cli
20
+ compshare -h
21
+ compshare --version
22
+ ```
23
+
24
+ 本地开发:
25
+
26
+ ```bash
27
+ git clone https://github.com/BennielAllan/compshare-cli.git
28
+ cd compshare-cli
29
+ python -m venv .venv
30
+ source .venv/bin/activate
31
+ python -m pip install -e '.[dev]'
32
+ ```
33
+
34
+ Windows PowerShell 中可先激活虚拟环境,再直接调用 `compshare`:
35
+
36
+ ```powershell
37
+ .\.venv\Scripts\Activate.ps1
38
+ compshare -h
39
+ compshare config --name default
40
+ ```
41
+
42
+ 不激活虚拟环境时,使用 PowerShell 调用运算符 `&` 直接执行虚拟环境内的入口:
43
+
44
+ ```powershell
45
+ & '.\.venv\Scripts\compshare.exe' -h
46
+ & '.\.venv\Scripts\compshare.exe' config --name default
47
+ ```
48
+
49
+ PowerShell 会把单引号中的路径解析为字符串,所以带参数执行时不能省略 `&`。路径包含空格时必须保留引号;从当前目录运行可执行文件时要使用 `.\` 前缀。若 `Activate.ps1` 被本机执行策略拦截,可直接使用上述不激活的写法,无需修改系统策略。
50
+
51
+ ## 配置
52
+
53
+ ```bash
54
+ compshare config --name default
55
+ ```
56
+
57
+ 凭证保存在 `~/.config/compshare/config.json`,支持多个配置:
58
+
59
+ ```bash
60
+ compshare config list
61
+ compshare config use production
62
+ compshare --profile production instance list
63
+ ```
64
+
65
+ `config list` 会明确标注当前凭证来自配置文件、环境变量或两者混合;JSON 输出还会分别说明
66
+ 公钥和私钥的来源,但不会显示凭证内容。
67
+
68
+ 也可以使用环境变量:
69
+
70
+ ```bash
71
+ export COMPSHARE_PUBLIC_KEY='...'
72
+ export COMPSHARE_PRIVATE_KEY='...'
73
+ ```
74
+
75
+ ## 快速开始
76
+
77
+ ```bash
78
+ # 查规格、库存和实例
79
+ compshare instance search --region cn-sh2 --zone cn-sh2-02
80
+ compshare instance search --region cn-sh2 --zone cn-sh2-02 \
81
+ --gpu 4090 --image IMAGE_ID --available
82
+ compshare instance list --all
83
+
84
+ # 聚焦查看实例信息;多个分组可组合
85
+ compshare instance show INSTANCE_ID --ip --softwares
86
+ compshare instance show INSTANCE_ID --spec --disks --billing
87
+ compshare instance show INSTANCE_ID --image --status
88
+
89
+ # 交互式创建
90
+ compshare instance create
91
+
92
+ # 批量操作与等待
93
+ compshare instance stop INSTANCE_1 INSTANCE_2
94
+ compshare instance start INSTANCE_1 INSTANCE_2
95
+ compshare instance wait INSTANCE_1 INSTANCE_2 --state Running
96
+
97
+ # 设置、查询和延期定时关机
98
+ compshare instance schedule set INSTANCE_ID --at 2h
99
+ compshare instance schedule show INSTANCE_ID
100
+ compshare instance schedule extend INSTANCE_ID --by 1h
101
+
102
+ # SSH
103
+ compshare instance ssh INSTANCE_ID
104
+ # 默认自动填写 API 返回的密码;需要手动输入时可关闭
105
+ compshare instance ssh INSTANCE_ID --no-auto-password
106
+ # 自动登录后执行远程命令;远程参数用 -- 与 CLI 选项分隔
107
+ compshare instance ssh INSTANCE_ID -- nvidia-smi --query-gpu=name
108
+ compshare instance ssh INSTANCE_ID -- 'cd /workspace && python train.py'
109
+ # Agent/脚本:在统一 JSON 契约的 data 中返回 phase、exit_code、stdout、stderr
110
+ compshare --json instance ssh INSTANCE_ID -- nvidia-smi
111
+ # 强制刷新或禁用本地 SSH 连接缓存
112
+ compshare instance ssh INSTANCE_ID --refresh
113
+ compshare instance ssh INSTANCE_ID --no-cache
114
+ # 在本地与实例间双向复制;实例路径以 : 开头,目录会自动递归复制
115
+ compshare instance cp INSTANCE_ID ./model.bin :/workspace/model.bin
116
+ compshare instance cp INSTANCE_ID ./dataset :/workspace/dataset
117
+ compshare instance cp INSTANCE_ID :/workspace/result ./result
118
+
119
+ # 长时间运行的远程任务;提交后立即返回 Job ID
120
+ compshare instance job submit INSTANCE_ID --name build --cwd /workspace/project -- make -j8
121
+ compshare instance job show INSTANCE_ID JOB_ID
122
+ compshare instance job logs INSTANCE_ID JOB_ID --follow
123
+ compshare instance job wait INSTANCE_ID JOB_ID --timeout 300
124
+
125
+ # 产品问答
126
+ compshare ask "按量实例关机以后,云硬盘还收费吗?"
127
+ ```
128
+
129
+ 自动化创建示例:
130
+
131
+ ```bash
132
+ compshare instance create \
133
+ --region cn-sh2 \
134
+ --zone cn-sh2-02 \
135
+ --gpu 4090 \
136
+ --count 1 \
137
+ --cpu 16 \
138
+ --memory 64GiB \
139
+ --image IMAGE_ID \
140
+ --disk 100GiB \
141
+ --charge Postpay \
142
+ --max-price 20 \
143
+ --yes
144
+ ```
145
+
146
+ 使用 `--dry-run` 只检查库存、价格和请求,不创建资源。
147
+
148
+ 本地实例模板可以保存常用创建参数,不依赖控制台或后端模板接口:
149
+
150
+ ```bash
151
+ compshare instance template create training-4090 \
152
+ --description '4090 训练环境' \
153
+ --gpu 4090 \
154
+ --count 1 \
155
+ --cpu 16 \
156
+ --memory 64GiB \
157
+ --image IMAGE_ID \
158
+ --region cn-sh2 \
159
+ --zone cn-sh2-02 \
160
+ --disk 100GiB \
161
+ --charge Postpay
162
+
163
+ compshare instance template list
164
+ compshare instance template show training-4090
165
+ compshare instance create --template training-4090 --yes
166
+
167
+ # 本次显式参数优先于模板值
168
+ compshare instance create --template training-4090 --cpu 32 --memory 128GiB --yes
169
+ ```
170
+
171
+ 模板存放在 `~/.config/compshare/instance-templates.json`;可用
172
+ `compshare instance template path` 查看实际路径。模板只保存实例创建参数,不保存 API
173
+ 凭证、`--yes`、`--show-sensitive` 或其他执行控制选项。
174
+
175
+ ## 远程任务
176
+
177
+ `instance ssh INSTANCE_ID -- COMMAND` 适合需要同步获取结果的短命令。安装、编译和计算等
178
+ 长任务可通过 `instance job` 在实例上脱离 SSH 运行,本地终端关闭或网络中断不会终止任务:
179
+
180
+ ```bash
181
+ # 普通命令及参数会被安全转义
182
+ compshare instance job submit INSTANCE_ID \
183
+ --name training \
184
+ --cwd /workspace/project \
185
+ -- python train.py --epochs 100
186
+
187
+ # 管道、重定向等 Shell 语法应显式使用 sh -lc
188
+ compshare instance job submit INSTANCE_ID --name install -- \
189
+ sh -lc 'apt-get update && apt-get install -y ffmpeg'
190
+
191
+ compshare instance job list INSTANCE_ID
192
+ compshare instance job show INSTANCE_ID JOB_ID
193
+ compshare instance job logs INSTANCE_ID JOB_ID --tail 200
194
+ compshare instance job logs INSTANCE_ID JOB_ID --follow
195
+ compshare instance job wait INSTANCE_ID JOB_ID --timeout 300
196
+ compshare instance job cancel INSTANCE_ID JOB_ID --yes
197
+ compshare instance job prune INSTANCE_ID --older-than 7d --yes
198
+ ```
199
+
200
+ Agent 可按字节偏移增量读取 stdout 和 stderr;响应中的 `StdoutNextOffset`、
201
+ `StderrNextOffset` 可直接用于下一次请求:
202
+
203
+ ```bash
204
+ compshare --json instance job logs INSTANCE_ID JOB_ID \
205
+ --stdout-offset 0 --stderr-offset 0 --limit 65536
206
+ ```
207
+
208
+ 任务记录遵循 XDG Base Directory 规范,保存在实例 SSH 用户的
209
+ `${XDG_STATE_HOME:-$HOME/.local/state}/compshare/jobs/JOB_ID/`。目录权限为 `0700`,文件权限为
210
+ `0600`。任务可以抵抗本地断线,但实例关机或重启会将未完成任务标记为 `Interrupted`;实例重装
211
+ 或删除会移除系统盘上的任务记录和日志,重要产物应写入数据盘或共享存储。
212
+
213
+ ## 功能入口
214
+
215
+ ```text
216
+ compshare config API 凭证配置
217
+ compshare feedback 反馈 CLI 问题或建议
218
+ compshare doctor 配置、鉴权、网络与 SSH 环境诊断
219
+ compshare ask 产品使用和计费问答
220
+ compshare instance GPU 实例、规格、库存、价格和生命周期
221
+ compshare image 平台、自定义、社区及共享镜像
222
+ compshare storage 云盘和 US3 挂载
223
+ compshare team 团队、邀请、成员额度、账单和审计
224
+ ```
225
+
226
+ ## 产品问答
227
+
228
+ ```bash
229
+ compshare ask "按量实例关机以后,云硬盘还收费吗?"
230
+ compshare --json ask "如何创建自定义镜像?"
231
+ ```
232
+
233
+ 普通输出显示答案和引用资料标题;`--json` 返回完整的答案、引用、请求 ID 和检索元数据。
234
+ 命令只向内置问答服务发送 `question` 字段,不会附带 API 凭证、配置文件或其他命令上下文。
235
+
236
+ ## 反馈
237
+
238
+ ```bash
239
+ compshare feedback bug "创建实例时发生错误"
240
+ compshare feedback suggest "希望支持保存默认创建规格"
241
+ ```
242
+
243
+ 反馈会直接发送到内置的 CompShare Insights 服务地址。开发环境可使用
244
+ `COMPSHARE_INSIGHTS_URL=http://127.0.0.1:18080` 覆盖服务地址。
245
+
246
+ CLI 会异步统计指令使用情况,且仅发送指令名称、CLI 版本、操作系统和发生时间;不会发送
247
+ 命令参数、凭证、资源 ID 或命令输出。
248
+
249
+ 查看完整命令和参数:
250
+
251
+ ```bash
252
+ compshare -h
253
+ compshare instance -h
254
+ compshare instance create -h
255
+ ```
256
+
257
+ ## 命令补全
258
+
259
+ CLI 支持 Bash、Zsh、Fish 和 PowerShell 命令补全。为当前 Shell 自动安装:
260
+
261
+ ```bash
262
+ compshare --install-completion
263
+ ```
264
+
265
+ ## 脚本调用
266
+
267
+ `--json`、`--profile` 和 `--show-sensitive` 可以放在命令前,也可以放在常见的子命令参数位置;
268
+ `--` 之后的同名参数始终原样传给远程命令:
269
+
270
+ ```bash
271
+ compshare --json instance list --status Running --all
272
+ compshare image list --source community --profile production
273
+ compshare instance show uhost-xxxxxxxx --json --show-sensitive
274
+ ```
275
+
276
+ JSON 模式不会启动交互向导。生命周期操作可使用 `--wait`、`--no-wait` 和
277
+ `--timeout` 控制等待行为;部分批量操作失败时,CLI 返回非零退出码。默认输出会递归隐藏
278
+ 密码、IP、访问 URL 和登录命令;只有显式指定 `--show-sensitive` 才会输出原值。
279
+ `instance show` 可使用 `--ip`、`--softwares`、`--spec`、`--disks`、`--billing`、
280
+ `--image` 和 `--status` 聚焦输出一个或多个信息分组;不传这些选项时保持完整摘要。
281
+
282
+ 所有 JSON 命令只输出一个 UTF-8 文档,并使用相同的顶层契约。成功响应如下:
283
+
284
+ ```json
285
+ {
286
+ "ok": true,
287
+ "schema_version": "1",
288
+ "data": {"version": "0.3.4"}
289
+ }
290
+ ```
291
+
292
+ 错误响应中的 `code` 稳定且不受语言设置影响,适合 Agent 分支判断;`message` 用于显示,
293
+ 接口错误的 Action、RetCode 和请求 ID 放在 `details` 中:
294
+
295
+ ```json
296
+ {
297
+ "ok": false,
298
+ "schema_version": "1",
299
+ "error": {
300
+ "code": "api_error",
301
+ "message": "...",
302
+ "details": {
303
+ "action": "DescribeCompShareInstance",
304
+ "ret_code": 171,
305
+ "request_uuid": "..."
306
+ }
307
+ }
308
+ }
309
+ ```
310
+
311
+ 列表响应使用 `data.items`,总数、过滤数、偏移量和是否返回全部结果位于 `meta`;列表只返回
312
+ 表格对应的摘要字段,完整 README、镜像详情和实例详情应通过相应的 `show` 命令读取。
313
+ API 的 Action、RetCode 和请求 ID 也会移到 `meta`。发生默认脱敏时,
314
+ `meta.redacted_fields` 会列出被替换为 `***` 的 JSON 路径。业务字段本身仍沿用 API 返回的名称、
315
+ 类型和单位,本版本不做语义转换。
316
+
317
+ `--json --version` 和 `--json --help` 同样遵循上述契约;JSON 帮助直接返回命令、参数和子命令
318
+ 结构,不输出 Rich 终端表格。
319
+
320
+ Region 和 Zone 是资源参数,放在对应子命令后:
321
+
322
+ ```bash
323
+ compshare instance list --region cn-sh2 --zone cn-sh2-02
324
+ compshare instance create --region cn-sh2 --zone cn-sh2-02
325
+ compshare image list --source custom
326
+ compshare storage disk list
327
+ ```
328
+
329
+ 镜像列表、网络检测和软件端口列表可进行全局查询,Region/Zone 只是可选过滤条件。CLI
330
+ 不保存或自动注入默认 Region/Zone;确实依赖地域的资源操作仍需显式传入。按实例 ID
331
+ 执行的生命周期操作使用 `DescribeCompShareInstance` 响应中的 Region 和 Zone;响应缺少任一
332
+ 字段时会停止操作,不会从 Zone 推导或用请求 Region 补齐。
333
+
334
+ ## 语言
335
+
336
+ ```bash
337
+ compshare --lang en --help
338
+ compshare --lang zh --help
339
+ ```
340
+
341
+ `--lang` 会保存所选语言,后续命令继续使用;也可以通过 `COMPSHARE_LANG=en` 临时覆盖。
342
+
343
+ ## 安全说明
344
+
345
+ - 配置目录和文件分别使用 `0700`、`0600` 权限。
346
+ - 本地实例模板文件同样使用 `0600` 权限,所在目录使用 `0700` 权限。
347
+ - API 私钥不会出现在命令输出中。
348
+ - 默认使用 `***` 隐藏密码、私钥、IP、访问 URL、令牌和登录命令,包括嵌套 JSON 字段。
349
+ - `instance ssh` 在支持的交互式终端中通过伪终端自动填写登录密码,不会打印密码或将其放入进程参数。
350
+ - 自动登录会接受首次出现的 SSH 主机密钥;已记录主机的密钥发生变化时仍会拒绝连接。
351
+ - `instance ssh INSTANCE_ID -- COMMAND` 可非交互执行远程命令,透传命令输出和退出码。
352
+ - `instance job submit` 使用 `nohup` 和独立进程会话在远端运行长任务,任务状态和日志不依赖
353
+ 本地 CLI 进程;取消任务和清理历史记录需要显式确认。
354
+ - `--json instance ssh INSTANCE_ID -- COMMAND` 会真实执行命令;成功信息位于 `data`,连接或认证失败使用统一的 `error` 对象。
355
+ - `--json` 始终以 UTF-8 字节输出,不依赖 Windows 活动代码页或终端编码。
356
+ - `instance ssh` 默认将 API 返回的 SSH 命令和密码按 profile/实例缓存 1 小时;Windows 使用当前用户的 DPAPI 加密密码,其他平台将缓存文件权限限制为当前用户。可用 `--refresh` 强制更新、`--no-cache` 禁用;重置密码和重装实例会清除对应缓存。
357
+ - 实例创建是异步操作;`instance create` 默认等待到 `Running`,显式使用 `--no-wait` 才会在创建接口返回后立即退出。`instance ssh` 也会默认等待实例运行后再连接。
358
+ - `instance cp INSTANCE_ID SOURCE DESTINATION` 可自动认证并双向复制文件或目录;实例侧路径
359
+ 使用 `:/path` 表示。旧的 `instance scp INSTANCE_ID LOCAL_PATH REMOTE_PATH` 上传语法继续兼容。
360
+ - `--json instance cp ...` 会真实执行复制,并在统一契约中返回 `phase`、`exit_code`、
361
+ `stdout` 和 `stderr`;只有显式使用 `--print` 才仅输出命令。
362
+ - `--show-sensitive` 会恢复这些字段的原始值;请勿在共享终端、CI 日志或 Agent 会话中使用。
363
+ - 删除、关机、重启、重装和改配等操作默认要求输入 `y` 或 `n` 确认;空输入或无效输入
364
+ 最多重试三次。JSON 模式不启动确认提示,执行这类操作必须显式使用 `--yes`。
365
+
366
+ ## 开发校验
367
+
368
+ ```bash
369
+ ruff check .
370
+ ruff format --check .
371
+ pytest
372
+ ```
373
+
374
+ 公开 API 覆盖范围和版本变化见 [CHANGELOG.md](CHANGELOG.md),维护者发布流程见
375
+ [RELEASING.md](RELEASING.md)。