Test availability of required commands
[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 [ -f testing.conf ] || die "Configuration file 'testing.conf' not found"
30 . testing.conf
31
32 # execute command
33 # $1 - command to execute
34 # $2 - whether or not to log command exit status
35 # (0 -> disable exit status logging)
36 execute()
37 {
38 cmd=${1}
39 echo $cmd >>$LOGFILE 2>&1
40 $cmd >>$LOGFILE 2>&1
41 status=$?
42 [ "$2" != 0 ] && log_status $status
43 if [ $status != 0 ]; then
44 echo
45 echo "! Command $cmd failed, exiting (status $status)"
46 echo "! Check why here $LOGFILE"
47 exit 1
48 fi
49 }
50
51 # execute command in chroot
52 # $1 - command to execute
53 execute_chroot()
54 {
55 execute "chroot $LOOPDIR $@"
56 }
57
58 # write green status message to console
59 # $1 - msg
60 echo_ok()
61 {
62 echo -e "${GREEN}$1${NORMAL}"
63 }
64
65 # write red status message to console
66 # $1 - msg
67 echo_failed()
68 {
69 echo -e "${RED}$1${NORMAL}"
70 }
71
72 # log an action
73 # $1 - current action description
74 log_action()
75 {
76 /bin/echo -n "[....] $1 "
77 }
78
79 # log an action status
80 # $1 - exit status of action
81 log_status()
82 {
83 tput hpa 0
84 if [ $1 -eq 0 ]; then
85 /bin/echo -ne "[${GREEN} ok ${NORMAL}"
86 else
87 /bin/echo -ne "[${RED}FAIL${NORMAL}"
88 fi
89 echo
90 }
91
92 # the following two functions are stolen from [1]
93 # [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
94
95 declare -a on_exit_items
96
97 # perform registered actions on exit
98 on_exit()
99 {
100 for i in "${on_exit_items[@]}"
101 do
102 eval $i >>$LOGFILE 2>&1
103 done
104 on_exit_items=""
105 trap - EXIT
106 }
107
108 # register a command to execute when the calling script terminates. The
109 # registered commands are called in FIFO order.
110 # $* - command to register
111 do_on_exit()
112 {
113 local n=${#on_exit_items[*]}
114 on_exit_items[$n]="$*"
115 if [ $n -eq 0 ]; then
116 trap on_exit EXIT
117 fi
118 }
119
120 # wait for a mount to disappear
121 # $1 - device/image to wait for
122 # $2 - maximum time to wait in seconds, default is 5 seconds
123 graceful_umount()
124 {
125 secs=$2
126 [ ! $secs ] && secs=5
127
128 let steps=$secs*100
129 for i in `seq 1 $steps`
130 do
131 umount $1 >>$LOGFILE 2>&1
132 mount | grep $1 >/dev/null 2>&1
133 [ $? -eq 0 ] || return 0
134 sleep 0.01
135 done
136
137 return 1
138 }
139
140 # load qemu NBD kernel module, if not already loaded
141 load_qemu_nbd()
142 {
143 lsmod | grep ^nbd[[:space:]]* >/dev/null 2>&1
144 if [ $? != 0 ]
145 then
146 log_action "Loading NBD kernel module"
147 execute "modprobe nbd max_part=16"
148 fi
149 }
150
151 # check if given commands exist in $PATH
152 # $* - commands to check
153 check_commands()
154 {
155 for i in $*
156 do
157 command -v $i >/dev/null || { die "Required command $i not found"; exit 1; }
158 done
159 }
160
161 #############################################
162 # search and replace strings throughout a
163 # whole directory
164 #
165
166 function searchandreplace {
167
168 SEARCHSTRING="$1"
169 REPLACESTRING="$2"
170 DESTDIR="$3"
171
172 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
173
174
175 ###########################################
176 # search and replace in each found file the
177 # given string
178 #
179
180 for eachfoundfile in `find $DESTDIR -type f`
181 do
182 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
183 done
184
185 }
186
187 #############################################
188 # add a bridge
189 #
190
191 function umlbr_add {
192 brctl addbr "umlbr$1"
193 brctl setfd "umlbr$1" 0
194 brctl setageing "umlbr$1" 3600
195 brctl stp "umlbr$1" off
196 ifconfig "umlbr$1" "$2" netmask "$3" up
197 }
198
199 #############################################
200 # delete a bridge
201 #
202
203 function umlbr_del {
204 ifconfig "umlbr$1" down &> /dev/null 2>&1
205 brctl delbr "umlbr$1" &> /dev/null 2>&1
206 }
207
208 #############################################
209 # add a tap interface to a bridge
210 #
211
212 function umlbr_add_tap {
213 tunctl -t "tap$1_$2" &> /dev/null 2>&1
214 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
215 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
216 cecho-n "$2.."
217 }
218
219 #############################################
220 # delete a tap interface from a bridge
221 #
222
223 function umlbr_del_tap {
224 ifconfig "umlbr$2" down &> /dev/null 2>&1
225 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
226 tunctl -d "tap$1_$2" &> /dev/null 2>&1
227 cecho-n "$2.."
228 }
229