Updates docs for SR1 with final revision
[genesis.git] / fuel / 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
13 import common
14 from dha_adapters.libvirt_adapter import LibvirtAdapter
15
16 exec_cmd = common.exec_cmd
17 err = common.err
18 log = common.log
19 check_dir_exists = common.check_dir_exists
20 check_file_exists = common.check_file_exists
21 check_if_root = common.check_if_root
22
23
24 class ExecutionEnvironment(object):
25
26     def __init__(self, storage_dir, dha_file, root_dir):
27         self.storage_dir = storage_dir
28         self.dha = LibvirtAdapter(dha_file)
29         self.root_dir = root_dir
30         self.parser = etree.XMLParser(remove_blank_text=True)
31         self.fuel_node_id = self.dha.get_fuel_node_id()
32
33     def delete_vm(self, node_id):
34         vm_name = self.dha.get_node_property(node_id, 'libvirtName')
35         r, c = exec_cmd('virsh dumpxml %s' % vm_name, False)
36         if c:
37             return
38         self.undefine_vm_delete_disk(r, vm_name)
39
40     def undefine_vm_delete_disk(self, printout, vm_name):
41         disk_files = []
42         xml_dump = etree.fromstring(printout, self.parser)
43         disks = xml_dump.xpath('/domain/devices/disk')
44         for disk in disks:
45             sources = disk.xpath('source')
46             for source in sources:
47                 source_file = source.get('file')
48                 if source_file:
49                     disk_files.append(source_file)
50         log('Deleting VM %s with disks %s' % (vm_name, disk_files))
51         exec_cmd('virsh destroy %s' % vm_name, False)
52         exec_cmd('virsh undefine %s' % vm_name, False)
53         for file in disk_files:
54             exec_cmd('rm -f %s' % file)
55
56     def define_vm(self, vm_name, temp_vm_file, disk_path):
57         log('Creating VM %s with disks %s' % (vm_name, disk_path))
58         with open(temp_vm_file) as f:
59             vm_xml = etree.parse(f)
60         names = vm_xml.xpath('/domain/name')
61         for name in names:
62             name.text = vm_name
63         uuids = vm_xml.xpath('/domain/uuid')
64         for uuid in uuids:
65             uuid.getparent().remove(uuid)
66         disks = vm_xml.xpath('/domain/devices/disk')
67         for disk in disks:
68             if (disk.get('type') == 'file' and
69                 disk.get('device') == 'disk'):
70                 sources = disk.xpath('source')
71                 for source in sources:
72                     disk.remove(source)
73                 source = etree.Element('source')
74                 source.set('file', disk_path)
75                 disk.append(source)
76         with open(temp_vm_file, 'w') as f:
77             vm_xml.write(f, pretty_print=True, xml_declaration=True)
78         exec_cmd('virsh define %s' % temp_vm_file)