perf: 上传到主机插件支持复制到本机路径

This commit is contained in:
xiaojunnuo
2024-05-30 10:54:18 +08:00
parent d9eb927b0a
commit 92446c3399
7 changed files with 107 additions and 29 deletions
@@ -14,6 +14,7 @@ import * as fs from 'fs';
@IsTaskPlugin({
name: 'uploadCertToHost',
title: '上传证书到主机',
desc: '也支持复制证书到本机',
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
@@ -53,10 +54,22 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
name: 'pi-access-selector',
type: 'ssh',
},
rules: [{ required: true, message: '此项必填' }],
rules: [{ required: false, message: '' }],
})
accessId!: string;
@TaskInput({
title: '复制到当前主机',
helper:
'开启后,将直接复制到当前主机某个目录,由于是docker启动,实际上复制到的是docker容器内的目录,你需要事先在docker-compose.yaml中配置主机目录映射: volumes: /your_target_path:/your_target_path',
component: {
name: 'a-switch',
value: false,
vModel: 'checked',
},
})
copyToThisHost!: boolean;
@TaskOutput({
title: '证书保存路径',
})
@@ -74,29 +87,46 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
this.accessService = this.ctx.accessService;
this.logger = this.ctx.logger;
}
copyFile(srcFile: string, destFile: string) {
const dir = destFile.substring(0, destFile.lastIndexOf('/'));
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.copyFileSync(srcFile, destFile);
}
async execute(): Promise<void> {
const { crtPath, keyPath, cert, accessId } = this;
const certReader = new CertReader(cert);
const connectConf = await this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
const saveCrtPath = certReader.saveToFile('crt');
const saveKeyPath = certReader.saveToFile('key');
await sshClient.uploadFiles({
connectConf,
transports: [
{
localPath: saveCrtPath,
remotePath: crtPath,
},
{
localPath: saveKeyPath,
remotePath: keyPath,
},
],
});
this.logger.info('证书上传成功:crtPath=', crtPath, ',keyPath=', keyPath);
if (this.copyToThisHost) {
this.copyFile(saveCrtPath, crtPath);
this.copyFile(saveKeyPath, keyPath);
this.logger.info('证书复制成功:crtPath=', crtPath, ',keyPath=', keyPath);
} else {
if (!accessId) {
throw new Error('主机登录授权配置不能为空');
}
const connectConf = await this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
await sshClient.uploadFiles({
connectConf,
transports: [
{
localPath: saveCrtPath,
remotePath: crtPath,
},
{
localPath: saveKeyPath,
remotePath: keyPath,
},
],
});
this.logger.info('证书上传成功:crtPath=', crtPath, ',keyPath=', keyPath);
}
//删除临时文件
fs.unlinkSync(saveCrtPath);