Apache Solr open-source search software
SOLR-16711: Extract SolrCLI tool implementations into their own package and classes (Take 2) (#1568)
Eric Pugh346e1b92f27d
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex d3a39f5067a..bd655230fef 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -96,6 +96,8 @@ Improvements* SOLR-16752: Docker: Leave out optional apt packages, slightly reducing image size and lowering attack surface (Quang-Cuong Bui)+* SOLR-16711: Extract SolrCLI tool implementations into their own classes and reorganize CLI related classes into new org.apache.solr.cli package. (Eric Pugh)+Optimizations---------------------diff --git a/solr/bin/post b/solr/bin/postindex dd9407e9ad3..c9ad8510802 100755--- a/solr/bin/post+++ b/solr/bin/post@@ -165,7 +165,7 @@ while [ $# -gt 0 ]; doif [[ "${1:2:4}" == "url=" ]]; thenSOLR_URL=${1:6}fi- else+ elsekey="${1:1}"shift# echo "$1: PROP"@@ -231,8 +231,8 @@ if [[ -n "$RECURSIVE" ]]; thenPROPS+=('-Drecursive=yes')fi-echo "$JAVA" -classpath "${TOOL_JAR[0]}" "${PROPS[@]}" org.apache.solr.util.SimplePostTool "${PARAMS[@]}"-"$JAVA" -classpath "${TOOL_JAR[0]}" "${PROPS[@]}" org.apache.solr.util.SimplePostTool "${PARAMS[@]}"+echo "$JAVA" -classpath "${TOOL_JAR[0]}" "${PROPS[@]}" org.apache.solr.cli.SimplePostTool "${PARAMS[@]}"+"$JAVA" -classpath "${TOOL_JAR[0]}" "${PROPS[@]}" org.apache.solr.cli.SimplePostTool "${PARAMS[@]}"# post smoker:# bin/post -c signals -out yes -type application/json -d '[{"id": 2, "val": 0.47}]'diff --git a/solr/bin/postlogs b/solr/bin/postlogsindex 98d8e90407d..71a5e7a255d 100755--- a/solr/bin/postlogs+++ b/solr/bin/postlogs@@ -33,4 +33,4 @@SOLR_TIP="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"/..-java -classpath "$SOLR_TIP/server/lib/ext/*:$SOLR_TIP/server/solr-webapp/webapp/WEB-INF/lib/*" org.apache.solr.util.SolrLogPostTool $1 $2+java -classpath "$SOLR_TIP/server/lib/ext/*:$SOLR_TIP/server/solr-webapp/webapp/WEB-INF/lib/*" org.apache.solr.cli.SolrLogPostTool $1 $2diff --git a/solr/bin/solr b/solr/bin/solrindex 270b713be4f..11f441c0059 100644--- a/solr/bin/solr+++ b/solr/bin/solr@@ -739,7 +739,7 @@ function run_tool() {"$JAVA" $SOLR_SSL_OPTS $AUTHC_OPTS ${SOLR_ZK_CREDS_AND_ACLS:-} -Dsolr.install.dir="$SOLR_TIP" \-Dlog4j.configurationFile="$DEFAULT_SERVER_DIR/resources/log4j2-console.xml" \-classpath "$DEFAULT_SERVER_DIR/solr-webapp/webapp/WEB-INF/lib/*:$DEFAULT_SERVER_DIR/lib/ext/*:$DEFAULT_SERVER_DIR/lib/*" \- org.apache.solr.util.SolrCLI "$@"+ org.apache.solr.cli.SolrCLI "$@"return $?} # end run_tool functiondiff --git a/solr/bin/solr.cmd b/solr/bin/solr.cmdindex 6a025dab2c6..69ce6258927 100755--- a/solr/bin/solr.cmd+++ b/solr/bin/solr.cmd@@ -1432,7 +1432,7 @@ IF "%FG%"=="1" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" -Dsolr.default.confdir="%DEFAULT_CONFDIR%"^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI status -maxWaitSecs !SOLR_START_WAIT! -solr !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:%SOLR_PORT%/solr+ org.apache.solr.cli.SolrCLI status -maxWaitSecs !SOLR_START_WAIT! -solr !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:%SOLR_PORT%/solrIF NOT "!ERRORLEVEL!"=="0" (set "SCRIPT_ERROR=Solr did not start or was not reachable. Check the logs for errors."goto err@@ -1448,7 +1448,7 @@ REM Run the requested example-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-Dsolr.install.symDir="%SOLR_TIP%" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI run_example -script "%SDIR%\solr.cmd" -e %EXAMPLE% -d "%SOLR_SERVER_DIR%" ^+ org.apache.solr.cli.SolrCLI run_example -script "%SDIR%\solr.cmd" -e %EXAMPLE% -d "%SOLR_SERVER_DIR%" ^-urlScheme !SOLR_URL_SCHEME! !PASS_TO_RUN_EXAMPLE!REM End of run_example@@ -1470,7 +1470,7 @@ for /f "usebackq" %%i in (`dir /b "%SOLR_TIP%\bin" ^| findstr /i "^solr-.*\.port"%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI status -solr !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:!SOME_SOLR_PORT!/solr+ org.apache.solr.cli.SolrCLI status -solr !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:!SOME_SOLR_PORT!/solr@echo.))@@ -1519,14 +1519,14 @@ echo ZK_HOST: !ZK_HOST!"%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI healthcheck -collection !HEALTHCHECK_COLLECTION! -zkHost !ZK_HOST! %HEALTHCHECK_VERBOSE%+ org.apache.solr.cli.SolrCLI healthcheck -collection !HEALTHCHECK_COLLECTION! -zkHost !ZK_HOST! %HEALTHCHECK_VERBOSE%goto done:run_solrcli"%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI %*+ org.apache.solr.cli.SolrCLI %*if errorlevel 1 (exit /b 1)@@ -1562,7 +1562,7 @@ IF NOT "!SOLR_URL_SCHEME!"=="" SET "CONFIG_ARGS=!CONFIG_ARGS! -scheme !SOLR_URL_"%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI config !CONFIG_ARGS!+ org.apache.solr.cli.SolrCLI config !CONFIG_ARGS!if errorlevel 1 (exit /b 1)@@ -1572,7 +1572,7 @@ goto done"%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI version+ org.apache.solr.cli.SolrCLI versiongoto done:parse_create_args@@ -1675,13 +1675,13 @@ if "%SCRIPT_CMD%"=="create_core" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI create_core -name !CREATE_NAME! -solrUrl !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:!CREATE_PORT!/solr ^+ org.apache.solr.cli.SolrCLI create_core -name !CREATE_NAME! -solrUrl !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:!CREATE_PORT!/solr ^-confdir !CREATE_CONFDIR! -configsetsDir "%SOLR_TIP%\server\solr\configsets" %CREATE_VERBOSE%) else ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" -Dsolr.default.confdir="%DEFAULT_CONFDIR%"^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI create -name !CREATE_NAME! -shards !CREATE_NUM_SHARDS! -replicationFactor !CREATE_REPFACT! ^+ org.apache.solr.cli.SolrCLI create -name !CREATE_NAME! -shards !CREATE_NUM_SHARDS! -replicationFactor !CREATE_REPFACT! ^-confname !CREATE_CONFNAME! -confdir !CREATE_CONFDIR! -configsetsDir "%SOLR_TIP%\server\solr\configsets" ^-solrUrl !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:!CREATE_PORT!/solr %CREATE_VERBOSE%)@@ -1756,7 +1756,7 @@ if "!DELETE_CONFIG!"=="" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^-org.apache.solr.util.SolrCLI delete -name !DELETE_NAME! -deleteConfig !DELETE_CONFIG! ^+org.apache.solr.cli.SolrCLI delete -name !DELETE_NAME! -deleteConfig !DELETE_CONFIG! ^-solrUrl !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:!DELETE_PORT!/solr %DELETE_VERBOSE%goto done@@ -1892,7 +1892,7 @@ IF "!ZK_OP!"=="upconfig" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI !ZK_OP! -confname !CONFIGSET_NAME! -confdir !CONFIGSET_DIR! -zkHost !ZK_HOST! %ZK_VERBOSE%^+ org.apache.solr.cli.SolrCLI !ZK_OP! -confname !CONFIGSET_NAME! -confdir !CONFIGSET_DIR! -zkHost !ZK_HOST! %ZK_VERBOSE%^-configsetsDir "%SOLR_TIP%/server/solr/configsets") ELSE IF "!ZK_OP!"=="downconfig" (IF "!CONFIGSET_NAME!"=="" (@@ -1906,7 +1906,7 @@ IF "!ZK_OP!"=="upconfig" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI !ZK_OP! -confname !CONFIGSET_NAME! -confdir !CONFIGSET_DIR! -zkHost !ZK_HOST! %ZK_VERBOSE%+ org.apache.solr.cli.SolrCLI !ZK_OP! -confname !CONFIGSET_NAME! -confdir !CONFIGSET_DIR! -zkHost !ZK_HOST! %ZK_VERBOSE%) ELSE IF "!ZK_OP!"=="cp" (IF "%ZK_SRC%"=="" (set ERROR_MSG="<src> must be specified for 'cp' command"@@ -1925,7 +1925,7 @@ IF "!ZK_OP!"=="upconfig" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -src !ZK_SRC! -dst !ZK_DST! -recurse !ZK_RECURSE! %ZK_VERBOSE%+ org.apache.solr.cli.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -src !ZK_SRC! -dst !ZK_DST! -recurse !ZK_RECURSE! %ZK_VERBOSE%) ELSE IF "!ZK_OP!"=="mv" (IF "%ZK_SRC%"=="" (set ERROR_MSG="<src> must be specified for 'mv' command"@@ -1938,7 +1938,7 @@ IF "!ZK_OP!"=="upconfig" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -src !ZK_SRC! -dst !ZK_DST! %ZK_VERBOSE%+ org.apache.solr.cli.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -src !ZK_SRC! -dst !ZK_DST! %ZK_VERBOSE%) ELSE IF "!ZK_OP!"=="rm" (IF "%ZK_SRC"=="" (set ERROR_MSG="Zookeeper path to remove must be specified when using the 'rm' command"@@ -1947,7 +1947,7 @@ IF "!ZK_OP!"=="upconfig" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! -recurse !ZK_RECURSE! %ZK_VERBOSE%+ org.apache.solr.cli.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! -recurse !ZK_RECURSE! %ZK_VERBOSE%) ELSE IF "!ZK_OP!"=="ls" (IF "%ZK_SRC"=="" (set ERROR_MSG="Zookeeper path to remove must be specified when using the 'ls' command"@@ -1956,7 +1956,7 @@ IF "!ZK_OP!"=="upconfig" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! -recurse !ZK_RECURSE! %ZK_VERBOSE%+ org.apache.solr.cli.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! -recurse !ZK_RECURSE! %ZK_VERBOSE%) ELSE IF "!ZK_OP!"=="mkroot" (IF "%ZK_SRC"=="" (set ERROR_MSG="Zookeeper path to create must be specified when using the 'mkroot' command"@@ -1965,7 +1965,7 @@ IF "!ZK_OP!"=="upconfig" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%SOLR_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! %ZK_VERBOSE%+ org.apache.solr.cli.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! %ZK_VERBOSE%) ELSE (set ERROR_MSG="Unknown zk option !ZK_OP!"goto zk_short_usage@@ -2026,7 +2026,7 @@ if "!AUTH_PORT!"=="" ("%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^- org.apache.solr.util.SolrCLI auth %AUTH_PARAMS% -solrIncludeFile "%SOLR_INCLUDE%" -authConfDir "%SOLR_HOME%" ^+ org.apache.solr.cli.SolrCLI auth %AUTH_PARAMS% -solrIncludeFile "%SOLR_INCLUDE%" -authConfDir "%SOLR_HOME%" ^-solrUrl !SOLR_URL_SCHEME!://%SOLR_TOOL_HOST%:!AUTH_PORT!/solrgoto donediff --git a/solr/core/src/java/org/apache/solr/cli/ApiTool.java b/solr/core/src/java/org/apache/solr/cli/ApiTool.javanew file mode 100644index 00000000000..02971ab7828--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ApiTool.java@@ -0,0 +1,84 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.io.PrintStream;+import java.net.URI;+import java.util.List;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;+import org.apache.solr.common.util.NamedList;+import org.noggit.CharArr;+import org.noggit.JSONWriter;++public class ApiTool extends ToolBase {+ /** Used to send an arbitrary HTTP request to a Solr API endpoint. */+ public ApiTool() {+ this(CLIO.getOutStream());+ }++ public ApiTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "api";+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("get")+ .argName("URL")+ .hasArg()+ .required(true)+ .desc("Send a GET request to a Solr API endpoint.")+ .build());+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ String getUrl = cli.getOptionValue("get");+ if (getUrl != null) {+ getUrl = getUrl.replace("+", "%20");+ URI uri = new URI(getUrl);+ String solrUrl = SolrCLI.getSolrUrlFromUri(uri);+ String path = uri.getPath();+ try (var solrClient = SolrCLI.getSolrClient(solrUrl)) {+ NamedList<Object> response =+ solrClient.request(+ // For path parameter we need the path without the root so from the second / char+ // (because root can be configured)+ // E.g URL is http://localhost:8983/solr/admin/info/system path is+ // /solr/admin/info/system and the path without root is /admin/info/system+ new GenericSolrRequest(+ SolrRequest.METHOD.GET,+ path.substring(path.indexOf("/", path.indexOf("/") + 1)),+ SolrCLI.getSolrParamsFromUri(uri)));++ // pretty-print the response to stdout+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(response.asMap());+ echo(arr.toString());+ }+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/AssertTool.java b/solr/core/src/java/org/apache/solr/cli/AssertTool.javanew file mode 100644index 00000000000..b88dad4d805--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/AssertTool.java@@ -0,0 +1,395 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.IOException;+import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.nio.file.Files;+import java.nio.file.Path;+import java.nio.file.Paths;+import java.nio.file.attribute.FileOwnerAttributeView;+import java.util.List;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.HelpFormatter;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrClient;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.SolrServerException;+import org.apache.solr.client.solrj.impl.Http2SolrClient;+import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.client.solrj.request.HealthCheckRequest;+import org.apache.solr.client.solrj.response.CollectionAdminResponse;+import org.apache.solr.common.SolrException;+import org.apache.solr.common.util.NamedList;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/** Asserts various conditions and exists with error code if fails, else continues with no output */+public class AssertTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());+ private static String message = null;+ private static boolean useExitCode = false;+ private static Long timeoutMs = 1000L;++ public AssertTool() {+ this(CLIO.getOutStream());+ }++ public AssertTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "assert";+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("R")+ .desc("Asserts that we are NOT the root user.")+ .longOpt("not-root")+ .build(),+ Option.builder("r").desc("Asserts that we are the root user.").longOpt("root").build(),+ Option.builder("S")+ .desc("Asserts that Solr is NOT running on a certain URL. Default timeout is 1000ms.")+ .longOpt("not-started")+ .hasArg(true)+ .argName("url")+ .build(),+ Option.builder("s")+ .desc("Asserts that Solr is running on a certain URL. Default timeout is 1000ms.")+ .longOpt("started")+ .hasArg(true)+ .argName("url")+ .build(),+ Option.builder("u")+ .desc("Asserts that we run as same user that owns <directory>.")+ .longOpt("same-user")+ .hasArg(true)+ .argName("directory")+ .build(),+ Option.builder("x")+ .desc("Asserts that directory <directory> exists.")+ .longOpt("exists")+ .hasArg(true)+ .argName("directory")+ .build(),+ Option.builder("X")+ .desc("Asserts that directory <directory> does NOT exist.")+ .longOpt("not-exists")+ .hasArg(true)+ .argName("directory")+ .build(),+ Option.builder("c")+ .desc(+ "Asserts that Solr is running in cloud mode. Also fails if Solr not running. URL should be for root Solr path.")+ .longOpt("cloud")+ .hasArg(true)+ .argName("url")+ .build(),+ Option.builder("C")+ .desc(+ "Asserts that Solr is not running in cloud mode. Also fails if Solr not running. URL should be for root Solr path.")+ .longOpt("not-cloud")+ .hasArg(true)+ .argName("url")+ .build(),+ Option.builder("m")+ .desc("Exception message to be used in place of the default error message.")+ .longOpt("message")+ .hasArg(true)+ .argName("message")+ .build(),+ Option.builder("t")+ .desc("Timeout in ms for commands supporting a timeout.")+ .longOpt("timeout")+ .hasArg(true)+ .type(Long.class)+ .argName("ms")+ .build(),+ Option.builder("e")+ .desc("Return an exit code instead of printing error message on assert fail.")+ .longOpt("exitcode")+ .build());+ }++ @Override+ public int runTool(CommandLine cli) throws Exception {+ verbose = cli.hasOption(SolrCLI.OPTION_VERBOSE.getOpt());++ int toolExitStatus = 0;+ try {+ toolExitStatus = runAssert(cli);+ } catch (Exception exc) {+ // since this is a CLI, spare the user the stacktrace+ String excMsg = exc.getMessage();+ if (excMsg != null) {+ if (verbose) {+ CLIO.err("\nERROR: " + exc + "\n");+ } else {+ CLIO.err("\nERROR: " + excMsg + "\n");+ }+ toolExitStatus = 100; // Exit >= 100 means error, else means number of tests that failed+ } else {+ throw exc;+ }+ }+ return toolExitStatus;+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ runAssert(cli);+ }++ /**+ * Custom run method which may return exit code+ *+ * @param cli the command line object+ * @return 0 on success, or a number corresponding to number of tests that failed+ * @throws Exception if a tool failed, e.g. authentication failure+ */+ protected int runAssert(CommandLine cli) throws Exception {+ if (cli.getOptions().length == 0 || cli.getArgs().length > 0 || cli.hasOption("h")) {+ new HelpFormatter()+ .printHelp(+ "bin/solr assert [-m <message>] [-e] [-rR] [-s <url>] [-S <url>] [-c <url>] [-C <url>] [-u <dir>] [-x <dir>] [-X <dir>]",+ SolrCLI.getToolOptions(this));+ return 1;+ }+ if (cli.hasOption("m")) {+ message = cli.getOptionValue("m");+ }+ if (cli.hasOption("t")) {+ timeoutMs = Long.parseLong(cli.getOptionValue("t"));+ }+ if (cli.hasOption("e")) {+ useExitCode = true;+ }++ int ret = 0;+ if (cli.hasOption("r")) {+ ret += assertRootUser();+ }+ if (cli.hasOption("R")) {+ ret += assertNotRootUser();+ }+ if (cli.hasOption("x")) {+ ret += assertFileExists(cli.getOptionValue("x"));+ }+ if (cli.hasOption("X")) {+ ret += assertFileNotExists(cli.getOptionValue("X"));+ }+ if (cli.hasOption("u")) {+ ret += sameUser(cli.getOptionValue("u"));+ }+ if (cli.hasOption("s")) {+ ret += assertSolrRunning(cli.getOptionValue("s"));+ }+ if (cli.hasOption("S")) {+ ret += assertSolrNotRunning(cli.getOptionValue("S"));+ }+ if (cli.hasOption("c")) {+ ret += assertSolrRunningInCloudMode(cli.getOptionValue("c"));+ }+ if (cli.hasOption("C")) {+ ret += assertSolrNotRunningInCloudMode(cli.getOptionValue("C"));+ }+ return ret;+ }++ public static int assertSolrRunning(String url) throws Exception {+ StatusTool status = new StatusTool();+ try {+ status.waitToSeeSolrUp(url, timeoutMs, TimeUnit.MILLISECONDS);+ } catch (Exception se) {+ if (SolrCLI.exceptionIsAuthRelated(se)) {+ throw se;+ }+ return exitOrException(+ "Solr is not running on url "+ + url+ + " after "+ + TimeUnit.SECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS)+ + " seconds");+ }+ return 0;+ }++ public static int assertSolrNotRunning(String url) throws Exception {+ StatusTool status = new StatusTool();+ long timeout =+ System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS);+ try (SolrClient solrClient = SolrCLI.getSolrClient(url)) {+ NamedList<Object> response = solrClient.request(new HealthCheckRequest());+ Integer statusCode = (Integer) response.findRecursive("responseHeader", "status");+ SolrCLI.checkCodeForAuthError(statusCode);+ } catch (SolrException se) {+ throw se;+ } catch (IOException | SolrServerException e) {+ log.debug("Opening connection to {} failed, Solr does not seem to be running", url, e);+ return 0;+ }+ while (System.nanoTime() < timeout) {+ try {+ status.waitToSeeSolrUp(url, 1, TimeUnit.SECONDS);+ try {+ log.debug("Solr still up. Waiting before trying again to see if it was stopped");+ Thread.sleep(1000L);+ } catch (InterruptedException interrupted) {+ timeout = 0; // stop looping+ }+ } catch (Exception se) {+ if (SolrCLI.exceptionIsAuthRelated(se)) {+ throw se;+ }+ return exitOrException(se.getMessage());+ }+ }+ return exitOrException(+ "Solr is still running at "+ + url+ + " after "+ + TimeUnit.SECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS)+ + " seconds");+ }++ public static int assertSolrRunningInCloudMode(String url) throws Exception {+ if (!isSolrRunningOn(url)) {+ return exitOrException(+ "Solr is not running on url "+ + url+ + " after "+ + TimeUnit.SECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS)+ + " seconds");+ }++ if (!runningSolrIsCloud(url)) {+ return exitOrException("Solr is not running in cloud mode on " + url);+ }+ return 0;+ }++ public static int assertSolrNotRunningInCloudMode(String url) throws Exception {+ if (!isSolrRunningOn(url)) {+ return exitOrException(+ "Solr is not running on url "+ + url+ + " after "+ + TimeUnit.SECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS)+ + " seconds");+ }++ if (runningSolrIsCloud(url)) {+ return exitOrException("Solr is not running in standalone mode on " + url);+ }+ return 0;+ }++ public static int sameUser(String directory) throws Exception {+ if (Files.exists(Paths.get(directory))) {+ String userForDir = userForDir(Paths.get(directory));+ if (!currentUser().equals(userForDir)) {+ return exitOrException("Must run as user " + userForDir + ". We are " + currentUser());+ }+ } else {+ return exitOrException("Directory " + directory + " does not exist.");+ }+ return 0;+ }++ public static int assertFileExists(String directory) throws Exception {+ if (!Files.exists(Paths.get(directory))) {+ return exitOrException("Directory " + directory + " does not exist.");+ }+ return 0;+ }++ public static int assertFileNotExists(String directory) throws Exception {+ if (Files.exists(Paths.get(directory))) {+ return exitOrException("Directory " + directory + " should not exist.");+ }+ return 0;+ }++ public static int assertRootUser() throws Exception {+ if (!currentUser().equals("root")) {+ return exitOrException("Must run as root user");+ }+ return 0;+ }++ public static int assertNotRootUser() throws Exception {+ if (currentUser().equals("root")) {+ return exitOrException("Not allowed to run as root user");+ }+ return 0;+ }++ public static String currentUser() {+ return System.getProperty("user.name");+ }++ public static String userForDir(Path pathToDir) {+ try {+ FileOwnerAttributeView ownerAttributeView =+ Files.getFileAttributeView(pathToDir, FileOwnerAttributeView.class);+ return ownerAttributeView.getOwner().getName();+ } catch (IOException e) {+ return "N/A";+ }+ }++ private static int exitOrException(String msg) throws SolrCLI.AssertionFailureException {+ if (useExitCode) {+ return 1;+ } else {+ throw new SolrCLI.AssertionFailureException(message != null ? message : msg);+ }+ }++ private static boolean isSolrRunningOn(String url) throws Exception {+ StatusTool status = new StatusTool();+ try {+ status.waitToSeeSolrUp(url, timeoutMs, TimeUnit.MILLISECONDS);+ return true;+ } catch (Exception se) {+ if (SolrCLI.exceptionIsAuthRelated(se)) {+ throw se;+ }+ return false;+ }+ }++ private static boolean runningSolrIsCloud(String url) throws Exception {+ try (final SolrClient client = new Http2SolrClient.Builder(url).build()) {+ final SolrRequest<CollectionAdminResponse> request =+ new CollectionAdminRequest.ClusterStatus();+ final CollectionAdminResponse response = request.process(client);+ return true; // throws an exception otherwise+ } catch (Exception e) {+ if (SolrCLI.exceptionIsAuthRelated(e)) {+ throw e;+ }+ return false;+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/AuthTool.java b/solr/core/src/java/org/apache/solr/cli/AuthTool.javanew file mode 100644index 00000000000..7976fb603b4--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/AuthTool.java@@ -0,0 +1,636 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.io.Console;+import java.io.File;+import java.io.IOException;+import java.io.PrintStream;+import java.nio.charset.StandardCharsets;+import java.nio.file.Files;+import java.nio.file.Path;+import java.util.Arrays;+import java.util.Base64;+import java.util.List;+import java.util.Locale;+import java.util.concurrent.TimeUnit;+import java.util.stream.Collectors;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.HelpFormatter;+import org.apache.commons.cli.Option;+import org.apache.lucene.util.Constants;+import org.apache.solr.common.cloud.SolrZkClient;+import org.apache.solr.common.util.StrUtils;+import org.apache.solr.security.Sha256AuthenticationProvider;+import org.apache.zookeeper.KeeperException;++// Authentication tool+public class AuthTool extends ToolBase {+ public AuthTool() {+ this(CLIO.getOutStream());+ }++ public AuthTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "auth";+ }++ List<String> authenticationVariables =+ Arrays.asList(+ "SOLR_AUTHENTICATION_CLIENT_BUILDER", "SOLR_AUTH_TYPE", "SOLR_AUTHENTICATION_OPTS");++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("type")+ .argName("type")+ .hasArg()+ .desc(+ "The authentication mechanism to enable (basicAuth or kerberos). Defaults to 'basicAuth'.")+ .build(),+ Option.builder("credentials")+ .argName("credentials")+ .hasArg()+ .desc(+ "Credentials in the format username:password. Example: -credentials solr:SolrRocks")+ .build(),+ Option.builder("prompt")+ .argName("prompt")+ .hasArg()+ .desc(+ "Prompts the user to provide the credentials. Use either -credentials or -prompt, not both.")+ .build(),+ Option.builder("config")+ .argName("config")+ .hasArgs()+ .desc(+ "Configuration parameters (Solr startup parameters). Required for Kerberos authentication.")+ .build(),+ Option.builder("blockUnknown")+ .argName("blockUnknown")+ .desc(+ "Blocks all access for unknown users (requires authentication for all endpoints).")+ .hasArg()+ .build(),+ Option.builder("solrIncludeFile")+ .argName("solrIncludeFile")+ .hasArg()+ .desc(+ "The Solr include file which contains overridable environment variables for configuring Solr configurations.")+ .build(),+ Option.builder("updateIncludeFileOnly")+ .argName("updateIncludeFileOnly")+ .desc(+ "Only update the solr.in.sh or solr.in.cmd file, and skip actual enabling/disabling"+ + " authentication (i.e. don't update security.json).")+ .hasArg()+ .build(),+ Option.builder("authConfDir")+ .argName("authConfDir")+ .hasArg()+ .required()+ .desc(+ "This is where any authentication related configuration files, if any, would be placed.")+ .build(),+ Option.builder("solrUrl").argName("solrUrl").hasArg().desc("Solr URL.").build(),+ Option.builder("zkHost")+ .argName("zkHost")+ .hasArg()+ .desc("ZooKeeper host to connect to.")+ .build(),+ SolrCLI.OPTION_VERBOSE);+ }++ private void ensureArgumentIsValidBooleanIfPresent(CommandLine cli, String argName) {+ if (cli.hasOption(argName)) {+ final String value = cli.getOptionValue(argName);+ if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value)) {+ echo("Argument [" + argName + "] must be either true or false, but was [" + value + "]");+ SolrCLI.exit(1);+ }+ }+ }++ @Override+ public int runTool(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ if (cli.getOptions().length == 0+ || cli.getArgs().length == 0+ || cli.getArgs().length > 1+ || cli.hasOption("h")) {+ new HelpFormatter()+ .printHelp("bin/solr auth <enable|disable> [OPTIONS]", SolrCLI.getToolOptions(this));+ return 1;+ }++ ensureArgumentIsValidBooleanIfPresent(cli, "blockUnknown");+ ensureArgumentIsValidBooleanIfPresent(cli, "updateIncludeFileOnly");++ String type = cli.getOptionValue("type", "basicAuth");+ switch (type) {+ case "basicAuth":+ return handleBasicAuth(cli);+ case "kerberos":+ return handleKerberos(cli);+ default:+ CLIO.out("Only type=basicAuth or kerberos supported at the moment.");+ SolrCLI.exit(1);+ }+ return 1;+ }++ private int handleKerberos(CommandLine cli) throws Exception {+ String cmd = cli.getArgs()[0];+ boolean updateIncludeFileOnly =+ Boolean.parseBoolean(cli.getOptionValue("updateIncludeFileOnly", "false"));+ String securityJson =+ "{"+ + "\n \"authentication\":{"+ + "\n \"class\":\"solr.KerberosPlugin\""+ + "\n }"+ + "\n}";++ switch (cmd) {+ case "enable":+ String zkHost = null;+ boolean zkInaccessible = false;++ if (!updateIncludeFileOnly) {+ try {+ zkHost = SolrCLI.getZkHost(cli);+ } catch (Exception ex) {+ CLIO.out(+ "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"+ + securityJson+ + "\n");+ zkInaccessible = true;+ }+ if (zkHost == null) {+ if (!zkInaccessible) {+ CLIO.out(+ "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"+ + securityJson+ + "\n");+ zkInaccessible = true;+ }+ }++ // check if security is already enabled or not+ if (!zkInaccessible) {+ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(10000, TimeUnit.MILLISECONDS)+ .build()) {+ checkSecurityJsonExists(zkClient);+ } catch (Exception ex) {+ CLIO.out(+ "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"+ + securityJson+ + "\n");+ zkInaccessible = true;+ }+ }+ }++ if (!updateIncludeFileOnly) {+ if (!zkInaccessible) {+ echoIfVerbose("Uploading following security.json: " + securityJson, cli);+ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(10000, TimeUnit.MILLISECONDS)+ .build()) {+ zkClient.setData(+ "/security.json", securityJson.getBytes(StandardCharsets.UTF_8), true);+ } catch (Exception ex) {+ if (!zkInaccessible) {+ CLIO.out(+ "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"+ + securityJson);+ zkInaccessible = true;+ }+ }+ }+ }++ String config = StrUtils.join(Arrays.asList(cli.getOptionValues("config")), ' ');+ // config is base64 encoded (to get around parsing problems), decode it+ config = config.replace(" ", "");+ config =+ new String(+ Base64.getDecoder().decode(config.getBytes(StandardCharsets.UTF_8)),+ StandardCharsets.UTF_8);+ config = config.replace("\n", "").replace("\r", "");++ String solrIncludeFilename = cli.getOptionValue("solrIncludeFile");+ File includeFile = new File(solrIncludeFilename);+ if (!includeFile.exists() || !includeFile.canWrite()) {+ CLIO.out(+ "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");+ printAuthEnablingInstructions(config);+ System.exit(0);+ }++ // update the solr.in.sh file to contain the necessary authentication lines+ updateIncludeFileEnableAuth(includeFile.toPath(), null, config, cli);+ echo(+ "Successfully enabled Kerberos authentication; please restart any running Solr nodes.");+ return 0;++ case "disable":+ clearSecurityJson(cli, updateIncludeFileOnly);++ solrIncludeFilename = cli.getOptionValue("solrIncludeFile");+ includeFile = new File(solrIncludeFilename);+ if (!includeFile.exists() || !includeFile.canWrite()) {+ CLIO.out(+ "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");+ CLIO.out(+ "Security has been disabled. Please remove any SOLR_AUTH_TYPE or SOLR_AUTHENTICATION_OPTS configuration from solr.in.sh/solr.in.cmd.\n");+ System.exit(0);+ }++ // update the solr.in.sh file to comment out the necessary authentication lines+ updateIncludeFileDisableAuth(includeFile.toPath(), cli);+ return 0;++ default:+ CLIO.out("Valid auth commands are: enable, disable.");+ SolrCLI.exit(1);+ }++ CLIO.out("Options not understood.");+ new HelpFormatter()+ .printHelp("bin/solr auth <enable|disable> [OPTIONS]", SolrCLI.getToolOptions(this));+ return 1;+ }++ private int handleBasicAuth(CommandLine cli) throws Exception {+ String cmd = cli.getArgs()[0];+ boolean prompt = Boolean.parseBoolean(cli.getOptionValue("prompt", "false"));+ boolean updateIncludeFileOnly =+ Boolean.parseBoolean(cli.getOptionValue("updateIncludeFileOnly", "false"));+ switch (cmd) {+ case "enable":+ if (!prompt && !cli.hasOption("credentials")) {+ CLIO.out("Option -credentials or -prompt is required with enable.");+ new HelpFormatter()+ .printHelp("bin/solr auth <enable|disable> [OPTIONS]", SolrCLI.getToolOptions(this));+ SolrCLI.exit(1);+ } else if (!prompt+ && (cli.getOptionValue("credentials") == null+ || !cli.getOptionValue("credentials").contains(":"))) {+ CLIO.out("Option -credentials is not in correct format.");+ new HelpFormatter()+ .printHelp("bin/solr auth <enable|disable> [OPTIONS]", SolrCLI.getToolOptions(this));+ SolrCLI.exit(1);+ }++ String zkHost = null;++ if (!updateIncludeFileOnly) {+ try {+ zkHost = SolrCLI.getZkHost(cli);+ } catch (Exception ex) {+ if (cli.hasOption("zkHost")) {+ CLIO.out(+ "Couldn't get ZooKeeper host. Please make sure that ZooKeeper is running and the correct zkHost has been passed in.");+ } else {+ CLIO.out(+ "Couldn't get ZooKeeper host. Please make sure Solr is running in cloud mode, or a zkHost has been passed in.");+ }+ SolrCLI.exit(1);+ }+ if (zkHost == null) {+ if (cli.hasOption("zkHost")) {+ CLIO.out(+ "Couldn't get ZooKeeper host. Please make sure that ZooKeeper is running and the correct zkHost has been passed in.");+ } else {+ CLIO.out(+ "Couldn't get ZooKeeper host. Please make sure Solr is running in cloud mode, or a zkHost has been passed in.");+ }+ SolrCLI.exit(1);+ }++ // check if security is already enabled or not+ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(10000, TimeUnit.MILLISECONDS)+ .build()) {+ checkSecurityJsonExists(zkClient);+ }+ }++ String username, password;+ if (cli.hasOption("credentials")) {+ String credentials = cli.getOptionValue("credentials");+ username = credentials.split(":")[0];+ password = credentials.split(":")[1];+ } else {+ Console console = System.console();+ // keep prompting until they've entered a non-empty username & password+ do {+ username = console.readLine("Enter username: ");+ } while (username == null || username.trim().length() == 0);+ username = username.trim();++ do {+ password = new String(console.readPassword("Enter password: "));+ } while (password.length() == 0);+ }++ boolean blockUnknown = Boolean.parseBoolean(cli.getOptionValue("blockUnknown", "true"));++ String securityJson =+ "{"+ + "\n \"authentication\":{"+ + "\n \"blockUnknown\": "+ + blockUnknown+ + ","+ + "\n \"class\":\"solr.BasicAuthPlugin\","+ + "\n \"credentials\":{\""+ + username+ + "\":\""+ + Sha256AuthenticationProvider.getSaltedHashedValue(password)+ + "\"}"+ + "\n },"+ + "\n \"authorization\":{"+ + "\n \"class\":\"solr.RuleBasedAuthorizationPlugin\","+ + "\n \"permissions\":["+ + "\n {\"name\":\"security-edit\", \"role\":\"admin\"},"+ + "\n {\"name\":\"security-read\", \"role\":\"admin\"},"+ + "\n {\"name\":\"config-edit\", \"role\":\"admin\"},"+ + "\n {\"name\":\"config-read\", \"role\":\"admin\"},"+ + "\n {\"name\":\"collection-admin-edit\", \"role\":\"admin\"},"+ + "\n {\"name\":\"collection-admin-read\", \"role\":\"admin\"},"+ + "\n {\"name\":\"core-admin-edit\", \"role\":\"admin\"},"+ + "\n {\"name\":\"core-admin-read\", \"role\":\"admin\"},"+ + "\n {\"name\":\"all\", \"role\":\"admin\"}"+ + "\n ],"+ + "\n \"user-role\":{\""+ + username+ + "\":\"admin\"}"+ + "\n }"+ + "\n}";++ if (!updateIncludeFileOnly) {+ echoIfVerbose("Uploading following security.json: " + securityJson, cli);+ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(10000, TimeUnit.MILLISECONDS)+ .build()) {+ zkClient.setData("/security.json", securityJson.getBytes(StandardCharsets.UTF_8), true);+ }+ }++ String solrIncludeFilename = cli.getOptionValue("solrIncludeFile");+ File includeFile = new File(solrIncludeFilename);+ if (!includeFile.exists() || !includeFile.canWrite()) {+ CLIO.out(+ "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");+ printAuthEnablingInstructions(username, password);+ System.exit(0);+ }+ String authConfDir = cli.getOptionValue("authConfDir");+ File basicAuthConfFile = new File(authConfDir + File.separator + "basicAuth.conf");++ if (!basicAuthConfFile.getParentFile().canWrite()) {+ CLIO.out("Cannot write to file: " + basicAuthConfFile.getAbsolutePath());+ printAuthEnablingInstructions(username, password);+ System.exit(0);+ }++ Files.writeString(+ basicAuthConfFile.toPath(),+ "httpBasicAuthUser=" + username + "\nhttpBasicAuthPassword=" + password,+ StandardCharsets.UTF_8);++ // update the solr.in.sh file to contain the necessary authentication lines+ updateIncludeFileEnableAuth(+ includeFile.toPath(), basicAuthConfFile.getAbsolutePath(), null, cli);+ final String successMessage =+ String.format(+ Locale.ROOT,+ "Successfully enabled basic auth with username [%s] and password [%s].",+ username,+ password);+ echo(successMessage);+ return 0;++ case "disable":+ clearSecurityJson(cli, updateIncludeFileOnly);++ solrIncludeFilename = cli.getOptionValue("solrIncludeFile");+ includeFile = new File(solrIncludeFilename);+ if (!includeFile.exists() || !includeFile.canWrite()) {+ CLIO.out(+ "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");+ CLIO.out(+ "Security has been disabled. Please remove any SOLR_AUTH_TYPE or SOLR_AUTHENTICATION_OPTS configuration from solr.in.sh/solr.in.cmd.\n");+ System.exit(0);+ }++ // update the solr.in.sh file to comment out the necessary authentication lines+ updateIncludeFileDisableAuth(includeFile.toPath(), cli);+ return 0;++ default:+ CLIO.out("Valid auth commands are: enable, disable.");+ SolrCLI.exit(1);+ }++ CLIO.out("Options not understood.");+ new HelpFormatter()+ .printHelp("bin/solr auth <enable|disable> [OPTIONS]", SolrCLI.getToolOptions(this));+ return 1;+ }++ private void checkSecurityJsonExists(SolrZkClient zkClient)+ throws KeeperException, InterruptedException {+ if (zkClient.exists("/security.json", true)) {+ byte[] oldSecurityBytes = zkClient.getData("/security.json", null, null, true);+ if (!"{}".equals(new String(oldSecurityBytes, StandardCharsets.UTF_8).trim())) {+ CLIO.out(+ "Security is already enabled. You can disable it with 'bin/solr auth disable'. Existing security.json: \n"+ + new String(oldSecurityBytes, StandardCharsets.UTF_8));+ SolrCLI.exit(1);+ }+ }+ }++ private void clearSecurityJson(CommandLine cli, boolean updateIncludeFileOnly) throws Exception {+ String zkHost;+ if (!updateIncludeFileOnly) {+ zkHost = SolrCLI.getZkHost(cli);+ if (zkHost == null) {+ stdout.print("ZK Host not found. Solr should be running in cloud mode.");+ SolrCLI.exit(1);+ }++ echoIfVerbose("Uploading following security.json: {}", cli);++ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(10000, TimeUnit.MILLISECONDS)+ .build()) {+ zkClient.setData("/security.json", "{}".getBytes(StandardCharsets.UTF_8), true);+ }+ }+ }++ private void printAuthEnablingInstructions(String username, String password) {+ if (Constants.WINDOWS) {+ CLIO.out(+ "\nAdd the following lines to the solr.in.cmd file so that the solr.cmd script can use subsequently.\n");+ CLIO.out(+ "set SOLR_AUTH_TYPE=basic\n"+ + "set SOLR_AUTHENTICATION_OPTS=\"-Dbasicauth="+ + username+ + ":"+ + password+ + "\"\n");+ } else {+ CLIO.out(+ "\nAdd the following lines to the solr.in.sh file so that the ./solr script can use subsequently.\n");+ CLIO.out(+ "SOLR_AUTH_TYPE=\"basic\"\n"+ + "SOLR_AUTHENTICATION_OPTS=\"-Dbasicauth="+ + username+ + ":"+ + password+ + "\"\n");+ }+ }++ private void printAuthEnablingInstructions(String kerberosConfig) {+ if (Constants.WINDOWS) {+ CLIO.out(+ "\nAdd the following lines to the solr.in.cmd file so that the solr.cmd script can use subsequently.\n");+ CLIO.out(+ "set SOLR_AUTH_TYPE=kerberos\n"+ + "set SOLR_AUTHENTICATION_OPTS=\""+ + kerberosConfig+ + "\"\n");+ } else {+ CLIO.out(+ "\nAdd the following lines to the solr.in.sh file so that the ./solr script can use subsequently.\n");+ CLIO.out(+ "SOLR_AUTH_TYPE=\"kerberos\"\n"+ + "SOLR_AUTHENTICATION_OPTS=\""+ + kerberosConfig+ + "\"\n");+ }+ }++ /**+ * This will update the include file (e.g. solr.in.sh / solr.in.cmd) with the authentication+ * parameters.+ *+ * @param includeFile The include file+ * @param basicAuthConfFile If basicAuth, the path of the file containing credentials. If not,+ * null.+ * @param kerberosConfig If kerberos, the config string containing startup parameters. If not,+ * null.+ */+ private void updateIncludeFileEnableAuth(+ Path includeFile, String basicAuthConfFile, String kerberosConfig, CommandLine cli)+ throws IOException {+ assert !(basicAuthConfFile != null+ && kerberosConfig != null); // only one of the two needs to be populated+ List<String> includeFileLines = Files.readAllLines(includeFile, StandardCharsets.UTF_8);+ for (int i = 0; i < includeFileLines.size(); i++) {+ String line = includeFileLines.get(i);+ if (authenticationVariables.contains(line.trim().split("=")[0].trim())) { // Non-Windows+ includeFileLines.set(i, "# " + line);+ }+ if (line.trim().split("=")[0].trim().startsWith("set ")+ && authenticationVariables.contains(+ line.trim().split("=")[0].trim().substring(4))) { // Windows+ includeFileLines.set(i, "REM " + line);+ }+ }+ includeFileLines.add(""); // blank line++ if (basicAuthConfFile != null) { // for basicAuth+ if (Constants.WINDOWS) {+ includeFileLines.add("REM The following lines added by solr.cmd for enabling BasicAuth");+ includeFileLines.add("set SOLR_AUTH_TYPE=basic");+ includeFileLines.add(+ "set SOLR_AUTHENTICATION_OPTS=\"-Dsolr.httpclient.config=" + basicAuthConfFile + "\"");+ } else {+ includeFileLines.add("# The following lines added by ./solr for enabling BasicAuth");+ includeFileLines.add("SOLR_AUTH_TYPE=\"basic\"");+ includeFileLines.add(+ "SOLR_AUTHENTICATION_OPTS=\"-Dsolr.httpclient.config=" + basicAuthConfFile + "\"");+ }+ } else { // for kerberos+ if (Constants.WINDOWS) {+ includeFileLines.add("REM The following lines added by solr.cmd for enabling BasicAuth");+ includeFileLines.add("set SOLR_AUTH_TYPE=kerberos");+ includeFileLines.add(+ "set SOLR_AUTHENTICATION_OPTS=\"-Dsolr.httpclient.config=" + basicAuthConfFile + "\"");+ } else {+ includeFileLines.add("# The following lines added by ./solr for enabling BasicAuth");+ includeFileLines.add("SOLR_AUTH_TYPE=\"kerberos\"");+ includeFileLines.add("SOLR_AUTHENTICATION_OPTS=\"" + kerberosConfig + "\"");+ }+ }++ String lines = includeFileLines.stream().collect(Collectors.joining(System.lineSeparator()));+ Files.writeString(includeFile, lines, StandardCharsets.UTF_8);++ if (basicAuthConfFile != null) {+ echoIfVerbose("Written out credentials file: " + basicAuthConfFile, cli);+ }+ echoIfVerbose("Updated Solr include file: " + includeFile.toAbsolutePath(), cli);+ }++ private void updateIncludeFileDisableAuth(Path includeFile, CommandLine cli) throws IOException {+ List<String> includeFileLines = Files.readAllLines(includeFile, StandardCharsets.UTF_8);+ boolean hasChanged = false;+ for (int i = 0; i < includeFileLines.size(); i++) {+ String line = includeFileLines.get(i);+ if (authenticationVariables.contains(line.trim().split("=")[0].trim())) { // Non-Windows+ includeFileLines.set(i, "# " + line);+ hasChanged = true;+ }+ if (line.trim().split("=")[0].trim().startsWith("set ")+ && authenticationVariables.contains(+ line.trim().split("=")[0].trim().substring(4))) { // Windows+ includeFileLines.set(i, "REM " + line);+ hasChanged = true;+ }+ }+ if (hasChanged) {+ String lines = includeFileLines.stream().collect(Collectors.joining(System.lineSeparator()));+ Files.writeString(includeFile, lines, StandardCharsets.UTF_8);+ echoIfVerbose("Commented out necessary lines from " + includeFile.toAbsolutePath(), cli);+ }+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {}+}diff --git a/solr/core/src/java/org/apache/solr/util/CLIO.java b/solr/core/src/java/org/apache/solr/cli/CLIO.javasimilarity index 97%rename from solr/core/src/java/org/apache/solr/util/CLIO.javarename to solr/core/src/java/org/apache/solr/cli/CLIO.javaindex 8cec4382c19..72ca66c12dc 100644--- a/solr/core/src/java/org/apache/solr/util/CLIO.java+++ b/solr/core/src/java/org/apache/solr/cli/CLIO.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import java.io.PrintStream;import org.apache.solr.common.util.SuppressForbidden;diff --git a/solr/core/src/java/org/apache/solr/cli/ConfigSetDownloadTool.java b/solr/core/src/java/org/apache/solr/cli/ConfigSetDownloadTool.javanew file mode 100644index 00000000000..13134507362--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ConfigSetDownloadTool.java@@ -0,0 +1,107 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.nio.file.Files;+import java.nio.file.Path;+import java.nio.file.Paths;+import java.util.List;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.common.cloud.SolrZkClient;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ConfigSetDownloadTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public ConfigSetDownloadTool() {+ this(CLIO.getOutStream());+ }++ public ConfigSetDownloadTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("confname")+ .argName("confname")+ .hasArg()+ .required(true)+ .desc("Configset name in ZooKeeper.")+ .build(),+ Option.builder("confdir")+ .argName("confdir")+ .hasArg()+ .required(true)+ .desc("Local directory with configs.")+ .build(),+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public String getName() {+ return "downconfig";+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);+ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("solrUrl")+ + " is running in standalone server mode, downconfig can only be used when running in SolrCloud mode.\n");+ }++ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(30000, TimeUnit.MILLISECONDS)+ .build()) {+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);+ String confName = cli.getOptionValue("confname");+ String confDir = cli.getOptionValue("confdir");+ Path configSetPath = Paths.get(confDir);+ // we try to be nice about having the "conf" in the directory, and we create it if it's not+ // there.+ if (!configSetPath.endsWith("/conf")) {+ configSetPath = Paths.get(configSetPath.toString(), "conf");+ }+ Files.createDirectories(configSetPath);+ echo(+ "Downloading configset "+ + confName+ + " from ZooKeeper at "+ + zkHost+ + " to directory "+ + configSetPath.toAbsolutePath());++ zkClient.downConfig(confName, configSetPath);+ } catch (Exception e) {+ log.error("Could not complete downconfig operation for reason: ", e);+ throw (e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/ConfigSetUploadTool.java b/solr/core/src/java/org/apache/solr/cli/ConfigSetUploadTool.javanew file mode 100644index 00000000000..e3decef4b83--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ConfigSetUploadTool.java@@ -0,0 +1,113 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.nio.file.Path;+import java.util.List;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.common.cloud.SolrZkClient;+import org.apache.solr.common.cloud.ZkMaintenanceUtils;+import org.apache.solr.core.ConfigSetService;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ConfigSetUploadTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public ConfigSetUploadTool() {+ this(CLIO.getOutStream());+ }++ public ConfigSetUploadTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("confname")+ .argName("confname") // Comes out in help message+ .hasArg() // Has one sub-argument+ .required(true) // confname argument must be present+ .desc("Configset name in ZooKeeper.")+ .build(), // passed as -confname value+ Option.builder("confdir")+ .argName("confdir")+ .hasArg()+ .required(true)+ .desc("Local directory with configs.")+ .build(),+ Option.builder("configsetsDir")+ .argName("configsetsDir")+ .hasArg()+ .required(false)+ .desc("Parent directory of example configsets.")+ .build(),+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public String getName() {+ return "upconfig";+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);+ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("solrUrl")+ + " is running in standalone server mode, upconfig can only be used when running in SolrCloud mode.\n");+ }++ String confName = cli.getOptionValue("confname");+ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(30000, TimeUnit.MILLISECONDS)+ .build()) {+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);+ Path confPath =+ ConfigSetService.getConfigsetPath(+ cli.getOptionValue("confdir"), cli.getOptionValue("configsetsDir"));++ echo(+ "Uploading "+ + confPath.toAbsolutePath()+ + " for config "+ + cli.getOptionValue("confname")+ + " to ZooKeeper at "+ + zkHost);+ ZkMaintenanceUtils.uploadToZK(+ zkClient,+ confPath,+ ZkMaintenanceUtils.CONFIGS_ZKNODE + "/" + confName,+ ZkMaintenanceUtils.UPLOAD_FILENAME_EXCLUDE_PATTERN);++ } catch (Exception e) {+ log.error("Could not complete upconfig operation for reason: ", e);+ throw (e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/ConfigTool.java b/solr/core/src/java/org/apache/solr/cli/ConfigTool.javanew file mode 100644index 00000000000..b1fac68d2b5--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ConfigTool.java@@ -0,0 +1,144 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.io.PrintStream;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrClient;+import org.apache.solr.client.solrj.impl.Http2SolrClient;+import org.apache.solr.common.util.NamedList;+import org.noggit.CharArr;+import org.noggit.JSONWriter;++/** Sends a POST to the Config API to perform a specified action. */+public class ConfigTool extends ToolBase {++ public ConfigTool() {+ this(CLIO.getOutStream());+ }++ public ConfigTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "config";+ }++ @Override+ public List<Option> getOptions() {+ List<Option> configOptions =+ List.of(+ Option.builder("action")+ .argName("ACTION")+ .hasArg()+ .required(false)+ .desc(+ "Config API action, one of: set-property, unset-property; default is 'set-property'.")+ .build(),+ Option.builder("property")+ .argName("PROP")+ .hasArg()+ .required(true)+ .desc(+ "Name of the Config API property to apply the action to, such as: 'updateHandler.autoSoftCommit.maxTime'.")+ .build(),+ Option.builder("value")+ .argName("VALUE")+ .hasArg()+ .required(false)+ .desc("Set the property to this value; accepts JSON objects and strings.")+ .build(),+ SolrCLI.OPTION_SOLRURL,+ SolrCLI.OPTION_ZKHOST,+ Option.builder("p")+ .argName("PORT")+ .hasArg()+ .required(false)+ .desc("The port of the Solr node to use when applying configuration change.")+ .longOpt("port")+ .build(),+ Option.builder("s")+ .argName("SCHEME")+ .hasArg()+ .required(false)+ .desc(+ "The scheme for accessing Solr. Accepted values: http or https. Default is 'http'")+ .longOpt("scheme")+ .build());+ return SolrCLI.joinOptions(configOptions, SolrCLI.cloudOptions);+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ String solrUrl;+ try {+ solrUrl = SolrCLI.resolveSolrUrl(cli);+ } catch (IllegalStateException e) {+ // Fallback to using the provided scheme and port+ final String scheme = cli.getOptionValue("scheme", "http");+ if (cli.hasOption("port")) {+ solrUrl = scheme + "://localhost:" + cli.getOptionValue("port", "8983") + "/solr";+ } else {+ throw e;+ }+ }++ String action = cli.getOptionValue("action", "set-property");+ String collection = cli.getOptionValue("collection", "gettingstarted");+ String property = cli.getOptionValue("property");+ String value = cli.getOptionValue("value");++ Map<String, Object> jsonObj = new HashMap<>();+ if (value != null) {+ Map<String, String> setMap = new HashMap<>();+ setMap.put(property, value);+ jsonObj.put(action, setMap);+ } else {+ jsonObj.put(action, property);+ }++ CharArr arr = new CharArr();+ (new JSONWriter(arr, 0)).write(jsonObj);+ String jsonBody = arr.toString();++ String updatePath = "/" + collection + "/config";++ echo("\nPOSTing request to Config API: " + solrUrl + updatePath);+ echo(jsonBody);++ try (SolrClient solrClient = new Http2SolrClient.Builder(solrUrl).build()) {+ NamedList<Object> result = SolrCLI.postJsonToSolr(solrClient, updatePath, jsonBody);+ Integer statusCode = (Integer) result.findRecursive("responseHeader", "status");+ if (statusCode == 0) {+ if (value != null) {+ echo("Successfully " + action + " " + property + " to " + value);+ } else {+ echo("Successfully " + action + " " + property);+ }+ } else {+ throw new Exception("Failed to " + action + " property due to:\n" + result);+ }+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/CreateCollectionTool.java b/solr/core/src/java/org/apache/solr/cli/CreateCollectionTool.javanew file mode 100644index 00000000000..610e94a231a--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/CreateCollectionTool.java@@ -0,0 +1,184 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import static org.apache.solr.common.params.CommonParams.NAME;++import java.io.PrintStream;+import java.nio.file.Path;+import java.util.Collections;+import java.util.List;+import java.util.Locale;+import java.util.Optional;+import java.util.Set;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrServerException;+import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.common.cloud.ZkMaintenanceUtils;+import org.apache.solr.common.cloud.ZkStateReader;+import org.apache.solr.common.params.CollectionAdminParams;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.core.ConfigSetService;+import org.noggit.CharArr;+import org.noggit.JSONWriter;++/** Supports create_collection command in the bin/solr script. */+public class CreateCollectionTool extends ToolBase {++ public CreateCollectionTool() {+ this(CLIO.getOutStream());+ }++ public CreateCollectionTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "create_collection";+ }++ @Override+ public List<Option> getOptions() {+ return SolrCLI.CREATE_COLLECTION_OPTIONS;+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);+ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("solrUrl")+ + " is running in standalone server mode, please use the create_core command instead;\n"+ + "create_collection can only be used when running in SolrCloud mode.\n");+ }++ try (CloudHttp2SolrClient cloudSolrClient =+ new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())+ .build()) {+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);+ cloudSolrClient.connect();+ runCloudTool(cloudSolrClient, cli);+ }+ }++ protected void runCloudTool(CloudSolrClient cloudSolrClient, CommandLine cli) throws Exception {++ Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();+ if (liveNodes.isEmpty())+ throw new IllegalStateException(+ "No live nodes found! Cannot create a collection until "+ + "there is at least 1 live node in the cluster.");++ String solrUrl = cli.getOptionValue("solrUrl");+ if (solrUrl == null) {+ String firstLiveNode = liveNodes.iterator().next();+ solrUrl = ZkStateReader.from(cloudSolrClient).getBaseUrlForNodeName(firstLiveNode);+ }++ String collectionName = cli.getOptionValue(NAME);++ // build a URL to create the collection+ int numShards = Integer.parseInt(cli.getOptionValue("shards", String.valueOf(1)));+ int replicationFactor =+ Integer.parseInt(cli.getOptionValue("replicationFactor", String.valueOf(1)));++ String confname = cli.getOptionValue("confname");+ String confdir = cli.getOptionValue("confdir");+ String configsetsDir = cli.getOptionValue("configsetsDir");++ boolean configExistsInZk =+ confname != null+ && !confname.trim().isEmpty()+ && ZkStateReader.from(cloudSolrClient)+ .getZkClient()+ .exists("/configs/" + confname, true);++ if (CollectionAdminParams.SYSTEM_COLL.equals(collectionName)) {+ // do nothing+ } else if (configExistsInZk) {+ echo("Re-using existing configuration directory " + confname);+ } else if (confdir != null && !confdir.trim().isEmpty()) {+ if (confname == null || confname.trim().isEmpty()) {+ confname = collectionName;+ }+ Path confPath = ConfigSetService.getConfigsetPath(confdir, configsetsDir);++ echoIfVerbose(+ "Uploading "+ + confPath.toAbsolutePath()+ + " for config "+ + confname+ + " to ZooKeeper at "+ + cloudSolrClient.getClusterStateProvider().getQuorumHosts(),+ cli);+ ZkMaintenanceUtils.uploadToZK(+ ZkStateReader.from(cloudSolrClient).getZkClient(),+ confPath,+ ZkMaintenanceUtils.CONFIGS_ZKNODE + "/" + confname,+ ZkMaintenanceUtils.UPLOAD_FILENAME_EXCLUDE_PATTERN);+ }++ // since creating a collection is a heavy-weight operation, check for existence first+ if (SolrCLI.safeCheckCollectionExists(solrUrl, collectionName)) {+ throw new IllegalStateException(+ "\nCollection '"+ + collectionName+ + "' already exists!\nChecked collection existence using CollectionAdminRequest");+ }++ // doesn't seem to exist ... try to create+ echoIfVerbose(+ "\nCreating new collection '" + collectionName + "' using CollectionAdminRequest", cli);++ NamedList<Object> response;+ try {+ response =+ cloudSolrClient.request(+ CollectionAdminRequest.createCollection(+ collectionName, confname, numShards, replicationFactor));+ } catch (SolrServerException sse) {+ throw new Exception(+ "Failed to create collection '" + collectionName + "' due to: " + sse.getMessage());+ }++ if (cli.hasOption(SolrCLI.OPTION_VERBOSE.getOpt())) {+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(response.asMap());+ echo(arr.toString());+ } else {+ String endMessage =+ String.format(+ Locale.ROOT,+ "Created collection '%s' with %d shard(s), %d replica(s)",+ collectionName,+ numShards,+ replicationFactor);+ if (confname != null && !confname.trim().isEmpty()) {+ endMessage += String.format(Locale.ROOT, " with config-set '%s'", confname);+ }++ echo(endMessage);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/CreateCoreTool.java b/solr/core/src/java/org/apache/solr/cli/CreateCoreTool.javanew file mode 100644index 00000000000..0efcb5bfb7e--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/CreateCoreTool.java@@ -0,0 +1,183 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import static org.apache.solr.common.params.CommonParams.NAME;++import java.io.File;+import java.io.FileNotFoundException;+import java.io.IOException;+import java.io.PrintStream;+import java.util.List;+import java.util.Locale;+import java.util.Map;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.commons.io.FileUtils;+import org.apache.commons.io.file.PathUtils;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.request.CoreAdminRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;+import org.apache.solr.client.solrj.response.CoreAdminResponse;+import org.apache.solr.common.params.CommonParams;++public class CreateCoreTool extends ToolBase {++ public CreateCoreTool() {+ this(CLIO.getOutStream());+ }++ public CreateCoreTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "create_core";+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ SolrCLI.OPTION_SOLRURL,+ Option.builder(NAME)+ .argName("NAME")+ .hasArg()+ .required(true)+ .desc("Name of the core to create.")+ .build(),+ Option.builder("confdir")+ .argName("CONFIG")+ .hasArg()+ .required(false)+ .desc(+ "Configuration directory to copy when creating the new core; default is "+ + SolrCLI.DEFAULT_CONFIG_SET+ + '.')+ .build(),+ Option.builder("configsetsDir")+ .argName("DIR")+ .hasArg()+ .required(true)+ .desc("Path to configsets directory on the local system.")+ .build(),+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.DEFAULT_SOLR_URL);+ if (!solrUrl.endsWith("/")) solrUrl += "/";++ File configsetsDir = new File(cli.getOptionValue("configsetsDir"));+ if (!configsetsDir.isDirectory())+ throw new FileNotFoundException(configsetsDir.getAbsolutePath() + " not found!");++ String configSet = cli.getOptionValue("confdir", SolrCLI.DEFAULT_CONFIG_SET);+ File configSetDir = new File(configsetsDir, configSet);+ if (!configSetDir.isDirectory()) {+ // we allow them to pass a directory instead of a configset name+ File possibleConfigDir = new File(configSet);+ if (possibleConfigDir.isDirectory()) {+ configSetDir = possibleConfigDir;+ } else {+ throw new FileNotFoundException(+ "Specified config directory "+ + configSet+ + " not found in "+ + configsetsDir.getAbsolutePath());+ }+ }++ String coreName = cli.getOptionValue(NAME);++ String coreRootDirectory; // usually same as solr home, but not always+ try (var solrClient = SolrCLI.getSolrClient(solrUrl)) {+ Map<String, Object> systemInfo =+ solrClient+ .request(+ new GenericSolrRequest(SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH))+ .asMap();+ if ("solrcloud".equals(systemInfo.get("mode"))) {+ throw new IllegalStateException(+ "Solr at "+ + solrUrl+ + " is running in SolrCloud mode, please use create_collection command instead.");+ }++ // convert raw JSON into user-friendly output+ coreRootDirectory = (String) systemInfo.get("core_root");++ // Fall back to solr_home, in case we are running against older server that does not return+ // the property+ if (coreRootDirectory == null) coreRootDirectory = (String) systemInfo.get("solr_home");+ if (coreRootDirectory == null)+ coreRootDirectory = configsetsDir.getParentFile().getAbsolutePath();+ }++ if (SolrCLI.safeCheckCoreExists(solrUrl, coreName)) {+ throw new IllegalArgumentException(+ "\nCore '"+ + coreName+ + "' already exists!\nChecked core existence using Core API command");+ }++ File coreInstanceDir = new File(coreRootDirectory, coreName);+ File confDir = new File(configSetDir, "conf");+ if (!coreInstanceDir.isDirectory()) {+ coreInstanceDir.mkdirs();+ if (!coreInstanceDir.isDirectory())+ throw new IOException(+ "Failed to create new core instance directory: " + coreInstanceDir.getAbsolutePath());++ if (confDir.isDirectory()) {+ FileUtils.copyDirectoryToDirectory(confDir, coreInstanceDir);+ } else {+ // hmmm ... the configset we're cloning doesn't have a conf sub-directory,+ // we'll just assume it is OK if it has solrconfig.xml+ if ((new File(configSetDir, "solrconfig.xml")).isFile()) {+ FileUtils.copyDirectory(configSetDir, new File(coreInstanceDir, "conf"));+ } else {+ throw new IllegalArgumentException(+ "\n"+ + configSetDir.getAbsolutePath()+ + " doesn't contain a conf subdirectory or solrconfig.xml\n");+ }+ }+ echoIfVerbose(+ "\nCopying configuration to new core instance directory:\n"+ + coreInstanceDir.getAbsolutePath(),+ cli);+ }++ echoIfVerbose("\nCreating new core '" + coreName + "' using CoreAdminRequest", cli);++ try (var solrClient = SolrCLI.getSolrClient(solrUrl)) {+ CoreAdminResponse res = CoreAdminRequest.createCore(coreName, coreName, solrClient);+ if (cli.hasOption(SolrCLI.OPTION_VERBOSE.getOpt())) {+ echo(res.jsonStr());+ echo("\n");+ } else {+ echo(String.format(Locale.ROOT, "\nCreated new core '%s'", coreName));+ }+ } catch (Exception e) {+ /* create-core failed, cleanup the copied configset before propagating the error. */+ PathUtils.deleteDirectory(coreInstanceDir.toPath());+ throw e;+ }+ }+} // end CreateCoreTool classdiff --git a/solr/core/src/java/org/apache/solr/cli/CreateTool.java b/solr/core/src/java/org/apache/solr/cli/CreateTool.javanew file mode 100644index 00000000000..e3933928607--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/CreateTool.java@@ -0,0 +1,67 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import static org.apache.solr.common.params.CommonParams.SYSTEM_INFO_PATH;++import java.io.PrintStream;+import java.util.List;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;+import org.apache.solr.common.util.NamedList;++public class CreateTool extends ToolBase {++ public CreateTool() {+ this(CLIO.getOutStream());+ }++ public CreateTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "create";+ }++ @Override+ public List<Option> getOptions() {+ return SolrCLI.CREATE_COLLECTION_OPTIONS;+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.DEFAULT_SOLR_URL);+ if (!solrUrl.endsWith("/")) solrUrl += "/";++ ToolBase tool;+ try (var solrClient = SolrCLI.getSolrClient(solrUrl)) {+ NamedList<Object> systemInfo =+ solrClient.request(new GenericSolrRequest(SolrRequest.METHOD.GET, SYSTEM_INFO_PATH));+ if ("solrcloud".equals(systemInfo.get("mode"))) {+ tool = new CreateCollectionTool(stdout);+ } else {+ tool = new CreateCoreTool(stdout);+ }+ tool.runImpl(cli);+ }+ }+} // end CreateTool classdiff --git a/solr/core/src/java/org/apache/solr/cli/DeleteTool.java b/solr/core/src/java/org/apache/solr/cli/DeleteTool.javanew file mode 100644index 00000000000..d4d3d2e1943--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/DeleteTool.java@@ -0,0 +1,237 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import static org.apache.solr.common.params.CommonParams.NAME;+import static org.apache.solr.common.params.CommonParams.SYSTEM_INFO_PATH;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.Collections;+import java.util.List;+import java.util.Map;+import java.util.Optional;+import java.util.Set;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrClient;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.SolrServerException;+import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.impl.Http2SolrClient;+import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.client.solrj.request.CoreAdminRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;+import org.apache.solr.common.cloud.ZkStateReader;+import org.apache.solr.common.util.NamedList;+import org.noggit.CharArr;+import org.noggit.JSONWriter;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class DeleteTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public DeleteTool() {+ this(CLIO.getOutStream());+ }++ public DeleteTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "delete";+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ SolrCLI.OPTION_SOLRURL,+ Option.builder(NAME)+ .argName("NAME")+ .hasArg()+ .required(true)+ .desc("Name of the core / collection to delete.")+ .build(),+ Option.builder("deleteConfig")+ .argName("true|false")+ .hasArg()+ .required(false)+ .desc(+ "Flag to indicate if the underlying configuration directory for a collection should also be deleted; default is true.")+ .build(),+ Option.builder("forceDeleteConfig")+ .required(false)+ .desc(+ "Skip safety checks when deleting the configuration directory used by a collection.")+ .build(),+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.DEFAULT_SOLR_URL);+ if (!solrUrl.endsWith("/")) solrUrl += "/";++ try (var solrClient = SolrCLI.getSolrClient(solrUrl)) {+ Map<String, Object> systemInfo =+ solrClient+ .request(new GenericSolrRequest(SolrRequest.METHOD.GET, SYSTEM_INFO_PATH))+ .asMap();+ if ("solrcloud".equals(systemInfo.get("mode"))) {+ deleteCollection(cli);+ } else {+ deleteCore(cli, solrClient);+ }+ }+ }++ protected void deleteCollection(CommandLine cli) throws Exception {+ String zkHost = SolrCLI.getZkHost(cli);+ try (CloudSolrClient cloudSolrClient =+ new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())+ .withInternalClientBuilder(+ new Http2SolrClient.Builder()+ .withIdleTimeout(30, TimeUnit.SECONDS)+ .withConnectionTimeout(15, TimeUnit.SECONDS))+ .build()) {+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost, cli);+ cloudSolrClient.connect();+ deleteCollection(cloudSolrClient, cli);+ }+ }++ protected void deleteCollection(CloudSolrClient cloudSolrClient, CommandLine cli)+ throws Exception {+ Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();+ if (liveNodes.isEmpty())+ throw new IllegalStateException(+ "No live nodes found! Cannot delete a collection until "+ + "there is at least 1 live node in the cluster.");++ ZkStateReader zkStateReader = ZkStateReader.from(cloudSolrClient);+ String collectionName = cli.getOptionValue(NAME);+ if (!zkStateReader.getClusterState().hasCollection(collectionName)) {+ throw new IllegalArgumentException("Collection " + collectionName + " not found!");+ }++ String configName =+ zkStateReader.getClusterState().getCollection(collectionName).getConfigName();+ boolean deleteConfig = "true".equals(cli.getOptionValue("deleteConfig", "true"));+ if (deleteConfig && configName != null) {+ if (cli.hasOption("forceDeleteConfig")) {+ log.warn(+ "Skipping safety checks, configuration directory {} will be deleted with impunity.",+ configName);+ } else {+ // need to scan all Collections to see if any are using the config+ Set<String> collections = zkStateReader.getClusterState().getCollectionsMap().keySet();++ // give a little note to the user if there are many collections in case it takes a while+ if (collections.size() > 50)+ if (log.isInfoEnabled()) {+ log.info(+ "Scanning {} to ensure no other collections are using config {}",+ collections.size(),+ configName);+ }++ Optional<String> inUse =+ collections.stream()+ .filter(name -> !name.equals(collectionName)) // ignore this collection+ .filter(+ name ->+ configName.equals(+ zkStateReader.getClusterState().getCollection(name).getConfigName()))+ .findFirst();+ if (inUse.isPresent()) {+ deleteConfig = false;+ log.warn(+ "Configuration directory {} is also being used by {}{}",+ configName,+ inUse.get(),+ "; configuration will not be deleted from ZooKeeper. You can pass the -forceDeleteConfig flag to force delete.");+ }+ }+ }++ echoIfVerbose(+ "\nDeleting collection '" + collectionName + "' using CollectionAdminRequest", cli);++ NamedList<Object> response;+ try {+ response = cloudSolrClient.request(CollectionAdminRequest.deleteCollection(collectionName));+ } catch (SolrServerException sse) {+ throw new Exception(+ "Failed to delete collection '" + collectionName + "' due to: " + sse.getMessage());+ }++ if (deleteConfig) {+ String configZnode = "/configs/" + configName;+ try {+ zkStateReader.getZkClient().clean(configZnode);+ } catch (Exception exc) {+ echo(+ "\nWARNING: Failed to delete configuration directory "+ + configZnode+ + " in ZooKeeper due to: "+ + exc.getMessage()+ + "\nYou'll need to manually delete this znode using the zkcli script.");+ }+ }++ if (response != null) {+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(response.asMap());+ echo(arr.toString());+ echo("\n");+ }++ echo("Deleted collection '" + collectionName + "' using CollectionAdminRequest");+ }++ protected void deleteCore(CommandLine cli, SolrClient solrClient) throws Exception {+ String coreName = cli.getOptionValue(NAME);++ echo("\nDeleting core '" + coreName + "' using CoreAdminRequest\n");++ NamedList<Object> response;+ try {+ CoreAdminRequest.Unload unloadRequest = new CoreAdminRequest.Unload(true);+ unloadRequest.setDeleteIndex(true);+ unloadRequest.setDeleteDataDir(true);+ unloadRequest.setDeleteInstanceDir(true);+ unloadRequest.setCoreName(coreName);+ response = solrClient.request(unloadRequest);+ } catch (SolrServerException sse) {+ throw new Exception("Failed to delete core '" + coreName + "' due to: " + sse.getMessage());+ }++ if (response != null) {+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(response.asMap());+ echoIfVerbose(arr.toString(), cli);+ echoIfVerbose("\n", cli);+ }+ }+} // end DeleteTool classdiff --git a/solr/core/src/java/org/apache/solr/util/ExportTool.java b/solr/core/src/java/org/apache/solr/cli/ExportTool.javasimilarity index 99%rename from solr/core/src/java/org/apache/solr/util/ExportTool.javarename to solr/core/src/java/org/apache/solr/cli/ExportTool.javaindex f5e7f53c3c8..f230c37e7e1 100644--- a/solr/core/src/java/org/apache/solr/util/ExportTool.java+++ b/solr/core/src/java/org/apache/solr/cli/ExportTool.java@@ -15,7 +15,7 @@* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import static org.apache.solr.common.params.CommonParams.FL;import static org.apache.solr.common.params.CommonParams.JAVABIN;@@ -83,7 +83,7 @@ import org.apache.solr.common.util.StrUtils;import org.noggit.CharArr;import org.noggit.JSONWriter;-public class ExportTool extends SolrCLI.ToolBase {+public class ExportTool extends ToolBase {@Overridepublic String getName() {return "export";@@ -176,7 +176,7 @@ public class ExportTool extends SolrCLI.ToolBase {static Set<String> formats = Set.of(JAVABIN, "jsonl");@Override- protected void runImpl(CommandLine cli) throws Exception {+ public void runImpl(CommandLine cli) throws Exception {String url = cli.getOptionValue("url");Info info = new MultiThreadedRunner(url);info.query = cli.getOptionValue("query", "*:*");diff --git a/solr/core/src/java/org/apache/solr/cli/HealthcheckTool.java b/solr/core/src/java/org/apache/solr/cli/HealthcheckTool.javanew file mode 100644index 00000000000..9d3dbb9927f--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/HealthcheckTool.java@@ -0,0 +1,320 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import static org.apache.solr.common.params.CommonParams.DISTRIB;+import static org.apache.solr.common.params.CommonParams.NAME;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.ArrayList;+import java.util.Collection;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import java.util.Set;+import org.apache.commons.cli.CommandLine;+import org.apache.solr.client.solrj.SolrQuery;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.request.GenericSolrRequest;+import org.apache.solr.client.solrj.response.QueryResponse;+import org.apache.solr.common.cloud.ClusterState;+import org.apache.solr.common.cloud.DocCollection;+import org.apache.solr.common.cloud.Replica;+import org.apache.solr.common.cloud.Slice;+import org.apache.solr.common.cloud.ZkCoreNodeProps;+import org.apache.solr.common.cloud.ZkStateReader;+import org.apache.solr.common.params.CommonParams;+import org.apache.solr.common.util.NamedList;+import org.noggit.CharArr;+import org.noggit.JSONWriter;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class HealthcheckTool extends SolrCloudTool {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ enum ShardState {+ healthy,+ degraded,+ down,+ no_leader+ }++ /** Requests health information about a specific collection in SolrCloud. */+ public HealthcheckTool() {+ this(CLIO.getOutStream());+ }++ public HealthcheckTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "healthcheck";+ }++ @Override+ protected void runCloudTool(CloudSolrClient cloudSolrClient, CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String collection = cli.getOptionValue("collection");+ if (collection == null)+ throw new IllegalArgumentException("Must provide a collection to run a healthcheck against!");++ log.debug("Running healthcheck for {}", collection);++ ZkStateReader zkStateReader = ZkStateReader.from(cloudSolrClient);++ ClusterState clusterState = zkStateReader.getClusterState();+ Set<String> liveNodes = clusterState.getLiveNodes();+ final DocCollection docCollection = clusterState.getCollectionOrNull(collection);+ if (docCollection == null || docCollection.getSlices() == null)+ throw new IllegalArgumentException("Collection " + collection + " not found!");++ Collection<Slice> slices = docCollection.getSlices();++ SolrQuery q = new SolrQuery("*:*");+ q.setRows(0);+ QueryResponse qr = cloudSolrClient.query(collection, q);+ SolrCLI.checkCodeForAuthError(qr.getStatus());+ String collErr = null;+ long docCount = -1;+ try {+ docCount = qr.getResults().getNumFound();+ } catch (Exception exc) {+ collErr = String.valueOf(exc);+ }++ List<Object> shardList = new ArrayList<>();+ boolean collectionIsHealthy = (docCount != -1);++ for (Slice slice : slices) {+ String shardName = slice.getName();+ // since we're reporting health of this shard, there's no guarantee of a leader+ String leaderUrl = null;+ try {+ leaderUrl = zkStateReader.getLeaderUrl(collection, shardName, 1000);+ } catch (Exception exc) {+ log.warn("Failed to get leader for shard {} due to: {}", shardName, exc);+ }++ List<ReplicaHealth> replicaList = new ArrayList<>();+ for (Replica r : slice.getReplicas()) {++ String uptime = null;+ String memory = null;+ String replicaStatus;+ long numDocs = -1L;++ ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(r);+ String coreUrl = replicaCoreProps.getCoreUrl();+ boolean isLeader = coreUrl.equals(leaderUrl);++ // if replica's node is not live, its status is DOWN+ String nodeName = replicaCoreProps.getNodeName();+ if (nodeName == null || !liveNodes.contains(nodeName)) {+ replicaStatus = Replica.State.DOWN.toString();+ } else {+ // query this replica directly to get doc count and assess health+ q = new SolrQuery("*:*");+ q.setRows(0);+ q.set(DISTRIB, "false");+ try (var solrClient = SolrCLI.getSolrClient(coreUrl)) {+ qr = solrClient.query(q);+ numDocs = qr.getResults().getNumFound();++ NamedList<Object> systemInfo =+ solrClient.request(+ new GenericSolrRequest(SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH));+ uptime = SolrCLI.uptime((Long) systemInfo.findRecursive("jvm", "jmx", "upTimeMS"));+ String usedMemory = (String) systemInfo.findRecursive("jvm", "memory", "used");+ String totalMemory = (String) systemInfo.findRecursive("jvm", "memory", "total");+ memory = usedMemory + " of " + totalMemory;++ // if we get here, we can trust the state+ replicaStatus = replicaCoreProps.getState();+ } catch (Exception exc) {+ log.error("ERROR: {} when trying to reach: {}", exc, coreUrl);++ if (SolrCLI.checkCommunicationError(exc)) {+ replicaStatus = Replica.State.DOWN.toString();+ } else {+ replicaStatus = "error: " + exc;+ }+ }+ }++ replicaList.add(+ new ReplicaHealth(+ shardName, r.getName(), coreUrl, replicaStatus, numDocs, isLeader, uptime, memory));+ }++ ShardHealth shardHealth = new ShardHealth(shardName, replicaList);+ if (ShardState.healthy != shardHealth.getShardState())+ collectionIsHealthy = false; // at least one shard is un-healthy++ shardList.add(shardHealth.asMap());+ }++ Map<String, Object> report = new LinkedHashMap<>();+ report.put("collection", collection);+ report.put("status", collectionIsHealthy ? "healthy" : "degraded");+ if (collErr != null) {+ report.put("error", collErr);+ }+ report.put("numDocs", docCount);+ report.put("numShards", slices.size());+ report.put("shards", shardList);++ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(report);+ echo(arr.toString());+ }+} // end HealthcheckTool++class ReplicaHealth implements Comparable<ReplicaHealth> {+ String shard;+ String name;+ String url;+ String status;+ long numDocs;+ boolean isLeader;+ String uptime;+ String memory;++ ReplicaHealth(+ String shard,+ String name,+ String url,+ String status,+ long numDocs,+ boolean isLeader,+ String uptime,+ String memory) {+ this.shard = shard;+ this.name = name;+ this.url = url;+ this.numDocs = numDocs;+ this.status = status;+ this.isLeader = isLeader;+ this.uptime = uptime;+ this.memory = memory;+ }++ public Map<String, Object> asMap() {+ Map<String, Object> map = new LinkedHashMap<>();+ map.put(NAME, name);+ map.put("url", url);+ map.put("numDocs", numDocs);+ map.put("status", status);+ if (uptime != null) map.put("uptime", uptime);+ if (memory != null) map.put("memory", memory);+ if (isLeader) map.put("leader", true);+ return map;+ }++ @Override+ public String toString() {+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(asMap());+ return arr.toString();+ }++ @Override+ public int hashCode() {+ return this.shard.hashCode() + (isLeader ? 1 : 0);+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj) return true;+ if (obj == null) return false;+ if (!(obj instanceof ReplicaHealth)) return true;+ ReplicaHealth that = (ReplicaHealth) obj;+ return this.shard.equals(that.shard) && this.isLeader == that.isLeader;+ }++ @Override+ public int compareTo(ReplicaHealth other) {+ if (this == other) return 0;+ if (other == null) return 1;++ int myShardIndex = Integer.parseInt(this.shard.substring("shard".length()));++ int otherShardIndex = Integer.parseInt(other.shard.substring("shard".length()));++ if (myShardIndex == otherShardIndex) {+ // same shard index, list leaders first+ return this.isLeader ? -1 : 1;+ }++ return myShardIndex - otherShardIndex;+ }+}++class ShardHealth {+ String shard;+ List<ReplicaHealth> replicas;++ ShardHealth(String shard, List<ReplicaHealth> replicas) {+ this.shard = shard;+ this.replicas = replicas;+ }++ public HealthcheckTool.ShardState getShardState() {+ boolean healthy = true;+ boolean hasLeader = false;+ boolean atLeastOneActive = false;+ for (ReplicaHealth replicaHealth : replicas) {+ if (replicaHealth.isLeader) hasLeader = true;++ if (!Replica.State.ACTIVE.toString().equals(replicaHealth.status)) {+ healthy = false;+ } else {+ atLeastOneActive = true;+ }+ }++ if (!hasLeader) return HealthcheckTool.ShardState.no_leader;++ return healthy+ ? HealthcheckTool.ShardState.healthy+ : (atLeastOneActive+ ? HealthcheckTool.ShardState.degraded+ : HealthcheckTool.ShardState.down);+ }++ public Map<String, Object> asMap() {+ Map<String, Object> map = new LinkedHashMap<>();+ map.put("shard", shard);+ map.put("status", getShardState().toString());+ List<Object> replicaList = new ArrayList<>();+ for (ReplicaHealth replica : replicas) replicaList.add(replica.asMap());+ map.put("replicas", replicaList);+ return map;+ }++ @Override+ public String toString() {+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(asMap());+ return arr.toString();+ }+}diff --git a/solr/core/src/java/org/apache/solr/util/PackageTool.java b/solr/core/src/java/org/apache/solr/cli/PackageTool.javasimilarity index 98%rename from solr/core/src/java/org/apache/solr/util/PackageTool.javarename to solr/core/src/java/org/apache/solr/cli/PackageTool.javaindex 77d3770dbb7..0450bf96c13 100644--- a/solr/core/src/java/org/apache/solr/util/PackageTool.java+++ b/solr/core/src/java/org/apache/solr/cli/PackageTool.java@@ -14,11 +14,11 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;+import static org.apache.solr.cli.SolrCLI.getSolrClient;import static org.apache.solr.packagemanager.PackageUtils.print;import static org.apache.solr.packagemanager.PackageUtils.printGreen;-import static org.apache.solr.util.SolrCLI.getSolrClient;import java.lang.invoke.MethodHandles;import java.nio.file.Files;@@ -45,11 +45,10 @@ import org.apache.solr.packagemanager.RepositoryManager;import org.apache.solr.packagemanager.SolrPackage;import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease;import org.apache.solr.packagemanager.SolrPackageInstance;-import org.apache.solr.util.SolrCLI.StatusTool;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-public class PackageTool extends SolrCLI.ToolBase {+public class PackageTool extends ToolBase {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());@@ -76,7 +75,7 @@ public class PackageTool extends SolrCLI.ToolBase {"We really need to print the stacktrace here, otherwise "+ "there shall be little else information to debug problems. Other SolrCLI tools "+ "don't print stack traces, hence special treatment is needed here.")- protected void runImpl(CommandLine cli) throws Exception {+ public void runImpl(CommandLine cli) throws Exception {try {solrUrl = cli.getOptionValues("solrUrl")[cli.getOptionValues("solrUrl").length - 1];solrBaseUrl = solrUrl.replaceAll("/solr$", ""); // strip out ending "/solr"@@ -282,7 +281,6 @@ public class PackageTool extends SolrCLI.ToolBase {default:throw new RuntimeException("Unrecognized command: " + cmd);}- ;} finally {packageManager.close();}diff --git a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.javanew file mode 100644index 00000000000..5df4f7ff196--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java@@ -0,0 +1,1029 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.io.File;+import java.io.IOException;+import java.io.InputStream;+import java.io.PrintStream;+import java.net.Socket;+import java.net.URL;+import java.nio.charset.StandardCharsets;+import java.nio.file.Files;+import java.util.Arrays;+import java.util.Collections;+import java.util.HashMap;+import java.util.List;+import java.util.Locale;+import java.util.Map;+import java.util.Optional;+import java.util.Scanner;+import java.util.Set;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.commons.exec.DefaultExecuteResultHandler;+import org.apache.commons.exec.DefaultExecutor;+import org.apache.commons.exec.ExecuteException;+import org.apache.commons.exec.Executor;+import org.apache.commons.exec.OS;+import org.apache.commons.exec.environment.EnvironmentUtils;+import org.apache.commons.io.FileUtils;+import org.apache.solr.client.solrj.SolrClient;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.impl.Http2SolrClient;+import org.apache.solr.common.SolrException;+import org.noggit.CharArr;+import org.noggit.JSONWriter;++/** Supports an interactive session with the user to launch (or relaunch the -e cloud example) */+public class RunExampleTool extends ToolBase {++ private static final String PROMPT_FOR_NUMBER = "Please enter %s [%d]: ";+ private static final String PROMPT_FOR_NUMBER_IN_RANGE =+ "Please enter %s between %d and %d [%d]: ";+ private static final String PROMPT_NUMBER_TOO_SMALL =+ "%d is too small! " + PROMPT_FOR_NUMBER_IN_RANGE;+ private static final String PROMPT_NUMBER_TOO_LARGE =+ "%d is too large! " + PROMPT_FOR_NUMBER_IN_RANGE;++ protected InputStream userInput;+ protected Executor executor;+ protected String script;+ protected File serverDir;+ protected File exampleDir;+ protected String urlScheme;++ /** Default constructor used by the framework when running as a command-line application. */+ public RunExampleTool() {+ this(null, System.in, CLIO.getOutStream());+ }++ public RunExampleTool(Executor executor, InputStream userInput, PrintStream stdout) {+ super(stdout);+ this.executor = (executor != null) ? executor : new DefaultExecutor();+ this.userInput = userInput;+ }++ @Override+ public String getName() {+ return "run_example";+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("noprompt")+ .required(false)+ .desc(+ "Don't prompt for input; accept all defaults when running examples that accept user input.")+ .build(),+ Option.builder("e")+ .argName("NAME")+ .hasArg()+ .required(true)+ .desc("Name of the example to launch, one of: cloud, techproducts, schemaless, films.")+ .longOpt("example")+ .build(),+ Option.builder("script")+ .argName("PATH")+ .hasArg()+ .required(false)+ .desc("Path to the bin/solr script.")+ .build(),+ Option.builder("d")+ .argName("DIR")+ .hasArg()+ .required(true)+ .desc("Path to the Solr server directory.")+ .longOpt("serverDir")+ .build(),+ Option.builder("force")+ .argName("FORCE")+ .desc("Force option in case Solr is run as root.")+ .build(),+ Option.builder("exampleDir")+ .argName("DIR")+ .hasArg()+ .required(false)+ .desc(+ "Path to the Solr example directory; if not provided, ${serverDir}/../example is expected to exist.")+ .build(),+ Option.builder("urlScheme")+ .argName("SCHEME")+ .hasArg()+ .required(false)+ .desc("Solr URL scheme: http or https, defaults to http if not specified.")+ .build(),+ Option.builder("p")+ .argName("PORT")+ .hasArg()+ .required(false)+ .desc("Specify the port to start the Solr HTTP listener on; default is 8983.")+ .longOpt("port")+ .build(),+ Option.builder("h")+ .argName("HOSTNAME")+ .hasArg()+ .required(false)+ .desc("Specify the hostname for this Solr instance.")+ .longOpt("host")+ .build(),+ Option.builder("z")+ .argName("ZKHOST")+ .hasArg()+ .required(false)+ .desc("ZooKeeper connection string; only used when running in SolrCloud mode using -c.")+ .longOpt("zkhost")+ .build(),+ Option.builder("c")+ .required(false)+ .desc(+ "Start Solr in SolrCloud mode; if -z not supplied, an embedded ZooKeeper instance is started on Solr port+1000, such as 9983 if Solr is bound to 8983.")+ .longOpt("cloud")+ .build(),+ Option.builder("m")+ .argName("MEM")+ .hasArg()+ .required(false)+ .desc(+ "Sets the min (-Xms) and max (-Xmx) heap size for the JVM, such as: -m 4g results in: -Xms4g -Xmx4g; by default, this script sets the heap size to 512m.")+ .longOpt("memory")+ .build(),+ Option.builder("a")+ .argName("OPTS")+ .hasArg()+ .required(false)+ .desc(+ "Additional options to be passed to the JVM when starting example Solr server(s).")+ .longOpt("addlopts")+ .build());+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ this.urlScheme = cli.getOptionValue("urlScheme", "http");++ serverDir = new File(cli.getOptionValue("serverDir"));+ if (!serverDir.isDirectory())+ throw new IllegalArgumentException(+ "Value of -serverDir option is invalid! "+ + serverDir.getAbsolutePath()+ + " is not a directory!");++ script = cli.getOptionValue("script");+ if (script != null) {+ if (!(new File(script)).isFile())+ throw new IllegalArgumentException(+ "Value of -script option is invalid! " + script + " not found");+ } else {+ File scriptFile = new File(serverDir.getParentFile(), "bin/solr");+ if (scriptFile.isFile()) {+ script = scriptFile.getAbsolutePath();+ } else {+ scriptFile = new File(serverDir.getParentFile(), "bin/solr.cmd");+ if (scriptFile.isFile()) {+ script = scriptFile.getAbsolutePath();+ } else {+ throw new IllegalArgumentException(+ "Cannot locate the bin/solr script! Please pass -script to this application.");+ }+ }+ }++ exampleDir =+ (cli.hasOption("exampleDir"))+ ? new File(cli.getOptionValue("exampleDir"))+ : new File(serverDir.getParent(), "example");+ if (!exampleDir.isDirectory())+ throw new IllegalArgumentException(+ "Value of -exampleDir option is invalid! "+ + exampleDir.getAbsolutePath()+ + " is not a directory!");++ echoIfVerbose(+ "Running with\nserverDir="+ + serverDir.getAbsolutePath()+ + ",\nexampleDir="+ + exampleDir.getAbsolutePath()+ + "\nscript="+ + script,+ cli);++ String exampleType = cli.getOptionValue("example");+ if ("cloud".equals(exampleType)) {+ runCloudExample(cli);+ } else if ("techproducts".equals(exampleType)+ || "schemaless".equals(exampleType)+ || "films".equals(exampleType)) {+ runExample(cli, exampleType);+ } else {+ throw new IllegalArgumentException(+ "Unsupported example "+ + exampleType+ + "! Please choose one of: cloud, schemaless, techproducts, or films");+ }+ }++ protected void runExample(CommandLine cli, String exampleName) throws Exception {+ File exDir = setupExampleDir(serverDir, exampleDir, exampleName);+ String collectionName = "schemaless".equals(exampleName) ? "gettingstarted" : exampleName;+ String configSet =+ "techproducts".equals(exampleName) ? "sample_techproducts_configs" : "_default";++ boolean isCloudMode = cli.hasOption('c');+ String zkHost = cli.getOptionValue('z');+ int port = Integer.parseInt(cli.getOptionValue('p', "8983"));+ Map<String, Object> nodeStatus =+ startSolr(new File(exDir, "solr"), isCloudMode, cli, port, zkHost, 30);++ // invoke the CreateTool+ File configsetsDir = new File(serverDir, "solr/configsets");++ String solrUrl = (String) nodeStatus.get("baseUrl");++ // safe check if core / collection already exists+ boolean alreadyExists = false;+ if (nodeStatus.get("cloud") != null) {+ if (SolrCLI.safeCheckCollectionExists(solrUrl, collectionName)) {+ alreadyExists = true;+ echo(+ "\nWARNING: Collection '"+ + collectionName+ + "' already exists!\nChecked collection existence using Collections API");+ }+ } else {+ String coreName = collectionName;+ if (SolrCLI.safeCheckCoreExists(solrUrl, coreName)) {+ alreadyExists = true;+ echo(+ "\nWARNING: Core '"+ + coreName+ + "' already exists!\nChecked core existence using Core API command");+ }+ }++ if (!alreadyExists) {+ String[] createArgs =+ new String[] {+ "-name", collectionName,+ "-shards", "1",+ "-replicationFactor", "1",+ "-confname", collectionName,+ "-confdir", configSet,+ "-configsetsDir", configsetsDir.getAbsolutePath(),+ "-solrUrl", solrUrl+ };+ CreateTool createTool = new CreateTool(stdout);+ int createCode =+ createTool.runTool(+ SolrCLI.processCommandLineArgs(+ createTool.getName(), createTool.getOptions(), createArgs));+ if (createCode != 0)+ throw new Exception(+ "Failed to create " + collectionName + " using command: " + Arrays.asList(createArgs));+ }++ if ("techproducts".equals(exampleName) && !alreadyExists) {++ File exampledocsDir = new File(exampleDir, "exampledocs");+ if (!exampledocsDir.isDirectory()) {+ File readOnlyExampleDir = new File(serverDir.getParentFile(), "example");+ if (readOnlyExampleDir.isDirectory()) {+ exampledocsDir = new File(readOnlyExampleDir, "exampledocs");+ }+ }++ if (exampledocsDir.isDirectory()) {+ String updateUrl = String.format(Locale.ROOT, "%s/%s/update", solrUrl, collectionName);+ echo("Indexing tech product example docs from " + exampledocsDir.getAbsolutePath());++ String currentPropVal = System.getProperty("url");+ System.setProperty("url", updateUrl);+ SimplePostTool.main(new String[] {exampledocsDir.getAbsolutePath() + "/*.xml"});+ if (currentPropVal != null) {+ System.setProperty("url", currentPropVal); // reset+ } else {+ System.clearProperty("url");+ }+ } else {+ echo(+ "exampledocs directory not found, skipping indexing step for the techproducts example");+ }+ } else if ("films".equals(exampleName) && !alreadyExists) {+ SolrClient solrClient = new Http2SolrClient.Builder(solrUrl).build();++ echo("Adding dense vector field type to films schema \"_default\"");+ try {+ SolrCLI.postJsonToSolr(+ solrClient,+ "/" + collectionName + "/schema",+ "{\n"+ + " \"add-field-type\" : {\n"+ + " \"name\":\"knn_vector_10\",\n"+ + " \"class\":\"solr.DenseVectorField\",\n"+ + " \"vectorDimension\":10,\n"+ + " \"similarityFunction\":cosine\n"+ + " \"knnAlgorithm\":hnsw\n"+ + " }\n"+ + " }");+ } catch (Exception ex) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);+ }++ echo(+ "Adding name, initial_release_date, and film_vector fields to films schema \"_default\"");+ try {+ SolrCLI.postJsonToSolr(+ solrClient,+ "/" + collectionName + "/schema",+ "{\n"+ + " \"add-field\" : {\n"+ + " \"name\":\"name\",\n"+ + " \"type\":\"text_general\",\n"+ + " \"multiValued\":false,\n"+ + " \"stored\":true\n"+ + " },\n"+ + " \"add-field\" : {\n"+ + " \"name\":\"initial_release_date\",\n"+ + " \"type\":\"pdate\",\n"+ + " \"stored\":true\n"+ + " },\n"+ + " \"add-field\" : {\n"+ + " \"name\":\"film_vector\",\n"+ + " \"type\":\"knn_vector_10\",\n"+ + " \"indexed\":true\n"+ + " \"stored\":true\n"+ + " }\n"+ + " }");+ } catch (Exception ex) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);+ }++ echo("Adding paramsets \"algo\" and \"algo_b\" to films configuration for relevancy tuning");+ try {+ SolrCLI.postJsonToSolr(+ solrClient,+ "/" + collectionName + "/config/params",+ "{\n"+ + " \"set\": {\n"+ + " \"algo_a\":{\n"+ + " \"defType\":\"dismax\",\n"+ + " \"qf\":\"name\"\n"+ + " }\n"+ + " },\n"+ + " \"set\": {\n"+ + " \"algo_b\":{\n"+ + " \"defType\":\"dismax\",\n"+ + " \"qf\":\"name\",\n"+ + " \"mm\":\"100%\"\n"+ + " }\n"+ + " }\n"+ + " }\n");+ } catch (Exception ex) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);+ }++ File filmsJsonFile = new File(exampleDir, "films/films.json");+ String updateUrl = String.format(Locale.ROOT, "%s/%s/update/json", solrUrl, collectionName);+ echo("Indexing films example docs from " + filmsJsonFile.getAbsolutePath());+ String currentPropVal = System.getProperty("url");+ System.setProperty("url", updateUrl);+ SimplePostTool.main(new String[] {filmsJsonFile.getAbsolutePath()});+ if (currentPropVal != null) {+ System.setProperty("url", currentPropVal); // reset+ } else {+ System.clearProperty("url");+ }+ }++ echo(+ "\nSolr "+ + exampleName+ + " example launched successfully. Direct your Web browser to "+ + solrUrl+ + " to visit the Solr Admin UI");+ }++ protected void runCloudExample(CommandLine cli) throws Exception {++ boolean prompt = !cli.hasOption("noprompt");+ int numNodes = 2;+ int[] cloudPorts = new int[] {8983, 7574, 8984, 7575};+ File cloudDir = new File(exampleDir, "cloud");+ if (!cloudDir.isDirectory()) cloudDir.mkdir();++ echo("\nWelcome to the SolrCloud example!\n");++ Scanner readInput = prompt ? new Scanner(userInput, StandardCharsets.UTF_8.name()) : null;+ if (prompt) {+ echo(+ "This interactive session will help you launch a SolrCloud cluster on your local workstation.");++ // get the number of nodes to start+ numNodes =+ promptForInt(+ readInput,+ "To begin, how many Solr nodes would you like to run in your local cluster? (specify 1-4 nodes) [2]: ",+ "a number",+ numNodes,+ 1,+ 4);++ echo("Ok, let's start up " + numNodes + " Solr nodes for your example SolrCloud cluster.");++ // get the ports for each port+ for (int n = 0; n < numNodes; n++) {+ String promptMsg =+ String.format(+ Locale.ROOT, "Please enter the port for node%d [%d]: ", (n + 1), cloudPorts[n]);+ int port = promptForPort(readInput, n + 1, promptMsg, cloudPorts[n]);+ while (!isPortAvailable(port)) {+ port =+ promptForPort(+ readInput,+ n + 1,+ "Oops! Looks like port "+ + port+ + " is already being used by another process. Please choose a different port.",+ cloudPorts[n]);+ }++ cloudPorts[n] = port;+ echoIfVerbose("Using port " + port + " for node " + (n + 1), cli);+ }+ } else {+ echo("Starting up " + numNodes + " Solr nodes for your example SolrCloud cluster.\n");+ }++ // setup a unique solr.solr.home directory for each node+ File node1Dir = setupExampleDir(serverDir, cloudDir, "node1");+ for (int n = 2; n <= numNodes; n++) {+ File nodeNDir = new File(cloudDir, "node" + n);+ if (!nodeNDir.isDirectory()) {+ echo("Cloning " + node1Dir.getAbsolutePath() + " into\n " + nodeNDir.getAbsolutePath());+ FileUtils.copyDirectory(node1Dir, nodeNDir);+ } else {+ echo(nodeNDir.getAbsolutePath() + " already exists.");+ }+ }++ // deal with extra args passed to the script to run the example+ String zkHost = cli.getOptionValue('z');++ // start the first node (most likely with embedded ZK)+ Map<String, Object> nodeStatus =+ startSolr(new File(node1Dir, "solr"), true, cli, cloudPorts[0], zkHost, 30);++ if (zkHost == null) {+ @SuppressWarnings("unchecked")+ Map<String, Object> cloudStatus = (Map<String, Object>) nodeStatus.get("cloud");+ if (cloudStatus != null) {+ String zookeeper = (String) cloudStatus.get("ZooKeeper");+ if (zookeeper != null) zkHost = zookeeper;+ }+ if (zkHost == null)+ throw new Exception("Could not get the ZooKeeper connection string for node1!");+ }++ if (numNodes > 1) {+ // start the other nodes+ for (int n = 1; n < numNodes; n++)+ startSolr(+ new File(cloudDir, "node" + (n + 1) + "/solr"), true, cli, cloudPorts[n], zkHost, 30);+ }++ String solrUrl = (String) nodeStatus.get("baseUrl");+ if (solrUrl.endsWith("/")) solrUrl = solrUrl.substring(0, solrUrl.length() - 1);++ // wait until live nodes == numNodes+ waitToSeeLiveNodes(zkHost, numNodes);++ // create the collection+ String collectionName = createCloudExampleCollection(numNodes, readInput, prompt, solrUrl);++ // update the config to enable soft auto-commit+ echo("\nEnabling auto soft-commits with maxTime 3 secs using the Config API");+ setCollectionConfigProperty(solrUrl, collectionName);++ echo("\n\nSolrCloud example running, please visit: " + solrUrl + " \n");+ }++ protected void setCollectionConfigProperty(String solrUrl, String collectionName) {+ ConfigTool configTool = new ConfigTool(stdout);+ String[] configArgs =+ new String[] {+ "-collection",+ collectionName,+ "-property",+ "updateHandler.autoSoftCommit.maxTime",+ "-value",+ "3000",+ "-solrUrl",+ solrUrl+ };++ // let's not fail if we get this far ... just report error and finish up+ try {+ configTool.runTool(+ SolrCLI.processCommandLineArgs(+ configTool.getName(), configTool.getOptions(), configArgs));+ } catch (Exception exc) {+ CLIO.err(+ "Failed to update '"+ + "updateHandler.autoSoftCommit.maxTime"+ + "' property due to: "+ + exc);+ }+ }++ /** wait until the number of live nodes == numNodes. */+ protected void waitToSeeLiveNodes(String zkHost, int numNodes) {+ try (CloudSolrClient cloudClient =+ new CloudSolrClient.Builder(Collections.singletonList(zkHost), Optional.empty()).build()) {+ cloudClient.connect();+ Set<String> liveNodes = cloudClient.getClusterState().getLiveNodes();+ int numLiveNodes = (liveNodes != null) ? liveNodes.size() : 0;+ long timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);+ while (System.nanoTime() < timeout && numLiveNodes < numNodes) {+ echo(+ "\nWaiting up to "+ + 10+ + " seconds to see "+ + (numNodes - numLiveNodes)+ + " more nodes join the SolrCloud cluster ...");+ try {+ Thread.sleep(2000);+ } catch (InterruptedException ie) {+ Thread.interrupted();+ }+ liveNodes = cloudClient.getClusterState().getLiveNodes();+ numLiveNodes = (liveNodes != null) ? liveNodes.size() : 0;+ }+ if (numLiveNodes < numNodes) {+ echo(+ "\nWARNING: Only "+ + numLiveNodes+ + " of "+ + numNodes+ + " are active in the cluster after "+ + 10+ + " seconds! Please check the solr.log for each node to look for errors.\n");+ }+ } catch (Exception exc) {+ CLIO.err("Failed to see if " + numNodes + " joined the SolrCloud cluster due to: " + exc);+ }+ }++ protected Map<String, Object> startSolr(+ File solrHomeDir,+ boolean cloudMode,+ CommandLine cli,+ int port,+ String zkHost,+ int maxWaitSecs)+ throws Exception {++ String extraArgs = readExtraArgs(cli.getArgs());++ String host = cli.getOptionValue('h');+ String memory = cli.getOptionValue('m');++ String hostArg = (host != null && !"localhost".equals(host)) ? " -h " + host : "";+ String zkHostArg = (zkHost != null) ? " -z " + zkHost : "";+ String memArg = (memory != null) ? " -m " + memory : "";+ String cloudModeArg = cloudMode ? "-cloud " : "";+ String forceArg = cli.hasOption("force") ? " -force" : "";++ String addlOpts = cli.getOptionValue('a');+ String addlOptsArg = (addlOpts != null) ? " -a \"" + addlOpts + "\"" : "";++ File cwd = new File(System.getProperty("user.dir"));+ File binDir = (new File(script)).getParentFile();++ boolean isWindows = (OS.isFamilyDOS() || OS.isFamilyWin9x() || OS.isFamilyWindows());+ String callScript = (!isWindows && cwd.equals(binDir.getParentFile())) ? "bin/solr" : script;++ String cwdPath = cwd.getAbsolutePath();+ String solrHome = solrHomeDir.getAbsolutePath();++ // don't display a huge path for solr home if it is relative to the cwd+ if (!isWindows && cwdPath.length() > 1 && solrHome.startsWith(cwdPath))+ solrHome = solrHome.substring(cwdPath.length() + 1);++ String startCmd =+ String.format(+ Locale.ROOT,+ "\"%s\" start %s -p %d -s \"%s\" %s %s %s %s %s %s",+ callScript,+ cloudModeArg,+ port,+ solrHome,+ hostArg,+ zkHostArg,+ memArg,+ forceArg,+ extraArgs,+ addlOptsArg);+ startCmd = startCmd.replaceAll("\\s+", " ").trim(); // for pretty printing++ echo("\nStarting up Solr on port " + port + " using command:");+ echo(startCmd + "\n");++ String solrUrl =+ String.format(+ Locale.ROOT, "%s://%s:%d/solr", urlScheme, (host != null ? host : "localhost"), port);++ Map<String, Object> nodeStatus = checkPortConflict(solrUrl, solrHomeDir, port);+ if (nodeStatus != null)+ return nodeStatus; // the server they are trying to start is already running++ int code = 0;+ if (isWindows) {+ // On Windows, the execution doesn't return, so we have to execute async+ // and when calling the script, it seems to be inheriting the environment that launched this+ // app, so we have to prune out env vars that may cause issues+ Map<String, String> startEnv = new HashMap<>();+ Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment();+ if (procEnv != null) {+ for (Map.Entry<String, String> entry : procEnv.entrySet()) {+ String envVar = entry.getKey();+ String envVarVal = entry.getValue();+ if (envVarVal != null && !"EXAMPLE".equals(envVar) && !envVar.startsWith("SOLR_")) {+ startEnv.put(envVar, envVarVal);+ }+ }+ }+ DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();+ executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd), startEnv, handler);++ // wait for execution.+ try {+ handler.waitFor(3000);+ } catch (InterruptedException ie) {+ // safe to ignore ...+ Thread.interrupted();+ }+ if (handler.hasResult() && handler.getExitValue() != 0) {+ throw new Exception(+ "Failed to start Solr using command: "+ + startCmd+ + " Exception : "+ + handler.getException());+ }+ } else {+ try {+ code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd));+ } catch (ExecuteException e) {+ throw new Exception(+ "Failed to start Solr using command: " + startCmd + " Exception : " + e);+ }+ }+ if (code != 0) throw new Exception("Failed to start Solr using command: " + startCmd);++ return getNodeStatus(solrUrl, maxWaitSecs);+ }++ protected Map<String, Object> checkPortConflict(String solrUrl, File solrHomeDir, int port) {+ // quickly check if the port is in use+ if (isPortAvailable(port)) return null; // not in use ... try to start++ Map<String, Object> nodeStatus = null;+ try {+ nodeStatus = (new StatusTool()).getStatus(solrUrl);+ } catch (Exception ignore) {+ /* just trying to determine if this example is already running. */+ }++ if (nodeStatus != null) {+ String solr_home = (String) nodeStatus.get("solr_home");+ if (solr_home != null) {+ String solrHomePath = solrHomeDir.getAbsolutePath();+ if (!solrHomePath.endsWith("/")) solrHomePath += "/";+ if (!solr_home.endsWith("/")) solr_home += "/";++ if (solrHomePath.equals(solr_home)) {+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(nodeStatus);+ echo("Solr is already setup and running on port " + port + " with status:\n" + arr);+ echo(+ "\nIf this is not the example node you are trying to start, please choose a different port.");+ nodeStatus.put("baseUrl", solrUrl);+ return nodeStatus;+ }+ }+ }++ throw new IllegalStateException("Port " + port + " is already being used by another process.");+ }++ protected String readExtraArgs(String[] extraArgsArr) {+ String extraArgs = "";+ if (extraArgsArr != null && extraArgsArr.length > 0) {+ StringBuilder sb = new StringBuilder();+ int app = 0;+ for (int e = 0; e < extraArgsArr.length; e++) {+ String arg = extraArgsArr[e];+ if ("e".equals(arg) || "example".equals(arg)) {+ e++; // skip over the example arg+ continue;+ }++ if (app > 0) sb.append(" ");+ sb.append(arg);+ ++app;+ }+ extraArgs = sb.toString().trim();+ }+ return extraArgs;+ }++ protected String createCloudExampleCollection(+ int numNodes, Scanner readInput, boolean prompt, String solrUrl) throws Exception {+ // yay! numNodes SolrCloud nodes running+ int numShards = 2;+ int replicationFactor = 2;+ String cloudConfig = "_default";+ String collectionName = "gettingstarted";++ File configsetsDir = new File(serverDir, "solr/configsets");++ if (prompt) {+ echo(+ "\nNow let's create a new collection for indexing documents in your "+ + numNodes+ + "-node cluster.");++ while (true) {+ collectionName =+ prompt(+ readInput,+ "Please provide a name for your new collection: [" + collectionName + "] ",+ collectionName);++ // Test for existence and then prompt to either create another collection or skip the+ // creation step+ if (SolrCLI.safeCheckCollectionExists(solrUrl, collectionName)) {+ echo("\nCollection '" + collectionName + "' already exists!");+ int oneOrTwo =+ promptForInt(+ readInput,+ "Do you want to re-use the existing collection or create a new one? Enter 1 to reuse, 2 to create new [1]: ",+ "a 1 or 2",+ 1,+ 1,+ 2);+ if (oneOrTwo == 1) {+ return collectionName;+ } else {+ continue;+ }+ } else {+ break; // user selected a collection that doesn't exist ... proceed on+ }+ }++ numShards =+ promptForInt(+ readInput,+ "How many shards would you like to split " + collectionName + " into? [2]",+ "a shard count",+ 2,+ 1,+ 4);++ replicationFactor =+ promptForInt(+ readInput,+ "How many replicas per shard would you like to create? [2] ",+ "a replication factor",+ 2,+ 1,+ 4);++ echo(+ "Please choose a configuration for the "+ + collectionName+ + " collection, available options are:");+ String validConfigs = "_default or sample_techproducts_configs [" + cloudConfig + "] ";+ cloudConfig = prompt(readInput, validConfigs, cloudConfig);++ // validate the cloudConfig name+ while (!isValidConfig(configsetsDir, cloudConfig)) {+ echo(+ cloudConfig+ + " is not a valid configuration directory! Please choose a configuration for the "+ + collectionName+ + " collection, available options are:");+ cloudConfig = prompt(readInput, validConfigs, cloudConfig);+ }+ } else {+ // must verify if default collection exists+ if (SolrCLI.safeCheckCollectionExists(solrUrl, collectionName)) {+ echo(+ "\nCollection '"+ + collectionName+ + "' already exists! Skipping collection creation step.");+ return collectionName;+ }+ }++ // invoke the CreateCollectionTool+ String[] createArgs =+ new String[] {+ "-name", collectionName,+ "-shards", String.valueOf(numShards),+ "-replicationFactor", String.valueOf(replicationFactor),+ "-confname", collectionName,+ "-confdir", cloudConfig,+ "-configsetsDir", configsetsDir.getAbsolutePath(),+ "-solrUrl", solrUrl+ };++ CreateCollectionTool createCollectionTool = new CreateCollectionTool(stdout);+ int createCode =+ createCollectionTool.runTool(+ SolrCLI.processCommandLineArgs(+ createCollectionTool.getName(), createCollectionTool.getOptions(), createArgs));++ if (createCode != 0)+ throw new Exception(+ "Failed to create collection using command: " + Arrays.asList(createArgs));++ return collectionName;+ }++ protected boolean isValidConfig(File configsetsDir, String config) {+ File configDir = new File(configsetsDir, config);+ if (configDir.isDirectory()) return true;++ // not a built-in configset ... maybe it's a custom directory?+ configDir = new File(config);+ return configDir.isDirectory();+ }++ protected Map<String, Object> getNodeStatus(String solrUrl, int maxWaitSecs) throws Exception {+ StatusTool statusTool = new StatusTool();+ if (verbose) echo("\nChecking status of Solr at " + solrUrl + " ...");++ URL solrURL = new URL(solrUrl);+ Map<String, Object> nodeStatus =+ statusTool.waitToSeeSolrUp(solrUrl, maxWaitSecs, TimeUnit.SECONDS);+ nodeStatus.put("baseUrl", solrUrl);+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(nodeStatus);+ String mode = (nodeStatus.get("cloud") != null) ? "cloud" : "standalone";+ if (verbose)+ echo(+ "\nSolr is running on "+ + solrURL.getPort()+ + " in "+ + mode+ + " mode with status:\n"+ + arr);++ return nodeStatus;+ }++ protected File setupExampleDir(File serverDir, File exampleParentDir, String dirName)+ throws IOException {+ File solrXml = new File(serverDir, "solr/solr.xml");+ if (!solrXml.isFile())+ throw new IllegalArgumentException(+ "Value of -serverDir option is invalid! " + solrXml.getAbsolutePath() + " not found!");++ File zooCfg = new File(serverDir, "solr/zoo.cfg");+ if (!zooCfg.isFile())+ throw new IllegalArgumentException(+ "Value of -serverDir option is invalid! " + zooCfg.getAbsolutePath() + " not found!");++ File solrHomeDir = new File(exampleParentDir, dirName + "/solr");+ if (!solrHomeDir.isDirectory()) {+ echo("Creating Solr home directory " + solrHomeDir);+ solrHomeDir.mkdirs();+ } else {+ echo("Solr home directory " + solrHomeDir.getAbsolutePath() + " already exists.");+ }++ copyIfNeeded(solrXml, new File(solrHomeDir, "solr.xml"));+ copyIfNeeded(zooCfg, new File(solrHomeDir, "zoo.cfg"));++ return solrHomeDir.getParentFile();+ }++ protected void copyIfNeeded(File src, File dest) throws IOException {+ if (!dest.isFile()) Files.copy(src.toPath(), dest.toPath());++ if (!dest.isFile())+ throw new IllegalStateException("Required file " + dest.getAbsolutePath() + " not found!");+ }++ protected boolean isPortAvailable(int port) {+ try (Socket s = new Socket("localhost", port)) {+ assert s != null; // To allow compilation..+ return false;+ } catch (IOException e) {+ return true;+ }+ }++ protected Integer promptForPort(Scanner s, int node, String prompt, Integer defVal) {+ return promptForInt(s, prompt, "a port for node " + node, defVal, null, null);+ }++ protected Integer promptForInt(+ Scanner s, String prompt, String label, Integer defVal, Integer min, Integer max) {+ Integer inputAsInt = null;++ String value = prompt(s, prompt, null /* default is null since we handle that here */);+ if (value != null) {+ int attempts = 3;+ while (value != null && --attempts > 0) {+ try {+ inputAsInt = Integer.valueOf(value);++ if (min != null) {+ if (inputAsInt < min) {+ value =+ prompt(+ s,+ String.format(+ Locale.ROOT,+ PROMPT_NUMBER_TOO_SMALL,+ inputAsInt,+ label,+ min,+ max,+ defVal));+ inputAsInt = null;+ continue;+ }+ }++ if (max != null) {+ if (inputAsInt > max) {+ value =+ prompt(+ s,+ String.format(+ Locale.ROOT,+ PROMPT_NUMBER_TOO_LARGE,+ inputAsInt,+ label,+ min,+ max,+ defVal));+ inputAsInt = null;+ }+ }++ } catch (NumberFormatException nfe) {+ if (verbose) echo(value + " is not a number!");++ if (min != null && max != null) {+ value =+ prompt(+ s,+ String.format(+ Locale.ROOT, PROMPT_FOR_NUMBER_IN_RANGE, label, min, max, defVal));+ } else {+ value = prompt(s, String.format(Locale.ROOT, PROMPT_FOR_NUMBER, label, defVal));+ }+ }+ }+ if (attempts == 0 && inputAsInt == null)+ echo("Too many failed attempts! Going with default value " + defVal);+ }++ return (inputAsInt != null) ? inputAsInt : defVal;+ }++ protected String prompt(Scanner s, String prompt) {+ return prompt(s, prompt, null);+ }++ protected String prompt(Scanner s, String prompt, String defaultValue) {+ echo(prompt);+ String nextInput = s.nextLine();+ if (nextInput != null) {+ nextInput = nextInput.trim();+ if (nextInput.isEmpty()) nextInput = null;+ }+ return (nextInput != null) ? nextInput : defaultValue;+ }+}diff --git a/solr/core/src/java/org/apache/solr/util/SimplePostTool.java b/solr/core/src/java/org/apache/solr/cli/SimplePostTool.javasimilarity index 99%rename from solr/core/src/java/org/apache/solr/util/SimplePostTool.javarename to solr/core/src/java/org/apache/solr/cli/SimplePostTool.javaindex 0cf6122c4bc..8a364259a71 100644--- a/solr/core/src/java/org/apache/solr/util/SimplePostTool.java+++ b/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import static java.nio.charset.StandardCharsets.US_ASCII;import static java.nio.charset.StandardCharsets.UTF_8;@@ -66,14 +66,15 @@ import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpression;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;+import org.apache.solr.util.RTimer;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;/**- * A simple utility class for posting raw updates to a Solr server, has a main method so it can be- * run on the command line. View this not as a best-practice code example, but as a standalone+ * A simple utility class for posting raw updates to a Solr server. It has a main method, so it can+ * be run on the command line. View this not as a best-practice code example, but as a standalone* example built with an explicit purpose of not having external jar dependencies.*/public class SimplePostTool {@@ -357,7 +358,7 @@ public class SimplePostTool {}}- private int doWebMode() {+ private void doWebMode() {reset();int numPagesPosted = 0;try {@@ -366,7 +367,7 @@ public class SimplePostTool {}if (args[0].equals("-")) {// Skip posting url if special param "-" given- return 0;+ return;}// Set Extracting handler as defaultsolrUrl = appendUrlPath(solrUrl, "/extract");@@ -391,7 +392,7 @@ public class SimplePostTool {} catch (MalformedURLException e) {fatal("Wrong URL trying to append /extract to " + solrUrl);}- return numPagesPosted;+ return;}private void doStdinMode() {@@ -898,11 +899,11 @@ public class SimplePostTool {if (!urlStr.contains("resource.name"))urlStr =appendParam(- urlStr, "resource.name=" + URLEncoder.encode(file.getAbsolutePath(), "UTF-8"));+ urlStr, "resource.name=" + URLEncoder.encode(file.getAbsolutePath(), UTF_8));if (!urlStr.contains("literal.id"))urlStr =appendParam(- urlStr, "literal.id=" + URLEncoder.encode(file.getAbsolutePath(), "UTF-8"));+ urlStr, "literal.id=" + URLEncoder.encode(file.getAbsolutePath(), UTF_8));url = new URL(urlStr);}} else {diff --git a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java b/solr/core/src/java/org/apache/solr/cli/SolrCLI.javanew file mode 100755index 00000000000..6ab176d4aa3--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java@@ -0,0 +1,652 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import static org.apache.solr.common.SolrException.ErrorCode.FORBIDDEN;+import static org.apache.solr.common.SolrException.ErrorCode.UNAUTHORIZED;+import static org.apache.solr.common.params.CommonParams.NAME;++import com.google.common.annotations.VisibleForTesting;+import java.io.File;+import java.lang.invoke.MethodHandles;+import java.net.ConnectException;+import java.net.SocketException;+import java.net.URI;+import java.net.URL;+import java.util.ArrayList;+import java.util.Arrays;+import java.util.Collections;+import java.util.Enumeration;+import java.util.List;+import java.util.Locale;+import java.util.Map;+import java.util.Optional;+import java.util.Set;+import java.util.TreeSet;+import java.util.concurrent.TimeUnit;+import java.util.stream.Collectors;+import java.util.stream.Stream;+import java.util.zip.ZipEntry;+import java.util.zip.ZipInputStream;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.GnuParser;+import org.apache.commons.cli.HelpFormatter;+import org.apache.commons.cli.Option;+import org.apache.commons.cli.Options;+import org.apache.commons.cli.ParseException;+import org.apache.solr.client.solrj.SolrClient;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.SolrServerException;+import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.impl.Http2SolrClient;+import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;+import org.apache.solr.client.solrj.request.CoreAdminRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;+import org.apache.solr.common.SolrException;+import org.apache.solr.common.cloud.ZkStateReader;+import org.apache.solr.common.params.CommonParams;+import org.apache.solr.common.params.ModifiableSolrParams;+import org.apache.solr.common.util.ContentStreamBase;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.util.SolrVersion;+import org.apache.solr.util.StartupLoggingUtils;+import org.apache.solr.util.configuration.SSLConfigurationsFactory;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/** Command-line utility for working with Solr. */+public class SolrCLI implements CLIO {+ private static final long MAX_WAIT_FOR_CORE_LOAD_NANOS =+ TimeUnit.NANOSECONDS.convert(1, TimeUnit.MINUTES);++ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());+ public static final String DEFAULT_SOLR_URL = "http://localhost:8983/solr";+ public static final String ZK_HOST = "localhost:9983";++ public static final Option OPTION_ZKHOST =+ Option.builder("z")+ .argName("HOST")+ .hasArg()+ .required(false)+ .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST)+ .longOpt("zkHost")+ .build();+ public static final Option OPTION_SOLRURL =+ Option.builder("solrUrl")+ .argName("HOST")+ .hasArg()+ .required(false)+ .desc(+ "Base Solr URL, which can be used to determine the zkHost if that's not known; defaults to: "+ + DEFAULT_SOLR_URL)+ .build();+ public static final Option OPTION_VERBOSE =+ Option.builder("verbose").required(false).desc("Enable more verbose command output.").build();++ public static final Option OPTION_RECURSE =+ Option.builder("recurse")+ .argName("recurse")+ .hasArg()+ .required(false)+ .desc("Recurse (true|false), default is false.")+ // .type(Boolean.class)+ .build();++ public static final List<Option> cloudOptions =+ List.of(+ OPTION_ZKHOST,+ Option.builder("c")+ .argName("COLLECTION")+ .hasArg()+ .required(false)+ .desc("Name of collection; no default.")+ .longOpt("collection")+ .build(),+ OPTION_VERBOSE);++ public static void exit(int exitStatus) {+ try {+ System.exit(exitStatus);+ } catch (java.lang.SecurityException secExc) {+ if (exitStatus != 0)+ throw new RuntimeException("SolrCLI failed to exit with status " + exitStatus);+ }+ }++ /** Runs a tool. */+ public static void main(String[] args) throws Exception {+ if (args == null || args.length == 0 || args[0] == null || args[0].trim().length() == 0) {+ CLIO.err(+ "Invalid command-line args! Must pass the name of a tool to run.\n"+ + "Supported tools:\n");+ displayToolOptions();+ exit(1);+ }++ if (args.length == 1 && Arrays.asList("-v", "-version", "version").contains(args[0])) {+ // Simple version tool, no need for its own class+ CLIO.out(SolrVersion.LATEST.toString());+ exit(0);+ }++ SSLConfigurationsFactory.current().init();++ Tool tool = null;+ try {+ tool = findTool(args);+ } catch (IllegalArgumentException iae) {+ CLIO.err(iae.getMessage());+ System.exit(1);+ }+ CommandLine cli = parseCmdLine(tool.getName(), args, tool.getOptions());+ System.exit(tool.runTool(cli));+ }++ public static Tool findTool(String[] args) throws Exception {+ String toolType = args[0].trim().toLowerCase(Locale.ROOT);+ return newTool(toolType);+ }++ public static CommandLine parseCmdLine(String toolName, String[] args, List<Option> toolOptions) {+ // the parser doesn't like -D props+ List<String> toolArgList = new ArrayList<>();+ List<String> dashDList = new ArrayList<>();+ for (int a = 1; a < args.length; a++) {+ String arg = args[a];+ if (arg.startsWith("-D")) {+ dashDList.add(arg);+ } else {+ toolArgList.add(arg);+ }+ }+ String[] toolArgs = toolArgList.toArray(new String[0]);++ // process command-line args to configure this application+ CommandLine cli = processCommandLineArgs(toolName, toolOptions, toolArgs);++ List<String> argList = cli.getArgList();+ argList.addAll(dashDList);++ // for SSL support, try to accommodate relative paths set for SSL store props+ String solrInstallDir = System.getProperty("solr.install.dir");+ if (solrInstallDir != null) {+ checkSslStoreSysProp(solrInstallDir, "keyStore");+ checkSslStoreSysProp(solrInstallDir, "trustStore");+ }++ return cli;+ }++ protected static void checkSslStoreSysProp(String solrInstallDir, String key) {+ String sysProp = "javax.net.ssl." + key;+ String keyStore = System.getProperty(sysProp);+ if (keyStore == null) return;++ File keyStoreFile = new File(keyStore);+ if (keyStoreFile.isFile()) return; // configured setting is OK++ keyStoreFile = new File(solrInstallDir, "server/" + keyStore);+ if (keyStoreFile.isFile()) {+ System.setProperty(sysProp, keyStoreFile.getAbsolutePath());+ } else {+ CLIO.err(+ "WARNING: "+ + sysProp+ + " file "+ + keyStore+ + " not found! https requests to Solr will likely fail; please update your "+ + sysProp+ + " setting to use an absolute path.");+ }+ }++ public static void raiseLogLevelUnlessVerbose(CommandLine cli) {+ if (!cli.hasOption(OPTION_VERBOSE.getOpt())) {+ StartupLoggingUtils.changeLogLevel("WARN");+ }+ }++ /** Support options common to all tools. */+ public static List<Option> getCommonToolOptions() {+ return List.of();+ }++ // Creates an instance of the requested tool, using classpath scanning if necessary+ private static Tool newTool(String toolType) throws Exception {+ if ("healthcheck".equals(toolType)) return new HealthcheckTool();+ else if ("status".equals(toolType)) return new StatusTool();+ else if ("api".equals(toolType)) return new ApiTool();+ else if ("create_collection".equals(toolType)) return new CreateCollectionTool();+ else if ("create_core".equals(toolType)) return new CreateCoreTool();+ else if ("create".equals(toolType)) return new CreateTool();+ else if ("delete".equals(toolType)) return new DeleteTool();+ else if ("config".equals(toolType)) return new ConfigTool();+ else if ("run_example".equals(toolType)) return new RunExampleTool();+ else if ("upconfig".equals(toolType)) return new ConfigSetUploadTool();+ else if ("downconfig".equals(toolType)) return new ConfigSetDownloadTool();+ else if ("rm".equals(toolType)) return new ZkRmTool();+ else if ("mv".equals(toolType)) return new ZkMvTool();+ else if ("cp".equals(toolType)) return new ZkCpTool();+ else if ("ls".equals(toolType)) return new ZkLsTool();+ else if ("mkroot".equals(toolType)) return new ZkMkrootTool();+ else if ("assert".equals(toolType)) return new AssertTool();+ else if ("auth".equals(toolType)) return new AuthTool();+ else if ("export".equals(toolType)) return new ExportTool();+ else if ("package".equals(toolType)) return new PackageTool();++ // If you add a built-in tool to this class, add it here to avoid+ // classpath scanning++ for (Class<? extends Tool> next : findToolClassesInPackage("org.apache.solr.util")) {+ Tool tool = next.getConstructor().newInstance();+ if (toolType.equals(tool.getName())) return tool;+ }++ throw new IllegalArgumentException(toolType + " is not a valid command!");+ }++ private static void displayToolOptions() throws Exception {+ HelpFormatter formatter = new HelpFormatter();+ formatter.printHelp("healthcheck", getToolOptions(new HealthcheckTool()));+ formatter.printHelp("status", getToolOptions(new StatusTool()));+ formatter.printHelp("api", getToolOptions(new ApiTool()));+ formatter.printHelp("create_collection", getToolOptions(new CreateCollectionTool()));+ formatter.printHelp("create_core", getToolOptions(new CreateCoreTool()));+ formatter.printHelp("create", getToolOptions(new CreateTool()));+ formatter.printHelp("delete", getToolOptions(new DeleteTool()));+ formatter.printHelp("config", getToolOptions(new ConfigTool()));+ formatter.printHelp("run_example", getToolOptions(new RunExampleTool()));+ formatter.printHelp("upconfig", getToolOptions(new ConfigSetUploadTool()));+ formatter.printHelp("downconfig", getToolOptions(new ConfigSetDownloadTool()));+ formatter.printHelp("rm", getToolOptions(new ZkRmTool()));+ formatter.printHelp("cp", getToolOptions(new ZkCpTool()));+ formatter.printHelp("mv", getToolOptions(new ZkMvTool()));+ formatter.printHelp("ls", getToolOptions(new ZkLsTool()));+ formatter.printHelp("export", getToolOptions(new ExportTool()));+ formatter.printHelp("package", getToolOptions(new PackageTool()));++ List<Class<? extends Tool>> toolClasses = findToolClassesInPackage("org.apache.solr.util");+ for (Class<? extends Tool> next : toolClasses) {+ Tool tool = next.getConstructor().newInstance();+ formatter.printHelp(tool.getName(), getToolOptions(tool));+ }+ }++ public static Options getToolOptions(Tool tool) {+ Options options = new Options();+ options.addOption("help", false, "Print this message");+ options.addOption(OPTION_VERBOSE);+ List<Option> toolOpts = tool.getOptions();+ for (Option toolOpt : toolOpts) {+ options.addOption(toolOpt);+ }+ return options;+ }++ public static List<Option> joinOptions(List<Option> lhs, List<Option> rhs) {+ if (lhs == null) {+ return rhs == null ? List.of() : rhs;+ }++ if (rhs == null) {+ return lhs;+ }++ return Stream.concat(lhs.stream(), rhs.stream()).collect(Collectors.toUnmodifiableList());+ }++ /** Parses the command-line arguments passed by the user. */+ public static CommandLine processCommandLineArgs(+ String toolName, List<Option> customOptions, String[] args) {+ Options options = new Options();++ options.addOption("help", false, "Print this message");+ options.addOption(OPTION_VERBOSE);++ if (customOptions != null) {+ for (Option customOption : customOptions) {+ options.addOption(customOption);+ }+ }++ CommandLine cli = null;+ try {+ cli = (new GnuParser()).parse(options, args);+ } catch (ParseException exp) {+ boolean hasHelpArg = false;+ if (args != null) {+ for (String arg : args) {+ if ("--help".equals(arg) || "-help".equals(arg)) {+ hasHelpArg = true;+ break;+ }+ }+ }+ if (!hasHelpArg) {+ CLIO.err("Failed to parse command-line arguments due to: " + exp.getMessage());+ }+ HelpFormatter formatter = new HelpFormatter();+ formatter.printHelp(toolName, options);+ exit(1);+ }++ if (cli.hasOption("help")) {+ HelpFormatter formatter = new HelpFormatter();+ formatter.printHelp(toolName, options);+ exit(0);+ }++ return cli;+ }++ /** Scans Jar files on the classpath for Tool implementations to activate. */+ private static List<Class<? extends Tool>> findToolClassesInPackage(String packageName) {+ List<Class<? extends Tool>> toolClasses = new ArrayList<>();+ try {+ ClassLoader classLoader = SolrCLI.class.getClassLoader();+ String path = packageName.replace('.', '/');+ Enumeration<URL> resources = classLoader.getResources(path);+ Set<String> classes = new TreeSet<>();+ while (resources.hasMoreElements()) {+ URL resource = resources.nextElement();+ classes.addAll(findClasses(resource.getFile(), packageName));+ }++ for (String classInPackage : classes) {+ Class<?> theClass = Class.forName(classInPackage);+ if (Tool.class.isAssignableFrom(theClass)) toolClasses.add(theClass.asSubclass(Tool.class));+ }+ } catch (Exception e) {+ // safe to squelch this as it's just looking for tools to run+ log.debug("Failed to find Tool impl classes in {}, due to: ", packageName, e);+ }+ return toolClasses;+ }++ private static Set<String> findClasses(String path, String packageName) throws Exception {+ Set<String> classes = new TreeSet<>();+ if (path.startsWith("file:") && path.contains("!")) {+ String[] split = path.split("!");+ URL jar = new URL(split[0]);+ try (ZipInputStream zip = new ZipInputStream(jar.openStream())) {+ ZipEntry entry;+ while ((entry = zip.getNextEntry()) != null) {+ if (entry.getName().endsWith(".class")) {+ String className =+ entry+ .getName()+ .replaceAll("[$].*", "")+ .replaceAll("[.]class", "")+ .replace('/', '.');+ if (className.startsWith(packageName)) classes.add(className);+ }+ }+ }+ }+ return classes;+ }++ /**+ * Determine if a request to Solr failed due to a communication error, which is generally+ * retry-able.+ */+ public static boolean checkCommunicationError(Exception exc) {+ Throwable rootCause = SolrException.getRootCause(exc);+ boolean wasCommError =+ (rootCause instanceof ConnectException+ || rootCause instanceof SolrServerException+ || rootCause instanceof SocketException);+ return wasCommError;+ }++ public static void checkCodeForAuthError(int code) {+ if (code == UNAUTHORIZED.code || code == FORBIDDEN.code) {+ throw new SolrException(+ SolrException.ErrorCode.getErrorCode(code),+ "Solr requires authentication for request. Please supply valid credentials. HTTP code="+ + code);+ }+ }++ public static boolean exceptionIsAuthRelated(Exception exc) {+ return (exc instanceof SolrException+ && Arrays.asList(UNAUTHORIZED.code, FORBIDDEN.code).contains(((SolrException) exc).code()));+ }++ public static SolrClient getSolrClient(String solrUrl) {+ return new Http2SolrClient.Builder(solrUrl).maxConnectionsPerHost(32).build();+ }++ /**+ * Get Solr base url with port if present and root from URI+ *+ * @param uri Full Solr URI (e.g. http://localhost:8983/solr/admin/info/system)+ * @return Solr base url with port and root (from above example http://localhost:8983/solr)+ */+ public static String getSolrUrlFromUri(URI uri) {+ return uri.getScheme() + "://" + uri.getAuthority() + "/" + uri.getPath().split("/")[1];+ }++ public static ModifiableSolrParams getSolrParamsFromUri(URI uri) {+ ModifiableSolrParams paramsMap = new ModifiableSolrParams();+ String[] params = uri.getQuery() == null ? new String[] {} : uri.getQuery().split("&");+ for (String param : params) {+ String[] paramSplit = param.split("=");+ paramsMap.add(paramSplit[0], paramSplit[1]);+ }+ return paramsMap;+ }++ public static final String JSON_CONTENT_TYPE = "application/json";++ public static NamedList<Object> postJsonToSolr(+ SolrClient solrClient, String updatePath, String jsonBody) throws Exception {+ ContentStreamBase.StringStream contentStream = new ContentStreamBase.StringStream(jsonBody);+ contentStream.setContentType(JSON_CONTENT_TYPE);+ ContentStreamUpdateRequest req = new ContentStreamUpdateRequest(updatePath);+ req.addContentStream(contentStream);+ return solrClient.request(req);+ }++ public static final String DEFAULT_CONFIG_SET = "_default";++ private static final long MS_IN_MIN = 60 * 1000L;+ private static final long MS_IN_HOUR = MS_IN_MIN * 60L;+ private static final long MS_IN_DAY = MS_IN_HOUR * 24L;++ @VisibleForTesting+ public static final String uptime(long uptimeMs) {+ if (uptimeMs <= 0L) return "?";++ long numDays = (uptimeMs >= MS_IN_DAY) ? (uptimeMs / MS_IN_DAY) : 0L;+ long rem = uptimeMs - (numDays * MS_IN_DAY);+ long numHours = (rem >= MS_IN_HOUR) ? (rem / MS_IN_HOUR) : 0L;+ rem = rem - (numHours * MS_IN_HOUR);+ long numMinutes = (rem >= MS_IN_MIN) ? (rem / MS_IN_MIN) : 0L;+ rem = rem - (numMinutes * MS_IN_MIN);+ long numSeconds = Math.round(rem / 1000.0);+ return String.format(+ Locale.ROOT,+ "%d days, %d hours, %d minutes, %d seconds",+ numDays,+ numHours,+ numMinutes,+ numSeconds);+ }++ public static final List<Option> CREATE_COLLECTION_OPTIONS =+ List.of(+ OPTION_ZKHOST,+ OPTION_SOLRURL,+ Option.builder(NAME)+ .argName("NAME")+ .hasArg()+ .required(true)+ .desc("Name of collection to create.")+ .build(),+ Option.builder("shards")+ .argName("#")+ .hasArg()+ .required(false)+ .desc("Number of shards; default is 1.")+ .build(),+ Option.builder("replicationFactor")+ .argName("#")+ .hasArg()+ .required(false)+ .desc(+ "Number of copies of each document across the collection (replicas per shard); default is 1.")+ .build(),+ Option.builder("confdir")+ .argName("NAME")+ .hasArg()+ .required(false)+ .desc(+ "Configuration directory to copy when creating the new collection; default is "+ + DEFAULT_CONFIG_SET+ + '.')+ .build(),+ Option.builder("confname")+ .argName("NAME")+ .hasArg()+ .required(false)+ .desc("Configuration name; default is the collection name.")+ .build(),+ Option.builder("configsetsDir")+ .argName("DIR")+ .hasArg()+ .required(true)+ .desc("Path to configsets directory on the local system.")+ .build(),+ OPTION_VERBOSE);++ /**+ * Get the base URL of a live Solr instance from either the solrUrl command-line option from+ * ZooKeeper.+ */+ public static String resolveSolrUrl(CommandLine cli) throws Exception {+ String solrUrl = cli.getOptionValue("solrUrl");+ if (solrUrl == null) {+ String zkHost = cli.getOptionValue("zkHost");+ if (zkHost == null)+ throw new IllegalStateException(+ "Must provide either the '-solrUrl' or '-zkHost' parameters!");++ try (CloudSolrClient cloudSolrClient =+ new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())+ .build()) {+ cloudSolrClient.connect();+ Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();+ if (liveNodes.isEmpty())+ throw new IllegalStateException(+ "No live nodes found! Cannot determine 'solrUrl' from ZooKeeper: " + zkHost);++ String firstLiveNode = liveNodes.iterator().next();+ solrUrl = ZkStateReader.from(cloudSolrClient).getBaseUrlForNodeName(firstLiveNode);+ }+ }+ return solrUrl;+ }++ /**+ * Get the ZooKeeper connection string from either the zkHost command-line option or by looking it+ * up from a running Solr instance based on the solrUrl option.+ */+ public static String getZkHost(CommandLine cli) throws Exception {+ String zkHost = cli.getOptionValue("zkHost");+ if (zkHost != null) return zkHost;++ // find it using the localPort+ String solrUrl = cli.getOptionValue("solrUrl");+ if (solrUrl == null)+ throw new IllegalStateException(+ "Must provide either the -zkHost or -solrUrl parameters to use the create_collection command!");++ if (!solrUrl.endsWith("/")) solrUrl += "/";++ try (var solrClient = getSolrClient(solrUrl)) {+ // hit Solr to get system info+ NamedList<Object> systemInfo =+ solrClient.request(+ new GenericSolrRequest(SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH));++ // convert raw JSON into user-friendly output+ StatusTool statusTool = new StatusTool();+ Map<String, Object> status = statusTool.reportStatus(systemInfo, solrClient);+ @SuppressWarnings("unchecked")+ Map<String, Object> cloud = (Map<String, Object>) status.get("cloud");+ if (cloud != null) {+ String zookeeper = (String) cloud.get("ZooKeeper");+ if (zookeeper.endsWith("(embedded)")) {+ zookeeper = zookeeper.substring(0, zookeeper.length() - "(embedded)".length());+ }+ zkHost = zookeeper;+ }+ }++ return zkHost;+ }++ public static boolean safeCheckCollectionExists(String solrUrl, String collection) {+ boolean exists = false;+ try (var solrClient = getSolrClient(solrUrl); ) {+ NamedList<Object> existsCheckResult = solrClient.request(new CollectionAdminRequest.List());+ @SuppressWarnings("unchecked")+ List<String> collections = (List<String>) existsCheckResult.get("collections");+ exists = collections != null && collections.contains(collection);+ } catch (Exception exc) {+ // just ignore it since we're only interested in a positive result here+ }+ return exists;+ }++ @SuppressWarnings("unchecked")+ public static boolean safeCheckCoreExists(String solrUrl, String coreName) {+ boolean exists = false;+ try (var solrClient = getSolrClient(solrUrl)) {+ boolean wait = false;+ final long startWaitAt = System.nanoTime();+ do {+ if (wait) {+ final int clamPeriodForStatusPollMs = 1000;+ Thread.sleep(clamPeriodForStatusPollMs);+ }+ NamedList<Object> existsCheckResult =+ CoreAdminRequest.getStatus(coreName, solrClient).getResponse();+ NamedList<Object> status = (NamedList) existsCheckResult.get("status");+ NamedList<Object> coreStatus = (NamedList) status.get(coreName);+ Map<String, Object> failureStatus =+ (Map<String, Object>) existsCheckResult.get("initFailures");+ String errorMsg = (String) failureStatus.get(coreName);+ final boolean hasName = coreStatus != null && coreStatus.asMap().containsKey(NAME);+ exists = hasName || errorMsg != null;+ wait = hasName && errorMsg == null && "true".equals(coreStatus.get("isLoading"));+ } while (wait && System.nanoTime() - startWaitAt < MAX_WAIT_FOR_CORE_LOAD_NANOS);+ } catch (Exception exc) {+ // just ignore it since we're only interested in a positive result here+ }+ return exists;+ }++ public static class AssertionFailureException extends Exception {+ public AssertionFailureException(String message) {+ super(message);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/SolrCloudTool.java b/solr/core/src/java/org/apache/solr/cli/SolrCloudTool.javanew file mode 100644index 00000000000..7946efb1d28--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/SolrCloudTool.java@@ -0,0 +1,66 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.Collections;+import java.util.List;+import java.util.Optional;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/**+ * Helps build SolrCloud aware tools by initializing a CloudSolrClient instance before running the+ * tool.+ */+public abstract class SolrCloudTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ protected SolrCloudTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return SolrCLI.cloudOptions;+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = cli.getOptionValue(SolrCLI.OPTION_ZKHOST.getOpt(), SolrCLI.ZK_HOST);++ log.debug("Connecting to Solr cluster: {}", zkHost);+ try (var cloudSolrClient =+ new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())+ .build()) {++ cloudSolrClient.connect();+ runCloudTool(cloudSolrClient, cli);+ }+ }++ /** Runs a SolrCloud tool with CloudSolrClient initialized */+ protected abstract void runCloudTool(CloudSolrClient cloudSolrClient, CommandLine cli)+ throws Exception;+}diff --git a/solr/core/src/java/org/apache/solr/util/SolrLogPostTool.java b/solr/core/src/java/org/apache/solr/cli/SolrLogPostTool.javasimilarity index 94%rename from solr/core/src/java/org/apache/solr/util/SolrLogPostTool.javarename to solr/core/src/java/org/apache/solr/cli/SolrLogPostTool.javaindex d1c361b9164..8ee87a3b8a8 100644--- a/solr/core/src/java/org/apache/solr/util/SolrLogPostTool.java+++ b/solr/core/src/java/org/apache/solr/cli/SolrLogPostTool.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import java.io.BufferedReader;import java.io.IOException;@@ -33,7 +33,6 @@ import java.util.regex.Pattern;import java.util.stream.Collectors;import java.util.stream.Stream;import org.apache.solr.client.solrj.SolrClient;-import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.Http2SolrClient;import org.apache.solr.client.solrj.request.UpdateRequest;import org.apache.solr.common.SolrInputDocument;@@ -79,7 +78,7 @@ public class SolrLogPostTool {try (LineNumberReader reader =new LineNumberReader(Files.newBufferedReader(file, Charset.defaultCharset()))) {LogRecordReader recordReader = new LogRecordReader(reader);- SolrInputDocument doc = null;+ SolrInputDocument doc;String fileName = file.getFileName().toString();while (true) {try {@@ -115,8 +114,7 @@ public class SolrLogPostTool {}}- private static void sendBatch(SolrClient client, UpdateRequest request, boolean lastRequest)- throws SolrServerException, IOException {+ private static void sendBatch(SolrClient client, UpdateRequest request, boolean lastRequest) {final String beginMessage =lastRequest ? "Sending last batch ..." : "Sending batch of 300 log records...";CLIO.out(beginMessage);@@ -216,7 +214,7 @@ public class SolrLogPostTool {if (!m.find() && buf.length() < 10000) {// Line does not start with a timestamp so append to the stack trace- buf.append(line.replace("\t", " ") + "<br/>");+ buf.append(line.replace("\t", " ")).append("<br/>");if (line.startsWith("Caused by:")) {this.cause = line;}@@ -266,8 +264,7 @@ public class SolrLogPostTool {doc.setField(fieldName, fieldValue);}- private void parseError(SolrInputDocument lineRecord, String line, String trace)- throws IOException {+ private void parseError(SolrInputDocument lineRecord, String line, String trace) {lineRecord.setField("type_s", "error");// Don't include traces that have only the %html header.@@ -327,7 +324,7 @@ public class SolrLogPostTool {private String parseCollection(String line) {char[] ca = {' ', ']', ','};- String parts[] = line.split("c:");+ String[] parts = line.split("c:");if (parts.length >= 2) {return readUntil(parts[1], ca);} else {@@ -354,7 +351,7 @@ public class SolrLogPostTool {private String parseCore(String line) {char[] ca = {' ', ']', '}', ','};- String parts[] = line.split("x:");+ String[] parts = line.split("x:");if (parts.length >= 2) {return readUntil(parts[1], ca);} else {@@ -364,7 +361,7 @@ public class SolrLogPostTool {private String parseShard(String line) {char[] ca = {' ', ']', '}', ','};- String parts[] = line.split("s:");+ String[] parts = line.split("s:");if (parts.length >= 2) {return readUntil(parts[1], ca);} else {@@ -374,7 +371,7 @@ public class SolrLogPostTool {private String parseReplica(String line) {char[] ca = {' ', ']', '}', ','};- String parts[] = line.split("r:");+ String[] parts = line.split("r:");if (parts.length >= 2) {return readUntil(parts[1], ca);} else {@@ -384,7 +381,7 @@ public class SolrLogPostTool {private String parsePath(String line) {char[] ca = {' '};- String parts[] = line.split(" path=");+ String[] parts = line.split(" path=");if (parts.length == 2) {return readUntil(parts[1], ca);} else {@@ -394,7 +391,7 @@ public class SolrLogPostTool {private String parseQTime(String line) {char[] ca = {'\n', '\r'};- String parts[] = line.split(" QTime=");+ String[] parts = line.split(" QTime=");if (parts.length == 2) {return readUntil(parts[1], ca);} else {@@ -404,7 +401,7 @@ public class SolrLogPostTool {private String parseNode(String line) {char[] ca = {' ', ']', '}', ','};- String parts[] = line.split("node_name=n:");+ String[] parts = line.split("node_name=n:");if (parts.length >= 2) {return readUntil(parts[1], ca);} else {@@ -414,7 +411,7 @@ public class SolrLogPostTool {private String parseStatus(String line) {char[] ca = {' ', '\n', '\r'};- String parts[] = line.split(" status=");+ String[] parts = line.split(" status=");if (parts.length == 2) {return readUntil(parts[1], ca);} else {@@ -424,7 +421,7 @@ public class SolrLogPostTool {private String parseHits(String line) {char[] ca = {' '};- String parts[] = line.split(" hits=");+ String[] parts = line.split(" hits=");if (parts.length == 2) {return readUntil(parts[1], ca);} else {@@ -434,7 +431,7 @@ public class SolrLogPostTool {private String parseParams(String line) {char[] ca = {' '};- String parts[] = line.split(" params=");+ String[] parts = line.split(" params=");if (parts.length == 2) {String p = readUntil(parts[1].substring(1), ca);return p.substring(0, p.length() - 1);@@ -458,11 +455,6 @@ public class SolrLogPostTool {return builder.toString();}- private void addOrReplaceFieldValue(- SolrInputDocument doc, String fieldName, String fieldValue) {- doc.setField(fieldName, fieldValue);- }-private void addParams(SolrInputDocument doc, String params) {String[] pairs = params.split("&");for (String pair : pairs) {@@ -526,7 +518,7 @@ public class SolrLogPostTool {// Special params used to determine what stage a query is.// So we populate with defaults.- // The absence of the distrib params means its a distributed query.+ // The absence of the distrib params means it's a distributed query.setFieldIfUnset(doc, "distrib_s", "true");setFieldIfUnset(doc, "shards_s", "false");setFieldIfUnset(doc, "ids_s", "false");diff --git a/solr/core/src/java/org/apache/solr/cli/StatusTool.java b/solr/core/src/java/org/apache/solr/cli/StatusTool.javanew file mode 100644index 00000000000..0a9501ec5aa--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/StatusTool.java@@ -0,0 +1,195 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.io.PrintStream;+import java.net.URL;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.TimeoutException;+import javax.net.ssl.SSLPeerUnverifiedException;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrClient;+import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;+import org.apache.solr.common.params.CommonParams;+import org.apache.solr.common.util.NamedList;+import org.noggit.CharArr;+import org.noggit.JSONWriter;++public class StatusTool extends ToolBase {+ /** Get the status of a Solr server. */+ public StatusTool() {+ this(CLIO.getOutStream());+ }++ public StatusTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public String getName() {+ return "status";+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("solr")+ .argName("URL")+ .hasArg()+ .required(false)+ .desc(+ "Address of the Solr Web application, defaults to: "+ + SolrCLI.DEFAULT_SOLR_URL+ + '.')+ .build(),+ Option.builder("maxWaitSecs")+ .argName("SECS")+ .hasArg()+ .required(false)+ .desc("Wait up to the specified number of seconds to see Solr running.")+ .build());+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ int maxWaitSecs = Integer.parseInt(cli.getOptionValue("maxWaitSecs", "0"));+ String solrUrl = cli.getOptionValue("solr", SolrCLI.DEFAULT_SOLR_URL);+ if (maxWaitSecs > 0) {+ int solrPort = (new URL(solrUrl)).getPort();+ echo("Waiting up to " + maxWaitSecs + " seconds to see Solr running on port " + solrPort);+ try {+ waitToSeeSolrUp(solrUrl, maxWaitSecs, TimeUnit.SECONDS);+ echo("Started Solr server on port " + solrPort + ". Happy searching!");+ } catch (TimeoutException timeout) {+ throw new Exception(+ "Solr at " + solrUrl + " did not come online within " + maxWaitSecs + " seconds!");+ }+ } else {+ try {+ CharArr arr = new CharArr();+ new JSONWriter(arr, 2).write(getStatus(solrUrl));+ echo(arr.toString());+ } catch (Exception exc) {+ if (SolrCLI.exceptionIsAuthRelated(exc)) {+ throw exc;+ }+ if (SolrCLI.checkCommunicationError(exc)) {+ // this is not actually an error from the tool as it's ok if Solr is not online.+ CLIO.err("Solr at " + solrUrl + " not online.");+ } else {+ throw new Exception(+ "Failed to get system information from " + solrUrl + " due to: " + exc);+ }+ }+ }+ }++ public Map<String, Object> waitToSeeSolrUp(String solrUrl, long maxWait, TimeUnit unit)+ throws Exception {+ long timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(maxWait, unit);+ while (System.nanoTime() < timeout) {+ try {+ return getStatus(solrUrl);+ } catch (SSLPeerUnverifiedException exc) {+ throw exc;+ } catch (Exception exc) {+ if (SolrCLI.exceptionIsAuthRelated(exc)) {+ throw exc;+ }+ try {+ Thread.sleep(2000L);+ } catch (InterruptedException interrupted) {+ timeout = 0; // stop looping+ }+ }+ }+ throw new TimeoutException(+ "Did not see Solr at "+ + solrUrl+ + " come online within "+ + TimeUnit.SECONDS.convert(maxWait, unit)+ + " seconds!");+ }++ public Map<String, Object> getStatus(String solrUrl) throws Exception {+ Map<String, Object> status;++ if (!solrUrl.endsWith("/")) solrUrl += "/";++ try (var solrClient = SolrCLI.getSolrClient(solrUrl)) {+ NamedList<Object> systemInfo =+ solrClient.request(+ new GenericSolrRequest(SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH));+ // convert raw JSON into user-friendly output+ status = reportStatus(systemInfo, solrClient);+ }++ return status;+ }++ public Map<String, Object> reportStatus(NamedList<Object> info, SolrClient solrClient)+ throws Exception {+ Map<String, Object> status = new LinkedHashMap<>();++ String solrHome = (String) info.get("solr_home");+ status.put("solr_home", solrHome != null ? solrHome : "?");+ status.put("version", info.findRecursive("lucene", "solr-impl-version"));+ status.put("startTime", info.findRecursive("jvm", "jmx", "startTime").toString());+ status.put("uptime", SolrCLI.uptime((Long) info.findRecursive("jvm", "jmx", "upTimeMS")));++ String usedMemory = (String) info.findRecursive("jvm", "memory", "used");+ String totalMemory = (String) info.findRecursive("jvm", "memory", "total");+ status.put("memory", usedMemory + " of " + totalMemory);++ // if this is a Solr in solrcloud mode, gather some basic cluster info+ if ("solrcloud".equals(info.get("mode"))) {+ String zkHost = (String) info.get("zkHost");+ status.put("cloud", getCloudStatus(solrClient, zkHost));+ }++ return status;+ }++ /**+ * Calls the CLUSTERSTATUS endpoint in Solr to get basic status information about the SolrCloud+ * cluster.+ */+ @SuppressWarnings("unchecked")+ protected Map<String, String> getCloudStatus(SolrClient solrClient, String zkHost)+ throws Exception {+ Map<String, String> cloudStatus = new LinkedHashMap<>();+ cloudStatus.put("ZooKeeper", (zkHost != null) ? zkHost : "?");++ NamedList<Object> json = solrClient.request(new CollectionAdminRequest.ClusterStatus());++ List<String> liveNodes = (List<String>) json.findRecursive("cluster", "live_nodes");+ cloudStatus.put("liveNodes", String.valueOf(liveNodes.size()));++ Map<String, Object> collections =+ ((NamedList) json.findRecursive("cluster", "collections")).asMap();+ cloudStatus.put("collections", String.valueOf(collections.size()));++ return cloudStatus;+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/Tool.java b/solr/core/src/java/org/apache/solr/cli/Tool.javanew file mode 100644index 00000000000..120eac2a9a1--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/Tool.java@@ -0,0 +1,31 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.util.List;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;++public interface Tool {+ /** Defines the interface to a Solr tool that can be run from this command-line app. */+ String getName();++ List<Option> getOptions();++ int runTool(CommandLine cli) throws Exception;+}diff --git a/solr/core/src/java/org/apache/solr/cli/ToolBase.java b/solr/core/src/java/org/apache/solr/cli/ToolBase.javanew file mode 100644index 00000000000..43a42f77d7b--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ToolBase.java@@ -0,0 +1,70 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++package org.apache.solr.cli;++import java.io.PrintStream;+import org.apache.commons.cli.CommandLine;++public abstract class ToolBase implements Tool {++ protected PrintStream stdout;+ protected boolean verbose = false;++ protected ToolBase() {+ this(CLIO.getOutStream());+ }++ protected ToolBase(PrintStream stdout) {+ this.stdout = stdout;+ }++ protected void echoIfVerbose(final String msg, CommandLine cli) {+ if (cli.hasOption(SolrCLI.OPTION_VERBOSE.getOpt())) {+ echo(msg);+ }+ }++ protected void echo(final String msg) {+ stdout.println(msg);+ }++ @Override+ public int runTool(CommandLine cli) throws Exception {+ verbose = cli.hasOption(SolrCLI.OPTION_VERBOSE.getOpt());++ int toolExitStatus = 0;+ try {+ runImpl(cli);+ } catch (Exception exc) {+ // since this is a CLI, spare the user the stacktrace+ String excMsg = exc.getMessage();+ if (excMsg != null) {+ CLIO.err("\nERROR: " + excMsg + "\n");+ if (verbose) {+ exc.printStackTrace(CLIO.getErrStream());+ }+ toolExitStatus = 1;+ } else {+ throw exc;+ }+ }+ return toolExitStatus;+ }++ public abstract void runImpl(CommandLine cli) throws Exception;+}diff --git a/solr/core/src/java/org/apache/solr/cli/ZkCpTool.java b/solr/core/src/java/org/apache/solr/cli/ZkCpTool.javanew file mode 100644index 00000000000..01df580fbb7--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ZkCpTool.java@@ -0,0 +1,112 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.List;+import java.util.Locale;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.common.cloud.SolrZkClient;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ZkCpTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public ZkCpTool() {+ this(CLIO.getOutStream());+ }++ public ZkCpTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("src")+ .argName("src")+ .hasArg()+ .required(true)+ .desc("Source file or directory, may be local or a Znode.")+ .build(),+ Option.builder("dst")+ .argName("dst")+ .hasArg()+ .required(true)+ .desc("Destination of copy, may be local or a Znode.")+ .build(),+ SolrCLI.OPTION_RECURSE,+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public String getName() {+ return "cp";+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);+ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("solrUrl")+ + " is running in standalone server mode, cp can only be used when running in SolrCloud mode.\n");+ }++ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(30000, TimeUnit.MILLISECONDS)+ .build()) {+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);+ String src = cli.getOptionValue("src");+ String dst = cli.getOptionValue("dst");+ Boolean recurse = Boolean.parseBoolean(cli.getOptionValue("recurse"));+ echo("Copying from '" + src + "' to '" + dst + "'. ZooKeeper at " + zkHost);++ boolean srcIsZk = src.toLowerCase(Locale.ROOT).startsWith("zk:");+ boolean dstIsZk = dst.toLowerCase(Locale.ROOT).startsWith("zk:");++ String srcName = src;+ if (srcIsZk) {+ srcName = src.substring(3);+ } else if (srcName.toLowerCase(Locale.ROOT).startsWith("file:")) {+ srcName = srcName.substring(5);+ }++ String dstName = dst;+ if (dstIsZk) {+ dstName = dst.substring(3);+ } else {+ if (dstName.toLowerCase(Locale.ROOT).startsWith("file:")) {+ dstName = dstName.substring(5);+ }+ }+ zkClient.zkTransfer(srcName, srcIsZk, dstName, dstIsZk, recurse);+ } catch (Exception e) {+ log.error("Could not complete the zk operation for reason: ", e);+ throw (e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/ZkLsTool.java b/solr/core/src/java/org/apache/solr/cli/ZkLsTool.javanew file mode 100644index 00000000000..81f14e21e77--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ZkLsTool.java@@ -0,0 +1,94 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.List;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.common.cloud.SolrZkClient;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ZkLsTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public ZkLsTool() {+ this(CLIO.getOutStream());+ }++ public ZkLsTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("path")+ .argName("path")+ .hasArg()+ .required(true)+ .desc("Path to list.")+ .build(),+ SolrCLI.OPTION_RECURSE,+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public String getName() {+ return "ls";+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);++ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("zkHost")+ + " is running in standalone server mode, 'zk ls' can only be used when running in SolrCloud mode.\n");+ }++ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(30000, TimeUnit.MILLISECONDS)+ .build()) {+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);++ String znode = cli.getOptionValue("path");+ Boolean recurse = Boolean.parseBoolean(cli.getOptionValue("recurse"));+ echoIfVerbose(+ "Getting listing for ZooKeeper node "+ + znode+ + " from ZooKeeper at "+ + zkHost+ + " recurse: "+ + recurse,+ cli);+ stdout.print(zkClient.listZnode(znode, recurse));+ } catch (Exception e) {+ log.error("Could not complete ls operation for reason: ", e);+ throw (e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/ZkMkrootTool.java b/solr/core/src/java/org/apache/solr/cli/ZkMkrootTool.javanew file mode 100644index 00000000000..d9c4e3e20d1--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ZkMkrootTool.java@@ -0,0 +1,85 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.List;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.common.cloud.SolrZkClient;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ZkMkrootTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public ZkMkrootTool() {+ this(CLIO.getOutStream());+ }++ public ZkMkrootTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("path")+ .argName("path")+ .hasArg()+ .required(true)+ .desc("Path to create.")+ .build(),+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public String getName() {+ return "mkroot";+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);++ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("zkHost")+ + " is running in standalone server mode, 'zk mkroot' can only be used when running in SolrCloud mode.\n");+ }++ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(30000, TimeUnit.MILLISECONDS)+ .build()) {+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);++ String znode = cli.getOptionValue("path");+ echo("Creating ZooKeeper path " + znode + " on ZooKeeper at " + zkHost);+ zkClient.makePath(znode, true);+ } catch (Exception e) {+ log.error("Could not complete mkroot operation for reason: ", e);+ throw (e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/ZkMvTool.java b/solr/core/src/java/org/apache/solr/cli/ZkMvTool.javanew file mode 100644index 00000000000..6fed21223d7--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ZkMvTool.java@@ -0,0 +1,108 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.List;+import java.util.Locale;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrServerException;+import org.apache.solr.common.cloud.SolrZkClient;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ZkMvTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public ZkMvTool() {+ this(CLIO.getOutStream());+ }++ public ZkMvTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("src")+ .argName("src")+ .hasArg()+ .required(true)+ .desc("Source Znode to move from.")+ .build(),+ Option.builder("dst")+ .argName("dst")+ .hasArg()+ .required(true)+ .desc("Destination Znode to move to.")+ .build(),+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public String getName() {+ return "mv";+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);+ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("solrUrl")+ + " is running in standalone server mode, downconfig can only be used when running in SolrCloud mode.\n");+ }++ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(30000, TimeUnit.MILLISECONDS)+ .build()) {+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);+ String src = cli.getOptionValue("src");+ String dst = cli.getOptionValue("dst");++ if (src.toLowerCase(Locale.ROOT).startsWith("file:")+ || dst.toLowerCase(Locale.ROOT).startsWith("file:")) {+ throw new SolrServerException(+ "mv command operates on znodes and 'file:' has been specified.");+ }+ String source = src;+ if (src.toLowerCase(Locale.ROOT).startsWith("zk")) {+ source = src.substring(3);+ }++ String dest = dst;+ if (dst.toLowerCase(Locale.ROOT).startsWith("zk")) {+ dest = dst.substring(3);+ }++ echo("Moving Znode " + source + " to " + dest + " on ZooKeeper at " + zkHost);+ zkClient.moveZnode(source, dest);+ } catch (Exception e) {+ log.error("Could not complete mv operation for reason: ", e);+ throw (e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/ZkRmTool.java b/solr/core/src/java/org/apache/solr/cli/ZkRmTool.javanew file mode 100644index 00000000000..0c03397c05b--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/ZkRmTool.java@@ -0,0 +1,105 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */+package org.apache.solr.cli;++import java.io.PrintStream;+import java.lang.invoke.MethodHandles;+import java.util.List;+import java.util.Locale;+import java.util.concurrent.TimeUnit;+import org.apache.commons.cli.CommandLine;+import org.apache.commons.cli.Option;+import org.apache.solr.client.solrj.SolrServerException;+import org.apache.solr.common.cloud.SolrZkClient;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ZkRmTool extends ToolBase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ public ZkRmTool() {+ this(CLIO.getOutStream());+ }++ public ZkRmTool(PrintStream stdout) {+ super(stdout);+ }++ @Override+ public List<Option> getOptions() {+ return List.of(+ Option.builder("path")+ .argName("path")+ .hasArg()+ .required(true)+ .desc("Path to remove.")+ .build(),+ SolrCLI.OPTION_RECURSE,+ SolrCLI.OPTION_ZKHOST,+ SolrCLI.OPTION_VERBOSE);+ }++ @Override+ public String getName() {+ return "rm";+ }++ @Override+ public void runImpl(CommandLine cli) throws Exception {+ SolrCLI.raiseLogLevelUnlessVerbose(cli);+ String zkHost = SolrCLI.getZkHost(cli);++ if (zkHost == null) {+ throw new IllegalStateException(+ "Solr at "+ + cli.getOptionValue("zkHost")+ + " is running in standalone server mode, 'zk rm' can only be used when running in SolrCloud mode.\n");+ }+ String target = cli.getOptionValue("path");+ boolean recurse = Boolean.parseBoolean(cli.getOptionValue("recurse"));++ String znode = target;+ if (target.toLowerCase(Locale.ROOT).startsWith("zk:")) {+ znode = target.substring(3);+ }+ if (znode.equals("/")) {+ throw new SolrServerException("You may not remove the root ZK node ('/')!");+ }+ echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);+ try (SolrZkClient zkClient =+ new SolrZkClient.Builder()+ .withUrl(zkHost)+ .withTimeout(30000, TimeUnit.MILLISECONDS)+ .build()) {+ if (!recurse && zkClient.getChildren(znode, null, true).size() != 0) {+ throw new SolrServerException(+ "ZooKeeper node " + znode + " has children and recurse has NOT been specified.");+ }+ echo(+ "Removing ZooKeeper node "+ + znode+ + " from ZooKeeper at "+ + zkHost+ + " recurse: "+ + recurse);+ zkClient.clean(znode);+ } catch (Exception e) {+ log.error("Could not complete rm operation for reason: ", e);+ throw (e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/cli/package-info.java b/solr/core/src/java/org/apache/solr/cli/package-info.javanew file mode 100644index 00000000000..99504eda49d--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cli/package-info.java@@ -0,0 +1,19 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++/** Solr Command Line Interface classes */+package org.apache.solr.cli;diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.javaindex bc66611146a..ef2f29cb62a 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java@@ -41,6 +41,7 @@ import org.apache.commons.cli.Option;import org.apache.commons.cli.Options;import org.apache.commons.cli.ParseException;import org.apache.commons.cli.PosixParser;+import org.apache.solr.cli.CLIO;import org.apache.solr.common.SolrException;import org.apache.solr.common.cloud.ClusterProperties;import org.apache.solr.common.cloud.SolrZkClient;@@ -52,7 +53,6 @@ import org.apache.solr.core.ConfigSetService;import org.apache.solr.core.CoreContainer;import org.apache.solr.core.NodeConfig;import org.apache.solr.core.SolrXmlConfig;-import org.apache.solr.util.CLIO;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.xml.sax.SAXException;diff --git a/solr/core/src/java/org/apache/solr/core/BlobRepository.java b/solr/core/src/java/org/apache/solr/core/BlobRepository.javaindex 5aa887f07f9..044b715a891 100644--- a/solr/core/src/java/org/apache/solr/core/BlobRepository.java+++ b/solr/core/src/java/org/apache/solr/core/BlobRepository.java@@ -40,6 +40,7 @@ import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;+import org.apache.solr.cli.SimplePostTool;import org.apache.solr.common.SolrException;import org.apache.solr.common.cloud.ClusterState;import org.apache.solr.common.cloud.DocCollection;@@ -49,7 +50,6 @@ import org.apache.solr.common.cloud.ZkStateReader;import org.apache.solr.common.params.CollectionAdminParams;import org.apache.solr.common.util.StrUtils;import org.apache.solr.common.util.Utils;-import org.apache.solr.util.SimplePostTool;import org.apache.zookeeper.server.ByteBufferInputStream;import org.slf4j.Logger;import org.slf4j.LoggerFactory;diff --git a/solr/core/src/java/org/apache/solr/handler/BlobHandler.java b/solr/core/src/java/org/apache/solr/handler/BlobHandler.javaindex 4f43bb6493f..727eb1252df 100644--- a/solr/core/src/java/org/apache/solr/handler/BlobHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/BlobHandler.java@@ -44,6 +44,7 @@ import org.apache.lucene.search.TopDocs;import org.apache.lucene.search.TopFieldDocs;import org.apache.solr.api.AnnotatedApi;import org.apache.solr.api.Api;+import org.apache.solr.cli.SimplePostTool;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.params.CommonParams;@@ -69,7 +70,6 @@ import org.apache.solr.update.AddUpdateCommand;import org.apache.solr.update.CommitUpdateCommand;import org.apache.solr.update.processor.UpdateRequestProcessor;import org.apache.solr.update.processor.UpdateRequestProcessorChain;-import org.apache.solr.util.SimplePostTool;import org.apache.solr.util.plugin.PluginInfoInitialized;import org.slf4j.Logger;import org.slf4j.LoggerFactory;diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.javaindex b130563099f..e484a8a0722 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.java@@ -40,6 +40,7 @@ import java.util.TreeMap;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.lucene.util.BytesRef;+import org.apache.solr.cli.SimplePostTool.BAOS;import org.apache.solr.cloud.ZkController;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;@@ -62,7 +63,6 @@ import org.apache.solr.response.JSONResponseWriter;import org.apache.solr.response.RawResponseWriter;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.security.AuthorizationContext;-import org.apache.solr.util.SimplePostTool.BAOS;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;diff --git a/solr/core/src/java/org/apache/solr/packagemanager/PackageManager.java b/solr/core/src/java/org/apache/solr/packagemanager/PackageManager.javaindex c11b2691075..5554d0dd022 100644--- a/solr/core/src/java/org/apache/solr/packagemanager/PackageManager.java+++ b/solr/core/src/java/org/apache/solr/packagemanager/PackageManager.java@@ -24,6 +24,7 @@ import com.jayway.jsonpath.PathNotFoundException;import java.io.Closeable;import java.io.IOException;import java.lang.invoke.MethodHandles;+import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;@@ -36,6 +37,7 @@ import java.util.Scanner;import java.util.Set;import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;+import org.apache.solr.cli.SolrCLI;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.client.solrj.SolrServerException;@@ -59,7 +61,6 @@ import org.apache.solr.packagemanager.SolrPackage.Command;import org.apache.solr.packagemanager.SolrPackage.Manifest;import org.apache.solr.packagemanager.SolrPackage.Plugin;import org.apache.solr.pkg.SolrPackageLoader;-import org.apache.solr.util.SolrCLI;import org.apache.solr.util.SolrVersion;import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;@@ -180,7 +181,7 @@ public class PackageManager implements Closeable {List<SolrPackageInstance> ret = new ArrayList<>();packages = new HashMap<>();try {- if (zkClient.exists(ZkStateReader.SOLR_PKGS_PATH, true) == true) {+ if (zkClient.exists(ZkStateReader.SOLR_PKGS_PATH, true)) {@SuppressWarnings("unchecked")Map<String, List<Map<?, ?>>> packagesZnodeMap =(Map<String, List<Map<?, ?>>>)@@ -188,7 +189,7 @@ public class PackageManager implements Closeable {.readValue(new String(zkClient.getData(ZkStateReader.SOLR_PKGS_PATH, null, null, true),- "UTF-8"),+ StandardCharsets.UTF_8),Map.class).get("packages");for (String packageName : packagesZnodeMap.keySet()) {@@ -248,7 +249,7 @@ public class PackageManager implements Closeable {if (packages == null) return Collections.emptyMap();Map<String, SolrPackageInstance> ret = new HashMap<>();for (String packageName : packages.keySet()) {- if (StrUtils.isNullOrEmpty(packageName) == false+ if (!StrUtils.isNullOrEmpty(packageName)&& // There can be an empty key, storing the version herepackages.get(packageName)!= null) { // null means the package was undeployed from this package before@@ -323,9 +324,9 @@ public class PackageManager implements Closeable {List<String> existingCollections = zkClient.getChildren("/collections", null, true);Set<String> nonExistent = new HashSet<>(collections);nonExistent.removeAll(existingCollections);- if (nonExistent.isEmpty() == false) {+ if (!nonExistent.isEmpty()) {throw new SolrException(- ErrorCode.BAD_REQUEST, "Collection(s) doesn't exist: " + nonExistent.toString());+ ErrorCode.BAD_REQUEST, "Collection(s) doesn't exist: " + nonExistent);}} catch (KeeperException | InterruptedException e) {throw new SolrException(@@ -355,10 +356,8 @@ public class PackageManager implements Closeable {List<String> deployedCollections = deployResult.first();List<String> previouslyDeployedOnCollections = deployResult.second();- // Verify- boolean verifySuccess = true;// Verify that package was successfully deployed- verifySuccess =+ boolean verifySuccess =verify(packageInstance, deployedCollections, shouldDeployClusterPlugins, overrides);if (verifySuccess) {PackageUtils.printGreen(@@ -415,7 +414,6 @@ public class PackageManager implements Closeable {// Get package paramstry {- @SuppressWarnings("unchecked")boolean packageParamsExist =solrClient.request(@@ -468,8 +466,7 @@ public class PackageManager implements Closeable {// If it is a fresh deploy on a collection, run setup commands all the plugins in the packageif (!isUpdate) {for (Plugin plugin : packageInstance.plugins) {- if ("collection".equalsIgnoreCase(plugin.type) == false || collections.isEmpty())- continue;+ if (!"collection".equalsIgnoreCase(plugin.type) || collections.isEmpty()) continue;Map<String, String> systemParams =Map.of("collection",@@ -531,7 +528,7 @@ public class PackageManager implements Closeable {}}- if (previouslyDeployed.isEmpty() == false) {+ if (!previouslyDeployed.isEmpty()) {PackageUtils.printRed("Already Deployed on "+ previouslyDeployed@@ -545,7 +542,7 @@ public class PackageManager implements Closeable {collections.stream().filter(c -> !previouslyDeployed.contains(c)).collect(Collectors.toList());- return new Pair<List<String>, List<String>>(deployedCollections, previouslyDeployed);+ return new Pair<>(deployedCollections, previouslyDeployed);}@SuppressWarnings("unchecked")@@ -556,7 +553,7 @@ public class PackageManager implements Closeable {if (isUpdate) {for (Plugin plugin : packageInstance.plugins) {- if ("cluster".equalsIgnoreCase(plugin.type) == false) continue;+ if (!"cluster".equalsIgnoreCase(plugin.type)) continue;SolrPackageInstance deployedPackage =getPackagesDeployedAsClusterLevelPlugins().get(packageInstance.name);if (deployedPackage == null) {@@ -592,7 +589,7 @@ public class PackageManager implements Closeable {}} else {for (Plugin plugin : packageInstance.plugins) {- if ("cluster".equalsIgnoreCase(plugin.type) == false) continue;+ if (!"cluster".equalsIgnoreCase(plugin.type)) continue;// Check if this cluster level plugin is already deployed{Map<String, Object> clusterprops = null;@@ -680,7 +677,7 @@ public class PackageManager implements Closeable {PackageUtils.print(PackageUtils.YELLOW,"Execute this command. (If you choose no, you can manually deploy/undeploy this plugin later) (y/n): ");- try (Scanner scanner = new Scanner(System.in, "UTF-8")) {+ try (Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8)) {String userInput = scanner.next();if ("no".trim().equalsIgnoreCase(userInput) || "n".trim().equalsIgnoreCase(userInput)) {shouldExecute = false;@@ -708,9 +705,7 @@ public class PackageManager implements Closeable {String[] overrides,String collection) {Map<String, String> collectionParameterOverrides =- isUpdate- ? getPackageParams(packageInstance.name, collection)- : new HashMap<String, String>();+ isUpdate ? getPackageParams(packageInstance.name, collection) : new HashMap<>();if (overrides != null) {for (String override : overrides) {collectionParameterOverrides.put(override.split("=")[0], override.split("=")[1]);@@ -719,7 +714,7 @@ public class PackageManager implements Closeable {return collectionParameterOverrides;}- @SuppressWarnings({"rawtypes", "unchecked"})+ @SuppressWarnings({"unchecked"})Map<String, String> getPackageParams(String packageName, String collection) {try {NamedList<Object> response =@@ -747,7 +742,7 @@ public class PackageManager implements Closeable {SolrPackageInstance pkg,List<String> collections,boolean shouldDeployClusterPlugins,- String overrides[]) {+ String[] overrides) {boolean success = true;for (Plugin plugin : pkg.plugins) {Command cmd = plugin.verifyCommand;@@ -871,8 +866,7 @@ public class PackageManager implements Closeable {SolrPackageInstance latest = null;if (versions != null && !versions.isEmpty()) {latest = versions.get(0);- for (int i = 0; i < versions.size(); i++) {- SolrPackageInstance pkg = versions.get(i);+ for (SolrPackageInstance pkg : versions) {if (pkg.version.equals(version)) {return pkg;}@@ -970,8 +964,7 @@ public class PackageManager implements Closeable {"Cluster level plugins from package " + packageName + " not deployed.");} else {for (Plugin plugin : deployedPackage.plugins) {- if (!shouldUndeployClusterPlugins || "cluster".equalsIgnoreCase(plugin.type) == false)- continue;+ if (!shouldUndeployClusterPlugins || !"cluster".equalsIgnoreCase(plugin.type)) continue;Map<String, String> systemParams =Map.of(@@ -1025,7 +1018,9 @@ public class PackageManager implements Closeable {// Run the uninstall command for all pluginsfor (Plugin plugin : deployedPackage.plugins) {- if ("collection".equalsIgnoreCase(plugin.type) == false) continue;+ if (!"collection".equalsIgnoreCase(plugin.type)) {+ continue;+ }Map<String, String> systemParams =Map.of(@@ -1096,7 +1091,7 @@ public class PackageManager implements Closeable {} catch (KeeperException | InterruptedException e) {throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, e);}- Map<String, String> deployed = new HashMap<String, String>();+ Map<String, String> deployed = new HashMap<>();for (String collection : allCollections) {// Check package version installedString paramsJson =diff --git a/solr/core/src/java/org/apache/solr/util/SolrCLI.java b/solr/core/src/java/org/apache/solr/util/SolrCLI.javadeleted file mode 100755index 090a02f50ac..00000000000--- a/solr/core/src/java/org/apache/solr/util/SolrCLI.java+++ /dev/null@@ -1,4422 +0,0 @@-/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF 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.- */-package org.apache.solr.util;--import static org.apache.solr.common.SolrException.ErrorCode.FORBIDDEN;-import static org.apache.solr.common.SolrException.ErrorCode.UNAUTHORIZED;-import static org.apache.solr.common.params.CommonParams.DISTRIB;-import static org.apache.solr.common.params.CommonParams.NAME;-import static org.apache.solr.common.params.CommonParams.SYSTEM_INFO_PATH;--import com.google.common.annotations.VisibleForTesting;-import java.io.Console;-import java.io.File;-import java.io.FileNotFoundException;-import java.io.IOException;-import java.io.InputStream;-import java.io.PrintStream;-import java.lang.invoke.MethodHandles;-import java.net.ConnectException;-import java.net.Socket;-import java.net.SocketException;-import java.net.URI;-import java.net.URL;-import java.nio.charset.StandardCharsets;-import java.nio.file.Files;-import java.nio.file.Path;-import java.nio.file.Paths;-import java.nio.file.attribute.FileOwnerAttributeView;-import java.util.ArrayList;-import java.util.Arrays;-import java.util.Base64;-import java.util.Collection;-import java.util.Collections;-import java.util.Enumeration;-import java.util.HashMap;-import java.util.LinkedHashMap;-import java.util.List;-import java.util.Locale;-import java.util.Map;-import java.util.Optional;-import java.util.Scanner;-import java.util.Set;-import java.util.TreeSet;-import java.util.concurrent.TimeUnit;-import java.util.concurrent.TimeoutException;-import java.util.stream.Collectors;-import java.util.stream.Stream;-import java.util.zip.ZipEntry;-import java.util.zip.ZipInputStream;-import javax.net.ssl.SSLPeerUnverifiedException;-import org.apache.commons.cli.CommandLine;-import org.apache.commons.cli.GnuParser;-import org.apache.commons.cli.HelpFormatter;-import org.apache.commons.cli.Option;-import org.apache.commons.cli.Options;-import org.apache.commons.cli.ParseException;-import org.apache.commons.exec.DefaultExecuteResultHandler;-import org.apache.commons.exec.DefaultExecutor;-import org.apache.commons.exec.ExecuteException;-import org.apache.commons.exec.Executor;-import org.apache.commons.exec.OS;-import org.apache.commons.exec.environment.EnvironmentUtils;-import org.apache.commons.io.FileUtils;-import org.apache.commons.io.file.PathUtils;-import org.apache.lucene.util.Constants;-import org.apache.solr.client.solrj.SolrClient;-import org.apache.solr.client.solrj.SolrQuery;-import org.apache.solr.client.solrj.SolrRequest;-import org.apache.solr.client.solrj.SolrServerException;-import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;-import org.apache.solr.client.solrj.impl.CloudSolrClient;-import org.apache.solr.client.solrj.impl.Http2SolrClient;-import org.apache.solr.client.solrj.request.CollectionAdminRequest;-import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;-import org.apache.solr.client.solrj.request.CoreAdminRequest;-import org.apache.solr.client.solrj.request.GenericSolrRequest;-import org.apache.solr.client.solrj.request.HealthCheckRequest;-import org.apache.solr.client.solrj.response.CollectionAdminResponse;-import org.apache.solr.client.solrj.response.CoreAdminResponse;-import org.apache.solr.client.solrj.response.QueryResponse;-import org.apache.solr.common.SolrException;-import org.apache.solr.common.cloud.ClusterState;-import org.apache.solr.common.cloud.DocCollection;-import org.apache.solr.common.cloud.Replica;-import org.apache.solr.common.cloud.Slice;-import org.apache.solr.common.cloud.SolrZkClient;-import org.apache.solr.common.cloud.ZkCoreNodeProps;-import org.apache.solr.common.cloud.ZkMaintenanceUtils;-import org.apache.solr.common.cloud.ZkStateReader;-import org.apache.solr.common.params.CollectionAdminParams;-import org.apache.solr.common.params.CommonParams;-import org.apache.solr.common.params.ModifiableSolrParams;-import org.apache.solr.common.util.ContentStreamBase;-import org.apache.solr.common.util.NamedList;-import org.apache.solr.common.util.StrUtils;-import org.apache.solr.core.ConfigSetService;-import org.apache.solr.security.Sha256AuthenticationProvider;-import org.apache.solr.util.configuration.SSLConfigurationsFactory;-import org.noggit.CharArr;-import org.noggit.JSONWriter;-import org.slf4j.Logger;-import org.slf4j.LoggerFactory;--/** Command-line utility for working with Solr. */-public class SolrCLI implements CLIO {- private static final long MAX_WAIT_FOR_CORE_LOAD_NANOS =- TimeUnit.NANOSECONDS.convert(1, TimeUnit.MINUTES);-- /** Defines the interface to a Solr tool that can be run from this command-line app. */- public interface Tool {- String getName();-- List<Option> getOptions();-- int runTool(CommandLine cli) throws Exception;- }-- public abstract static class ToolBase implements Tool {- protected PrintStream stdout;- protected boolean verbose = false;-- protected ToolBase() {- this(CLIO.getOutStream());- }-- protected ToolBase(PrintStream stdout) {- this.stdout = stdout;- }-- protected void echoIfVerbose(final String msg, CommandLine cli) {- if (cli.hasOption(OPTION_VERBOSE.getOpt())) {- echo(msg);- }- }-- protected void echo(final String msg) {- stdout.println(msg);- }-- @Override- public int runTool(CommandLine cli) throws Exception {- verbose = cli.hasOption(OPTION_VERBOSE.getOpt());-- int toolExitStatus = 0;- try {- runImpl(cli);- } catch (Exception exc) {- // since this is a CLI, spare the user the stacktrace- String excMsg = exc.getMessage();- if (excMsg != null) {- CLIO.err("\nERROR: " + excMsg + "\n");- if (verbose) {- exc.printStackTrace(CLIO.getErrStream());- }- toolExitStatus = 1;- } else {- throw exc;- }- }- return toolExitStatus;- }-- protected abstract void runImpl(CommandLine cli) throws Exception;- }- /**- * Helps build SolrCloud aware tools by initializing a CloudSolrClient instance before running the- * tool.- */- public abstract static class SolrCloudTool extends ToolBase {-- protected SolrCloudTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return cloudOptions;- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = cli.getOptionValue(OPTION_ZKHOST.getOpt(), ZK_HOST);-- log.debug("Connecting to Solr cluster: {}", zkHost);- try (var cloudSolrClient =- new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())- .build()) {-- cloudSolrClient.connect();- runCloudTool(cloudSolrClient, cli);- }- }-- /** Runs a SolrCloud tool with CloudSolrClient initialized */- protected abstract void runCloudTool(CloudSolrClient cloudSolrClient, CommandLine cli)- throws Exception;- }-- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- public static final String DEFAULT_SOLR_URL = "http://localhost:8983/solr";- public static final String ZK_HOST = "localhost:9983";-- public static final Option OPTION_ZKHOST =- Option.builder("z")- .argName("HOST")- .hasArg()- .required(false)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST)- .longOpt("zkHost")- .build();- public static final Option OPTION_SOLRURL =- Option.builder("solrUrl")- .argName("HOST")- .hasArg()- .required(false)- .desc(- "Base Solr URL, which can be used to determine the zkHost if that's not known; defaults to: "- + DEFAULT_SOLR_URL)- .build();- public static final Option OPTION_VERBOSE =- Option.builder("verbose").required(false).desc("Enable more verbose command output.").build();-- public static final Option OPTION_RECURSE =- Option.builder("recurse")- .argName("recurse")- .hasArg()- .required(false)- .desc("Recurse (true|false), default is false.")- // .type(Boolean.class)- .build();-- public static final List<Option> cloudOptions =- List.of(- OPTION_ZKHOST,- Option.builder("c")- .argName("COLLECTION")- .hasArg()- .required(false)- .desc("Name of collection; no default.")- .longOpt("collection")- .build(),- OPTION_VERBOSE);-- private static void exit(int exitStatus) {- try {- System.exit(exitStatus);- } catch (java.lang.SecurityException secExc) {- if (exitStatus != 0)- throw new RuntimeException("SolrCLI failed to exit with status " + exitStatus);- }- }-- /** Runs a tool. */- public static void main(String[] args) throws Exception {- if (args == null || args.length == 0 || args[0] == null || args[0].trim().length() == 0) {- CLIO.err(- "Invalid command-line args! Must pass the name of a tool to run.\n"- + "Supported tools:\n");- displayToolOptions();- exit(1);- }-- if (args.length == 1 && Arrays.asList("-v", "-version", "version").contains(args[0])) {- // Simple version tool, no need for its own class- CLIO.out(SolrVersion.LATEST.toString());- exit(0);- }-- SSLConfigurationsFactory.current().init();-- Tool tool = null;- try {- tool = findTool(args);- } catch (IllegalArgumentException iae) {- CLIO.err(iae.getMessage());- System.exit(1);- }- CommandLine cli = parseCmdLine(tool.getName(), args, tool.getOptions());- System.exit(tool.runTool(cli));- }-- public static Tool findTool(String[] args) throws Exception {- String toolType = args[0].trim().toLowerCase(Locale.ROOT);- return newTool(toolType);- }-- /**- * @deprecated Use the method that takes a tool name as the first argument instead.- */- @Deprecated- public static CommandLine parseCmdLine(String[] args, List<Option> toolOptions) throws Exception {- return parseCmdLine(SolrCLI.class.getName(), args, toolOptions);- }-- public static CommandLine parseCmdLine(String toolName, String[] args, List<Option> toolOptions)- throws Exception {- // the parser doesn't like -D props- List<String> toolArgList = new ArrayList<>();- List<String> dashDList = new ArrayList<>();- for (int a = 1; a < args.length; a++) {- String arg = args[a];- if (arg.startsWith("-D")) {- dashDList.add(arg);- } else {- toolArgList.add(arg);- }- }- String[] toolArgs = toolArgList.toArray(new String[0]);-- // process command-line args to configure this application- CommandLine cli =- processCommandLineArgs(toolName, joinCommonAndToolOptions(toolOptions), toolArgs);-- List<String> argList = cli.getArgList();- argList.addAll(dashDList);-- // for SSL support, try to accommodate relative paths set for SSL store props- String solrInstallDir = System.getProperty("solr.install.dir");- if (solrInstallDir != null) {- checkSslStoreSysProp(solrInstallDir, "keyStore");- checkSslStoreSysProp(solrInstallDir, "trustStore");- }-- return cli;- }-- protected static void checkSslStoreSysProp(String solrInstallDir, String key) {- String sysProp = "javax.net.ssl." + key;- String keyStore = System.getProperty(sysProp);- if (keyStore == null) return;-- File keyStoreFile = new File(keyStore);- if (keyStoreFile.isFile()) return; // configured setting is OK-- keyStoreFile = new File(solrInstallDir, "server/" + keyStore);- if (keyStoreFile.isFile()) {- System.setProperty(sysProp, keyStoreFile.getAbsolutePath());- } else {- CLIO.err(- "WARNING: "- + sysProp- + " file "- + keyStore- + " not found! https requests to Solr will likely fail; please update your "- + sysProp- + " setting to use an absolute path.");- }- }-- private static void raiseLogLevelUnlessVerbose(CommandLine cli) {- if (!cli.hasOption(OPTION_VERBOSE.getOpt())) {- StartupLoggingUtils.changeLogLevel("WARN");- }- }-- /** Support options common to all tools. */- public static List<Option> getCommonToolOptions() {- return List.of();- }-- // Creates an instance of the requested tool, using classpath scanning if necessary- private static Tool newTool(String toolType) throws Exception {- if ("healthcheck".equals(toolType)) return new HealthcheckTool();- else if ("status".equals(toolType)) return new StatusTool();- else if ("api".equals(toolType)) return new ApiTool();- else if ("create_collection".equals(toolType)) return new CreateCollectionTool();- else if ("create_core".equals(toolType)) return new CreateCoreTool();- else if ("create".equals(toolType)) return new CreateTool();- else if ("delete".equals(toolType)) return new DeleteTool();- else if ("config".equals(toolType)) return new ConfigTool();- else if ("run_example".equals(toolType)) return new RunExampleTool();- else if ("upconfig".equals(toolType)) return new ConfigSetUploadTool();- else if ("downconfig".equals(toolType)) return new ConfigSetDownloadTool();- else if ("rm".equals(toolType)) return new ZkRmTool();- else if ("mv".equals(toolType)) return new ZkMvTool();- else if ("cp".equals(toolType)) return new ZkCpTool();- else if ("ls".equals(toolType)) return new ZkLsTool();- else if ("mkroot".equals(toolType)) return new ZkMkrootTool();- else if ("assert".equals(toolType)) return new AssertTool();- else if ("auth".equals(toolType)) return new AuthTool();- else if ("export".equals(toolType)) return new ExportTool();- else if ("package".equals(toolType)) return new PackageTool();-- // If you add a built-in tool to this class, add it here to avoid- // classpath scanning-- for (Class<? extends Tool> next : findToolClassesInPackage("org.apache.solr.util")) {- Tool tool = next.getConstructor().newInstance();- if (toolType.equals(tool.getName())) return tool;- }-- throw new IllegalArgumentException(toolType + " is not a valid command!");- }-- private static void displayToolOptions() throws Exception {- HelpFormatter formatter = new HelpFormatter();- formatter.printHelp("healthcheck", getToolOptions(new HealthcheckTool()));- formatter.printHelp("status", getToolOptions(new StatusTool()));- formatter.printHelp("api", getToolOptions(new ApiTool()));- formatter.printHelp("create_collection", getToolOptions(new CreateCollectionTool()));- formatter.printHelp("create_core", getToolOptions(new CreateCoreTool()));- formatter.printHelp("create", getToolOptions(new CreateTool()));- formatter.printHelp("delete", getToolOptions(new DeleteTool()));- formatter.printHelp("config", getToolOptions(new ConfigTool()));- formatter.printHelp("run_example", getToolOptions(new RunExampleTool()));- formatter.printHelp("upconfig", getToolOptions(new ConfigSetUploadTool()));- formatter.printHelp("downconfig", getToolOptions(new ConfigSetDownloadTool()));- formatter.printHelp("rm", getToolOptions(new ZkRmTool()));- formatter.printHelp("cp", getToolOptions(new ZkCpTool()));- formatter.printHelp("mv", getToolOptions(new ZkMvTool()));- formatter.printHelp("ls", getToolOptions(new ZkLsTool()));- formatter.printHelp("export", getToolOptions(new ExportTool()));- formatter.printHelp("package", getToolOptions(new PackageTool()));-- List<Class<? extends Tool>> toolClasses = findToolClassesInPackage("org.apache.solr.util");- for (Class<? extends Tool> next : toolClasses) {- Tool tool = next.getConstructor().newInstance();- formatter.printHelp(tool.getName(), getToolOptions(tool));- }- }-- private static Options getToolOptions(Tool tool) {- Options options = new Options();- options.addOption("help", false, "Print this message");- options.addOption(OPTION_VERBOSE);- List<Option> toolOpts = joinCommonAndToolOptions(tool.getOptions());- for (Option toolOpt : toolOpts) {- options.addOption(toolOpt);- }- return options;- }-- public static List<Option> joinCommonAndToolOptions(List<Option> toolOpts) {- return joinOptions(getCommonToolOptions(), toolOpts);- }-- public static List<Option> joinOptions(List<Option> lhs, List<Option> rhs) {- if (lhs == null) {- return rhs == null ? List.of() : rhs;- }-- if (rhs == null) {- return lhs;- }-- return Stream.concat(lhs.stream(), rhs.stream()).collect(Collectors.toUnmodifiableList());- }-- /**- * @deprecated Use the method that takes a tool name as the first argument instead.- */- @Deprecated- public static CommandLine processCommandLineArgs(List<Option> customOptions, String[] args) {- return processCommandLineArgs(SolrCLI.class.getName(), customOptions, args);- }-- /** Parses the command-line arguments passed by the user. */- public static CommandLine processCommandLineArgs(- String toolName, List<Option> customOptions, String[] args) {- Options options = new Options();-- options.addOption("help", false, "Print this message");- options.addOption(OPTION_VERBOSE);-- if (customOptions != null) {- for (Option customOption : customOptions) {- options.addOption(customOption);- }- }-- CommandLine cli = null;- try {- cli = (new GnuParser()).parse(options, args);- } catch (ParseException exp) {- boolean hasHelpArg = false;- if (args != null) {- for (String arg : args) {- if ("--help".equals(arg) || "-help".equals(arg)) {- hasHelpArg = true;- break;- }- }- }- if (!hasHelpArg) {- CLIO.err("Failed to parse command-line arguments due to: " + exp.getMessage());- }- HelpFormatter formatter = new HelpFormatter();- formatter.printHelp(toolName, options);- exit(1);- }-- if (cli.hasOption("help")) {- HelpFormatter formatter = new HelpFormatter();- formatter.printHelp(toolName, options);- exit(0);- }-- return cli;- }-- /** Scans Jar files on the classpath for Tool implementations to activate. */- private static List<Class<? extends Tool>> findToolClassesInPackage(String packageName) {- List<Class<? extends Tool>> toolClasses = new ArrayList<>();- try {- ClassLoader classLoader = SolrCLI.class.getClassLoader();- String path = packageName.replace('.', '/');- Enumeration<URL> resources = classLoader.getResources(path);- Set<String> classes = new TreeSet<>();- while (resources.hasMoreElements()) {- URL resource = resources.nextElement();- classes.addAll(findClasses(resource.getFile(), packageName));- }-- for (String classInPackage : classes) {- Class<?> theClass = Class.forName(classInPackage);- if (Tool.class.isAssignableFrom(theClass)) toolClasses.add(theClass.asSubclass(Tool.class));- }- } catch (Exception e) {- // safe to squelch this as it's just looking for tools to run- log.debug("Failed to find Tool impl classes in {}, due to: ", packageName, e);- }- return toolClasses;- }-- private static Set<String> findClasses(String path, String packageName) throws Exception {- Set<String> classes = new TreeSet<>();- if (path.startsWith("file:") && path.contains("!")) {- String[] split = path.split("!");- URL jar = new URL(split[0]);- try (ZipInputStream zip = new ZipInputStream(jar.openStream())) {- ZipEntry entry;- while ((entry = zip.getNextEntry()) != null) {- if (entry.getName().endsWith(".class")) {- String className =- entry- .getName()- .replaceAll("[$].*", "")- .replaceAll("[.]class", "")- .replace('/', '.');- if (className.startsWith(packageName)) classes.add(className);- }- }- }- }- return classes;- }-- /**- * Determine if a request to Solr failed due to a communication error, which is generally- * retry-able.- */- public static boolean checkCommunicationError(Exception exc) {- Throwable rootCause = SolrException.getRootCause(exc);- boolean wasCommError =- (rootCause instanceof ConnectException- || rootCause instanceof SolrServerException- || rootCause instanceof SocketException);- return wasCommError;- }-- private static void checkCodeForAuthError(int code) {- if (code == UNAUTHORIZED.code || code == FORBIDDEN.code) {- throw new SolrException(- SolrException.ErrorCode.getErrorCode(code),- "Solr requires authentication for request. Please supply valid credentials. HTTP code="- + code);- }- }-- private static boolean exceptionIsAuthRelated(Exception exc) {- return (exc instanceof SolrException- && Arrays.asList(UNAUTHORIZED.code, FORBIDDEN.code).contains(((SolrException) exc).code()));- }-- public static SolrClient getSolrClient(String solrUrl) {- return new Http2SolrClient.Builder(solrUrl).maxConnectionsPerHost(32).build();- }-- /**- * Get Solr base url with port if present and root from URI- *- * @param uri Full Solr URI (e.g. http://localhost:8983/solr/admin/info/system)- * @return Solr base url with port and root (from above example http://localhost:8983/solr)- */- public static String getSolrUrlFromUri(URI uri) {- return uri.getScheme() + "://" + uri.getAuthority() + "/" + uri.getPath().split("/")[1];- }-- public static ModifiableSolrParams getSolrParamsFromUri(URI uri) {- ModifiableSolrParams paramsMap = new ModifiableSolrParams();- String[] params = uri.getQuery() == null ? new String[] {} : uri.getQuery().split("&");- for (String param : params) {- String[] paramSplit = param.split("=");- paramsMap.add(paramSplit[0], paramSplit[1]);- }- return paramsMap;- }-- public static final String JSON_CONTENT_TYPE = "application/json";-- public static NamedList<Object> postJsonToSolr(- SolrClient solrClient, String updatePath, String jsonBody) throws Exception {- ContentStreamBase.StringStream contentStream = new ContentStreamBase.StringStream(jsonBody);- contentStream.setContentType(JSON_CONTENT_TYPE);- ContentStreamUpdateRequest req = new ContentStreamUpdateRequest(updatePath);- req.addContentStream(contentStream);- return solrClient.request(req);- }-- /** Get the status of a Solr server. */- public static class StatusTool extends ToolBase {-- public StatusTool() {- this(CLIO.getOutStream());- }-- public StatusTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "status";- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("solr")- .argName("URL")- .hasArg()- .required(false)- .desc("Address of the Solr Web application, defaults to: " + DEFAULT_SOLR_URL + '.')- .build(),- Option.builder("maxWaitSecs")- .argName("SECS")- .hasArg()- .required(false)- .desc("Wait up to the specified number of seconds to see Solr running.")- .build());- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- int maxWaitSecs = Integer.parseInt(cli.getOptionValue("maxWaitSecs", "0"));- String solrUrl = cli.getOptionValue("solr", DEFAULT_SOLR_URL);- if (maxWaitSecs > 0) {- int solrPort = (new URL(solrUrl)).getPort();- echo("Waiting up to " + maxWaitSecs + " seconds to see Solr running on port " + solrPort);- try {- waitToSeeSolrUp(solrUrl, maxWaitSecs);- echo("Started Solr server on port " + solrPort + ". Happy searching!");- } catch (TimeoutException timeout) {- throw new Exception(- "Solr at " + solrUrl + " did not come online within " + maxWaitSecs + " seconds!");- }- } else {- try {- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(getStatus(solrUrl));- echo(arr.toString());- } catch (Exception exc) {- if (exceptionIsAuthRelated(exc)) {- throw exc;- }- if (checkCommunicationError(exc)) {- // this is not actually an error from the tool as it's ok if Solr is not online.- CLIO.err("Solr at " + solrUrl + " not online.");- } else {- throw new Exception(- "Failed to get system information from " + solrUrl + " due to: " + exc);- }- }- }- }-- public Map<String, Object> waitToSeeSolrUp(String solrUrl, int maxWaitSecs) throws Exception {- long timeout =- System.nanoTime() + TimeUnit.NANOSECONDS.convert(maxWaitSecs, TimeUnit.SECONDS);- while (System.nanoTime() < timeout) {- try {- return getStatus(solrUrl);- } catch (SSLPeerUnverifiedException exc) {- throw exc;- } catch (Exception exc) {- if (exceptionIsAuthRelated(exc)) {- throw exc;- }- try {- Thread.sleep(2000L);- } catch (InterruptedException interrupted) {- timeout = 0; // stop looping- }- }- }- throw new TimeoutException(- "Did not see Solr at " + solrUrl + " come online within " + maxWaitSecs + " seconds!");- }-- public Map<String, Object> getStatus(String solrUrl) throws Exception {- Map<String, Object> status = null;-- if (!solrUrl.endsWith("/")) solrUrl += "/";-- try (var solrClient = getSolrClient(solrUrl)) {- NamedList<Object> systemInfo =- solrClient.request(- new GenericSolrRequest(SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH));- // convert raw JSON into user-friendly output- status = reportStatus(systemInfo, solrClient);- }-- return status;- }-- public Map<String, Object> reportStatus(NamedList<Object> info, SolrClient solrClient)- throws Exception {- Map<String, Object> status = new LinkedHashMap<>();-- String solrHome = (String) info.get("solr_home");- status.put("solr_home", solrHome != null ? solrHome : "?");- status.put("version", (String) info.findRecursive("lucene", "solr-impl-version"));- status.put("startTime", info.findRecursive("jvm", "jmx", "startTime").toString());- status.put("uptime", uptime((Long) info.findRecursive("jvm", "jmx", "upTimeMS")));-- String usedMemory = (String) info.findRecursive("jvm", "memory", "used");- String totalMemory = (String) info.findRecursive("jvm", "memory", "total");- status.put("memory", usedMemory + " of " + totalMemory);-- // if this is a Solr in solrcloud mode, gather some basic cluster info- if ("solrcloud".equals(info.get("mode"))) {- String zkHost = (String) info.get("zkHost");- status.put("cloud", getCloudStatus(solrClient, zkHost));- }-- return status;- }-- /**- * Calls the CLUSTERSTATUS endpoint in Solr to get basic status information about the SolrCloud- * cluster.- */- @SuppressWarnings("unchecked")- protected Map<String, String> getCloudStatus(SolrClient solrClient, String zkHost)- throws Exception {- Map<String, String> cloudStatus = new LinkedHashMap<>();- cloudStatus.put("ZooKeeper", (zkHost != null) ? zkHost : "?");-- NamedList<Object> json = solrClient.request(new CollectionAdminRequest.ClusterStatus());-- List<String> liveNodes = (List<String>) json.findRecursive("cluster", "live_nodes");- cloudStatus.put("liveNodes", String.valueOf(liveNodes.size()));-- Map<String, Object> collections =- ((NamedList) json.findRecursive("cluster", "collections")).asMap();- cloudStatus.put("collections", String.valueOf(collections.size()));-- return cloudStatus;- }- } // end StatusTool class-- /** Used to send an arbitrary HTTP request to a Solr API endpoint. */- public static class ApiTool extends ToolBase {-- public ApiTool() {- this(CLIO.getOutStream());- }-- public ApiTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "api";- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("get")- .argName("URL")- .hasArg()- .required(true)- .desc("Send a GET request to a Solr API endpoint.")- .build());- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- String getUrl = cli.getOptionValue("get");- if (getUrl != null) {- getUrl = getUrl.replace("+", "%20");- URI uri = new URI(getUrl);- String solrUrl = getSolrUrlFromUri(uri);- String path = uri.getPath();- try (var solrClient = getSolrClient(solrUrl)) {- NamedList<Object> response =- solrClient.request(- // For path parameter we need the path without the root so from the second / char- // (because root can be configured)- // E.g URL is http://localhost:8983/solr/admin/info/system path is- // /solr/admin/info/system and the path without root is /admin/info/system- new GenericSolrRequest(- SolrRequest.METHOD.GET,- path.substring(path.indexOf("/", path.indexOf("/") + 1)),- getSolrParamsFromUri(uri)));-- // pretty-print the response to stdout- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(response.asMap());- echo(arr.toString());- }- }- }- } // end ApiTool class-- private static final String DEFAULT_CONFIG_SET = "_default";-- private static final long MS_IN_MIN = 60 * 1000L;- private static final long MS_IN_HOUR = MS_IN_MIN * 60L;- private static final long MS_IN_DAY = MS_IN_HOUR * 24L;-- @VisibleForTesting- static final String uptime(long uptimeMs) {- if (uptimeMs <= 0L) return "?";-- long numDays = (uptimeMs >= MS_IN_DAY) ? (uptimeMs / MS_IN_DAY) : 0L;- long rem = uptimeMs - (numDays * MS_IN_DAY);- long numHours = (rem >= MS_IN_HOUR) ? (rem / MS_IN_HOUR) : 0L;- rem = rem - (numHours * MS_IN_HOUR);- long numMinutes = (rem >= MS_IN_MIN) ? (rem / MS_IN_MIN) : 0L;- rem = rem - (numMinutes * MS_IN_MIN);- long numSeconds = Math.round(rem / 1000.0);- return String.format(- Locale.ROOT,- "%d days, %d hours, %d minutes, %d seconds",- numDays,- numHours,- numMinutes,- numSeconds);- }-- static class ReplicaHealth implements Comparable<ReplicaHealth> {- String shard;- String name;- String url;- String status;- long numDocs;- boolean isLeader;- String uptime;- String memory;-- ReplicaHealth(- String shard,- String name,- String url,- String status,- long numDocs,- boolean isLeader,- String uptime,- String memory) {- this.shard = shard;- this.name = name;- this.url = url;- this.numDocs = numDocs;- this.status = status;- this.isLeader = isLeader;- this.uptime = uptime;- this.memory = memory;- }-- public Map<String, Object> asMap() {- Map<String, Object> map = new LinkedHashMap<>();- map.put(NAME, name);- map.put("url", url);- map.put("numDocs", numDocs);- map.put("status", status);- if (uptime != null) map.put("uptime", uptime);- if (memory != null) map.put("memory", memory);- if (isLeader) map.put("leader", true);- return map;- }-- @Override- public String toString() {- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(asMap());- return arr.toString();- }-- @Override- public int hashCode() {- return this.shard.hashCode() + (isLeader ? 1 : 0);- }-- @Override- public boolean equals(Object obj) {- if (this == obj) return true;- if (obj == null) return false;- if (!(obj instanceof ReplicaHealth)) return true;- ReplicaHealth that = (ReplicaHealth) obj;- return this.shard.equals(that.shard) && this.isLeader == that.isLeader;- }-- @Override- public int compareTo(ReplicaHealth other) {- if (this == other) return 0;- if (other == null) return 1;-- int myShardIndex = Integer.parseInt(this.shard.substring("shard".length()));-- int otherShardIndex = Integer.parseInt(other.shard.substring("shard".length()));-- if (myShardIndex == otherShardIndex) {- // same shard index, list leaders first- return this.isLeader ? -1 : 1;- }-- return myShardIndex - otherShardIndex;- }- }-- enum ShardState {- healthy,- degraded,- down,- no_leader- }-- static class ShardHealth {- String shard;- List<ReplicaHealth> replicas;-- ShardHealth(String shard, List<ReplicaHealth> replicas) {- this.shard = shard;- this.replicas = replicas;- }-- public ShardState getShardState() {- boolean healthy = true;- boolean hasLeader = false;- boolean atLeastOneActive = false;- for (ReplicaHealth replicaHealth : replicas) {- if (replicaHealth.isLeader) hasLeader = true;-- if (!Replica.State.ACTIVE.toString().equals(replicaHealth.status)) {- healthy = false;- } else {- atLeastOneActive = true;- }- }-- if (!hasLeader) return ShardState.no_leader;-- return healthy- ? ShardState.healthy- : (atLeastOneActive ? ShardState.degraded : ShardState.down);- }-- public Map<String, Object> asMap() {- Map<String, Object> map = new LinkedHashMap<>();- map.put("shard", shard);- map.put("status", getShardState().toString());- List<Object> replicaList = new ArrayList<>();- for (ReplicaHealth replica : replicas) replicaList.add(replica.asMap());- map.put("replicas", replicaList);- return map;- }-- @Override- public String toString() {- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(asMap());- return arr.toString();- }- }-- /** Requests health information about a specific collection in SolrCloud. */- public static class HealthcheckTool extends SolrCloudTool {-- public HealthcheckTool() {- this(CLIO.getOutStream());- }-- public HealthcheckTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "healthcheck";- }-- @Override- protected void runCloudTool(CloudSolrClient cloudSolrClient, CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String collection = cli.getOptionValue("collection");- if (collection == null)- throw new IllegalArgumentException(- "Must provide a collection to run a healthcheck against!");-- log.debug("Running healthcheck for {}", collection);-- ZkStateReader zkStateReader = ZkStateReader.from(cloudSolrClient);-- ClusterState clusterState = zkStateReader.getClusterState();- Set<String> liveNodes = clusterState.getLiveNodes();- final DocCollection docCollection = clusterState.getCollectionOrNull(collection);- if (docCollection == null || docCollection.getSlices() == null)- throw new IllegalArgumentException("Collection " + collection + " not found!");-- Collection<Slice> slices = docCollection.getSlices();-- SolrQuery q = new SolrQuery("*:*");- q.setRows(0);- QueryResponse qr = cloudSolrClient.query(collection, q);- checkCodeForAuthError(qr.getStatus());- String collErr = null;- long docCount = -1;- try {- docCount = qr.getResults().getNumFound();- } catch (Exception exc) {- collErr = String.valueOf(exc);- }-- List<Object> shardList = new ArrayList<>();- boolean collectionIsHealthy = (docCount != -1);-- for (Slice slice : slices) {- String shardName = slice.getName();- // since we're reporting health of this shard, there's no guarantee of a leader- String leaderUrl = null;- try {- leaderUrl = zkStateReader.getLeaderUrl(collection, shardName, 1000);- } catch (Exception exc) {- log.warn("Failed to get leader for shard {} due to: {}", shardName, exc);- }-- List<ReplicaHealth> replicaList = new ArrayList<>();- for (Replica r : slice.getReplicas()) {-- String uptime = null;- String memory = null;- String replicaStatus = null;- long numDocs = -1L;-- ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(r);- String coreUrl = replicaCoreProps.getCoreUrl();- boolean isLeader = coreUrl.equals(leaderUrl);-- // if replica's node is not live, its status is DOWN- String nodeName = replicaCoreProps.getNodeName();- if (nodeName == null || !liveNodes.contains(nodeName)) {- replicaStatus = Replica.State.DOWN.toString();- } else {- // query this replica directly to get doc count and assess health- q = new SolrQuery("*:*");- q.setRows(0);- q.set(DISTRIB, "false");- try (var solrClient = getSolrClient(coreUrl)) {- qr = solrClient.query(q);- numDocs = qr.getResults().getNumFound();-- NamedList<Object> systemInfo =- solrClient.request(- new GenericSolrRequest(- SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH));- uptime = uptime((Long) systemInfo.findRecursive("jvm", "jmx", "upTimeMS"));- String usedMemory = (String) systemInfo.findRecursive("jvm", "memory", "used");- String totalMemory = (String) systemInfo.findRecursive("jvm", "memory", "total");- memory = usedMemory + " of " + totalMemory;-- // if we get here, we can trust the state- replicaStatus = replicaCoreProps.getState();- } catch (Exception exc) {- log.error("ERROR: {} when trying to reach: {}", exc, coreUrl);-- if (checkCommunicationError(exc)) {- replicaStatus = Replica.State.DOWN.toString();- } else {- replicaStatus = "error: " + exc;- }- }- }-- replicaList.add(- new ReplicaHealth(- shardName,- r.getName(),- coreUrl,- replicaStatus,- numDocs,- isLeader,- uptime,- memory));- }-- ShardHealth shardHealth = new ShardHealth(shardName, replicaList);- if (ShardState.healthy != shardHealth.getShardState())- collectionIsHealthy = false; // at least one shard is un-healthy-- shardList.add(shardHealth.asMap());- }-- Map<String, Object> report = new LinkedHashMap<>();- report.put("collection", collection);- report.put("status", collectionIsHealthy ? "healthy" : "degraded");- if (collErr != null) {- report.put("error", collErr);- }- report.put("numDocs", docCount);- report.put("numShards", slices.size());- report.put("shards", shardList);-- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(report);- echo(arr.toString());- }- } // end HealthcheckTool-- private static final List<Option> CREATE_COLLECTION_OPTIONS =- List.of(- OPTION_ZKHOST,- OPTION_SOLRURL,- Option.builder(NAME)- .argName("NAME")- .hasArg()- .required(true)- .desc("Name of collection to create.")- .build(),- Option.builder("shards")- .argName("#")- .hasArg()- .required(false)- .desc("Number of shards; default is 1.")- .build(),- Option.builder("replicationFactor")- .argName("#")- .hasArg()- .required(false)- .desc(- "Number of copies of each document across the collection (replicas per shard); default is 1.")- .build(),- Option.builder("confdir")- .argName("NAME")- .hasArg()- .required(false)- .desc(- "Configuration directory to copy when creating the new collection; default is "- + DEFAULT_CONFIG_SET- + '.')- .build(),- Option.builder("confname")- .argName("NAME")- .hasArg()- .required(false)- .desc("Configuration name; default is the collection name.")- .build(),- Option.builder("configsetsDir")- .argName("DIR")- .hasArg()- .required(true)- .desc("Path to configsets directory on the local system.")- .build(),- OPTION_VERBOSE);-- /**- * Get the base URL of a live Solr instance from either the solrUrl command-line option from- * ZooKeeper.- */- public static String resolveSolrUrl(CommandLine cli) throws Exception {- String solrUrl = cli.getOptionValue("solrUrl");- if (solrUrl == null) {- String zkHost = cli.getOptionValue("zkHost");- if (zkHost == null)- throw new IllegalStateException(- "Must provide either the '-solrUrl' or '-zkHost' parameters!");-- try (CloudSolrClient cloudSolrClient =- new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())- .build()) {- cloudSolrClient.connect();- Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();- if (liveNodes.isEmpty())- throw new IllegalStateException(- "No live nodes found! Cannot determine 'solrUrl' from ZooKeeper: " + zkHost);-- String firstLiveNode = liveNodes.iterator().next();- solrUrl = ZkStateReader.from(cloudSolrClient).getBaseUrlForNodeName(firstLiveNode);- }- }- return solrUrl;- }-- /**- * Get the ZooKeeper connection string from either the zkHost command-line option or by looking it- * up from a running Solr instance based on the solrUrl option.- */- public static String getZkHost(CommandLine cli) throws Exception {- String zkHost = cli.getOptionValue("zkHost");- if (zkHost != null) return zkHost;-- // find it using the localPort- String solrUrl = cli.getOptionValue("solrUrl");- if (solrUrl == null)- throw new IllegalStateException(- "Must provide either the -zkHost or -solrUrl parameters to use the create_collection command!");-- if (!solrUrl.endsWith("/")) solrUrl += "/";-- try (var solrClient = getSolrClient(solrUrl)) {- // hit Solr to get system info- NamedList<Object> systemInfo =- solrClient.request(- new GenericSolrRequest(SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH));-- // convert raw JSON into user-friendly output- StatusTool statusTool = new StatusTool();- Map<String, Object> status = statusTool.reportStatus(systemInfo, solrClient);- @SuppressWarnings("unchecked")- Map<String, Object> cloud = (Map<String, Object>) status.get("cloud");- if (cloud != null) {- String zookeeper = (String) cloud.get("ZooKeeper");- if (zookeeper.endsWith("(embedded)")) {- zookeeper = zookeeper.substring(0, zookeeper.length() - "(embedded)".length());- }- zkHost = zookeeper;- }- }-- return zkHost;- }-- public static boolean safeCheckCollectionExists(String solrUrl, String collection) {- boolean exists = false;- try (var solrClient = getSolrClient(solrUrl); ) {- NamedList<Object> existsCheckResult = solrClient.request(new CollectionAdminRequest.List());- @SuppressWarnings("unchecked")- List<String> collections = (List<String>) existsCheckResult.get("collections");- exists = collections != null && collections.contains(collection);- } catch (Exception exc) {- // just ignore it since we're only interested in a positive result here- }- return exists;- }-- @SuppressWarnings("unchecked")- public static boolean safeCheckCoreExists(String solrUrl, String coreName) {- boolean exists = false;- try (var solrClient = getSolrClient(solrUrl)) {- boolean wait = false;- final long startWaitAt = System.nanoTime();- do {- if (wait) {- final int clamPeriodForStatusPollMs = 1000;- Thread.sleep(clamPeriodForStatusPollMs);- }- NamedList<Object> existsCheckResult =- CoreAdminRequest.getStatus(coreName, solrClient).getResponse();- NamedList<Object> status = (NamedList) existsCheckResult.get("status");- NamedList<Object> coreStatus = (NamedList) status.get(coreName);- Map<String, Object> failureStatus =- (Map<String, Object>) existsCheckResult.get("initFailures");- String errorMsg = (String) failureStatus.get(coreName);- final boolean hasName = coreStatus != null && coreStatus.asMap().containsKey(NAME);- exists = hasName || errorMsg != null;- wait = hasName && errorMsg == null && "true".equals(coreStatus.get("isLoading"));- } while (wait && System.nanoTime() - startWaitAt < MAX_WAIT_FOR_CORE_LOAD_NANOS);- } catch (Exception exc) {- // just ignore it since we're only interested in a positive result here- }- return exists;- }-- /** Supports create_collection command in the bin/solr script. */- public static class CreateCollectionTool extends ToolBase {-- public CreateCollectionTool() {- this(CLIO.getOutStream());- }-- public CreateCollectionTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "create_collection";- }-- @Override- public List<Option> getOptions() {- return CREATE_COLLECTION_OPTIONS;- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("solrUrl")- + " is running in standalone server mode, please use the create_core command instead;\n"- + "create_collection can only be used when running in SolrCloud mode.\n");- }-- try (CloudHttp2SolrClient cloudSolrClient =- new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())- .build()) {- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);- cloudSolrClient.connect();- runCloudTool(cloudSolrClient, cli);- }- }-- protected void runCloudTool(CloudSolrClient cloudSolrClient, CommandLine cli) throws Exception {-- Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();- if (liveNodes.isEmpty())- throw new IllegalStateException(- "No live nodes found! Cannot create a collection until "- + "there is at least 1 live node in the cluster.");-- String solrUrl = cli.getOptionValue("solrUrl");- if (solrUrl == null) {- String firstLiveNode = liveNodes.iterator().next();- solrUrl = ZkStateReader.from(cloudSolrClient).getBaseUrlForNodeName(firstLiveNode);- }-- String collectionName = cli.getOptionValue(NAME);-- // build a URL to create the collection- int numShards = optionAsInt(cli, "shards", 1);- int replicationFactor = optionAsInt(cli, "replicationFactor", 1);-- String confname = cli.getOptionValue("confname");- String confdir = cli.getOptionValue("confdir");- String configsetsDir = cli.getOptionValue("configsetsDir");-- boolean configExistsInZk =- confname != null- && !confname.trim().isEmpty()- && ZkStateReader.from(cloudSolrClient)- .getZkClient()- .exists("/configs/" + confname, true);-- if (CollectionAdminParams.SYSTEM_COLL.equals(collectionName)) {- // do nothing- } else if (configExistsInZk) {- echo("Re-using existing configuration directory " + confname);- } else if (confdir != null && !confdir.trim().isEmpty()) {- if (confname == null || confname.trim().isEmpty()) {- confname = collectionName;- }- Path confPath = ConfigSetService.getConfigsetPath(confdir, configsetsDir);-- echoIfVerbose(- "Uploading "- + confPath.toAbsolutePath()- + " for config "- + confname- + " to ZooKeeper at "- + cloudSolrClient.getClusterStateProvider().getQuorumHosts(),- cli);- ZkMaintenanceUtils.uploadToZK(- ZkStateReader.from(cloudSolrClient).getZkClient(),- confPath,- ZkMaintenanceUtils.CONFIGS_ZKNODE + "/" + confname,- ZkMaintenanceUtils.UPLOAD_FILENAME_EXCLUDE_PATTERN);- }-- // since creating a collection is a heavy-weight operation, check for existence first- if (safeCheckCollectionExists(solrUrl, collectionName)) {- throw new IllegalStateException(- "\nCollection '"- + collectionName- + "' already exists!\nChecked collection existence using CollectionAdminRequest");- }-- // doesn't seem to exist ... try to create- echoIfVerbose(- "\nCreating new collection '" + collectionName + "' using CollectionAdminRequest", cli);-- NamedList<Object> response = null;- try {- response =- cloudSolrClient.request(- CollectionAdminRequest.createCollection(- collectionName, confname, numShards, replicationFactor));- } catch (SolrServerException sse) {- throw new Exception(- "Failed to create collection '" + collectionName + "' due to: " + sse.getMessage());- }-- if (cli.hasOption(OPTION_VERBOSE.getOpt())) {- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(response.asMap());- echo(arr.toString());- } else {- String endMessage =- String.format(- Locale.ROOT,- "Created collection '%s' with %d shard(s), %d replica(s)",- collectionName,- numShards,- replicationFactor);- if (confname != null && !confname.trim().isEmpty()) {- endMessage += String.format(Locale.ROOT, " with config-set '%s'", confname);- }-- echo(endMessage);- }- }-- protected int optionAsInt(CommandLine cli, String option, int defaultVal) {- return Integer.parseInt(cli.getOptionValue(option, String.valueOf(defaultVal)));- }- } // end CreateCollectionTool class-- public static class CreateCoreTool extends ToolBase {-- public CreateCoreTool() {- this(CLIO.getOutStream());- }-- public CreateCoreTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "create_core";- }-- @Override- public List<Option> getOptions() {- return List.of(- OPTION_SOLRURL,- Option.builder(NAME)- .argName("NAME")- .hasArg()- .required(true)- .desc("Name of the core to create.")- .build(),- Option.builder("confdir")- .argName("CONFIG")- .hasArg()- .required(false)- .desc(- "Configuration directory to copy when creating the new core; default is "- + DEFAULT_CONFIG_SET- + '.')- .build(),- Option.builder("configsetsDir")- .argName("DIR")- .hasArg()- .required(true)- .desc("Path to configsets directory on the local system.")- .build(),- OPTION_VERBOSE);- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- String solrUrl = cli.getOptionValue("solrUrl", DEFAULT_SOLR_URL);- if (!solrUrl.endsWith("/")) solrUrl += "/";-- File configsetsDir = new File(cli.getOptionValue("configsetsDir"));- if (!configsetsDir.isDirectory())- throw new FileNotFoundException(configsetsDir.getAbsolutePath() + " not found!");-- String configSet = cli.getOptionValue("confdir", DEFAULT_CONFIG_SET);- File configSetDir = new File(configsetsDir, configSet);- if (!configSetDir.isDirectory()) {- // we allow them to pass a directory instead of a configset name- File possibleConfigDir = new File(configSet);- if (possibleConfigDir.isDirectory()) {- configSetDir = possibleConfigDir;- } else {- throw new FileNotFoundException(- "Specified config directory "- + configSet- + " not found in "- + configsetsDir.getAbsolutePath());- }- }-- String coreName = cli.getOptionValue(NAME);-- String coreRootDirectory = null; // usually same as solr home, but not always- try (var solrClient = getSolrClient(solrUrl)) {- Map<String, Object> systemInfo =- solrClient- .request(- new GenericSolrRequest(SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH))- .asMap();- if ("solrcloud".equals(systemInfo.get("mode"))) {- throw new IllegalStateException(- "Solr at "- + solrUrl- + " is running in SolrCloud mode, please use create_collection command instead.");- }-- // convert raw JSON into user-friendly output- coreRootDirectory = (String) systemInfo.get("core_root");-- // Fall back to solr_home, in case we are running against older server that does not return- // the property- if (coreRootDirectory == null) coreRootDirectory = (String) systemInfo.get("solr_home");- if (coreRootDirectory == null)- coreRootDirectory = configsetsDir.getParentFile().getAbsolutePath();- }-- if (safeCheckCoreExists(solrUrl, coreName)) {- throw new IllegalArgumentException(- "\nCore '"- + coreName- + "' already exists!\nChecked core existence using Core API command");- }-- File coreInstanceDir = new File(coreRootDirectory, coreName);- File confDir = new File(configSetDir, "conf");- if (!coreInstanceDir.isDirectory()) {- coreInstanceDir.mkdirs();- if (!coreInstanceDir.isDirectory())- throw new IOException(- "Failed to create new core instance directory: " + coreInstanceDir.getAbsolutePath());-- if (confDir.isDirectory()) {- FileUtils.copyDirectoryToDirectory(confDir, coreInstanceDir);- } else {- // hmmm ... the configset we're cloning doesn't have a conf sub-directory,- // we'll just assume it is OK if it has solrconfig.xml- if ((new File(configSetDir, "solrconfig.xml")).isFile()) {- FileUtils.copyDirectory(configSetDir, new File(coreInstanceDir, "conf"));- } else {- throw new IllegalArgumentException(- "\n"- + configSetDir.getAbsolutePath()- + " doesn't contain a conf subdirectory or solrconfig.xml\n");- }- }- echoIfVerbose(- "\nCopying configuration to new core instance directory:\n"- + coreInstanceDir.getAbsolutePath(),- cli);- }-- echoIfVerbose("\nCreating new core '" + coreName + "' using CoreAdminRequest", cli);-- try (var solrClient = getSolrClient(solrUrl)) {- CoreAdminResponse res = CoreAdminRequest.createCore(coreName, coreName, solrClient);- if (cli.hasOption(OPTION_VERBOSE.getOpt())) {- echo(res.jsonStr());- echo("\n");- } else {- echo(String.format(Locale.ROOT, "\nCreated new core '%s'", coreName));- }- } catch (Exception e) {- /* create-core failed, cleanup the copied configset before propagating the error. */- PathUtils.deleteDirectory(coreInstanceDir.toPath());- throw e;- }- }- } // end CreateCoreTool class-- public static class CreateTool extends ToolBase {-- public CreateTool() {- this(CLIO.getOutStream());- }-- public CreateTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "create";- }-- @Override- public List<Option> getOptions() {- return CREATE_COLLECTION_OPTIONS;- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String solrUrl = cli.getOptionValue("solrUrl", DEFAULT_SOLR_URL);- if (!solrUrl.endsWith("/")) solrUrl += "/";-- ToolBase tool = null;- try (var solrClient = getSolrClient(solrUrl)) {- NamedList<Object> systemInfo =- solrClient.request(new GenericSolrRequest(SolrRequest.METHOD.GET, SYSTEM_INFO_PATH));- if ("solrcloud".equals(systemInfo.get("mode"))) {- tool = new CreateCollectionTool(stdout);- } else {- tool = new CreateCoreTool(stdout);- }- tool.runImpl(cli);- }- }- } // end CreateTool class-- public static class ConfigSetUploadTool extends ToolBase {-- public ConfigSetUploadTool() {- this(CLIO.getOutStream());- }-- public ConfigSetUploadTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("confname")- .argName("confname") // Comes out in help message- .hasArg() // Has one sub-argument- .required(true) // confname argument must be present- .desc("Configset name in ZooKeeper.")- .build(), // passed as -confname value- Option.builder("confdir")- .argName("confdir")- .hasArg()- .required(true)- .desc("Local directory with configs.")- .build(),- Option.builder("configsetsDir")- .argName("configsetsDir")- .hasArg()- .required(false)- .desc("Parent directory of example configsets.")- .build(),- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- public String getName() {- return "upconfig";- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("solrUrl")- + " is running in standalone server mode, upconfig can only be used when running in SolrCloud mode.\n");- }-- String confName = cli.getOptionValue("confname");- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(30000, TimeUnit.MILLISECONDS)- .build()) {- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);- Path confPath =- ConfigSetService.getConfigsetPath(- cli.getOptionValue("confdir"), cli.getOptionValue("configsetsDir"));-- echo(- "Uploading "- + confPath.toAbsolutePath().toString()- + " for config "- + cli.getOptionValue("confname")- + " to ZooKeeper at "- + zkHost);- ZkMaintenanceUtils.uploadToZK(- zkClient,- confPath,- ZkMaintenanceUtils.CONFIGS_ZKNODE + "/" + confName,- ZkMaintenanceUtils.UPLOAD_FILENAME_EXCLUDE_PATTERN);-- } catch (Exception e) {- log.error("Could not complete upconfig operation for reason: ", e);- throw (e);- }- }- }-- public static class ConfigSetDownloadTool extends ToolBase {-- public ConfigSetDownloadTool() {- this(CLIO.getOutStream());- }-- public ConfigSetDownloadTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("confname")- .argName("confname")- .hasArg()- .required(true)- .desc("Configset name in ZooKeeper.")- .build(),- Option.builder("confdir")- .argName("confdir")- .hasArg()- .required(true)- .desc("Local directory with configs.")- .build(),- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- public String getName() {- return "downconfig";- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("solrUrl")- + " is running in standalone server mode, downconfig can only be used when running in SolrCloud mode.\n");- }-- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(30000, TimeUnit.MILLISECONDS)- .build()) {- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);- String confName = cli.getOptionValue("confname");- String confDir = cli.getOptionValue("confdir");- Path configSetPath = Paths.get(confDir);- // we try to be nice about having the "conf" in the directory, and we create it if it's not- // there.- if (configSetPath.endsWith("/conf") == false) {- configSetPath = Paths.get(configSetPath.toString(), "conf");- }- Files.createDirectories(configSetPath);- echo(- "Downloading configset "- + confName- + " from ZooKeeper at "- + zkHost- + " to directory "- + configSetPath.toAbsolutePath());-- zkClient.downConfig(confName, configSetPath);- } catch (Exception e) {- log.error("Could not complete downconfig operation for reason: ", e);- throw (e);- }- }- } // End ConfigSetDownloadTool class-- public static class ZkRmTool extends ToolBase {-- public ZkRmTool() {- this(CLIO.getOutStream());- }-- public ZkRmTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("path")- .argName("path")- .hasArg()- .required(true)- .desc("Path to remove.")- .build(),- OPTION_RECURSE,- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- public String getName() {- return "rm";- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);-- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("zkHost")- + " is running in standalone server mode, 'zk rm' can only be used when running in SolrCloud mode.\n");- }- String target = cli.getOptionValue("path");- Boolean recurse = Boolean.parseBoolean(cli.getOptionValue("recurse"));-- String znode = target;- if (target.toLowerCase(Locale.ROOT).startsWith("zk:")) {- znode = target.substring(3);- }- if (znode.equals("/")) {- throw new SolrServerException("You may not remove the root ZK node ('/')!");- }- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(30000, TimeUnit.MILLISECONDS)- .build()) {- if (recurse == false && zkClient.getChildren(znode, null, true).size() != 0) {- throw new SolrServerException(- "ZooKeeper node " + znode + " has children and recurse has NOT been specified.");- }- echo(- "Removing ZooKeeper node "- + znode- + " from ZooKeeper at "- + zkHost- + " recurse: "- + Boolean.toString(recurse));- zkClient.clean(znode);- } catch (Exception e) {- log.error("Could not complete rm operation for reason: ", e);- throw (e);- }- }- } // End RmTool class-- public static class ZkLsTool extends ToolBase {-- public ZkLsTool() {- this(CLIO.getOutStream());- }-- public ZkLsTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("path")- .argName("path")- .hasArg()- .required(true)- .desc("Path to list.")- .build(),- OPTION_RECURSE,- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- public String getName() {- return "ls";- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);-- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("zkHost")- + " is running in standalone server mode, 'zk ls' can only be used when running in SolrCloud mode.\n");- }-- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(30000, TimeUnit.MILLISECONDS)- .build()) {- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);-- String znode = cli.getOptionValue("path");- Boolean recurse = Boolean.parseBoolean(cli.getOptionValue("recurse"));- echoIfVerbose(- "Getting listing for ZooKeeper node "- + znode- + " from ZooKeeper at "- + zkHost- + " recurse: "- + Boolean.toString(recurse),- cli);- stdout.print(zkClient.listZnode(znode, recurse));- } catch (Exception e) {- log.error("Could not complete ls operation for reason: ", e);- throw (e);- }- }- } // End zkLsTool class-- public static class ZkMkrootTool extends ToolBase {-- public ZkMkrootTool() {- this(CLIO.getOutStream());- }-- public ZkMkrootTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("path")- .argName("path")- .hasArg()- .required(true)- .desc("Path to create.")- .build(),- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- public String getName() {- return "mkroot";- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);-- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("zkHost")- + " is running in standalone server mode, 'zk mkroot' can only be used when running in SolrCloud mode.\n");- }-- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(30000, TimeUnit.MILLISECONDS)- .build()) {- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);-- String znode = cli.getOptionValue("path");- echo("Creating ZooKeeper path " + znode + " on ZooKeeper at " + zkHost);- zkClient.makePath(znode, true);- } catch (Exception e) {- log.error("Could not complete mkroot operation for reason: ", e);- throw (e);- }- }- } // End zkMkrootTool class-- public static class ZkCpTool extends ToolBase {-- public ZkCpTool() {- this(CLIO.getOutStream());- }-- public ZkCpTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("src")- .argName("src")- .hasArg()- .required(true)- .desc("Source file or directory, may be local or a Znode.")- .build(),- Option.builder("dst")- .argName("dst")- .hasArg()- .required(true)- .desc("Destination of copy, may be local or a Znode.")- .build(),- OPTION_RECURSE,- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- public String getName() {- return "cp";- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("solrUrl")- + " is running in standalone server mode, cp can only be used when running in SolrCloud mode.\n");- }-- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(30000, TimeUnit.MILLISECONDS)- .build()) {- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);- String src = cli.getOptionValue("src");- String dst = cli.getOptionValue("dst");- Boolean recurse = Boolean.parseBoolean(cli.getOptionValue("recurse"));- echo("Copying from '" + src + "' to '" + dst + "'. ZooKeeper at " + zkHost);-- boolean srcIsZk = src.toLowerCase(Locale.ROOT).startsWith("zk:");- boolean dstIsZk = dst.toLowerCase(Locale.ROOT).startsWith("zk:");-- String srcName = src;- if (srcIsZk) {- srcName = src.substring(3);- } else if (srcName.toLowerCase(Locale.ROOT).startsWith("file:")) {- srcName = srcName.substring(5);- }-- String dstName = dst;- if (dstIsZk) {- dstName = dst.substring(3);- } else {- if (dstName.toLowerCase(Locale.ROOT).startsWith("file:")) {- dstName = dstName.substring(5);- }- }- zkClient.zkTransfer(srcName, srcIsZk, dstName, dstIsZk, recurse);- } catch (Exception e) {- log.error("Could not complete the zk operation for reason: ", e);- throw (e);- }- }- } // End CpTool class-- public static class ZkMvTool extends ToolBase {-- public ZkMvTool() {- this(CLIO.getOutStream());- }-- public ZkMvTool(PrintStream stdout) {- super(stdout);- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("src")- .argName("src")- .hasArg()- .required(true)- .desc("Source Znode to move from.")- .build(),- Option.builder("dst")- .argName("dst")- .hasArg()- .required(true)- .desc("Destination Znode to move to.")- .build(),- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- public String getName() {- return "mv";- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String zkHost = getZkHost(cli);- if (zkHost == null) {- throw new IllegalStateException(- "Solr at "- + cli.getOptionValue("solrUrl")- + " is running in standalone server mode, downconfig can only be used when running in SolrCloud mode.\n");- }-- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(30000, TimeUnit.MILLISECONDS)- .build()) {- echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...", cli);- String src = cli.getOptionValue("src");- String dst = cli.getOptionValue("dst");-- if (src.toLowerCase(Locale.ROOT).startsWith("file:")- || dst.toLowerCase(Locale.ROOT).startsWith("file:")) {- throw new SolrServerException(- "mv command operates on znodes and 'file:' has been specified.");- }- String source = src;- if (src.toLowerCase(Locale.ROOT).startsWith("zk")) {- source = src.substring(3);- }-- String dest = dst;- if (dst.toLowerCase(Locale.ROOT).startsWith("zk")) {- dest = dst.substring(3);- }-- echo("Moving Znode " + source + " to " + dest + " on ZooKeeper at " + zkHost);- zkClient.moveZnode(source, dest);- } catch (Exception e) {- log.error("Could not complete mv operation for reason: ", e);- throw (e);- }- }- } // End MvTool class-- public static class DeleteTool extends ToolBase {-- public DeleteTool() {- this(CLIO.getOutStream());- }-- public DeleteTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "delete";- }-- @Override- public List<Option> getOptions() {- return List.of(- OPTION_SOLRURL,- Option.builder(NAME)- .argName("NAME")- .hasArg()- .required(true)- .desc("Name of the core / collection to delete.")- .build(),- Option.builder("deleteConfig")- .argName("true|false")- .hasArg()- .required(false)- .desc(- "Flag to indicate if the underlying configuration directory for a collection should also be deleted; default is true.")- .build(),- Option.builder("forceDeleteConfig")- .required(false)- .desc(- "Skip safety checks when deleting the configuration directory used by a collection.")- .build(),- OPTION_ZKHOST,- OPTION_VERBOSE);- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- String solrUrl = cli.getOptionValue("solrUrl", DEFAULT_SOLR_URL);- if (!solrUrl.endsWith("/")) solrUrl += "/";-- try (var solrClient = getSolrClient(solrUrl)) {- Map<String, Object> systemInfo =- solrClient- .request(new GenericSolrRequest(SolrRequest.METHOD.GET, SYSTEM_INFO_PATH))- .asMap();- if ("solrcloud".equals(systemInfo.get("mode"))) {- deleteCollection(cli);- } else {- deleteCore(cli, solrClient);- }- }- }-- protected void deleteCollection(CommandLine cli) throws Exception {- String zkHost = getZkHost(cli);- try (CloudSolrClient cloudSolrClient =- new CloudHttp2SolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())- .withInternalClientBuilder(- new Http2SolrClient.Builder().idleTimeout(30000).connectionTimeout(15000))- .build()) {- echoIfVerbose("Connecting to ZooKeeper at " + zkHost, cli);- cloudSolrClient.connect();- deleteCollection(cloudSolrClient, cli);- }- }-- protected void deleteCollection(CloudSolrClient cloudSolrClient, CommandLine cli)- throws Exception {- Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();- if (liveNodes.isEmpty())- throw new IllegalStateException(- "No live nodes found! Cannot delete a collection until "- + "there is at least 1 live node in the cluster.");-- String firstLiveNode = liveNodes.iterator().next();- ZkStateReader zkStateReader = ZkStateReader.from(cloudSolrClient);- String solrUrl = zkStateReader.getBaseUrlForNodeName(firstLiveNode);- String collectionName = cli.getOptionValue(NAME);- if (!zkStateReader.getClusterState().hasCollection(collectionName)) {- throw new IllegalArgumentException("Collection " + collectionName + " not found!");- }-- String configName =- zkStateReader.getClusterState().getCollection(collectionName).getConfigName();- boolean deleteConfig = "true".equals(cli.getOptionValue("deleteConfig", "true"));- if (deleteConfig && configName != null) {- if (cli.hasOption("forceDeleteConfig")) {- log.warn(- "Skipping safety checks, configuration directory {} will be deleted with impunity.",- configName);- } else {- // need to scan all Collections to see if any are using the config- Set<String> collections = zkStateReader.getClusterState().getCollectionsMap().keySet();-- // give a little note to the user if there are many collections in case it takes a while- if (collections.size() > 50)- if (log.isInfoEnabled()) {- log.info(- "Scanning {} to ensure no other collections are using config {}",- collections.size(),- configName);- }-- Optional<String> inUse =- collections.stream()- .filter(name -> !name.equals(collectionName)) // ignore this collection- .filter(- name ->- configName.equals(- zkStateReader.getClusterState().getCollection(name).getConfigName()))- .findFirst();- if (inUse.isPresent()) {- deleteConfig = false;- log.warn(- "Configuration directory {} is also being used by {}{}",- configName,- inUse.get(),- "; configuration will not be deleted from ZooKeeper. You can pass the -forceDeleteConfig flag to force delete.");- }- }- }-- echoIfVerbose(- "\nDeleting collection '" + collectionName + "' using CollectionAdminRequest", cli);-- NamedList<Object> response = null;- try {- response = cloudSolrClient.request(CollectionAdminRequest.deleteCollection(collectionName));- } catch (SolrServerException sse) {- throw new Exception(- "Failed to delete collection '" + collectionName + "' due to: " + sse.getMessage());- }-- if (deleteConfig) {- String configZnode = "/configs/" + configName;- try {- zkStateReader.getZkClient().clean(configZnode);- } catch (Exception exc) {- echo(- "\nWARNING: Failed to delete configuration directory "- + configZnode- + " in ZooKeeper due to: "- + exc.getMessage()- + "\nYou'll need to manually delete this znode using the zkcli script.");- }- }-- if (response != null) {- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(response.asMap());- echo(arr.toString());- echo("\n");- }-- echo("Deleted collection '" + collectionName + "' using CollectionAdminRequest");- }-- protected void deleteCore(CommandLine cli, SolrClient solrClient) throws Exception {- String coreName = cli.getOptionValue(NAME);-- echo("\nDeleting core '" + coreName + "' using CoreAdminRequest\n");-- NamedList<Object> response = null;- try {- CoreAdminRequest.Unload unloadRequest = new CoreAdminRequest.Unload(true);- unloadRequest.setDeleteIndex(true);- unloadRequest.setDeleteDataDir(true);- unloadRequest.setDeleteInstanceDir(true);- unloadRequest.setCoreName(coreName);- response = solrClient.request(unloadRequest);- } catch (SolrServerException sse) {- throw new Exception("Failed to delete core '" + coreName + "' due to: " + sse.getMessage());- }-- if (response != null) {- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(response.asMap());- echoIfVerbose(arr.toString(), cli);- echoIfVerbose("\n", cli);- }- }- } // end DeleteTool class-- /** Sends a POST to the Config API to perform a specified action. */- public static class ConfigTool extends ToolBase {-- public ConfigTool() {- this(CLIO.getOutStream());- }-- public ConfigTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "config";- }-- @Override- public List<Option> getOptions() {- List<Option> configOptions =- List.of(- Option.builder("action")- .argName("ACTION")- .hasArg()- .required(false)- .desc(- "Config API action, one of: set-property, unset-property; default is 'set-property'.")- .build(),- Option.builder("property")- .argName("PROP")- .hasArg()- .required(true)- .desc(- "Name of the Config API property to apply the action to, such as: 'updateHandler.autoSoftCommit.maxTime'.")- .build(),- Option.builder("value")- .argName("VALUE")- .hasArg()- .required(false)- .desc("Set the property to this value; accepts JSON objects and strings.")- .build(),- OPTION_SOLRURL,- OPTION_ZKHOST,- Option.builder("p")- .argName("PORT")- .hasArg()- .required(false)- .desc("The port of the Solr node to use when applying configuration change.")- .longOpt("port")- .build(),- Option.builder("s")- .argName("SCHEME")- .hasArg()- .required(false)- .desc(- "The scheme for accessing Solr. Accepted values: http or https. Default is 'http'")- .longOpt("scheme")- .build());- return joinOptions(configOptions, cloudOptions);- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- String solrUrl;- try {- solrUrl = resolveSolrUrl(cli);- } catch (IllegalStateException e) {- // Fallback to using the provided scheme and port- final String scheme = cli.getOptionValue("scheme", "http");- if (cli.hasOption("port")) {- solrUrl = scheme + "://localhost:" + cli.getOptionValue("port", "8983") + "/solr";- } else {- throw e;- }- }-- String action = cli.getOptionValue("action", "set-property");- String collection = cli.getOptionValue("collection", "gettingstarted");- String property = cli.getOptionValue("property");- String value = cli.getOptionValue("value");-- Map<String, Object> jsonObj = new HashMap<>();- if (value != null) {- Map<String, String> setMap = new HashMap<>();- setMap.put(property, value);- jsonObj.put(action, setMap);- } else {- jsonObj.put(action, property);- }-- CharArr arr = new CharArr();- (new JSONWriter(arr, 0)).write(jsonObj);- String jsonBody = arr.toString();-- String updatePath = "/" + collection + "/config";-- echo("\nPOSTing request to Config API: " + solrUrl + updatePath);- echo(jsonBody);-- try (SolrClient solrClient = new Http2SolrClient.Builder(solrUrl).build()) {- NamedList<Object> result = postJsonToSolr(solrClient, updatePath, jsonBody);- Integer statusCode = (Integer) result.findRecursive("responseHeader", "status");- if (statusCode == 0) {- if (value != null) {- echo("Successfully " + action + " " + property + " to " + value);- } else {- echo("Successfully " + action + " " + property);- }- } else {- throw new Exception("Failed to " + action + " property due to:\n" + result);- }- }- }- } // end ConfigTool class-- /** Supports an interactive session with the user to launch (or relaunch the -e cloud example) */- public static class RunExampleTool extends ToolBase {-- private static final String PROMPT_FOR_NUMBER = "Please enter %s [%d]: ";- private static final String PROMPT_FOR_NUMBER_IN_RANGE =- "Please enter %s between %d and %d [%d]: ";- private static final String PROMPT_NUMBER_TOO_SMALL =- "%d is too small! " + PROMPT_FOR_NUMBER_IN_RANGE;- private static final String PROMPT_NUMBER_TOO_LARGE =- "%d is too large! " + PROMPT_FOR_NUMBER_IN_RANGE;-- protected InputStream userInput;- protected Executor executor;- protected String script;- protected File serverDir;- protected File exampleDir;- protected String urlScheme;-- /** Default constructor used by the framework when running as a command-line application. */- public RunExampleTool() {- this(null, System.in, CLIO.getOutStream());- }-- public RunExampleTool(Executor executor, InputStream userInput, PrintStream stdout) {- super(stdout);- this.executor = (executor != null) ? executor : new DefaultExecutor();- this.userInput = userInput;- }-- @Override- public String getName() {- return "run_example";- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("noprompt")- .required(false)- .desc(- "Don't prompt for input; accept all defaults when running examples that accept user input.")- .build(),- Option.builder("e")- .argName("NAME")- .hasArg()- .required(true)- .desc(- "Name of the example to launch, one of: cloud, techproducts, schemaless, films.")- .longOpt("example")- .build(),- Option.builder("script")- .argName("PATH")- .hasArg()- .required(false)- .desc("Path to the bin/solr script.")- .build(),- Option.builder("d")- .argName("DIR")- .hasArg()- .required(true)- .desc("Path to the Solr server directory.")- .longOpt("serverDir")- .build(),- Option.builder("force")- .argName("FORCE")- .desc("Force option in case Solr is run as root.")- .build(),- Option.builder("exampleDir")- .argName("DIR")- .hasArg()- .required(false)- .desc(- "Path to the Solr example directory; if not provided, ${serverDir}/../example is expected to exist.")- .build(),- Option.builder("urlScheme")- .argName("SCHEME")- .hasArg()- .required(false)- .desc("Solr URL scheme: http or https, defaults to http if not specified.")- .build(),- Option.builder("p")- .argName("PORT")- .hasArg()- .required(false)- .desc("Specify the port to start the Solr HTTP listener on; default is 8983.")- .longOpt("port")- .build(),- Option.builder("h")- .argName("HOSTNAME")- .hasArg()- .required(false)- .desc("Specify the hostname for this Solr instance.")- .longOpt("host")- .build(),- Option.builder("z")- .argName("ZKHOST")- .hasArg()- .required(false)- .desc(- "ZooKeeper connection string; only used when running in SolrCloud mode using -c.")- .longOpt("zkhost")- .build(),- Option.builder("c")- .required(false)- .desc(- "Start Solr in SolrCloud mode; if -z not supplied, an embedded ZooKeeper instance is started on Solr port+1000, such as 9983 if Solr is bound to 8983.")- .longOpt("cloud")- .build(),- Option.builder("m")- .argName("MEM")- .hasArg()- .required(false)- .desc(- "Sets the min (-Xms) and max (-Xmx) heap size for the JVM, such as: -m 4g results in: -Xms4g -Xmx4g; by default, this script sets the heap size to 512m.")- .longOpt("memory")- .build(),- Option.builder("a")- .argName("OPTS")- .hasArg()- .required(false)- .desc(- "Additional options to be passed to the JVM when starting example Solr server(s).")- .longOpt("addlopts")- .build());- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- this.urlScheme = cli.getOptionValue("urlScheme", "http");-- serverDir = new File(cli.getOptionValue("serverDir"));- if (!serverDir.isDirectory())- throw new IllegalArgumentException(- "Value of -serverDir option is invalid! "- + serverDir.getAbsolutePath()- + " is not a directory!");-- script = cli.getOptionValue("script");- if (script != null) {- if (!(new File(script)).isFile())- throw new IllegalArgumentException(- "Value of -script option is invalid! " + script + " not found");- } else {- File scriptFile = new File(serverDir.getParentFile(), "bin/solr");- if (scriptFile.isFile()) {- script = scriptFile.getAbsolutePath();- } else {- scriptFile = new File(serverDir.getParentFile(), "bin/solr.cmd");- if (scriptFile.isFile()) {- script = scriptFile.getAbsolutePath();- } else {- throw new IllegalArgumentException(- "Cannot locate the bin/solr script! Please pass -script to this application.");- }- }- }-- exampleDir =- (cli.hasOption("exampleDir"))- ? new File(cli.getOptionValue("exampleDir"))- : new File(serverDir.getParent(), "example");- if (!exampleDir.isDirectory())- throw new IllegalArgumentException(- "Value of -exampleDir option is invalid! "- + exampleDir.getAbsolutePath()- + " is not a directory!");-- echoIfVerbose(- "Running with\nserverDir="- + serverDir.getAbsolutePath()- + ",\nexampleDir="- + exampleDir.getAbsolutePath()- + "\nscript="- + script,- cli);-- String exampleType = cli.getOptionValue("example");- if ("cloud".equals(exampleType)) {- runCloudExample(cli);- } else if ("techproducts".equals(exampleType)- || "schemaless".equals(exampleType)- || "films".equals(exampleType)) {- runExample(cli, exampleType);- } else {- throw new IllegalArgumentException(- "Unsupported example "- + exampleType- + "! Please choose one of: cloud, schemaless, techproducts, or films");- }- }-- protected void runExample(CommandLine cli, String exampleName) throws Exception {- File exDir = setupExampleDir(serverDir, exampleDir, exampleName);- String collectionName = "schemaless".equals(exampleName) ? "gettingstarted" : exampleName;- String configSet =- "techproducts".equals(exampleName) ? "sample_techproducts_configs" : "_default";-- boolean isCloudMode = cli.hasOption('c');- String zkHost = cli.getOptionValue('z');- int port = Integer.parseInt(cli.getOptionValue('p', "8983"));- Map<String, Object> nodeStatus =- startSolr(new File(exDir, "solr"), isCloudMode, cli, port, zkHost, 30);-- // invoke the CreateTool- File configsetsDir = new File(serverDir, "solr/configsets");-- String solrUrl = (String) nodeStatus.get("baseUrl");-- // safe check if core / collection already exists- boolean alreadyExists = false;- if (nodeStatus.get("cloud") != null) {- if (safeCheckCollectionExists(solrUrl, collectionName)) {- alreadyExists = true;- echo(- "\nWARNING: Collection '"- + collectionName- + "' already exists!\nChecked collection existence using Collections API");- }- } else {- String coreName = collectionName;- if (safeCheckCoreExists(solrUrl, coreName)) {- alreadyExists = true;- echo(- "\nWARNING: Core '"- + coreName- + "' already exists!\nChecked core existence using Core API command");- }- }-- if (!alreadyExists) {- String[] createArgs =- new String[] {- "-name", collectionName,- "-shards", "1",- "-replicationFactor", "1",- "-confname", collectionName,- "-confdir", configSet,- "-configsetsDir", configsetsDir.getAbsolutePath(),- "-solrUrl", solrUrl- };- CreateTool createTool = new CreateTool(stdout);- int createCode =- createTool.runTool(- processCommandLineArgs(- createTool.getName(),- joinCommonAndToolOptions(createTool.getOptions()),- createArgs));- if (createCode != 0)- throw new Exception(- "Failed to create "- + collectionName- + " using command: "- + Arrays.asList(createArgs));- }-- if ("techproducts".equals(exampleName) && !alreadyExists) {-- File exampledocsDir = new File(exampleDir, "exampledocs");- if (!exampledocsDir.isDirectory()) {- File readOnlyExampleDir = new File(serverDir.getParentFile(), "example");- if (readOnlyExampleDir.isDirectory()) {- exampledocsDir = new File(readOnlyExampleDir, "exampledocs");- }- }-- if (exampledocsDir.isDirectory()) {- String updateUrl = String.format(Locale.ROOT, "%s/%s/update", solrUrl, collectionName);- echo("Indexing tech product example docs from " + exampledocsDir.getAbsolutePath());-- String currentPropVal = System.getProperty("url");- System.setProperty("url", updateUrl);- SimplePostTool.main(new String[] {exampledocsDir.getAbsolutePath() + "/*.xml"});- if (currentPropVal != null) {- System.setProperty("url", currentPropVal); // reset- } else {- System.clearProperty("url");- }- } else {- echo(- "exampledocs directory not found, skipping indexing step for the techproducts example");- }- } else if ("films".equals(exampleName) && !alreadyExists) {- SolrClient solrClient = new Http2SolrClient.Builder(solrUrl).build();-- echo("Adding dense vector field type to films schema \"_default\"");- try {- SolrCLI.postJsonToSolr(- solrClient,- "/" + collectionName + "/schema",- "{\n"- + " \"add-field-type\" : {\n"- + " \"name\":\"knn_vector_10\",\n"- + " \"class\":\"solr.DenseVectorField\",\n"- + " \"vectorDimension\":10,\n"- + " \"similarityFunction\":cosine\n"- + " \"knnAlgorithm\":hnsw\n"- + " }\n"- + " }");- } catch (Exception ex) {- throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);- }-- echo(- "Adding name, initial_release_date, and film_vector fields to films schema \"_default\"");- try {- SolrCLI.postJsonToSolr(- solrClient,- "/" + collectionName + "/schema",- "{\n"- + " \"add-field\" : {\n"- + " \"name\":\"name\",\n"- + " \"type\":\"text_general\",\n"- + " \"multiValued\":false,\n"- + " \"stored\":true\n"- + " },\n"- + " \"add-field\" : {\n"- + " \"name\":\"initial_release_date\",\n"- + " \"type\":\"pdate\",\n"- + " \"stored\":true\n"- + " },\n"- + " \"add-field\" : {\n"- + " \"name\":\"film_vector\",\n"- + " \"type\":\"knn_vector_10\",\n"- + " \"indexed\":true\n"- + " \"stored\":true\n"- + " }\n"- + " }");- } catch (Exception ex) {- throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);- }-- echo(- "Adding paramsets \"algo\" and \"algo_b\" to films configuration for relevancy tuning");- try {- SolrCLI.postJsonToSolr(- solrClient,- "/" + collectionName + "/config/params",- "{\n"- + " \"set\": {\n"- + " \"algo_a\":{\n"- + " \"defType\":\"dismax\",\n"- + " \"qf\":\"name\"\n"- + " }\n"- + " },\n"- + " \"set\": {\n"- + " \"algo_b\":{\n"- + " \"defType\":\"dismax\",\n"- + " \"qf\":\"name\",\n"- + " \"mm\":\"100%\"\n"- + " }\n"- + " }\n"- + " }\n");- } catch (Exception ex) {- throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);- }-- File filmsJsonFile = new File(exampleDir, "films/films.json");- String updateUrl = String.format(Locale.ROOT, "%s/%s/update/json", solrUrl, collectionName);- echo("Indexing films example docs from " + filmsJsonFile.getAbsolutePath());- String currentPropVal = System.getProperty("url");- System.setProperty("url", updateUrl);- SimplePostTool.main(new String[] {filmsJsonFile.getAbsolutePath()});- if (currentPropVal != null) {- System.setProperty("url", currentPropVal); // reset- } else {- System.clearProperty("url");- }- }-- echo(- "\nSolr "- + exampleName- + " example launched successfully. Direct your Web browser to "- + solrUrl- + " to visit the Solr Admin UI");- }-- protected void runCloudExample(CommandLine cli) throws Exception {-- boolean prompt = !cli.hasOption("noprompt");- int numNodes = 2;- int[] cloudPorts = new int[] {8983, 7574, 8984, 7575};- File cloudDir = new File(exampleDir, "cloud");- if (!cloudDir.isDirectory()) cloudDir.mkdir();-- echo("\nWelcome to the SolrCloud example!\n");-- Scanner readInput = prompt ? new Scanner(userInput, StandardCharsets.UTF_8.name()) : null;- if (prompt) {- echo(- "This interactive session will help you launch a SolrCloud cluster on your local workstation.");-- // get the number of nodes to start- numNodes =- promptForInt(- readInput,- "To begin, how many Solr nodes would you like to run in your local cluster? (specify 1-4 nodes) [2]: ",- "a number",- numNodes,- 1,- 4);-- echo("Ok, let's start up " + numNodes + " Solr nodes for your example SolrCloud cluster.");-- // get the ports for each port- for (int n = 0; n < numNodes; n++) {- String promptMsg =- String.format(- Locale.ROOT, "Please enter the port for node%d [%d]: ", (n + 1), cloudPorts[n]);- int port = promptForPort(readInput, n + 1, promptMsg, cloudPorts[n]);- while (!isPortAvailable(port)) {- port =- promptForPort(- readInput,- n + 1,- "Oops! Looks like port "- + port- + " is already being used by another process. Please choose a different port.",- cloudPorts[n]);- }-- cloudPorts[n] = port;- echoIfVerbose("Using port " + port + " for node " + (n + 1), cli);- }- } else {- echo("Starting up " + numNodes + " Solr nodes for your example SolrCloud cluster.\n");- }-- // setup a unique solr.solr.home directory for each node- File node1Dir = setupExampleDir(serverDir, cloudDir, "node1");- for (int n = 2; n <= numNodes; n++) {- File nodeNDir = new File(cloudDir, "node" + n);- if (!nodeNDir.isDirectory()) {- echo("Cloning " + node1Dir.getAbsolutePath() + " into\n " + nodeNDir.getAbsolutePath());- FileUtils.copyDirectory(node1Dir, nodeNDir);- } else {- echo(nodeNDir.getAbsolutePath() + " already exists.");- }- }-- // deal with extra args passed to the script to run the example- String zkHost = cli.getOptionValue('z');-- // start the first node (most likely with embedded ZK)- Map<String, Object> nodeStatus =- startSolr(new File(node1Dir, "solr"), true, cli, cloudPorts[0], zkHost, 30);-- if (zkHost == null) {- @SuppressWarnings("unchecked")- Map<String, Object> cloudStatus = (Map<String, Object>) nodeStatus.get("cloud");- if (cloudStatus != null) {- String zookeeper = (String) cloudStatus.get("ZooKeeper");- if (zookeeper != null) zkHost = zookeeper;- }- if (zkHost == null)- throw new Exception("Could not get the ZooKeeper connection string for node1!");- }-- if (numNodes > 1) {- // start the other nodes- for (int n = 1; n < numNodes; n++)- startSolr(- new File(cloudDir, "node" + (n + 1) + "/solr"), true, cli, cloudPorts[n], zkHost, 30);- }-- String solrUrl = (String) nodeStatus.get("baseUrl");- if (solrUrl.endsWith("/")) solrUrl = solrUrl.substring(0, solrUrl.length() - 1);-- // wait until live nodes == numNodes- waitToSeeLiveNodes(10 /* max wait */, zkHost, numNodes);-- // create the collection- String collectionName = createCloudExampleCollection(numNodes, readInput, prompt, solrUrl);-- // update the config to enable soft auto-commit- echo("\nEnabling auto soft-commits with maxTime 3 secs using the Config API");- setCollectionConfigProperty(- solrUrl, collectionName, "updateHandler.autoSoftCommit.maxTime", "3000");-- echo("\n\nSolrCloud example running, please visit: " + solrUrl + " \n");- }-- protected void setCollectionConfigProperty(- String solrUrl, String collectionName, String propName, String propValue) {- ConfigTool configTool = new ConfigTool(stdout);- String[] configArgs =- new String[] {- "-collection",- collectionName,- "-property",- propName,- "-value",- propValue,- "-solrUrl",- solrUrl- };-- // let's not fail if we get this far ... just report error and finish up- try {- configTool.runTool(- processCommandLineArgs(- configTool.getName(),- joinCommonAndToolOptions(configTool.getOptions()),- configArgs));- } catch (Exception exc) {- CLIO.err("Failed to update '" + propName + "' property due to: " + exc);- }- }-- protected void waitToSeeLiveNodes(int maxWaitSecs, String zkHost, int numNodes) {- CloudSolrClient cloudClient = null;- try {- cloudClient =- new CloudSolrClient.Builder(Collections.singletonList(zkHost), Optional.empty())- .build();- cloudClient.connect();- Set<String> liveNodes = cloudClient.getClusterState().getLiveNodes();- int numLiveNodes = (liveNodes != null) ? liveNodes.size() : 0;- long timeout =- System.nanoTime() + TimeUnit.NANOSECONDS.convert(maxWaitSecs, TimeUnit.SECONDS);- while (System.nanoTime() < timeout && numLiveNodes < numNodes) {- echo(- "\nWaiting up to "- + maxWaitSecs- + " seconds to see "- + (numNodes - numLiveNodes)- + " more nodes join the SolrCloud cluster ...");- try {- Thread.sleep(2000);- } catch (InterruptedException ie) {- Thread.interrupted();- }- liveNodes = cloudClient.getClusterState().getLiveNodes();- numLiveNodes = (liveNodes != null) ? liveNodes.size() : 0;- }- if (numLiveNodes < numNodes) {- echo(- "\nWARNING: Only "- + numLiveNodes- + " of "- + numNodes- + " are active in the cluster after "- + maxWaitSecs- + " seconds! Please check the solr.log for each node to look for errors.\n");- }- } catch (Exception exc) {- CLIO.err("Failed to see if " + numNodes + " joined the SolrCloud cluster due to: " + exc);- } finally {- if (cloudClient != null) {- try {- cloudClient.close();- } catch (Exception ignore) {- }- }- }- }-- protected Map<String, Object> startSolr(- File solrHomeDir,- boolean cloudMode,- CommandLine cli,- int port,- String zkHost,- int maxWaitSecs)- throws Exception {-- String extraArgs = readExtraArgs(cli.getArgs());-- String host = cli.getOptionValue('h');- String memory = cli.getOptionValue('m');-- String hostArg = (host != null && !"localhost".equals(host)) ? " -h " + host : "";- String zkHostArg = (zkHost != null) ? " -z " + zkHost : "";- String memArg = (memory != null) ? " -m " + memory : "";- String cloudModeArg = cloudMode ? "-cloud " : "";- String forceArg = cli.hasOption("force") ? " -force" : "";-- String addlOpts = cli.getOptionValue('a');- String addlOptsArg = (addlOpts != null) ? " -a \"" + addlOpts + "\"" : "";-- File cwd = new File(System.getProperty("user.dir"));- File binDir = (new File(script)).getParentFile();-- boolean isWindows = (OS.isFamilyDOS() || OS.isFamilyWin9x() || OS.isFamilyWindows());- String callScript = (!isWindows && cwd.equals(binDir.getParentFile())) ? "bin/solr" : script;-- String cwdPath = cwd.getAbsolutePath();- String solrHome = solrHomeDir.getAbsolutePath();-- // don't display a huge path for solr home if it is relative to the cwd- if (!isWindows && cwdPath.length() > 1 && solrHome.startsWith(cwdPath))- solrHome = solrHome.substring(cwdPath.length() + 1);-- String startCmd =- String.format(- Locale.ROOT,- "\"%s\" start %s -p %d -s \"%s\" %s %s %s %s %s %s",- callScript,- cloudModeArg,- port,- solrHome,- hostArg,- zkHostArg,- memArg,- forceArg,- extraArgs,- addlOptsArg);- startCmd = startCmd.replaceAll("\\s+", " ").trim(); // for pretty printing-- echo("\nStarting up Solr on port " + port + " using command:");- echo(startCmd + "\n");-- String solrUrl =- String.format(- Locale.ROOT, "%s://%s:%d/solr", urlScheme, (host != null ? host : "localhost"), port);-- Map<String, Object> nodeStatus = checkPortConflict(solrUrl, solrHomeDir, port, cli);- if (nodeStatus != null)- return nodeStatus; // the server they are trying to start is already running-- int code = 0;- if (isWindows) {- // On Windows, the execution doesn't return, so we have to execute async- // and when calling the script, it seems to be inheriting the environment that launched this- // app so we have to prune out env vars that may cause issues- Map<String, String> startEnv = new HashMap<>();- Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment();- if (procEnv != null) {- for (Map.Entry<String, String> entry : procEnv.entrySet()) {- String envVar = entry.getKey();- String envVarVal = entry.getValue();- if (envVarVal != null && !"EXAMPLE".equals(envVar) && !envVar.startsWith("SOLR_")) {- startEnv.put(envVar, envVarVal);- }- }- }- DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();- executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd), startEnv, handler);-- // wait for execution.- try {- handler.waitFor(3000);- } catch (InterruptedException ie) {- // safe to ignore ...- Thread.interrupted();- }- if (handler.hasResult() && handler.getExitValue() != 0) {- throw new Exception(- "Failed to start Solr using command: "- + startCmd- + " Exception : "- + handler.getException());- }- } else {- try {- code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd));- } catch (ExecuteException e) {- throw new Exception(- "Failed to start Solr using command: " + startCmd + " Exception : " + e);- }- }- if (code != 0) throw new Exception("Failed to start Solr using command: " + startCmd);-- return getNodeStatus(solrUrl, maxWaitSecs);- }-- protected Map<String, Object> checkPortConflict(- String solrUrl, File solrHomeDir, int port, CommandLine cli) {- // quickly check if the port is in use- if (isPortAvailable(port)) return null; // not in use ... try to start-- Map<String, Object> nodeStatus = null;- try {- nodeStatus = (new StatusTool()).getStatus(solrUrl);- } catch (Exception ignore) {- /* just trying to determine if this example is already running. */- }-- if (nodeStatus != null) {- String solr_home = (String) nodeStatus.get("solr_home");- if (solr_home != null) {- String solrHomePath = solrHomeDir.getAbsolutePath();- if (!solrHomePath.endsWith("/")) solrHomePath += "/";- if (!solr_home.endsWith("/")) solr_home += "/";-- if (solrHomePath.equals(solr_home)) {- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(nodeStatus);- echo(- "Solr is already setup and running on port "- + port- + " with status:\n"- + arr.toString());- echo(- "\nIf this is not the example node you are trying to start, please choose a different port.");- nodeStatus.put("baseUrl", solrUrl);- return nodeStatus;- }- }- }-- throw new IllegalStateException(- "Port " + port + " is already being used by another process.");- }-- protected String readExtraArgs(String[] extraArgsArr) {- String extraArgs = "";- if (extraArgsArr != null && extraArgsArr.length > 0) {- StringBuilder sb = new StringBuilder();- int app = 0;- for (int e = 0; e < extraArgsArr.length; e++) {- String arg = extraArgsArr[e];- if ("e".equals(arg) || "example".equals(arg)) {- e++; // skip over the example arg- continue;- }-- if (app > 0) sb.append(" ");- sb.append(arg);- ++app;- }- extraArgs = sb.toString().trim();- }- return extraArgs;- }-- protected String createCloudExampleCollection(- int numNodes, Scanner readInput, boolean prompt, String solrUrl) throws Exception {- // yay! numNodes SolrCloud nodes running- int numShards = 2;- int replicationFactor = 2;- String cloudConfig = "_default";- String collectionName = "gettingstarted";-- File configsetsDir = new File(serverDir, "solr/configsets");-- if (prompt) {- echo(- "\nNow let's create a new collection for indexing documents in your "- + numNodes- + "-node cluster.");-- while (true) {- collectionName =- prompt(- readInput,- "Please provide a name for your new collection: [" + collectionName + "] ",- collectionName);-- // Test for existence and then prompt to either create another or skip the create step- if (safeCheckCollectionExists(solrUrl, collectionName)) {- echo("\nCollection '" + collectionName + "' already exists!");- int oneOrTwo =- promptForInt(- readInput,- "Do you want to re-use the existing collection or create a new one? Enter 1 to reuse, 2 to create new [1]: ",- "a 1 or 2",- 1,- 1,- 2);- if (oneOrTwo == 1) {- return collectionName;- } else {- continue;- }- } else {- break; // user selected a collection that doesn't exist ... proceed on- }- }-- numShards =- promptForInt(- readInput,- "How many shards would you like to split " + collectionName + " into? [2]",- "a shard count",- 2,- 1,- 4);-- replicationFactor =- promptForInt(- readInput,- "How many replicas per shard would you like to create? [2] ",- "a replication factor",- 2,- 1,- 4);-- echo(- "Please choose a configuration for the "- + collectionName- + " collection, available options are:");- String validConfigs = "_default or sample_techproducts_configs [" + cloudConfig + "] ";- cloudConfig = prompt(readInput, validConfigs, cloudConfig);-- // validate the cloudConfig name- while (!isValidConfig(configsetsDir, cloudConfig)) {- echo(- cloudConfig- + " is not a valid configuration directory! Please choose a configuration for the "- + collectionName- + " collection, available options are:");- cloudConfig = prompt(readInput, validConfigs, cloudConfig);- }- } else {- // must verify if default collection exists- if (safeCheckCollectionExists(solrUrl, collectionName)) {- echo(- "\nCollection '"- + collectionName- + "' already exists! Skipping collection creation step.");- return collectionName;- }- }-- // invoke the CreateCollectionTool- String[] createArgs =- new String[] {- "-name", collectionName,- "-shards", String.valueOf(numShards),- "-replicationFactor", String.valueOf(replicationFactor),- "-confname", collectionName,- "-confdir", cloudConfig,- "-configsetsDir", configsetsDir.getAbsolutePath(),- "-solrUrl", solrUrl- };-- CreateCollectionTool createCollectionTool = new CreateCollectionTool(stdout);- int createCode =- createCollectionTool.runTool(- processCommandLineArgs(- createCollectionTool.getName(),- joinCommonAndToolOptions(createCollectionTool.getOptions()),- createArgs));-- if (createCode != 0)- throw new Exception(- "Failed to create collection using command: " + Arrays.asList(createArgs));-- return collectionName;- }-- protected boolean isValidConfig(File configsetsDir, String config) {- File configDir = new File(configsetsDir, config);- if (configDir.isDirectory()) return true;-- // not a built-in configset ... maybe it's a custom directory?- configDir = new File(config);- if (configDir.isDirectory()) return true;-- return false;- }-- protected Map<String, Object> getNodeStatus(String solrUrl, int maxWaitSecs) throws Exception {- StatusTool statusTool = new StatusTool();- if (verbose) echo("\nChecking status of Solr at " + solrUrl + " ...");-- URL solrURL = new URL(solrUrl);- Map<String, Object> nodeStatus = statusTool.waitToSeeSolrUp(solrUrl, maxWaitSecs);- nodeStatus.put("baseUrl", solrUrl);- CharArr arr = new CharArr();- new JSONWriter(arr, 2).write(nodeStatus);- String mode = (nodeStatus.get("cloud") != null) ? "cloud" : "standalone";- if (verbose)- echo(- "\nSolr is running on "- + solrURL.getPort()- + " in "- + mode- + " mode with status:\n"- + arr.toString());-- return nodeStatus;- }-- protected File setupExampleDir(File serverDir, File exampleParentDir, String dirName)- throws IOException {- File solrXml = new File(serverDir, "solr/solr.xml");- if (!solrXml.isFile())- throw new IllegalArgumentException(- "Value of -serverDir option is invalid! " + solrXml.getAbsolutePath() + " not found!");-- File zooCfg = new File(serverDir, "solr/zoo.cfg");- if (!zooCfg.isFile())- throw new IllegalArgumentException(- "Value of -serverDir option is invalid! " + zooCfg.getAbsolutePath() + " not found!");-- File solrHomeDir = new File(exampleParentDir, dirName + "/solr");- if (!solrHomeDir.isDirectory()) {- echo("Creating Solr home directory " + solrHomeDir);- solrHomeDir.mkdirs();- } else {- echo("Solr home directory " + solrHomeDir.getAbsolutePath() + " already exists.");- }-- copyIfNeeded(solrXml, new File(solrHomeDir, "solr.xml"));- copyIfNeeded(zooCfg, new File(solrHomeDir, "zoo.cfg"));-- return solrHomeDir.getParentFile();- }-- protected void copyIfNeeded(File src, File dest) throws IOException {- if (!dest.isFile()) Files.copy(src.toPath(), dest.toPath());-- if (!dest.isFile())- throw new IllegalStateException("Required file " + dest.getAbsolutePath() + " not found!");- }-- protected boolean isPortAvailable(int port) {- Socket s = null;- try {- s = new Socket("localhost", port);- return false;- } catch (IOException e) {- return true;- } finally {- if (s != null) {- try {- s.close();- } catch (IOException ignore) {- }- }- }- }-- protected Integer promptForPort(Scanner s, int node, String prompt, Integer defVal) {- return promptForInt(s, prompt, "a port for node " + node, defVal, null, null);- }-- protected Integer promptForInt(- Scanner s, String prompt, String label, Integer defVal, Integer min, Integer max) {- Integer inputAsInt = null;-- String value = prompt(s, prompt, null /* default is null since we handle that here */);- if (value != null) {- int attempts = 3;- while (value != null && --attempts > 0) {- try {- inputAsInt = Integer.valueOf(value);-- if (min != null) {- if (inputAsInt < min) {- value =- prompt(- s,- String.format(- Locale.ROOT,- PROMPT_NUMBER_TOO_SMALL,- inputAsInt,- label,- min,- max,- defVal));- inputAsInt = null;- continue;- }- }-- if (max != null) {- if (inputAsInt > max) {- value =- prompt(- s,- String.format(- Locale.ROOT,- PROMPT_NUMBER_TOO_LARGE,- inputAsInt,- label,- min,- max,- defVal));- inputAsInt = null;- continue;- }- }-- } catch (NumberFormatException nfe) {- if (verbose) echo(value + " is not a number!");-- if (min != null && max != null) {- value =- prompt(- s,- String.format(- Locale.ROOT, PROMPT_FOR_NUMBER_IN_RANGE, label, min, max, defVal));- } else {- value = prompt(s, String.format(Locale.ROOT, PROMPT_FOR_NUMBER, label, defVal));- }- }- }- if (attempts == 0 && value != null && inputAsInt == null)- echo("Too many failed attempts! Going with default value " + defVal);- }-- return (inputAsInt != null) ? inputAsInt : defVal;- }-- protected String prompt(Scanner s, String prompt) {- return prompt(s, prompt, null);- }-- protected String prompt(Scanner s, String prompt, String defaultValue) {- echo(prompt);- String nextInput = s.nextLine();- if (nextInput != null) {- nextInput = nextInput.trim();- if (nextInput.isEmpty()) nextInput = null;- }- return (nextInput != null) ? nextInput : defaultValue;- }- } // end RunExampleTool class-- /**- * Asserts various conditions and exists with error code if fails, else continues with no output- */- public static class AssertTool extends ToolBase {-- private static String message = null;- private static boolean useExitCode = false;- private static Optional<Long> timeoutMs = Optional.empty();-- public AssertTool() {- this(CLIO.getOutStream());- }-- public AssertTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "assert";- }-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("R")- .desc("Asserts that we are NOT the root user.")- .longOpt("not-root")- .build(),- Option.builder("r").desc("Asserts that we are the root user.").longOpt("root").build(),- Option.builder("S")- .desc("Asserts that Solr is NOT running on a certain URL. Default timeout is 1000ms.")- .longOpt("not-started")- .hasArg(true)- .argName("url")- .build(),- Option.builder("s")- .desc("Asserts that Solr is running on a certain URL. Default timeout is 1000ms.")- .longOpt("started")- .hasArg(true)- .argName("url")- .build(),- Option.builder("u")- .desc("Asserts that we run as same user that owns <directory>.")- .longOpt("same-user")- .hasArg(true)- .argName("directory")- .build(),- Option.builder("x")- .desc("Asserts that directory <directory> exists.")- .longOpt("exists")- .hasArg(true)- .argName("directory")- .build(),- Option.builder("X")- .desc("Asserts that directory <directory> does NOT exist.")- .longOpt("not-exists")- .hasArg(true)- .argName("directory")- .build(),- Option.builder("c")- .desc(- "Asserts that Solr is running in cloud mode. Also fails if Solr not running. URL should be for root Solr path.")- .longOpt("cloud")- .hasArg(true)- .argName("url")- .build(),- Option.builder("C")- .desc(- "Asserts that Solr is not running in cloud mode. Also fails if Solr not running. URL should be for root Solr path.")- .longOpt("not-cloud")- .hasArg(true)- .argName("url")- .build(),- Option.builder("m")- .desc("Exception message to be used in place of the default error message.")- .longOpt("message")- .hasArg(true)- .argName("message")- .build(),- Option.builder("t")- .desc("Timeout in ms for commands supporting a timeout.")- .longOpt("timeout")- .hasArg(true)- .type(Long.class)- .argName("ms")- .build(),- Option.builder("e")- .desc("Return an exit code instead of printing error message on assert fail.")- .longOpt("exitcode")- .build());- }-- @Override- public int runTool(CommandLine cli) throws Exception {- verbose = cli.hasOption(OPTION_VERBOSE.getOpt());-- int toolExitStatus = 0;- try {- toolExitStatus = runAssert(cli);- } catch (Exception exc) {- // since this is a CLI, spare the user the stacktrace- String excMsg = exc.getMessage();- if (excMsg != null) {- if (verbose) {- CLIO.err("\nERROR: " + exc + "\n");- } else {- CLIO.err("\nERROR: " + excMsg + "\n");- }- toolExitStatus = 100; // Exit >= 100 means error, else means number of tests that failed- } else {- throw exc;- }- }- return toolExitStatus;- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {- runAssert(cli);- }-- /**- * Custom run method which may return exit code- *- * @param cli the command line object- * @return 0 on success, or a number corresponding to number of tests that failed- * @throws Exception if a tool failed, e.g. authentication failure- */- protected int runAssert(CommandLine cli) throws Exception {- if (cli.getOptions().length == 0 || cli.getArgs().length > 0 || cli.hasOption("h")) {- new HelpFormatter()- .printHelp(- "bin/solr assert [-m <message>] [-e] [-rR] [-s <url>] [-S <url>] [-c <url>] [-C <url>] [-u <dir>] [-x <dir>] [-X <dir>]",- getToolOptions(this));- return 1;- }- if (cli.hasOption("m")) {- message = cli.getOptionValue("m");- }- if (cli.hasOption("t")) {- timeoutMs = Optional.of(Long.parseLong(cli.getOptionValue("t")));- }- if (cli.hasOption("e")) {- useExitCode = true;- }-- int ret = 0;- if (cli.hasOption("r")) {- ret += assertRootUser();- }- if (cli.hasOption("R")) {- ret += assertNotRootUser();- }- if (cli.hasOption("x")) {- ret += assertFileExists(cli.getOptionValue("x"));- }- if (cli.hasOption("X")) {- ret += assertFileNotExists(cli.getOptionValue("X"));- }- if (cli.hasOption("u")) {- ret += sameUser(cli.getOptionValue("u"));- }- if (cli.hasOption("s")) {- ret += assertSolrRunning(cli.getOptionValue("s"));- }- if (cli.hasOption("S")) {- ret += assertSolrNotRunning(cli.getOptionValue("S"));- }- if (cli.hasOption("c")) {- ret += assertSolrRunningInCloudMode(cli.getOptionValue("c"));- }- if (cli.hasOption("C")) {- ret += assertSolrNotRunningInCloudMode(cli.getOptionValue("C"));- }- return ret;- }-- public static int assertSolrRunning(String url) throws Exception {- StatusTool status = new StatusTool();- try {- status.waitToSeeSolrUp(url, timeoutMs.orElse(1000L).intValue() / 1000);- } catch (Exception se) {- if (exceptionIsAuthRelated(se)) {- throw se;- }- return exitOrException(- "Solr is not running on url " + url + " after " + timeoutMs.orElse(1000L) / 1000 + "s");- }- return 0;- }-- public static int assertSolrNotRunning(String url) throws Exception {- StatusTool status = new StatusTool();- long timeout =- System.nanoTime()- + TimeUnit.NANOSECONDS.convert(timeoutMs.orElse(1000L), TimeUnit.MILLISECONDS);- try (SolrClient solrClient = getSolrClient(url)) {- NamedList<Object> response = solrClient.request(new HealthCheckRequest());- Integer statusCode = (Integer) response.findRecursive("responseHeader", "status");- checkCodeForAuthError(statusCode);- } catch (SolrException se) {- throw se;- } catch (IOException | SolrServerException e) {- log.debug("Opening connection to {} failed, Solr does not seem to be running", url, e);- return 0;- }- while (System.nanoTime() < timeout) {- try {- status.waitToSeeSolrUp(url, 1);- try {- log.debug("Solr still up. Waiting before trying again to see if it was stopped");- Thread.sleep(1000L);- } catch (InterruptedException interrupted) {- timeout = 0; // stop looping- }- } catch (Exception se) {- if (exceptionIsAuthRelated(se)) {- throw se;- }- return exitOrException(se.getMessage());- }- }- return exitOrException(- "Solr is still running at " + url + " after " + timeoutMs.orElse(1000L) / 1000 + "s");- }-- public static int assertSolrRunningInCloudMode(String url) throws Exception {- if (!isSolrRunningOn(url)) {- return exitOrException(- "Solr is not running on url " + url + " after " + timeoutMs.orElse(1000L) / 1000 + "s");- }-- if (!runningSolrIsCloud(url)) {- return exitOrException("Solr is not running in cloud mode on " + url);- }- return 0;- }-- public static int assertSolrNotRunningInCloudMode(String url) throws Exception {- if (!isSolrRunningOn(url)) {- return exitOrException(- "Solr is not running on url " + url + " after " + timeoutMs.orElse(1000L) / 1000 + "s");- }-- if (runningSolrIsCloud(url)) {- return exitOrException("Solr is not running in standalone mode on " + url);- }- return 0;- }-- public static int sameUser(String directory) throws Exception {- if (Files.exists(Paths.get(directory))) {- String userForDir = userForDir(Paths.get(directory));- if (!currentUser().equals(userForDir)) {- return exitOrException("Must run as user " + userForDir + ". We are " + currentUser());- }- } else {- return exitOrException("Directory " + directory + " does not exist.");- }- return 0;- }-- public static int assertFileExists(String directory) throws Exception {- if (!Files.exists(Paths.get(directory))) {- return exitOrException("Directory " + directory + " does not exist.");- }- return 0;- }-- public static int assertFileNotExists(String directory) throws Exception {- if (Files.exists(Paths.get(directory))) {- return exitOrException("Directory " + directory + " should not exist.");- }- return 0;- }-- public static int assertRootUser() throws Exception {- if (!currentUser().equals("root")) {- return exitOrException("Must run as root user");- }- return 0;- }-- public static int assertNotRootUser() throws Exception {- if (currentUser().equals("root")) {- return exitOrException("Not allowed to run as root user");- }- return 0;- }-- public static String currentUser() {- return System.getProperty("user.name");- }-- public static String userForDir(Path pathToDir) {- try {- FileOwnerAttributeView ownerAttributeView =- Files.getFileAttributeView(pathToDir, FileOwnerAttributeView.class);- return ownerAttributeView.getOwner().getName();- } catch (IOException e) {- return "N/A";- }- }-- private static int exitOrException(String msg) throws AssertionFailureException {- if (useExitCode) {- return 1;- } else {- throw new AssertionFailureException(message != null ? message : msg);- }- }-- private static boolean isSolrRunningOn(String url) throws Exception {- StatusTool status = new StatusTool();- try {- status.waitToSeeSolrUp(url, timeoutMs.orElse(1000L).intValue() / 1000);- return true;- } catch (Exception se) {- if (exceptionIsAuthRelated(se)) {- throw se;- }- return false;- }- }-- private static boolean runningSolrIsCloud(String url) throws Exception {- try (final SolrClient client = new Http2SolrClient.Builder(url).build()) {- final SolrRequest<CollectionAdminResponse> request =- new CollectionAdminRequest.ClusterStatus();- final CollectionAdminResponse response = request.process(client);- return response != null;- } catch (Exception e) {- if (exceptionIsAuthRelated(e)) {- throw e;- }- return false;- }- }- } // end AssertTool class-- public static class AssertionFailureException extends Exception {- public AssertionFailureException(String message) {- super(message);- }- }-- // Authentication tool- public static class AuthTool extends ToolBase {- public AuthTool() {- this(CLIO.getOutStream());- }-- public AuthTool(PrintStream stdout) {- super(stdout);- }-- @Override- public String getName() {- return "auth";- }-- List<String> authenticationVariables =- Arrays.asList(- "SOLR_AUTHENTICATION_CLIENT_BUILDER", "SOLR_AUTH_TYPE", "SOLR_AUTHENTICATION_OPTS");-- @Override- public List<Option> getOptions() {- return List.of(- Option.builder("type")- .argName("type")- .hasArg()- .desc(- "The authentication mechanism to enable (basicAuth or kerberos). Defaults to 'basicAuth'.")- .build(),- Option.builder("credentials")- .argName("credentials")- .hasArg()- .desc(- "Credentials in the format username:password. Example: -credentials solr:SolrRocks")- .build(),- Option.builder("prompt")- .argName("prompt")- .hasArg()- .desc(- "Prompts the user to provide the credentials. Use either -credentials or -prompt, not both.")- .build(),- Option.builder("config")- .argName("config")- .hasArgs()- .desc(- "Configuration parameters (Solr startup parameters). Required for Kerberos authentication.")- .build(),- Option.builder("blockUnknown")- .argName("blockUnknown")- .desc(- "Blocks all access for unknown users (requires authentication for all endpoints).")- .hasArg()- .build(),- Option.builder("solrIncludeFile")- .argName("solrIncludeFile")- .hasArg()- .desc(- "The Solr include file which contains overridable environment variables for configuring Solr configurations.")- .build(),- Option.builder("updateIncludeFileOnly")- .argName("updateIncludeFileOnly")- .desc(- "Only update the solr.in.sh or solr.in.cmd file, and skip actual enabling/disabling"- + " authentication (i.e. don't update security.json).")- .hasArg()- .build(),- Option.builder("authConfDir")- .argName("authConfDir")- .hasArg()- .required()- .desc(- "This is where any authentication related configuration files, if any, would be placed.")- .build(),- Option.builder("solrUrl").argName("solrUrl").hasArg().desc("Solr URL.").build(),- Option.builder("zkHost")- .argName("zkHost")- .hasArg()- .desc("ZooKeeper host to connect to.")- .build(),- OPTION_VERBOSE);- }-- private void ensureArgumentIsValidBooleanIfPresent(CommandLine cli, String argName) {- if (cli.hasOption(argName)) {- final String value = cli.getOptionValue(argName);- if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value)) {- echo("Argument [" + argName + "] must be either true or false, but was [" + value + "]");- exit(1);- }- }- }-- @Override- public int runTool(CommandLine cli) throws Exception {- raiseLogLevelUnlessVerbose(cli);- if (cli.getOptions().length == 0- || cli.getArgs().length == 0- || cli.getArgs().length > 1- || cli.hasOption("h")) {- new HelpFormatter()- .printHelp("bin/solr auth <enable|disable> [OPTIONS]", getToolOptions(this));- return 1;- }-- ensureArgumentIsValidBooleanIfPresent(cli, "blockUnknown");- ensureArgumentIsValidBooleanIfPresent(cli, "updateIncludeFileOnly");-- String type = cli.getOptionValue("type", "basicAuth");- switch (type) {- case "basicAuth":- return handleBasicAuth(cli);- case "kerberos":- return handleKerberos(cli);- default:- CLIO.out("Only type=basicAuth or kerberos supported at the moment.");- exit(1);- }- return 1;- }-- private int handleKerberos(CommandLine cli) throws Exception {- String cmd = cli.getArgs()[0];- boolean updateIncludeFileOnly =- Boolean.parseBoolean(cli.getOptionValue("updateIncludeFileOnly", "false"));- String securityJson =- "{"- + "\n \"authentication\":{"- + "\n \"class\":\"solr.KerberosPlugin\""- + "\n }"- + "\n}";-- switch (cmd) {- case "enable":- String zkHost = null;- boolean zkInaccessible = false;-- if (!updateIncludeFileOnly) {- try {- zkHost = getZkHost(cli);- } catch (Exception ex) {- CLIO.out(- "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"- + securityJson- + "\n");- zkInaccessible = true;- }- if (zkHost == null) {- if (zkInaccessible == false) {- CLIO.out(- "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"- + securityJson- + "\n");- zkInaccessible = true;- }- }-- // check if security is already enabled or not- if (!zkInaccessible) {- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(10000, TimeUnit.MILLISECONDS)- .build()) {- if (zkClient.exists("/security.json", true)) {- byte oldSecurityBytes[] = zkClient.getData("/security.json", null, null, true);- if (!"{}".equals(new String(oldSecurityBytes, StandardCharsets.UTF_8).trim())) {- CLIO.out(- "Security is already enabled. You can disable it with 'bin/solr auth disable'. Existing security.json: \n"- + new String(oldSecurityBytes, StandardCharsets.UTF_8));- exit(1);- }- }- } catch (Exception ex) {- if (zkInaccessible == false) {- CLIO.out(- "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"- + securityJson- + "\n");- zkInaccessible = true;- }- }- }- }-- if (!updateIncludeFileOnly) {- if (!zkInaccessible) {- echoIfVerbose("Uploading following security.json: " + securityJson, cli);- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(10000, TimeUnit.MILLISECONDS)- .build()) {- zkClient.setData(- "/security.json", securityJson.getBytes(StandardCharsets.UTF_8), true);- } catch (Exception ex) {- if (zkInaccessible == false) {- CLIO.out(- "Unable to access ZooKeeper. Please add the following security.json to ZooKeeper (in case of SolrCloud):\n"- + securityJson);- zkInaccessible = true;- }- }- }- }-- String config = StrUtils.join(Arrays.asList(cli.getOptionValues("config")), ' ');- // config is base64 encoded (to get around parsing problems), decode it- config = config.replace(" ", "");- config =- new String(- Base64.getDecoder().decode(config.getBytes(StandardCharsets.UTF_8)),- StandardCharsets.UTF_8);- config = config.replace("\n", "").replace("\r", "");-- String solrIncludeFilename = cli.getOptionValue("solrIncludeFile");- File includeFile = new File(solrIncludeFilename);- if (includeFile.exists() == false || includeFile.canWrite() == false) {- CLIO.out(- "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");- printAuthEnablingInstructions(config);- System.exit(0);- }-- // update the solr.in.sh file to contain the necessary authentication lines- updateIncludeFileEnableAuth(includeFile.toPath(), null, config, cli);- echo(- "Successfully enabled Kerberos authentication; please restart any running Solr nodes.");- return 0;-- case "disable":- if (!updateIncludeFileOnly) {- zkHost = getZkHost(cli);- if (zkHost == null) {- stdout.print("ZK Host not found. Solr should be running in cloud mode.");- exit(1);- }-- echoIfVerbose("Uploading following security.json: {}", cli);-- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(10000, TimeUnit.MILLISECONDS)- .build()) {- zkClient.setData("/security.json", "{}".getBytes(StandardCharsets.UTF_8), true);- }- }-- solrIncludeFilename = cli.getOptionValue("solrIncludeFile");- includeFile = new File(solrIncludeFilename);- if (!includeFile.exists() || !includeFile.canWrite()) {- CLIO.out(- "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");- CLIO.out(- "Security has been disabled. Please remove any SOLR_AUTH_TYPE or SOLR_AUTHENTICATION_OPTS configuration from solr.in.sh/solr.in.cmd.\n");- System.exit(0);- }-- // update the solr.in.sh file to comment out the necessary authentication lines- updateIncludeFileDisableAuth(includeFile.toPath(), cli);- return 0;-- default:- CLIO.out("Valid auth commands are: enable, disable.");- exit(1);- }-- CLIO.out("Options not understood.");- new HelpFormatter()- .printHelp("bin/solr auth <enable|disable> [OPTIONS]", getToolOptions(this));- return 1;- }-- private int handleBasicAuth(CommandLine cli) throws Exception {- String cmd = cli.getArgs()[0];- boolean prompt = Boolean.parseBoolean(cli.getOptionValue("prompt", "false"));- boolean updateIncludeFileOnly =- Boolean.parseBoolean(cli.getOptionValue("updateIncludeFileOnly", "false"));- switch (cmd) {- case "enable":- if (!prompt && !cli.hasOption("credentials")) {- CLIO.out("Option -credentials or -prompt is required with enable.");- new HelpFormatter()- .printHelp("bin/solr auth <enable|disable> [OPTIONS]", getToolOptions(this));- exit(1);- } else if (!prompt- && (cli.getOptionValue("credentials") == null- || !cli.getOptionValue("credentials").contains(":"))) {- CLIO.out("Option -credentials is not in correct format.");- new HelpFormatter()- .printHelp("bin/solr auth <enable|disable> [OPTIONS]", getToolOptions(this));- exit(1);- }-- String zkHost = null;-- if (!updateIncludeFileOnly) {- try {- zkHost = getZkHost(cli);- } catch (Exception ex) {- if (cli.hasOption("zkHost")) {- CLIO.out(- "Couldn't get ZooKeeper host. Please make sure that ZooKeeper is running and the correct zkHost has been passed in.");- } else {- CLIO.out(- "Couldn't get ZooKeeper host. Please make sure Solr is running in cloud mode, or a zkHost has been passed in.");- }- exit(1);- }- if (zkHost == null) {- if (cli.hasOption("zkHost")) {- CLIO.out(- "Couldn't get ZooKeeper host. Please make sure that ZooKeeper is running and the correct zkHost has been passed in.");- } else {- CLIO.out(- "Couldn't get ZooKeeper host. Please make sure Solr is running in cloud mode, or a zkHost has been passed in.");- }- exit(1);- }-- // check if security is already enabled or not- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(10000, TimeUnit.MILLISECONDS)- .build()) {- if (zkClient.exists("/security.json", true)) {- byte oldSecurityBytes[] = zkClient.getData("/security.json", null, null, true);- if (!"{}".equals(new String(oldSecurityBytes, StandardCharsets.UTF_8).trim())) {- CLIO.out(- "Security is already enabled. You can disable it with 'bin/solr auth disable'. Existing security.json: \n"- + new String(oldSecurityBytes, StandardCharsets.UTF_8));- exit(1);- }- }- }- }-- String username, password;- if (cli.hasOption("credentials")) {- String credentials = cli.getOptionValue("credentials");- username = credentials.split(":")[0];- password = credentials.split(":")[1];- } else {- Console console = System.console();- // keep prompting until they've entered a non-empty username & password- do {- username = console.readLine("Enter username: ");- } while (username == null || username.trim().length() == 0);- username = username.trim();-- do {- password = new String(console.readPassword("Enter password: "));- } while (password.length() == 0);- }-- boolean blockUnknown = Boolean.valueOf(cli.getOptionValue("blockUnknown", "true"));-- String securityJson =- "{"- + "\n \"authentication\":{"- + "\n \"blockUnknown\": "- + blockUnknown- + ","- + "\n \"class\":\"solr.BasicAuthPlugin\","- + "\n \"credentials\":{\""- + username- + "\":\""- + Sha256AuthenticationProvider.getSaltedHashedValue(password)- + "\"}"- + "\n },"- + "\n \"authorization\":{"- + "\n \"class\":\"solr.RuleBasedAuthorizationPlugin\","- + "\n \"permissions\":["- + "\n {\"name\":\"security-edit\", \"role\":\"admin\"},"- + "\n {\"name\":\"security-read\", \"role\":\"admin\"},"- + "\n {\"name\":\"config-edit\", \"role\":\"admin\"},"- + "\n {\"name\":\"config-read\", \"role\":\"admin\"},"- + "\n {\"name\":\"collection-admin-edit\", \"role\":\"admin\"},"- + "\n {\"name\":\"collection-admin-read\", \"role\":\"admin\"},"- + "\n {\"name\":\"core-admin-edit\", \"role\":\"admin\"},"- + "\n {\"name\":\"core-admin-read\", \"role\":\"admin\"},"- + "\n {\"name\":\"all\", \"role\":\"admin\"}"- + "\n ],"- + "\n \"user-role\":{\""- + username- + "\":\"admin\"}"- + "\n }"- + "\n}";-- if (!updateIncludeFileOnly) {- echoIfVerbose("Uploading following security.json: " + securityJson, cli);- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(10000, TimeUnit.MILLISECONDS)- .build()) {- zkClient.setData(- "/security.json", securityJson.getBytes(StandardCharsets.UTF_8), true);- }- }-- String solrIncludeFilename = cli.getOptionValue("solrIncludeFile");- File includeFile = new File(solrIncludeFilename);- if (includeFile.exists() == false || includeFile.canWrite() == false) {- CLIO.out(- "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");- printAuthEnablingInstructions(username, password);- System.exit(0);- }- String authConfDir = cli.getOptionValue("authConfDir");- File basicAuthConfFile = new File(authConfDir + File.separator + "basicAuth.conf");-- if (basicAuthConfFile.getParentFile().canWrite() == false) {- CLIO.out("Cannot write to file: " + basicAuthConfFile.getAbsolutePath());- printAuthEnablingInstructions(username, password);- System.exit(0);- }-- Files.writeString(- basicAuthConfFile.toPath(),- "httpBasicAuthUser=" + username + "\nhttpBasicAuthPassword=" + password,- StandardCharsets.UTF_8);-- // update the solr.in.sh file to contain the necessary authentication lines- updateIncludeFileEnableAuth(- includeFile.toPath(), basicAuthConfFile.getAbsolutePath(), null, cli);- final String successMessage =- String.format(- Locale.ROOT,- "Successfully enabled basic auth with username [%s] and password [%s].",- username,- password);- echo(successMessage);- return 0;-- case "disable":- if (!updateIncludeFileOnly) {- zkHost = getZkHost(cli);- if (zkHost == null) {- stdout.print("ZK Host not found. Solr should be running in cloud mode.");- exit(1);- }-- echoIfVerbose("Uploading following security.json: {}", cli);-- try (SolrZkClient zkClient =- new SolrZkClient.Builder()- .withUrl(zkHost)- .withTimeout(10000, TimeUnit.MILLISECONDS)- .build()) {- zkClient.setData("/security.json", "{}".getBytes(StandardCharsets.UTF_8), true);- }- }-- solrIncludeFilename = cli.getOptionValue("solrIncludeFile");- includeFile = new File(solrIncludeFilename);- if (!includeFile.exists() || !includeFile.canWrite()) {- CLIO.out(- "Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");- CLIO.out(- "Security has been disabled. Please remove any SOLR_AUTH_TYPE or SOLR_AUTHENTICATION_OPTS configuration from solr.in.sh/solr.in.cmd.\n");- System.exit(0);- }-- // update the solr.in.sh file to comment out the necessary authentication lines- updateIncludeFileDisableAuth(includeFile.toPath(), cli);- return 0;-- default:- CLIO.out("Valid auth commands are: enable, disable.");- exit(1);- }-- CLIO.out("Options not understood.");- new HelpFormatter()- .printHelp("bin/solr auth <enable|disable> [OPTIONS]", getToolOptions(this));- return 1;- }-- private void printAuthEnablingInstructions(String username, String password) {- if (Constants.WINDOWS) {- CLIO.out(- "\nAdd the following lines to the solr.in.cmd file so that the solr.cmd script can use subsequently.\n");- CLIO.out(- "set SOLR_AUTH_TYPE=basic\n"- + "set SOLR_AUTHENTICATION_OPTS=\"-Dbasicauth="- + username- + ":"- + password- + "\"\n");- } else {- CLIO.out(- "\nAdd the following lines to the solr.in.sh file so that the ./solr script can use subsequently.\n");- CLIO.out(- "SOLR_AUTH_TYPE=\"basic\"\n"- + "SOLR_AUTHENTICATION_OPTS=\"-Dbasicauth="- + username- + ":"- + password- + "\"\n");- }- }-- private void printAuthEnablingInstructions(String kerberosConfig) {- if (Constants.WINDOWS) {- CLIO.out(- "\nAdd the following lines to the solr.in.cmd file so that the solr.cmd script can use subsequently.\n");- CLIO.out(- "set SOLR_AUTH_TYPE=kerberos\n"- + "set SOLR_AUTHENTICATION_OPTS=\""- + kerberosConfig- + "\"\n");- } else {- CLIO.out(- "\nAdd the following lines to the solr.in.sh file so that the ./solr script can use subsequently.\n");- CLIO.out(- "SOLR_AUTH_TYPE=\"kerberos\"\n"- + "SOLR_AUTHENTICATION_OPTS=\""- + kerberosConfig- + "\"\n");- }- }-- /**- * This will update the include file (e.g. solr.in.sh / solr.in.cmd) with the authentication- * parameters.- *- * @param includeFile The include file- * @param basicAuthConfFile If basicAuth, the path of the file containing credentials. If not,- * null.- * @param kerberosConfig If kerberos, the config string containing startup parameters. If not,- * null.- */- private void updateIncludeFileEnableAuth(- Path includeFile, String basicAuthConfFile, String kerberosConfig, CommandLine cli)- throws IOException {- assert !(basicAuthConfFile != null- && kerberosConfig != null); // only one of the two needs to be populated- List<String> includeFileLines = Files.readAllLines(includeFile, StandardCharsets.UTF_8);- for (int i = 0; i < includeFileLines.size(); i++) {- String line = includeFileLines.get(i);- if (authenticationVariables.contains(line.trim().split("=")[0].trim())) { // Non-Windows- includeFileLines.set(i, "# " + line);- }- if (line.trim().split("=")[0].trim().startsWith("set ")- && authenticationVariables.contains(- line.trim().split("=")[0].trim().substring(4))) { // Windows- includeFileLines.set(i, "REM " + line);- }- }- includeFileLines.add(""); // blank line-- if (basicAuthConfFile != null) { // for basicAuth- if (Constants.WINDOWS) {- includeFileLines.add("REM The following lines added by solr.cmd for enabling BasicAuth");- includeFileLines.add("set SOLR_AUTH_TYPE=basic");- includeFileLines.add(- "set SOLR_AUTHENTICATION_OPTS=\"-Dsolr.httpclient.config="- + basicAuthConfFile- + "\"");- } else {- includeFileLines.add("# The following lines added by ./solr for enabling BasicAuth");- includeFileLines.add("SOLR_AUTH_TYPE=\"basic\"");- includeFileLines.add(- "SOLR_AUTHENTICATION_OPTS=\"-Dsolr.httpclient.config=" + basicAuthConfFile + "\"");- }- } else { // for kerberos- if (Constants.WINDOWS) {- includeFileLines.add("REM The following lines added by solr.cmd for enabling BasicAuth");- includeFileLines.add("set SOLR_AUTH_TYPE=kerberos");- includeFileLines.add(- "set SOLR_AUTHENTICATION_OPTS=\"-Dsolr.httpclient.config="- + basicAuthConfFile- + "\"");- } else {- includeFileLines.add("# The following lines added by ./solr for enabling BasicAuth");- includeFileLines.add("SOLR_AUTH_TYPE=\"kerberos\"");- includeFileLines.add("SOLR_AUTHENTICATION_OPTS=\"" + kerberosConfig + "\"");- }- }-- String lines = includeFileLines.stream().collect(Collectors.joining(System.lineSeparator()));- Files.writeString(includeFile, lines, StandardCharsets.UTF_8);-- if (basicAuthConfFile != null) {- echoIfVerbose("Written out credentials file: " + basicAuthConfFile, cli);- }- echoIfVerbose("Updated Solr include file: " + includeFile.toAbsolutePath(), cli);- }-- private void updateIncludeFileDisableAuth(Path includeFile, CommandLine cli)- throws IOException {- List<String> includeFileLines = Files.readAllLines(includeFile, StandardCharsets.UTF_8);- boolean hasChanged = false;- for (int i = 0; i < includeFileLines.size(); i++) {- String line = includeFileLines.get(i);- if (authenticationVariables.contains(line.trim().split("=")[0].trim())) { // Non-Windows- includeFileLines.set(i, "# " + line);- hasChanged = true;- }- if (line.trim().split("=")[0].trim().startsWith("set ")- && authenticationVariables.contains(- line.trim().split("=")[0].trim().substring(4))) { // Windows- includeFileLines.set(i, "REM " + line);- hasChanged = true;- }- }- if (hasChanged) {- String lines =- includeFileLines.stream().collect(Collectors.joining(System.lineSeparator()));- Files.writeString(includeFile, lines, StandardCharsets.UTF_8);- echoIfVerbose("Commented out necessary lines from " + includeFile.toAbsolutePath(), cli);- }- }-- @Override- protected void runImpl(CommandLine cli) throws Exception {}- }-}diff --git a/solr/core/src/test/org/apache/solr/util/ApiToolTest.java b/solr/core/src/test/org/apache/solr/cli/ApiToolTest.javasimilarity index 93%rename from solr/core/src/test/org/apache/solr/util/ApiToolTest.javarename to solr/core/src/test/org/apache/solr/cli/ApiToolTest.javaindex 245f73a144e..c0f6fd8328b 100644--- a/solr/core/src/test/org/apache/solr/util/ApiToolTest.java+++ b/solr/core/src/test/org/apache/solr/cli/ApiToolTest.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import java.net.URI;import java.net.URISyntaxException;@@ -23,12 +23,10 @@ import org.apache.solr.SolrTestCase;import org.apache.solr.common.params.ModifiableSolrParams;import org.junit.Test;-/** Unit test for SolrCLI's ApiTool */public class ApiToolTest extends SolrTestCase {@Testpublic void testParsingGetUrl() throws URISyntaxException {- SolrCLI.ApiTool apiTool = new SolrCLI.ApiTool();String urlWithPercent20SpaceDelimiter ="http://localhost:8983/solr/COLL_NAME/sql?stmt=select%20id%20from%20COLL_NAME%20limit%2010";diff --git a/solr/core/src/test/org/apache/solr/util/AuthToolTest.java b/solr/core/src/test/org/apache/solr/cli/AuthToolTest.javasimilarity index 89%rename from solr/core/src/test/org/apache/solr/util/AuthToolTest.javarename to solr/core/src/test/org/apache/solr/cli/AuthToolTest.javaindex 718b4fc7f99..cb2d5caff4d 100644--- a/solr/core/src/test/org/apache/solr/util/AuthToolTest.java+++ b/solr/core/src/test/org/apache/solr/cli/AuthToolTest.java@@ -15,10 +15,10 @@* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;-import static org.apache.solr.util.SolrCLI.findTool;-import static org.apache.solr.util.SolrCLI.parseCmdLine;+import static org.apache.solr.cli.SolrCLI.findTool;+import static org.apache.solr.cli.SolrCLI.parseCmdLine;import java.nio.file.Files;import java.nio.file.Path;@@ -30,7 +30,6 @@ import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;-/** Unit test for SolrCLI's AuthTool */public class AuthToolTest extends SolrCloudTestCase {private Path dir;@@ -80,8 +79,8 @@ public class AuthToolTest extends SolrCloudTestCase {}private int runTool(String[] args) throws Exception {- SolrCLI.Tool tool = findTool(args);- assertTrue(tool instanceof SolrCLI.AuthTool);+ Tool tool = findTool(args);+ assertTrue(tool instanceof AuthTool);CommandLine cli = parseCmdLine(tool.getName(), args, tool.getOptions());return tool.runTool(cli);}diff --git a/solr/core/src/test/org/apache/solr/util/SimplePostToolTest.java b/solr/core/src/test/org/apache/solr/cli/SimplePostToolTest.javasimilarity index 98%rename from solr/core/src/test/org/apache/solr/util/SimplePostToolTest.javarename to solr/core/src/test/org/apache/solr/cli/SimplePostToolTest.javaindex e844582eeb1..2f157057175 100644--- a/solr/core/src/test/org/apache/solr/util/SimplePostToolTest.java+++ b/solr/core/src/test/org/apache/solr/cli/SimplePostToolTest.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import java.io.ByteArrayInputStream;import java.io.File;@@ -31,8 +31,8 @@ import java.util.HashMap;import java.util.HashSet;import java.util.Set;import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.util.SimplePostTool.PageFetcher;-import org.apache.solr.util.SimplePostTool.PageFetcherResult;+import org.apache.solr.cli.SimplePostTool.PageFetcher;+import org.apache.solr.cli.SimplePostTool.PageFetcherResult;import org.junit.Before;import org.junit.Test;@@ -256,7 +256,7 @@ public class SimplePostToolTest extends SolrTestCaseJ4 {sb.append("User-agent: * # match all bots\n");sb.append("Disallow: # This is void\n");sb.append("Disallow: /disallow # Disallow this path\n");- sb.append("Disallow: /nonexistingpath # Disallow this path\n");+ sb.append("Disallow: /nonexistentpath # Disallow this path\n");this.robotsCache.put("[ff01::114]",super.parseRobotsTxt(diff --git a/solr/core/src/test/org/apache/solr/util/SolrCliUptimeTest.java b/solr/core/src/test/org/apache/solr/cli/SolrCliUptimeTest.javasimilarity index 98%rename from solr/core/src/test/org/apache/solr/util/SolrCliUptimeTest.javarename to solr/core/src/test/org/apache/solr/cli/SolrCliUptimeTest.javaindex ff8def5eab6..4589ea539ef 100644--- a/solr/core/src/test/org/apache/solr/util/SolrCliUptimeTest.java+++ b/solr/core/src/test/org/apache/solr/cli/SolrCliUptimeTest.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import org.apache.solr.SolrTestCase;import org.junit.Test;diff --git a/solr/core/src/test/org/apache/solr/util/SolrLogPostToolTest.java b/solr/core/src/test/org/apache/solr/cli/SolrLogPostToolTest.javasimilarity index 98%rename from solr/core/src/test/org/apache/solr/util/SolrLogPostToolTest.javarename to solr/core/src/test/org/apache/solr/cli/SolrLogPostToolTest.javaindex 1cfeff22845..2ebfb9f1b82 100644--- a/solr/core/src/test/org/apache/solr/util/SolrLogPostToolTest.java+++ b/solr/core/src/test/org/apache/solr/cli/SolrLogPostToolTest.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import java.io.BufferedReader;import java.io.StringReader;@@ -22,9 +22,9 @@ import java.util.ArrayList;import java.util.Collection;import java.util.List;import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.cli.SolrLogPostTool.LogRecordReader;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.SolrInputField;-import org.apache.solr.util.SolrLogPostTool.LogRecordReader;import org.junit.BeforeClass;import org.junit.Test;@@ -331,8 +331,8 @@ public class SolrLogPostToolTest extends SolrTestCaseJ4 {ArrayList<SolrInputDocument> list = new ArrayList<>();try {- LogRecordReader logRecordReader = new SolrLogPostTool.LogRecordReader(bufferedReader);- SolrInputDocument doc = null;+ LogRecordReader logRecordReader = new LogRecordReader(bufferedReader);+ SolrInputDocument doc;while ((doc = logRecordReader.readRecord()) != null) {list.add(doc);}diff --git a/solr/core/src/test/org/apache/solr/util/TestExportTool.java b/solr/core/src/test/org/apache/solr/cli/TestExportTool.javasimilarity index 99%rename from solr/core/src/test/org/apache/solr/util/TestExportTool.javarename to solr/core/src/test/org/apache/solr/cli/TestExportTool.javaindex 5e86e83347d..82424998ff0 100644--- a/solr/core/src/test/org/apache/solr/util/TestExportTool.java+++ b/solr/core/src/test/org/apache/solr/cli/TestExportTool.java@@ -15,7 +15,7 @@* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import java.io.File;import java.io.FileInputStream;diff --git a/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java b/solr/core/src/test/org/apache/solr/cli/TestSolrCLIRunExample.javasimilarity index 95%rename from solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.javarename to solr/core/src/test/org/apache/solr/cli/TestSolrCLIRunExample.javaindex e822c3fe24e..78afd0751dc 100644--- a/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java+++ b/solr/core/src/test/org/apache/solr/cli/TestSolrCLIRunExample.java@@ -14,7 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.util;+package org.apache.solr.cli;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;@@ -48,6 +48,7 @@ import org.apache.solr.cloud.MiniSolrCloudCluster;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.embedded.JettyConfig;import org.apache.solr.embedded.JettySolrRunner;+import org.apache.solr.util.ExternalPaths;import org.junit.After;import org.junit.AfterClass;import org.junit.BeforeClass;@@ -365,12 +366,11 @@ public class TestSolrCLIRunExample extends SolrTestCaseJ4 {RunExampleExecutor executor = new RunExampleExecutor(stdoutSim);closeables.add(executor);- SolrCLI.RunExampleTool tool = new SolrCLI.RunExampleTool(executor, System.in, stdoutSim);+ RunExampleTool tool = new RunExampleTool(executor, System.in, stdoutSim);try {int status =tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));+ SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), toolArgs));if (status == -1) {// maybe it's the port, try again@@ -380,8 +380,7 @@ public class TestSolrCLIRunExample extends SolrTestCaseJ4 {Thread.sleep(100);status =tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));+ SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), toolArgs));}assertEquals("it should be ok " + tool + " " + Arrays.toString(toolArgs), 0, status);@@ -488,11 +487,9 @@ public class TestSolrCLIRunExample extends SolrTestCaseJ4 {RunExampleExecutor executor = new RunExampleExecutor(stdoutSim);closeables.add(executor);- SolrCLI.RunExampleTool tool = new SolrCLI.RunExampleTool(executor, userInputSim, stdoutSim);+ RunExampleTool tool = new RunExampleTool(executor, userInputSim, stdoutSim);try {- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));+ tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), toolArgs));} catch (Exception e) {System.err.println("RunExampleTool failed due to: "@@ -554,11 +551,10 @@ public class TestSolrCLIRunExample extends SolrTestCaseJ4 {}// delete the collection- SolrCLI.DeleteTool deleteTool = new SolrCLI.DeleteTool(stdoutSim);+ DeleteTool deleteTool = new DeleteTool(stdoutSim);String[] deleteArgs = new String[] {"-name", collectionName, "-solrUrl", solrUrl};deleteTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(deleteTool.getOptions()), deleteArgs));+ SolrCLI.processCommandLineArgs(deleteTool.getName(), deleteTool.getOptions(), deleteArgs));// dump all the output written by the SolrCLI commands to stdout// System.out.println(toolOutput);@@ -603,11 +599,9 @@ public class TestSolrCLIRunExample extends SolrTestCaseJ4 {DefaultExecutor executor = new DefaultExecutor();- SolrCLI.RunExampleTool tool = new SolrCLI.RunExampleTool(executor, System.in, stdoutSim);+ RunExampleTool tool = new RunExampleTool(executor, System.in, stdoutSim);int code =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));+ tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), toolArgs));assertEquals("Execution should have failed with return code 1", 1, code);}}diff --git a/solr/core/src/test/org/apache/solr/cloud/PackageManagerCLITest.java b/solr/core/src/test/org/apache/solr/cloud/PackageManagerCLITest.javaindex 96f1d78649d..b1e11c45a3c 100644--- a/solr/core/src/test/org/apache/solr/cloud/PackageManagerCLITest.java+++ b/solr/core/src/test/org/apache/solr/cloud/PackageManagerCLITest.java@@ -19,11 +19,11 @@ package org.apache.solr.cloud;import java.lang.invoke.MethodHandles;import java.util.Arrays;+import org.apache.solr.cli.PackageTool;+import org.apache.solr.cli.SolrCLI;import org.apache.solr.client.solrj.request.CollectionAdminRequest;import org.apache.solr.core.TestSolrConfigHandler;import org.apache.solr.util.LogLevel;-import org.apache.solr.util.PackageTool;-import org.apache.solr.util.SolrCLI;import org.eclipse.jetty.server.Handler;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.ServerConnector;@@ -215,10 +215,7 @@ public class PackageManagerCLITest extends SolrCloudTestCase {}private void run(PackageTool tool, String[] args) throws Exception {- int res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ int res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertEquals("Non-zero status returned for: " + Arrays.toString(args), 0, res);}diff --git a/solr/core/src/test/org/apache/solr/cloud/SolrCLIZkUtilsTest.java b/solr/core/src/test/org/apache/solr/cloud/SolrCLIZkUtilsTest.javaindex 0ee5d289456..38f6a674dd3 100644--- a/solr/core/src/test/org/apache/solr/cloud/SolrCLIZkUtilsTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/SolrCLIZkUtilsTest.java@@ -31,9 +31,15 @@ import java.nio.file.attribute.BasicFileAttributes;import java.util.ArrayList;import java.util.List;import java.util.concurrent.TimeUnit;+import org.apache.solr.cli.ConfigSetDownloadTool;+import org.apache.solr.cli.ConfigSetUploadTool;+import org.apache.solr.cli.SolrCLI;+import org.apache.solr.cli.ZkCpTool;+import org.apache.solr.cli.ZkLsTool;+import org.apache.solr.cli.ZkMvTool;+import org.apache.solr.cli.ZkRmTool;import org.apache.solr.common.cloud.SolrZkClient;import org.apache.solr.common.cloud.ZkMaintenanceUtils;-import org.apache.solr.util.SolrCLI;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.data.Stat;import org.junit.AfterClass;@@ -93,12 +99,9 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {configSet.toAbsolutePath().toString(),};- SolrCLI.ConfigSetUploadTool tool = new SolrCLI.ConfigSetUploadTool();+ ConfigSetUploadTool tool = new ConfigSetUploadTool();- int res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ int res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertEquals("tool should have returned 0 for success ", 0, res);// Now do we have that config up on ZK?verifyZkLocalPathsMatch(srcPathCheck, "/configs/upconfig2");@@ -116,10 +119,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {configSet.toAbsolutePath().toString(),};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertTrue("tool should have returned non-zero for failure ", 0 != res);String content =@@ -153,11 +153,10 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {zkAddr,};- SolrCLI.ConfigSetDownloadTool downTool = new SolrCLI.ConfigSetDownloadTool();+ ConfigSetDownloadTool downTool = new ConfigSetDownloadTool();int res =downTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(downTool.getOptions()), args));+ SolrCLI.processCommandLineArgs(downTool.getName(), downTool.getOptions(), args));assertEquals("Download should have succeeded.", 0, res);verifyZkLocalPathsMatch(Paths.get(tmp.toAbsolutePath().toString(), "conf"), "/configs/downconfig1");@@ -170,7 +169,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {// Now copy it up and back and insure it's still a file in the new placeAbstractDistribZkTestBase.copyConfigUp(tmp.getParent(), "myconfset", "downconfig2", zkAddr);Path tmp2 = createTempDir("downConfigNewPlace2");- downTool = new SolrCLI.ConfigSetDownloadTool();+ downTool = new ConfigSetDownloadTool();args =new String[] {"-confname",@@ -183,8 +182,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {res =downTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(downTool.getOptions()), args));+ SolrCLI.processCommandLineArgs(downTool.getName(), downTool.getOptions(), args));assertEquals("Download should have succeeded.", 0, res);verifyZkLocalPathsMatch(Paths.get(tmp.toAbsolutePath().toString(), "conf"), "/configs/downconfig2");@@ -211,12 +209,10 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- SolrCLI.ZkCpTool cpTool = new SolrCLI.ZkCpTool();+ ZkCpTool cpTool = new ZkCpTool();int res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy from zk -> zk should have succeeded.", 0, res);verifyZnodesMatch("/configs/cp1", "/cp2");@@ -235,9 +231,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);verifyZkLocalPathsMatch(tmp, "/configs/cp1");@@ -256,9 +250,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);verifyZkLocalPathsMatch(tmp, "/configs/cp1");@@ -276,9 +268,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);verifyZkLocalPathsMatch(srcPathCheck, "/cp3");@@ -296,9 +286,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);verifyZkLocalPathsMatch(srcPathCheck, "/cp4");@@ -309,9 +297,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertTrue("Copy should NOT have succeeded, recurse not specified.", 0 != res);// try with recurse = false@@ -328,9 +314,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertTrue("Copy should NOT have succeeded, recurse set to false.", 0 != res);// NOTE: really can't test copying to '.' because the test framework doesn't allow altering the@@ -345,9 +329,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should nave created intermediate directory locally.", 0, res);assertTrue("File should have been copied to a directory successfully",@@ -368,9 +350,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy up to intermediate file should have succeeded.", 0, res);assertTrue("Should have created an intermediate node on ZK",@@ -391,9 +371,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy up to named file should have succeeded.", 0, res);assertTrue("Should NOT have created an intermediate node on ZK",@@ -417,9 +395,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy to local named file should have succeeded.", 0, res);Path locPath = Paths.get(localNamed);assertTrue("Should have found file: " + localNamed, Files.exists(locPath));@@ -444,9 +420,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy from somewhere in ZK to ZK root should have succeeded.", 0, res);assertTrue("Should have found znode /solrconfig.xml: ", zkClient.exists("/solrconfig.xml", true));@@ -466,9 +440,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);verifyZkLocalPathsMatch(srcPathCheck, "/cp7/" + srcPathCheck.getFileName().toString());@@ -492,9 +464,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);String content =@@ -503,9 +473,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {assertTrue("There should be content in the node! ", content.contains("{Some Arbitrary Data}"));res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);tmp = createTempDir("cp8");@@ -521,9 +489,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {zkAddr,};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);// Next, copy cp7 down and verify that zknode.data exists for cp7@@ -544,9 +510,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);content =@@ -571,9 +535,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);Path tmp2 = createTempDir("cp9");@@ -590,9 +552,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {zkAddr,};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);assertTrue("Empty files should NOT be copied down as directories", emptyDest.toFile().isFile());@@ -612,9 +572,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);// Now copy it all back and make sure empty file is still a file when recursively copying.@@ -631,9 +589,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {zkAddr,};res =- cpTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(cpTool.getOptions()), args));+ cpTool.runTool(SolrCLI.processCommandLineArgs(cpTool.getName(), cpTool.getOptions(), args));assertEquals("Copy should have succeeded.", 0, res);Path locEmpty = Paths.get(tmp2.toAbsolutePath().toString(), "stopwords", "emptyfile");@@ -658,12 +614,10 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- SolrCLI.ZkMvTool mvTool = new SolrCLI.ZkMvTool();+ ZkMvTool mvTool = new ZkMvTool();int res =- mvTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(mvTool.getOptions()), args));+ mvTool.runTool(SolrCLI.processCommandLineArgs(mvTool.getName(), mvTool.getOptions(), args));assertEquals("Move should have succeeded.", 0, res);// Now does the moved directory match the original on disk?@@ -680,9 +634,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {// Still in mv2res =- mvTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(mvTool.getOptions()), args));+ mvTool.runTool(SolrCLI.processCommandLineArgs(mvTool.getName(), mvTool.getOptions(), args));assertTrue("Move should NOT have succeeded with file: specified.", 0 != res);// Let's move it to yet another place with no zk: prefix.@@ -694,9 +646,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- mvTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(mvTool.getOptions()), args));+ mvTool.runTool(SolrCLI.processCommandLineArgs(mvTool.getName(), mvTool.getOptions(), args));assertEquals("Move should have succeeded.", 0, res);assertFalse("Znode /mv3 really should be gone", zkClient.exists("/mv3", true));@@ -712,9 +662,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- mvTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(mvTool.getOptions()), args));+ mvTool.runTool(SolrCLI.processCommandLineArgs(mvTool.getName(), mvTool.getOptions(), args));assertEquals("Move should have succeeded.", 0, res);assertTrue("Should be able to move a single file",@@ -731,9 +679,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};res =- mvTool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(mvTool.getOptions()), args));+ mvTool.runTool(SolrCLI.processCommandLineArgs(mvTool.getName(), mvTool.getOptions(), args));assertEquals("Move should have succeeded.", 0, res);assertTrue("Should be able to move a single file to a parent znode",@@ -760,13 +706,10 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {ByteArrayOutputStream baos = new ByteArrayOutputStream();PrintStream ps = new PrintStream(baos, false, StandardCharsets.UTF_8.name());- SolrCLI.ZkLsTool tool = new SolrCLI.ZkLsTool(ps);+ ZkLsTool tool = new ZkLsTool(ps);- int res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));- String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);+ int res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));+ String content = baos.toString(StandardCharsets.UTF_8);assertEquals("List should have succeeded", res, 0);assertTrue("Return should contain the conf directory", content.contains("lister"));@@ -780,11 +723,8 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));- content = new String(baos.toByteArray(), StandardCharsets.UTF_8);+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));+ content = baos.toString(StandardCharsets.UTF_8);assertEquals("List should have succeeded", res, 0);assertTrue("Return should contain the conf directory", content.contains("lister"));@@ -798,11 +738,8 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));- content = new String(baos.toByteArray(), StandardCharsets.UTF_8);+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));+ content = baos.toString(StandardCharsets.UTF_8);assertEquals("List should have succeeded", res, 0);assertTrue("Return should contain the conf directory", content.contains("lister"));@@ -816,11 +753,8 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));- content = new String(baos.toByteArray(), StandardCharsets.UTF_8);+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));+ content = baos.toString(StandardCharsets.UTF_8);assertEquals("List should have succeeded", res, 0);assertTrue("Return should contain the conf directory", content.contains("lister"));@@ -831,11 +765,8 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-path", "/", "-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));- content = new String(baos.toByteArray(), StandardCharsets.UTF_8);+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));+ content = baos.toString(StandardCharsets.UTF_8);assertEquals("List should have succeeded", res, 0);assertFalse("Return should not contain /zookeeper", content.contains("/zookeeper"));@@ -847,11 +778,8 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));- content = new String(baos.toByteArray(), StandardCharsets.UTF_8);+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));+ content = baos.toString(StandardCharsets.UTF_8);assertEquals("List should have succeeded", res, 0);assertTrue("Return should contain the conf directory", content.contains("lister"));@@ -873,12 +801,9 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-path", "/configs/rm1", "-zkHost", zkAddr,};- SolrCLI.ZkRmTool tool = new SolrCLI.ZkRmTool();+ ZkRmTool tool = new ZkRmTool();- int res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ int res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertTrue("Should have failed to remove node with children unless -recurse is set to true", res != 0);@@ -893,10 +818,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertTrue("Should have failed to remove node with children if -recurse is set to false", res != 0);@@ -908,10 +830,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertEquals("Should have removed node /configs/rm1", res, 0);assertFalse("Znode /configs/toremove really should be gone", zkClient.exists("/configs/rm1", true));@@ -924,10 +843,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {"-zkHost", zkAddr,};- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertEquals("Should have removed node /configs/rm2", res, 0);assertFalse("Znode /configs/toremove2 really should be gone", zkClient.exists("/configs/rm2", true));@@ -941,10 +857,7 @@ public class SolrCLIZkUtilsTest extends SolrCloudTestCase {};AbstractDistribZkTestBase.copyConfigUp(configSet, "cloud-subdirs", "rm3", zkAddr);- res =- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertNotEquals("Should fail when trying to remove /.", 0, res);}diff --git a/solr/core/src/test/org/apache/solr/cloud/SolrCloudExampleTest.java b/solr/core/src/test/org/apache/solr/cloud/SolrCloudExampleTest.javaindex c73c43c9413..bee0dd60723 100644--- a/solr/core/src/test/org/apache/solr/cloud/SolrCloudExampleTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/SolrCloudExampleTest.java@@ -33,6 +33,11 @@ import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;import java.util.stream.Stream;import org.apache.commons.cli.CommandLine;+import org.apache.solr.cli.ConfigTool;+import org.apache.solr.cli.CreateCollectionTool;+import org.apache.solr.cli.DeleteTool;+import org.apache.solr.cli.HealthcheckTool;+import org.apache.solr.cli.SolrCLI;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrRequest;@@ -45,7 +50,6 @@ import org.apache.solr.common.cloud.Slice;import org.apache.solr.common.cloud.ZkStateReader;import org.apache.solr.common.util.NamedList;import org.apache.solr.util.ExternalPaths;-import org.apache.solr.util.SolrCLI;import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -65,7 +69,6 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {}@Test- // 12-Jun-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 04-May-2018public void testLoadDocsIntoGettingStartedCollection() throws Exception {waitForThingsToLevelOut(30, TimeUnit.SECONDS);@@ -104,9 +107,8 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {// NOTE: not calling SolrCLI.main as the script does because it calls System.exit which is a// no-no in a JUnit test- SolrCLI.CreateCollectionTool tool = new SolrCLI.CreateCollectionTool();- CommandLine cli =- SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args);+ CreateCollectionTool tool = new CreateCollectionTool();+ CommandLine cli = SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args);log.info("Creating the '{}' collection using SolrCLI with: {}", testCollectionName, solrUrl);tool.runTool(cli);assertTrue(@@ -192,9 +194,8 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {"-collection", testCollectionName,"-zkHost", zkHost};- SolrCLI.HealthcheckTool tool = new SolrCLI.HealthcheckTool();- CommandLine cli =- SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args);+ HealthcheckTool tool = new HealthcheckTool();+ CommandLine cli = SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args);assertEquals("Healthcheck action failed!", 0, tool.runTool(cli));}@@ -204,9 +205,8 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {"-name", testCollectionName,"-solrUrl", solrUrl};- SolrCLI.DeleteTool tool = new SolrCLI.DeleteTool();- CommandLine cli =- SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args);+ DeleteTool tool = new DeleteTool();+ CommandLine cli = SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args);assertEquals("Delete action failed!", 0, tool.runTool(cli));assertFalse(SolrCLI.safeCheckCollectionExists(@@ -217,7 +217,6 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {* Uses the SolrCLI config action to activate soft auto-commits for the getting started* collection.*/- @SuppressWarnings("unchecked")protected void doTestConfigUpdate(String testCollectionName, String solrUrl) throws Exception {if (!solrUrl.endsWith("/")) solrUrl += "/";@@ -242,9 +241,8 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {Map<String, Integer> startTimes = getSoftAutocommitInterval(testCollectionName, solrClient);- SolrCLI.ConfigTool tool = new SolrCLI.ConfigTool();- CommandLine cli =- SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args);+ ConfigTool tool = new ConfigTool();+ CommandLine cli = SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args);log.info("Sending set-property '{}'={} to SolrCLI.ConfigTool.", prop, maxTime);assertEquals("Set config property failed!", 0, tool.runTool(cli));@@ -272,18 +270,18 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {// for all the cores have been done.boolean allGood = false;Map<String, Integer> curSoftCommitInterval = null;- for (int idx = 0; idx < 600 && allGood == false; ++idx) {+ for (int idx = 0; idx < 600 && !allGood; ++idx) {curSoftCommitInterval = getSoftAutocommitInterval(testCollectionName, solrClient);// no point in even trying if they're not the same size!if (curSoftCommitInterval.size() > 0 && curSoftCommitInterval.size() == startTimes.size()) {allGood = true;for (Map.Entry<String, Integer> currEntry : curSoftCommitInterval.entrySet()) {- if (currEntry.getValue().equals(maxTime) == false) {+ if (!currEntry.getValue().equals(maxTime)) {allGood = false;}}}- if (allGood == false) {+ if (!allGood) {Thread.sleep(100);}}@@ -292,7 +290,6 @@ public class SolrCloudExampleTest extends AbstractFullDistribZkTestBase {}// Collect all the autoSoftCommit intervals.- @SuppressWarnings("unchecked")private Map<String, Integer> getSoftAutocommitInterval(String collection, SolrClient solrClient)throws Exception {Map<String, Integer> ret = new HashMap<>();diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java b/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.javaindex b6fcb4bb6b3..376dc97a502 100644--- a/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java@@ -20,10 +20,10 @@ import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.cli.SimplePostTool;import org.apache.solr.common.cloud.ZkNodeProps;import org.apache.solr.common.util.JavaBinCodec;import org.apache.solr.common.util.Utils;-import org.apache.solr.util.SimplePostTool;import org.junit.Test;public class ZkNodePropsTest extends SolrTestCaseJ4 {diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.java b/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.javaindex 339bca8d122..14c9b2ffc9b 100644--- a/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.java+++ b/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.java@@ -37,6 +37,7 @@ import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import org.apache.commons.io.FileUtils;import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.cli.SimplePostTool;import org.apache.solr.client.solrj.impl.CloudSolrClient;import org.apache.solr.common.LinkedHashMapWriter;import org.apache.solr.common.MapWriter;@@ -52,7 +53,6 @@ import org.apache.solr.search.SolrCache;import org.apache.solr.util.RESTfulServerProvider;import org.apache.solr.util.RestTestBase;import org.apache.solr.util.RestTestHarness;-import org.apache.solr.util.SimplePostTool;import org.apache.solr.util.TimeOut;import org.eclipse.jetty.servlet.ServletHolder;import org.junit.After;diff --git a/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.java b/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.javaindex 8d4e8c68ad9..9916919d4e3 100644--- a/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.java+++ b/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.java@@ -32,6 +32,7 @@ import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ByteArrayEntity;import org.apache.http.util.EntityUtils;+import org.apache.solr.cli.SimplePostTool;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.CloudLegacySolrClient;@@ -44,7 +45,6 @@ import org.apache.solr.common.cloud.DocCollection;import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.util.StrUtils;import org.apache.solr.util.RTimer;-import org.apache.solr.util.SimplePostTool;import org.junit.Test;import org.noggit.JSONParser;import org.slf4j.Logger;diff --git a/solr/core/src/test/org/apache/solr/pkg/TestPackages.java b/solr/core/src/test/org/apache/solr/pkg/TestPackages.javaindex fc925364759..086cdf5a1d3 100644--- a/solr/core/src/test/org/apache/solr/pkg/TestPackages.java+++ b/solr/core/src/test/org/apache/solr/pkg/TestPackages.java@@ -41,6 +41,7 @@ import org.apache.lucene.analysis.core.WhitespaceTokenizerFactory;import org.apache.lucene.analysis.pattern.PatternReplaceCharFilterFactory;import org.apache.lucene.util.ResourceLoader;import org.apache.lucene.util.ResourceLoaderAware;+import org.apache.solr.cli.SimplePostTool;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrRequest;@@ -77,7 +78,6 @@ import org.apache.solr.search.QParser;import org.apache.solr.search.QParserPlugin;import org.apache.solr.security.AuthorizationContext;import org.apache.solr.util.LogLevel;-import org.apache.solr.util.SimplePostTool;import org.apache.solr.util.plugin.SolrCoreAware;import org.apache.zookeeper.data.Stat;import org.junit.After;diff --git a/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.java b/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.javaindex 8d68c590bc0..2e0db1c763c 100644--- a/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.java+++ b/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.java@@ -27,6 +27,7 @@ import org.apache.lucene.document.StoredField;import org.apache.lucene.document.TextField;import org.apache.lucene.util.BytesRef;import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.cli.SimplePostTool;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.params.CommonParams;@@ -38,7 +39,6 @@ import org.apache.solr.request.LocalSolrQueryRequest;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.BinaryResponseWriter.Resolver;import org.apache.solr.search.SolrReturnFields;-import org.apache.solr.util.SimplePostTool;import org.junit.BeforeClass;/**diff --git a/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.java b/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.javaindex bcc7d0d67d8..013f1577a24 100644--- a/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.java+++ b/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.java@@ -36,6 +36,8 @@ import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ByteArrayEntity;+import org.apache.solr.cli.SolrCLI;+import org.apache.solr.cli.StatusTool;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.client.solrj.SolrServerException;@@ -63,7 +65,6 @@ import org.apache.solr.common.util.TimeSource;import org.apache.solr.common.util.Utils;import org.apache.solr.embedded.JettySolrRunner;import org.apache.solr.util.LogLevel;-import org.apache.solr.util.SolrCLI;import org.apache.solr.util.TimeOut;import org.junit.After;import org.junit.Before;@@ -300,12 +301,10 @@ public class BasicAuthIntegrationTest extends SolrCloudAuthTestCase {String[] toolArgs = new String[] {"status", "-solr", baseUrl};ByteArrayOutputStream baos = new ByteArrayOutputStream();PrintStream stdoutSim = new PrintStream(baos, true, StandardCharsets.UTF_8.name());- SolrCLI.StatusTool tool = new SolrCLI.StatusTool(stdoutSim);+ StatusTool tool = new StatusTool(stdoutSim);try {System.setProperty("basicauth", "harry:HarryIsUberCool");- tool.runTool(- SolrCLI.processCommandLineArgs(- SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));+ tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), toolArgs));Map<?, ?> obj = (Map<?, ?>) Utils.fromJSON(new ByteArrayInputStream(baos.toByteArray()));assertTrue(obj.containsKey("version"));assertTrue(obj.containsKey("startTime"));diff --git a/solr/modules/hdfs/src/java/org/apache/solr/hdfs/snapshots/SolrSnapshotsTool.java b/solr/modules/hdfs/src/java/org/apache/solr/hdfs/snapshots/SolrSnapshotsTool.javaindex a73b3145647..5f903cbd932 100644--- a/solr/modules/hdfs/src/java/org/apache/solr/hdfs/snapshots/SolrSnapshotsTool.java+++ b/solr/modules/hdfs/src/java/org/apache/solr/hdfs/snapshots/SolrSnapshotsTool.java@@ -44,6 +44,7 @@ import org.apache.commons.cli.Options;import org.apache.commons.cli.ParseException;import org.apache.commons.cli.PosixParser;import org.apache.hadoop.fs.Path;+import org.apache.solr.cli.CLIO;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.CloudSolrClient;import org.apache.solr.client.solrj.request.CollectionAdminRequest;@@ -56,7 +57,6 @@ import org.apache.solr.common.util.NamedList;import org.apache.solr.core.snapshots.CollectionSnapshotMetaData;import org.apache.solr.core.snapshots.CollectionSnapshotMetaData.CoreSnapshotMetaData;import org.apache.solr.core.snapshots.SolrSnapshotManager;-import org.apache.solr.util.CLIO;import org.slf4j.Logger;import org.slf4j.LoggerFactory;diff --git a/solr/packaging/test/test_export.bats b/solr/packaging/test/test_export.batsindex e6f1d5fbde1..3e797b3107a 100644--- a/solr/packaging/test/test_export.bats+++ b/solr/packaging/test/test_export.bats@@ -29,7 +29,7 @@ teardown() {}@test "Check export command" {- run solr start -c -Dsolr.modules=sql+ run solr start -crun solr create_collection -c COLL_NAMErun solr export -url "http://localhost:8983/solr/COLL_NAME" -query "*:* -id:test" -out "${BATS_TEST_TMPDIR}/output"refute_output --partial 'Unrecognized option'diff --git a/solr/packaging/test/test_postlogs.bats b/solr/packaging/test/test_postlogs.batsnew file mode 100644index 00000000000..32f7c379a7c--- /dev/null+++ b/solr/packaging/test/test_postlogs.bats@@ -0,0 +1,51 @@+#!/usr/bin/env bats++# Licensed to the Apache Software Foundation (ASF) under one or more+# contributor license agreements. See the NOTICE file distributed with+# this work for additional information regarding copyright ownership.+# The ASF 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.++load bats_helper++setup_file() {+ common_clean_setup+ solr start -c+}++teardown_file() {+ common_setup+ solr stop -all+}++setup() {+ common_setup+}++teardown() {+ # save a snapshot of SOLR_HOME for failed tests+ save_home_on_failure++ delete_all_collections+}++@test "post solr log into solr" {+ run solr create_collection -c COLL_NAME+ assert_output --partial "Created collection 'COLL_NAME'"++ run postlogs http://localhost:8983/solr/COLL_NAME ${SOLR_LOGS_DIR}/solr.log+ assert_output --partial 'Sending last batch'+ assert_output --partial 'Committed'++ run curl 'http://localhost:8983/solr/COLL_NAME/select?q=*:*'+ refute_output --partial '"numFound":0'+}diff --git a/solr/solr-ref-guide/modules/deployment-guide/pages/docker-networking.adoc b/solr/solr-ref-guide/modules/deployment-guide/pages/docker-networking.adocindex 6c2acf88520..66b1b75446e 100644--- a/solr/solr-ref-guide/modules/deployment-guide/pages/docker-networking.adoc+++ b/solr/solr-ref-guide/modules/deployment-guide/pages/docker-networking.adoc@@ -205,7 +205,7 @@ To load data, and see it was split over shards:[source,bash,subs="attributes"]----docker exec -it --user=solr zksolr1 bin/post -c my_collection1 example/exampledocs/manufacturers.xml-# /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -classpath /opt/solr/server/lib/ext/*:/opt/solr/server/solr-webapp/webapp/WEB-INF/lib/* -Dauto=yes -Dc=my_collection1 -Ddata=files org.apache.solr.util.SimplePostTool example/exampledocs/manufacturers.xml+# /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -classpath /opt/solr/server/lib/ext/*:/opt/solr/server/solr-webapp/webapp/WEB-INF/lib/* -Dauto=yes -Dc=my_collection1 -Ddata=files org.apache.solr.cli.SimplePostTool example/exampledocs/manufacturers.xml# SimplePostTool version {solr-full-version}# Posting files to [base] url http://localhost:8983/solr/my_collection1/update...# Entering auto mode. File endings considered are xml,json,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,logdiff --git a/solr/solr-ref-guide/modules/getting-started/pages/tutorial-films.adoc b/solr/solr-ref-guide/modules/getting-started/pages/tutorial-films.adocindex 2a171d54c9f..0df21b0f71e 100644--- a/solr/solr-ref-guide/modules/getting-started/pages/tutorial-films.adoc+++ b/solr/solr-ref-guide/modules/getting-started/pages/tutorial-films.adoc@@ -310,7 +310,7 @@ Each command will produce output similar to the below seen while indexing JSON:[,console]----$ bin/post -c films example/films/films.json-# bin/java -classpath /solr-{solr-full-version}/server/solr-webapp/webapp/WEB-INF/lib/solr-core-{solr-full-version}.jar -Dauto=yes -Dc=films -Ddata=files org.apache.solr.util.SimplePostTool example/films/films.json+# bin/java -classpath /solr-{solr-full-version}/server/solr-webapp/webapp/WEB-INF/lib/solr-core-{solr-full-version}.jar -Dauto=yes -Dc=films -Ddata=files org.apache.solr.cli.SimplePostTool example/films/films.jsonSimplePostTool version 5.0.0Posting files to [base] url http://localhost:8983/solr/films/update...Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,logdiff --git a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractDistribZkTestBase.java b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractDistribZkTestBase.javaindex 9f904f11f8c..435a2a931ba 100644--- a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractDistribZkTestBase.java+++ b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractDistribZkTestBase.java@@ -28,6 +28,8 @@ import java.util.concurrent.TimeoutException;import java.util.concurrent.atomic.AtomicInteger;import org.apache.commons.io.FileUtils;import org.apache.solr.BaseDistributedSearchTestCase;+import org.apache.solr.cli.ConfigSetUploadTool;+import org.apache.solr.cli.SolrCLI;import org.apache.solr.client.solrj.impl.CloudSolrClient;import org.apache.solr.client.solrj.request.CollectionAdminRequest;import org.apache.solr.common.cloud.ClusterState;@@ -39,7 +41,6 @@ import org.apache.solr.common.cloud.ZkStateReader;import org.apache.solr.core.Diagnostics;import org.apache.solr.core.MockDirectoryFactory;import org.apache.solr.embedded.JettySolrRunner;-import org.apache.solr.util.SolrCLI;import org.apache.solr.util.TimeOut;import org.apache.zookeeper.KeeperException;import org.junit.BeforeClass;@@ -417,12 +418,9 @@ public abstract class AbstractDistribZkTestBase extends BaseDistributedSearchTes"-configsetsDir", configSetDir.toString(),};- SolrCLI.ConfigSetUploadTool tool = new SolrCLI.ConfigSetUploadTool();+ ConfigSetUploadTool tool = new ConfigSetUploadTool();- int res =- tool.runTool(- SolrCLI.processCommandLineArgs(- tool.getName(), SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));+ int res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), tool.getOptions(), args));assertEquals("Tool should have returned 0 for success, returned: " + res, res, 0);}}