Use pxe bridge to replace the bridge in VM template 15/42915/1
authorAlex Yang <yangyang1@zte.com.cn>
Wed, 13 Sep 2017 13:10:40 +0000 (21:10 +0800)
committerZhijiang Hu <hu.zhijiang@zte.com.cn>
Mon, 25 Sep 2017 06:07:18 +0000 (06:07 +0000)
1. Eliminate hard code about pxe bridge
2. Replace the bridge in daisy VM template with the pxe bridge
provided by jenkins

Change-Id: I7a8463181847417d10705ce9ff4db6e68d505c6d
Signed-off-by: Alex Yang <yangyang1@zte.com.cn>
(cherry picked from commit 56975272e92efa3f5f6f719028dc147c1d90e9ce)

ci/deploy/deploy.sh
deploy/deploy.py
deploy/environment.py
deploy/libvirt_utils.py

index e60cdd5..2b987f7 100755 (executable)
@@ -365,6 +365,15 @@ function clean_up_target_vnetworks()
     done
 }
 
+function update_pxe_bridge()
+{
+    bridge_name=$(grep "<source * bridge" $BMDEPLOY_DAISY_SERVER_VM | awk -F "'" '{print $2}') || True
+    if [ ${bridge_name} ] && [ ${bridge_name} != ${BRIDGE} ] && [ ! -z ${bridge_name} ]; then
+        echo "Use $BRIDGE to replace the bridge in $BMDEPLOY_DAISY_SERVER_VM"
+        sed -i -e "/source * bridge/{s/source.*$/source bridge=\'$BRIDGE\'\/>/;}" $BMDEPLOY_DAISY_SERVER_VM
+    fi
+}
+
 function create_daisy_vm_and_networks()
 {
     echo "====== Create Daisy VM ======"
@@ -372,6 +381,7 @@ function create_daisy_vm_and_networks()
     if [ $IS_BARE == 0 ];then
         create_node $VMDELOY_DAISY_SERVER_NET daisy1 $VMDEPLOY_DAISY_SERVER_VM daisy
     else
+        update_pxe_bridge
         virsh define $BMDEPLOY_DAISY_SERVER_VM
         virsh start daisy
     fi
index 71c3974..42a9d2f 100644 (file)
 # TODO:
 # [ ] 1. specify VM templates (Server, Controller & Compute) in deploy.yml
 # [ ] 2. specify network templates in deploy.yml
-# [ ] 3. specify adapter(ipmi, libvirt) in deploy.yml
-# [ ] 4. get ipmi user/password from PDF (Pod Descriptor File)
-# [ ] 5. get pxe bridge from jjb
-# [ ] 6. enlarge the vm size of Controller & Compute in deploy.yml
-# [ ] 7. support scenarios options and configuration
+# [x] 3. specify adapter(ipmi, libvirt) in deploy.yml
+# [x] 4. get ipmi user/password from PDF (Pod Descriptor File)
+# [x] 5. get pxe bridge from jjb
+# [x] 6. enlarge the vm size of Controller & Compute in deploy.yml
+# [x] 7. support scenarios options and configuration
 ##############################################################################
 
 import argparse
@@ -85,11 +85,11 @@ class DaisyDeployment(object):
         self.adapter = self._get_adapter_info()
         LI('The adapter is %s' % self.adapter)
 
-        # TODO: modify the jjb code to provide bridge name
+        # Virtual deployment always uses 'daisy1' as default bridge.
         if self.adapter == 'libvirt':
             self.pxe_bridge = 'daisy1'
-        else:
-            self.pxe_bridge = 'br7'
+
+        LI('Use %s as the bridge name in daisy deployment.' % self.pxe_bridge)
 
         self.daisy_server_info = self._get_daisy_server_info()
 
index 5371e6c..1424054 100644 (file)
@@ -135,7 +135,6 @@ class BareMetalEnvironment(DaisyEnvironmentBase):
 
     def create_daisy_server_vm(self):
         # TODO: refactor the structure of deploy.yml, add VM template param of Daisy Server
-        #       add self.pxe_bridge into the vm template
         if 'template' in self.deploy_struct:
             # get VM name of Daisy Server from the template
             template = self.deploy_struct['template']
@@ -144,7 +143,8 @@ class BareMetalEnvironment(DaisyEnvironmentBase):
 
         create_vm(template,
                   name=self.daisy_server_info['name'],
-                  disks=[self.daisy_server_info['image']])
+                  disks=[self.daisy_server_info['image']],
+                  physical_bridge=self.pxe_bridge)
 
     def reboot_nodes(self, boot_dev=None):
         for node in self.deploy_struct['hosts']:
@@ -203,7 +203,6 @@ class VirtualEnvironment(DaisyEnvironmentBase):
 
     def create_daisy_server_vm(self):
         # TODO: refactor the structure of deploy.yml, add VM template param of Daisy Server
-        #       add self.pxe_bridge into the vm template
         if 'template' in self.deploy_struct:
             # get VM name of Daisy Server from the template
             template = self.deploy_struct['template']
index 8e4523d..3b9eae1 100644 (file)
@@ -69,6 +69,24 @@ def modify_vm_disk_file(root, disks):
         devices.append(disk)
 
 
+def modify_vm_bridge(root, bridge):
+    devices = root.find('./devices')
+    for interface in devices.findall('interface'):
+        source = interface.find('source')
+        if interface.attrib.get('type', None) == 'bridge' \
+                and source is not None \
+                and source.attrib.get('bridge', None) == bridge:
+            # pxebr is already in the VM template
+            return
+
+    for interface in devices.findall('interface'):
+        devices.remove(interface)
+
+    interface = ET.Element('interface', attrib={'type': 'bridge'})
+    interface.append(ET.Element('source', attrib={'bridge': bridge}))
+    devices.append(interface)
+
+
 def create_virtual_disk(disk_file, size):
     LI('Create virtual disk file %s size %d GB' % (disk_file, size))
     cmd = 'qemu-img create -f qcow2 {disk_file} {size}G'.format(
@@ -79,16 +97,18 @@ def create_virtual_disk(disk_file, size):
         err_exit('Fail to create qemu image !')
 
 
-def create_vm(template, name=None, disks=None):
+def create_vm(template, name=None, disks=None, physical_bridge=None):
     LI('Begin to create VM %s' % template)
 
-    if name or disks:
+    if name or disks or physical_bridge:
         tree = ET.ElementTree(file=template)
         root = tree.getroot()
         if name:
             modify_vm_name(root, name)
         if disks:
             modify_vm_disk_file(root, disks)
+        if physical_bridge:
+            modify_vm_bridge(root, physical_bridge)
 
         temp_file = path_join(WORKSPACE, 'tmp.xml')
         tree.write(temp_file)