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