[elbe-devel] [PATCH 8/9] elbepack: soapclient: centralize of soapclient creation from cli args

Thomas Weißschuh thomas.weissschuh at linutronix.de
Thu Aug 1 12:40:29 CEST 2024


Deduplicate the logic, especially as in the future all parameters will
be passed through the commandline.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/control.py | 44 +++-----------------------------------------
 elbepack/commands/prjrepo.py | 40 +++-------------------------------------
 elbepack/config.py           | 37 +++++++++++++++++++++++++++++++++++++
 elbepack/soapclient.py       |  5 +++++
 4 files changed, 48 insertions(+), 78 deletions(-)

diff --git a/elbepack/commands/control.py b/elbepack/commands/control.py
index c642b27a19b8..6ad19813bdeb 100644
--- a/elbepack/commands/control.py
+++ b/elbepack/commands/control.py
@@ -15,7 +15,7 @@ from urllib.error import URLError
 from suds import WebFault
 
 from elbepack.cli import add_argument, add_arguments_from_decorated_function
-from elbepack.config import add_argument_soaptimeout, cfg
+from elbepack.config import add_arguments_soapclient
 from elbepack.elbexml import ElbeXML, ValidationMode
 from elbepack.soapclient import ElbeSoapClient
 
@@ -369,38 +369,7 @@ def run_command(argv):
 
     aparser = argparse.ArgumentParser(prog='elbe control')
 
-    aparser.add_argument('--host', dest='host', default=cfg['soaphost'],
-                         help='Ip or hostname of elbe-daemon.')
-
-    aparser.add_argument('--port', dest='port', default=cfg['soapport'],
-                         help='Port of soap itf on elbe-daemon.')
-
-    aparser.add_argument('--pass', dest='passwd', default=cfg['elbepass'],
-                         help='Password (default is foo).')
-
-    aparser.add_argument('--user', dest='user', default=cfg['elbeuser'],
-                         help='Username (default is root).')
-
-    add_argument_soaptimeout(aparser)
-
-    aparser.add_argument(
-        '--retries',
-        dest='retries',
-        type=int,
-        default=10,
-        help='How many times to retry the connection to the server before '
-             'giving up (default is 10 times, yielding 10 seconds).')
-
-    devel = aparser.add_argument_group(
-        'options for elbe developers',
-        "Caution: Don't use these options in a productive environment")
-    devel.add_argument('--skip-urlcheck', action='store_true',
-                       dest='url_validation', default=ValidationMode.CHECK_ALL,
-                       help='Skip URL Check inside initvm')
-
-    devel.add_argument('--debug', action='store_true',
-                       dest='debug', default=False,
-                       help='Enable debug mode.')
+    add_arguments_soapclient(aparser)
 
     subparsers = aparser.add_subparsers(required=True)
 
@@ -413,14 +382,7 @@ def run_command(argv):
     args.parser = aparser
 
     try:
