0b298e5e834447effc48a49a9aba4cb467115b91
[genesis.git] / fuel / deploy / configure_network.py
1 import common
2 import os
3 import yaml
4 import io
5 import re
6
7 N = common.N
8 E = common.E
9 R = common.R
10 RO = common.RO
11 exec_cmd = common.exec_cmd
12 parse = common.parse
13 err = common.err
14
15 P1 = re.compile('!\s.*')
16
17 class ConfigureNetwork(object):
18
19     def __init__(self, yaml_config_dir, network_yaml, env_id, dea):
20         self.yaml_config_dir = yaml_config_dir
21         self.network_yaml = network_yaml
22         self.env_id = env_id
23         self.dea = dea
24
25     def download_settings(self):
26         exec_cmd('fuel network --env %s --download --dir %s'
27                  % (self.env_id, self.yaml_config_dir))
28
29     def upload_settings(self):
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
35         self.download_settings()
36
37         self.apply_network_config()
38
39         self.upload_settings()
40
41         self.verify()
42
43     def apply_network_config(self):
44
45         with io.open(self.network_yaml) as stream:
46             network_config = yaml.load(stream)
47         networks = network_config['networks']
48
49         net = self.dea.get_networks()
50         net['fuelweb_admin'] = net['management']
51         if 'vlan' in net['fuelweb_admin']:
52             del net['fuelweb_admin']['vlan']
53         del net['management']
54         net_names = [n for n in net.iterkeys()]
55
56         for i in range(len(networks)):
57             if networks[i]['name'] == 'management':
58                 networks = networks[:i] + networks[i+1:]
59                 network_config['networks'] = networks
60                 break
61
62         for network in networks:
63             name = network['name']
64             if name in net_names:
65                 if ('vlan' in net[name] and net[name]['vlan'] is not None):
66                     network['vlan_start'] = net[name]['vlan']
67                 network['cidr'] = net[name]['cidr']
68                 network['ip_ranges'][0][0] = net[name]['start']
69                 network['ip_ranges'][0][1] = net[name]['end']
70
71         with io.open(self.network_yaml, 'w') as stream:
72             yaml.dump(network_config, stream, default_flow_style=False)
73
74     def verify(self):
75         ret = exec_cmd('mktemp -d')
76         temp_dir = ret.splitlines()[0]
77
78         exec_cmd('fuel network --env %s --download --dir %s'
79                  % (self.env_id, temp_dir))
80
81         ret = exec_cmd('diff -C0 %s %s'
82                        % (self.network_yaml,
83                           temp_dir + '/network_%s.yaml' % self.env_id))
84         diff_list = []
85         for l in ret.splitlines():
86             m = P1.match(l)
87             if m and '_vip' not in l:
88                 diff_list.append(l)
89         if diff_list:
90             err('Uploaded network yaml rejected by Fuel\n')
91