virtual_fuel: prepare class to allow multiple bridges
[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 del_vm_nics(self):
71         interfaces = self.vm_xml.xpath('/domain/devices/interface')
72         for interface in interfaces:
73             interface.getparent().remove(interface)
74
75     def add_vm_nic(self, bridge):
76         interface = etree.Element('interface')
77         interface.set('type', 'bridge')
78         source = etree.SubElement(interface, 'source')
79         source.set('bridge', bridge)
80         model = etree.SubElement(interface, 'model')
81         model.set('type', 'virtio')
82
83         devices = self.vm_xml.xpath('/domain/devices')
84         if devices:
85             device = devices[0]
86             device.append(interface)
87         else:
88             err('No devices!')
89
90     def create_volume(self, pool, name, su, img_type='qcow2'):
91         log('Creating image using Libvirt volumes in pool %s, name: %s' %
92             (pool, name))
93         size, unit = get_size_and_unit(su)
94         if size == None:
95             err('Could not determine size and unit of %s' % s)
96
97         vol_xml = VOL_XML_TEMPLATE.format(name=name, unit=unit, size=size,
98                                           format_type=img_type)
99         fname = os.path.join(self.temp_dir, '%s_vol.xml' % name)
100         with file(fname, 'w') as f:
101             f.write(vol_xml)
102
103         exec_cmd('virsh vol-create --pool %s %s' % (pool, fname))
104         vol_path = exec_cmd('virsh vol-path --pool %s %s' % (pool, name))
105
106         delete(fname)
107
108         return vol_path
109
110     def create_image(self, disk_path, disk_size):
111         if os.environ.get('LIBVIRT_DEFAULT_URI') == None:
112             exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))
113         else:
114             pool = DEFAULT_POOL # FIXME
115             name = os.path.basename(disk_path)
116             disk_path = self.create_volume(pool, name, disk_size)
117
118         return disk_path
119
120     def create_vm(self):
121         stamp = time.strftime("%Y%m%d%H%M%S")
122         disk_path = '%s/%s-%s.raw' % (self.storage_dir, self.vm_name, stamp)
123         disk_sizes = self.dha.get_disks()
124         disk_size = disk_sizes['fuel']
125         disk_path = self.create_image(disk_path, disk_size)
126
127         self.del_vm_nics()
128         self.add_vm_nic(self.pxe_bridge)
129         self.update_vm_template_file()
130
131         vm_definition_overwrite = self.dha.get_vm_definition('fuel')
132
133         self.define_vm(self.vm_name, self.temp_vm_file, disk_path,
134                        vm_definition_overwrite)
135
136     def setup_environment(self):
137         check_if_root()
138         self.cleanup_environment()
139         self.create_vm()
140
141     def cleanup_environment(self):
142         self.delete_vm(self.fuel_node_id)