2016-06-04 16:41:35 +00:00
|
|
|
import * as plugins from "./npmci.plugins"
|
2016-06-04 23:31:21 +00:00
|
|
|
import * as NpmciEnv from "./npmci.env";
|
|
|
|
|
|
|
|
|
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();
|
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 16:41:35 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
|
2016-06-05 00:17:55 +00:00
|
|
|
let readDockerfiles = function(){
|
2016-06-05 05:50:04 +00:00
|
|
|
return plugins.through2.obj(function(file,enc,cb){
|
2016-06-05 03:16:14 +00:00
|
|
|
let myDockerfile = new Dockerfile({
|
|
|
|
filePath:file.path,
|
|
|
|
read:true
|
|
|
|
});
|
2016-06-05 00:17:55 +00:00
|
|
|
NpmciEnv.dockerFiles.push(
|
2016-06-05 03:16:14 +00:00
|
|
|
myDockerfile
|
2016-06-05 00:17:55 +00:00
|
|
|
);
|
2016-06-05 03:16:14 +00:00
|
|
|
cb(null,file);
|
2016-06-05 05:17:02 +00:00
|
|
|
});
|
2016-06-05 00:17:55 +00:00
|
|
|
}
|
2016-06-04 23:22:04 +00:00
|
|
|
|
2016-06-05 04:20:05 +00:00
|
|
|
let sortDockerfiles = function(){
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
let redoSort:boolean;
|
2016-06-05 06:19:03 +00:00
|
|
|
let sortCounter:number = 0;
|
2016-06-05 04:20:05 +00:00
|
|
|
let sortFunction = function(){
|
|
|
|
redoSort = false;
|
|
|
|
let notYetBuiltImages:string[] = [];
|
|
|
|
NpmciEnv.dockerFiles.forEach((dockerFileArg)=>{
|
|
|
|
notYetBuiltImages.push(dockerFileArg.cleanTag);
|
|
|
|
});
|
|
|
|
NpmciEnv.dockerFiles.sort(function(a,b){
|
2016-06-05 06:30:08 +00:00
|
|
|
console.log("iteration: " + sortCounter.toString());
|
|
|
|
console.log(notYetBuiltImages)
|
|
|
|
console.log(a.cleanTag);
|
2016-06-05 04:20:05 +00:00
|
|
|
let aIndex = notYetBuiltImages.indexOf(a.cleanTag);
|
|
|
|
if(aIndex != -1){notYetBuiltImages.splice(aIndex,1)}
|
2016-06-05 06:30:08 +00:00
|
|
|
console.log(notYetBuiltImages);
|
2016-06-05 07:33:51 +00:00
|
|
|
if(notYetBuiltImages.indexOf(b.baseImage) != -1){
|
2016-06-05 04:20:05 +00:00
|
|
|
redoSort = true;
|
2016-06-05 06:30:08 +00:00
|
|
|
return -1;
|
2016-06-05 04:20:05 +00:00
|
|
|
} else {
|
2016-06-05 07:42:08 +00:00
|
|
|
return 0
|
2016-06-05 04:20:05 +00:00
|
|
|
}
|
|
|
|
});
|
2016-06-05 06:30:08 +00:00
|
|
|
if(redoSort && sortCounter <= 50){
|
2016-06-05 06:19:03 +00:00
|
|
|
sortCounter++;
|
2016-06-05 04:20:05 +00:00
|
|
|
sortFunction();
|
|
|
|
} else {
|
|
|
|
done.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sortFunction();
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
|
2016-06-05 03:16:14 +00:00
|
|
|
let buildDockerfiles = function(){
|
2016-06-05 04:20:05 +00:00
|
|
|
let done = plugins.q.defer();
|
|
|
|
NpmciEnv.dockerFiles.forEach(function(dockerfileArg){
|
|
|
|
dockerfileArg.build();
|
|
|
|
})
|
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
2016-06-05 03:16:14 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 00:17:55 +00:00
|
|
|
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-05 02:19:54 +00:00
|
|
|
content:string;
|
2016-06-04 23:31:21 +00:00
|
|
|
baseImage:string;
|
2016-06-05 00:17:55 +00:00
|
|
|
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-05 00:17:55 +00:00
|
|
|
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);
|
2016-06-04 23:31:21 +00:00
|
|
|
};
|
|
|
|
build(){
|
2016-06-05 03:16:14 +00:00
|
|
|
if(!this.buildTag){
|
|
|
|
let tag = 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-04 23:31:21 +00:00
|
|
|
};
|
2016-06-05 00:17:55 +00:00
|
|
|
push(){
|
2016-06-05 02:45:46 +00:00
|
|
|
if(this.buildTag){
|
|
|
|
plugins.shelljs.exec("docker push " + this.buildTag);
|
|
|
|
} else {
|
|
|
|
plugins.beautylog.error("Dockerfile hasn't been built yet!");
|
|
|
|
}
|
2016-06-05 00:17:55 +00:00
|
|
|
}
|
2016-06-04 23:31:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 02:19:54 +00:00
|
|
|
let dockerFileVersion = function(dockerfileNameArg:string):string{
|
|
|
|
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 02:19:54 +00:00
|
|
|
let dockerBaseImage = function(dockerfileContentArg:string){
|
|
|
|
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n/
|
|
|
|
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-05 02:45:46 +00:00
|
|
|
export let dockerTag = function(repoArg:string,versionArg:string):string{
|
|
|
|
let tagString:string;
|
|
|
|
let registry = NpmciEnv.dockerRegistry;
|
2016-06-05 02:19:54 +00:00
|
|
|
if(process.env.CI_BUILD_STAGE == "test"){
|
2016-06-05 02:45:46 +00:00
|
|
|
registry = "registry.gitlab.com";
|
|
|
|
}
|
|
|
|
let repo = repoArg;
|
|
|
|
let version = versionArg;
|
|
|
|
if(process.env.CI_BUILD_STAGE == "test" || process.env.CI_BUILD_STAGE == "build"){
|
|
|
|
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
|
|
|
};
|