2 * Copyright (c) 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include "command-line.h"
30 #include "ovsdb-error.h"
36 #define THIS_MODULE VLM_ovsdb_tool
38 static const struct command all_commands[];
40 static void usage(void) NO_RETURN;
41 static void parse_options(int argc, char *argv[]);
44 main(int argc, char *argv[])
46 set_program_name(argv[0]);
49 parse_options(argc, argv);
50 signal(SIGPIPE, SIG_IGN);
51 run_command(argc - optind, argv + optind, all_commands);
56 parse_options(int argc, char *argv[])
58 static struct option long_options[] = {
59 {"verbose", optional_argument, 0, 'v'},
60 {"help", no_argument, 0, 'h'},
61 {"version", no_argument, 0, 'V'},
64 char *short_options = long_options_to_short_options(long_options);
69 c = getopt_long(argc, argv, short_options, long_options, NULL);
79 OVS_PRINT_VERSION(0, 0);
83 vlog_set_verbosity(optarg);
99 printf("%s: Open vSwitch database management utility\n"
100 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
101 " create DB SCHEMA create DB with the given SCHEMA\n"
102 " compact DB [DST] compact DB in-place (or to DST)\n"
103 " extract-schema DB print DB's schema on stdout\n"
104 " query DB TRNS execute read-only transaction on DB\n"
105 " transact DB TRNS execute read/write transaction on DB\n",
106 program_name, program_name);
108 printf("\nOther options:\n"
109 " -h, --help display this help message\n"
110 " -V, --version display version information\n");
115 parse_json(const char *s)
117 struct json *json = json_from_string(s);
118 if (json->type == JSON_STRING) {
119 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
125 print_and_free_json(struct json *json)
127 char *string = json_to_string(json, JSSF_SORT);
134 check_ovsdb_error(struct ovsdb_error *error)
137 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
142 do_create(int argc UNUSED, char *argv[])
144 const char *db_file_name = argv[1];
145 const char *schema_file_name = argv[2];
146 struct ovsdb_schema *schema;
147 struct ovsdb_file *db_file;
150 /* Read schema from file and convert to JSON. */
151 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
152 json = ovsdb_schema_to_json(schema);
154 /* Create database file. */
155 check_ovsdb_error(ovsdb_file_open(db_file_name, O_RDWR | O_CREAT | O_EXCL,
157 check_ovsdb_error(ovsdb_file_write(db_file, json));
158 check_ovsdb_error(ovsdb_file_commit(db_file));
159 ovsdb_file_close(db_file);
165 transact(int flags, const char *db_file_name, const char *transaction)
167 struct json *request, *result;
170 check_ovsdb_error(ovsdb_open(db_file_name, flags, &db));
172 request = parse_json(transaction);
173 result = ovsdb_execute(db, request, 0, NULL);
174 json_destroy(request);
176 print_and_free_json(result);
181 do_query(int argc UNUSED, char *argv[])
183 transact(O_RDONLY, argv[1], argv[2]);
187 do_transact(int argc UNUSED, char *argv[])
189 transact(O_RDWR, argv[1], argv[2]);
193 do_help(int argc UNUSED, char *argv[] UNUSED)
198 static const struct command all_commands[] = {
199 { "create", 2, 2, do_create },
200 { "query", 2, 2, do_query },
201 { "transact", 2, 2, do_transact },
202 { "help", 0, INT_MAX, do_help },
203 { NULL, 0, 0, NULL },