-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
install_all_packages.ts
118 lines (98 loc) · 3.13 KB
/
install_all_packages.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import fetch from 'node-fetch';
import { kibanaPackageJson } from '@kbn/utils';
import { ToolingLog } from '@kbn/tooling-log';
const REGISTRY_URL = 'https://epr-snapshot.elastic.co';
const KIBANA_URL = 'http://localhost:5601';
const KIBANA_USERNAME = 'elastic';
const KIBANA_PASSWORD = 'changeme';
const KIBANA_VERSION = kibanaPackageJson.version;
const SKIP_PACKAGES: string[] = [];
async function installPackage(name: string, version: string) {
const start = Date.now();
const res = await fetch(`${KIBANA_URL}/api/fleet/epm/packages/${name}/${version}`, {
headers: {
accept: '*/*',
'content-type': 'application/json',
'kbn-xsrf': 'xyz',
Authorization:
'Basic ' + Buffer.from(`${KIBANA_USERNAME}:${KIBANA_PASSWORD}`).toString('base64'),
},
body: JSON.stringify({ force: true }),
method: 'POST',
});
const end = Date.now();
const body = await res.json();
return { body, status: res.status, took: (end - start) / 1000 };
}
async function deletePackage(name: string, version: string) {
const res = await fetch(`${KIBANA_URL}/api/fleet/epm/packages/${name}-${version}`, {
headers: {
accept: '*/*',
'content-type': 'application/json',
'kbn-xsrf': 'xyz',
Authorization:
'Basic ' + Buffer.from(`${KIBANA_USERNAME}:${KIBANA_PASSWORD}`).toString('base64'),
},
method: 'DELETE',
});
const body = await res.json();
return { body, status: res.status };
}
async function getAllPackages() {
const res = await fetch(
`${REGISTRY_URL}/search?experimental=true&kibana.version=${KIBANA_VERSION}`,
{
headers: {
accept: '*/*',
},
method: 'GET',
}
);
const body = await res.json();
return body;
}
function logResult(
logger: ToolingLog,
pkg: { name: string; version: string },
result: { took?: number; status?: number }
) {
const pre = `${pkg.name}-${pkg.version} ${result.took ? ` took ${result.took}s` : ''} : `;
if (result.status !== 200) {
logger.info('❌ ' + pre + JSON.stringify(result));
} else {
logger.info('✅ ' + pre + 200);
}
}
export async function run() {
const logger = new ToolingLog({
level: 'info',
writeTo: process.stdout,
});
const allPackages = await getAllPackages();
logger.info('INSTALLING packages');
for (const pkg of allPackages) {
if (SKIP_PACKAGES.includes(pkg.name)) {
logger.info(`Skipping ${pkg.name}`);
continue;
}
const result = await installPackage(pkg.name, pkg.version);
logResult(logger, pkg, result);
}
const deletePackages = process.argv.includes('--delete');
if (!deletePackages) return;
logger.info('DELETING packages');
for (const pkg of allPackages) {
if (SKIP_PACKAGES.includes(pkg.name)) {
logger.info(`Skipping ${pkg.name}`);
continue;
}
const result = await deletePackage(pkg.name, pkg.version);
logResult(logger, pkg, result);
}
}