Provide do_on_exit() function
[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 function cecho {
59 echo -e "\033[1;31m$1\033[0m"
60 }
61 function cgecho {
62 echo -e "\033[1;32m$1\033[0m"
63 }
64
65 function cecho-n {
66 echo -en "\033[1;31m$1\033[0m"
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 i in "${on_exit_items[@]}"
98 do
99 eval $i >>$LOGFILE 2>&1
100 done
101 on_exit_items=""
102 trap - EXIT
103 }
104
105 # register a command to execute when the calling script terminates. The
106 # registered commands are called in FIFO order.
107 # $* - command to register
108 do_on_exit()
109 {
110 local n=${#on_exit_items[*]}
111 on_exit_items[$n]="$*"
112 if [ $n -eq 0 ]; then
113 trap on_exit EXIT
114 fi
115 }
116
117 #############################################
118 # search and replace strings throughout a
119 # whole directory
120 #
121
122 function searchandreplace {
123
124 SEARCHSTRING="$1"
125 REPLACESTRING="$2"
126 DESTDIR="$3"
127
128 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
129
130
131 ###########################################
132 # search and replace in each found file the
133 # given string
134 #
135
136 for eachfoundfile in `find $DESTDIR -type f`
137 do
138 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
139 done
140
141 }
142
143 #############################################
144 # add a bridge
145 #
146
147 function umlbr_add {
148 brctl addbr "umlbr$1"
149 brctl setfd "umlbr$1" 0
150 brctl setageing "umlbr$1" 3600
151 brctl stp "umlbr$1" off
152 ifconfig "umlbr$1" "$2" netmask "$3" up
153 }
154
155 #############################################
156 # delete a bridge
157 #
158
159 function umlbr_del {
160 ifconfig "umlbr$1" down &> /dev/null 2>&1
161 brctl delbr "umlbr$1" &> /dev/null 2>&1
162 }
163
164 #############################################
165 # add a tap interface to a bridge
166 #
167
168 function umlbr_add_tap {
169 tunctl -t "tap$1_$2" &> /dev/null 2>&1
170 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
171 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
172 cecho-n "$2.."
173 }
174
175 #############################################
176 # delete a tap interface from a bridge
177 #
178
179 function umlbr_del_tap {
180 ifconfig "umlbr$2" down &> /dev/null 2>&1
181 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
182 tunctl -d "tap$1_$2" &> /dev/null 2>&1
183 cecho-n "$2.."
184 }
185