Add guess (-g) to osrf_ctl.sh
authorThomas Berezansky <tsbere@mvlc.org>
Wed, 5 Oct 2011 14:33:08 +0000 (10:33 -0400)
committerThomas Berezansky <tsbere@mvlc.org>
Wed, 5 Oct 2011 14:33:08 +0000 (10:33 -0400)
Attempts to check config file for hostname and localhost blocks.

If it finds localhost but not hostname it enables -l automatically.

Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
bin/osrf_ctl.sh.in

index 23a3fc7..20c549b 100755 (executable)
@@ -21,6 +21,7 @@ OPT_ACTION=""
 OPT_CONFIG=""
 OPT_PID_DIR=""
 OSRF_HOSTNAME=""
+OSRF_AUTO_HOST=""
 
 # ---------------------------------------------------------------------------
 # Make sure we're running as the correct user
@@ -40,6 +41,7 @@ Optional parameters:";
   -c    full path to C configuration file (opensrf_core.xml)
   -d    store PID files in this directory
   -l    accept 'localhost' as the fully-qualified domain name
+  -g    guess for use of 'localhost' based on configuration
 
 Actions include:
     start_router
@@ -73,12 +75,13 @@ EOF
 # ---------------------------------------------------------------------------
 # Load the command line options and set the global vars
 # ---------------------------------------------------------------------------
-while getopts  "a:d:c:lh" flag; do
+while getopts  "a:d:c:lgh" flag; do
        case $flag in   
                "a")            OPT_ACTION="$OPTARG";;
                "c")            OPT_CONFIG="$OPTARG";;
                "d")            OPT_PID_DIR="$OPTARG";;
                "l")            export OSRF_HOSTNAME="localhost";;
+               "g")            OSRF_AUTO_HOST="YES";;
                "h"|*)  usage;;
        esac;
 done
@@ -91,6 +94,26 @@ if [ ! -r "$OPT_CONFIG" ]; then
        echo "Please specify the location of the opensrf_core.xml file using the -c flag";
        exit 1;
 fi;
+
+if [ "_$OSRF_HOSTNAME" == "_" -a "$OSRF_AUTO_HOST" == "YES" ]; then
+       temp_settings_file=$(xpath -q -e '/config/opensrf/settings_config/text()' $OPT_CONFIG 2>/dev/null || echo '')
+       if [ -n "$temp_settings_file" -a -r "$temp_settings_file" ]; then
+               temp_host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()')
+               temp_host_block=$(xpath -q -e "/opensrf/hosts/$temp_host" $temp_settings_file 2>/dev/null || echo '')
+               temp_localhost_block=$(xpath -q -e '/opensrf/hosts/localhost' $temp_settings_file 2>/dev/null || echo '')
+               if [ -z "$temp_host_block" -a -n "$temp_localhost_block" ]; then
+                       echo "Guess: Found no $temp_host config, but found localhost. Using localhost."
+                       export OSRF_HOSTNAME="localhost"
+               elif [ -z "$temp_host_block$temp_localhost_block" ]; then
+                       echo "Found no host config and no localhost config."
+                       exit 1;
+               fi
+       else
+               echo "Guess: Could not read $OPT_CONFIG. Maybe missing xpath?"
+               exit 1;
+       fi
+fi
+
 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run/opensrf;
 [ -z "$OPT_ACTION" ] && usage;