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