| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  | import * as plugins from './npmci.plugins' | 
					
						
							|  |  |  | import {bash} from './npmci.bash' | 
					
						
							|  |  |  | import * as env from './npmci.env' | 
					
						
							|  |  |  | import * as sshModule from './npmci.ssh' | 
					
						
							| 
									
										
										
										
											2016-06-01 05:42:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:05:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  | // types
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:05:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * defines possible prepare services | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  | export type TPrepService = 'npm' | 'docker' | 'docker-gitlab' | 'ssh'; | 
					
						
							| 
									
										
										
										
											2016-09-04 16:05:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * authenticates npm with token from env var | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-03 01:58:37 +02:00
										 |  |  | let npm = function(){ | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |     let done = plugins.q.defer() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let npmrcPrefix: string = '//registry.npmjs.org/:_authToken=' | 
					
						
							|  |  |  |     let npmToken: string = process.env.NPMCI_TOKEN_NPM | 
					
						
							|  |  |  |     let npmrcFileString = npmrcPrefix + npmToken | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (npmToken) { | 
					
						
							|  |  |  |         plugins.beautylog.info('found access token') | 
					
						
							| 
									
										
										
										
											2016-06-03 01:58:37 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |         plugins.beautylog.error('no access token found! Exiting!') | 
					
						
							|  |  |  |         process.exit(1) | 
					
						
							| 
									
										
										
										
											2016-06-03 01:58:37 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |     plugins.smartfile.memory.toFsSync(npmrcFileString,'/root/.npmrc') | 
					
						
							|  |  |  |     done.resolve() | 
					
						
							|  |  |  |     return done.promise | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-03 01:58:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:05:47 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * logs in docker | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-01 05:42:37 +02:00
										 |  |  | let docker = function(){ | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |     let done = plugins.q.defer() | 
					
						
							|  |  |  |     env.setDockerRegistry('docker.io') | 
					
						
							| 
									
										
										
										
											2016-06-02 13:08:15 +02:00
										 |  |  |     let dockerRegex = /^([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)/ | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |     if (!process.env.NPMCI_LOGIN_DOCKER) { | 
					
						
							|  |  |  |         plugins.beautylog.error('You have to specify Login Data to the Docker Registry') | 
					
						
							|  |  |  |         process.exit(1) | 
					
						
							| 
									
										
										
										
											2016-06-02 17:57:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |     plugins.shelljs.exec('docker login -u gitlab-ci-token -p ' + process.env.CI_BUILD_TOKEN + ' ' + 'registry.gitlab.com') // Always also login to GitLab Registry
 | 
					
						
							|  |  |  |     let dockerRegexResultArray = dockerRegex.exec(process.env.NPMCI_LOGIN_DOCKER) | 
					
						
							|  |  |  |     let username = dockerRegexResultArray[1] | 
					
						
							|  |  |  |     let password = dockerRegexResultArray[2] | 
					
						
							|  |  |  |     plugins.shelljs.exec('docker login -u ' + username + ' -p ' + password) | 
					
						
							|  |  |  |     done.resolve() | 
					
						
							|  |  |  |     return done.promise | 
					
						
							| 
									
										
										
										
											2016-06-01 05:42:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:05:47 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * prepare docker for gitlab registry | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-03 01:58:37 +02:00
										 |  |  | let dockerGitlab = function(){ | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |     let done = plugins.q.defer() | 
					
						
							|  |  |  |     env.setDockerRegistry('registry.gitlab.com') | 
					
						
							|  |  |  |     plugins.shelljs.exec('docker login -u gitlab-ci-token -p ' + process.env.CI_BUILD_TOKEN + ' ' + 'registry.gitlab.com') | 
					
						
							|  |  |  |     done.resolve() | 
					
						
							|  |  |  |     return done.promise | 
					
						
							| 
									
										
										
										
											2016-06-03 01:58:37 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-01 05:42:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:05:47 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * prepare ssh | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-25 16:29:06 +02:00
										 |  |  | let ssh = function(){ | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |     let done = plugins.q.defer() | 
					
						
							| 
									
										
										
										
											2016-06-25 16:29:06 +02:00
										 |  |  |     sshModule.ssh() | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |         .then(done.resolve) | 
					
						
							|  |  |  |     return done.promise | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 16:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-04 16:05:47 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * the main exported prepare function | 
					
						
							|  |  |  |  * @param servieArg describes the service to prepare | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  | export let prepare = function(serviceArg: TPrepService){ | 
					
						
							| 
									
										
										
										
											2016-06-01 05:42:37 +02:00
										 |  |  |     switch (serviceArg) { | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |         case 'npm': | 
					
						
							|  |  |  |             return npm() | 
					
						
							|  |  |  |         case 'docker': | 
					
						
							|  |  |  |             return docker() | 
					
						
							|  |  |  |         case 'docker-gitlab': | 
					
						
							|  |  |  |             return dockerGitlab() | 
					
						
							|  |  |  |         case 'ssh': | 
					
						
							|  |  |  |             return ssh() | 
					
						
							| 
									
										
										
										
											2016-06-03 01:58:37 +02:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  |             break | 
					
						
							| 
									
										
										
										
											2016-06-01 05:42:37 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-24 23:21:40 +01:00
										 |  |  | } |