[elbe-devel] [PATCH 4/9] elbepack: remove global configuration sshport

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


Instead of having a global variable, pass around the value explicitly
from the entrypoints to the usage site.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/pbuilder.py   |  4 +++-
 elbepack/commands/preprocess.py |  5 ++++-
 elbepack/config.py              | 15 +++++++++++----
 elbepack/init/__init__.py       |  2 ++
 elbepack/init/libvirt.xml.mako  |  4 ++--
 elbepack/initvmaction.py        | 10 ++++++----
 elbepack/xmlpreprocess.py       | 12 ++++++------
 7 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/elbepack/commands/pbuilder.py b/elbepack/commands/pbuilder.py
index 024e373970e4..0b2a3beda4cc 100644
--- a/elbepack/commands/pbuilder.py
+++ b/elbepack/commands/pbuilder.py
@@ -8,6 +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.directories import run_elbe
 from elbepack.filesystem import TmpdirFilesystem
 from elbepack.xmlpreprocess import preprocess_file
@@ -28,6 +29,7 @@ from elbepack.xmlpreprocess import preprocess_file
               help="Deactivates the compiler cache 'ccache'")
 @add_argument('--xmlfile', help='xmlfile to use')
 @add_argument('--project', help='project directory on the initvm')
+ at add_argument_sshport
 def _create(args):
     crossopt = []
     if args.cross:
@@ -38,7 +40,7 @@ def _create(args):
         ccacheopt = ['--ccache-size', args.ccachesize]
 
     if args.xmlfile:
-        with preprocess_file(args.xmlfile, variants=args.variants) as preproc:
+        with preprocess_file(args.xmlfile, variants=args.variants, sshport=args.sshport) 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 57b6ff928bd4..a1f97a8c6d1e 100644
--- a/elbepack/commands/preprocess.py
+++ b/elbepack/commands/preprocess.py
@@ -6,6 +6,7 @@ import argparse
 import os
 import sys
 
+from elbepack.config import add_argument_sshport
 from elbepack.xmlpreprocess import XMLPreprocessError, xmlpreprocess
 
 
@@ -14,6 +15,7 @@ def _add_arguments(parser):
                         type=lambda v: v.split(','),
                         help='enable only tags with empty or given variant')
     parser.add_argument('-p', '--proxy', help='add proxy to mirrors')
+    add_argument_sshport(parser)
     parser.add_argument('-z', '--gzip', type=int, default=9,
                         help='gzip compression level 1-9 (0: no compression)')
 
@@ -38,7 +40,8 @@ def run_command(argv):
 
     try:
         xmlpreprocess(args.xmlfile, args.output,
-                      variants=args.variants, proxy=args.proxy, gzip=args.gzip)
+                      variants=args.variants, proxy=args.proxy, gzip=args.gzip,
+                      sshport=args.sshport)
     except XMLPreprocessError as e:
         print(e, file=sys.stderr)
         sys.exit(114)
diff --git a/elbepack/config.py b/elbepack/config.py
index c9a0edff2a9a..b6ac2b337730 100644
--- a/elbepack/config.py
+++ b/elbepack/config.py
@@ -4,13 +4,14 @@
 
 import os
 
+from elbepack.cli import add_argument_to_parser_or_function
+
 
 class Config(dict):
     def __init__(self):
         dict.__init__(self)
         self['soaphost'] = 'localhost'
         self['soapport'] = '7587'
-        self['sshport'] = '5022'
         self['elbeuser'] = 'root'
         self['elbepass'] = 'foo'
         self['initvm_domain'] = 'initvm'
@@ -18,9 +19,6 @@ class Config(dict):
         if 'ELBE_SOAPPORT' in os.environ:
             self['soapport'] = os.environ['ELBE_SOAPPORT']
 
-        if 'ELBE_SSHPORT' in os.environ:
-            self['sshport'] = os.environ['ELBE_SSHPORT']
-
         if 'ELBE_SOAPHOST' in os.environ:
             self['soaphost'] = os.environ['ELBE_SOAPHOST']
 
@@ -43,3 +41,12 @@ def add_argument_soaptimeout(parser):
         type=int,
         default=os.environ.get('ELBE_SOAPTIMEOUT_SECS', '90'),
     )
+
+
+def add_argument_sshport(parser_or_func):
+    return add_argument_to_parser_or_function(
+        parser_or_func,
+        '--sshport',
+        type=int,
+        default=os.environ.get('ELBE_SSHPORT', '5022'),
+    )
diff --git a/elbepack/init/__init__.py b/elbepack/init/__init__.py
index 9889adcea00b..00fc5b697986 100644
--- a/elbepack/init/__init__.py
+++ b/elbepack/init/__init__.py
@@ -20,6 +20,7 @@ from elbepack.xmldefaults import ElbeDefaults
 
 
 def create_initvm(name, xmlfile, directory, *,
+                  sshport,
                   buildtype=None, skip_validation=False, cdrom=None, fail_on_warning=False,
                   build_bin=True, build_sources=True):
     if not skip_validation:
