fec6299d4cfffd8e116322f941119ba2048e3ae1
[apex.git] / lib / python / apex / network_environment.py
1 ##############################################################################
2 # Copyright (c) 2016 Tim Rozet (trozet@redhat.com) and others.
3 #
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 import yaml
11 import re
12 from .common.constants import ADMIN_NETWORK
13 from .common.constants import PRIVATE_NETWORK
14 from .common.constants import STORAGE_NETWORK
15 from .common.constants import PUBLIC_NETWORK
16 from .common.constants import API_NETWORK
17
18 PORTS = '/ports'
19 # Resources defined by <resource name>: <prefix>
20 EXTERNAL_RESOURCES = {'OS::TripleO::Network::External': None,
21                       'OS::TripleO::Network::Ports::ExternalVipPort': PORTS,
22                       'OS::TripleO::Controller::Ports::ExternalPort': PORTS,
23                       'OS::TripleO::Compute::Ports::ExternalPort': PORTS}
24 TENANT_RESOURCES = {'OS::TripleO::Network::Tenant': None,
25                     'OS::TripleO::Controller::Ports::TenantPort': PORTS,
26                     'OS::TripleO::Compute::Ports::TenantPort': PORTS}
27 STORAGE_RESOURCES = {'OS::TripleO::Network::Storage': None,
28                      'OS::TripleO::Network::Ports::StorageVipPort': PORTS,
29                      'OS::TripleO::Controller::Ports::StoragePort': PORTS,
30                      'OS::TripleO::Compute::Ports::StoragePort': PORTS}
31 API_RESOURCES = {'OS::TripleO::Network::InternalApi': None,
32                  'OS::TripleO::Network::Ports::InternalApiVipPort': PORTS,
33                  'OS::TripleO::Controller::Ports::InternalApiPort': PORTS,
34                  'OS::TripleO::Compute::Ports::InternalApiPort': PORTS}
35
36 # A list of flags that will be set to true when IPv6 is enabled
37 IPV6_FLAGS = ["NovaIPv6", "MongoDbIPv6", "CorosyncIPv6", "CephIPv6",
38               "RabbitIPv6", "MemcachedIPv6"]
39
40
41 class NetworkEnvironment:
42     """
43     This class creates a Network Environment to be used in TripleO Heat
44     Templates.
45
46     The class builds upon an existing network-environment file and modifies
47     based on a NetworkSettings object.
48     """
49     def __init__(self, net_settings, filename):
50         with open(filename, 'r') as net_env_fh:
51             self.netenv_obj = yaml.load(net_env_fh)
52             self._update_net_environment(net_settings)
53
54     def _update_net_environment(self, settings_obj):
55         """
56         Updates Network Environment according to Network Settings
57         :param: network settings object
58         :return:  None
59         """
60         if not settings_obj:
61             raise NetworkEnvException("Network Settings does not exist")
62
63         net_settings = settings_obj.get_network_settings()
64         enabled_networks = settings_obj.get_enabled_networks()
65         param_def = 'parameter_defaults'
66         reg = 'resource_registry'
67         for key, prefix in TENANT_RESOURCES.items():
68             if prefix is None:
69                 prefix = ''
70             m = re.split('%s/\w+\.yaml' % prefix, self.netenv_obj[reg][key])
71             if m is not None:
72                 tht_dir = m[0]
73                 break
74         if not tht_dir:
75             raise NetworkEnvException('Unable to parse THT Directory')
76
77         admin_cidr = net_settings[ADMIN_NETWORK]['cidr']
78         admin_prefix = str(admin_cidr.prefixlen)
79         self.netenv_obj[param_def]['ControlPlaneSubnetCidr'] = admin_prefix
80         self.netenv_obj[param_def]['ControlPlaneDefaultRoute'] = \
81             net_settings[ADMIN_NETWORK]['provisioner_ip']
82         public_cidr = net_settings[PUBLIC_NETWORK]['cidr']
83         self.netenv_obj[param_def]['ExternalNetCidr'] = str(public_cidr)
84         if net_settings[PUBLIC_NETWORK]['vlan'] != 'native':
85             self.netenv_obj[param_def]['ExternalNetworkVlanID'] = \
86                 net_settings[PUBLIC_NETWORK]['vlan']
87         public_range = \
88             net_settings[PUBLIC_NETWORK]['usable_ip_range'].split(',')
89         self.netenv_obj[param_def]['ExternalAllocationPools'] = \
90             [{'start':
91               public_range[0],
92               'end': public_range[1]
93               }]
94         self.netenv_obj[param_def]['ExternalInterfaceDefaultRoute'] = \
95             net_settings[PUBLIC_NETWORK]['gateway']
96         self.netenv_obj[param_def]['EC2MetadataIp'] = \
97             net_settings[ADMIN_NETWORK]['provisioner_ip']
98         self.netenv_obj[param_def]['DnsServers'] = net_settings['dns_servers']
99
100         if public_cidr.version == 6:
101             postfix = '/external_v6.yaml'
102         else:
103             postfix = '/external.yaml'
104
105         for key, prefix in EXTERNAL_RESOURCES.items():
106             if prefix is None:
107                 prefix = ''
108             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
109
110         if PRIVATE_NETWORK in enabled_networks:
111             priv_range = net_settings[PRIVATE_NETWORK][
112                 'usable_ip_range'].split(',')
113             self.netenv_obj[param_def]['TenantAllocationPools'] = \
114                 [{'start':
115                   priv_range[0],
116                   'end': priv_range[1]
117                   }]
118             priv_cidr = net_settings[PRIVATE_NETWORK]['cidr']
119             self.netenv_obj[param_def]['TenantNetCidr'] = str(priv_cidr)
120             if priv_cidr.version == 6:
121                 postfix = '/tenant_v6.yaml'
122             else:
123                 postfix = '/tenant.yaml'
124             if net_settings[PRIVATE_NETWORK]['vlan'] != 'native':
125                 self.netenv_obj[param_def]['TenantNetworkVlanID'] = \
126                     net_settings[PRIVATE_NETWORK]['vlan']
127         else:
128             postfix = '/noop.yaml'
129
130         for key, prefix in TENANT_RESOURCES.items():
131             if prefix is None:
132                 prefix = ''
133             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
134
135         if STORAGE_NETWORK in enabled_networks:
136             storage_range = net_settings[STORAGE_NETWORK][
137                 'usable_ip_range'].split(',')
138             self.netenv_obj[param_def]['StorageAllocationPools'] = \
139                 [{'start':
140                   storage_range[0],
141                   'end':
142                   storage_range[1]
143                   }]
144             storage_cidr = net_settings[STORAGE_NETWORK]['cidr']
145             self.netenv_obj[param_def]['StorageNetCidr'] = str(storage_cidr)
146             if storage_cidr.version == 6:
147                 postfix = '/storage_v6.yaml'
148             else:
149                 postfix = '/storage.yaml'
150             if net_settings[STORAGE_NETWORK]['vlan'] != 'native':
151                 self.netenv_obj[param_def]['StorageNetworkVlanID'] = \
152                     net_settings[STORAGE_NETWORK]['vlan']
153         else:
154             postfix = '/noop.yaml'
155
156         for key, prefix in STORAGE_RESOURCES.items():
157             if prefix is None:
158                 prefix = ''
159             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
160
161         if API_NETWORK in enabled_networks:
162             api_range = net_settings[API_NETWORK][
163                 'usable_ip_range'].split(',')
164             self.netenv_obj[param_def]['InternalApiAllocationPools'] = \
165                 [{'start': api_range[0],
166                   'end': api_range[1]
167                   }]
168             api_cidr = net_settings[API_NETWORK]['cidr']
169             self.netenv_obj[param_def]['InternalApiNetCidr'] = str(api_cidr)
170             if api_cidr.version == 6:
171                 postfix = '/internal_api_v6.yaml'
172             else:
173                 postfix = '/internal_api.yaml'
174             if net_settings[API_NETWORK]['vlan'] != 'native':
175                 self.netenv_obj[param_def]['InternalApiNetworkVlanID'] = \
176                     net_settings[API_NETWORK]['vlan']
177         else:
178             postfix = '/noop.yaml'
179
180         for key, prefix in API_RESOURCES.items():
181             if prefix is None:
182                 prefix = ''
183             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
184
185         # Set IPv6 related flags to True. Not that we do not set those to False
186         # when IPv4 is configured, we'll use the default or whatever the user
187         # may have set.
188         if settings_obj.get_ip_addr_family() == 6:
189             for flag in IPV6_FLAGS:
190                 self.netenv_obj[param_def][flag] = True
191
192     def get_netenv_settings(self):
193         """
194         Getter for netenv settings
195         :return: Dictionary of network environment settings
196         """
197         return self.netenv_obj
198
199
200 class NetworkEnvException(Exception):
201     def __init__(self, value):
202         self.value = value
203
204     def __str__(self):
205             return self.value