npmci/dist/mod_docker/mod.helpers.js
2017-10-09 18:10:01 +02:00

172 lines
13 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./mod.plugins");
const paths = require("../npmci.paths");
const NpmciConfig = require("../npmci.config");
const mod_classes_dockerfile_1 = require("./mod.classes.dockerfile");
/**
* creates instance of class Dockerfile for all Dockerfiles in cwd
* @returns Promise<Dockerfile[]>
*/
exports.readDockerfiles = () => __awaiter(this, void 0, void 0, function* () {
let fileTree = yield plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*');
// create the Dockerfile array
let readDockerfilesArray = [];
plugins.beautylog.info(`found ${fileTree.length} Dockerfiles:`);
console.log(fileTree);
for (let dockerfilePath of fileTree) {
let myDockerfile = new mod_classes_dockerfile_1.Dockerfile({
filePath: dockerfilePath,
read: true
});
readDockerfilesArray.push(myDockerfile);
}
return readDockerfilesArray;
});
/**
* sorts Dockerfiles into a dependency chain
* @param sortableArrayArg an array of instances of class Dockerfile
* @returns Promise<Dockerfile[]>
*/
exports.sortDockerfiles = (sortableArrayArg) => {
let done = plugins.q.defer();
plugins.beautylog.info('sorting Dockerfiles:');
let sortedArray = [];
let cleanTagsOriginal = exports.cleanTagsArrayFunction(sortableArrayArg, sortedArray);
let sorterFunctionCounter = 0;
let sorterFunction = function () {
sortableArrayArg.forEach((dockerfileArg) => {
let cleanTags = exports.cleanTagsArrayFunction(sortableArrayArg, sortedArray);
if (cleanTags.indexOf(dockerfileArg.baseImage) === -1 && sortedArray.indexOf(dockerfileArg) === -1) {
sortedArray.push(dockerfileArg);
}
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) !== -1) {
dockerfileArg.localBaseImageDependent = true;
}
});
if (sortableArrayArg.length === sortedArray.length) {
let counter = 1;
for (let dockerfile of sortedArray) {
plugins.beautylog.log(`tag ${counter}: -> ${dockerfile.cleanTag}`);
counter++;
}
done.resolve(sortedArray);
}
else if (sorterFunctionCounter < 10) {
sorterFunctionCounter++;
sorterFunction();
}
};
sorterFunction();
return done.promise;
};
/**
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
*/
exports.mapDockerfiles = (sortedArray) => __awaiter(this, void 0, void 0, function* () {
sortedArray.forEach((dockerfileArg) => {
if (dockerfileArg.localBaseImageDependent) {
sortedArray.forEach((dockfile2) => {
if (dockfile2.cleanTag === dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2;
}
});
}
});
return sortedArray;
});
/**
* builds the correspoding real docker image for each Dockerfile class instance
*/
exports.buildDockerfiles = (sortedArrayArg) => __awaiter(this, void 0, void 0, function* () {
for (let dockerfileArg of sortedArrayArg) {
yield dockerfileArg.build();
}
return sortedArrayArg;
});
/**
* tests all Dockerfiles in by calling class Dockerfile.test();
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
*/
exports.testDockerfiles = (sortedArrayArg) => __awaiter(this, void 0, void 0, function* () {
for (let dockerfileArg of sortedArrayArg) {
yield dockerfileArg.test();
}
return sortedArrayArg;
});
/**
* returns a version for a docker file
* @execution SYNC
*/
exports.dockerFileVersion = (dockerfileNameArg) => {
let versionString;
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
let regexResultArray = versionRegex.exec(dockerfileNameArg);
if (regexResultArray && regexResultArray.length === 2) {
versionString = regexResultArray[1];
}
else {
versionString = 'latest';
}
return versionString;
};
/**
* returns the docker base image for a Dockerfile
*/
exports.dockerBaseImage = function (dockerfileContentArg) {
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/;
let regexResultArray = baseImageRegex.exec(dockerfileContentArg);
return regexResultArray[1];
};
/**
* returns the docker tag
*/
exports.getDockerTagString = (registryArg, repoArg, versionArg, suffixArg) => {
// determine wether the repo should be mapped accordingly to the registry
let mappedRepo = NpmciConfig.configObject.dockerRegistryRepoMap[registryArg];
let repo = (() => {
if (mappedRepo) {
return mappedRepo;
}
else {
return repoArg;
}
})();
// determine wether the version contais a suffix
let version = versionArg;
if (suffixArg) {
version = versionArg + '_' + suffixArg;
}
let tagString = `${registryArg}/${repo}:${version}`;
return tagString;
};
exports.getDockerBuildArgs = () => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.info('checking for env vars to be supplied to the docker build');
let buildArgsString = '';
for (let key in NpmciConfig.configObject.dockerBuildargEnvMap) {
let targetValue = process.env[NpmciConfig.configObject.dockerBuildargEnvMap[key]];
buildArgsString = `${buildArgsString} --build-arg ${key}=${targetValue}`;
}
return buildArgsString;
});
/**
*
*/
exports.cleanTagsArrayFunction = function (dockerfileArrayArg, trackingArrayArg) {
let cleanTagsArray = [];
dockerfileArrayArg.forEach(function (dockerfileArg) {
if (trackingArrayArg.indexOf(dockerfileArg) === -1) {
cleanTagsArray.push(dockerfileArg.cleanTag);
}
});
return cleanTagsArray;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi90cy9tb2RfZG9ja2VyL21vZC5oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFBQSx5Q0FBd0M7QUFDeEMsd0NBQXVDO0FBRXZDLCtDQUE4QztBQUc5QyxxRUFBcUQ7QUFFckQ7OztHQUdHO0FBQ1EsUUFBQSxlQUFlLEdBQUcsR0FBZ0MsRUFBRTtJQUM3RCxJQUFJLFFBQVEsR0FBRyxNQUFNLE9BQU8sQ0FBQyxTQUFTLENBQUMsRUF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