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