You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ho...@apache.org on 2023/08/18 20:21:05 UTC

[solr] branch main updated: SOLR-16941: Use smarter default Solr URL in CLI (#1849)

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

houston 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 7f2f6dc6f2d SOLR-16941: Use smarter default Solr URL in CLI (#1849)
7f2f6dc6f2d is described below

commit 7f2f6dc6f2d38cde3211a89f8859add791d74060
Author: Houston Putman <ho...@apache.org>
AuthorDate: Fri Aug 18 16:21:00 2023 -0400

    SOLR-16941: Use smarter default Solr URL in CLI (#1849)
---
 solr/CHANGES.txt                                   |  2 ++
 solr/bin/solr                                      | 22 ++++++++++------
 .../src/java/org/apache/solr/cli/CreateTool.java   |  4 +--
 .../core/src/java/org/apache/solr/cli/SolrCLI.java | 30 +++++++++++++++++-----
 solr/packaging/test/test_ssl.bats                  |  4 +--
 5 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 098080ed7c2..90544c4c05a 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -97,6 +97,8 @@ Improvements
   This is useful when using the ZkCli on a remote Solr using the embedded ZK, or Solr running in a Docker container.
   The SOLR_ZK_EMBEDDED_HOST envVar or -Dsolr.zk.embedded.host sysProp control this bind address. (Houston Putman)
 
+* SOLR-16941: The SolrCLI now uses a smarter default for the Solr URL if none is provided, using the same envVars used when running Solr. (Houston Putman)
+
 Optimizations
 ---------------------
 
diff --git a/solr/bin/solr b/solr/bin/solr
index bae4b722432..7ee0fa3c50f 100644
--- a/solr/bin/solr
+++ b/solr/bin/solr
@@ -147,6 +147,11 @@ fi
 : "${SOLR_STOP_WAIT:=180}"
 : "${SOLR_START_WAIT:=$SOLR_STOP_WAIT}" # defaulting to $SOLR_STOP_WAIT for backwards compatibility reasons
 
+# Store whether a solr port was explicitly provided, for the "solr stop" command.
+PROVIDED_SOLR_PORT="${SOLR_PORT:-}"
+: "${SOLR_PORT:=8983}"
+export SOLR_PORT
+
 # test that Java exists, is executable and correct version
 JAVA_VER=$("$JAVA" -version 2>&1)
 # shellcheck disable=SC2181
@@ -278,6 +283,7 @@ if [ "$SOLR_SSL_ENABLED" == "true" ]; then
 else
   SOLR_JETTY_CONFIG+=("--module=http")
 fi
+export SOLR_URL_SCHEME
 
 # Requestlog options
 if [ "${SOLR_REQUESTLOG_ENABLED:-true}" == "true" ]; then
@@ -330,6 +336,7 @@ AUTHC_OPTS="${AUTHC_OPTS:-}${SOLR_AUTHENTICATION_OPTS:+ $SOLR_AUTHENTICATION_OPT
 
 # Set the SOLR_TOOL_HOST variable for use when connecting to a running Solr instance
 SOLR_TOOL_HOST="${SOLR_HOST:-localhost}"
+export SOLR_TOOL_HOST
 
 function print_usage() {
   CMD="${1:-}"
@@ -547,7 +554,7 @@ function print_usage() {
     echo ""
   elif [ "$CMD" == "package" ]; then
     echo ""
-    run_tool package -solrUrl "http://fake.to.allow.help.to.run" "help"
+    run_tool package "help"
   fi
 } # end print_usage
 
@@ -1436,8 +1443,8 @@ fi
 
 : "${STOP_KEY:=solrrocks}"
 
-# stop all if no port specified
-if [[ "$SCRIPT_CMD" == "stop" && -z "${SOLR_PORT:-}" ]]; then
+# stop all if no port specified or "all" is used
+if [[ "$SCRIPT_CMD" == "stop" ]]; then
   if $stop_all; then
     none_stopped=true
     find "$SOLR_PID_DIR" -name "solr-*.pid" -type f | while read PIDF
@@ -1455,8 +1462,9 @@ if [[ "$SCRIPT_CMD" == "stop" && -z "${SOLR_PORT:-}" ]]; then
     # if $none_stopped; then
     #   echo -e "\nNo Solr nodes found to stop.\n"
     # fi
-  else
-    # not stopping all and don't have a port, but if we can find the pid file for the default port 8983, then use that
+    exit
+  elif [[ -z "${PROVIDED_SOLR_PORT:-}" ]]; then
+    # not stopping all and don't have a port, but if we can find a single pid file for Solr, then use that
     none_stopped=true
     numSolrs=$(find "$SOLR_PID_DIR" -name "solr-*.pid" -type f | wc -l | tr -d ' ')
     if [ "$numSolrs" -eq 1 ]; then
@@ -1480,12 +1488,10 @@ if [[ "$SCRIPT_CMD" == "stop" && -z "${SOLR_PORT:-}" ]]; then
       fi
       exit 1
     fi
+    exit
   fi
-  exit
 fi
 
-: "${SOLR_PORT:=8983}"
-
 if [ -n "${SOLR_PORT_ADVERTISE:-}" ]; then
   SOLR_OPTS+=("-Dsolr.port.advertise=$SOLR_PORT_ADVERTISE")
 fi
diff --git a/solr/core/src/java/org/apache/solr/cli/CreateTool.java b/solr/core/src/java/org/apache/solr/cli/CreateTool.java
index 9d2f6315469..91a2214eeb4 100644
--- a/solr/core/src/java/org/apache/solr/cli/CreateTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/CreateTool.java
@@ -130,7 +130,7 @@ public class CreateTool extends ToolBase {
 
   protected void createCore(CommandLine cli, SolrClient solrClient) throws Exception {
     String coreName = cli.getOptionValue("name");
-    String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.DEFAULT_SOLR_URL);
+    String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.getDefaultSolrUrl());
 
     final String solrInstallDir = System.getProperty("solr.install.dir");
     final String confDirName = cli.getOptionValue("confdir", SolrCLI.DEFAULT_CONFIG_SET);
@@ -343,7 +343,7 @@ public class CreateTool extends ToolBase {
     if (confDirectoryName.equals("_default")
         && (confName.equals("") || confName.equals("_default"))) {
       final String collectionName = cli.getOptionValue("collection");
-      final String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.DEFAULT_SOLR_URL);
+      final String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.getDefaultSolrUrl());
       final String curlCommand =
           String.format(
               Locale.ROOT,
diff --git a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
index c3ddd46b85b..0459892eeea 100755
--- a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
+++ b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
@@ -75,7 +75,7 @@ public class SolrCLI implements CLIO {
       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";
+
   public static final String ZK_HOST = "localhost:9983";
 
   public static final Option OPTION_ZKHOST =
@@ -97,13 +97,13 @@ public class SolrCLI implements CLIO {
           .required(false)
           .desc(
               "Base Solr URL, which can be used to determine the zkHost if that's not known; defaults to: "
-                  + DEFAULT_SOLR_URL
+                  + getDefaultSolrUrl()
                   + '.')
           .build();
   public static final Option OPTION_VERBOSE =
       Option.builder("verbose").required(false).desc("Enable more verbose command output.").build();
 
-  // should this be boolean or just a otption?
+  // should this be boolean or just an option?
   public static final Option OPTION_RECURSE =
       Option.builder("r")
           .longOpt("recurse")
@@ -187,6 +187,22 @@ public class SolrCLI implements CLIO {
     return cli;
   }
 
+  public static String getDefaultSolrUrl() {
+    String scheme = System.getenv("SOLR_URL_SCHEME");
+    if (scheme == null) {
+      scheme = "http";
+    }
+    String host = System.getenv("SOLR_TOOL_HOST");
+    if (host == null) {
+      host = "localhost";
+    }
+    String port = System.getenv("SOLR_PORT");
+    if (port == null) {
+      port = "8983";
+    }
+    return String.format(Locale.ROOT, "%s://%s:%s", scheme.toLowerCase(Locale.ROOT), host, port);
+  }
+
   protected static void checkSslStoreSysProp(String solrInstallDir, String key) {
     String sysProp = "javax.net.ssl." + key;
     String keyStore = System.getProperty(sysProp);
@@ -488,11 +504,11 @@ public class SolrCLI implements CLIO {
     if (solrUrl == null) {
       String zkHost = cli.getOptionValue("zkHost");
       if (zkHost == null) {
-        solrUrl = DEFAULT_SOLR_URL;
+        solrUrl = SolrCLI.getDefaultSolrUrl();
         CLIO.getOutStream()
             .println(
                 "Neither -zkHost or -solrUrl parameters provided so assuming solrUrl is "
-                    + DEFAULT_SOLR_URL
+                    + solrUrl
                     + ".");
       } else {
 
@@ -526,11 +542,11 @@ public class SolrCLI implements CLIO {
 
     String solrUrl = cli.getOptionValue("solrUrl");
     if (solrUrl == null) {
-      solrUrl = DEFAULT_SOLR_URL;
+      solrUrl = getDefaultSolrUrl();
       CLIO.getOutStream()
           .println(
               "Neither -zkHost or -solrUrl parameters provided so assuming solrUrl is "
-                  + DEFAULT_SOLR_URL
+                  + solrUrl
                   + ".");
     }
     solrUrl = normalizeSolrUrl(solrUrl);
diff --git a/solr/packaging/test/test_ssl.bats b/solr/packaging/test/test_ssl.bats
index 3e53a7c6b29..9f3fdfe806c 100644
--- a/solr/packaging/test/test_ssl.bats
+++ b/solr/packaging/test/test_ssl.bats
@@ -54,8 +54,8 @@ teardown() {
   solr start -c
   solr assert --started https://localhost:8983/solr --timeout 5000
 
-  run curl --cacert "$ssl_dir/solr-ssl.pem" 'https://localhost:8983/solr/admin/collections?action=CREATE&collection.configName=_default&name=test&numShards=2&replicationFactor=1&router.name=compositeId&wt=json'
-  assert_output --partial '"status":0'
+  run solr create -c test -s 2
+  assert_output --partial "Created collection 'test'"
 
   run curl --http2 --cacert "$ssl_dir/solr-ssl.pem" 'https://localhost:8983/solr/test/select?q=*:*'
   assert_output --partial '"numFound":0'