Updates docs for SR1 with final revision
[genesis.git] / fuel / deploy / environments / libvirt_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 import glob
13
14 import common
15 from execution_environment import ExecutionEnvironment
16
17 exec_cmd = common.exec_cmd
18 err = common.err
19 log = common.log
20 check_dir_exists = common.check_dir_exists
21 check_file_exists = common.check_file_exists
22 check_if_root = common.check_if_root
23
24
25 class LibvirtEnvironment(ExecutionEnvironment):
26
27     def __init__(self, storage_dir, dha_file, dea, root_dir):
28         super(LibvirtEnvironment, self).__init__(
29             storage_dir, dha_file, root_dir)
30         self.dea = dea
31         self.network_dir = '%s/%s' % (self.root_dir,
32                                       self.dha.get_virt_net_conf_dir())
33         self.node_ids = self.dha.get_all_node_ids()
34         self.net_names = self.collect_net_names()
35
36     def create_storage(self, node_id, disk_path, disk_sizes):
37         if node_id == self.fuel_node_id:
38             disk_size = disk_sizes['fuel']
39         else:
40             roles = self.dea.get_node_role(node_id)
41             role = 'controller' if 'controller' in roles else 'compute'
42             disk_size = disk_sizes[role]
43         exec_cmd('fallocate -l %s %s' % (disk_size, disk_path))
44
45     def create_vms(self):
46         temp_dir = exec_cmd('mktemp -d')
47         disk_sizes = self.dha.get_disks()
48         for node_id in self.node_ids:
49             vm_name = self.dha.get_node_property(node_id, 'libvirtName')
50             vm_template = '%s/%s' % (self.root_dir,
51                                      self.dha.get_node_property(
52                                          node_id, 'libvirtTemplate'))
53             check_file_exists(vm_template)
54             disk_path = '%s/%s.raw' % (self.storage_dir, vm_name)
55             self.create_storage(node_id, disk_path, disk_sizes)
56             temp_vm_file = '%s/%s' % (temp_dir, vm_name)
57             exec_cmd('cp %s %s' % (vm_template, temp_vm_file))
58             self.define_vm(vm_name, temp_vm_file, disk_path)
59         exec_cmd('rm -fr %s' % temp_dir)
60
61     def start_vms(self):
62         for node_id in self.node_ids:
63             self.dha.node_power_on(node_id)
64
65     def create_networks(self):
66         for net_file in glob.glob('%s/*' % self.network_dir):
67             exec_cmd('virsh net-define %s' % net_file)
68         for net in self.net_names:
69             log('Creating network %s' % net)
70             exec_cmd('virsh net-autostart %s' % net)
71             exec_cmd('virsh net-start %s' % net)
72
73     def delete_networks(self):
74         for net in self.net_names:
75             log('Deleting network %s' % net)
76             exec_cmd('virsh net-destroy %s' % net, False)
77             exec_cmd('virsh net-undefine %s' % net, False)
78
79     def get_net_name(self, net_file):
80         with open(net_file) as f:
81             net_xml = etree.parse(f)
82             name_list = net_xml.xpath('/network/name')
83             for name in name_list:
84                 net_name = name.text
85         return net_name
86
87     def collect_net_names(self):
88         net_list = []
89         for net_file in glob.glob('%s/*' % self.network_dir):
90             name = self.get_net_name(net_file)
91             net_list.append(name)
92         return net_list
93
94     def delete_vms(self):
95         for node_id in self.node_ids:
96             self.delete_vm(node_id)
97
98     def setup_environment(self):
99         check_dir_exists(self.network_dir)
100         self.cleanup_environment()
101         self.create_networks()
102         self.create_vms()
103         self.start_vms()
104
105     def cleanup_environment(self):
106         self.delete_vms()
107         self.delete_networks()