#!/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 script ..." 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 script ..." 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 # 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" # Guts mkdir -p $SCRIPTSERVER_DIR if [ $MODE = create ]; then debug 10 "create: creating script ..." gen_script > $SCRIPTSERVER_DIR/$TARGET_HOSTNAME.sh elif [ $MODE = delete ]; then debug 10 "create_or_delete: deleting script ..." RM_OUTPUT=$(rm $SCRIPTSERVER_DIR/$TARGET_HOSTNAME.sh 2>&1) || warning "rm: failed (output was: $RM_OUTPUT)" fi return 0 } purge() { # Guts rm $SCRIPTSERVER_DIR/*.sh 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 $SCRIPTSERVER_DIR/*.sh 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 } gen_script() { # Prologue echo '#!/bin/sh' echo 'set -e' echo 'PROGNAME=$(basename $0)' echo echo '(' # Create MDI script. We could echo 'wget ...' but then it requires the # install client to know where it is. We can just wget it here and embed it # in a 'here' document. echo "cat > /root/mdi <<'D_I_EOF'" wget --quiet -O - http://www.pasta.net/svn/mdi/trunk/bin/mdi echo "D_I_EOF" echo "chmod 755 /root/mdi" echo # Tell the install server we've finished. echo "echo $TARGET_HOSTNAME | nc $RELEASESERVER_IPADDR $RELEASESERVER_PORT" echo # Epilogue echo ') 2>&1 | tee -a /root/postinstall.log' } main "$@"