文档目录

脚本库相关接口

创建脚本 (CreateGuestVmScript)

API请求

URLs
POST zstack/v1/scripts
Headers
Authorization: OAuth the-session-uuid
Body
{
  "params": {
    "name": "script1",
    "description": "this is a script",
    "scriptContent": "ZWNobyBoZWxsb3dvcmxkCg\u003d\u003d",
    "renderParams": "[{\"key\":\"key\",\"value\":\"value\",\"description\":\"description\"}]",
    "platform": "Linux",
    "scriptType": "Shell",
    "scriptTimeout": 30
  },
  "systemTags": [],
  "userTags": []
}
Note: 上述示例中systemTagsuserTags字段可以省略。列出是为了表示body中可以包含这两个字段。

Curl示例

curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X POST -d '{"params":{"name":"script1","description":"this is a script","scriptContent":"ZWNobyBoZWxsb3dvcmxkCg==","renderParams":"[{\"key\":\"key\",\"value\":\"value\",\"description\":\"description\"}]","platform":"Linux","scriptType":"Shell","scriptTimeout":30}}' http://localhost:8080/zstack/v1/scripts
参数列表
名字 类型 位置 描述 可选值 起始版本
name String body(包含在params结构中) 资源名称 5.2.0
description (可选) String body(包含在params结构中) 资源的详细描述 5.2.0
scriptContent String body(包含在params结构中) 脚本内容 5.2.0
renderParams (可选) String body(包含在params结构中) 参数 5.2.0
platform String body(包含在params结构中) 平台
  • Windows
  • Linux
5.2.0
scriptType String body(包含在params结构中) 脚本类型
  • Shell
  • Python
  • Perl
  • Bat
  • Powershell
5.2.0
scriptTimeout (可选) int body(包含在params结构中) 超时时间 5.2.0
resourceUuid (可选) String body(包含在params结构中) 资源UUID 5.2.0
tagUuids (可选) List body(包含在params结构中) 标签UUID列表 5.2.0
systemTags (可选) List body 系统标签 5.2.0
userTags (可选) List body 用户标签 5.2.0

API返回

返回示例
{
  "inventory": {
    "uuid": "a90f02b86916371a87bb5f2716dbfc0c",
    "name": "script1",
    "description": "this is a script",
    "scriptContent": "echo helloworld",
    "renderParams": "[{\"key\":\"key\",\"value\":\"value\",\"description\":\"description\"}]",
    "platform": "Linux",
    "scriptType": "Shell",
    "scriptTimeout": 30,
    "createDate": "Sep 23, 2024 11:53:59 AM",
    "lastOpDate": "Sep 23, 2024 11:53:59 AM"
  }
}
名字 类型 描述 起始版本
success boolean 5.2.0
error ErrorCode 错误码,若不为null,则表示操作失败, 操作成功时该字段为null。 详情参考error 5.2.0
inventory GuestVmScriptInventory 详情参考inventory 5.2.0
#error
名字 类型 描述 起始版本
code String 错误码号,错误的全局唯一标识,例如SYS.1000, HOST.1001 5.2.0
description String 错误的概要描述 5.2.0
details String 错误的详细信息 5.2.0
elaboration String 保留字段,默认为null 5.2.0
opaque LinkedHashMap 保留字段,默认为null 5.2.0
cause ErrorCode 根错误,引发当前错误的源错误,若无原错误,该字段为null 5.2.0
#inventory
名字 类型 描述 起始版本
uuid String 资源的UUID,唯一标示该资源 5.2.0
name String 资源名称 5.2.0
description String 资源的详细描述 5.2.0
scriptContent String 脚本内容 5.2.0
renderParams String 参数 5.2.0
platform String 平台 5.2.0
scriptType String 类型 5.2.0
scriptTimeout Integer 超时时间 5.2.0
createDate Timestamp 创建时间 5.2.0
lastOpDate Timestamp 最后一次修改时间 5.2.0

SDK示例

Java SDK
CreateGuestVmScriptAction action = new CreateGuestVmScriptAction();
action.name = "script1";
action.description = "this is a script";
action.scriptContent = "ZWNobyBoZWxsb3dvcmxkCg==";
action.renderParams = "[{"key":"key","value":"value","description":"description"}]";
action.platform = "Linux";
action.scriptType = "Shell";
action.scriptTimeout = 30;
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
CreateGuestVmScriptAction.Result res = action.call();
Python SDK
CreateGuestVmScriptAction action = CreateGuestVmScriptAction()
action.name = "script1"
action.description = "this is a script"
action.scriptContent = "ZWNobyBoZWxsb3dvcmxkCg=="
action.renderParams = "[{"key":"key","value":"value","description":"description"}]"
action.platform = "Linux"
action.scriptType = "Shell"
action.scriptTimeout = 30
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
CreateGuestVmScriptAction.Result res = action.call()

