perf: 支持华为云cdn

pull/229/head
xiaojunnuo 2024-10-29 13:59:20 +08:00
parent fea4669d82
commit 81a3fdbc29
4 changed files with 131 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import log4js, { LoggingEvent, Logger } from "log4js";
import log4js, { LoggingEvent, Logger } from 'log4js';
const OutputAppender = {
configure: (config: any, layouts: any, findAppender: any, levels: any) => {
@ -18,18 +18,22 @@ const OutputAppender = {
},
};
// @ts-ignore
log4js.configure({
appenders: { std: { type: "stdout" }, output: { type: OutputAppender } },
categories: { default: { appenders: ["std"], level: "info" }, pipeline: { appenders: ["std", "output"], level: "info" } },
});
export const logger = log4js.getLogger("default");
export function resetLogConfigure() {
// @ts-ignore
log4js.configure({
appenders: { std: { type: 'stdout' }, output: { type: OutputAppender } },
categories: { default: { appenders: ['std'], level: 'info' }, pipeline: { appenders: ['std', 'output'], level: 'info' } },
});
}
resetLogConfigure();
export const logger = log4js.getLogger('default');
export function buildLogger(write: (text: string) => void) {
const logger = log4js.getLogger("pipeline");
logger.addContext("outputHandler", {
const logger = log4js.getLogger('pipeline');
logger.addContext('outputHandler', {
write,
});
return logger;
}
export type ILogger = Logger;

View File

@ -38,6 +38,8 @@
"@certd/plugin-cert": "^1.26.15",
"@certd/plugin-plus": "^1.26.15",
"@certd/plus-core": "^1.26.15",
"@huaweicloud/huaweicloud-sdk-cdn": "^3.1.120",
"@huaweicloud/huaweicloud-sdk-core": "^3.1.120",
"@koa/cors": "^5.0.0",
"@midwayjs/bootstrap": "~3.17.1",
"@midwayjs/cache": "~3.14.0",

View File

@ -1,2 +1,3 @@
export * from './access/index.js';
export * from './dns-provider/index.js';
export * from './plugins/deploy-to-cdn/index.js';

View File

@ -0,0 +1,115 @@
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, resetLogConfigure, RunStrategy, TaskInput } from '@certd/pipeline';
import { HuaweiAccess } from '../../access/index.js';
import { CertInfo } from '@certd/plugin-cert';
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from '@certd/plugin-plus';
@IsTaskPlugin({
name: 'HauweiDeployCertToCDN',
title: '部署证书至华为云CDN',
icon: 'ant-design:huawei-outlined',
group: pluginGroups.huawei.key,
desc: '',
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
},
})
export class HauweiDeployCertToCDN extends AbstractTaskPlugin {
@TaskInput({
title: '域名证书',
helper: '请选择前置任务输出的域名证书',
component: {
name: 'output-selector',
from: ['CertApply', 'CertApplyLego'],
},
required: true,
})
cert!: CertInfo;
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
certDomains!: string[];
@TaskInput({
title: 'Access授权',
helper: '华为云授权AccessKeyId、AccessKeySecret',
component: {
name: 'access-selector',
type: 'huawei',
},
required: true,
})
accessId!: string;
@TaskInput(
createRemoteSelectInputDefine({
title: 'CDN域名',
helper: '请选择域名或输入域名',
typeName: 'HauweiDeployCertToCDN',
action: HauweiDeployCertToCDN.prototype.onGetDomainList.name,
})
)
domains!: string[];
async execute(): Promise<void> {
this.logger.info('开始部署证书到华为云cdn');
const { cdn, client } = await this.getCdnClient();
const httpsConfig = new cdn.HttpPutBody()
.withHttpsStatus('on')
.withCertificateType('server')
.withCertificateName(this.appendTimeSuffix('certd'))
.withCertificateValue(this.cert.crt)
.withPrivateKey(this.cert.key);
const config = new cdn.Configs().withHttps(httpsConfig);
const body = new cdn.ModifyDomainConfigRequestBody().withConfigs(config);
if (!this.domains || this.domains.length === 0) {
throw new Error('您还未配置CDN域名');
}
this.logger.info('部署域名:', JSON.stringify(this.domains));
for (const domain of this.domains) {
this.logger.info('部署到域名:', domain);
const req = new cdn.UpdateDomainFullConfigRequest().withDomainName(domain).withBody(body);
await client.updateDomainFullConfig(req);
this.logger.info(`部署到域名${domain}完成:`);
}
this.logger.info('部署证书到华为云cdn完成');
}
async getCdnClient() {
const access = await this.accessService.getById<HuaweiAccess>(this.accessId);
const { BasicCredentials } = await import('@huaweicloud/huaweicloud-sdk-core');
const cdn = await import('@huaweicloud/huaweicloud-sdk-cdn/v2/public-api.js');
//恢复华为云把log4j的config改了的问题
resetLogConfigure();
const credentials = new BasicCredentials().withAk(access.accessKeyId).withSk(access.accessKeySecret);
const client = cdn.CdnClient.newBuilder().withCredential(credentials).withEndpoint('https://cdn.myhuaweicloud.com').build();
return {
client,
cdn,
};
}
async onGetDomainList(data: any) {
const { client, cdn } = await this.getCdnClient();
const request = new cdn.ListDomainsRequest();
request.pageNumber = 1000;
const result = await client.listDomains(request);
if (!result || !result.domains || result.domains.length === 0) {
throw new Error('未找到CDN域名您可以手动输入');
}
const domains = result.domains.map(domain => {
return {
value: domain.domainName,
label: domain.domainName,
domain: domain.domainName,
};
});
return this.ctx.utils.options.buildGroupOptions(domains, this.certDomains);
}
}
new HauweiDeployCertToCDN();