From 635b042690637bff85e97e07c7aac4b87a8a124b Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Mon, 6 Jan 2025 23:47:08 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E8=85=BE=E8=AE=AF?= =?UTF-8?q?=E4=BA=91CLB=E6=8F=92=E4=BB=B6=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=9D=9Esni=E6=83=85=E5=86=B5=EF=BC=8Csni=E6=83=85=E5=86=B5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A1=AB=E5=86=99=E5=A4=9A=E4=B8=AA=E5=9F=9F?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/deploy-to-clb/index.ts | 87 +++++++++++-------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-clb/index.ts b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-clb/index.ts index 62eba96d0..4177c968c 100644 --- a/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-clb/index.ts +++ b/packages/ui/certd-server/src/plugins/plugin-tencent/plugin/deploy-to-clb/index.ts @@ -6,7 +6,7 @@ import { TencentAccess } from '@certd/plugin-lib'; title: '腾讯云-部署到CLB', icon: 'svg:icon-tencentcloud', group: pluginGroups.tencent.key, - desc: '暂时只支持单向认证证书,暂时只支持通用负载均衡,必须开启sni', + desc: '暂时只支持单向认证证书,暂时只支持通用负载均衡', default: { strategy: { runStrategy: RunStrategy.SkipWhenSucceed, @@ -59,16 +59,22 @@ export class DeployCertToTencentCLB extends AbstractTaskPlugin { @TaskInput({ title: '监听器ID', - helper: '如果没有配置,则根据域名或负载均衡id匹配监听器', + required: true, }) listenerId!: string; @TaskInput({ title: '域名', - required: true, - helper: '要更新的支持https的负载均衡的域名', + required: false, + component: { + name: 'a-select', + vModel: 'value', + open: false, + mode: 'tags', + }, + helper: '如果开启了sni,则此项必须填写,未开启,则不要填写', }) - domain!: string; + domain!: string | string[]; @TaskInput({ title: '域名证书', @@ -122,33 +128,48 @@ export class DeployCertToTencentCLB extends AbstractTaskPlugin { async execute(): Promise { const client = this.client; - const lastCertId = await this.getCertIdFromProps(client); - if (!this.domain) { + + if (!this.domain || this.domain.length === 0) { await this.updateListener(client); } else { - await this.updateByDomainAttr(client); + const domains = Array.isArray(this.domain) ? this.domain : [this.domain]; + for (const domain of domains) { + this.logger.info(`开始更新域名证书:${domain},请确保已经开启了sni`); + const lastCertId = await this.getCertIdFromProps(client, domain); + + await this.updateByDomainAttr(client, domain); + + const checkDeployed = async (wait = 5) => { + await this.ctx.utils.sleep(wait * 1000); + this.logger.info(`等待${wait}秒`); + const newCertId = await this.getCertIdFromProps(client, domain); + this.logger.info(`oldCertId:${lastCertId} , newCertId:${newCertId}`); + if ((lastCertId && newCertId === lastCertId) || (!lastCertId && !newCertId)) { + return false; + } + this.logger.info('腾讯云证书ID:', newCertId); + return true; + }; + let count = 0; + while (true) { + count++; + const res = await checkDeployed(5); + if (res) { + break; + } + if (count > 6) { + throw new Error('部署可能失败,请确认是否已开启sni'); + } + } + } } - try { - await this.ctx.utils.sleep(2000); - let newCertId = await this.getCertIdFromProps(client); - if ((lastCertId && newCertId === lastCertId) || (!lastCertId && !newCertId)) { - await this.ctx.utils.sleep(2000); - newCertId = await this.getCertIdFromProps(client); - } - if (newCertId === lastCertId) { - return; - } - this.logger.info('腾讯云证书ID:', newCertId); - } catch (e) { - this.logger.warn('查询腾讯云证书失败', e); - } return; } - async getCertIdFromProps(client: any) { - const listenerRet = await this.getListenerList(client, this.loadBalancerId, [this.listenerId]); - return this.getCertIdFromListener(listenerRet[0], this.domain); + async getCertIdFromProps(client: any, domain: string) { + const listenerRet = await this.getListenerList(client, this.loadBalancerId, this.listenerId ? [this.listenerId] : null); + return this.getCertIdFromListener(listenerRet[0], domain); } getCertIdFromListener(listener: any, domain: string) { @@ -178,21 +199,13 @@ export class DeployCertToTencentCLB extends AbstractTaskPlugin { return ret; } - async updateByDomainAttr(client: any) { + async updateByDomainAttr(client: any, domain) { const params: any = this.buildProps(); - params.Domain = this.domain; + + params.Domain = domain; const ret = await client.ModifyDomainAttributes(params); this.checkRet(ret); - this.logger.info( - '设置腾讯云CLB证书(sni)成功:', - ret.RequestId, - '->loadBalancerId:', - this.loadBalancerId, - 'listenerId', - this.listenerId, - 'domain:', - this.domain - ); + this.logger.info('设置腾讯云CLB证书(sni)成功:', ret.RequestId, '->loadBalancerId:', this.loadBalancerId, 'listenerId', this.listenerId, 'domain:', domain); return ret; } appendTimeSuffix(name: string) {