增加ssh key操作service

This commit is contained in:
whyour
2022-05-08 22:41:56 +08:00
parent ae40947801
commit 732d63d588
8 changed files with 174 additions and 51 deletions
+6 -6
View File
@@ -39,7 +39,7 @@ export default class ScheduleService {
{ maxBuffer: this.maxBuffer },
async (error, stdout, stderr) => {
if (error) {
await this.logger.info(
await this.logger.error(
'执行任务%s失败,时间:%s, 错误信息:%j',
command,
new Date().toLocaleString(),
@@ -48,7 +48,7 @@ export default class ScheduleService {
}
if (stderr) {
await this.logger.info(
await this.logger.error(
'执行任务%s失败,时间:%s, 错误信息:%j',
command,
new Date().toLocaleString(),
@@ -58,7 +58,7 @@ export default class ScheduleService {
},
);
} catch (error) {
await this.logger.info(
await this.logger.error(
'执行任务%s失败,时间:%s, 错误信息:%j',
command,
new Date().toLocaleString(),
@@ -97,7 +97,7 @@ export default class ScheduleService {
{ maxBuffer: this.maxBuffer },
async (error, stdout, stderr) => {
if (error) {
await this.logger.info(
await this.logger.error(
'执行任务%s失败,时间:%s, 错误信息:%j',
command,
new Date().toLocaleString(),
@@ -106,7 +106,7 @@ export default class ScheduleService {
}
if (stderr) {
await this.logger.info(
await this.logger.error(
'执行任务%s失败,时间:%s, 错误信息:%j',
command,
new Date().toLocaleString(),
@@ -122,7 +122,7 @@ export default class ScheduleService {
});
},
(err) => {
this.logger.info(
this.logger.error(
'执行任务%s失败,时间:%s, 错误信息:%j',
command,
new Date().toLocaleString(),
+67
View File
@@ -0,0 +1,67 @@
import { Service, Inject } from 'typedi';
import winston from 'winston';
import fs from 'fs';
import os from 'os';
import path from 'path';
@Service()
export default class SshKeyService {
private homedir = os.homedir();
private sshPath = path.resolve(this.homedir, '.ssh');
private sshConfigFilePath = path.resolve(this.homedir, '.ssh/config');
constructor(@Inject('logger') private logger: winston.Logger) {}
private generatePrivateKeyFile(alias: string, key: string): void {
try {
fs.writeFileSync(`${this.sshPath}/${alias}`, key, { encoding: 'utf8' });
} catch (error) {
this.logger.error('生成私钥文件失败', error);
}
}
private removePrivateKeyFile(alias: string): void {
try {
fs.unlinkSync(`${this.sshPath}/${alias}`);
} catch (error) {
this.logger.error('删除私钥文件失败', error);
}
}
private generateSingleSshConfig(alias: string, host: string): string {
return `\nHost ${alias}\n Hostname ${host}\n IdentityFile=${this.sshPath}/${alias}`;
}
private generateSshConfig(configs: string[]) {
try {
for (const config of configs) {
fs.appendFileSync(this.sshConfigFilePath, config, { encoding: 'utf8' });
}
} catch (error) {
this.logger.error('写入ssh配置文件失败', error);
}
}
private removeSshConfig(config: string) {
try {
fs.readFileSync(this.sshConfigFilePath, { encoding: 'utf8' }).replace(
config,
'',
);
} catch (error) {
this.logger.error(`删除ssh配置文件${config}失败`, error);
}
}
public addSSHKey(key: string, alias: string, host: string): void {
this.generatePrivateKeyFile(alias, key);
const config = this.generateSingleSshConfig(alias, host);
this.generateSshConfig([config]);
}
public removeSSHKey(alias: string, host: string): void {
this.removePrivateKeyFile(alias);
const config = this.generateSingleSshConfig(alias, host);
this.removeSshConfig(config);
}
}