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 # Pull the UUID for this node out of the map.
244 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
248 echo "Creating $NAME.xml"
249 substitute_vars $template_file tmp/$NAME.xml
251 # install the XML file
252 $VIRSH undefine $NAME > /dev/null 2>&1 || true
253 $VIRSH define tmp/$NAME.xml
256 create_node_configure_image ()
261 diskimage mount "$disk"
266 # Provides an easy way of removing nodes from $NODE.
267 create_node_null () {
271 ##############################
273 hack_nodes_functions=
276 # Expand out any abbreviations in NODES.
285 for i in ${ips//,/ } ; do
286 ns="${ns}${ns:+ }${t}:${i}"
291 for i in $(seq ${ips/-/ }) ; do
292 ns="${ns}${ns:+ }${t}:${i}"
296 ns="${ns}${ns:+ }${n}"
301 # Apply nodes hacks. Some of this is about backward compatibility
302 # but the hacks also fill in the node names and whether they're
303 # part of the CTDB cluster. The order is the order that
304 # configuration modules register their hacks.
305 run_hooks hack_nodes_functions
307 if [ -n "$NUMNODES" ] ; then
308 # Attempt to respect NUMNODES. Reduce the number of CTDB
310 local numnodes=$NUMNODES
314 if [ "$ctdb_node" = 1 ] ; then
315 if [ $numnodes -gt 0 ] ; then
316 numnodes=$(($numnodes - 1))
324 hack_all_nodes_with hack_filter
326 [ $numnodes -gt 0 ] && \
327 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."
330 # Check IP addresses for duplicates.
332 # This function doesn't modify anything...
335 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
336 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
337 ip_offsets="${ip_offsets}${ip_offset}:"
339 hack_all_nodes_with get_ip_offset
342 ##############################
344 sanity_check_cluster_name ()
346 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
347 die "Cluster names should be restricted to the characters A-Za-z0-9. \
348 Some cluster filesystems have problems with other characters."
353 common_nodelist_hacking ()
355 # Rework the NODES list
358 # Build /etc/hosts and hack the names of the ctdb nodes
359 hosts_line_hack_name ()
361 # Ignore nodes without names (e.g. "null")
362 [ "$node_type" != "null" -a -n "$name" ] || return 0
366 local ip_addr="${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
368 if [ "$ctdb_node" = 1 ] ; then
369 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
370 sname="${CLUSTER}n${num_ctdb_nodes}"
371 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
374 hosts_line="$ip_addr ${name}.${ld} $name"
377 # This allows you to add a function to your configuration file
378 # to modify hostnames (and other aspects of nodes). This
379 # function can access/modify $name (the existing name),
380 # $node_type and $ctdb_node (1, if the node is a member of the
381 # CTDB cluster, 0 otherwise).
382 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
383 local old_name="$name"
384 $HOSTNAME_HACKING_FUNCTION
385 if [ "$name" != "$old_name" ] ; then
386 hosts_line="$ip_addr ${name}.${ld} $name"
392 hosts_file="tmp/hosts.$CLUSTER"
394 local num_ctdb_nodes=0
395 local ld=$(echo $DOMAIN | tr A-Z a-z)
396 echo "# autocluster $CLUSTER"
397 hack_all_nodes_with hosts_line_hack_name
401 # Build /etc/ctdb/nodes
404 [ "$ctdb_node" = 1 ] || return 0
405 echo "${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
406 num_nodes=$(($num_nodes + 1))
408 nodes_file="tmp/nodes.$CLUSTER"
410 hack_all_nodes_with ctdb_nodes_line >$nodes_file
411 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
414 uuid_map="tmp/uuid_map.$CLUSTER"
417 echo "${ip_offset} $(uuidgen) ${node_type}"
419 hack_all_nodes_with uuid_map_line >$uuid_map
422 create_cluster_hooks=
423 cluster_created_hooks=
429 sanity_check_cluster_name
431 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
433 # Run hooks before doing anything else.
434 run_hooks create_cluster_hooks
436 common_nodelist_hacking
438 for_each_node call_func create_node
440 echo "Cluster $CLUSTER created"
443 run_hooks cluster_created_hooks
446 cluster_created_hosts_message ()
448 echo "You may want to add this to your /etc/hosts file:"
452 register_hook cluster_created_hooks cluster_created_hosts_message
457 local single_node_ip_offset="$2"
459 sanity_check_cluster_name
461 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
463 common_nodelist_hacking
466 set -- $(IFS=: ; echo $n)
467 [ $single_node_ip_offset -eq $2 ] || continue
468 call_func create_node "$@"
470 echo "Requested node created"
472 echo "You may want to update your /etc/hosts file:"
479 ###############################
480 # test the proxy setup
482 export http_proxy=$WEBPROXY
483 wget -O /dev/null $INSTALL_SERVER || \
484 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
490 kickstart_floppy_create_hooks=
495 local NAME="$BASENAME"
496 local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
497 run_hooks hack_disk_hooks "base"
501 echo "Testing WEBPROXY $WEBPROXY"
505 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
506 di=$(readlink "$DISK")
509 local di_dirname="${di%/*}"
510 mkdir -p "$di_dirname"
512 echo "Creating the disk"
513 qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
516 mkdir -p mnt tmp tmp/ISO
520 echo "Creating kickstart file from template"
521 substitute_vars "$KICKSTART" "tmp/ks.cfg"
523 if [ $INSTALLKEY = "--skip" ]; then
525 --------------------------------------------------------------------------------------
526 WARNING: You have not entered an install key. Some RHEL packages will not be installed.
528 Please enter a valid RHEL install key in your config file like this:
530 INSTALLKEY="1234-5678-0123-4567"
532 The install will continue without an install key in 5 seconds
533 --------------------------------------------------------------------------------------
538 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
541 (*) ISO="${ISO_DIR}/${ISO}"
544 echo "Creating kickstart floppy"
545 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
546 mkdosfs tmp/floppy.img
547 mount -o loop -t msdos tmp/floppy.img mnt
549 mount -o loop,ro $ISO tmp/ISO
551 echo "Setting up bootloader"
552 cp tmp/ISO/isolinux/isolinux.bin tmp
553 cp tmp/ISO/isolinux/vmlinuz tmp
554 cp tmp/ISO/isolinux/initrd.img tmp
556 run_hooks kickstart_floppy_create_hooks
563 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
565 rm -f $KVMLOG/serial.$NAME
567 # boot the install CD
568 $VIRSH create tmp/$NAME.xml
570 echo "Waiting for install to start"
573 # wait for the install to finish
574 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
576 die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
577 You may need to increase the value of CREATE_BASE_TIMEOUT.
578 Alternatively, the install might have completed but KS_DONE_MESSAGE
579 (currently \"${KS_DONE_MESSAGE}\")
580 may not have matched anything at the end of the kickstart output."
588 Install finished, base image $DISK created
592 To ensure that this image does not change
594 Note that the root password has been set to $ROOTPASSWORD
599 ###############################
605 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
613 diskimage mount $DISK
619 echo "Creating $NAME.xml"
620 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
622 # boot the base system
623 $VIRSH create tmp/$NAME.xml
626 ######################################################################
628 # Updating a disk image...
634 call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
637 # setup the files from $BASE_TEMPLATES/, substituting any variables
638 # based on the config
639 copy_base_dir_substitute_templates ()
643 local d="$BASE_TEMPLATES/$dir"
644 [ -d "$d" ] || return 0
647 for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
648 f="${f#./}" # remove leading "./" for clarity
649 if [ -d "$d/$f" ]; then
650 # Don't chmod existing directory
651 if diskimage is_directory "/$f" ; then
654 diskimage mkdir_p "/$f"
657 diskimage substitute_vars "$d/$f" "/$f"
659 diskimage chmod_reference "$d/$f" "/$f"
665 setup_base_ssh_keys ()
667 # this is needed as git doesn't store file permissions other
669 # Note that we protect the wildcards from the local shell.
670 diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
671 diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
672 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
673 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
674 diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
676 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
677 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
678 diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
682 register_hook setup_base_hooks setup_base_ssh_keys
684 setup_base_grub_conf ()
686 echo "Adjusting grub.conf"
687 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
688 diskimage sed "/boot/grub/grub.conf" \
689 -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
690 -e "s/ nodmraid//" -e "s/ nompath//" \
691 -e "s/quiet/noapic divider=10${o:+ }${o}/g"
694 register_hook setup_base_hooks setup_base_grub_conf
701 echo "Copy base files"
702 copy_base_dir_substitute_templates "all"
703 if [ -n "$type" ] ; then
704 copy_base_dir_substitute_templates "$type"
707 run_hooks setup_base_hooks
710 # setup various networking components
713 # This avoids doing anything when we're called from boot_base().
714 if [ -z "$hosts_file" ] ; then
715 echo "Skipping network-related setup"
719 echo "Setting up networks"
720 diskimage append_text_file "$hosts_file" "/etc/hosts"
722 echo "Setting up /etc/ctdb/nodes"
723 diskimage mkdir_p "/etc/ctdb"
724 diskimage put "$nodes_file" "/etc/ctdb/nodes"
726 [ "$WEBPROXY" = "" ] || {
727 diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
730 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
731 echo "Enabling nfs mount of $NFSSHARE"
732 diskimage mkdir_p "$NFS_MOUNTPOINT"
733 diskimage append_text "$NFSSHARE $NFS_MOUNTPOINT nfs nfsvers=3,intr 0 0" "/etc/fstab"
736 diskimage mkdir_p "/etc/yum.repos.d"
737 echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
739 diskimage rm_rf "/etc/udev/rules.d/70-persistent-net.rules"
741 echo "Setting up network interfaces: "
743 for n in $NETWORKS ; do
744 local dev="${n#*,}" # Strip address, comma
745 dev="${dev%,*}" # Strip comma, interface
748 diskimage substitute_vars \
749 - "/etc/sysconfig/network-scripts/ifcfg-${dev}"
753 IPADDR=${n%.*}.@@IPNUM@@
754 NETMASK=255.255.255.0
759 register_hook setup_base_hooks setup_network
762 [ -z "$TIMEZONE" ] && {
763 [ -r /etc/timezone ] && {
764 TIMEZONE=`cat /etc/timezone`
766 [ -r /etc/sysconfig/clock ] && {
767 . /etc/sysconfig/clock
770 TIMEZONE="${TIMEZONE// /_}"
772 [ -n "$TIMEZONE" ] || \
773 die "Unable to determine TIMEZONE - please set in config"
776 # substite a set of variables of the form @@XX@@ for the shell
777 # variables $XX in a file.
779 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
780 # contain a filename whose contents are substituted, with variable
781 # substitution applied to those contents. If filename starts with '|'
782 # it is a command instead - however, quoting is extremely fragile.
784 infile="${1:-/dev/null}" # if empty then default to /dev/null
785 outfile="$2" # optional
788 cat "$infile" >"$tmp_out"
790 # Handle any indirects by looping until nothing changes.
791 # However, only handle 10 levels of recursion.
794 if ! _substitute_vars "$tmp_out" "@@@" ; then
796 die "Failed to expand template $infile"
799 # No old version of file means no changes made.
800 if [ ! -f "${tmp_out}.old" ] ; then
804 rm -f "${tmp_out}.old"
806 count=$(($count + 1))
807 if [ $count -ge 10 ] ; then
809 die "Recursion too deep in $infile - only 10 levels allowed!"
813 # Now regular variables.
814 if ! _substitute_vars "$tmp_out" "@@" ; then
816 die "Failed to expand template $infile"
818 rm -f "${tmp_out}.old"
820 if [ -n "$outfile" ] ; then
821 mv "$tmp_out" "$outfile"
829 # Delimiter @@ means to substitute contents of variable.
830 # Delimiter @@@ means to substitute contents of file named by variable.
831 # @@@ supports leading '|' in variable value, which means to excute a
833 _substitute_vars() {(
837 # Get the list of variables used in the template. The grep
838 # gets rid of any blank lines and lines with extraneous '@'s
839 # next to template substitutions.
840 VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
841 grep '^[A-Z0-9_][A-Z0-9_]*$' |
846 # variable variables are fun .....
849 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
853 if [ "$delimiter" = "@@@" ] ; then
855 c="${f#|}" # Is is a command, signified by a leading '|'?
856 if [ "$c" = "$f" ] ; then
857 # No leading '|', cat file.
861 die "Could not substitute contents of file $f"
864 # Leading '|', execute command.
865 # Quoting problems here - using eval "$c" doesn't help.
869 die "Could not execute command $c"
874 # escape some pesky chars
875 # This first one can be too slow if done using a bash
876 # variable pattern subsitution.
877 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
880 echo "s#${delimiter}${v}${delimiter}#${s}#g"
883 # Get the in-place sed to make a backup of the old file.
884 # Remove the backup if it is the same as the resulting file -
885 # this acts as a flag to the caller that no changes were made.
886 sed -i.old -f $tmp "$tmp_out"
887 if cmp -s "${tmp_out}.old" "$tmp_out" ; then
888 rm -f "${tmp_out}.old"
895 which $1 > /dev/null || die "Please install $1 to continue"
898 # Set a variable if it isn't already set. This allows environment
899 # variables to override default config settings.
904 [ "${!v+x}" ] || eval "$v=\"$e\""
910 for i in "${installdir}/config.d/"*.defconf ; do
915 # Print the list of config variables defined in config.d/.
916 get_config_options () {( # sub-shell for local declaration of defconf()
918 defconf() { options="$options $1" ; }
923 # Produce a list of long options, suitable for use with getopt, that
924 # represent the config variables defined in config.d/.
925 getopt_config_options () {
926 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
930 # Unconditionally set the config variable associated with the given
936 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
937 # unset so defconf will set it
942 # Dump all of the current config variables.
945 for o in $(get_config_options) ; do
946 echo "${o}=\"${!o}\""
951 # $COLUMNS is set in interactive bash shells. It probably isn't set
952 # in this shell, so let's set it if it isn't.
953 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
957 # Print text assuming it starts after other text in $startcol and
958 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
959 # Long "words" will extend past $COLUMNS - 2.
964 local width=$(($COLUMNS - 2 - $startcol))
965 [ $width -lt 0 ] && width=$((78 - $startcol))
970 if [ $startcol -gt 0 ] ; then
971 padding=$(printf "\n%${startcol}s" " ")
977 while [ -n "$text" ] ; do
980 # If we already have output then arrange padding on the next line.
981 [ -n "$out" ] && out="${out}${padding}"
983 # Break the text at $width.
984 out="${out}${text:0:${width}}"
985 text="${text:${width}}"
987 # If we have left over text then the line break may be ugly,
988 # so let's check and try to break it on a space.
989 if [ -n "$text" ] ; then
990 # The 'x's stop us producing a special character like '(',
991 # ')' or '!'. Yuck - there must be a better way.
992 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
993 # We didn't break on a space. Arrange for the
994 # beginning of the broken "word" to appear on the next
995 # line but not if it will make us loop infinitely.
996 if [ "${orig}" != "${out##* }${text}" ] ; then
997 text="${out##* }${text}"
1000 # Hmmm, doing that would make us loop, so add the
1001 # rest of the word from the remainder of the text
1002 # to this line and let it extend past $COLUMNS - 2.
1003 out="${out}${text%% *}"
1004 if [ "${text# *}" != "$text" ] ; then
1005 # Remember the text after the next space for next time.
1008 # No text after next space.
1013 # We broke on a space. If it will be at the beginning
1014 # of the next line then remove it.
1023 # Display usage text, trying these approaches in order.
1024 # 1. See if it all fits on one line before $COLUMNS - 2.
1025 # 2. See if splitting before the default value and indenting it
1026 # to $startcol means that nothing passes $COLUMNS - 2.
1027 # 3. Treat the message and default value as a string and just us fill_text()
1029 usage_display_text () {
1034 local width=$(($COLUMNS - 2 - $startcol))
1035 [ $width -lt 0 ] && width=$((78 - $startcol))
1037 default="(default \"$default\")"
1039 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1040 echo "${desc} ${default}"
1042 local padding=$(printf "%${startcol}s" " ")
1044 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1046 echo "${padding}${default}"
1048 fill_text $startcol "${desc} ${default}"
1053 # Display usage information for long config options.
1054 usage_smart_display () {( # sub-shell for local declaration of defconf()
1058 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1060 printf " --%-25s " "${longopt}=${3}"
1062 usage_display_text $startcol "$4" "$2"
1069 # Display usage information for long config options.
1070 usage_config_options (){
1071 usage_smart_display load_config
1075 local releases=$(cd $installdir/releases && echo *.release)
1076 releases="${releases//.release}"
1077 releases="${releases// /\", \"}"
1078 echo "\"$releases\""
1081 ######################################################################
1085 ######################################################################
1089 ############################
1090 # parse command line options
1091 long_opts=$(getopt_config_options)
1092 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
1093 [ $? != 0 ] && usage
1095 use_default_config=true
1097 # We do 2 passes of the options. The first time we just handle usage
1098 # and check whether -c is being used.
1099 eval set -- "$getopt_output"
1102 -c) shift 2 ; use_default_config=false ;;
1105 --) shift ; break ;;
1106 --with-release) shift 2 ;; # Don't set use_default_config=false!!!
1108 -h|--help) usage ;; # Usage should be shown here for real defaults.
1109 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1110 *) usage ;; # shouldn't happen, so this is reasonable.
1115 $use_default_config && [ -r "$config" ] && . "$config"
1117 eval set -- "$getopt_output"
1121 # force at least ./local_file to avoid accidental file from $PATH
1122 -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
1123 -e) run_hooks post_config_hooks ; eval "$2" ; exit ;;
1124 -E) eval "$2" ; shift 2 ;;
1125 -x) set -x; shift ;;
1126 --dump) run_hooks post_config_hooks ; dump_config ;;
1127 --) shift ; break ;;
1128 -h|--help) usage ;; # Redundant.
1130 # Putting --opt1|opt2|... into a variable and having case
1131 # match against it as a pattern doesn't work. The | is
1132 # part of shell syntax, so we need to do this. Look away
1133 # now to stop your eyes from bleeding! :-)
1134 x=",${long_opts}" # Now each option is surrounded by , and :
1135 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1136 # Our option, $1, surrounded by , and : was in $x, so is legal.
1137 setconf_longopt "$1" "$2"; shift 2
1142 *) usage ;; # shouldn't happen, so this is reasonable.
1146 run_hooks post_config_hooks
1152 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1155 # check for needed programs
1156 check_command expect
1158 [ $# -lt 1 ] && usage
1169 [ $# != 0 ] && usage
1173 [ $# != 1 ] && usage
1177 [ $# != 2 ] && usage
1178 create_one_node "$1" "$2"
1186 [ $# != 1 ] && usage
1187 diskimage mount "$1"
1190 [ $# != 0 ] && usage