mirror of
https://github.com/certd/certd.git
synced 2026-04-24 04:17:25 +08:00
feat: 升级midway,支持esm
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
// @ts-ignore
|
||||
import ssh2, { ConnectConfig } from 'ssh2';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import * as _ from 'lodash-es';
|
||||
import { ILogger } from '@certd/pipeline';
|
||||
import iconv from 'iconv-lite';
|
||||
import {SshAccess} from "../access";
|
||||
import { SshAccess } from '../access/index.js';
|
||||
export class AsyncSsh2Client {
|
||||
conn: ssh2.Client;
|
||||
logger: ILogger;
|
||||
connConf: ssh2.ConnectConfig;
|
||||
windows:boolean = false;
|
||||
encoding:string;
|
||||
windows = false;
|
||||
encoding: string;
|
||||
constructor(connConf: SshAccess, logger: ILogger) {
|
||||
this.connConf = connConf;
|
||||
this.logger = logger;
|
||||
@@ -19,7 +19,7 @@ export class AsyncSsh2Client {
|
||||
}
|
||||
|
||||
convert(buffer: Buffer) {
|
||||
if(this.encoding){
|
||||
if (this.encoding) {
|
||||
return iconv.decode(buffer, this.encoding);
|
||||
}
|
||||
return buffer.toString();
|
||||
@@ -31,6 +31,7 @@ export class AsyncSsh2Client {
|
||||
const conn = new ssh2.Client();
|
||||
conn
|
||||
.on('error', (err: any) => {
|
||||
this.logger.error('连接失败', err);
|
||||
reject(err);
|
||||
})
|
||||
.on('ready', () => {
|
||||
@@ -77,7 +78,7 @@ export class AsyncSsh2Client {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
let data: string = '';
|
||||
let data = '';
|
||||
stream
|
||||
.on('close', (code: any, signal: any) => {
|
||||
this.logger.info(`[${this.connConf.host}][close]:code:${code}`);
|
||||
@@ -88,13 +89,13 @@ export class AsyncSsh2Client {
|
||||
}
|
||||
})
|
||||
.on('data', (ret: Buffer) => {
|
||||
const out = this.convert(ret)
|
||||
data += out
|
||||
const out = this.convert(ret);
|
||||
data += out;
|
||||
this.logger.info(`[${this.connConf.host}][info]: ` + out.trimEnd());
|
||||
})
|
||||
.stderr.on('data', (ret:Buffer) => {
|
||||
const err = this.convert(ret)
|
||||
data += err
|
||||
.stderr.on('data', (ret: Buffer) => {
|
||||
const err = this.convert(ret);
|
||||
data += err;
|
||||
this.logger.info(`[${this.connConf.host}][error]: ` + err.trimEnd());
|
||||
});
|
||||
});
|
||||
@@ -103,9 +104,7 @@ export class AsyncSsh2Client {
|
||||
|
||||
async shell(script: string | string[]): Promise<string[]> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
this.logger.info(
|
||||
`执行shell脚本:[${this.connConf.host}][shell]: ` + script
|
||||
);
|
||||
this.logger.info(`执行shell脚本:[${this.connConf.host}][shell]: ` + script);
|
||||
this.conn.shell((err: Error, stream: any) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
@@ -118,15 +117,15 @@ export class AsyncSsh2Client {
|
||||
resolve(output);
|
||||
})
|
||||
.on('data', (ret: Buffer) => {
|
||||
const data = this.convert(ret)
|
||||
const data = this.convert(ret);
|
||||
this.logger.info('' + data);
|
||||
output.push(data);
|
||||
})
|
||||
.stderr.on('data', (ret:Buffer) => {
|
||||
const data = this.convert(ret)
|
||||
.stderr.on('data', (ret: Buffer) => {
|
||||
const data = this.convert(ret);
|
||||
output.push(data);
|
||||
this.logger.info(`[${this.connConf.host}][error]: ` + data);
|
||||
});
|
||||
});
|
||||
stream.end(script + '\nexit\n');
|
||||
});
|
||||
});
|
||||
@@ -161,15 +160,15 @@ export class SshClient {
|
||||
const sftp = await conn.getSftp();
|
||||
this.logger.info('开始上传');
|
||||
for (const transport of transports) {
|
||||
let filePath = path.dirname(transport.remotePath);
|
||||
const filePath = path.dirname(transport.remotePath);
|
||||
let mkdirCmd = `mkdir -p ${filePath} `;
|
||||
if(conn.windows){
|
||||
if(filePath.indexOf("/") > -1){
|
||||
this.logger.info("--------------------------")
|
||||
this.logger.info("请注意:windows下,文件目录分隔应该写成\\而不是/")
|
||||
this.logger.info("--------------------------")
|
||||
if (conn.windows) {
|
||||
if (filePath.indexOf('/') > -1) {
|
||||
this.logger.info('--------------------------');
|
||||
this.logger.info('请注意:windows下,文件目录分隔应该写成\\而不是/');
|
||||
this.logger.info('--------------------------');
|
||||
}
|
||||
const spec = await conn.exec(`echo %COMSPEC%`);
|
||||
const spec = await conn.exec('echo %COMSPEC%');
|
||||
if (spec.toString().trim() === '%COMSPEC%') {
|
||||
mkdirCmd = `New-Item -ItemType Directory -Path "${filePath}" -Force`;
|
||||
} else {
|
||||
@@ -184,10 +183,7 @@ export class SshClient {
|
||||
});
|
||||
}
|
||||
|
||||
async exec(options: {
|
||||
connectConf: SshAccess;
|
||||
script: string | Array<string>;
|
||||
}) {
|
||||
async exec(options: { connectConf: SshAccess; script: string | Array<string> }) {
|
||||
let { script } = options;
|
||||
const { connectConf } = options;
|
||||
if (_.isArray(script)) {
|
||||
@@ -203,10 +199,7 @@ export class SshClient {
|
||||
});
|
||||
}
|
||||
|
||||
async shell(options: {
|
||||
connectConf: SshAccess;
|
||||
script: string;
|
||||
}): Promise<string[]> {
|
||||
async shell(options: { connectConf: SshAccess; script: string }): Promise<string[]> {
|
||||
const { connectConf, script } = options;
|
||||
return await this._call({
|
||||
connectConf,
|
||||
@@ -216,10 +209,7 @@ export class SshClient {
|
||||
});
|
||||
}
|
||||
|
||||
async _call(options: {
|
||||
connectConf: SshAccess;
|
||||
callable: any;
|
||||
}): Promise<string[]> {
|
||||
async _call(options: { connectConf: SshAccess; callable: any }): Promise<string[]> {
|
||||
const { connectConf, callable } = options;
|
||||
const conn = new AsyncSsh2Client(connectConf, this.logger);
|
||||
await conn.connect();
|
||||
|
||||
Reference in New Issue
Block a user