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"
32 #include "ovsdb-error.h"
33 #include "socket-util.h"
39 VLOG_DEFINE_THIS_MODULE(ovsdb_tool);
41 /* -m, --more: Verbosity level for "show-log" command output. */
42 static int show_log_verbosity;
44 static const struct command all_commands[];
46 static void usage(void) NO_RETURN;
47 static void parse_options(int argc, char *argv[]);
50 main(int argc, char *argv[])
52 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 " convert DB SCHEMA [DST] convert DB to SCHEMA (to DST)\n"
113 " db-version DB report version of schema used by DB\n"
114 " schema-version SCHEMA report SCHEMA's schema version\n"
115 " query DB TRNS execute read-only transaction on DB\n"
116 " transact DB TRNS execute read/write transaction on DB\n"
117 " show-log DB prints information about DB's log entries\n",
118 program_name, program_name);
120 printf("\nOther options:\n"
121 " -m, --more increase show-log verbosity\n"
122 " -h, --help display this help message\n"
123 " -V, --version display version information\n");
128 parse_json(const char *s)
130 struct json *json = json_from_string(s);
131 if (json->type == JSON_STRING) {
132 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
138 print_and_free_json(struct json *json)
140 char *string = json_to_string(json, JSSF_SORT);
147 check_ovsdb_error(struct ovsdb_error *error)
150 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
155 do_create(int argc OVS_UNUSED, char *argv[])
157 const char *db_file_name = argv[1];
158 const char *schema_file_name = argv[2];
159 struct ovsdb_schema *schema;
160 struct ovsdb_log *log;
163 /* Read schema from file and convert to JSON. */
164 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
165 json = ovsdb_schema_to_json(schema);
166 ovsdb_schema_destroy(schema);
168 /* Create database file. */
169 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
171 check_ovsdb_error(ovsdb_log_write(log, json));
172 check_ovsdb_error(ovsdb_log_commit(log));
173 ovsdb_log_close(log);
179 compact_or_convert(const char *src_name, const char *dst_name,
180 const struct ovsdb_schema *new_schema,
183 struct lockfile *src_lock;
184 struct lockfile *dst_lock;
185 bool in_place = dst_name == NULL;
189 /* Lock the source, if we will be replacing it. */
191 retval = lockfile_lock(src_name, 0, &src_lock);
193 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
197 /* Get (temporary) destination and lock it. */
199 dst_name = xasprintf("%s.tmp", src_name);
201 retval = lockfile_lock(dst_name, 0, &dst_lock);
203 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
207 check_ovsdb_error(new_schema
208 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
209 : ovsdb_file_open(src_name, true, &db, NULL));
210 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
213 /* Replace source. */
215 if (rename(dst_name, src_name)) {
216 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
219 fsync_parent_dir(dst_name);
220 lockfile_unlock(src_lock);
223 lockfile_unlock(dst_lock);
227 do_compact(int argc OVS_UNUSED, char *argv[])
229 compact_or_convert(argv[1], argv[2], NULL, "compacted by ovsdb-tool");
233 do_convert(int argc OVS_UNUSED, char *argv[])
235 const char *schema_file_name = argv[2];
236 struct ovsdb_schema *new_schema;
238 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &new_schema));
239 compact_or_convert(argv[1], argv[3], new_schema,
240 "converted by ovsdb-tool");
241 ovsdb_schema_destroy(new_schema);
245 do_db_version(int argc OVS_UNUSED, char *argv[])
247 const char *db_file_name = argv[1];
250 check_ovsdb_error(ovsdb_file_open(db_file_name, true, &db, NULL));
251 puts(db->schema->version);
256 do_schema_version(int argc OVS_UNUSED, char *argv[])
258 const char *schema_file_name = argv[1];
259 struct ovsdb_schema *schema;
261 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
262 puts(schema->version);
263 ovsdb_schema_destroy(schema);
267 transact(bool read_only, const char *db_file_name, const char *transaction)
269 struct json *request, *result;
272 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
274 request = parse_json(transaction);
275 result = ovsdb_execute(db, request, 0, NULL);
276 json_destroy(request);
278 print_and_free_json(result);
283 do_query(int argc OVS_UNUSED, char *argv[])
285 transact(true, argv[1], argv[2]);
289 do_transact(int argc OVS_UNUSED, char *argv[])
291 transact(false, argv[1], argv[2]);
295 print_db_changes(struct shash *tables, struct shash *names)
297 struct shash_node *n1;
299 SHASH_FOR_EACH (n1, tables) {
300 const char *table = n1->name;
301 struct json *rows = n1->data;
302 struct shash_node *n2;
304 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
308 SHASH_FOR_EACH (n2, json_object(rows)) {
309 const char *row_uuid = n2->name;
310 struct json *columns = n2->data;
311 struct shash_node *n3;
312 char *old_name, *new_name;
313 bool free_new_name = false;
315 old_name = new_name = shash_find_data(names, row_uuid);
316 if (columns->type == JSON_OBJECT) {
317 struct json *new_name_json;
319 new_name_json = shash_find_data(json_object(columns), "name");
321 new_name = json_to_string(new_name_json, JSSF_SORT);
322 free_new_name = true;
326 printf("\ttable %s", table);
330 printf(" insert row %s:\n", new_name);
332 printf(" insert row %.8s:\n", row_uuid);
335 printf(" row %s:\n", old_name);
338 if (columns->type == JSON_OBJECT) {
339 if (show_log_verbosity > 1) {
340 SHASH_FOR_EACH (n3, json_object(columns)) {
341 const char *column = n3->name;
342 struct json *value = n3->data;
345 value_string = json_to_string(value, JSSF_SORT);
346 printf("\t\t%s=%s\n", column, value_string);
351 || (new_name != old_name && strcmp(old_name, new_name))) {
353 shash_delete(names, shash_find(names, row_uuid));
356 shash_add(names, row_uuid, (new_name
358 : xmemdup0(row_uuid, 8)));
360 } else if (columns->type == JSON_NULL) {
361 struct shash_node *node;
363 printf("\t\tdelete row\n");
364 node = shash_find(names, row_uuid);
366 shash_delete(names, node);
379 do_show_log(int argc OVS_UNUSED, char *argv[])
381 const char *db_file_name = argv[1];
383 struct ovsdb_log *log;
386 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
392 check_ovsdb_error(ovsdb_log_read(log, &json));
397 printf("record %u:", i);
398 if (json->type == JSON_OBJECT) {
399 struct json *date, *comment;
401 date = shash_find_data(json_object(json), "_date");
402 if (date && date->type == JSON_INTEGER) {
403 time_t t = json_integer(date);
406 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
410 comment = shash_find_data(json_object(json), "_comment");
411 if (comment && comment->type == JSON_STRING) {
412 printf(" \"%s\"", json_string(comment));
415 if (i > 0 && show_log_verbosity > 0) {
417 print_db_changes(json_object(json), &names);
424 /* XXX free 'names'. */
428 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
433 static const struct command all_commands[] = {
434 { "create", 2, 2, do_create },
435 { "compact", 1, 2, do_compact },
436 { "convert", 2, 3, do_convert },
437 { "db-version", 1, 1, do_db_version },
438 { "schema-version", 1, 1, do_schema_version },
439 { "query", 2, 2, do_query },
440 { "transact", 2, 2, do_transact },
441 { "show-log", 1, 1, do_show_log },
442 { "help", 0, INT_MAX, do_help },
443 { NULL, 0, 0, NULL },