@@ -72,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),
+         'sshport': sshport,
          'cfg': cfg}
 
     if http_proxy != '':
diff --git a/elbepack/init/libvirt.xml.mako b/elbepack/init/libvirt.xml.mako
index 8308a1f42245..de6d944a4536 100644
--- a/elbepack/init/libvirt.xml.mako
+++ b/elbepack/init/libvirt.xml.mako
@@ -32,8 +32,8 @@ for f in prj.node("portforwarding"):
         f.text("proto"), f.text("host"), f.text("buildenv"))
 
 forward += ',hostfwd=%s::%s-:%s' % ("tcp", cfg['soapport'], "7588")
-if cfg['sshport'] != '-1':
-    forward += ',hostfwd=%s::%s-:%s' % ("tcp", cfg['sshport'], "22")
+if sshport != -1:
+    forward += ',hostfwd=%s::%s-:%s' % ("tcp", sshport, "22")
 
 %><domain type='qemu'
 xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
diff --git a/elbepack/initvmaction.py b/elbepack/initvmaction.py
index 52ce583e2508..c866a42bfb61 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 cfg
+from elbepack.config import add_argument_sshport, cfg
 from elbepack.directories import run_elbe
 from elbepack.elbexml import ElbeXML, ValidationError, ValidationMode
 from elbepack.filesystem import TmpdirFilesystem
@@ -87,7 +87,7 @@ 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) as xmlfile:
+    with preprocess_file(xmlfile, variants=args.variants, sshport=args.sshport) as xmlfile:
 
         ps = run_elbe(['control', 'create_project'],
                       capture_output=True, encoding='utf-8', check=True)
@@ -320,11 +320,12 @@ def _create(args):
             elbepack.__path__[0],
             'init/default-init.xml')
 
-    with preprocess_file(xmlfile, variants=args.variants) as preproc:
+    with preprocess_file(xmlfile, variants=args.variants, sshport=args.sshport) as preproc:
         create_initvm(
             cfg['initvm_domain'],
             preproc,
             args.directory,
+            sshport=args.sshport,
             cdrom=cdrom,
             build_bin=args.build_bin,
             build_sources=args.build_sources,
@@ -383,10 +384,11 @@ def _submit(args):
     _submit_with_repodir_and_dl_result(xmlfile, cdrom, args)
 
 
+ at add_argument_sshport
 def _sync(args):
     top_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
     excludes = ['.git*', '*.pyc', 'elbe-build*', 'initvm', '__pycache__', 'docs', 'examples']
-    ssh = ['ssh', '-p', cfg['sshport'], '-oUserKnownHostsFile=/dev/null']
+    ssh = ['ssh', '-p', str(args.sshport), '-oUserKnownHostsFile=/dev/null']
     subprocess.run([
         'rsync', '--info=name1,stats1', '--archive', '--times',
         *[arg for e in excludes for arg in ('--exclude', e)],
diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index 510fb7aa21ba..e6fecd674a0a 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -129,7 +129,7 @@ def preprocess_iso_option(xml):
         print(f'[WARN] {violation}')
 
 
-def preprocess_initvm_ports(xml):
+def preprocess_initvm_ports(xml, sshport):
     """Filters out the default port forwardings to prevent qemu conflict"""
 
     for forward in xml.iterfind('initvm/portforwarding/forward'):
@@ -139,7 +139,7 @@ def preprocess_initvm_ports(xml):
         if prot is None or benv is None or host is None:
             continue
         if prot.text == 'tcp' and (
-                host.text == cfg['sshport'] and benv.text == '22' or
+                host.text == sshport and benv.text == '22' or
                 host.text == cfg['soapport'] and benv.text == '7588'):
             forward.getparent().remove(forward)
 
@@ -323,7 +323,7 @@ def preprocess_passwd(xml):
                         'backwards compatibility reasons. This is considered insecure nowadays.')
 
 
-def xmlpreprocess(xml_input_file, xml_output_file, *, variants=None, proxy=None, gzip=9):
+def xmlpreprocess(xml_input_file, xml_output_file, *, sshport, 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 +405,7 @@ def xmlpreprocess(xml_input_file, xml_output_file, *, variants=None, proxy=None,
 
         preprocess_iso_option(xml)
 
-        preprocess_initvm_ports(xml)
+        preprocess_initvm_ports(xml, sshport)
 
         preprocess_mirrors(xml)
 
@@ -439,8 +439,8 @@ def xmlpreprocess(xml_input_file, xml_output_file, *, variants=None, proxy=None,
 
 
 @contextlib.contextmanager
-def preprocess_file(xmlfile, *, variants):
+def preprocess_file(xmlfile, *, variants, sshport):
     with tempfile.NamedTemporaryFile(suffix='elbe.xml') as preproc:
-        xmlpreprocess(xmlfile, preproc, variants=variants)
+        xmlpreprocess(xmlfile, preproc, variants=variants, sshport=sshport)
         preproc.seek(0)
         yield preproc.name

-- 
2.45.2



More information about the elbe-devel mailing list