#!/bin/bash set -e PROGNAME=$(basename $0) # $HeadURL: http://sos16-vip/svn/kickstart/trunk/bin/defaults-dumper $ $LastChangedRevision: 669 $ # Load libraries . $(dirname $0)/../include/mkdi.sh main() { local TARGET_HOSTNAME TARGET_IPADDR TARGET_RELEASE # Defaults for options VERBOSELEVEL=2 # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-release=*) TARGET_RELEASE=${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 [ $# -ge 1 ] || usage MODE=$1; shift debug 10 "main: MODE=$MODE" # Sanity checks and derivations # Guts case $MODE in create) create --target-hostname=$TARGET_HOSTNAME --target-ipaddr=$TARGET_IPADDR --target-release=$TARGET_RELEASE "$@" || return $? ;; delete) delete --target-hostname=$TARGET_HOSTNAME --target-ipaddr=$TARGET_IPADDR --target-release=$TARGET_RELEASE "$@" || return $? ;; list) list "$@" || return $? ;; purge) purge "$@" || return $? ;; *) usage ;; esac return 0 } create() { local TARGET_HOSTNAME TARGET_IPADDR TARGET_RELEASE # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-release=*) TARGET_RELEASE=${1#*=} ;; # General options --) shift; break ;; -*) internal "create: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 0 ] || internal "create: $#: wrong arg count" # Create entry info "creating preseed file ..." create_or_delete --target-hostname=$TARGET_HOSTNAME --target-ipaddr=$TARGET_IPADDR --target-release=$TARGET_RELEASE create || return $? return 0 } delete() { local TARGET_HOSTNAME TARGET_IPADDR TARGET_RELEASE # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-release=*) TARGET_RELEASE=${1#*=} ;; # General options --) shift; break ;; -*) internal "create: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 0 ] || internal "delete: $#: wrong arg count" # Delete entry info "deleting preseed file ..." create_or_delete --target-hostname=$TARGET_HOSTNAME --target-ipaddr=$TARGET_IPADDR --target-release=$TARGET_RELEASE delete || return $? return 0 } create_or_delete() { local TARGET_HOSTNAME TARGET_IPADDR TARGET_RELEASE local MODE LINE DHCPD_OUTPUT PAACRT_PROT PAACRT_HOST PAACRT_PATH # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-release=*) TARGET_RELEASE=${1#*=} ;; # General options --) shift; break ;; -*) internal "create_or_delete: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 1 ] || internal "create_or_delete: $#: wrong arg count" MODE=$1 # Sanity checks and derivations validate_vars MODE TARGET_HOSTNAME TARGET_IPADDR TARGET_RELEASE || return $? debug 10 "main: MODE=$MODE, TARGET_HOSTNAME=$TARGET_HOSTNAME, TARGET_IPADDR=$TARGET_IPADDR, TARGET_RELEASE=$TARGET_RELEASE" [[ $PAACRT_URL =~ (http|file)://([^/]*)(/.*) ]] || { error "$PAACRT_URL: invalid PAACRT URL"; return 1; } PAACRT_PROT=${BASH_REMATCH[1]} PAACRT_HOST=${BASH_REMATCH[2]} PAACRT_PATH=${BASH_REMATCH[3]} debug 10 "create_or_delete: PAACRT_PROT=$PAACRT_PROT, PAACRT_HOST=$PAACRT_HOST, PAACRT_PATH=$PAACRT_PATH" [ $PAACRT_PROT = http ] || { error "$PAACRT_URL: only http is currently supported"; return 1; } # Guts mkdir -p $PRESEEDSERVER_DIR if [ $MODE = create ]; then debug 10 "create: creating file ..." { echo "d-i mirror/country string manual" echo "d-i mirror/http/hostname string $PAACRT_HOST" echo "d-i mirror/http/directory string ${PAACRT_PATH%/}/debian-$TARGET_RELEASE.$TARGET_HOSTNAME/" #echo "d-i mirror/suite string $TARGET_RELEASE" echo "d-i mirror/http/proxy string" #echo "d-i clock-setup/utc boolean true" echo "d-i time/zone string US/Eastern" echo "d-i partman-auto/method string regular" echo "d-i partman-auto/disk string /dev/hda" echo "d-i partman-auto/choose_recipe select atomic" # We only got asked two questions; maybe one of these answers is superfluous? echo "d-i partman/confirm_write_new_label boolean true" echo "d-i partman/choose_partition select finish" echo "d-i partman/confirm boolean true" # Note single quotes to avoid a lot of escaping of '$'. echo "d-i passwd/root-password-crypted password $ROOT_PW" # Don't create any normal users echo "d-i passwd/make-user boolean false" echo "popularity-contest popularity-contest/participate boolean false" echo "tasksel tasksel/first multiselect" echo "d-i grub-installer/only_debian boolean true" #echo "d-i preseed/late_command string pwd > /target/NEW_FILE_IN_ROOT" echo "d-i preseed/late_command string in-target sh -c 'wget http://$SCRIPTSERVER_IPADDR${SCRIPTSERVER_DIR#$SCRIPTSERVER_DOCROOT}/$TARGET_HOSTNAME.sh && sh $TARGET_HOSTNAME.sh'" echo "d-i finish-install/reboot_in_progress note" } > $PRESEEDSERVER_DIR/$TARGET_HOSTNAME.cfg elif [ $MODE = delete ]; then debug 10 "create_or_delete: deleting file ..." RM_OUTPUT=$(rm $PRESEEDSERVER_DIR/$TARGET_HOSTNAME.cfg 2>&1) || warning "rm: failed (output was: $RM_OUTPUT)" fi return 0 } purge() { # Guts rm $PRESEEDSERVER_DIR/*.cfg 2>/dev/null || true return 0 } list() { # Process options while [ "X$1" != X ]; do case $1 in # Application specific options # General options --) shift; break ;; -*) internal "list: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 0 ] || internal "list: $#: wrong arg count" # Guts ls -l $PRESEEDSERVER_DIR/*.cfg 2>/dev/null return 0 } usage() { local RC RC=${1:-1} { echo "Usage: $PROGNAME [ ] { create | delete | list }" echo } | if [ $RC = 0 ]; then cat else cat >&2 fi exit $RC } main "$@"