This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cluster.js
303 lines (258 loc) · 8.24 KB
/
cluster.js
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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
const execa = require('execa');
const chalk = require('chalk');
const path = require('path');
const { downloadSnapshot, installSnapshot, installSource, installArchive } = require('./install');
const { ES_BIN } = require('./paths');
const {
log: defaultLog,
parseEsLog,
extractConfigFiles,
decompress,
NativeRealm,
} = require('./utils');
const { createCliError } = require('./errors');
const { promisify } = require('util');
const treeKillAsync = promisify(require('tree-kill'));
// listen to data on stream until map returns anything but undefined
const first = (stream, map) =>
new Promise(resolve => {
const onData = data => {
const result = map(data);
if (result !== undefined) {
resolve(result);
stream.removeListener('data', onData);
}
};
stream.on('data', onData);
});
exports.Cluster = class Cluster {
constructor(log = defaultLog) {
this._log = log;
}
/**
* Builds and installs ES from source
*
* @param {Object} options
* @property {Array} options.installPath
* @property {Array} options.sourcePath
* @returns {Promise<{installPath}>}
*/
async installSource(options = {}) {
this._log.info(chalk.bold('Installing from source'));
this._log.indent(4);
const { installPath } = await installSource({ log: this._log, ...options });
this._log.indent(-4);
return { installPath };
}
/**
* Download ES from a snapshot
*
* @param {Object} options
* @property {Array} options.installPath
* @property {Array} options.sourcePath
* @returns {Promise<{installPath}>}
*/
async downloadSnapshot(options = {}) {
this._log.info(chalk.bold('Downloading snapshot'));
this._log.indent(4);
const { installPath } = await downloadSnapshot({
log: this._log,
...options,
});
this._log.indent(-4);
return { installPath };
}
/**
* Download and installs ES from a snapshot
*
* @param {Object} options
* @property {Array} options.installPath
* @property {Array} options.sourcePath
* @returns {Promise<{installPath}>}
*/
async installSnapshot(options = {}) {
this._log.info(chalk.bold('Installing from snapshot'));
this._log.indent(4);
const { installPath } = await installSnapshot({
log: this._log,
...options,
});
this._log.indent(-4);
return { installPath };
}
/**
* Installs ES from a local tar
*
* @param {String} path
* @param {Object} options
* @property {Array} options.installPath
* @returns {Promise<{installPath}>}
*/
async installArchive(path, options = {}) {
this._log.info(chalk.bold('Installing from an archive'));
this._log.indent(4);
const { installPath } = await installArchive(path, {
log: this._log,
...options,
});
this._log.indent(-4);
return { installPath };
}
/**
* Unpakcs a tar or zip file containing the data directory for an
* ES cluster.
*
* @param {String} installPath
* @param {String} archivePath
*/
async extractDataDirectory(installPath, archivePath) {
this._log.info(chalk.bold(`Extracting data directory`));
this._log.indent(4);
// decompress excludes the root directory as that is how our archives are
// structured. This works in our favor as we can explicitly extract into the data dir
const extractPath = path.resolve(installPath, 'data');
this._log.info(`Data archive: ${archivePath}`);
this._log.info(`Extract path: ${extractPath}`);
await decompress(archivePath, extractPath);
this._log.indent(-4);
}
/**
* Starts ES and returns resolved promise once started
*
* @param {String} installPath
* @param {Object} options
* @property {Array} options.esArgs
* @property {String} options.password - super user password used to bootstrap
* @returns {Promise}
*/
async start(installPath, options = {}) {
this._exec(installPath, options);
await Promise.race([
// wait for native realm to be setup and es to be started
Promise.all([
first(this._process.stdout, data => {
if (/started/.test(data)) {
return true;
}
}),
this._nativeRealmSetup,
]),
// await the outcome of the process in case it exits before starting
this._outcome.then(() => {
throw createCliError('ES exited without starting');
}),
]);
}
/**
* Starts Elasticsearch and waits for Elasticsearch to exit
*
* @param {String} installPath
* @param {Object} options
* @property {Array} options.esArgs
* @returns {Promise<undefined>}
*/
async run(installPath, options = {}) {
this._exec(installPath, options);
// log native realm setup errors so they aren't uncaught
this._nativeRealmSetup.catch(error => {
this._log.error(error);
this.stop();
});
// await the final outcome of the process
await this._outcome;
}
/**
* Stops ES process, if it's running
*
* @returns {Promise}
*/
async stop() {
if (this._stopCalled) {
return;
}
this._stopCalled = true;
if (!this._process || !this._outcome) {
throw new Error('ES has not been started');
}
await treeKillAsync(this._process.pid);
await this._outcome;
}
/**
* Common logic from this.start() and this.run()
*
* Start the elasticsearch process (stored at `this._process`)
* and "pipe" its stdio to `this._log`. Also create `this._outcome`
* which will be resolved/rejected when the process exits.
*
* @private
* @param {String} installPath
* @param {Object} options
* @property {Array} options.esArgs
* @return {undefined}
*/
_exec(installPath, options = {}) {
if (this._process || this._outcome) {
throw new Error('ES has already been started');
}
this._log.info(chalk.bold('Starting'));
this._log.indent(4);
const args = extractConfigFiles(options.esArgs || [], installPath, {
log: this._log,
}).reduce((acc, cur) => acc.concat(['-E', cur]), []);
this._log.debug('%s %s', ES_BIN, args.join(' '));
this._process = execa(ES_BIN, args, {
cwd: installPath,
stdio: ['ignore', 'pipe', 'pipe'],
});
// parse log output to find http port
const httpPort = first(this._process.stdout, data => {
const match = data.toString('utf8').match(/HttpServer.+publish_address {[0-9.]+:([0-9]+)/);
if (match) {
return match[1];
}
});
// once the http port is available setup the native realm
this._nativeRealmSetup = httpPort.then(async port => {
const nativeRealm = new NativeRealm(options.password, port, this._log);
await nativeRealm.setPasswords(options);
});
// parse and forward es stdout to the log
this._process.stdout.on('data', data => {
const lines = parseEsLog(data.toString());
lines.forEach(line => {
this._log.info(line.formattedMessage);
});
});
// forward es stderr to the log
this._process.stderr.on('data', data => this._log.error(chalk.red(data.toString())));
// observe the exit code of the process and reflect in _outcome promies
const exitCode = new Promise(resolve => this._process.once('exit', resolve));
this._outcome = exitCode.then(code => {
if (this._stopCalled) {
return;
}
// JVM exits with 143 on SIGTERM and 130 on SIGINT, dont' treat them as errors
if (code > 0 && !(code === 143 || code === 130)) {
throw createCliError(`ES exited with code ${code}`);
}
});
}
};