feat: 自动化流程

This commit is contained in:
xiaojunnuo
2020-12-21 00:32:17 +08:00
parent e84f5e8b0a
commit 6529fd2fdb
27 changed files with 343 additions and 184 deletions

View File

@@ -1,5 +1,10 @@
import fs from 'fs'
import logger from '../utils/util.log.js'
export class AbstractPlugin {
constructor () {
this.logger = logger
}
async executeFromContextFile (options = {}) {
const { contextPath } = options
const contextJson = fs.readFileSync(contextPath)
@@ -13,5 +18,10 @@ export class AbstractPlugin {
return context
}
getAccessProvider (accessProvider, accessProviders) {
if (typeof accessProvider === 'string' && accessProviders) {
accessProvider = accessProviders[accessProvider]
}
return accessProvider
}
}

View File

@@ -1,20 +1,13 @@
import { AbstractPlugin } from '../abstract-plugin.js'
import { AbstractPlugin } from '../abstract-plugin/index.js'
export class AbstractAliyunPlugin extends AbstractPlugin {
format (pem) {
pem = pem.replace(/\r/g, '')
pem = pem.replace(/\n\n/g, '')
pem = pem.replace(/\n\n/g, '\n')
pem = pem.replace(/\n$/g, '')
return pem
}
getAccessProvider (accessProvider, accessProviders) {
if (typeof accessProvider === 'string' && accessProviders) {
accessProvider = accessProviders[accessProvider]
}
return accessProvider
}
checkRet (ret) {
if (ret.code != null) {
throw new Error('执行失败:', ret.Message)

View File

@@ -1,7 +1,7 @@
import { AbstractPlugin } from '../../abstract-plugin/index.js'
import { AbstractAliyunPlugin } from '../../aliyun/abstract-aliyun.js'
import Core from '@alicloud/pop-core'
import dayjs from 'dayjs'
export class UploadCertToAliyunPlugin extends AbstractPlugin {
export class DeployCertToAliyunCDN extends AbstractAliyunPlugin {
/**
* 插件定义
* 名称
@@ -10,7 +10,7 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
*/
static define () {
return {
name: 'deployToCdn',
name: 'deployCertToAliyunCDN',
label: '部署到阿里云CDN',
input: {
domainName: {
@@ -21,10 +21,11 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
label: '证书名称'
},
certType: {
value: 'upload',
label: '证书来源',
options: [
{ value: 'upload', label: '直接上传' },
{ value: 'cas', label: '从证书库需要uploadCertToAliyun插件作为前置任务' }
{ value: 'cas', label: '从证书库', desc: '需要uploadCertToAliyun作为前置任务' }
],
required: true
},
@@ -76,12 +77,8 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
ServerCertificateStatus: 'on',
CertName: CertName,
CertType: certType,
ServerCertificate: context.aliyunCertId
}
if (certType === 'upload') {
// eslint-disable-next-line no-unused-expressions
params.ServerCertificate = this.format(cert.crt.toString()),
params.PrivateKey = this.format(cert.key.toString())
ServerCertificate: super.format(cert.crt.toString()),
PrivateKey: super.format(cert.key.toString())
}
return params
}
@@ -92,6 +89,6 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
}
const ret = await client.request('SetDomainServerCertificate', params, requestOption)
this.checkRet(ret)
console.log('设置cdn证书成功', ret)
this.logger.info('设置cdn证书成功:', ret.RequestId)
}
}

View File

@@ -1,8 +1,7 @@
import { AbstractPlugin } from '../../abstract-plugin/index.js'
import Core from '@alicloud/pop-core'
import dayjs from 'dayjs'
import { AbstractAliyunPlugin } from '../abstract-aliyun.js'
export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
export class UploadCertToAliyun extends AbstractAliyunPlugin {
/**
* 插件定义
* 名称
@@ -11,7 +10,7 @@ export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
*/
static define () {
return {
name: 'updateToAliyun',
name: 'uploadCertToAliyun',
label: '上传证书到阿里云',
input: {
name: {
@@ -42,8 +41,8 @@ export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
})
}
async execute ({ accessProviders, cert, args, context }) {
const { name, provider } = args
async execute ({ accessProviders, cert, args, context, logger }) {
const { name, accessProvider } = args
const certName = name + '-' + dayjs().format('YYYYMMDDHHmmss')
const params = {
RegionId: 'cn-hangzhou',
@@ -56,10 +55,11 @@ export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
method: 'POST'
}
const accesseProvider = this.getAccessProvider(provider, accessProviders)
const client = this.getClient(accesseProvider)
const provider = super.getAccessProvider(accessProvider, accessProviders)
const client = this.getClient(provider)
const ret = await client.request('CreateUserCertificate', params, requestOption)
this.checkRet(ret)
this.logger.info('证书上传成功certId=', ret.CertId)
context.aliyunCertId = ret.CertId
}
}

View File

@@ -1,4 +1,5 @@
import { UploadCertToAliyun } from './upload/upload-cert-to-aliyun/index.js'
import { UploadCertToAliyun } from './aliyun/upload-to-aliyun/index.js'
import { DeployCertToAliyunCDN } from './aliyun/deploy-to-cdn/index.js'
export default {
UploadCertToAliyun
UploadCertToAliyun, DeployCertToAliyunCDN
}

View File

@@ -0,0 +1,21 @@
import pkg from 'chai'
import { DeployCertToAliyunCDN } from '../../src/aliyun/deploy-to-cdn/index.js'
import options from '../options.js'
import { Certd } from '@certd/certd'
const { expect } = pkg
describe('DeployToAliyunCDN', function () {
it('#execute', async function () {
const plugin = new DeployCertToAliyunCDN()
const certd = new Certd()
const cert = certd.readCurrentCert('xiaojunnuo@qq.com', ['*.docmirror.cn'])
const ret = await plugin.execute({
accessProviders: options.accessProviders,
cert,
args: { domainName: 'certd-cdn-upload.docmirror.cn', certName: 'certd部署测试', certType: 'cas', accessProvider: 'aliyun' },
context: {
aliyunCertId: '4947435'
}
})
console.log('context:', context)
})
})

View File

@@ -1,19 +1,19 @@
import pkg from 'chai'
import { UploadToAliyunPlugin } from '../../src/aliyun/upload-to-aliyun/index.js'
import { UploadCertToAliyun } from '../../src/aliyun/upload-to-aliyun/index.js'
import options from '../options.js'
import { Certd } from '@certd/certd'
const { expect } = pkg
describe('PluginUploadToAliyun', function () {
it('#execute', async function () {
const plugin = new UploadToAliyunPlugin()
const plugin = new UploadCertToAliyun()
const certd = new Certd()
const cert = certd.readCurrentCert('xiaojunnuo@qq.com', ['*.docmirror.club', 'docmirror.club'])
const cert = certd.readCurrentCert('xiaojunnuo@qq.com', ['_.docmirror.cn'])
const context = {}
await plugin.execute({
accessProviders: options.accessProviders,
cert,
args: { name: '上传证书到阿里云测试', provider: 'aliyun' },
context
context,
args: { name: 'certd部署测试', provider: 'aliyun' }
})
console.log('context:', context)

View File

@@ -1,6 +1,7 @@
import _ from 'lodash'
import optionsPrivate from '../../../test/options.private.mjs'
const defaultOptions = {
version: '1.0.0',
accessProviders: {
aliyun: {
providerType: 'aliyun',
@@ -31,55 +32,7 @@ const defaultOptions = {
organizationUnit: 'IT Department',
emailAddress: 'xiaojunnuo@qq.com'
}
},
deploy: [
{
deployName: '流程1-部署到阿里云系列产品',
tasks: [
{
name: '上传证书到云',
taskType: 'uploadCertToCloud',
certStore: 'aliyun'
},
{
name: '部署证书到SLB',
taskType: 'deployCertToAliyunSLB',
certStore: 'aliyun'
},
{
name: '部署证书到阿里云集群Ingress',
taskType: 'deployCertToAliyunK8sIngress',
certStore: 'aliyun'
}
]
},
{
deployName: '流程2-部署到nginx服务器',
tasks: [
{
name: '上传证书到服务器,并重启nginx',
taskType: 'sshAndExecute',
ssh: 'myLinux',
upload: [
{ from: '{certPath}', to: '/xxx/xxx/xxx.cert.pem' },
{ from: '{keyPath}', to: '/xxx/xxx/xxx.key' }
],
script: 'sudo systemctl restart nginx'
}
]
},
{
deployName: '流程3-触发jenkins任务',
tasks: [
{
name: '触发jenkins任务',
taskType: 'sshAndExecute',
ssh: 'myLinux',
script: 'sudo systemctl restart nginx'
}
]
}
]
}
}
_.merge(defaultOptions, optionsPrivate)