import { UsageCollectionSetup } from 'src/plugins/usage_collection/server' ; import { Plugin , CoreSetup , CoreStart , PluginInitializerContext , Logger , SavedObjectsClient , SavedObjectsServiceStart , } from '../../../../src/core/server' ; import { SecurityPluginStart } from '../../security/server' ; import { InfraPluginSetup } from '../../infra/server' ; import { PluginSetupContract as FeaturesPluginSetup } from '../../features/server' ; import { SecurityPluginSetup } from '../../security/server' ; import { LicensingPluginSetup } from '../../licensing/server' ; import { DEPRECATION_LOGS_SOURCE_ID , DEPRECATION_LOGS_INDEX } from '../common/constants' ; import { CredentialStore , credentialStoreFactory } from './lib/reindexing/credential_store' ; import { ReindexWorker } from './lib/reindexing' ; import { registerUpgradeAssistantUsageCollector } from './lib/telemetry' ; import { versionService } from './lib/version' ; import { createReindexWorker } from './routes/reindex_indices' ; import { registerRoutes } from './routes/register_routes' ; import { telemetrySavedObjectType , reindexOperationSavedObjectType , mlSavedObjectType , } from './saved_object_types' ; import { handleEsError } from './shared_imports' ; import { RouteDependencies } from './types' ; interface PluginsSetup { usageCollection : UsageCollectionSetup ; licensing : LicensingPluginSetup ; features : FeaturesPluginSetup ; infra : InfraPluginSetup ; security ? : SecurityPluginSetup ; } interface PluginsStart { security : SecurityPluginStart ; } export class UpgradeAssistantServerPlugin implements Plugin { private readonly logger : Logger ; private readonly credentialStore : CredentialStore ; private readonly kibanaVersion : string ; private licensing ? : LicensingPluginSetup ; private savedObjectsServiceStart ? : SavedObjectsServiceStart ; private securityPluginStart ? : SecurityPluginStart ; private worker ? : ReindexWorker ; constructor ( { logger , env } ) { this . logger = logger . get ( ) ; this . credentialStore = credentialStoreFactory ( this . logger ) ; this . kibanaVersion = env . packageInfo . version ; } private getWorker ( ) { if ( ! this . worker ) { throw new Error ( 'Worker unavailable' ) ; } return this . worker ; } setup ( { http , getStartServices , savedObjects } , { usageCollection , features , licensing , infra , security } ) { this . licensing = licensing ; savedObjects . registerType ( reindexOperationSavedObjectType ) ; savedObjects . registerType ( telemetrySavedObjectType ) ; savedObjects . registerType ( mlSavedObjectType ) ; features . registerElasticsearchFeature ( { id : 'upgrade_assistant' , management : { stack : [ 'upgrade_assistant' ] , } , privileges : [ { requiredClusterPrivileges : [ 'manage' ] , ui : [ ] , } , ] , } ) ; infra ?. defineInternalSourceConfiguration ( DEPRECATION_LOGS_SOURCE_ID , { name : 'deprecationLogs' , description : 'deprecation logs' , logIndices : { type : 'index_name' , indexName : DEPRECATION_LOGS_INDEX , } , logColumns : [ { timestampColumn : { id : 'timestampField' } } , { messageColumn : { id : 'messageField' } } , ] , } ) ; const router = http . createRouter ( ) ; const dependencies = { router , credentialStore : this . credentialStore , log : this . logger , licensing , getSavedObjectsService : ( ) => { if ( ! this . savedObjectsServiceStart ) { throw new Error ( 'Saved Objects Start service not available' ) ; } return this . savedObjectsServiceStart ; } , getSecurityPlugin : ( ) => this . securityPluginStart , lib : { handleEsError , } , config : { isSecurityEnabled : ( ) => security !== undefined && security . license . isEnabled ( ) , } , } ; versionService . setup ( this . kibanaVersion ) ; registerRoutes ( dependencies , this . getWorker . bind ( this ) ) ; if ( usageCollection ) { getStartServices ( ) . then ( ( [ { elasticsearch } ] ) => { registerUpgradeAssistantUsageCollector ( { elasticsearch , usageCollection , } ) ; } ) ; } } start ( { savedObjects , elasticsearch } , { security } ) { this . savedObjectsServiceStart = savedObjects ; this . securityPluginStart = security ; this . worker = createReindexWorker ( { credentialStore : this . credentialStore , licensing : this . licensing ! , elasticsearchService : elasticsearch , logger : this . logger , savedObjects : new SavedObjectsClient ( this . savedObjectsServiceStart . createInternalRepository ( ) ) , security : this . securityPluginStart , } ) ; this . worker . start ( ) ; } stop ( ) { if ( this . worker ) { this . worker . stop ( ) ; } } }