Annotation of embedaddon/strongswan/testing/scripts/function.sh, revision 1.1
1.1 ! misho 1: #!/bin/bash
! 2: # provides some general-purpose script functions
! 3: #
! 4: # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
! 5: # Zuercher Hochschule Winterthur
! 6: #
! 7: # This program is free software; you can redistribute it and/or modify it
! 8: # under the terms of the GNU General Public License as published by the
! 9: # Free Software Foundation; either version 2 of the License, or (at your
! 10: # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
! 11: #
! 12: # This program is distributed in the hope that it will be useful, but
! 13: # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 14: # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
! 15: # for more details.
! 16:
! 17: export TERM=xterm
! 18: RED=$(tput setaf 1)
! 19: GREEN=$(tput setaf 2)
! 20: YELLOW=$(tput setaf 3)
! 21: NORMAL=$(tput op)
! 22:
! 23: # exit with given error message
! 24: # $1 - error message
! 25: die() {
! 26: echo -e "${RED}$1${NORMAL}"
! 27: exit 1
! 28: }
! 29:
! 30: # execute command
! 31: # $1 - command to execute
! 32: # $2 - whether or not to log command exit status
! 33: # (0 -> disable exit status logging)
! 34: execute()
! 35: {
! 36: cmd=${1}
! 37: echo $cmd >>$LOGFILE 2>&1
! 38: $cmd >>$LOGFILE 2>&1
! 39: status=$?
! 40: [ "$2" != 0 ] && log_status $status
! 41: if [ $status != 0 ]; then
! 42: echo
! 43: echo "! Command $cmd failed, exiting (status $status)"
! 44: echo "! Check why here $LOGFILE"
! 45: exit 1
! 46: fi
! 47: }
! 48:
! 49: # execute command in chroot
! 50: # $1 - command to execute
! 51: execute_chroot()
! 52: {
! 53: execute "chroot $LOOPDIR env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $@"
! 54: }
! 55:
! 56: # write green status message to console
! 57: # $1 - msg
! 58: echo_ok()
! 59: {
! 60: echo -e "${GREEN}$1${NORMAL}"
! 61: }
! 62:
! 63: # write red status message to console
! 64: # $1 - msg
! 65: echo_failed()
! 66: {
! 67: echo -e "${RED}$1${NORMAL}"
! 68: }
! 69:
! 70: # write yellow status message to console
! 71: # $1 - msg
! 72: echo_warn()
! 73: {
! 74: echo -e "${YELLOW}$1${NORMAL}"
! 75: }
! 76:
! 77: # log an action
! 78: # $1 - current action description
! 79: log_action()
! 80: {
! 81: /bin/echo -n "[....] $1 "
! 82: }
! 83:
! 84: # log an action status
! 85: # $1 - exit status of action
! 86: log_status()
! 87: {
! 88: tput hpa 0
! 89: if [ $1 -eq 0 ]; then
! 90: /bin/echo -ne "[${GREEN} ok ${NORMAL}"
! 91: else
! 92: /bin/echo -ne "[${RED}FAIL${NORMAL}"
! 93: fi
! 94: echo
! 95: }
! 96:
! 97: # the following two functions are stolen from [1]
! 98: # [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
! 99:
! 100: declare -a on_exit_items
! 101:
! 102: # perform registered actions on exit
! 103: on_exit()
! 104: {
! 105: for ((onex=${#on_exit_items[@]}-1; onex>=0; onex--))
! 106: do
! 107: echo "On_Exit: ${on_exit_items[$onex]}" >>$LOGFILE
! 108: ${on_exit_items[$onex]} >>$LOGFILE 2>&1
! 109: done
! 110: on_exit_items=""
! 111: trap - EXIT
! 112: }
! 113:
! 114: # register a command to execute when the calling script terminates. The
! 115: # registered commands are called in FILO order.
! 116: # $* - command to register
! 117: do_on_exit()
! 118: {
! 119: local n=${#on_exit_items[*]}
! 120: on_exit_items[$n]="$*"
! 121: if [ $n -eq 0 ]; then
! 122: trap on_exit EXIT
! 123: fi
! 124: }
! 125:
! 126: # wait for a mount to disappear
! 127: # $1 - device/image to wait for
! 128: # $2 - maximum time to wait in seconds, default is 5 seconds
! 129: graceful_umount()
! 130: {
! 131: secs=$2
! 132: [ ! $secs ] && secs=5
! 133:
! 134: let steps=$secs*100
! 135: for st in `seq 1 $steps`
! 136: do
! 137: umount $1 >>$LOGFILE 2>&1
! 138: mount | grep $1 >/dev/null 2>&1
! 139: [ $? -eq 0 ] || return 0
! 140: sleep 0.01
! 141: done
! 142:
! 143: return 1
! 144: }
! 145:
! 146: # load qemu NBD kernel module, if not already loaded
! 147: load_qemu_nbd()
! 148: {
! 149: lsmod | grep ^nbd[[:space:]]* >/dev/null 2>&1
! 150: if [ $? != 0 ]
! 151: then
! 152: log_action "Loading NBD kernel module"
! 153: execute "modprobe nbd max_part=16"
! 154: fi
! 155: }
! 156:
! 157: # check if given commands exist in $PATH
! 158: # $* - commands to check
! 159: check_commands()
! 160: {
! 161: for i in $*
! 162: do
! 163: command -v $i >/dev/null || { die "Required command $i not found"; exit 1; }
! 164: done
! 165: }
! 166:
! 167: # check if any of the given virtual guests are running
! 168: # $* - names of guests to check
! 169: running_any()
! 170: {
! 171: command -v virsh >/dev/null || return 1
! 172: for host in $*
! 173: do
! 174: virsh list --name 2>/dev/null | grep "^$host$" >/dev/null && return 0
! 175: done
! 176: return 1
! 177: }
! 178:
! 179: #############################################
! 180: # search and replace strings throughout a
! 181: # whole directory
! 182: #
! 183:
! 184: function searchandreplace {
! 185:
! 186: SEARCHSTRING="$1"
! 187: REPLACESTRING="$2"
! 188: DESTDIR="$3"
! 189:
! 190: [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
! 191:
! 192:
! 193: ###########################################
! 194: # search and replace in each found file the
! 195: # given string
! 196: #
! 197:
! 198: for eachfoundfile in `find $DESTDIR -type f`
! 199: do
! 200: sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
! 201: done
! 202:
! 203: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>