Merge "Add create vm script and foreman config"
[genesis.git] / fuel / deploy / cloud / configure_nodes.py
1 import common
2 import yaml
3 import io
4 import glob
5
6 N = common.N
7 E = common.E
8 R = common.R
9 RO = common.RO
10 exec_cmd = common.exec_cmd
11 parse = common.parse
12 err = common.err
13 check_file_exists = common.check_file_exists
14 log = common.log
15
16
17 class ConfigureNodes(object):
18
19     def __init__(self, yaml_config_dir, env_id, node_id_roles_dict, dea):
20         self.yaml_config_dir = yaml_config_dir
21         self.env_id = env_id
22         self.node_id_roles_dict = node_id_roles_dict
23         self.dea = dea
24
25     def config_nodes(self):
26         log('Configure nodes')
27         for node_id, roles_blade in self.node_id_roles_dict.iteritems():
28             exec_cmd('fuel node set --node-id %s --role %s --env %s'
29                      % (node_id, roles_blade[0], self.env_id))
30
31         self.download_deployment_config()
32         for node_id, roles_blade in self.node_id_roles_dict.iteritems():
33             self.download_interface_config(node_id)
34             self.modify_node_interface(node_id, roles_blade)
35             self.modify_node_network_schemes(node_id, roles_blade)
36             self.upload_interface_config(node_id)
37         self.upload_deployment_config()
38
39     def modify_node_network_schemes(self, node_id, roles_blade):
40         log('Modify network transformations for node %s' % node_id)
41         type = self.dea.get_node_property(roles_blade[1], 'transformations')
42         transformations = self.dea.get_transformations(type)
43
44         for node_file in glob.glob('%s/deployment_%s/*_%s.yaml'
45                                    % (self.yaml_config_dir, self.env_id,
46                                       node_id)):
47             with io.open(node_file) as stream:
48                node = yaml.load(stream)
49
50             node['network_scheme']['transformations'] = transformations
51
52             with io.open(node_file, 'w') as stream:
53                yaml.dump(node, stream, default_flow_style=False)
54
55     def download_deployment_config(self):
56         log('Download deployment config for environment %s' % self.env_id)
57         exec_cmd('fuel deployment --env %s --default --dir %s'
58                  % (self.env_id, self.yaml_config_dir))
59
60     def upload_deployment_config(self):
61         log('Upload deployment config for environment %s' % self.env_id)
62         exec_cmd('fuel deployment --env %s --upload --dir %s'
63                  % (self.env_id, self.yaml_config_dir))
64
65     def download_interface_config(self, node_id):
66         log('Download interface config for node %s' % node_id)
67         exec_cmd('fuel node --env %s --node %s --network --download '
68                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
69
70     def upload_interface_config(self, node_id):
71         log('Upload interface config for node %s' % node_id)
72         exec_cmd('fuel node --env %s --node %s --network --upload '
73                  '--dir %s' % (self.env_id, node_id, self.yaml_config_dir))
74
75     def modify_node_interface(self, node_id, roles_blade):
76         log('Modify interface config for node %s' % node_id)
77         interface_yaml = ('%s/node_%s/interfaces.yaml'
78                           % (self.yaml_config_dir, node_id))
79         check_file_exists(interface_yaml)
80
81         with io.open(interface_yaml) as stream:
82             interfaces = yaml.load(stream)
83
84         net_name_id = {}
85         for interface in interfaces:
86             for network in interface['assigned_networks']:
87                  net_name_id[network['name']] = network['id']
88
89         type = self.dea.get_node_property(roles_blade[1], 'interfaces')
90         interface_config = self.dea.get_interfaces(type)
91
92         for interface in interfaces:
93             interface['assigned_networks'] = []
94             if interface['name'] in interface_config:
95                 for net_name in interface_config[interface['name']]:
96                     net = {}
97                     net['id'] = net_name_id[net_name]
98                     net['name'] = net_name
99                     interface['assigned_networks'].append(net)
100
101         with io.open(interface_yaml, 'w') as stream:
102             yaml.dump(interfaces, stream, default_flow_style=False)