Merge "Sync with recent changes in fuel-main" into stable/brahmaputra
[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 %s' % vm_name, False)
50         for file in disk_files:
51             delete(file)
52
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')
58         for name in names:
59             name.text = vm_name
60         uuids = vm_xml.xpath('/domain/uuid')
61         for uuid in uuids:
62             uuid.getparent().remove(uuid)
63         disks = vm_xml.xpath('/domain/devices/disk')
64         for disk in disks:
65             if (disk.get('type') == 'file' and
66                 disk.get('device') == 'disk'):
67                 sources = disk.xpath('source')
68                 for source in sources:
69                     disk.remove(source)
70                 source = etree.Element('source')
71                 source.set('file', disk_path)
72                 disk.append(source)
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)