Fixes undercloud install failure with setting hostname
[apex.git] / apex / network / network_data.py
1 ##############################################################################
2 # Copyright (c) 2017 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 copy
11 import logging
12 import pprint
13
14 from apex.common import utils
15 from apex.common.constants import (
16     ADMIN_NETWORK,
17     TENANT_NETWORK,
18     STORAGE_NETWORK,
19     EXTERNAL_NETWORK,
20     API_NETWORK
21 )
22 from apex import NetworkSettings
23
24
25 class NetworkDataException(Exception):
26     pass
27
28
29 def create_network_data(ns, target=None):
30     """
31     Creates network data file for deployments
32     :param ns: Network Settings
33     :param target: Target file to write
34     :return: list of networks and properties
35     """
36     network_data = list()
37     if not isinstance(ns, NetworkSettings):
38         raise NetworkDataException('Invalid network settings given')
39
40     nets = ns['networks']
41
42     # TODO(trozet) change this all to be dynamic after TripleO bug
43     # https://bugs.launchpad.net/tripleo/+bug/1720849 is fixed
44
45     for net in nets.keys():
46         if net == ADMIN_NETWORK:
47             # we dont need to add ctlplane network to network data
48             continue
49         elif net == EXTERNAL_NETWORK:
50             network = nets[net][0]
51             net_name = net.title()
52             net_lower = net.lower()
53         elif net == API_NETWORK:
54             network = nets[net]
55             net_name = 'InternalApi'
56             net_lower = 'internal_api'
57         else:
58             network = nets[net]
59             net_name = net.title()
60             net_lower = net.lower()
61         # TODO(trozet): add ipv6 support
62         tmp_net = {'name': net_name,
63                    'name_lower': net_lower,
64                    'vip': net != TENANT_NETWORK,
65                    'enabled': net in ns.enabled_network_list}
66         if 'gateway' in network:
67             tmp_net['gateway_ip'] = str(network['gateway'])
68         if 'overcloud_ip_range' in network:
69             net_range = network['overcloud_ip_range']
70             tmp_net['allocation_pools'] = [{'start': str(net_range[0]),
71                                            'end': str(net_range[1])}]
72         elif tmp_net['enabled']:
73             logging.error("overcloud ip range is missing and must be provided "
74                           "in network settings when network is enabled for "
75                           "network {}".format(net))
76             raise NetworkDataException("overcloud_ip_range missing from "
77                                        "network: {}".format(net))
78         if 'cidr' in network:
79             tmp_net['ip_subnet'] = str(network['cidr'])
80         elif tmp_net['enabled']:
81             logging.error("cidr is missing and must be provided in network "
82                           "settings when network is enabled for network "
83                           "{}".format(net))
84             raise NetworkDataException("cidr is null for network {}".format(
85                 net))
86         tmp_net['mtu'] = network.get('mtu', 1500)
87         network_data.append(copy.deepcopy(tmp_net))
88
89     # have to do this due to the aforementioned bug
90     storage_mgmt_net = {
91         'name': 'StorageMgmt',
92         'enabled': False,
93         'name_lower': 'storage_mgmt',
94         'ip_subnet': '172.16.3.0/24',
95         'allocation_pools': [{'start': '172.16.3.4', 'end': '172.16.3.250'}],
96         'vip': True,
97     }
98     network_data.append(storage_mgmt_net)
99     if target:
100         logging.debug("Writing network data to {}".format(target))
101         utils.dump_yaml(network_data, target)
102     logging.debug("Network data parsed as:\n "
103                   "{}".format(pprint.pformat(network_data)))
104     return network_data