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)
61 def set_vm_nic(self, temp_vm_file):
62 with open(temp_vm_file) as f:
63 vm_xml = etree.parse(f)
64 interfaces = vm_xml.xpath('/domain/devices/interface')
65 for interface in interfaces:
66 interface.getparent().remove(interface)
67 interface = etree.Element('interface')
68 interface.set('type', 'bridge')
69 source = etree.SubElement(interface, 'source')
70 source.set('bridge', self.pxe_bridge)
71 model = etree.SubElement(interface, 'model')
72 model.set('type', 'virtio')
73 devices = vm_xml.xpath('/domain/devices')
76 device.append(interface)
77 with open(temp_vm_file, 'w') as f:
78 vm_xml.write(f, pretty_print=True, xml_declaration=True)
80 def create_volume(self, pool, name, su, img_type='qcow2'):
81 log('Creating image using Libvirt volumes in pool %s, name: %s' %
83 size, unit = get_size_and_unit(su)
85 err('Could not determine size and unit of %s' % s)
87 vol_xml = VOL_XML_TEMPLATE.format(name=name, unit=unit, size=size,
89 fname = os.path.join(self.temp_dir, '%s_vol.xml' % name)
90 with file(fname, 'w') as f:
93 exec_cmd('virsh vol-create --pool %s %s' % (pool, fname))
94 vol_path = exec_cmd('virsh vol-path --pool %s %s' % (pool, name))
100 def create_image(self, disk_path, disk_size):
101 if os.environ.get('LIBVIRT_DEFAULT_URI') == None:
102 exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))
104 pool = DEFAULT_POOL # FIXME
105 name = os.path.basename(disk_path)
106 disk_path = self.create_volume(pool, name, disk_size)
111 stamp = time.strftime("%Y%m%d%H%M%S")
112 disk_path = '%s/%s-%s.raw' % (self.storage_dir, self.vm_name, stamp)
113 disk_sizes = self.dha.get_disks()
114 disk_size = disk_sizes['fuel']
115 disk_path = self.create_image(disk_path, disk_size)
117 temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
118 exec_cmd('cp %s %s' % (self.vm_template, temp_vm_file))
119 self.set_vm_nic(temp_vm_file)
120 vm_definition_overwrite = self.dha.get_vm_definition('fuel')
121 self.define_vm(self.vm_name, temp_vm_file, disk_path,
122 vm_definition_overwrite)
124 def setup_environment(self):
126 self.cleanup_environment()
129 def cleanup_environment(self):
130 self.delete_vm(self.fuel_node_id)