[elbe-devel] [PATCH 05/18] elbepack: initvmaction: pass around full soapclient
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Tue Aug 13 13:15:35 CEST 2024
Add arguments for full soapclient to replace the various calls to
"elbe control" in future commits.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
elbepack/initvm.py | 31 ++++++++++++++++---------------
elbepack/initvmaction.py | 20 +++++++++++---------
elbepack/soapclient.py | 2 ++
3 files changed, 29 insertions(+), 24 deletions(-)
diff --git a/elbepack/initvm.py b/elbepack/initvm.py
index 74102bf6ba29..83b7b6fe4fee 100644
--- a/elbepack/initvm.py
+++ b/elbepack/initvm.py
@@ -19,19 +19,19 @@ from elbepack.directories import run_elbe
from elbepack.treeutils import etree
-def _is_soap_port_reachable(port):
+def _is_soap_port_reachable(control):
"""
Test if a service is bound to the soap port.
"""
try:
- with socket.create_connection(('127.0.0.1', port)):
+ with socket.create_connection((control.host, control.port)):
pass
except Exception:
return False
return True
-def _test_soap_communication(port, sleep=10, wait=120):
+def _test_soap_communication(control, sleep=10, wait=120):
"""
Test communication with soap service.
@@ -42,8 +42,9 @@ def _test_soap_communication(port, sleep=10, wait=120):
"""
stop = time.time() + wait
while True:
- if _is_soap_port_reachable(port):
- ps = run_elbe(['control', '--port', str(port), 'list_projects'],
+ if _is_soap_port_reachable(control):
+ control.connect()
+ ps = run_elbe(['control', '--port', str(control.port), 'list_projects'],
capture_output=True, encoding='utf-8')
if ps.returncode == 0:
break
@@ -94,12 +95,12 @@ class _InitVM(abc.ABC):
class LibvirtInitVM(_InitVM):
- def __init__(self, /, domain, directory, soapport, uri='qemu:///system'):
+ def __init__(self, /, domain, directory, control, uri='qemu:///system'):
self._uri = uri
self._libvirt = importlib.import_module('libvirt', package=__name__)
self._domain = domain
self._directory = directory
- self._soapport = soapport
+ self.control = control
self._conn = None
self._connect()
@@ -261,7 +262,7 @@ class LibvirtInitVM(_InitVM):
if state == self._libvirt.VIR_DOMAIN_SHUTOFF:
self.start()
elif state == self._libvirt.VIR_DOMAIN_RUNNING:
- _test_soap_communication(self._soapport)
+ _test_soap_communication(self.control)
else:
raise CliError(124, 'Elbe initvm in bad state.')
@@ -313,9 +314,9 @@ class LibvirtInitVM(_InitVM):
class QemuInitVM(_InitVM):
- def __init__(self, /, directory, soapport):
+ def __init__(self, /, directory, control):
self._directory = directory
- self._soapport = soapport
+ self.control = control
def _get_initvmdir(self):
if not os.path.isdir(self._directory):
@@ -330,7 +331,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(self._soapport):
+ if _is_soap_port_reachable(self.control):
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.')
@@ -346,11 +347,11 @@ class QemuInitVM(_InitVM):
raise with_cli_details(e, 211, 'Running QEMU failed')
# This will sys.exit on error.
- _test_soap_communication(self._soapport, sleep=1, wait=60)
+ _test_soap_communication(self.control, sleep=1, wait=60)
print('initvm started successfully')
def ensure(self):
- if not _is_soap_port_reachable(self._soapport):
+ if not _is_soap_port_reachable(self.control):
raise CliError(206, 'Elbe initvm in bad state.\nNo process found on soap port.')
def stop(self):
@@ -378,7 +379,7 @@ class QemuInitVM(_InitVM):
# Shutting down the VM will break the connection.
pass
- if _is_soap_port_reachable(self._soapport):
+ if _is_soap_port_reachable(self.control):
raise RuntimeError('stopping initvm failed')
def attach(self):
@@ -402,7 +403,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(self._soapport):
+ if _is_soap_port_reachable(self.control):
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 780cd464cb9a..e8806c8caaa0 100644
--- a/elbepack/initvmaction.py
+++ b/elbepack/initvmaction.py
@@ -13,12 +13,13 @@ import time
import elbepack
import elbepack.initvm
from elbepack.cli import CliError, add_argument, with_cli_details
-from elbepack.config import add_argument_soapport, add_argument_sshport
+from elbepack.config import add_argument_sshport, add_arguments_soapclient
from elbepack.directories import run_elbe
from elbepack.elbexml import ElbeXML, ValidationError, ValidationMode
from elbepack.filesystem import TmpdirFilesystem
from elbepack.init import create_initvm
from elbepack.repodir import Repodir, RepodirError
+from elbepack.soapclient import ElbeSoapClient
from elbepack.treeutils import etree
from elbepack.xmlpreprocess import preprocess_file
@@ -50,18 +51,19 @@ def _add_initvm_from_args_arguments(parser_or_func):
default=os.environ.get('ELBE_INITVM_DOMAIN', 'initvm'),
help='Name of the libvirt initvm')
- parser_or_func = add_argument_soapport(parser_or_func)
+ parser_or_func = add_arguments_soapclient(parser_or_func)
return parser_or_func
def _initvm_from_args(args):
+ control = ElbeSoapClient.from_args(args)
if args.qemu_mode:
- return elbepack.initvm.QemuInitVM(args.directory, soapport=args.soapport)
+ return elbepack.initvm.QemuInitVM(args.directory, control=control)
else:
return elbepack.initvm.LibvirtInitVM(directory=args.directory,
domain=args.domain,
- soapport=args.soapport)
+ control=control)
@_add_initvm_from_args_arguments
@@ -89,19 +91,19 @@ def _attach(args):
_initvm_from_args(args).attach()
-def _submit_with_repodir_and_dl_result(xmlfile, cdrom, args):
+def _submit_with_repodir_and_dl_result(control, xmlfile, cdrom, args):
fname = f'elbe-repodir-{time.time_ns()}.xml'
preprocess_xmlfile = os.path.join(os.path.dirname(xmlfile), fname)
try:
with Repodir(xmlfile, preprocess_xmlfile):
- _submit_and_dl_result(preprocess_xmlfile, cdrom, args)
+ _submit_and_dl_result(control, preprocess_xmlfile, cdrom, args)
except RepodirError as err:
raise with_cli_details(err, 127, 'elbe repodir failed')
finally:
os.remove(preprocess_xmlfile)
-def _submit_and_dl_result(xmlfile, cdrom, args):
+def _submit_and_dl_result(control, xmlfile, cdrom, args):
with preprocess_file(xmlfile, variants=args.variants, sshport=args.sshport,
soapport=args.soapport) as xmlfile:
@@ -375,7 +377,7 @@ def _create(args):
elif cdrom is not None:
xmlfile = tmp.fname('source.xml')
- _submit_with_repodir_and_dl_result(xmlfile, cdrom, args)
+ _submit_with_repodir_and_dl_result(initvm.control, xmlfile, cdrom, args)
@_add_initvm_from_args_arguments
@@ -400,7 +402,7 @@ def _submit(args):
else:
args.parser.error('Unknown file ending (use either xml or iso)')
- _submit_with_repodir_and_dl_result(xmlfile, cdrom, args)
+ _submit_with_repodir_and_dl_result(initvm.control, xmlfile, cdrom, args)
@add_argument_sshport
diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
index 6073a324f0ce..6fbc9eeb586a 100644
--- a/elbepack/soapclient.py
+++ b/elbepack/soapclient.py
@@ -44,6 +44,8 @@ class ElbeSoapClient:
self._retries = retries
self._user = user
self._passwd = passwd
+ self.host = host
+ self.port = port
def connect(self):
control = None
--
2.46.0
More information about the elbe-devel
mailing list