Automatic Deployment
[genesis.git] / fuel / deploy / cloud_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.debug('Configure nodes\n')
27         for node_id, roles_shelf_blade in self.node_id_roles_dict.iteritems():
28             exec_cmd('fuel node set --node-id %s --role %s --env %s'
29                      % (node_id, ','.join(roles_shelf_blade[0]), self.env_id))
30
31         self.download_deployment_config()
32         self.modify_node_network_schemes()
33         self.upload_deployment_config()
34
35         for node_id, roles_shelf_blade in self.node_id_roles_dict.iteritems():
36             self.download_interface_config(node_id)
37             self.modify_node_interface(node_id)
38             self.upload_interface_config(node_id)
39
40     def modify_node_network_schemes(self):
41         LOG.debug('Modify node network schemes in environment %s\n' % self.env_id)
42         for node_file in glob.glob('%s/deployment_%s/*.yaml'
43                                    % (self.yaml_config_dir, self.env_id)):
44              check_file_exists(node_file)
45
46              if 'compute' in node_file:
47                  node_type = 'compute'
48              else:
49                  node_type = 'controller'
50
51              network_scheme = self.dea.get_network_scheme(node_type)
52
53              with io.open(node_file) as stream:
54                  node = yaml.load(stream)
55
56              node['network_scheme']['transformations'] = network_scheme
57
58              with io.open(node_file, 'w') as stream:
59                  yaml.dump(node, stream, default_flow_style=False)
60
61
62     def download_deployment_config(self):
63         LOG.debug('Download deployment config for environment %s\n' % self.env_id)
64         r, c = exec_cmd('fuel deployment --env %s --default --dir %s'
65                         % (self.env_id, self.yaml_config_dir))
66
67     def upload_deployment_config(self):
68         LOG.debug('Upload deployment config for environment %s\n' % self.env_id)
69         r, c = exec_cmd('fuel deployment --env %s --upload --dir %s'
70                         % (self.env_id, self.yaml_config_dir))
71
72     def download_interface_config(self, node_id):
73         LOG.debug('Download interface config for node %s\n' % node_id)
74         r, c = exec_cmd('fuel node --env %s --node %s --network --download '
75                         '--dir %s' % (self.env_id, node_id,
76                                       self.yaml_config_dir))
77
78     def upload_interface_config(self, node_id):
79         LOG.debug('Upload interface config for node %s\n' % node_id)
80         r, c = exec_cmd('fuel node --env %s --node %s --network --upload '
81                         '--dir %s' % (self.env_id, node_id,
82                                       self.yaml_config_dir))
83
84     def modify_node_interface(self, node_id):
85         LOG.debug('Modify interface config for node %s\n' % node_id)
86         interface_yaml = (self.yaml_config_dir + '/node_%s/interfaces.yaml'
87                           % node_id)
88
89         with io.open(interface_yaml) as stream:
90             interfaces = yaml.load(stream)
91
92         net_name_id = {}
93         for interface in interfaces:
94             for network in interface['assigned_networks']:
95                  net_name_id[network['name']] = network['id']
96
97         interface_config = self.dea.get_interfaces()
98
99         for interface in interfaces:
100             interface['assigned_networks'] = []
101             for net_name in interface_config[interface['name']]:
102                 net = {}
103                 net['id'] = net_name_id[net_name]
104                 net['name'] = net_name
105                 interface['assigned_networks'].append(net)
106
107         with io.open(interface_yaml, 'w') as stream:
108             yaml.dump(interfaces, stream, default_flow_style=False)