删除脚本 (DeleteGuestVmScript)

API请求

URLs
DELETE zstack/v1/scripts/{uuid}
Headers
Authorization: OAuth the-session-uuid

Curl示例

curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X DELETE http://localhost:8080/zstack/v1/scripts/ed1ef717239e37c29163dcad7576b8d4
参数列表
名字 类型 位置 描述 可选值 起始版本
uuid String url 资源的UUID,唯一标示该资源 5.2.0
deleteMode (可选) String body 删除模式
  • Permissive
  • Enforcing,Permissive
5.2.0
systemTags (可选) List body 系统标签 5.2.0
userTags (可选) List body 用户标签 5.2.0

API返回

该API成功时返回一个空的JSON结构{},出错时返回的JSON结构包含一个error字段,例如:
{
	"error": {
		"code": "SYS.1001",
		"description": "A message or a operation timeout",
		"details": "Create VM on KVM timeout after 300s"
	}
}

SDK示例

Java SDK
DeleteGuestVmScriptAction action = new DeleteGuestVmScriptAction();
action.uuid = "ed1ef717239e37c29163dcad7576b8d4";
action.deleteMode = "Permissive";
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
DeleteGuestVmScriptAction.Result res = action.call();
Python SDK
DeleteGuestVmScriptAction action = DeleteGuestVmScriptAction()
action.uuid = "ed1ef717239e37c29163dcad7576b8d4"
action.deleteMode = "Permissive"
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
DeleteGuestVmScriptAction.Result res = action.call()

更新脚本 (UpdateGuestVmScript)

API请求

URLs
PUT zstack/v1/scripts/{uuid}/actions
Headers
Authorization: OAuth the-session-uuid
Body
{
  "updateGuestVmScript": {
    "name": "test-script",
    "description": "desc",
    "scriptContent": "ls -lh",
    "renderParams": "",
    "platform": "Linux",
    "scriptType": "Shell",
    "scriptTimeout": 60
  },
  "systemTags": [],
  "userTags": []
}
Note: 上述示例中systemTagsuserTags字段可以省略。列出是为了表示body中可以包含这两个字段。

Curl示例

curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X PUT -d '{"updateGuestVmScript":{"name":"test-script","description":"desc","scriptContent":"ls -lh","renderParams":"","platform":"Linux","scriptType":"Shell","scriptTimeout":60}}' http://localhost:8080/zstack/v1/scripts/110b774720573fd6aec8ec70b669ffb8/actions
参数列表
名字 类型 位置 描述 可选值 起始版本
uuid String url 资源的UUID,唯一标示该资源 5.2.0
name (可选) String body(包含在updateGuestVmScript结构中) 资源名称 5.2.0
description (可选) String body(包含在updateGuestVmScript结构中) 资源的详细描述 5.2.0
scriptContent (可选) String body(包含在updateGuestVmScript结构中) 脚本内容 5.2.0
renderParams (可选) String body(包含在updateGuestVmScript结构中) 参数 5.2.0
platform (可选) String body(包含在updateGuestVmScript结构中) 平台
  • Windows
  • Linux
5.2.0
scriptType (可选) String body(包含在updateGuestVmScript结构中) 脚本类型
  • Shell
  • Python
  • Perl
  • Bat
  • Powershell
5.2.0
scriptTimeout (可选) Integer body(包含在updateGuestVmScript结构中) 超时时间 5.2.0
systemTags (可选) List body 系统标签 5.2.0
userTags (可选) List body 用户标签 5.2.0

API返回

该API成功时返回一个空的JSON结构{},出错时返回的JSON结构包含一个error字段,例如:
{
	"error": {
		"code": "SYS.1001",
		"description": "A message or a operation timeout",
		"details": "Create VM on KVM timeout after 300s"
	}
}

SDK示例

Java SDK
UpdateGuestVmScriptAction action = new UpdateGuestVmScriptAction();
action.uuid = "110b774720573fd6aec8ec70b669ffb8";
action.name = "test-script";
action.description = "desc";
action.scriptContent = "ls -lh";
action.renderParams = "";
action.platform = "Linux";
action.scriptType = "Shell";
action.scriptTimeout = 60;
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
UpdateGuestVmScriptAction.Result res = action.call();
Python SDK
UpdateGuestVmScriptAction action = UpdateGuestVmScriptAction()
action.uuid = "110b774720573fd6aec8ec70b669ffb8"
action.name = "test-script"
action.description = "desc"
action.scriptContent = "ls -lh"
action.renderParams = ""
action.platform = "Linux"
action.scriptType = "Shell"
action.scriptTimeout = 60
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
UpdateGuestVmScriptAction.Result res = action.call()

