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