npmci/dist/npmci.build.docker.js

281 lines
20 KiB
JavaScript
Raw Permalink 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-04 15:56:56 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtY2kuYnVpbGQuZG9ja2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvbnBtY2kuYnVpbGQuZG9ja2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSxpQkFBaUIsQ0FBQyxDQUFBO0FBQzNDLE1BQVksS0FBSyxXQUFNLGVBQWUsQ0FBQyxDQUFBO0FBQ3ZDLE1BQVksUUFBUSxXQUFNLGFBQWEsQ0FBQyxDQUFB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