-
Notifications
You must be signed in to change notification settings - Fork 8k
/
bootstrap.ts
145 lines (120 loc) · 4.73 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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import chalk from 'chalk';
import { getPackages } from '@kbn/repo-packages';
import { CliArgs, Env, RawConfigService } from '@kbn/config';
import { CriticalError } from '@kbn/core-base-server-internal';
import { Root } from './root';
import { MIGRATION_EXCEPTION_CODE } from './constants';
interface BootstrapArgs {
configs: string[];
cliArgs: CliArgs;
applyConfigOverrides: (config: Record<string, any>) => Record<string, any>;
}
/**
*
* @internal
* @param param0 - options
*/
export async function bootstrap({ configs, cliArgs, applyConfigOverrides }: BootstrapArgs) {
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('@kbn/repo-info');
const env = Env.createDefault(REPO_ROOT, {
configs,
cliArgs,
repoPackages: getPackages(REPO_ROOT),
});
const rawConfigService = new RawConfigService(env.configs, applyConfigOverrides);
rawConfigService.loadConfig();
const root = new Root(rawConfigService, env, onRootShutdown);
const cliLogger = root.logger.get('cli');
const rootLogger = root.logger.get('root');
rootLogger.info('Kibana is starting');
cliLogger.debug('Kibana configurations evaluated in this order: ' + env.configs.join(', '));
process.on('SIGHUP', () => reloadConfiguration());
// 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: any) => {
if (!msg || msg.reloadConfiguration !== true) {
return;
}
reloadConfiguration();
});
function reloadConfiguration(reason = 'SIGHUP signal received') {
cliLogger.info(`Reloading Kibana configuration (reason: ${reason}).`, { tags: ['config'] });
try {
rawConfigService.reloadConfig();
} catch (err) {
return shutdown(err);
}
cliLogger.info(`Reloaded Kibana configuration (reason: ${reason}).`, { tags: ['config'] });
}
process.on('SIGINT', () => {
rootLogger.info('SIGINT received - initiating shutdown');
shutdown();
});
process.on('SIGTERM', () => {
rootLogger.info('SIGTERM received - initiating shutdown');
shutdown();
});
function shutdown(reason?: Error) {
rawConfigService.stop();
return root.shutdown(reason);
}
try {
const prebootContract = await root.preboot();
let isSetupOnHold = false;
if (prebootContract) {
const { preboot } = prebootContract;
// If setup is on hold then preboot server is supposed to serve user requests and we can let
// dev parent process know that we are ready for dev mode.
isSetupOnHold = preboot.isSetupOnHold();
if (process.send && isSetupOnHold) {
process.send(['SERVER_LISTENING']);
}
if (isSetupOnHold) {
rootLogger.info('Holding setup until preboot stage is completed.');
const { shouldReloadConfig } = await preboot.waitUntilCanSetup();
if (shouldReloadConfig) {
await reloadConfiguration('configuration might have changed during preboot stage');
}
}
}
await root.setup();
await root.start();
// Notify parent process if we haven't done that yet during preboot stage.
if (process.send && !isSetupOnHold) {
process.send(['SERVER_LISTENING']);
}
} catch (err) {
await shutdown(err);
}
}
function onRootShutdown(reason?: any) {
if (reason !== undefined) {
if (reason.code !== MIGRATION_EXCEPTION_CODE) {
// 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 no-console
console.error(`\n${chalk.white.bgRed(' FATAL ')} ${reason}\n`);
}
process.exit(reason instanceof CriticalError ? reason.processExitCode : 1);
}
process.exit(0);
}