npmci/dist/npmci.build.docker.js

257 lines
35 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-07 04:05:13 +00:00
var cleanTagsOriginal = exports.cleanTagsArrayFunction(sortableArrayArg, sortedArray);
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-07 04:05:13 +00:00
var cleanTags = exports.cleanTagsArrayFunction(sortableArrayArg, sortedArray);
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-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 () {
2016-06-07 04:35:10 +00:00
plugins.beautylog.info("now building Dockerfile for " + this.cleanTag);
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 () {
2016-06-07 08:21:56 +00:00
var testFile = plugins.path.join(paths.NpmciTestDir, "test_" + this.version + ".sh");
2016-06-07 10:12:19 +00:00
var testFileExists = plugins.smartfile.checks.fileExistsSync(testFile);
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-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 () {
2016-06-07 04:14:36 +00:00
var containerId = npmci_bash_1.bashBare("docker inspect --type=image --format=\"{{.Id}}\" " + this.buildTag);
2016-06-07 01:59:47 +00:00
return containerId;
};
;
Dockerfile.prototype.patchContents = function () {
var done = plugins.q.defer();
if (this.localBaseImageDependent == true) {
2016-06-07 04:35:10 +00:00
plugins.beautylog.info("patching Dockerfile due to local build dependency!");
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;
2016-06-07 10:36:00 +00:00
if (stageArg == "build" || stageArg == "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
2016-06-07 10:36:00 +00:00
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2Z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