You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by th...@apache.org on 2021/12/03 16:32:32 UTC

[solr] branch main updated: SOLR-15199: Pass thru all command actions not explicitly handled by the bin/solr script, such as 'api', to the SolrCLI Java app (#441)

This is an automated email from the ASF dual-hosted git repository.

thelabdude pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new b144f73  SOLR-15199: Pass thru all command actions not explicitly handled by the bin/solr script, such as 'api', to the SolrCLI Java app (#441)
b144f73 is described below

commit b144f7341837b3b1f44af968e663c9345dd8b3b4
Author: Timothy Potter <th...@gmail.com>
AuthorDate: Fri Dec 3 09:32:26 2021 -0700

    SOLR-15199: Pass thru all command actions not explicitly handled by the bin/solr script, such as 'api', to the SolrCLI Java app (#441)
---
 solr/CHANGES.txt                                   |  2 +
 solr/bin/solr                                      |  7 ++--
 solr/bin/solr.cmd                                  | 31 ++++------------
 .../src/java/org/apache/solr/util/SolrCLI.java     | 43 ++++++++++++++++------
 .../test/org/apache/solr/util/AuthToolTest.java    |  2 +-
 .../solr/cloud/AbstractDistribZkTestBase.java      |  2 +-
 6 files changed, 47 insertions(+), 40 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 8820b02..c6c68d8 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -464,6 +464,8 @@ Bug Fixes
 * SOLR-15828: AuthTool (in SolrCLI) should include the config-read, collection-admin-read, core-admin-read, and all permissions in the initial security.json
   to avoid warnings in the security UI (Timothy Potter)
 
+* SOLR-15199: Pass thru all command actions not explicitly handled by the bin/solr script, such as 'api', to the SolrCLI Java app (Timothy Potter)
+
 ==================  8.11.0 ==================
 
 Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
diff --git a/solr/bin/solr b/solr/bin/solr
index 20b337b..4d609ad 100755
--- a/solr/bin/solr
+++ b/solr/bin/solr
@@ -334,7 +334,7 @@ function print_usage() {
   if [ -z "$CMD" ]; then
     echo ""
     echo "Usage: solr COMMAND OPTIONS"
-    echo "       where COMMAND is one of: start, stop, restart, status, healthcheck, create, create_core, create_collection, delete, version, zk, auth, assert, config, export"
+    echo "       where COMMAND is one of: start, stop, restart, status, healthcheck, create, create_core, create_collection, delete, version, zk, auth, assert, config, export, api, package"
     echo ""
     echo "  Standalone server example (start Solr running in the background on port 8984):"
     echo ""
@@ -1575,8 +1575,9 @@ fi
 
 # verify the command given is supported
 if [ "$SCRIPT_CMD" != "stop" ] && [ "$SCRIPT_CMD" != "start" ] && [ "$SCRIPT_CMD" != "restart" ] && [ "$SCRIPT_CMD" != "status" ] && [ "$SCRIPT_CMD" != "assert" ]; then
-  print_usage "" "$SCRIPT_CMD is not a valid command!"
-  exit 1
+  # handoff this command to the SolrCLI and let it handle the option parsing and validation
+  run_tool $SCRIPT_CMD "$@"
+  exit $?
 fi
 
 #Check current Ulimits for Open Files and Max Processes.  Warn if they are below the recommended values.
diff --git a/solr/bin/solr.cmd b/solr/bin/solr.cmd
index bcd5eec..2526f28 100755
--- a/solr/bin/solr.cmd
+++ b/solr/bin/solr.cmd
@@ -228,9 +228,10 @@ IF "%1"=="status" goto get_info
 IF "%1"=="version" goto get_version
 IF "%1"=="-v" goto get_version
 IF "%1"=="-version" goto get_version
-IF "%1"=="assert" goto run_assert
-IF "%1"=="export" goto run_export
-IF "%1"=="package" goto run_package
+IF "%1"=="assert" goto run_solrcli
+IF "%1"=="export" goto run_solrcli
+IF "%1"=="package" goto run_solrcli
+IF "%1"=="api" goto run_solrcli
 
 REM Only allow the command to be the first argument, assume start if not supplied
 IF "%1"=="start" goto set_script_cmd
@@ -307,7 +308,7 @@ goto done
 :script_usage
 @echo.
 @echo Usage: solr COMMAND OPTIONS
-@echo        where COMMAND is one of: start, stop, restart, healthcheck, create, create_core, create_collection, delete, version, zk, auth, assert, config, export
+@echo        where COMMAND is one of: start, stop, restart, status, healthcheck, create, create_core, create_collection, delete, version, zk, auth, assert, config, export, api, package
 @echo.
 @echo   Standalone server example (start Solr running in the background on port 8984):
 @echo.
@@ -1459,32 +1460,16 @@ echo ZK_HOST: !ZK_HOST!
   org.apache.solr.util.SolrCLI healthcheck -collection !HEALTHCHECK_COLLECTION! -zkHost !ZK_HOST! %HEALTHCHECK_VERBOSE%
 goto done
 
-:run_assert
+: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.util.SolrCLI %*
 if errorlevel 1 (
    exit /b 1
 )
 goto done
 
-:run_export
-"%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 %*
-goto done:
-
-:run_package
-REM TODO: Compute the running Solr URL and populate it as a parameter (as has been done for the shell script)
-REM Without that, users will have to supply -solrUrl parameter in every request. Life can be so hard for Windows users!
-"%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 %*
-goto done:
-
 :parse_config_args
 IF [%1]==[] goto run_config
 IF "%1"=="-z" goto set_config_zk
@@ -2049,10 +2034,8 @@ FOR /f "usebackq tokens=3" %%a IN (`^""%JAVA%" -version 2^>^&1 ^| findstr "versi
   REM Remove surrounding quotes
   set JAVA_VERSION_INFO=!JAVA_VERSION_INFO:"=!
 
-  echo "java version info is !JAVA_VERSION_INFO!"
   REM Extract the major Java version, e.g. 7, 8, 9, 10 ...
   for /f "tokens=1,2 delims=._-" %%a in ("!JAVA_VERSION_INFO!") do (
-    echo "Extracted major version is %%a"
     if %%a GEQ 9 (
       set JAVA_MAJOR_VERSION=%%a
     ) else (
diff --git a/solr/core/src/java/org/apache/solr/util/SolrCLI.java b/solr/core/src/java/org/apache/solr/util/SolrCLI.java
index 5ef7618..bd83ffa 100755
--- a/solr/core/src/java/org/apache/solr/util/SolrCLI.java
+++ b/solr/core/src/java/org/apache/solr/util/SolrCLI.java
@@ -274,8 +274,14 @@ public class SolrCLI implements CLIO {
 
     SSLConfigurationsFactory.current().init();
 
-    Tool tool = findTool(args);
-    CommandLine cli = parseCmdLine(args, tool.getOptions());
+    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));
   }
 
@@ -284,7 +290,15 @@ public class SolrCLI implements CLIO {
     return newTool(toolType);
   }
 
+  /**
+   * @deprecated Use the method that takes a tool name as the first argument instead.
+   */
+  @Deprecated
   public static CommandLine parseCmdLine(String[] args, Option[] toolOptions) throws Exception {
+    return parseCmdLine(SolrCLI.class.getName(), args, toolOptions);
+  }
+
+  public static CommandLine parseCmdLine(String toolName, String[] args, Option[] toolOptions) throws Exception {
     // the parser doesn't like -D props
     List<String> toolArgList = new ArrayList<String>();
     List<String> dashDList = new ArrayList<String>();
@@ -300,7 +314,7 @@ public class SolrCLI implements CLIO {
 
     // process command-line args to configure this application
     CommandLine cli =
-        processCommandLineArgs(joinCommonAndToolOptions(toolOptions), toolArgs);
+        processCommandLineArgs(toolName, joinCommonAndToolOptions(toolOptions), toolArgs);
 
     List<String> argList = cli.getArgList();
     argList.addAll(dashDList);
@@ -400,7 +414,7 @@ public class SolrCLI implements CLIO {
         return tool;
     }
 
-    throw new IllegalArgumentException(toolType + " not supported!");
+    throw new IllegalArgumentException(toolType + " is not a valid command!");
   }
 
   private static void displayToolOptions() throws Exception {
@@ -459,11 +473,18 @@ public class SolrCLI implements CLIO {
     return options.toArray(new Option[0]);
   }
 
+  /**
+   * @deprecated Use the method that takes a tool name as the first argument instead.
+   */
+  @Deprecated
+  public static CommandLine processCommandLineArgs(Option[] customOptions, String[] args) {
+    return processCommandLineArgs(SolrCLI.class.getName(), customOptions, args);
+  }
 
   /**
    * Parses the command-line arguments passed by the user.
    */
-  public static CommandLine processCommandLineArgs(Option[] customOptions, String[] args) {
+  public static CommandLine processCommandLineArgs(String toolName, Option[] customOptions, String[] args) {
     Options options = new Options();
 
     options.addOption("help", false, "Print this message");
@@ -492,13 +513,13 @@ public class SolrCLI implements CLIO {
             + exp.getMessage());
       }
       HelpFormatter formatter = new HelpFormatter();
-      formatter.printHelp(SolrCLI.class.getName(), options);
+      formatter.printHelp(toolName, options);
       exit(1);
     }
 
     if (cli.hasOption("help")) {
       HelpFormatter formatter = new HelpFormatter();
-      formatter.printHelp(SolrCLI.class.getName(), options);
+      formatter.printHelp(toolName, options);
       exit(0);
     }
 
@@ -995,7 +1016,7 @@ public class SolrCLI implements CLIO {
           Option.builder("get")
               .argName("URL")
               .hasArg()
-              .required(false)
+              .required(true)
               .desc("Send a GET request to a Solr API endpoint.")
               .build()
       };
@@ -2807,7 +2828,7 @@ public class SolrCLI implements CLIO {
         };
         CreateTool createTool = new CreateTool(stdout);
         int createCode =
-            createTool.runTool(processCommandLineArgs(joinCommonAndToolOptions(createTool.getOptions()), createArgs));
+            createTool.runTool(processCommandLineArgs(createTool.getName(), joinCommonAndToolOptions(createTool.getOptions()), createArgs));
         if (createCode != 0)
           throw new Exception("Failed to create "+collectionName+" using command: "+ Arrays.asList(createArgs));
       }
@@ -2945,7 +2966,7 @@ public class SolrCLI implements CLIO {
 
       // let's not fail if we get this far ... just report error and finish up
       try {
-        configTool.runTool(processCommandLineArgs(joinCommonAndToolOptions(configTool.getOptions()), configArgs));
+        configTool.runTool(processCommandLineArgs(configTool.getName(), joinCommonAndToolOptions(configTool.getOptions()), configArgs));
       } catch (Exception exc) {
         CLIO.err("Failed to update '"+propName+"' property due to: "+exc);
       }
@@ -3203,7 +3224,7 @@ public class SolrCLI implements CLIO {
       CreateCollectionTool createCollectionTool = new CreateCollectionTool(stdout);
       int createCode =
           createCollectionTool.runTool(
-              processCommandLineArgs(joinCommonAndToolOptions(createCollectionTool.getOptions()), createArgs));
+              processCommandLineArgs(createCollectionTool.getName(), joinCommonAndToolOptions(createCollectionTool.getOptions()), createArgs));
 
       if (createCode != 0)
         throw new Exception("Failed to create collection using command: "+ Arrays.asList(createArgs));
diff --git a/solr/core/src/test/org/apache/solr/util/AuthToolTest.java b/solr/core/src/test/org/apache/solr/util/AuthToolTest.java
index b35dc12..81a79c5 100644
--- a/solr/core/src/test/org/apache/solr/util/AuthToolTest.java
+++ b/solr/core/src/test/org/apache/solr/util/AuthToolTest.java
@@ -73,7 +73,7 @@ public class AuthToolTest extends SolrCloudTestCase {
   private int runTool(String[] args) throws Exception {
     SolrCLI.Tool tool = findTool(args);
     assertTrue(tool instanceof SolrCLI.AuthTool);
-    CommandLine cli = parseCmdLine(args, tool.getOptions());
+    CommandLine cli = parseCmdLine(tool.getName(), args, tool.getOptions());
     return tool.runTool(cli);
   }
 }
diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractDistribZkTestBase.java b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractDistribZkTestBase.java
index 1a5bf98..cdb975c 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
@@ -347,7 +347,7 @@ public abstract class AbstractDistribZkTestBase extends BaseDistributedSearchTes
 
     SolrCLI.ConfigSetUploadTool tool = new SolrCLI.ConfigSetUploadTool();
 
-    int res = tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));
+    int res = tool.runTool(SolrCLI.processCommandLineArgs(tool.getName(), SolrCLI.joinCommonAndToolOptions(tool.getOptions()), args));
     assertEquals("Tool should have returned 0 for success, returned: " + res, res, 0);
 
   }