Merge "Corrected links associated with release docs. To be updated along with the...
[genesis.git] / fuel / deploy / environments / virtual_fuel.py
1 from lxml import etree
2
3 import common
4 from execution_environment import ExecutionEnvironment
5
6 exec_cmd = common.exec_cmd
7 log = common.log
8 check_file_exists = common.check_file_exists
9 check_if_root = common.check_if_root
10
11 class VirtualFuel(ExecutionEnvironment):
12
13     def __init__(self, storage_dir, pxe_bridge, dha_file, root_dir):
14         super(VirtualFuel, self).__init__(
15             storage_dir, dha_file, root_dir)
16         self.pxe_bridge = pxe_bridge
17
18     def set_vm_nic(self, temp_vm_file):
19         with open(temp_vm_file) as f:
20             vm_xml = etree.parse(f)
21         interfaces = vm_xml.xpath('/domain/devices/interface')
22         for interface in interfaces:
23             interface.getparent().remove(interface)
24         interface = etree.Element('interface')
25         interface.set('type', 'bridge')
26         source = etree.SubElement(interface, 'source')
27         source.set('bridge', self.pxe_bridge)
28         model = etree.SubElement(interface, 'model')
29         model.set('type', 'virtio')
30         devices = vm_xml.xpath('/domain/devices')
31         if devices:
32             device = devices[0]
33             device.append(interface)
34         with open(temp_vm_file, 'w') as f:
35             vm_xml.write(f, pretty_print=True, xml_declaration=True)
36
37     def create_vm(self):
38         temp_dir = exec_cmd('mktemp -d')
39         vm_name = self.dha.get_node_property(self.fuel_node_id, 'libvirtName')
40         vm_template = '%s/%s' % (self.root_dir,
41                                  self.dha.get_node_property(
42                                      self.fuel_node_id, 'libvirtTemplate'))
43         check_file_exists(vm_template)
44         disk_path = '%s/%s.raw' % (self.storage_dir, vm_name)
45         disk_sizes = self.dha.get_disks()
46         disk_size = disk_sizes['fuel']
47         exec_cmd('fallocate -l %s %s' % (disk_size, disk_path))
48         temp_vm_file = '%s/%s' % (temp_dir, vm_name)
49         exec_cmd('cp %s %s' % (vm_template, temp_vm_file))
50         self.set_vm_nic(temp_vm_file)
51         self.define_vm(vm_name, temp_vm_file, disk_path)
52         exec_cmd('rm -fr %s' % temp_dir)
53
54     def setup_environment(self):
55         check_if_root()
56         self.cleanup_environment()
57         self.create_vm()
58
59     def cleanup_environment(self):
60         self.delete_vm(self.fuel_node_id)