diff -up ClusterLabs-resource-agents-5434e96/heartbeat/galera.bandini ClusterLabs-resource-agents-5434e96/heartbeat/galera
--- ClusterLabs-resource-agents-5434e96/heartbeat/galera.bandini	2017-08-29 20:31:55.705448316 +0200
+++ ClusterLabs-resource-agents-5434e96/heartbeat/galera	2017-08-29 20:33:48.652952264 +0200
@@ -1,4 +1,5 @@
 #!/bin/sh
+set -x
 #
 # Copyright (c) 2014 David Vossel <dvossel@redhat.com>
 #                    All Rights Reserved.
@@ -68,6 +69,19 @@
 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
 . ${OCF_FUNCTIONS_DIR}/mysql-common.sh
 
+# In regular circumstances NODENAME gives the name of the pacemaker
+# node the resource agent is running on. But when galera runs in a
+# container (pacemaker bundle), NODENAME will be the bundle replica's
+# name.
+# We want to know the hosting node's name, because bundle replicas can
+# move across physical nodes when they are restarted, whereas the
+# attributes defined by the resource agent represent the state of a
+# galera database, so they have to "stick" to the node they describe.
+OCF_NODENAME=$(ocf_local_nodename)
+#NODENAME=${OCF_RESKEY_CRM_meta_physical_host:-${OCF_NODENAME}}
+NODENAME=$(attribute_target)
+METALNAME=${OCF_RESKEY_CRM_meta_physical_host:-${OCF_NODENAME}}
+
 # It is common for some galera instances to store
 # check user that can be used to query status
 # in this file
@@ -277,7 +291,7 @@ get_status_variable()
 
 set_bootstrap_node()
 {
-    local node=$1
+    local node=$(attribute_target $1)
 
     ${HA_SBIN_DIR}/crm_attribute -N $node -l reboot --name "${INSTANCE_ATTR_NAME}-bootstrap" -v "true"
 }
@@ -305,7 +319,7 @@ clear_no_grastate()
 
 is_no_grastate()
 {
-    local node=$1
+    local node=$(attribute_target $1)
     ${HA_SBIN_DIR}/crm_attribute -N $node -l reboot --name "${INSTANCE_ATTR_NAME}-no-grastate" -Q 2>/dev/null
 }
 
