From 604ac38ad38f7b920b72b8011d8ada0896aacac0 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Mon, 16 Mar 2015 15:21:19 +1100 Subject: Remove internal use of vircmd 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 --- autocluster | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/autocluster b/autocluster index 60d2e97..dafc605 100755 --- a/autocluster +++ b/autocluster @@ -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 -- cgit