summaryrefslogtreecommitdiffstats
path: root/lib/python
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python')
-rwxr-xr-xlib/python/apex-python-utils.py24
-rw-r--r--lib/python/apex/net_env.py14
2 files changed, 38 insertions, 0 deletions
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):