Updates docs for SR1 with final revision
[genesis.git] / fuel / deploy / environments / virtual_fuel.py
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 ###############################################################################
9
10
11 from lxml import etree
12
13 import common
14 from execution_environment import ExecutionEnvironment
15
16 exec_cmd = common.exec_cmd
17 log = common.log
18 check_file_exists = common.check_file_exists
19 check_if_root = common.check_if_root
20
21
22 class VirtualFuel(ExecutionEnvironment):
23
24     def __init__(self, storage_dir, pxe_bridge, dha_file, root_dir):
25         super(VirtualFuel, self).__init__(storage_dir, dha_file, root_dir)
26         self.pxe_bridge = pxe_bridge
27
28     def set_vm_nic(self, temp_vm_file):
29         with open(temp_vm_file) as f:
30             vm_xml = etree.parse(f)
31         interfaces = vm_xml.xpath('/domain/devices/interface')
32         for interface in interfaces:
33             interface.getparent().remove(interface)
34         interface = etree.Element('interface')
35         interface.set('type', 'bridge')
36         source = etree.SubElement(interface, 'source')
37         source.set('bridge', self.pxe_bridge)
38         model = etree.SubElement(interface, 'model')
39         model.set('type', 'virtio')
40         devices = vm_xml.xpath('/domain/devices')
41         if devices:
42             device = devices[0]
43             device.append(interface)
44         with open(temp_vm_file, 'w') as f:
45             vm_xml.write(f, pretty_print=True, xml_declaration=True)
46
47     def create_vm(self):
48         temp_dir = exec_cmd('mktemp -d')
49         vm_name = self.dha.get_node_property(self.fuel_node_id, 'libvirtName')
50         vm_template = '%s/%s' % (self.root_dir,
51                                  self.dha.get_node_property(
52                                      self.fuel_node_id, 'libvirtTemplate'))
53         check_file_exists(vm_template)
54         disk_path = '%s/%s.raw' % (self.storage_dir, vm_name)
55         disk_sizes = self.dha.get_disks()
56         disk_size = disk_sizes['fuel']
57         exec_cmd('fallocate -l %s %s' % (disk_size, disk_path))
58         temp_vm_file = '%s/%s' % (temp_dir, vm_name)
59         exec_cmd('cp %s %s' % (vm_template, temp_vm_file))
60         self.set_vm_nic(temp_vm_file)
61         self.define_vm(vm_name, temp_vm_file, disk_path)
62         exec_cmd('rm -fr %s' % temp_dir)
63
64     def setup_environment(self):
65         check_if_root()
66         self.cleanup_environment()
67         self.create_vm()
68
69     def cleanup_environment(self):
70         self.delete_vm(self.fuel_node_id)