Run top-level ssh with -n
[autocluster.git] / autocluster
index 113950e7fe30b18fe8d6dba24a95f4d28cde17f0..b987a1221688179048759ea75c79ba4825a2a979 100755 (executable)
@@ -39,15 +39,6 @@ usage ()
 Usage: autocluster [OPTION] ... <COMMAND>
   options:
      -c <file>                   specify config file (default is "config")
-EOF
-
-    releases=$(list_releases)
-
-    usage_smart_display \
-       defconf "WITH_RELEASE" "" \
-       "<string>" "specify preset options for a release using a version string.  Possible values are: ${releases}."
-
-cat <<EOF
      -e <expr>                   execute <expr> and exit
      -E <expr>                   execute <expr> and continue
      -x                          enable script debugging
@@ -61,10 +52,16 @@ EOF
     cat <<EOF
 
   commands:
+     base [ create | boot ] ...
+
+     cluster [ build |
+               destroy | undefine |
+               create | update_hosts | boot | setup ] ...
+
      create base
            create a base image
 
-     create cluster CLUSTERNAME
+     create cluster [ CLUSTERNAME ]
            create a full cluster
 
      create node CLUSTERNAME IP_OFFSET
@@ -78,9 +75,6 @@ EOF
 
      bootbase
            boot the base image
-
-     testproxy
-           test your proxy setup
 EOF
     exit 1
 }
@@ -88,8 +82,45 @@ EOF
 ###############################
 
 die () {
-    fill_text 0 "ERROR: $*" >&2
-    exit 1
+    if [ "$no_sanity" = 1 ] ; then
+       fill_text 0 "WARNING: $*" >&2
+    else
+       fill_text 0 "ERROR: $*" >&2
+       exit 1
+    fi
+}
+
+announce ()
+{
+    echo "######################################################################"
+    printf "# %-66s #\n" "$*"
+    echo "######################################################################"
+    echo ""
+}
+
+waitfor ()
+{
+    local file="$1"
+    local msg="$2"
+    local timeout="$3"
+
+    local tmpfile=$(mktemp)
+
+    cat <<EOF >"$tmpfile"
+spawn tail -n 10000 -f $file
+expect -timeout $timeout -re "$msg"
+EOF
+
+    export LANG=C
+    expect "$tmpfile"
+    rm -f "$tmpfile"
+
+    if ! grep -E "$msg" "$file" > /dev/null; then
+       echo "Failed to find \"$msg\" in \"$file\""
+       return 1
+    fi
+
+    return 0
 }
 
 ###############################
@@ -122,6 +153,11 @@ for_each_node ()
     done
 }
 
+node_is_ctdb_node_DEFAULT ()
+{
+    echo 0
+}
+
 hack_one_node_with ()
 {
     local filter="$1" ; shift
@@ -147,6 +183,53 @@ 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"
+       shift
+
+    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"
@@ -158,10 +241,11 @@ register_hook ()
 run_hooks ()
 {
     local hook_var="$1"
+    shift
 
     local i
     for i in ${!hook_var} ; do
-       $i
+       $i "$@"
     done
 }
 
@@ -175,6 +259,26 @@ clear_hooks ()
 
 ##############################
 
+# These hooks are intended to customise the value of $DISK.  They have
+# access to 1 argument ("base", "system", "shared") and the variables
+# $VIRTBASE, $CLUSTER, $BASENAME (for "base"), $NAME (for "system"),
+# $SHARED_DISK_NUM (for "shared").  A hook must be deterministic and
+# should not be stateful, since they can be called multiple times for
+# the same disk.
+hack_disk_hooks=""
+
+create_node_DEFAULT ()
+{
+    local type="$1"
+    local ip_offset="$2"
+    local name="$3"
+    local ctdb_node="$4"
+
+    echo "Creating node \"$name\" (of type \"${type}\")"
+
+    create_node_COMMON "$name" "$ip_offset" "$type"
+}
+
 # common node creation stuff
 create_node_COMMON ()
 {
@@ -187,31 +291,51 @@ create_node_COMMON ()
        die "Error: if BASE_FORMAT is \"qcow2\" then SYSTEM_DISK_FORMAT must also be \"qcow2\"."
     fi
 
-    IPNUM=$(($FIRSTIP + $ip_offset))
-    DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
-    local base_disk="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
+    local IPNUM=$(($FIRSTIP + $ip_offset))
+    make_network_map
 
+    # Determine base image name.  We use $DISK temporarily to allow
+    # the path to be hacked.
+    local DISK="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
     if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
-       base_disk="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
+       DISK="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
     fi
+    run_hooks hack_disk_hooks "base"
+    local base_disk="$DISK"
+
+    # Determine the system disk image name.
+    DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
+    run_hooks hack_disk_hooks "system"
 
-    mkdir -p $VIRTBASE/$CLUSTER tmp
+    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"
 
-    rm -f "$DISK"
     case "$SYSTEM_DISK_FORMAT" in
        qcow2)
            echo "Creating the disk..."
-           qemu-img create -b "$base_disk" -f qcow2 "$DISK"
+           qemu-img create -b "$base_disk" -f qcow2 "$di"
            create_node_configure_image "$DISK" "$type"
            ;;
        raw)
            echo "Creating the disk..."
