3e0560a46c1da689f226085ea6da9abfa5a149cf
[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 #############################################
90 # search and replace strings throughout a
91 # whole directory
92 #
93
94 function searchandreplace {
95
96 SEARCHSTRING="$1"
97 REPLACESTRING="$2"
98 DESTDIR="$3"
99
100 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
101
102
103 ###########################################
104 # search and replace in each found file the
105 # given string
106 #
107
108 for eachfoundfile in `find $DESTDIR -type f`
109 do
110 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
111 done
112
113 }
114
115 #############################################
116 # add a bridge
117 #
118
119 function umlbr_add {
120 brctl addbr "umlbr$1"
121 brctl setfd "umlbr$1" 0
122 brctl setageing "umlbr$1" 3600
123 brctl stp "umlbr$1" off
124 ifconfig "umlbr$1" "$2" netmask "$3" up
125 }
126
127 #############################################
128 # delete a bridge
129 #
130
131 function umlbr_del {
132 ifconfig "umlbr$1" down &> /dev/null 2>&1
133 brctl delbr "umlbr$1" &> /dev/null 2>&1
134 }
135
136 #############################################
137 # add a tap interface to a bridge
138 #
139
140 function umlbr_add_tap {
141 tunctl -t "tap$1_$2" &> /dev/null 2>&1
142 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
143 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
144 cecho-n "$2.."
145 }
146
147 #############################################
148 # delete a tap interface from a bridge
149 #
150
151 function umlbr_del_tap {
152 ifconfig "umlbr$2" down &> /dev/null 2>&1
153 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
154 tunctl -d "tap$1_$2" &> /dev/null 2>&1
155 cecho-n "$2.."
156 }
157