npmci/ts/mod_ssh/index.ts

60 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-06-15 13:46:08 +00:00
import * as plugins from './mod.plugins'
2016-11-24 22:21:40 +00:00
let sshInstance: plugins.smartssh.SshInstance
2016-06-24 00:54:55 +00:00
2017-08-27 13:24:17 +00:00
export let handleCli = async (argvArg) => {
if (argvArg._.length >= 2) {
let action: string = argvArg._[1]
switch (action) {
case 'prepare':
await prepare()
break
default:
plugins.beautylog.error(`action >>${action}<< not supported`)
}
}
}
2017-05-18 20:40:09 +00:00
/**
* checks if not undefined
*/
let notUndefined = (stringArg: string) => {
return (stringArg && stringArg !== 'undefined' && stringArg !== '##')
}
/**
* 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 () => {
2017-03-08 13:50:41 +00:00
sshInstance = new plugins.smartssh.SshInstance() // init ssh instance
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_SSHKEY_*', evaluateSshEnv)
if (!process.env.NPMTS_TEST) {
sshInstance.writeToDisk()
} else {
plugins.beautylog.log('In test mode, so not storing SSH keys to disk!')
2017-06-15 13:46:08 +00:00
}
2016-11-24 22:21:40 +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
*/
2017-08-27 13:24:17 +00:00
let evaluateSshEnv = async (sshkeyEnvVarArg: string) => {
let sshEnvArray = sshkeyEnvVarArg.split('|')
2017-03-08 13:50:41 +00:00
let sshKey = new plugins.smartssh.SshKey()
2017-08-27 13:24:17 +00:00
plugins.beautylog.info('Found SSH identity for ' + sshEnvArray[1])
if (notUndefined(sshEnvArray[0])) {
2017-03-08 13:50:41 +00:00
plugins.beautylog.log('---> host defined!')
2017-08-27 13:24:17 +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])) {
2017-03-08 13:50:41 +00:00
plugins.beautylog.log('---> privKey defined!')
2017-08-27 13:24:17 +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])) {
2017-06-15 13:46:08 +00:00
plugins.beautylog.log('---> pubKey defined!')
2017-08-27 13:24:17 +00:00
sshKey.pubKeyBase64 = sshEnvArray[2]
2017-06-15 13:46:08 +00:00
}
2016-11-24 22:21:40 +00:00
2017-03-08 13:50:41 +00:00
sshInstance.addKey(sshKey)
return
2016-11-24 22:21:40 +00:00
}