-           cp -v --sparse=always "$base_disk" "$DISK"
+           cp -v --sparse=always "$base_disk" "$di"
            create_node_configure_image "$DISK" "$type"
            ;;
        reflink)
            echo "Creating the disk..."
-           cp -v --reflink=always "$base_disk" "$DISK"
+           cp -v --reflink=always "$base_disk" "$di"
+           create_node_configure_image "$DISK" "$type"
+           ;;
+       mmclone)
+           echo "Creating the disk (using mmclone)..."
+           local base_snap="${base_disk}.snap"
+           [ -f "$base_snap" ] || mmclone snap "$base_disk" "$base_snap"
+           mmclone copy "$base_snap" "$di"
            create_node_configure_image "$DISK" "$type"
            ;;
        none)
@@ -221,11 +345,11 @@ create_node_COMMON ()
            die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
     esac
 
-    set_macaddrs $CLUSTER $ip_offset
-
     # Pull the UUID for this node out of the map.
     UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
     
+    mkdir -p tmp
+
     echo "Creating $NAME.xml"
     substitute_vars $template_file tmp/$NAME.xml
     
@@ -244,16 +368,53 @@ create_node_configure_image ()
     diskimage unmount
 }
 
-# Provides an easy way of removing nodes from $NODE.
-create_node_null () {
-    :
+hack_network_map_hooks=""
+
+# Uses: CLUSTER, NAME, NETWORKS, FIRSTIP, ip_offset
+make_network_map ()
+{
+    network_map="tmp/network_map.$NAME"
+
+    if [ -n "$CLUSTER" ] ; then
+       local md5=$(echo "$CLUSTER" | md5sum)
+       local nh=$(printf "%02x" $ip_offset)
+       local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
+    else
+       local mac_prefix="02:42:42:00:00:"
+    fi
+
+    local n
+    local count=1
+    for n in $NETWORKS ; do
+       local ch=$(printf "%02x" $count)
+       local mac="${mac_prefix}${ch}"
+
+       set -- ${n//,/ }
+       local ip_bits="$1" ; shift
+       local dev="$1" ; shift
+       local opts="$*"
+
+       local net="${ip_bits%/*}"
+       local netname="acnet_${net//./_}"
+
+       local ip="${net%.*}.${IPNUM}/${ip_bits#*/}"
+
+       local ipv6="fc00:${net//./:}::${IPNUM}/64"
+
+       # This can be used to override the variables in the echo
+       # statement below.  The hook can use any other variables
+       # available in this function.
+       run_hooks hack_network_map_hooks
+
+       echo "${netname} ${dev} ${ip} ${ipv6} ${mac} ${opts}"
+       count=$(($count + 1))
+    done >"$network_map"
 }
 
 ##############################
 
-hack_nodes_functions=
-
-expand_nodes () {
+expand_nodes ()
+{
     # Expand out any abbreviations in NODES.
     local ns=""
     local n
@@ -279,35 +440,6 @@ expand_nodes () {
     done
     NODES="$ns"
 
-    # Apply nodes hacks.  Some of this is about backward compatibility
-    # but the hacks also fill in the node names and whether they're
-    # part of the CTDB cluster.  The order is the order that
-    # configuration modules register their hacks.
-    run_hooks hack_nodes_functions
-
-    if [ -n "$NUMNODES" ] ; then
-       # Attempt to respect NUMNODES.  Reduce the number of CTDB
-       # nodes to NUMNODES.
-       local numnodes=$NUMNODES
-
-       hack_filter ()
-       {
-           if [ "$ctdb_node" = 1 ] ; then
-               if [ $numnodes -gt 0 ] ; then
-                   numnodes=$(($numnodes - 1))
-               else
-                   node_type="null"
-                   ctdb_node=0
-               fi
-           fi
-       }
-
-       hack_all_nodes_with hack_filter
-                       
-       [ $numnodes -gt 0 ] && \
-           die "Can't not use NUMNODES to increase the number of nodes over that specified by NODES.  You need to set NODES instead - please read the documentation."
-    fi
-    
     # Check IP addresses for duplicates.
     local ip_offsets=":"
     # This function doesn't modify anything...
@@ -318,6 +450,22 @@ expand_nodes () {
        ip_offsets="${ip_offsets}${ip_offset}:"
     }
     hack_all_nodes_with get_ip_offset
+
+    # Determine node names and whether they're in the CTDB cluster
+    declare -A node_count
+    _get_name_ctdb_node ()
+    {
+       local count=$((${node_count[$node_type]:-0} + 1))
+       node_count[$node_type]=$count
+       name=$(call_func node_name_format "$node_type" "$CLUSTER" $count) || {
+           echo "ERROR: Node type \"${node_type}\" not defined!"
+           echo "Valid node types are:"
+           set | sed -n 's@^node_name_format_\(.*\) ().*@  \1@p'
+           exit 1
+       }
+       ctdb_node=$(call_func node_is_ctdb_node "$node_type")
+    }
+    hack_all_nodes_with _get_name_ctdb_node
 }
 
 ##############################
@@ -331,7 +479,7 @@ Some cluster filesystems have problems with other characters."
 
 hosts_file=
 
-common_nodelist_hacking ()
+cluster_nodelist_hacking ()
 {
     # Rework the NODES list
     expand_nodes
@@ -339,13 +487,11 @@ common_nodelist_hacking ()
     # Build /etc/hosts and hack the names of the ctdb nodes
     hosts_line_hack_name ()
     {
-       # Ignore nodes without names (e.g. "null")
-       [ "$node_type" != "null" -a -n "$name" ] || return 0
-
        local sname=""
        local hosts_line
-       local ip_addr="$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
-       
+       local ip_addr="${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
+
+       # Primary name for CTDB nodes is <CLUSTER>n<num>
        if [ "$ctdb_node" = 1 ] ; then
            num_ctdb_nodes=$(($num_ctdb_nodes + 1))
            sname="${CLUSTER}n${num_ctdb_nodes}"
@@ -383,13 +529,23 @@ common_nodelist_hacking ()
     ctdb_nodes_line ()
     {
        [ "$ctdb_node" = 1 ] || return 0
-       echo "$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
+       echo "${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
        num_nodes=$(($num_nodes + 1))
     }
     nodes_file="tmp/nodes.$CLUSTER"
     local num_nodes=0
     hack_all_nodes_with ctdb_nodes_line >$nodes_file
-    : "${NUMNODES:=${num_nodes}}"  # Set $NUMNODES if necessary
+
+    # Build /etc/ctdb/nodes.ipv6
+    ctdb_nodes_line_ipv6 ()
+    {
+       [ "$ctdb_node" = 1 ] || return 0
+       echo "fc00:${NETWORK_PRIVATE_PREFIX//./:}::$(($FIRSTIP + $ip_offset))"
+       num_nodes=$(($num_nodes + 1))
+    }
+    nodes_file_ipv6="tmp/nodes.$CLUSTER.ipv6"
+    local num_nodes=0
+    hack_all_nodes_with ctdb_nodes_line_ipv6 >$nodes_file_ipv6
 
     # Build UUID map
     uuid_map="tmp/uuid_map.$CLUSTER"
@@ -403,9 +559,12 @@ common_nodelist_hacking ()
 create_cluster_hooks=
 cluster_created_hooks=
 
-create_cluster ()
+cluster_create ()
 {
-    CLUSTER="$1"
+    # Use $1.  If not set then use value from configuration file.
+    CLUSTER="${1:-${CLUSTER}}"
+    announce "cluster create \"${CLUSTER}\""
+    [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
 
     sanity_check_cluster_name
 
@@ -414,8 +573,6 @@ create_cluster ()
     # Run hooks before doing anything else.
     run_hooks create_cluster_hooks
 
-    common_nodelist_hacking
-
     for_each_node call_func create_node
 
     echo "Cluster $CLUSTER created"
@@ -432,6 +589,150 @@ cluster_created_hosts_message ()
 
 register_hook cluster_created_hooks cluster_created_hosts_message
 
+cluster_destroy ()
+{
+    announce "cluster destroy \"${CLUSTER}\""
+    [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
+
+    virsh_cluster destroy || true
+}
+
+cluster_undefine ()
+{
+    announce "cluster undefine \"${CLUSTER}\""
+    [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
+
+    virsh_cluster undefine --managed-save || true
+}
+
+cluster_update_hosts ()
+{
+    announce "cluster update_hosts \"${CLUSTER}\""
+    [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
+
+    [ -n "$hosts_file" ] || hosts_file="tmp/hosts.${CLUSTER}"
+    [ -r "$hosts_file" ] || die "Missing hosts file \"${hosts_file}\""
+
+    # Building a general node name regexp is a bit cumbersome.  :-)
+    local name_regexp="("
+    for i in $(set | sed -n -e "s@^\(node_name_format_.*\) ().*@\1@p") ; do
+       # Format node name with placeholders (remembering that "_" is
+       # not valid in a real cluster name)
+       local t=$("$i" "_" "0")
+       # now replace the placeholders with regexps - order is
+       # important here, since the cluster name can contain digits
+       t=$(sed -r -e "s@[[:digit:]]+@[[:digit:]]+@" -e "s@_@${CLUSTER}@" <<<"$t")
+       # add to the regexp
+       name_regexp="${name_regexp}${t}|"
+    done
+    name_regexp="${name_regexp}${CLUSTER}n[[:digit:]]+)"
+
+    local pat="# autocluster ${CLUSTER}\$|[[:space:]]${name_regexp}"
+
+    local t="/etc/hosts.${CLUSTER}"
+    grep -E "$pat" /etc/hosts >"$t" || true
+    if diff -B "$t" "$hosts_file" >/dev/null ; then
+       rm "$t"
+       return
+    fi
+
+    local old=/etc/hosts.old.autocluster
+    cp /etc/hosts "$old"
+    local new=/etc/hosts.new
+    grep -Ev "$pat" "$old" |
+    cat -s - "$hosts_file" >"$new"
+
+    mv "$new" /etc/hosts
+
+    echo "Made these changes to /etc/hosts:"
+    diff -u "$old" /etc/hosts || true
+}
+
+cluster_boot ()
+{
+    announce "cluster boot \"${CLUSTER}\""
+    [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
+
+    virsh_cluster start || return $?
+
+    local nodes=$(list_all_cluster_nodes)
+
+    # Wait for each node
+    local i
+    for i in $nodes ; do
+       waitfor "${KVMLOG}/serial.$i" "login:" 300 || {
+           vircmd destroy "$CLUSTER_PATTERN"
+           die "Failed to create cluster"
+       }
+    done
+
+    # Move past the last line of log output
+    echo ""
+}
+
+cluster_setup_tasks_DEFAULT ()
+{
+    local stage="$1"
+
+    # By default nodes have no tasks
+    case "$stage" in
+       install_packages) echo "" ;;
+       setup_clusterfs)  echo "" ;;
+       setup_node)       echo "" ;;
+       setup_cluster)    echo "" ;;
+    esac
+}
+
+cluster_setup ()
+{
+    announce "cluster setup \"${CLUSTER}\""
+    [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
+
+    local ssh="ssh -n -o StrictHostKeyChecking=no"
+    local setup_clusterfs_done=false
+    local setup_cluster_done=false
+
+    _cluster_setup_do_stage ()
+    {
+       local stage="$1"
+       local type="$2"
+       local ip_offset="$3"
+       local name="$4"
+       local ctdb_node="$5"
+
+       local tasks=$(call_func cluster_setup_tasks "$type" "$stage")
+
+       if [ -n "$tasks" ] ; then
+           # These tasks are only done on 1 node
+           case "$stage" in
+               setup_clusterfs)
+                   if $setup_clusterfs_done ; then
+                       return
+                   else
+                       setup_clusterfs_done=true
+                   fi
+                   ;;
+               setup_cluster)
+                   if $setup_cluster_done ; then
+                       return
+                   else
+                       setup_cluster_done=true
+                   fi
+                   ;;
+           esac
+
+           $ssh "$name" ./scripts/cluster_setup.sh "$stage" $tasks
+       fi
+
+    }
+
+    local stages="install_packages setup_clusterfs setup_node setup_cluster"
+    local stage
+    for stage in $stages ; do
+       for_each_node _cluster_setup_do_stage "$stage"
+    done
+}
+
 create_one_node ()
 {
     CLUSTER="$1"
@@ -441,8 +742,6 @@ create_one_node ()
 
     mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
 
-    common_nodelist_hacking
-
     for n in $NODES ; do
        set -- $(IFS=: ; echo $n)
        [ $single_node_ip_offset -eq $2 ] || continue
@@ -470,42 +769,62 @@ test_proxy() {
 
 kickstart_floppy_create_hooks=
 
-# create base image
-create_base() {
+guess_install_network ()
+{
+    # Figure out IP address to use during base install.  Default to
+    # the IP address of the 1st (private) network. If a gateway is
+    # specified then use the IP address associated with it.
+    INSTALL_IP=""
+    INSTALL_GW=""
+    local netname dev ip ipv6 mac opts
+    while read netname dev ip ipv6 mac opts; do
+       local o
+       for o in $opts ; do
+           case "$o" in
+               gw\=*)
+                   INSTALL_GW="${o#gw=}"
+                   INSTALL_IP="$ip"
+           esac
+       done
+       [ -n "$INSTALL_IP" ] || INSTALL_IP="$ip"
+    done <"$network_map"
+}
 
-    NAME="$BASENAME"
-    DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
+# create base image
+base_create()
+{
+    local NAME="$BASENAME"
+    local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
+    run_hooks hack_disk_hooks "base"
 
     mkdir -p $KVMLOG
 
     echo "Testing WEBPROXY $WEBPROXY"
     test_proxy
 
+    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 the disk"
-    qemu-img create -f $BASE_FORMAT "$DISK" $DISKSIZE
+    qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
 
     rm -rf tmp
     mkdir -p mnt tmp tmp/ISO
 
     setup_timezone
 
-    echo "Creating kickstart file from template"
-    substitute_vars "$KICKSTART" "tmp/ks.cfg"
-
-    if [ $INSTALLKEY = "--skip" ]; then
-       cat <<EOF
---------------------------------------------------------------------------------------
-WARNING: You have not entered an install key. Some RHEL packages will not be installed.
-
-Please enter a valid RHEL install key in your config file like this:
+    IPNUM=$FIRSTIP
+    make_network_map
 
-  INSTALLKEY="1234-5678-0123-4567"
+    guess_install_network
 
-The install will continue without an install key in 5 seconds
---------------------------------------------------------------------------------------
-EOF
-       sleep 5
-    fi
+    echo "Creating kickstart file from template"
+    substitute_vars "$KICKSTART" "tmp/ks.cfg"
 
     # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
     case "$ISO" in
@@ -515,7 +834,7 @@ EOF
     
     echo "Creating kickstart floppy"
     dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
-    mkdosfs tmp/floppy.img
+    mkdosfs -n KICKSTART tmp/floppy.img
     mount -o loop -t msdos tmp/floppy.img mnt
     cp tmp/ks.cfg mnt
     mount -o loop,ro $ISO tmp/ISO
@@ -560,6 +879,7 @@ may not have matched anything at the end of the kickstart output."
 Install finished, base image $DISK created
 
 You may wish to run
+   chcon -t virt_content_t $DISK
    chattr +i $DISK
 To ensure that this image does not change
 
@@ -570,16 +890,17 @@ EOF
 
 ###############################
 # boot the base disk
-boot_base() {
-    CLUSTER="$1"
+base_boot() {
+    rm -rf tmp
+    mkdir -p tmp
 
     NAME="$BASENAME"
     DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
 
-    rm -rf tmp
-    mkdir -p tmp
-
     IPNUM=$FIRSTIP
+
+    make_network_map
+
     CLUSTER="base"
 
     diskimage mount $DISK
@@ -597,28 +918,6 @@ boot_base() {
 
 ######################################################################
 
-# various functions...
-
-# Set some MAC address variables based on a hash of the cluster name
-# plus the node number and each adapter number.
-set_macaddrs () {
-    local cname="$1"
-    local ip_offset="$2"
-
-    local md5=$(echo $cname | md5sum)
-    local nh=$(printf "%02x" $ip_offset)
-    local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
-
-    MAC1="${mac_prefix}01"
-    MAC2="${mac_prefix}02"
-    MAC3="${mac_prefix}03"
-    MAC4="${mac_prefix}04"
-    MAC5="${mac_prefix}05"
-    MAC6="${mac_prefix}06"
-}
-
-######################################################################
-
 # Updating a disk image...
 
 diskimage ()
@@ -679,10 +978,18 @@ setup_base_grub_conf ()
 {
     echo "Adjusting grub.conf"
     local o="$EXTRA_KERNEL_OPTIONS" # For readability.
-    diskimage sed "/boot/grub/grub.conf" \
-       -e "s/console=ttyS0,19200/console=ttyS0,115200/"  \
-       -e "s/ nodmraid//" -e "s/ nompath//"  \
-       -e "s/quiet/noapic divider=10${o:+ }${o}/g"
+    local grub_configs="/boot/grub/grub.conf"
+    if ! diskimage is_file "$grub_configs" ; then
+       grub_configs="/etc/default/grub /boot/grub2/grub.cfg"
+    fi
+    local c
+    for c in $grub_configs ; do
+       diskimage sed "$c" \
+           -e "s/console=ttyS0,19200/console=ttyS0,115200/"  \
+           -e "s/ console=tty1//" -e "s/ rhgb/ norhgb/"  \
+           -e "s/ nodmraid//" -e "s/ nompath//"  \
+           -e "s/quiet/noapic divider=10${o:+ }${o}/g"
+    done
 }
 
 register_hook setup_base_hooks setup_base_grub_conf
@@ -701,6 +1008,31 @@ setup_base()
     run_hooks setup_base_hooks
 }
 
+ipv4_prefix_to_netmask ()
+{
+    local prefix="$1"
+
+    local div=$(($prefix / 8))
+    local mod=$(($prefix % 8))
+
+    local octet
+    for octet in 1 2 3 4 ; do
+       if [ $octet -le $div ] ; then
+           echo -n "255"
+       elif [ $mod -ne 0 -a $octet -eq $(($div + 1)) ] ; then
+           local shift=$((8 - $mod))
+           echo -n $(( (255 >> $shift << $shift) ))
+       else
+           echo -n 0
+       fi
+       if [ $octet -lt 4 ] ; then
+           echo -n '.'
+       fi
+    done
+
+    echo
+}
+
 # setup various networking components
 setup_network()
 {
@@ -715,7 +1047,17 @@ setup_network()
 
     echo "Setting up /etc/ctdb/nodes"
     diskimage mkdir_p "/etc/ctdb"
-    diskimage put "$nodes_file" "/etc/ctdb/nodes"
+    if [ "$NETWORK_STACK" = "ipv4" ] ; then
+       diskimage put "$nodes_file" "/etc/ctdb/nodes"
+    elif [ "$NETWORK_STACK" = "ipv6" ] ; then
+       diskimage put "$nodes_file_ipv6" "/etc/ctdb/nodes"
+    elif [ "$NETWORK_STACK" = "dual" ] ; then
+       diskimage put "$nodes_file" "/etc/ctdb/nodes.ipv4"
+       diskimage put "$nodes_file_ipv6" "/etc/ctdb/nodes.ipv6"
+       diskimage put "$nodes_file" "/etc/ctdb/nodes"
+    else
+       die "Error: Invalid NETWORK_STACK value \"$NETWORK_STACK\"."
+    fi
 
     [ "$WEBPROXY" = "" ] || {
        diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
@@ -730,11 +1072,78 @@ setup_network()
     diskimage mkdir_p "/etc/yum.repos.d"
     echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
 
-    diskimage rm_f "/etc/udev/rules.d/70-persistent-net.rules"
+    diskimage rm_rf "/etc/udev/rules.d/70-persistent-net.rules"
+
+    echo "Setting up network interfaces: "
+    local netname dev ip ipv6 mac opts
+    while read netname dev ip ipv6 mac opts; do
+       echo "  $dev"
+
+       local o gw addr mask
+       gw=""
+       for o in $opts ; do
+           case "$o" in
+               gw\=*)
+                   gw="${o#gw=}"
+           esac
+       done
+
+       addr=${ip%/*}
+       mask=$(ipv4_prefix_to_netmask ${ip#*/})
+
+       cat <<EOF | \
+           diskimage put - "/etc/sysconfig/network-scripts/ifcfg-${dev}"
+DEVICE=$dev
+ONBOOT=yes
+TYPE=Ethernet
+IPADDR=$addr
+NETMASK=$mask
+HWADDR=$mac
+IPV6INIT=yes
+IPV6ADDR=$ipv6
+${gw:+GATEWAY=}${gw}
+EOF
+
+       # This goes to 70-persistent-net.rules
+       cat <<EOF
+# Generated by autocluster
+SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${mac}", ATTR{type}=="1", KERNEL=="eth*", NAME="${dev}"
+
+EOF
+    done <"$network_map" |
+    diskimage put - "/etc/udev/rules.d/70-persistent-net.rules"
 }
 
 register_hook setup_base_hooks setup_network
 
+setup_base_cluster_setup_config ()
+{
+    local f
+    {
+       echo "# Generated by autocluster"
+       echo
+       # This is a bit of a hack.  Perhaps these script belong
+       # elsewhere, since they no longer have templates?
+       for f in $(find "${BASE_TEMPLATES}/all/root/scripts" -type f |
+           xargs grep -l '^#config:') ; do
+
+           b=$(basename "$f")
+           echo "# $b"
+           local vs v
+           vs=$(sed -n 's@^#config: *@@p' "$f")
+           for v in $vs ; do
+               # This could substitute the values in directly using
+               # ${!v} but then no sanity checking is done to make
+               # sure variables are set.
+               echo "${v}=\"@@${v}@@\""
+           done
+           echo
+       done
+    } | diskimage substitute_vars - "/root/scripts/cluster_setup.config"
+}
+
+register_hook setup_base_hooks setup_base_cluster_setup_config
+
 setup_timezone() {
     [ -z "$TIMEZONE" ] && {
        [ -r /etc/timezone ] && {
@@ -1048,94 +1457,24 @@ usage_config_options (){
     usage_smart_display load_config
 }
 
-list_releases () {
-    local releases=$(cd $installdir/releases && echo *.release)
-    releases="${releases//.release}"
-    releases="${releases// /\", \"}"
-    echo "\"$releases\""
-}
-
-with_release () {
-    local release="$1"
-    shift # subsequent args are passed to release file
-
-    # This simply loads an extra config file from $installdir/releases
-    f="${installdir}/releases/${release}.release"
-    if [ -r "$f" ] ; then
-       . "$f"
-    else
-       f="${installdir}/releases/${release%%-*}.release"
-       if [ -r "$f" ] ; then
-           . "$f" "${release#*-}"
-       else
-           echo "Unknown release \"${release}\" specified to --with-release"
-           printf "%-25s" "Supported releases are: "
-           fill_text 25 "$(list_releases)"
-           exit 1
-       fi
-    fi
-
-}
-
-has_public_addresses_DEFAULT ()
+actions_init ()
 {
-    false
+    actions=""
 }
 
-# Build public address configuration.
-# * 1st public IP:  unless specified, last octet is $FIRSTIP + $PUBLIC_IP_OFFSET
-# * Excluded nodes: unless specified via comma-separated list of IP offsets,
-#                   nodes are excluded via their node types
-# * Number of public addresses per interface is either specified or $NUMNODES.
-make_public_addresses_old () {
-    local firstip="${1:-$(($FIRSTIP + $PUBLIC_IP_OFFSET))}"
-    local excluded_nodes="$2" 
-    local num_addrs="${3:-${NUMNODES}}"
-
-    # For delimiting matches.
-    excluded_nodes="${excluded_nodes:+,}${excluded_nodes}${excluded_nodes:+,}"
-    # Avoid spaces
-    excluded_nodes="${excluded_nodes// /}"
-
-    make_public_addresses_for_node ()
-    {
-       [ "$ctdb_node" = 1 ] || return 0
-
-       echo "[/etc/ctdb/public_addresses:${name}.${DOMAIN}]"
-
-       if [ -n "$excluded_nodes" -a \
-           "${excluded_nodes/,${ip_offset},}" = "$excluded_nodes" ] ||
-           ([ -z "$excluded_nodes" ] &&
-               call_func has_public_addresses "$node_type") ; then
-
-           local e i
-           for e in $IPNET1 $IPNET2 ; do
-               for i in $(seq $firstip $(($firstip + $num_addrs - 1))) ; do
-                   if [ $i -gt 254 ] ; then
-                       die "make_public_addresses: octet > 254 - consider setting PUBLIC_IP_OFFSET"
-                   fi
-                   printf "\t${IPBASE}.${e}.${i}/24 eth${e}\n"
-               done
-           done            
-       fi
-       echo 
-    }
-    hack_all_nodes_with make_public_addresses_for_node
+actions_add ()
+{
+    actions="${actions}${actions:+ }$*"
 }
 
-make_public_addresses() {
-       local firstip="${1:-$[${FIRSTIP} + ${PUBLIC_IP_OFFSET}]}"
-       local num_addrs="${2:-${NUMNODES}}"
-
-       if [ $[ $firstip + $num_addrs - 1 ] -gt 254 ]; then
-               die "make_public_addresses: last octet > 254 - change PUBLIC_IP_OFFSET"
-       fi
+actions_run ()
+{
+    [ -n "$actions" ] || usage
 
-       local e
-       for e in $IPNET0 $IPNET1 $IPNET2 ; do
-               echo -ne "${IPBASE}.${e}.${firstip},${num_addrs},eth${e} "
-       done
-       echo
+    local a
+    for a in $actions ; do
+       $a
+    done
 }
 
 ######################################################################
@@ -1149,7 +1488,7 @@ load_config
 ############################
 # parse command line options
 long_opts=$(getopt_config_options)
-getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
+getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump -l "$long_opts" -- "$@")
 [ $? != 0 ] && usage
 
 use_default_config=true
@@ -1163,7 +1502,6 @@ while true ; do
        -e) shift 2 ;;
        -E) shift 2 ;;
        --) shift ; break ;;
-       --with-release) shift 2 ;; # Don't set use_default_config=false!!!
        --dump|-x) shift ;;
        -h|--help) usage ;; # Usage should be shown here for real defaults.
        --*) shift 2 ;; # Assume other long opts are valid and take an arg.
@@ -1178,16 +1516,24 @@ eval set -- "$getopt_output"
 
 while true ; do
     case "$1" in
-       # force at least ./local_file to avoid accidental file from $PATH
-       -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
-       -e) run_hooks post_config_hooks ; eval "$2" ; exit ;;
-       -E) eval "$2" ; shift 2 ;;
-       --with-release)
-           with_release "$2"
+       -c)
+           b=$(basename $2)
+           # force at least ./local_file to avoid accidental file
+           # from $PATH
+           . "$(dirname $2)/${b}"
+           # If $CLUSTER is unset then try to base it on the filename
+           if [ ! -n "$CLUSTER" ] ; then
+               case "$b" in
+                   *.autocluster)
+                       CLUSTER="${b%.autocluster}"
+               esac
+           fi
            shift 2
            ;;
+       -e) no_sanity=1 ; run_hooks post_config_hooks ; eval "$2" ; exit ;;
+       -E) eval "$2" ; shift 2 ;;
        -x) set -x; shift ;;