执行脚本 (ExecuteGuestVmScript)

API请求

URLs
PUT zstack/v1/scripts/{uuid}/actions
Headers
Authorization: OAuth the-session-uuid
Body
{
  "executeGuestVmScript": {
    "vmInstanceUuids": [
      "c9cc2aa748383595b144d9d12ff3abb2"
    ],
    "scriptTimeout": 60
  },
  "systemTags": [],
  "userTags": []
}
Note: 上述示例中systemTagsuserTags字段可以省略。列出是为了表示body中可以包含这两个字段。

Curl示例

curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X PUT -d '{"executeGuestVmScript":{"vmInstanceUuids":["c9cc2aa748383595b144d9d12ff3abb2"],"scriptTimeout":60}}' http://localhost:8080/zstack/v1/scripts/f9f25df251013694bbbf68bb42b1bde1/actions
参数列表
名字 类型 位置 描述 可选值 起始版本
uuid String url 资源的UUID,唯一标示该资源 5.2.0
vmInstanceUuids List body(包含在executeGuestVmScript结构中) 云主机UUID 5.2.0
scriptTimeout (可选) Integer body(包含在executeGuestVmScript结构中) 超时时间 5.2.0
logPath (可选) String body(包含在executeGuestVmScript结构中) 日志路径 5.2.0
systemTags (可选) List body 系统标签 5.2.0
userTags (可选) List body 用户标签 5.2.0

API返回

返回示例
{
  "inventory": {
    "uuid": "e5dc4c630a7138a692e45ad372c5ba17",
    "scriptUuid": "da3f23e42960342b81c8d7f4cbceeb69",
    "recordName": "202308111633_script1",
    "scriptTimeout": 30,
    "status": "Running",
    "executor": "admin",
    "executionCount": 20,
    "version": 1,
    "startTime": "Sep 23, 2024 11:50:03 AM",
    "endTime": "Sep 23, 2024 11:50:03 AM"
  }
}
名字 类型 描述 起始版本
success boolean 5.2.0
error ErrorCode 错误码,若不为null,则表示操作失败, 操作成功时该字段为null。 详情参考error 5.2.0
inventory GuestVmScriptExecutedRecordInventory 详情参考inventory 5.2.0
#error
名字 类型 描述 起始版本
code String 错误码号,错误的全局唯一标识,例如SYS.1000, HOST.1001 5.2.0
description String 错误的概要描述 5.2.0
details String 错误的详细信息 5.2.0
elaboration String 保留字段,默认为null 5.2.0
opaque LinkedHashMap 保留字段,默认为null 5.2.0
cause ErrorCode 根错误,引发当前错误的源错误,若无原错误,该字段为null 5.2.0
#inventory
名字 类型 描述 起始版本
uuid String 资源的UUID,唯一标示该资源 5.2.0
scriptUuid String 脚本UUID 5.2.0
recordName String 记录名称 5.2.0
scriptTimeout Integer 超时时间 5.2.0
status String 状态 5.2.0
executor String 执行人 5.2.0
executionCount Integer 执行次数 5.2.0
version Integer 版本 5.2.0
scriptContent String 脚本内容 5.2.0
startTime Timestamp 开始时间 5.2.0
endTime Timestamp 结束时间 5.2.0

SDK示例

Java SDK
ExecuteGuestVmScriptAction action = new ExecuteGuestVmScriptAction();
action.uuid = "f9f25df251013694bbbf68bb42b1bde1";
action.vmInstanceUuids = asList("c9cc2aa748383595b144d9d12ff3abb2");
action.scriptTimeout = 60;
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
ExecuteGuestVmScriptAction.Result res = action.call();
Python SDK
ExecuteGuestVmScriptAction action = new ExecuteGuestVmScriptAction();
action.uuid = "f9f25df251013694bbbf68bb42b1bde1";
action.vmInstanceUuids = asList("c9cc2aa748383595b144d9d12ff3abb2");
action.scriptTimeout = 60;
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
ExecuteGuestVmScriptAction.Result res = action.call();

查询脚本 (QueryGuestVmScript)

API请求

URLs
GET zstack/v1/scripts
GET zstack/v1/scripts/{uuid}
Headers
Authorization: OAuth the-session-uuid

