]>
git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/ovsdb-tool.c
61aea75dc57809dc6f44c7eecfe816ec70e20d7a
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 program_name
, program_name
);
109 printf("\nOther options:\n"
110 " -h, --help display this help message\n"
111 " -V, --version display version information\n");
116 parse_json(const char *s
)
118 struct json
*json
= json_from_string(s
);
119 if (json
->type
== JSON_STRING
) {
120 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
126 print_and_free_json(struct json
*json
)
128 char *string
= json_to_string(json
, JSSF_SORT
);
135 check_ovsdb_error(struct ovsdb_error
*error
)
138 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
143 do_create(int argc UNUSED
, char *argv
[])
145 const char *db_file_name
= argv
[1];
146 const char *schema_file_name
= argv
[2];
147 struct ovsdb_schema
*schema
;
148 struct ovsdb_log
*log
;
151 /* Read schema from file and convert to JSON. */
152 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
153 json
= ovsdb_schema_to_json(schema
);
155 /* Create database file. */
156 check_ovsdb_error(ovsdb_log_open(db_file_name
, O_RDWR
| O_CREAT
| O_EXCL
,
158 check_ovsdb_error(ovsdb_log_write(log
, json
));
159 check_ovsdb_error(ovsdb_log_commit(log
));
160 ovsdb_log_close(log
);
166 transact(bool read_only
, const char *db_file_name
, const char *transaction
)
168 struct json
*request
, *result
;
171 check_ovsdb_error(ovsdb_file_open(db_file_name
, read_only
, &db
));
173 request
= parse_json(transaction
);
174 result
= ovsdb_execute(db
, request
, 0, NULL
);
175 json_destroy(request
);
177 print_and_free_json(result
);
182 do_query(int argc UNUSED
, char *argv
[])
184 transact(true, argv
[1], argv
[2]);
188 do_transact(int argc UNUSED
, char *argv
[])
190 transact(false, argv
[1], argv
[2]);
194 do_help(int argc UNUSED
, char *argv
[] UNUSED
)
199 static const struct command all_commands
[] = {
200 { "create", 2, 2, do_create
},
201 { "query", 2, 2, do_query
},
202 { "transact", 2, 2, do_transact
},
203 { "help", 0, INT_MAX
, do_help
},
204 { NULL
, 0, 0, NULL
},