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 ] ...
57 cluster [ destroy | create | update_hosts | boot | configure ] ...
62 create cluster [ CLUSTERNAME ]
65 create node CLUSTERNAME IP_OFFSET
66 (re)create a single cluster node
69 mount a qemu disk on mnt/
72 unmount a qemu disk from mnt/
80 ###############################
83 if [ "$no_sanity" = 1 ] ; then
84 fill_text 0 "WARNING: $*" >&2
86 fill_text 0 "ERROR: $*" >&2
93 echo "######################################################################"
94 printf "# %-66s #\n" "$*"
95 echo "######################################################################"
99 ###############################
101 # Indirectly call a function named by ${1}_${2}
103 local func="$1" ; shift
104 local type="$1" ; shift
106 local f="${func}_${type}"
107 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
111 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
114 die "No function defined for \"${func}\" \"${type}\""
119 # Note that this will work if you pass "call_func f" because the first
120 # element of the node tuple is the node type. Nice... :-)
125 "$@" $(IFS=: ; echo $n)
129 hack_one_node_with ()
131 local filter="$1" ; shift
140 local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
141 nodes="${nodes}${nodes:+ }${item}"
144 # This also gets used for non-filtering iteration.
145 hack_all_nodes_with ()
150 for_each_node hack_one_node_with "$filter"
159 eval "$hook_var=\"${!hook_var}${!hook_var:+ }${new_hook}\""
168 for i in ${!hook_var} ; do
173 # Use with care, since this may clear some autocluster defaults.!
178 eval "$hook_var=\"\""
181 ##############################
183 # These hooks are intended to customise the value of $DISK. They have
184 # access to 1 argument ("base", "system", "shared") and the variables
185 # $VIRTBASE, $CLUSTER, $BASENAME (for "base"), $NAME (for "system"),
186 # $SHARED_DISK_NUM (for "shared"). A hook must be deterministic and
187 # should not be stateful, since they can be called multiple times for
191 # common node creation stuff
192 create_node_COMMON ()
197 local template_file="${4:-$NODE_TEMPLATE}"
199 if [ "$SYSTEM_DISK_FORMAT" != "qcow2" -a "$BASE_FORMAT" = "qcow2" ] ; then
200 die "Error: if BASE_FORMAT is \"qcow2\" then SYSTEM_DISK_FORMAT must also be \"qcow2\"."
203 local IPNUM=$(($FIRSTIP + $ip_offset))
206 # Determine base image name. We use $DISK temporarily to allow
207 # the path to be hacked.
208 local DISK="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
209 if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
210 DISK="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
212 run_hooks hack_disk_hooks "base"
213 local base_disk="$DISK"
215 # Determine the system disk image name.
216 DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
217 run_hooks hack_disk_hooks "system"
220 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
221 di=$(readlink "$DISK")
224 local di_dirname="${di%/*}"
225 mkdir -p "$di_dirname"
227 case "$SYSTEM_DISK_FORMAT" in
229 echo "Creating the disk..."
230 qemu-img create -b "$base_disk" -f qcow2 "$di"
231 create_node_configure_image "$DISK" "$type"
234 echo "Creating the disk..."
235 cp -v --sparse=always "$base_disk" "$di"
236 create_node_configure_image "$DISK" "$type"
239 echo "Creating the disk..."
240 cp -v --reflink=always "$base_disk" "$di"
241 create_node_configure_image "$DISK" "$type"
244 echo "Creating the disk (using mmclone)..."
245 local base_snap="${base_disk}.snap"
246 [ -f "$base_snap" ] || mmclone snap "$base_disk" "$base_snap"
247 mmclone copy "$base_snap" "$di"
248 create_node_configure_image "$DISK" "$type"
251 echo "Skipping disk image creation as requested"
254 die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
257 # Pull the UUID for this node out of the map.
258 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
262 echo "Creating $NAME.xml"
263 substitute_vars $template_file tmp/$NAME.xml
265 # install the XML file
266 $VIRSH undefine $NAME > /dev/null 2>&1 || true
267 $VIRSH define tmp/$NAME.xml
270 create_node_configure_image ()
275 diskimage mount "$disk"
280 # Provides an easy way of removing nodes from $NODE.
281 create_node_null () {
285 hack_network_map_hooks=""
287 # Uses: CLUSTER, NAME, NETWORKS, FIRSTIP, ip_offset
290 network_map="tmp/network_map.$NAME"
292 if [ -n "$CLUSTER" ] ; then
293 local md5=$(echo "$CLUSTER" | md5sum)
294 local nh=$(printf "%02x" $ip_offset)
295 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
297 local mac_prefix="02:42:42:00:00:"
302 for n in $NETWORKS ; do
303 local ch=$(printf "%02x" $count)
304 local mac="${mac_prefix}${ch}"
307 local ip_bits="$1" ; shift
308 local dev="$1" ; shift
311 local net="${ip_bits%/*}"
312 local netname="acnet_${net//./_}"
314 local ip="${net%.*}.${IPNUM}"
315 local mask="255.255.255.0"
317 # This can be used to override the variables in the echo
318 # statement below. The hook can use any other variables
319 # available in this function.
320 run_hooks hack_network_map_hooks
322 echo "${netname} ${dev} ${ip} ${mask} ${mac} ${opts}"
323 count=$(($count + 1))
327 ##############################
329 hack_nodes_functions=
332 # Expand out any abbreviations in NODES.
341 for i in ${ips//,/ } ; do
342 ns="${ns}${ns:+ }${t}:${i}"
347 for i in $(seq ${ips/-/ }) ; do
348 ns="${ns}${ns:+ }${t}:${i}"
352 ns="${ns}${ns:+ }${n}"
357 # Apply nodes hacks. Some of this is about backward compatibility
358 # but the hacks also fill in the node names and whether they're
359 # part of the CTDB cluster. The order is the order that
360 # configuration modules register their hacks.
361 run_hooks hack_nodes_functions
363 if [ -n "$NUMNODES" ] ; then
364 # Attempt to respect NUMNODES. Reduce the number of CTDB
366 local numnodes=$NUMNODES
370 if [ "$ctdb_node" = 1 ] ; then
371 if [ $numnodes -gt 0 ] ; then
372 numnodes=$(($numnodes - 1))
380 hack_all_nodes_with hack_filter
382 [ $numnodes -gt 0 ] && \
383 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."
386 # Check IP addresses for duplicates.
388 # This function doesn't modify anything...
391 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
392 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
393 ip_offsets="${ip_offsets}${ip_offset}:"
395 hack_all_nodes_with get_ip_offset
398 ##############################
400 sanity_check_cluster_name ()
402 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
403 die "Cluster names should be restricted to the characters A-Za-z0-9. \
404 Some cluster filesystems have problems with other characters."
409 common_nodelist_hacking ()
411 # Rework the NODES list
414 # Build /etc/hosts and hack the names of the ctdb nodes
415 hosts_line_hack_name ()
417 # Ignore nodes without names (e.g. "null")
418 [ "$node_type" != "null" -a -n "$name" ] || return 0
422 local ip_addr="${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
424 if [ "$ctdb_node" = 1 ] ; then
425 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
426 sname="${CLUSTER}n${num_ctdb_nodes}"
427 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
430 hosts_line="$ip_addr ${name}.${ld} $name"
433 # This allows you to add a function to your configuration file
434 # to modify hostnames (and other aspects of nodes). This
435 # function can access/modify $name (the existing name),
436 # $node_type and $ctdb_node (1, if the node is a member of the
437 # CTDB cluster, 0 otherwise).
438 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
439 local old_name="$name"
440 $HOSTNAME_HACKING_FUNCTION
441 if [ "$name" != "$old_name" ] ; then
442 hosts_line="$ip_addr ${name}.${ld} $name"
448 hosts_file="tmp/hosts.$CLUSTER"
450 local num_ctdb_nodes=0
451 local ld=$(echo $DOMAIN | tr A-Z a-z)
452 echo "# autocluster $CLUSTER"
453 hack_all_nodes_with hosts_line_hack_name
457 # Build /etc/ctdb/nodes
460 [ "$ctdb_node" = 1 ] || return 0
461 echo "${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
462 num_nodes=$(($num_nodes + 1))
464 nodes_file="tmp/nodes.$CLUSTER"
466 hack_all_nodes_with ctdb_nodes_line >$nodes_file
467 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
470 uuid_map="tmp/uuid_map.$CLUSTER"
473 echo "${ip_offset} $(uuidgen) ${node_type}"
475 hack_all_nodes_with uuid_map_line >$uuid_map
478 create_cluster_hooks=
479 cluster_created_hooks=
483 # Use $1. If not set then use value from configuration file.
484 CLUSTER="${1:-${CLUSTER}}"
485 announce "cluster create \"${CLUSTER}\""
486 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
488 sanity_check_cluster_name
490 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
492 # Run hooks before doing anything else.
493 run_hooks create_cluster_hooks
495 common_nodelist_hacking
497 for_each_node call_func create_node
499 echo "Cluster $CLUSTER created"
502 run_hooks cluster_created_hooks
505 cluster_created_hosts_message ()
507 echo "You may want to add this to your /etc/hosts file:"
511 register_hook cluster_created_hooks cluster_created_hosts_message
515 announce "cluster destroy \"${CLUSTER}\""
516 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
518 vircmd destroy "$CLUSTER" || true
521 cluster_update_hosts ()
523 announce "cluster update_hosts \"${CLUSTER}\""
524 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
526 [ -n "$hosts_file" ] || hosts_file="tmp/hosts.${CLUSTER}"
527 [ -r "$hosts_file" ] || die "Missing hosts file \"${hosts_file}\""
529 local pat="# autocluster ${CLUSTER}\$|[[:space:]]${CLUSTER}(n|base)[[:digit:]]+"
531 local t="/etc/hosts.${CLUSTER}"
532 grep -E "$pat" /etc/hosts >"$t" || true
533 if diff -B "$t" "$hosts_file" >/dev/null ; then
538 local old=/etc/hosts.old.autocluster
540 local new=/etc/hosts.new
541 grep -Ev "$pat" "$old" |
542 cat -s - "$hosts_file" >"$new"
546 echo "Made these changes to /etc/hosts:"
547 diff -u "$old" /etc/hosts || true
552 [ -n "$CLUSTER_PATTERN" ] || CLUSTER_PATTERN="$CLUSTER"
553 announce "cluster boot \"${CLUSTER_PATTERN}\""
554 [ -n "$CLUSTER_PATTERN" ] || die "\$CLUSTER_PATTERN not set"
556 vircmd start "$CLUSTER_PATTERN"
558 local nodes=$(vircmd dominfo "$CLUSTER_PATTERN" 2>/dev/null | \
559 sed -n -e 's/Name: *//p')
564 waitfor "${KVMLOG}/serial.$i" "login:" 300 || {
565 vircmd destroy "$CLUSTER_PATTERN"
566 die "Failed to create cluster"
570 # Move past the last line of log output
576 announce "cluster configure \"${CLUSTER}\""
577 [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
579 local n1="${CLUSTER}n1"
580 local ssh="ssh -o StrictHostKeyChecking=no"
582 case "$CLUSTER_TYPE" in
584 # Build node doesn't really need CTDB/Samba to be
585 # installed, if the packages are not present in the repo,
587 $ssh "$n1" "./scripts/setup_build.sh || true"
591 $ssh "$n1" ./scripts/setup_ad_server.sh
595 [ -n "$CLUSTER_PATTERN" ] || CLUSTER_PATTERN="$CLUSTER"
597 local nodes=$(vircmd dominfo "$CLUSTER_PATTERN" 2>/dev/null | \
598 sed -n -e 's/Name: *//p')
601 $ssh "$i" ./scripts/install_gpfs_nas.sh
603 $ssh "$n1" ./scripts/setup_gpfs.sh
605 if [ "$AUTH_METHOD" = "winbind" ]; then
606 args="-UAdministrator%${AD_ADMIN_PASS}"
610 $ssh "$n1" ./scripts/setup_cluster.sh "$args"
618 local single_node_ip_offset="$2"
620 sanity_check_cluster_name
622 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
624 common_nodelist_hacking
627 set -- $(IFS=: ; echo $n)
628 [ $single_node_ip_offset -eq $2 ] || continue
629 call_func create_node "$@"
631 echo "Requested node created"
633 echo "You may want to update your /etc/hosts file:"
640 ###############################
641 # test the proxy setup
643 export http_proxy=$WEBPROXY
644 wget -O /dev/null $INSTALL_SERVER || \
645 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
651 kickstart_floppy_create_hooks=
653 guess_install_network ()
655 # Figure out IP address to use during base install. Default to
656 # the IP address of the 1st (private) network. If a gateway is
657 # specified then use the IP address associated with it.
660 local netname dev ip mask mac opts
661 while read netname dev ip mask mac opts; do
666 INSTALL_GW="${o#gw=}"
667 INSTALL_IP="${ip}${FIRSTIP}"
670 [ -n "$INSTALL_IP" ] || INSTALL_IP="$ip"
677 local NAME="$BASENAME"
678 local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
679 run_hooks hack_disk_hooks "base"
683 echo "Testing WEBPROXY $WEBPROXY"
687 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
688 di=$(readlink "$DISK")
691 local di_dirname="${di%/*}"
692 mkdir -p "$di_dirname"
694 echo "Creating the disk"
695 qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
698 mkdir -p mnt tmp tmp/ISO
704 guess_install_network
706 echo "Creating kickstart file from template"
707 substitute_vars "$KICKSTART" "tmp/ks.cfg"
709 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
712 (*) ISO="${ISO_DIR}/${ISO}"
715 echo "Creating kickstart floppy"
716 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
717 mkdosfs -n KICKSTART tmp/floppy.img
718 mount -o loop -t msdos tmp/floppy.img mnt
720 mount -o loop,ro $ISO tmp/ISO
722 echo "Setting up bootloader"
723 cp tmp/ISO/isolinux/isolinux.bin tmp
724 cp tmp/ISO/isolinux/vmlinuz tmp
725 cp tmp/ISO/isolinux/initrd.img tmp
727 run_hooks kickstart_floppy_create_hooks
734 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
736 rm -f $KVMLOG/serial.$NAME
738 # boot the install CD
739 $VIRSH create tmp/$NAME.xml
741 echo "Waiting for install to start"
744 # wait for the install to finish
745 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
747 die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
748 You may need to increase the value of CREATE_BASE_TIMEOUT.
749 Alternatively, the install might have completed but KS_DONE_MESSAGE
750 (currently \"${KS_DONE_MESSAGE}\")
751 may not have matched anything at the end of the kickstart output."
759 Install finished, base image $DISK created
762 chcon -t virt_content_t $DISK
764 To ensure that this image does not change
766 Note that the root password has been set to $ROOTPASSWORD
771 ###############################
778 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
786 diskimage mount $DISK
792 echo "Creating $NAME.xml"
793 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
795 # boot the base system
796 $VIRSH create tmp/$NAME.xml
799 ######################################################################
801 # Updating a disk image...
807 call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
810 # setup the files from $BASE_TEMPLATES/, substituting any variables
811 # based on the config
812 copy_base_dir_substitute_templates ()
816 local d="$BASE_TEMPLATES/$dir"
817 [ -d "$d" ] || return 0
820 for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
821 f="${f#./}" # remove leading "./" for clarity
822 if [ -d "$d/$f" ]; then
823 # Don't chmod existing directory
824 if diskimage is_directory "/$f" ; then
827 diskimage mkdir_p "/$f"
830 diskimage substitute_vars "$d/$f" "/$f"
832 diskimage chmod_reference "$d/$f" "/$f"
838 setup_base_ssh_keys ()
840 # this is needed as git doesn't store file permissions other
842 # Note that we protect the wildcards from the local shell.
843 diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
844 diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
845 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
846 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
847 diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
849 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
850 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
851 diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
855 register_hook setup_base_hooks setup_base_ssh_keys
857 setup_base_grub_conf ()
859 echo "Adjusting grub.conf"
860 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
861 local grub_configs="/boot/grub/grub.conf"
862 if ! diskimage is_file "$grub_configs" ; then
863 grub_configs="/etc/default/grub /boot/grub2/grub.cfg"
866 for c in $grub_configs ; do
868 -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
869 -e "s/ console=tty1//" -e "s/ rhgb/ norhgb/" \
870 -e "s/ nodmraid//" -e "s/ nompath//" \
871 -e "s/quiet/noapic divider=10${o:+ }${o}/g"
875 register_hook setup_base_hooks setup_base_grub_conf
882 echo "Copy base files"
883 copy_base_dir_substitute_templates "all"
884 if [ -n "$type" ] ; then
885 copy_base_dir_substitute_templates "$type"
888 run_hooks setup_base_hooks
891 # setup various networking components
894 # This avoids doing anything when we're called from boot_base().
895 if [ -z "$hosts_file" ] ; then
896 echo "Skipping network-related setup"
900 echo "Setting up networks"
901 diskimage append_text_file "$hosts_file" "/etc/hosts"
903 echo "Setting up /etc/ctdb/nodes"
904 diskimage mkdir_p "/etc/ctdb"
905 diskimage put "$nodes_file" "/etc/ctdb/nodes"
907 [ "$WEBPROXY" = "" ] || {
908 diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
911 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
912 echo "Enabling nfs mount of $NFSSHARE"
913 diskimage mkdir_p "$NFS_MOUNTPOINT"
914 diskimage append_text "$NFSSHARE $NFS_MOUNTPOINT nfs nfsvers=3,intr 0 0" "/etc/fstab"
917 diskimage mkdir_p "/etc/yum.repos.d"
918 echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
920 diskimage rm_rf "/etc/udev/rules.d/70-persistent-net.rules"
922 echo "Setting up network interfaces: "
923 local netname dev ip mask mac opts
924 while read netname dev ip mask mac opts; do
937 diskimage put - "/etc/sysconfig/network-scripts/ifcfg-${dev}"
947 # This goes to 70-persistent-net.rules
949 # Generated by autocluster
950 SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${mac}", ATTR{type}=="1", KERNEL=="eth*", NAME="${dev}"
953 done <"$network_map" |
954 diskimage put - "/etc/udev/rules.d/70-persistent-net.rules"
957 register_hook setup_base_hooks setup_network
960 [ -z "$TIMEZONE" ] && {
961 [ -r /etc/timezone ] && {
962 TIMEZONE=`cat /etc/timezone`
964 [ -r /etc/sysconfig/clock ] && {
965 . /etc/sysconfig/clock
968 TIMEZONE="${TIMEZONE// /_}"
970 [ -n "$TIMEZONE" ] || \
971 die "Unable to determine TIMEZONE - please set in config"
974 # substite a set of variables of the form @@XX@@ for the shell
975 # variables $XX in a file.
977 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
978 # contain a filename whose contents are substituted, with variable
979 # substitution applied to those contents. If filename starts with '|'
980 # it is a command instead - however, quoting is extremely fragile.
982 infile="${1:-/dev/null}" # if empty then default to /dev/null
983 outfile="$2" # optional
986 cat "$infile" >"$tmp_out"
988 # Handle any indirects by looping until nothing changes.
989 # However, only handle 10 levels of recursion.
992 if ! _substitute_vars "$tmp_out" "@@@" ; then
994 die "Failed to expand template $infile"
997 # No old version of file means no changes made.
998 if [ ! -f "${tmp_out}.old" ] ; then
1002 rm -f "${tmp_out}.old"
1004 count=$(($count + 1))
1005 if [ $count -ge 10 ] ; then
1007 die "Recursion too deep in $infile - only 10 levels allowed!"
1011 # Now regular variables.
1012 if ! _substitute_vars "$tmp_out" "@@" ; then
1014 die "Failed to expand template $infile"
1016 rm -f "${tmp_out}.old"
1018 if [ -n "$outfile" ] ; then
1019 mv "$tmp_out" "$outfile"
1027 # Delimiter @@ means to substitute contents of variable.
1028 # Delimiter @@@ means to substitute contents of file named by variable.
1029 # @@@ supports leading '|' in variable value, which means to excute a
1031 _substitute_vars() {(
1033 delimiter="${2:-@@}"
1035 # Get the list of variables used in the template. The grep
1036 # gets rid of any blank lines and lines with extraneous '@'s
1037 # next to template substitutions.
1038 VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
1039 grep '^[A-Z0-9_][A-Z0-9_]*$' |
1044 # variable variables are fun .....
1047 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
1051 if [ "$delimiter" = "@@@" ] ; then
1053 c="${f#|}" # Is is a command, signified by a leading '|'?
1054 if [ "$c" = "$f" ] ; then
1055 # No leading '|', cat file.
1059 die "Could not substitute contents of file $f"
1062 # Leading '|', execute command.
1063 # Quoting problems here - using eval "$c" doesn't help.
1067 die "Could not execute command $c"
1072 # escape some pesky chars
1073 # This first one can be too slow if done using a bash
1074 # variable pattern subsitution.
1075 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
1078 echo "s#${delimiter}${v}${delimiter}#${s}#g"
1081 # Get the in-place sed to make a backup of the old file.
1082 # Remove the backup if it is the same as the resulting file -
1083 # this acts as a flag to the caller that no changes were made.
1084 sed -i.old -f $tmp "$tmp_out"
1085 if cmp -s "${tmp_out}.old" "$tmp_out" ; then
1086 rm -f "${tmp_out}.old"
1093 which $1 > /dev/null || die "Please install $1 to continue"
1096 # Set a variable if it isn't already set. This allows environment
1097 # variables to override default config settings.
1102 [ "${!v+x}" ] || eval "$v=\"$e\""
1108 for i in "${installdir}/config.d/"*.defconf ; do
1113 # Print the list of config variables defined in config.d/.
1114 get_config_options () {( # sub-shell for local declaration of defconf()
1116 defconf() { options="$options $1" ; }
1121 # Produce a list of long options, suitable for use with getopt, that
1122 # represent the config variables defined in config.d/.
1123 getopt_config_options () {
1124 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
1128 # Unconditionally set the config variable associated with the given
1130 setconf_longopt () {
1134 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
1135 # unset so defconf will set it
1140 # Dump all of the current config variables.
1143 for o in $(get_config_options) ; do
1144 echo "${o}=\"${!o}\""
1149 # $COLUMNS is set in interactive bash shells. It probably isn't set
1150 # in this shell, so let's set it if it isn't.
1151 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
1155 # Print text assuming it starts after other text in $startcol and
1156 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
1157 # Long "words" will extend past $COLUMNS - 2.
1162 local width=$(($COLUMNS - 2 - $startcol))
1163 [ $width -lt 0 ] && width=$((78 - $startcol))
1168 if [ $startcol -gt 0 ] ; then
1169 padding=$(printf "\n%${startcol}s" " ")
1175 while [ -n "$text" ] ; do
1178 # If we already have output then arrange padding on the next line.
1179 [ -n "$out" ] && out="${out}${padding}"
1181 # Break the text at $width.
1182 out="${out}${text:0:${width}}"
1183 text="${text:${width}}"
1185 # If we have left over text then the line break may be ugly,
1186 # so let's check and try to break it on a space.
1187 if [ -n "$text" ] ; then
1188 # The 'x's stop us producing a special character like '(',
1189 # ')' or '!'. Yuck - there must be a better way.
1190 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1191 # We didn't break on a space. Arrange for the
1192 # beginning of the broken "word" to appear on the next
1193 # line but not if it will make us loop infinitely.
1194 if [ "${orig}" != "${out##* }${text}" ] ; then
1195 text="${out##* }${text}"
1198 # Hmmm, doing that would make us loop, so add the
1199 # rest of the word from the remainder of the text
1200 # to this line and let it extend past $COLUMNS - 2.
1201 out="${out}${text%% *}"
1202 if [ "${text# *}" != "$text" ] ; then
1203 # Remember the text after the next space for next time.
1206 # No text after next space.
1211 # We broke on a space. If it will be at the beginning
1212 # of the next line then remove it.
1221 # Display usage text, trying these approaches in order.
1222 # 1. See if it all fits on one line before $COLUMNS - 2.
1223 # 2. See if splitting before the default value and indenting it
1224 # to $startcol means that nothing passes $COLUMNS - 2.
1225 # 3. Treat the message and default value as a string and just us fill_text()
1227 usage_display_text () {
1232 local width=$(($COLUMNS - 2 - $startcol))
1233 [ $width -lt 0 ] && width=$((78 - $startcol))
1235 default="(default \"$default\")"
1237 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1238 echo "${desc} ${default}"
1240 local padding=$(printf "%${startcol}s" " ")
1242 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1244 echo "${padding}${default}"
1246 fill_text $startcol "${desc} ${default}"
1251 # Display usage information for long config options.
1252 usage_smart_display () {( # sub-shell for local declaration of defconf()
1256 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1258 printf " --%-25s " "${longopt}=${3}"
1260 usage_display_text $startcol "$4" "$2"
1267 # Display usage information for long config options.
1268 usage_config_options (){
1269 usage_smart_display load_config
1279 actions="${actions}${actions:+ }$*"
1284 [ -n "$actions" ] || usage
1287 for a in $actions ; do
1292 ######################################################################
1296 ######################################################################
1300 ############################
1301 # parse command line options
1302 long_opts=$(getopt_config_options)
1303 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump -l "$long_opts" -- "$@")
1304 [ $? != 0 ] && usage
1306 use_default_config=true
1308 # We do 2 passes of the options. The first time we just handle usage
1309 # and check whether -c is being used.
1310 eval set -- "$getopt_output"
1313 -c) shift 2 ; use_default_config=false ;;
1316 --) shift ; break ;;
1318 -h|--help) usage ;; # Usage should be shown here for real defaults.
1319 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1320 *) usage ;; # shouldn't happen, so this is reasonable.
1325 $use_default_config && [ -r "$config" ] && . "$config"
1327 eval set -- "$getopt_output"
1333 # force at least ./local_file to avoid accidental file
1335 . "$(dirname $2)/${b}"
1336 # If $CLUSTER is unset then try to base it on the filename
1337 if [ ! -n "$CLUSTER" ] ; then
1340 CLUSTER="${b%.autocluster}"
1345 -e) no_sanity=1 ; run_hooks post_config_hooks ; eval "$2" ; exit ;;
1346 -E) eval "$2" ; shift 2 ;;
1347 -x) set -x; shift ;;
1348 --dump) no_sanity=1 ; run_hooks post_config_hooks ; dump_config ;;
1349 --) shift ; break ;;
1350 -h|--help) usage ;; # Redundant.
1352 # Putting --opt1|opt2|... into a variable and having case
1353 # match against it as a pattern doesn't work. The | is
1354 # part of shell syntax, so we need to do this. Look away
1355 # now to stop your eyes from bleeding! :-)
1356 x=",${long_opts}" # Now each option is surrounded by , and :
1357 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1358 # Our option, $1, surrounded by , and : was in $x, so is legal.
1359 setconf_longopt "$1" "$2"; shift 2
1364 *) usage ;; # shouldn't happen, so this is reasonable.
1368 run_hooks post_config_hooks
1374 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1377 # check for needed programs
1378 check_command expect
1380 [ $# -lt 1 ] && usage
1390 create|boot) actions_add "base_${t}" ;;
1401 destroy|create|update_hosts|boot|configure)
1402 actions_add "cluster_${t}" ;;
1414 [ $# != 0 ] && usage
1418 [ $# != 1 ] && usage
1422 [ $# != 2 ] && usage
1423 create_one_node "$1" "$2"
1431 [ $# != 1 ] && usage
1432 diskimage mount "$1"
1435 [ $# != 0 ] && usage