feat(logging): use smartlog

This commit is contained in:
2018-11-24 15:00:19 +01:00
parent 8604c63d37
commit 0079addfc5
20 changed files with 594 additions and 300 deletions

View File

@ -1,3 +1,4 @@
import { logger } from '../npmci.logging';
import * as plugins from './mod.plugins';
import * as paths from '../npmci.paths';
import { bash } from '../npmci.bash';
@ -10,7 +11,7 @@ import { DockerRegistry } from './mod.classes.dockerregistry';
import { RegistryStorage } from './mod.classes.registrystorage';
// instances
let npmciRegistryStorage = new RegistryStorage();
const npmciRegistryStorage = new RegistryStorage();
export { Dockerfile, helpers };
@ -23,7 +24,7 @@ export let modArgvArg; // will be set through the build command
export let handleCli = async argvArg => {
modArgvArg = argvArg;
if (argvArg._.length >= 2) {
let action: string = argvArg._[1];
const action: string = argvArg._[1];
switch (action) {
case 'build':
await build();
@ -42,10 +43,11 @@ export let handleCli = async argvArg => {
await pull(argvArg);
break;
default:
plugins.beautylog.error(`>>npmci docker ...<< action >>${action}<< not supported`);
logger.log('error', `>>npmci docker ...<< action >>${action}<< not supported`);
}
} else {
plugins.beautylog.log(
logger.log(
'info',
`>>npmci docker ...<< cli arguments invalid... Please read the documentation.`
);
}
@ -56,7 +58,7 @@ export let handleCli = async argvArg => {
*/
export let build = async () => {
await prepare();
plugins.beautylog.log('now building Dockerfiles...');
logger.log('info', 'now building Dockerfiles...');
await helpers
.readDockerfiles()
.then(helpers.sortDockerfiles)
@ -78,7 +80,7 @@ export let login = async () => {
export let prepare = async () => {
// Always login to GitLab Registry
if (!process.env.CI_BUILD_TOKEN || process.env.CI_BUILD_TOKEN === '') {
plugins.beautylog.error('No registry token specified by gitlab!');
logger.log('error', 'No registry token specified by gitlab!');
process.exit(1);
}
npmciRegistryStorage.addRegistry(
@ -98,40 +100,41 @@ export let prepare = async () => {
export let push = async argvArg => {
await prepare();
let registryUrlArg = argvArg._[2];
const registryUrlArg = argvArg._[2];
let suffix = null;
if (argvArg._.length >= 4) {
suffix = argvArg._[3];
}
let dockerfileArray = await helpers
const dockerfileArray = await helpers
.readDockerfiles()
.then(helpers.sortDockerfiles)
.then(helpers.mapDockerfiles);
let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
const localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
if (!localDockerRegistry) {
plugins.beautylog.error(
logger.log(
'error',
`Cannot push to registry ${registryUrlArg}, because it was not found in the authenticated registry list.`
);
process.exit(1);
}
for (let dockerfile of dockerfileArray) {
for (const dockerfile of dockerfileArray) {
await dockerfile.push(localDockerRegistry, suffix);
}
};
export let pull = async argvArg => {
await prepare();
let registryUrlArg = argvArg._[2];
const registryUrlArg = argvArg._[2];
let suffix = null;
if (argvArg._.length >= 4) {
suffix = argvArg._[3];
}
let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
let dockerfileArray = await helpers
const localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
const dockerfileArray = await helpers
.readDockerfiles()
.then(helpers.sortDockerfiles)
.then(helpers.mapDockerfiles);
for (let dockerfile of dockerfileArray) {
for (const dockerfile of dockerfileArray) {
await dockerfile.pull(localDockerRegistry, suffix);
}
};

View File

@ -1,3 +1,4 @@
import { logger } from '../npmci.logging';
import * as plugins from './mod.plugins';
import * as NpmciEnv from '../npmci.env';
import { bash } from '../npmci.bash';
@ -10,16 +11,16 @@ import * as helpers from './mod.helpers';
* class Dockerfile represents a Dockerfile on disk in npmci
*/
export class Dockerfile {
filePath: string;
repo: string;
version: string;
cleanTag: string;
buildTag: string;
containerName: string;
content: string;
baseImage: string;
localBaseImageDependent: boolean;
localBaseDockerfile: Dockerfile;
public filePath: string;
public repo: string;
public version: string;
public cleanTag: string;
public buildTag: string;
public containerName: string;
public content: string;
public baseImage: string;
public localBaseImageDependent: boolean;
public localBaseDockerfile: Dockerfile;
constructor(options: { filePath?: string; fileContents?: string | Buffer; read?: boolean }) {
this.filePath = options.filePath;
this.repo = NpmciEnv.repo.user + '/' + NpmciEnv.repo.repo;
@ -38,10 +39,10 @@ export class Dockerfile {
/**
* builds the Dockerfile
*/
async build() {
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag);
let buildArgsString = await helpers.getDockerBuildArgs();
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
public async build() {
logger.log('info', 'now building Dockerfile for ' + this.cleanTag);
const buildArgsString = await helpers.getDockerBuildArgs();
const buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
await bash(buildCommand);
return;
}
@ -49,8 +50,8 @@ export class Dockerfile {
/**
* pushes the Dockerfile to a registry
*/
async push(dockerRegistryArg: DockerRegistry, versionSuffix: string = null) {
let pushTag = helpers.getDockerTagString(
public async push(dockerRegistryArg: DockerRegistry, versionSuffix: string = null) {
const pushTag = helpers.getDockerTagString(
dockerRegistryArg.registryUrl,
this.repo,
this.version,
@ -63,8 +64,8 @@ export class Dockerfile {
/**
* pulls the Dockerfile from a registry
*/
async pull(registryArg: DockerRegistry, versionSuffixArg: string = null) {
let pullTag = helpers.getDockerTagString(
public async pull(registryArg: DockerRegistry, versionSuffixArg: string = null) {
const pullTag = helpers.getDockerTagString(
registryArg.registryUrl,
this.repo,
this.version,
@ -77,9 +78,9 @@ export class Dockerfile {
/**
* tests the Dockerfile;
*/
async test() {
let testFile: string = plugins.path.join(paths.NpmciTestDir, 'test_' + this.version + '.sh');
let testFileExists: boolean = plugins.smartfile.fs.fileExistsSync(testFile);
public async test() {
const testFile: string = plugins.path.join(paths.NpmciTestDir, 'test_' + this.version + '.sh');
const testFileExists: boolean = plugins.smartfile.fs.fileExistsSync(testFile);
if (testFileExists) {
// run tests
await bash(
@ -93,17 +94,15 @@ export class Dockerfile {
await bash(`docker rm npmci_test_container`);
await bash(`docker rmi --force npmci_test_image`);
} else {
plugins.beautylog.warn(
'skipping tests for ' + this.cleanTag + ' because no testfile was found!'
);
logger.log('warn', 'skipping tests for ' + this.cleanTag + ' because no testfile was found!');
}
}
/**
* gets the id of a Dockerfile
*/
async getId() {
let containerId = await bash('docker inspect --type=image --format="{{.Id}}" ' + this.buildTag);
public async getId() {
const containerId = await bash('docker inspect --type=image --format="{{.Id}}" ' + this.buildTag);
return containerId;
}
}

View File

@ -1,3 +1,4 @@
import { logger } from '../npmci.logging';
import * as plugins from './mod.plugins';
import { bash } from '../npmci.bash';
@ -8,26 +9,26 @@ export interface IDockerRegistryConstructorOptions {
}
export class DockerRegistry {
registryUrl: string;
username: string;
password: string;
public registryUrl: string;
public username: string;
public password: string;
constructor(optionsArg: IDockerRegistryConstructorOptions) {
this.registryUrl = optionsArg.registryUrl;
this.username = optionsArg.username;
this.password = optionsArg.password;
plugins.beautylog.info(`created DockerRegistry for ${this.registryUrl}`);
logger.log('info', `created DockerRegistry for ${this.registryUrl}`);
}
static fromEnvString(envString: string): DockerRegistry {
let dockerRegexResultArray = envString.split('|');
public static fromEnvString(envString: string): DockerRegistry {
const dockerRegexResultArray = envString.split('|');
if (dockerRegexResultArray.length !== 3) {
plugins.beautylog.error('malformed docker env var...');
logger.log('error', 'malformed docker env var...');
process.exit(1);
return;
}
let registryUrl = dockerRegexResultArray[0];
let username = dockerRegexResultArray[1];
let password = dockerRegexResultArray[2];
const registryUrl = dockerRegexResultArray[0];
const username = dockerRegexResultArray[1];
const password = dockerRegexResultArray[2];
return new DockerRegistry({
registryUrl: registryUrl,
username: username,
@ -35,13 +36,13 @@ export class DockerRegistry {
});
}
async login() {
public async login() {
if (this.registryUrl === 'docker.io') {
await bash(`docker login -u ${this.username} -p ${this.password}`);
plugins.beautylog.info('Logged in to standard docker hub');
logger.log('info', 'Logged in to standard docker hub');
} else {
await bash(`docker login -u ${this.username} -p ${this.password} ${this.registryUrl}`);
}
plugins.beautylog.ok(`docker authenticated for ${this.registryUrl}!`);
logger.log('ok', `docker authenticated for ${this.registryUrl}!`);
}
}

View File

@ -1,5 +1,6 @@
import { logger } from '../npmci.logging';
import * as plugins from './mod.plugins';
import { Objectmap } from 'lik';
import { Objectmap } from '@pushrocks/lik';
import { DockerRegistry } from './mod.classes.dockerregistry';
@ -23,6 +24,6 @@ export class RegistryStorage {
await this.objectMap.forEach(async registryArg => {
await registryArg.login();
});
plugins.beautylog.success('logged in successfully into all available DockerRegistries!');
logger.log('success', 'logged in successfully into all available DockerRegistries!');
}
}

View File

@ -1,3 +1,4 @@
import { logger } from '../npmci.logging';
import * as plugins from './mod.plugins';
import * as paths from '../npmci.paths';
import * as NpmciEnv from '../npmci.env';
@ -11,14 +12,14 @@ import { Dockerfile } from './mod.classes.dockerfile';
* @returns Promise<Dockerfile[]>
*/
export let readDockerfiles = async (): Promise<Dockerfile[]> => {
let fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*');
const fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*');
// create the Dockerfile array
let readDockerfilesArray: Dockerfile[] = [];
plugins.beautylog.info(`found ${fileTree.length} Dockerfiles:`);
const readDockerfilesArray: Dockerfile[] = [];
logger.log('info', `found ${fileTree.length} Dockerfiles:`);
console.log(fileTree);
for (let dockerfilePath of fileTree) {
let myDockerfile = new Dockerfile({
for (const dockerfilePath of fileTree) {
const myDockerfile = new Dockerfile({
filePath: dockerfilePath,
read: true
});
@ -34,14 +35,14 @@ export let readDockerfiles = async (): Promise<Dockerfile[]> => {
* @returns Promise<Dockerfile[]>
*/
export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): Promise<Dockerfile[]> => {
let done = plugins.smartpromise.defer<Dockerfile[]>();
plugins.beautylog.info('sorting Dockerfiles:');
let sortedArray: Dockerfile[] = [];
let cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg, sortedArray);
const done = plugins.smartpromise.defer<Dockerfile[]>();
logger.log('info', 'sorting Dockerfiles:');
const sortedArray: Dockerfile[] = [];
const cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg, sortedArray);
let sorterFunctionCounter: number = 0;
let sorterFunction = function() {
const sorterFunction = () => {
sortableArrayArg.forEach(dockerfileArg => {
let cleanTags = cleanTagsArrayFunction(sortableArrayArg, sortedArray);
const cleanTags = cleanTagsArrayFunction(sortableArrayArg, sortedArray);
if (
cleanTags.indexOf(dockerfileArg.baseImage) === -1 &&
sortedArray.indexOf(dockerfileArg) === -1
@ -54,8 +55,8 @@ export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): Promise<Dockerfil
});
if (sortableArrayArg.length === sortedArray.length) {
let counter = 1;
for (let dockerfile of sortedArray) {
plugins.beautylog.log(`tag ${counter}: -> ${dockerfile.cleanTag}`);
for (const dockerfile of sortedArray) {
logger.log('info', `tag ${counter}: -> ${dockerfile.cleanTag}`);
counter++;
}
done.resolve(sortedArray);
@ -88,7 +89,7 @@ export let mapDockerfiles = async (sortedArray: Dockerfile[]): Promise<Dockerfil
* builds the correspoding real docker image for each Dockerfile class instance
*/
export let buildDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
for (let dockerfileArg of sortedArrayArg) {
for (const dockerfileArg of sortedArrayArg) {
await dockerfileArg.build();
}
return sortedArrayArg;
@ -99,7 +100,7 @@ export let buildDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
*/
export let testDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
for (let dockerfileArg of sortedArrayArg) {
for (const dockerfileArg of sortedArrayArg) {
await dockerfileArg.test();
}
return sortedArrayArg;
@ -111,8 +112,8 @@ export let testDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
*/
export let dockerFileVersion = (dockerfileNameArg: string): string => {
let versionString: string;
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
let regexResultArray = versionRegex.exec(dockerfileNameArg);
const versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
const regexResultArray = versionRegex.exec(dockerfileNameArg);
if (regexResultArray && regexResultArray.length === 2) {
versionString = regexResultArray[1];
} else {
@ -124,9 +125,9 @@ export let dockerFileVersion = (dockerfileNameArg: string): string => {
/**
* returns the docker base image for a Dockerfile
*/
export let dockerBaseImage = function(dockerfileContentArg: string) {
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/;
let regexResultArray = baseImageRegex.exec(dockerfileContentArg);
export let dockerBaseImage = (dockerfileContentArg: string) => {
const baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/;
const regexResultArray = baseImageRegex.exec(dockerfileContentArg);
return regexResultArray[1];
};
@ -140,8 +141,8 @@ export let getDockerTagString = (
suffixArg?: string
): string => {
// determine wether the repo should be mapped accordingly to the registry
let mappedRepo = NpmciConfig.configObject.dockerRegistryRepoMap[registryArg];
let repo = (() => {
const mappedRepo = NpmciConfig.configObject.dockerRegistryRepoMap[registryArg];
const repo = (() => {
if (mappedRepo) {
return mappedRepo;
} else {
@ -155,15 +156,15 @@ export let getDockerTagString = (
version = versionArg + '_' + suffixArg;
}
let tagString = `${registryArg}/${repo}:${version}`;
const tagString = `${registryArg}/${repo}:${version}`;
return tagString;
};
export let getDockerBuildArgs = async (): Promise<string> => {
plugins.beautylog.info('checking for env vars to be supplied to the docker build');
logger.log('info', 'checking for env vars to be supplied to the docker build');
let buildArgsString: string = '';
for (let key in NpmciConfig.configObject.dockerBuildargEnvMap) {
let targetValue = process.env[NpmciConfig.configObject.dockerBuildargEnvMap[key]];
for (const key in NpmciConfig.configObject.dockerBuildargEnvMap) {
const targetValue = process.env[NpmciConfig.configObject.dockerBuildargEnvMap[key]];
buildArgsString = `${buildArgsString} --build-arg ${key}=${targetValue}`;
}
return buildArgsString;
@ -172,12 +173,12 @@ export let getDockerBuildArgs = async (): Promise<string> => {
/**
*
*/
export let cleanTagsArrayFunction = function(
export let cleanTagsArrayFunction = (
dockerfileArrayArg: Dockerfile[],
trackingArrayArg: Dockerfile[]
): string[] {
let cleanTagsArray: string[] = [];
dockerfileArrayArg.forEach(function(dockerfileArg) {
): string[] => {
const cleanTagsArray: string[] = [];
dockerfileArrayArg.forEach((dockerfileArg) => {
if (trackingArrayArg.indexOf(dockerfileArg) === -1) {
cleanTagsArray.push(dockerfileArg.cleanTag);
}