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