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 ###############################################################################
20 exec_cmd = common.exec_cmd
23 check_file_exists = common.check_file_exists
25 backup = common.backup
28 class ConfigureNodes(object):
30 def __init__(self, yaml_config_dir, env_id, node_id_roles_dict, dea):
31 self.yaml_config_dir = yaml_config_dir
33 self.node_id_roles_dict = node_id_roles_dict
36 def config_nodes(self):
37 log('Configure nodes')
38 for node_id, roles_blade in self.node_id_roles_dict.iteritems():
39 exec_cmd('fuel node set --node-id %s --role %s --env %s'
40 % (node_id, roles_blade[0], self.env_id))
42 self.download_deployment_config()
43 for node_id, roles_blade in self.node_id_roles_dict.iteritems():
44 self.download_interface_config(node_id)
45 self.modify_node_interface(node_id, roles_blade)
46 self.modify_node_network_schemes(node_id, roles_blade)
47 self.upload_interface_config(node_id)
48 self.upload_deployment_config()
50 def modify_node_network_schemes(self, node_id, roles_blade):
51 log('Modify network transformations for node %s' % node_id)
52 type = self.dea.get_node_property(roles_blade[1], 'transformations')
53 transformations = self.dea.get_property(type)
54 deployment_dir = '%s/deployment_%s' % (
55 self.yaml_config_dir, self.env_id)
56 backup(deployment_dir)
57 for node_file in glob.glob(deployment_dir + '/*_%s.yaml' % node_id):
58 with io.open(node_file) as stream:
59 node = yaml.load(stream)
61 node['network_scheme'].update(transformations)
63 with io.open(node_file, 'w') as stream:
64 yaml.dump(node, stream, default_flow_style=False)
66 def download_deployment_config(self):
67 log('Download deployment config for environment %s' % self.env_id)
68 exec_cmd('fuel deployment --env %s --default --dir %s'
69 % (self.env_id, self.yaml_config_dir))
71 def upload_deployment_config(self):
72 log('Upload deployment config for environment %s' % self.env_id)
73 exec_cmd('fuel deployment --env %s --upload --dir %s'
74 % (self.env_id, self.yaml_config_dir))
76 def download_interface_config(self, node_id):
77 log('Download interface config for node %s' % node_id)
78 exec_cmd('fuel node --env %s --node %s --network --download '
79 '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
81 def upload_interface_config(self, node_id):
82 log('Upload interface config for node %s' % node_id)
83 exec_cmd('fuel node --env %s --node %s --network --upload '
84 '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
86 def modify_node_interface(self, node_id, roles_blade):
87 log('Modify interface config for node %s' % node_id)
88 interface_yaml = ('%s/node_%s/interfaces.yaml'
89 % (self.yaml_config_dir, node_id))
90 check_file_exists(interface_yaml)
91 backup('%s/node_%s' % (self.yaml_config_dir, node_id))
93 with io.open(interface_yaml) as stream:
94 interfaces = yaml.load(stream)
97 for interface in interfaces:
98 for network in interface['assigned_networks']:
99 net_name_id[network['name']] = network['id']
101 type = self.dea.get_node_property(roles_blade[1], 'interfaces')
102 interface_config = self.dea.get_property(type)
104 for interface in interfaces:
105 interface['assigned_networks'] = []
106 if interface['name'] in interface_config:
107 for net_name in interface_config[interface['name']]:
109 net['id'] = net_name_id[net_name]
110 net['name'] = net_name
111 interface['assigned_networks'].append(net)
113 with io.open(interface_yaml, 'w') as stream:
114 yaml.dump(interfaces, stream, default_flow_style=False)