npmci/ts/mod_ssh/index.ts

65 lines
1.8 KiB
TypeScript
Raw Normal View History

import { logger } from '../npmci.logging.js';
import * as plugins from './mod.plugins.js';
2018-04-04 20:25:13 +00:00
let sshInstance: plugins.smartssh.SshInstance;
2016-06-24 00:54:55 +00:00
2021-10-19 01:09:50 +00:00
export let handleCli = async (argvArg: any) => {
2017-08-27 13:24:17 +00:00
if (argvArg._.length >= 2) {
2018-11-24 14:00:19 +00:00
const action: string = argvArg._[1];
2017-08-27 13:24:17 +00:00
switch (action) {
case 'prepare':
2018-04-04 20:25:13 +00:00
await prepare();
break;
2017-08-27 13:24:17 +00:00
default:
2018-11-24 14:00:19 +00:00
logger.log('error', `action >>${action}<< not supported`);
2018-04-04 20:25:13 +00:00
process.exit(1);
2017-08-27 13:24:17 +00:00
}
} else {
2018-11-24 14:00:19 +00:00
logger.log('error', `>>npmci ssh ...<< please specify an action!`);
2018-04-04 20:25:13 +00:00
process.exit(1);
2017-08-27 13:24:17 +00:00
}
2018-04-04 20:25:13 +00:00
};
2017-08-27 13:24:17 +00:00
2017-05-18 20:40:09 +00:00
/**
* checks if not undefined
*/
2018-11-24 14:00:19 +00:00
const notUndefined = (stringArg: string) => {
2018-04-04 20:25:13 +00:00
return stringArg && stringArg !== 'undefined' && stringArg !== '##';
};
2017-05-18 20:40:09 +00:00
/**
* checks for ENV vars in form of NPMCI_SSHKEY_* and deploys any found ones
*/
2017-08-27 13:24:17 +00:00
export let prepare = async () => {
2018-04-04 20:25:13 +00:00
sshInstance = new plugins.smartssh.SshInstance(); // init ssh instance
2023-07-12 13:35:38 +00:00
plugins.smartobject.forEachMinimatch(process.env, 'NPMCI_SSHKEY_*', evaluateSshEnv);
2017-03-08 13:50:41 +00:00
if (!process.env.NPMTS_TEST) {
2018-04-04 20:25:13 +00:00
sshInstance.writeToDisk();
2017-03-08 13:50:41 +00:00
} else {
2018-11-24 14:00:19 +00:00
logger.log('info', 'In test mode, so not storing SSH keys to disk!');
2017-06-15 13:46:08 +00:00
}
2018-04-04 20:25:13 +00:00
};
2016-06-23 20:22:03 +00:00
/**
2017-06-15 13:46:08 +00:00
* gets called for each found SSH ENV Var and deploys it
*/
2018-11-24 14:00:19 +00:00
const evaluateSshEnv = async (sshkeyEnvVarArg: string) => {
const sshEnvArray = sshkeyEnvVarArg.split('|');
const sshKey = new plugins.smartssh.SshKey();
logger.log('info', 'Found SSH identity for ' + sshEnvArray[1]);
2017-08-27 13:24:17 +00:00
if (notUndefined(sshEnvArray[0])) {
2018-11-24 14:00:19 +00:00
logger.log('info', '---> host defined!');
2018-04-04 20:25:13 +00:00
sshKey.host = sshEnvArray[0];
2017-03-08 13:50:41 +00:00
}
2017-08-27 13:24:17 +00:00
if (notUndefined(sshEnvArray[1])) {
2018-11-24 14:00:19 +00:00
logger.log('info', '---> privKey defined!');
2018-04-04 20:25:13 +00:00
sshKey.privKeyBase64 = sshEnvArray[1];
2017-06-15 13:46:08 +00:00
}
2017-08-27 13:24:17 +00:00
if (notUndefined(sshEnvArray[2])) {
2018-11-24 14:00:19 +00:00
logger.log('info', '---> pubKey defined!');
2018-04-04 20:25:13 +00:00
sshKey.pubKeyBase64 = sshEnvArray[2];
2017-06-15 13:46:08 +00:00
}
2016-11-24 22:21:40 +00:00
2018-04-04 20:25:13 +00:00
sshInstance.addKey(sshKey);
return;
};