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"
31 #include "ovsdb-error.h"
37 #define THIS_MODULE VLM_ovsdb_tool
39 static const struct command all_commands[];
41 static void usage(void) NO_RETURN;
42 static void parse_options(int argc, char *argv[]);
45 main(int argc, char *argv[])
47 set_program_name(argv[0]);
50 parse_options(argc, argv);
51 signal(SIGPIPE, SIG_IGN);
52 run_command(argc - optind, argv + optind, all_commands);
57 parse_options(int argc, char *argv[])
59 static struct option long_options[] = {
60 {"verbose", optional_argument, 0, 'v'},
61 {"help", no_argument, 0, 'h'},
62 {"version", no_argument, 0, 'V'},
65 char *short_options = long_options_to_short_options(long_options);
70 c = getopt_long(argc, argv, short_options, long_options, NULL);
80 OVS_PRINT_VERSION(0, 0);
84 vlog_set_verbosity(optarg);
100 printf("%s: Open vSwitch database management utility\n"
101 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
102 " create DB SCHEMA create DB with the given SCHEMA\n"
103 " compact DB [DST] compact DB in-place (or to DST)\n"
104 " extract-schema DB print DB's schema on stdout\n"
105 " query DB TRNS execute read-only transaction on DB\n"
106 " transact DB TRNS execute read/write transaction on DB\n"
107 " show-log DB prints information about DB's log entries\n",
108 program_name, program_name);
110 printf("\nOther options:\n"
111 " -h, --help display this help message\n"
112 " -V, --version display version information\n");
117 parse_json(const char *s)
119 struct json *json = json_from_string(s);
120 if (json->type == JSON_STRING) {
121 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
127 print_and_free_json(struct json *json)
129 char *string = json_to_string(json, JSSF_SORT);
136 check_ovsdb_error(struct ovsdb_error *error)
139 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
144 do_create(int argc UNUSED, char *argv[])
146 const char *db_file_name = argv[1];
147 const char *schema_file_name = argv[2];
148 struct ovsdb_schema *schema;
149 struct ovsdb_log *log;
152 /* Read schema from file and convert to JSON. */
153 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
154 json = ovsdb_schema_to_json(schema);
156 /* Create database file. */
157 check_ovsdb_error(ovsdb_log_open(db_file_name, O_RDWR | O_CREAT | O_EXCL,
159 check_ovsdb_error(ovsdb_log_write(log, json));
160 check_ovsdb_error(ovsdb_log_commit(log));
161 ovsdb_log_close(log);
167 transact(bool read_only, const char *db_file_name, const char *transaction)
169 struct json *request, *result;
172 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db));
174 request = parse_json(transaction);
175 result = ovsdb_execute(db, request, 0, NULL);
176 json_destroy(request);
178 print_and_free_json(result);
183 do_query(int argc UNUSED, char *argv[])
185 transact(true, argv[1], argv[2]);
189 do_transact(int argc UNUSED, char *argv[])
191 transact(false, argv[1], argv[2]);
195 do_show_log(int argc UNUSED, char *argv[])
197 const char *db_file_name = argv[1];
198 struct ovsdb_log *log;
201 check_ovsdb_error(ovsdb_log_open(db_file_name, O_RDONLY, &log));
205 check_ovsdb_error(ovsdb_log_read(log, &json));
210 printf("record %u:", i);
211 if (json->type == JSON_OBJECT) {
212 struct json *date, *comment;
214 date = shash_find_data(json_object(json), "_date");
215 if (date && date->type == JSON_INTEGER) {
216 time_t t = json_integer(date);
219 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
223 comment = shash_find_data(json_object(json), "_comment");
224 if (comment && comment->type == JSON_STRING) {
225 printf(" \"%s\"", json_string(comment));
234 do_help(int argc UNUSED, char *argv[] UNUSED)
239 static const struct command all_commands[] = {
240 { "create", 2, 2, do_create },
241 { "query", 2, 2, do_query },
242 { "transact", 2, 2, do_transact },
243 { "show-log", 1, 1, do_show_log },
244 { "help", 0, INT_MAX, do_help },
245 { NULL, 0, 0, NULL },