]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - testing/scripts/function.sh
c512b8add66f5872ab9f4001be1c34dea6ecfec7
2 # provides some general-purpose script functions
4 # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
5 # Zuercher Hochschule Winterthur
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>.
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
20 YELLOW
=$
(tput setaf
3)
23 # exit with given error message
26 echo -e "${RED}$1${NORMAL}"
31 # $1 - command to execute
32 # $2 - whether or not to log command exit status
33 # (0 -> disable exit status logging)
37 echo $cmd >>$LOGFILE 2>&1
40 [ "$2" != 0 ] && log_status
$status
41 if [ $status != 0 ]; then
43 echo "! Command $cmd failed, exiting (status $status)"
44 echo "! Check why here $LOGFILE"
49 # execute command in chroot
50 # $1 - command to execute
53 execute
"chroot $LOOPDIR env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $@"
56 # write green status message to console
60 echo -e "${GREEN}$1${NORMAL}"
63 # write red status message to console
67 echo -e "${RED}$1${NORMAL}"
70 # write yellow status message to console
74 echo -e "${YELLOW}$1${NORMAL}"
78 # $1 - current action description
81 /bin
/echo -n "[....] $1 "
84 # log an action status
85 # $1 - exit status of action
90 /bin
/echo -ne "[${GREEN} ok ${NORMAL}"
92 /bin
/echo -ne "[${RED}FAIL${NORMAL}"
97 # the following two functions are stolen from [1]
98 # [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
100 declare -a on_exit_items
102 # perform registered actions on exit
105 for ((onex
=${#on_exit_items[@]}-1; onex
>=0; onex--
))
107 echo "On_Exit: ${on_exit_items[$onex]}" >>$LOGFILE
108 ${on_exit_items[$onex]} >>$LOGFILE 2>&1
114 # register a command to execute when the calling script terminates. The
115 # registered commands are called in FILO order.
116 # $* - command to register
119 local n
=${#on_exit_items[*]}
120 on_exit_items
[$n]="$*"
121 if [ $n -eq 0 ]; then
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
132 [ ! $secs ] && secs
=5
135 for st
in `seq 1 $steps`
137 umount
$1 >>$LOGFILE 2>&1
138 mount |
grep $1 >/dev
/null
2>&1
139 [ $?
-eq 0 ] ||
return 0
146 # load qemu NBD kernel module, if not already loaded
149 lsmod |
grep ^nbd
[[:space
:]]* >/dev
/null
2>&1
152 log_action
"Loading NBD kernel module"
153 execute
"modprobe nbd max_part=16"
157 # check if given commands exist in $PATH
158 # $* - commands to check
163 command -v $i >/dev
/null ||
{ die
"Required command $i not found"; exit 1; }
167 # check if any of the given virtual guests are running
168 # $* - names of guests to check
171 command -v virsh
>/dev
/null ||
return 1
174 virsh list
--name 2>/dev
/null |
grep "^$host$" >/dev
/null
&& return 0
179 #############################################
180 # search and replace strings throughout a
184 function searchandreplace
{
190 [ -d "$DESTDIR" ] || die
"$DESTDIR is not a directory!"
193 ###########################################
194 # search and replace in each found file the
198 for eachfoundfile
in `find $DESTDIR -type f`
200 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"