2 * Copyright (c) 2009, 2010 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"
31 #include "ovsdb-error.h"
37 #define THIS_MODULE VLM_ovsdb_tool
39 /* -m, --more: Verbosity level for "show-log" command output. */
40 static int show_log_verbosity;
42 static const struct command all_commands[];
44 static void usage(void) NO_RETURN;
45 static void parse_options(int argc, char *argv[]);
48 main(int argc, char *argv[])
50 set_program_name(argv[0]);
53 parse_options(argc, argv);
54 signal(SIGPIPE, SIG_IGN);
55 run_command(argc - optind, argv + optind, all_commands);
60 parse_options(int argc, char *argv[])
62 static struct option long_options[] = {
63 {"more", no_argument, 0, 'm'},
64 {"verbose", optional_argument, 0, 'v'},
65 {"help", no_argument, 0, 'h'},
66 {"version", no_argument, 0, 'V'},
69 char *short_options = long_options_to_short_options(long_options);
74 c = getopt_long(argc, argv, short_options, long_options, NULL);
88 OVS_PRINT_VERSION(0, 0);
92 vlog_set_verbosity(optarg);
108 printf("%s: Open vSwitch database management utility\n"
109 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
110 " create DB SCHEMA create DB with the given SCHEMA\n"
111 " compact DB [DST] compact DB in-place (or to DST)\n"
112 " extract-schema DB print DB's schema on stdout\n"
113 " query DB TRNS execute read-only transaction on DB\n"
114 " transact DB TRNS execute read/write transaction on DB\n"
115 " show-log DB prints information about DB's log entries\n",
116 program_name, program_name);
118 printf("\nOther options:\n"
119 " -m, --more increase show-log verbosity\n"
120 " -h, --help display this help message\n"
121 " -V, --version display version information\n");
126 parse_json(const char *s)
128 struct json *json = json_from_string(s);
129 if (json->type == JSON_STRING) {
130 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
136 print_and_free_json(struct json *json)
138 char *string = json_to_string(json, JSSF_SORT);
145 check_ovsdb_error(struct ovsdb_error *error)
148 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
153 do_create(int argc UNUSED, char *argv[])
155 const char *db_file_name = argv[1];
156 const char *schema_file_name = argv[2];
157 struct ovsdb_schema *schema;
158 struct ovsdb_log *log;
161 /* Read schema from file and convert to JSON. */
162 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
163 json = ovsdb_schema_to_json(schema);
165 /* Create database file. */
166 check_ovsdb_error(ovsdb_log_open(db_file_name, O_RDWR | O_CREAT | O_EXCL,
168 check_ovsdb_error(ovsdb_log_write(log, json));
169 check_ovsdb_error(ovsdb_log_commit(log));
170 ovsdb_log_close(log);
176 transact(bool read_only, const char *db_file_name, const char *transaction)
178 struct json *request, *result;
181 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db));
183 request = parse_json(transaction);
184 result = ovsdb_execute(db, request, 0, NULL);
185 json_destroy(request);
187 print_and_free_json(result);
192 do_query(int argc UNUSED, char *argv[])
194 transact(true, argv[1], argv[2]);
198 do_transact(int argc UNUSED, char *argv[])
200 transact(false, argv[1], argv[2]);
204 print_db_changes(struct shash *tables, struct shash *names)
206 struct shash_node *n1;
208 SHASH_FOR_EACH (n1, tables) {
209 const char *table = n1->name;
210 struct json *rows = n1->data;
211 struct shash_node *n2;
213 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
217 SHASH_FOR_EACH (n2, json_object(rows)) {
218 const char *row_uuid = n2->name;
219 struct json *columns = n2->data;
220 struct shash_node *n3;
221 char *old_name, *new_name;
222 bool free_new_name = false;
224 old_name = new_name = shash_find_data(names, row_uuid);
225 if (columns->type == JSON_OBJECT) {
226 struct json *new_name_json;
228 new_name_json = shash_find_data(json_object(columns), "name");
230 new_name = json_to_string(new_name_json, JSSF_SORT);
231 free_new_name = true;
235 printf("\ttable %s", table);
239 printf(" insert row %s:\n", new_name);
241 printf(" insert row %.8s:\n", row_uuid);
244 printf(" row %s:\n", old_name);
247 if (columns->type == JSON_OBJECT) {
248 if (show_log_verbosity > 1) {
249 SHASH_FOR_EACH (n3, json_object(columns)) {
250 const char *column = n3->name;
251 struct json *value = n3->data;
254 value_string = json_to_string(value, JSSF_SORT);
255 printf("\t\t%s=%s\n", column, value_string);
260 || (new_name != old_name && strcmp(old_name, new_name))) {
262 shash_delete(names, shash_find(names, row_uuid));
265 shash_add(names, row_uuid, (new_name
267 : xmemdup0(row_uuid, 8)));
269 } else if (columns->type == JSON_NULL) {
270 printf("\t\tdelete row\n");
271 shash_delete(names, shash_find(names, row_uuid));
283 do_show_log(int argc UNUSED, char *argv[])
285 const char *db_file_name = argv[1];
287 struct ovsdb_log *log;
290 check_ovsdb_error(ovsdb_log_open(db_file_name, O_RDONLY, &log));
295 check_ovsdb_error(ovsdb_log_read(log, &json));
300 printf("record %u:", i);
301 if (json->type == JSON_OBJECT) {
302 struct json *date, *comment;
304 date = shash_find_data(json_object(json), "_date");
305 if (date && date->type == JSON_INTEGER) {
306 time_t t = json_integer(date);
309 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
313 comment = shash_find_data(json_object(json), "_comment");
314 if (comment && comment->type == JSON_STRING) {
315 printf(" \"%s\"", json_string(comment));
318 if (i > 0 && show_log_verbosity > 0) {
320 print_db_changes(json_object(json), &names);
327 /* XXX free 'names'. */
331 do_help(int argc UNUSED, char *argv[] UNUSED)
336 static const struct command all_commands[] = {
337 { "create", 2, 2, do_create },
338 { "query", 2, 2, do_query },
339 { "transact", 2, 2, do_transact },
340 { "show-log", 1, 1, do_show_log },
341 { "help", 0, INT_MAX, do_help },
342 { NULL, 0, 0, NULL },