Files
certd/packages/plugins/plugin-host/src/plugin/upload-to-host/index.ts

105 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-06-25 23:25:56 +08:00
import { AbstractTaskPlugin, IAccessService, ILogger, IsTaskPlugin, RunStrategy, TaskInput, TaskOutput } from "@certd/pipeline";
2022-11-07 23:31:20 +08:00
import { SshClient } from "../../lib/ssh";
import { CertInfo, CertReader } from "@certd/plugin-cert";
2023-01-11 20:39:48 +08:00
import * as fs from "fs";
2022-11-07 23:31:20 +08:00
2023-01-11 20:39:48 +08:00
@IsTaskPlugin({
name: "uploadCertToHost",
title: "上传证书到主机",
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({
title: "证书保存路径",
})
crtPath!: string;
@TaskInput({
title: "私钥保存路径",
})
keyPath!: string;
@TaskInput({
title: "域名证书",
helper: "请选择前置任务输出的域名证书",
component: {
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({
title: "主机登录配置",
helper: "access授权",
component: {
name: "pi-access-selector",
type: "ssh",
2022-11-07 23:31:20 +08:00
},
2023-01-11 20:39:48 +08:00
rules: [{ required: true, message: "此项必填" }],
})
accessId!: string;
@TaskInput({
title: "是否sudo",
component: {
name: "a-checkbox",
vModel: "checked",
},
})
sudo!: boolean;
@TaskOutput({
title: "证书保存路径",
})
hostCrtPath!: string;
@TaskOutput({
title: "私钥保存路径",
})
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;
}
2023-01-11 20:39:48 +08:00
async execute(): Promise<void> {
const { crtPath, keyPath, cert, accessId, sudo } = this;
const certReader = new CertReader(cert);
const connectConf = await this.accessService.getById(accessId);
2022-11-07 23:31:20 +08:00
const sshClient = new SshClient(this.logger);
2023-01-11 20:39:48 +08:00
const saveCrtPath = certReader.saveToFile("crt");
const saveKeyPath = certReader.saveToFile("key");
2023-01-11 20:39:48 +08:00
2022-11-07 23:31:20 +08:00
await sshClient.uploadFiles({
connectConf,
transports: [
{
2023-01-11 20:39:48 +08:00
localPath: saveCrtPath,
2022-11-07 23:31:20 +08:00
remotePath: crtPath,
},
{
2023-01-11 20:39:48 +08:00
localPath: saveKeyPath,
2022-11-07 23:31:20 +08:00
remotePath: keyPath,
},
],
sudo,
});
this.logger.info("证书上传成功crtPath=", crtPath, ",keyPath=", keyPath);
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();