perf: 上传到主机,支持设置不mkdirs

pull/101/head
xiaojunnuo 2024-07-08 11:19:02 +08:00
parent f9c9fce581
commit 5ba9831ed1
2 changed files with 56 additions and 32 deletions

View File

@ -4,13 +4,13 @@ import path from 'path';
import _ from 'lodash'; import _ from 'lodash';
import { ILogger } from '@certd/pipeline'; import { ILogger } from '@certd/pipeline';
import iconv from 'iconv-lite'; import iconv from 'iconv-lite';
import {SshAccess} from "../access"; import { SshAccess } from '../access';
export class AsyncSsh2Client { export class AsyncSsh2Client {
conn: ssh2.Client; conn: ssh2.Client;
logger: ILogger; logger: ILogger;
connConf: ssh2.ConnectConfig; connConf: ssh2.ConnectConfig;
windows:boolean = false; windows = false;
encoding:string; encoding: string;
constructor(connConf: SshAccess, logger: ILogger) { constructor(connConf: SshAccess, logger: ILogger) {
this.connConf = connConf; this.connConf = connConf;
this.logger = logger; this.logger = logger;
@ -19,7 +19,7 @@ export class AsyncSsh2Client {
} }
convert(buffer: Buffer) { convert(buffer: Buffer) {
if(this.encoding){ if (this.encoding) {
return iconv.decode(buffer, this.encoding); return iconv.decode(buffer, this.encoding);
} }
return buffer.toString(); return buffer.toString();
@ -77,7 +77,7 @@ export class AsyncSsh2Client {
reject(err); reject(err);
return; return;
} }
let data: string = ''; let data = '';
stream stream
.on('close', (code: any, signal: any) => { .on('close', (code: any, signal: any) => {
this.logger.info(`[${this.connConf.host}][close]:code:${code}`); this.logger.info(`[${this.connConf.host}][close]:code:${code}`);
@ -88,14 +88,16 @@ export class AsyncSsh2Client {
} }
}) })
.on('data', (ret: Buffer) => { .on('data', (ret: Buffer) => {
const out = this.convert(ret) const out = this.convert(ret);
data += out data += out;
this.logger.info(`[${this.connConf.host}][info]: ` + out.trimEnd()); this.logger.info(`[${this.connConf.host}][info]: ` + out.trimEnd());
}) })
.stderr.on('data', (ret:Buffer) => { .stderr.on('data', (ret: Buffer) => {
const err = this.convert(ret) const err = this.convert(ret);
data += err data += err;
this.logger.info(`[${this.connConf.host}][error]: ` + err.trimEnd()); this.logger.info(
`[${this.connConf.host}][error]: ` + err.trimEnd()
);
}); });
}); });
}); });
@ -118,15 +120,15 @@ export class AsyncSsh2Client {
resolve(output); resolve(output);
}) })
.on('data', (ret: Buffer) => { .on('data', (ret: Buffer) => {
const data = this.convert(ret) const data = this.convert(ret);
this.logger.info('' + data); this.logger.info('' + data);
output.push(data); output.push(data);
}) })
.stderr.on('data', (ret:Buffer) => { .stderr.on('data', (ret: Buffer) => {
const data = this.convert(ret) const data = this.convert(ret);
output.push(data); output.push(data);
this.logger.info(`[${this.connConf.host}][error]: ` + data); this.logger.info(`[${this.connConf.host}][error]: ` + data);
}); });
stream.end(script + '\nexit\n'); stream.end(script + '\nexit\n');
}); });
}); });
@ -153,30 +155,39 @@ export class SshClient {
} }
* @param options * @param options
*/ */
async uploadFiles(options: { connectConf: SshAccess; transports: any }) { async uploadFiles(options: {
const { connectConf, transports } = options; connectConf: SshAccess;
transports: any;
mkdirs: boolean;
}) {
const { connectConf, transports, mkdirs } = options;
await this._call({ await this._call({
connectConf, connectConf,
callable: async (conn: AsyncSsh2Client) => { callable: async (conn: AsyncSsh2Client) => {
const sftp = await conn.getSftp(); const sftp = await conn.getSftp();
this.logger.info('开始上传'); this.logger.info('开始上传');
for (const transport of transports) { for (const transport of transports) {
let filePath = path.dirname(transport.remotePath); if (mkdirs !== false) {
let mkdirCmd = `mkdir -p ${filePath} `; const filePath = path.dirname(transport.remotePath);
if(conn.windows){ let mkdirCmd = `mkdir -p ${filePath} `;
if(filePath.indexOf("/") > -1){ if (conn.windows) {
this.logger.info("--------------------------") if (filePath.indexOf('/') > -1) {
this.logger.info("请注意windows下文件目录分隔应该写成\\而不是/") this.logger.info('--------------------------');
this.logger.info("--------------------------") this.logger.info(
} '请注意windows下文件目录分隔应该写成\\而不是/'
const spec = await conn.exec(`echo %COMSPEC%`); );
if (spec.toString().trim() === '%COMSPEC%') { this.logger.info('--------------------------');
mkdirCmd = `New-Item -ItemType Directory -Path "${filePath}" -Force`; }
} else { const spec = await conn.exec('echo %COMSPEC%');
mkdirCmd = `if not exist "${filePath}" mkdir "${filePath}"`; if (spec.toString().trim() === '%COMSPEC%') {
mkdirCmd = `New-Item -ItemType Directory -Path "${filePath}" -Force`;
} else {
mkdirCmd = `if not exist "${filePath}" mkdir "${filePath}"`;
}
} }
await conn.exec(mkdirCmd);
} }
await conn.exec(mkdirCmd);
await conn.fastPut({ sftp, ...transport }); await conn.fastPut({ sftp, ...transport });
} }
this.logger.info('文件全部上传成功'); this.logger.info('文件全部上传成功');

View File

@ -48,6 +48,7 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
required: true, required: true,
}) })
cert!: CertInfo; cert!: CertInfo;
@TaskInput({ @TaskInput({
title: '主机登录配置', title: '主机登录配置',
helper: 'access授权', helper: 'access授权',
@ -59,13 +60,24 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
}) })
accessId!: string; accessId!: string;
@TaskInput({
title: '自动创建远程目录',
helper: '是否自动创建远程目录,如果关闭则你需要自己确保远程目录存在',
default: true,
component: {
name: 'a-switch',
vModel: 'checked',
},
})
mkdirs = true;
@TaskInput({ @TaskInput({
title: '仅复制到当前主机', title: '仅复制到当前主机',
helper: helper:
'开启后将直接复制到当前主机某个目录不上传到主机由于是docker启动实际上是复制到docker容器内的“证书保存路径”你需要事先在docker-compose.yaml中配置主机目录映射 volumes: /your_target_path:/your_target_path', '开启后将直接复制到当前主机某个目录不上传到主机由于是docker启动实际上是复制到docker容器内的“证书保存路径”你需要事先在docker-compose.yaml中配置主机目录映射 volumes: /your_target_path:/your_target_path',
default: false,
component: { component: {
name: 'a-switch', name: 'a-switch',
default: false,
vModel: 'checked', vModel: 'checked',
}, },
}) })
@ -135,6 +147,7 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
remotePath: keyPath, remotePath: keyPath,
}, },
], ],
mkdirs: this.mkdirs,
}); });
this.logger.info( this.logger.info(
'证书上传成功crtPath=', '证书上传成功crtPath=',