Merge "Add support for kubernetes deployment"
[apex.git] / apex / overcloud / config.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 """
11 Utilities for generating overcloud configuration
12 """
13
14 import logging
15 import os
16
17 from jinja2 import Environment
18 from jinja2 import FileSystemLoader
19 from apex.common.exceptions import ApexDeployException
20
21
22 def create_nic_template(network_settings, deploy_settings, role, template_dir,
23                         target_dir):
24     """
25     Creates NIC heat template files
26     :param ns: Network settings
27     :param ds: Deploy Settings
28     :param role: controller or compute
29     :param template_dir: directory where base templates are stored
30     :param target_dir: to store rendered nic template
31     :return:
32     """
33     # TODO(trozet): rather than use Jinja2 to build these files, use with py
34     if role not in ['controller', 'compute']:
35         raise ApexDeployException("Invalid type for overcloud node: {"
36                                   "}".format(type))
37     logging.info("Creating template for {}".format(role))
38     template_file = 'nics-template.yaml.jinja2'
39     nets = network_settings.get('networks')
40     env = Environment(loader=FileSystemLoader(template_dir), autoescape=True)
41     template = env.get_template(template_file)
42     ds = deploy_settings.get('deploy_options')
43     ext_net = 'br-ex'
44     ovs_dpdk_br = ''
45     if ds['dataplane'] == 'fdio':
46         nets['tenant']['nic_mapping'][role]['phys_type'] = 'vpp_interface'
47         if ds['sdn_controller'] == 'opendaylight':
48             if role == 'compute':
49                 nets['external'][0]['nic_mapping'][role]['phys_type'] = \
50                     'vpp_interface'
51                 ext_net = 'vpp_interface'
52             if ds.get('dvr') is True:
53                 nets['admin']['nic_mapping'][role]['phys_type'] = \
54                     'linux_bridge'
55         else:
56             nets['external'][0]['nic_mapping'][role]['phys_type'] = \
57                 'linux_bridge'
58     elif ds['dataplane'] == 'ovs_dpdk':
59         ovs_dpdk_br = 'br-phy'
60     if (ds.get('performance', {}).get(role.title(), {}).get('vpp', {})
61             .get('uio-driver')):
62         nets['tenant']['nic_mapping'][role]['uio-driver'] =\
63             ds['performance'][role.title()]['vpp']['uio-driver']
64         if ds['sdn_controller'] == 'opendaylight' and role == 'compute':
65             nets['external'][0]['nic_mapping'][role]['uio-driver'] =\
66                 ds['performance'][role.title()]['vpp']['uio-driver']
67     if (ds.get('performance', {}).get(role.title(), {}).get('vpp', {})
68             .get('interface-options')):
69         nets['tenant']['nic_mapping'][role]['interface-options'] =\
70             ds['performance'][role.title()]['vpp']['interface-options']
71
72     template_output = template.render(
73         nets=nets,
74         role=role,
75         external_net_af=network_settings.get_ip_addr_family(),
76         external_net_type=ext_net,
77         ovs_dpdk_bridge=ovs_dpdk_br)
78
79     logging.debug("Template output: {}".format(template_output))
80     target = os.path.join(target_dir, "{}.yaml".format(role))
81     with open(target, "w") as f:
82         f.write(template_output)
83     logging.info("Wrote template {}".format(target))