#!/bin/bash set -e PROGNAME=$(basename $0) # $HeadURL: http://sos16-vip/svn/kickstart/trunk/bin/defaults-dumper $ $LastChangedRevision: 669 $ # Load common functions . $(dirname $0)/../include/mkdi.sh main() { local TARGET_HOSTNAME # Defaults for options VERBOSELEVEL=2 # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; # General options --help|-h) usage 0 ;; --verbose|-v) VERBOSELEVEL=3 ;; -d) VERBOSELEVEL=$2; shift ;; --debug=*) VERBOSELEVEL=${1#*=} ;; --) shift; break ;; -*) usage ;; *) break ;; esac shift done # Process arguments [ $# = 1 ] || usage MODE=$1; shift debug 10 "main: MODE=$MODE" # Sanity checks and derivations # Delegate case $MODE in edit) edit --target-hostname=$TARGET_HOSTNAME "$@" || return $? ;; *) usage ;; esac return 0 } edit() { local TARGET_HOSTNAME ISCSISERVER_HOSTNAME ISCSISERVER_VOLUME_GROUP TARGET_HVM_FLAG TARGET_IPADDR TARGET_MACADDR TARGET_RELEASE TARGET_PROFILE TARGET_RAM TARGET_ROOT_DISK_SIZE TARGET_VM_FLAG VMSERVER_HOSTNAME RELEASE_CMDLINE local OLD_TARGET_HOSTNAME OLD_ISCSISERVER_HOSTNAME OLD_ISCSISERVER_VOLUME_GROUP OLD_TARGET_HVM_FLAG OLD_TARGET_IPADDR OLD_TARGET_MACADDR OLD_TARGET_RELEASE OLD_TARGET_PROFILE OLD_TARGET_RAM OLD_TARGET_ROOT_DISK_SIZE OLD_TARGET_VM_FLAG OLD_VMSERVER_HOSTNAME OLD_RELEASE_CMDLINE local DB_VAR DB_VAR2 LC_DB_VAR RESPONSE DFLT_RESPONSE ALL_SET_FLAG # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; # General options --) shift; break ;; -*) internal "edit: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 0 ] || internal "edit: $#: invalid argument count" # Guts # Signal processing and locking trap sighandler_generic 1 2 15 debug 10 "edit: TARGET_HOSTNAME=$TARGET_HOSTNAME" # Acquire all kickstart-editor options by questioning the user. for DB_VAR in $DB_VARS; do debug 10 "edit: processing $DB_VAR ..." LC_DB_VAR=`echo $DB_VAR | tr '[A-Z]' '[a-z]'` for PREFIX in validate rationalise help; do type -t ${PREFIX}_$LC_DB_VAR > /dev/null || internal "${PREFIX}_$LC_DB_VAR(): not defined" done # Set prompt case $DB_VAR in ISCSISERVER_HOSTNAME) PROMPT="iSCSI server" ;; TARGET_HVM_FLAG) PROMPT="target is fully virtualised VM" ;; TARGET_VM_FLAG) PROMPT="target is VM" ;; TARGET_IPADDR) PROMPT="target IP address" ;; TARGET_MACADDR) PROMPT="target MAC address" ;; TARGET_RELEASE) PROMPT="target OS" ;; TARGET_RAM) PROMPT="target RAM" ;; ISCSISERVER_VOLUME_GROUP) PROMPT="VG on iSCSI server" ;; *) PROMPT=`echo $DB_VAR | tr '[A-Z]' '[a-z]' | sed 's/_/ /g'` esac # Force values (these only have meaning in the in_progress table so # we force them to be expanded at the time when that info is written). case $DB_VAR in RELEASE_CMDLINE) FORCED_VALUE=auto ;; MAILTO) FORCED_VALUE=auto ;; *) FORCED_VALUE= ;; esac [ "X$FORCED_VALUE" = X ] || eval "$DB_VAR=\"$FORCED_VALUE\"" # Absolute default value case $DB_VAR in TARGET_HOSTNAME) DFLT_VALUE= ;; TARGET_PROFILE) DFLT_VALUE= ;; *) DFLT_VALUE=auto ;; esac debug 10 "edit: DB_VAR=$DB_VAR, LC_DB_VAR=$LC_DB_VAR, PROMPT=\"$PROMPT\", FORCED_VALUE=$FORCED_VALUE, DFLT_VALUE=$DFLT_VALUE" # Keep asking till we get a sensible answer. while :; do eval "CURRENT_VALUE=\"\$$DB_VAR\"" eval "OLD_VALUE=\"\$OLD_$DB_VAR\"" # This only gets populated once load_defaults runs below # Get existing or prompted value into response variable (from where it will shortly be validated) if [ "X$CURRENT_VALUE" != X ]; then RESPONSE="$CURRENT_VALUE" else # Work out a sensible default to provide { [ "X$OLD_VALUE" != X ] && DFLT_RESPONSE=$OLD_VALUE; } || DFLT_RESPONSE=$DFLT_VALUE # Prompt user echo -n "$PROMPT [$DFLT_RESPONSE]: " >&2 # Read response read RESPONSE < /dev/tty # Process response case $RESPONSE in "") RESPONSE=$DFLT_RESPONSE ;; .) RESPONSE= ;; \?|h|help) help_$LC_DB_VAR; continue ;; esac fi # Validate response debug 10 "edit: validating $RESPONSE ..." ! validate_$LC_DB_VAR "$RESPONSE" || { RESPONSE=`rationalise_$LC_DB_VAR $RESPONSE` || internal "edit: rationaliser failed!" debug 10 "edit: after rationalising value is $RESPONSE" eval "$DB_VAR=\"$RESPONSE\"" break } # If validation failed then say so and loop round to ask again, but # blank the existing value so that the code above is not led into not # prompting for a new value. echo "invalid $PROMPT" >&2 eval "$DB_VAR=" done # If the variable for which we just acquired the value is the hostname # then we can pull a lot of host-specific info out of the database # which can be used as defaults to later questions. (The '--force=true' is there # to allow for the fact that there may not be a row in the datbase presently.) [ $DB_VAR != TARGET_HOSTNAME ] || load_parameters --force=true --prefix=OLD_ || return $? # Once all values have been collected, break out of value acquisition loop. ALL_SET_FLAG=true for DB_VAR2 in $DB_VARS; do eval "debug 5 \"edit: DB_VAR2=$DB_VAR2, $DB_VAR2=\$$DB_VAR2\"" eval "[ \"X\$$DB_VAR2\" != X ]" || { ALL_SET_FLAG=false; break; } done ! $ALL_SET_FLAG || break done # Save values. debug 10 "edit: TARGET_HVM_FLAG=$TARGET_HVM_FLAG" save_parameters || return $? return 0 } usage() { local RC=${1:-1} { echo "Usage: $PROGNAME [ ] edit" echo # Standard options echo "Options: -h | --help show this help" echo " -v | --verbose be verbose" echo " -d | --debug= be very verbose" # Application-specific options echo " --target-hostname=" echo } | if [ $RC = 0 ]; then cat else cat >&2 fi exit $RC } sighandler_generic() { echo >&2 warning "cleaning up; please wait ..." # exitdel exit 2 } help_target_root_disk_size() { help_prologue printf "$HELP_FORMAT_STRING" "" "root disk size (GB)" help_epilogue } help_release_cmdline() { internal "help_release_cmdline: this function should never be called" } help_mailto() { internal "help_mailto: this function should never be called" } help_target_ram() { help_prologue printf "$HELP_FORMAT_STRING" "" "RAM (MB)" help_epilogue } help_target_profile() { help_prologue printf "$HELP_FORMAT_STRING" "" "configuration profile" help_epilogue } help_target_release() { help_prologue printf "$HELP_FORMAT_STRING" "lenny" "Debian 5.0.4 (aka 'lenny')" printf "$HELP_FORMAT_STRING" "squeeze" "Debian 6 (aka 'squeeze')" help_epilogue } help_target_hostname() { help_prologue printf "$HELP_FORMAT_STRING" "" "name of the host to install" help_epilogue } help_target_vm_flag() { help_prologue printf "$HELP_FORMAT_STRING" "" "target is a VM" help_epilogue } help_iscsiserver_volume_group() { help_prologue printf "$HELP_FORMAT_STRING" "" "VG on iSCSI server in which to create LV" help_epilogue } help_target_hvm_flag() { help_prologue printf "$HELP_FORMAT_STRING" "" "target is fully virtualised" help_epilogue } help_iscsiserver_hostname() { help_prologue printf "$HELP_FORMAT_STRING" "" "name of the iSCSI server" help_epilogue } help_target_macaddr() { help_prologue printf "$HELP_FORMAT_STRING" "" "MAC address in \"AA:BB:CC:DD:EE:FF\" format" printf "$HELP_FORMAT_STRING" "auto" "MAC address is assigned automatically (only for VMs)" help_epilogue } help_target_ipaddr() { help_prologue printf "$HELP_FORMAT_STRING" "" "IP address" printf "$HELP_FORMAT_STRING" "auto" "IP address is looked up in DNS" help_epilogue } help_vmserver_hostname() { help_prologue printf "$HELP_FORMAT_STRING" "" "name of VM server that run this VM" help_epilogue } help_prologue() { echo printf "$HELP_FORMAT_STRING" "" "accept the default value" printf "$HELP_FORMAT_STRING" "h or ?" "display this help" printf "$HELP_FORMAT_STRING" "." "clear the default value" } help_epilogue() { echo } help_generic() { local ITEM help_prologue for ITEM in "$@"; do printf "$HELP_FORMAT_STRING" "$ITEM" "$ITEM" done help_epilogue } # Entry point main "$@"