From 9747d40734dc44922d80573d68dec7da83f19981 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Tue, 9 May 2023 10:16:49 +0800 Subject: [PATCH] refactor: register --- packages/core/pipeline/src/core/executor.ts | 2 +- packages/core/pipeline/src/plugin/api.ts | 2 +- .../pipeline/src/plugin/test/echo-plugin.ts | 4 ++ packages/core/pipeline/test/echo-plugin.ts | 2 +- .../src/dns-provider/aliyun-dns-provider.ts | 2 +- .../src/plugin/deploy-to-ack-ingress/index.ts | 2 +- .../src/plugin/deploy-to-cdn/index.ts | 2 +- .../src/plugin/upload-to-aliyun/index.ts | 2 +- .../test/huawei/access-service-test.ts | 10 +++ .../plugin-all/test/huawei/pipeline.huawei.ts | 66 +++++++++++++++++++ .../plugin-all/test/huawei/pipeline.test.ts | 19 ++++++ .../plugin-all/test/pipeline/pipeline.test.ts | 1 + .../plugin-all/test/plugin/echo-plugin.ts | 25 +++++++ .../plugin-cert/src/dns-provider/api.ts | 2 +- .../src/plugin/cert-plugin/index.ts | 4 +- .../src/plugin/host-shell-execute/index.ts | 2 +- .../src/plugin/upload-to-host/index.ts | 2 +- .../src/dns-provider/huawei-dns-provider.ts | 2 +- .../src/dns-provider/dnspod-dns-provider.ts | 2 +- .../src/plugin/deploy-to-cdn/index.ts | 2 +- .../src/plugin/deploy-to-clb/index.ts | 2 +- .../src/plugin/deploy-to-tke-ingress/index.ts | 2 +- .../src/plugin/upload-to-tencent/index.ts | 2 +- .../src/plugins/pipeline/test/echo-plugin.ts | 2 +- 24 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 packages/plugins/plugin-all/test/huawei/access-service-test.ts create mode 100644 packages/plugins/plugin-all/test/huawei/pipeline.huawei.ts create mode 100644 packages/plugins/plugin-all/test/huawei/pipeline.test.ts create mode 100644 packages/plugins/plugin-all/test/plugin/echo-plugin.ts diff --git a/packages/core/pipeline/src/core/executor.ts b/packages/core/pipeline/src/core/executor.ts index b569e3bfb..353c1d60d 100644 --- a/packages/core/pipeline/src/core/executor.ts +++ b/packages/core/pipeline/src/core/executor.ts @@ -160,7 +160,7 @@ export class Executor { }); Decorator.inject(define.autowire, instance, context); - await instance.onInit(); + await instance.onInstance(); await instance.execute(); //输出到output context diff --git a/packages/core/pipeline/src/plugin/api.ts b/packages/core/pipeline/src/plugin/api.ts index 2f8ce8fc3..b81645b43 100644 --- a/packages/core/pipeline/src/plugin/api.ts +++ b/packages/core/pipeline/src/plugin/api.ts @@ -34,7 +34,7 @@ export type PluginDefine = Registrable & { }; export interface ITaskPlugin { - onInit(): Promise; + onInstance(): Promise; execute(): Promise; } diff --git a/packages/core/pipeline/src/plugin/test/echo-plugin.ts b/packages/core/pipeline/src/plugin/test/echo-plugin.ts index bf2ae03de..924d85c30 100644 --- a/packages/core/pipeline/src/plugin/test/echo-plugin.ts +++ b/packages/core/pipeline/src/plugin/test/echo-plugin.ts @@ -20,6 +20,10 @@ export class EchoPlugin implements ITaskPlugin { // @ts-ignore logger: ILogger; + onInstance(): Promise { + throw new Error("Method not implemented."); + } + async execute(): Promise { return Promise.resolve(undefined); } diff --git a/packages/core/pipeline/test/echo-plugin.ts b/packages/core/pipeline/test/echo-plugin.ts index f40a8d534..73f17d2dc 100644 --- a/packages/core/pipeline/test/echo-plugin.ts +++ b/packages/core/pipeline/test/echo-plugin.ts @@ -23,7 +23,7 @@ export class EchoPlugin implements ITaskPlugin { certInfo!: any; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit(): Promise {} + async onInstance(): Promise {} async execute(): Promise { console.log("input :cert", this.cert); } diff --git a/packages/plugins/plugin-aliyun/src/dns-provider/aliyun-dns-provider.ts b/packages/plugins/plugin-aliyun/src/dns-provider/aliyun-dns-provider.ts index 61220aece..9400a75ec 100644 --- a/packages/plugins/plugin-aliyun/src/dns-provider/aliyun-dns-provider.ts +++ b/packages/plugins/plugin-aliyun/src/dns-provider/aliyun-dns-provider.ts @@ -16,7 +16,7 @@ export class AliyunDnsProvider implements IDnsProvider { access!: AliyunAccess; @Autowire() logger!: ILogger; - async onInit() { + async onInstance() { const access: any = this.access; this.client = new Core({ accessKeyId: access.accessKeyId, diff --git a/packages/plugins/plugin-aliyun/src/plugin/deploy-to-ack-ingress/index.ts b/packages/plugins/plugin-aliyun/src/plugin/deploy-to-ack-ingress/index.ts index f3f97284b..8fb6aa6c1 100644 --- a/packages/plugins/plugin-aliyun/src/plugin/deploy-to-ack-ingress/index.ts +++ b/packages/plugins/plugin-aliyun/src/plugin/deploy-to-ack-ingress/index.ts @@ -109,7 +109,7 @@ export class DeployCertToAliyunAckIngressPlugin implements ITaskPlugin { logger!: ILogger; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit(): Promise {} + async onInstance(): Promise {} async execute(): Promise { console.log("开始部署证书到阿里云cdn"); const { regionId, ingressClass, clusterId, isPrivateIpAddress, cert } = this; diff --git a/packages/plugins/plugin-aliyun/src/plugin/deploy-to-cdn/index.ts b/packages/plugins/plugin-aliyun/src/plugin/deploy-to-cdn/index.ts index 4bb59b053..34e0d8ce0 100644 --- a/packages/plugins/plugin-aliyun/src/plugin/deploy-to-cdn/index.ts +++ b/packages/plugins/plugin-aliyun/src/plugin/deploy-to-cdn/index.ts @@ -56,7 +56,7 @@ export class DeployCertToAliyunCDN implements ITaskPlugin { @Autowire() logger!: ILogger; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit() {} + async onInstance() {} async execute(): Promise { console.log("开始部署证书到阿里云cdn"); const access = (await this.accessService.getById(this.accessId)) as AliyunAccess; diff --git a/packages/plugins/plugin-aliyun/src/plugin/upload-to-aliyun/index.ts b/packages/plugins/plugin-aliyun/src/plugin/upload-to-aliyun/index.ts index 78fcc15f6..4e461dd41 100644 --- a/packages/plugins/plugin-aliyun/src/plugin/upload-to-aliyun/index.ts +++ b/packages/plugins/plugin-aliyun/src/plugin/upload-to-aliyun/index.ts @@ -66,7 +66,7 @@ export class UploadCertToAliyun implements ITaskPlugin { logger!: Logger; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit() {} + async onInstance() {} async execute(): Promise { console.log("开始部署证书到阿里云cdn"); diff --git a/packages/plugins/plugin-all/test/huawei/access-service-test.ts b/packages/plugins/plugin-all/test/huawei/access-service-test.ts new file mode 100644 index 000000000..9892f9667 --- /dev/null +++ b/packages/plugins/plugin-all/test/huawei/access-service-test.ts @@ -0,0 +1,10 @@ +import { IAccessService } from "@certd/pipeline"; +import { hauweiSecret } from "../user.secret"; +import { HuaweiAccess } from "@certd/plugin-huawei"; +export class AccessServiceTest implements IAccessService { + async getById(id: any): Promise { + return { + ...hauweiSecret, + } as HuaweiAccess; + } +} diff --git a/packages/plugins/plugin-all/test/huawei/pipeline.huawei.ts b/packages/plugins/plugin-all/test/huawei/pipeline.huawei.ts new file mode 100644 index 000000000..4b836463b --- /dev/null +++ b/packages/plugins/plugin-all/test/huawei/pipeline.huawei.ts @@ -0,0 +1,66 @@ +import { ConcurrencyStrategy, NextStrategy, Pipeline, RunStrategy } from "@certd/pipeline"; + +let idIndex = 0; +function generateId() { + idIndex++; + return idIndex + ""; +} +export const pipeline: Pipeline = { + version: 1, + id: generateId(), + title: "华为管道测试", + userId: 1, + triggers: [], + stages: [ + { + id: generateId(), + title: "证书申请阶段", + concurrency: ConcurrencyStrategy.Serial, + next: NextStrategy.AllSuccess, + tasks: [ + { + id: generateId(), + title: "申请证书任务", + steps: [ + { + id: generateId(), + title: "申请证书", + type: "CertApply", + input: { + domains: ["*.powerleader.chat"], + email: "xiaojunnuo@qq.com", + dnsProviderType: "huawei", + accessId: "111", + }, + }, + ], + }, + ], + }, + { + id: generateId(), + title: "证书部署阶段", + concurrency: ConcurrencyStrategy.Serial, + next: NextStrategy.AllSuccess, + tasks: [ + { + id: generateId(), + title: "测试输出参数任务", + steps: [ + { + id: generateId(), + title: "输出参数(echo插件)", + type: "EchoPlugin", + input: { + cert: "cert", + }, + strategy: { + runStrategy: RunStrategy.SkipWhenSucceed, + }, + }, + ], + }, + ], + }, + ], +}; diff --git a/packages/plugins/plugin-all/test/huawei/pipeline.test.ts b/packages/plugins/plugin-all/test/huawei/pipeline.test.ts new file mode 100644 index 000000000..8726f9f02 --- /dev/null +++ b/packages/plugins/plugin-all/test/huawei/pipeline.test.ts @@ -0,0 +1,19 @@ +//import { expect } from "chai"; +import "mocha"; +import { Executor, RunHistory, FileStorage } from "@certd/pipeline"; +import { pipeline } from "./pipeline.huawei"; +import { AccessServiceTest } from "./access-service-test"; +import "../../src"; +import "../plugin/echo-plugin"; +describe("pipeline-hauwei-test", function () { + it("#pipeline", async function () { + this.timeout(120000); + function onChanged(history: RunHistory) { + console.log("changed:"); + } + + const executor = new Executor({ userId: "test", pipeline, onChanged, accessService: new AccessServiceTest(), storage: new FileStorage() }); + await executor.run(1, "user"); + // expect(define.name).eq("EchoPlugin"); + }); +}); diff --git a/packages/plugins/plugin-all/test/pipeline/pipeline.test.ts b/packages/plugins/plugin-all/test/pipeline/pipeline.test.ts index 87e69267f..53d561957 100644 --- a/packages/plugins/plugin-all/test/pipeline/pipeline.test.ts +++ b/packages/plugins/plugin-all/test/pipeline/pipeline.test.ts @@ -4,6 +4,7 @@ import { Executor, RunHistory, FileStorage } from "@certd/pipeline"; import { pipeline } from "./pipeline.define"; import { AccessServiceTest } from "./access-service-test"; import "../../src"; +import "../plugin/echo-plugin"; describe("pipeline", function () { it("#pipeline", async function () { this.timeout(120000); diff --git a/packages/plugins/plugin-all/test/plugin/echo-plugin.ts b/packages/plugins/plugin-all/test/plugin/echo-plugin.ts new file mode 100644 index 000000000..288cbb844 --- /dev/null +++ b/packages/plugins/plugin-all/test/plugin/echo-plugin.ts @@ -0,0 +1,25 @@ +import { Autowire, IsTaskPlugin, TaskInput, ITaskPlugin } from "@certd/pipeline"; + +@IsTaskPlugin({ + name: "EchoPlugin", + title: "测试插件", + desc: "test", +}) +export class EchoPlugin implements ITaskPlugin { + @TaskInput({ + title: "测试属性", + component: { + name: "text", + }, + }) + test?: string; + + async execute(): Promise { + console.log("output", this.test); + } + + onInstance(): Promise { + return Promise.resolve(undefined); + } +} +new EchoPlugin(); diff --git a/packages/plugins/plugin-cert/src/dns-provider/api.ts b/packages/plugins/plugin-cert/src/dns-provider/api.ts index 3693f75f6..7f5f3902b 100644 --- a/packages/plugins/plugin-cert/src/dns-provider/api.ts +++ b/packages/plugins/plugin-cert/src/dns-provider/api.ts @@ -17,7 +17,7 @@ export type RemoveRecordOptions = CreateRecordOptions & { }; export interface IDnsProvider { - onInit(): Promise; + onInstance(): Promise; createRecord(options: CreateRecordOptions): Promise; removeRecord(options: RemoveRecordOptions): Promise; } diff --git a/packages/plugins/plugin-cert/src/plugin/cert-plugin/index.ts b/packages/plugins/plugin-cert/src/plugin/cert-plugin/index.ts index 2d144d163..8394905f2 100644 --- a/packages/plugins/plugin-cert/src/plugin/cert-plugin/index.ts +++ b/packages/plugins/plugin-cert/src/plugin/cert-plugin/index.ts @@ -156,7 +156,7 @@ export class CertApplyPlugin implements ITaskPlugin { }) cert?: CertInfo; - async onInit() { + async onInstance() { this.acme = new AcmeService({ userContext: this.userContext, logger: this.logger }); } @@ -228,7 +228,7 @@ export class CertApplyPlugin implements ITaskPlugin { const dnsProvider: IDnsProvider = new DnsProviderClass(); const context = { access, logger: this.logger, http: this.http }; Decorator.inject(dnsProviderDefine.autowire, dnsProvider, context); - await dnsProvider.onInit(); + await dnsProvider.onInstance(); const cert = await this.acme.order({ email, diff --git a/packages/plugins/plugin-host/src/plugin/host-shell-execute/index.ts b/packages/plugins/plugin-host/src/plugin/host-shell-execute/index.ts index 9423fd63d..5794e51d8 100644 --- a/packages/plugins/plugin-host/src/plugin/host-shell-execute/index.ts +++ b/packages/plugins/plugin-host/src/plugin/host-shell-execute/index.ts @@ -48,7 +48,7 @@ export class HostShellExecutePlugin implements ITaskPlugin { logger!: ILogger; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit() {} + async onInstance() {} async execute(): Promise { const { script, accessId } = this; const connectConf = this.accessService.getById(accessId); diff --git a/packages/plugins/plugin-host/src/plugin/upload-to-host/index.ts b/packages/plugins/plugin-host/src/plugin/upload-to-host/index.ts index 2191dec2f..32d9309d9 100644 --- a/packages/plugins/plugin-host/src/plugin/upload-to-host/index.ts +++ b/packages/plugins/plugin-host/src/plugin/upload-to-host/index.ts @@ -64,7 +64,7 @@ export class UploadCertToHostPlugin implements ITaskPlugin { }) hostKeyPath!: string; - async onInit() {} + async onInstance() {} async execute(): Promise { const { crtPath, keyPath, cert, accessId, sudo } = this; const connectConf = this.accessService.getById(accessId); diff --git a/packages/plugins/plugin-huawei/src/dns-provider/huawei-dns-provider.ts b/packages/plugins/plugin-huawei/src/dns-provider/huawei-dns-provider.ts index 2158b4a65..35492bd6a 100644 --- a/packages/plugins/plugin-huawei/src/dns-provider/huawei-dns-provider.ts +++ b/packages/plugins/plugin-huawei/src/dns-provider/huawei-dns-provider.ts @@ -17,7 +17,7 @@ export class HuaweiDnsProvider implements IDnsProvider { @Autowire() logger!: ILogger; endpoint = "https://domains-external.myhuaweicloud.com"; - async onInit() { + async onInstance() { const access: any = this.access; this.client = new HuaweiYunClient(access); } diff --git a/packages/plugins/plugin-tencent/src/dns-provider/dnspod-dns-provider.ts b/packages/plugins/plugin-tencent/src/dns-provider/dnspod-dns-provider.ts index 9729af529..2d2e25cd0 100644 --- a/packages/plugins/plugin-tencent/src/dns-provider/dnspod-dns-provider.ts +++ b/packages/plugins/plugin-tencent/src/dns-provider/dnspod-dns-provider.ts @@ -20,7 +20,7 @@ export class DnspodDnsProvider implements IDnsProvider { loginToken: any; - async onInit() { + async onInstance() { const access: DnspodAccess = this.access as DnspodAccess; this.loginToken = access.id + "," + access.token; } diff --git a/packages/plugins/plugin-tencent/src/plugin/deploy-to-cdn/index.ts b/packages/plugins/plugin-tencent/src/plugin/deploy-to-cdn/index.ts index 8fecb5a60..fd3eab750 100644 --- a/packages/plugins/plugin-tencent/src/plugin/deploy-to-cdn/index.ts +++ b/packages/plugins/plugin-tencent/src/plugin/deploy-to-cdn/index.ts @@ -54,7 +54,7 @@ export class DeployToCdnPlugin implements ITaskPlugin { logger!: ILogger; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit() {} + async onInstance() {} async execute(): Promise { const accessProvider: TencentAccess = (await this.accessService.getById(this.accessId)) as TencentAccess; diff --git a/packages/plugins/plugin-tencent/src/plugin/deploy-to-clb/index.ts b/packages/plugins/plugin-tencent/src/plugin/deploy-to-clb/index.ts index a8cf7609d..80fd802db 100644 --- a/packages/plugins/plugin-tencent/src/plugin/deploy-to-clb/index.ts +++ b/packages/plugins/plugin-tencent/src/plugin/deploy-to-clb/index.ts @@ -78,7 +78,7 @@ export class DeployToClbPlugin implements ITaskPlugin { logger!: ILogger; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit() {} + async onInstance() {} async execute(): Promise { const accessProvider = (await this.accessService.getById(this.accessId)) as TencentAccess; const client = this.getClient(accessProvider, this.region); diff --git a/packages/plugins/plugin-tencent/src/plugin/deploy-to-tke-ingress/index.ts b/packages/plugins/plugin-tencent/src/plugin/deploy-to-tke-ingress/index.ts index 1a01cb8ac..8046758ef 100644 --- a/packages/plugins/plugin-tencent/src/plugin/deploy-to-tke-ingress/index.ts +++ b/packages/plugins/plugin-tencent/src/plugin/deploy-to-tke-ingress/index.ts @@ -87,7 +87,7 @@ export class DeployCertToTencentTKEIngressPlugin implements ITaskPlugin { accessService!: IAccessService; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit() {} + async onInstance() {} async execute(): Promise { const accessProvider = this.accessService.getById(this.accessId); diff --git a/packages/plugins/plugin-tencent/src/plugin/upload-to-tencent/index.ts b/packages/plugins/plugin-tencent/src/plugin/upload-to-tencent/index.ts index 67f2f77f1..c4e4515b8 100644 --- a/packages/plugins/plugin-tencent/src/plugin/upload-to-tencent/index.ts +++ b/packages/plugins/plugin-tencent/src/plugin/upload-to-tencent/index.ts @@ -49,7 +49,7 @@ export class UploadToTencentPlugin implements ITaskPlugin { logger!: ILogger; // eslint-disable-next-line @typescript-eslint/no-empty-function - async onInit() {} + async onInstance() {} async execute(): Promise { const { accessId, name, cert } = this; diff --git a/packages/ui/certd-server/src/plugins/pipeline/test/echo-plugin.ts b/packages/ui/certd-server/src/plugins/pipeline/test/echo-plugin.ts index 7adee962d..83c4498cc 100644 --- a/packages/ui/certd-server/src/plugins/pipeline/test/echo-plugin.ts +++ b/packages/ui/certd-server/src/plugins/pipeline/test/echo-plugin.ts @@ -19,7 +19,7 @@ export class EchoPlugin implements ITaskPlugin { // @ts-ignore logger: ILogger; - async onInit(){} + async onInstance(){} async execute(): Promise { return Promise.resolve(undefined);