npmci/dist/mod_docker/mod.classes.dockerfile.js

97 lines
8.0 KiB
JavaScript
Raw Normal View History

2017-08-27 23:03:59 +00:00
"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("./mod.plugins");
const NpmciEnv = require("../npmci.env");
const npmci_bash_1 = require("../npmci.bash");
const paths = require("../npmci.paths");
const helpers = require("./mod.helpers");
/**
* class Dockerfile represents a Dockerfile on disk in npmci
*/
class Dockerfile {
constructor(options) {
this.filePath = options.filePath;
this.repo = NpmciEnv.repo.user + '/' + NpmciEnv.repo.repo;
this.version = helpers.dockerFileVersion(plugins.path.parse(options.filePath).base);
this.cleanTag = this.repo + ':' + this.version;
this.buildTag = this.cleanTag;
this.containerName = 'dockerfile-' + this.version;
if (options.filePath && options.read) {
this.content = plugins.smartfile.fs.toStringSync(plugins.path.resolve(options.filePath));
}
this.baseImage = helpers.dockerBaseImage(this.content);
this.localBaseImageDependent = false;
}
/**
* builds the Dockerfile
*/
build() {
return __awaiter(this, void 0, void 0, function* () {
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag);
2017-08-28 17:11:24 +00:00
let buildArgsString = yield helpers.getDockerBuildArgs();
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
2017-08-27 23:03:59 +00:00
yield npmci_bash_1.bash(buildCommand);
return;
});
}
/**
* pushes the Dockerfile to a registry
*/
push(dockerRegistryArg, versionSuffix = null) {
return __awaiter(this, void 0, void 0, function* () {
let pushTag = helpers.getDockerTagString(dockerRegistryArg.registryUrl, this.repo, this.version, versionSuffix);
yield npmci_bash_1.bash(`docker tag ${this.buildTag} ${pushTag}`);
yield npmci_bash_1.bash(`docker push ${pushTag}`);
});
}
/**
* pulls the Dockerfile from a registry
*/
pull(registryArg, versionSuffixArg = null) {
return __awaiter(this, void 0, void 0, function* () {
let pullTag = helpers.getDockerTagString(registryArg.registryUrl, this.repo, this.version, versionSuffixArg);
2017-08-28 00:43:43 +00:00
yield npmci_bash_1.bash(`docker pull ${pullTag}`);
2017-08-27 23:03:59 +00:00
yield npmci_bash_1.bash(`docker tag ${pullTag} ${this.buildTag}`);
});
}
/**
* tests the Dockerfile;
*/
test() {
return __awaiter(this, void 0, void 0, function* () {
let testFile = plugins.path.join(paths.NpmciTestDir, 'test_' + this.version + '.sh');
let testFileExists = plugins.smartfile.fs.fileExistsSync(testFile);
if (testFileExists) {
// run tests
2017-10-11 16:14:05 +00:00
yield npmci_bash_1.bash(`docker run --name npmci_test_container --entrypoint="bash" ${this.buildTag} -c "mkdir /npmci_test"`);
yield npmci_bash_1.bash(`docker cp ${testFile} npmci_test_container:/npmci_test/test.sh`);
yield npmci_bash_1.bash(`docker commit npmci_test_container npmci_test_image`);
2017-10-11 16:15:22 +00:00
yield npmci_bash_1.bash(`docker run --entrypoint="bash" npmci_test_image -x /npmci_test/test.sh`);
yield npmci_bash_1.bash(`docker rm npmci_test_container`);
yield npmci_bash_1.bash(`docker rmi --force npmci_test_image`);
2017-08-27 23:03:59 +00:00
}
else {
plugins.beautylog.warn('skipping tests for ' + this.cleanTag + ' because no testfile was found!');
}
});
}
/**
* gets the id of a Dockerfile
*/
getId() {
return __awaiter(this, void 0, void 0, function* () {
let containerId = yield npmci_bash_1.bash('docker inspect --type=image --format=\"{{.Id}}\" ' + this.buildTag);
return containerId;
});
}
}
exports.Dockerfile = Dockerfile;
2017-10-11 16:14:05 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kLmNsYXNzZXMuZG9ja2VyZmlsZS5qcyIsIn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