From 399c23623d5718ca3edf3462d1deb8aecc393c5b Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Mon, 8 Feb 2021 15:46:03 +0800 Subject: [PATCH] refactor: host --- .../api/src/dns-provider/abstract-dns-provider.js | 2 +- packages/core/api/src/plugin/abstract-plugin.js | 8 ++++++-- packages/core/executor/src/index.js | 4 ++++ packages/plugins/plugin-host/src/plugins/ssh.js | 10 +++++----- .../plugin-host/src/plugins/upload-to-host/index.js | 1 + test/options.js | 12 +++++------- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/packages/core/api/src/dns-provider/abstract-dns-provider.js b/packages/core/api/src/dns-provider/abstract-dns-provider.js index 2eb10273..211e4a1f 100644 --- a/packages/core/api/src/dns-provider/abstract-dns-provider.js +++ b/packages/core/api/src/dns-provider/abstract-dns-provider.js @@ -1,6 +1,6 @@ import _ from 'lodash-es' import logger from '../utils/util.log.js' -import commonUtil from '../utils/util.common' +import commonUtil from '../utils/util.common.js' export class AbstractDnsProvider { constructor ({ accessProviders }) { this.logger = logger diff --git a/packages/core/api/src/plugin/abstract-plugin.js b/packages/core/api/src/plugin/abstract-plugin.js index d7f44bbf..9a537002 100644 --- a/packages/core/api/src/plugin/abstract-plugin.js +++ b/packages/core/api/src/plugin/abstract-plugin.js @@ -65,10 +65,14 @@ export class AbstractPlugin { } getAccessProvider (accessProvider, accessProviders = this.accessProviders) { + let access = accessProvider if (typeof accessProvider === 'string' && accessProviders) { - accessProvider = accessProviders[accessProvider] + access = accessProviders[accessProvider] } - return accessProvider + if (access == null) { + throw new Error(`accessProvider :${accessProvider}不存在`) + } + return access } async sleep (time) { diff --git a/packages/core/executor/src/index.js b/packages/core/executor/src/index.js index 24662f8e..7cd54505 100644 --- a/packages/core/executor/src/index.js +++ b/packages/core/executor/src/index.js @@ -63,6 +63,9 @@ export class Executor { logger.info('证书无更新,无需重新部署') logger.info('任务完成') return { cert } + } else { + // 强制重新运行,清空保存的状态 + await certd.certStore.setCurrentFile('context.json', '{}') } } // 读取上次执行进度 @@ -118,6 +121,7 @@ export class Executor { logger.info('此流程已被禁用,跳过') logger.info('') deployTrace.set({ value: { current: 'skip', status: 'disabled', remark: '流程禁用' } }) + deployTrace.set({ tasks: null }) continue } try { diff --git a/packages/plugins/plugin-host/src/plugins/ssh.js b/packages/plugins/plugin-host/src/plugins/ssh.js index 4d4f17c6..9601675c 100644 --- a/packages/plugins/plugin-host/src/plugins/ssh.js +++ b/packages/plugins/plugin-host/src/plugins/ssh.js @@ -29,7 +29,7 @@ export class SshClient { try { for (const transport of transports) { logger.info('上传文件:', JSON.stringify(transport)) - await this.exec({ conn, cmd: 'mkdir ' + path.dirname(transport.remotePath) }) + await this.exec({ connectConf, script: 'mkdir -p ' + path.dirname(transport.remotePath) }) await this.fastPut({ sftp, ...transport }) } resolve() @@ -59,10 +59,10 @@ export class SshClient { let data = null stream.on('close', (code, signal) => { console.log(`[${connectConf.host}][close]:code:${code}, signal:${signal} `) + if (code === 0) { - resolve(data.toString()) - } else { - reject(data.toString()) + data = data ? data.toString() : null + resolve(data) } conn.end() }).on('data', (ret) => { @@ -70,7 +70,7 @@ export class SshClient { data = ret }).stderr.on('data', (err) => { console.log(`[${connectConf.host}][error]: ` + err) - data = err + reject(new Error(err.toString())) stream.close() }) }) diff --git a/packages/plugins/plugin-host/src/plugins/upload-to-host/index.js b/packages/plugins/plugin-host/src/plugins/upload-to-host/index.js index 21f30ed6..381c99fd 100644 --- a/packages/plugins/plugin-host/src/plugins/upload-to-host/index.js +++ b/packages/plugins/plugin-host/src/plugins/upload-to-host/index.js @@ -45,6 +45,7 @@ export class UploadCertToHost extends AbstractHostPlugin { async execute ({ cert, props, context }) { const { crtPath, keyPath, accessProvider } = props const connectConf = this.getAccessProvider(accessProvider) + console.log('connectConf', connectConf) const sshClient = new SshClient() await sshClient.uploadFiles({ connectConf, diff --git a/test/options.js b/test/options.js index 6aadb261..2394f87c 100644 --- a/test/options.js +++ b/test/options.js @@ -79,24 +79,22 @@ const defaultOptions = { }, { deployName: '流程2-部署到nginx服务器', - disabled: true, + disabled: false, tasks: [ { taskName: '上传证书到服务器', type: 'uploadCertToHost', props:{ - accessProvider: 'aliyun-linux', - upload: [ - { from: '{certPath}', to: '/xxx/xxx/xxx.cert.pem' }, - { from: '{keyPath}', to: '/xxx/xxx/xxx.key' } - ] + accessProvider: 'aliyun-ssh', + crtPath: '/root/certd-test/cert.pem', + keyPath: '/root/certd-test/cert.key' } }, { taskName: '重启linux', type: 'hostShellExecute', props:{ - accessProvider: 'aliyun-linux', + accessProvider: 'aliyun-ssh', script: ['ls'] } }