import apm from 'elastic-apm-node' ; import { config as pathConfig } from '@osd/utils' ; import { mapToObject } from '@osd/std' ; import { ConfigService , Env , RawConfigurationProvider , coreDeprecationProvider } from './config' ; import { CoreApp } from './core_app' ; import { AuditTrailService } from './audit_trail' ; import { OpenSearchService } from './opensearch' ; import { HttpService } from './http' ; import { HttpResourcesService } from './http_resources' ; import { RenderingService } from './rendering' ; import { LegacyService , ensureValidConfiguration } from './legacy' ; import { Logger , LoggerFactory , LoggingService , ILoggingSystem } from './logging' ; import { UiSettingsService } from './ui_settings' ; import { PluginsService , config as pluginsConfig } from './plugins' ; import { SavedObjectsService } from '../server/saved_objects' ; import { MetricsService , opsConfig } from './metrics' ; import { CapabilitiesService } from './capabilities' ; import { EnvironmentService , config as pidConfig } from './environment' ; import { StatusService } from './status/status_service' ; import { config as cspConfig } from './csp' ; import { config as opensearchConfig } from './opensearch' ; import { config as httpConfig } from './http' ; import { config as loggingConfig } from './logging' ; import { config as devConfig } from './dev' ; import { config as opensearchDashboardsConfig } from './opensearch_dashboards_config' ; import { savedObjectsConfig , savedObjectsMigrationConfig } from './saved_objects' ; import { config as uiSettingsConfig } from './ui_settings' ; import { config as statusConfig } from './status' ; import { ContextService } from './context' ; import { RequestHandlerContext } from '.' ; import { InternalCoreSetup , InternalCoreStart , ServiceConfigDescriptor } from './internal_types' ; import { CoreUsageDataService } from './core_usage_data' ; import { CoreRouteHandlerContext } from './core_route_handler_context' ; const coreId = Symbol ( 'core' ) ; const rootConfigPath = '' ; export class Server { public readonly configService : ConfigService ; private readonly capabilities : CapabilitiesService ; private readonly context : ContextService ; private readonly opensearch : OpenSearchService ; private readonly http : HttpService ; private readonly rendering : RenderingService ; private readonly legacy : LegacyService ; private readonly log : Logger ; private readonly plugins : PluginsService ; private readonly savedObjects : SavedObjectsService ; private readonly uiSettings : UiSettingsService ; private readonly environment : EnvironmentService ; private readonly metrics : MetricsService ; private readonly httpResources : HttpResourcesService ; private readonly status : StatusService ; private readonly logging : LoggingService ; private readonly coreApp : CoreApp ; private readonly auditTrail : AuditTrailService ; private readonly coreUsageData : CoreUsageDataService ; #pluginsInitialized ? : boolean ; private coreStart ? : InternalCoreStart ; private readonly logger : LoggerFactory ; constructor ( rawConfigProvider , public readonly env , private readonly loggingSystem ) { this . logger = this . loggingSystem . asLoggerFactory ( ) ; this . log = this . logger . get ( 'server' ) ; this . configService = new ConfigService ( rawConfigProvider , env , this . logger ) ; const core = { coreId , configService : this . configService , env , logger : this . logger } ; this . context = new ContextService ( core ) ; this . http = new HttpService ( core ) ; this . rendering = new RenderingService ( core ) ; this . plugins = new PluginsService ( core ) ; this . legacy = new LegacyService ( core ) ; this . opensearch = new OpenSearchService ( core ) ; this . savedObjects = new SavedObjectsService ( core ) ; this . uiSettings = new UiSettingsService ( core ) ; this . capabilities = new CapabilitiesService ( core ) ; this . environment = new EnvironmentService ( core ) ; this . metrics = new MetricsService ( core ) ; this . status = new StatusService ( core ) ; this . coreApp = new CoreApp ( core ) ; this . httpResources = new HttpResourcesService ( core ) ; this . auditTrail = new AuditTrailService ( core ) ; this . logging = new LoggingService ( core ) ; this . coreUsageData = new CoreUsageDataService ( core ) ; } public async setup ( ) { this . log . debug ( 'setting up server' ) ; const setupTransaction = apm . startTransaction ( 'server_setup' , 'opensearch_dashboards_platform' ) ; const environmentSetup = await this . environment . setup ( ) ; const { pluginTree , uiPlugins } = await this . plugins . discover ( { environment : environmentSetup , } ) ; const legacyConfigSetup = await this . legacy . setupLegacyConfig ( ) ; await this . configService . validate ( ) ; await ensureValidConfiguration ( this . configService , legacyConfigSetup ) ; const contextServiceSetup = this . context . setup ( { pluginDependencies : new Map ( [ ... pluginTree . asOpaqueIds , [ this . legacy . legacyId , [ ... pluginTree . asOpaqueIds . keys ( ) ] ] , ] ) , } ) ; const auditTrailSetup = this . auditTrail . setup ( ) ; const httpSetup = await this . http . setup ( { context : contextServiceSetup , } ) ; const capabilitiesSetup = this . capabilities . setup ( { http : httpSetup } ) ; const opensearchServiceSetup = await this . opensearch . setup ( { http : httpSetup , } ) ; const savedObjectsSetup = await this . savedObjects . setup ( { http : httpSetup , opensearch : opensearchServiceSetup , } ) ; const uiSettingsSetup = await this . uiSettings . setup ( { http : httpSetup , savedObjects : savedObjectsSetup , } ) ; const metricsSetup = await this . metrics . setup ( { http : httpSetup } ) ; const statusSetup = await this . status . setup ( { opensearch : opensearchServiceSetup , pluginDependencies : pluginTree . asNames , savedObjects : savedObjectsSetup , environment : environmentSetup , http : httpSetup , metrics : metricsSetup , } ) ; const renderingSetup = await this . rendering . setup ( { http : httpSetup , status : statusSetup , uiPlugins , } ) ; const httpResourcesSetup = this . httpResources . setup ( { http : httpSetup , rendering : renderingSetup , } ) ; const loggingSetup = this . logging . setup ( { loggingSystem : this . loggingSystem , } ) ; this . coreUsageData . setup ( { metrics : metricsSetup } ) ; const coreSetup = { capabilities : capabilitiesSetup , context : contextServiceSetup , opensearch : opensearchServiceSetup , environment : environmentSetup , http : httpSetup , savedObjects : savedObjectsSetup , status : statusSetup , uiSettings : uiSettingsSetup , rendering : renderingSetup , httpResources : httpResourcesSetup , auditTrail : auditTrailSetup , logging : loggingSetup , metrics : metricsSetup , } ; const pluginsSetup = await this . plugins . setup ( coreSetup ) ; this . #pluginsInitialized = pluginsSetup . initialized ; await this . legacy . setup ( { core : { ... coreSetup , plugins : pluginsSetup , rendering : renderingSetup } , plugins : mapToObject ( pluginsSetup . contracts ) , uiPlugins , } ) ; this . registerCoreContext ( coreSetup ) ; this . coreApp . setup ( coreSetup ) ; setupTransaction ?. end ( ) ; return coreSetup ; } public async start ( ) { this . log . debug ( 'starting server' ) ; const startTransaction = apm . startTransaction ( 'server_start' , 'opensearch_dashboards_platform' ) ; const auditTrailStart = this . auditTrail . start ( ) ; const opensearchStart = await this . opensearch . start ( { auditTrail : auditTrailStart , } ) ; const soStartSpan = startTransaction ?. startSpan ( 'saved_objects.migration' , 'migration' ) ; const savedObjectsStart = await this . savedObjects . start ( { opensearch : opensearchStart , pluginsInitialized : this . #pluginsInitialized , } ) ; soStartSpan ?. end ( ) ; const capabilitiesStart = this . capabilities . start ( ) ; const uiSettingsStart = await this . uiSettings . start ( ) ; const metricsStart = await this . metrics . start ( ) ; const httpStart = this . http . getStartContract ( ) ; const coreUsageDataStart = this . coreUsageData . start ( { opensearch : opensearchStart , savedObjects : savedObjectsStart , } ) ; this . coreStart = { capabilities : capabilitiesStart , opensearch : opensearchStart , http : httpStart , metrics : metricsStart , savedObjects : savedObjectsStart , uiSettings : uiSettingsStart , auditTrail : auditTrailStart , coreUsageData : coreUsageDataStart , } ; const pluginsStart = await this . plugins . start ( this . coreStart ) ; await this . legacy . start ( { core : { ... this . coreStart , plugins : pluginsStart , } , plugins : mapToObject ( pluginsStart . contracts ) , } ) ; await this . http . start ( ) ; startTransaction ?. end ( ) ; return this . coreStart ; } public async stop ( ) { this . log . debug ( 'stopping server' ) ; await this . legacy . stop ( ) ; await this . plugins . stop ( ) ; await this . savedObjects . stop ( ) ; await this . opensearch . stop ( ) ; await this . http . stop ( ) ; await this . uiSettings . stop ( ) ; await this . rendering . stop ( ) ; await this . metrics . stop ( ) ; await this . status . stop ( ) ; await this . logging . stop ( ) ; await this . auditTrail . stop ( ) ; } private registerCoreContext ( coreSetup ) { coreSetup . http . registerRouteHandlerContext ( coreId , 'core' , async ( context , req , res ) : Promise < RequestHandlerContext [ 'core' ] > => { return new CoreRouteHandlerContext ( this . coreStart ! , req ) ; } ) ; } public async setupCoreConfig ( ) { const configDescriptors = [ pathConfig , cspConfig , opensearchConfig , loggingConfig , httpConfig , pluginsConfig , devConfig , opensearchDashboardsConfig , savedObjectsConfig , savedObjectsMigrationConfig , uiSettingsConfig , opsConfig , statusConfig , pidConfig , ] ; this . configService . addDeprecationProvider ( rootConfigPath , coreDeprecationProvider ) ; for ( const descriptor of configDescriptors ) { if ( descriptor . deprecations ) { this . configService . addDeprecationProvider ( descriptor . path , descriptor . deprecations ) ; } await this . configService . setSchema ( descriptor . path , descriptor . schema ) ; } } }