improve bash, implement retagging

This commit is contained in:
Philipp Kunz 2016-06-07 03:57:43 +02:00
parent 8dc0db3b71
commit 5a05092bc8
3 changed files with 74 additions and 33 deletions

View File

@ -1,13 +1,23 @@
import "typings-global"; import "typings-global";
import * as plugins from "./npmci.plugins"; import * as plugins from "./npmci.plugins";
export let bash = (commandArg:string,retryArg = 2) => {
export let bash = (commandArg:string,retryArg = 2,bareArg = false) => {
let exitCode:number;
let stdOut:string;
let execResult;
if(!process.env.NPMTS_TEST){ if(!process.env.NPMTS_TEST){
for (let i = 0; i <= retryArg; i++){ for (let i = 0; i <= retryArg; i++){
let exitCode:number = plugins.shelljs.exec( if(!bareArg){
"bash -c \"source /usr/local/nvm/nvm.sh &&" + execResult = plugins.shelljs.exec(
commandArg + "bash -c \"source /usr/local/nvm/nvm.sh &&" +
"\"" commandArg +
).code; "\""
).code;
} else {
execResult = plugins.shelljs.exec(bareArg);
}
exitCode = execResult.code;
stdOut = execResult.stdout;
if(exitCode !== 0 && i == retryArg){ if(exitCode !== 0 && i == retryArg){
process.exit(1); process.exit(1);
} else if(exitCode == 0){ } else if(exitCode == 0){
@ -20,22 +30,9 @@ export let bash = (commandArg:string,retryArg = 2) => {
} else { } else {
plugins.beautylog.log("ShellExec would be: " + commandArg.blue) plugins.beautylog.log("ShellExec would be: " + commandArg.blue)
} }
return stdOut;
} }
export let bashBare = (commandArg,retryArg = 2) => { export let bashBare = (commandArg,retryArg = 2) => {
if (!process.env.NPMTS_TEST){ return bash(commandArg,retryArg,true);
for(let i = 0; i <= retryArg; i++){
let exitCode:number = plugins.shelljs.exec(commandArg).code;
if(exitCode !== 0 && i == retryArg){
process.exit(1);
} else if(exitCode == 0){
i = retryArg + 1; // if everything works out ok retrials are not wanted
} else {
plugins.beautylog.warn("Something went wrong! Exit Code: " + exitCode.toString());
plugins.beautylog.info("Retry " + (i + 1).toString() + " of " + retryArg.toString());
}
}
} else {
plugins.beautylog.log("ShellExec would be: " + commandArg.blue)
}
} }

View File

@ -1,4 +1,5 @@
import * as plugins from "./npmci.plugins" import * as plugins from "./npmci.plugins";
import * as paths from "./npmci.paths";
import * as NpmciEnv from "./npmci.env"; import * as NpmciEnv from "./npmci.env";
import {bashBare} from "./npmci.bash"; import {bashBare} from "./npmci.bash";
@ -32,10 +33,6 @@ export let readDockerfiles = function(){
return done.promise; return done.promise;
} }
export let getDockerImagesGitlab = function(sortableArrayArg:Dockerfile[]){
}
export let sortDockerfiles = function(sortableArrayArg:Dockerfile[]){ export let sortDockerfiles = function(sortableArrayArg:Dockerfile[]){
let done = plugins.q.defer(); let done = plugins.q.defer();
let sortedArray:Dockerfile[] = []; let sortedArray:Dockerfile[] = [];
@ -77,7 +74,7 @@ export let mapDockerfiles = function(sortedArray:Dockerfile[]){
return done.promise; return done.promise;
} }
export let buildDockerfiles = function(sortedArrayArg:Dockerfile[]){ export let buildDockerfiles = (sortedArrayArg:Dockerfile[]) => {
let done = plugins.q.defer(); let done = plugins.q.defer();
sortedArrayArg.forEach(function(dockerfileArg){ sortedArrayArg.forEach(function(dockerfileArg){
dockerfileArg.build(); dockerfileArg.build();
@ -95,12 +92,39 @@ export let pushDockerfiles = function(sortedArrayArg:Dockerfile[]){
return done.promise; return done.promise;
} }
export let pullDockerfileImages = (sortableArrayArg:Dockerfile[]) => {
let done = plugins.q.defer();
sortableArrayArg;
done.resolve();
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 { export class Dockerfile {
filePath:string; filePath:string;
repo:string; repo:string;
version:string; version:string;
cleanTag:string; cleanTag:string;
buildTag:string; buildTag:string;
releaseTag:string;
containerName:string
content:string; content:string;
patchedContent:string; patchedContent:string;
baseImage:string; baseImage:string;
@ -111,6 +135,9 @@ export class Dockerfile {
this.repo = NpmciEnv.repo.user + "/" + NpmciEnv.repo.repo; this.repo = NpmciEnv.repo.user + "/" + NpmciEnv.repo.repo;
this.version = dockerFileVersion(plugins.path.parse(options.filePath).base); this.version = dockerFileVersion(plugins.path.parse(options.filePath).base);
this.cleanTag = this.repo + ":" + this.version; this.cleanTag = this.repo + ":" + this.version;
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){ if(options.filePath && options.read){
this.content = plugins.smartfile.local.toStringSync(plugins.path.resolve(options.filePath)); this.content = plugins.smartfile.local.toStringSync(plugins.path.resolve(options.filePath));
}; };
@ -120,9 +147,7 @@ export class Dockerfile {
build(){ build(){
let done = plugins.q.defer(); let done = plugins.q.defer();
this.patchContents(); this.patchContents();
let tag = dockerTag(this.repo,this.version); bashBare("docker build -t " + this.buildTag + " -f " + this.filePath + " .");
bashBare("docker build -t " + tag + " -f " + this.filePath + " .");
this.buildTag = tag;
NpmciEnv.dockerFilesBuilt.push(this); NpmciEnv.dockerFilesBuilt.push(this);
this.restoreContents(); this.restoreContents();
done.resolve(); done.resolve();
@ -138,6 +163,24 @@ export class Dockerfile {
done.resolve(); done.resolve();
return done.promise; return done.promise;
} }
pull(){
bashBare("docker pull " + this.buildTag);
};
test(){
bashBare("docker run -v " +
plugins.path.join(paths.NpmciProjectDir,"./test") + ":/test/ " +
"--name " + this.containerName);
bashBare("docker stop /" + this.containerName);
};
release(){
bashBare("docker tag " + this.getId() + " " + this.releaseTag);
bashBare("docker push " + this.releaseTag);
}
getId(){
let containerId = bashBare("docker inspect --format=\"{{.Id}}\" " + this.buildTag);
return containerId;
};
patchContents(){ patchContents(){
let done = plugins.q.defer(); let done = plugins.q.defer();
if(this.localBaseImageDependent == true){ if(this.localBaseImageDependent == true){
@ -187,10 +230,10 @@ export let dockerBaseImage = function(dockerfileContentArg:string){
return regexResultArray[1]; return regexResultArray[1];
} }
export let dockerTag = function(repoArg:string,versionArg:string):string{ export let dockerTag = function(repoArg:string,versionArg:string,stageArg:string):string{
let tagString:string; let tagString:string;
let registry = NpmciEnv.dockerRegistry; let registry = NpmciEnv.dockerRegistry;
if(NpmciEnv.buildStage == "build" || NpmciEnv.buildStage == "test"){ if(stageArg == "build" || stageArg == "test"){
registry = "registry.gitlab.com"; registry = "registry.gitlab.com";
} }
let repo = repoArg; let repo = repoArg;

View File

@ -4,4 +4,5 @@ import * as plugins from "./npmci.plugins";
export let cwd = process.cwd(); export let cwd = process.cwd();
export let NpmciPackageRoot = plugins.path.join(__dirname,"../"); export let NpmciPackageRoot = plugins.path.join(__dirname,"../");
export let NpmciPackageConfig = plugins.path.join(NpmciPackageRoot,"./config.json") export let NpmciPackageConfig = plugins.path.join(NpmciPackageRoot,"./config.json");
export let NpmciProjectDir = process.cwd();