Merge "Fix ssl cert upload path"
[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 import copy
11 import io
12
13 import six
14 import yaml
15
16 from common import (
17     exec_cmd,
18     check_file_exists,
19     log,
20     backup,
21 )
22
23
24 class ConfigureNodes(object):
25
26     def __init__(self, yaml_config_dir, env_id, node_id_roles_dict, dea):
27         self.yaml_config_dir = yaml_config_dir
28         self.env_id = env_id
29         self.node_id_roles_dict = node_id_roles_dict
30         self.dea = dea
31
32     def config_nodes(self):
33         log('Configure nodes')
34
35         # Assign nodes to environment with given roles
36         for node_id, roles_blade in self.node_id_roles_dict.iteritems():
37             exec_cmd('fuel node set --node-id %s --role %s --env %s'
38                      % (node_id, roles_blade[0], self.env_id))
39
40         for node_id, roles_blade in self.node_id_roles_dict.iteritems():
41             # Modify interfaces configuration
42             self.download_interface_config(node_id)
43             self.modify_node_interface(node_id, roles_blade)
44             self.upload_interface_config(node_id)
45             # Modify node attributes
46             self.download_attributes(node_id)
47             self.modify_node_attributes(node_id, roles_blade)
48             self.upload_attributes(node_id)
49
50         # Currently not used, we use default deployment facts
51         # which are generated by fuel based on type segmentation
52         # and network to nic assignment
53         #
54         # Download our modified deployment configuration, which includes our
55         # changes to network topology etc.
56         #self.download_deployment_config()
57         #for node_id, roles_blade in self.node_id_roles_dict.iteritems():
58         #    self.modify_node_network_schemes(node_id, roles_blade)
59         #self.upload_deployment_config()
60
61     def modify_node_network_schemes(self, node_id, roles_blade):
62         log('Modify network transformations for node %s' % node_id)
63         type = self.dea.get_node_property(roles_blade[1], 'transformations')
64         transformations = self.dea.get_property(type)
65         deployment_dir = '%s/deployment_%s' % (
66             self.yaml_config_dir, self.env_id)
67         backup(deployment_dir)
68         node_file = ('%s/%s.yaml' % (deployment_dir, node_id))
69         with io.open(node_file) as stream:
70             node = yaml.load(stream)
71
72         node['network_scheme'].update(transformations)
73
74         with io.open(node_file, 'w') as stream:
75             yaml.dump(node, stream, default_flow_style=False)
76
77     def download_deployment_config(self):
78         log('Download deployment config for environment %s' % self.env_id)
79         exec_cmd('fuel deployment --env %s --default --dir %s'
80                  % (self.env_id, self.yaml_config_dir))
81
82     def upload_deployment_config(self):
83         log('Upload deployment config for environment %s' % self.env_id)
84         exec_cmd('fuel deployment --env %s --upload --dir %s'
85                  % (self.env_id, self.yaml_config_dir))
86
87     def download_interface_config(self, node_id):
88         log('Download interface config for node %s' % node_id)
89         exec_cmd('fuel node --env %s --node %s --network --download '
90                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
91
92     def upload_interface_config(self, node_id):
93         log('Upload interface config for node %s' % node_id)
94         exec_cmd('fuel node --env %s --node %s --network --upload '
95                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
96
97     def download_attributes(self, node_id):
98         log('Download attributes for node %s' % node_id)
99         exec_cmd('fuel node --env %s --node %s --attributes --download '
100                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
101
102     def upload_attributes(self, node_id):
103         log('Upload attributes for node %s' % node_id)
104         exec_cmd('fuel node --env %s --node %s --attributes --upload '
105                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
106
107     def modify_node_attributes(self, node_id, roles_blade):
108         log('Modify attributes for node {0}'.format(node_id))
109         dea_key = self.dea.get_node_property(roles_blade[1], 'attributes')
110         if not dea_key:
111             # Node attributes are not overridden. Nothing to do.
112             return
113         new_attributes = self.dea.get_property(dea_key)
114         attributes_yaml = ('%s/node_%s/attributes.yaml'
115                            % (self.yaml_config_dir, node_id))
116         check_file_exists(attributes_yaml)
117         backup('%s/node_%s' % (self.yaml_config_dir, node_id))
118
119         with open(attributes_yaml) as stream:
120             attributes = yaml.load(stream)
121         result_attributes = self._merge_dicts(attributes, new_attributes)
122
123         with open(attributes_yaml, 'w') as stream:
124             yaml.dump(result_attributes, stream, default_flow_style=False)
125
126     # interface configuration can
127     # looks like this:
128     #
129     # interfaces_dpdk:
130     #   ens3:
131     #   - fuelweb_admin
132     #   ens4:
133     #   - storage
134     #   - management
135     #   ens5:
136     #   - interface_properties:
137     #       dpdk:
138     #         enabled:
139     #           value: true
140     #   - private
141     #   ens6:
142     #   - public
143     def modify_node_interface(self, node_id, roles_blade):
144         log('Modify interface config for node %s' % node_id)
145         interface_yaml = ('%s/node_%s/interfaces.yaml'
146                           % (self.yaml_config_dir, node_id))
147         check_file_exists(interface_yaml)
148         backup('%s/node_%s' % (self.yaml_config_dir, node_id))
149
150         with io.open(interface_yaml) as stream:
151             interfaces = yaml.load(stream)
152
153         net_name_id = {}
154         for interface in interfaces:
155             for network in interface['assigned_networks']:
156                 net_name_id[network['name']] = network['id']
157
158         type = self.dea.get_node_property(roles_blade[1], 'interfaces')
159         interface_config = self.dea.get_property(type)
160
161         for interface in interfaces:
162             interface['assigned_networks'] = []
163             if interface['name'] in interface_config:
164                 for prop in interface_config[interface['name']]:
165                     net = {}
166                     # net name
167                     if isinstance(prop, six.string_types):
168                         net['id'] = net_name_id[prop]
169                         net['name'] = prop
170                         interface['assigned_networks'].append(net)
171                     # network properties
172                     elif isinstance(prop, dict):
173                         if 'interface_properties' not in prop:
174                             log('Interface configuration contains unknown dict: %s' % prop)
175                             continue
176                         interface['attributes'] = self._merge_dicts(
177                             interface.get('attributes', {}),
178                             prop.get('interface_properties', {}))
179
180         with io.open(interface_yaml, 'w') as stream:
181             yaml.dump(interfaces, stream, default_flow_style=False)
182
183     def _merge_dicts(self, dict1, dict2):
184         """Recursively merge dictionaries."""
185         result = copy.deepcopy(dict1)
186         for k, v in six.iteritems(dict2):
187             if isinstance(result.get(k), list) and isinstance(v, list):
188                 result[k].extend(v)
189                 continue
190             if isinstance(result.get(k), dict) and isinstance(v, dict):
191                 result[k] = self._merge_dicts(result[k], v)
192                 continue
193             result[k] = copy.deepcopy(v)
194         return result