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