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