feat: deployFlow

This commit is contained in:
xiaojunnuo
2020-12-16 00:35:05 +08:00
parent a4bd29e6bf
commit 06603759fd
23 changed files with 6397 additions and 9 deletions
+14
View File
@@ -0,0 +1,14 @@
{
"extends": "standard",
"env": {
"mocha": true
},
"overrides": [
{
"files": ["*.test.js", "*.spec.js"],
"rules": {
"no-unused-expressions": "off"
}
}
]
}
+27
View File
@@ -0,0 +1,27 @@
{
"name": "@certd/plugins",
"version": "0.0.1",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"dependencies": {
"@alicloud/pop-core": "^1.7.10",
"@types/node": "^14.14.13",
"lodash": "^4.17.20",
"log4js": "^6.3.0"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^7.15.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"mocha": "^8.2.1"
},
"author": "Greper",
"license": "MIT"
}
@@ -0,0 +1,17 @@
import fs from 'fs'
export class AbstractPlugin {
async executeFromContextFile (options = {}) {
const { contextPath } = options
const contextJson = fs.readFileSync(contextPath)
const context = JSON.parse(contextJson)
const newContext = await this.execute(options, context)
fs.writeFileSync(JSON.stringify(newContext || context))
}
async execute (options, context) {
console.log('请实现此方法,context:', context)
return context
}
}
+4
View File
@@ -0,0 +1,4 @@
import { UploadCertToAliyun } from './upload/upload-cert-to-aliyun/index.js'
export default {
UploadCertToAliyun
}
@@ -0,0 +1,33 @@
import { AbstractPlugin } from '../../abstract-plugin/index.js'
import Core from '@alicloud/pop-core'
import dayjs from 'dayjs'
export class UploadCertToAliyunPlugin extends AbstractPlugin {
getClient (aliyunProvider) {
this.client = new Core({
accessKeyId: aliyunProvider.accessKeyId,
accessKeySecret: aliyunProvider.accessKeySecret,
endpoint: 'https://alidns.aliyuncs.com',
apiVersion: '2015-01-09'
})
}
async execute ({ providers, cert, args, context }) {
const { name, provider } = args
const certName = name + '-' + dayjs().format('YYYYMMDDHHmmss')
const params = {
RegionId: 'cn-hangzhou',
Name: certName,
Cert: cert.crt.toString(),
Key: cert.key.toString()
}
const requestOption = {
method: 'POST'
}
const client = this.getClient(providers[provider])
const ret = await client.request('CreateUserCertificate', params, requestOption)
context.AliyunCertId = ret.CertId
}
}
+11
View File
@@ -0,0 +1,11 @@
import util from './util.js'
import log4js from 'log4js'
import path from 'path'
const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
const filename = path.join(util.getUserBasePath(), '/logs/certd.log')
log4js.configure({
appenders: { std: { type: 'stdout' }, file: { type: 'file', pattern: 'yyyy-MM-dd', daysToKeep: 3, filename } },
categories: { default: { appenders: ['file', 'std'], level: level } }
})
const logger = log4js.getLogger('certd')
export default logger
+87
View File
@@ -0,0 +1,87 @@
import _ from 'lodash'
import optionsPrivate from './options.private.js'
const defaultOptions = {
providers: {
aliyun: {
providerType: 'aliyun',
accessKeyId: '',
accessKeySecret: ''
},
myLinux: {
providerType: 'SSH',
username: 'xxx',
password: 'xxx',
host: '1111.com',
port: 22,
publicKey: ''
}
},
cert: {
domains: ['*.docmirror.club', 'docmirror.club'],
email: 'xiaojunnuo@qq.com',
challenge: {
challengeType: 'dns',
dnsProvider: 'aliyun'
},
csrInfo: {
country: 'CN',
state: 'GuangDong',
locality: 'ShengZhen',
organization: 'CertD Org.',
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)
export default defaultOptions
@@ -0,0 +1,17 @@
import pkg from 'chai'
import UploadCertToAliyun from '../../src/upload/upload-cert-to-aliyun/index.js'
import options from '../options'
import Certd from '@certd/certd'
const { expect } = pkg
describe('PluginUploadCertToAliyun', function () {
it('#execute', function () {
const plugin = new UploadCertToAliyun()
const certd = new Certd()
const cert = certd.readCurrentCert('xiaojunnuo@qq.com', ['*.docmirror.cn'])
plugin.execute({
providers: options.providers,
cert,
args: { name: '上传证书到阿里云', provider: 'aliyun' }
})
})
})
File diff suppressed because it is too large Load Diff