tsdocker/dist/npmdocker.docker.js

186 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");
let config;
2017-02-11 19:23:10 +00:00
/**
* the docker data used to build the internal testing container
*/
2016-07-19 17:21:06 +00:00
let dockerData = {
2017-02-11 19:23:10 +00:00
imageTag: 'npmdocker-temp-image:latest',
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();
2017-02-11 19:23:10 +00:00
plugins.beautylog.ora.text('checking docker...');
if (plugins.shelljs.which('docker')) {
plugins.beautylog.ok('Docker found!');
2016-07-18 18:48:34 +00:00
done.resolve();
}
else {
2017-02-11 19:23:10 +00:00
done.reject(new Error('docker not found on this machine'));
2016-07-18 18:48:34 +00:00
}
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();
2017-02-11 19:23:10 +00:00
plugins.beautylog.ora.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);
2017-02-11 19:23:10 +00:00
plugins.beautylog.ok('Dockerfile created!');
2016-07-19 17:21:06 +00:00
done.resolve();
return done.promise;
};
/**
* builds the Dockerimage from the built Dockerfile
*/
let buildDockerImage = () => {
let done = plugins.q.defer();
2017-02-11 19:23:10 +00:00
plugins.beautylog.ora.text('pulling latest base image from registry...');
2016-07-19 17:21:06 +00:00
plugins.shelljs.exec(`docker pull ${config.baseImage}`, {
silent: true
}, () => {
2017-02-11 19:23:10 +00:00
plugins.beautylog.ora.text('building Dockerimage...');
// are we creating a build context form project ?
2017-02-11 19:23:10 +00:00
if (process.env.CI === 'true') {
plugins.beautylog.ora.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
}, () => {
2017-02-11 19:23:10 +00:00
plugins.beautylog.ok('Dockerimage built!');
2016-07-19 22:40:37 +00:00
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();
2017-02-11 19:23:10 +00:00
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;
};
2017-02-11 22:37:01 +00:00
/**
* builds an environment string that docker cli understands
*/
2016-08-04 20:25:15 +00:00
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;
};
2017-02-11 22:37:01 +00:00
/**
* creates string to mount the docker.sock inside the testcontainer
*/
2016-08-04 20:25:15 +00:00
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();
2017-02-11 19:23:10 +00:00
plugins.beautylog.ora.text('starting Container...');
plugins.beautylog.ora.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;
};
2017-02-11 22:37:01 +00:00
/**
* cleans up: deletes the test container
*/
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;
};
2017-02-11 22:37:01 +00:00
/**
* cleans up deletes the test image
*/
2016-07-19 17:21:06 +00:00
let deleteDockerImage = () => {
let done = plugins.q.defer();
plugins.shelljs.exec(`docker rmi ${dockerData.imageTag}`, {
silent: true
});
done.resolve();
return done.promise;
};
2017-02-11 22:37:01 +00:00
/**
* cleans up, deletes the build context
*/
let deleteBuildContext = () => {
let done = plugins.q.defer();
plugins.smartfile.fs.remove(paths.buildContextDir)
2017-02-11 19:23:10 +00:00
.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(() => {
2017-02-11 19:23:10 +00:00
plugins.beautylog.ok('ensured clean Docker environment!');
done.resolve();
});
};
let postClean = () => {
let done = plugins.q.defer();
deleteDockerContainer()
.then(deleteDockerImage)
.then(deleteBuildContext)
.then(() => {
2017-02-11 19:23:10 +00:00
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;
};
2017-02-11 22:37:01 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLCtDQUErQztBQUMvQywyQ0FBMkM7QUFDM0MsaURBQWdEO0FBS2hELElBQUksTUFBZSxDQUFBO0FBRW5COztHQUVHO0FBQ0gsSUFBSSxVQUFVLEdBQUc7SUFDZixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7SUFDekMsd0JBQXdCLEVBQUUsRUFBRTtJQUM1QixnQkFBZ0IsRUFBRSxFQUFFO0lBQ3BCLGVBQWUsRUFBRSxFQUFFO0NBQ3BCLENBQUE7QUFFRDs7R0FFRztBQUNILElBQUksV0FBVyxHQUFHO0lBQ2hCLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDNUIsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsSUFBSSxDQUFDLG9CQUFvQixDQUFDLENBQUE7SUFDaEQsRUFBRSxDQUFDLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUMsUUFBUSxDQUFDLENBQUMsQ0FBQyxDQUFDO1FBQ3BDLE9BQU8sQ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