4348d6e975cc2f6248fd452ebe2601a234a25920
[cascardo/ovs.git] / ovn / utilities / ovn-ctl
1 #!/bin/sh
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 case $0 in
16     */*) dir0=`echo "$0" | sed 's,/[^/]*$,,'` ;;
17     *) dir0=./ ;;
18 esac
19 . "$dir0/ovs-lib" || exit 1
20
21 for dir in "$sbindir" "$bindir" /sbin /bin /usr/sbin /usr/bin; do
22     case :$PATH: in
23         *:$dir:*) ;;
24         *) PATH=$PATH:$dir ;;
25     esac
26 done
27
28
29 ## ----- ##
30 ## start ##
31 ## ----- ##
32
33 pidfile_is_running () {
34     pidfile=$1
35     test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
36 } >/dev/null 2>&1
37
38 stop_ovsdb () {
39     if pidfile_is_running $DB_NB_PID; then
40         ovs-appctl -t ovnnb_db exit
41     fi
42
43     if pidfile_is_running $DB_SB_PID; then
44         ovs-appctl -t ovnsb_db exit
45     fi
46 }
47
48 start_ovsdb () {
49     # Check and eventually start ovsdb-server for Northbound DB
50     if ! pidfile_is_running $DB_NB_PID; then
51         upgrade_db "$DB_NB_FILE" "$DB_NB_SCHEMA" 1>/dev/null 2>/dev/null
52
53         set ovsdb-server
54
55         set "$@" --detach $OVN_NB_LOG --log-file=$OVN_NB_LOGFILE --remote=punix:$DB_NB_SOCK --remote=ptcp:$DB_NB_PORT --pidfile=$DB_NB_PID --unixctl=ovnnb_db
56
57         $@ $DB_NB_FILE
58     fi
59
60     # Check and eventually start ovsdb-server for Southbound DB
61     if ! pidfile_is_running $DB_SB_PID; then
62         upgrade_db "$DB_SB_FILE" "$DB_SB_SCHEMA" 1>/dev/null 2>/dev/null
63
64         set ovsdb-server
65
66         set "$@" --detach $OVN_SB_LOG --log-file=$OVN_SB_LOGFILE --remote=punix:$DB_SB_SOCK --remote=ptcp:$DB_SB_PORT --pidfile=$DB_SB_PID --unixctl=ovnsb_db
67         $@ $DB_SB_FILE
68     fi
69 }
70
71 status_ovsdb () {
72   if ! pidfile_is_running $DB_NB_PID; then
73       log_success_msg "OVN Northbound DB is not running"
74   else
75       log_success_msg "OVN Northbound DB is running"
76   fi
77
78   if ! pidfile_is_running $DB_SB_PID; then
79       log_success_msg "OVN Southbound DB is not running"
80   else
81       log_success_msg "OVN Southbound DB is running"
82   fi
83 }
84
85 start_northd () {
86   if test X"$OVN_MANAGE_OVSDB" = Xyes; then
87       start_ovsdb
88   fi
89
90   if ! pidfile_is_running $DB_NB_PID; then
91       log_failure_msg "OVN Northbound DB is not running"
92       exit
93   fi
94   if ! pidfile_is_running $DB_SB_PID; then
95       log_failure_msg "OVN Southbound DB is not running"
96       exit
97   fi
98
99   if daemon_is_running ovn-northd; then
100       log_success_msg "ovn-northd is already running"
101   else
102       set ovn-northd
103       set "$@" $OVN_NORTHD_LOG --ovnnb-db=unix:$DB_NB_SOCK --ovnsb-db=unix:$DB_SB_SOCK
104       OVS_RUNDIR=${OVN_RUNDIR} start_daemon "$OVN_NORTHD_PRIORITY" "$OVN_NORTHD_WRAPPER" "$@"
105   fi
106 }
107
108 start_controller () {
109     set ovn-controller "unix:$DB_SOCK"
110     set "$@" -vconsole:emer -vsyslog:err -vfile:info
111     OVS_RUNDIR=${OVN_RUNDIR} start_daemon "$OVN_CONTROLLER_PRIORITY" "$OVN_CONTROLLER_WRAPPER" "$@"
112 }
113
114 ## ---- ##
115 ## stop ##
116 ## ---- ##
117
118 stop_northd () {
119     OVS_RUNDIR=${OVN_RUNDIR} stop_daemon ovn-northd
120
121     if test X"$OVN_MANAGE_OVSDB" = Xyes; then
122         stop_ovsdb
123     fi
124 }
125
126 stop_controller () {
127     OVS_RUNDIR=${OVN_RUNDIR} stop_daemon ovn-controller
128 }
129
130 ## ------- ##
131 ## restart ##
132 ## ------- ##
133
134 restart_northd () {
135     stop_northd
136     start_northd
137 }
138
139 restart_controller () {
140     stop_controller
141     start_controller
142 }
143
144 restart_ovsdb () {
145     stop_ovsdb
146     start_ovsdb
147 }
148
149 ## ---- ##
150 ## main ##
151 ## ---- ##
152
153 set_defaults () {
154     OVN_DIR=$rundir
155     OVN_MANAGE_OVSDB=yes
156
157     DB_NB_SOCK=$OVN_DIR/ovnnb_db.sock
158     DB_NB_PID=$OVN_DIR/ovnnb_db.pid
159     DB_NB_FILE=$OVN_DIR/ovnnb_db.db
160     DB_NB_PORT=6641
161
162     DB_SB_SOCK=$OVN_DIR/ovnsb_db.sock
163     DB_SB_PID=$OVN_DIR/ovnsb_db.pid
164     DB_SB_FILE=$OVN_DIR/ovnsb_db.db
165     DB_SB_PORT=6642
166
167     DB_NB_SCHEMA=$datadir/ovn-nb.ovsschema
168     DB_SB_SCHEMA=$datadir/ovn-sb.ovsschema
169
170     DB_SOCK=$rundir/db.sock
171     DB_CONF_FILE=$dbdir/conf.db
172
173     OVN_NORTHD_PRIORITY=-10
174     OVN_NORTHD_WRAPPER=
175     OVN_CONTROLLER_PRIORITY=-10
176     OVN_CONTROLLER_WRAPPER=
177
178     OVS_RUNDIR=${OVS_RUNDIR:-${rundir}}
179     OVN_RUNDIR=${OVN_RUNDIR:-${OVS_RUNDIR}}
180
181     OVN_CONTROLLER_LOG="-vconsole:emer -vsyslog:err -vfile:info"
182     OVN_NORTHD_LOG="-vconsole:emer -vsyslog:err -vfile:info"
183     OVN_NB_LOG="-vconsole:off"
184     OVN_SB_LOG="-vconsole:off"
185     OVN_NB_LOGFILE="$OVS_LOGDIR/ovsdb-server-nb.log"
186     OVN_SB_LOGFILE="$OVS_LOGDIR/ovsdb-server-sb.log"
187 }
188
189 set_option () {
190     var=`echo "$option" | tr abcdefghijklmnopqrstuvwxyz- ABCDEFGHIJKLMNOPQRSTUVWXYZ_`
191     eval set=\${$var+yes}
192     eval old_value=\$$var
193     if test X$set = X || \
194         (test $type = bool && \
195         test X"$old_value" != Xno && test X"$old_value" != Xyes); then
196         echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
197         return
198     fi
199     eval $var=\$value
200 }
201
202 usage () {
203     set_defaults
204     cat << EOF
205 $0: controls Open Virtual Network daemons
206 usage: $0 [OPTIONS] COMMAND
207
208 This program is intended to be invoked internally by Open Virtual Network
209 startup scripts.  System administrators should not normally invoke it directly.
210
211 Commands:
212   start_northd           start ovn-northd
213   start_ovsdb            start ovn related ovsdb-server processes
214   start_controller       start ovn-controller
215   stop_northd            stop ovn-northd
216   stop_ovsdb             stop ovn related ovsdb-server processes
217   stop_controller        stop ovn-controller
218   restart_northd         restart ovn-northd
219   restart_ovsdb          restart ovn related ovsdb-server processes
220   restart_controller     restart ovn-controller
221
222 Options:
223   --ovn-northd-priority=NICE     set ovn-northd's niceness (default: $OVN_NORTHD_PRIORITY)
224   --ovn-northd-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
225   --ovn-controller-priority=NICE     set ovn-northd's niceness (default: $OVN_CONTROLLER_PRIORITY)
226   --ovn-controller-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
227   --ovn-manage-ovsdb=yes|no        Whether or not the OVN databases should be
228                                    automatically started and stopped along
229                                    with ovn-northd. The default is "yes". If
230                                    this is set to "no", the "start_ovsdb" and
231                                    "stop_ovsdb" commands must be used to start
232                                    and stop the OVN databases.
233   --ovn-controller-log=STRING        ovn controller process logging params (default: $OVN_CONTROLLER_LOG)
234   --ovn-northd-log=STRING            ovn northd process logging params (default: $OVN_NORTHD_LOG)
235   --ovn-nb-log=STRING             ovn NB ovsdb-server processes logging params (default: $OVN_NB_LOG)
236   --ovn-sb-log=STRING             ovn SB ovsdb-server processes logging params (default: $OVN_SB_LOG)
237   -h, --help                     display this help message
238
239 File location options:
240   --db-sock=SOCKET     JSON-RPC socket name (default: $DB_SOCK)
241   --db-nb-file=FILE    OVN_Northbound db file (default: $DB_NB_FILE)
242   --db-sb-file=FILE    OVN_Southbound db file (default: $DB_SB_FILE)
243   --db-nb-schema=FILE  OVN_Northbound db file (default: $DB_NB_SCHEMA)
244   --db-sb-schema=FILE  OVN_Southbound db file (default: $DB_SB_SCHEMA)
245   --db-nb-port=PORT    OVN Northbound db ptcp port (default: $DB_NB_PORT)
246   --db-sb-port=PORT    OVN Southbound db ptcp port (default: $DB_SB_PORT)
247   --ovn-dir=FILE       OVN Databases directory (default: $OVN_DIR)
248   --ovn-nb-logfile=FILE OVN Northbound log file (default: $OVS_LOGDIR/ovsdb-server-nb.log)
249   --ovn-sb-logfile=FILE OVN Southbound log file (default: $OVS_LOGDIR/ovsdb-server-sb.log)
250
251 Default directories with "configure" option and environment variable override:
252   logs: /usr/local/var/log/openvswitch (--with-logdir, OVS_LOGDIR)
253   pidfiles and sockets: /usr/local/var/run/openvswitch (--with-rundir, OVS_RUNDIR)
254   ovn-nb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
255   ovn-sb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
256   system configuration: /usr/local/etc (--sysconfdir, OVS_SYSCONFDIR)
257   data files: /usr/local/share/openvswitch (--pkgdatadir, OVS_PKGDATADIR)
258   user binaries: /usr/local/bin (--bindir, OVS_BINDIR)
259   system binaries: /usr/local/sbin (--sbindir, OVS_SBINDIR)
260 EOF
261 }
262
263 set_defaults
264 command=
265 for arg
266 do
267     case $arg in
268         -h | --help)
269             usage
270             ;;
271         --[a-z]*=*)
272             option=`expr X"$arg" : 'X--\([^=]*\)'`
273             value=`expr X"$arg" : 'X[^=]*=\(.*\)'`
274             type=string
275             set_option
276             ;;
277         --no-[a-z]*)
278             option=`expr X"$arg" : 'X--no-\(.*\)'`
279             value=no
280             type=bool
281             set_option
282             ;;
283         --[a-z]*)
284             option=`expr X"$arg" : 'X--\(.*\)'`
285             value=yes
286             type=bool
287             set_option
288             ;;
289         -*)
290             echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
291             exit 1
292             ;;
293         *)
294             if test X"$command" = X; then
295                 command=$arg
296             else
297                 echo >&2 "$0: exactly one non-option argument required (use --help for help)"
298                 exit 1
299             fi
300             ;;
301     esac
302 done
303 case $command in
304     start_northd)
305         start_northd
306         ;;
307     start_ovsdb)
308         start_ovsdb
309         ;;
310     start_controller)
311         start_controller
312         ;;
313     stop_northd)
314         stop_northd
315         ;;
316     stop_ovsdb)
317        stop_ovsdb
318         ;;
319     stop_controller)
320         stop_controller
321         ;;
322     restart_northd)
323         restart_northd
324         ;;
325     restart_ovsdb)
326         restart_ovsdb
327         ;;
328     restart_controller)
329         restart_controller
330         ;;
331     status_northd)
332         daemon_status ovn-northd || exit 1
333         ;;
334     status_ovsdb)
335         status_ovsdb
336         ;;
337     status_controller)
338         daemon_status ovn-controller || exit 1
339         ;;
340     help)
341         usage
342         ;;
343     preheat)
344         echo >&2 "$0: preheating ovn to 350 degrees F."
345         exit 1
346         ;;
347     '')
348         echo >&2 "$0: missing command name (use --help for help)"
349         exit 1
350         ;;
351     *)
352         echo >&2 "$0: unknown command \"$command\" (use --help for help)"
353         exit 1
354         ;;
355 esac