[elbe-devel] [PATCH 11/13] config: improve usage of env vars and remove hardcoded values

Manuel Traut manut at linutronix.de
Thu Sep 28 23:59:59 CEST 2017


this enables specifying remote elbe daemon host, username and password
via cmdline environment.

Signed-off-by: Manuel Traut <manut at linutronix.de>
---
 elbepack/commands/control.py | 10 +++++-----
 elbepack/commands/prjrepo.py | 10 +++++-----
 elbepack/config.py           | 15 +++++++++++++--
 elbepack/dbaction.py         |  5 +++--
 4 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/elbepack/commands/control.py b/elbepack/commands/control.py
index c592ce67..8208df1d 100755
--- a/elbepack/commands/control.py
+++ b/elbepack/commands/control.py
@@ -37,17 +37,17 @@ from elbepack.elbexml import ValidationMode
 def run_command (argv):
     oparser = OptionParser (usage="usage: elbe control [options] <command>")
 
-    oparser.add_option ("--host", dest="host", default="localhost",
+    oparser.add_option ("--host", dest="host", default=cfg['soaphost'],
                         help="Ip or hostname of elbe-daemon.")
 
     oparser.add_option ("--port", dest="port", default=cfg['soapport'],
                         help="Port of soap itf on elbe-daemon.")
 
-    oparser.add_option ("--pass", dest="passwd", default="foo",
-                        help="Password (default is foo).")
+    oparser.add_option ("--pass", dest="passwd", default=cfg['passwd'],
+                        help="Password (default is %s)." % cfg['passwd'])
 
-    oparser.add_option ("--user", dest="user", default="root",
-                        help="Username (default is root).")
+    oparser.add_option ("--user", dest="user", default=cfg['user'],
+                        help="Username (default is %s)." % cfg['user'])
 
     oparser.add_option ("--retries", dest="retries", default="10",
                         help="How many times to retry the connection to the server before giving up (default is 10 times, yielding 10 seconds).")
diff --git a/elbepack/commands/prjrepo.py b/elbepack/commands/prjrepo.py
index b09b434c..76f12bdc 100755
--- a/elbepack/commands/prjrepo.py
+++ b/elbepack/commands/prjrepo.py
@@ -35,17 +35,17 @@ from elbepack.config import cfg
 def run_command(argv):
     oparser = OptionParser(usage="usage: elbe prjrepo [options] <command>")
 
-    oparser.add_option("--host", dest="host", default="localhost",
+    oparser.add_option("--host", dest="host", default=cfg['soaphost'],
                        help="Ip or hostname of elbe-daemon.")
 
     oparser.add_option("--port", dest="port", default=cfg['soapport'],
                        help="Port of soap itf on elbe-daemon.")
 
-    oparser.add_option("--pass", dest="passwd", default="foo",
-                       help="Password (default is foo).")
+    oparser.add_option("--pass", dest="passwd", default=cfg['passwd'],
+                       help="Password (default is %s)." % cfg['passwd'])
 
-    oparser.add_option("--user", dest="user", default="root",
-                       help="Username (default is root).")
+    oparser.add_option("--user", dest="user", default=cfg['user'],
+                       help="Username (default is %s)." % cfg['user'])
 
     oparser.add_option(
         "--retries",
diff --git a/elbepack/config.py b/elbepack/config.py
index f4983fbe..08aa9fa3 100644
--- a/elbepack/config.py
+++ b/elbepack/config.py
@@ -22,14 +22,25 @@ import os
 class Config(dict):
     def __init__(self):
         self['soapport'] = "7587"
+        self['soaphost'] = "localhost"
+        self['user']     = "root"
+        self['passwd']   = "foo"
         self['pbuilder_jobs'] = "auto"
 
         if os.environ.has_key('ELBE_SOAPPORT'):
             self['soapport'] = os.environ['ELBE_SOAPPORT']
 
-        if os.environ.has_key('ELBE_PBUILDER_JOBS'):
-            self['pbuilder_jobs'] = os.environ['ELBE_PBUILDER_JOBS']
+        if 'ELBE_SOAPHOST' in os.environ:
+            self['soaphost'] = os.environ['ELBE_SOAPHOST']
+
+        if 'ELBE_USER' in os.environ:
+            self['user'] = os.environ['ELBE_USER']
 
+        if 'ELBE_PASSWD' in os.environ:
+            self['passwd'] = os.environ['ELBE_PASSWD']
+
+        if 'ELBE_PBUILDER_JOBS' in os.environ:
+            self['pbuilder_jobs'] = os.environ['ELBE_PBUILDER_JOBS']
 
 cfg = Config()
 
diff --git a/elbepack/dbaction.py b/elbepack/dbaction.py
index 92515c17..ba65814d 100644
--- a/elbepack/dbaction.py
+++ b/elbepack/dbaction.py
@@ -24,6 +24,7 @@ from optparse import OptionParser
 from getpass import getpass
 from shutil import copyfileobj
 from elbepack.db import ElbeDB, ElbeDBError
+from elbepack.config import cfg
 
 class DbAction(object):
 
@@ -54,9 +55,9 @@ class InitAction(DbAction):
 
     def execute(self, args):
         oparser = OptionParser (usage="usage: %prog db init [options]")
-        oparser.add_option ("--name", dest="name", default="root")
+        oparser.add_option ("--name", dest="name", default=cfg['user'])
         oparser.add_option ("--fullname", dest="fullname", default="Admin")
-        oparser.add_option ("--password", dest="password", default="foo")
+        oparser.add_option ("--password", dest="password", default=cfg['passwd'])
         oparser.add_option ("--email", dest="email", default="root at localhost")
         oparser.add_option ("--noadmin", dest="admin", default=True,
                 action="store_false")
-- 
2.14.1




More information about the elbe-devel mailing list