tsdocker/dist/npmdocker.docker.js

156 lines
12 KiB
JavaScript
Raw Normal View History

2016-07-18 18:48:34 +00:00
"use strict";
2017-03-28 23:01:37 +00:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
2016-07-18 18:48:34 +00:00
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...');
2017-03-28 23:01:37 +00:00
if (plugins.smartshell.which('docker')) {
2017-02-11 19:23:10 +00:00
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
*/
2017-03-28 23:01:37 +00:00
let buildDockerImage = () => __awaiter(this, void 0, void 0, function* () {
2017-02-11 19:23:10 +00:00
plugins.beautylog.ora.text('pulling latest base image from registry...');
2017-03-28 23:01:37 +00:00
yield plugins.smartshell.execSilent(`docker pull ${config.baseImage}`).then(() => __awaiter(this, void 0, void 0, function* () {
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);
}
2017-03-28 23:01:37 +00:00
yield plugins.smartshell.execSilent(`docker build -f ${paths.dockerfile} -t ${dockerData.imageTag} ${paths.assets}`).then(() => __awaiter(this, void 0, void 0, function* () {
2017-02-11 19:23:10 +00:00
plugins.beautylog.ok('Dockerimage built!');
2017-03-28 23:01:37 +00:00
}));
}));
});
let buildDockerProjectMountString = () => __awaiter(this, void 0, void 0, function* () {
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`;
}
;
2017-03-28 23:01:37 +00:00
});
2017-02-11 22:37:01 +00:00
/**
* builds an environment string that docker cli understands
*/
2017-03-28 23:01:37 +00:00
let buildDockerEnvString = () => __awaiter(this, void 0, void 0, function* () {
2016-08-04 20:25:15 +00:00
for (let keyValueObjectArg of config.keyValueObjectArray) {
let envString = dockerData.dockerEnvString = dockerData.dockerEnvString + `-e ${keyValueObjectArg.key}=${keyValueObjectArg.value} `;
}
;
2017-03-28 23:01:37 +00:00
});
2017-02-11 22:37:01 +00:00
/**
* creates string to mount the docker.sock inside the testcontainer
*/
2017-03-28 23:01:37 +00:00
let buildDockerSockString = () => __awaiter(this, void 0, void 0, function* () {
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
}
;
2017-03-28 23:01:37 +00:00
});
2016-08-04 20:25:15 +00:00
/**
* creates a container by running the built Dockerimage
*/
2017-03-28 23:01:37 +00:00
let runDockerImage = () => __awaiter(this, void 0, void 0, function* () {
2016-08-04 20:25:15 +00:00
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');
2017-03-28 23:01:37 +00:00
config.exitCode = (yield plugins.smartshell.exec(`docker run ${dockerData.dockerProjectMountString} ${dockerData.dockerSockString} ${dockerData.dockerEnvString} --name ${dockerData.containerName} ${dockerData.imageTag}`)).exitCode;
});
2017-02-11 22:37:01 +00:00
/**
* cleans up: deletes the test container
*/
2017-03-28 23:01:37 +00:00
let deleteDockerContainer = () => __awaiter(this, void 0, void 0, function* () {
yield plugins.smartshell.execSilent(`docker rm -f ${dockerData.containerName}`);
});
2017-02-11 22:37:01 +00:00
/**
* cleans up deletes the test image
*/
2017-03-28 23:01:37 +00:00
let deleteDockerImage = () => __awaiter(this, void 0, void 0, function* () {
yield plugins.smartshell.exec(`docker rmi ${dockerData.imageTag}`);
});
2017-02-11 22:37:01 +00:00
/**
* cleans up, deletes the build context
*/
2017-03-28 23:01:37 +00:00
let deleteBuildContext = () => __awaiter(this, void 0, void 0, function* () {
yield plugins.smartfile.fs.remove(paths.buildContextDir);
});
let preClean = () => __awaiter(this, void 0, void 0, function* () {
yield deleteDockerImage()
.then(deleteDockerContainer)
2017-03-28 23:01:37 +00:00
.then(() => __awaiter(this, void 0, void 0, function* () {
2017-02-11 19:23:10 +00:00
plugins.beautylog.ok('ensured clean Docker environment!');
2017-03-28 23:01:37 +00:00
}));
});
let postClean = () => __awaiter(this, void 0, void 0, function* () {
yield deleteDockerContainer()
.then(deleteDockerImage)
.then(deleteBuildContext)
2017-03-28 23:01:37 +00:00
.then(() => __awaiter(this, void 0, void 0, function* () {
2017-02-11 19:23:10 +00:00
plugins.beautylog.ok('cleaned up!');
2017-03-28 23:01:37 +00:00
}));
});
exports.run = (configArg) => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.ora.start();
2016-07-19 17:21:06 +00:00
config = configArg;
2017-03-28 23:01:37 +00:00
let resultConfig = yield 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)
2017-03-28 23:01:37 +00:00
.catch(err => { console.log(err); });
return config;
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLCtDQUErQztBQUMvQywyQ0FBMkM7QUFDM0MsaURBQWdEO0FBS2hELElBQUksTUFBZSxDQUFBO0FBRW5COztHQUVHO0FBQ0gsSUFBSSxVQUFVLEdBQUc7SUFDZixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7SUFDekMsd0JBQXdCLEVBQUUsRUFBRTtJQUM1QixnQkFBZ0IsRUFBRSxFQUFFO0lBQ3B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