Drop cecho 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 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 #############################################
121 # search and replace strings throughout a
122 # whole directory
123 #
124
125 function searchandreplace {
126
127 SEARCHSTRING="$1"
128 REPLACESTRING="$2"
129 DESTDIR="$3"
130
131 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
132
133
134 ###########################################
135 # search and replace in each found file the
136 # given string
137 #
138
139 for eachfoundfile in `find $DESTDIR -type f`
140 do
141 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
142 done
143
144 }
145
146 #############################################
147 # add a bridge
148 #
149
150 function umlbr_add {
151 brctl addbr "umlbr$1"
152 brctl setfd "umlbr$1" 0
153 brctl setageing "umlbr$1" 3600
154 brctl stp "umlbr$1" off
155 ifconfig "umlbr$1" "$2" netmask "$3" up
156 }
157
158 #############################################
159 # delete a bridge
160 #
161
162 function umlbr_del {
163 ifconfig "umlbr$1" down &> /dev/null 2>&1
164 brctl delbr "umlbr$1" &> /dev/null 2>&1
165 }
166
167 #############################################
168 # add a tap interface to a bridge
169 #
170
171 function umlbr_add_tap {
172 tunctl -t "tap$1_$2" &> /dev/null 2>&1
173 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
174 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
175 cecho-n "$2.."
176 }
177
178 #############################################
179 # delete a tap interface from a bridge
180 #
181
182 function umlbr_del_tap {
183 ifconfig "umlbr$2" down &> /dev/null 2>&1
184 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
185 tunctl -d "tap$1_$2" &> /dev/null 2>&1
186 cecho-n "$2.."
187 }
188