-
Notifications
You must be signed in to change notification settings - Fork 916
/
cli.ts
267 lines (239 loc) · 9.08 KB
/
cli.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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* 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.
*/
/** ***********************************************************
*
* Run `node scripts/opensearch_archiver --help` for usage information
*
*************************************************************/
import Path from 'path';
import readline from 'readline';
import { RunWithCommands, createFlagError } from '@osd/dev-utils';
import { Client, ClientOptions } from '@opensearch-project/opensearch';
import { readConfigFile } from '@osd/test';
import { OpenSearchArchiver } from './opensearch_archiver';
const resolveConfigPath = (v: string) => Path.resolve(process.cwd(), v);
const defaultConfigPath = resolveConfigPath('test/functional/config.js');
export function runCli() {
new RunWithCommands({
description: 'CLI to manage archiving/restoring data in opensearch',
globalFlags: {
string: ['opensearch-url', 'opensearch-dashboards-url', 'dir', 'config'],
help: `
--config path to an FTR config file that sets --opensearch-url, --opensearch-dashboards-url, and --dir
default: ${defaultConfigPath}
--opensearch-url url for OpenSearch, prefer the --config flag
--opensearch-dashboards-url url for OpenSearch Dashboards, prefer the --config flag
--dir where archives are stored, prefer the --config flag
`,
},
async extendContext({ log, flags, addCleanupTask }) {
const configPath = flags.config || defaultConfigPath;
if (typeof configPath !== 'string') {
throw createFlagError('--config must be a string');
}
const config = await readConfigFile(log, Path.resolve(configPath));
let opensearchUrl = flags['opensearch-url'];
if (opensearchUrl && typeof opensearchUrl !== 'string') {
throw createFlagError('--opensearch-url must be a string');
}
if (!opensearchUrl && config) {
opensearchUrl = config.get('servers.opensearch.serverUrl');
}
if (!opensearchUrl) {
throw createFlagError('--opensearch-url or --config must be defined');
}
let opensearchDashboardsUrl = flags['opensearch-dashboards-url'];
if (opensearchDashboardsUrl && typeof opensearchDashboardsUrl !== 'string') {
throw createFlagError('--opensearch-dashboards-url must be a string');
}
if (!opensearchDashboardsUrl && config) {
opensearchDashboardsUrl = config.get('servers.opensearchDashboards.serverUrl') as string;
}
if (!opensearchDashboardsUrl) {
throw createFlagError('---url or --config must be defined');
}
let dir = flags.dir;
if (dir && typeof dir !== 'string') {
throw createFlagError('--dir must be a string');
}
if (!dir && config) {
dir = Path.resolve(config.get('opensearchArchiver.directory'));
}
if (!dir) {
throw createFlagError('--dir or --config must be defined');
}
const clientOptions: ClientOptions = {
node: opensearchUrl.toString(),
};
const client = new Client(clientOptions);
addCleanupTask(() => client.close());
const opensearchArchiver = new OpenSearchArchiver({
log,
client,
dataDir: dir,
opensearchDashboardsUrl,
});
return {
opensearchArchiver,
};
},
})
.command({
name: 'save',
usage: 'save [name] [...indices]',
description: `
archive the [indices ...] into the --dir with [name]
Example:
Save all [logstash-*] indices from http://localhost:9200 to [snapshots/my_test_data] directory
WARNING: If the [my_test_data] snapshot exists it will be deleted!
$ node scripts/opensearch_archiver save my_test_data logstash-* --dir snapshots
`,
flags: {
boolean: ['raw'],
string: ['query'],
help: `
--raw don't gzip the archives
--query query object to limit the documents being archived, needs to be properly escaped JSON
`,
},
async run({ flags, opensearchArchiver }) {
const [name, ...indices] = flags._;
if (!name) {
throw createFlagError('missing [name] argument');
}
if (!indices.length) {
throw createFlagError('missing [...indices] arguments');
}
const raw = flags.raw;
if (typeof raw !== 'boolean') {
throw createFlagError('--raw does not take a value');
}
const query = flags.query;
let parsedQuery;
if (typeof query === 'string' && query.length > 0) {
try {
parsedQuery = JSON.parse(query);
} catch (err) {
throw createFlagError('--query should be valid JSON');
}
}
await opensearchArchiver.save(name, indices, { raw, query: parsedQuery });
},
})
.command({
name: 'load',
usage: 'load [name]',
description: `
load the archive in --dir with [name]
Example:
Load the [my_test_data] snapshot from the archive directory and opensearch instance defined
in the [test/functional/config.js] config file
WARNING: If the indices exist already they will be deleted!
$ node scripts/opensearch_archiver load my_test_data --config test/functional/config.js
`,
flags: {
boolean: ['use-create'],
help: `
--use-create use create instead of index for loading documents
`,
},
async run({ flags, opensearchArchiver }) {
const [name] = flags._;
if (!name) {
throw createFlagError('missing [name] argument');
}
if (flags._.length > 1) {
throw createFlagError(`unknown extra arguments: [${flags._.slice(1).join(', ')}]`);
}
const useCreate = flags['use-create'];
if (typeof useCreate !== 'boolean') {
throw createFlagError('--use-create does not take a value');
}
await opensearchArchiver.load(name, { useCreate });
},
})
.command({
name: 'unload',
usage: 'unload [name]',
description: 'remove indices created by the archive in --dir with [name]',
async run({ flags, opensearchArchiver }) {
const [name] = flags._;
if (!name) {
throw createFlagError('missing [name] argument');
}
if (flags._.length > 1) {
throw createFlagError(`unknown extra arguments: [${flags._.slice(1).join(', ')}]`);
}
await opensearchArchiver.unload(name);
},
})
.command({
name: 'edit',
usage: 'edit [prefix]',
description:
'extract the archives under the prefix, wait for edits to be completed, and then recompress the archives',
async run({ flags, opensearchArchiver }) {
const [prefix] = flags._;
if (!prefix) {
throw createFlagError('missing [prefix] argument');
}
if (flags._.length > 1) {
throw createFlagError(`unknown extra arguments: [${flags._.slice(1).join(', ')}]`);
}
await opensearchArchiver.edit(prefix, async () => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
await new Promise<void>((resolveInput) => {
rl.question(`Press enter when you're done`, () => {
rl.close();
resolveInput();
});
});
});
},
})
.command({
name: 'empty-opensearch-dashboards-index',
description:
'[internal] Delete any OpenSearch Dashboards indices, and initialize the OpenSearch Dashboards index as OpenSearch Dashboards would do on startup.',
async run({ opensearchArchiver }) {
await opensearchArchiver.emptyOpenSearchDashboardsIndex();
},
})
.command({
name: 'rebuild-all',
description: '[internal] read and write all archives in --dir to remove any inconsistencies',
async run({ opensearchArchiver }) {
await opensearchArchiver.rebuildAll();
},
})
.execute();
}