Curl示例

curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X GET http://localhost:8080/zstack/v1/scripts?q=uuid={uuid}&q=name='script1'&q=platform='Linux'&q=scriptType='Shell'
curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X GET http://localhost:8080/zstack/v1/scripts/a37904a75a75326c87f5db7fe4bac326

可查询字段

运行zstack-cli命令行工具,输入QueryGuestVmScript并按Tab键查看所有可查询字段以及可跨表查询的资源名。

API返回

返回示例
{
  "inventories": [
    {
      "name": "script1",
      "description": "this is a script",
      "scriptContent": "ZWNobyBoZWxsb3dvcmxkCg\u003d\u003d",
      "renderParams": "[{\"key\":\"key\",\"value\":\"value\",\"description\":\"description\"}]",
      "platform": "Linux",
      "scriptType": "Shell",
      "scriptTimeout": 30
    }
  ]
}
名字 类型 描述 起始版本
success boolean 5.2.0
error ErrorCode 错误码,若不为null,则表示操作失败, 操作成功时该字段为null。 详情参考error 5.2.0
inventory SchedulerTriggerInventory 详情参考inventories 5.2.0
#error
名字 类型 描述 起始版本
code String 错误码号,错误的全局唯一标识,例如SYS.1000, HOST.1001 5.2.0
description String 错误的概要描述 5.2.0
details String 错误的详细信息 5.2.0
elaboration String 保留字段,默认为null 5.2.0
opaque LinkedHashMap 保留字段,默认为null 5.2.0
cause ErrorCode 根错误,引发当前错误的源错误,若无原错误,该字段为null 5.2.0
#inventories
名字 类型 描述 起始版本
uuid String 资源的UUID,唯一标示该资源 5.2.0
name String 资源名称 5.2.0
description String 资源的详细描述 5.2.0
scriptContent String 脚本内容 5.2.0
renderParams String 参数 5.2.0
platform String 平台 5.2.0
scriptType String 类型 5.2.0
scriptTimeout Integer 超时时间 5.2.0
createDate Timestamp 创建时间 5.2.0
lastOpDate Timestamp 最后一次修改时间 5.2.0

SDK示例

Java SDK
QueryGuestVmScriptAction action = new QueryGuestVmScriptAction();
action.conditions = asList("uuid={uuid}","name='script1'","platform='Linux'","scriptType='Shell'");
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
QueryGuestVmScriptAction.Result res = action.call();
Python SDK
QueryGuestVmScriptAction action = QueryGuestVmScriptAction()
action.conditions = ["uuid={uuid}","name='script1'","platform='Linux'","scriptType='Shell'"]
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
QueryGuestVmScriptAction.Result res = action.call()

查询脚本执行记录 (QueryGuestVmScriptExecutedRecord)

API请求

URLs
GET zstack/v1/scripts/records
GET zstack/v1/scripts/records/{uuid}
Headers
Authorization: OAuth the-session-uuid

Curl示例

curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X GET http://localhost:8080/zstack/v1/scripts/records?q=uuid={uuid}&q=scriptUuid={uuid}&q=platform='Linux'&q=scriptType='Shell'&q=executedVmNum=2&q=executedUser='admin'
curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X GET http://localhost:8080/zstack/v1/scripts/records/a26b5051d35633b89903fbef2cf01c8a

可查询字段

运行zstack-cli命令行工具,输入QueryGuestVmScriptExecutedRecord并按Tab键查看所有可查询字段以及可跨表查询的资源名。

API返回

返回示例
{
  "inventories": [
    {
      "uuid": "459a1b622be53d6ebc830c0682b8016b",
      "scriptUuid": "692066c11aa83d11a5af28ada4fb7057",
      "recordName": "202308111633_script1",
      "scriptTimeout": 30,
      "executor": "admin",
      "executionCount": 20,
      "startTime": "Sep 23, 2024 11:51:30 AM",
      "endTime": "Sep 23, 2024 11:51:30 AM"
    }
  ]
}
名字 类型 描述 起始版本
success boolean 5.2.0
error ErrorCode 错误码,若不为null,则表示操作失败, 操作成功时该字段为null。 详情参考error 5.2.0
inventories List 详情参考inventories 5.2.0
#error
名字 类型 描述 起始版本
code String 错误码号,错误的全局唯一标识,例如SYS.1000, HOST.1001 5.2.0
description String 错误的概要描述 5.2.0
details String 错误的详细信息 5.2.0
elaboration String 保留字段,默认为null 5.2.0
opaque LinkedHashMap 保留字段,默认为null 5.2.0
cause ErrorCode 根错误,引发当前错误的源错误,若无原错误,该字段为null 5.2.0
#inventories
名字 类型 描述 起始版本
uuid String 资源的UUID,唯一标示该资源 5.2.0
scriptUuid String 脚本UUID 5.2.0
recordName String 记录名称 5.2.0
scriptTimeout Integer 超时时间 5.2.0
status String 状态 5.2.0
executor String 执行人 5.2.0
executionCount Integer 执行次数 5.2.0
version Integer 版本 5.2.0
scriptContent String 脚本内容 5.2.0
startTime Timestamp 开始时间 5.2.0
endTime Timestamp 结束时间 5.2.0

