ovsdb-tool: Add commands for printing the database checksum.
[cascardo/ovs.git] / ovsdb / ovsdb-tool.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "command-line.h"
26 #include "compiler.h"
27 #include "file.h"
28 #include "lockfile.h"
29 #include "log.h"
30 #include "json.h"
31 #include "ovsdb.h"
32 #include "ovsdb-error.h"
33 #include "socket-util.h"
34 #include "table.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(ovsdb_tool);
40
41 /* -m, --more: Verbosity level for "show-log" command output. */
42 static int show_log_verbosity;
43
44 static const struct command all_commands[];
45
46 static void usage(void) NO_RETURN;
47 static void parse_options(int argc, char *argv[]);
48
49 int
50 main(int argc, char *argv[])
51 {
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);
56     return 0;
57 }
58
59 static void
60 parse_options(int argc, char *argv[])
61 {
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'},
67         {0, 0, 0, 0},
68     };
69     char *short_options = long_options_to_short_options(long_options);
70
71     for (;;) {
72         int c;
73
74         c = getopt_long(argc, argv, short_options, long_options, NULL);
75         if (c == -1) {
76             break;
77         }
78
79         switch (c) {
80         case 'm':
81             show_log_verbosity++;
82             break;
83
84         case 'h':
85             usage();
86
87         case 'V':
88             OVS_PRINT_VERSION(0, 0);
89             exit(EXIT_SUCCESS);
90
91         case 'v':
92             vlog_set_verbosity(optarg);
93             break;
94
95         case '?':
96             exit(EXIT_FAILURE);
97
98         default:
99             abort();
100         }
101     }
102     free(short_options);
103 }
104
105 static void
106 usage(void)
107 {
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            "  db-cksum DB        report checksum of schema used by DB\n"
115            "  schema-version SCHEMA  report SCHEMA's schema version\n"
116            "  schema-cksum SCHEMA  report SCHEMA's checksum\n"
117            "  query DB TRNS      execute read-only transaction on DB\n"
118            "  transact DB TRNS   execute read/write transaction on DB\n"
119            "  show-log DB        prints information about DB's log entries\n",
120            program_name, program_name);
121     vlog_usage();
122     printf("\nOther options:\n"
123            "  -m, --more                  increase show-log verbosity\n"
124            "  -h, --help                  display this help message\n"
125            "  -V, --version               display version information\n");
126     exit(EXIT_SUCCESS);
127 }
128 \f
129 static struct json *
130 parse_json(const char *s)
131 {
132     struct json *json = json_from_string(s);
133     if (json->type == JSON_STRING) {
134         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
135     }
136     return json;
137 }
138
139 static void
140 print_and_free_json(struct json *json)
141 {
142     char *string = json_to_string(json, JSSF_SORT);
143     json_destroy(json);
144     puts(string);
145     free(string);
146 }
147
148 static void
149 check_ovsdb_error(struct ovsdb_error *error)
150 {
151     if (error) {
152         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
153     }
154 }
155 \f
156 static void
157 do_create(int argc OVS_UNUSED, char *argv[])
158 {
159     const char *db_file_name = argv[1];
160     const char *schema_file_name = argv[2];
161     struct ovsdb_schema *schema;
162     struct ovsdb_log *log;
163     struct json *json;
164
165     /* Read schema from file and convert to JSON. */
166     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
167     json = ovsdb_schema_to_json(schema);
168     ovsdb_schema_destroy(schema);
169
170     /* Create database file. */
171     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
172                                      -1, &log));
173     check_ovsdb_error(ovsdb_log_write(log, json));
174     check_ovsdb_error(ovsdb_log_commit(log));
175     ovsdb_log_close(log);
176
177     json_destroy(json);
178 }
179
180 static void
181 compact_or_convert(const char *src_name, const char *dst_name,
182                    const struct ovsdb_schema *new_schema,
183                    const char *comment)
184 {
185     struct lockfile *src_lock;
186     struct lockfile *dst_lock;
187     bool in_place = dst_name == NULL;
188     struct ovsdb *db;
189     int retval;
190
191     /* Lock the source, if we will be replacing it. */
192     if (in_place) {
193         retval = lockfile_lock(src_name, 0, &src_lock);
194         if (retval) {
195             ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
196         }
197     }
198
199     /* Get (temporary) destination and lock it. */
200     if (in_place) {
201         dst_name = xasprintf("%s.tmp", src_name);
202     }
203     retval = lockfile_lock(dst_name, 0, &dst_lock);
204     if (retval) {
205         ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
206     }
207
208     /* Save a copy. */
209     check_ovsdb_error(new_schema
210                       ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
211                       : ovsdb_file_open(src_name, true, &db, NULL));
212     check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
213     ovsdb_destroy(db);
214
215     /* Replace source. */
216     if (in_place) {
217         if (rename(dst_name, src_name)) {
218             ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
219                       dst_name, src_name);
220         }
221         fsync_parent_dir(dst_name);
222         lockfile_unlock(src_lock);
223     }
224
225     lockfile_unlock(dst_lock);
226 }
227
228 static void
229 do_compact(int argc OVS_UNUSED, char *argv[])
230 {
231     compact_or_convert(argv[1], argv[2], NULL, "compacted by ovsdb-tool");
232 }
233
234 static void
235 do_convert(int argc OVS_UNUSED, char *argv[])
236 {
237     const char *schema_file_name = argv[2];
238     struct ovsdb_schema *new_schema;
239
240     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &new_schema));
241     compact_or_convert(argv[1], argv[3], new_schema,
242                        "converted by ovsdb-tool");
243     ovsdb_schema_destroy(new_schema);
244 }
245
246 static void
247 do_db_version(int argc OVS_UNUSED, char *argv[])
248 {
249     const char *db_file_name = argv[1];
250     struct ovsdb_schema *schema;
251
252     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
253     puts(schema->version);
254     ovsdb_schema_destroy(schema);
255 }
256
257 static void
258 do_db_cksum(int argc OVS_UNUSED, char *argv[])
259 {
260     const char *db_file_name = argv[1];
261     struct ovsdb_schema *schema;
262
263     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
264     puts(schema->cksum);
265     ovsdb_schema_destroy(schema);
266 }
267
268 static void
269 do_schema_version(int argc OVS_UNUSED, char *argv[])
270 {
271     const char *schema_file_name = argv[1];
272     struct ovsdb_schema *schema;
273
274     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
275     puts(schema->version);
276     ovsdb_schema_destroy(schema);
277 }
278
279 static void
280 do_schema_cksum(int argc OVS_UNUSED, char *argv[])
281 {
282     const char *schema_file_name = argv[1];
283     struct ovsdb_schema *schema;
284
285     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
286     puts(schema->cksum);
287     ovsdb_schema_destroy(schema);
288 }
289
290 static void
291 transact(bool read_only, const char *db_file_name, const char *transaction)
292 {
293     struct json *request, *result;
294     struct ovsdb *db;
295
296     check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
297
298     request = parse_json(transaction);
299     result = ovsdb_execute(db, request, 0, NULL);
300     json_destroy(request);
301
302     print_and_free_json(result);
303     ovsdb_destroy(db);
304 }
305
306 static void
307 do_query(int argc OVS_UNUSED, char *argv[])
308 {
309     transact(true, argv[1], argv[2]);
310 }
311
312 static void
313 do_transact(int argc OVS_UNUSED, char *argv[])
314 {
315     transact(false, argv[1], argv[2]);
316 }
317
318 static void
319 print_db_changes(struct shash *tables, struct shash *names)
320 {
321     struct shash_node *n1;
322
323     SHASH_FOR_EACH (n1, tables) {
324         const char *table = n1->name;
325         struct json *rows = n1->data;
326         struct shash_node *n2;
327
328         if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
329             continue;
330         }
331
332         SHASH_FOR_EACH (n2, json_object(rows)) {
333             const char *row_uuid = n2->name;
334             struct json *columns = n2->data;
335             struct shash_node *n3;
336             char *old_name, *new_name;
337             bool free_new_name = false;
338
339             old_name = new_name = shash_find_data(names, row_uuid);
340             if (columns->type == JSON_OBJECT) {
341                 struct json *new_name_json;
342
343                 new_name_json = shash_find_data(json_object(columns), "name");
344                 if (new_name_json) {
345                     new_name = json_to_string(new_name_json, JSSF_SORT);
346                     free_new_name = true;
347                 }
348             }
349
350             printf("\ttable %s", table);
351
352             if (!old_name) {
353                 if (new_name) {
354                     printf(" insert row %s:\n", new_name);
355                 } else {
356                     printf(" insert row %.8s:\n", row_uuid);
357                 }
358             } else {
359                 printf(" row %s:\n", old_name);
360             }
361
362             if (columns->type == JSON_OBJECT) {
363                 if (show_log_verbosity > 1) {
364                     SHASH_FOR_EACH (n3, json_object(columns)) {
365                         const char *column = n3->name;
366                         struct json *value = n3->data;
367                         char *value_string;
368
369                         value_string = json_to_string(value, JSSF_SORT);
370                         printf("\t\t%s=%s\n", column, value_string);
371                         free(value_string);
372                     }
373                 }
374                 if (!old_name
375                     || (new_name != old_name && strcmp(old_name, new_name))) {
376                     if (old_name) {
377                         shash_delete(names, shash_find(names, row_uuid));
378                         free(old_name);
379                     }
380                     shash_add(names, row_uuid, (new_name
381                                                 ? xstrdup(new_name)
382                                                 : xmemdup0(row_uuid, 8)));
383                 }
384             } else if (columns->type == JSON_NULL) {
385                 struct shash_node *node;
386
387                 printf("\t\tdelete row\n");
388                 node = shash_find(names, row_uuid);
389                 if (node) {
390                     shash_delete(names, node);
391                 }
392                 free(old_name);
393             }
394
395             if (free_new_name) {
396                 free(new_name);
397             }
398         }
399     }
400 }
401
402 static void
403 do_show_log(int argc OVS_UNUSED, char *argv[])
404 {
405     const char *db_file_name = argv[1];
406     struct shash names;
407     struct ovsdb_log *log;
408     unsigned int i;
409
410     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
411                                      -1, &log));
412     shash_init(&names);
413     for (i = 0; ; i++) {
414         struct json *json;
415
416         check_ovsdb_error(ovsdb_log_read(log, &json));
417         if (!json) {
418             break;
419         }
420
421         printf("record %u:", i);
422         if (json->type == JSON_OBJECT) {
423             struct json *date, *comment;
424
425             date = shash_find_data(json_object(json), "_date");
426             if (date && date->type == JSON_INTEGER) {
427                 time_t t = json_integer(date);
428                 char s[128];
429
430                 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
431                 printf(" %s", s);
432             }
433
434             comment = shash_find_data(json_object(json), "_comment");
435             if (comment && comment->type == JSON_STRING) {
436                 printf(" \"%s\"", json_string(comment));
437             }
438
439             if (i > 0 && show_log_verbosity > 0) {
440                 putchar('\n');
441                 print_db_changes(json_object(json), &names);
442             }
443         }
444         json_destroy(json);
445         putchar('\n');
446     }
447
448     /* XXX free 'names'. */
449 }
450
451 static void
452 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
453 {
454     usage();
455 }
456
457 static const struct command all_commands[] = {
458     { "create", 2, 2, do_create },
459     { "compact", 1, 2, do_compact },
460     { "convert", 2, 3, do_convert },
461     { "db-version", 1, 1, do_db_version },
462     { "db-cksum", 1, 1, do_db_cksum },
463     { "schema-version", 1, 1, do_schema_version },
464     { "schema-cksum", 1, 1, do_schema_cksum },
465     { "query", 2, 2, do_query },
466     { "transact", 2, 2, do_transact },
467     { "show-log", 1, 1, do_show_log },
468     { "help", 0, INT_MAX, do_help },
469     { NULL, 0, 0, NULL },
470 };