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 ###############################################################################
11 from lxml import etree
12 from dha_adapters.libvirt_adapter import LibvirtAdapter
21 class ExecutionEnvironment(object):
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()
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)
35 self.undefine_vm_delete_disk(r, vm_name)
37 def undefine_vm_delete_disk(self, printout, vm_name):
39 xml_dump = etree.fromstring(printout, self.parser)
40 disks = xml_dump.xpath('/domain/devices/disk')
42 sources = disk.xpath('source')
43 for source in sources:
44 source_file = source.get('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:
53 def overwrite_xml(self, vm_xml, vm_definition_overwrite):
54 if not vm_definition_overwrite:
56 for key, value in vm_definition_overwrite.iteritems():
57 if key == 'attribute_equlas':
60 vm_xml.text = str(value)
62 if key == 'attribute':
63 for attr_key, attr_value in value.iteritems():
64 vm_xml.set(attr_key, str(attr_value))
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):
75 self.overwrite_xml(xml_element, value)
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')
85 uuids = vm_xml.xpath('/domain/uuid')
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')
92 if (disk.get('type') == 'file' and
93 disk.get('device') == 'disk'):
94 sources = disk.xpath('source')
95 for source in sources:
97 source = etree.Element('source')
98 source.set('file', disk_path)
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)