Move shared disk presence logic from nodes into shared disk code
authorMartin Schwenke <martin@meltin.net>
Mon, 30 Jun 2014 02:08:16 +0000 (12:08 +1000)
committerMartin Schwenke <martin@meltin.net>
Wed, 2 Jul 2014 04:17:18 +0000 (14:17 +1000)
Currently this is distributed around the node logic and is hard to
manage.

Instead, introduce 2 new functions (well, meta-functions, called using
call_func):

* is_dedicated_storage_node()

  Defaults to false.  Is true for sofs_storage.

* node_has_shared_disks()

  Defaults to false.  True for sofs_storage.  True for sofs_front when
  there are no dedicated storage nodes.

Signed-off-by: Martin Schwenke <martin@meltin.net>
config.d/00base.defconf
config.d/10shareddisk.defconf
config.d/50sofs.defconf
templates/node.xml

index 7511e8a..6128d81 100644 (file)
@@ -545,9 +545,6 @@ create_node_rhel_base ()
     local name="$2"
     local ctdb_node="$3"
 
-    # rhel_base nodes do not have shared disks
-    local SHAREDDISK_TEMPLATE=""
-
     echo "Creating RHEL base node $name"
     create_node_COMMON "$name" "$ip_offset" "$type"
 }
index ed21f9c..7b7b645 100644 (file)
@@ -114,43 +114,43 @@ shared_disk_setup ()
     shared_disk_ids="tmp/shared_disk_ids.${CLUSTER}"
     rm -f "$shared_disk_ids"
 
-    if [ -n "$SHAREDDISKSIZE" -a -n "$SHAREDDISK_TEMPLATE" -a \
-       -n "$SHAREDDISK_COUNT" -a "$SHAREDDISK_COUNT" != 0 ] ; then
+    # Early exit if no shared disks
+    have_shared_disks || return 0
 
-       if [ "$SHARED_DISK_TYPE" = "iscsi" ] ; then
-           iscsi_out="tmp/iscsi.${CLUSTER}"  # Global, not local.
+    if [ "$SHARED_DISK_TYPE" = "iscsi" ] ; then
+       iscsi_out="tmp/iscsi.${CLUSTER}"  # Global, not local.
 
-           cat <<EOF >"$iscsi_out"
+       cat <<EOF >"$iscsi_out"
 tgt-admin --delete tid=${ISCSI_TID} --force
 tgtadm --lld iscsi --mode target --op new \
        --tid ${ISCSI_TID} -T ${ISCSI_IQN}.${CLUSTER}:tgtd
 EOF
-           local uc=$(echo "$CLUSTER" | tr a-z A-Z)
+       local uc=$(echo "$CLUSTER" | tr a-z A-Z)
+    fi
+
+    echo "Creating ${SHAREDDISK_COUNT} shared disks"
+    local SHARED_DISK_NUM
+    for SHARED_DISK_NUM in $(seq 1 $SHAREDDISK_COUNT); do
+       local DISK="$VIRTBASE/$CLUSTER/shared${SHARED_DISK_NUM}"
+       run_hooks hack_disk_hooks "shared"
+       local di="$DISK"
+       if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
+           di=$(readlink "$DISK")
        fi
+       rm -f "$di"
+       local di_dirname="${di%/*}"
+       mkdir -p "$di_dirname"
 
