npmci/dist/npmci.build.docker.js

281 lines
20 KiB
JavaScript
Raw 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-09-04 15:56:56 +00:00
/**
* builds a cwd of Dockerfiles by triggering a promisechain
*/
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-09-04 15:56:56 +00:00
/**
* creates instance of class Dockerfile for all Dockerfiles in cwd
* @returns Promise<Dockerfile[]>
*/
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-11-24 22:21:40 +00:00
plugins.gulp.src('./Dockerfile*')
2016-06-05 11:01:45 +00:00
.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-09-04 15:56:56 +00:00
/**
* sorts Dockerfiles into a dependency chain
* @param sortableArrayArg an array of instances of class Dockerfile
* @returns Promise<Dockerfile[]>
*/
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-11-24 22:21:40 +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
;
2016-11-24 22:21:40 +00:00
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) !== -1) {
dockerfileArg.localBaseImageDependent = true;
}
;
2016-06-05 04:20:05 +00:00
});
2016-11-24 22:21:40 +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;
};
2016-09-04 15:56:56 +00:00
/**
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
*/
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) => {
2016-11-24 22:21:40 +00:00
if (dockfile2.cleanTag === dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2;
}
});
}
;
});
done.resolve(sortedArray);
return done.promise;
};
2016-09-04 15:56:56 +00:00
/**
* builds the correspoding real docker image for each Dockerfile class instance
*/
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-09-04 15:56:56 +00:00
/**
* pushes the real Dockerfile images to a Docker registry
*/
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-09-04 15:56:56 +00:00
/**
* pulls corresponding real Docker images for instances of Dockerfile from a registry.
* This is needed if building, testing, and publishing of Docker images is carried out in seperate CI stages.
*/
2016-11-24 22:21:40 +00:00
exports.pullDockerfileImages = (sortableArrayArg, registryArg = 'registry.gitlab.com') => {
2016-07-18 14:56:53 +00:00
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-09-04 15:56:56 +00:00
/**
* tests all Dockerfiles in by calling class Dockerfile.test();
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
*/
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-09-04 15:56:56 +00:00
/**
* class Dockerfile represents a Dockerfile on disk in npmci
*/
2016-07-18 14:56:53 +00:00
class Dockerfile {
constructor(options) {
2016-06-05 02:48:39 +00:00
this.filePath = options.filePath;
2016-11-24 22:21:40 +00:00
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-11-24 22:21:40 +00:00
this.cleanTag = this.repo + ':' + this.version;
2016-06-07 17:41:14 +00:00
this.buildTag = this.cleanTag;
2016-11-24 22:21:40 +00:00
this.testTag = exports.dockerTag('registry.gitlab.com', this.repo, this.version, 'test');
2016-06-07 17:41:14 +00:00
this.releaseTag = exports.dockerTag(NpmciEnv.dockerRegistry, this.repo, this.version);
2016-11-24 22:21:40 +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-09-04 15:56:56 +00:00
/**
* builds the Dockerfile
*/
2016-07-18 14:56:53 +00:00
build() {
let done = plugins.q.defer();
2016-11-24 22:21:40 +00:00
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag);
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-09-04 15:56:56 +00:00
/**
* pushes the Dockerfile to a registry
*/
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) {
2016-11-24 22:21:40 +00:00
case 'release':
2016-06-07 20:27:10 +00:00
pushTag = this.releaseTag;
break;
2016-11-24 22:21:40 +00:00
case 'test':
2016-06-07 20:27:10 +00:00
default:
pushTag = this.testTag;
break;
}
2016-11-24 22:21:40 +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
}
2016-09-04 15:56:56 +00:00
;
/**
* pulls the Dockerfile from a registry
*/
2016-07-18 14:56:53 +00:00
pull(registryArg) {
let pullTag = this.testTag;
2016-11-24 22:21:40 +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-09-04 15:56:56 +00:00
/**
* tests the Dockerfile;
*/
2016-07-18 14:56:53 +00:00
test() {
2016-11-24 22:21:40 +00:00
let testFile = plugins.path.join(paths.NpmciTestDir, 'test_' + this.version + '.sh');
2016-07-18 14:56:53 +00:00
let testFileExists = plugins.smartfile.fs.fileExistsSync(testFile);
2016-06-07 10:12:19 +00:00
if (testFileExists) {
2016-11-24 22:21:40 +00:00
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 {
2016-11-24 22:21:40 +00:00
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-09-04 15:56:56 +00:00
/**
* gets the id of a Dockerfile
*/
2016-07-18 14:56:53 +00:00
getId() {
2016-11-24 22:21:40 +00:00
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-09-04 15:56:56 +00:00
/**
*
*/
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-11-24 22:21:40 +00:00
if (regexResultArray && regexResultArray.length === 2) {
2016-06-05 02:48:39 +00:00
versionString = regexResultArray[1];
}
else {
2016-11-24 22:21:40 +00:00
versionString = 'latest';
2016-06-05 02:48:39 +00:00
}
return versionString;
};
2016-09-04 15:56:56 +00:00
/**
*
*/
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-09-04 15:56:56 +00:00
/**
*
*/
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) {
2016-11-24 22:21:40 +00:00
version = versionArg + '_' + suffixArg;
2016-06-05 02:48:39 +00:00
}
2016-06-07 17:41:14 +00:00
;
2016-11-24 22:21:40 +00:00
tagString = registry + '/' + repo + ':' + version;
2016-06-05 02:48:39 +00:00
return tagString;
};
2016-09-04 15:56:56 +00:00
/**
*
*/
exports.cleanTagsArrayFunction = function (dockerfileArrayArg, trackingArrayArg) {
2016-07-18 14:56:53 +00:00
let cleanTagsArray = [];
dockerfileArrayArg.forEach(function (dockerfileArg) {
2016-11-24 22:21:40 +00:00
if (trackingArrayArg.indexOf(dockerfileArg) === -1) {
cleanTagsAr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