f4d6f8738d77f997c5a9cddb9d1b418856952046
[genesis.git] / fuel / deploy / cloud_deploy / cloud / configure_network.py
1 import common
2 import yaml
3 import io
4
5 N = common.N
6 E = common.E
7 R = common.R
8 RO = common.RO
9 exec_cmd = common.exec_cmd
10 parse = common.parse
11 err = common.err
12 check_file_exists = common.check_file_exists
13 LOG = common.LOG
14
15 class ConfigureNetwork(object):
16
17     def __init__(self, yaml_config_dir, env_id, dea):
18         self.yaml_config_dir = yaml_config_dir
19         self.env_id = env_id
20         self.dea = dea
21         self.required_networks = []
22
23     def download_network_config(self):
24         LOG.debug('Download network config for environment %s\n' % self.env_id)
25         exec_cmd('fuel network --env %s --download --dir %s'
26                  % (self.env_id, self.yaml_config_dir))
27
28     def upload_network_config(self):
29         LOG.debug('Upload network config for environment %s\n' % self.env_id)
30         exec_cmd('fuel network --env %s --upload --dir %s'
31                  % (self.env_id, self.yaml_config_dir))
32
33     def config_network(self):
34         LOG.debug('Configure network\n')
35         self.download_network_config()
36         self.modify_network_config()
37         self.upload_network_config()
38
39     def modify_network_config(self):
40         LOG.debug('Modify network config for environment %s\n' % self.env_id)
41         network_yaml = (self.yaml_config_dir + '/network_%s.yaml'
42                         % self.env_id)
43         check_file_exists(network_yaml)
44
45         network_config = self.dea.get_networks()
46
47
48         with io.open(network_yaml) as stream:
49             network = yaml.load(stream)
50
51         net_names = self.dea.get_network_names()
52         net_id = {}
53         for net in network['networks']:
54             if net['name'] in net_names:
55                 net_id[net['name']] = {'id': net['id'],
56                                        'group_id': net['group_id']}
57
58         for network in network_config['networks']:
59             network.update(net_id[network['name']])
60
61         with io.open(network_yaml, 'w') as stream:
62             yaml.dump(network_config, stream, default_flow_style=False)