testing: Ensure no guest is running when modifying images
[strongswan.git] / testing / scripts / function.sh
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 NORMAL=$(tput op)
21
22 # exit with given error message
23 # $1 - error message
24 die() {
25 echo -e "${RED}$1${NORMAL}"
26 exit 1
27 }
28
29 # execute command
30 # $1 - command to execute
31 # $2 - whether or not to log command exit status
32 # (0 -> disable exit status logging)
33 execute()
34 {
35 cmd=${1}
36 echo $cmd >>$LOGFILE 2>&1
37 $cmd >>$LOGFILE 2>&1
38 status=$?
39 [ "$2" != 0 ] && log_status $status
40 if [ $status != 0 ]; then
41 echo
42 echo "! Command $cmd failed, exiting (status $status)"
43 echo "! Check why here $LOGFILE"
44 exit 1
45 fi
46 }
47
48 # execute command in chroot
49 # $1 - command to execute
50 execute_chroot()
51 {
52 execute "chroot $LOOPDIR $@"
53 }
54
55 # write green status message to console
56 # $1 - msg
57 echo_ok()
58 {
59 echo -e "${GREEN}$1${NORMAL}"
60 }
61
62 # write red status message to console
63 # $1 - msg
64 echo_failed()
65 {
66 echo -e "${RED}$1${NORMAL}"
67 }
68
69 # log an action
70 # $1 - current action description
71 log_action()
72 {
73 /bin/echo -n "[....] $1 "
74 }
75
76 # log an action status
77 # $1 - exit status of action
78 log_status()
79 {
80 tput hpa 0
81 if [ $1 -eq 0 ]; then
82 /bin/echo -ne "[${GREEN} ok ${NORMAL}"
83 else
84 /bin/echo -ne "[${RED}FAIL${NORMAL}"
85 fi
86 echo
87 }
88
89 # the following two functions are stolen from [1]
90 # [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
91
92 declare -a on_exit_items
93
94 # perform registered actions on exit
95 on_exit()
96 {
97 for ((onex=${#on_exit_items[@]}-1; onex>=0; onex--))
98 do
99 echo "On_Exit: ${on_exit_items[$onex]}" >>$LOGFILE
100 ${on_exit_items[$onex]} >>$LOGFILE 2>&1
101 done
102 on_exit_items=""
103 trap - EXIT
104 }
105
106 # register a command to execute when the calling script terminates. The
107 # registered commands are called in FILO order.
108 # $* - command to register
109 do_on_exit()
110 {
111 local n=${#on_exit_items[*]}
112 on_exit_items[$n]="$*"
113 if [ $n -eq 0 ]; then
114 trap on_exit EXIT
115 fi
116 }
117
118 # wait for a mount to disappear
119 # $1 - device/image to wait for
120 # $2 - maximum time to wait in seconds, default is 5 seconds
121 graceful_umount()
122 {
123 secs=$2
124 [ ! $secs ] && secs=5
125
126 let steps=$secs*100
127 for st in `seq 1 $steps`
128 do
129 umount $1 >>$LOGFILE 2>&1
130 mount | grep $1 >/dev/null 2>&1
131 [ $? -eq 0 ] || return 0
132 sleep 0.01
133 done
134
135 return 1
136 }
137
138 # load qemu NBD kernel module, if not already loaded
139 load_qemu_nbd()
140 {
141 lsmod | grep ^nbd[[:space:]]* >/dev/null 2>&1
142 if [ $? != 0 ]
143 then
144 log_action "Loading NBD kernel module"
145 execute "modprobe nbd max_part=16"
146 fi
147 }
148
149 # check if given commands exist in $PATH
150 # $* - commands to check
151 check_commands()
152 {
153 for i in $*
154 do
155 command -v $i >/dev/null || { die "Required command $i not found"; exit 1; }
156 done
157 }
158
159 # check if any of the given virtual guests are running
160 # $* - names of guests to check
161 running_any()
162 {
163 command -v virsh >/dev/null || return 1
164 for host in $*
165 do
166 virsh list --name | grep "^$host$" >/dev/null && return 0
167 done
168 return 1
169 }
170
171 #############################################
172 # search and replace strings throughout a
173 # whole directory
174 #
175
176 function searchandreplace {
177
178 SEARCHSTRING="$1"
179 REPLACESTRING="$2"
180 DESTDIR="$3"
181
182 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
183
184
185 ###########################################
186 # search and replace in each found file the
187 # given string
188 #
189
190 for eachfoundfile in `find $DESTDIR -type f`
191 do
192 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
193 done
194
195 }