onnode: Future-proof get_nodes_with_status().
authorMartin Schwenke <martin@meltin.net>
Tue, 17 May 2011 04:24:30 +0000 (14:24 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Wed, 8 Jun 2011 04:22:44 +0000 (14:22 +1000)
The current code requires knowledge of the number of status bits
output by "ctdb status -Y".

This changes the code to be completely general.

Signed-off-by: Martin Schwenke <martin@meltin.net>
tools/onnode

index 77a1207d6c23433f300a66f1caad3d42eaeb18d8..9adb5caf68a3d0ef59e1b571a309ef0225d6f04c 100755 (executable)
@@ -138,18 +138,6 @@ get_nodes_with_status ()
     local all_nodes="$1"
     local status="$2"
 
-    local bits
-    case "$status" in
-       healthy)
-           bits="0:0:0:0:0:0"
-           ;;
-       connected)
-           bits="0:[0-1]:[0-1]:[0-1]:[0-1]:[0-1]"
-           ;;
-       *)
-           invalid_nodespec
-    esac
-
     if [ -z "$ctdb_status_output" ] ; then
        # FIXME: need to do something if $CTDB_NODES_SOCKETS is set.
        ctdb_status_output=$(ctdb -Y status 2>/dev/null)
@@ -160,22 +148,35 @@ get_nodes_with_status ()
        ctdb_status_output="${ctdb_status_output#* }"
     fi
 
-    local nodes=""
-    local i
-    for i in $ctdb_status_output ; do
-       # Try removing bits from end.
-       local t="${i%:${bits}:}"
-       if [ "$t" != "$i" ] ; then
-           # Succeeded.  Get address.  NOTE: this is an optimisation.
-           # It might be better to get the node number and then get
-           # the nth node to get the address.  This would make things
-           # more consistent if $ctdb_base/nodes actually contained
-           # hostnames.
-           nodes="${nodes} ${t#:*:}"
-       fi
-    done
-
-    echo $nodes
+    (
+       local i
+       for i in $ctdb_status_output ; do
+
+           IFS="${IFS}:"
+           set -- $i
+           shift # line starts with : so 1st field is empty
+           local pnn="$1" ; shift
+           local ip="$1" ; shift
+
+           case "$status" in
+               healthy)
+                   # If any bit is not 0, don't match this address.
+                   local s
+                   for s ; do
+                       [ "$s" = "0" ] || continue 2
+                   done
+                   ;;
+               connected)
+                   # If disconnected bit is not 0, don't match this address.
+                   [ "$1" = "0" ] || continue
+                   ;;
+               *)
+                   invalid_nodespec
+           esac
+           
+           echo_nth "$pnn" $all_nodes
+       done
+    )
 }
 
 ctdb_props="" # cache