npmci/ts/mod_docker/index.ts

146 lines
3.7 KiB
TypeScript
Raw Normal View History

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 * 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-11-24 14:00:19 +00:00
const 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-11-24 14:00:19 +00:00
const 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-11-24 14:00:19 +00:00
logger.log('error', `>>npmci docker ...<< action >>${action}<< not supported`);
2017-08-27 13:24:17 +00:00
}
} else {
2018-11-24 14:00:19 +00:00
logger.log(
'info',
2018-04-04 20:25:13 +00:00
`>>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();
2018-11-24 14:00:19 +00:00
logger.log('info', 'now building Dockerfiles...');
2018-04-04 20:25:13 +00:00
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-11-24 14:00:19 +00:00
logger.log('error', 'No registry token specified by gitlab!');
2018-04-04 20:25:13 +00:00
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();
2018-11-24 14:00:19 +00:00
const registryUrlArg = argvArg._[2];
2018-04-04 20:25:13 +00:00
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-11-24 14:00:19 +00:00
const dockerfileArray = await helpers
2018-04-04 20:25:13 +00:00
.readDockerfiles()
2017-08-27 23:03:59 +00:00
.then(helpers.sortDockerfiles)
2018-04-04 20:25:13 +00:00
.then(helpers.mapDockerfiles);
2018-11-24 14:00:19 +00:00
const localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
if (!localDockerRegistry) {
2018-11-24 14:00:19 +00:00
logger.log(
'error',
2018-04-04 20:25:13 +00:00
`Cannot push to registry ${registryUrlArg}, because it was not found in the authenticated registry list.`
);
process.exit(1);
}
2018-11-24 14:00:19 +00:00
for (const 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();
2018-11-24 14:00:19 +00:00
const registryUrlArg = argvArg._[2];
2018-04-04 20:25:13 +00:00
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-11-24 14:00:19 +00:00
const localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
const dockerfileArray = await helpers
2018-04-04 20:25:13 +00:00
.readDockerfiles()
2017-08-27 23:03:59 +00:00
.then(helpers.sortDockerfiles)
2018-04-04 20:25:13 +00:00
.then(helpers.mapDockerfiles);
2018-11-24 14:00:19 +00:00
for (const 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);
};