tsdocker/dist/npmdocker.docker.js

167 lines
13 KiB
JavaScript
Raw Normal View History

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-08-04 20:25:15 +00:00
containerName: "npmdocker-temp-container",
dockerProjectMountString: "",
dockerSockString: "",
dockerEnvString: ""
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...");
// 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);
}
2016-07-19 22:40:37 +00:00
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;
};
2016-08-04 20:25:15 +00:00
let buildDockerProjectMountString = () => {
2016-07-19 17:21:06 +00:00
let done = plugins.q.defer();
if (process.env.CI != "true") {
2016-08-04 20:25:15 +00:00
dockerData.dockerProjectMountString = `-v ${paths.cwd}:/workspace`;
}
;
2016-08-04 20:25:15 +00:00
done.resolve();
return done.promise;
};
let buildDockerEnvString = () => {
let done = plugins.q.defer();
for (let keyValueObjectArg of config.keyValueObjectArray) {
let envString = dockerData.dockerEnvString = dockerData.dockerEnvString + `-e ${keyValueObjectArg.key}=${keyValueObjectArg.value} `;
}
;
done.resolve();
return done.promise;
};
let buildDockerSockString = () => {
let done = plugins.q.defer();
2016-07-28 16:01:02 +00:00
if (config.dockerSock) {
2016-08-04 20:25:15 +00:00
dockerData.dockerSockString = `-v /var/run/docker.sock:/var/run/docker.sock`;
2016-07-28 16:01:02 +00:00
}
;
2016-08-04 20:25:15 +00:00
done.resolve();
return done;
};
/**
* 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();
plugins.beautylog.log("now running Dockerimage");
2016-08-04 20:25:15 +00:00
config.exitCode = plugins.shelljs.exec(`docker run ${dockerData.dockerProjectMountString} ${dockerData.dockerSockString} ${dockerData.dockerEnvString} --name ${dockerData.containerName} ${dockerData.imageTag}`).code;
2016-07-19 17:21:06 +00:00
done.resolve();
return done.promise;
};
let deleteDockerContainer = () => {
2016-07-19 17:21:06 +00:00
let done = plugins.q.defer();
plugins.shelljs.exec(`docker rm -f ${dockerData.containerName}`, {
2016-07-19 17:21:06 +00:00
silent: true
});
done.resolve();
return done.promise;
};
let deleteDockerImage = () => {
let done = plugins.q.defer();
plugins.shelljs.exec(`docker rmi ${dockerData.imageTag}`, {
silent: true
});
done.resolve();
return done.promise;
};
let deleteBuildContext = () => {
let done = plugins.q.defer();
plugins.smartfile.fs.remove(paths.buildContextDir)
.then(done.resolve);
2016-07-19 17:21:06 +00:00
return done.promise;
};
let preClean = () => {
let done = plugins.q.defer();
deleteDockerImage()
.then(deleteDockerContainer)
.then(() => {
plugins.beautylog.ok("ensured clean Docker environment!");
done.resolve();
});
};
let postClean = () => {
let done = plugins.q.defer();
deleteDockerContainer()
.then(deleteDockerImage)
.then(deleteBuildContext)
.then(() => {
plugins.beautylog.ok("cleaned up!");
done.resolve();
});
};
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(preClean)
2016-07-19 17:21:06 +00:00
.then(buildDockerFile)
.then(buildDockerImage)
2016-08-04 20:25:15 +00:00
.then(buildDockerProjectMountString)
.then(buildDockerEnvString)
.then(buildDockerSockString)
2016-07-19 17:21:06 +00:00
.then(runDockerImage)
.then(postClean)
2016-07-19 17:21:06 +00:00
.then(() => {
2016-07-19 22:40:37 +00:00
done.resolve(config);
2016-08-04 20:25:15 +00:00
}).catch(err => { console.log(err); });
2016-07-18 22:37:13 +00:00
return done.promise;
};
2016-08-04 20:36:00 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIj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