UNPKG

9.19 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2019 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18 return new (P || (P = Promise))(function (resolve, reject) {
19 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
22 step((generator = generator.apply(thisArg, _arguments || [])).next());
23 });
24};
25Object.defineProperty(exports, "__esModule", { value: true });
26const automation_client_1 = require("@atomist/automation-client");
27const sdm_1 = require("@atomist/sdm");
28const _ = require("lodash");
29const CancelGoalOnCanceled_1 = require("../../handlers/events/delivery/goals/CancelGoalOnCanceled");
30const GoalAutomationEventListener_1 = require("../../handlers/events/delivery/goals/GoalAutomationEventListener");
31const CacheCleanupAutomationEventListener_1 = require("../../handlers/events/delivery/goals/k8s/CacheCleanupAutomationEventListener");
32const defaultSoftwareDeliveryMachineConfiguration_1 = require("../../machine/defaultSoftwareDeliveryMachineConfiguration");
33const array_1 = require("../../util/misc/array");
34const goalSigning_1 = require("../signing/goalSigning");
35const SdmGoalMetricReportingAutomationEventListener_1 = require("../util/SdmGoalMetricReportingAutomationEventListener");
36const startupMessage_1 = require("../util/startupMessage");
37const InvokeSdmStartupListenersAutomationEventListener_1 = require("./InvokeSdmStartupListenersAutomationEventListener");
38const modes_1 = require("./modes");
39/**
40 * Configure and set up a Software Delivery Machine instance with the automation-client framework for standalone
41 * or single goal based execution
42 * @param {(configuration: (Configuration & SoftwareDeliveryMachineOptions)) => SoftwareDeliveryMachine} machineMaker
43 * @param {ConfigureOptions} options
44 * @returns {ConfigurationPostProcessor}
45 */
46function configureSdm(machineMaker, options = {}) {
47 return (config) => __awaiter(this, void 0, void 0, function* () {
48 let mergedConfig = config;
49 // Configure the local SDM
50 mergedConfig = (yield doWithSdmLocal(local => {
51 return local.configureLocal()(mergedConfig);
52 })) || mergedConfig;
53 const defaultSdmConfiguration = defaultSoftwareDeliveryMachineConfiguration_1.defaultSoftwareDeliveryMachineConfiguration(config);
54 mergedConfig = _.merge(defaultSdmConfiguration, mergedConfig);
55 sdm_1.validateConfigurationValues(mergedConfig, options);
56 const sdm = yield machineMaker(mergedConfig);
57 yield doWithSdmLocal(local => sdm.addExtensionPacks(local.LocalLifecycle, local.LocalSdmConfig));
58 // Configure the job forking ability
59 yield configureJobLaunching(mergedConfig, sdm);
60 configureGoalSigning(mergedConfig);
61 yield registerMetadata(mergedConfig, sdm);
62 // Register startup message detail
63 _.update(mergedConfig, "logging.banner.contributors", old => !!old ? old : []);
64 mergedConfig.logging.banner.contributors.push(startupMessage_1.sdmStartupMessage(sdm), startupMessage_1.sdmExtensionPackStartupMessage(sdm));
65 _.update(mergedConfig, "listeners", old => !!old ? old : []);
66 mergedConfig.listeners.push(new InvokeSdmStartupListenersAutomationEventListener_1.InvokeSdmStartupListenersAutomationEventListener(sdm), new SdmGoalMetricReportingAutomationEventListener_1.SdmGoalMetricReportingAutomationEventListener());
67 return mergedConfig;
68 });
69}
70exports.configureSdm = configureSdm;
71/**
72 * Configure how this SDM is going to handle goals
73 * @param mergedConfig
74 * @param machine
75 */
76function configureJobLaunching(mergedConfig, machine) {
77 return __awaiter(this, void 0, void 0, function* () {
78 const forked = process.env.ATOMIST_ISOLATED_GOAL === "true";
79 if (forked) {
80 configureSdmToRunExactlyOneGoal(mergedConfig, machine);
81 }
82 else {
83 // initialize the GoalSchedulers
84 for (const goalScheduler of array_1.toArray(mergedConfig.sdm.goalScheduler || [])) {
85 if (!!goalScheduler.initialize) {
86 yield goalScheduler.initialize(mergedConfig);
87 }
88 }
89 _.update(mergedConfig, "commands", old => !!old ? old : []);
90 mergedConfig.commands.push(...machine.commandHandlers);
91 _.update(mergedConfig, "events", old => !!old ? old : []);
92 mergedConfig.events.push(...machine.eventHandlers);
93 _.update(mergedConfig, "ingesters", old => !!old ? old : []);
94 mergedConfig.ingesters.push(...machine.ingesters);
95 }
96 });
97}
98/**
99 * Configure SDM to run only one goal
100 * @param mergedConfig
101 * @param machine
102 */
103function configureSdmToRunExactlyOneGoal(mergedConfig, machine) {
104 if (process.env.ATOMIST_JOB_NAME) {
105 mergedConfig.name = process.env.ATOMIST_REGISTRATION_NAME;
106 }
107 else {
108 mergedConfig.name = `${mergedConfig.name}-${process.env.ATOMIST_GOAL_ID || automation_client_1.guid()}`;
109 }
110 // Force ephemeral policy and no handlers or ingesters
111 mergedConfig.policy = "ephemeral";
112 mergedConfig.commands = [];
113 mergedConfig.events = [() => new CancelGoalOnCanceled_1.CancelGoalOnCanceled()];
114 mergedConfig.ingesters = [];
115 mergedConfig.listeners.push(new GoalAutomationEventListener_1.GoalAutomationEventListener(machine), new CacheCleanupAutomationEventListener_1.CacheCleanupAutomationEventListener(machine));
116 // Disable app events for forked clients
117 mergedConfig.applicationEvents.enabled = false;
118 // Set workers to one but leave enablement unset so that it can be defined elsewhere.
119 mergedConfig.cluster.workers = 1;
120}
121/**
122 * Configure SDM to sign and verify goals
123 * @param mergedConfig
124 */
125function configureGoalSigning(mergedConfig) {
126 if (!!mergedConfig.sdm.goalSigning && mergedConfig.sdm.goalSigning.enabled === true) {
127 mergedConfig.listeners.push(new goalSigning_1.GoalSigningAutomationEventListener(mergedConfig.sdm.goalSigning));
128 }
129}
130function registerMetadata(config, machine) {
131 return __awaiter(this, void 0, void 0, function* () {
132 // tslint:disable-next-line:no-implicit-dependencies
133 const sdmPj = require("@atomist/sdm/package.json");
134 // tslint:disable-next-line:no-implicit-dependencies
135 const sdmCorePj = require("@atomist/sdm-core/package.json");
136 config.metadata = Object.assign({}, config.metadata, { "atomist.sdm": `${sdmPj.name}:${sdmPj.version}`, "atomist.sdm-core": `${sdmCorePj.name}:${sdmCorePj.version}`, "atomist.sdm.name": machine.name, "atomist.sdm.extension-packs": machine.extensionPacks.map(ex => `${ex.name}:${ex.version}`).join(", ") });
137 config.sdm.name = machine.name;
138 yield doWithSdmLocal(() => {
139 // tslint:disable-next-line:no-implicit-dependencies
140 const sdmLocalPj = require("@atomist/sdm-local/package.json");
141 config.metadata["atomist.sdm-local"] = `${sdmLocalPj.name}:${sdmLocalPj.version}`;
142 });
143 });
144}
145/**
146 * Perform the given operation with the sdm-local module if it's available.
147 * If it isn't, silently continue without error.
148 * @param {(sdmLocal: any) => any} callback
149 * @return {any}
150 */
151function doWithSdmLocal(callback) {
152 return __awaiter(this, void 0, void 0, function* () {
153 if (modes_1.isInLocalMode() || modes_1.isGitHubAction()) {
154 // tslint:disable-next-line:no-implicit-dependencies
155 const local = attemptToRequire("@atomist/sdm-local", !process.env.ATOMIST_NPM_LOCAL_LINK);
156 if (local) {
157 return callback(local);
158 }
159 else {
160 automation_client_1.logger.warn("Skipping local mode configuration because 'ATOMIST_NPM_LOCAL_LINK' was defined, " +
161 "but '@atomist/sdm-local' could not be loaded");
162 return undefined;
163 }
164 }
165 });
166}
167/**
168 * Attempt to NPM require module
169 * @param module
170 * @param failOnError
171 */
172function attemptToRequire(module, failOnError) {
173 try {
174 return require(module);
175 }
176 catch (err) {
177 if (failOnError) {
178 throw new Error(`Unable to load '${module}'. Please install with 'npm install ${module}'.`);
179 }
180 else {
181 return undefined;
182 }
183 }
184}
185//# sourceMappingURL=configureSdm.js.map
\No newline at end of file