Autodeploy inspired on Prototype #2
[genesis.git] / fuel / 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('Download network config for environment %s' % 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('Upload network config for environment %s' % 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('Configure network')
35         self.download_network_config()
36         self.modify_network_config()
37         self.upload_network_config()
38
39     def modify_network_config(self):
40         log('Modify network config for environment %s' % self.env_id)
41         network_yaml = ('%s/network_%s.yaml'
42                         % (self.yaml_config_dir, self.env_id))
43         check_file_exists(network_yaml)
44
45         network_config = self.dea.get_property('network')
46
47         with io.open(network_yaml) as stream:
48             network = yaml.load(stream)
49
50         net_names = self.dea.get_network_names()
51         net_id = {}
52         for net in network['networks']:
53             if net['name'] in net_names:
54                 net_id[net['name']] = {'id': net['id'],
55                                        'group_id': net['group_id']}
56
57         for network in network_config['networks']:
58             network.update(net_id[network['name']])
59
60         with io.open(network_yaml, 'w') as stream:
61             yaml.dump(network_config, stream, default_flow_style=False)