@@ -321,7 +335,7 @@ set_last_commit()
 
 get_last_commit()
 {
-    local node=$1
+    local node=$(attribute_target $1)
 
     if [ -z "$node" ]; then
        ${HA_SBIN_DIR}/crm_attribute -N $NODENAME -l reboot --name "${INSTANCE_ATTR_NAME}-last-committed" -Q 2>/dev/null
@@ -390,7 +404,7 @@ master_exists()
 
 clear_master_score()
 {
-    local node=$1
+    local node=$(attribute_target $1)
     if [ -z "$node" ]; then
         $CRM_MASTER -D
     else 
@@ -400,7 +414,7 @@ clear_master_score()
 
 set_master_score()
 {
-    local node=$1
+    local node=$(attribute_target $1)
 
     if [ -z "$node" ]; then
         $CRM_MASTER -v 100
@@ -418,9 +432,9 @@ promote_everyone()
             ocf_log err "Could not determine pacemaker node from galera name <${node}>."
             return
         else
+	        # if running in a bundle, use the bundle's pacemaker remote name
             node=$pcmk_node
         fi
-
         set_master_score $node
     done
 }
@@ -505,7 +519,7 @@ detect_first_master()
 
         greater_than_equal_long "$last_commit" "$best_commit"
         if [ $? -eq 0 ]; then
-            best_node=$node
+            best_node=$(attribute_target $node)
             best_commit=$last_commit
         fi
 
@@ -515,9 +529,12 @@ detect_first_master()
         return
     fi
 
-    ocf_log info "Promoting $best_node to be our bootstrap node"
-    set_master_score $best_node
+    ocf_log info "Selecting $best_node to be our bootstrap node"
     set_bootstrap_node $best_node
+    if [ "$best_node" = "${NODENAME}" ]; then
+	ocf_log info "We are the bootstrap node, promoting ourself to bootstrap the cluster"
+	set_master_score
+    fi
 }
 
 detect_last_commit()
@@ -703,7 +720,7 @@ galera_start()
     local rc
     local galera_node
 
-    galera_node=$(pcmk_to_galera_name $NODENAME)
+    galera_node=$(pcmk_to_galera_name $METALNAME)
     if [ -z "$galera_node" ]; then
         ocf_exit_reason "Could not determine galera name from pacemaker node <${NODENAME}>."
         return $OCF_ERR_CONFIGURED
@@ -732,7 +749,7 @@ galera_start()
     master_exists
     if [ $? -eq 0 ]; then
         ocf_log info "Master instances are already up, setting master score so this instance will join galera cluster."
-        set_master_score $NODENAME
+        set_master_score
     else
         clear_master_score
         detect_first_master
@@ -776,7 +793,7 @@ galera_monitor()
     fi
 
     # if we make it here, mysql is running. Check cluster status now.
-    galera_node=$(pcmk_to_galera_name $NODENAME)
+    galera_node=$(pcmk_to_galera_name $METALNAME)
     if [ -z "$galera_node" ]; then
         ocf_exit_reason "Could not determine galera name from pacemaker node <${NODENAME}>."
         return $OCF_ERR_CONFIGURED
diff -up ClusterLabs-resource-agents-5434e96/heartbeat/ocf-shellfuncs.in.bandini ClusterLabs-resource-agents-5434e96/heartbeat/ocf-shellfuncs.in
--- ClusterLabs-resource-agents-5434e96/heartbeat/ocf-shellfuncs.in.bandini	2017-08-29 20:32:28.685595466 +0200
+++ ClusterLabs-resource-agents-5434e96/heartbeat/ocf-shellfuncs.in	2017-08-29 20:33:19.735823242 +0200
@@ -827,6 +827,45 @@ ocf_stop_trace() {
 	set +x
 }
 
+function list_index_for_word() {
+	echo $1 | tr ' ' '\n' | awk -v x="$2" '$0~x {print NR}'
+}
+
+function list_word_at_index() {
+	echo $1 | tr ' ' '\n' | awk -v n="$2" 'n == NR' 
+}
+
+function attribute_target() {
+
+    if [ x$1 = x ]; then
+	    if [ x$OCF_RESKEY_CRM_meta_container_attribute_target = xhost -a x$OCF_RESKEY_CRM_meta_physical_host != x ]; then
+	        echo $OCF_RESKEY_CRM_meta_physical_host
+	    else
+	        echo $OCF_RESKEY_CRM_meta_on_node
+	    fi
+
+    elif [ x"$OCF_RESKEY_CRM_meta_notify_all_uname" != x ]; then
+	    index=$(list_index_for_word "$OCF_RESKEY_CRM_meta_notify_all_uname" $1)
+	    mapping=""
+	    if [ x$index != x ]; then
+	        mapping=$(list_word_at_index "$OCF_RESKEY_CRM_meta_all_hosts" $index)
+	    fi
+	    if [ x$mapping != x -a x$mapping != xNA ]; then
+	        echo $mapping
+	        return
+	    fi
+    fi
+    echo $1
+}
+
+# list_index_for_word "node0 node1 node2 node3 node4 node5" node4 --> 5
+# list_word_at_index "NA host1 host2 host3 host4 host5" 3      --> host2
+
+# list_index_for_word "node1 node2 node3 node4 node5" node7 --> "" 
+# list_word_at_index "host1 host2 host3 host4 host5" 8      --> ""
+
+# attribute_target node1                                    --> host1
+
 __ocf_set_defaults "$@"
 
 : ${OCF_TRACE_RA:=$OCF_RESKEY_trace_ra}
diff -up ClusterLabs-resource-agents-5434e96/heartbeat/rabbitmq-cluster.bandini ClusterLabs-resource-agents-5434e96/heartbeat/rabbitmq-cluster
--- ClusterLabs-resource-agents-5434e96/heartbeat/rabbitmq-cluster.bandini	2017-08-29 20:31:55.706448320 +0200
+++ ClusterLabs-resource-agents-5434e96/heartbeat/rabbitmq-cluster	2017-08-29 20:33:48.655952277 +0200
@@ -37,7 +37,8 @@ RMQ_DATA_DIR="/var/lib/rabbitmq/mnesia"
 RMQ_PID_DIR="/var/run/rabbitmq"
 RMQ_PID_FILE="/var/run/rabbitmq/rmq.pid"
 RMQ_LOG_DIR="/var/log/rabbitmq"
-NODENAME=$(ocf_local_nodename)
+#NODENAME=${OCF_RESKEY_CRM_meta_physical_host:-$(ocf_local_nodename)}
+NODENAME=$(attribute_target)
 
 # this attr represents the current active local rmq node name.
 # when rmq stops or the node is fenced, this attr disappears
@@ -340,7 +341,7 @@ rmq_notify() {
 
 	# forget each stopped rmq instance in the provided pcmk node in the list.
 	for node in $(echo "$node_list"); do
-		local rmq_node="$(${HA_SBIN_DIR}/crm_attribute -N $node -l forever --query --name $RMQ_CRM_ATTR_COOKIE_LAST_KNOWN -q)"
+		local rmq_node="$(${HA_SBIN_DIR}/crm_attribute -N $(attribute_target $node) -l forever --query --name $RMQ_CRM_ATTR_COOKIE_LAST_KNOWN -q)"
 		if [ -z "$rmq_node" ]; then
 			ocf_log warn "Unable to map pcmk node $node to a known rmq node."
 			continue	
diff -up ClusterLabs-resource-agents-5434e96/heartbeat/redis.bandini ClusterLabs-resource-agents-5434e96/heartbeat/redis
--- ClusterLabs-resource-agents-5434e96/heartbeat/redis.bandini	2017-08-29 20:31:55.708448329 +0200
+++ ClusterLabs-resource-agents-5434e96/heartbeat/redis	2017-08-29 20:33:48.657952286 +0200
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+set -x
+
 . ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs
 
 : ${OCF_RESKEY_bin:=/usr/bin/redis-server}
@@ -185,7 +187,13 @@ function last_known_master()
 }
 
 function crm_master_reboot() {
-	"${HA_SBIN_DIR}/crm_master" -l reboot "$@"
+        local node=$(attribute_target)
+        if [ -z "$node" ]; then
+	    "${HA_SBIN_DIR}/crm_master" -l reboot "$@"
+        else
+
+	    "${HA_SBIN_DIR}/crm_master" -N $node -l reboot "$@"
+        fi
 }
 
 function calculate_score()
@@ -537,7 +545,8 @@ function validate() {
 	fi
 }
 
-NODENAME=$(ocf_local_nodename)
+#NODENAME=${OCF_RESKEY_CRM_meta_physical_host:-$(ocf_local_nodename)}
+NODENAME=$(attribute_target)
 if [ -f "$REDIS_CONFIG" ]; then
 	clientpasswd="$(cat $REDIS_CONFIG | sed -n -e 's/^\s*requirepass\s*\(.*\)\s*$/\1/p' | tail -n 1)"
 fi
