npmci/ts/manager.docker/index.ts

191 lines
5.3 KiB
TypeScript
Raw Permalink Normal View History

import { logger } from '../npmci.logging.js';
import * as plugins from './mod.plugins.js';
import * as paths from '../npmci.paths.js';
import { bash } from '../npmci.bash.js';
2019-08-29 18:26:23 +00:00
// classes
import { Npmci } from '../npmci.classes.npmci.js';
import { Dockerfile } from './mod.classes.dockerfile.js';
import { DockerRegistry } from './mod.classes.dockerregistry.js';
import { RegistryStorage } from './mod.classes.registrystorage.js';
2019-08-29 18:26:23 +00:00
export class NpmciDockerManager {
public npmciRef: Npmci;
public npmciRegistryStorage = new RegistryStorage();
constructor(npmciArg: Npmci) {
this.npmciRef = npmciArg;
}
/**
* handle cli input
* @param argvArg
*/
2021-10-19 01:09:50 +00:00
public handleCli = async (argvArg: any) => {
2019-08-29 18:26:23 +00:00
if (argvArg._.length >= 2) {
const action: string = argvArg._[1];
switch (action) {
case 'build':
await this.build();
break;
case 'login':
case 'prepare':
await this.login();
break;
case 'test':
await this.test();
break;
case 'push':
await this.push(argvArg);
break;
case 'pull':
await this.pull(argvArg);
break;
default:
logger.log('error', `>>npmci docker ...<< action >>${action}<< not supported`);
}
} else {
logger.log(
'info',
`>>npmci docker ...<< cli arguments invalid... Please read the documentation.`
);
}
2019-09-01 11:51:11 +00:00
};
2019-08-29 18:26:23 +00:00
/**
* builds a cwd of Dockerfiles by triggering a promisechain
*/
public build = async () => {
await this.prepare();
logger.log('info', 'now building Dockerfiles...');
await Dockerfile.readDockerfiles(this)
.then(Dockerfile.sortDockerfiles)
.then(Dockerfile.mapDockerfiles)
.then(Dockerfile.buildDockerfiles);
2019-09-01 11:51:11 +00:00
};
2019-08-29 18:26:23 +00:00
/**
* login to the DockerRegistries
*/
public login = async () => {
await this.prepare();
await this.npmciRegistryStorage.loginAll();
2019-09-01 11:51:11 +00:00
};
2019-08-29 18:26:23 +00:00
/**
* logs in docker
*/
public prepare = async () => {
// Always login to GitLab Registry
2023-06-25 21:59:25 +00:00
if (process.env.GITLAB_CI) {
console.log('gitlab ci detected');
if (!process.env.CI_JOB_TOKEN || process.env.CI_JOB_TOKEN === '') {
logger.log('error', 'Running in Gitlab CI, but no registry token specified by gitlab!');
process.exit(1);
}
this.npmciRegistryStorage.addRegistry(
new DockerRegistry({
registryUrl: 'registry.gitlab.com',
username: 'gitlab-ci-token',
password: process.env.CI_JOB_TOKEN,
})
);
2019-08-29 18:26:23 +00:00
}
// handle registries
2023-07-12 13:35:38 +00:00
await plugins.smartobject.forEachMinimatch(
2019-08-29 18:26:23 +00:00
process.env,
'NPMCI_LOGIN_DOCKER*',
2021-10-19 01:09:50 +00:00
async (envString: string) => {
2019-08-29 18:26:23 +00:00
this.npmciRegistryStorage.addRegistry(DockerRegistry.fromEnvString(envString));
}
);
return;
2019-09-01 11:51:11 +00:00
};
2019-08-29 18:26:23 +00:00
/**
* pushes an image towards a registry
* @param argvArg
*/
2021-10-19 01:09:50 +00:00
public push = async (argvArg: any) => {
2019-08-29 18:26:23 +00:00
await this.prepare();
let dockerRegistryUrls: string[] = [];
// lets parse the input of cli and npmextra
if (argvArg._.length >= 3 && argvArg._[2] !== 'npmextra') {
dockerRegistryUrls.push(argvArg._[2]);
} else {
if (this.npmciRef.npmciConfig.getConfig().dockerRegistries.length === 0) {
logger.log(
'warn',
`There are no docker registries listed in npmextra.json! This is strange!`
);
}
dockerRegistryUrls = dockerRegistryUrls.concat(
this.npmciRef.npmciConfig.getConfig().dockerRegistries
);
}
// lets determine the suffix
let suffix = null;
if (argvArg._.length >= 4) {
suffix = argvArg._[3];
}
// lets push to the registries
for (const dockerRegistryUrl of dockerRegistryUrls) {
const dockerfileArray = await Dockerfile.readDockerfiles(this)
.then(Dockerfile.sortDockerfiles)
.then(Dockerfile.mapDockerfiles);
2021-11-07 03:20:14 +00:00
const dockerRegistryToPushTo = await this.npmciRegistryStorage.getRegistryByUrl(
dockerRegistryUrl
);
2019-08-29 18:26:23 +00:00
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);
}
}
2019-09-01 11:51:11 +00:00
};
2019-08-29 18:26:23 +00:00
/**
* pulls an image
*/
2021-10-19 01:09:50 +00:00
public pull = async (argvArg: any) => {
2019-08-29 18:26:23 +00:00
await this.prepare();
const registryUrlArg = argvArg._[2];
let suffix = null;
if (argvArg._.length >= 4) {
suffix = argvArg._[3];
}
2021-10-19 01:09:50 +00:00
const localDockerRegistry = await this.npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
2019-08-29 18:26:23 +00:00
const dockerfileArray = await Dockerfile.readDockerfiles(this)
.then(Dockerfile.sortDockerfiles)
.then(Dockerfile.mapDockerfiles);
for (const dockerfile of dockerfileArray) {
await dockerfile.pull(localDockerRegistry, suffix);
}
2019-09-01 11:51:11 +00:00
};
2019-08-29 18:26:23 +00:00
/**
* tests docker files
*/
public test = async () => {
await this.prepare();
return await Dockerfile.readDockerfiles(this).then(Dockerfile.testDockerfiles);
2019-09-01 11:51:11 +00:00
};
2023-07-01 20:05:43 +00:00
/**
* can be used to get the Dockerfiles in the directory
*/
getDockerfiles = async () => {
const dockerfiles = await Dockerfile.readDockerfiles(this);
return dockerfiles;
}
2019-08-29 18:26:23 +00:00
}