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