npmci/dist/npmci.build.docker.js
2016-06-05 08:19:03 +02:00

137 lines
16 KiB
JavaScript

"use strict";
var plugins = require("./npmci.plugins");
var NpmciEnv = require("./npmci.env");
exports.build = function () {
var done = plugins.q.defer();
plugins.gulp.src("./Dockerfile*")
.pipe(readDockerfiles())
.pipe(plugins.gulpFunction(function () {
sortDockerfiles()
.then(buildDockerfiles)
.then(done.resolve);
}, "atEnd"));
return done.promise;
};
var readDockerfiles = function () {
return plugins.through2.obj(function (file, enc, cb) {
var myDockerfile = new Dockerfile({
filePath: file.path,
read: true
});
NpmciEnv.dockerFiles.push(myDockerfile);
cb(null, file);
});
};
var sortDockerfiles = function () {
var done = plugins.q.defer();
var redoSort;
var sortCounter = 0;
var sortFunction = function () {
redoSort = false;
var notYetBuiltImages = [];
NpmciEnv.dockerFiles.forEach(function (dockerFileArg) {
notYetBuiltImages.push(dockerFileArg.cleanTag);
});
NpmciEnv.dockerFiles.sort(function (a, b) {
plugins.beautylog.log("sort build order for Dockerimages");
var aIndex = notYetBuiltImages.indexOf(a.cleanTag);
if (aIndex != -1) {
notYetBuiltImages.splice(aIndex, 1);
}
if (notYetBuiltImages.indexOf(b.cleanTag) != -1) {
redoSort = true;
return 1;
}
else {
return 0;
}
});
if (redoSort && sortCounter <= 100) {
sortCounter++;
sortFunction();
}
else {
done.resolve();
}
};
sortFunction();
return done.promise;
};
var buildDockerfiles = function () {
var done = plugins.q.defer();
NpmciEnv.dockerFiles.forEach(function (dockerfileArg) {
dockerfileArg.build();
});
done.resolve();
return done.promise;
};
var Dockerfile = (function () {
function Dockerfile(options) {
this.filePath = options.filePath;
this.repo = NpmciEnv.repo.user + "/" + NpmciEnv.repo.repo;
this.version = dockerFileVersion(plugins.path.parse(options.filePath).base);
this.cleanTag = this.repo + ":" + this.version;
if (options.filePath && options.read) {
this.content = plugins.smartfile.local.toStringSync(plugins.path.resolve(options.filePath));
}
;
this.baseImage = dockerBaseImage(this.content);
}
;
Dockerfile.prototype.build = function () {
if (!this.buildTag) {
var tag = exports.dockerTag(this.repo, this.version);
plugins.shelljs.exec("docker build -t " + tag + " -f " + this.filePath + " .");
this.buildTag = tag;
NpmciEnv.dockerFilesBuilt.push(this);
}
else {
plugins.beautylog.error("This Dockerfile already has been built!");
}
};
;
Dockerfile.prototype.push = function () {
if (this.buildTag) {
plugins.shelljs.exec("docker push " + this.buildTag);
}
else {
plugins.beautylog.error("Dockerfile hasn't been built yet!");
}
};
return Dockerfile;
}());
exports.Dockerfile = Dockerfile;
var dockerFileVersion = function (dockerfileNameArg) {
var versionString;
var versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
var regexResultArray = versionRegex.exec(dockerfileNameArg);
if (regexResultArray && regexResultArray.length == 2) {
versionString = regexResultArray[1];
}
else {
versionString = "latest";
}
return versionString;
};
var dockerBaseImage = function (dockerfileContentArg) {
var baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n/;
var regexResultArray = baseImageRegex.exec(dockerfileContentArg);
return regexResultArray[1];
};
exports.dockerTag = function (repoArg, versionArg) {
var tagString;
var registry = NpmciEnv.dockerRegistry;
if (process.env.CI_BUILD_STAGE == "test") {
registry = "registry.gitlab.com";
}
var repo = repoArg;
var version = versionArg;
if (process.env.CI_BUILD_STAGE == "test" || process.env.CI_BUILD_STAGE == "build") {
version = version + "_test";
}
tagString = registry + "/" + repo + ":" + version;
return tagString;
};
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5wbWNpLmJ1aWxkLmRvY2tlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsSUFBWSxPQUFPLFdBQU0saUJBQ3pCLENBQUMsQ0FEeUM7QUFDMUMsSUFBWSxRQUFRLFdBQU0sYUFBYSxDQUFDLENBQUE7QUFHN0IsYUFBSyxHQUFHO0lBQ2YsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ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