import chalk from 'chalk' ; import { isMaster } from 'cluster' ; import { CliArgs , Env , RawConfigService } from './config' ; import { Root } from './root' ; import { CriticalError } from './errors' ; interface OpenSearchDashboardsFeatures { isClusterModeSupported : boolean ; isReplModeSupported : boolean ; } interface BootstrapArgs { configs : string [ ] ; cliArgs : CliArgs ; applyConfigOverrides : ( config : Record < string , any > ) => Record < string , any > ; features : OpenSearchDashboardsFeatures ; } internal param param0 export async function bootstrap ( { configs , cliArgs , applyConfigOverrides , features , } ) { if ( cliArgs . repl && ! features . isReplModeSupported ) { onRootShutdown ( 'OpenSearch Dashboards REPL mode can only be run in development mode.' ) ; } if ( cliArgs . optimize ) { return ; } const { REPO_ROOT } = require ( '@osd/utils' ) ; const env = Env . createDefault ( REPO_ROOT , { configs , cliArgs , isDevClusterMaster : isMaster && cliArgs . dev && features . isClusterModeSupported , } ) ; const rawConfigService = new RawConfigService ( env . configs , applyConfigOverrides ) ; rawConfigService . loadConfig ( ) ; const root = new Root ( rawConfigService , env , onRootShutdown ) ; process . on ( 'SIGHUP' , ( ) => reloadLoggingConfig ( ) ) ; 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 ? ) { rawConfigService . stop ( ) ; return root . shutdown ( reason ) ; } try { await root . setup ( ) ; await root . start ( ) ; } catch ( err ) { await shutdown ( err ) ; } } function onRootShutdown ( reason ? ) { if ( reason !== undefined ) { console . error ( `\n${ chalk . white . bgRed ( ' FATAL ' ) } ${ reason }\n` ) ; process . exit ( reason instanceof CriticalError ? reason . processExitCode : 1 ) ; } process . exit ( 0 ) ; }