ovs-sandbox: Show the running program on xterm's title
[cascardo/ovs.git] / tutorial / ovs-sandbox
1 #! /bin/sh
2 #
3 # Copyright (c) 2013 Nicira, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -e
18
19 run() {
20     (cd "$sandbox" && "$@") || exit 1
21 }
22
23 run_xterm() {
24     title=$1;
25     shift
26     run xterm -T "$title" -e "$@"  &
27 }
28
29 rungdb() {
30     under_gdb=$1
31     shift
32     # Remove the --detach and to put the process under gdb control.
33     # Also remove --vconsole:off to allow error message to show up
34     # on the console.
35     # Use "DISPLAY" variable to determine out if X is supported
36     if $under_gdb && [ "$DISPLAY" ]; then
37         args=`echo $@ |sed s/--detach//g | sed s/--vconsole:off//g`
38         xterm_title=$1
39         run_xterm $xterm_title gdb --args $args
40     else
41         run $@
42     fi
43 }
44
45 gdb_vswitchd=false;
46 builddir=
47 srcdir=
48 schema=
49 installed=false
50 built=false
51
52 for option; do
53     # This option-parsing mechanism borrowed from a Autoconf-generated
54     # configure script under the following license:
55
56     # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
57     # 2002, 2003, 2004, 2005, 2006, 2009, 2013 Free Software Foundation, Inc.
58     # This configure script is free software; the Free Software Foundation
59     # gives unlimited permission to copy, distribute and modify it.
60
61     # If the previous option needs an argument, assign it.
62     if test -n "$prev"; then
63         eval $prev=\$option
64         prev=
65         continue
66     fi
67     case $option in
68         *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
69         *) optarg=yes ;;
70     esac
71
72     case $dashdash$option in
73         --)
74             dashdash=yes ;;
75         -h|--help)
76             cat <<EOF
77 ovs-sandbox, for starting a sandboxed dummy Open vSwitch environment
78 usage: $0 [OPTION...]
79
80 If you run ovs-sandbox from an OVS build directory, it uses the OVS that
81 you built.  Otherwise, if you have an installed Open vSwitch, it uses
82 the installed version.
83
84 These options force ovs-sandbox to use a particular OVS build:
85   -b, --builddir=DIR   specify Open vSwitch build directory
86   -s, --srcdir=DIR     specify Open vSwitch source directory
87 These options force ovs-sandbox to use an installed Open vSwitch:
88   -i, --installed      use installed Open vSwitch
89   -g, --gdb-vswitchd   run ovs-vswitchd under gdb
90   -S, --schema=FILE    use FILE as vswitch.ovsschema
91
92 Other options:
93   -h, --help           Print this usage message.
94 EOF
95             exit 0
96             ;;
97
98         --b*=*)
99             builddir=$optarg
100             built=:
101             ;;
102         -b|--b*)
103             prev=builddir
104             built=:
105             ;;
106         --sr*=*)
107             srcdir=$optarg
108             built=false
109             ;;
110         -s|--sr*)
111             prev=srcdir
112             built=false
113             ;;
114         -i|--installed)
115             installed=:
116             ;;
117         --sc*=*)
118             schema=$optarg
119             installed=:
120             ;;
121         -S|--sc*)
122             prev=schema
123             installed=:
124             ;;
125         -g|--gdb-v*)
126             gdb_vswitchd=true
127             ;;
128         -*)
129             echo "unrecognized option $option (use --help for help)" >&2
130             exit 1
131             ;;
132         *)
133             echo "$option: non-option arguments not supported (use --help for help)" >&2
134             exit 1
135             ;;
136     esac
137     shift
138 done
139
140 if $installed && $built; then
141     echo "sorry, conflicting options (use --help for help)" >&2
142     exit 1
143 elif $installed || $built; then
144     :
145 elif test -e vswitchd/ovs-vswitchd; then
146     built=:
147     builddir=.
148 elif (ovs-vswitchd --version) >/dev/null 2>&1; then
149     installed=:
150 else
151     echo "can't find an OVS build or install (use --help for help)" >&2
152     exit 1
153 fi
154
155 if $built; then
156     if test ! -e "$builddir"/vswitchd/ovs-vswitchd; then
157         echo "$builddir does not appear to be an OVS build directory" >&2
158         exit 1
159     fi
160     builddir=`cd $builddir && pwd`
161
162     # Find srcdir.
163     case $srcdir in
164         '')
165             srcdir=$builddir
166             if test ! -e "$srcdir"/WHY-OVS.md; then
167                 srcdir=`cd $builddir/.. && pwd`
168             fi
169             ;;
170         /*) ;;
171         *) srcdir=`pwd`/$srcdir ;;
172     esac
173     schema=$srcdir/vswitchd/vswitch.ovsschema
174     if test ! -e "$schema"; then
175         echo >&2 'source directory not found, please use --srcdir'
176         exit 1
177     fi
178
179     # Put built tools early in $PATH.
180     if test ! -e $builddir/vswitchd/ovs-vswitchd; then
181         echo >&2 'build not found, please change set $builddir or change directory'
182         exit 1
183     fi
184     PATH=$builddir/ovsdb:$builddir/vswitchd:$builddir/utilities:$PATH
185     export PATH
186 else
187     case $schema in
188         '')
189             for schema in \
190                 /usr/local/share/openvswitch/vswitch.ovsschema \
191                 /usr/share/openvswitch/vswitch.ovsschema \
192                 none; do
193                 if test -r $schema; then
194                     break
195                 fi
196             done
197             ;;
198         /*) ;;
199         *) schema=`pwd`/$schema ;;
200     esac
201     if test ! -r "$schema"; then
202         echo "can't find vswitch.ovsschema, please specify --schema" >&2
203         exit 1
204     fi
205 fi
206
207 # Create sandbox.
208 rm -rf sandbox
209 mkdir sandbox
210 sandbox=`cd sandbox && pwd`
211
212 # Set up environment for OVS programs to sandbox themselves.
213 OVS_RUNDIR=$sandbox; export OVS_RUNDIR
214 OVS_LOGDIR=$sandbox; export OVS_LOGDIR
215 OVS_DBDIR=$sandbox; export OVS_DBDIR
216 OVS_SYSCONFDIR=$sandbox; export OVS_SYSCONFDIR
217
218 if $built; then
219     # Easy access to OVS manpages.
220     (cd "$builddir" && ${MAKE} install-man mandir="$sandbox"/man)
221     MANPATH=$sandbox/man:; export MANPATH
222 fi
223
224 # Ensure cleanup.
225 trap 'kill `cat "$sandbox"/*.pid`' 0 1 2 3 13 14 15
226
227 # Create database and start ovsdb-server.
228 touch "$sandbox"/.conf.db.~lock~
229 run ovsdb-tool create conf.db "$schema"
230 run ovsdb-server --detach --no-chdir --pidfile -vconsole:off --log-file \
231     --remote=punix:"$sandbox"/db.sock
232
233 # Start ovs-vswitchd.
234 rungdb $gdb_vswitchd ovs-vswitchd --detach --no-chdir --pidfile -vconsole:off --log-file \
235     --enable-dummy=override -vvconn -vnetdev_dummy
236
237 cat <<EOF
238
239
240
241 ----------------------------------------------------------------------
242 You are running in a dummy Open vSwitch environment.  You can use
243 ovs-vsctl, ovs-ofctl, ovs-appctl, and other tools to work with the
244 dummy switch.  
245
246 Log files, pidfiles, and the configuration database are in the
247 "sandbox" subdirectory.
248
249 Exit the shell to kill the running daemons.
250 EOF
251
252 status=0; $SHELL || status=$?
253
254 cat <<EOF
255 ----------------------------------------------------------------------
256
257
258
259 EOF
260
261 exit $status