npmci/ts/mod_docker/index.ts

143 lines
3.7 KiB
TypeScript
Raw Normal View History

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