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. :-)
24 installdir="`dirname \"$0\"`"
26 autocluster=`which $0`
27 installdir="`dirname \"$autocluster\"`"
29 ##END-INSTALLDIR-MAGIC##
36 Usage: autocluster [OPTION] ... <COMMAND>
38 -c <file> specify config file (default is "config")
41 releases=$(list_releases)
44 defconf "WITH_RELEASE" "" \
45 "<string>" "specify preset options for a release using a version string. Possible values are: ${releases}."
48 -e <expr> execute <expr> and exit (advanced debugging)
49 -x enable script debugging
50 --dump dump config settings and exit
52 configuration options:
63 create cluster CLUSTERNAME
66 create node CLUSTERNAME IP_OFFSET
67 (re)create a single cluster node
70 mount a qemu disk on mnt/
73 unmount a qemu disk from mnt/
84 ###############################
87 fill_text 0 "ERROR: $*" >&2
91 ###############################
93 # Indirectly call a function named by ${1}_${2}
95 local func="$1" ; shift
96 local type="$1" ; shift
98 local f="${func}_${type}"
99 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
103 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
106 die "No function defined for \"${func}\" \"${type}\""
111 # Note that this will work if you pass "call_func f" because the first
112 # element of the node tuple is the node type. Nice... :-)
117 "$@" $(IFS=: ; echo $n)
121 hack_one_node_with ()
123 local filter="$1" ; shift
132 local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
133 nodes="${nodes}${nodes:+ }${item}"
136 # This also gets used for non-filtering iteration.
137 hack_all_nodes_with ()
142 for_each_node hack_one_node_with "$filter"
151 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 # common node creation stuff
175 create_node_COMMON ()
180 local template_file="${4:-$NODE_TEMPLATE}"
182 if [ "$SYSTEM_DISK_FORMAT" = "raw" -a "$BASE_FORMAT" != "raw" ] ; then
183 die "Error: if SYSTEM_DISK_FORMAT is \"raw\" then BASE_FORMAT must also be \"raw\"."
186 IPNUM=$(($FIRSTIP + $ip_offset))
187 DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
188 local base_disk="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
190 if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
191 base_disk="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
194 mkdir -p $VIRTBASE/$CLUSTER tmp
197 case "$SYSTEM_DISK_FORMAT" in
199 echo "Creating the disk..."
200 qemu-img create -b "$base_disk" -f qcow2 "$DISK"
201 create_node_configure_image "$DISK" "$type"
204 echo "Creating the disk..."
205 cp -v --sparse=always "$base_disk" "$DISK"
206 create_node_configure_image "$DISK" "$type"
209 echo "Skipping disk image creation as requested"
212 die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
215 set_macaddrs $CLUSTER $ip_offset
217 # Pull the UUID for this node out of the map.
218 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
220 echo "Creating $NAME.xml"
221 substitute_vars $template_file tmp/$NAME.xml
223 # install the XML file
224 $VIRSH undefine $NAME > /dev/null 2>&1 || true
225 $VIRSH define tmp/$NAME.xml
228 create_node_configure_image ()
238 # Provides an easy way of removing nodes from $NODE.
239 create_node_null () {
243 ##############################
245 hack_nodes_functions=
248 # Expand out any abbreviations in NODES.
257 for i in ${ips//,/ } ; do
258 ns="${ns}${ns:+ }${t}:${i}"
263 for i in $(seq ${ips/-/ }) ; do
264 ns="${ns}${ns:+ }${t}:${i}"
268 ns="${ns}${ns:+ }${n}"
273 # Apply nodes hacks. Some of this is about backward compatibility
274 # but the hacks also fill in the node names and whether they're
275 # part of the CTDB cluster. The order is the order that
276 # configuration modules register their hacks.
277 run_hooks hack_nodes_functions
279 if [ -n "$NUMNODES" ] ; then
280 # Attempt to respect NUMNODES. Reduce the number of CTDB
282 local numnodes=$NUMNODES
286 if [ "$ctdb_node" = 1 ] ; then
287 if [ $numnodes -gt 0 ] ; then
288 numnodes=$(($numnodes - 1))
296 hack_all_nodes_with hack_filter
298 [ $numnodes -gt 0 ] && \
299 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."
302 # Check IP addresses for duplicates.
304 # This function doesn't modify anything...
307 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
308 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
309 ip_offsets="${ip_offsets}${ip_offset}:"
311 hack_all_nodes_with get_ip_offset
314 ##############################
316 sanity_check_cluster_name ()
318 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
319 die "Cluster names should be restricted to the characters A-Za-z0-9. \
320 Some cluster filesystems have problems with other characters."
325 common_nodelist_hacking ()
327 # Rework the NODES list
330 # Build /etc/hosts and hack the names of the ctdb nodes
331 hosts_line_hack_name ()
333 # Ignore nodes without names (e.g. "null")
334 [ "$node_type" != "null" -a -n "$name" ] || return 0
338 local ip_addr="$IPBASE.0.$(($FIRSTIP + $ip_offset))"
340 if [ "$ctdb_node" = 1 ] ; then
341 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
342 sname="${CLUSTER}n${num_ctdb_nodes}"
343 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
346 hosts_line="$ip_addr ${name}.${ld} $name"
349 # This allows you to add a function to your configuration file
350 # to modify hostnames (and other aspects of nodes). This
351 # function can access/modify $name (the existing name),
352 # $node_type and $ctdb_node (1, if the node is a member of the
353 # CTDB cluster, 0 otherwise).
354 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
355 local old_name="$name"
356 $HOSTNAME_HACKING_FUNCTION
357 if [ "$name" != "$old_name" ] ; then
358 hosts_line="$ip_addr ${name}.${ld} $name"
364 hosts_file="tmp/hosts.$CLUSTER"
366 local num_ctdb_nodes=0
367 local ld=$(echo $DOMAIN | tr A-Z a-z)
368 echo "# autocluster $CLUSTER"
369 hack_all_nodes_with hosts_line_hack_name
373 # Build /etc/ctdb/nodes
376 [ "$ctdb_node" = 1 ] || return 0
377 echo "$IPBASE.0.$(($FIRSTIP + $ip_offset))"
378 num_nodes=$(($num_nodes + 1))
380 nodes_file="tmp/nodes.$CLUSTER"
382 hack_all_nodes_with ctdb_nodes_line >$nodes_file
383 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
386 uuid_map="tmp/uuid_map.$CLUSTER"
389 echo "${ip_offset} $(uuidgen) ${node_type}"
391 hack_all_nodes_with uuid_map_line >$uuid_map
394 create_cluster_hooks=
395 cluster_created_hooks=
401 sanity_check_cluster_name
403 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
405 # Run hooks before doing anything else.
406 run_hooks create_cluster_hooks
408 common_nodelist_hacking
410 for_each_node call_func create_node
412 echo "Cluster $CLUSTER created"
415 run_hooks cluster_created_hooks
418 cluster_created_hosts_message ()
420 echo "You may want to add this to your /etc/hosts file:"
424 register_hook cluster_created_hooks cluster_created_hosts_message
429 local single_node_ip_offset="$2"
431 sanity_check_cluster_name
433 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
435 common_nodelist_hacking
438 set -- $(IFS=: ; echo $n)
439 [ $single_node_ip_offset -eq $2 ] || continue
440 call_func create_node "$@"
442 echo "Requested node created"
444 echo "You may want to update your /etc/hosts file:"
451 ###############################
452 # test the proxy setup
454 export http_proxy=$WEBPROXY
455 wget -O /dev/null $INSTALL_SERVER || \
456 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
462 kickstart_floppy_create_hooks=
468 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
472 echo "Testing WEBPROXY $WEBPROXY"
475 echo "Creating the disk"
476 qemu-img create -f $BASE_FORMAT "$DISK" $DISKSIZE
479 mkdir -p mnt tmp tmp/ISO
483 echo "Creating kickstart file from template"
484 substitute_vars "$KICKSTART" "tmp/ks.cfg"
486 if [ $INSTALLKEY = "--skip" ]; then
488 --------------------------------------------------------------------------------------
489 WARNING: You have not entered an install key. Some RHEL packages will not be installed.
491 Please enter a valid RHEL install key in your config file like this:
493 INSTALLKEY="1234-5678-0123-4567"
495 The install will continue without an install key in 5 seconds
496 --------------------------------------------------------------------------------------
501 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
504 (*) ISO="${ISO_DIR}/${ISO}"
507 echo "Creating kickstart floppy"
508 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
509 mkdosfs tmp/floppy.img
510 mount -o loop -t msdos tmp/floppy.img mnt
512 mount -o loop,ro $ISO tmp/ISO
514 echo "Setting up bootloader"
515 cp tmp/ISO/isolinux/isolinux.bin tmp
516 cp tmp/ISO/isolinux/vmlinuz tmp
517 cp tmp/ISO/isolinux/initrd.img tmp
519 run_hooks kickstart_floppy_create_hooks
526 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
528 rm -f $KVMLOG/serial.$NAME
530 # boot the install CD
531 $VIRSH create tmp/$NAME.xml
533 echo "Waiting for install to start"
536 # wait for the install to finish
537 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
539 die "Failed to create base image $DISK"
547 Install finished, base image $DISK created
551 To ensure that this image does not change
553 Note that the root password has been set to $ROOTPASSWORD
558 ###############################
564 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
578 echo "Creating $NAME.xml"
579 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
581 # boot the base system
582 $VIRSH create tmp/$NAME.xml
585 ######################################################################
587 # various functions...
589 # Set some MAC address variables based on a hash of the cluster name
590 # plus the node number and each adapter number.
595 local md5=$(echo $cname | md5sum)
596 local nh=$(printf "%02x" $ip_offset)
597 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
599 MAC1="${mac_prefix}01"
600 MAC2="${mac_prefix}02"
601 MAC3="${mac_prefix}03"
602 MAC4="${mac_prefix}04"
603 MAC5="${mac_prefix}05"
604 MAC6="${mac_prefix}06"
607 # mount a qemu image via nbd
609 echo "Connecting nbd to $1"
612 killall -9 -q $QEMU_NBD || true
613 $QEMU_NBD -p 1300 $1 &
615 [ -r $NBD_DEVICE ] || {
616 mknod $NBD_DEVICE b 43 0
618 umount mnt 2> /dev/null || true
619 nbd-client -d $NBD_DEVICE > /dev/null 2>&1 || true
620 killall -9 -q nbd-client || true
621 nbd-client localhost 1300 $NBD_DEVICE > /dev/null 2>&1 || true &
627 echo "Disconnecting nbd"
629 nbd-client -d $NBD_DEVICE > /dev/null 2>&1 || true
630 killall -9 -q nbd-client || true
631 killall -q $QEMU_NBD || true
638 case "$SYSTEM_DISK_FORMAT" in
642 extra_mount_options=""
646 extra_mount_options=",loop"
649 die "Error: unknown SYSTEM_DISK_FORMAT=${SYSTEM_DISK_FORMAT}."
655 case "$SYSTEM_DISK_FORMAT" in
663 die "Error: unknown SYSTEM_DISK_FORMAT=${SYSTEM_DISK_FORMAT}."
667 # mount a qemu image via nbd
672 local device extra_mount_options
676 echo "Mounting disk ${disk}..."
678 for m in $SYSTEM_DISK_MOUNTS ; do
680 o="${m#*:}" # Offset is after colon
681 m="${m%:*}" # Mountpoint is before colon
682 echo " mount ${m} from offset ${o}"
684 for i in $(seq 1 5); do
685 mount -o offset=${o}${extra_mount_options} $device "mnt${m}" && {
689 umount mnt 2>/dev/null || true
692 [ $mount_ok = 1 ] || die "Failed to mount $disk"
696 echo "Mounted directory does not look like a root filesystem"
702 # unmount a qemu image
704 echo "Unmounting disk"
707 # umounts are done in reverse. Be lazy and construct a reverse
708 # list, since the shell will handle any strange whitespace for
712 for m in $SYSTEM_DISK_MOUNTS ; do
713 umounts="${m%:*}${umounts:+ }${umounts}"
715 for m in $umounts ; do
717 umount "mnt${m}" || umount "mnt${m}" || true
722 # setup the files from $BASE_TEMPLATES/, substituting any variables
723 # based on the config
724 copy_base_dir_substitute_templates ()
728 local d="$BASE_TEMPLATES/$dir"
729 [ -d "$d" ] || return 0
732 for f in $(cd "$d" && find . \! -name '*~') ; do
733 if [ -d "$d/$f" ]; then
736 substitute_vars "$d/$f" "mnt/$f"
738 chmod --reference="$d/$f" "mnt/$f"
744 setup_base_ssh_keys ()
746 # this is needed as git doesn't store file permissions other
748 chmod 600 mnt/etc/ssh/*key mnt/root/.ssh/*
749 chmod 700 mnt/etc/ssh mnt/root/.ssh mnt/root
750 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
751 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
752 cat "$HOME/.ssh/id_rsa.pub" >> mnt/root/.ssh/authorized_keys
754 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
755 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
756 cat "$HOME/.ssh/id_dsa.pub" >> mnt/root/.ssh/authorized_keys
760 register_hook setup_base_hooks setup_base_ssh_keys
762 setup_base_grub_conf ()
764 echo "Adjusting grub.conf"
765 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
766 sed -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
767 -e "s/ nodmraid//" -e "s/ nompath//" \
768 -e "s/quiet/noapic divider=10${o:+ }${o}/g" mnt/boot/grub/grub.conf -i.org
771 register_hook setup_base_hooks setup_base_grub_conf
778 echo "Copy base files"
779 copy_base_dir_substitute_templates "all"
780 if [ -n "$type" ] ; then
781 copy_base_dir_substitute_templates "$type"
784 run_hooks setup_base_hooks
787 # setup various networking components
790 # This avoids doing anything when we're called from boot_base().
791 if [ -z "$hosts_file" ] ; then
792 echo "Skipping network-related setup"
796 echo "Setting up networks"
797 cat $hosts_file >>mnt/etc/hosts
799 echo "Setting up /etc/ctdb/nodes"
800 mkdir -p mnt/etc/ctdb
801 cp $nodes_file mnt/etc/ctdb/nodes
803 [ "$WEBPROXY" = "" ] || {
804 echo "export http_proxy=$WEBPROXY" >> mnt/etc/bashrc
807 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
808 echo "Enabling nfs mount of $NFSSHARE"
809 mkdir -p "mnt$NFS_MOUNTPOINT"
810 echo "$NFSSHARE $NFS_MOUNTPOINT nfs intr" >> mnt/etc/fstab
813 mkdir -p mnt/etc/yum.repos.d
814 echo '@@@YUM_TEMPLATE@@@' | substitute_vars - > mnt/etc/yum.repos.d/autocluster.repo
817 register_hook setup_base_hooks setup_network
820 [ -z "$TIMEZONE" ] && {
821 [ -r /etc/timezone ] && {
822 TIMEZONE=`cat /etc/timezone`
824 [ -r /etc/sysconfig/clock ] && {
825 . /etc/sysconfig/clock
828 TIMEZONE="${TIMEZONE// /_}"
830 [ -n "$TIMEZONE" ] || \
831 die "Unable to determine TIMEZONE - please set in config"
834 # substite a set of variables of the form @@XX@@ for the shell
835 # variables $XX in a file.
837 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
838 # contain a filename whose contents are substituted, with variable
839 # substitution applied to those contents. If filename starts with '|'
840 # it is a command instead - however, quoting is extremely fragile.
842 infile="${1:-/dev/null}" # if empty then default to /dev/null
843 outfile="$2" # optional
845 instring=$(cat $infile)
847 # Handle any indirects by looping until nothing changes.
848 # However, only handle 10 levels of recursion.
851 outstring=$(_substitute_vars "$instring" "@@@")
852 [ $? -eq 0 ] || die "Failed to expand template $infile"
854 [ "$instring" = "$outstring" ] && break
856 count=$(($count + 1))
857 [ $count -lt 10 ] || \
858 die "Recursion too deep in $infile - only 10 levels allowed!"
860 instring="$outstring"
863 # Now regular variables.
864 outstring=$(_substitute_vars "$instring" "@@")
865 [ $? -eq 0 ] || die "Failed to expand template $infile"
867 if [ -n "$outfile" ] ; then
868 echo "$outstring" > "$outfile"
875 # Delimiter @@ means to substitute contents of variable.
876 # Delimiter @@@ means to substitute contents of file named by variable.
877 # @@@ supports leading '|' in variable value, which means to excute a
879 _substitute_vars() {(
883 # get the list of variables used in the template
884 VARS=`echo "$instring" |
885 tr -cs "A-Z0-9_$delimiter" '\012' |
887 sed -n -e "s#^${delimiter}\(.*\)${delimiter}\\$#\1#p"`
891 # variable variables are fun .....
894 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
898 if [ "$delimiter" = "@@@" ] ; then
900 c="${f#|}" # Is is a command, signified by a leading '|'?
901 if [ "$c" = "$f" ] ; then
902 # No leading '|', cat file.
906 die "Could not substitute contents of file $f"
909 # Leading '|', execute command.
910 # Quoting problems here - using eval "$c" doesn't help.
914 die "Could not execute command $c"
919 # escape some pesky chars
924 echo "s#${delimiter}${v}${delimiter}#${s}#g"
927 echo "$instring" | sed -f $tmp
933 which $1 > /dev/null || die "Please install $1 to continue"
936 # Set a variable if it isn't already set. This allows environment
937 # variables to override default config settings.
942 [ "${!v+x}" ] || eval "$v=\"$e\""
948 for i in "${installdir}/config.d/"*.defconf ; do
953 # Print the list of config variables defined in config.d/.
954 get_config_options () {( # sub-shell for local declaration of defconf()
956 defconf() { options="$options $1" ; }
961 # Produce a list of long options, suitable for use with getopt, that
962 # represent the config variables defined in config.d/.
963 getopt_config_options () {
964 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
968 # Unconditionally set the config variable associated with the given
974 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
975 # unset so defconf will set it
980 # Dump all of the current config variables.
983 for o in $(get_config_options) ; do
984 echo "${o}=\"${!o}\""
989 # $COLUMNS is set in interactive bash shells. It probably isn't set
990 # in this shell, so let's set it if it isn't.
991 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
995 # Print text assuming it starts after other text in $startcol and
996 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
997 # Long "words" will extend past $COLUMNS - 2.
1002 local width=$(($COLUMNS - 2 - $startcol))
1003 [ $width -lt 0 ] && width=$((78 - $startcol))
1008 if [ $startcol -gt 0 ] ; then
1009 padding=$(printf "\n%${startcol}s" " ")
1015 while [ -n "$text" ] ; do
1018 # If we already have output then arrange padding on the next line.
1019 [ -n "$out" ] && out="${out}${padding}"
1021 # Break the text at $width.
1022 out="${out}${text:0:${width}}"
1023 text="${text:${width}}"
1025 # If we have left over text then the line break may be ugly,
1026 # so let's check and try to break it on a space.
1027 if [ -n "$text" ] ; then
1028 # The 'x's stop us producing a special character like '(',
1029 # ')' or '!'. Yuck - there must be a better way.
1030 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1031 # We didn't break on a space. Arrange for the
1032 # beginning of the broken "word" to appear on the next
1033 # line but not if it will make us loop infinitely.
1034 if [ "${orig}" != "${out##* }${text}" ] ; then
1035 text="${out##* }${text}"
1038 # Hmmm, doing that would make us loop, so add the
1039 # rest of the word from the remainder of the text
1040 # to this line and let it extend past $COLUMNS - 2.
1041 out="${out}${text%% *}"
1042 if [ "${text# *}" != "$text" ] ; then
1043 # Remember the text after the next space for next time.
1046 # No text after next space.
1051 # We broke on a space. If it will be at the beginning
1052 # of the next line then remove it.
1061 # Display usage text, trying these approaches in order.
1062 # 1. See if it all fits on one line before $COLUMNS - 2.
1063 # 2. See if splitting before the default value and indenting it
1064 # to $startcol means that nothing passes $COLUMNS - 2.
1065 # 3. Treat the message and default value as a string and just us fill_text()
1067 usage_display_text () {
1072 local width=$(($COLUMNS - 2 - $startcol))
1073 [ $width -lt 0 ] && width=$((78 - $startcol))
1075 default="(default \"$default\")"
1077 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1078 echo "${desc} ${default}"
1080 local padding=$(printf "%${startcol}s" " ")
1082 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1084 echo "${padding}${default}"
1086 fill_text $startcol "${desc} ${default}"
1091 # Display usage information for long config options.
1092 usage_smart_display () {( # sub-shell for local declaration of defconf()
1096 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1098 printf " --%-25s " "${longopt}=${3}"
1100 usage_display_text $startcol "$4" "$2"
1107 # Display usage information for long config options.
1108 usage_config_options (){
1109 usage_smart_display load_config
1113 local releases=$(cd $installdir/releases && echo *.release)
1114 releases="${releases//.release}"
1115 releases="${releases// /\", \"}"
1116 echo "\"$releases\""
1121 shift # subsequent args are passed to release file
1123 # This simply loads an extra config file from $installdir/releases
1124 f="${installdir}/releases/${release}.release"
1125 if [ -r "$f" ] ; then
1128 f="${installdir}/releases/${release%%-*}.release"
1129 if [ -r "$f" ] ; then
1130 . "$f" "${release#*-}"
1132 echo "Unknown release \"${release}\" specified to --with-release"
1133 printf "%-25s" "Supported releases are: "
1134 fill_text 25 "$(list_releases)"
1141 has_public_addresses_DEFAULT ()
1146 # Build public address configuration.
1147 # * 1st public IP: unless specified, last octet is $FIRSTIP + $PUBLIC_IP_OFFSET
1148 # * Excluded nodes: unless specified via comma-separated list of IP offsets,
1149 # nodes are excluded via their node types
1150 # * Number of public addresses per interface is either specified or $NUMNODES.
1151 make_public_addresses () {
1152 local firstip="${1:-$(($FIRSTIP + $PUBLIC_IP_OFFSET))}"
1153 local excluded_nodes="$2"
1154 local num_addrs="${3:-${NUMNODES}}"
1156 # For delimiting matches.
1157 excluded_nodes="${excluded_nodes:+,}${excluded_nodes}${excluded_nodes:+,}"
1159 excluded_nodes="${excluded_nodes// /}"
1161 make_public_addresses_for_node ()
1163 [ "$ctdb_node" = 1 ] || return 0
1165 echo "[/etc/ctdb/public_addresses:${name}.${DOMAIN}]"
1167 if [ -n "$excluded_nodes" -a \
1168 "${excluded_nodes/,${ip_offset},}" = "$excluded_nodes" ] ||
1169 ([ -z "$excluded_nodes" ] &&
1170 call_func has_public_addresses "$node_type") ; then
1173 for e in "1" "2" ; do
1174 for i in $(seq $firstip $(($firstip + $num_addrs - 1))) ; do
1175 if [ $i -gt 254 ] ; then
1176 die "make_public_addresses: octet > 254 - consider setting PUBLIC_IP_OFFSET"
1178 printf "\t${IPBASE}.${e}.${i}/24 eth${e}\n"
1184 hack_all_nodes_with make_public_addresses_for_node
1187 ######################################################################
1191 ############################
1192 # parse command line options
1193 long_opts=$(getopt_config_options)
1194 getopt_output=$(getopt -n autocluster -o "c:e:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
1195 [ $? != 0 ] && usage
1197 use_default_config=true
1199 # We do 2 passes of the options. The first time we just handle usage
1200 # and check whether -c is being used.
1201 eval set -- "$getopt_output"
1204 -c) shift 2 ; use_default_config=false ;;
1206 --) shift ; break ;;
1207 --with-release) shift 2 ;; # Don't set use_default_config=false!!!
1209 -h|--help) usage ;; # Usage should be shown here for real defaults.
1210 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1211 *) usage ;; # shouldn't happen, so this is reasonable.
1216 $use_default_config && [ -r "$config" ] && . "$config"
1218 eval set -- "$getopt_output"
1222 # force at least ./local_file to avoid accidental file from $PATH
1223 -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
1224 -e) eval "$2" ; exit ;;
1229 -x) set -x; shift ;;
1230 --dump) dump_config ;;
1231 --) shift ; break ;;
1232 -h|--help) usage ;; # Redundant.
1234 # Putting --opt1|opt2|... into a variable and having case
1235 # match against it as a pattern doesn't work. The | is
1236 # part of shell syntax, so we need to do this. Look away
1237 # now to stop your eyes from bleeding! :-)
1238 x=",${long_opts}" # Now each option is surrounded by , and :
1239 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1240 # Our option, $1, surrounded by , and : was in $x, so is legal.
1241 setconf_longopt "$1" "$2"; shift 2
1246 *) usage ;; # shouldn't happen, so this is reasonable.
1254 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1257 # check for needed programs
1258 check_command expect
1259 if [ "$SYSTEM_DISK_FORMAT" != "raw" ] ; then
1260 check_command $QEMU_NBD
1261 check_command nbd-client
1264 [ $# -lt 1 ] && usage
1275 [ $# != 0 ] && usage
1279 [ $# != 1 ] && usage
1283 [ $# != 2 ] && usage
1284 create_one_node "$1" "$2"
1292 [ $# != 1 ] && usage
1296 [ $# != 0 ] && usage