Merge "Smaller non-HA virtual deployment template"
[fuel.git] / deploy / cloud / configure_nodes.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 import yaml
12 import io
13 import glob
14
15 from common import (
16     exec_cmd,
17     check_file_exists,
18     log,
19     backup,
20 )
21
22
23 class ConfigureNodes(object):
24
25     def __init__(self, yaml_config_dir, env_id, node_id_roles_dict, dea):
26         self.yaml_config_dir = yaml_config_dir
27         self.env_id = env_id
28         self.node_id_roles_dict = node_id_roles_dict
29         self.dea = dea
30
31     def config_nodes(self):
32         log('Configure nodes')
33         for node_id, roles_blade in self.node_id_roles_dict.iteritems():
34             exec_cmd('fuel node set --node-id %s --role %s --env %s'
35                      % (node_id, roles_blade[0], self.env_id))
36
37         self.download_deployment_config()
38         for node_id, roles_blade in self.node_id_roles_dict.iteritems():
39             self.download_interface_config(node_id)
40             self.modify_node_interface(node_id, roles_blade)
41             self.modify_node_network_schemes(node_id, roles_blade)
42             self.upload_interface_config(node_id)
43         self.upload_deployment_config()
44
45     def modify_node_network_schemes(self, node_id, roles_blade):
46         log('Modify network transformations for node %s' % node_id)
47         type = self.dea.get_node_property(roles_blade[1], 'transformations')
48         transformations = self.dea.get_property(type)
49         deployment_dir = '%s/deployment_%s' % (
50             self.yaml_config_dir, self.env_id)
51         backup(deployment_dir)
52         for node_file in glob.glob(deployment_dir + '/*_%s.yaml' % node_id):
53             with io.open(node_file) as stream:
54                 node = yaml.load(stream)
55
56             node['network_scheme'].update(transformations)
57
58             with io.open(node_file, 'w') as stream:
59                 yaml.dump(node, stream, default_flow_style=False)
60
61     def download_deployment_config(self):
62         log('Download deployment config for environment %s' % self.env_id)
63         exec_cmd('fuel deployment --env %s --default --dir %s'
64                  % (self.env_id, self.yaml_config_dir))
65
66     def upload_deployment_config(self):
67         log('Upload deployment config for environment %s' % self.env_id)
68         exec_cmd('fuel deployment --env %s --upload --dir %s'
69                  % (self.env_id, self.yaml_config_dir))
70
71     def download_interface_config(self, node_id):
72         log('Download interface config for node %s' % node_id)
73         exec_cmd('fuel node --env %s --node %s --network --download '
74                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
75
76     def upload_interface_config(self, node_id):
77         log('Upload interface config for node %s' % node_id)
78         exec_cmd('fuel node --env %s --node %s --network --upload '
79                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
80
81     def modify_node_interface(self, node_id, roles_blade):
82         log('Modify interface config for node %s' % node_id)
83         interface_yaml = ('%s/node_%s/interfaces.yaml'
84                           % (self.yaml_config_dir, node_id))
85         check_file_exists(interface_yaml)
86         backup('%s/node_%s' % (self.yaml_config_dir, node_id))
87
88         with io.open(interface_yaml) as stream:
89             interfaces = yaml.load(stream)
90
91         net_name_id = {}
92         for interface in interfaces:
93             for network in interface['assigned_networks']:
94                 net_name_id[network['name']] = network['id']
95
96         type = self.dea.get_node_property(roles_blade[1], 'interfaces')
97         interface_config = self.dea.get_property(type)
98
99         for interface in interfaces:
100             interface['assigned_networks'] = []
101             if interface['name'] in interface_config:
102                 for net_name in interface_config[interface['name']]:
103                     net = {}
104                     net['id'] = net_name_id[net_name]
105                     net['name'] = net_name
106                     interface['assigned_networks'].append(net)
107
108         with io.open(interface_yaml, 'w') as stream:
109             yaml.dump(interfaces, stream, default_flow_style=False)