VirtualFuel: Add temp_dir and vm_name attributes
[fuel.git] / deploy / environments / virtual_fuel.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 execution_environment import ExecutionEnvironment
13 import tempfile
14
15 from common import (
16     exec_cmd,
17     check_file_exists,
18     check_if_root,
19     delete,
20 )
21
22
23 class VirtualFuel(ExecutionEnvironment):
24
25     def __init__(self, storage_dir, pxe_bridge, dha_file, root_dir):
26         super(VirtualFuel, self).__init__(storage_dir, dha_file, root_dir)
27         self.pxe_bridge = pxe_bridge
28         self.temp_dir = tempfile.mkdtemp()
29         self.vm_name = self.dha.get_node_property(self.fuel_node_id,
30                                                   'libvirtName')
31
32     def __del__(self):
33         delete(self.temp_dir)
34
35     def set_vm_nic(self, temp_vm_file):
36         with open(temp_vm_file) as f:
37             vm_xml = etree.parse(f)
38         interfaces = vm_xml.xpath('/domain/devices/interface')
39         for interface in interfaces:
40             interface.getparent().remove(interface)
41         interface = etree.Element('interface')
42         interface.set('type', 'bridge')
43         source = etree.SubElement(interface, 'source')
44         source.set('bridge', self.pxe_bridge)
45         model = etree.SubElement(interface, 'model')
46         model.set('type', 'virtio')
47         devices = vm_xml.xpath('/domain/devices')
48         if devices:
49             device = devices[0]
50             device.append(interface)
51         with open(temp_vm_file, 'w') as f:
52             vm_xml.write(f, pretty_print=True, xml_declaration=True)
53
54     def create_vm(self):
55         vm_template = '%s/%s' % (self.root_dir,
56                                  self.dha.get_node_property(
57                                      self.fuel_node_id, 'libvirtTemplate'))
58         check_file_exists(vm_template)
59         disk_path = '%s/%s.raw' % (self.storage_dir, self.vm_name)
60         disk_sizes = self.dha.get_disks()
61         disk_size = disk_sizes['fuel']
62         exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))
63         temp_vm_file = '%s/%s' % (self.temp_dir, self.vm_name)
64         exec_cmd('cp %s %s' % (vm_template, temp_vm_file))
65         self.set_vm_nic(temp_vm_file)
66         vm_definition_overwrite = self.dha.get_vm_definition('fuel')
67         self.define_vm(self.vm_name, temp_vm_file, disk_path,
68                        vm_definition_overwrite)
69
70     def setup_environment(self):
71         check_if_root()
72         self.cleanup_environment()
73         self.create_vm()
74
75     def cleanup_environment(self):
76         self.delete_vm(self.fuel_node_id)