Remove internal use of vircmd
authorMartin Schwenke <martin@meltin.net>
Mon, 16 Mar 2015 04:21:19 +0000 (15:21 +1100)
committerAmitay Isaacs <amitay@gmail.com>
Mon, 16 Mar 2015 04:49:59 +0000 (15:49 +1100)
It is too easy to clobber/interact with domains that are no specified
in the cluster configuration.

Replace use of vircmd with new function virsh_cluster().  The only
possible loss here is that unmatched nodes may not be destroyed, so
duplicate IP addresses might occur when cluster configuration changes.
To try to avoid this a warning is printed by virsh_cluster() when
domains that are not part of the cluster are found to match
CLUSTER/CLUSTER_PATTERN.

Signed-off-by: Martin Schwenke <martin@meltin.net>
autocluster

index 60d2e97cd621ddb090e3d3165a43c1631a7eba67..dafc6058878aaa7d8c896e8b3cfb875e108acefe 100755 (executable)
@@ -183,6 +183,52 @@ hack_all_nodes_with ()
     NODES="$nodes"
 }
 
+list_all_cluster_nodes ()
+{
+    # Local function only defined in subshell
+    (
+       print_node_name ()
+       {
+           echo "$3"
+       }
+       for_each_node print_node_name
+    ) | sort
+}
+
+list_all_virsh_domains ()
+{
+    local pattern="${CLUSTER_PATTERN:-${CLUSTER}[a-z]*[0-9]}"
+
+    local domains=$(virsh list --all | awk '{print $2}' | tail -n +3)
+    local d
+    for d in $domains ; do
+       case "$d" in
+           ($pattern) echo "$d" ;;
+       esac
+    done | sort
+}
+
+virsh_cluster ()
+{
+    local command="$1"
+
+    local nodes=$(list_all_cluster_nodes)
+    local domains=$(list_all_virsh_domains)
+
+    if [ "$nodes" != "$domains" ] ; then
+       echo "WARNING: Found matching virsh domains that are not part of this cluster!"
+       echo
+    fi
+
+    local ret=0
+    local n
+    for n in $nodes ; do
+       virsh "$command" "$n" 2>&1 || ret=$?
+    done
+
+    return $ret
+}
+
 register_hook ()
 {
     local hook_var="$1"
@@ -547,7 +593,7 @@ cluster_destroy ()
     announce "cluster destroy \"${CLUSTER}\""
     [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
 
-    vircmd destroy "$CLUSTER" || true
+    virsh_cluster destroy || true
 }
 
 cluster_undefine ()
@@ -555,7 +601,7 @@ cluster_undefine ()
     announce "cluster undefine \"${CLUSTER}\""
     [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
 
-    vircmd undefine "$CLUSTER" || true
+    virsh_cluster undefine || true
 }
 
 cluster_update_hosts ()
@@ -603,14 +649,12 @@ cluster_update_hosts ()
 
 cluster_boot ()
 {
-    [ -n "$CLUSTER_PATTERN" ] || CLUSTER_PATTERN="$CLUSTER"
-    announce "cluster boot \"${CLUSTER_PATTERN}\""
-    [ -n "$CLUSTER_PATTERN" ] || die "\$CLUSTER_PATTERN not set"
+    announce "cluster boot \"${CLUSTER}\""
+    [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
 
-    vircmd start "$CLUSTER_PATTERN"
+    virsh_cluster start || return $?
 
-    local nodes=$(vircmd dominfo "$CLUSTER_PATTERN" 2>/dev/null | \
-       sed -n -e 's/Name: *//p')
+    local nodes=$(list_all_cluster_nodes)
 
     # Wait for each node
     local i