npmci/ts/npmci.build.docker.ts

265 lines
9.0 KiB
TypeScript
Raw Permalink Normal View History

2016-06-07 01:57:43 +00:00
import * as plugins from "./npmci.plugins";
import * as paths from "./npmci.paths";
2016-06-04 23:31:21 +00:00
import * as NpmciEnv from "./npmci.env";
2016-06-05 12:55:08 +00:00
import {bashBare} from "./npmci.bash";
2016-06-06 18:30:06 +00:00
2016-06-04 16:41:35 +00:00
export let build = function(){
2016-06-05 04:26:05 +00:00
let done = plugins.q.defer();
readDockerfiles()
.then(sortDockerfiles)
.then(mapDockerfiles)
2016-06-05 14:43:27 +00:00
.then(buildDockerfiles)
2016-06-05 15:17:15 +00:00
.then(pushDockerfiles)
2016-06-05 14:43:27 +00:00
.then(() => {
done.resolve();
});
2016-06-04 16:41:35 +00:00
return done.promise;
}
2016-06-05 11:01:45 +00:00
export let readDockerfiles = function(){
let done = plugins.q.defer();
let readDockerfilesArray:Dockerfile[] = []
plugins.gulp.src("./Dockerfile*")
.pipe(plugins.through2.obj(function(file,enc,cb){
let myDockerfile = new Dockerfile({
filePath:file.path,
read:true
});
readDockerfilesArray.push(myDockerfile);
cb(null,file);
},function(){
done.resolve(readDockerfilesArray);
}));
return done.promise;
}
2016-06-04 23:22:04 +00:00
2016-06-05 11:01:45 +00:00
export let sortDockerfiles = function(sortableArrayArg:Dockerfile[]){
2016-06-05 04:20:05 +00:00
let done = plugins.q.defer();
2016-06-07 04:05:13 +00:00
let sortedArray:Dockerfile[] = [];
let cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg,sortedArray);
2016-06-05 09:08:20 +00:00
let sorterFunctionCounter:number = 0;
let sorterFunction = function(){
2016-06-05 11:01:45 +00:00
sortableArrayArg.forEach((dockerfileArg)=>{
2016-06-07 04:05:13 +00:00
let cleanTags = 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 11:50:45 +00:00
} else if (sorterFunctionCounter < 10) {
2016-06-05 09:08:20 +00:00
sorterFunctionCounter++;
sorterFunction();
};
}
sorterFunction();
2016-06-05 04:20:05 +00:00
return done.promise;
};
export let mapDockerfiles = function(sortedArray:Dockerfile[]){
let done = plugins.q.defer();
sortedArray.forEach((dockerfileArg) => {
if(dockerfileArg.localBaseImageDependent){
sortedArray.forEach((dockfile2:Dockerfile) => {
if(dockfile2.cleanTag == dockerfileArg.baseImage){
dockerfileArg.localBaseDockerfile = dockfile2;
}
})
};
});
done.resolve(sortedArray);
return done.promise;
2016-06-05 04:20:05 +00:00
}
2016-06-07 01:57:43 +00:00
export let buildDockerfiles = (sortedArrayArg:Dockerfile[]) => {
2016-06-05 04:20:05 +00:00
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;
}
export let pushDockerfiles = function(sortedArrayArg:Dockerfile[]){
let 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-07 01:57:43 +00:00
export let pullDockerfileImages = (sortableArrayArg:Dockerfile[]) => {
let done = plugins.q.defer();
2016-06-07 02:31:25 +00:00
sortableArrayArg.forEach((dockerfileArg) => {
dockerfileArg.pull();
});
done.resolve(sortableArrayArg);
2016-06-07 01:57:43 +00:00
return done.promise;
}
export let testDockerfiles = (sortedArrayArg:Dockerfile[]) => {
let done = plugins.q.defer();
sortedArrayArg.forEach(function(dockerfileArg){
dockerfileArg.test();
});
done.resolve(sortedArrayArg);
return done.promise;
};
export let releaseDockerfiles = (sortedArrayArg:Dockerfile[]) => {
let done = plugins.q.defer();
sortedArrayArg.forEach(function(dockerfileArg){
dockerfileArg.release();
});
done.resolve(sortedArrayArg);
return done.promise;
}
export class Dockerfile {
2016-06-05 02:45:46 +00:00
filePath:string;
2016-06-04 23:31:21 +00:00
repo:string;
version:string;
2016-06-05 04:20:05 +00:00
cleanTag:string;
buildTag:string;
2016-06-07 01:57:43 +00:00
releaseTag:string;
containerName:string
2016-06-05 02:19:54 +00:00
content:string;
patchedContent:string;
2016-06-04 23:31:21 +00:00
baseImage:string;
localBaseImageDependent:boolean;
localBaseDockerfile:Dockerfile;
constructor(options:{filePath?:string,fileContents?:string|Buffer,read?:boolean}){
2016-06-05 02:45:46 +00:00
this.filePath = options.filePath;
2016-06-05 02:19:54 +00:00
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-07 01:57:43 +00:00
this.buildTag = dockerTag(this.repo,this.version,"build");
this.releaseTag = dockerTag(this.repo,this.version,"release");
this.containerName = "dockerfile-" + this.version;
if(options.filePath && options.read){
2016-06-05 02:19:54 +00:00
this.content = plugins.smartfile.local.toStringSync(plugins.path.resolve(options.filePath));
};
this.baseImage = dockerBaseImage(this.content);
this.localBaseImageDependent = false;
2016-06-04 23:31:21 +00:00
};
build(){
2016-06-07 04:35:10 +00:00
plugins.beautylog.info("now building Dockerfile for " + this.cleanTag);
let done = plugins.q.defer();
this.patchContents();
2016-06-07 01:57:43 +00:00
bashBare("docker build -t " + this.buildTag + " -f " + this.filePath + " .");
NpmciEnv.dockerFilesBuilt.push(this);
this.restoreContents();
done.resolve();
return done.promise;
2016-06-04 23:31:21 +00:00
};
push(){
let done = plugins.q.defer();
2016-06-05 02:45:46 +00:00
if(this.buildTag){
2016-06-05 12:55:08 +00:00
bashBare("docker push " + this.buildTag);
2016-06-05 02:45:46 +00:00
} else {
plugins.beautylog.error("Dockerfile hasn't been built yet!");
}
done.resolve();
return done.promise;
}
2016-06-07 01:57:43 +00:00
pull(){
bashBare("docker pull " + this.buildTag);
};
test(){
let testExists = plugins.smartfile.checks.fileExistsSync(
plugins.path.join(paths.NpmciProjectDir,("./test/test_" + this.version + ".sh"))
);
if(testExists){
bashBare("docker run -v " +
plugins.path.join(paths.NpmciProjectDir,"./test") + ":/test/ " +
2016-06-07 04:51:05 +00:00
"--name " + this.containerName + " " + this.buildTag + " /test/" + "test_" + this.version + ".sh");
} else {
plugins.beautylog.warn("skipping tests for " + this.cleanTag + " because no testfile was found!");
}
2016-06-07 01:57:43 +00:00
};
release(){
bashBare("docker tag " + this.getId() + " " + this.releaseTag);
bashBare("docker push " + this.releaseTag);
}
getId(){
2016-06-07 04:14:36 +00:00
let containerId = bashBare("docker inspect --type=image --format=\"{{.Id}}\" " + this.buildTag);
2016-06-07 01:57:43 +00:00
return containerId;
};
patchContents(){
let 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;
};
restoreContents(){
let 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-04 23:31:21 +00:00
}
2016-06-05 11:01:45 +00:00
export let dockerFileVersion = function(dockerfileNameArg:string):string{
2016-06-05 02:19:54 +00:00
let versionString:string;
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
let regexResultArray = versionRegex.exec(dockerfileNameArg);
2016-06-05 06:06:32 +00:00
if(regexResultArray && regexResultArray.length == 2){
2016-06-05 02:19:54 +00:00
versionString = regexResultArray[1];
2016-06-04 23:31:21 +00:00
} else {
2016-06-05 02:19:54 +00:00
versionString = "latest";
2016-06-04 23:31:21 +00:00
}
2016-06-05 02:19:54 +00:00
return versionString;
2016-06-04 23:31:21 +00:00
}
2016-06-05 11:01:45 +00:00
export let dockerBaseImage = function(dockerfileContentArg:string){
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/
2016-06-05 02:19:54 +00:00
let regexResultArray = baseImageRegex.exec(dockerfileContentArg)
return regexResultArray[1];
2016-06-04 23:31:21 +00:00
}
2016-06-04 23:22:04 +00:00
2016-06-07 01:57:43 +00:00
export let dockerTag = function(repoArg:string,versionArg:string,stageArg:string):string{
2016-06-05 02:45:46 +00:00
let tagString:string;
let registry = NpmciEnv.dockerRegistry;
2016-06-07 01:57:43 +00:00
if(stageArg == "build" || stageArg == "test"){
2016-06-05 02:45:46 +00:00
registry = "registry.gitlab.com";
}
let repo = repoArg;
let version = versionArg;
2016-06-05 19:11:30 +00:00
if(NpmciEnv.buildStage == "build" || NpmciEnv.buildStage == "test"){
2016-06-05 02:45:46 +00:00
version = version + "_test";
2016-06-05 02:19:54 +00:00
}
2016-06-05 02:45:46 +00:00
tagString = registry + "/" + repo + ":" + version;
return tagString;
2016-06-05 02:19:54 +00:00
};
export let cleanTagsArrayFunction = function(dockerfileArrayArg:Dockerfile[],trackingArrayArg:Dockerfile[]):string[]{
let cleanTagsArray:string[] = [];
dockerfileArrayArg.forEach(function(dockerfileArg){
if(trackingArrayArg.indexOf(dockerfileArg) == -1){
cleanTagsArray.push(dockerfileArg.cleanTag);
}
});
return cleanTagsArray;
}