ovn: Fix some split db issues in ovn-ctl.
[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 $rundir/ovnnb_db.ctl exit
41     fi
42
43     if pidfile_is_running $DB_SB_PID; then
44         ovs-appctl -t $rundir/ovnsb_db.ctl 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.ctl
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.ctl
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_MANAGE_OVSDB=yes
155
156     DB_NB_SOCK=$rundir/ovnnb_db.sock
157     DB_NB_PID=$rundir/ovnnb_db.pid
158     DB_NB_FILE=$dbdir/ovnnb_db.db
159     DB_NB_PORT=6641
160
161     DB_SB_SOCK=$rundir/ovnsb_db.sock
162     DB_SB_PID=$rundir/ovnsb_db.pid
163     DB_SB_FILE=$dbdir/ovnsb_db.db
164     DB_SB_PORT=6642
165
166     DB_NB_SCHEMA=$datadir/ovn-nb.ovsschema
167     DB_SB_SCHEMA=$datadir/ovn-sb.ovsschema
168
169     DB_SOCK=$rundir/db.sock
170     DB_CONF_FILE=$dbdir/conf.db
171
172     OVN_NORTHD_PRIORITY=-10
173     OVN_NORTHD_WRAPPER=
174     OVN_CONTROLLER_PRIORITY=-10
175     OVN_CONTROLLER_WRAPPER=
176
177     OVS_RUNDIR=${OVS_RUNDIR:-${rundir}}
178     OVN_RUNDIR=${OVN_RUNDIR:-${OVS_RUNDIR}}
179
180     OVN_CONTROLLER_LOG="-vconsole:emer -vsyslog:err -vfile:info"
181     OVN_NORTHD_LOG="-vconsole:emer -vsyslog:err -vfile:info"
182     OVN_NB_LOG="-vconsole:off"
183     OVN_SB_LOG="-vconsole:off"
184     OVN_NB_LOGFILE="$logdir/ovsdb-server-nb.log"
185     OVN_SB_LOGFILE="$logdir/ovsdb-server-sb.log"
186 }
187
188 set_option () {
189     var=`echo "$option" | tr abcdefghijklmnopqrstuvwxyz- ABCDEFGHIJKLMNOPQRSTUVWXYZ_`
190     eval set=\${$var+yes}
191     eval old_value=\$$var
192     if test X$set = X || \
193         (test $type = bool && \
194         test X"$old_value" != Xno && test X"$old_value" != Xyes); then
195         echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
196         return
197     fi
198     eval $var=\$value
199 }
200
201 usage () {
202     set_defaults
203     cat << EOF
204 $0: controls Open Virtual Network daemons
205 usage: $0 [OPTIONS] COMMAND
206
207 This program is intended to be invoked internally by Open Virtual Network
208 startup scripts.  System administrators should not normally invoke it directly.
209
210 Commands:
211   start_northd           start ovn-northd
212   start_ovsdb            start ovn related ovsdb-server processes
213   start_controller       start ovn-controller
214   stop_northd            stop ovn-northd
215   stop_ovsdb             stop ovn related ovsdb-server processes
216   stop_controller        stop ovn-controller
217   restart_northd         restart ovn-northd
218   restart_ovsdb          restart ovn related ovsdb-server processes
219   restart_controller     restart ovn-controller
220
221 Options:
222   --ovn-northd-priority=NICE     set ovn-northd's niceness (default: $OVN_NORTHD_PRIORITY)
223   --ovn-northd-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
224   --ovn-controller-priority=NICE     set ovn-northd's niceness (default: $OVN_CONTROLLER_PRIORITY)
225   --ovn-controller-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
226   --ovn-manage-ovsdb=yes|no        Whether or not the OVN databases should be
227                                    automatically started and stopped along
228                                    with ovn-northd. The default is "yes". If
229                                    this is set to "no", the "start_ovsdb" and
230                                    "stop_ovsdb" commands must be used to start
231                                    and stop the OVN databases.
232   --ovn-controller-log=STRING        ovn controller process logging params (default: $OVN_CONTROLLER_LOG)
233   --ovn-northd-log=STRING            ovn northd process logging params (default: $OVN_NORTHD_LOG)
234   --ovn-nb-log=STRING             ovn NB ovsdb-server processes logging params (default: $OVN_NB_LOG)
235   --ovn-sb-log=STRING             ovn SB ovsdb-server processes logging params (default: $OVN_SB_LOG)
236   -h, --help                     display this help message
237
238 File location options:
239   --db-sock=SOCKET     JSON-RPC socket name (default: $DB_SOCK)
240   --db-nb-file=FILE    OVN_Northbound db file (default: $DB_NB_FILE)
241   --db-sb-file=FILE    OVN_Southbound db file (default: $DB_SB_FILE)
242   --db-nb-schema=FILE  OVN_Northbound db file (default: $DB_NB_SCHEMA)
243   --db-sb-schema=FILE  OVN_Southbound db file (default: $DB_SB_SCHEMA)
244   --db-nb-port=PORT    OVN Northbound db ptcp port (default: $DB_NB_PORT)
245   --db-sb-port=PORT    OVN Southbound db ptcp port (default: $DB_SB_PORT)
246   --ovn-nb-logfile=FILE OVN Northbound log file (default: $OVN_NB_LOGFILE)
247   --ovn-sb-logfile=FILE OVN Southbound log file (default: $OVN_SB_LOGFILE)
248
249 Default directories with "configure" option and environment variable override:
250   logs: /usr/local/var/log/openvswitch (--with-logdir, OVS_LOGDIR)
251   pidfiles and sockets: /usr/local/var/run/openvswitch (--with-rundir, OVS_RUNDIR)
252   ovn-nb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
253   ovn-sb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
254   system configuration: /usr/local/etc (--sysconfdir, OVS_SYSCONFDIR)
255   data files: /usr/local/share/openvswitch (--pkgdatadir, OVS_PKGDATADIR)
256   user binaries: /usr/local/bin (--bindir, OVS_BINDIR)
257   system binaries: /usr/local/sbin (--sbindir, OVS_SBINDIR)
258 EOF
259 }
260
261 set_defaults
262 command=
263 for arg
264 do
265     case $arg in
266         -h | --help)
267             usage
268             ;;
269         --[a-z]*=*)
270             option=`expr X"$arg" : 'X--\([^=]*\)'`
271             value=`expr X"$arg" : 'X[^=]*=\(.*\)'`
272             type=string
273             set_option
274             ;;
275         --no-[a-z]*)
276             option=`expr X"$arg" : 'X--no-\(.*\)'`
277             value=no
278             type=bool
279             set_option
280             ;;
281         --[a-z]*)
282             option=`expr X"$arg" : 'X--\(.*\)'`
283             value=yes
284             type=bool
285             set_option
286             ;;
287         -*)
288             echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
289             exit 1
290             ;;
291         *)
292             if test X"$command" = X; then
293                 command=$arg
294             else
295                 echo >&2 "$0: exactly one non-option argument required (use --help for help)"
296                 exit 1
297             fi
298             ;;
299     esac
300 done
301 case $command in
302     start_northd)
303         start_northd
304         ;;
305     start_ovsdb)
306         start_ovsdb
307         ;;
308     start_controller)
309         start_controller
310         ;;
311     stop_northd)
312         stop_northd
313         ;;
314     stop_ovsdb)
315        stop_ovsdb
316         ;;
317     stop_controller)
318         stop_controller
319         ;;
320     restart_northd)
321         restart_northd
322         ;;
323     restart_ovsdb)
324         restart_ovsdb
325         ;;
326     restart_controller)
327         restart_controller
328         ;;
329     status_northd)
330         daemon_status ovn-northd || exit 1
331         ;;
332     status_ovsdb)
333         status_ovsdb
334         ;;
335     status_controller)
336         daemon_status ovn-controller || exit 1
337         ;;
338     help)
339         usage
340         ;;
341     preheat)
342         echo >&2 "$0: preheating ovn to 350 degrees F."
343         exit 1
344         ;;
345     '')
346         echo >&2 "$0: missing command name (use --help for help)"
347         exit 1
348         ;;
349     *)
350         echo >&2 "$0: unknown command \"$command\" (use --help for help)"
351         exit 1
352         ;;
353 esac