2024-05-27 18:38:41 +08:00
|
|
|
|
import {
|
|
|
|
|
|
AbstractTaskPlugin,
|
|
|
|
|
|
IAccessService,
|
|
|
|
|
|
ILogger,
|
|
|
|
|
|
IsTaskPlugin,
|
|
|
|
|
|
RunStrategy,
|
|
|
|
|
|
TaskInput,
|
|
|
|
|
|
TaskOutput,
|
|
|
|
|
|
} from '@certd/pipeline';
|
|
|
|
|
|
import { SshClient } from '../../lib/ssh';
|
|
|
|
|
|
import { CertInfo, CertReader } from '@certd/plugin-cert';
|
|
|
|
|
|
import * as fs from 'fs';
|
2022-11-07 23:31:20 +08:00
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@IsTaskPlugin({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
name: 'uploadCertToHost',
|
|
|
|
|
|
title: '上传证书到主机',
|
2024-05-30 10:54:18 +08:00
|
|
|
|
desc: '也支持复制证书到本机',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
default: {
|
|
|
|
|
|
strategy: {
|
|
|
|
|
|
runStrategy: RunStrategy.SkipWhenSucceed,
|
2022-11-07 23:31:20 +08:00
|
|
|
|
},
|
2023-01-11 20:39:48 +08:00
|
|
|
|
},
|
|
|
|
|
|
})
|
2023-05-24 15:41:35 +08:00
|
|
|
|
export class UploadCertToHostPlugin extends AbstractTaskPlugin {
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '证书保存路径',
|
|
|
|
|
|
helper: '需要有写入权限,路径要包含证书文件名',
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: '/root/deploy/nginx/cert.crt',
|
|
|
|
|
|
},
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
crtPath!: string;
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '私钥保存路径',
|
|
|
|
|
|
helper: '需要有写入权限,路径要包含证书文件名',
|
|
|
|
|
|
component: {
|
|
|
|
|
|
placeholder: '/root/deploy/nginx/cert.crt',
|
|
|
|
|
|
},
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
keyPath!: string;
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '域名证书',
|
|
|
|
|
|
helper: '请选择前置任务输出的域名证书',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
component: {
|
2024-05-27 18:38:41 +08:00
|
|
|
|
name: 'pi-output-selector',
|
2022-11-07 23:31:20 +08:00
|
|
|
|
},
|
2023-01-11 20:39:48 +08:00
|
|
|
|
required: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
cert!: CertInfo;
|
|
|
|
|
|
@TaskInput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '主机登录配置',
|
|
|
|
|
|
helper: 'access授权',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
component: {
|
2024-05-27 18:38:41 +08:00
|
|
|
|
name: 'pi-access-selector',
|
|
|
|
|
|
type: 'ssh',
|
2022-11-07 23:31:20 +08:00
|
|
|
|
},
|
2024-05-30 10:54:18 +08:00
|
|
|
|
rules: [{ required: false, message: '' }],
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
accessId!: string;
|
|
|
|
|
|
|
2024-05-30 10:54:18 +08:00
|
|
|
|
@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;
|
|
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
@TaskOutput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '证书保存路径',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
hostCrtPath!: string;
|
|
|
|
|
|
|
|
|
|
|
|
@TaskOutput({
|
2024-05-27 18:38:41 +08:00
|
|
|
|
title: '私钥保存路径',
|
2023-01-11 20:39:48 +08:00
|
|
|
|
})
|
|
|
|
|
|
hostKeyPath!: string;
|
|
|
|
|
|
|
2023-06-25 23:25:56 +08:00
|
|
|
|
accessService!: IAccessService;
|
|
|
|
|
|
logger!: ILogger;
|
|
|
|
|
|
|
|
|
|
|
|
async onInstance() {
|
|
|
|
|
|
this.accessService = this.ctx.accessService;
|
|
|
|
|
|
this.logger = this.ctx.logger;
|
|
|
|
|
|
}
|
2024-05-30 10:54:18 +08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2023-01-11 20:39:48 +08:00
|
|
|
|
async execute(): Promise<void> {
|
2024-04-08 10:05:11 +08:00
|
|
|
|
const { crtPath, keyPath, cert, accessId } = this;
|
2023-05-23 18:01:20 +08:00
|
|
|
|
const certReader = new CertReader(cert);
|
2023-01-11 20:39:48 +08:00
|
|
|
|
|
2024-05-27 18:38:41 +08:00
|
|
|
|
const saveCrtPath = certReader.saveToFile('crt');
|
|
|
|
|
|
const saveKeyPath = certReader.saveToFile('key');
|
2023-01-11 20:39:48 +08:00
|
|
|
|
|
2024-05-30 10:54:18 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2022-11-07 23:31:20 +08:00
|
|
|
|
|
2023-01-11 20:39:48 +08:00
|
|
|
|
//删除临时文件
|
|
|
|
|
|
fs.unlinkSync(saveCrtPath);
|
|
|
|
|
|
fs.unlinkSync(saveKeyPath);
|
|
|
|
|
|
|
|
|
|
|
|
//输出
|
|
|
|
|
|
this.hostCrtPath = crtPath;
|
|
|
|
|
|
this.hostKeyPath = keyPath;
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-05-09 09:56:31 +08:00
|
|
|
|
|
|
|
|
|
|
new UploadCertToHostPlugin();
|