virtual_fuel: add XML tree as attribute of VirtualFuel
[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 import os
15 import re
16 import time
17
18 from common import (
19     exec_cmd,
20     check_file_exists,
21     check_if_root,
22     delete,
23     log,
24 )
25
26 VOL_XML_TEMPLATE = '''<volume type='file'>
27   <name>{name}</name>
28   <capacity unit='{unit}'>{size!s}</capacity>
29   <target>
30     <format type='{format_type}'/>
31   </target>
32 </volume>'''
33
34 DEFAULT_POOL = 'jenkins'
35
36 def get_size_and_unit(s):
37     p = re.compile('^(\d+)\s*(\D+)')
38     m = p.match(s)
39     if m == None:
40         return None, None
41     size = m.groups()[0]
42     unit = m.groups()[1]
43     return size, unit
44
45 class VirtualFuel(ExecutionEnvironment):
46
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,
52                                                   'libvirtName')
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)
59
60         self.temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
61         self.update_vm_template_file()
62
63     def __del__(self):
64         delete(self.temp_dir)
65
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)
69
70     def set_vm_nic(self):
71         interfaces = self.vm_xml.xpath('/domain/devices/interface')
72         for interface in interfaces:
73             interface.getparent().remove(interface)
74         interface = etree.Element('interface')
75         interface.set('type', 'bridge')
76         source = etree.SubElement(interface, 'source')
77         source.set('bridge', self.pxe_bridge)
78         model = etree.SubElement(interface, 'model')
79         model.set('type', 'virtio')
80         devices = self.vm_xml.xpath('/domain/devices')
81         if devices:
82             device = devices[0]
83             device.append(interface)
84
85         self.update_vm_template_file()
86
87     def create_volume(self, pool, name, su, img_type='qcow2'):
88         log('Creating image using Libvirt volumes in pool %s, name: %s' %
89             (pool, name))
90         size, unit = get_size_and_unit(su)
91         if size == None:
92             err('Could not determine size and unit of %s' % s)
93
94         vol_xml = VOL_XML_TEMPLATE.format(name=name, unit=unit, size=size,
95                                           format_type=img_type)
96         fname = os.path.join(self.temp_dir, '%s_vol.xml' % name)
97         with file(fname, 'w') as f:
98             f.write(vol_xml)
99
100         exec_cmd('virsh vol-create --pool %s %s' % (pool, fname))
101         vol_path = exec_cmd('virsh vol-path --pool %s %s' % (pool, name))
102
103         delete(fname)
104
105         return vol_path
106
107     def create_image(self, disk_path, disk_size):
108         if os.environ.get('LIBVIRT_DEFAULT_URI') == None:
109             exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))
110         else:
111             pool = DEFAULT_POOL # FIXME
112             name = os.path.basename(disk_path)
113             disk_path = self.create_volume(pool, name, disk_size)
114
115         return disk_path
116
117     def create_vm(self):
118         stamp = time.strftime("%Y%m%d%H%M%S")
119         disk_path = '%s/%s-%s.raw' % (self.storage_dir, self.vm_name, stamp)
120         disk_sizes = self.dha.get_disks()
121         disk_size = disk_sizes['fuel']
122         disk_path = self.create_image(disk_path, disk_size)
123
124         temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
125         exec_cmd('cp %s %s' % (self.vm_template, temp_vm_file))
126         self.set_vm_nic(temp_vm_file)
127         vm_definition_overwrite = self.dha.get_vm_definition('fuel')
128         self.define_vm(self.vm_name, temp_vm_file, disk_path,
129                        vm_definition_overwrite)
130
131     def setup_environment(self):
132         check_if_root()
133         self.cleanup_environment()
134         self.create_vm()
135
136     def cleanup_environment(self):
137         self.delete_vm(self.fuel_node_id)