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 " extract-schema DB print DB's schema on stdout\n"
114 " query DB TRNS execute read-only transaction on DB\n"
115 " transact DB TRNS execute read/write transaction on DB\n"
116 " show-log DB prints information about DB's log entries\n",
117 program_name, program_name);
119 printf("\nOther options:\n"
120 " -m, --more increase show-log verbosity\n"
121 " -h, --help display this help message\n"
122 " -V, --version display version information\n");
127 parse_json(const char *s)
129 struct json *json = json_from_string(s);
130 if (json->type == JSON_STRING) {
131 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
137 print_and_free_json(struct json *json)
139 char *string = json_to_string(json, JSSF_SORT);
146 check_ovsdb_error(struct ovsdb_error *error)
149 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
154 do_create(int argc OVS_UNUSED, char *argv[])
156 const char *db_file_name = argv[1];
157 const char *schema_file_name = argv[2];
158 struct ovsdb_schema *schema;
159 struct ovsdb_log *log;
162 /* Read schema from file and convert to JSON. */
163 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
164 json = ovsdb_schema_to_json(schema);
165 ovsdb_schema_destroy(schema);
167 /* Create database file. */
168 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
170 check_ovsdb_error(ovsdb_log_write(log, json));
171 check_ovsdb_error(ovsdb_log_commit(log));
172 ovsdb_log_close(log);
178 compact_or_convert(const char *src_name, const char *dst_name,
179 const struct ovsdb_schema *new_schema,
182 struct lockfile *src_lock;
183 struct lockfile *dst_lock;
184 bool in_place = dst_name == NULL;
188 /* Lock the source, if we will be replacing it. */
190 retval = lockfile_lock(src_name, 0, &src_lock);
192 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
196 /* Get (temporary) destination and lock it. */
198 dst_name = xasprintf("%s.tmp", src_name);
200 retval = lockfile_lock(dst_name, 0, &dst_lock);
202 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
206 check_ovsdb_error(new_schema
207 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
208 : ovsdb_file_open(src_name, true, &db, NULL));
209 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
212 /* Replace source. */
214 if (rename(dst_name, src_name)) {
215 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
218 fsync_parent_dir(dst_name);
219 lockfile_unlock(src_lock);
222 lockfile_unlock(dst_lock);
226 do_compact(int argc OVS_UNUSED, char *argv[])
228 compact_or_convert(argv[1], argv[2], NULL, "compacted by ovsdb-tool");
232 do_convert(int argc OVS_UNUSED, char *argv[])
234 const char *schema_file_name = argv[2];
235 struct ovsdb_schema *new_schema;
237 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &new_schema));
238 compact_or_convert(argv[1], argv[3], new_schema,
239 "converted by ovsdb-tool");
240 ovsdb_schema_destroy(new_schema);
244 transact(bool read_only, const char *db_file_name, const char *transaction)
246 struct json *request, *result;
249 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
251 request = parse_json(transaction);
252 result = ovsdb_execute(db, request, 0, NULL);
253 json_destroy(request);
255 print_and_free_json(result);
260 do_query(int argc OVS_UNUSED, char *argv[])
262 transact(true, argv[1], argv[2]);
266 do_transact(int argc OVS_UNUSED, char *argv[])
268 transact(false, argv[1], argv[2]);
272 print_db_changes(struct shash *tables, struct shash *names)
274 struct shash_node *n1;
276 SHASH_FOR_EACH (n1, tables) {
277 const char *table = n1->name;
278 struct json *rows = n1->data;
279 struct shash_node *n2;
281 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
285 SHASH_FOR_EACH (n2, json_object(rows)) {
286 const char *row_uuid = n2->name;
287 struct json *columns = n2->data;
288 struct shash_node *n3;
289 char *old_name, *new_name;
290 bool free_new_name = false;
292 old_name = new_name = shash_find_data(names, row_uuid);
293 if (columns->type == JSON_OBJECT) {
294 struct json *new_name_json;
296 new_name_json = shash_find_data(json_object(columns), "name");
298 new_name = json_to_string(new_name_json, JSSF_SORT);
299 free_new_name = true;
303 printf("\ttable %s", table);
307 printf(" insert row %s:\n", new_name);
309 printf(" insert row %.8s:\n", row_uuid);
312 printf(" row %s:\n", old_name);
315 if (columns->type == JSON_OBJECT) {
316 if (show_log_verbosity > 1) {
317 SHASH_FOR_EACH (n3, json_object(columns)) {
318 const char *column = n3->name;
319 struct json *value = n3->data;
322 value_string = json_to_string(value, JSSF_SORT);
323 printf("\t\t%s=%s\n", column, value_string);
328 || (new_name != old_name && strcmp(old_name, new_name))) {
330 shash_delete(names, shash_find(names, row_uuid));
333 shash_add(names, row_uuid, (new_name
335 : xmemdup0(row_uuid, 8)));
337 } else if (columns->type == JSON_NULL) {
338 struct shash_node *node;
340 printf("\t\tdelete row\n");
341 node = shash_find(names, row_uuid);
343 shash_delete(names, node);
356 do_show_log(int argc OVS_UNUSED, char *argv[])
358 const char *db_file_name = argv[1];
360 struct ovsdb_log *log;
363 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
369 check_ovsdb_error(ovsdb_log_read(log, &json));
374 printf("record %u:", i);
375 if (json->type == JSON_OBJECT) {
376 struct json *date, *comment;
378 date = shash_find_data(json_object(json), "_date");
379 if (date && date->type == JSON_INTEGER) {
380 time_t t = json_integer(date);
383 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
387 comment = shash_find_data(json_object(json), "_comment");
388 if (comment && comment->type == JSON_STRING) {
389 printf(" \"%s\"", json_string(comment));
392 if (i > 0 && show_log_verbosity > 0) {
394 print_db_changes(json_object(json), &names);
401 /* XXX free 'names'. */
405 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
410 static const struct command all_commands[] = {
411 { "create", 2, 2, do_create },
412 { "compact", 1, 2, do_compact },
413 { "convert", 2, 3, do_convert },
414 { "query", 2, 2, do_query },
415 { "transact", 2, 2, do_transact },
416 { "show-log", 1, 1, do_show_log },
417 { "help", 0, INT_MAX, do_help },
418 { NULL, 0, 0, NULL },