refactor: 重构

This commit is contained in:
xiaojunnuo
2021-02-08 13:40:28 +08:00
parent 82f86d9556
commit cb8c8186f1
38 changed files with 807 additions and 1895 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
{
"name": "@certd/plugins",
"name": "@certd/plugin-host",
"version": "0.1.13",
"description": "",
"main": "src/index.js",
@@ -17,7 +17,8 @@
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"mocha": "^8.2.1"
"mocha": "^8.2.1",
"@certd/certd": "^0.1.13"
},
"author": "Greper",
"license": "MIT",

View File

@@ -12,12 +12,6 @@ export class HostShellExecute extends AbstractHostPlugin {
name: 'hostShellExecute',
label: '执行远程主机脚本命令',
input: {
script: {
label: 'shell脚本命令',
component: {
name: 'a-textarea'
}
},
accessProvider: {
label: '主机登录配置',
type: [String, Object],
@@ -27,6 +21,12 @@ export class HostShellExecute extends AbstractHostPlugin {
filter: 'ssh'
},
required: true
},
script: {
label: 'shell脚本命令',
component: {
name: 'a-textarea'
}
}
},
output: {
@@ -39,7 +39,7 @@ export class HostShellExecute extends AbstractHostPlugin {
const { script, accessProvider } = props
const connectConf = this.getAccessProvider(accessProvider)
const sshClient = new SshClient()
const ret = await sshClient.shell({
const ret = await sshClient.exec({
connectConf,
script
})

View File

@@ -1,6 +1,7 @@
import ssh2 from 'ssh2'
import path from 'path'
import { util } from '@certd/api'
import _ from 'lodash-es'
const logger = util.logger
export class SshClient {
/**
@@ -42,6 +43,42 @@ export class SshClient {
})
}
exec ({ connectConf, script }) {
if (_.isArray(script)) {
script = script.join('\n')
}
return new Promise((resolve, reject) => {
this.connect({
connectConf,
onReady: (conn) => {
conn.exec(script, (err, stream) => {
if (err) {
reject(err)
return
}
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())
}
conn.end()
}).on('data', (ret) => {
console.log(`[${connectConf.host}][info]: ` + ret)
data = ret
}).stderr.on('data', (err) => {
console.log(`[${connectConf.host}][error]: ` + err)
data = err
stream.close()
})
})
}
})
})
}
shell ({ connectConf, script }) {
return new Promise((resolve, reject) => {
this.connect({
@@ -88,24 +125,4 @@ export class SshClient {
})
})
}
exec ({ conn, cmd }) {
return new Promise((resolve, reject) => {
conn.exec(cmd, (err, stream) => {
if (err) {
logger.error('执行命令出错', err)
reject(err)
// return conn.end()
}
stream.on('close', (code, signal) => {
// logger.info('Stream :: close :: code: ' + code + ', signal: ' + signal)
// conn.end()
resolve()
}).on('data', (data) => {
logger.info('data', data.toString())
})
})
})
}
}

View File

@@ -16,13 +16,12 @@ describe('HostShellExecute', function () {
const context = {}
const uploadOpts = {
cert,
props: { script: 'ls ', accessProvider: 'aliyun-ssh' },
props: { script: ['ls ', 'ls '], accessProvider: 'aliyun-ssh' },
context
}
const ret = await plugin.doExecute(uploadOpts)
for (const retElement of ret) {
console.log('-----' + retElement)
}
expect(ret).ok
console.log('-----' + JSON.stringify(ret))
await plugin.doRollback(uploadOpts)
})