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 ###############################################################################
23 class ConfigureNodes(object):
25 def __init__(self, yaml_config_dir, env_id, node_id_roles_dict, dea):
26 self.yaml_config_dir = yaml_config_dir
28 self.node_id_roles_dict = node_id_roles_dict
31 def config_nodes(self):
32 log('Configure nodes')
34 # Super dirty fix since Fuel 7 requires user defined roles to be
35 # assigned before anything else (BUG fixed in Fuel 8)!
36 for node_id, roles_blade in self.node_id_roles_dict.iteritems():
37 if "opendaylight" in roles_blade[0] or "onos" in roles_blade[0] or "contrail" in roles_blade[0]:
38 exec_cmd('fuel node set --node-id %s --role %s --env %s'
39 % (node_id, roles_blade[0], self.env_id))
41 for node_id, roles_blade in self.node_id_roles_dict.iteritems():
42 if "opendaylight" not in roles_blade[0] and "onos" not in roles_blade[0] and "contrail" not in roles_blade[0]:
43 exec_cmd('fuel node set --node-id %s --role %s --env %s'
44 % (node_id, roles_blade[0], self.env_id))
46 # Download the unmodified default deployment configuration, because we
47 # need it for the network config.
48 self.download_deployment_config()
49 for node_id, roles_blade in self.node_id_roles_dict.iteritems():
50 self.download_interface_config(node_id)
51 self.modify_node_interface(node_id, roles_blade)
52 self.modify_node_network_schemes(node_id, roles_blade)
53 self.upload_interface_config(node_id)
55 # Download our modified deployment configuration, which includes our
56 # changes to network topology etc.
57 self.download_deployment_config()
58 self.upload_deployment_config()
60 def modify_node_network_schemes(self, node_id, roles_blade):
61 log('Modify network transformations for node %s' % node_id)
62 type = self.dea.get_node_property(roles_blade[1], 'transformations')
63 transformations = self.dea.get_property(type)
64 deployment_dir = '%s/deployment_%s' % (
65 self.yaml_config_dir, self.env_id)
66 backup(deployment_dir)
67 for node_file in glob.glob(deployment_dir + '/*_%s.yaml' % node_id):
68 with io.open(node_file) as stream:
69 node = yaml.load(stream)
71 node['network_scheme'].update(transformations)
73 with io.open(node_file, 'w') as stream:
74 yaml.dump(node, stream, default_flow_style=False)
76 def download_deployment_config(self):
77 log('Download deployment config for environment %s' % self.env_id)
78 exec_cmd('fuel deployment --env %s --default --dir %s'
79 % (self.env_id, self.yaml_config_dir))
81 def upload_deployment_config(self):
82 log('Upload deployment config for environment %s' % self.env_id)
83 exec_cmd('fuel deployment --env %s --upload --dir %s'
84 % (self.env_id, self.yaml_config_dir))
86 def download_interface_config(self, node_id):
87 log('Download interface config for node %s' % node_id)
88 exec_cmd('fuel node --env %s --node %s --network --download '
89 '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
91 def upload_interface_config(self, node_id):
92 log('Upload interface config for node %s' % node_id)
93 exec_cmd('fuel node --env %s --node %s --network --upload '
94 '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
96 def modify_node_interface(self, node_id, roles_blade):
97 log('Modify interface config for node %s' % node_id)
98 interface_yaml = ('%s/node_%s/interfaces.yaml'
99 % (self.yaml_config_dir, node_id))
100 check_file_exists(interface_yaml)
101 backup('%s/node_%s' % (self.yaml_config_dir, node_id))
103 with io.open(interface_yaml) as stream:
104 interfaces = yaml.load(stream)
107 for interface in interfaces:
108 for network in interface['assigned_networks']:
109 net_name_id[network['name']] = network['id']
111 type = self.dea.get_node_property(roles_blade[1], 'interfaces')
112 interface_config = self.dea.get_property(type)
114 for interface in interfaces:
115 interface['assigned_networks'] = []
116 if interface['name'] in interface_config:
117 for net_name in interface_config[interface['name']]:
119 net['id'] = net_name_id[net_name]
120 net['name'] = net_name
121 interface['assigned_networks'].append(net)
123 with io.open(interface_yaml, 'w') as stream:
124 yaml.dump(interfaces, stream, default_flow_style=False)