Files
certd/packages/plugins/plugin-aliyun/src/plugin/deploy-to-cdn/index.ts

107 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-06-25 23:25:56 +08:00
import { AbstractTaskPlugin, IAccessService, ILogger, IsTaskPlugin, RunStrategy, TaskInput } from "@certd/pipeline";
2022-11-08 22:10:42 +08:00
import dayjs from "dayjs";
import Core from "@alicloud/pop-core";
import RPCClient from "@alicloud/pop-core";
import { AliyunAccess } from "../../access";
2022-12-29 23:52:51 +08:00
@IsTaskPlugin({
name: "DeployCertToAliyunCDN",
title: "部署证书至阿里云CDN",
desc: "依赖证书申请前置任务自动部署域名证书至阿里云CDN",
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
2022-11-08 22:10:42 +08:00
},
2022-12-29 23:52:51 +08:00
},
2022-11-08 22:10:42 +08:00
})
2023-05-24 15:41:35 +08:00
export class DeployCertToAliyunCDN extends AbstractTaskPlugin {
2022-12-29 23:52:51 +08:00
@TaskInput({
title: "CDN加速域名",
2023-06-25 23:25:56 +08:00
helper: "你在阿里云上配置的CDN加速域名比如:certd.docmirror.cn",
2022-12-29 23:52:51 +08:00
required: true,
})
domainName!: string;
@TaskInput({
title: "证书名称",
helper: "上传后将以此名称作为前缀备注",
})
certName!: string;
@TaskInput({
title: "域名证书",
helper: "请选择前置任务输出的域名证书",
component: {
name: "pi-output-selector",
},
required: true,
})
cert!: string;
@TaskInput({
title: "Access授权",
helper: "阿里云授权AccessKeyId、AccessKeySecret",
component: {
name: "pi-access-selector",
type: "aliyun",
},
required: true,
})
accessId!: string;
accessService!: IAccessService;
2023-01-11 20:39:48 +08:00
logger!: ILogger;
2023-06-25 23:25:56 +08:00
async onInstance() {
this.accessService = this.ctx.accessService;
this.logger = this.ctx.logger;
}
2022-12-29 23:52:51 +08:00
async execute(): Promise<void> {
2022-11-08 22:10:42 +08:00
console.log("开始部署证书到阿里云cdn");
2022-12-29 23:52:51 +08:00
const access = (await this.accessService.getById(this.accessId)) as AliyunAccess;
2022-11-08 22:10:42 +08:00
const client = this.getClient(access);
2022-12-29 23:52:51 +08:00
const params = await this.buildParams();
2022-11-08 22:10:42 +08:00
await this.doRequest(client, params);
console.log("部署完成");
}
getClient(access: AliyunAccess) {
return new Core({
accessKeyId: access.accessKeyId,
accessKeySecret: access.accessKeySecret,
endpoint: "https://cdn.aliyuncs.com",
apiVersion: "2018-05-10",
});
}
2022-12-29 23:52:51 +08:00
async buildParams() {
const CertName = (this.certName ?? "certd") + "-" + dayjs().format("YYYYMMDDHHmmss");
const cert: any = this.cert;
2022-11-08 22:10:42 +08:00
return {
RegionId: "cn-hangzhou",
2022-12-29 23:52:51 +08:00
DomainName: this.domainName,
2022-11-08 22:10:42 +08:00
ServerCertificateStatus: "on",
CertName: CertName,
CertType: "upload",
ServerCertificate: cert.crt,
PrivateKey: cert.key,
};
}
async doRequest(client: RPCClient, params: any) {
const requestOption = {
method: "POST",
};
const ret: any = await client.request("SetDomainServerCertificate", params, requestOption);
this.checkRet(ret);
this.logger.info("设置cdn证书成功:", ret.RequestId);
}
checkRet(ret: any) {
if (ret.code != null) {
throw new Error("执行失败:" + ret.Message);
}
}
}
2023-05-09 09:56:31 +08:00
new DeployCertToAliyunCDN();