命令接口
更新时间:2024-08-02
创建命令
创建命令,支持保存、仅执行、执行并保存
请求参数
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
execution | String | 是 | 执行动作。枚举值:SAVE(仅保存),RUN(仅执行),SAVE_AND_RUN(保存并执行) |
action | Action | 是 | 执行动作详情 |
parameters | Map | 否 | 执行命令时的参数值,仅在命令有参数且需要执行时需要 |
targetSelectorType | String | 否 | 实例选择器类型,仅在执行动作为执行或保存并执行时需要。默认值为INSTANCES_LIST。可选值:INSTANCES_LIST(实例列表),ALL_INSTANCES(全部实例),TAG_INSTANCES(实例标签选择),INSTANCES_IMPORT(实例列表导入) |
targets | List<Target> | 否 | 实例ID列表,仅在targetSelectorType为INSTANCES_LIST时需要 |
targetSelector | TargetSelector | 否 | 实例选择器 |
请求
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.ca import ca_client, ca_model
4
5if __name__ == '__main__':
6 HOST = b'http://ca.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),endpoint=HOST)
11 client = ca_client.CaClient(config)
12
13 user_id = ''
14 execution = 'SAVE_AND_RUN'
15 action = ca_model.Action(
16 "", "", "COMMAND", "yyy-python-test", 60,
17 ca_model.Command(
18 "SHELL", "ls", "INDIVIDUAL", False, [], "root", "/home"
19 )
20 )
21 parameter = {}
22 target_selector = ca_model.TargetSelector("BCC", [])
23 target_selector_type = "INSTANCES_LIST"
24 targets = [ca_model.Target("BCC", "i-kBdE8Tav")]
25 response = client.create_action(execution, user_id, action, targets, parameter, target_selector,
26 target_selector_type)
删除命令
删除已保存的命令
接口描述
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
id | String | 是 | 命令id |
响应参数
参数名称 | 类型 | 描述 |
---|---|---|
requestId | String | 请求id |
code | String | 响应状态,成功为success |
请求示例
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.ca import ca_client, ca_model
4
5if __name__ == '__main__':
6 HOST = b'http://ca.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),endpoint=HOST)
11 client = client.CaClient(config)
12
13 id = "c-xadYvQ1FSA5viD4E"
14 response = client.delete_action(id)
响应示例
JSON
1{
2 "requestId": "b7d01b02-40ea-4ed2-9969-925790******",
3 "code": "success",
4}
查询命令详情
查询已保存的命令或公共命令
请求参数
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
id | String | 是 | 命令ID |
user_id | String | 是 | 用户ID |
响应参数
参数名称 | 类型 | 描述 |
---|---|---|
requestId | String | 请求ID |
code | String | 响应状态,成功为success |
result | Action | 命令详情 |
请求示例
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.ca import ca_client, ca_model
4
5if __name__ == '__main__':
6 HOST = b'http://ca.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),endpoint=HOST)
11 client = ca_client.CaClient(config)
12
13 id = "c-k7zXV794Jjz1WZC8"
14 user_id = ""
15 response = client.get_action(id, user_id)
16 print(response)
响应示例
JSON
1{
2 "requestId" : "8f713916-d41b-472a-a06b-8a80cb******",
3 "code": "success",
4 "result": {
5 "id": "a-CwkIRFAaoUCx",
6 "name": "test_command",
7 "description": "安装和升级HAS-Agent到最新版本",
8 "timeoutSecond": 300,
9 "type": "COMMAND",
10 "command": {
11 "type": "SHELL",
12 "content": "",
13 "enableParameter": false,
14 "parameters": [
15 {
16 "name": "参数名",
17 "desc" : "desc"
18 }
19 ],
20 "user": "work",
21 "workDir": "/home/work/"
22 },
23 "createdTimestamp": 1659534994121,
24 "updatedTimestamp": 1659534994121
25 }
26}
修改我的命令
修改我的命令
请求参数
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
action | UpdateActionRequest | 是 | 执行动作详情 |
UpdateActionRequest
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
ref | String | 是 | 需要修改的命令ID |
name | String | 是 | 修改后的命令名称 |
description | String | 是 | 修改后的命令描述 |
command | Command | 是 | 修改后的命令 |
响应参数
参数名称 | 类型 | 描述 |
---|---|---|
requestId | String | 请求id |
code | String | 响应状态,成功为success |
请求示例
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.ca import ca_client, ca_model
4
5if __name__ == '__main__':
6 HOST = b'http://ca.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),endpoint=HOST)
11 client = ca_client.CaClient(config)
12
13 action = ca_model.Action(
14 "c-k7zXV794Jjz1WZC8", "", "COMMAND", "yyy-python-test-update", 60,
15 ca_model.Command(
16 "SHELL", "ls", "INDIVIDUAL", False, [], "root", "/home"
17 )
18 )
19 execution = "SAVE"
20 response = client.update_action(action, execution)
21 print(response)
响应示例
JSON
1{
2 "requestId": "be82abc6-eb4c-4a51-9f52-f59b97******",
3 "code": "success"
4}
命令列表查询
查询我的命令列表或公共命令列表
请求参数
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
action | ActionFilter | 是 | 命令过滤器 |
page_no | int | 是 | 页码 |
page_size | int | 是 | 页大小 |
sort | String | 否 | 排序字段,可选值createTime(命令创建时间) |
ascending | bool | 否 | 是否升序,默认false |
ActionFilter
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
command | CommandFilter | 是 | 执行动作详情 |
CommandFilter
参数名称 | 类型 | 是否必须 | 参数位置 | 描述 |
---|---|---|---|---|
scope | String | 是 | RequestBody参数 | 按命令可见范围筛选。枚举值:INDIVIDUAL(个人命令),GLOBAL(公共命令) |
type | String | 否 | RequestBody参数 | 按命令类型筛选。枚举值:SHELL,POWERSHELL |
name | String | 否 | RequestBody参数 | 按命令名称筛选 |
响应参数
公共响应体中的result如下:
参数名称 | 类型 | 描述 |
---|---|---|
pageNo | int | 页码 |
pageSize | int | 页大小 |
totalCount | int | 总数 |
data | List<Action> | 命令列表 |
请求示例
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.ca import ca_client, ca_model
4
5if __name__ == '__main__':
6 HOST = b'http://ca.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),endpoint=HOST)
11 client = ca_client.CaClient(config)
12
13 action = ca_model.Action(
14 "", "", "COMMAND", "", 0,
15 ca_model.Command(
16 None, "", "INDIVIDUAL", False, {}, "", ""
17 )
18 )
19 ascending = False
20 page_no = 1
21 page_size = 10
22 sort = "createTime"
23 user_id = ""
24
25 response = client.action_list(action, page_no, page_size, sort, ascending, user_id)
26 print(response)
响应示例
JSON
1{
2 "requestId": "ca1316b5-3fab-4c91-aeac-ad51fd******",
3 "code": "success",
4 "result": {
5 "pageNo": 1,
6 "pageSize": 10,
7 "totalCount": 1,
8 "data": [
9 {
10 "id": "c-4CtIumYWlo******",
11 "type": "COMMAND",
12 "name": "cmd_test_test",
13 "timeoutSecond": 30,
14 "command": {
15 "type": "SHELL",
16 "content": "ls",
17 "scope": "INDIVIDUAL",
18 "enableParameter": false,
19 "workDir": "/home",
20 "user": "root"
21 },
22 "createdTimestamp": 1694672762000,
23 "updatedTimestamp": 1694672762000
24 }
25 ]
26 }
27}
执行命令
执行已创建的我的命令或公共命令
请求参数
参数名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
action | Action | 是 | 所要执行的命令,仅需填写id |
parameters | Map | 否 | 执行命令时的参数值,仅在命令有参数时需要 |
targetSelectorType | String | 否 | 实例选择器类型。默认值为INSTANCES_LIST。INSTANCES_LIST(实例列表),ALL_INSTANCES(全部实例),TAG_INSTANCES(实例标签选择),INSTANCES_IMPORT(实例列表导入) |
targets | List<Target> | 否 | 实例ID列表,仅在targetSelectorType为INSTANCES_LIST时需要 |
targetSelector | TargetSelector | 否 | 实例选择器 |
响应参数
公共响应体中的result如下:
参数名称 | 类型 | 描述 |
---|---|---|
actionId | String | 命令ID |
runId | String | 执行ID |
请求示例
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.ca import ca_client, ca_model
4
5if __name__ == '__main__':
6 HOST = b'http://ca.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),endpoint=HOST)
11 client = ca_client.CaClient(config)
12
13 user_id = ''
14 action = ca_model.Action(
15 "c-4cMSvlmdfWp2GdA9"
16 )
17 parameter = {}
18 target_selector = ca_model.TargetSelector("BCC", [])
19 target_selector_type = "INSTANCES_LIST"
20 targets = [ca_model.Target("BCC", "i-ZCHupg0z")]
21 response = client.action_run(action, parameter, user_id, target_selector_type, targets, target_selector)
22 print(response)
响应示例
JSON
1{
2 "requestId": "87c25fbd-21c9-4082-904a-0d8c85******",
3 "code": "success",
4 "result": {
5 "runId": "r-2InH1HsWXU******",
6 "actionId": "c-WgEtWtD3sV******"
7 }
8}
附录
Action
参数名称 | 类型 | 描述 |
---|---|---|
id | String | 命令ID,仅被保存的命令拥有 |
type | String | Action类型。枚举值:COMMAND(命令),FILE_UPLOAD(上传文件) |
name | String | 命令名称,仅被保存的命令拥有 |
description | String | 动作描述 |
timeoutSecond | int | 动作的超时时间(秒) |
command | Command | 命令详情 |
fileUpload | FileUpload | 文件上传详情 |
createdTimestamp | long | 命令创建时间。unix时间戳(毫秒) |
updateTimestamp | long | 命令最后一次被修改时间。unix时间戳(毫秒) |
Command
参数名称 | 类型 | 描述 |
---|---|---|
type | String | 脚本类型。枚举值:SHELL,POWERSHELL |
scope | String | 命令可见范围。GLOBAL表示公共命令,INDIVIDUAL表示个人命令 |
content | long | 命令脚本内容 |
enableParameter | bool | 命令是否包含参数 |
parameters | List<Parameter> | 命令参数列表 |
user | String | 命令在虚机的执行用户 |
workDir | int | 命令在虚机的执行路径 |
execParams | Map<String, String> | 命令执行时的实际参数值 |
FileUpload
参数名称 | 类型 | 描述 |
---|---|---|
os | String | 操作系统。枚举值:LINUX,WINDOWS |
filename | String | 文件名称 |
filepath | String | 目标路径 |
bosBucketName | String | bos桶名称 |
bosFilePath | String | bos文件路径 |
bosEtag | String | 文件唯一标识符 |
overwrite | bool | 是否覆盖目标路径下的同名文件 |
user | String | 用户,仅Linux文件上传需要 |
group | String | 用户组,仅Linux文件上传需要 |
mode | String | 文件权限, 仅Linux文件上传需要 |
Parameter
参数名称 | 类型 | 描述 |
---|---|---|
name | String | 参数名称 |
desc | String | 参数描述 |
TargetSelector
参数名称 | 类型 | 描述 |
---|---|---|
instanceType | String | 实例类型。枚举值:BCC,BBC |
tags | List<Tag> | 实例标签列表 |
importInstances | TargetImport | 实例标签列表 |
Tag
参数名称 | 类型 | 描述 |
---|---|---|
tagKey | String | 标签Key |
tagValue | String | 标签Value |
TargetImport
参数名称 | 类型 | 描述 |
---|---|---|
keywordType | String | 实例列表导入类型。枚举值:instanceId(通过实例ID导入),internalIp表示(通过实例内网导入) |
instances | List<String> | 实例清单列表 |
Target
参数名称 | 类型 | 描述 |
---|---|---|
instanceType | String | 实例类型。枚举值:BCC,BBC |
instanceId | List<String> | 实例ID列表 |