-       echo "Creating ${SHAREDDISK_COUNT} shared disks"
-       local SHARED_DISK_NUM
-       for SHARED_DISK_NUM in $(seq 1 $SHAREDDISK_COUNT); do
-           local DISK="$VIRTBASE/$CLUSTER/shared${SHARED_DISK_NUM}"
-           run_hooks hack_disk_hooks "shared"
-           local di="$DISK"
-           if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
-               di=$(readlink "$DISK")
-           fi
-           rm -f "$di"
-           local di_dirname="${di%/*}"
-           mkdir -p "$di_dirname"
-
-           if [ "$SHARED_DISK_TYPE" = "iscsi" ] ; then
-               dd if=/dev/zero seek=$SHAREDDISKSIZE bs=1 count=1 of="$di"
-
-               local paths=$SHARED_DISK_MULTIPATH_NUMPATHS  # readability
-               local zc=$(printf "%03d" $SHARED_DISK_NUM)
-               local p
-               for p in $(seq 1 $paths) ; do
-                   local lun=$(($paths * ($SHARED_DISK_NUM - 1) + $p))
-                   # vendor_id AUTCLSTR used by /etc/init.d/iscsimultipath
-                   cat <<EOF >>"$iscsi_out"
+       if [ "$SHARED_DISK_TYPE" = "iscsi" ] ; then
+           dd if=/dev/zero seek=$SHAREDDISKSIZE bs=1 count=1 of="$di"
+
+           local paths=$SHARED_DISK_MULTIPATH_NUMPATHS  # readability
+           local zc=$(printf "%03d" $SHARED_DISK_NUM)
+           local p
+           for p in $(seq 1 $paths) ; do
+               local lun=$(($paths * ($SHARED_DISK_NUM - 1) + $p))
+               # vendor_id AUTCLSTR used by /etc/init.d/iscsimultipath
+               cat <<EOF >>"$iscsi_out"
 tgtadm --lld iscsi --mode logicalunit --op new \
     --tid ${ISCSI_TID} --lun ${lun} \
     -b ${DISK}
@@ -158,26 +158,21 @@ tgtadm --lld iscsi --mode logicalunit --op update \
     --tid ${ISCSI_TID} --lun ${lun} \
     --params vendor_id=AUTCLSTR,product_id=${uc},product_rev=0001,scsi_sn=SHARE${zc}
 EOF
-               done
-           else
-               qemu-img create -f raw "$di" $SHAREDDISKSIZE
-           fi
-           # setup a nice ID at the start of the disk
-           "$SHARED_DISK_ID_GEN" "$SHARED_DISK_NUM" > tmp/diskid
-           dd if=tmp/diskid of="$di" conv=notrunc bs=1 > /dev/null 2>&1
-           head -n 1 tmp/diskid >>"$shared_disk_ids"
-       done
-       echo
-
-       if [ "$SHARED_DISK_TYPE" = "iscsi" ] ; then
-           register_hook cluster_created_hooks iscsi_cluster_created
-           register_hook setup_base_hooks shared_disk_iscsi_setup_base
+           done
+       else
+           qemu-img create -f raw "$di" $SHAREDDISKSIZE
        fi
-    else
-       SHAREDDISKSIZE=""
-       SHAREDDISK_TEMPLATE=""
-       SHAREDDISK_COUNT=""
+       # setup a nice ID at the start of the disk
+       "$SHARED_DISK_ID_GEN" "$SHARED_DISK_NUM" > tmp/diskid
+       dd if=tmp/diskid of="$di" conv=notrunc bs=1 > /dev/null 2>&1
+       head -n 1 tmp/diskid >>"$shared_disk_ids"
+    done
+    echo
+
+    if [ "$SHARED_DISK_TYPE" = "iscsi" ] ; then
+       register_hook cluster_created_hooks iscsi_cluster_created
     fi
+    register_hook setup_base_hooks shared_disk_setup_base
 }
 
 shared_disk_id_default ()
@@ -198,24 +193,85 @@ install_shared_disk_ids ()
     fi
 }
 
-register_hook setup_base_hooks install_shared_disk_ids
+shared_disk_setup_base ()
+{
+    _SHARED_DISK_TEMPLATE=""
+
+    node_has_shared_disks "$type" || return 0
+
+    install_shared_disk_ids
+
+    if [ "$SHARED_DISK_TYPE" = "iscsi" ] ; then
+       shared_disk_iscsi_setup_base
+    else
+       _SHARED_DISK_TEMPLATE="$SHAREDDISK_TEMPLATE"
+    fi
+}
 
 ##########
 
