-
Notifications
You must be signed in to change notification settings - Fork 915
/
bootstrap.ts
148 lines (125 loc) · 4.8 KB
/
bootstrap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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.
*/
import chalk from 'chalk';
import { isMaster as isClusterManager } from 'cluster';
import { CliArgs, Env, RawConfigService } from './config';
import { Root } from './root';
import { CriticalError } from './errors';
interface OpenSearchDashboardsFeatures {
// Indicates whether we can run OpenSearch Dashboards in a so called cluster mode in which
// OpenSearch Dashboards is run as a "worker" process together with optimizer "worker" process
// that are orchestrated by the "master" process (dev mode only feature).
isClusterModeSupported: boolean;
// Indicates whether we can run OpenSearch Dashboards in REPL mode (dev mode only feature).
isReplModeSupported: boolean;
}
interface BootstrapArgs {
configs: string[];
cliArgs: CliArgs;
applyConfigOverrides: (config: Record<string, any>) => Record<string, any>;
features: OpenSearchDashboardsFeatures;
}
/**
*
* @internal
* @param param0 - options
*/
export async function bootstrap({
configs,
cliArgs,
applyConfigOverrides,
features,
}: BootstrapArgs) {
if (cliArgs.repl && !features.isReplModeSupported) {
onRootShutdown('OpenSearch Dashboards REPL mode can only be run in development mode.');
}
if (cliArgs.optimize) {
// --optimize is deprecated and does nothing now, avoid starting up and just shutdown
return;
}
// `bootstrap` is exported from the `src/core/server/index` module,
// meaning that any test importing, implicitly or explicitly, anything concrete
// from `core/server` will load `dev-utils`. As some tests are mocking the `fs` package,
// and as `REPO_ROOT` is initialized on the fly when importing `dev-utils` and requires
// the `fs` package, it causes failures. This is why we use a dynamic `require` here.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { REPO_ROOT } = require('@osd/utils');
const env = Env.createDefault(REPO_ROOT, {
configs,
cliArgs,
isDevClusterMaster: isClusterManager && cliArgs.dev && features.isClusterModeSupported,
isDevClusterManager: isClusterManager && cliArgs.dev && features.isClusterModeSupported,
});
const rawConfigService = new RawConfigService(env.configs, applyConfigOverrides);
rawConfigService.loadConfig();
const root = new Root(rawConfigService, env, onRootShutdown);
process.on('SIGHUP', () => reloadLoggingConfig());
// This is only used by the LogRotator service
// in order to be able to reload the log configuration
// under the cluster mode
process.on('message', (msg) => {
if (!msg || msg.reloadLoggingConfig !== true) {
return;
}
reloadLoggingConfig();
});
function reloadLoggingConfig() {
const cliLogger = root.logger.get('cli');
cliLogger.info('Reloading logging configuration due to SIGHUP.', { tags: ['config'] });
try {
rawConfigService.reloadConfig();
} catch (err) {
return shutdown(err);
}
cliLogger.info('Reloaded logging configuration due to SIGHUP.', { tags: ['config'] });
}
process.on('SIGINT', () => shutdown());
process.on('SIGTERM', () => shutdown());
function shutdown(reason?: Error) {
rawConfigService.stop();
return root.shutdown(reason);
}
try {
await root.setup();
await root.start();
} catch (err) {
await shutdown(err);
}
}
function onRootShutdown(reason?: any) {
if (reason !== undefined) {
// There is a chance that logger wasn't configured properly and error that
// that forced root to shut down could go unnoticed. To prevent this we always
// mirror such fatal errors in standard output with `console.error`.
// eslint-disable-next-line
console.error(`\n${chalk.white.bgRed(' FATAL ')} ${reason}\n`);
process.exit(reason instanceof CriticalError ? reason.processExitCode : 1);
}
process.exit(0);
}