130 lines
9.9 KiB
JavaScript
130 lines
9.9 KiB
JavaScript
"use strict";
|
|
const plugins = require("./npmdocker.plugins");
|
|
const paths = require("./npmdocker.paths");
|
|
const snippets = require("./npmdocker.snippets");
|
|
const npmdocker_promisechain_1 = require("./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();
|
|
npmdocker_promisechain_1.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();
|
|
npmdocker_promisechain_1.npmdockerOra.text("building Dockerfile...");
|
|
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();
|
|
npmdocker_promisechain_1.npmdockerOra.text("pulling latest base image from registry...");
|
|
plugins.shelljs.exec(`docker pull ${config.baseImage}`, {
|
|
silent: true
|
|
}, () => {
|
|
npmdocker_promisechain_1.npmdockerOra.text("building Dockerimage...");
|
|
// are we creating a build context form project ?
|
|
if (process.env.CI == "true") {
|
|
npmdocker_promisechain_1.npmdockerOra.text("creating build context...");
|
|
plugins.smartfile.fs.copySync(paths.cwd, paths.buildContextDir);
|
|
}
|
|
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();
|
|
npmdocker_promisechain_1.npmdockerOra.text("starting Container...");
|
|
npmdocker_promisechain_1.npmdockerOra.end();
|
|
// Are we mounting the project directory?
|
|
let dockerProjectMountString = "";
|
|
if (process.env.CI != "true") {
|
|
dockerProjectMountString = `-v ${paths.cwd}:/workspace`;
|
|
}
|
|
;
|
|
// Are we mounting docker.sock?
|
|
let dockerSockString = "";
|
|
if (config.dockerSock) {
|
|
dockerSockString = `-v /var/run/docker.sock:/var/run/docker.sock`;
|
|
}
|
|
;
|
|
config.exitCode = plugins.shelljs.exec(`docker run ${dockerProjectMountString} ${dockerSockString} --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
|
|
});
|
|
plugins.beautylog.ok("removed test image!");
|
|
plugins.beautylog.ok("Cleaned up!");
|
|
done.resolve();
|
|
return done.promise;
|
|
};
|
|
let deleteBuildContext = () => {
|
|
let done = plugins.q.defer();
|
|
plugins.smartfile.fs.removeSync(paths.buildContextDir);
|
|
done.resolve();
|
|
return done.promise;
|
|
};
|
|
exports.run = (configArg) => {
|
|
let done = plugins.q.defer();
|
|
config = configArg;
|
|
checkDocker()
|
|
.then(buildDockerFile)
|
|
.then(buildDockerImage)
|
|
.then(runDockerImage)
|
|
.then(deleteDockerContainter)
|
|
.then(deleteDockerImage)
|
|
.then(deleteBuildContext)
|
|
.then(() => {
|
|
done.resolve(config);
|
|
});
|
|
return done.promise;
|
|
};
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHFCQUFxQixDQUFDLENBQUE7QUFDL0MsTUFBWSxLQUFLLFdBQU0sbUJBQW1CLENBQUMsQ0FBQTtBQUMzQyxNQUFZLFFBQVEsV0FBTSxzQkFBc0IsQ0FBQyxDQUFBO0FBRWpELHlDQUEyQiwwQkFBMEIsQ0FBQyxDQUFBO0FBRXRELElBQUksTUFBTSxDQUFDO0FBQ1gsSUFBSSxVQUFVLEdBQUc7SUFDYixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7Q0FDNUMsQ0FBQztBQUVGOztHQUVHO0FBQ0gsSUFBSSxXQUFXLEdBQUc7SUFDZCxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQzdCLHFDQUFZLENBQUMsSUFBSSxDQUFDLG9CQUFvQixDQUFDLENBQUM7SUFDeEMsRUFBRSxDQUFBLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUMsUUFBUSxDQUFDLENBQUMsQ0FBQSxDQUFDO1FBQ2hDLE9BQU8sQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLGVBQWUsQ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