UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

132 lines 4.86 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 fs = require("fs-extra"); const projectVersioner_1 = require("../../internal/delivery/build/local/projectVersioner"); const KubernetesGoalScheduler_1 = require("../../pack/k8s/KubernetesGoalScheduler"); /** * Simple test to see if SDM is running in Kubernetes. It is called * from a non-async function, so it must be non-async. * * @return `true` if process is running in Kubernetes, `false` otherwise. */ function runningInK8s() { return fs.pathExistsSync(KubernetesGoalScheduler_1.K8sNamespaceFile); } exports.runningInK8s = runningInK8s; /** * Return environment variables required by the container goal * execution machinery. * * @param goalEvent SDM goal event being executed as a container goal * @param ctx SDM context for goal execution * @return SDM goal environment variables */ function containerEnvVars(goalEvent, ctx) { return __awaiter(this, void 0, void 0, function* () { const version = yield projectVersioner_1.getGoalVersion({ owner: goalEvent.repo.owner, repo: goalEvent.repo.name, providerId: goalEvent.repo.providerId, sha: goalEvent.sha, branch: goalEvent.branch, context: ctx.context, }); return [{ name: "ATOMIST_SLUG", value: `${goalEvent.repo.owner}/${goalEvent.repo.name}`, }, { name: "ATOMIST_OWNER", value: goalEvent.repo.owner, }, { name: "ATOMIST_REPO", value: goalEvent.repo.name, }, { name: "ATOMIST_SHA", value: goalEvent.sha, }, { name: "ATOMIST_BRANCH", value: goalEvent.branch, }, { name: "ATOMIST_VERSION", value: version, }, { name: "ATOMIST_GOAL_SET_ID", value: goalEvent.goalSetId, }, { name: "ATOMIST_GOAL", value: goalEvent.uniqueName, }].filter(e => !!e.value); }); } exports.containerEnvVars = containerEnvVars; /** * Copy cloned project to location that can be mounted into container. * It ensures the destination direction exists and is empty. If it * fails it throws an error and tries to ensure the destination * directory does not exist. * * @param src Location of project directory * @param dest Location to copy project to */ function copyProject(src, dest) { return __awaiter(this, void 0, void 0, function* () { try { yield fs.emptyDir(dest); } catch (e) { e.message = `Failed to empty directory '${dest}'`; throw e; } try { yield fs.copy(src, dest); } catch (e) { e.message = `Failed to copy project from '${src}' to '${dest}'`; try { yield fs.remove(dest); } catch (err) { e.message += `; Failed to clean up '${dest}': ${err.message}`; } throw e; } }); } exports.copyProject = copyProject; /** * Write to client and progress logs. Add newline to progress log. * * @param msg Message to write, should not have newline at end * @param l Logger method, e.g., `logger.warn` * @param p Progress log */ function loglog(msg, l, p) { l(msg); p.write(msg + "\n"); } exports.loglog = loglog; //# sourceMappingURL=util.js.map