@@ -11,6 +11,7 @@ import { agentPolicyService } from './agent_policy';
1111import { outputService } from './output' ;
1212import {
1313 ensureInstalledDefaultPackages ,
14+ ensureInstalledPackage ,
1415 ensurePackagesCompletedInstall ,
1516} from './epm/packages/install' ;
1617import {
@@ -20,6 +21,8 @@ import {
2021 Installation ,
2122 Output ,
2223 DEFAULT_AGENT_POLICIES_PACKAGES ,
24+ FLEET_SERVER_PACKAGE ,
25+ FLEET_SERVER_INDICES ,
2326} from '../../common' ;
2427import { SO_SEARCH_LIMIT } from '../constants' ;
2528import { getPackageInfo } from './epm/packages' ;
@@ -29,6 +32,8 @@ import { settingsService } from '.';
2932import { awaitIfPending } from './setup_utils' ;
3033import { createDefaultSettings } from './settings' ;
3134import { ensureAgentActionPolicyChangeExists } from './agents' ;
35+ import { appContextService } from './app_context' ;
36+ import { runFleetServerMigration } from './fleet_server_migration' ;
3237
3338const FLEET_ENROLL_USERNAME = 'fleet_enroll' ;
3439const FLEET_ENROLL_ROLE = 'fleet_enroll' ;
@@ -77,6 +82,15 @@ async function createSetupSideEffects(
7782 // By moving this outside of the Promise.all, the upgrade will occur first, and then we'll attempt to reinstall any
7883 // packages that are stuck in the installing state.
7984 await ensurePackagesCompletedInstall ( soClient , callCluster ) ;
85+ if ( appContextService . getConfig ( ) ?. agents . fleetServerEnabled ) {
86+ await ensureInstalledPackage ( {
87+ savedObjectsClient : soClient ,
88+ pkgName : FLEET_SERVER_PACKAGE ,
89+ callCluster,
90+ } ) ;
91+ await ensureFleetServerIndicesCreated ( esClient ) ;
92+ await runFleetServerMigration ( ) ;
93+ }
8094
8195 // If we just created the default policy, ensure default packages are added to it
8296 if ( defaultAgentPolicyCreated ) {
@@ -144,6 +158,21 @@ async function updateFleetRoleIfExists(callCluster: CallESAsCurrentUser) {
144158 return putFleetRole ( callCluster ) ;
145159}
146160
161+ async function ensureFleetServerIndicesCreated ( esClient : ElasticsearchClient ) {
162+ await Promise . all (
163+ FLEET_SERVER_INDICES . map ( async ( index ) => {
164+ const res = await esClient . indices . exists ( {
165+ index,
166+ } ) ;
167+ if ( res . statusCode === 404 ) {
168+ await esClient . indices . create ( {
169+ index,
170+ } ) ;
171+ }
172+ } )
173+ ) ;
174+ }
175+
147176async function putFleetRole ( callCluster : CallESAsCurrentUser ) {
148177 return callCluster ( 'transport.request' , {
149178 method : 'PUT' ,
0 commit comments