refactor: 1

master
xiaojunnuo 2021-01-08 15:39:23 +08:00
parent ae6b0fb111
commit 30cd62664b
3 changed files with 30 additions and 17 deletions

View File

@ -5,12 +5,19 @@ import _ from 'lodash-es'
import dayjs from 'dayjs'
import { Trace } from './trace.js'
const logger = util.logger
function createDefaultOptions () {
return {
args: {
forceCert: false,
forceDeploy: true,
forceRedeploy: false
}
}
}
export class Executor {
constructor (args = {}) {
const { plugins } = args
this.plugins = {}
constructor () {
this.usePlugins(DefaultPlugins)
this.usePlugins(plugins)
this.trace = new Trace()
}
@ -33,12 +40,10 @@ export class Executor {
}
}
async run (options, args) {
async run (options) {
logger.info('------------------- Cert-D ---------------------')
try {
if (args != null) {
_.merge(options.args, args)
}
options = _.merge(createDefaultOptions(), options)
return await this.doRun(options)
} catch (e) {
logger.error('任务执行出错:', e)
@ -67,14 +72,14 @@ export class Executor {
}
}
// 读取上次执行进度
let context = {
certIsNew: !!cert.isNew
}
let context = {}
const contextJson = await certd.certStore.getCurrentFile('context.json')
if (contextJson) {
context = JSON.parse(contextJson)
}
context.certIsNew = !!cert.isNew
const trace = new Trace(context)
// 运行部署任务
try {
@ -85,10 +90,17 @@ export class Executor {
logger.info('任务完成')
trace.print()
return {
const result = trace.get({ })
const returnData = {
cert,
context
context,
result
}
if (result.status === 'error') {
const err = new Error(result.remark)
err.data = returnData
}
return returnData
}
async runCertd (certd) {
@ -125,6 +137,7 @@ export class Executor {
trace.set({ deployName, value: { status: 'success', remark: '执行成功' } })
} catch (e) {
trace.set({ deployName, value: { status: 'error', remark: '执行失败:' + e.message } })
trace.set({ value: { status: 'error', remark: deployName + '执行失败:' + e.message } })
logger.error('流程执行失败', e)
}

View File

@ -41,10 +41,10 @@ export class Trace {
print () {
const context = this.context
logger.info('---------------------------任务结果总览--------------------------')
if (!context.certIsNew) {
this.printTraceLine({ current: 'skip', remark: '还未到过期时间,跳过' }, '更新证书')
} else {
if (context.certIsNew) {
this.printTraceLine({ current: 'success', remark: '证书更新成功' }, '更新证书')
} else {
this.printTraceLine({ current: 'skip', remark: '还未到过期时间,跳过' }, '更新证书')
}
const trace = this.get({ })
// logger.info('trace', trace)

View File

@ -54,7 +54,7 @@ export class UploadCertToTencent extends AbstractTencentPlugin {
async execute ({ cert, props, context, logger }) {
const { name, accessProvider } = props
const certName = this.appendTimeSuffix(name)
const certName = this.appendTimeSuffix(name || cert.domain)
const provider = this.getAccessProvider(accessProvider)
const client = this.getClient(provider)