2 # main autocluster script
4 # Copyright (C) Andrew Tridgell 2008
5 # Copyright (C) Martin Schwenke 2008
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <http://www.gnu.org/licenses/>.
20 ##BEGIN-INSTALLDIR-MAGIC##
21 # There are better ways of doing this but not if you still want to be
22 # able to run straight out of a git tree. :-)
26 autocluster=$(which "$0")
28 if [ -L "$autocluster" ] ; then
29 autocluster=$(readlink "$autocluster")
31 installdir=$(dirname "$autocluster")
32 ##END-INSTALLDIR-MAGIC##
39 Usage: autocluster [OPTION] ... <COMMAND>
41 -c <file> specify config file (default is "config")
42 -e <expr> execute <expr> and exit
43 -E <expr> execute <expr> and continue
44 -x enable script debugging
45 --dump dump config settings and exit
47 configuration options:
55 base [ create | boot ] ...
59 create | update_hosts | boot | setup ] ...
64 create cluster [ CLUSTERNAME ]
67 create node CLUSTERNAME IP_OFFSET
68 (re)create a single cluster node
71 mount a qemu disk on mnt/
74 unmount a qemu disk from mnt/
82 ###############################
85 if [ "$no_sanity" = 1 ] ; then
86 fill_text 0 "WARNING: $*" >&2
88 fill_text 0 "ERROR: $*" >&2
95 echo "######################################################################"
96 printf "# %-66s #\n" "$*"
97 echo "######################################################################"
107 local tmpfile=$(mktemp)
109 cat <<EOF >"$tmpfile"
110 spawn tail -n 10000 -f $file
111 expect -timeout $timeout -re "$msg"
118 if ! grep -E "$msg" "$file" > /dev/null; then
119 echo "Failed to find \"$msg\" in \"$file\""
126 ###############################
128 # Indirectly call a function named by ${1}_${2}
130 local func="$1" ; shift
131 local type="$1" ; shift
133 local f="${func}_${type}"
134 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
138 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
141 die "No function defined for \"${func}\" \"${type}\""
146 # Note that this will work if you pass "call_func f" because the first
147 # element of the node tuple is the node type. Nice... :-)
152 "$@" $(IFS=: ; echo $n)
156 node_is_ctdb_node_DEFAULT ()
161 hack_one_node_with ()
163 local filter="$1" ; shift
172 local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
173 nodes="${nodes}${nodes:+ }${item}"
176 # This also gets used for non-filtering iteration.
177 hack_all_nodes_with ()
182 for_each_node hack_one_node_with "$filter"
191 eval "$hook_var=\"${!hook_var}${!hook_var:+ }${new_hook}\""
200 for i in ${!hook_var} ; do
205 # Use with care, since this may clear some autocluster defaults.!
210 eval "$hook_var=\"\""
213 ##############################
215 # These hooks are intended to customise the value of $DISK. They have
216 # access to 1 argument ("base", "system", "shared") and the variables
217 # $VIRTBASE, $CLUSTER, $BASENAME (for "base"), $NAME (for "system"),
218 # $SHARED_DISK_NUM (for "shared"). A hook must be deterministic and
219 # should not be stateful, since they can be called multiple times for
223 create_node_DEFAULT ()
230 echo "Creating node \"$name\" (of type \"${type}\")"
232 create_node_COMMON "$name" "$ip_offset" "$type"
235 # common node creation stuff
236 create_node_COMMON ()
241 local template_file="${4:-$NODE_TEMPLATE}"
243 if [ "$SYSTEM_DISK_FORMAT" != "qcow2" -a "$BASE_FORMAT" = "qcow2" ] ; then
244 die "Error: if BASE_FORMAT is \"qcow2\" then SYSTEM_DISK_FORMAT must also be \"qcow2\"."
247 local IPNUM=$(($FIRSTIP + $ip_offset))
250 # Determine base image name. We use $DISK temporarily to allow
251 # the path to be hacked.
252 local DISK="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
253 if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
254 DISK="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
256 run_hooks hack_disk_hooks "base"
257 local base_disk="$DISK"
259 # Determine the system disk image name.
260 DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
261 run_hooks hack_disk_hooks "system"
264 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
265 di=$(readlink "$DISK")
268 local di_dirname="${di%/*}"
269 mkdir -p "$di_dirname"
271 case "$SYSTEM_DISK_FORMAT" in
273 echo "Creating the disk..."
274 qemu-img create -b "$base_disk" -f qcow2 "$di"
275 create_node_configure_image "$DISK" "$type"
278 echo "Creating the disk..."
279 cp -v --sparse=always "$base_disk" "$di"
280 create_node_configure_image "$DISK" "$type"
283 echo "Creating the disk..."
284 cp -v --reflink=always "$base_disk" "$di"
285 create_node_configure_image "$DISK" "$type"
288 echo "Creating the disk (using mmclone)..."
289 local base_snap="${base_disk}.snap"
290 [ -f "$base_snap" ] || mmclone snap "$base_disk" "$base_snap"
291 mmclone copy "$base_snap" "$di"
292 create_node_configure_image "$DISK" "$type"
295 echo "Skipping disk image creation as requested"
298 die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
301 # Pull the UUID for this node out of the map.
302 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
306 echo "Creating $NAME.xml"
307 substitute_vars $template_file tmp/$NAME.xml
309 # install the XML file
310 $VIRSH undefine $NAME > /dev/null 2>&1 || true
311 $VIRSH define tmp/$NAME.xml
314 create_node_configure_image ()
319 diskimage mount "$disk"
324 hack_network_map_hooks=""
326 # Uses: CLUSTER, NAME, NETWORKS, FIRSTIP, ip_offset
329 network_map="tmp/network_map.$NAME"
331 if [ -n "$CLUSTER" ] ; then
332 local md5=$(echo "$CLUSTER" | md5sum)
333 local nh=$(printf "%02x" $ip_offset)
334 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
336 local mac_prefix="02:42:42:00:00:"
341 for n in $NETWORKS ; do
342 local ch=$(printf "%02x" $count)
343 local mac="${mac_prefix}${ch}"
346 local ip_bits="$1" ; shift
347 local dev="$1" ; shift
350 local net="${ip_bits%/*}"
351 local netname="acnet_${net//./_}"
353 local ip="${net%.*}.${IPNUM}/${ip_bits#*/}"
355 # This can be used to override the variables in the echo
356 # statement below. The hook can use any other variables
357 # available in this function.
358 run_hooks hack_network_map_hooks
360 echo "${netname} ${dev} ${ip} ${mac} ${opts}"
361 count=$(($count + 1))
365 ##############################
369 # Expand out any abbreviations in NODES.
378 for i in ${ips//,/ } ; do
379 ns="${ns}${ns:+ }${t}:${i}"
384 for i in $(seq ${ips/-/ }) ; do
385 ns="${ns}${ns:+ }${t}:${i}"
389 ns="${ns}${ns:+ }${n}"
394 # Check IP addresses for duplicates.
396 # This function doesn't modify anything...
399 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
400 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
401 ip_offsets="${ip_offsets}${ip_offset}:"
403 hack_all_nodes_with get_ip_offset
405 # Determine node names and whether they're in the CTDB cluster
406 declare -A node_count
407 _get_name_ctdb_node ()
409 local count=$((${node_count[$node_type]:-0} + 1))
410 node_count[$node_type]=$count
411 name=$(call_func node_name_format "$node_type" "$CLUSTER" $count) || {
412 echo "ERROR: Node type \"${node_type}\" not defined!"
413 echo "Valid node types are:"
414 set | sed -n 's@^node_name_format_\(.*\) ().*@ \1@p'
417 ctdb_node=$(call_func node_is_ctdb_node "$node_type")
419 hack_all_nodes_with _get_name_ctdb_node
422 ##############################
424 sanity_check_cluster_name ()
426 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
427 die "Cluster names should be restricted to the characters A-Za-z0-9. \
428 Some cluster filesystems have problems with other characters."
433 cluster_nodelist_hacking ()
435 # Rework the NODES list
438 # Build /etc/hosts and hack the names of the ctdb nodes
439 hosts_line_hack_name ()
443 local ip_addr="${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
445 # Primary name for CTDB nodes is <CLUSTER>n<num>
446 if [ "$ctdb_node" = 1 ] ; then
447 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
448 sname="${CLUSTER}n${num_ctdb_nodes}"
449 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
452 hosts_line="$ip_addr ${name}.${ld} $name"
455 # This allows you to add a function to your configuration file
456 # to modify hostnames (and other aspects of nodes). This
457 # function can access/modify $name (the existing name),
458 # $node_type and $ctdb_node (1, if the node is a member of the
459 # CTDB cluster, 0 otherwise).
460 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
461 local old_name="$name"
462 $HOSTNAME_HACKING_FUNCTION
463 if [ "$name" != "$old_name" ] ; then
464 hosts_line="$ip_addr ${name}.${ld} $name"
470 hosts_file="tmp/hosts.$CLUSTER"
472 local num_ctdb_nodes=0
473 local ld=$(echo $DOMAIN | tr A-Z a-z)
474 echo "# autocluster $CLUSTER"
475 hack_all_nodes_with hosts_line_hack_name
479 # Build /etc/ctdb/nodes
482 [ "$ctdb_node" = 1 ] || return 0
483 echo "${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
484 num_nodes=$(($num_nodes + 1))
486 nodes_file="tmp/nodes.$CLUSTER"
488 hack_all_nodes_with ctdb_nodes_line >$nodes_file
491 uuid_map="tmp/uuid_map.$CLUSTER"
494 echo "${ip_offset} $(uuidgen) ${node_type}"
496 hack_all_nodes_with uuid_map_line >$uuid_map
499 create_cluster_hooks=
500 cluster_created_hooks=
504 # Use $1. If not set then use value from configuration file.
505 CLUSTER="${1:-${CLUSTER}}"
506 announce "cluster create \"${CLUSTER}\""
507 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
509 sanity_check_cluster_name
511 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
513 # Run hooks before doing anything else.
514 run_hooks create_cluster_hooks
516 for_each_node call_func create_node
518 echo "Cluster $CLUSTER created"
521 run_hooks cluster_created_hooks
524 cluster_created_hosts_message ()
526 echo "You may want to add this to your /etc/hosts file:"
530 register_hook cluster_created_hooks cluster_created_hosts_message
534 announce "cluster destroy \"${CLUSTER}\""
535 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
537 vircmd destroy "$CLUSTER" || true
542 announce "cluster undefine \"${CLUSTER}\""
543 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
545 vircmd undefine "$CLUSTER" || true
548 cluster_update_hosts ()
550 announce "cluster update_hosts \"${CLUSTER}\""
551 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
553 [ -n "$hosts_file" ] || hosts_file="tmp/hosts.${CLUSTER}"
554 [ -r "$hosts_file" ] || die "Missing hosts file \"${hosts_file}\""
556 # Building a general node name regexp is a bit cumbersome. :-)
557 local name_regexp="("
558 for i in $(set | sed -n -e "s@^\(node_name_format_.*\) ().*@\1@p") ; do
559 # Format node name with placeholders (remembering that "_" is
560 # not valid in a real cluster name)
561 local t=$("$i" "_" "0")
562 # now replace the placeholders with regexps - order is
563 # important here, since the cluster name can contain digits
564 t=$(sed -r -e "s@[[:digit:]]+@[[:digit:]]+@" -e "s@_@${CLUSTER}@" <<<"$t")
566 name_regexp="${name_regexp}${t}|"
568 name_regexp="${name_regexp}${CLUSTER}n[[:digit:]]+)"
570 local pat="# autocluster ${CLUSTER}\$|[[:space:]]${name_regexp}"
572 local t="/etc/hosts.${CLUSTER}"
573 grep -E "$pat" /etc/hosts >"$t" || true
574 if diff -B "$t" "$hosts_file" >/dev/null ; then
579 local old=/etc/hosts.old.autocluster
581 local new=/etc/hosts.new
582 grep -Ev "$pat" "$old" |
583 cat -s - "$hosts_file" >"$new"
587 echo "Made these changes to /etc/hosts:"
588 diff -u "$old" /etc/hosts || true
593 [ -n "$CLUSTER_PATTERN" ] || CLUSTER_PATTERN="$CLUSTER"
594 announce "cluster boot \"${CLUSTER_PATTERN}\""
595 [ -n "$CLUSTER_PATTERN" ] || die "\$CLUSTER_PATTERN not set"
597 vircmd start "$CLUSTER_PATTERN"
599 local nodes=$(vircmd dominfo "$CLUSTER_PATTERN" 2>/dev/null | \
600 sed -n -e 's/Name: *//p')
605 waitfor "${KVMLOG}/serial.$i" "login:" 300 || {
606 vircmd destroy "$CLUSTER_PATTERN"
607 die "Failed to create cluster"
611 # Move past the last line of log output
615 cluster_setup_tasks_DEFAULT ()
619 # By default nodes have no tasks
621 install_packages) echo "" ;;
622 setup_clusterfs) echo "" ;;
623 setup_node) echo "" ;;
624 setup_cluster) echo "" ;;
630 announce "cluster setup \"${CLUSTER}\""
631 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
633 local ssh="ssh -o StrictHostKeyChecking=no"
634 local setup_clusterfs_done=false
635 local setup_cluster_done=false
637 _cluster_setup_do_stage ()
645 local tasks=$(call_func cluster_setup_tasks "$type" "$stage")
647 if [ -n "$tasks" ] ; then
648 # These tasks are only done on 1 node
651 if $setup_clusterfs_done ; then
654 setup_clusterfs_done=true
658 if $setup_cluster_done ; then
661 setup_cluster_done=true
666 $ssh "$name" ./scripts/cluster_setup.sh "$stage" $tasks
671 local stages="install_packages setup_clusterfs setup_node setup_cluster"
673 for stage in $stages ; do
674 for_each_node _cluster_setup_do_stage "$stage"
681 local single_node_ip_offset="$2"
683 sanity_check_cluster_name
685 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
688 set -- $(IFS=: ; echo $n)
689 [ $single_node_ip_offset -eq $2 ] || continue
690 call_func create_node "$@"
692 echo "Requested node created"
694 echo "You may want to update your /etc/hosts file:"
701 ###############################
702 # test the proxy setup
704 export http_proxy=$WEBPROXY
705 wget -O /dev/null $INSTALL_SERVER || \
706 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
712 kickstart_floppy_create_hooks=
714 guess_install_network ()
716 # Figure out IP address to use during base install. Default to
717 # the IP address of the 1st (private) network. If a gateway is
718 # specified then use the IP address associated with it.
721 local netname dev ip mac opts
722 while read netname dev ip mac opts; do
727 INSTALL_GW="${o#gw=}"
731 [ -n "$INSTALL_IP" ] || INSTALL_IP="$ip"
738 local NAME="$BASENAME"
739 local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
740 run_hooks hack_disk_hooks "base"
744 echo "Testing WEBPROXY $WEBPROXY"
748 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
749 di=$(readlink "$DISK")
752 local di_dirname="${di%/*}"
753 mkdir -p "$di_dirname"
755 echo "Creating the disk"
756 qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
759 mkdir -p mnt tmp tmp/ISO
766 guess_install_network
768 echo "Creating kickstart file from template"
769 substitute_vars "$KICKSTART" "tmp/ks.cfg"
771 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
774 (*) ISO="${ISO_DIR}/${ISO}"
777 echo "Creating kickstart floppy"
778 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
779 mkdosfs -n KICKSTART tmp/floppy.img
780 mount -o loop -t msdos tmp/floppy.img mnt
782 mount -o loop,ro $ISO tmp/ISO
784 echo "Setting up bootloader"
785 cp tmp/ISO/isolinux/isolinux.bin tmp
786 cp tmp/ISO/isolinux/vmlinuz tmp
787 cp tmp/ISO/isolinux/initrd.img tmp
789 run_hooks kickstart_floppy_create_hooks
796 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
798 rm -f $KVMLOG/serial.$NAME
800 # boot the install CD
801 $VIRSH create tmp/$NAME.xml
803 echo "Waiting for install to start"
806 # wait for the install to finish
807 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
809 die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
810 You may need to increase the value of CREATE_BASE_TIMEOUT.
811 Alternatively, the install might have completed but KS_DONE_MESSAGE
812 (currently \"${KS_DONE_MESSAGE}\")
813 may not have matched anything at the end of the kickstart output."
821 Install finished, base image $DISK created
824 chcon -t virt_content_t $DISK
826 To ensure that this image does not change
828 Note that the root password has been set to $ROOTPASSWORD
833 ###############################
840 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
848 diskimage mount $DISK
854 echo "Creating $NAME.xml"
855 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
857 # boot the base system
858 $VIRSH create tmp/$NAME.xml
861 ######################################################################
863 # Updating a disk image...
869 call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
872 # setup the files from $BASE_TEMPLATES/, substituting any variables
873 # based on the config
874 copy_base_dir_substitute_templates ()
878 local d="$BASE_TEMPLATES/$dir"
879 [ -d "$d" ] || return 0
882 for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
883 f="${f#./}" # remove leading "./" for clarity
884 if [ -d "$d/$f" ]; then
885 # Don't chmod existing directory
886 if diskimage is_directory "/$f" ; then
889 diskimage mkdir_p "/$f"
892 diskimage substitute_vars "$d/$f" "/$f"
894 diskimage chmod_reference "$d/$f" "/$f"
900 setup_base_ssh_keys ()
902 # this is needed as git doesn't store file permissions other
904 # Note that we protect the wildcards from the local shell.
905 diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
906 diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
907 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
908 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
909 diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
911 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
912 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
913 diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
917 register_hook setup_base_hooks setup_base_ssh_keys
919 setup_base_grub_conf ()
921 echo "Adjusting grub.conf"
922 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
923 local grub_configs="/boot/grub/grub.conf"
924 if ! diskimage is_file "$grub_configs" ; then
925 grub_configs="/etc/default/grub /boot/grub2/grub.cfg"
928 for c in $grub_configs ; do
930 -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
931 -e "s/ console=tty1//" -e "s/ rhgb/ norhgb/" \
932 -e "s/ nodmraid//" -e "s/ nompath//" \
933 -e "s/quiet/noapic divider=10${o:+ }${o}/g"
937 register_hook setup_base_hooks setup_base_grub_conf
944 echo "Copy base files"
945 copy_base_dir_substitute_templates "all"
946 if [ -n "$type" ] ; then
947 copy_base_dir_substitute_templates "$type"
950 run_hooks setup_base_hooks
953 ipv4_prefix_to_netmask ()
957 local div=$(($prefix / 8))
958 local mod=$(($prefix % 8))
961 for octet in 1 2 3 4 ; do
962 if [ $octet -le $div ] ; then
964 elif [ $mod -ne 0 -a $octet -eq $(($div + 1)) ] ; then
965 local shift=$((8 - $mod))
966 echo -n $(( (255 >> $shift << $shift) ))
970 if [ $octet -lt 4 ] ; then
978 # setup various networking components
981 # This avoids doing anything when we're called from boot_base().
982 if [ -z "$hosts_file" ] ; then
983 echo "Skipping network-related setup"
987 echo "Setting up networks"
988 diskimage append_text_file "$hosts_file" "/etc/hosts"
990 echo "Setting up /etc/ctdb/nodes"
991 diskimage mkdir_p "/etc/ctdb"
992 diskimage put "$nodes_file" "/etc/ctdb/nodes"
994 [ "$WEBPROXY" = "" ] || {
995 diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
998 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
999 echo "Enabling nfs mount of $NFSSHARE"
1000 diskimage mkdir_p "$NFS_MOUNTPOINT"
1001 diskimage append_text "$NFSSHARE $NFS_MOUNTPOINT nfs nfsvers=3,intr 0 0" "/etc/fstab"
1004 diskimage mkdir_p "/etc/yum.repos.d"
1005 echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
1007 diskimage rm_rf "/etc/udev/rules.d/70-persistent-net.rules"
1009 echo "Setting up network interfaces: "
1010 local netname dev ip mac opts
1011 while read netname dev ip mac opts; do
1014 local o gw addr mask
1024 mask=$(ipv4_prefix_to_netmask ${ip#*/})
1027 diskimage put - "/etc/sysconfig/network-scripts/ifcfg-${dev}"
1034 ${gw:+GATEWAY=}${gw}
1037 # This goes to 70-persistent-net.rules
1039 # Generated by autocluster
1040 SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${mac}", ATTR{type}=="1", KERNEL=="eth*", NAME="${dev}"
1043 done <"$network_map" |
1044 diskimage put - "/etc/udev/rules.d/70-persistent-net.rules"
1047 register_hook setup_base_hooks setup_network
1049 setup_base_cluster_setup_config ()
1053 echo "# Generated by autocluster"
1055 # This is a bit of a hack. Perhaps these script belong
1056 # elsewhere, since they no longer have templates?
1057 for f in $(find "${BASE_TEMPLATES}/all/root/scripts" -type f |
1058 xargs grep -l '^#config:') ; do
1063 vs=$(sed -n 's@^#config: *@@p' "$f")
1065 # This could substitute the values in directly using
1066 # ${!v} but then no sanity checking is done to make
1067 # sure variables are set.
1068 echo "${v}=\"@@${v}@@\""
1072 } | diskimage substitute_vars - "/root/scripts/cluster_setup.config"
1075 register_hook setup_base_hooks setup_base_cluster_setup_config
1078 [ -z "$TIMEZONE" ] && {
1079 [ -r /etc/timezone ] && {
1080 TIMEZONE=`cat /etc/timezone`
1082 [ -r /etc/sysconfig/clock ] && {
1083 . /etc/sysconfig/clock
1086 TIMEZONE="${TIMEZONE// /_}"
1088 [ -n "$TIMEZONE" ] || \
1089 die "Unable to determine TIMEZONE - please set in config"
1092 # substite a set of variables of the form @@XX@@ for the shell
1093 # variables $XX in a file.
1095 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
1096 # contain a filename whose contents are substituted, with variable
1097 # substitution applied to those contents. If filename starts with '|'
1098 # it is a command instead - however, quoting is extremely fragile.
1099 substitute_vars() {(
1100 infile="${1:-/dev/null}" # if empty then default to /dev/null
1101 outfile="$2" # optional
1104 cat "$infile" >"$tmp_out"
1106 # Handle any indirects by looping until nothing changes.
1107 # However, only handle 10 levels of recursion.
1110 if ! _substitute_vars "$tmp_out" "@@@" ; then
1112 die "Failed to expand template $infile"
1115 # No old version of file means no changes made.
1116 if [ ! -f "${tmp_out}.old" ] ; then
1120 rm -f "${tmp_out}.old"
1122 count=$(($count + 1))
1123 if [ $count -ge 10 ] ; then
1125 die "Recursion too deep in $infile - only 10 levels allowed!"
1129 # Now regular variables.
1130 if ! _substitute_vars "$tmp_out" "@@" ; then
1132 die "Failed to expand template $infile"
1134 rm -f "${tmp_out}.old"
1136 if [ -n "$outfile" ] ; then
1137 mv "$tmp_out" "$outfile"
1145 # Delimiter @@ means to substitute contents of variable.
1146 # Delimiter @@@ means to substitute contents of file named by variable.
1147 # @@@ supports leading '|' in variable value, which means to excute a
1149 _substitute_vars() {(
1151 delimiter="${2:-@@}"
1153 # Get the list of variables used in the template. The grep
1154 # gets rid of any blank lines and lines with extraneous '@'s
1155 # next to template substitutions.
1156 VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
1157 grep '^[A-Z0-9_][A-Z0-9_]*$' |
1162 # variable variables are fun .....
1165 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
1169 if [ "$delimiter" = "@@@" ] ; then
1171 c="${f#|}" # Is is a command, signified by a leading '|'?
1172 if [ "$c" = "$f" ] ; then
1173 # No leading '|', cat file.
1177 die "Could not substitute contents of file $f"
1180 # Leading '|', execute command.
1181 # Quoting problems here - using eval "$c" doesn't help.
1185 die "Could not execute command $c"
1190 # escape some pesky chars
1191 # This first one can be too slow if done using a bash
1192 # variable pattern subsitution.
1193 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
1196 echo "s#${delimiter}${v}${delimiter}#${s}#g"
1199 # Get the in-place sed to make a backup of the old file.
1200 # Remove the backup if it is the same as the resulting file -
1201 # this acts as a flag to the caller that no changes were made.
1202 sed -i.old -f $tmp "$tmp_out"
1203 if cmp -s "${tmp_out}.old" "$tmp_out" ; then
1204 rm -f "${tmp_out}.old"
1211 which $1 > /dev/null || die "Please install $1 to continue"
1214 # Set a variable if it isn't already set. This allows environment
1215 # variables to override default config settings.
1220 [ "${!v+x}" ] || eval "$v=\"$e\""
1226 for i in "${installdir}/config.d/"*.defconf ; do
1231 # Print the list of config variables defined in config.d/.
1232 get_config_options () {( # sub-shell for local declaration of defconf()
1234 defconf() { options="$options $1" ; }
1239 # Produce a list of long options, suitable for use with getopt, that
1240 # represent the config variables defined in config.d/.
1241 getopt_config_options () {
1242 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
1246 # Unconditionally set the config variable associated with the given
1248 setconf_longopt () {
1252 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
1253 # unset so defconf will set it
1258 # Dump all of the current config variables.
1261 for o in $(get_config_options) ; do
1262 echo "${o}=\"${!o}\""
1267 # $COLUMNS is set in interactive bash shells. It probably isn't set
1268 # in this shell, so let's set it if it isn't.
1269 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
1273 # Print text assuming it starts after other text in $startcol and
1274 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
1275 # Long "words" will extend past $COLUMNS - 2.
1280 local width=$(($COLUMNS - 2 - $startcol))
1281 [ $width -lt 0 ] && width=$((78 - $startcol))
1286 if [ $startcol -gt 0 ] ; then
1287 padding=$(printf "\n%${startcol}s" " ")
1293 while [ -n "$text" ] ; do
1296 # If we already have output then arrange padding on the next line.
1297 [ -n "$out" ] && out="${out}${padding}"
1299 # Break the text at $width.
1300 out="${out}${text:0:${width}}"
1301 text="${text:${width}}"
1303 # If we have left over text then the line break may be ugly,
1304 # so let's check and try to break it on a space.
1305 if [ -n "$text" ] ; then
1306 # The 'x's stop us producing a special character like '(',
1307 # ')' or '!'. Yuck - there must be a better way.
1308 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1309 # We didn't break on a space. Arrange for the
1310 # beginning of the broken "word" to appear on the next
1311 # line but not if it will make us loop infinitely.
1312 if [ "${orig}" != "${out##* }${text}" ] ; then
1313 text="${out##* }${text}"
1316 # Hmmm, doing that would make us loop, so add the
1317 # rest of the word from the remainder of the text
1318 # to this line and let it extend past $COLUMNS - 2.
1319 out="${out}${text%% *}"
1320 if [ "${text# *}" != "$text" ] ; then
1321 # Remember the text after the next space for next time.
1324 # No text after next space.
1329 # We broke on a space. If it will be at the beginning
1330 # of the next line then remove it.
1339 # Display usage text, trying these approaches in order.
1340 # 1. See if it all fits on one line before $COLUMNS - 2.
1341 # 2. See if splitting before the default value and indenting it
1342 # to $startcol means that nothing passes $COLUMNS - 2.
1343 # 3. Treat the message and default value as a string and just us fill_text()
1345 usage_display_text () {
1350 local width=$(($COLUMNS - 2 - $startcol))
1351 [ $width -lt 0 ] && width=$((78 - $startcol))
1353 default="(default \"$default\")"
1355 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1356 echo "${desc} ${default}"
1358 local padding=$(printf "%${startcol}s" " ")
1360 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1362 echo "${padding}${default}"
1364 fill_text $startcol "${desc} ${default}"
1369 # Display usage information for long config options.
1370 usage_smart_display () {( # sub-shell for local declaration of defconf()
1374 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1376 printf " --%-25s " "${longopt}=${3}"
1378 usage_display_text $startcol "$4" "$2"
1385 # Display usage information for long config options.
1386 usage_config_options (){
1387 usage_smart_display load_config
1397 actions="${actions}${actions:+ }$*"
1402 [ -n "$actions" ] || usage
1405 for a in $actions ; do
1410 ######################################################################
1414 ######################################################################
1418 ############################
1419 # parse command line options
1420 long_opts=$(getopt_config_options)
1421 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump -l "$long_opts" -- "$@")
1422 [ $? != 0 ] && usage
1424 use_default_config=true
1426 # We do 2 passes of the options. The first time we just handle usage
1427 # and check whether -c is being used.
1428 eval set -- "$getopt_output"
1431 -c) shift 2 ; use_default_config=false ;;
1434 --) shift ; break ;;
1436 -h|--help) usage ;; # Usage should be shown here for real defaults.
1437 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1438 *) usage ;; # shouldn't happen, so this is reasonable.
1443 $use_default_config && [ -r "$config" ] && . "$config"
1445 eval set -- "$getopt_output"
1451 # force at least ./local_file to avoid accidental file
1453 . "$(dirname $2)/${b}"
1454 # If $CLUSTER is unset then try to base it on the filename
1455 if [ ! -n "$CLUSTER" ] ; then
1458 CLUSTER="${b%.autocluster}"
1463 -e) no_sanity=1 ; run_hooks post_config_hooks ; eval "$2" ; exit ;;
1464 -E) eval "$2" ; shift 2 ;;
1465 -x) set -x; shift ;;
1466 --dump) no_sanity=1 ; run_hooks post_config_hooks ; dump_config ;;
1467 --) shift ; break ;;
1468 -h|--help) usage ;; # Redundant.
1470 # Putting --opt1|opt2|... into a variable and having case
1471 # match against it as a pattern doesn't work. The | is
1472 # part of shell syntax, so we need to do this. Look away
1473 # now to stop your eyes from bleeding! :-)
1474 x=",${long_opts}" # Now each option is surrounded by , and :
1475 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1476 # Our option, $1, surrounded by , and : was in $x, so is legal.
1477 setconf_longopt "$1" "$2"; shift 2
1482 *) usage ;; # shouldn't happen, so this is reasonable.
1486 run_hooks post_config_hooks
1492 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1495 # check for needed programs
1496 check_command expect
1498 [ $# -lt 1 ] && usage
1508 create|boot) actions_add "base_${t}" ;;
1519 destroy|undefine|create|update_hosts|boot|setup)
1520 actions_add "cluster_${t}" ;;
1522 for t in destroy undefine create update_hosts boot setup ; do
1523 actions_add "cluster_${t}"
1529 cluster_nodelist_hacking
1538 [ $# != 0 ] && usage
1542 [ $# != 1 ] && usage
1546 [ $# != 2 ] && usage
1547 create_one_node "$1" "$2"
1555 [ $# != 1 ] && usage
1556 diskimage mount "$1"
1559 [ $# != 0 ] && usage