1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | const automation_client_1 = require("@atomist/automation-client");
|
27 | const sdm_1 = require("@atomist/sdm");
|
28 | const _ = require("lodash");
|
29 | const CancelGoalOnCanceled_1 = require("../../handlers/events/delivery/goals/CancelGoalOnCanceled");
|
30 | const GoalAutomationEventListener_1 = require("../../handlers/events/delivery/goals/GoalAutomationEventListener");
|
31 | const CacheCleanupAutomationEventListener_1 = require("../../handlers/events/delivery/goals/k8s/CacheCleanupAutomationEventListener");
|
32 | const defaultSoftwareDeliveryMachineConfiguration_1 = require("../../machine/defaultSoftwareDeliveryMachineConfiguration");
|
33 | const array_1 = require("../../util/misc/array");
|
34 | const goalSigning_1 = require("../signing/goalSigning");
|
35 | const SdmGoalMetricReportingAutomationEventListener_1 = require("../util/SdmGoalMetricReportingAutomationEventListener");
|
36 | const startupMessage_1 = require("../util/startupMessage");
|
37 | const InvokeSdmStartupListenersAutomationEventListener_1 = require("./InvokeSdmStartupListenersAutomationEventListener");
|
38 | const modes_1 = require("./modes");
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function configureSdm(machineMaker, options = {}) {
|
47 | return (config) => __awaiter(this, void 0, void 0, function* () {
|
48 | let mergedConfig = config;
|
49 |
|
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 |
|
59 | yield configureJobLaunching(mergedConfig, sdm);
|
60 | configureGoalSigning(mergedConfig);
|
61 | yield registerMetadata(mergedConfig, sdm);
|
62 |
|
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 | }
|
70 | exports.configureSdm = configureSdm;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function 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 |
|
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 |
|
100 |
|
101 |
|
102 |
|
103 | function 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 |
|
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 |
|
117 | mergedConfig.applicationEvents.enabled = false;
|
118 |
|
119 | mergedConfig.cluster.workers = 1;
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function 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 | }
|
130 | function registerMetadata(config, machine) {
|
131 | return __awaiter(this, void 0, void 0, function* () {
|
132 |
|
133 | const sdmPj = require("@atomist/sdm/package.json");
|
134 |
|
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 |
|
140 | const sdmLocalPj = require("@atomist/sdm-local/package.json");
|
141 | config.metadata["atomist.sdm-local"] = `${sdmLocalPj.name}:${sdmLocalPj.version}`;
|
142 | });
|
143 | });
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function doWithSdmLocal(callback) {
|
152 | return __awaiter(this, void 0, void 0, function* () {
|
153 | if (modes_1.isInLocalMode() || modes_1.isGitHubAction()) {
|
154 |
|
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 |
|
169 |
|
170 |
|
171 |
|
172 | function 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 |
|
\ | No newline at end of file |