npmci/dist/npmci.build.docker.js

251 lines
33 KiB
JavaScript
Raw Permalink Normal View History

2016-06-04 21:20:39 +00:00
"use strict";
var plugins = require("./npmci.plugins");
2016-06-07 01:59:47 +00:00
var paths = require("./npmci.paths");
2016-06-05 02:48:39 +00:00
var NpmciEnv = require("./npmci.env");
2016-06-05 12:55:08 +00:00
var npmci_bash_1 = require("./npmci.bash");
2016-06-04 21:20:39 +00:00
exports.build = function () {
var 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-06-05 14:43:27 +00:00
.then(function () {
done.resolve();
});
2016-06-04 21:20:39 +00:00
return done.promise;
};
2016-06-05 11:01:45 +00:00
exports.readDockerfiles = function () {
var done = plugins.q.defer();
var readDockerfilesArray = [];
plugins.gulp.src("./Dockerfile*")
.pipe(plugins.through2.obj(function (file, enc, cb) {
var 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-06-05 04:20:05 +00:00
var done = plugins.q.defer();
2016-06-05 09:08:20 +00:00
var sortedArray = [];
2016-06-05 11:50:45 +00:00
var trackingArray = [];
2016-06-05 09:08:20 +00:00
var sorterFunctionCounter = 0;
var sorterFunction = function () {
2016-06-05 11:01:45 +00:00
sortableArrayArg.forEach(function (dockerfileArg) {
2016-06-05 11:50:45 +00:00
var cleanTags = exports.cleanTagsArrayFunction(sortableArrayArg, trackingArray);
if (cleanTags.indexOf(dockerfileArg.baseImage) == -1 && trackingArray.indexOf(dockerfileArg) == -1) {
2016-06-05 09:08:20 +00:00
sortedArray.push(dockerfileArg);
2016-06-05 11:50:45 +00:00
trackingArray.push(dockerfileArg);
2016-06-05 09:08:20 +00:00
}
else if (cleanTags.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-06-05 04:20:05 +00:00
var done = plugins.q.defer();
sortedArray.forEach(function (dockerfileArg) {
if (dockerfileArg.localBaseImageDependent) {
sortedArray.forEach(function (dockfile2) {
if (dockfile2.cleanTag == dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2;
}
});
}
;
});
done.resolve(sortedArray);
return done.promise;
};
exports.buildDockerfiles = function (sortedArrayArg) {
var 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;
};
exports.pushDockerfiles = function (sortedArrayArg) {
var done = plugins.q.defer();
sortedArrayArg.forEach(function (dockerfileArg) {
dockerfileArg.push();
});
done.resolve(sortedArrayArg);
2016-06-05 04:20:05 +00:00
return done.promise;
2016-06-05 02:48:39 +00:00
};
2016-06-07 01:59:47 +00:00
exports.pullDockerfileImages = function (sortableArrayArg) {
var done = plugins.q.defer();
2016-06-07 02:31:25 +00:00
sortableArrayArg.forEach(function (dockerfileArg) {
dockerfileArg.pull();
});
done.resolve(sortableArrayArg);
2016-06-07 01:59:47 +00:00
return done.promise;
};
exports.testDockerfiles = function (sortedArrayArg) {
var done = plugins.q.defer();
sortedArrayArg.forEach(function (dockerfileArg) {
dockerfileArg.test();
});
done.resolve(sortedArrayArg);
return done.promise;
};
exports.releaseDockerfiles = function (sortedArrayArg) {
var done = plugins.q.defer();
sortedArrayArg.forEach(function (dockerfileArg) {
dockerfileArg.release();
});
done.resolve(sortedArrayArg);
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;
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 01:59:47 +00:00
this.buildTag = exports.dockerTag(this.repo, this.version, "build");
this.releaseTag = exports.dockerTag(this.repo, this.version, "release");
this.containerName = "dockerfile-" + 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));
}
;
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
}
;
Dockerfile.prototype.build = function () {
var done = plugins.q.defer();
this.patchContents();
2016-06-07 01:59:47 +00:00
npmci_bash_1.bashBare("docker build -t " + this.buildTag + " -f " + this.filePath + " .");
NpmciEnv.dockerFilesBuilt.push(this);
this.restoreContents();
done.resolve();
return done.promise;
2016-06-05 02:48:39 +00:00
};
;
Dockerfile.prototype.push = function () {
var done = plugins.q.defer();
2016-06-05 02:48:39 +00:00
if (this.buildTag) {
2016-06-05 12:55:08 +00:00
npmci_bash_1.bashBare("docker push " + this.buildTag);
2016-06-05 02:48:39 +00:00
}
else {
plugins.beautylog.error("Dockerfile hasn't been built yet!");
}
done.resolve();
return done.promise;
2016-06-05 02:48:39 +00:00
};
2016-06-07 01:59:47 +00:00
Dockerfile.prototype.pull = function () {
npmci_bash_1.bashBare("docker pull " + this.buildTag);
};
;
Dockerfile.prototype.test = function () {
var testExists = plugins.smartfile.checks.fileExistsSync(plugins.path.join(paths.NpmciProjectDir, ("./test/test_" + this.version + ".sh")));
if (testExists) {
npmci_bash_1.bashBare("docker run -v " +
plugins.path.join(paths.NpmciProjectDir, "./test") + ":/test/ " +
"--name " + this.containerName + " /test/" + "test_" + this.version + ".sh");
}
else {
plugins.beautylog.warn("skipping tests for " + this.cleanTag + " because no testfile was found!");
}
2016-06-07 01:59:47 +00:00
};
;
Dockerfile.prototype.release = function () {
npmci_bash_1.bashBare("docker tag " + this.getId() + " " + this.releaseTag);
npmci_bash_1.bashBare("docker push " + this.releaseTag);
};
Dockerfile.prototype.getId = function () {
var containerId = npmci_bash_1.bashBare("docker inspect --format=\"{{.Id}}\" " + this.buildTag);
return containerId;
};
;
Dockerfile.prototype.patchContents = function () {
var done = plugins.q.defer();
if (this.localBaseImageDependent == true) {
this.patchedContent = this.content.replace(/FROM\s[a-zA-Z0-9\/\-\:]*/, 'FROM ' + this.localBaseDockerfile.buildTag);
plugins.smartfile.memory.toFsSync(this.patchedContent, {
fileName: plugins.path.parse(this.filePath).name,
filePath: plugins.path.parse(this.filePath).dir
});
}
done.resolve();
return done.promise;
};
;
Dockerfile.prototype.restoreContents = function () {
var done = plugins.q.defer();
if (this.localBaseImageDependent == true) {
plugins.smartfile.memory.toFsSync(this.content, {
fileName: plugins.path.parse(this.filePath).name,
filePath: plugins.path.parse(this.filePath).dir
});
}
done.resolve();
return done.promise;
};
;
2016-06-05 02:48:39 +00:00
return Dockerfile;
}());
exports.Dockerfile = Dockerfile;
2016-06-05 11:01:45 +00:00
exports.dockerFileVersion = function (dockerfileNameArg) {
2016-06-05 02:48:39 +00:00
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;
};
2016-06-05 11:01:45 +00:00
exports.dockerBaseImage = function (dockerfileContentArg) {
var baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/;
2016-06-05 02:48:39 +00:00
var regexResultArray = baseImageRegex.exec(dockerfileContentArg);
return regexResultArray[1];
};
2016-06-07 01:59:47 +00:00
exports.dockerTag = function (repoArg, versionArg, stageArg) {
2016-06-05 02:48:39 +00:00
var tagString;
var registry = NpmciEnv.dockerRegistry;
2016-06-07 01:59:47 +00:00
if (stageArg == "build" || stageArg == "test") {
2016-06-05 02:48:39 +00:00
registry = "registry.gitlab.com";
}
var repo = repoArg;
var version = versionArg;
if (NpmciEnv.buildStage == "build" || NpmciEnv.buildStage == "test") {
2016-06-05 02:48:39 +00:00
version = version + "_test";
}
tagString = registry + "/" + repo + ":" + version;
return tagString;
};
exports.cleanTagsArrayFunction = function (dockerfileArrayArg, trackingArrayArg) {
var cleanTagsArray = [];
dockerfileArrayArg.forEach(function (dockerfileArg) {
if (trackingArrayArg.indexOf(dockerfileArg) == -1) {
cleanTagsArray.push(dockerfileArg.cleanTag);
}
});
return cleanTagsArray;
};
2016-06-04 21:20:39 +00:00
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5wbWNpLmJ1aWxkLmRvY2tlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsSUFBWSxPQUFPLFdBQU0saUJBQWlCLENBQUMsQ0FBQTtBQUMzQyxJQUFZLEtBQUssV0FBTSxlQUFlLENBQUMsQ0FBQTtBQUN2QyxJQUFZLFFBQVEsV0FBTSxhQUFhLENBQUMsQ0FBQTtBQUN4QywyQkFBdUIsY0FBYyxDQUFDLENBQUE7QUFFM0IsYUFBSyxHQUFHO0lBQ2YsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0F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