Move execute wrappers to function.sh file
[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 # execute command
18 # $1 - command to execute
19 # $2 - whether or not to log command exit status
20 # (0 -> disable exit status logging)
21 execute()
22 {
23 cmd=${1}
24 echo $cmd >>$LOGFILE 2>&1
25 $cmd >>$LOGFILE 2>&1
26 status=$?
27 [ "$2" != 0 ] && log_status $status
28 if [ $status != 0 ]; then
29 echo
30 echo "! Command $cmd failed, exiting (status $status)"
31 echo "! Check why here $LOGFILE"
32 exit 1
33 fi
34 }
35
36 # execute command in chroot
37 # $1 - command to execute
38 execute_chroot()
39 {
40 execute "chroot $LOOPDIR $@"
41 }
42
43 export TERM=xterm
44
45 function cecho {
46 echo -e "\033[1;31m$1\033[0m"
47 }
48 function cgecho {
49 echo -e "\033[1;32m$1\033[0m"
50 }
51
52 function cecho-n {
53 echo -en "\033[1;31m$1\033[0m"
54 }
55
56 # log an action
57 # $1 - current action description
58 log_action()
59 {
60 /bin/echo -n "[....] $1 "
61 }
62
63 # log an action status
64 # $1 - exit status of action
65 log_status()
66 {
67 RED=$(tput setaf 1)
68 GREEN=$(tput setaf 2)
69 NORMAL=$(tput op)
70
71 tput hpa 0
72 if [ $1 -eq 0 ]; then
73 /bin/echo -ne "[${GREEN} ok ${NORMAL}"
74 else
75 /bin/echo -ne "[${RED}FAIL${NORMAL}"
76 fi
77 echo
78 }
79
80 #############################################
81 # output all args to stderr and exit with
82 # return code 1
83 #
84
85 die() {
86 echo $* 1>&2
87 exit 1
88 }
89
90 #############################################
91 # search and replace strings throughout a
92 # whole directory
93 #
94
95 function searchandreplace {
96
97 SEARCHSTRING="$1"
98 REPLACESTRING="$2"
99 DESTDIR="$3"
100
101 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
102
103
104 ###########################################
105 # search and replace in each found file the
106 # given string
107 #
108
109 for eachfoundfile in `find $DESTDIR -type f`
110 do
111 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
112 done
113
114 }
115
116 #############################################
117 # add a bridge
118 #
119
120 function umlbr_add {
121 brctl addbr "umlbr$1"
122 brctl setfd "umlbr$1" 0
123 brctl setageing "umlbr$1" 3600
124 brctl stp "umlbr$1" off
125 ifconfig "umlbr$1" "$2" netmask "$3" up
126 }
127
128 #############################################
129 # delete a bridge
130 #
131
132 function umlbr_del {
133 ifconfig "umlbr$1" down &> /dev/null 2>&1
134 brctl delbr "umlbr$1" &> /dev/null 2>&1
135 }
136
137 #############################################
138 # add a tap interface to a bridge
139 #
140
141 function umlbr_add_tap {
142 tunctl -t "tap$1_$2" &> /dev/null 2>&1
143 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
144 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
145 cecho-n "$2.."
146 }
147
148 #############################################
149 # delete a tap interface from a bridge
150 #
151
152 function umlbr_del_tap {
153 ifconfig "umlbr$2" down &> /dev/null 2>&1
154 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
155 tunctl -d "tap$1_$2" &> /dev/null 2>&1
156 cecho-n "$2.."
157 }
158