[ovs-dev] [PATCH v1 ovn] ovn-nb/sbctl.c: Use env variables for passing options.

amginwal at gmail.com amginwal at gmail.com
Wed Oct 9 23:56:40 UTC 2019


From: Aliasgar Ginwala <aginwala at ebay.com>

Add new env variables OVN_NBCTL_OPTIONS and OVN_SBCTL_OPTIONS for
ovn-nbctl and ovn-sbctl respectively where user can set any single
supported option. e.g export OVN_NBCTL_OPTIONS=--no-leader-only.
Above env var OVN_NBCTL_OPTIONS have no effect if user runs
command as ovn-nbctl --no-leader-only <command>

Signed-off-by: Aliasgar Ginwala <aginwala at ebay.com>
---
 utilities/ovn-nbctl.8.xml |  7 +++++++
 utilities/ovn-nbctl.c     | 36 ++++++++++++++++++++++++++++++++++--
 utilities/ovn-sbctl.8.in  |  6 ++++++
 utilities/ovn-sbctl.c     | 36 ++++++++++++++++++++++++++++++++++--
 4 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/utilities/ovn-nbctl.8.xml b/utilities/ovn-nbctl.8.xml
index fd75c0e44..6a7962973 100644
--- a/utilities/ovn-nbctl.8.xml
+++ b/utilities/ovn-nbctl.8.xml
@@ -1178,6 +1178,13 @@
           wait at all.  Use the <code>sync</code> command to override this
           behavior.
         </p>
+
+        <p>
+          If the <env>OVN_NBCTL_OPTIONS</env> environment variable is set,
+          its value is used as the default to set above options. If user
+          passes options via cli, <env>OVN_NBCTL_OPTIONS</env> environment
+          variable will have no effect.
+        </p>
       </dd>
 
     <dt><code>--db</code> <var>database</var></dt>
diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index a89a9cb4d..8d2bc0968 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -124,18 +124,49 @@ static char * OVS_WARN_UNUSED_RESULT main_loop(const char *args,
 static void server_loop(struct ovsdb_idl *idl, int argc, char *argv[]);
 
 int
-main(int argc, char *argv[])
+main(int argc, char *argv_[])
 {
     struct ovsdb_idl *idl;
     struct shash local_options;
 
-    set_program_name(argv[0]);
+    set_program_name(argv_[0]);
     fatal_ignore_sigpipe();
     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
     vlog_set_levels_from_string_assert("reconnect:warn");
 
     nbctl_cmd_init();
 
+    /* Check if options are set via env var. */
+    static int ops_passed = false;
+    int i, j = 0;
+    char *ovn_nbctl_options = getenv("OVN_NBCTL_OPTIONS");
+    char **argv = xcalloc(argc + 1, sizeof( *argv_) + 1);
+    if (ovn_nbctl_options) {
+        for (i = 0; i < argc; i++) {
+            if (strcmp(argv_[i], ovn_nbctl_options) == 0) {
+                ops_passed = true;
+                break;
+            }
+        }
+        /* if option not passed via cli, read env var set by user.*/
+        if (!ops_passed) {
+            for (i = 0, j = 0; i < argc; i++, j++) {
+                if (j == 1) {
+                    argv[j] = ovn_nbctl_options;
+                    j++;
+                }
+                argv[j] = xstrdup(argv_[i]);
+            }
+            argc = j;
+        }
+    }
+    if (ops_passed || !ovn_nbctl_options) {
+        /* Copy args for parsing as is from argv_ */
+        for (i = 0; i < argc; i++) {
+            argv[i] = xstrdup(argv_[i]);
+        }
+    }
+
     /* ovn-nbctl has three operation modes:
      *
      *    - Direct: Executes commands by contacting ovsdb-server directly.
@@ -240,6 +271,7 @@ main(int argc, char *argv[])
     idl = the_idl = NULL;
 
     free(args);
+    free(argv);
     exit(EXIT_SUCCESS);
 }
 
diff --git a/utilities/ovn-sbctl.8.in b/utilities/ovn-sbctl.8.in
index 2aaa457e8..b9cfde897 100644
--- a/utilities/ovn-sbctl.8.in
+++ b/utilities/ovn-sbctl.8.in
@@ -93,6 +93,12 @@ to approximately \fIsecs\fR seconds.  If the timeout expires,
 would normally happen only if the database cannot be contacted, or if
 the system is overloaded.)
 .
+.IP "\fBOVN_SBCTL_OPTIONS\fR"
+If \fBOVN_SBCTL_OPTIONS\fR environment variable is set,
+its value is used as the default to set above options. If user
+passes options via cli, \fBOVN_SBCTL_OPTIONS\fR environment
+variable will have no effect.
+.
 .so lib/vlog.man
 .so lib/common.man
 .
diff --git a/utilities/ovn-sbctl.c b/utilities/ovn-sbctl.c
index 9a9b6f0ec..51bfe7101 100644
--- a/utilities/ovn-sbctl.c
+++ b/utilities/ovn-sbctl.c
@@ -93,7 +93,7 @@ static bool do_sbctl(const char *args, struct ctl_command *, size_t n,
                      struct ovsdb_idl *);
 
 int
-main(int argc, char *argv[])
+main(int argc, char *argv_[])
 {
     struct ovsdb_idl *idl;
     struct ctl_command *commands;
@@ -101,13 +101,44 @@ main(int argc, char *argv[])
     unsigned int seqno;
     size_t n_commands;
 
-    set_program_name(argv[0]);
+    set_program_name(argv_[0]);
     fatal_ignore_sigpipe();
     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
     vlog_set_levels_from_string_assert("reconnect:warn");
 
     sbctl_cmd_init();
 
+    /* Check if options are set via env var. */
+    static int ops_passed = false;
+    int i, j = 0;
+    char *ovn_sbctl_options = getenv("OVN_SBCTL_OPTIONS");
+    char **argv = xcalloc(argc + 1, sizeof( *argv_) + 1);
+    if (ovn_sbctl_options) {
+        for (i = 0; i < argc; i++) {
+            if (strcmp(argv_[i], ovn_sbctl_options) == 0) {
+                ops_passed = true;
+                break;
+            }
+        }
+        /* if option not passed via cli, read env var set by user.*/
+        if (!ops_passed) {
+            for (i = 0, j = 0; i < argc; i++, j++) {
+                if (j == 1) {
+                    argv[j] = ovn_sbctl_options;
+                    j++;
+                }
+                argv[j] = xstrdup(argv_[i]);
+            }
+            argc = j;
+        }
+    }
+    if (ops_passed || !ovn_sbctl_options) {
+        /* Copy args for parsing as is from argv_ */
+        for (i = 0; i < argc; i++) {
+            argv[i] = xstrdup(argv_[i]);
+        }
+    }
+
     /* Parse command line. */
     char *args = process_escape_args(argv);
     shash_init(&local_options);
@@ -147,6 +178,7 @@ main(int argc, char *argv[])
             seqno = ovsdb_idl_get_seqno(idl);
             if (do_sbctl(args, commands, n_commands, idl)) {
                 free(args);
+                free(argv);
                 exit(EXIT_SUCCESS);
             }
         }
-- 
2.20.1 (Apple Git-117)



More information about the dev mailing list