tsdocker/ts/npmdocker.docker.ts
2016-07-20 00:40:37 +02:00

114 lines
3.2 KiB
TypeScript

import * as plugins from "./npmdocker.plugins";
import * as paths from "./npmdocker.paths";
import * as snippets from "./npmdocker.snippets";
import {npmdockerOra} from "./npmdocker.promisechain";
let config;
let dockerData = {
imageTag: "npmdocker-temp-image:latest",
containerName: "npmdocker-temp-container"
};
/**
* check if docker is available
*/
let checkDocker = () => {
let done = plugins.q.defer();
npmdockerOra.text("checking docker...");
if(plugins.shelljs.which("docker")){
plugins.beautylog.ok("Docker found!")
done.resolve();
} else {
done.reject(new Error("docker not found on this machine"));
}
return done.promise;
};
/**
* builds the Dockerfile according to the config in the project
*/
let buildDockerFile = () => {
let done = plugins.q.defer();
npmdockerOra.text("building Dockerfile...");
let dockerfile:string = snippets.dockerfileSnippet({
baseImage:config.baseImage,
command:config.command
});
plugins.beautylog.info(`Base image is: ${config.baseImage}`);
plugins.beautylog.info(`Command is: ${config.command}`);
plugins.smartfile.memory.toFsSync(dockerfile,paths.dockerfile);
plugins.beautylog.ok("Dockerfile created!");
done.resolve();
return done.promise
};
/**
* builds the Dockerimage from the built Dockerfile
*/
let buildDockerImage = () => {
let done = plugins.q.defer();
npmdockerOra.text("pulling latest base image from registry...");
plugins.shelljs.exec(`docker pull ${config.baseImage}`,{
silent:true
},() => {
npmdockerOra.text("building Dockerimage...");
plugins.shelljs.exec(`docker build -f ${paths.dockerfile} -t ${dockerData.imageTag} ${paths.assets}`,{
silent:true
},() => {
plugins.beautylog.ok("Dockerimage built!")
done.resolve();
});
}); // first pull latest version of baseImage
return done.promise
};
/**
* creates a container by running the built Dockerimage
*/
let runDockerImage = () => {
let done = plugins.q.defer();
npmdockerOra.text("starting Container...");
npmdockerOra.end();
config.exitCode = plugins.shelljs.exec(`docker run -v ${paths.cwd}:/workspace --name ${dockerData.containerName} ${dockerData.imageTag}`).code;
done.resolve();
return done.promise;
};
let deleteDockerContainter = () => {
let done = plugins.q.defer();
plugins.shelljs.exec(`docker rm ${dockerData.containerName}`,{
silent:true
});
done.resolve();
plugins.beautylog.ok("removed test container!");
return done.promise
};
let deleteDockerImage = () => {
let done = plugins.q.defer();
plugins.shelljs.exec(`docker rmi ${dockerData.imageTag}`,{
silent:true
});
done.resolve();
plugins.beautylog.ok("removed test image!");
plugins.beautylog.ok("Cleaned up!");
return done.promise
};
export let run = (configArg) => {
let done = plugins.q.defer();
config = configArg;
checkDocker()
.then(buildDockerFile)
.then(buildDockerImage)
.then(runDockerImage)
.then(deleteDockerContainter)
.then(deleteDockerImage)
.then(() => {
done.resolve(config);
})
return done.promise;
}