-       --dump) run_hooks post_config_hooks ; dump_config ;;
+       --dump) no_sanity=1 ; run_hooks post_config_hooks ; dump_config ;;
        --) shift ; break ;;
        -h|--help) usage ;; # Redundant.
        --*)
@@ -1221,21 +1567,50 @@ check_command expect
 
 [ $# -lt 1 ] && usage
 
-command="$1"
+t="$1"
 shift
 
-case $command in
+case "$t" in
+    base)
+       actions_init
+       for t in "$@" ; do
+           case "$t" in
+               create|boot) actions_add "base_${t}" ;;
+               *) usage ;;
+           esac
+       done
+       actions_run
+       ;;
+
+    cluster)
+       actions_init
+       for t in "$@" ; do
+           case "$t" in
+               destroy|undefine|create|update_hosts|boot|setup)
+                   actions_add "cluster_${t}" ;;
+               build)
+                   for t in destroy undefine create update_hosts boot setup ; do
+                       actions_add "cluster_${t}"
+                   done
+                   ;;
+               *) usage ;;
+           esac
+       done
+       cluster_nodelist_hacking
+       actions_run
+       ;;
+
     create)
-       type=$1
+       t="$1"
        shift
-       case $type in
+       case "$t" in
            base)
                [ $# != 0 ] && usage
-               create_base
+               base_create
                ;;
            cluster)
                [ $# != 1 ] && usage
-               create_cluster "$1"
+               cluster_create "$1"
                ;;
            node)
                [ $# != 2 ] && usage
@@ -1255,10 +1630,7 @@ case $command in
        diskimage unmount
        ;;
     bootbase)
-       boot_base;
-       ;;
-    testproxy)
-       test_proxy;
+       base_boot;
        ;;
     *)
        usage;