This patch adds import of libvirt library
This patch adds new method _query_libvirt_domain:
- This method needs imported libvirt library to work
- It tries to connect to hypervisor, if it succeeds, it checks if
guest with entered name exists and tries to recover it's IP
from DHCP lease in "default" network. If it fails, it queries
user for new libvirt domain, otherwise it returns the string with
the domain
Signed-off-by: Jiri Prochazka <jprochaz(a)redhat.com>
---
lnst/Controller/Wizard.py | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/lnst/Controller/Wizard.py b/lnst/Controller/Wizard.py
index 570f0d1..20e5816 100644
--- a/lnst/Controller/Wizard.py
+++ b/lnst/Controller/Wizard.py
@@ -10,6 +10,7 @@ __author__ = """
jprochaz(a)redhat.com (Jiri Prochazka)
"""
+import libvirt
import socket
import sys
import time
@@ -375,3 +376,32 @@ class Wizard:
return port
except:
sys.stderr.write("Invalid port entered\n")
+
+ def _query_libvirt_domain(self):
+ """ Queries user for libvirt_domain
+ @note Virtual host must be running under libvirt
+ and has to have an IP from "default" network
+ DHCP server
+ @return String representing libvirt_domain of host
+ """
+ conn = libvirt.openReadOnly("qemu:///system")
+
+ if conn is None:
+ sys.stderr.write("Failed to open connection to hypervisor")
+ raise Exception("FAIL")
+
+ while True:
+ libvirt_domain = raw_input("Enter libvirt domain of virtual host:
")
+ if libvirt_domain == "":
+ sys.stderr.write("No domain entered\n")
+ continue
+ try:
+ conn.lookupByName(libvirt_domain)
+ except:
+ continue
+
+ for lease in conn.networkLookupByName("default").DHCPLeases():
+ if lease["hostname"] == libvirt_domain:
+ return (lease["ipaddr"], libvirt_domain)
+
+ sys.stderr.write("Couldn't find any IP associated with selected
libvirt_domain\n")
--
2.4.3