npmci/dist/npmci.build.docker.js

221 lines
18 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-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-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-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;
};
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-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-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-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-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-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-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-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
}
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-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-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-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-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-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;
};
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-07-18 14:56:53 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtY2kuYnVpbGQuZG9ja2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvbnBtY2kuYnVpbGQuZG9ja2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQ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