npmci/dist/npmci.build.docker.js

281 lines
20 KiB
JavaScript
Raw Normal View History

2016-06-04 21:20:39 +00:00
"use strict";
2016-07-18 14:56:53 +00:00
const plugins = require("./npmci.plugins");
const paths = require("./npmci.paths");
const NpmciEnv = require("./npmci.env");
const npmci_bash_1 = require("./npmci.bash");
2016-09-04 15:56:56 +00:00
/**
* builds a cwd of Dockerfiles by triggering a promisechain
*/
2016-06-04 21:20:39 +00:00
exports.build = function () {
2016-07-18 14:56:53 +00:00
let done = plugins.q.defer();
exports.readDockerfiles()
.then(exports.sortDockerfiles)
.then(exports.mapDockerfiles)
2016-06-05 14:43:27 +00:00
.then(exports.buildDockerfiles)
2016-06-05 15:17:15 +00:00
.then(exports.pushDockerfiles)
2016-07-18 14:56:53 +00:00
.then(() => {
2016-06-05 14:43:27 +00:00
done.resolve();
});
2016-06-04 21:20:39 +00:00
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* creates instance of class Dockerfile for all Dockerfiles in cwd
* @returns Promise<Dockerfile[]>
*/
2016-06-05 11:01:45 +00:00
exports.readDockerfiles = function () {
2016-07-18 14:56:53 +00:00
let done = plugins.q.defer();
let readDockerfilesArray = [];
2016-06-05 11:01:45 +00:00
plugins.gulp.src("./Dockerfile*")
.pipe(plugins.through2.obj(function (file, enc, cb) {
2016-07-18 14:56:53 +00:00
let myDockerfile = new Dockerfile({
2016-06-05 02:48:39 +00:00
filePath: file.path,
read: true
});
2016-06-05 11:01:45 +00:00
readDockerfilesArray.push(myDockerfile);
cb(null, file);
2016-06-05 11:01:45 +00:00
}, function () {
done.resolve(readDockerfilesArray);
}));
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* sorts Dockerfiles into a dependency chain
* @param sortableArrayArg an array of instances of class Dockerfile
* @returns Promise<Dockerfile[]>
*/
2016-06-05 11:01:45 +00:00
exports.sortDockerfiles = function (sortableArrayArg) {
2016-07-18 14:56:53 +00:00
let done = plugins.q.defer();
let sortedArray = [];
let cleanTagsOriginal = exports.cleanTagsArrayFunction(sortableArrayArg, sortedArray);
let sorterFunctionCounter = 0;
let sorterFunction = function () {
sortableArrayArg.forEach((dockerfileArg) => {
let cleanTags = exports.cleanTagsArrayFunction(sortableArrayArg, sortedArray);
2016-06-07 04:05:13 +00:00
if (cleanTags.indexOf(dockerfileArg.baseImage) == -1 && sortedArray.indexOf(dockerfileArg) == -1) {
2016-06-05 09:08:20 +00:00
sortedArray.push(dockerfileArg);
}
2016-06-07 04:05:13 +00:00
;
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) != -1) {
dockerfileArg.localBaseImageDependent = true;
}
;
2016-06-05 04:20:05 +00:00
});
2016-06-05 11:50:45 +00:00
if (sortableArrayArg.length == sortedArray.length) {
2016-06-05 11:01:45 +00:00
done.resolve(sortedArray);
2016-06-05 04:20:05 +00:00
}
2016-06-05 11:50:45 +00:00
else if (sorterFunctionCounter < 10) {
2016-06-05 09:08:20 +00:00
sorterFunctionCounter++;
sorterFunction();
2016-06-05 04:20:05 +00:00
}
2016-06-05 09:08:20 +00:00
;
};
sorterFunction();
2016-06-05 04:20:05 +00:00
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
*/
exports.mapDockerfiles = function (sortedArray) {
2016-07-18 14:56:53 +00:00
let done = plugins.q.defer();
sortedArray.forEach((dockerfileArg) => {
if (dockerfileArg.localBaseImageDependent) {
2016-07-18 14:56:53 +00:00
sortedArray.forEach((dockfile2) => {
if (dockfile2.cleanTag == dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2;
}
});
}
;
});
done.resolve(sortedArray);
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* builds the correspoding real docker image for each Dockerfile class instance
*/
2016-07-18 14:56:53 +00:00
exports.buildDockerfiles = (sortedArrayArg) => {
let done = plugins.q.defer();
sortedArrayArg.forEach(function (dockerfileArg) {
2016-06-05 04:20:05 +00:00
dockerfileArg.build();
});
2016-06-05 15:17:15 +00:00
done.resolve(sortedArrayArg);
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* pushes the real Dockerfile images to a Docker registry
*/
2016-06-07 20:27:10 +00:00
exports.pushDockerfiles = function (sortedArrayArg) {
2016-07-18 14:56:53 +00:00
let done = plugins.q.defer();
2016-06-05 15:17:15 +00:00
sortedArrayArg.forEach(function (dockerfileArg) {
2016-06-07 20:27:10 +00:00
dockerfileArg.push(NpmciEnv.buildStage);
2016-06-05 15:17:15 +00:00
});
done.resolve(sortedArrayArg);
2016-06-05 04:20:05 +00:00
return done.promise;
2016-06-05 02:48:39 +00:00
};
2016-09-04 15:56:56 +00:00
/**
* pulls corresponding real Docker images for instances of Dockerfile from a registry.
* This is needed if building, testing, and publishing of Docker images is carried out in seperate CI stages.
*/
2016-07-18 14:56:53 +00:00
exports.pullDockerfileImages = (sortableArrayArg, registryArg = "registry.gitlab.com") => {
let done = plugins.q.defer();
sortableArrayArg.forEach((dockerfileArg) => {
2016-06-07 17:41:14 +00:00
dockerfileArg.pull(registryArg);
2016-06-07 02:31:25 +00:00
});
done.resolve(sortableArrayArg);
2016-06-07 01:59:47 +00:00
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* tests all Dockerfiles in by calling class Dockerfile.test();
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
*/
2016-07-18 14:56:53 +00:00
exports.testDockerfiles = (sortedArrayArg) => {
let done = plugins.q.defer();
2016-06-07 01:59:47 +00:00
sortedArrayArg.forEach(function (dockerfileArg) {
dockerfileArg.test();
});
done.resolve(sortedArrayArg);
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* class Dockerfile represents a Dockerfile on disk in npmci
*/
2016-07-18 14:56:53 +00:00
class Dockerfile {
constructor(options) {
2016-06-05 02:48:39 +00:00
this.filePath = options.filePath;
this.repo = NpmciEnv.repo.user + "/" + NpmciEnv.repo.repo;
2016-06-05 11:01:45 +00:00
this.version = exports.dockerFileVersion(plugins.path.parse(options.filePath).base);
2016-06-05 04:20:05 +00:00
this.cleanTag = this.repo + ":" + this.version;
2016-06-07 17:41:14 +00:00
this.buildTag = this.cleanTag;
this.testTag = exports.dockerTag("registry.gitlab.com", this.repo, this.version, "test");
this.releaseTag = exports.dockerTag(NpmciEnv.dockerRegistry, this.repo, this.version);
2016-06-07 01:59:47 +00:00
this.containerName = "dockerfile-" + this.version;
2016-06-05 02:48:39 +00:00
if (options.filePath && options.read) {
2016-06-23 20:22:03 +00:00
this.content = plugins.smartfile.fs.toStringSync(plugins.path.resolve(options.filePath));
2016-06-05 02:48:39 +00:00
}
;
2016-06-05 11:01:45 +00:00
this.baseImage = exports.dockerBaseImage(this.content);
this.localBaseImageDependent = false;
2016-06-05 02:48:39 +00:00
}
;
2016-09-04 15:56:56 +00:00
/**
* builds the Dockerfile
*/
2016-07-18 14:56:53 +00:00
build() {
let done = plugins.q.defer();
2016-06-07 17:41:14 +00:00
plugins.beautylog.info("now building Dockerfile for " + this.cleanTag);
2016-06-07 01:59:47 +00:00
npmci_bash_1.bashBare("docker build -t " + this.buildTag + " -f " + this.filePath + " .");
NpmciEnv.dockerFilesBuilt.push(this);
done.resolve();
return done.promise;
2016-07-18 14:56:53 +00:00
}
2016-06-05 02:48:39 +00:00
;
2016-09-04 15:56:56 +00:00
/**
* pushes the Dockerfile to a registry
*/
2016-07-18 14:56:53 +00:00
push(stageArg) {
let done = plugins.q.defer();
let pushTag;
2016-06-07 20:27:10 +00:00
switch (stageArg) {
case "release":
pushTag = this.releaseTag;
break;
case "test":
default:
pushTag = this.testTag;
break;
}
2016-06-07 17:41:14 +00:00
npmci_bash_1.bashBare("docker tag " + this.buildTag + " " + pushTag);
npmci_bash_1.bashBare("docker push " + pushTag);
done.resolve();
return done.promise;
2016-07-18 14:56:53 +00:00
}
2016-09-04 15:56:56 +00:00
;
/**
* pulls the Dockerfile from a registry
*/
2016-07-18 14:56:53 +00:00
pull(registryArg) {
let pullTag = this.testTag;
2016-06-07 17:41:14 +00:00
npmci_bash_1.bashBare("docker pull " + pullTag);
npmci_bash_1.bashBare("docker tag " + pullTag + " " + this.buildTag);
2016-07-18 14:56:53 +00:00
}
2016-06-07 01:59:47 +00:00
;
2016-09-04 15:56:56 +00:00
/**
* tests the Dockerfile;
*/
2016-07-18 14:56:53 +00:00
test() {
let testFile = plugins.path.join(paths.NpmciTestDir, "test_" + this.version + ".sh");
let testFileExists = plugins.smartfile.fs.fileExistsSync(testFile);
2016-06-07 10:12:19 +00:00
if (testFileExists) {
npmci_bash_1.bashBare("docker run --name npmci_test_container " + this.buildTag + " mkdir /npmci_test");
npmci_bash_1.bashBare("docker cp " + testFile + " npmci_test_container:/npmci_test/test.sh");
npmci_bash_1.bashBare("docker commit npmci_test_container npmci_test_image");
npmci_bash_1.bashBare("docker run npmci_test_image sh /npmci_test/test.sh");
npmci_bash_1.bashBare("docker rm npmci_test_container");
npmci_bash_1.bashBare("docker rmi --force npmci_test_image");
}
else {
plugins.beautylog.warn("skipping tests for " + this.cleanTag + " because no testfile was found!");
}
2016-07-18 14:56:53 +00:00
}
2016-06-07 01:59:47 +00:00
;
2016-09-04 15:56:56 +00:00
/**
* gets the id of a Dockerfile
*/
2016-07-18 14:56:53 +00:00
getId() {
let containerId = npmci_bash_1.bashBare("docker inspect --type=image --format=\"{{.Id}}\" " + this.buildTag);
2016-06-07 01:59:47 +00:00
return containerId;
2016-07-18 14:56:53 +00:00
}
2016-06-07 01:59:47 +00:00
;
2016-07-18 14:56:53 +00:00
}
2016-06-05 02:48:39 +00:00
exports.Dockerfile = Dockerfile;
2016-09-04 15:56:56 +00:00
/**
*
*/
2016-06-05 11:01:45 +00:00
exports.dockerFileVersion = function (dockerfileNameArg) {
2016-07-18 14:56:53 +00:00
let versionString;
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
let regexResultArray = versionRegex.exec(dockerfileNameArg);
2016-06-05 06:06:32 +00:00
if (regexResultArray && regexResultArray.length == 2) {
2016-06-05 02:48:39 +00:00
versionString = regexResultArray[1];
}
else {
versionString = "latest";
}
return versionString;
};
2016-09-04 15:56:56 +00:00
/**
*
*/
2016-06-05 11:01:45 +00:00
exports.dockerBaseImage = function (dockerfileContentArg) {
2016-07-18 14:56:53 +00:00
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/;
let regexResultArray = baseImageRegex.exec(dockerfileContentArg);
2016-06-05 02:48:39 +00:00
return regexResultArray[1];
};
2016-09-04 15:56:56 +00:00
/**
*
*/
2016-06-07 17:41:14 +00:00
exports.dockerTag = function (registryArg, repoArg, versionArg, suffixArg) {
2016-07-18 14:56:53 +00:00
let tagString;
let registry = registryArg;
let repo = repoArg;
let version = versionArg;
2016-06-07 17:41:14 +00:00
if (suffixArg) {
version = versionArg + "_" + suffixArg;
2016-06-05 02:48:39 +00:00
}
2016-06-07 17:41:14 +00:00
;
2016-06-05 02:48:39 +00:00
tagString = registry + "/" + repo + ":" + version;
return tagString;
};
2016-09-04 15:56:56 +00:00
/**
*
*/
exports.cleanTagsArrayFunction = function (dockerfileArrayArg, trackingArrayArg) {
2016-07-18 14:56:53 +00:00
let cleanTagsArray = [];
dockerfileArrayArg.forEach(function (dockerfileArg) {
if (trackingArrayArg.indexOf(dockerfileArg) == -1) {
cleanTagsArray.push(dockerfileArg.cleanTag);
}
});
return cleanTagsArray;
};
2016-09-20 10:43:37 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtY2kuYnVpbGQuZG9ja2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvbnBtY2kuYnVpbGQuZG9ja2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwyQ0FBMkM7QUFDM0MsdUNBQXVDO0FBQ3ZDLHdDQUF3QztBQUN4Qyw2Q0FBc0M7QUFFdEM7O0dBRUc7QUFDUSxRQUFBLEtBQUssR0FBRztJQUNmLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQy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