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";
|
|
|
|
|
|
|
|
let config;
|
2016-07-19 00:10:36 +00:00
|
|
|
let imageTag = "npmdocker-temp-image"
|
|
|
|
let containerName = "npmdocker-temp-container"
|
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();
|
|
|
|
if(plugins.shelljs.which("docker")){
|
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-18 22:59:57 +00:00
|
|
|
let dockerfile = snippets.dockerfileSnippet({
|
|
|
|
baseImage:config.baseImage,
|
|
|
|
command:config.command
|
|
|
|
});
|
2016-07-19 00:03:54 +00:00
|
|
|
plugins.smartfile.memory.toFsSync(JSON.stringify(dockerfile),paths.dockerfile);
|
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 00:10:36 +00:00
|
|
|
plugins.shelljs.exec(`docker pull ${config.baseImage}`); // first pull latest version of baseImage
|
|
|
|
plugins.shelljs.exec(`docker build -f ${paths.dockerfile} -v ${paths.cwd}:/workdir -t ${imageTag} ${paths.assets}`);
|
2016-07-19 00:03:54 +00:00
|
|
|
done.resolve();
|
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 00:10:36 +00:00
|
|
|
plugins.shelljs.exec(`docker run --name ${containerName} ${imageTag}`);
|
2016-07-18 22:37:13 +00:00
|
|
|
return done.promise
|
|
|
|
};
|
|
|
|
|
|
|
|
let deleteDockerContainter = () => {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
return done.promise
|
|
|
|
};
|
|
|
|
|
|
|
|
let deleteDockerImage = () => {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
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(() => {
|
|
|
|
done.resolve(configArg);
|
|
|
|
})
|
2016-07-18 22:37:13 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|