From 683f9010b19232897648913e767a0dc05dbf498e Mon Sep 17 00:00:00 2001 From: Feng Pan Date: Sat, 7 May 2016 16:04:00 -0400 Subject: Change NIC template format Convert compute and controller NIC templates to jinja2 format. Added support for IPv6 for those NIC templates. Existing IPv4 template generation behavior does not change. The .template files are kept in tree for now, they will be removed after VLAN changes are made. Change-Id: I2ffc7c403af3659db780ece2bd9195cd62748f0c Signed-off-by: Feng Pan --- lib/python/apex-python-utils.py | 24 ++++++++++++++++++++++++ lib/python/apex/net_env.py | 14 ++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'lib/python') diff --git a/lib/python/apex-python-utils.py b/lib/python/apex-python-utils.py index 802e8571..1d5b4a7c 100755 --- a/lib/python/apex-python-utils.py +++ b/lib/python/apex-python-utils.py @@ -13,6 +13,8 @@ import sys import apex import logging import os +from jinja2 import Environment, FileSystemLoader + def parse_net_settings(settings_args): settings = apex.NetworkSettings(settings_args.path, @@ -27,6 +29,14 @@ def find_ip(int_args): print(interface.ip) +def build_nic_template(nic_args): + env = Environment(loader=FileSystemLoader(nic_args.template_directory)) + template = env.get_template(nic_args.template_filename) + print(template.render(enabled_networks=nic_args.enabled_networks, + external_net_type=nic_args.ext_net_type, + external_net_af=nic_args.address_family)) + + parser = argparse.ArgumentParser() parser.add_argument('--DEBUG', action='store_true', default=False, help="Turn on debug messages") @@ -49,6 +59,20 @@ get_int_ip.add_argument('-af', '--address_family', default=4, type=int, help='IP Address family') get_int_ip.set_defaults(func=find_ip) +nic_template = subparsers.add_parser('nic_template', help='Build NIC templates') +nic_template.add_argument('-d', '--template_directory', required=True, + help='Template file directory') +nic_template.add_argument('-f', '--template_filename', required=True, + help='Template file to process') +nic_template.add_argument('-n', '--enabled_networks', required=True, + help='enabled network list') +nic_template.add_argument('-e', '--ext_net_type', default='interface', + choices=['interface', 'br-ex'], + help='External network type') +nic_template.add_argument('-af', '--address_family', type=int, default=4, + help='IP address family') +nic_template.set_defaults(func=build_nic_template) + args = parser.parse_args(sys.argv[1:]) if args.DEBUG: logging.basicConfig(level=logging.DEBUG) diff --git a/lib/python/apex/net_env.py b/lib/python/apex/net_env.py index ec46fe28..3ca28f8a 100644 --- a/lib/python/apex/net_env.py +++ b/lib/python/apex/net_env.py @@ -227,12 +227,26 @@ class NetworkSettings: bash_str += "{}_{}={}\n".format(network, key, value) bash_str += "enabled_network_list='{}'\n" \ .format(' '.join(self.enabled_network_list)) + bash_str += "ip_addr_family={}\n".format(self.get_ip_addr_family()) if path: with open(path, 'w') as file: file.write(bash_str) else: print(bash_str) + def get_ip_addr_family(self): + """ + Returns IP address family for current deployment. + + If any enabled network has IPv6 CIDR, the deployment is classified as + IPv6. + """ + for network in self.enabled_network_list: + cidr = ipaddress.ip_network(self.settings_obj[network]['cidr']) + if cidr.version == 6: + return 6 + + return 4 class NetworkSettingsException(Exception): def __init__(self, value): -- cgit 1.2.3-korg