-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathinstall.ts
342 lines (309 loc) · 10.5 KB
/
install.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import Boom from 'boom';
import { SavedObjectsClientContract } from 'src/core/server';
import {
RegistryDataStream,
ElasticsearchAssetType,
TemplateRef,
RegistryElasticsearch,
InstallablePackage,
} from '../../../../types';
import { CallESAsCurrentUser } from '../../../../types';
import { Field, loadFieldsFromYaml, processFields } from '../../fields/field';
import { getPipelineNameForInstallation } from '../ingest_pipeline/install';
import { generateMappings, generateTemplateName, getTemplate } from './template';
import * as Registry from '../../registry';
import { removeAssetsFromInstalledEsByType, saveInstalledEsRefs } from '../../packages/install';
export const installTemplates = async (
installablePackage: InstallablePackage,
callCluster: CallESAsCurrentUser,
paths: string[],
savedObjectsClient: SavedObjectsClientContract
): Promise<TemplateRef[]> => {
// install any pre-built index template assets,
// atm, this is only the base package's global index templates
// Install component templates first, as they are used by the index templates
await installPreBuiltComponentTemplates(paths, callCluster);
await installPreBuiltTemplates(paths, callCluster);
// remove package installation's references to index templates
await removeAssetsFromInstalledEsByType(
savedObjectsClient,
installablePackage.name,
ElasticsearchAssetType.indexTemplate
);
// build templates per data stream from yml files
const dataStreams = installablePackage.data_streams;
if (!dataStreams) return [];
// get template refs to save
const installedTemplateRefs = dataStreams.map((dataStream) => ({
id: generateTemplateName(dataStream),
type: ElasticsearchAssetType.indexTemplate,
}));
// add package installation's references to index templates
await saveInstalledEsRefs(savedObjectsClient, installablePackage.name, installedTemplateRefs);
if (dataStreams) {
const installTemplatePromises = dataStreams.reduce<Array<Promise<TemplateRef>>>(
(acc, dataStream) => {
acc.push(
installTemplateForDataStream({
pkg: installablePackage,
callCluster,
dataStream,
})
);
return acc;
},
[]
);
const res = await Promise.all(installTemplatePromises);
const installedTemplates = res.flat();
return installedTemplates;
}
return [];
};
const installPreBuiltTemplates = async (paths: string[], callCluster: CallESAsCurrentUser) => {
const templatePaths = paths.filter((path) => isTemplate(path));
const templateInstallPromises = templatePaths.map(async (path) => {
const { file } = Registry.pathParts(path);
const templateName = file.substr(0, file.lastIndexOf('.'));
const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
let templateAPIPath = '_template';
// v2 index templates need to be installed through the new API endpoint.
// Checking for 'template' and 'composed_of' should catch them all.
// For the new v2 format, see https://github.com/elastic/elasticsearch/issues/53101
if (content.hasOwnProperty('template') || content.hasOwnProperty('composed_of')) {
templateAPIPath = '_index_template';
}
const callClusterParams: {
method: string;
path: string;
ignore: number[];
body: any;
} = {
method: 'PUT',
path: `/${templateAPIPath}/${templateName}`,
ignore: [404],
body: content,
};
// This uses the catch-all endpoint 'transport.request' because there is no
// convenience endpoint using the new _index_template API yet.
// The existing convenience endpoint `indices.putTemplate` only sends to _template,
// which does not support v2 templates.
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
return callCluster('transport.request', callClusterParams);
});
try {
return await Promise.all(templateInstallPromises);
} catch (e) {
throw new Boom(`Error installing prebuilt index templates ${e.message}`, {
statusCode: 400,
});
}
};
const installPreBuiltComponentTemplates = async (
paths: string[],
callCluster: CallESAsCurrentUser
) => {
const templatePaths = paths.filter((path) => isComponentTemplate(path));
const templateInstallPromises = templatePaths.map(async (path) => {
const { file } = Registry.pathParts(path);
const templateName = file.substr(0, file.lastIndexOf('.'));
const content = JSON.parse(Registry.getAsset(path).toString('utf8'));
const callClusterParams: {
method: string;
path: string;
ignore: number[];
body: any;
} = {
method: 'PUT',
path: `/_component_template/${templateName}`,
ignore: [404],
body: content,
};
// This uses the catch-all endpoint 'transport.request' because there is no
// convenience endpoint for component templates yet.
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
return callCluster('transport.request', callClusterParams);
});
try {
return await Promise.all(templateInstallPromises);
} catch (e) {
throw new Boom(`Error installing prebuilt component templates ${e.message}`, {
statusCode: 400,
});
}
};
const isTemplate = (path: string) => {
const pathParts = Registry.pathParts(path);
return pathParts.type === ElasticsearchAssetType.indexTemplate;
};
const isComponentTemplate = (path: string) => {
const pathParts = Registry.pathParts(path);
return pathParts.type === ElasticsearchAssetType.componentTemplate;
};
/**
* installTemplateForDataStream installs one template for each data stream
*
* The template is currently loaded with the pkgkey-package-data_stream
*/
export async function installTemplateForDataStream({
pkg,
callCluster,
dataStream,
}: {
pkg: InstallablePackage;
callCluster: CallESAsCurrentUser;
dataStream: RegistryDataStream;
}): Promise<TemplateRef> {
const fields = await loadFieldsFromYaml(pkg, dataStream.path);
return installTemplate({
callCluster,
fields,
dataStream,
packageVersion: pkg.version,
packageName: pkg.name,
});
}
function putComponentTemplate(
body: object | undefined,
name: string,
callCluster: CallESAsCurrentUser
): { clusterPromise: Promise<any>; name: string } | undefined {
if (body) {
const callClusterParams: {
method: string;
path: string;
ignore: number[];
body: any;
} = {
method: 'PUT',
path: `/_component_template/${name}`,
ignore: [404],
body,
};
return { clusterPromise: callCluster('transport.request', callClusterParams), name };
}
}
function buildComponentTemplates(registryElasticsearch: RegistryElasticsearch | undefined) {
let mappingsTemplate;
let settingsTemplate;
if (registryElasticsearch && registryElasticsearch['index_template.mappings']) {
mappingsTemplate = {
template: {
mappings: {
...registryElasticsearch['index_template.mappings'],
// temporary change until https://github.com/elastic/elasticsearch/issues/58956 is resolved
// hopefully we'll be able to remove the entire properties section once that issue is resolved
properties: {
// if the timestamp_field changes here: https://github.com/elastic/kibana/blob/master/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/template.ts#L309
// we'll need to update this as well
'@timestamp': {
type: 'date',
},
},
},
},
};
}
if (registryElasticsearch && registryElasticsearch['index_template.settings']) {
settingsTemplate = {
template: {
settings: registryElasticsearch['index_template.settings'],
},
};
}
return { settingsTemplate, mappingsTemplate };
}
async function installDataStreamComponentTemplates(
templateName: string,
registryElasticsearch: RegistryElasticsearch | undefined,
callCluster: CallESAsCurrentUser
) {
const templates: string[] = [];
const componentPromises: Array<Promise<any>> = [];
const compTemplates = buildComponentTemplates(registryElasticsearch);
const mappings = putComponentTemplate(
compTemplates.mappingsTemplate,
`${templateName}-mappings`,
callCluster
);
const settings = putComponentTemplate(
compTemplates.settingsTemplate,
`${templateName}-settings`,
callCluster
);
if (mappings) {
templates.push(mappings.name);
componentPromises.push(mappings.clusterPromise);
}
if (settings) {
templates.push(settings.name);
componentPromises.push(settings.clusterPromise);
}
// TODO: Check return values for errors
await Promise.all(componentPromises);
return templates;
}
export async function installTemplate({
callCluster,
fields,
dataStream,
packageVersion,
packageName,
}: {
callCluster: CallESAsCurrentUser;
fields: Field[];
dataStream: RegistryDataStream;
packageVersion: string;
packageName: string;
}): Promise<TemplateRef> {
const mappings = generateMappings(processFields(fields));
const templateName = generateTemplateName(dataStream);
let pipelineName;
if (dataStream.ingest_pipeline) {
pipelineName = getPipelineNameForInstallation({
pipelineName: dataStream.ingest_pipeline,
dataStream,
packageVersion,
});
}
const composedOfTemplates = await installDataStreamComponentTemplates(
templateName,
dataStream.elasticsearch,
callCluster
);
const template = getTemplate({
type: dataStream.type,
templateName,
mappings,
pipelineName,
packageName,
composedOfTemplates,
});
// TODO: Check return values for errors
const callClusterParams: {
method: string;
path: string;
ignore: number[];
body: any;
} = {
method: 'PUT',
path: `/_index_template/${templateName}`,
ignore: [404],
body: template,
};
// This uses the catch-all endpoint 'transport.request' because there is no
// convenience endpoint using the new _index_template API yet.
// The existing convenience endpoint `indices.putTemplate` only sends to _template,
// which does not support v2 templates.
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
await callCluster('transport.request', callClusterParams);
return {
templateName,
indexTemplate: template,
};
}