From 577aebdfecb001155242aa9831613310f87ed13a Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 11 Dec 2009 11:28:36 -0800 Subject: [PATCH] ovs-vsctl: Add --dry-run option. --- lib/ovsdb-idl.c | 16 +++++++++++++++- lib/ovsdb-idl.h | 1 + utilities/ovs-vsctl.8.in | 3 +++ utilities/ovs-vsctl.c | 14 +++++++++++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/ovsdb-idl.c b/lib/ovsdb-idl.c index 059682c7..98ec3dd5 100644 --- a/lib/ovsdb-idl.c +++ b/lib/ovsdb-idl.c @@ -79,6 +79,7 @@ struct ovsdb_idl_txn { struct ovsdb_idl *idl; struct hmap txn_rows; enum ovsdb_idl_txn_status status; + bool dry_run; }; static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5); @@ -791,9 +792,16 @@ ovsdb_idl_txn_create(struct ovsdb_idl *idl) txn->idl = idl; txn->status = TXN_INCOMPLETE; hmap_init(&txn->txn_rows); + txn->dry_run = false; return txn; } +void +ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn) +{ + txn->dry_run = true; +} + void ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn) { @@ -1028,6 +1036,12 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn) } } + if (txn->dry_run) { + struct json *op = json_object_create(); + json_object_put_string(op, "op", "abort"); + json_array_add(operations, op); + } + if (!any_updates) { txn->status = TXN_SUCCESS; } else if (!jsonrpc_session_send( @@ -1209,7 +1223,7 @@ ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl, if (error->type == JSON_STRING) { if (!strcmp(error->u.string, "timed out")) { soft_errors++; - } else { + } else if (strcmp(error->u.string, "aborted")) { hard_errors++; } } else { diff --git a/lib/ovsdb-idl.h b/lib/ovsdb-idl.h index 5915a1bb..fd6aa8b9 100644 --- a/lib/ovsdb-idl.h +++ b/lib/ovsdb-idl.h @@ -41,6 +41,7 @@ enum ovsdb_idl_txn_status { const char *ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status); struct ovsdb_idl_txn *ovsdb_idl_txn_create(struct ovsdb_idl *); +void ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *); void ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *); void ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *); enum ovsdb_idl_txn_status ovsdb_idl_txn_commit(struct ovsdb_idl_txn *); diff --git a/utilities/ovs-vsctl.8.in b/utilities/ovs-vsctl.8.in index a836fb73..1ea80b79 100644 --- a/utilities/ovs-vsctl.8.in +++ b/utilities/ovs-vsctl.8.in @@ -96,6 +96,9 @@ lines are printed as \fB\\n\fR, and any instances of \fB\\\fR that would otherwise appear in the output are doubled. Prints a blank line for each command that has no output. . +.IP "\fB\-\-dry\-run\fR" +Prevents \fBovs\-vsctl\fR from actually modifying the database. +. .so lib/vlog.man . .SH COMMANDS diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c index e86cb7cd..9c019cc0 100644 --- a/utilities/ovs-vsctl.c +++ b/utilities/ovs-vsctl.c @@ -45,6 +45,9 @@ static const char *db; /* --oneline: Write each command's output as a single line? */ static bool oneline; +/* --dry-run: Do not commit any changes. */ +static bool dry_run; + static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN; static char *default_db(void); static void usage(void) NO_RETURN; @@ -138,12 +141,14 @@ parse_options(int argc, char *argv[]) OPT_DB = UCHAR_MAX + 1, OPT_ONELINE, OPT_NO_SYSLOG, - OPT_NO_WAIT + OPT_NO_WAIT, + OPT_DRY_RUN }; static struct option long_options[] = { {"db", required_argument, 0, OPT_DB}, {"no-syslog", no_argument, 0, OPT_NO_SYSLOG}, {"no-wait", no_argument, 0, OPT_NO_WAIT}, + {"dry-run", no_argument, 0, OPT_DRY_RUN}, {"oneline", no_argument, 0, OPT_ONELINE}, {"verbose", optional_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, @@ -176,6 +181,10 @@ parse_options(int argc, char *argv[]) /* XXX not yet implemented */ break; + case OPT_DRY_RUN: + dry_run = true; + break; + case 'h': usage(); @@ -1192,6 +1201,9 @@ do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl) int i, start; txn = ovsdb_idl_txn_create(idl); + if (dry_run) { + ovsdb_idl_txn_set_dry_run(txn); + } ovs = ovsrec_open_vswitch_first(idl); if (!ovs) { -- 2.30.2