Adds detection for virsh default network IP 65/42765/1
authorTim Rozet <trozet@redhat.com>
Fri, 22 Sep 2017 14:35:53 +0000 (10:35 -0400)
committerTim Rozet <trozet@redhat.com>
Fri, 22 Sep 2017 14:35:53 +0000 (10:35 -0400)
In case a user already has a default virsh network and the default IP is
not 192.168.122.1, this patch will allow detecting and using the found
value.

Change-Id: Id5d46a820d89e86c9b5897f5d29b83ed32f9d6f9
Signed-off-by: Tim Rozet <trozet@redhat.com>
apex/virtual/virtual_utils.py

index 255d2c6..1fe2c39 100644 (file)
@@ -14,6 +14,7 @@ import os
 import platform
 import pprint
 import subprocess
+import xml.etree.ElementTree as ET
 
 from apex.common import utils
 from apex.virtual import configure_vm as vm_lib
@@ -26,6 +27,28 @@ DEFAULT_PASS = 'password'
 DEFAULT_VIRT_IP = '192.168.122.1'
 
 
+def get_virt_ip():
+    try:
+        virsh_net_xml = subprocess.check_output(['virsh', 'net-dumpxml',
+                                                 'default'],
+                                                stderr=subprocess.STDOUT)
+    except subprocess.CalledProcessError:
+        logging.warning('Unable to detect default virsh network IP.  Will '
+                        'use 192.168.122.1')
+        return DEFAULT_VIRT_IP
+
+    tree = ET.fromstring(virsh_net_xml)
+    ip_tag = tree.find('ip')
+    if ip_tag:
+        virsh_ip = ip_tag.get('address')
+        if virsh_ip:
+            logging.debug("Detected virsh default network ip: "
+                          "{}".format(virsh_ip))
+            return virsh_ip
+
+    return DEFAULT_VIRT_IP
+
+
 def generate_inventory(target_file, ha_enabled=False, num_computes=1,
                        controller_ram=DEFAULT_RAM, arch=platform.machine(),
                        compute_ram=DEFAULT_RAM, vcpus=4):
@@ -42,7 +65,7 @@ def generate_inventory(target_file, ha_enabled=False, num_computes=1,
     """
 
     node = {'mac_address': '',
-            'ipmi_ip': DEFAULT_VIRT_IP,
+            'ipmi_ip': get_virt_ip(),
             'ipmi_user': DEFAULT_USER,
             'ipmi_pass': DEFAULT_PASS,
             'pm_type': 'pxe_ipmitool',
@@ -86,7 +109,7 @@ def host_setup(node):
     vbmc_manager = vbmc_lib.VirtualBMCManager()
     for name, port in node.items():
         vbmc_manager.add(username=DEFAULT_USER, password=DEFAULT_PASS,
-                         port=port, address=DEFAULT_VIRT_IP, domain_name=name,
+                         port=port, address=get_virt_ip(), domain_name=name,
                          libvirt_uri='qemu:///system',
                          libvirt_sasl_password=False,
                          libvirt_sasl_username=False)