| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  | import * as plugins from './mod.plugins'; | 
					
						
							|  |  |  | import { bash } from '../npmci.bash'; | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface IDockerRegistryConstructorOptions { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |   registryUrl: string; | 
					
						
							|  |  |  |   username: string; | 
					
						
							|  |  |  |   password: string; | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class DockerRegistry { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |   registryUrl: string; | 
					
						
							|  |  |  |   username: string; | 
					
						
							|  |  |  |   password: string; | 
					
						
							|  |  |  |   constructor(optionsArg: IDockerRegistryConstructorOptions) { | 
					
						
							|  |  |  |     this.registryUrl = optionsArg.registryUrl; | 
					
						
							|  |  |  |     this.username = optionsArg.username; | 
					
						
							|  |  |  |     this.password = optionsArg.password; | 
					
						
							|  |  |  |     plugins.beautylog.info(`created DockerRegistry for ${this.registryUrl}`); | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |   static fromEnvString(envString: string): DockerRegistry { | 
					
						
							|  |  |  |     let dockerRegexResultArray = envString.split('|'); | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     if (dockerRegexResultArray.length !== 3) { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |       plugins.beautylog.error('malformed docker env var...'); | 
					
						
							|  |  |  |       process.exit(1); | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     let registryUrl = dockerRegexResultArray[0]; | 
					
						
							|  |  |  |     let username = dockerRegexResultArray[1]; | 
					
						
							|  |  |  |     let password = dockerRegexResultArray[2]; | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     return new DockerRegistry({ | 
					
						
							|  |  |  |       registryUrl: registryUrl, | 
					
						
							|  |  |  |       username: username, | 
					
						
							|  |  |  |       password: password | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |   async login() { | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     if (this.registryUrl === 'docker.io') { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |       await bash(`docker login -u ${this.username} -p ${this.password}`); | 
					
						
							|  |  |  |       plugins.beautylog.info('Logged in to standard docker hub'); | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |       await bash(`docker login -u ${this.username} -p ${this.password} ${this.registryUrl}`); | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     plugins.beautylog.ok(`docker authenticated for ${this.registryUrl}!`); | 
					
						
							| 
									
										
										
										
											2017-08-28 01:03:59 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } |