[elbe-devel] [PATCH 6/9] elbepack: pass around soapport explicitly

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


Instead of relying on the global configuration, use explicit parameters.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/pbuilder.py   |  6 ++++--
 elbepack/commands/preprocess.py |  5 +++--
 elbepack/config.py              | 10 ++++++++++
 elbepack/init/__init__.py       |  3 ++-
 elbepack/init/libvirt.xml.mako  |  2 +-
 elbepack/initvm.py              | 29 +++++++++++++++--------------
 elbepack/initvmaction.py        | 16 +++++++++++-----
 elbepack/xmlpreprocess.py       | 15 ++++++++-------
 8 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/elbepack/commands/pbuilder.py b/elbepack/commands/pbuilder.py
index 0b2a3beda4cc..11bec4b11fe6 100644
--- a/elbepack/commands/pbuilder.py
+++ b/elbepack/commands/pbuilder.py
@@ -8,7 +8,7 @@ import sys
 
 from elbepack.cli import add_argument, add_arguments_from_decorated_function
 from elbepack.commands.preprocess import add_xmlpreprocess_passthrough_arguments
-from elbepack.config import add_argument_sshport
+from elbepack.config import add_argument_soapport, add_argument_sshport
 from elbepack.directories import run_elbe
 from elbepack.filesystem import TmpdirFilesystem
 from elbepack.xmlpreprocess import preprocess_file
@@ -30,6 +30,7 @@ from elbepack.xmlpreprocess import preprocess_file
 @add_argument('--xmlfile', help='xmlfile to use')
 @add_argument('--project', help='project directory on the initvm')
 @add_argument_sshport
+ at add_argument_soapport
 def _create(args):
     crossopt = []
     if args.cross:
@@ -40,7 +41,8 @@ def _create(args):
         ccacheopt = ['--ccache-size', args.ccachesize]
 
     if args.xmlfile:
-        with preprocess_file(args.xmlfile, variants=args.variants, sshport=args.sshport) as preproc:
+        with preprocess_file(args.xmlfile, variants=args.variants, sshport=args.sshport,
+                             soapport=args.soapport) as preproc:
             ps = run_elbe(['control', 'create_project'],
                           capture_output=True, encoding='utf-8')
             if ps.returncode != 0:
diff --git a/elbepack/commands/preprocess.py b/elbepack/commands/preprocess.py
index a1f97a8c6d1e..1427d377ed02 100644
--- a/elbepack/commands/preprocess.py
+++ b/elbepack/commands/preprocess.py
@@ -6,7 +6,7 @@ import argparse
 import os
 import sys
 
-from elbepack.config import add_argument_sshport
+from elbepack.config import add_argument_soapport, add_argument_sshport
 from elbepack.xmlpreprocess import XMLPreprocessError, xmlpreprocess
 
 
@@ -16,6 +16,7 @@ def _add_arguments(parser):
                         help='enable only tags with empty or given variant')
     parser.add_argument('-p', '--proxy', help='add proxy to mirrors')
     add_argument_sshport(parser)
+    add_argument_soapport(parser, '--soapport')
     parser.add_argument('-z', '--gzip', type=int, default=9,
                         help='gzip compression level 1-9 (0: no compression)')
 
@@ -41,7 +42,7 @@ def run_command(argv):
     try:
         xmlpreprocess(args.xmlfile, args.output,
                       variants=args.variants, proxy=args.proxy, gzip=args.gzip,
-                      sshport=args.sshport)
+                      sshport=args.sshport, soapport=args.soapport)
     except XMLPreprocessError as e:
         print(e, file=sys.stderr)
         sys.exit(114)
diff --git a/elbepack/config.py b/elbepack/config.py
index 854c55d77681..6d406505fcc6 100644
--- a/elbepack/config.py
+++ b/elbepack/config.py
@@ -39,6 +39,16 @@ def add_argument_soaptimeout(parser):
     )
 
 
+def add_argument_soapport(parser_or_func, arg='--port'):
+    return add_argument_to_parser_or_function(
+        parser_or_func,
+        arg,
+        dest='soapport',
+        type=int,
+        default=os.environ.get('ELBE_SOAPPORT', '7587'),
+    )
+
+
 def add_argument_sshport(parser_or_func):
     return add_argument_to_parser_or_function(
         parser_or_func,
diff --git a/elbepack/init/__init__.py b/elbepack/init/__init__.py
index 00fc5b697986..f6cac5a7c27e 100644
--- a/elbepack/init/__init__.py
+++ b/elbepack/init/__init__.py
@@ -20,7 +20,7 @@ from elbepack.xmldefaults import ElbeDefaults
 
 
 def create_initvm(name, xmlfile, directory, *,
-                  sshport,
+                  sshport, soapport,
                   buildtype=None, skip_validation=False, cdrom=None, fail_on_warning=False,
                   build_bin=True, build_sources=True):
     if not skip_validation:
@@ -73,6 +73,7 @@ def create_initvm(name, xmlfile, directory, *,
          'http_proxy': initvm_http_proxy,
          'pkgs': xml.node('/initvm/pkg-list') or [],
          'preseed': get_initvm_preseed(xml),
+         'soapport': soapport,
          'sshport': sshport,
          'cfg': cfg}
 
diff --git a/elbepack/init/libvirt.xml.mako b/elbepack/init/libvirt.xml.mako
index de6d944a4536..5804615397ea 100644
--- a/elbepack/init/libvirt.xml.mako
+++ b/elbepack/init/libvirt.xml.mako
@@ -31,7 +31,7 @@ for f in prj.node("portforwarding"):
     forward += ',hostfwd=%s::%s-:%s' % (
         f.text("proto"), f.text("host"), f.text("buildenv"))
 
-forward += ',hostfwd=%s::%s-:%s' % ("tcp", cfg['soapport'], "7588")
+forward += ',hostfwd=%s::%s-:%s' % ("tcp", soapport, "7588")
 if sshport != -1:
     forward += ',hostfwd=%s::%s-:%s' % ("tcp", sshport, "22")
 
diff --git a/elbepack/initvm.py b/elbepack/initvm.py
index e0dfb5481591..499fd52a1368 100644
--- a/elbepack/initvm.py
+++ b/elbepack/initvm.py
@@ -15,16 +15,14 @@ import textwrap
 import time
 
 from elbepack.cli import CliError, with_cli_details
-from elbepack.config import cfg
 from elbepack.directories import run_elbe
 from elbepack.treeutils import etree
 
 
-def _is_soap_port_reachable():
+def _is_soap_port_reachable(port):
     """
     Test if a service is bound to the soap port.
     """
-    port = int(cfg['soapport'])
     try:
         with socket.create_connection(('127.0.0.1', port)):
             pass
@@ -33,7 +31,7 @@ def _is_soap_port_reachable():
     return True
 
 
-def _test_soap_communication(sleep=10, wait=120):
+def _test_soap_communication(port, sleep=10, wait=120):
     """
     Test communication with soap service.
 
@@ -44,8 +42,9 @@ def _test_soap_communication(sleep=10, wait=120):
     """
     stop = time.time() + wait
     while True:
-        if _is_soap_port_reachable():
-            ps = run_elbe(['control', 'list_projects'], capture_output=True, encoding='utf-8')
+        if _is_soap_port_reachable(port):
+            ps = run_elbe(['control', '--port', str(port), 'list_projects'],
+                          capture_output=True, encoding='utf-8')
             if ps.returncode == 0:
                 break
         if time.time() > stop:
@@ -88,11 +87,12 @@ class _InitVM(abc.ABC):
 
 
 class LibvirtInitVM(_InitVM):
-    def __init__(self, /, domain, directory, uri='qemu:///system'):
+    def __init__(self, /, domain, directory, soapport, uri='qemu:///system'):
         self._uri = uri
         self._libvirt = importlib.import_module('libvirt', package=__name__)
         self._domain = domain
         self._directory = directory
+        self._soapport = soapport
         self._conn = None
 
         self._connect()
@@ -249,7 +249,7 @@ class LibvirtInitVM(_InitVM):
         if state == self._libvirt.VIR_DOMAIN_SHUTOFF:
             self.start()
         elif state == self._libvirt.VIR_DOMAIN_RUNNING:
-            _test_soap_communication()
+            _test_soap_communication(self._soapport)
         else:
             raise CliError(124, 'Elbe initvm in bad state.')
 
@@ -301,8 +301,9 @@ class LibvirtInitVM(_InitVM):
 
 
 class QemuInitVM(_InitVM):
-    def __init__(self, /, directory):
+    def __init__(self, /, directory, soapport):
         self._directory = directory
+        self._soapport = soapport
 
     def _get_initvmdir(self):
         if not os.path.isdir(self._directory):
@@ -317,7 +318,7 @@ class QemuInitVM(_InitVM):
         initvmdir = self._get_initvmdir()
 
         # Test if there is already a process bound to the expected port.
-        if _is_soap_port_reachable():
+        if _is_soap_port_reachable(self._soapport):
             if os.path.exists(os.path.join(initvmdir, 'qemu-monitor-socket')):
                 # If the unix socket exists, assume this VM is bound to the soap port.
                 print('This initvm is already running.')
@@ -333,11 +334,11 @@ class QemuInitVM(_InitVM):
                 raise with_cli_details(e, 211, 'Running QEMU failed')
 
             # This will sys.exit on error.
-            _test_soap_communication(sleep=1, wait=60)
+            _test_soap_communication(self._soapport, sleep=1, wait=60)
             print('initvm started successfully')
 
     def ensure(self):
-        if not _is_soap_port_reachable():
+        if not _is_soap_port_reachable(self._soapport):
             raise CliError(206, 'Elbe initvm in bad state.\nNo process found on soap port.')
 
     def stop(self):
@@ -365,7 +366,7 @@ class QemuInitVM(_InitVM):
             # Shutting down the VM will break the connection.
             pass
 
-        if _is_soap_port_reachable():
+        if _is_soap_port_reachable(self._soapport):
             print('\nstopping initvm failed!')
         else:
             print('\ninitvm stopped successfully')
@@ -391,7 +392,7 @@ class QemuInitVM(_InitVM):
                            cwd=initvmdir, check=False)
         else:
             msg = 'No unix socket found for the console of this vm!\nUnable to attach.'
-            if _is_soap_port_reachable():
+            if _is_soap_port_reachable(self._soapport):
                 msg += '\nThere seems to be another initvm running. The soap port is in use.'
             raise CliError(212, msg)
 
diff --git a/elbepack/initvmaction.py b/elbepack/initvmaction.py
index 46498b216aa8..01cf89f7dc96 100644
--- a/elbepack/initvmaction.py
+++ b/elbepack/initvmaction.py
@@ -13,7 +13,7 @@ import time
 import elbepack
 import elbepack.initvm
 from elbepack.cli import CliError, add_argument, with_cli_details
-from elbepack.config import add_argument_sshport
+from elbepack.config import add_argument_soapport, add_argument_sshport
 from elbepack.directories import run_elbe
 from elbepack.elbexml import ElbeXML, ValidationError, ValidationMode
 from elbepack.filesystem import TmpdirFilesystem
@@ -44,15 +44,18 @@ def _add_initvm_from_args_arguments(f):
         default=os.environ.get('ELBE_INITVM_DOMAIN', 'initvm'),
         help='Name of the libvirt initvm')(f)
 
+    f = add_argument_soapport(f)
+
     return f
 
 
 def _initvm_from_args(args):
     if args.qemu_mode:
-        return elbepack.initvm.QemuInitVM(args.directory)
+        return elbepack.initvm.QemuInitVM(args.directory, soapport=args.soapport)
     else:
         return elbepack.initvm.LibvirtInitVM(directory=args.directory,
-                                             domain=args.domain)
+                                             domain=args.domain,
+                                             soapport=args.soapport)
 
 
 @_add_initvm_from_args_arguments
