npmci/dist/npmci.build.docker.js

137 lines
17 KiB
JavaScript
Raw Permalink Normal View History

2016-06-04 21:20:39 +00:00
"use strict";
var plugins = require("./npmci.plugins");
2016-06-05 02:48:39 +00:00
var NpmciEnv = require("./npmci.env");
2016-06-04 21:20:39 +00:00
exports.build = function () {
var done = plugins.q.defer();
2016-06-05 05:41:14 +00:00
plugins.gulp.src("./Dockerfile*")
2016-06-05 05:17:02 +00:00
.pipe(readDockerfiles())
2016-06-05 04:20:05 +00:00
.pipe(plugins.gulpFunction(function () {
sortDockerfiles()
.then(buildDockerfiles)
.then(done.resolve);
}, "atEnd"));
2016-06-04 21:20:39 +00:00
return done.promise;
};
2016-06-05 02:48:39 +00:00
var readDockerfiles = function () {
2016-06-05 05:50:04 +00:00
return plugins.through2.obj(function (file, enc, cb) {
var myDockerfile = new Dockerfile({
2016-06-05 02:48:39 +00:00
filePath: file.path,
read: true
});
NpmciEnv.dockerFiles.push(myDockerfile);
cb(null, file);
2016-06-05 05:17:02 +00:00
});
};
2016-06-05 09:08:20 +00:00
var cleanTagsArrayFunction = function () {
var cleanTagsArray = [];
NpmciEnv.dockerFiles.forEach(function (dockerfileArg) {
cleanTagsArray.push(dockerfileArg.cleanTag);
});
return cleanTagsArray;
};
2016-06-05 04:20:05 +00:00
var sortDockerfiles = function () {
var done = plugins.q.defer();
2016-06-05 09:08:20 +00:00
var sortableArray = NpmciEnv.dockerFiles;
var sortedArray = [];
var sorterFunctionCounter = 0;
var sorterFunction = function () {
var cleanTags = cleanTagsArrayFunction();
sortableArray.forEach(function (dockerfileArg) {
if (cleanTags.indexOf(dockerfileArg.baseImage) == -1) {
var dockerfileArgIndex = sortableArray.indexOf(dockerfileArg);
sortableArray.splice(dockerfileArgIndex);
sortedArray.push(dockerfileArg);
}
2016-06-05 04:20:05 +00:00
});
2016-06-05 09:08:20 +00:00
if (sortableArray.length == 0) {
console.log(sortedArray);
NpmciEnv.dockerFiles = sortedArray;
done.resolve();
2016-06-05 04:20:05 +00:00
}
2016-06-05 09:08:20 +00:00
else if (sorterFunctionCounter < 100) {
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;
};
var buildDockerfiles = function () {
var done = plugins.q.defer();
NpmciEnv.dockerFiles.forEach(function (dockerfileArg) {
dockerfileArg.build();
});
done.resolve();
return done.promise;
2016-06-05 02:48:39 +00:00
};
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);
2016-06-05 04:20:05 +00:00
this.cleanTag = this.repo + ":" + this.version;
2016-06-05 02:48:39 +00:00
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!");
}
2016-06-05 02:48:39 +00:00
};
;
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);
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;
};
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;
2016-06-05 08:23:55 +00:00
if (process.env.CI_BUILD_STAGE == "build" || process.env.CI_BUILD_STAGE == "test") {
2016-06-05 02:48:39 +00:00
registry = "registry.gitlab.com";
}
var repo = repoArg;
var version = versionArg;
2016-06-05 08:23:55 +00:00
if (process.env.CI_BUILD_STAGE == "build" || process.env.CI_BUILD_STAGE == "test") {
2016-06-05 02:48:39 +00:00
version = version + "_test";
}
tagString = registry + "/" + repo + ":" + version;
return tagString;
};
2016-06-04 21:20:39 +00:00
2016-06-05 09:08:20 +00:00
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5wbWNpLmJ1aWxkLmRvY2tlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsSUFBWSxPQUFPLFdBQU0saUJBQ3pCLENBQUMsQ0FEeUM7QUFDMUMsSUFBWSxRQUFRLFdBQU0sYUFBYSxDQUFDLENBQUE7QUFHN0IsYUFBSyxHQUFHO0lBQ2YsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixPQUFPLENBQUMsSUFBSSxDQUFDLEdBQUcsQ0FBQyxlQUFlLENBQUM7U0FDNUIsSUFBSSxDQUFDLGVBQWUsRUFBRSxDQUFDO1NBQ3ZCLElBQUksQ0FBQyxPQUFPLENBQUMsWUFBWSxDQUFDO1FBQ3ZCLGVBQWUsRUFBRTthQUNaLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQzthQUN0QixJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQzVCLENBQUMsRUFBQyxPQUFPLENBQUMsQ0FBQyxDQUFDO0lBQ2hCLE1BQU0sQ0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