2016-07-18 18:48:34 +00:00
|
|
|
"use strict";
|
|
|
|
const plugins = require("./npmdocker.plugins");
|
2016-07-19 17:21:06 +00:00
|
|
|
const paths = require("./npmdocker.paths");
|
|
|
|
const snippets = require("./npmdocker.snippets");
|
2016-07-19 22:40:37 +00:00
|
|
|
const npmdocker_promisechain_1 = require("./npmdocker.promisechain");
|
2016-07-19 17:21:06 +00:00
|
|
|
let config;
|
|
|
|
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-18 18:48:34 +00:00
|
|
|
/**
|
|
|
|
* check if docker is available
|
|
|
|
*/
|
2016-07-19 17:21:06 +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
|
|
|
npmdocker_promisechain_1.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 18:48:34 +00:00
|
|
|
done.resolve();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
done.reject(new Error("docker not found on this machine"));
|
|
|
|
}
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-07-19 17:21:06 +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
|
|
|
npmdocker_promisechain_1.npmdockerOra.text("building Dockerfile...");
|
2016-07-19 17:21:06 +00:00
|
|
|
let dockerfile = 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();
|
2016-07-19 22:40:37 +00:00
|
|
|
npmdocker_promisechain_1.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
|
|
|
npmdocker_promisechain_1.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-19 17:21:06 +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
|
|
|
npmdocker_promisechain_1.npmdockerOra.text("starting Container...");
|
|
|
|
npmdocker_promisechain_1.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;
|
|
|
|
};
|
|
|
|
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;
|
|
|
|
};
|
2016-07-18 22:37:13 +00:00
|
|
|
exports.run = (configArg) => {
|
|
|
|
let done = plugins.q.defer();
|
2016-07-19 17:21:06 +00:00
|
|
|
config = configArg;
|
|
|
|
checkDocker()
|
|
|
|
.then(buildDockerFile)
|
|
|
|
.then(buildDockerImage)
|
|
|
|
.then(runDockerImage)
|
|
|
|
.then(deleteDockerContainter)
|
|
|
|
.then(deleteDockerImage)
|
|
|
|
.then(() => {
|
2016-07-19 22:40:37 +00:00
|
|
|
done.resolve(config);
|
2016-07-19 17:21:06 +00:00
|
|
|
});
|
2016-07-18 22:37:13 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-07-19 22:40:37 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHFCQUFxQixDQUFDLENBQUE7QUFDL0MsTUFBWSxLQUFLLFdBQU0sbUJBQW1CLENBQUMsQ0FBQTtBQUMzQyxNQUFZLFFBQVEsV0FBTSxzQkFBc0IsQ0FBQyxDQUFBO0FBRWpELHlDQUEyQiwwQkFBMEIsQ0F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