151 lines
12 KiB
JavaScript
151 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!');
|
|
}));
|
|
yield plugins.smartfile.fs.remove(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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiO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