@@ -94,7 +97,8 @@ def _submit_with_repodir_and_dl_result(xmlfile, cdrom, args):
 
 def _submit_and_dl_result(xmlfile, cdrom, args):
 
-    with preprocess_file(xmlfile, variants=args.variants, sshport=args.sshport) as xmlfile:
+    with preprocess_file(xmlfile, variants=args.variants, sshport=args.sshport,
+                         soapport=args.soapport) as xmlfile:
 
         ps = run_elbe(['control', 'create_project'],
                       capture_output=True, encoding='utf-8', check=True)
@@ -327,12 +331,14 @@ def _create(args):
             elbepack.__path__[0],
             'init/default-init.xml')
 
-    with preprocess_file(xmlfile, variants=args.variants, sshport=args.sshport) as preproc:
+    with preprocess_file(xmlfile, variants=args.variants, sshport=args.sshport,
+                         soapport=args.soapport) as preproc:
         create_initvm(
             args.domain,
             preproc,
             args.directory,
             sshport=args.sshport,
+            soapport=args.soapport,
             cdrom=cdrom,
             build_bin=args.build_bin,
             build_sources=args.build_sources,
diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index e6fecd674a0a..cf5df69934a7 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -23,7 +23,6 @@ with warnings.catch_warnings():
     from passlib.hash import sha512_crypt
 
 from elbepack.archivedir import ArchivedirError, combinearchivedir
-from elbepack.config import cfg
 from elbepack.isooptions import iso_option_valid
 from elbepack.treeutils import dbsfed_schema, xml_bool
 from elbepack.validate import error_log_to_strings
@@ -129,7 +128,7 @@ def preprocess_iso_option(xml):
         print(f'[WARN] {violation}')
 
 
-def preprocess_initvm_ports(xml, sshport):
+def preprocess_initvm_ports(xml, sshport, soapport):
     """Filters out the default port forwardings to prevent qemu conflict"""
 
     for forward in xml.iterfind('initvm/portforwarding/forward'):
@@ -140,7 +139,7 @@ def preprocess_initvm_ports(xml, sshport):
             continue
         if prot.text == 'tcp' and (
                 host.text == sshport and benv.text == '22' or
-                host.text == cfg['soapport'] and benv.text == '7588'):
+                host.text == soapport and benv.text == '7588'):
             forward.getparent().remove(forward)
 
 
@@ -323,7 +322,9 @@ def preprocess_passwd(xml):
                         'backwards compatibility reasons. This is considered insecure nowadays.')
 
 
-def xmlpreprocess(xml_input_file, xml_output_file, *, sshport, variants=None, proxy=None, gzip=9):
+def xmlpreprocess(xml_input_file, xml_output_file, *,
+                  sshport, soapport,
+                  variants=None, proxy=None, gzip=9):
     """Preprocesses the input XML data to make sure the `output`
        can be validated against the current schema.
        `xml_input_file` is a path (str) to the input file.
@@ -405,7 +406,7 @@ def xmlpreprocess(xml_input_file, xml_output_file, *, sshport, variants=None, pr
 
         preprocess_iso_option(xml)
 
-        preprocess_initvm_ports(xml, sshport)
+        preprocess_initvm_ports(xml, sshport, soapport)
 
         preprocess_mirrors(xml)
 
@@ -439,8 +440,8 @@ def xmlpreprocess(xml_input_file, xml_output_file, *, sshport, variants=None, pr
 
 
 @contextlib.contextmanager
-def preprocess_file(xmlfile, *, variants, sshport):
+def preprocess_file(xmlfile, *, variants, sshport, soapport):
     with tempfile.NamedTemporaryFile(suffix='elbe.xml') as preproc:
-        xmlpreprocess(xmlfile, preproc, variants=variants, sshport=sshport)
+        xmlpreprocess(xmlfile, preproc, variants=variants, sshport=sshport, soapport=soapport)
         preproc.seek(0)
         yield preproc.name

-- 
2.45.2



More information about the elbe-devel mailing list