npmci/ts/mod_docker/index.ts

172 lines
4.5 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
2019-06-19 08:41:58 +00:00
// config
import { configObject } from '../npmci.config';
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
*/
2019-06-19 09:35:19 +00:00
export const handleCli = async argvArg => {
2018-04-04 20:25:13 +00:00
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
*/
2019-06-19 09:35:19 +00:00
export const 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
*/
2019-06-19 09:35:19 +00:00
export const 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
*/
2019-06-19 09:35:19 +00:00
export const prepare = async () => {
2017-08-27 13:24:17 +00:00
// 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
2019-06-19 08:41:58 +00:00
/**
* pushes an image towards a registry
* @param argvArg
*/
export const push = async argvArg => {
2018-04-04 20:25:13 +00:00
await prepare();
2019-06-19 08:41:58 +00:00
let dockerRegistryUrls: string[] = [];
// lets parse the input of cli and npmextra
if (argvArg._.length >= 3 && argvArg._[2] !== 'npmextra') {
dockerRegistryUrls.push(argvArg._[2]);
2019-06-19 09:35:19 +00:00
} else {
if (configObject.dockerRegistries.length === 0) {
2019-06-19 09:36:04 +00:00
logger.log('warn', `There are no docker registries listed in npmextra.json! This is strange!`);
2019-06-19 09:35:19 +00:00
}
2019-06-19 08:41:58 +00:00
dockerRegistryUrls = dockerRegistryUrls.concat(configObject.dockerRegistries);
}
// lets determine the suffix
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
}
2019-06-19 08:41:58 +00:00
// lets push to the registries
for (const dockerRegistryUrl of dockerRegistryUrls) {
const dockerfileArray = await helpers
.readDockerfiles()
.then(helpers.sortDockerfiles)
.then(helpers.mapDockerfiles);
const dockerRegistryToPushTo = npmciRegistryStorage.getRegistryByUrl(dockerRegistryUrl);
if (!dockerRegistryToPushTo) {
logger.log(
'error',
`Cannot push to registry ${dockerRegistryUrl}, because it was not found in the authenticated registry list.`
);
process.exit(1);
}
for (const dockerfile of dockerfileArray) {
await dockerfile.push(dockerRegistryToPushTo, 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
2019-06-19 09:35:19 +00:00
export const pull = async argvArg => {
2018-04-04 20:25:13 +00:00
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
2019-06-19 09:35:19 +00:00
/**
* tests docker files
*/
export const test = async () => {
2018-04-04 20:25:13 +00:00
await prepare();
return await helpers.readDockerfiles().then(helpers.testDockerfiles);
};