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 %s' % vm_name, False)
50 for file in disk_files:
53 def define_vm(self, vm_name, temp_vm_file, disk_path):
54 log('Creating VM %s with disks %s' % (vm_name, disk_path))
55 with open(temp_vm_file) as f:
56 vm_xml = etree.parse(f)
57 names = vm_xml.xpath('/domain/name')
60 uuids = vm_xml.xpath('/domain/uuid')
62 uuid.getparent().remove(uuid)
63 disks = vm_xml.xpath('/domain/devices/disk')
65 if (disk.get('type') == 'file' and
66 disk.get('device') == 'disk'):
67 sources = disk.xpath('source')
68 for source in sources:
70 source = etree.Element('source')
71 source.set('file', disk_path)
73 with open(temp_vm_file, 'w') as f:
74 vm_xml.write(f, pretty_print=True, xml_declaration=True)
75 exec_cmd('virsh define %s' % temp_vm_file)