tsdocker/dist/npmdocker.docker.js

156 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, 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(() => __awaiter(this, void 0, void 0, function* () {
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.exec(`docker rmi ${dockerData.imageTag}`);
});
/**
* 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLCtDQUErQztBQUMvQywyQ0FBMkM7QUFDM0MsaURBQWdEO0FBS2hELElBQUksTUFBZSxDQUFBO0FBRW5COztHQUVHO0FBQ0gsSUFBSSxVQUFVLEdBQUc7SUFDZixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7SUFDekMsd0JBQXdCLEVBQUUsRUFBRTtJQUM1QixnQkFBZ0IsRUFBRSxFQUFFO0lBQ3BCLGVBQWUsRUFBRSxFQUFFO0NBQ3BCLENBQUE7QUFFRDs7R0FFRztBQUNILElBQUksV0FBVyxHQUFHO0lBQ2hCLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDNUIsT0FBTyxDQUFDLFNBQV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