diff --git a/packages/core/pipeline/src/core/run-history.ts b/packages/core/pipeline/src/core/run-history.ts index 040f37ab..20d72ee9 100644 --- a/packages/core/pipeline/src/core/run-history.ts +++ b/packages/core/pipeline/src/core/run-history.ts @@ -120,10 +120,9 @@ export class RunHistory { delete e.stack; delete e.cause; if (runnable.runnableType === "step") { - this._loggers[runnable.id].error(`[${runnable.runnableType}] [${runnable.title}] :`, e, stack, cause); - } else { - this._loggers[runnable.id].error(`[${runnable.runnableType}] [${runnable.title}] :`, e.message); + this._loggers[runnable.id].error(stack, cause); } + this._loggers[runnable.id].error(`[${runnable.runnableType}] [${runnable.title}] :`, e.message); } finally(runnable: Runnable) { diff --git a/packages/plugins/plugin-cert/src/plugin/cert-plugin/custom/index.ts b/packages/plugins/plugin-cert/src/plugin/cert-plugin/custom/index.ts index 841b8914..94ed7613 100644 --- a/packages/plugins/plugin-cert/src/plugin/cert-plugin/custom/index.ts +++ b/packages/plugins/plugin-cert/src/plugin/cert-plugin/custom/index.ts @@ -6,6 +6,7 @@ import dayjs from "dayjs"; export { CertReader }; export type { CertInfo }; + @IsTaskPlugin({ name: "CertApplyUpload", icon: "ph:certificate", @@ -62,6 +63,19 @@ export type { CertInfo }; }, }) export class CertApplyUploadPlugin extends CertApplyBaseConvertPlugin { + @TaskInput({ + title: "过期前提醒", + value: 10, + component: { + name: "a-input-number", + vModel: "value", + }, + required: true, + order: 100, + helper: "到期前多少天提醒", + }) + renewDays!: number; + @TaskInput({ title: "手动上传证书", component: { @@ -97,6 +111,7 @@ export class CertApplyUploadPlugin extends CertApplyBaseConvertPlugin { this.userContext = this.ctx.userContext; this.lastStatus = this.ctx.lastStatus as Step; } + async onInit(): Promise {} async getCertFromStore() { @@ -107,48 +122,54 @@ export class CertApplyUploadPlugin extends CertApplyBaseConvertPlugin { } catch (e) { this.logger.warn("读取cert失败:", e); } - if (certReader == null) { - certReader = new CertReader(this.uploadCert); - } - if (!certReader.expires || certReader.expires < new Date().getTime()) { - throw new Error("证书已过期,停止部署,请重新上传证书"); - } - return certReader; } - async execute(): Promise { - let certReader = await this.getCertFromStore(); - const crtMd5 = this.ctx.utils.hash.md5(certReader.cert.crt); - - const leftDays = dayjs(certReader.expires).diff(dayjs(), "day"); - this.logger.info(`证书过期时间${dayjs(certReader.expires).format("YYYY-MM-DD HH:mm:ss")},剩余${leftDays}天`); - - if (!this.ctx.inputChanged) { - this.logger.info("输入参数无变化"); - const lastCrtMd5 = this.lastStatus?.status?.output?.certMd5; - this.logger.info("证书MD5", crtMd5); - this.logger.info("上次证书MD5", lastCrtMd5); - if (lastCrtMd5 === crtMd5) { - this.logger.info("证书无变化,跳过"); - //输出证书MD5 - this.certMd5 = crtMd5; - await this.output(certReader, false); - return "skip"; + private checkExpires(certReader: CertReader) { + const renewDays = (this.renewDays ?? 10) * 24 * 60 * 60 * 1000; + if (certReader.expires) { + if (certReader.expires < new Date().getTime()) { + throw new Error("证书已过期,停止部署,请尽快上传新证书"); + } + if (certReader.expires < new Date().getTime() + renewDays) { + throw new Error("证书即将已过期,停止部署,请尽快上传新证书"); + } + } + } + + async execute(): Promise { + const oldCertReader = await this.getCertFromStore(); + if (oldCertReader) { + const leftDays = dayjs(oldCertReader.expires).diff(dayjs(), "day"); + this.logger.info(`证书过期时间${dayjs(oldCertReader.expires).format("YYYY-MM-DD HH:mm:ss")},剩余${leftDays}天`); + this.checkExpires(oldCertReader); + if (!this.ctx.inputChanged) { + this.logger.info("输入参数无变化"); + const lastCrtMd5 = this.lastStatus?.status?.output?.certMd5; + const newCrtMd5 = this.ctx.utils.hash.md5(this.uploadCert.crt); + this.logger.info("证书MD5", newCrtMd5); + this.logger.info("上次证书MD5", lastCrtMd5); + if (lastCrtMd5 === newCrtMd5) { + this.logger.info("证书无变化,跳过"); + //输出证书MD5 + this.certMd5 = newCrtMd5; + await this.output(oldCertReader, false); + return "skip"; + } + this.logger.info("证书有变化,重新部署"); + } else { + this.logger.info("输入参数有变化,重新部署"); } - this.logger.info("证书有变化,重新部署"); - } else { - this.logger.info("输入参数有变化,重新部署"); } - certReader = new CertReader(this.uploadCert); + const newCertReader = new CertReader(this.uploadCert); this.clearLastStatus(); //输出证书MD5 - this.certMd5 = this.ctx.utils.hash.md5(certReader.cert.crt); - const newLeftDays = dayjs(certReader.expires).diff(dayjs(), "day"); - this.logger.info(`新证书过期时间${dayjs(certReader.expires).format("YYYY-MM-DD HH:mm:ss")},剩余${newLeftDays}天`); - - await this.output(certReader, true); + this.certMd5 = this.ctx.utils.hash.md5(newCertReader.cert.crt); + const newLeftDays = dayjs(newCertReader.expires).diff(dayjs(), "day"); + this.logger.info(`新证书过期时间${dayjs(newCertReader.expires).format("YYYY-MM-DD HH:mm:ss")},剩余${newLeftDays}天`); + this.checkExpires(newCertReader); + await this.output(newCertReader, true); //必须output之后执行 await this.emitCertApplySuccess();