-        control = ElbeSoapClient(
-            args.host,
-            args.port,
-            args.user,
-            args.passwd,
-            args.soaptimeout,
-            debug=args.debug,
-            retries=args.retries)
+        control = ElbeSoapClient.from_args(args)
     except URLError:
         print(
             f'Failed to connect to Soap server {args.host}:{args.port}\n',
diff --git a/elbepack/commands/prjrepo.py b/elbepack/commands/prjrepo.py
index a10592db1a2e..494e0033e762 100644
--- a/elbepack/commands/prjrepo.py
+++ b/elbepack/commands/prjrepo.py
@@ -16,7 +16,7 @@ import debian.deb822
 from suds import WebFault
 
 from elbepack.cli import add_argument, add_arguments_from_decorated_function
-from elbepack.config import add_argument_soaptimeout, cfg
+from elbepack.config import add_arguments_soapclient
 from elbepack.soapclient import ElbeSoapClient
 
 
@@ -129,33 +129,7 @@ def run_command(argv):
 
     aparser = argparse.ArgumentParser(prog='elbe prjrepo')
 
-    aparser.add_argument('--host', dest='host', default=cfg['soaphost'],
-                         help='Ip or hostname of elbe-daemon.')
-
-    aparser.add_argument('--port', dest='port', default=cfg['soapport'],
-                         help='Port of soap itf on elbe-daemon.')
-
-    aparser.add_argument('--pass', dest='passwd', default=cfg['elbepass'],
-                         help='Password (default is foo).')
-
-    aparser.add_argument('--user', dest='user', default=cfg['elbeuser'],
-                         help='Username (default is root).')
-
-    add_argument_soaptimeout(aparser)
-
-    aparser.add_argument(
-        '--retries',
-        dest='retries',
-        type=int, default=10,
-        help='How many times to retry the connection to the server before\
-                giving up (default is 10 times, yielding 10 seconds).')
-
-    devel = aparser.add_argument_group(
-        'options for elbe developers',
-        "Caution: Don't use these options in a productive environment")
-    devel.add_argument('--debug', action='store_true',
-                       dest='debug', default=False,
-                       help='Enable debug mode.')
+    add_arguments_soapclient(aparser)
 
     subparsers = aparser.add_subparsers(required=True)
 
@@ -168,15 +142,7 @@ def run_command(argv):
 
     # Try to connect to initvm via SOAP
     try:
-        control = ElbeSoapClient(
-            args.host,
-            args.port,
-            args.user,
-            args.passwd,
-            args.soaptimeout,
-            debug=args.debug,
-            retries=args.retries,
-        )
+        control = ElbeSoapClient.from_args(args)
     except URLError:
         print(
             f'Failed to connect to Soap server {args.host}:{args.port}\n',
diff --git a/elbepack/config.py b/elbepack/config.py
index 6d406505fcc6..98e2925afac4 100644
--- a/elbepack/config.py
+++ b/elbepack/config.py
@@ -49,6 +49,43 @@ def add_argument_soapport(parser_or_func, arg='--port'):
     )
 
 
+def add_arguments_soapclient(parser):
+    parser.add_argument(
+        '--host',
+        dest='soaphost',
+        default=os.environ.get('ELBE_SOAPHOST', 'localhost'),
+    )
+    add_argument_soapport(parser)
+    add_argument_soaptimeout(parser)
+
+    parser.add_argument(
+        '--user',
+        dest='soapuser',
+        default=os.environ.get('ELBE_USER', 'root'),
+    )
+
+    parser.add_argument(
+        '--pass',
+        dest='soappassword',
+        default=os.environ.get('ELBE_USER', 'foo'),
+    )
+
+    parser.add_argument(
+        '--retries',
+        dest='retries',
+        type=int,
+        default=10,
+        help='How many times to retry the connection to the server before '
+             'giving up (default is 10 times, yielding 10 seconds).')
+
+    devel = parser.add_argument_group(
+        'options for elbe developers',
+        "Caution: Don't use these options in a productive environment")
+    devel.add_argument('--debug', action='store_true',
+                       dest='debug', default=False,
+                       help='Enable debug mode.')
+
+
 def add_argument_sshport(parser_or_func):
     return add_argument_to_parser_or_function(
         parser_or_func,
diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
index 21e2fd980e20..1bd369a1a48a 100644
--- a/elbepack/soapclient.py
+++ b/elbepack/soapclient.py
@@ -81,6 +81,11 @@ class ElbeSoapClient:
         # We have a Connection, now login
         self.service.login(user, passwd)
 
+    @classmethod
+    def from_args(cls, args):
+        return cls(args.soaphost, args.soapport, args.soapuser, args.soappassword,
+                   args.soaptimeout, debug=args.debug, retries=args.retries)
+
     def download_file(self, builddir, filename, dst_fname):
         fp = open(dst_fname, 'wb')
         part = 0

-- 
2.45.2



More information about the elbe-devel mailing list