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