tsdocker/ts/npmdocker.docker.ts

114 lines
3.2 KiB
TypeScript
Raw Normal View History

2016-07-18 18:48:34 +00:00
import * as plugins from "./npmdocker.plugins";
import * as paths from "./npmdocker.paths";
2016-07-18 22:59:57 +00:00
import * as snippets from "./npmdocker.snippets";
2016-07-19 22:40:37 +00:00
import {npmdockerOra} from "./npmdocker.promisechain";
2016-07-18 22:59:57 +00:00
let config;
2016-07-19 17:21:06 +00:00
let dockerData = {
imageTag: "npmdocker-temp-image:latest",
2016-07-19 22:40:37 +00:00
containerName: "npmdocker-temp-container"
2016-07-19 17:21:06 +00:00
};
2016-07-19 22:40:37 +00:00
2016-07-18 18:48:34 +00:00
/**
* check if docker is available
*/
2016-07-18 22:59:57 +00:00
let checkDocker = () => {
2016-07-18 18:48:34 +00:00
let done = plugins.q.defer();
2016-07-19 22:40:37 +00:00
npmdockerOra.text("checking docker...");
2016-07-18 18:48:34 +00:00
if(plugins.shelljs.which("docker")){
2016-07-19 17:21:06 +00:00
plugins.beautylog.ok("Docker found!")
2016-07-18 22:59:57 +00:00
done.resolve();
2016-07-18 18:48:34 +00:00
} else {
done.reject(new Error("docker not found on this machine"));
}
return done.promise;
};
2016-07-18 22:37:13 +00:00
/**
* builds the Dockerfile according to the config in the project
*/
let buildDockerFile = () => {
let done = plugins.q.defer();
2016-07-19 22:40:37 +00:00
npmdockerOra.text("building Dockerfile...");
2016-07-19 17:21:06 +00:00
let dockerfile:string = snippets.dockerfileSnippet({
2016-07-18 22:59:57 +00:00
baseImage:config.baseImage,
command:config.command
});
2016-07-19 17:21:06 +00:00
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();
2016-07-18 22:37:13 +00:00
return done.promise
};
/**
* builds the Dockerimage from the built Dockerfile
*/
let buildDockerImage = () => {
let done = plugins.q.defer();
2016-07-19 22:40:37 +00:00
npmdockerOra.text("pulling latest base image from registry...");
2016-07-19 17:21:06 +00:00
plugins.shelljs.exec(`docker pull ${config.baseImage}`,{
silent:true
},() => {
2016-07-19 22:40:37 +00:00
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
2016-07-18 22:37:13 +00:00
return done.promise
};
/**
* creates a container by running the built Dockerimage
*/
let runDockerImage = () => {
let done = plugins.q.defer();
2016-07-19 22:40:37 +00:00
npmdockerOra.text("starting Container...");
npmdockerOra.end();
config.exitCode = plugins.shelljs.exec(`docker run -v ${paths.cwd}:/workspace --name ${dockerData.containerName} ${dockerData.imageTag}`).code;
2016-07-19 17:21:06 +00:00
done.resolve();
return done.promise;
2016-07-18 22:37:13 +00:00
};
let deleteDockerContainter = () => {
let done = plugins.q.defer();
2016-07-19 17:21:06 +00:00
plugins.shelljs.exec(`docker rm ${dockerData.containerName}`,{
silent:true
});
done.resolve();
plugins.beautylog.ok("removed test container!");
2016-07-18 22:37:13 +00:00
return done.promise
};
let deleteDockerImage = () => {
let done = plugins.q.defer();
2016-07-19 17:21:06 +00:00
plugins.shelljs.exec(`docker rmi ${dockerData.imageTag}`,{
silent:true
});
done.resolve();
plugins.beautylog.ok("removed test image!");
plugins.beautylog.ok("Cleaned up!");
2016-07-18 22:37:13 +00:00
return done.promise
};
export let run = (configArg) => {
let done = plugins.q.defer();
2016-07-18 22:59:57 +00:00
config = configArg;
checkDocker()
2016-07-18 22:37:13 +00:00
.then(buildDockerFile)
.then(buildDockerImage)
.then(runDockerImage)
.then(deleteDockerContainter)
.then(deleteDockerImage)
2016-07-18 22:59:57 +00:00
.then(() => {
2016-07-19 22:40:37 +00:00
done.resolve(config);
2016-07-18 22:59:57 +00:00
})
2016-07-18 22:37:13 +00:00
return done.promise;
}