UNPKG

@atomist/sdm-pack-docker

Version:

Extension Pack for an Atomist SDM to integrate Docker

139 lines 6.33 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const sdm_pack_fingerprints_1 = require("@atomist/sdm-pack-fingerprints"); const DockerFileParser_1 = require("../parse/DockerFileParser"); exports.DockerPathType = "docker-path"; exports.DockerPortsType = "docker-ports"; /** * Construct a Docker base image fingerprint from the given image and version * @param {string} image * @param {string} version * @return {FP} */ function createDockerBaseFingerprint(image, version, path) { const data = { image, version, path }; return { type: exports.DockerFrom.name, name: image, abbreviation: `dbi-${image}`, version: "0.0.1", data, sha: sdm_pack_fingerprints_1.sha256(JSON.stringify(data)), }; } exports.createDockerBaseFingerprint = createDockerBaseFingerprint; function parseDockerfile(p, f) { return __awaiter(this, void 0, void 0, function* () { const imageName = yield automation_client_1.astUtils.findValues(p, DockerFileParser_1.DockerFileParser, f.path, "//FROM/image/name"); const imageVersion = yield automation_client_1.astUtils.findValues(p, DockerFileParser_1.DockerFileParser, f.path, "//FROM/image/tag"); return createDockerBaseFingerprint(imageName[0], imageVersion[0] || "latest", f.path); }); } exports.parseDockerfile = parseDockerfile; exports.dockerBaseFingerprint = (p) => __awaiter(this, void 0, void 0, function* () { var e_1, _a; const fps = []; try { for (var _b = __asyncValues(automation_client_1.projectUtils.fileIterator(p, "**/Dockerfile", () => __awaiter(this, void 0, void 0, function* () { return true; }))), _c; _c = yield _b.next(), !_c.done;) { const f = _c.value; if (f && (yield f.getContent()) !== "") { fps.push(yield parseDockerfile(p, f)); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b); } finally { if (e_1) throw e_1.error; } } return fps; }); exports.applyDockerBaseFingerprint = (p, fp) => __awaiter(this, void 0, void 0, function* () { const newFP = fp.data; try { yield automation_client_1.astUtils.doWithAllMatches(p, DockerFileParser_1.DockerFileParser, fp.data.path, "//FROM/image/tag", n => n.$value = newFP.version); return (true); } catch (e) { automation_client_1.logger.error(e); return false; } }); exports.DockerFrom = { displayName: "Docker base image", name: "docker-base-image", apply: exports.applyDockerBaseFingerprint, extract: exports.dockerBaseFingerprint, toDisplayableFingerprint: fp => fp.data.version, }; function createDockerPortsFingerprint(data) { return { type: exports.DockerPortsType, name: exports.DockerPortsType, abbreviation: `dps`, version: "0.0.1", data, sha: sdm_pack_fingerprints_1.sha256(JSON.stringify(data)), }; } exports.extractDockerPortsFingerprint = (p) => __awaiter(this, void 0, void 0, function* () { const ports = yield automation_client_1.astUtils.findValues(p, DockerFileParser_1.DockerFileParser, "**/Dockerfile", "//EXPOSE/port"); return ports.length > 0 ? createDockerPortsFingerprint(ports) : undefined; }); exports.DockerPorts = { displayName: "Docker ports", name: exports.DockerPortsType, extract: exports.extractDockerPortsFingerprint, toDisplayableFingerprint: fp => fp.data.join(","), }; exports.extractDockerPathFingerprint = (p) => __awaiter(this, void 0, void 0, function* () { const paths = yield automation_client_1.projectUtils.gatherFromFiles(p, "**/Dockerfile", (f) => __awaiter(this, void 0, void 0, function* () { return f.path; })); return paths.length === 1 ? { type: exports.DockerPathType, name: exports.DockerPathType, abbreviation: "dpa", version: "0.0.1", data: paths[0], sha: sdm_pack_fingerprints_1.sha256(JSON.stringify(paths[0])), } : undefined; }); exports.DockerfilePath = { displayName: "Dockerfile path", name: exports.DockerPathType, extract: exports.extractDockerPathFingerprint, toDisplayableFingerprint: fp => fp.data, }; //# sourceMappingURL=docker.js.map