1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ###############################################################################
11 from lxml import etree
12 from execution_environment import ExecutionEnvironment
26 VOL_XML_TEMPLATE = '''<volume type='file'>
28 <capacity unit='{unit}'>{size!s}</capacity>
30 <format type='{format_type}'/>
34 DEFAULT_POOL = 'jenkins'
36 def get_size_and_unit(s):
37 p = re.compile('^(\d+)\s*(\D+)')
45 class VirtualFuel(ExecutionEnvironment):
47 def __init__(self, storage_dir, pxe_bridge, dha_file, root_dir):
48 super(VirtualFuel, self).__init__(storage_dir, dha_file, root_dir)
49 self.pxe_bridge = pxe_bridge
50 self.temp_dir = tempfile.mkdtemp()
51 self.vm_name = self.dha.get_node_property(self.fuel_node_id,
53 self.vm_template = '%s/%s' % (self.root_dir,
54 self.dha.get_node_property(
55 self.fuel_node_id, 'libvirtTemplate'))
56 check_file_exists(self.vm_template)
57 with open(self.vm_template) as f:
58 self.vm_xml = etree.parse(f)
60 self.temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
61 self.update_vm_template_file()
66 def update_vm_template_file(self):
67 with open(self.temp_vm_file, "wc") as f:
68 self.vm_xml.write(f, pretty_print=True, xml_declaration=True)
70 def del_vm_nics(self):
71 interfaces = self.vm_xml.xpath('/domain/devices/interface')
72 for interface in interfaces:
73 interface.getparent().remove(interface)
75 def add_vm_nic(self, bridge):
76 interface = etree.Element('interface')
77 interface.set('type', 'bridge')
78 source = etree.SubElement(interface, 'source')
79 source.set('bridge', bridge)
80 model = etree.SubElement(interface, 'model')
81 model.set('type', 'virtio')
83 devices = self.vm_xml.xpath('/domain/devices')
86 device.append(interface)
90 def create_volume(self, pool, name, su, img_type='qcow2'):
91 log('Creating image using Libvirt volumes in pool %s, name: %s' %
93 size, unit = get_size_and_unit(su)
95 err('Could not determine size and unit of %s' % s)
97 vol_xml = VOL_XML_TEMPLATE.format(name=name, unit=unit, size=size,
99 fname = os.path.join(self.temp_dir, '%s_vol.xml' % name)
100 with file(fname, 'w') as f:
103 exec_cmd('virsh vol-create --pool %s %s' % (pool, fname))
104 vol_path = exec_cmd('virsh vol-path --pool %s %s' % (pool, name))
110 def create_image(self, disk_path, disk_size):
111 if os.environ.get('LIBVIRT_DEFAULT_URI') == None:
112 exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))
114 pool = DEFAULT_POOL # FIXME
115 name = os.path.basename(disk_path)
116 disk_path = self.create_volume(pool, name, disk_size)
121 stamp = time.strftime("%Y%m%d%H%M%S")
122 disk_path = '%s/%s-%s.raw' % (self.storage_dir, self.vm_name, stamp)
123 disk_sizes = self.dha.get_disks()
124 disk_size = disk_sizes['fuel']
125 disk_path = self.create_image(disk_path, disk_size)
128 for bridge in self.pxe_bridge:
129 self.add_vm_nic(bridge)
130 self.update_vm_template_file()
132 vm_definition_overwrite = self.dha.get_vm_definition('fuel')
134 self.define_vm(self.vm_name, self.temp_vm_file, disk_path,
135 vm_definition_overwrite)
137 def setup_environment(self):
139 self.cleanup_environment()
142 def cleanup_environment(self):
143 self.delete_vm(self.fuel_node_id)