-# Allow other configuration files to override this function but still
-# use the magic.
-shared_disk_iscsi_setup_base()
+node_has_shared_disks_DEFAULT ()
+{
+    false
+}
+
+node_has_shared_disks ()
 {
-    shared_disk_iscsi_setup_base_internal
+    local node_type="$1"
+
+    call_func node_has_shared_disks "$node_type"
 }
 
+have_shared_disks ()
+{
+    [ -n "$SHAREDDISKSIZE" -a -n "$SHAREDDISK_TEMPLATE" -a \
+       -n "$SHAREDDISK_COUNT" -a "$SHAREDDISK_COUNT" != 0 ] || \
+       return 1
+
+    local ret=false
+
+    _check ()
+    {
+       if node_has_shared_disks "$1" ; then
+           ret=true
+       fi
+    }
+
+    for_each_node _check
+
+    $ret
+}
+
+is_dedicated_storage_node_DEFAULT ()
+{
+    false
+}
+
+have_dedicated_storage_nodes ()
+{
+    local ret=false
+
+    _check ()
+    {
+       if call_func is_dedicated_storage_node "$1" ; then
+           ret=true
+       fi
+    }
+
+    for_each_node _check
+
+    $ret
+}
+
+##########
+
 # Override this if you use a different scheme for IP addresses.
 shared_disk_iscsi_setup_base_get_ip ()
 {
     echo "${NETWORK_PRIVATE_PREFIX}.${IPNUM}"
 }
 
-shared_disk_iscsi_setup_base_internal ()
+shared_disk_iscsi_setup_base ()
 {
     echo "Setting up iSCSI for shared disks"
 
index 38bb710..2976848 100644 (file)
@@ -38,11 +38,6 @@ create_node_sofs_front ()
 
     echo "Creating SoFS front-end node $name"
 
-    # Drop shared disks on this node if it shouldn't have them...
-    if have_dedicated_storage_nodes ; then
-       local SHAREDDISK_TEMPLATE=""
-    fi
-
     create_node_COMMON "$name" "$ip_offset" "$type"
 }
 
@@ -57,18 +52,17 @@ create_node_sofs_storage ()
     create_node_COMMON "$name" "$ip_offset" "$type"
 }
 
-have_dedicated_storage_nodes ()
+node_has_shared_disks_sofs_front ()
 {
-    # True if there are sofs_storage nodes
-    [ "${NODES/sofs_storage:/}" != "$NODES" ]
+    ! have_dedicated_storage_nodes
 }
 
-shared_disk_iscsi_setup_base ()
+node_has_shared_disks_sofs_storage ()
 {
-    # Do the iSCSI setup on this node if there are no sofs_storage nodes
-    # or if this is a sofs_storage node.
-    if [ "${NODES/sofs_storage:/}" = "$NODES" -o \
-       "$type" = "sofs_storage" ] ; then
-       shared_disk_iscsi_setup_base_internal
-    fi
+    true
+}
+
+is_dedicated_storage_node_sofs_storage ()
+{
+    true
 }
index 7e7779d..2bfe338 100644 (file)
@@ -23,7 +23,7 @@
       <target dev='@@SYSTEM_DISK_PREFIX@@a' bus='@@SYSTEM_DISK_TYPE@@'/>
       <driver name='qemu' type='@@SYSTEM_DISK_FORMAT@@' cache='@@SYSTEM_DISK_CACHE@@'/>
     </disk>
-@@@SHAREDDISK_TEMPLATE@@@
+@@@_SHARED_DISK_TEMPLATE@@@
 @@@NETWORK_TEMPLATE@@@
     <input type='mouse' bus='ps2'/>
     <graphics type='vnc' port='-1' listen='127.0.0.1'/>