-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
226 lines (197 loc) · 6.25 KB
/
index.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
const spawnSync = require('child_process').spawnSync;
const fs = require('fs');
const os = require('os');
const path = require('path');
const process = require('process');
const versionMap = {
'2': '2.19.1',
'1': '1.3.20',
'2.19': '2.19.1',
'2.18': '2.18.0',
'2.17': '2.17.0',
'2.16': '2.16.0',
'2.15': '2.15.0',
'2.14': '2.14.0',
'2.13': '2.13.0',
'2.12': '2.12.0',
'2.11': '2.11.1',
'2.10': '2.10.0',
'2.9': '2.9.0',
'2.8': '2.8.0',
'2.7': '2.7.0',
'2.6': '2.6.0',
'2.5': '2.5.0',
'2.4': '2.4.1',
'2.3': '2.3.0',
'2.2': '2.2.1',
'2.1': '2.1.0',
'2.0': '2.0.1',
'1.3': '1.3.20',
'1.2': '1.2.4',
'1.1': '1.1.0',
'1.0': '1.0.1'
};
function run() {
const args = Array.from(arguments);
console.log(args.join(' '));
const command = args.shift();
// spawn is safer and more lightweight than exec
const ret = spawnSync(command, args, {stdio: 'inherit'});
if (ret.status !== 0) {
throw ret.error;
}
}
function addToEnv(value) {
fs.appendFileSync(process.env.GITHUB_ENV, `${value}\n`);
}
function addToPath(value) {
fs.appendFileSync(process.env.GITHUB_PATH, `${value}\n`);
}
function getVersion() {
let version = process.env['INPUT_OPENSEARCH-VERSION'] || '2';
if (versionMap[version]) {
version = versionMap[version];
}
if (!/^[21]\.\d{1,2}\.\d{1,2}$/.test(version)) {
throw `OpenSearch version not supported: ${version}`;
}
if (isWindows() && (version[0] == '1' || parseInt(version.split('.')[1]) < 4)) {
throw `OpenSearch version not supported on Windows (requires 2.4+)`;
}
return version;
}
function isWindows() {
return process.platform == 'win32';
}
function getUrl() {
let arch = process.arch;
if (!['x64', 'arm64'].includes(arch)) {
throw `Unsupported architecture: ${arch}`;
}
let url;
if (process.platform == 'darwin') {
// TODO use Mac build when available
// https://github.com/opensearch-project/opensearch-build/issues/38
url = `https://artifacts.opensearch.org/releases/bundle/opensearch/${opensearchVersion}/opensearch-${opensearchVersion}-linux-${arch}.tar.gz`;
} else if (isWindows()) {
url = `https://artifacts.opensearch.org/releases/bundle/opensearch/${opensearchVersion}/opensearch-${opensearchVersion}-windows-x64.zip`;
} else {
url = `https://artifacts.opensearch.org/releases/bundle/opensearch/${opensearchVersion}/opensearch-${opensearchVersion}-linux-${arch}.tar.gz`;
}
return url;
}
function download() {
const url = getUrl();
if (isWindows()) {
run('curl', '-s', '-o', 'opensearch.zip', url);
run('unzip', '-q', 'opensearch.zip');
} else {
run('wget', '-q', '-O', 'opensearch.tar.gz', url);
run('tar', 'xfz', 'opensearch.tar.gz');
}
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, {recursive: true});
}
if (isWindows()) {
// fix for: cross-device link not permitted
run('mv', `opensearch-${opensearchVersion}`, opensearchHome)
} else {
fs.renameSync(`opensearch-${opensearchVersion}`, opensearchHome);
}
}
// log4j
function fixLog4j() {
// string comparison not ideal, but works for current versions
if (opensearchVersion >= '1.2.2') {
return;
}
const jvmOptionsPath = path.join(opensearchHome, 'config', 'jvm.options');
if (!fs.readFileSync(jvmOptionsPath).includes('log4j2.formatMsgNoLookups')) {
fs.appendFileSync(jvmOptionsPath, '\n-Dlog4j2.formatMsgNoLookups=true\n');
// remove jndi for extra safety
const coreJarPath = fs.readdirSync(path.join(opensearchHome, 'lib')).filter(fn => fn.includes('log4j-core-'))[0];
run('zip', '-q', '-d', path.join(opensearchHome, 'lib', coreJarPath), 'org/apache/logging/log4j/core/lookup/JndiLookup.class');
}
}
function installPlugins() {
let plugins = (process.env['INPUT_PLUGINS'] || '').trim();
if (plugins.length > 0) {
console.log('Installing plugins');
// split here instead of above since JS returns [''] for empty array
plugins = plugins.split(/\s*[,\n]\s*/);
// validate
plugins.forEach( function(plugin) {
if (!/^\w\S+$/.test(plugin)) {
throw `Invalid plugin: ${plugin}`;
}
});
let pluginCmd = path.join(opensearchHome, 'bin', 'opensearch-plugin');
if (isWindows()) {
pluginCmd += '.bat';
}
run(pluginCmd, 'install', '--silent', '--batch', ...plugins);
}
}
function setConfig(dir) {
let config = process.env['INPUT_CONFIG'] || '';
config += '\n';
config += 'plugins.security.disabled: true\n';
config += 'discovery.type: single-node\n';
const file = path.join(dir, 'config', 'opensearch.yml');
// overwrite instead of append to play nicely with caching
// alternatively, could append to copy of original file
fs.writeFileSync(file, config);
}
function startServer() {
if (isWindows()) {
const serviceCmd = path.join(opensearchHome, 'bin', 'opensearch-service.bat');
run(serviceCmd, 'install');
run(serviceCmd, 'start');
} else {
run(path.join(opensearchHome, 'bin', 'opensearch'), '-d');
}
}
function getPort() {
const config = process.env['INPUT_CONFIG'] || '';
const match = config.match(/\bhttp\.port: +(\d{4,5})\b/);
return match ? parseInt(match[1]) : 9200;
}
function waitForReady() {
console.log("Waiting for server to be ready");
for (let i = 0; i < 30; i++) {
let ret = spawnSync('curl', ['-s', `localhost:${getPort()}`]);
if (ret.status === 0) {
break;
}
spawnSync('sleep', ['1']);
}
}
const opensearchVersion = getVersion();
const cacheDir = path.join(os.homedir(), 'opensearch');
const opensearchHome = path.join(cacheDir, opensearchVersion);
// java compatibility
// https://opensearch.org/docs/latest/opensearch/install/compatibility/
const javaHome = process.env.JAVA_HOME_11_X64;
// not set on ubuntu-22.04, but defaults to Java 17
if (javaHome) {
process.env.OPENSEARCH_JAVA_HOME = javaHome;
addToEnv(`OPENSEARCH_JAVA_HOME=${javaHome}`);
if (isWindows()) {
process.env.JAVA_HOME = javaHome;
}
}
if (!fs.existsSync(opensearchHome)) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'opensearch-'));
process.chdir(tmpDir);
download();
fixLog4j();
installPlugins();
} else {
console.log('OpenSearch cached');
fixLog4j();
}
setConfig(opensearchHome);
startServer();
waitForReady();
addToEnv(`OPENSEARCH_HOME=${opensearchHome}`);
addToPath(path.join(opensearchHome, 'bin'));