LP#1204123 osrf_control warn and exit on bad host
authorBill Erickson <berick@esilibrary.com>
Fri, 30 Aug 2013 18:17:02 +0000 (14:17 -0400)
committerJason Stephenson <jstephenson@mvlc.org>
Wed, 4 Sep 2013 15:07:59 +0000 (11:07 -0400)
Any time a start action is issued (start, start_all, restart,
restart_all), osrf_control will first verify that the selected hostname
(or localhost) hosts the requested service(s).  If not, the user is
warned and the script exits before any action is taken.

Note: we do not make a similar verification for stop and signal actions,
since those may be reasonably be used even when a service is not
configured to run on the selected host.

Thanks to Jason Stephenson for the suggestion.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>

bin/opensrf-perl.pl.in

index 5c161a2..f9ab8ca 100755 (executable)
@@ -94,6 +94,20 @@ if ($opt_localhost) {
 my $C_COMMAND = "opensrf-c -c $opt_config -x opensrf -p $opt_pid_dir -h $hostname";
 my $PY_COMMAND = "opensrf.py -f $opt_config -p $opt_pid_dir ". ($opt_localhost ? '-l' : '');
 
+sub verify_services {
+    my $service = shift;
+    my @services = (@perl_services, map {$_->{service}} @nonperl_services);
+    if (@services) {
+        return 1 unless $service;
+        return 1 if grep { $_ eq $service } @services;
+        msg("$service is not configured to run on $hostname");
+    } else {
+        msg("No services are configured to run on $hostname");
+    }
+    msg("Perhaps you meant to use --localhost?") unless $opt_localhost;
+    exit;
+}
+
 sub do_signal_send {
     my $service = shift;
     my $signal = shift;
@@ -371,8 +385,8 @@ sub do_start {
         }
     }
 
-    msg("$service is not configured to run on $hostname");
-    return 1;
+    # should not get here
+    return 0;
 }
 
 sub do_start_all {
@@ -667,11 +681,20 @@ HELP
 exit;
 }
 
-# starting services
-do_init() and do_start($opt_service) if $opt_start;
-do_init() and do_stop($opt_service) and do_start($opt_service) if $opt_restart;
-do_init() and do_start_all() if $opt_start_all;
-do_init() and do_stop_all() and do_start_all() if $opt_restart_all;
+# we do not verify services for stop/signal actions, since those may
+# legitimately be used against services not (or no longer) configured
+# to run on the selected host.
+do_init() and verify_services($opt_service) if 
+    $opt_start or 
+    $opt_start_all or
+    $opt_restart or
+    $opt_restart_all;
+
+# starting services.  do_init() handled above
+do_start($opt_service) if $opt_start;
+do_stop($opt_service) and do_start($opt_service) if $opt_restart;
+do_start_all() if $opt_start_all;
+do_stop_all() and do_start_all() if $opt_restart_all;
 
 # stopping services
 do_stop($opt_service) if $opt_stop;