tsdocker/dist/npmdocker.docker.js

152 lines
12 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, plugins.path.join(paths.cwd, 'npmdocker'));
plugins.beautylog.ok('Dockerfile created!');
plugins.beautylog.ora.stop();
done.resolve();
return done.promise;
};
/**
* builds the Dockerimage from the built Dockerfile
*/
let buildDockerImage = () => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.info('pulling latest base image from registry...');
yield plugins.smartshell.exec(`docker pull ${config.baseImage}`);
plugins.beautylog.ora.text('building Dockerimage...');
let execResult = yield plugins.smartshell.execSilent(`docker build -f npmdocker -t ${dockerData.imageTag} ${paths.cwd}`);
if (execResult.exitCode !== 0) {
console.log(execResult.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);
}
}));
});
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(() => __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.ok('cleaned up!');
}));
plugins.smartfile.fs.removeSync(paths.npmdockerFile);
});
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLCtDQUErQztBQUMvQywyQ0FBMkM7QUFDM0MsaURBQWlEO0FBS2pELElBQUksTUFBZSxDQUFDO0FBRXBCOztHQUVHO0FBQ0gsSUFBSSxVQUFVLEdBQUc7SUFDZixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7SUFDekMsd0JBQXdCLEVBQUUsRUFBRTtJQUM1QixnQkFBZ0IsRUFBRSxFQUFFO0lBQ3BCLGVBQWUsRUFBRSxFQUFFO0NBQ3BCLENBQUM7QUFFRjs7R0FFRztBQUNILElBQUksV0FBVyxHQUFHLEdBQUcsRUFBRTtJQUNyQixJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQzdCLE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxvQkFBb0IsQ0FBQyxDQUFDO0lBQ2pELElBQUks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