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")
44 releases=$(list_releases)
47 defconf "WITH_RELEASE" "" \
48 "<string>" "specify preset options for a release using a version string. Possible values are: ${releases}."
51 -e <expr> execute <expr> and exit
52 -E <expr> execute <expr> and continue
53 -x enable script debugging
54 --dump dump config settings and exit
56 configuration options:
67 create cluster CLUSTERNAME
70 create node CLUSTERNAME IP_OFFSET
71 (re)create a single cluster node
74 mount a qemu disk on mnt/
77 unmount a qemu disk from mnt/
88 ###############################
91 fill_text 0 "ERROR: $*" >&2
95 ###############################
97 # Indirectly call a function named by ${1}_${2}
99 local func="$1" ; shift
100 local type="$1" ; shift
102 local f="${func}_${type}"
103 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
107 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
110 die "No function defined for \"${func}\" \"${type}\""
115 # Note that this will work if you pass "call_func f" because the first
116 # element of the node tuple is the node type. Nice... :-)
121 "$@" $(IFS=: ; echo $n)
125 hack_one_node_with ()
127 local filter="$1" ; shift
136 local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
137 nodes="${nodes}${nodes:+ }${item}"
140 # This also gets used for non-filtering iteration.
141 hack_all_nodes_with ()
146 for_each_node hack_one_node_with "$filter"
155 eval "$hook_var=\"${!hook_var}${!hook_var:+ }${new_hook}\""
163 for i in ${!hook_var} ; do
168 # Use with care, since this may clear some autocluster defaults.!
173 eval "$hook_var=\"\""
176 ##############################
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 IPNUM=$(($FIRSTIP + $ip_offset))
191 DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
192 local base_disk="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
194 if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
195 base_disk="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
198 mkdir -p $VIRTBASE/$CLUSTER tmp
201 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
202 di=$(readlink "$DISK")
205 case "$SYSTEM_DISK_FORMAT" in
207 echo "Creating the disk..."
208 qemu-img create -b "$base_disk" -f qcow2 "$di"
209 create_node_configure_image "$DISK" "$type"
212 echo "Creating the disk..."
213 cp -v --sparse=always "$base_disk" "$di"
214 create_node_configure_image "$DISK" "$type"
217 echo "Creating the disk..."
218 cp -v --reflink=always "$base_disk" "$di"
219 create_node_configure_image "$DISK" "$type"
222 echo "Creating the disk (using mmclone)..."
223 local base_snap="${base_disk}.snap"
224 [ -f "$base_snap" ] || mmclone snap "$base_disk" "$base_snap"
225 mmclone copy "$base_snap" "$di"
226 create_node_configure_image "$DISK" "$type"
229 echo "Skipping disk image creation as requested"
232 die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
235 set_macaddrs $CLUSTER $ip_offset
237 # Pull the UUID for this node out of the map.
238 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
240 echo "Creating $NAME.xml"
241 substitute_vars $template_file tmp/$NAME.xml
243 # install the XML file
244 $VIRSH undefine $NAME > /dev/null 2>&1 || true
245 $VIRSH define tmp/$NAME.xml
248 create_node_configure_image ()
253 diskimage mount "$disk"
258 # Provides an easy way of removing nodes from $NODE.
259 create_node_null () {
263 ##############################
265 hack_nodes_functions=
268 # Expand out any abbreviations in NODES.
277 for i in ${ips//,/ } ; do
278 ns="${ns}${ns:+ }${t}:${i}"
283 for i in $(seq ${ips/-/ }) ; do
284 ns="${ns}${ns:+ }${t}:${i}"
288 ns="${ns}${ns:+ }${n}"
293 # Apply nodes hacks. Some of this is about backward compatibility
294 # but the hacks also fill in the node names and whether they're
295 # part of the CTDB cluster. The order is the order that
296 # configuration modules register their hacks.
297 run_hooks hack_nodes_functions
299 if [ -n "$NUMNODES" ] ; then
300 # Attempt to respect NUMNODES. Reduce the number of CTDB
302 local numnodes=$NUMNODES
306 if [ "$ctdb_node" = 1 ] ; then
307 if [ $numnodes -gt 0 ] ; then
308 numnodes=$(($numnodes - 1))
316 hack_all_nodes_with hack_filter
318 [ $numnodes -gt 0 ] && \
319 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."
322 # Check IP addresses for duplicates.
324 # This function doesn't modify anything...
327 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
328 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
329 ip_offsets="${ip_offsets}${ip_offset}:"
331 hack_all_nodes_with get_ip_offset
334 ##############################
336 sanity_check_cluster_name ()
338 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
339 die "Cluster names should be restricted to the characters A-Za-z0-9. \
340 Some cluster filesystems have problems with other characters."
345 common_nodelist_hacking ()
347 # Rework the NODES list
350 # Build /etc/hosts and hack the names of the ctdb nodes
351 hosts_line_hack_name ()
353 # Ignore nodes without names (e.g. "null")
354 [ "$node_type" != "null" -a -n "$name" ] || return 0
358 local ip_addr="$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
360 if [ "$ctdb_node" = 1 ] ; then
361 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
362 sname="${CLUSTER}n${num_ctdb_nodes}"
363 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
366 hosts_line="$ip_addr ${name}.${ld} $name"
369 # This allows you to add a function to your configuration file
370 # to modify hostnames (and other aspects of nodes). This
371 # function can access/modify $name (the existing name),
372 # $node_type and $ctdb_node (1, if the node is a member of the
373 # CTDB cluster, 0 otherwise).
374 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
375 local old_name="$name"
376 $HOSTNAME_HACKING_FUNCTION
377 if [ "$name" != "$old_name" ] ; then
378 hosts_line="$ip_addr ${name}.${ld} $name"
384 hosts_file="tmp/hosts.$CLUSTER"
386 local num_ctdb_nodes=0
387 local ld=$(echo $DOMAIN | tr A-Z a-z)
388 echo "# autocluster $CLUSTER"
389 hack_all_nodes_with hosts_line_hack_name
393 # Build /etc/ctdb/nodes
396 [ "$ctdb_node" = 1 ] || return 0
397 echo "$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
398 num_nodes=$(($num_nodes + 1))
400 nodes_file="tmp/nodes.$CLUSTER"
402 hack_all_nodes_with ctdb_nodes_line >$nodes_file
403 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
406 uuid_map="tmp/uuid_map.$CLUSTER"
409 echo "${ip_offset} $(uuidgen) ${node_type}"
411 hack_all_nodes_with uuid_map_line >$uuid_map
414 create_cluster_hooks=
415 cluster_created_hooks=
421 sanity_check_cluster_name
423 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
425 # Run hooks before doing anything else.
426 run_hooks create_cluster_hooks
428 common_nodelist_hacking
430 for_each_node call_func create_node
432 echo "Cluster $CLUSTER created"
435 run_hooks cluster_created_hooks
438 cluster_created_hosts_message ()
440 echo "You may want to add this to your /etc/hosts file:"
444 register_hook cluster_created_hooks cluster_created_hosts_message
449 local single_node_ip_offset="$2"
451 sanity_check_cluster_name
453 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
455 common_nodelist_hacking
458 set -- $(IFS=: ; echo $n)
459 [ $single_node_ip_offset -eq $2 ] || continue
460 call_func create_node "$@"
462 echo "Requested node created"
464 echo "You may want to update your /etc/hosts file:"
471 ###############################
472 # test the proxy setup
474 export http_proxy=$WEBPROXY
475 wget -O /dev/null $INSTALL_SERVER || \
476 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
482 kickstart_floppy_create_hooks=
488 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
492 echo "Testing WEBPROXY $WEBPROXY"
496 if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
497 di=$(readlink "$DISK")
500 echo "Creating the disk"
501 qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
504 mkdir -p mnt tmp tmp/ISO
508 echo "Creating kickstart file from template"
509 substitute_vars "$KICKSTART" "tmp/ks.cfg"
511 if [ $INSTALLKEY = "--skip" ]; then
513 --------------------------------------------------------------------------------------
514 WARNING: You have not entered an install key. Some RHEL packages will not be installed.
516 Please enter a valid RHEL install key in your config file like this:
518 INSTALLKEY="1234-5678-0123-4567"
520 The install will continue without an install key in 5 seconds
521 --------------------------------------------------------------------------------------
526 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
529 (*) ISO="${ISO_DIR}/${ISO}"
532 echo "Creating kickstart floppy"
533 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
534 mkdosfs tmp/floppy.img
535 mount -o loop -t msdos tmp/floppy.img mnt
537 mount -o loop,ro $ISO tmp/ISO
539 echo "Setting up bootloader"
540 cp tmp/ISO/isolinux/isolinux.bin tmp
541 cp tmp/ISO/isolinux/vmlinuz tmp
542 cp tmp/ISO/isolinux/initrd.img tmp
544 run_hooks kickstart_floppy_create_hooks
551 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
553 rm -f $KVMLOG/serial.$NAME
555 # boot the install CD
556 $VIRSH create tmp/$NAME.xml
558 echo "Waiting for install to start"
561 # wait for the install to finish
562 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
564 die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
565 You may need to increase the value of CREATE_BASE_TIMEOUT.
566 Alternatively, the install might have completed but KS_DONE_MESSAGE
567 (currently \"${KS_DONE_MESSAGE}\")
568 may not have matched anything at the end of the kickstart output."
576 Install finished, base image $DISK created
580 To ensure that this image does not change
582 Note that the root password has been set to $ROOTPASSWORD
587 ###############################
593 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
601 diskimage mount $DISK
607 echo "Creating $NAME.xml"
608 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
610 # boot the base system
611 $VIRSH create tmp/$NAME.xml
614 ######################################################################
616 # various functions...
618 # Set some MAC address variables based on a hash of the cluster name
619 # plus the node number and each adapter number.
624 local md5=$(echo $cname | md5sum)
625 local nh=$(printf "%02x" $ip_offset)
626 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
628 MAC1="${mac_prefix}01"
629 MAC2="${mac_prefix}02"
630 MAC3="${mac_prefix}03"
631 MAC4="${mac_prefix}04"
632 MAC5="${mac_prefix}05"
633 MAC6="${mac_prefix}06"
636 ######################################################################
638 # Updating a disk image...
644 call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
647 # setup the files from $BASE_TEMPLATES/, substituting any variables
648 # based on the config
649 copy_base_dir_substitute_templates ()
653 local d="$BASE_TEMPLATES/$dir"
654 [ -d "$d" ] || return 0
657 for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
658 f="${f#./}" # remove leading "./" for clarity
659 if [ -d "$d/$f" ]; then
660 # Don't chmod existing directory
661 if diskimage is_directory "/$f" ; then
664 diskimage mkdir_p "/$f"
667 diskimage substitute_vars "$d/$f" "/$f"
669 diskimage chmod_reference "$d/$f" "/$f"
675 setup_base_ssh_keys ()
677 # this is needed as git doesn't store file permissions other
679 # Note that we protect the wildcards from the local shell.
680 diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
681 diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
682 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
683 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
684 diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
686 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
687 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
688 diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
692 register_hook setup_base_hooks setup_base_ssh_keys
694 setup_base_grub_conf ()
696 echo "Adjusting grub.conf"
697 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
698 diskimage sed "/boot/grub/grub.conf" \
699 -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
700 -e "s/ nodmraid//" -e "s/ nompath//" \
701 -e "s/quiet/noapic divider=10${o:+ }${o}/g"
704 register_hook setup_base_hooks setup_base_grub_conf
711 echo "Copy base files"
712 copy_base_dir_substitute_templates "all"
713 if [ -n "$type" ] ; then
714 copy_base_dir_substitute_templates "$type"
717 run_hooks setup_base_hooks
720 # setup various networking components
723 # This avoids doing anything when we're called from boot_base().
724 if [ -z "$hosts_file" ] ; then
725 echo "Skipping network-related setup"
729 echo "Setting up networks"
730 diskimage append_text_file "$hosts_file" "/etc/hosts"
732 echo "Setting up /etc/ctdb/nodes"
733 diskimage mkdir_p "/etc/ctdb"
734 diskimage put "$nodes_file" "/etc/ctdb/nodes"
736 [ "$WEBPROXY" = "" ] || {
737 diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
740 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
741 echo "Enabling nfs mount of $NFSSHARE"
742 diskimage mkdir_p "$NFS_MOUNTPOINT"
743 diskimage append_text "$NFSSHARE $NFS_MOUNTPOINT nfs nfsvers=3,intr 0 0" "/etc/fstab"
746 diskimage mkdir_p "/etc/yum.repos.d"
747 echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
749 diskimage rm_f "/etc/udev/rules.d/70-persistent-net.rules"
752 register_hook setup_base_hooks setup_network
755 [ -z "$TIMEZONE" ] && {
756 [ -r /etc/timezone ] && {
757 TIMEZONE=`cat /etc/timezone`
759 [ -r /etc/sysconfig/clock ] && {
760 . /etc/sysconfig/clock
763 TIMEZONE="${TIMEZONE// /_}"
765 [ -n "$TIMEZONE" ] || \
766 die "Unable to determine TIMEZONE - please set in config"
769 # substite a set of variables of the form @@XX@@ for the shell
770 # variables $XX in a file.
772 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
773 # contain a filename whose contents are substituted, with variable
774 # substitution applied to those contents. If filename starts with '|'
775 # it is a command instead - however, quoting is extremely fragile.
777 infile="${1:-/dev/null}" # if empty then default to /dev/null
778 outfile="$2" # optional
781 cat "$infile" >"$tmp_out"
783 # Handle any indirects by looping until nothing changes.
784 # However, only handle 10 levels of recursion.
787 if ! _substitute_vars "$tmp_out" "@@@" ; then
789 die "Failed to expand template $infile"
792 # No old version of file means no changes made.
793 if [ ! -f "${tmp_out}.old" ] ; then
797 rm -f "${tmp_out}.old"
799 count=$(($count + 1))
800 if [ $count -ge 10 ] ; then
802 die "Recursion too deep in $infile - only 10 levels allowed!"
806 # Now regular variables.
807 if ! _substitute_vars "$tmp_out" "@@" ; then
809 die "Failed to expand template $infile"
811 rm -f "${tmp_out}.old"
813 if [ -n "$outfile" ] ; then
814 mv "$tmp_out" "$outfile"
822 # Delimiter @@ means to substitute contents of variable.
823 # Delimiter @@@ means to substitute contents of file named by variable.
824 # @@@ supports leading '|' in variable value, which means to excute a
826 _substitute_vars() {(
830 # Get the list of variables used in the template. The grep
831 # gets rid of any blank lines and lines with extraneous '@'s
832 # next to template substitutions.
833 VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
834 grep '^[A-Z0-9_][A-Z0-9_]*$' |
839 # variable variables are fun .....
842 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
846 if [ "$delimiter" = "@@@" ] ; then
848 c="${f#|}" # Is is a command, signified by a leading '|'?
849 if [ "$c" = "$f" ] ; then
850 # No leading '|', cat file.
854 die "Could not substitute contents of file $f"
857 # Leading '|', execute command.
858 # Quoting problems here - using eval "$c" doesn't help.
862 die "Could not execute command $c"
867 # escape some pesky chars
868 # This first one can be too slow if done using a bash
869 # variable pattern subsitution.
870 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
873 echo "s#${delimiter}${v}${delimiter}#${s}#g"
876 # Get the in-place sed to make a backup of the old file.
877 # Remove the backup if it is the same as the resulting file -
878 # this acts as a flag to the caller that no changes were made.
879 sed -i.old -f $tmp "$tmp_out"
880 if cmp -s "${tmp_out}.old" "$tmp_out" ; then
881 rm -f "${tmp_out}.old"
888 which $1 > /dev/null || die "Please install $1 to continue"
891 # Set a variable if it isn't already set. This allows environment
892 # variables to override default config settings.
897 [ "${!v+x}" ] || eval "$v=\"$e\""
903 for i in "${installdir}/config.d/"*.defconf ; do
908 # Print the list of config variables defined in config.d/.
909 get_config_options () {( # sub-shell for local declaration of defconf()
911 defconf() { options="$options $1" ; }
916 # Produce a list of long options, suitable for use with getopt, that
917 # represent the config variables defined in config.d/.
918 getopt_config_options () {
919 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
923 # Unconditionally set the config variable associated with the given
929 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
930 # unset so defconf will set it
935 # Dump all of the current config variables.
938 for o in $(get_config_options) ; do
939 echo "${o}=\"${!o}\""
944 # $COLUMNS is set in interactive bash shells. It probably isn't set
945 # in this shell, so let's set it if it isn't.
946 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
950 # Print text assuming it starts after other text in $startcol and
951 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
952 # Long "words" will extend past $COLUMNS - 2.
957 local width=$(($COLUMNS - 2 - $startcol))
958 [ $width -lt 0 ] && width=$((78 - $startcol))
963 if [ $startcol -gt 0 ] ; then
964 padding=$(printf "\n%${startcol}s" " ")
970 while [ -n "$text" ] ; do
973 # If we already have output then arrange padding on the next line.
974 [ -n "$out" ] && out="${out}${padding}"
976 # Break the text at $width.
977 out="${out}${text:0:${width}}"
978 text="${text:${width}}"
980 # If we have left over text then the line break may be ugly,
981 # so let's check and try to break it on a space.
982 if [ -n "$text" ] ; then
983 # The 'x's stop us producing a special character like '(',
984 # ')' or '!'. Yuck - there must be a better way.
985 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
986 # We didn't break on a space. Arrange for the
987 # beginning of the broken "word" to appear on the next
988 # line but not if it will make us loop infinitely.
989 if [ "${orig}" != "${out##* }${text}" ] ; then
990 text="${out##* }${text}"
993 # Hmmm, doing that would make us loop, so add the
994 # rest of the word from the remainder of the text
995 # to this line and let it extend past $COLUMNS - 2.
996 out="${out}${text%% *}"
997 if [ "${text# *}" != "$text" ] ; then
998 # Remember the text after the next space for next time.
1001 # No text after next space.
1006 # We broke on a space. If it will be at the beginning
1007 # of the next line then remove it.
1016 # Display usage text, trying these approaches in order.
1017 # 1. See if it all fits on one line before $COLUMNS - 2.
1018 # 2. See if splitting before the default value and indenting it
1019 # to $startcol means that nothing passes $COLUMNS - 2.
1020 # 3. Treat the message and default value as a string and just us fill_text()
1022 usage_display_text () {
1027 local width=$(($COLUMNS - 2 - $startcol))
1028 [ $width -lt 0 ] && width=$((78 - $startcol))
1030 default="(default \"$default\")"
1032 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1033 echo "${desc} ${default}"
1035 local padding=$(printf "%${startcol}s" " ")
1037 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1039 echo "${padding}${default}"
1041 fill_text $startcol "${desc} ${default}"
1046 # Display usage information for long config options.
1047 usage_smart_display () {( # sub-shell for local declaration of defconf()
1051 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1053 printf " --%-25s " "${longopt}=${3}"
1055 usage_display_text $startcol "$4" "$2"
1062 # Display usage information for long config options.
1063 usage_config_options (){
1064 usage_smart_display load_config
1068 local releases=$(cd $installdir/releases && echo *.release)
1069 releases="${releases//.release}"
1070 releases="${releases// /\", \"}"
1071 echo "\"$releases\""
1076 shift # subsequent args are passed to release file
1078 # This simply loads an extra config file from $installdir/releases
1079 f="${installdir}/releases/${release}.release"
1080 if [ -r "$f" ] ; then
1083 f="${installdir}/releases/${release%%-*}.release"
1084 if [ -r "$f" ] ; then
1085 . "$f" "${release#*-}"
1087 echo "Unknown release \"${release}\" specified to --with-release"
1088 printf "%-25s" "Supported releases are: "
1089 fill_text 25 "$(list_releases)"
1096 has_public_addresses_DEFAULT ()
1101 make_public_addresses() {
1102 local firstip="${1:-$[${FIRSTIP} + ${PUBLIC_IP_OFFSET}]}"
1103 local num_addrs="${2:-${NUMNODES}}"
1105 if [ $(( $firstip + $num_addrs - 1 )) -gt 254 ]; then
1106 die "make_public_addresses: last octet > 254 - change PUBLIC_IP_OFFSET"
1110 for e in $IPNET1 $IPNET2 ; do
1111 echo -ne "${IPBASE}.${e}.${firstip},${num_addrs},eth${e} "
1116 ######################################################################
1120 ######################################################################
1124 ############################
1125 # parse command line options
1126 long_opts=$(getopt_config_options)
1127 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
1128 [ $? != 0 ] && usage
1130 use_default_config=true
1132 # We do 2 passes of the options. The first time we just handle usage
1133 # and check whether -c is being used.
1134 eval set -- "$getopt_output"
1137 -c) shift 2 ; use_default_config=false ;;
1140 --) shift ; break ;;
1141 --with-release) shift 2 ;; # Don't set use_default_config=false!!!
1143 -h|--help) usage ;; # Usage should be shown here for real defaults.
1144 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1145 *) usage ;; # shouldn't happen, so this is reasonable.
1150 $use_default_config && [ -r "$config" ] && . "$config"
1152 eval set -- "$getopt_output"
1156 # force at least ./local_file to avoid accidental file from $PATH
1157 -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
1158 -e) run_hooks post_config_hooks ; eval "$2" ; exit ;;
1159 -E) eval "$2" ; shift 2 ;;
1164 -x) set -x; shift ;;
1165 --dump) run_hooks post_config_hooks ; dump_config ;;
1166 --) shift ; break ;;
1167 -h|--help) usage ;; # Redundant.
1169 # Putting --opt1|opt2|... into a variable and having case
1170 # match against it as a pattern doesn't work. The | is
1171 # part of shell syntax, so we need to do this. Look away
1172 # now to stop your eyes from bleeding! :-)
1173 x=",${long_opts}" # Now each option is surrounded by , and :
1174 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1175 # Our option, $1, surrounded by , and : was in $x, so is legal.
1176 setconf_longopt "$1" "$2"; shift 2
1181 *) usage ;; # shouldn't happen, so this is reasonable.
1185 run_hooks post_config_hooks
1191 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1194 # check for needed programs
1195 check_command expect
1197 [ $# -lt 1 ] && usage
1208 [ $# != 0 ] && usage
1212 [ $# != 1 ] && usage
1216 [ $# != 2 ] && usage
1217 create_one_node "$1" "$2"
1225 [ $# != 1 ] && usage
1226 diskimage mount "$1"
1229 [ $# != 0 ] && usage