A simple method to separate configuration for base fuel, plugins, PODs
[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
11 import yaml
12 import io
13 import glob
14
15 from common import (
16     exec_cmd,
17     check_file_exists,
18     log,
19     backup,
20 )
21
22
23 class ConfigureNodes(object):
24
25     def __init__(self, yaml_config_dir, env_id, node_id_roles_dict, dea):
26         self.yaml_config_dir = yaml_config_dir
27         self.env_id = env_id
28         self.node_id_roles_dict = node_id_roles_dict
29         self.dea = dea
30
31     def config_nodes(self):
32         log('Configure nodes')
33
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))
40
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))
45
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()
53
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)
64
65             node['network_scheme'].update(transformations)
66
67             with io.open(node_file, 'w') as stream:
68                 yaml.dump(node, stream, default_flow_style=False)
69
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))
74
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))
79
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))
84
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))
89
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))
96
97         with io.open(interface_yaml) as stream:
98             interfaces = yaml.load(stream)
99
100         net_name_id = {}
101         for interface in interfaces:
102             for network in interface['assigned_networks']:
103                 net_name_id[network['name']] = network['id']
104
105         type = self.dea.get_node_property(roles_blade[1], 'interfaces')
106         interface_config = self.dea.get_property(type)
107
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']]:
112                     net = {}
113                     net['id'] = net_name_id[net_name]
114                     net['name'] = net_name
115                     interface['assigned_networks'].append(net)
116
117         with io.open(interface_yaml, 'w') as stream:
118             yaml.dump(interfaces, stream, default_flow_style=False)