Updates docs for SR1 with final revision
[genesis.git] / fuel / deploy / cloud / configure_network.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 common
12 import yaml
13 import io
14
15 N = common.N
16 E = common.E
17 R = common.R
18 RO = common.RO
19 exec_cmd = common.exec_cmd
20 parse = common.parse
21 err = common.err
22 check_file_exists = common.check_file_exists
23 log = common.log
24 backup = common.backup
25
26
27 class ConfigureNetwork(object):
28
29     def __init__(self, yaml_config_dir, env_id, dea):
30         self.yaml_config_dir = yaml_config_dir
31         self.env_id = env_id
32         self.dea = dea
33         self.required_networks = []
34
35     def download_network_config(self):
36         log('Download network config for environment %s' % self.env_id)
37         exec_cmd('fuel network --env %s --download --dir %s'
38                  % (self.env_id, self.yaml_config_dir))
39
40     def upload_network_config(self):
41         log('Upload network config for environment %s' % self.env_id)
42         exec_cmd('fuel network --env %s --upload --dir %s'
43                  % (self.env_id, self.yaml_config_dir))
44
45     def config_network(self):
46         log('Configure network')
47         self.download_network_config()
48         self.modify_network_config()
49         self.upload_network_config()
50
51     def modify_network_config(self):
52         log('Modify network config for environment %s' % self.env_id)
53         network_yaml = ('%s/network_%s.yaml'
54                         % (self.yaml_config_dir, self.env_id))
55         check_file_exists(network_yaml)
56         backup(network_yaml)
57
58         network_config = self.dea.get_property('network')
59
60         with io.open(network_yaml) as stream:
61             network = yaml.load(stream)
62
63         net_names = self.dea.get_network_names()
64         net_id = {}
65         for net in network['networks']:
66             if net['name'] in net_names:
67                 net_id[net['name']] = {'id': net['id'],
68                                        'group_id': net['group_id']}
69
70         for network in network_config['networks']:
71             network.update(net_id[network['name']])
72
73         with io.open(network_yaml, 'w') as stream:
74             yaml.dump(network_config, stream, default_flow_style=False)