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 ###############################################################################
24 class ConfigureNodes(object):
26 def __init__(self, yaml_config_dir, env_id, node_id_roles_dict, dea):
27 self.yaml_config_dir = yaml_config_dir
29 self.node_id_roles_dict = node_id_roles_dict
32 def config_nodes(self):
33 log('Configure nodes')
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))
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)
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
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()
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)
72 node['network_scheme'].update(transformations)
74 with io.open(node_file, 'w') as stream:
75 yaml.dump(node, stream, default_flow_style=False)
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))
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))
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))
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))
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))
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))
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')
111 # Node attributes are not overridden. Nothing to do.
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))
119 with open(attributes_yaml) as stream:
120 attributes = yaml.load(stream)
121 result_attributes = self._merge_dicts(attributes, new_attributes)
123 with open(attributes_yaml, 'w') as stream:
124 yaml.dump(result_attributes, stream, default_flow_style=False)
126 # interface configuration can
136 # - interface_properties:
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))
150 with io.open(interface_yaml) as stream:
151 interfaces = yaml.load(stream)
154 for interface in interfaces:
155 for network in interface['assigned_networks']:
156 net_name_id[network['name']] = network['id']
158 type = self.dea.get_node_property(roles_blade[1], 'interfaces')
159 interface_config = self.dea.get_property(type)
161 for interface in interfaces:
162 interface['assigned_networks'] = []
163 if interface['name'] in interface_config:
164 for prop in interface_config[interface['name']]:
167 if isinstance(prop, six.string_types):
168 net['id'] = net_name_id[prop]
170 interface['assigned_networks'].append(net)
172 elif isinstance(prop, dict):
173 if 'interface_properties' not in prop:
174 log('Interface configuration contains unknown dict: %s' % prop)
176 interface['attributes'] = self._merge_dicts(
177 interface.get('attributes', {}),
178 prop.get('interface_properties', {}))
180 with io.open(interface_yaml, 'w') as stream:
181 yaml.dump(interfaces, stream, default_flow_style=False)
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):
190 if isinstance(result.get(k), dict) and isinstance(v, dict):
191 result[k] = self._merge_dicts(result[k], v)
193 result[k] = copy.deepcopy(v)