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 hack_network_map_hooks=""
278 # Uses: CLUSTER, NAME, NETWORKS, FIRSTIP, ip_offset
281 network_map="tmp/network_map.$NAME"
283 if [ -n "$CLUSTER" ] ; then
284 local md5=$(echo "$CLUSTER" | md5sum)
285 local nh=$(printf "%02x" $ip_offset)
286 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
288 local mac_prefix="02:42:42:00:00:"
293 for n in $NETWORKS ; do
294 local ch=$(printf "%02x" $count)
295 local mac="${mac_prefix}${ch}"
298 local ip_bits="$1" ; shift
299 local dev="$1" ; shift
302 local net="${ip_bits%/*}"
303 local netname="acnet_${net//./_}"
305 local ip="${net%.*}.${IPNUM}"
306 local mask="255.255.255.0"
308 # This can be used to override the variables in the echo
309 # statement below. The hook can use any other variables
310 # available in this function.
311 run_hooks hack_network_map_hooks
313 echo "${netname} ${dev} ${ip} ${mask} ${mac} ${opts}"
314 count=$(($count + 1))
318 ##############################
320 hack_nodes_functions=
323 # Expand out any abbreviations in NODES.
332 for i in ${ips//,/ } ; do
333 ns="${ns}${ns:+ }${t}:${i}"
338 for i in $(seq ${ips/-/ }) ; do
339 ns="${ns}${ns:+ }${t}:${i}"
343 ns="${ns}${ns:+ }${n}"
348 # Apply nodes hacks. Some of this is about backward compatibility
349 # but the hacks also fill in the node names and whether they're
350 # part of the CTDB cluster. The order is the order that
351 # configuration modules register their hacks.
352 run_hooks hack_nodes_functions
354 if [ -n "$NUMNODES" ] ; then
355 # Attempt to respect NUMNODES. Reduce the number of CTDB
357 local numnodes=$NUMNODES
361 if [ "$ctdb_node" = 1 ] ; then
362 if [ $numnodes -gt 0 ] ; then
363 numnodes=$(($numnodes - 1))
371 hack_all_nodes_with hack_filter
373 [ $numnodes -gt 0 ] && \
374 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."
377 # Check IP addresses for duplicates.
379 # This function doesn't modify anything...
382 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
383 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
384 ip_offsets="${ip_offsets}${ip_offset}:"
386 hack_all_nodes_with get_ip_offset
389 ##############################
391 sanity_check_cluster_name ()
393 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
394 die "Cluster names should be restricted to the characters A-Za-z0-9. \
395 Some cluster filesystems have problems with other characters."
400 common_nodelist_hacking ()
402 # Rework the NODES list
405 # Build /etc/hosts and hack the names of the ctdb nodes
406 hosts_line_hack_name ()
408 # Ignore nodes without names (e.g. "null")
409 [ "$node_type" != "null" -a -n "$name" ] || return 0
413 local ip_addr="${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
415 if [ "$ctdb_node" = 1 ] ; then
416 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
417 sname="${CLUSTER}n${num_ctdb_nodes}"
418 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
421 hosts_line="$ip_addr ${name}.${ld} $name"
424 # This allows you to add a function to your configuration file
425 # to modify hostnames (and other aspects of nodes). This
426 # function can access/modify $name (the existing name),
427 # $node_type and $ctdb_node (1, if the node is a member of the
428 # CTDB cluster, 0 otherwise).
429 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
430 local old_name="$name"
431 $HOSTNAME_HACKING_FUNCTION
432 if [ "$name" != "$old_name" ] ; then
433 hosts_line="$ip_addr ${name}.${ld} $name"
439 hosts_file="tmp/hosts.$CLUSTER"
441 local num_ctdb_nodes=0
442 local ld=$(echo $DOMAIN | tr A-Z a-z)
443 echo "# autocluster $CLUSTER"
444 hack_all_nodes_with hosts_line_hack_name
448 # Build /etc/ctdb/nodes
451 [ "$ctdb_node" = 1 ] || return 0
452 echo "${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
453 num_nodes=$(($num_nodes + 1))
455 nodes_file="tmp/nodes.$CLUSTER"
457 hack_all_nodes_with ctdb_nodes_line >$nodes_file
458 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
461 uuid_map="tmp/uuid_map.$CLUSTER"
464 echo "${ip_offset} $(uuidgen) ${node_type}"
466 hack_all_nodes_with uuid_map_line >$uuid_map
469 create_cluster_hooks=
470 cluster_created_hooks=
476 sanity_check_cluster_name
478 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
480 # Run hooks before doing anything else.
481 run_hooks create_cluster_hooks
483 common_nodelist_hacking
485 for_each_node call_func create_node
487 echo "Cluster $CLUSTER created"
490 run_hooks cluster_created_hooks
493 cluster_created_hosts_message ()
495 echo "You may want to add this to your /etc/hosts file:"
499 register_hook cluster_created_hooks cluster_created_hosts_message
504 local single_node_ip_offset="$2"
506 sanity_check_cluster_name
508 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
510 common_nodelist_hacking
513 set -- $(IFS=: ; echo $n)
514 [ $single_node_ip_offset -eq $2 ] || continue
515 call_func create_node "$@"
517 echo "Requested node created"
519 echo "You may want to update your /etc/hosts file:"
526 ###############################
527 # test the proxy setup
529 export http_proxy=$WEBPROXY
530 wget -O /dev/null $INSTALL_SERVER || \
531 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
537 kickstart_floppy_create_hooks=
542 local NAME="$BASENAME"
543 local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
544 run_hooks hack_disk_hooks "base"
548 echo "Testing WEBPROXY $WEBPROXY"
552 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
553 di=$(readlink "$DISK")
556 local di_dirname="${di%/*}"
557 mkdir -p "$di_dirname"
559 echo "Creating the disk"
560 qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
563 mkdir -p mnt tmp tmp/ISO
569 echo "Creating kickstart file from template"
570 substitute_vars "$KICKSTART" "tmp/ks.cfg"
572 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
575 (*) ISO="${ISO_DIR}/${ISO}"
578 echo "Creating kickstart floppy"
579 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
580 mkdosfs -n KICKSTART tmp/floppy.img
581 mount -o loop -t msdos tmp/floppy.img mnt
583 mount -o loop,ro $ISO tmp/ISO
585 echo "Setting up bootloader"
586 cp tmp/ISO/isolinux/isolinux.bin tmp
587 cp tmp/ISO/isolinux/vmlinuz tmp
588 cp tmp/ISO/isolinux/initrd.img tmp
590 run_hooks kickstart_floppy_create_hooks
597 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
599 rm -f $KVMLOG/serial.$NAME
601 # boot the install CD
602 $VIRSH create tmp/$NAME.xml
604 echo "Waiting for install to start"
607 # wait for the install to finish
608 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
610 die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
611 You may need to increase the value of CREATE_BASE_TIMEOUT.
612 Alternatively, the install might have completed but KS_DONE_MESSAGE
613 (currently \"${KS_DONE_MESSAGE}\")
614 may not have matched anything at the end of the kickstart output."
622 Install finished, base image $DISK created
626 To ensure that this image does not change
628 Note that the root password has been set to $ROOTPASSWORD
633 ###############################
640 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
648 diskimage mount $DISK
654 echo "Creating $NAME.xml"
655 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
657 # boot the base system
658 $VIRSH create tmp/$NAME.xml
661 ######################################################################
663 # Updating a disk image...
669 call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
672 # setup the files from $BASE_TEMPLATES/, substituting any variables
673 # based on the config
674 copy_base_dir_substitute_templates ()
678 local d="$BASE_TEMPLATES/$dir"
679 [ -d "$d" ] || return 0
682 for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
683 f="${f#./}" # remove leading "./" for clarity
684 if [ -d "$d/$f" ]; then
685 # Don't chmod existing directory
686 if diskimage is_directory "/$f" ; then
689 diskimage mkdir_p "/$f"
692 diskimage substitute_vars "$d/$f" "/$f"
694 diskimage chmod_reference "$d/$f" "/$f"
700 setup_base_ssh_keys ()
702 # this is needed as git doesn't store file permissions other
704 # Note that we protect the wildcards from the local shell.
705 diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
706 diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
707 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
708 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
709 diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
711 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
712 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
713 diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
717 register_hook setup_base_hooks setup_base_ssh_keys
719 setup_base_grub_conf ()
721 echo "Adjusting grub.conf"
722 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
723 local grub_configs="/boot/grub/grub.conf"
724 if ! diskimage is_file "$grub_configs" ; then
725 grub_configs="/etc/default/grub /boot/grub2/grub.cfg"
728 for c in $grub_configs ; do
730 -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
731 -e "s/ nodmraid//" -e "s/ nompath//" \
732 -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 put - "/etc/sysconfig/network-scripts/ifcfg-${dev}"
797 # This goes to 70-persistent-net.rules
799 # Generated by autocluster
800 SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${mac}", ATTR{type}=="1", KERNEL=="eth*", NAME="${dev}"
803 done <"$network_map" |
804 diskimage put - "/etc/udev/rules.d/70-persistent-net.rules"
807 register_hook setup_base_hooks setup_network
810 [ -z "$TIMEZONE" ] && {
811 [ -r /etc/timezone ] && {
812 TIMEZONE=`cat /etc/timezone`
814 [ -r /etc/sysconfig/clock ] && {
815 . /etc/sysconfig/clock
818 TIMEZONE="${TIMEZONE// /_}"
820 [ -n "$TIMEZONE" ] || \
821 die "Unable to determine TIMEZONE - please set in config"
824 # substite a set of variables of the form @@XX@@ for the shell
825 # variables $XX in a file.
827 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
828 # contain a filename whose contents are substituted, with variable
829 # substitution applied to those contents. If filename starts with '|'
830 # it is a command instead - however, quoting is extremely fragile.
832 infile="${1:-/dev/null}" # if empty then default to /dev/null
833 outfile="$2" # optional
836 cat "$infile" >"$tmp_out"
838 # Handle any indirects by looping until nothing changes.
839 # However, only handle 10 levels of recursion.
842 if ! _substitute_vars "$tmp_out" "@@@" ; then
844 die "Failed to expand template $infile"
847 # No old version of file means no changes made.
848 if [ ! -f "${tmp_out}.old" ] ; then
852 rm -f "${tmp_out}.old"
854 count=$(($count + 1))
855 if [ $count -ge 10 ] ; then
857 die "Recursion too deep in $infile - only 10 levels allowed!"
861 # Now regular variables.
862 if ! _substitute_vars "$tmp_out" "@@" ; then
864 die "Failed to expand template $infile"
866 rm -f "${tmp_out}.old"
868 if [ -n "$outfile" ] ; then
869 mv "$tmp_out" "$outfile"
877 # Delimiter @@ means to substitute contents of variable.
878 # Delimiter @@@ means to substitute contents of file named by variable.
879 # @@@ supports leading '|' in variable value, which means to excute a
881 _substitute_vars() {(
885 # Get the list of variables used in the template. The grep
886 # gets rid of any blank lines and lines with extraneous '@'s
887 # next to template substitutions.
888 VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
889 grep '^[A-Z0-9_][A-Z0-9_]*$' |
894 # variable variables are fun .....
897 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
901 if [ "$delimiter" = "@@@" ] ; then
903 c="${f#|}" # Is is a command, signified by a leading '|'?
904 if [ "$c" = "$f" ] ; then
905 # No leading '|', cat file.
909 die "Could not substitute contents of file $f"
912 # Leading '|', execute command.
913 # Quoting problems here - using eval "$c" doesn't help.
917 die "Could not execute command $c"
922 # escape some pesky chars
923 # This first one can be too slow if done using a bash
924 # variable pattern subsitution.
925 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
928 echo "s#${delimiter}${v}${delimiter}#${s}#g"
931 # Get the in-place sed to make a backup of the old file.
932 # Remove the backup if it is the same as the resulting file -
933 # this acts as a flag to the caller that no changes were made.
934 sed -i.old -f $tmp "$tmp_out"
935 if cmp -s "${tmp_out}.old" "$tmp_out" ; then
936 rm -f "${tmp_out}.old"
943 which $1 > /dev/null || die "Please install $1 to continue"
946 # Set a variable if it isn't already set. This allows environment
947 # variables to override default config settings.
952 [ "${!v+x}" ] || eval "$v=\"$e\""
958 for i in "${installdir}/config.d/"*.defconf ; do
963 # Print the list of config variables defined in config.d/.
964 get_config_options () {( # sub-shell for local declaration of defconf()
966 defconf() { options="$options $1" ; }
971 # Produce a list of long options, suitable for use with getopt, that
972 # represent the config variables defined in config.d/.
973 getopt_config_options () {
974 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
978 # Unconditionally set the config variable associated with the given
984 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
985 # unset so defconf will set it
990 # Dump all of the current config variables.
993 for o in $(get_config_options) ; do
994 echo "${o}=\"${!o}\""
999 # $COLUMNS is set in interactive bash shells. It probably isn't set
1000 # in this shell, so let's set it if it isn't.
1001 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
1005 # Print text assuming it starts after other text in $startcol and
1006 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
1007 # Long "words" will extend past $COLUMNS - 2.
1012 local width=$(($COLUMNS - 2 - $startcol))
1013 [ $width -lt 0 ] && width=$((78 - $startcol))
1018 if [ $startcol -gt 0 ] ; then
1019 padding=$(printf "\n%${startcol}s" " ")
1025 while [ -n "$text" ] ; do
1028 # If we already have output then arrange padding on the next line.
1029 [ -n "$out" ] && out="${out}${padding}"
1031 # Break the text at $width.
1032 out="${out}${text:0:${width}}"
1033 text="${text:${width}}"
1035 # If we have left over text then the line break may be ugly,
1036 # so let's check and try to break it on a space.
1037 if [ -n "$text" ] ; then
1038 # The 'x's stop us producing a special character like '(',
1039 # ')' or '!'. Yuck - there must be a better way.
1040 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1041 # We didn't break on a space. Arrange for the
1042 # beginning of the broken "word" to appear on the next
1043 # line but not if it will make us loop infinitely.
1044 if [ "${orig}" != "${out##* }${text}" ] ; then
1045 text="${out##* }${text}"
1048 # Hmmm, doing that would make us loop, so add the
1049 # rest of the word from the remainder of the text
1050 # to this line and let it extend past $COLUMNS - 2.
1051 out="${out}${text%% *}"
1052 if [ "${text# *}" != "$text" ] ; then
1053 # Remember the text after the next space for next time.
1056 # No text after next space.
1061 # We broke on a space. If it will be at the beginning
1062 # of the next line then remove it.
1071 # Display usage text, trying these approaches in order.
1072 # 1. See if it all fits on one line before $COLUMNS - 2.
1073 # 2. See if splitting before the default value and indenting it
1074 # to $startcol means that nothing passes $COLUMNS - 2.
1075 # 3. Treat the message and default value as a string and just us fill_text()
1077 usage_display_text () {
1082 local width=$(($COLUMNS - 2 - $startcol))
1083 [ $width -lt 0 ] && width=$((78 - $startcol))
1085 default="(default \"$default\")"
1087 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1088 echo "${desc} ${default}"
1090 local padding=$(printf "%${startcol}s" " ")
1092 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1094 echo "${padding}${default}"
1096 fill_text $startcol "${desc} ${default}"
1101 # Display usage information for long config options.
1102 usage_smart_display () {( # sub-shell for local declaration of defconf()
1106 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1108 printf " --%-25s " "${longopt}=${3}"
1110 usage_display_text $startcol "$4" "$2"
1117 # Display usage information for long config options.
1118 usage_config_options (){
1119 usage_smart_display load_config
1123 local releases=$(cd $installdir/releases && echo *.release)
1124 releases="${releases//.release}"
1125 releases="${releases// /\", \"}"
1126 echo "\"$releases\""
1129 ######################################################################
1133 ######################################################################
1137 ############################
1138 # parse command line options
1139 long_opts=$(getopt_config_options)
1140 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
1141 [ $? != 0 ] && usage
1143 use_default_config=true
1145 # We do 2 passes of the options. The first time we just handle usage
1146 # and check whether -c is being used.
1147 eval set -- "$getopt_output"
1150 -c) shift 2 ; use_default_config=false ;;
1153 --) shift ; break ;;
1154 --with-release) shift 2 ;; # Don't set use_default_config=false!!!
1156 -h|--help) usage ;; # Usage should be shown here for real defaults.
1157 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1158 *) usage ;; # shouldn't happen, so this is reasonable.
1163 $use_default_config && [ -r "$config" ] && . "$config"
1165 eval set -- "$getopt_output"
1169 # force at least ./local_file to avoid accidental file from $PATH
1170 -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
1171 -e) no_sanity=1 ; run_hooks post_config_hooks ; eval "$2" ; exit ;;
1172 -E) eval "$2" ; shift 2 ;;
1173 -x) set -x; shift ;;
1174 --dump) no_sanity=1 ; run_hooks post_config_hooks ; dump_config ;;
1175 --) shift ; break ;;
1176 -h|--help) usage ;; # Redundant.
1178 # Putting --opt1|opt2|... into a variable and having case
1179 # match against it as a pattern doesn't work. The | is
1180 # part of shell syntax, so we need to do this. Look away
1181 # now to stop your eyes from bleeding! :-)
1182 x=",${long_opts}" # Now each option is surrounded by , and :
1183 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1184 # Our option, $1, surrounded by , and : was in $x, so is legal.
1185 setconf_longopt "$1" "$2"; shift 2
1190 *) usage ;; # shouldn't happen, so this is reasonable.
1194 run_hooks post_config_hooks
1200 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1203 # check for needed programs
1204 check_command expect
1206 [ $# -lt 1 ] && usage
1217 [ $# != 0 ] && usage
1221 [ $# != 1 ] && usage
1225 [ $# != 2 ] && usage
1226 create_one_node "$1" "$2"
1234 [ $# != 1 ] && usage
1235 diskimage mount "$1"
1238 [ $# != 0 ] && usage