Merge "[fuel] deploy.sh: Add timeout flag on fuel"
[armband.git] / patches / opnfv-fuel / 0018-virtual_fuel-add-XML-tree-as-attribute-of-VirtualFue.patch
1 From: Josep Puigdemont <josep.puigdemont@enea.com>
2 Date: Wed, 4 May 2016 14:27:23 +0200
3 Subject: [PATCH] virtual_fuel: add XML tree as attribute of VirtualFuel
4
5 Now the VM XML definition tree is an attribute of the object, this way
6 it can be used by all methods without having to re-read the file from
7 the file.
8
9 Methods added:
10 update_vm_template_file: Flushes the contents of the in-memory XML
11     representation of the VM to the backing file.
12
13 del_vm_nics: Deletes all interfaces from the VM
14
15 add_vm_nic: Adds a new NIC to the VM, it now takes the name of the
16     bridge as a parameter.
17
18 Add a function to flush the contents of the in-memory XML representation
19 to the file update_vm_template_file
20
21 Signed-off-by: Josep Puigdemont <josep.puigdemont@enea.com>
22 ---
23  deploy/environments/virtual_fuel.py | 37 +++++++++++++++++++++++++------------
24  1 file changed, 25 insertions(+), 12 deletions(-)
25
26 diff --git a/deploy/environments/virtual_fuel.py b/deploy/environments/virtual_fuel.py
27 index 92a234c..b68577e 100644
28 --- a/deploy/environments/virtual_fuel.py
29 +++ b/deploy/environments/virtual_fuel.py
30 @@ -13,6 +13,7 @@ from execution_environment import ExecutionEnvironment
31  import tempfile
32  import os
33  import re
34 +import time
35  
36  from common import (
37      exec_cmd,
38 @@ -50,28 +51,38 @@ class VirtualFuel(ExecutionEnvironment):
39                                        self.dha.get_node_property(
40                                            self.fuel_node_id, 'libvirtTemplate'))
41          check_file_exists(self.vm_template)
42 +        with open(self.vm_template) as f:
43 +            self.vm_xml = etree.parse(f)
44 +
45 +        self.temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
46 +        self.update_vm_template_file()
47  
48      def __del__(self):
49          delete(self.temp_dir)
50  
51 -    def set_vm_nic(self, temp_vm_file):
52 -        with open(temp_vm_file) as f:
53 -            vm_xml = etree.parse(f)
54 -        interfaces = vm_xml.xpath('/domain/devices/interface')
55 +    def update_vm_template_file(self):
56 +        with open(self.temp_vm_file, "wc") as f:
57 +            self.vm_xml.write(f, pretty_print=True, xml_declaration=True)
58 +
59 +    def del_vm_nics(self):
60 +        interfaces = self.vm_xml.xpath('/domain/devices/interface')
61          for interface in interfaces:
62              interface.getparent().remove(interface)
63 +
64 +    def add_vm_nic(self, bridge):
65          interface = etree.Element('interface')
66          interface.set('type', 'bridge')
67          source = etree.SubElement(interface, 'source')
68 -        source.set('bridge', self.pxe_bridge)
69 +        source.set('bridge', bridge)
70          model = etree.SubElement(interface, 'model')
71          model.set('type', 'virtio')
72 -        devices = vm_xml.xpath('/domain/devices')
73 +
74 +        devices = self.vm_xml.xpath('/domain/devices')
75          if devices:
76              device = devices[0]
77              device.append(interface)
78 -        with open(temp_vm_file, 'w') as f:
79 -            vm_xml.write(f, pretty_print=True, xml_declaration=True)
80 +        else:
81 +            err('No devices!')
82  
83      def create_volume(self, pool, name, su, img_type='qcow2'):
84          log('Creating image using Libvirt volumes in pool %s, name: %s' %
85 @@ -109,11 +120,13 @@ class VirtualFuel(ExecutionEnvironment):
86          disk_size = disk_sizes['fuel']
87          disk_path = self.create_image(disk_path, disk_size)
88  
89 -        temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
90 -        exec_cmd('cp %s %s' % (self.vm_template, temp_vm_file))
91 -        self.set_vm_nic(temp_vm_file)
92 +        self.del_vm_nics()
93 +        self.add_vm_nic(self.pxe_bridge)
94 +        self.update_vm_template_file()
95 +
96          vm_definition_overwrite = self.dha.get_vm_definition('fuel')
97 -        self.define_vm(self.vm_name, temp_vm_file, disk_path,
98 +
99 +        self.define_vm(self.vm_name, self.temp_vm_file, disk_path,
100                         vm_definition_overwrite)
101  
102      def setup_environment(self):