feat: midway注解方式编写插件

This commit is contained in:
xiaojunnuo
2023-01-11 20:39:48 +08:00
parent 52522f27e9
commit dcd1023a39
47 changed files with 484 additions and 714 deletions

View File

@@ -15,6 +15,7 @@
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-function": "off",
// "no-unused-expressions": "off",
"max-len": [0, 160, 2, { "ignoreUrls": true }]
}

View File

@@ -15,31 +15,31 @@
"ssh2": "^0.8.9"
},
"devDependencies": {
"log4js": "^6.7.1",
"dayjs": "^1.9.7",
"lodash-es": "^4.17.20",
"@types/lodash": "^4.14.186",
"vue-tsc": "^0.38.9",
"@alicloud/cs20151215": "^3.0.3",
"@alicloud/openapi-client": "^0.4.0",
"@alicloud/pop-core": "^1.7.10",
"@midwayjs/core": "^3.0.0",
"@midwayjs/decorator": "^3.0.0",
"@types/chai": "^4.3.3",
"@types/lodash": "^4.14.186",
"@types/mocha": "^10.0.0",
"@types/node-forge": "^1.3.0",
"@types/ssh2": "^1.11.6",
"@typescript-eslint/eslint-plugin": "^5.38.1",
"@typescript-eslint/parser": "^5.38.1",
"chai": "^4.3.6",
"dayjs": "^1.9.7",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"lodash-es": "^4.17.20",
"log4js": "^6.3.0",
"mocha": "^10.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.4",
"vite": "^3.1.0"
"vite": "^3.1.0",
"vue-tsc": "^0.38.9"
}
}

View File

