云托管
init
1. 接口描述
接口功能:初始化云托管代码项目
接口声明:init(params: { serverName: string; template?: string; targetPath?: string }): Promise<{ projectDir: string }>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| serverName | 是 | String | 服务名称,将作为项目目录名 |
| template | 否 | String | 模板标识符,默认为'helloworld' |
| targetPath | 否 | String | 目标路径,默认为当前目录 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| projectDir | String | 初始化后的项目目录 |
4. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});
async function test() {
const { projectDir } = await manager.cloudrun.init({
serverName: "my-server",
template: "helloworld",
targetPath: "./projects",
});
console.log("项目已初始化到:", projectDir);
}
test();
download
1. 接口描述
接口功能:下载云托管服务代码到本地目录
接口声明:download(params: { serverName: string; targetPath: string }): Promise<void>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| serverName | 是 | String | 要下载的服务名称 |
| targetPath | 是 | String | 下载的目标路径(绝对或相对路径) |
3. 返回结果
无直接返回数据,成功时 Promise 解析,失败时拒绝。
4. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});
async function test() {
await manager.cloudrun.download({
serverName: "my-server",
targetPath: "./downloads",
});
console.log("代码下载完成");
}
test();
list
1. 接口描述
接口功能:获取云托管服务列表
接口声明:list(params?: { pageSize?: number; pageNum?: number; serverName?: string; serverType?: CloudrunServerType }): Promise<ICloudrunListResponse>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| pageSize | 否 | Number | 每页数量,默认 10 |
| pageNum | 否 | Number | 页码,默认 1 |
| serverName | 否 | String | 服务名称筛选 |
| serverType | 否 | CloudrunServerType | 服务类型筛选(function/container) |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| ServerList | ICloudrunServerBaseInfo[] | 服务列表数组 |
| ServerList[].ServerName | String | 服务名称 |
| ServerList[].DefaultDomainName | String | 默认服务域名 |
| ServerList[].CustomDomainName | String | 自定义域名 |
| ServerList[].Status | String | 服务状态(running/deploying/deploy_failed) |
| ServerList[].UpdateTime | String | 更新时间 |
| ServerList[].AccessTypes | String[] | 公网访问类型数组 |
| ServerList[].CustomDomainNames | String[] | 自定义域名数组 |
| ServerList[].ServerType | String | 服务类型(function/container) |
| ServerList[].TrafficType | String | 流量类型 |
| Total | Number | 服务总数 |
| RequestId | String | 请求 ID |
4. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});
async function test() {
const { ServerList, Total } = await manager.cloudrun.list({
pageSize: 20,
pageNum: 1,
});
console.log(`共 ${Total} 个服务:`, ServerList);
}
test();
detail
1. 接口描述
接口功能:查询云托管服务详情
接口声明:detail(params: { serverName: string }): Promise<ICloudrunDetailResponse>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| serverName | 是 | String | 要查询的服务名称 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| BaseInfo | ICloudrunServerBaseInfo | 服务基本信息 |
| BaseInfo.ServerName | String | 服务名称 |
| BaseInfo.DefaultDomainName | String | 默认服务域名 |
| BaseInfo.CustomDomainName | String | 自定义域名 |
| BaseInfo.Status | String | 服务状态 |
| BaseInfo.UpdateTime | String | 更新时间 |
| BaseInfo.AccessTypes | String[] | 公网访问类型 |
| BaseInfo.CustomDomainNames | String[] | 自定义域名数组 |
| BaseInfo.ServerType | String | 服务类型 |
| BaseInfo.TrafficType | String | 流量类型 |
| ServerConfig | ICloudrunServerBaseConfig | 服务配置信息 |
| ServerConfig.EnvId | String | 环境 ID |
| ServerConfig.ServerName | String | 服务名称 |
| ServerConfig.OpenAccessTypes | String[] | 公网访问类型 |
| ServerConfig.Cpu | Number | CPU 规格 |
| ServerConfig.Mem | Number | 内存规格 |
| ServerConfig.MinNum | Number | 最小副本数 |
| ServerConfig.MaxNum | Number | 最大副本数 |
| ServerConfig.PolicyDetails | ICloudrunHpaPolicy[] | 扩缩容配置 |
| ServerConfig.PolicyDetails[].PolicyType | String | 扩缩容类型,可选值: - "cpu": 基于 CPU 利用率的扩缩容- "mem": 基于内存利用率的扩缩容;- "cpu/mem": 基于 CPU 和内存利用率的扩缩容 |
| ServerConfig.PolicyDetails[].PolicyThreshold | Number | 扩缩容阈值(百分比),取值范围 0-100,如 60 表示当资源利用率达到 60%时触发扩缩容 |
| ServerConfig.CustomLogs | String | 日志采集路径 |
| ServerConfig.EnvParams | String | 环境变量 |
| ServerConfig.InitialDelaySeconds | Number | 延迟检测时间 |
| ServerConfig.CreateTime | String | 创建时间 |
| ServerConfig.Port | Number | 服务端口 |
| ServerConfig.HasDockerfile | Boolean | 是否有 Dockerfile |
| ServerConfig.Dockerfile | String | Dockerfile 文件名 |
| ServerConfig.BuildDir | String | 构建目录 |
| ServerConfig.LogType | String | 日志类型 |
| ServerConfig.LogSetId | String | CLS 日志集 ID |
| ServerConfig.LogTopicId | String | CLS 主题 ID |
| ServerConfig.LogParseType | String | 日志解析类型 |
| ServerConfig.Tag | String | 服务标签 |
| ServerConfig.InternalAccess | String | 内网访问开关 |
| ServerConfig.InternalDomain | String | 内网域名 |
| ServerConfig.OperationMode | String | 运行模式 |
| ServerConfig.TimerScale | ICloudrunTimerScale[] | 定时扩缩容配置 |
| ServerConfig.TimerScale[].CycleType | String | 循环类型,可选值: - "none": 无循环- "daily": 每日循环- "weekly": 每周循环- "monthly": 每月循环 |
| ServerConfig.TimerScale[].StartDate | String | 循环起始日期(格式: YYYY-MM-DD) |
| ServerConfig.TimerScale[].EndDate | String | 循环结束日期(格式: YYYY-MM-DD) |
| ServerConfig.TimerScale[].StartTime | String | 起始时间(格式: HH:mm:ss) |
| ServerConfig.TimerScale[].EndTime | String | 结束时间(格式: HH:mm:ss) |
| ServerConfig.TimerScale[].ReplicaNum | Number | 副本个数(最小值: 0) |
| ServerConfig.EntryPoint | String[] | Dockerfile EntryPoint 参数 |
| ServerConfig.Cmd | String[] | Dockerfile Cmd 参数 |
| OnlineVersionInfos | ICloudrunOnlineVersionInfo[] | 在线版本信息 |
| OnlineVersionInfos[].VersionName | String | 版本名称 |
| OnlineVersionInfos[].ImageUrl | String | 镜像 URL |
| OnlineVersionInfos[].FlowRatio | String | 流量比例 |
| RequestId | String | 请求 ID |
4. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});
async function test() {
const detail = await manager.cloudrun.detail({
serverName: "my-server",
});
console.log("服务详情:", detail);
}
test();
delete
1. 接口描述
接口功能:删除指定的云托管服务
接口声明:delete(params: { serverName: string }): Promise<IResponseInfo>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| serverName | 是 | String | 要删除的服务名称 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| RequestId | String | 请求 ID |
4. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});
async function test() {
await manager.cloudrun.delete({
serverName: "my-server",
});
console.log("服务删除成功");
}
test();
deploy
1. 接口描述
接口功能:本地代码部署云托管服务
接口声明:deploy(params: { serverName: string; targetPath: string; deployInfo: { ReleaseType: ReleaseTypeEnum }; imageUrl?: string; serverConfig?: Partial<ICloudrunServerBaseConfig> }): Promise<IResponseInfo>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| serverName | 是 | String | 要部署的服务名称 |
| targetPath | 是 | String | 本地代码路径 |
| deployInfo | 是 | Object | 部署信息,包含:ReleaseType: ReleaseTypeEnum - 发布类型,可选值:"GRAY"(灰度发布)、"FULL"(全量发布) |
| imageUrl | 否 | String | 镜像地址,传入时走镜像部署模式,无需上传代码包 |
| serverConfig | 否 | Partial<ICloudrunServerBaseConfig> | 服务配置项,包含以下可选字段: |
- OpenAccessTypes: string[] - 公网访问类型数组,可选值:"OA" - 办公网访问"PUBLIC" - 公网访问"MINIAPP" - 小程序访问"VPC" - VPC 访问 | |||
| - Cpu: number - CPU 规格 | |||
| - Mem: number - 内存规格 | |||
| - MinNum: number - 最小实例数 | |||
| - MaxNum: number - 最大实例数 | |||
- PolicyDetails: ICloudrunHpaPolicy[] - 扩缩容配置数组,每个元素包含:PolicyType: string - 扩缩容类型,可选值:"cpu"、"mem"、"cpu/mem"PolicyThreshold: number - 扩缩容阈值(百分比),如 60 表示 60% | |||
| - CustomLogs: string - 自定义日志配置 | |||
| - EnvParams: string - 环境变量 JSON 字符串 | |||
| - Port: number - 服务端口(函数型服务固定为 3000) | |||
| - Dockerfile: string - Dockerfile 文件名 | |||
| - BuildDir: string - 构建目录 | |||
| - InternalAccess: string - 内网访问开关 | |||
| - InternalDomain: string - 内网域名 | |||
| - EntryPoint: string[] - Dockerfile EntryPoint 参数 | |||
| - Cmd: string[] - Dockerfile Cmd 参数 | |||
| - InstallDependency: boolean - 是否在线安装依赖,true 则本地不打包 node_modules | |||
| - OperationMode: string - 运行模式 | |||
- SessionAffinity: string - 会话亲和性,可选值:"open"、"close" | |||
- LogType: string - 日志类型,可选值:"none"、"default"、"custom" | |||
| - LogSetId: string - CLS 日志集 ID | |||
| - LogTopicId: string - CLS 主题 ID | |||
- LogParseType: string - 日志解析类型,可选值:"json"、"line" | |||
| - Tag: string - 服务标签 | |||
| - TimerScale: ICloudrunTimerScale[] - 定时扩缩容配置 | |||
- VpcConf: IVpcConf - VPC 网络配置,包含:VpcId: string - VPC IDVpcCIDR: string - VPC 网段SubnetId: string - 子网 IDSubnetCIDR: string - 子网网段 | |||
| - VolumesConf: IVolumeConf[] - 存储挂载配置(COS/CFS) | |||
- PublicNetConf: IPublicNetConf - 公网访问配置,包含:PublicNetStatus: string - 公网状态,可选值:"ENABLE"、"DISABLE" |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| RequestId | String | 请求 ID |
4. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});
async function test() {
await manager.cloudrun.deploy({
serverName: "my-server",
targetPath: "./my-project",
deployInfo: {
ReleaseType: "FULL",
},
serverConfig: {
Cpu: 0.5,
Mem: 1,
MinNum: 1,
MaxNum: 5,
VpcConf: {
VpcId: "vpc-xxxxxxxx",
VpcCIDR: "172.16.0.0/16",
SubnetId: "subnet-xxxxxxxx",
},
},
});
console.log("服务部署成功");
}
test();
镜像部署示例:
async function test() {
await manager.cloudrun.deploy({
serverName: "my-server",
targetPath: "./",
deployInfo: {
ReleaseType: "FULL",
},
imageUrl: "ccr.ccs.tencentyun.com/my-repo/my-image:latest",
});
console.log("服务部署成功");
}
test();
getTemplates
1. 接口描述
接口功能:获取云托管服务模板列表
接口声明:getTemplates(): Promise<ITemplate[]>
2. 输入参数
无
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| ITemplate[] | Array | 模板数组 |
| [].identifier | String | 模板唯一标识符 |
| [].title | String | 模板标题 |
| [].description | String | 模板描述 |
| [].runtimeVersion | String | 运行时版本 |
| [].language | String | 编程语言 |
| [].zipFileStore | String | 模板压缩包存储地址 |
4. 示例代码
import CloudBase from "@cloudbase/manager-node";
const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});
async function test() {
const templates = await manager.cloudrun.getTemplates();
console.log("可用模板:", templates);
templates.forEach((template) => {
console.log(`模板ID: ${template.identifier}`);
console.log(`标题: ${template.title}`);
console.log(`描述: ${template.description}`);
console.log(`运行时版本: ${template.runtimeVersion}`);
console.log(`语言: ${template.language}`);
console.log(`下载 地址: ${template.zipFileStore}`);
});
}
test();
describeServerManageTask
1. 接口描述
接口功能:查询云托管服务的管理任务。未指定任务 ID 时,查询该服务当前最新的任务。
接口声明:describeServerManageTask(params: { serverName: string; taskId?: number; operatorRemark?: string }): Promise<{ IsExist?: boolean; Task?: IServerManageTaskInfo; RequestId?: string }>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| serverName | 是 | String | 服务名称 |
| taskId | 否 | Number | 任务 ID,默认 0,表示查询服务当前最新的任务 |
| operatorRemark | 否 | String | 操作标识 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| IsExist | Boolean | 是否存在匹配的管理任务 |
| Task | IServerManageTaskInfo | 管理任务信息,不存在时可能为空 |
| Task.Id | Number | 任务 ID |
| Task.EnvId | String | 环境 ID |
| Task.ServerName | String | 服务名称 |
| Task.ChangeType | String | 变更类型 |
| Task.ReleaseType | String | 发布类型 |
| Task.DeployType | String | 部署类型 |
| Task.PreVersionName | String | 上一个版本名称 |
| Task.VersionName | String | 当前任务关联的版本名称 |
| Task.Status | String | 任务状态 |
| Task.Steps | ITaskStepInfo[] | 任务步骤信息,包含名称、状态、开始和结束时间、耗时及失败原因 |
| Task.FailReason | String | 任务失败原因 |
| Task.OperatorRemark | String | 操作标识 |
| RequestId | String | 请求 ID |
4. 示例代码
const result = await manager.cloudrun.describeServerManageTask({
serverName: "my-server",
});
console.log("是否存在任务:", result.IsExist);
console.log("任务信息:", result.Task);
describeVersionDetail
1. 接口描述
接口功能:查询云托管服务指定版本的详细配置和运行状态。
接口声明:describeVersionDetail(params: { ServerName: string; VersionName: string; Channel?: string }): Promise<IDescribeVersionDetailResponse>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| ServerName | 是 | String | 服务名称 |
| VersionName | 是 | String | 版本名称 |
| Channel | 否 | String | 渠道标识 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| Name | String | 服务名称 |
| Port | Number | 服务端口 |
| Cpu | Number | CPU 规格 |
| Mem | Number | 内存规格 |
| MinNum | Number | 最小实例数 |
| MaxNum | Number | 最大实例数 |
| PolicyDetails | ICloudrunHpaPolicy[] | 扩缩容策略 |
| Dockerfile | String | Dockerfile 文件名 |
| BuildDir | String | 构建目录 |
| EnvParams | String | 环境变量 |
| Status | String | 版本状态 |
| CreatedTime | String | 创建时间 |
| UpdatedTime | String | 更新时间 |
| LogPath | String | 日志采集路径 |
| EntryPoint | String | null | 容器入口命令 |
| Cmd | String | null | 容器启动命令 |
| VpcConf | IVpcConf | null | VPC 网络配置 |
| VolumesConf | IVolumeConf[] | null | 存储挂载配置 |
| BuildPacks | IBuildPacksInfo | null | 构建包信息,包含基础镜像、启动命令、语言、上传文件名和语言版本 |
| RequestId | String | 请求 ID |
4. 示例代码
const version = await manager.cloudrun.describeVersionDetail({
ServerName: "my-server",
VersionName: "my-server-0001",
});
console.log("版本状态:", version.Status);
console.log("CPU/内存:", version.Cpu, version.Mem);
submitServerRollback
1. 接口描述
接口功能:提交云托管服务版本回滚任务,将当前版本回滚至指定历史版本。
接口声明:submitServerRollback(params: { ServerName: string; CurrentVersionName: string; RollbackVersionName: string; OperatorRemark?: string }): Promise<ISubmitServerRollbackResponse>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| ServerName | 是 | String | 服务名称 |
| CurrentVersionName | 是 | String | 当前版本名称 |
| RollbackVersionName | 是 | String | 目标回滚版本名称 |
| OperatorRemark | 否 | String | 操作标识 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| TaskId | Number | 回滚任务 ID,可通过 describeServerManageTask 查询任务状态 |
| RequestId | String | 请求 ID |
4. 示例代码
const { TaskId } = await manager.cloudrun.submitServerRollback({
ServerName: "my-server",
CurrentVersionName: "my-server-0002",
RollbackVersionName: "my-server-0001",
OperatorRemark: "rollback-release",
});
console.log("已提交回滚任务:", TaskId);
deleteCloudRunVersions
1. 接口描述
接口功能:批量删除指定的云托管版本。
接口声明:deleteCloudRunVersions(params: IDeleteCloudRunVersionsParams): Promise<IDeleteCloudRunVersionsResponse>
该操作不可恢复。版本删除限制由服务端校验;请检查 FailVersions,确认每个版本的删除结果。
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| IsDeleteServer | 是 | Boolean | 是否删除服务;仅删除最后一个版本时 生效 |
| IsDeleteImage | 是 | Boolean | 是否删除镜像;仅删除服务时生效 |
| SimpleVersions | 是 | ISimpleVersion[] | 待删除版本列表,不能为空 |
| SimpleVersions[].EnvId | 否 | String | 环境 ID;不传时使用当前环境 |
| SimpleVersions[].ServerName | 是 | String | 服务名称 |
| SimpleVersions[].VersionName | 是 | String | 版本名称 |
| OperatorRemark | 否 | String | 操作标识 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| Result | String | 整体删除结果:succ、partial 或 fail |
| SuccessVersions | ISuccessDeleteVersions[] | 删除成功的版本列表 |
| SuccessVersions[].Version | ISimpleVersion | 已删除的版本信息 |
| SuccessVersions[].RequestId | String | 删除该版本的请求 ID |
| SuccessVersions[].Result | String | 删除结果 |
| FailVersions | IFailDeleteVersions[] | 删除失败的版本列表 |
| FailVersions[].Version | ISimpleVersion | 删除失败的版本信息 |
| FailVersions[].ErrorCode | Number | 错误码 |
| FailVersions[].ErrorMsg | String | 错误信息 |
| FailVersions[].RequestId | String | 删除该版本的请求 ID |
| RequestId | String | 请求 ID |
4. 示例代码
const result = await manager.cloudrun.deleteCloudRunVersions({
IsDeleteServer: false,
IsDeleteImage: false,
SimpleVersions: [
{
ServerName: "my-server",
VersionName: "my-server-0001",
},
],
OperatorRemark: "remove-unused-version",
});
console.log("删除结果:", result.Result);
console.log("成功版本:", result.SuccessVersions);
console.log("失败版本:", result.FailVersions);
getDeployRecords
1. 接口描述
接口功能:获取云托管服务的部署记录,结果按部署时间倒序排列。
接口声明:getDeployRecords(params: { serverName: string }): Promise<IDescribeCloudRunDeployRecordResponse>
2. 输入参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| serverName | 是 | String | 服务名称 |
3. 返回结果
| 字段 | 类型 | 说明 |
|---|---|---|
| DeployRecords | ICloudRunDeployRecordInfo[] | 部署记录列表 |
| DeployRecords[].DeployId | String | 部署 ID |
| DeployRecords[].DeployTime | String | 部署时间 |
| DeployRecords[].Status | String | 部署状态 |
| DeployRecords[].RunId | String | 运行版本 ID,可用于查询运行日志 |
| DeployRecords[].BuildId | Number | 构建 ID,可用于查询构建日志 |
| DeployRecords[].FlowRatio | Number | 流量比例,取值范围为 0-100 |
| DeployRecords[].ImageUrl | String | 镜像地址 |
| DeployRecords[].ScaleStatus | String | 扩缩容状态 |
| DeployRecords[].HasTraffic | Boolean | 是否已分配流量 |
| DeployRecords[].TrafficType | String | 流量类型 |
| DeployRecords[].IsReleasing | Boolean | 是否正在发布 |
| RequestId | String | 请求 ID |