ac5fc53565592451cabb59aa1d11b9b7674cf48d
[fuel.git] / 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 from execution_environment import ExecutionEnvironment
13 import tempfile
14
15 from common import (
16     exec_cmd,
17     check_file_exists,
18     check_if_root,
19     delete,
20 )
21
22
23 class VirtualFuel(ExecutionEnvironment):
24
25     def __init__(self, storage_dir, pxe_bridge, dha_file, root_dir):
26         super(VirtualFuel, self).__init__(storage_dir, dha_file, root_dir)
27         self.pxe_bridge = pxe_bridge
28         self.temp_dir = tempfile.mkdtemp()
29         self.vm_name = self.dha.get_node_property(self.fuel_node_id,
30                                                   'libvirtName')
31         self.vm_template = '%s/%s' % (self.root_dir,
32                                       self.dha.get_node_property(
33                                           self.fuel_node_id, 'libvirtTemplate'))
34         check_file_exists(self.vm_template)
35
36     def __del__(self):
37         delete(self.temp_dir)
38
39     def set_vm_nic(self, temp_vm_file):
40         with open(temp_vm_file) as f:
41             vm_xml = etree.parse(f)
42         interfaces = vm_xml.xpath('/domain/devices/interface')
43         for interface in interfaces:
44             interface.getparent().remove(interface)
45         interface = etree.Element('interface')
46         interface.set('type', 'bridge')
47         source = etree.SubElement(interface, 'source')
48         source.set('bridge', self.pxe_bridge)
49         model = etree.SubElement(interface, 'model')
50         model.set('type', 'virtio')
51         devices = vm_xml.xpath('/domain/devices')
52         if devices:
53             device = devices[0]
54             device.append(interface)
55         with open(temp_vm_file, 'w') as f:
56             vm_xml.write(f, pretty_print=True, xml_declaration=True)
57
58     def create_image(self, disk_path, disk_size):
59         exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))
60
61     def create_vm(self):
62         disk_path = '%s/%s.raw' % (self.storage_dir, self.vm_name)
63         disk_sizes = self.dha.get_disks()
64         disk_size = disk_sizes['fuel']
65         self.create_image(disk_path, disk_size)
66
67         temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
68         exec_cmd('cp %s %s' % (self.vm_template, temp_vm_file))
69         self.set_vm_nic(temp_vm_file)
70         vm_definition_overwrite = self.dha.get_vm_definition('fuel')
71         self.define_vm(self.vm_name, temp_vm_file, disk_path,
72                        vm_definition_overwrite)
73
74     def setup_environment(self):
75         check_if_root()
76         self.cleanup_environment()
77         self.create_vm()
78
79     def cleanup_environment(self):
80         self.delete_vm(self.fuel_node_id)