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:
58 create cluster CLUSTERNAME
61 create node CLUSTERNAME IP_OFFSET
62 (re)create a single cluster node
65 mount a qemu disk on mnt/
68 unmount a qemu disk from mnt/
79 ###############################
82 fill_text 0 "ERROR: $*" >&2
86 ###############################
88 # Indirectly call a function named by ${1}_${2}
90 local func="$1" ; shift
91 local type="$1" ; shift
93 local f="${func}_${type}"
94 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
98 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
101 die "No function defined for \"${func}\" \"${type}\""
106 # Note that this will work if you pass "call_func f" because the first
107 # element of the node tuple is the node type. Nice... :-)
112 "$@" $(IFS=: ; echo $n)
116 hack_one_node_with ()
118 local filter="$1" ; shift
127 local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
128 nodes="${nodes}${nodes:+ }${item}"
131 # This also gets used for non-filtering iteration.
132 hack_all_nodes_with ()
137 for_each_node hack_one_node_with "$filter"
146 eval "$hook_var=\"${!hook_var}${!hook_var:+ }${new_hook}\""
155 for i in ${!hook_var} ; do
160 # Use with care, since this may clear some autocluster defaults.!
165 eval "$hook_var=\"\""
168 ##############################
170 # These hooks are intended to customise the value of $DISK. They have
171 # access to 1 argument ("base", "system", "shared") and the variables
172 # $VIRTBASE, $CLUSTER, $BASENAME (for "base"), $NAME (for "system"),
173 # $SHARED_DISK_NUM (for "shared"). A hook must be deterministic and
174 # should not be stateful, since they can be called multiple times for
178 # common node creation stuff
179 create_node_COMMON ()
184 local template_file="${4:-$NODE_TEMPLATE}"
186 if [ "$SYSTEM_DISK_FORMAT" != "qcow2" -a "$BASE_FORMAT" = "qcow2" ] ; then
187 die "Error: if BASE_FORMAT is \"qcow2\" then SYSTEM_DISK_FORMAT must also be \"qcow2\"."
190 local IPNUM=$(($FIRSTIP + $ip_offset))
192 # Determine base image name. We use $DISK temporarily to allow
193 # the path to be hacked.
194 local DISK="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
195 if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
196 DISK="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
198 run_hooks hack_disk_hooks "base"
199 local base_disk="$DISK"
201 # Determine the system disk image name.
202 DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
203 run_hooks hack_disk_hooks "system"
206 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
207 di=$(readlink "$DISK")
210 local di_dirname="${di%/*}"
211 mkdir -p "$di_dirname"
213 case "$SYSTEM_DISK_FORMAT" in
215 echo "Creating the disk..."
216 qemu-img create -b "$base_disk" -f qcow2 "$di"
217 create_node_configure_image "$DISK" "$type"
220 echo "Creating the disk..."
221 cp -v --sparse=always "$base_disk" "$di"
222 create_node_configure_image "$DISK" "$type"
225 echo "Creating the disk..."
226 cp -v --reflink=always "$base_disk" "$di"
227 create_node_configure_image "$DISK" "$type"
230 echo "Creating the disk (using mmclone)..."
231 local base_snap="${base_disk}.snap"
232 [ -f "$base_snap" ] || mmclone snap "$base_disk" "$base_snap"
233 mmclone copy "$base_snap" "$di"
234 create_node_configure_image "$DISK" "$type"
237 echo "Skipping disk image creation as requested"
240 die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
243 set_macaddrs $CLUSTER $ip_offset
245 # Pull the UUID for this node out of the map.
246 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
250 echo "Creating $NAME.xml"
251 substitute_vars $template_file tmp/$NAME.xml
253 # install the XML file
254 $VIRSH undefine $NAME > /dev/null 2>&1 || true
255 $VIRSH define tmp/$NAME.xml
258 create_node_configure_image ()
263 diskimage mount "$disk"
268 # Provides an easy way of removing nodes from $NODE.
269 create_node_null () {
273 ##############################
275 hack_nodes_functions=
278 # Expand out any abbreviations in NODES.
287 for i in ${ips//,/ } ; do
288 ns="${ns}${ns:+ }${t}:${i}"
293 for i in $(seq ${ips/-/ }) ; do
294 ns="${ns}${ns:+ }${t}:${i}"
298 ns="${ns}${ns:+ }${n}"
303 # Apply nodes hacks. Some of this is about backward compatibility
304 # but the hacks also fill in the node names and whether they're
305 # part of the CTDB cluster. The order is the order that
306 # configuration modules register their hacks.
307 run_hooks hack_nodes_functions
309 if [ -n "$NUMNODES" ] ; then
310 # Attempt to respect NUMNODES. Reduce the number of CTDB
312 local numnodes=$NUMNODES
316 if [ "$ctdb_node" = 1 ] ; then
317 if [ $numnodes -gt 0 ] ; then
318 numnodes=$(($numnodes - 1))
326 hack_all_nodes_with hack_filter
328 [ $numnodes -gt 0 ] && \
329 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."
332 # Check IP addresses for duplicates.
334 # This function doesn't modify anything...
337 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
338 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
339 ip_offsets="${ip_offsets}${ip_offset}:"
341 hack_all_nodes_with get_ip_offset
344 ##############################
346 sanity_check_cluster_name ()
348 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
349 die "Cluster names should be restricted to the characters A-Za-z0-9. \
350 Some cluster filesystems have problems with other characters."
355 common_nodelist_hacking ()
357 # Rework the NODES list
360 # Build /etc/hosts and hack the names of the ctdb nodes
361 hosts_line_hack_name ()
363 # Ignore nodes without names (e.g. "null")
364 [ "$node_type" != "null" -a -n "$name" ] || return 0
368 local ip_addr="$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
370 if [ "$ctdb_node" = 1 ] ; then
371 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
372 sname="${CLUSTER}n${num_ctdb_nodes}"
373 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
376 hosts_line="$ip_addr ${name}.${ld} $name"
379 # This allows you to add a function to your configuration file
380 # to modify hostnames (and other aspects of nodes). This
381 # function can access/modify $name (the existing name),
382 # $node_type and $ctdb_node (1, if the node is a member of the
383 # CTDB cluster, 0 otherwise).
384 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
385 local old_name="$name"
386 $HOSTNAME_HACKING_FUNCTION
387 if [ "$name" != "$old_name" ] ; then
388 hosts_line="$ip_addr ${name}.${ld} $name"
394 hosts_file="tmp/hosts.$CLUSTER"
396 local num_ctdb_nodes=0
397 local ld=$(echo $DOMAIN | tr A-Z a-z)
398 echo "# autocluster $CLUSTER"
399 hack_all_nodes_with hosts_line_hack_name
403 # Build /etc/ctdb/nodes
406 [ "$ctdb_node" = 1 ] || return 0
407 echo "$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
408 num_nodes=$(($num_nodes + 1))
410 nodes_file="tmp/nodes.$CLUSTER"
412 hack_all_nodes_with ctdb_nodes_line >$nodes_file
413 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
416 uuid_map="tmp/uuid_map.$CLUSTER"
419 echo "${ip_offset} $(uuidgen) ${node_type}"
421 hack_all_nodes_with uuid_map_line >$uuid_map
424 create_cluster_hooks=
425 cluster_created_hooks=
431 sanity_check_cluster_name
433 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
435 # Run hooks before doing anything else.
436 run_hooks create_cluster_hooks
438 common_nodelist_hacking
440 for_each_node call_func create_node
442 echo "Cluster $CLUSTER created"
445 run_hooks cluster_created_hooks
448 cluster_created_hosts_message ()
450 echo "You may want to add this to your /etc/hosts file:"
454 register_hook cluster_created_hooks cluster_created_hosts_message
459 local single_node_ip_offset="$2"
461 sanity_check_cluster_name
463 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
465 common_nodelist_hacking
468 set -- $(IFS=: ; echo $n)
469 [ $single_node_ip_offset -eq $2 ] || continue
470 call_func create_node "$@"
472 echo "Requested node created"
474 echo "You may want to update your /etc/hosts file:"
481 ###############################
482 # test the proxy setup
484 export http_proxy=$WEBPROXY
485 wget -O /dev/null $INSTALL_SERVER || \
486 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
492 kickstart_floppy_create_hooks=
497 local NAME="$BASENAME"
498 local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
499 run_hooks hack_disk_hooks "base"
503 echo "Testing WEBPROXY $WEBPROXY"
507 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
508 di=$(readlink "$DISK")
511 local di_dirname="${di%/*}"
512 mkdir -p "$di_dirname"
514 echo "Creating the disk"
515 qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
518 mkdir -p mnt tmp tmp/ISO
522 echo "Creating kickstart file from template"
523 substitute_vars "$KICKSTART" "tmp/ks.cfg"
525 if [ $INSTALLKEY = "--skip" ]; then
527 --------------------------------------------------------------------------------------
528 WARNING: You have not entered an install key. Some RHEL packages will not be installed.
530 Please enter a valid RHEL install key in your config file like this:
532 INSTALLKEY="1234-5678-0123-4567"
534 The install will continue without an install key in 5 seconds
535 --------------------------------------------------------------------------------------
540 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
543 (*) ISO="${ISO_DIR}/${ISO}"
546 echo "Creating kickstart floppy"
547 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
548 mkdosfs tmp/floppy.img
549 mount -o loop -t msdos tmp/floppy.img mnt
551 mount -o loop,ro $ISO tmp/ISO
553 echo "Setting up bootloader"
554 cp tmp/ISO/isolinux/isolinux.bin tmp
555 cp tmp/ISO/isolinux/vmlinuz tmp
556 cp tmp/ISO/isolinux/initrd.img tmp
558 run_hooks kickstart_floppy_create_hooks
565 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
567 rm -f $KVMLOG/serial.$NAME
569 # boot the install CD
570 $VIRSH create tmp/$NAME.xml
572 echo "Waiting for install to start"
575 # wait for the install to finish
576 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
578 die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
579 You may need to increase the value of CREATE_BASE_TIMEOUT.
580 Alternatively, the install might have completed but KS_DONE_MESSAGE
581 (currently \"${KS_DONE_MESSAGE}\")
582 may not have matched anything at the end of the kickstart output."
590 Install finished, base image $DISK created
594 To ensure that this image does not change
596 Note that the root password has been set to $ROOTPASSWORD
601 ###############################
607 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
615 diskimage mount $DISK
621 echo "Creating $NAME.xml"
622 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
624 # boot the base system
625 $VIRSH create tmp/$NAME.xml
628 ######################################################################
630 # various functions...
632 # Set some MAC address variables based on a hash of the cluster name
633 # plus the node number and each adapter number.
638 local md5=$(echo $cname | md5sum)
639 local nh=$(printf "%02x" $ip_offset)
640 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
642 MAC1="${mac_prefix}01"
643 MAC2="${mac_prefix}02"
644 MAC3="${mac_prefix}03"
645 MAC4="${mac_prefix}04"
646 MAC5="${mac_prefix}05"
647 MAC6="${mac_prefix}06"
650 ######################################################################
652 # Updating a disk image...
658 call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
661 # setup the files from $BASE_TEMPLATES/, substituting any variables
662 # based on the config
663 copy_base_dir_substitute_templates ()
667 local d="$BASE_TEMPLATES/$dir"
668 [ -d "$d" ] || return 0
671 for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
672 f="${f#./}" # remove leading "./" for clarity
673 if [ -d "$d/$f" ]; then
674 # Don't chmod existing directory
675 if diskimage is_directory "/$f" ; then
678 diskimage mkdir_p "/$f"
681 diskimage substitute_vars "$d/$f" "/$f"
683 diskimage chmod_reference "$d/$f" "/$f"
689 setup_base_ssh_keys ()
691 # this is needed as git doesn't store file permissions other
693 # Note that we protect the wildcards from the local shell.
694 diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
695 diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
696 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
697 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
698 diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
700 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
701 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
702 diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
706 register_hook setup_base_hooks setup_base_ssh_keys
708 setup_base_grub_conf ()
710 echo "Adjusting grub.conf"
711 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
712 diskimage sed "/boot/grub/grub.conf" \
713 -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
714 -e "s/ nodmraid//" -e "s/ nompath//" \
715 -e "s/quiet/noapic divider=10${o:+ }${o}/g"
718 register_hook setup_base_hooks setup_base_grub_conf
725 echo "Copy base files"
726 copy_base_dir_substitute_templates "all"
727 if [ -n "$type" ] ; then
728 copy_base_dir_substitute_templates "$type"
731 run_hooks setup_base_hooks
734 # setup various networking components
737 # This avoids doing anything when we're called from boot_base().
738 if [ -z "$hosts_file" ] ; then
739 echo "Skipping network-related setup"
743 echo "Setting up networks"
744 diskimage append_text_file "$hosts_file" "/etc/hosts"
746 echo "Setting up /etc/ctdb/nodes"
747 diskimage mkdir_p "/etc/ctdb"
748 diskimage put "$nodes_file" "/etc/ctdb/nodes"
750 [ "$WEBPROXY" = "" ] || {
751 diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
754 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
755 echo "Enabling nfs mount of $NFSSHARE"
756 diskimage mkdir_p "$NFS_MOUNTPOINT"
757 diskimage append_text "$NFSSHARE $NFS_MOUNTPOINT nfs nfsvers=3,intr 0 0" "/etc/fstab"
760 diskimage mkdir_p "/etc/yum.repos.d"
761 echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
763 diskimage rm_rf "/etc/udev/rules.d/70-persistent-net.rules"
766 register_hook setup_base_hooks setup_network
769 [ -z "$TIMEZONE" ] && {
770 [ -r /etc/timezone ] && {
771 TIMEZONE=`cat /etc/timezone`
773 [ -r /etc/sysconfig/clock ] && {
774 . /etc/sysconfig/clock
777 TIMEZONE="${TIMEZONE// /_}"
779 [ -n "$TIMEZONE" ] || \
780 die "Unable to determine TIMEZONE - please set in config"
783 # substite a set of variables of the form @@XX@@ for the shell
784 # variables $XX in a file.
786 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
787 # contain a filename whose contents are substituted, with variable
788 # substitution applied to those contents. If filename starts with '|'
789 # it is a command instead - however, quoting is extremely fragile.
791 infile="${1:-/dev/null}" # if empty then default to /dev/null
792 outfile="$2" # optional
795 cat "$infile" >"$tmp_out"
797 # Handle any indirects by looping until nothing changes.
798 # However, only handle 10 levels of recursion.
801 if ! _substitute_vars "$tmp_out" "@@@" ; then
803 die "Failed to expand template $infile"
806 # No old version of file means no changes made.
807 if [ ! -f "${tmp_out}.old" ] ; then
811 rm -f "${tmp_out}.old"
813 count=$(($count + 1))
814 if [ $count -ge 10 ] ; then
816 die "Recursion too deep in $infile - only 10 levels allowed!"
820 # Now regular variables.
821 if ! _substitute_vars "$tmp_out" "@@" ; then
823 die "Failed to expand template $infile"
825 rm -f "${tmp_out}.old"
827 if [ -n "$outfile" ] ; then
828 mv "$tmp_out" "$outfile"
836 # Delimiter @@ means to substitute contents of variable.
837 # Delimiter @@@ means to substitute contents of file named by variable.
838 # @@@ supports leading '|' in variable value, which means to excute a
840 _substitute_vars() {(
844 # Get the list of variables used in the template. The grep
845 # gets rid of any blank lines and lines with extraneous '@'s
846 # next to template substitutions.
847 VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
848 grep '^[A-Z0-9_][A-Z0-9_]*$' |
853 # variable variables are fun .....
856 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
860 if [ "$delimiter" = "@@@" ] ; then
862 c="${f#|}" # Is is a command, signified by a leading '|'?
863 if [ "$c" = "$f" ] ; then
864 # No leading '|', cat file.
868 die "Could not substitute contents of file $f"
871 # Leading '|', execute command.
872 # Quoting problems here - using eval "$c" doesn't help.
876 die "Could not execute command $c"
881 # escape some pesky chars
882 # This first one can be too slow if done using a bash
883 # variable pattern subsitution.
884 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
887 echo "s#${delimiter}${v}${delimiter}#${s}#g"
890 # Get the in-place sed to make a backup of the old file.
891 # Remove the backup if it is the same as the resulting file -
892 # this acts as a flag to the caller that no changes were made.
893 sed -i.old -f $tmp "$tmp_out"
894 if cmp -s "${tmp_out}.old" "$tmp_out" ; then
895 rm -f "${tmp_out}.old"
902 which $1 > /dev/null || die "Please install $1 to continue"
905 # Set a variable if it isn't already set. This allows environment
906 # variables to override default config settings.
911 [ "${!v+x}" ] || eval "$v=\"$e\""
917 for i in "${installdir}/config.d/"*.defconf ; do
922 # Print the list of config variables defined in config.d/.
923 get_config_options () {( # sub-shell for local declaration of defconf()
925 defconf() { options="$options $1" ; }
930 # Produce a list of long options, suitable for use with getopt, that
931 # represent the config variables defined in config.d/.
932 getopt_config_options () {
933 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
937 # Unconditionally set the config variable associated with the given
943 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
944 # unset so defconf will set it
949 # Dump all of the current config variables.
952 for o in $(get_config_options) ; do
953 echo "${o}=\"${!o}\""
958 # $COLUMNS is set in interactive bash shells. It probably isn't set
959 # in this shell, so let's set it if it isn't.
960 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
964 # Print text assuming it starts after other text in $startcol and
965 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
966 # Long "words" will extend past $COLUMNS - 2.
971 local width=$(($COLUMNS - 2 - $startcol))
972 [ $width -lt 0 ] && width=$((78 - $startcol))
977 if [ $startcol -gt 0 ] ; then
978 padding=$(printf "\n%${startcol}s" " ")
984 while [ -n "$text" ] ; do
987 # If we already have output then arrange padding on the next line.
988 [ -n "$out" ] && out="${out}${padding}"
990 # Break the text at $width.
991 out="${out}${text:0:${width}}"
992 text="${text:${width}}"
994 # If we have left over text then the line break may be ugly,
995 # so let's check and try to break it on a space.
996 if [ -n "$text" ] ; then
997 # The 'x's stop us producing a special character like '(',
998 # ')' or '!'. Yuck - there must be a better way.
999 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1000 # We didn't break on a space. Arrange for the
1001 # beginning of the broken "word" to appear on the next
1002 # line but not if it will make us loop infinitely.
1003 if [ "${orig}" != "${out##* }${text}" ] ; then
1004 text="${out##* }${text}"
1007 # Hmmm, doing that would make us loop, so add the
1008 # rest of the word from the remainder of the text
1009 # to this line and let it extend past $COLUMNS - 2.
1010 out="${out}${text%% *}"
1011 if [ "${text# *}" != "$text" ] ; then
1012 # Remember the text after the next space for next time.
1015 # No text after next space.
1020 # We broke on a space. If it will be at the beginning
1021 # of the next line then remove it.
1030 # Display usage text, trying these approaches in order.
1031 # 1. See if it all fits on one line before $COLUMNS - 2.
1032 # 2. See if splitting before the default value and indenting it
1033 # to $startcol means that nothing passes $COLUMNS - 2.
1034 # 3. Treat the message and default value as a string and just us fill_text()
1036 usage_display_text () {
1041 local width=$(($COLUMNS - 2 - $startcol))
1042 [ $width -lt 0 ] && width=$((78 - $startcol))
1044 default="(default \"$default\")"
1046 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1047 echo "${desc} ${default}"
1049 local padding=$(printf "%${startcol}s" " ")
1051 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1053 echo "${padding}${default}"
1055 fill_text $startcol "${desc} ${default}"
1060 # Display usage information for long config options.
1061 usage_smart_display () {( # sub-shell for local declaration of defconf()
1065 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1067 printf " --%-25s " "${longopt}=${3}"
1069 usage_display_text $startcol "$4" "$2"
1076 # Display usage information for long config options.
1077 usage_config_options (){
1078 usage_smart_display load_config
1082 local releases=$(cd $installdir/releases && echo *.release)
1083 releases="${releases//.release}"
1084 releases="${releases// /\", \"}"
1085 echo "\"$releases\""
1088 has_public_addresses_DEFAULT ()
1093 make_public_addresses() {
1094 local firstip="${1:-$[${FIRSTIP} + ${PUBLIC_IP_OFFSET}]}"
1095 local num_addrs="${2:-${NUMNODES}}"
1097 if [ $(( $firstip + $num_addrs - 1 )) -gt 254 ]; then
1098 die "make_public_addresses: last octet > 254 - change PUBLIC_IP_OFFSET"
1102 for e in $IPNET1 $IPNET2 ; do
1103 echo -ne "${IPBASE}.${e}.${firstip},${num_addrs},eth${e} "
1108 ######################################################################
1112 ######################################################################
1116 ############################
1117 # parse command line options
1118 long_opts=$(getopt_config_options)
1119 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
1120 [ $? != 0 ] && usage
1122 use_default_config=true
1124 # We do 2 passes of the options. The first time we just handle usage
1125 # and check whether -c is being used.
1126 eval set -- "$getopt_output"
1129 -c) shift 2 ; use_default_config=false ;;
1132 --) shift ; break ;;
1133 --with-release) shift 2 ;; # Don't set use_default_config=false!!!
1135 -h|--help) usage ;; # Usage should be shown here for real defaults.
1136 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1137 *) usage ;; # shouldn't happen, so this is reasonable.
1142 $use_default_config && [ -r "$config" ] && . "$config"
1144 eval set -- "$getopt_output"
1148 # force at least ./local_file to avoid accidental file from $PATH
1149 -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
1150 -e) run_hooks post_config_hooks ; eval "$2" ; exit ;;
1151 -E) eval "$2" ; shift 2 ;;
1152 -x) set -x; shift ;;
1153 --dump) run_hooks post_config_hooks ; dump_config ;;
1154 --) shift ; break ;;
1155 -h|--help) usage ;; # Redundant.
1157 # Putting --opt1|opt2|... into a variable and having case
1158 # match against it as a pattern doesn't work. The | is
1159 # part of shell syntax, so we need to do this. Look away
1160 # now to stop your eyes from bleeding! :-)
1161 x=",${long_opts}" # Now each option is surrounded by , and :
1162 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1163 # Our option, $1, surrounded by , and : was in $x, so is legal.
1164 setconf_longopt "$1" "$2"; shift 2
1169 *) usage ;; # shouldn't happen, so this is reasonable.
1173 run_hooks post_config_hooks
1179 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1182 # check for needed programs
1183 check_command expect
1185 [ $# -lt 1 ] && usage
1196 [ $# != 0 ] && usage
1200 [ $# != 1 ] && usage
1204 [ $# != 2 ] && usage
1205 create_one_node "$1" "$2"
1213 [ $# != 1 ] && usage
1214 diskimage mount "$1"
1217 [ $# != 0 ] && usage