UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

101 lines 4.77 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) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const configuration_1 = require("@atomist/automation-client/lib/configuration"); const sdm_1 = require("@atomist/sdm"); const fs = require("fs-extra"); const _ = require("lodash"); const os = require("os"); const path = require("path"); const resolvePlaceholder_1 = require("../../machine/yaml/resolvePlaceholder"); const container_1 = require("./container"); const provider_1 = require("./provider"); const util_1 = require("./util"); function execute(name, registration) { const uniqueName = name.replace(/ /g, "_"); const executeGoal = sdm_1.goal({ displayName: name, uniqueName }, sdm_1.doWithProject((gi) => __awaiter(this, void 0, void 0, function* () { const { goalEvent, project } = gi; // Resolve placeholders const registrationToUse = _.cloneDeep(registration); yield configuration_1.resolvePlaceholders(registrationToUse, value => resolvePlaceholder_1.resolvePlaceholder(value, gi.goalEvent, gi, gi.parameters)); const env = Object.assign({}, process.env); // Mount the secrets let secrets; if (!!registrationToUse.secrets) { secrets = yield provider_1.prepareSecrets({ secrets: registrationToUse.secrets }, gi); secrets.env.forEach(e => env[e.name] = e.value); for (const f of secrets.files) { yield fs.ensureDir(path.dirname(f.mountPath)); yield fs.writeFile(f.mountPath, f.value); } } const goalName = goalEvent.uniqueName.split("#")[0].toLowerCase(); const namePrefix = "sdm-"; const nameSuffix = `-${goalEvent.goalSetId.slice(0, 7)}-${goalName}`; const tmpDir = path.join(os.homedir(), ".atomist", "tmp", goalEvent.repo.owner, goalEvent.repo.name, goalEvent.goalSetId); const inputDir = path.join(tmpDir, `${namePrefix}tmp-${automation_client_1.guid()}${nameSuffix}`); const outputDir = path.join(tmpDir, `${namePrefix}tmp-${automation_client_1.guid()}${nameSuffix}`); yield util_1.prepareInputAndOutput(inputDir, outputDir, gi); (yield util_1.containerEnvVars(goalEvent, gi, project.baseDir, inputDir, outputDir)).forEach(e => env[e.name] = e.value); try { const result = yield gi.spawn(registrationToUse.cmd, registrationToUse.args, { env }); if (result.code === 0) { const outputFile = path.join(outputDir, "result.json"); if ((yield fs.pathExists(outputFile))) { return yield util_1.processResult(yield fs.readJson(outputFile), gi); } } else { return { code: result.code, }; } // TODO catch } finally { // Cleanup secrets; if (!!secrets) { for (const f of secrets.files) { yield fs.unlink(f.mountPath); } } // Delete tmpDir if (!!tmpDir) { yield fs.remove(tmpDir); } } return { code: 0, }; }), { readOnly: false, detachHead: true, }), { progressReporter: container_1.ContainerProgressReporter }); return executeGoal; } exports.execute = execute; //# sourceMappingURL=execute.js.map