tsdocker/dist/npmdocker.docker.js
2017-04-02 14:48:23 +02:00

164 lines
13 KiB
JavaScript

"use strict";
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 });
const plugins = require("./npmdocker.plugins");
const paths = require("./npmdocker.paths");
const snippets = require("./npmdocker.snippets");
let config;
/**
* the docker data used to build the internal testing container
*/
let dockerData = {
imageTag: 'npmdocker-temp-image:latest',
containerName: 'npmdocker-temp-container',
dockerProjectMountString: '',
dockerSockString: '',
dockerEnvString: ''
};
/**
* check if docker is available
*/
let checkDocker = () => {
let done = plugins.q.defer();
plugins.beautylog.ora.text('checking docker...');
if (plugins.smartshell.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();
plugins.beautylog.ora.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 = () => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.ora.text('pulling latest base image from registry...');
yield plugins.smartshell.execSilent(`docker pull ${config.baseImage}`).then(() => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.ora.text('building Dockerimage...');
// are we creating a build context form project ?
if (process.env.CI === 'true') {
plugins.beautylog.ora.text('creating build context...');
plugins.smartfile.fs.copySync(paths.cwd, paths.buildContextDir);
}
yield plugins.smartshell.execSilent(`docker build -f ${paths.dockerfile} -t ${dockerData.imageTag} ${paths.assets}`).then((response) => __awaiter(this, void 0, void 0, function* () {
if (response.exitCode !== 0) {
console.log(response.stdout);
process.exit(1);
}
plugins.beautylog.ok('Dockerimage built!');
}));
}));
});
let buildDockerProjectMountString = () => __awaiter(this, void 0, void 0, function* () {
if (process.env.CI !== 'true') {
dockerData.dockerProjectMountString = `-v ${paths.cwd}:/workspace`;
}
;
});
/**
* builds an environment string that docker cli understands
*/
let buildDockerEnvString = () => __awaiter(this, void 0, void 0, function* () {
for (let keyValueObjectArg of config.keyValueObjectArray) {
let envString = dockerData.dockerEnvString = dockerData.dockerEnvString + `-e ${keyValueObjectArg.key}=${keyValueObjectArg.value} `;
}
;
});
/**
* creates string to mount the docker.sock inside the testcontainer
*/
let buildDockerSockString = () => __awaiter(this, void 0, void 0, function* () {
if (config.dockerSock) {
dockerData.dockerSockString = `-v /var/run/docker.sock:/var/run/docker.sock`;
}
;
});
/**
* creates a container by running the built Dockerimage
*/
let runDockerImage = () => __awaiter(this, void 0, void 0, function* () {
let done = plugins.q.defer();
plugins.beautylog.ora.text('starting Container...');
plugins.beautylog.ora.end();
plugins.beautylog.log('now running Dockerimage');
config.exitCode = (yield plugins.smartshell.exec(`docker run ${dockerData.dockerProjectMountString} ${dockerData.dockerSockString} ${dockerData.dockerEnvString} --name ${dockerData.containerName} ${dockerData.imageTag}`)).exitCode;
});
/**
* cleans up: deletes the test container
*/
let deleteDockerContainer = () => __awaiter(this, void 0, void 0, function* () {
yield plugins.smartshell.execSilent(`docker rm -f ${dockerData.containerName}`);
});
/**
* cleans up deletes the test image
*/
let deleteDockerImage = () => __awaiter(this, void 0, void 0, function* () {
yield plugins.smartshell.execSilent(`docker rmi ${dockerData.imageTag}`).then((response) => __awaiter(this, void 0, void 0, function* () {
if (response.exitCode !== 0) {
console.log(response.stdout);
}
}));
});
/**
* cleans up, deletes the build context
*/
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)
.then(() => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.ok('ensured clean Docker environment!');
}));
});
let postClean = () => __awaiter(this, void 0, void 0, function* () {
yield deleteDockerContainer()
.then(deleteDockerImage)
.then(deleteBuildContext)
.then(() => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.ok('cleaned up!');
}));
});
exports.run = (configArg) => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.ora.start();
config = configArg;
let resultConfig = yield checkDocker()
.then(preClean)
.then(buildDockerFile)
.then(buildDockerImage)
.then(buildDockerProjectMountString)
.then(buildDockerEnvString)
.then(buildDockerSockString)
.then(runDockerImage)
.then(postClean)
.catch(err => { console.log(err); });
return config;
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLCtDQUErQztBQUMvQywyQ0FBMkM7QUFDM0MsaURBQWdEO0FBS2hELElBQUksTUFBZSxDQUFBO0FBRW5COztHQUVHO0FBQ0gsSUFBSSxVQUFVLEdBQUc7SUFDZixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7SUFDekMsd0JBQXdCLEVBQUUsRUFBRTtJQUM1QixnQkFBZ0IsRUFBRSxFQUFFO0lBQ3BCLGVBQWUsRUFBRSxFQUFFO0NBQ3BCLENBQUE7QUFFRDs7R0FFRztBQUNILElBQU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