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 self.download_deployment_config()
47 for node_id, roles_blade in self.node_id_roles_dict.iteritems():
48 self.download_interface_config(node_id)
49 self.modify_node_interface(node_id, roles_blade)
50 self.modify_node_network_schemes(node_id, roles_blade)
51 self.upload_interface_config(node_id)
52 self.upload_deployment_config()
54 def modify_node_network_schemes(self, node_id, roles_blade):
55 log('Modify network transformations for node %s' % node_id)
56 type = self.dea.get_node_property(roles_blade[1], 'transformations')
57 transformations = self.dea.get_property(type)
58 deployment_dir = '%s/deployment_%s' % (
59 self.yaml_config_dir, self.env_id)
60 backup(deployment_dir)
61 for node_file in glob.glob(deployment_dir + '/*_%s.yaml' % node_id):
62 with io.open(node_file) as stream:
63 node = yaml.load(stream)
65 node['network_scheme'].update(transformations)
67 with io.open(node_file, 'w') as stream:
68 yaml.dump(node, stream, default_flow_style=False)
70 def download_deployment_config(self):
71 log('Download deployment config for environment %s' % self.env_id)
72 exec_cmd('fuel deployment --env %s --default --dir %s'
73 % (self.env_id, self.yaml_config_dir))
75 def upload_deployment_config(self):
76 log('Upload deployment config for environment %s' % self.env_id)
77 exec_cmd('fuel deployment --env %s --upload --dir %s'
78 % (self.env_id, self.yaml_config_dir))
80 def download_interface_config(self, node_id):
81 log('Download interface config for node %s' % node_id)
82 exec_cmd('fuel node --env %s --node %s --network --download '
83 '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
85 def upload_interface_config(self, node_id):
86 log('Upload interface config for node %s' % node_id)
87 exec_cmd('fuel node --env %s --node %s --network --upload '
88 '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
90 def modify_node_interface(self, node_id, roles_blade):
91 log('Modify interface config for node %s' % node_id)
92 interface_yaml = ('%s/node_%s/interfaces.yaml'
93 % (self.yaml_config_dir, node_id))
94 check_file_exists(interface_yaml)
95 backup('%s/node_%s' % (self.yaml_config_dir, node_id))
97 with io.open(interface_yaml) as stream:
98 interfaces = yaml.load(stream)
101 for interface in interfaces:
102 for network in interface['assigned_networks']:
103 net_name_id[network['name']] = network['id']
105 type = self.dea.get_node_property(roles_blade[1], 'interfaces')
106 interface_config = self.dea.get_property(type)
108 for interface in interfaces:
109 interface['assigned_networks'] = []
110 if interface['name'] in interface_config:
111 for net_name in interface_config[interface['name']]:
113 net['id'] = net_name_id[net_name]
114 net['name'] = net_name
115 interface['assigned_networks'].append(net)
117 with io.open(interface_yaml, 'w') as stream:
118 yaml.dump(interfaces, stream, default_flow_style=False)