| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | import * as plugins from './mod.plugins' | 
					
						
							|  |  |  | import * as paths from '../npmci.paths' | 
					
						
							|  |  |  | import { bash } from '../npmci.bash' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  | import * as helpers from './mod.helpers' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // classes
 | 
					
						
							|  |  |  | import { Dockerfile } from './mod.classes.dockerfile' | 
					
						
							|  |  |  | import { DockerRegistry } from './mod.classes.dockerregistry' | 
					
						
							|  |  |  | import { RegistryStorage } from './mod.classes.registrystorage' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // instances
 | 
					
						
							|  |  |  | let npmciRegistryStorage = new RegistryStorage() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export { | 
					
						
							|  |  |  |   Dockerfile, | 
					
						
							|  |  |  |   helpers | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | export let modArgvArg // will be set through the build command
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * handle cli input | 
					
						
							|  |  |  |  * @param argvArg | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export let handleCli = async (argvArg) => { | 
					
						
							|  |  |  |   modArgvArg = argvArg | 
					
						
							|  |  |  |   if (argvArg._.length >= 2) { | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     let action: string = argvArg._[ 1 ] | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |     switch (action) { | 
					
						
							|  |  |  |       case 'build': | 
					
						
							|  |  |  |         await build() | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |       case 'login': | 
					
						
							|  |  |  |         await login() | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |         break | 
					
						
							|  |  |  |       case 'test': | 
					
						
							|  |  |  |         await test() | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |       case 'push': | 
					
						
							|  |  |  |         await push(argvArg) | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |       case 'pull': | 
					
						
							|  |  |  |         await pull(argvArg) | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |         plugins.beautylog.error(`>>npmci docker ...<< action >>${action}<< not supported`) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |     plugins.beautylog.log(`>>npmci docker ...<< cli arguments invalid... Please read the documentation.`) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * builds a cwd of Dockerfiles by triggering a promisechain | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export let build = async () => { | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |   await prepare() | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   plugins.beautylog.log('now building Dockerfiles...') | 
					
						
							|  |  |  |   await helpers.readDockerfiles() | 
					
						
							|  |  |  |     .then(helpers.sortDockerfiles) | 
					
						
							|  |  |  |     .then(helpers.mapDockerfiles) | 
					
						
							|  |  |  |     .then(helpers.buildDockerfiles) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * login to the DockerRegistries | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export let login = async () => { | 
					
						
							|  |  |  |   await prepare() | 
					
						
							|  |  |  |   await npmciRegistryStorage.loginAll() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * logs in docker | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export let prepare = async () => { | 
					
						
							|  |  |  |   // Always login to GitLab Registry
 | 
					
						
							|  |  |  |   if (!process.env.CI_BUILD_TOKEN || process.env.CI_BUILD_TOKEN === '') { | 
					
						
							|  |  |  |     plugins.beautylog.error('No registry token specified by gitlab!') | 
					
						
							| 
									
										
										
										
											2017-08-28 01:35:21 +02:00
										 |  |  |     process.exit(1) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   npmciRegistryStorage.addRegistry(new DockerRegistry({ | 
					
						
							|  |  |  |     registryUrl: 'registry.gitlab.com', | 
					
						
							|  |  |  |     username: 'gitlab-ci-token', | 
					
						
							|  |  |  |     password: process.env.CI_BUILD_TOKEN | 
					
						
							|  |  |  |   })) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   // handle registries
 | 
					
						
							| 
									
										
										
										
											2017-08-28 01:35:21 +02:00
										 |  |  |   await plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_LOGIN_DOCKER*', async (envString) => { | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     npmciRegistryStorage.addRegistry( | 
					
						
							|  |  |  |       DockerRegistry.fromEnvString(envString) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   return | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  | export let push = async (argvArg) => { | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |   await prepare() | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   let registryUrlArg = argvArg._[ 2 ] | 
					
						
							|  |  |  |   let suffix = null | 
					
						
							|  |  |  |   if (argvArg._.length >= 4) { | 
					
						
							|  |  |  |     suffix = argvArg._[ 3 ] | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   let dockerfileArray = await helpers.readDockerfiles() | 
					
						
							|  |  |  |     .then(helpers.sortDockerfiles) | 
					
						
							|  |  |  |     .then(helpers.mapDockerfiles) | 
					
						
							|  |  |  |   let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg) | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |   if (!localDockerRegistry) { | 
					
						
							|  |  |  |     plugins.beautylog.error(`Cannot push to registry ${registryUrlArg}, because it was not found in the authenticated registry list.`) | 
					
						
							|  |  |  |     process.exit(1) | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   for (let dockerfile of dockerfileArray) { | 
					
						
							| 
									
										
										
										
											2017-08-28 02:36:12 +02:00
										 |  |  |     await dockerfile.push(localDockerRegistry, suffix) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  | export let pull = async (argvArg) => { | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |   await prepare() | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   let registryUrlArg = argvArg._[ 2 ] | 
					
						
							|  |  |  |   let suffix = null | 
					
						
							|  |  |  |   if (argvArg._.length >= 4) { | 
					
						
							|  |  |  |     suffix = argvArg._[ 3 ] | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg) | 
					
						
							|  |  |  |   let dockerfileArray = await helpers.readDockerfiles() | 
					
						
							|  |  |  |     .then(helpers.sortDockerfiles) | 
					
						
							|  |  |  |     .then(helpers.mapDockerfiles) | 
					
						
							|  |  |  |   for (let dockerfile of dockerfileArray) { | 
					
						
							| 
									
										
										
										
											2017-08-28 02:36:23 +02:00
										 |  |  |     await dockerfile.pull(localDockerRegistry, suffix) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  | export let test = async () => { | 
					
						
							| 
									
										
										
										
											2017-08-28 02:24:06 +02:00
										 |  |  |   await prepare() | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   return await helpers.readDockerfiles() | 
					
						
							|  |  |  |     .then(helpers.testDockerfiles) | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | } |