Skip to content

Commit 15b6ba0

Browse files
authored
[Ingest Manager] Better handling of package installation problems (#66541) (#66917)
* Better handle non-available package registry * Log errors in route handler only * Await installation of prebuilt component templates * Remove leftover import * Remove useless use of await.
1 parent ee58bd5 commit 15b6ba0

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,22 @@ export const createFleetSetupHandler: RequestHandler<
7474
export const ingestManagerSetupHandler: RequestHandler = async (context, request, response) => {
7575
const soClient = context.core.savedObjects.client;
7676
const callCluster = context.core.elasticsearch.adminClient.callAsCurrentUser;
77+
const logger = appContextService.getLogger();
7778
try {
7879
await setupIngestManager(soClient, callCluster);
7980
return response.ok({
8081
body: { isInitialized: true },
8182
});
8283
} catch (e) {
84+
if (e.isBoom) {
85+
logger.error(e.output.payload.message);
86+
return response.customError({
87+
statusCode: e.output.statusCode,
88+
body: { message: e.output.payload.message },
89+
});
90+
}
91+
logger.error(e.message);
92+
logger.error(e.stack);
8393
return response.customError({
8494
statusCode: 500,
8595
body: { message: e.message },

x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import Boom from 'boom';
78
import { Dataset, RegistryPackage, ElasticsearchAssetType, TemplateRef } from '../../../../types';
89
import { CallESAsCurrentUser } from '../../../../types';
910
import { Field, loadFieldsFromYaml, processFields } from '../../fields/field';
@@ -20,8 +21,8 @@ export const installTemplates = async (
2021
// install any pre-built index template assets,
2122
// atm, this is only the base package's global index templates
2223
// Install component templates first, as they are used by the index templates
23-
installPreBuiltComponentTemplates(pkgName, pkgVersion, callCluster);
24-
installPreBuiltTemplates(pkgName, pkgVersion, callCluster);
24+
await installPreBuiltComponentTemplates(pkgName, pkgVersion, callCluster);
25+
await installPreBuiltTemplates(pkgName, pkgVersion, callCluster);
2526

2627
// build templates per dataset from yml files
2728
const datasets = registryPackage.datasets;
@@ -53,16 +54,7 @@ const installPreBuiltTemplates = async (
5354
pkgVersion,
5455
(entry: Registry.ArchiveEntry) => isTemplate(entry)
5556
);
56-
// templatePaths.forEach(async path => {
57-
// const { file } = Registry.pathParts(path);
58-
// const templateName = file.substr(0, file.lastIndexOf('.'));
59-
// const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
60-
// await callCluster('indices.putTemplate', {
61-
// name: templateName,
62-
// body: content,
63-
// });
64-
// });
65-
templatePaths.forEach(async path => {
57+
const templateInstallPromises = templatePaths.map(async path => {
6658
const { file } = Registry.pathParts(path);
6759
const templateName = file.substr(0, file.lastIndexOf('.'));
6860
const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
@@ -91,8 +83,15 @@ const installPreBuiltTemplates = async (
9183
// The existing convenience endpoint `indices.putTemplate` only sends to _template,
9284
// which does not support v2 templates.
9385
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
94-
await callCluster('transport.request', callClusterParams);
86+
return callCluster('transport.request', callClusterParams);
9587
});
88+
try {
89+
return await Promise.all(templateInstallPromises);
90+
} catch (e) {
91+
throw new Boom(`Error installing prebuilt index templates ${e.message}`, {
92+
statusCode: 400,
93+
});
94+
}
9695
};
9796

9897
const installPreBuiltComponentTemplates = async (
@@ -105,7 +104,7 @@ const installPreBuiltComponentTemplates = async (
105104
pkgVersion,
106105
(entry: Registry.ArchiveEntry) => isComponentTemplate(entry)
107106
);
108-
templatePaths.forEach(async path => {
107+
const templateInstallPromises = templatePaths.map(async path => {
109108
const { file } = Registry.pathParts(path);
110109
const templateName = file.substr(0, file.lastIndexOf('.'));
111110
const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
@@ -124,8 +123,15 @@ const installPreBuiltComponentTemplates = async (
124123
// This uses the catch-all endpoint 'transport.request' because there is no
125124
// convenience endpoint for component templates yet.
126125
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
127-
await callCluster('transport.request', callClusterParams);
126+
return callCluster('transport.request', callClusterParams);
128127
});
128+
try {
129+
return await Promise.all(templateInstallPromises);
130+
} catch (e) {
131+
throw new Boom(`Error installing prebuilt component templates ${e.message}`, {
132+
statusCode: 400,
133+
});
134+
}
129135
};
130136

131137
const isTemplate = ({ path }: Registry.ArchiveEntry) => {

x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,13 @@ export async function ensureInstalledPackage(options: {
7373
if (installedPackage) {
7474
return installedPackage;
7575
}
76-
// if the requested packaged was not found to be installed, try installing
77-
try {
78-
await installLatestPackage({
79-
savedObjectsClient,
80-
pkgName,
81-
callCluster,
82-
});
83-
return await getInstallation({ savedObjectsClient, pkgName });
84-
} catch (err) {
85-
throw new Error(err.message);
86-
}
76+
// if the requested packaged was not found to be installed, install
77+
await installLatestPackage({
78+
savedObjectsClient,
79+
pkgName,
80+
callCluster,
81+
});
82+
return await getInstallation({ savedObjectsClient, pkgName });
8783
}
8884

8985
export async function installPackage(options: {

x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function getResponse(url: string): Promise<Response> {
1717
throw new Boom(response.statusText, { statusCode: response.status });
1818
}
1919
} catch (e) {
20-
throw Boom.boomify(e);
20+
throw new Boom(`Error connecting to package registry: ${e.message}`, { statusCode: 502 });
2121
}
2222
}
2323

0 commit comments

Comments
 (0)