Drop now obsolete UML helper functions
[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 ((onex=${#on_exit_items[@]}-1; onex>=0; onex--))
101 do
102 echo "On_Exit: ${on_exit_items[$onex]}" >>$LOGFILE
103 ${on_exit_items[$onex]} >>$LOGFILE 2>&1
104 done
105 on_exit_items=""
106 trap - EXIT
107 }
108
109 # register a command to execute when the calling script terminates. The
110 # registered commands are called in FILO order.
111 # $* - command to register
112 do_on_exit()
113 {
114 local n=${#on_exit_items[*]}
115 on_exit_items[$n]="$*"
116 if [ $n -eq 0 ]; then
117 trap on_exit EXIT
118 fi
119 }
120
121 # wait for a mount to disappear
122 # $1 - device/image to wait for
123 # $2 - maximum time to wait in seconds, default is 5 seconds
124 graceful_umount()
125 {
126 secs=$2
127 [ ! $secs ] && secs=5
128
129 let steps=$secs*100
130 for st in `seq 1 $steps`
131 do
132 umount $1 >>$LOGFILE 2>&1
133 mount | grep $1 >/dev/null 2>&1
134 [ $? -eq 0 ] || return 0
135 sleep 0.01
136 done
137
138 return 1
139 }
140
141 # load qemu NBD kernel module, if not already loaded
142 load_qemu_nbd()
143 {
144 lsmod | grep ^nbd[[:space:]]* >/dev/null 2>&1
145 if [ $? != 0 ]
146 then
147 log_action "Loading NBD kernel module"
148 execute "modprobe nbd max_part=16"
149 fi
150 }
151
152 # check if given commands exist in $PATH
153 # $* - commands to check
154 check_commands()
155 {
156 for i in $*
157 do
158 command -v $i >/dev/null || { die "Required command $i not found"; exit 1; }
159 done
160 }
161
162 #############################################
163 # search and replace strings throughout a
164 # whole directory
165 #
166
167 function searchandreplace {
168
169 SEARCHSTRING="$1"
170 REPLACESTRING="$2"
171 DESTDIR="$3"
172
173 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
174
175
176 ###########################################
177 # search and replace in each found file the
178 # given string
179 #
180
181 for eachfoundfile in `find $DESTDIR -type f`
182 do
183 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
184 done
185
186 }