-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·199 lines (164 loc) · 7.04 KB
/
cli.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
#!/usr/bin/env node
import { readFileSync, existsSync } from 'node:fs';
import { get } from './lib/config.js';
import { program, Option, InvalidArgumentError } from 'commander';
import { _error, _ok, _verbose2, _warning } from './lib/logging.js';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { prepareOpenSearch, runOpenSearch } from './lib/opensearch.js';
import { prepareDashboards, runDashboards } from './lib/dashboards.js';
import { camelCase, isGitHubSource, isVersion } from './lib/utils.js';
import { killSubprocesses } from './lib/subprocess.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const { version: pkgVersion } = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'));
const projects = get('projects');
const pluginSlugs = [];
const pluginDisplays = [];
for (const { slug, name } of projects) {
if (slug === 'dashboards') continue;
program.addOption(
new Option(`--${slug}-source <repo>`, `${name} plugin source to use`)
.implies({ [camelCase(`${slug}`)]: true })
.hideHelp(),
);
if (slug !== 'security') {
program.addOption(
new Option(`--no-${slug}`, `Prevent the inclusion of ${name} Dashboards plugin`)
.hideHelp(),
);
}
pluginSlugs.push(slug);
pluginDisplays.push(slug.padEnd(24, ' ') + name);
}
const fullVersion = input => {
const value = input?.trim?.();
if (isVersion(value)) return value;
throw new InvalidArgumentError('The value needs to be a complete version (e.g. 2.15.0).');
};
const gitOrVersionOrDirectory = input => {
const value = input?.trim?.();
if (isVersion(value)) return value;
if (isGitHubSource(value)) return value;
if (existsSync(value)) return value;
if (/[:\/]/.test(value))
throw new InvalidArgumentError(
'The value needs to be a complete version (e.g. 2.15.0) or in the form of github:user/repo/branch.');
return `github://${value}`;
};
const resolveDestination = value => {
const destination = value?.trim?.();
return resolve(destination);
};
program
.name('osd-launcher')
.description('CLI to ease the setup of OpenSearch and Dashboards')
.option('-os, --opensearch-version <version>', 'OpenSearch version to use', fullVersion)
.option(
'-osd, --dashboards-version <version|repo|directory>',
'Dashboards version to use\n<version>: use a released version\n<repo>: clone a git repo/branch/commit\n<directory>: configure and use existing code',
gitOrVersionOrDirectory,
)
.option(
'-d, --destination <path>',
'Location for deploying',
resolveDestination,
process.cwd(),
)
.option('--no-plugins', 'Prevent installation of Dashboards plugins')
.addOption(
new Option('--no-security', 'Disable the Security plugins in OpenSearch and Dashboards')
.conflicts(['securityVersion'])
)
.option('--refresh-downloads', 'Re-download artifacts even if they are available in cache')
.option(
'--opensearch-host <hostname|IP>',
'Hostname or IP address for OpenSearch to listen on',
'127.0.0.1',
)
.option('--opensearch-port <number>', 'Port number for OpenSearch to listen on', '9200')
.option(
'--dashboards-host <hostname|IP>',
'Hostname or IP address for OpenSearch to listen on',
'0.0.0.0',
)
.option('--dashboards-port <number>', 'Port number for OpenSearch to listen on', '5601')
.option('-u, --username <username>', 'Username to use if security is enable', 'admin')
.option('-p, --password <password>', 'Password to use if security is enabled')
.option('-dev --no-build', 'Skip building Dashboards when cloned')
// ToDo: Add ability to start installations as a service
//.option('--add-service', 'Create services for OpenSearch and Dashboards')
.version(pkgVersion, '-v, --version', 'Print launcher version')
.showHelpAfterError();
program.addHelpText('after', `
Fine-tuning Dashboards plugins:
The version of Dashboards plugins can be specified using --<name>-source <repo>.
The inclusion of a plugin can be prevented using --no-<name>.
Supported plugin names are:
${pluginDisplays.join('\n ')}
If a specific release version of Dashboards is requested, the plugins included with the
release will be installed and the fine-tuned source parameters have no effect.
Installing Dashboards from a GitHub source, if no plugin-specific source is requested, the
plugins will be cloned from the official sources.
<version> format:
A complete release version includes all 3 components of a semantic version. e.g. 2.15.0
<repo> format:
A GitHub source starts with "github:" and includes all 3 names of the use, the repository
and the branch:
github:opensearch-project/opensearch-dashboards/awesome-feature
A shorthand alternative is also supported to use a branch from the official repositories:
github://2.x
If Dashboards is cloned from a numeric branch name (e.g. 2.15 and 2.x), the plugins will
be cloned from the matching branch of the official sources, unless a specific source is
requested for them.
<dirctory> format:
A relative or absolute path to a local directory. e.g. /home/user/osd
Using a local directory, any existing plugins will be removed and fresh ones will be
cloned from the main branch of the official sources.
`);
program.parse();
const run = async () => {
const opts = program.opts();
if (opts.plugins !== true) {
opts.plugins = false;
for (const slug of pluginSlugs)
if (slug !== 'security' && !opts[camelCase(`${slug}-source`)])
opts[camelCase(slug)] = false;
}
if (isVersion(opts.dashboardsVersion)) opts.build = false;
if (opts.security === true && !opts.password)
return program.error('error: Password is required when security is enabled. Use -p or --password to set a password, or use --no-security to disable security.');
if (opts.security !== true && opts.password)
return program.error('error: Password cannot be set when security is disabled (--no-security).');
console.log(opts);
const osDir = await prepareOpenSearch(opts);
const osdDir = await prepareDashboards(opts);
let osChild, osdChild;
if (osDir) {
osChild = await runOpenSearch(osDir, 180, opts);
if (!osChild) throw `Failed to run OpenSearch`;
}
if (osdDir) {
if (osDir && !osChild) {
_warning(`Skipping Dashboards health-check!`);
} else {
osdChild = await runDashboards(osdDir, opts.build ? 1800 : 600, opts);
if (!osdChild) throw `Failed to run OpenSearch Dashboards`;
}
}
if (osDir && osdDir) {
if (osChild && osdChild)
_ok(`OpenSearch and Dashboards installed successfully.`);
} else if (osdDir && osdChild)
_ok(`Dashboards installed successfully.`);
else if (osDir && osChild)
_ok(`OpenSearch installed successfully.`);
killSubprocesses();
if (osDir && osChild)
_verbose2(`OpenSearch: http${opts.security === true ? 's' : ''}://${opts.opensearchHost}:${opts.opensearchPort}`);
if (osdDir && osdChild)
_verbose2(`Dashboards: http://${opts.dashboardsHost}:${opts.dashboardsPort}`);
};
run().catch(err => {
_error('Error:', err);
process.exit(1);
});