[ODL] Use OpenDaylight Boron
[fuel.git] / deploy / environments / execution_environment.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 dha_adapters.libvirt_adapter import LibvirtAdapter
13
14 from common import (
15     exec_cmd,
16     log,
17     delete,
18 )
19
20
21 class ExecutionEnvironment(object):
22
23     def __init__(self, storage_dir, dha_file, root_dir):
24         self.storage_dir = storage_dir
25         self.dha = LibvirtAdapter(dha_file)
26         self.root_dir = root_dir
27         self.parser = etree.XMLParser(remove_blank_text=True)
28         self.fuel_node_id = self.dha.get_fuel_node_id()
29
30     def delete_vm(self, node_id):
31         vm_name = self.dha.get_node_property(node_id, 'libvirtName')
32         r, c = exec_cmd('virsh dumpxml %s' % vm_name, False)
33         if c:
34             return
35         self.undefine_vm_delete_disk(r, vm_name)
36
37     def undefine_vm_delete_disk(self, printout, vm_name):
38         disk_files = []
39         xml_dump = etree.fromstring(printout, self.parser)
40         disks = xml_dump.xpath('/domain/devices/disk')
41         for disk in disks:
42             sources = disk.xpath('source')
43             for source in sources:
44                 source_file = source.get('file')
45                 if source_file:
46                     disk_files.append(source_file)
47         log('Deleting VM %s with disks %s' % (vm_name, disk_files))
48         exec_cmd('virsh destroy %s' % vm_name, False)
49         exec_cmd('virsh undefine --managed-save --remove-all-storage %s' % vm_name, False)
50         for file in disk_files:
51             delete(file)
52
53     def overwrite_xml(self, vm_xml, vm_definition_overwrite):
54         if not vm_definition_overwrite:
55             return
56         for key, value in vm_definition_overwrite.iteritems():
57             if key == 'attribute_equlas':
58                 continue
59             if key == 'value':
60                 vm_xml.text = str(value)
61                 return
62             if key == 'attribute':
63                 for attr_key, attr_value in value.iteritems():
64                     vm_xml.set(attr_key, str(attr_value))
65                 return
66
67             if isinstance(value, dict):
68                 only_when_attribute = value.get('attribute_equlas')
69             for xml_element in vm_xml.xpath(key):
70                 if only_when_attribute:
71                     for attr_key, attr_value in \
72                             only_when_attribute.iteritems():
73                         if attr_value != xml_element.get(attr_key):
74                             continue
75                 self.overwrite_xml(xml_element, value)
76
77     def define_vm(self, vm_name, temp_vm_file, disk_path,
78                   vm_definition_overwrite):
79         log('Creating VM %s with disks %s' % (vm_name, disk_path))
80         with open(temp_vm_file) as f:
81             vm_xml = etree.parse(f)
82         names = vm_xml.xpath('/domain/name')
83         for name in names:
84             name.text = vm_name
85         uuids = vm_xml.xpath('/domain/uuid')
86         for uuid in uuids:
87             uuid.getparent().remove(uuid)
88         self.overwrite_xml(vm_xml.xpath('/domain')[0],
89                            vm_definition_overwrite)
90         disks = vm_xml.xpath('/domain/devices/disk')
91         for disk in disks:
92             if (disk.get('type') == 'file' and
93                     disk.get('device') == 'disk'):
94                 sources = disk.xpath('source')
95                 for source in sources:
96                     disk.remove(source)
97                 source = etree.Element('source')
98                 source.set('file', disk_path)
99                 disk.append(source)
100         with open(temp_vm_file, 'w') as f:
101             vm_xml.write(f, pretty_print=True, xml_declaration=True)
102         exec_cmd('virsh define %s' % temp_vm_file)