Group together functions for working with oVirt SDK. Currently we have
only one (find_host), but I plan to extract other functions to make the
flow more clear and fix error handling.
Maybe these functions can be in a separate module, shared with other
oVirt plugins. Starting with minimal change of grouping them.
---
v2v/rhv-upload-plugin.py | 98 ++++++++++++++++++++--------------------
1 file changed, 50 insertions(+), 48 deletions(-)
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
index 1f42c4a55..7075ce3ba 100644
--- a/v2v/rhv-upload-plugin.py
+++ b/v2v/rhv-upload-plugin.py
@@ -57,54 +57,6 @@ def debug(s):
print(s, file=sys.stderr)
sys.stderr.flush()
-def find_host(connection):
- """Return the current host object or None."""
- try:
- with builtins.open("/etc/vdsm/vdsm.id") as f:
- vdsm_id = f.readline().strip()
- except Exception as e:
- # This is most likely not an oVirt host.
- debug("cannot read /etc/vdsm/vdsm.id, using any host: %s" % e)
- return None
-
- debug("hw_id = %r" % vdsm_id)
-
- system_service = connection.system_service()
- storage_name = params['output_storage']
- data_centers = system_service.data_centers_service().list(
- search='storage.name=%s' % storage_name,
- case_sensitive=True,
- )
- if len(data_centers) == 0:
- # The storage domain is not attached to a datacenter
- # (shouldn't happen, would fail on disk creation).
- debug("storange domain (%s) is not attached to a DC" % storage_name)
- return None
-
- datacenter = data_centers[0]
- debug("datacenter = %s" % datacenter.name)
-
- hosts_service = system_service.hosts_service()
- hosts = hosts_service.list(
- search="hw_id=%s and datacenter=%s and status=Up"
- % (vdsm_id, datacenter.name),
- case_sensitive=True,
- )
- if len(hosts) == 0:
- # Couldn't find a host that's fulfilling the following criteria:
- # - 'hw_id' equals to 'vdsm_id'
- # - Its status is 'Up'
- # - Belongs to the storage domain's datacenter
- debug("cannot find a running host with hw_id=%r, "
- "that belongs to datacenter '%s', "
- "using any host" % (vdsm_id, datacenter.name))
- return None
-
- host = hosts[0]
- debug("host.id = %r" % host.id)
-
- return types.Host(id = host.id)
-
def open(readonly):
# Parse out the username from the output_conn URL.
parsed = urlparse(params['output_conn'])
@@ -564,3 +516,53 @@ class UnixHTTPConnection(HTTPConnection):
if self.timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
self.sock.settimeout(timeout)
self.sock.connect(self.path)
+
+# oVirt SDK operations
+
+def find_host(connection):
+ """Return the current host object or None."""
+ try:
+ with builtins.open("/etc/vdsm/vdsm.id") as f:
+ vdsm_id = f.readline().strip()
+ except Exception as e:
+ # This is most likely not an oVirt host.
+ debug("cannot read /etc/vdsm/vdsm.id, using any host: %s" % e)
+ return None
+
+ debug("hw_id = %r" % vdsm_id)
+
+ system_service = connection.system_service()
+ storage_name = params['output_storage']
+ data_centers = system_service.data_centers_service().list(
+ search='storage.name=%s' % storage_name,
+ case_sensitive=True,
+ )
+ if len(data_centers) == 0:
+ # The storage domain is not attached to a datacenter
+ # (shouldn't happen, would fail on disk creation).
+ debug("storange domain (%s) is not attached to a DC" % storage_name)
+ return None
+
+ datacenter = data_centers[0]
+ debug("datacenter = %s" % datacenter.name)
+
+ hosts_service = system_service.hosts_service()
+ hosts = hosts_service.list(
+ search="hw_id=%s and datacenter=%s and status=Up"
+ % (vdsm_id, datacenter.name),
+ case_sensitive=True,
+ )
+ if len(hosts) == 0:
+ # Couldn't find a host that's fulfilling the following criteria:
+ # - 'hw_id' equals to 'vdsm_id'
+ # - Its status is 'Up'
+ # - Belongs to the storage domain's datacenter
+ debug("cannot find a running host with hw_id=%r, "
+ "that belongs to datacenter '%s', "
+ "using any host" % (vdsm_id, datacenter.name))
+ return None
+
+ host = hosts[0]
+ debug("host.id = %r" % host.id)
+
+ return types.Host(id = host.id)
--
2.21.0