1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | const appRoot = require("app-root-path");
|
20 | const cluster = require("cluster");
|
21 | const fs = require("fs-extra");
|
22 | const glob = require("glob");
|
23 | const stringify = require("json-stringify-safe");
|
24 | const _ = require("lodash");
|
25 | const p = require("path");
|
26 | const semver = require("semver");
|
27 | const globals_1 = require("./globals");
|
28 | const config_1 = require("./internal/util/config");
|
29 | const logger_1 = require("./internal/util/logger");
|
30 | const string_1 = require("./internal/util/string");
|
31 | const axiosHttpClient_1 = require("./spi/http/axiosHttpClient");
|
32 | const packageJson_1 = require("./util/packageJson");
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function defaultConfiguration() {
|
41 | const pj = packageJson_1.loadHostPackageJson() || {};
|
42 | pj.name = pj.name || "atm-client-" + string_1.guid();
|
43 | pj.version = pj.version || "0.0.0";
|
44 | pj.keywords = pj.keywords || [];
|
45 | const cfg = loadDefaultConfiguration();
|
46 | cfg.name = pj.name;
|
47 | cfg.version = pj.version;
|
48 | cfg.keywords = pj.keywords;
|
49 | cfg.application = pj.name.replace(/^@.*?\//, "");
|
50 | return cfg;
|
51 | }
|
52 | exports.defaultConfiguration = defaultConfiguration;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function configurationValue(path, defaultValue) {
|
61 | if (globals_1.automationClientInstance()) {
|
62 | const conf = globals_1.automationClientInstance().configuration;
|
63 | let value;
|
64 | if (!path || path.length === 0) {
|
65 | value = conf;
|
66 | }
|
67 | else {
|
68 | value = _.get(conf, path);
|
69 | }
|
70 | if (value != null) {
|
71 | return value;
|
72 | }
|
73 | else if (defaultValue !== undefined) {
|
74 | return defaultValue;
|
75 | }
|
76 | }
|
77 | else if (defaultValue) {
|
78 | return defaultValue;
|
79 | }
|
80 | throw new Error(`Required @Value '${path}' not available`);
|
81 | }
|
82 | exports.configurationValue = configurationValue;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function loadDefaultConfiguration() {
|
88 | const cfg = exports.LocalDefaultConfiguration;
|
89 | let envSpecificCfg = {};
|
90 | const nodeEnv = process.env.ATOMIST_ENV || process.env.NODE_ENV;
|
91 | if (nodeEnv === "production") {
|
92 | envSpecificCfg = exports.ProductionDefaultConfiguration;
|
93 | }
|
94 | else if (nodeEnv === "staging" || nodeEnv === "testing") {
|
95 | envSpecificCfg = exports.TestingDefaultConfiguration;
|
96 | }
|
97 | else if (nodeEnv) {
|
98 | cfg.environment = nodeEnv;
|
99 | }
|
100 | return mergeConfigs(cfg, envSpecificCfg);
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 | function userConfigDir() {
|
106 | const home = process.env[process.platform === "win32" ? "USERPROFILE" : "HOME"];
|
107 | return p.join(home, ".atomist");
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 | function userConfigPath() {
|
113 | const clientConfigFile = "client.config.json";
|
114 | return p.join(userConfigDir(), clientConfigFile);
|
115 | }
|
116 | exports.userConfigPath = userConfigPath;
|
117 |
|
118 |
|
119 |
|
120 | function writeUserConfig(cfg) {
|
121 | const cfgDir = userConfigDir();
|
122 | return fs.ensureDir(cfgDir)
|
123 | .then(() => fs.chmod(cfgDir, 0o700))
|
124 | .then(() => fs.writeJson(userConfigPath(), cfg, {
|
125 | spaces: 2,
|
126 | encoding: "utf8",
|
127 | mode: 0o600,
|
128 | }));
|
129 | }
|
130 | exports.writeUserConfig = writeUserConfig;
|
131 |
|
132 |
|
133 |
|
134 | function getUserConfig() {
|
135 | if (fs.existsSync(userConfigPath())) {
|
136 | try {
|
137 | const cfg = fs.readJsonSync(userConfigPath());
|
138 |
|
139 | if (cfg.name) {
|
140 | delete cfg.name;
|
141 | }
|
142 | if (cfg.version) {
|
143 | delete cfg.version;
|
144 | }
|
145 | return cfg;
|
146 | }
|
147 | catch (e) {
|
148 | e.message = `Failed to read user config: ${e.message}`;
|
149 | throw e;
|
150 | }
|
151 | }
|
152 | return undefined;
|
153 | }
|
154 | exports.getUserConfig = getUserConfig;
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | function cfgLog(source) {
|
161 | if (cluster.isMaster) {
|
162 | logger_1.logger.debug(`Loading ${source} configuration`);
|
163 | }
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | function mergeConfigs(obj, ...sources) {
|
174 | return _.mergeWith(obj, ...sources, (objValue, srcValue) => {
|
175 | if (_.isArray(srcValue)) {
|
176 | return srcValue;
|
177 | }
|
178 | });
|
179 | }
|
180 | exports.mergeConfigs = mergeConfigs;
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | function resolveModuleConfig(userConfig, name, version) {
|
205 | const cfg = {};
|
206 | if (userConfig) {
|
207 | cfgLog("user");
|
208 | const uc = _.cloneDeep(userConfig);
|
209 | let mc = {};
|
210 | if (userConfig.modules) {
|
211 | delete uc.modules;
|
212 | if (name) {
|
213 | let modCfg;
|
214 | const moduleConfigs = userConfig.modules.filter(m => m.name === name);
|
215 | if (version) {
|
216 | modCfg = moduleConfigs.find(m => !m.version || semver.satisfies(version, m.version));
|
217 | }
|
218 | else if (moduleConfigs.length > 0) {
|
219 | modCfg = moduleConfigs[0];
|
220 | }
|
221 | if (modCfg) {
|
222 | cfgLog("module");
|
223 | if (modCfg.name) {
|
224 | delete modCfg.name;
|
225 | }
|
226 | if (modCfg.version) {
|
227 | delete modCfg.version;
|
228 | }
|
229 | mc = modCfg;
|
230 | }
|
231 | }
|
232 | }
|
233 | mergeConfigs(cfg, uc, mc);
|
234 | }
|
235 | return cfg;
|
236 | }
|
237 | exports.resolveModuleConfig = resolveModuleConfig;
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | function loadUserConfiguration(name, version) {
|
248 | const userConfig = getUserConfig();
|
249 | return resolveModuleConfig(userConfig, name, version);
|
250 | }
|
251 | exports.loadUserConfiguration = loadUserConfiguration;
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | function loadAutomationConfig(cfgPath) {
|
264 | let cfg = {};
|
265 | if (!cfgPath) {
|
266 | const cfgFile = "atomist.config.js";
|
267 | const files = glob.sync(`${appRoot.path}/**/${cfgFile}`, { ignore: ["**/{.git,node_modules}/**"] });
|
268 | if (files.length === 1) {
|
269 | cfgPath = files[0];
|
270 | }
|
271 | else if (files.length > 1) {
|
272 | throw new Error(`More than one automation configuration found in package: ${files.join(", ")}`);
|
273 | }
|
274 | }
|
275 | if (cfgPath) {
|
276 | try {
|
277 | cfg = require(cfgPath).configuration;
|
278 | cfgLog("automation config");
|
279 | }
|
280 | catch (e) {
|
281 | e.message = `Failed to load ${cfgPath}.configuration: ${e.message}`;
|
282 | throw e;
|
283 | }
|
284 | }
|
285 | return cfg;
|
286 | }
|
287 | exports.loadAutomationConfig = loadAutomationConfig;
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | function loadAtomistConfigPath() {
|
299 | let cfg = {};
|
300 | if (process.env.ATOMIST_CONFIG_PATH) {
|
301 | try {
|
302 | cfg = fs.readJsonSync(process.env.ATOMIST_CONFIG_PATH);
|
303 | cfgLog("ATOMIST_CONFIG_PATH");
|
304 | }
|
305 | catch (e) {
|
306 | e.message = `Failed to read ATOMIST_CONFIG_PATH: ${e.message}`;
|
307 | throw e;
|
308 | }
|
309 | }
|
310 | return cfg;
|
311 | }
|
312 | exports.loadAtomistConfigPath = loadAtomistConfigPath;
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | function loadAtomistConfig() {
|
325 | let cfg = {};
|
326 | if (process.env.ATOMIST_CONFIG) {
|
327 | try {
|
328 | cfg = JSON.parse(process.env.ATOMIST_CONFIG);
|
329 | cfgLog("ATOMIST_CONFIG");
|
330 | }
|
331 | catch (e) {
|
332 | e.message = `Failed to parse contents of ATOMIST_CONFIG environment variable: ${e.message}`;
|
333 | throw e;
|
334 | }
|
335 | }
|
336 | return cfg;
|
337 | }
|
338 | exports.loadAtomistConfig = loadAtomistConfig;
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 | function resolveWorkspaceIds(cfg) {
|
354 | if (process.env.ATOMIST_WORKSPACES) {
|
355 | cfg.workspaceIds = process.env.ATOMIST_WORKSPACES.split(",");
|
356 | }
|
357 | else if (config_1.config("workspaceIds")) {
|
358 | cfg.workspaceIds = config_1.config("workspaceIds");
|
359 | }
|
360 | return cfg.workspaceIds;
|
361 | }
|
362 | exports.resolveWorkspaceIds = resolveWorkspaceIds;
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | function resolveConfigurationValue(environmentVariables, configKeyPaths, defaultValue) {
|
375 | for (const ev of environmentVariables) {
|
376 | if (process.env[ev]) {
|
377 | return process.env[ev];
|
378 | }
|
379 | }
|
380 | for (const cv of configKeyPaths) {
|
381 | if (config_1.config(cv)) {
|
382 | return config_1.config(cv);
|
383 | }
|
384 | }
|
385 | return defaultValue;
|
386 | }
|
387 | exports.resolveConfigurationValue = resolveConfigurationValue;
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | function resolvePort(cfg) {
|
393 | if (process.env.PORT) {
|
394 | cfg.http.port = parseInt(process.env.PORT, 10);
|
395 | }
|
396 | return cfg.http.port;
|
397 | }
|
398 | exports.resolvePort = resolvePort;
|
399 | const EnvironmentVariablePrefix = "ATOMIST_";
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 | function resolveEnvironmentVariables(cfg) {
|
407 | for (const key in process.env) {
|
408 | if (key.startsWith(EnvironmentVariablePrefix)
|
409 | && process.env.hasOwnProperty(key)) {
|
410 | const cleanKey = key.slice(EnvironmentVariablePrefix.length).split("_").join(".");
|
411 | if (cleanKey[0] !== cleanKey[0].toUpperCase()) {
|
412 | _.update(cfg, cleanKey, () => process.env[key]);
|
413 | }
|
414 | }
|
415 | }
|
416 | }
|
417 | exports.resolveEnvironmentVariables = resolveEnvironmentVariables;
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 | function resolvePlaceholders(cfg) {
|
425 | resolvePlaceholdersRecursively(cfg);
|
426 | }
|
427 | exports.resolvePlaceholders = resolvePlaceholders;
|
428 | function resolvePlaceholdersRecursively(obj) {
|
429 | for (const property in obj) {
|
430 | if (obj.hasOwnProperty(property)) {
|
431 | if (typeof obj[property] === "object") {
|
432 | resolvePlaceholdersRecursively(obj[property]);
|
433 | }
|
434 | else if (typeof obj[property] === "string") {
|
435 | obj[property] = resolvePlaceholder(obj[property]);
|
436 | }
|
437 | }
|
438 | }
|
439 | }
|
440 | const PlaceholderExpression = /\$\{([.a-zA-Z_-]+)([.:0-9a-zA-Z-_ \" ]+)*\}/g;
|
441 | function resolvePlaceholder(value) {
|
442 | if (PlaceholderExpression.test(value)) {
|
443 | PlaceholderExpression.lastIndex = 0;
|
444 | let result;
|
445 |
|
446 | while (result = PlaceholderExpression.exec(value)) {
|
447 | const fm = result[0];
|
448 | const envValue = process.env[result[1]];
|
449 | const defaultValue = result[2] ? result[2].trim().slice(1) : undefined;
|
450 | if (envValue) {
|
451 | value = value.split(fm).join(envValue);
|
452 | }
|
453 | else if (defaultValue) {
|
454 | value = value.split(fm).join(defaultValue);
|
455 | }
|
456 | else {
|
457 | throw new Error(`Environment variable '${result[1]}' is not defined`);
|
458 | }
|
459 | }
|
460 | }
|
461 | return value;
|
462 | }
|
463 |
|
464 |
|
465 |
|
466 | function invokePostProcessors(cfg) {
|
467 | return cfg.postProcessors.reduce((pp, fp) => pp.then(fp), Promise.resolve(cfg));
|
468 | }
|
469 | exports.invokePostProcessors = invokePostProcessors;
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | function validateConfiguration(cfg) {
|
477 | if (!cfg) {
|
478 | throw new Error(`no configuration defined`);
|
479 | }
|
480 | const errors = [];
|
481 | if (!cfg.name) {
|
482 | errors.push("you must set a 'name' property in your configuration");
|
483 | }
|
484 | if (!cfg.version) {
|
485 | errors.push("you must set a 'version' property in your configuration");
|
486 | }
|
487 | if (!cfg.apiKey) {
|
488 | console.info("INFO: To obtain an 'apiKey' visit https://app.atomist.com/apikeys and run 'atomist config' " +
|
489 | "to configure the apiKey in your local configuration");
|
490 | errors.push("you must set an 'apiKey' property in your configuration");
|
491 | }
|
492 | cfg.workspaceIds = cfg.workspaceIds || [];
|
493 | cfg.groups = cfg.groups || [];
|
494 | if (cfg.workspaceIds.length < 1 && cfg.groups.length < 1) {
|
495 | errors.push("you must either provide an array of 'groups' in your configuration or, more likely, provide " +
|
496 | "an array of 'workspaceIds' in your configuration or set the ATOMIST_WORKSPACES environment variable " +
|
497 | "to a comma-separated list of workspace IDs");
|
498 | }
|
499 | if (cfg.workspaceIds.length > 0 && cfg.groups.length > 0) {
|
500 | errors.push("you cannot specify both 'workspaceIds' and 'groups' in your configuration, you must set one " +
|
501 | "to an empty array");
|
502 | }
|
503 | if (errors.length > 0) {
|
504 | const msg = `Configuration (${stringify(cfg, string_1.obfuscateJson)}) is not correct: ${errors.join("; ")}`;
|
505 | throw new Error(msg);
|
506 | }
|
507 | }
|
508 | exports.validateConfiguration = validateConfiguration;
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 | function loadConfiguration(cfgPath) {
|
548 |
|
549 | global.__logger = logger_1.logger;
|
550 | let cfg;
|
551 | try {
|
552 | const defCfg = defaultConfiguration();
|
553 | const userCfg = loadUserConfiguration(defCfg.name, defCfg.version);
|
554 | const autoCfg = loadAutomationConfig(cfgPath);
|
555 | const atmPathCfg = loadAtomistConfigPath();
|
556 | const atmCfg = loadAtomistConfig();
|
557 | cfg = mergeConfigs({}, defCfg, userCfg, autoCfg, atmPathCfg, atmCfg);
|
558 | resolveWorkspaceIds(cfg);
|
559 | resolvePort(cfg);
|
560 | resolveEnvironmentVariables(cfg);
|
561 | resolvePlaceholders(cfg);
|
562 | }
|
563 | catch (e) {
|
564 | logger_1.logger.error(`Failed to load configuration: ${e.message}`);
|
565 | if (e.stack) {
|
566 | logger_1.logger.error(`Stack trace:\n${e.stack}`);
|
567 | }
|
568 | return Promise.reject(e);
|
569 | }
|
570 | return invokePostProcessors(cfg)
|
571 | .then(completeCfg => {
|
572 | completeCfg.postProcessors = [];
|
573 | try {
|
574 | validateConfiguration(completeCfg);
|
575 | }
|
576 | catch (e) {
|
577 | return Promise.reject(e);
|
578 | }
|
579 | return Promise.resolve(completeCfg);
|
580 | });
|
581 | }
|
582 | exports.loadConfiguration = loadConfiguration;
|
583 |
|
584 |
|
585 |
|
586 |
|
587 | exports.LocalDefaultConfiguration = {
|
588 | workspaceIds: [],
|
589 | groups: [],
|
590 | environment: "local",
|
591 | policy: "ephemeral",
|
592 | endpoints: {
|
593 | api: "https://automation.atomist.com/registration",
|
594 | graphql: "https://automation.atomist.com/graphql/team",
|
595 | },
|
596 | http: {
|
597 | enabled: true,
|
598 | host: "localhost",
|
599 | port: 2866,
|
600 | auth: {
|
601 | basic: {
|
602 | enabled: false,
|
603 | },
|
604 | bearer: {
|
605 | enabled: false,
|
606 | },
|
607 | },
|
608 | customizers: [],
|
609 | client: {
|
610 | factory: axiosHttpClient_1.DefaultHttpClientFactory,
|
611 | },
|
612 | },
|
613 | ws: {
|
614 | enabled: true,
|
615 | termination: {
|
616 | graceful: false,
|
617 | gracePeriod: 10000,
|
618 | },
|
619 | compress: false,
|
620 | timeout: 10000,
|
621 | },
|
622 | applicationEvents: {
|
623 | enabled: false,
|
624 | },
|
625 | cluster: {
|
626 | enabled: false,
|
627 | },
|
628 | logging: {
|
629 | level: "debug",
|
630 | file: {
|
631 | enabled: true,
|
632 | level: "debug",
|
633 | },
|
634 | banner: {
|
635 | enabled: true,
|
636 | contributors: [],
|
637 | },
|
638 | },
|
639 | statsd: {
|
640 | enabled: false,
|
641 | },
|
642 | commands: null,
|
643 | events: null,
|
644 | ingesters: [],
|
645 | listeners: [],
|
646 | postProcessors: [],
|
647 | };
|
648 |
|
649 |
|
650 |
|
651 | exports.ProductionDefaultConfiguration = {
|
652 | environment: "production",
|
653 | policy: "durable",
|
654 | http: {
|
655 | auth: {
|
656 | basic: {
|
657 | enabled: true,
|
658 | },
|
659 | bearer: {
|
660 | enabled: true,
|
661 | },
|
662 | },
|
663 | },
|
664 | ws: {
|
665 | termination: {
|
666 | graceful: true,
|
667 | },
|
668 | compress: true,
|
669 | },
|
670 | applicationEvents: {
|
671 | enabled: true,
|
672 | },
|
673 | cluster: {
|
674 | enabled: true,
|
675 | },
|
676 | logging: {
|
677 | level: "info",
|
678 | file: {
|
679 | enabled: false,
|
680 | },
|
681 | },
|
682 | statsd: {
|
683 | enabled: true,
|
684 | },
|
685 | };
|
686 |
|
687 |
|
688 |
|
689 | exports.TestingDefaultConfiguration = {
|
690 | environment: "testing",
|
691 | policy: "durable",
|
692 | http: {
|
693 | auth: {
|
694 | basic: {
|
695 | enabled: true,
|
696 | },
|
697 | bearer: {
|
698 | enabled: true,
|
699 | },
|
700 | },
|
701 | },
|
702 | ws: {
|
703 | termination: {
|
704 | graceful: true,
|
705 | },
|
706 | compress: true,
|
707 | },
|
708 | applicationEvents: {
|
709 | enabled: true,
|
710 | },
|
711 | cluster: {
|
712 | enabled: true,
|
713 | },
|
714 | logging: {
|
715 | level: "info",
|
716 | file: {
|
717 | enabled: false,
|
718 | },
|
719 | },
|
720 | statsd: {
|
721 | enabled: true,
|
722 | },
|
723 | };
|
724 |
|
\ | No newline at end of file |