@@ -1,48 +1,47 @@
import { AbstractAccess, IsAccess } from "@certd/pipeline";
import { AccessInput, IAccess, IsAccess } from "@certd/pipeline";
@IsAccess({
name: "ssh",
title: "主机登录授权",
desc: "",
input: {
host: {
title: "主机地址",
component: {
placeholder: "主机域名或IP地址",
},
required: true,
},
port: {
title: "端口",
value: "22",
component: {
placeholder: "22",
},
rules: [{ required: true, message: "此项必填" }],
},
username: {
title: "用户名",
value: "root",
rules: [{ required: true, message: "此项必填" }],
},
password: {
title: "密码",
component: {
name: "a-input-password",
vModel: "value",
},
helper: "登录密码或密钥必填一项",
},
privateKey: {
title: "密钥",
helper: "密钥或密码必填一项",
},
},
input: {},
})
export class SshAccess extends AbstractAccess {
host = "";
port = 22;
username = "root";
password?: string;
privateKey?: string;
export class SshAccess implements IAccess {
@AccessInput({
title: "主机地址",
component: {
placeholder: "主机域名或IP地址",
},
required: true,
})
host!: string;
@AccessInput({
title: "端口",
value: "22",
component: {
placeholder: "22",
},
rules: [{ required: true, message: "此项必填" }],
})
port!: string;
@AccessInput({
title: "用户名",
value: "root",
rules: [{ required: true, message: "此项必填" }],
})
username!: string;
@AccessInput({
title: "密码",
component: {
name: "a-input-password",
vModel: "value",
},
helper: "登录密码或密钥必填一项",
})
password!: string;
@AccessInput({
title: "密钥",
helper: "密钥或密码必填一项",
})
privateKey!: string;
}

View File

@@ -1,10 +1,11 @@
// @ts-ignore
import ssh2 from "ssh2";
import path from "path";
import _ from "lodash";
import { Logger } from "log4js";
import { ILogger } from "@certd/pipeline";
export class SshClient {
logger: Logger;
constructor(logger: Logger) {
logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
/**

View File

@@ -1,47 +1,56 @@
import { IsTask, TaskInput, TaskOutput, TaskPlugin, AbstractPlugin, RunStrategy } from "@certd/pipeline";
import { Autowire, IAccessService, IsTaskPlugin, ILogger, RunStrategy, TaskInput, ITaskPlugin } from "@certd/pipeline";
import { SshClient } from "../../lib/ssh";
import { CertInfo } from "@certd/plugin-cert";
@IsTask(() => {
return {
name: "hostShellExecute",
title: "执行远程主机脚本命令",
input: {
accessId: {
title: "主机登录配置",
helper: "登录",
component: {
name: "pi-access-selector",
type: "ssh",
},
required: true,
},
cert: {
title: "域名证书",
helper: "请选择前置任务输出的域名证书",
component: {
name: "pi-output-selector",
},
required: true,
},
script: {
title: "shell脚本命令",
component: {
name: "a-textarea",
vModel: "value",
},
},
@IsTaskPlugin({
name: "hostShellExecute",
title: "执行远程主机脚本命令",
input: {},
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
},
output: {},
};
},
output: {},
})
export class HostShellExecutePlugin extends AbstractPlugin implements TaskPlugin {
async execute(input: TaskInput): Promise<TaskOutput> {
const { script, accessId } = input;
export class HostShellExecutePlugin implements ITaskPlugin {
@TaskInput({
title: "主机登录配置",
helper: "登录",
component: {
name: "pi-access-selector",
type: "ssh",
},
required: true,
})
accessId!: string;
@TaskInput({
title: "域名证书",
helper: "请选择前置任务输出的域名证书",
component: {
name: "pi-output-selector",
},
required: true,
})
cert!: CertInfo;
@TaskInput({
title: "shell脚本命令",
component: {
name: "a-textarea",
vModel: "value",
},
})
script!: string;
@Autowire()
accessService!: IAccessService;
@Autowire()
logger!: ILogger;
// eslint-disable-next-line @typescript-eslint/no-empty-function
async onInit() {}
async execute(): Promise<void> {
const { script, accessId } = this;
const connectConf = this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
const ret = await sshClient.exec({
@@ -49,6 +58,5 @@ export class HostShellExecutePlugin extends AbstractPlugin implements TaskPlugin
script,
});
this.logger.info("exec res:", ret);
return {};
}
}

View File

@@ -1,71 +1,87 @@
import { IsTask, TaskInput, TaskOutput, TaskPlugin, AbstractPlugin, RunStrategy } from "@certd/pipeline";
import { Autowire, IAccessService, IsTaskPlugin, ITaskPlugin, ILogger, RunStrategy, TaskInput, TaskOutput } from "@certd/pipeline";
import { SshClient } from "../../lib/ssh";
import { CertInfo } from "@certd/plugin-cert";
import * as fs from "fs";
@IsTask(() => {
return {
name: "uploadCertToHost",
title: "上传证书到主机",
input: {
crtPath: {
title: "证书保存路径",
},
keyPath: {
title: "私钥保存路径",
},
cert: {
title: "域名证书",
helper: "请选择前置任务输出的域名证书",
component: {
name: "pi-output-selector",
},
required: true,
},
accessId: {
title: "主机登录配置",
helper: "access授权",
component: {
name: "pi-access-selector",
type: "ssh",
},
rules: [{ required: true, message: "此项必填" }],
},
sudo: {
title: "是否sudo",
component: {
name: "a-checkbox",
vModel: "checked",
},
},
@IsTaskPlugin({
name: "uploadCertToHost",
title: "上传证书到主机",
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
},
output: {
hostCrtPath: {
title: "上传成功后的证书路径",
},
hostKeyPath: {
title: "上传成功后的私钥路径",
},
},
};
},
})
export class UploadCertToHostPlugin extends AbstractPlugin implements TaskPlugin {
async execute(input: TaskInput): Promise<TaskOutput> {
const { crtPath, keyPath, cert, accessId, sudo } = input;
export class UploadCertToHostPlugin implements ITaskPlugin {
@TaskInput({
title: "证书保存路径",
})
crtPath!: string;
@TaskInput({
title: "私钥保存路径",
})
keyPath!: string;
@TaskInput({
title: "域名证书",
helper: "请选择前置任务输出的域名证书",
component: {
name: "pi-output-selector",
},
required: true,
})
cert!: CertInfo;
@TaskInput({
title: "主机登录配置",
helper: "access授权",
component: {
name: "pi-access-selector",
type: "ssh",
},
rules: [{ required: true, message: "此项必填" }],
})
accessId!: string;
@TaskInput({
title: "是否sudo",
component: {
name: "a-checkbox",
vModel: "checked",
},
})
sudo!: boolean;
@Autowire()
accessService!: IAccessService;
@Autowire()
logger!: ILogger;
@TaskOutput({
title: "证书保存路径",
})
hostCrtPath!: string;
@TaskOutput({
title: "私钥保存路径",
})
hostKeyPath!: string;
async onInit() {}
async execute(): Promise<void> {
const { crtPath, keyPath, cert, accessId, sudo } = this;
const connectConf = this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
const saveCrtPath = cert.saveToFile("crt");
const saveKeyPath = cert.saveToFile("key");
await sshClient.uploadFiles({
connectConf,
transports: [
{
localPath: cert.crtPath,
localPath: saveCrtPath,
remotePath: crtPath,
},
{
localPath: cert.keyPath,
localPath: saveKeyPath,
remotePath: keyPath,
},
],
@@ -73,9 +89,12 @@ export class UploadCertToHostPlugin extends AbstractPlugin implements TaskPlugin
});
this.logger.info("证书上传成功crtPath=", crtPath, ",keyPath=", keyPath);
return {
hostCrtPath: crtPath,
hostKeyPath: keyPath,
};
//删除临时文件
fs.unlinkSync(saveCrtPath);
fs.unlinkSync(saveKeyPath);
//输出
this.hostCrtPath = crtPath;
this.hostKeyPath = keyPath;
}
}