Skip to content

Commit 1d4534f

Browse files
Introduce unified entrypoint for CLI scripts (#85821)
CLI scripts have a common infrastructure in that they call to the shared elasticsearch-cli shell script which launches them with the appropriate java command line. However, each underlying Java class must implement its own main method. This commit introduces a single main method to be shared by CLIs. The new CliToolLauncher takes in system properties to determine which tool is being run, and a new CliToolProvider SPI allows defining and finding the named tools. relates #85758 Co-authored-by: William Brafford <[email protected]>
1 parent 7dee198 commit 1d4534f

File tree

99 files changed

+801
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+801
-343
lines changed

distribution/build.gradle

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
230230
* Properties to expand when copying packaging files *
231231
*****************************************************************************/
232232
configurations {
233-
['libs', 'libsLaunchers', 'libsVersionChecker', 'libsPluginCli', 'libsKeystoreCli', 'libsSecurityCli', 'libsGeoIpCli', 'libsAnsiConsole'].each {
233+
['libs', 'libsLaunchers', 'libsVersionChecker', 'libsCliLauncher', 'libsPluginCli', 'libsKeystoreCli', 'libsSecurityCli', 'libsGeoIpCli', 'libsAnsiConsole'].each {
234234
create(it) {
235235
canBeConsumed = false
236236
canBeResolved = true
@@ -251,6 +251,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
251251
libs project(':server')
252252

253253
libsVersionChecker project(':distribution:tools:java-version-checker')
254+
libsCliLauncher project(':distribution:tools:cli-launcher')
254255
libsLaunchers project(':distribution:tools:launchers')
255256
libsAnsiConsole project(':distribution:tools:ansi-console')
256257
libsPluginCli project(':distribution:tools:plugin-cli')
@@ -274,6 +275,9 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
274275
into('java-version-checker') {
275276
from(configurations.libsVersionChecker)
276277
}
278+
into('cli-launcher') {
279+
from(configurations.libsCliLauncher)
280+
}
277281
into('tools/geoip-cli') {
278282
from(configurations.libsGeoIpCli)
279283
}
@@ -370,12 +374,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
370374
// module provided bin files
371375
with copySpec {
372376
eachFile { it.setMode(0755) }
373-
if (testDistro == false) {
374-
from(defaultBinFiles)
375-
} else {
376-
from(defaultBinFiles)
377-
include 'x-pack-env', 'x-pack-security-env'
378-
}
377+
from(defaultBinFiles)
379378
if (distributionType != 'zip') {
380379
exclude '*.bat'
381380
}

distribution/packages/src/common/scripts/postinst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,15 @@ if [ "x$IS_UPGRADE" != "xtrue" ]; then
5959
# Don't exit immediately on error, we want to hopefully print some helpful banners
6060
set +e
6161
# Attempt to auto-configure security, this seems to be an installation
62-
if ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode \
63-
ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
64-
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
62+
if CLI_NAME="auto-configure-node" \
63+
CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
6564
/usr/share/elasticsearch/bin/elasticsearch-cli <<< ""; then
6665
# Above command runs as root and TLS keystores are created group-owned by root. It's simple to correct the ownership here
6766
chown root:elasticsearch "${ES_PATH_CONF}"/certs/http.p12
6867
chown root:elasticsearch "${ES_PATH_CONF}"/certs/http_ca.crt
6968
chown root:elasticsearch "${ES_PATH_CONF}"/certs/transport.p12
70-
if INITIAL_PASSWORD=$(ES_MAIN_CLASS=org.elasticsearch.xpack.security.enrollment.tool.AutoConfigGenerateElasticPasswordHash \
71-
ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
72-
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
69+
if INITIAL_PASSWORD=$(CLI_NAME=auto-config-gen-passwd \
70+
CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
7371
/usr/share/elasticsearch/bin/elasticsearch-cli); then
7472
echo "--------------------------- Security autoconfiguration information ------------------------------"
7573
echo

distribution/src/bin/elasticsearch

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,17 @@ then
6666
fi
6767

6868
if [[ $ENROLL_TO_CLUSTER = true ]]; then
69-
ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode \
70-
ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
71-
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
69+
CLI_NAME="auto-configure-node" \
70+
CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
7271
bin/elasticsearch-cli "${ARG_LIST[@]}" <<<"$KEYSTORE_PASSWORD"
7372
elif [[ $ATTEMPT_SECURITY_AUTO_CONFIG = true ]]; then
7473
# It is possible that an auto-conf failure prevents the node from starting, but this is only the exceptional case (exit code 1).
7574
# Most likely an auto-conf failure will leave the configuration untouched (exit codes 73, 78 and 80), optionally printing a message
7675
# if the error is uncommon or unexpected, but it should otherwise let the node to start as usual.
7776
# It is passed in all the command line options in order to read the node settings ones (-E), while the other parameters are ignored
7877
# (a small caveat is that it also inspects the -v option in order to provide more information on how auto config went)
79-
if ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode \
80-
ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
81-
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
78+
if CLI_NAME="auto-configure-node" \
79+
CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
8280
bin/elasticsearch-cli "${ARG_LIST[@]}" <<<"$KEYSTORE_PASSWORD"; then
8381
:
8482
else

distribution/src/bin/elasticsearch-cli

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,21 @@ set -e -o pipefail
44

55
source "`dirname "$0"`"/elasticsearch-env
66

7-
IFS=';' read -r -a additional_sources <<< "$ES_ADDITIONAL_SOURCES"
8-
for additional_source in "${additional_sources[@]}"
9-
do
10-
source "$ES_HOME"/bin/$additional_source
11-
done
12-
13-
IFS=';' read -r -a additional_classpath_directories <<< "$ES_ADDITIONAL_CLASSPATH_DIRECTORIES"
14-
for additional_classpath_directory in "${additional_classpath_directories[@]}"
15-
do
16-
ES_CLASSPATH="$ES_CLASSPATH:$ES_HOME/$additional_classpath_directory/*"
17-
done
18-
197
# use a small heap size for the CLI tools, and thus the serial collector to
20-
# avoid stealing many CPU cycles; a user can override by setting ES_JAVA_OPTS
21-
ES_JAVA_OPTS="-Xms4m -Xmx64m -XX:+UseSerialGC ${ES_JAVA_OPTS}"
8+
# avoid stealing many CPU cycles; a user can override by setting CLI_JAVA_OPTS
9+
CLI_JAVA_OPTS="-Xms4m -Xmx64m -XX:+UseSerialGC ${CLI_JAVA_OPTS}"
10+
11+
LAUNCHER_CLASSPATH=$ES_HOME/lib/*:$ES_HOME/lib/cli-launcher/*
2212

2313
exec \
2414
"$JAVA" \
25-
$ES_JAVA_OPTS \
15+
$CLI_JAVA_OPTS \
16+
-Dcli.name="$CLI_NAME" \
17+
-Dcli.script="$0" \
18+
-Dcli.libs="$CLI_LIBS" \
2619
-Des.path.home="$ES_HOME" \
2720
-Des.path.conf="$ES_PATH_CONF" \
2821
-Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
29-
-cp "$ES_CLASSPATH" \
30-
"$ES_MAIN_CLASS" \
22+
-cp "$LAUNCHER_CLASSPATH" \
23+
org.elasticsearch.launcher.CliToolLauncher \
3124
"$@"
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
call "%~dp0elasticsearch-env.bat" || exit /b 1
1+
@echo off
22

3-
if defined ES_ADDITIONAL_SOURCES (
4-
for %%a in ("%ES_ADDITIONAL_SOURCES:;=","%") do (
5-
call "%~dp0%%a"
6-
)
7-
)
3+
setlocal enabledelayedexpansion
84

9-
if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES (
10-
for %%a in ("%ES_ADDITIONAL_CLASSPATH_DIRECTORIES:;=","%") do (
11-
set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/%%a/*
12-
)
13-
)
5+
call "%~dp0elasticsearch-env.bat" || exit /b 1
146

157
rem use a small heap size for the CLI tools, and thus the serial collector to
168
rem avoid stealing many CPU cycles; a user can override by setting ES_JAVA_OPTS
179
set ES_JAVA_OPTS=-Xms4m -Xmx64m -XX:+UseSerialGC %ES_JAVA_OPTS%
1810

11+
set LAUNCHER_CLASSPATH=%ES_HOME%/lib/*;%ES_HOME%/lib/cli-launcher/*
12+
1913
%JAVA% ^
20-
%ES_JAVA_OPTS% ^
14+
%CLI_JAVA_OPTS% ^
15+
-Dcli.name="%CLI_NAME%" ^
16+
-Dcli.script="%CLI_SCRIPT%" ^
17+
-Dcli.libs="%CLI_LIBS%" ^
2118
-Des.path.home="%ES_HOME%" ^
2219
-Des.path.conf="%ES_PATH_CONF%" ^
2320
-Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
24-
-cp "%ES_CLASSPATH%" ^
25-
"%ES_MAIN_CLASS%" ^
21+
-cp "%LAUNCHER_CLASSPATH%" ^
22+
org.elasticsearch.launcher.CliToolLauncher ^
2623
%*
2724

2825
exit /b %ERRORLEVEL%
26+
27+
endlocal
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/bin/bash
22

3-
ES_MAIN_CLASS=org.elasticsearch.geoip.GeoIpCli \
4-
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/geoip-cli \
5-
"`dirname "$0"`"/elasticsearch-cli \
6-
"$@"
3+
CLI_LIBS=lib/tools/geoip-cli
4+
source "`dirname "$0"`"/elasticsearch-cli

distribution/src/bin/elasticsearch-geoip.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
setlocal enabledelayedexpansion
44
setlocal enableextensions
55

6-
set ES_MAIN_CLASS=org.elasticsearch.geoip.GeoIpCli
7-
set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/geoip-cli
6+
set CLI_SCRIPT=%~0
7+
set CLI_LIBS=lib/tools/geoip-cli
88
call "%~dp0elasticsearch-cli.bat" ^
99
%%* ^
1010
|| goto exit
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/bin/bash
22

3-
ES_MAIN_CLASS=org.elasticsearch.cli.keystore.KeyStoreCli \
4-
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli \
5-
"`dirname "$0"`"/elasticsearch-cli \
6-
"$@"
3+
CLI_LIBS=lib/tools/keystore-cli
4+
source "`dirname "$0"`"/elasticsearch-cli

distribution/src/bin/elasticsearch-keystore.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
setlocal enabledelayedexpansion
44
setlocal enableextensions
55

6-
set ES_MAIN_CLASS=org.elasticsearch.cli.keystore.KeyStoreCli
7-
set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli
6+
set CLI_SCRIPT=%~0
7+
set CLI_LIBS=lib/tools/keystore-cli
88
call "%~dp0elasticsearch-cli.bat" ^
99
%%* ^
1010
|| goto exit
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
#!/bin/bash
22

3-
ES_MAIN_CLASS=org.elasticsearch.cluster.coordination.NodeToolCli \
4-
"`dirname "$0"`"/elasticsearch-cli \
5-
"$@"
3+
source "`dirname "$0"`"/elasticsearch-cli

0 commit comments

Comments
 (0)