2020-12-16 00:35:05 +08:00
|
|
|
|
import fs from 'fs'
|
2020-12-21 00:32:17 +08:00
|
|
|
|
import logger from '../utils/util.log.js'
|
2020-12-28 00:22:12 +08:00
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
|
|
import Sleep from '../utils/util.sleep.js'
|
2020-12-16 00:35:05 +08:00
|
|
|
|
export class AbstractPlugin {
|
2020-12-21 00:32:17 +08:00
|
|
|
|
constructor () {
|
|
|
|
|
|
this.logger = logger
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-28 00:22:12 +08:00
|
|
|
|
appendTimeSuffix (name) {
|
|
|
|
|
|
if (name == null) {
|
|
|
|
|
|
name = 'certd'
|
|
|
|
|
|
}
|
|
|
|
|
|
return name + '-' + dayjs().format('YYYYMMDD-HHmmss')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-16 00:35:05 +08:00
|
|
|
|
async executeFromContextFile (options = {}) {
|
|
|
|
|
|
const { contextPath } = options
|
|
|
|
|
|
const contextJson = fs.readFileSync(contextPath)
|
|
|
|
|
|
const context = JSON.parse(contextJson)
|
2020-12-26 01:37:53 +08:00
|
|
|
|
options.context = context
|
2020-12-28 00:22:12 +08:00
|
|
|
|
await this.doExecute(options)
|
|
|
|
|
|
fs.writeFileSync(JSON.stringify(context))
|
2020-12-16 00:35:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-26 01:37:53 +08:00
|
|
|
|
async doExecute (options) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
return await this.execute(options)
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
logger.error('插件执行出错:', e)
|
|
|
|
|
|
throw e
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 执行
|
|
|
|
|
|
* @param options
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
*/
|
|
|
|
|
|
async execute (options) {
|
|
|
|
|
|
console.error('请实现此方法,context:', options.context)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async doRollback (options) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
return await this.rollback(options)
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
logger.error('插件rollback出错:', e)
|
|
|
|
|
|
throw e
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 回退,如有必要
|
|
|
|
|
|
* @param options
|
|
|
|
|
|
*/
|
|
|
|
|
|
async rollback (options) {
|
|
|
|
|
|
console.error('请实现此方法,rollback:', options.context)
|
2020-12-16 00:35:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-21 00:32:17 +08:00
|
|
|
|
getAccessProvider (accessProvider, accessProviders) {
|
|
|
|
|
|
if (typeof accessProvider === 'string' && accessProviders) {
|
|
|
|
|
|
accessProvider = accessProviders[accessProvider]
|
|
|
|
|
|
}
|
|
|
|
|
|
return accessProvider
|
|
|
|
|
|
}
|
2020-12-28 00:22:12 +08:00
|
|
|
|
|
|
|
|
|
|
async sleep (time) {
|
|
|
|
|
|
await Sleep(time)
|
|
|
|
|
|
}
|
2020-12-16 00:35:05 +08:00
|
|
|
|
}
|