ofpbuf: Rename 'data_delta' to 'xxx_offset'
[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 upgrade_ovn_dbs () {
34     ovn_dbs=$(ovs-appctl -t ovsdb-server ovsdb-server/list-dbs 2>/dev/null)
35     for db in $ovn_dbs; do
36         case $db in
37             OVN*)
38                 action "Removing $db from ovsdb-server" \
39                     ovs-appctl -t ovsdb-server ovsdb-server/remove-db $db
40                 ;;
41         esac
42     done
43     upgrade_db "$DB_NB_FILE" "$DB_NB_SCHEMA"
44     upgrade_db "$DB_SB_FILE" "$DB_SB_SCHEMA"
45     for db in $DB_NB_FILE $DB_SB_FILE; do
46         action "Adding $db to ovsdb-server" \
47             ovs-appctl -t ovsdb-server ovsdb-server/add-db $db || exit 1
48     done
49 }
50
51 start_northd () {
52     # We expect ovn-northd to be co-located with ovsdb-server handling both the
53     # OVN_Northbound and OVN_Southbound dbs.
54     upgrade_ovn_dbs
55
56     set ovn-northd
57     set "$@" -vconsole:emer -vsyslog:err -vfile:info
58     OVS_RUNDIR=${OVN_RUNDIR} start_daemon "$OVN_NORTHD_PRIORITY" "$OVN_NORTHD_WRAPPER" "$@"
59 }
60
61 start_controller () {
62     set ovn-controller "unix:$DB_SOCK"
63     set "$@" -vconsole:emer -vsyslog:err -vfile:info
64     OVS_RUNDIR=${OVN_RUNDIR} start_daemon "$OVN_CONTROLLER_PRIORITY" "$OVN_CONTROLLER_WRAPPER" "$@"
65 }
66
67 ## ---- ##
68 ## stop ##
69 ## ---- ##
70
71 stop_northd () {
72     OVS_RUNDIR=${OVN_RUNDIR} stop_daemon ovn-northd
73 }
74
75 stop_controller () {
76     OVS_RUNDIR=${OVN_RUNDIR} stop_daemon ovn-controller
77 }
78
79 ## ------- ##
80 ## restart ##
81 ## ------- ##
82
83 restart_northd () {
84     stop_northd
85     start_northd
86 }
87
88 restart_controller () {
89     stop_controller
90     start_controller
91 }
92
93 ## ---- ##
94 ## main ##
95 ## ---- ##
96
97 set_defaults () {
98     DB_SOCK=$rundir/db.sock
99     DB_NB_FILE=$dbdir/ovnnb.db
100     DB_SB_FILE=$dbdir/ovnsb.db
101     DB_NB_SCHEMA=$datadir/ovn-nb.ovsschema
102     DB_SB_SCHEMA=$datadir/ovn-sb.ovsschema
103
104     OVN_NORTHD_PRIORITY=-10
105     OVN_NORTHD_WRAPPER=
106     OVN_CONTROLLER_PRIORITY=-10
107     OVN_CONTROLLER_WRAPPER=
108
109     OVS_RUNDIR=${OVS_RUNDIR:-${rundir}}
110     OVN_RUNDIR=${OVN_RUNDIR:-${OVS_RUNDIR}}
111 }
112
113 set_option () {
114     var=`echo "$option" | tr abcdefghijklmnopqrstuvwxyz- ABCDEFGHIJKLMNOPQRSTUVWXYZ_`
115     eval set=\${$var+yes}
116     eval old_value=\$$var
117     if test X$set = X || \
118         (test $type = bool && \
119         test X"$old_value" != Xno && test X"$old_value" != Xyes); then
120         echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
121         return
122     fi
123     eval $var=\$value
124 }
125
126 usage () {
127     set_defaults
128     cat << EOF
129 $0: controls Open Virtual Network daemons
130 usage: $0 [OPTIONS] COMMAND
131
132 This program is intended to be invoked internally by Open Virtual Network
133 startup scripts.  System administrators should not normally invoke it directly.
134
135 Commands:
136   start_northd           start ovn-northd
137   start_controller       start ovn-controller
138   stop_northd            stop ovn-northd
139   stop_controller        stop ovn-controller
140   restart_northd         restart ovn-northd
141   restart_controller     restart ovn-controller
142
143 Options:
144   --ovn-northd-priority=NICE     set ovn-northd's niceness (default: $OVN_NORTHD_PRIORITY)
145   --ovn-northd-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
146   --ovn-controller-priority=NICE     set ovn-northd's niceness (default: $OVN_CONTROLLER_PRIORITY)
147   --ovn-controller-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
148   -h, --help                     display this help message
149
150 File location options:
151   --db-sock=SOCKET     JSON-RPC socket name (default: $DB_SOCK)
152   --db-nb-file=FILE    OVN_Northbound db file (default: $DB_NB_FILE)
153   --db-sb-file=FILE    OVN_Southbound db file (default: $DB_SB_FILE)
154   --db-nb-schema=FILE  OVN_Northbound db file (default: $DB_NB_SCHEMA)
155   --db-sb-schema=FILE  OVN_Southbound db file (default: $DB_SB_SCHEMA)
156
157 Default directories with "configure" option and environment variable override:
158   logs: /usr/local/var/log/openvswitch (--with-logdir, OVS_LOGDIR)
159   pidfiles and sockets: /usr/local/var/run/openvswitch (--with-rundir, OVS_RUNDIR)
160   ovn-nb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
161   ovn-sb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
162   system configuration: /usr/local/etc (--sysconfdir, OVS_SYSCONFDIR)
163   data files: /usr/local/share/openvswitch (--pkgdatadir, OVS_PKGDATADIR)
164   user binaries: /usr/local/bin (--bindir, OVS_BINDIR)
165   system binaries: /usr/local/sbin (--sbindir, OVS_SBINDIR)
166 EOF
167 }
168
169 set_defaults
170 command=
171 for arg
172 do
173     case $arg in
174         -h | --help)
175             usage
176             ;;
177         --[a-z]*=*)
178             option=`expr X"$arg" : 'X--\([^=]*\)'`
179             value=`expr X"$arg" : 'X[^=]*=\(.*\)'`
180             type=string
181             set_option
182             ;;
183         --no-[a-z]*)
184             option=`expr X"$arg" : 'X--no-\(.*\)'`
185             value=no
186             type=bool
187             set_option
188             ;;
189         --[a-z]*)
190             option=`expr X"$arg" : 'X--\(.*\)'`
191             value=yes
192             type=bool
193             set_option
194             ;;
195         -*)
196             echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
197             exit 1
198             ;;
199         *)
200             if test X"$command" = X; then
201                 command=$arg
202             else
203                 echo >&2 "$0: exactly one non-option argument required (use --help for help)"
204                 exit 1
205             fi
206             ;;
207     esac
208 done
209 case $command in
210     start_northd)
211         start_northd
212         ;;
213     start_controller)
214         start_controller
215         ;;
216     stop_northd)
217         stop_northd
218         ;;
219     stop_controller)
220         stop_controller
221         ;;
222     restart_northd)
223         restart_northd
224         ;;
225     restart_controller)
226         restart_controller
227         ;;
228     status_northd)
229         daemon_status ovn-northd || exit 1
230         ;;
231     status_controller)
232         daemon_status ovn-controller || exit 1
233         ;;
234     help)
235         usage
236         ;;
237     preheat)
238         echo >&2 "$0: preheating ovn to 350 degrees F."
239         exit 1
240         ;;
241     '')
242         echo >&2 "$0: missing command name (use --help for help)"
243         exit 1
244         ;;
245     *)
246         echo >&2 "$0: unknown command \"$command\" (use --help for help)"
247         exit 1
248         ;;
249 esac