SDK示例

Java SDK
QueryGuestVmScriptExecutedRecordAction action = new QueryGuestVmScriptExecutedRecordAction();
action.conditions = asList("uuid={uuid}","scriptUuid={uuid}","platform='Linux'","scriptType='Shell'","executedVmNum=2","executedUser='admin'");
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
QueryGuestVmScriptExecutedRecordAction.Result res = action.call();
Python SDK
QueryGuestVmScriptExecutedRecordAction action = QueryGuestVmScriptExecutedRecordAction()
action.conditions = ["uuid={uuid}","scriptUuid={uuid}","platform='Linux'","scriptType='Shell'","executedVmNum=2","executedUser='admin'"]
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
QueryGuestVmScriptExecutedRecordAction.Result res = action.call()

查询脚本执行记录明细 (QueryGuestVmScriptExecutedRecordDetail)

API请求

URLs
GET zstack/v1/scripts/records/details
Headers
Authorization: OAuth the-session-uuid

Curl示例

curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X GET http://localhost:8080/zstack/v1/scripts/records/details?q=recordUuid={ExecuteRecordUuid}&q=vmInstanceUuid={vmInstanceUuid}

可查询字段

运行zstack-cli命令行工具,输入QueryGuestVmScriptExecutedRecordDetail并按Tab键查看所有可查询字段以及可跨表查询的资源名。

API返回

返回示例
{
  "inventories": [
    {
      "recordUuid": "ab90a0142aef36fd97c3c759bc7e459d",
      "vmInstanceUuid": "4708f0f6f76e39e882886241761d0c27",
      "status": "Completed",
      "stdout": "helloworld",
      "errCause": "",
      "stderr": "1/0 is not a goodScript",
      "startTime": "Sep 23, 2024 11:53:03 AM",
      "endTime": "Sep 23, 2024 11:53:03 AM"
    }
  ]
}
名字 类型 描述 起始版本
success boolean 5.2.0
error ErrorCode 错误码,若不为null,则表示操作失败, 操作成功时该字段为null。 详情参考error 5.2.0
inventories List 详情参考inventories 5.2.0
#error
名字 类型 描述 起始版本
code String 错误码号,错误的全局唯一标识,例如SYS.1000, HOST.1001 5.2.0
description String 错误的概要描述 5.2.0
details String 错误的详细信息 5.2.0
elaboration String 保留字段,默认为null 5.2.0
opaque LinkedHashMap 保留字段,默认为null 5.2.0
cause ErrorCode 根错误,引发当前错误的源错误,若无原错误,该字段为null 5.2.0
#inventories
名字 类型 描述 起始版本
recordUuid String 记录UUID 5.2.0
vmInstanceUuid String 云主机UUID 5.2.0
vmName String 云主机名称 5.2.0
status String 状态 5.2.0
exitCode Integer 退出code 5.2.0
stdout String 标准输出 5.2.0
errCause String 错误原因 5.2.0
stderr String 错误输出 5.2.0
startTime Timestamp 开始时间 5.2.0
endTime Timestamp 结束时间 5.2.0

SDK示例

Java SDK
QueryGuestVmScriptExecutedRecordDetailAction action = new QueryGuestVmScriptExecutedRecordDetailAction();
action.conditions = asList("recordUuid={ExecuteRecordUuid}","vmInstanceUuid={vmInstanceUuid}");
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
QueryGuestVmScriptExecutedRecordDetailAction.Result res = action.call();
Python SDK
QueryGuestVmScriptExecutedRecordDetailAction action = QueryGuestVmScriptExecutedRecordDetailAction()
action.conditions = ["recordUuid={ExecuteRecordUuid}","vmInstanceUuid={vmInstanceUuid}"]
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
QueryGuestVmScriptExecutedRecordDetailAction.Result res = action.call()
开发手册 | 5.4.12 | ZStack Cloud · ZCF | ZStack 资源中心