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 if [ "$no_sanity" = 1 ] ; then
83 fill_text 0 "WARNING: $*" >&2
85 fill_text 0 "ERROR: $*" >&2
90 ###############################
92 # Indirectly call a function named by ${1}_${2}
94 local func="$1" ; shift
95 local type="$1" ; shift
97 local f="${func}_${type}"
98 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
102 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
105 die "No function defined for \"${func}\" \"${type}\""
110 # Note that this will work if you pass "call_func f" because the first
111 # element of the node tuple is the node type. Nice... :-)
116 "$@" $(IFS=: ; echo $n)
120 hack_one_node_with ()
122 local filter="$1" ; shift
131 local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
132 nodes="${nodes}${nodes:+ }${item}"
135 # This also gets used for non-filtering iteration.
136 hack_all_nodes_with ()
141 for_each_node hack_one_node_with "$filter"
150 eval "$hook_var=\"${!hook_var}${!hook_var:+ }${new_hook}\""
159 for i in ${!hook_var} ; do
164 # Use with care, since this may clear some autocluster defaults.!
169 eval "$hook_var=\"\""
172 ##############################
174 # These hooks are intended to customise the value of $DISK. They have
175 # access to 1 argument ("base", "system", "shared") and the variables
176 # $VIRTBASE, $CLUSTER, $BASENAME (for "base"), $NAME (for "system"),
177 # $SHARED_DISK_NUM (for "shared"). A hook must be deterministic and
178 # should not be stateful, since they can be called multiple times for
182 # common node creation stuff
183 create_node_COMMON ()
188 local template_file="${4:-$NODE_TEMPLATE}"
190 if [ "$SYSTEM_DISK_FORMAT" != "qcow2" -a "$BASE_FORMAT" = "qcow2" ] ; then
191 die "Error: if BASE_FORMAT is \"qcow2\" then SYSTEM_DISK_FORMAT must also be \"qcow2\"."
194 local IPNUM=$(($FIRSTIP + $ip_offset))
197 # Determine base image name. We use $DISK temporarily to allow
198 # the path to be hacked.
199 local DISK="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
200 if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
201 DISK="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
203 run_hooks hack_disk_hooks "base"
204 local base_disk="$DISK"
206 # Determine the system disk image name.
207 DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
208 run_hooks hack_disk_hooks "system"
211 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
212 di=$(readlink "$DISK")
215 local di_dirname="${di%/*}"
216 mkdir -p "$di_dirname"
218 case "$SYSTEM_DISK_FORMAT" in
220 echo "Creating the disk..."
221 qemu-img create -b "$base_disk" -f qcow2 "$di"
222 create_node_configure_image "$DISK" "$type"
225 echo "Creating the disk..."
226 cp -v --sparse=always "$base_disk" "$di"
227 create_node_configure_image "$DISK" "$type"
230 echo "Creating the disk..."
231 cp -v --reflink=always "$base_disk" "$di"
232 create_node_configure_image "$DISK" "$type"
235 echo "Creating the disk (using mmclone)..."
236 local base_snap="${base_disk}.snap"
237 [ -f "$base_snap" ] || mmclone snap "$base_disk" "$base_snap"
238 mmclone copy "$base_snap" "$di"
239 create_node_configure_image "$DISK" "$type"
242 echo "Skipping disk image creation as requested"
245 die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
248 # Pull the UUID for this node out of the map.
249 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
253 echo "Creating $NAME.xml"
254 substitute_vars $template_file tmp/$NAME.xml
256 # install the XML file
257 $VIRSH undefine $NAME > /dev/null 2>&1 || true
258 $VIRSH define tmp/$NAME.xml
261 create_node_configure_image ()
266 diskimage mount "$disk"
271 # Provides an easy way of removing nodes from $NODE.
272 create_node_null () {
276 # Uses: CLUSTER, NAME, NETWORKS, FIRSTIP, ip_offset
279 network_map="tmp/network_map.$NAME"
281 if [ -n "$CLUSTER" ] ; then
282 local md5=$(echo "$CLUSTER" | md5sum)
283 local nh=$(printf "%02x" $ip_offset)
284 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
286 local mac_prefix="02:42:42:00:00:"
291 for n in $NETWORKS ; do
292 local ch=$(printf "%02x" $count)
293 local mac="${mac_prefix}${ch}"
296 local ip_bits="$1" ; shift
297 local dev="$1" ; shift
300 local net="${ip_bits%/*}"
301 local netname="acnet_${net//./_}"
303 local ip="${net%.*}.${IPNUM}"
304 local mask="255.255.255.0"
305 echo "${netname} ${dev} ${ip} ${mask} ${mac} ${opts}"
306 count=$(($count + 1))
310 ##############################
312 hack_nodes_functions=
315 # Expand out any abbreviations in NODES.
324 for i in ${ips//,/ } ; do
325 ns="${ns}${ns:+ }${t}:${i}"
330 for i in $(seq ${ips/-/ }) ; do
331 ns="${ns}${ns:+ }${t}:${i}"
335 ns="${ns}${ns:+ }${n}"
340 # Apply nodes hacks. Some of this is about backward compatibility
341 # but the hacks also fill in the node names and whether they're
342 # part of the CTDB cluster. The order is the order that
343 # configuration modules register their hacks.
344 run_hooks hack_nodes_functions
346 if [ -n "$NUMNODES" ] ; then
347 # Attempt to respect NUMNODES. Reduce the number of CTDB
349 local numnodes=$NUMNODES
353 if [ "$ctdb_node" = 1 ] ; then
354 if [ $numnodes -gt 0 ] ; then
355 numnodes=$(($numnodes - 1))
363 hack_all_nodes_with hack_filter
365 [ $numnodes -gt 0 ] && \
366 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."
369 # Check IP addresses for duplicates.
371 # This function doesn't modify anything...
374 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
375 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
376 ip_offsets="${ip_offsets}${ip_offset}:"
378 hack_all_nodes_with get_ip_offset
381 ##############################
383 sanity_check_cluster_name ()
385 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
386 die "Cluster names should be restricted to the characters A-Za-z0-9. \
387 Some cluster filesystems have problems with other characters."
392 common_nodelist_hacking ()
394 # Rework the NODES list
397 # Build /etc/hosts and hack the names of the ctdb nodes
398 hosts_line_hack_name ()
400 # Ignore nodes without names (e.g. "null")
401 [ "$node_type" != "null" -a -n "$name" ] || return 0
405 local ip_addr="${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
407 if [ "$ctdb_node" = 1 ] ; then
408 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
409 sname="${CLUSTER}n${num_ctdb_nodes}"
410 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
413 hosts_line="$ip_addr ${name}.${ld} $name"
416 # This allows you to add a function to your configuration file
417 # to modify hostnames (and other aspects of nodes). This
418 # function can access/modify $name (the existing name),
419 # $node_type and $ctdb_node (1, if the node is a member of the
420 # CTDB cluster, 0 otherwise).
421 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
422 local old_name="$name"
423 $HOSTNAME_HACKING_FUNCTION
424 if [ "$name" != "$old_name" ] ; then
425 hosts_line="$ip_addr ${name}.${ld} $name"
431 hosts_file="tmp/hosts.$CLUSTER"
433 local num_ctdb_nodes=0
434 local ld=$(echo $DOMAIN | tr A-Z a-z)
435 echo "# autocluster $CLUSTER"
436 hack_all_nodes_with hosts_line_hack_name
440 # Build /etc/ctdb/nodes
443 [ "$ctdb_node" = 1 ] || return 0
444 echo "${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
445 num_nodes=$(($num_nodes + 1))
447 nodes_file="tmp/nodes.$CLUSTER"
449 hack_all_nodes_with ctdb_nodes_line >$nodes_file
450 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
453 uuid_map="tmp/uuid_map.$CLUSTER"
456 echo "${ip_offset} $(uuidgen) ${node_type}"
458 hack_all_nodes_with uuid_map_line >$uuid_map
461 create_cluster_hooks=
462 cluster_created_hooks=
468 sanity_check_cluster_name
470 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
472 # Run hooks before doing anything else.
473 run_hooks create_cluster_hooks
475 common_nodelist_hacking
477 for_each_node call_func create_node
479 echo "Cluster $CLUSTER created"
482 run_hooks cluster_created_hooks
485 cluster_created_hosts_message ()
487 echo "You may want to add this to your /etc/hosts file:"
491 register_hook cluster_created_hooks cluster_created_hosts_message
496 local single_node_ip_offset="$2"
498 sanity_check_cluster_name
500 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
502 common_nodelist_hacking
505 set -- $(IFS=: ; echo $n)
506 [ $single_node_ip_offset -eq $2 ] || continue
507 call_func create_node "$@"
509 echo "Requested node created"
511 echo "You may want to update your /etc/hosts file:"
518 ###############################
519 # test the proxy setup
521 export http_proxy=$WEBPROXY
522 wget -O /dev/null $INSTALL_SERVER || \
523 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
529 kickstart_floppy_create_hooks=
534 local NAME="$BASENAME"
535 local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
536 run_hooks hack_disk_hooks "base"
540 echo "Testing WEBPROXY $WEBPROXY"
544 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
545 di=$(readlink "$DISK")
548 local di_dirname="${di%/*}"
549 mkdir -p "$di_dirname"
551 echo "Creating the disk"
552 qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
555 mkdir -p mnt tmp tmp/ISO
561 echo "Creating kickstart file from template"
562 substitute_vars "$KICKSTART" "tmp/ks.cfg"
564 if [ $INSTALLKEY = "--skip" ]; then
566 --------------------------------------------------------------------------------------
567 WARNING: You have not entered an install key. Some RHEL packages will not be installed.
569 Please enter a valid RHEL install key in your config file like this:
571 INSTALLKEY="1234-5678-0123-4567"
573 The install will continue without an install key in 5 seconds
574 --------------------------------------------------------------------------------------
579 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
582 (*) ISO="${ISO_DIR}/${ISO}"
585 echo "Creating kickstart floppy"
586 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
587 mkdosfs tmp/floppy.img
588 mount -o loop -t msdos tmp/floppy.img mnt
590 mount -o loop,ro $ISO tmp/ISO
592 echo "Setting up bootloader"
593 cp tmp/ISO/isolinux/isolinux.bin tmp
594 cp tmp/ISO/isolinux/vmlinuz tmp
595 cp tmp/ISO/isolinux/initrd.img tmp
597 run_hooks kickstart_floppy_create_hooks
604 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
606 rm -f $KVMLOG/serial.$NAME
608 # boot the install CD
609 $VIRSH create tmp/$NAME.xml
611 echo "Waiting for install to start"
614 # wait for the install to finish
615 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
617 die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
618 You may need to increase the value of CREATE_BASE_TIMEOUT.
619 Alternatively, the install might have completed but KS_DONE_MESSAGE
620 (currently \"${KS_DONE_MESSAGE}\")
621 may not have matched anything at the end of the kickstart output."
629 Install finished, base image $DISK created
633 To ensure that this image does not change
635 Note that the root password has been set to $ROOTPASSWORD
640 ###############################
647 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
655 diskimage mount $DISK
661 echo "Creating $NAME.xml"
662 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
664 # boot the base system
665 $VIRSH create tmp/$NAME.xml
668 ######################################################################
670 # Updating a disk image...
676 call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
679 # setup the files from $BASE_TEMPLATES/, substituting any variables
680 # based on the config
681 copy_base_dir_substitute_templates ()
685 local d="$BASE_TEMPLATES/$dir"
686 [ -d "$d" ] || return 0
689 for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
690 f="${f#./}" # remove leading "./" for clarity
691 if [ -d "$d/$f" ]; then
692 # Don't chmod existing directory
693 if diskimage is_directory "/$f" ; then
696 diskimage mkdir_p "/$f"
699 diskimage substitute_vars "$d/$f" "/$f"
701 diskimage chmod_reference "$d/$f" "/$f"
707 setup_base_ssh_keys ()
709 # this is needed as git doesn't store file permissions other
711 # Note that we protect the wildcards from the local shell.
712 diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
713 diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
714 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
715 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
716 diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
718 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
719 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
720 diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
724 register_hook setup_base_hooks setup_base_ssh_keys
726 setup_base_grub_conf ()
728 echo "Adjusting grub.conf"
729 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
730 diskimage sed "/boot/grub/grub.conf" \
731 -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
732 -e "s/ nodmraid//" -e "s/ nompath//" \
733 -e "s/quiet/noapic divider=10${o:+ }${o}/g"
736 register_hook setup_base_hooks setup_base_grub_conf
743 echo "Copy base files"
744 copy_base_dir_substitute_templates "all"
745 if [ -n "$type" ] ; then
746 copy_base_dir_substitute_templates "$type"
749 run_hooks setup_base_hooks
752 # setup various networking components
755 # This avoids doing anything when we're called from boot_base().
756 if [ -z "$hosts_file" ] ; then
757 echo "Skipping network-related setup"
761 echo "Setting up networks"
762 diskimage append_text_file "$hosts_file" "/etc/hosts"
764 echo "Setting up /etc/ctdb/nodes"
765 diskimage mkdir_p "/etc/ctdb"
766 diskimage put "$nodes_file" "/etc/ctdb/nodes"
768 [ "$WEBPROXY" = "" ] || {
769 diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
772 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
773 echo "Enabling nfs mount of $NFSSHARE"
774 diskimage mkdir_p "$NFS_MOUNTPOINT"
775 diskimage append_text "$NFSSHARE $NFS_MOUNTPOINT nfs nfsvers=3,intr 0 0" "/etc/fstab"
778 diskimage mkdir_p "/etc/yum.repos.d"
779 echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
781 diskimage rm_rf "/etc/udev/rules.d/70-persistent-net.rules"
783 echo "Setting up network interfaces: "
784 local netname dev ip mask mac opts
785 while read netname dev ip mask mac opts; do
788 diskimage substitute_vars \
789 - "/etc/sysconfig/network-scripts/ifcfg-${dev}"
800 register_hook setup_base_hooks setup_network
803 [ -z "$TIMEZONE" ] && {
804 [ -r /etc/timezone ] && {
805 TIMEZONE=`cat /etc/timezone`
807 [ -r /etc/sysconfig/clock ] && {
808 . /etc/sysconfig/clock
811 TIMEZONE="${TIMEZONE// /_}"
813 [ -n "$TIMEZONE" ] || \
814 die "Unable to determine TIMEZONE - please set in config"
817 # substite a set of variables of the form @@XX@@ for the shell
818 # variables $XX in a file.
820 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
821 # contain a filename whose contents are substituted, with variable
822 # substitution applied to those contents. If filename starts with '|'
823 # it is a command instead - however, quoting is extremely fragile.
825 infile="${1:-/dev/null}" # if empty then default to /dev/null
826 outfile="$2" # optional
829 cat "$infile" >"$tmp_out"
831 # Handle any indirects by looping until nothing changes.
832 # However, only handle 10 levels of recursion.
835 if ! _substitute_vars "$tmp_out" "@@@" ; then
837 die "Failed to expand template $infile"
840 # No old version of file means no changes made.
841 if [ ! -f "${tmp_out}.old" ] ; then
845 rm -f "${tmp_out}.old"
847 count=$(($count + 1))
848 if [ $count -ge 10 ] ; then
850 die "Recursion too deep in $infile - only 10 levels allowed!"
854 # Now regular variables.
855 if ! _substitute_vars "$tmp_out" "@@" ; then
857 die "Failed to expand template $infile"
859 rm -f "${tmp_out}.old"
861 if [ -n "$outfile" ] ; then
862 mv "$tmp_out" "$outfile"
870 # Delimiter @@ means to substitute contents of variable.
871 # Delimiter @@@ means to substitute contents of file named by variable.
872 # @@@ supports leading '|' in variable value, which means to excute a
874 _substitute_vars() {(
878 # Get the list of variables used in the template. The grep
879 # gets rid of any blank lines and lines with extraneous '@'s
880 # next to template substitutions.
881 VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
882 grep '^[A-Z0-9_][A-Z0-9_]*$' |
887 # variable variables are fun .....
890 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
894 if [ "$delimiter" = "@@@" ] ; then
896 c="${f#|}" # Is is a command, signified by a leading '|'?
897 if [ "$c" = "$f" ] ; then
898 # No leading '|', cat file.
902 die "Could not substitute contents of file $f"
905 # Leading '|', execute command.
906 # Quoting problems here - using eval "$c" doesn't help.
910 die "Could not execute command $c"
915 # escape some pesky chars
916 # This first one can be too slow if done using a bash
917 # variable pattern subsitution.
918 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
921 echo "s#${delimiter}${v}${delimiter}#${s}#g"
924 # Get the in-place sed to make a backup of the old file.
925 # Remove the backup if it is the same as the resulting file -
926 # this acts as a flag to the caller that no changes were made.
927 sed -i.old -f $tmp "$tmp_out"
928 if cmp -s "${tmp_out}.old" "$tmp_out" ; then
929 rm -f "${tmp_out}.old"
936 which $1 > /dev/null || die "Please install $1 to continue"
939 # Set a variable if it isn't already set. This allows environment
940 # variables to override default config settings.
945 [ "${!v+x}" ] || eval "$v=\"$e\""
951 for i in "${installdir}/config.d/"*.defconf ; do
956 # Print the list of config variables defined in config.d/.
957 get_config_options () {( # sub-shell for local declaration of defconf()
959 defconf() { options="$options $1" ; }
964 # Produce a list of long options, suitable for use with getopt, that
965 # represent the config variables defined in config.d/.
966 getopt_config_options () {
967 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
971 # Unconditionally set the config variable associated with the given
977 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
978 # unset so defconf will set it
983 # Dump all of the current config variables.
986 for o in $(get_config_options) ; do
987 echo "${o}=\"${!o}\""
992 # $COLUMNS is set in interactive bash shells. It probably isn't set
993 # in this shell, so let's set it if it isn't.
994 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
998 # Print text assuming it starts after other text in $startcol and
999 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
1000 # Long "words" will extend past $COLUMNS - 2.
1005 local width=$(($COLUMNS - 2 - $startcol))
1006 [ $width -lt 0 ] && width=$((78 - $startcol))
1011 if [ $startcol -gt 0 ] ; then
1012 padding=$(printf "\n%${startcol}s" " ")
1018 while [ -n "$text" ] ; do
1021 # If we already have output then arrange padding on the next line.
1022 [ -n "$out" ] && out="${out}${padding}"
1024 # Break the text at $width.
1025 out="${out}${text:0:${width}}"
1026 text="${text:${width}}"
1028 # If we have left over text then the line break may be ugly,
1029 # so let's check and try to break it on a space.
1030 if [ -n "$text" ] ; then
1031 # The 'x's stop us producing a special character like '(',
1032 # ')' or '!'. Yuck - there must be a better way.
1033 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1034 # We didn't break on a space. Arrange for the
1035 # beginning of the broken "word" to appear on the next
1036 # line but not if it will make us loop infinitely.
1037 if [ "${orig}" != "${out##* }${text}" ] ; then
1038 text="${out##* }${text}"
1041 # Hmmm, doing that would make us loop, so add the
1042 # rest of the word from the remainder of the text
1043 # to this line and let it extend past $COLUMNS - 2.
1044 out="${out}${text%% *}"
1045 if [ "${text# *}" != "$text" ] ; then
1046 # Remember the text after the next space for next time.
1049 # No text after next space.
1054 # We broke on a space. If it will be at the beginning
1055 # of the next line then remove it.
1064 # Display usage text, trying these approaches in order.
1065 # 1. See if it all fits on one line before $COLUMNS - 2.
1066 # 2. See if splitting before the default value and indenting it
1067 # to $startcol means that nothing passes $COLUMNS - 2.
1068 # 3. Treat the message and default value as a string and just us fill_text()
1070 usage_display_text () {
1075 local width=$(($COLUMNS - 2 - $startcol))
1076 [ $width -lt 0 ] && width=$((78 - $startcol))
1078 default="(default \"$default\")"
1080 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1081 echo "${desc} ${default}"
1083 local padding=$(printf "%${startcol}s" " ")
1085 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1087 echo "${padding}${default}"
1089 fill_text $startcol "${desc} ${default}"
1094 # Display usage information for long config options.
1095 usage_smart_display () {( # sub-shell for local declaration of defconf()
1099 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1101 printf " --%-25s " "${longopt}=${3}"
1103 usage_display_text $startcol "$4" "$2"
1110 # Display usage information for long config options.
1111 usage_config_options (){
1112 usage_smart_display load_config
1116 local releases=$(cd $installdir/releases && echo *.release)
1117 releases="${releases//.release}"
1118 releases="${releases// /\", \"}"
1119 echo "\"$releases\""
1122 ######################################################################
1126 ######################################################################
1130 ############################
1131 # parse command line options
1132 long_opts=$(getopt_config_options)
1133 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
1134 [ $? != 0 ] && usage
1136 use_default_config=true
1138 # We do 2 passes of the options. The first time we just handle usage
1139 # and check whether -c is being used.
1140 eval set -- "$getopt_output"
1143 -c) shift 2 ; use_default_config=false ;;
1146 --) shift ; break ;;
1147 --with-release) shift 2 ;; # Don't set use_default_config=false!!!
1149 -h|--help) usage ;; # Usage should be shown here for real defaults.
1150 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1151 *) usage ;; # shouldn't happen, so this is reasonable.
1156 $use_default_config && [ -r "$config" ] && . "$config"
1158 eval set -- "$getopt_output"
1162 # force at least ./local_file to avoid accidental file from $PATH
1163 -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
1164 -e) no_sanity=1 ; run_hooks post_config_hooks ; eval "$2" ; exit ;;
1165 -E) eval "$2" ; shift 2 ;;
1166 -x) set -x; shift ;;
1167 --dump) no_sanity=1 ; run_hooks post_config_hooks ; dump_config ;;
1168 --) shift ; break ;;
1169 -h|--help) usage ;; # Redundant.
1171 # Putting --opt1|opt2|... into a variable and having case
1172 # match against it as a pattern doesn't work. The | is
1173 # part of shell syntax, so we need to do this. Look away
1174 # now to stop your eyes from bleeding! :-)
1175 x=",${long_opts}" # Now each option is surrounded by , and :
1176 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1177 # Our option, $1, surrounded by , and : was in $x, so is legal.
1178 setconf_longopt "$1" "$2"; shift 2
1183 *) usage ;; # shouldn't happen, so this is reasonable.
1187 run_hooks post_config_hooks
1193 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1196 # check for needed programs
1197 check_command expect
1199 [ $# -lt 1 ] && usage
1210 [ $# != 0 ] && usage
1214 [ $# != 1 ] && usage
1218 [ $# != 2 ] && usage
1219 create_one_node "$1" "$2"
1227 [ $# != 1 ] && usage
1228 diskimage mount "$1"
1231 [ $# != 0 ] && usage