diff options
author | Dan Radez <dradez@redhat.com> | 2016-05-20 12:29:55 -0400 |
---|---|---|
committer | Dan Radez <dradez@redhat.com> | 2016-05-28 08:42:02 -0400 |
commit | f7032844ca19b833b5ee05a7698ace2823aa2fc2 (patch) | |
tree | 38a8f53ebd64f4ae8253f81df0f33e201b95e357 /lib/python | |
parent | 1695a72d5680fae3cf35d6a6dbd64de5dd08daf3 (diff) |
Adding python tests and updating for conventions
- adding tests dir with a script to run python coverage
- adding main funtion to python per convention
- replacing underscores with dashes in CLI params per convention
Change-Id: I6f64174278e2a2e93eacdb7bee09aac49ce15d29
Signed-off-by: Dan Radez <dradez@redhat.com>
Diffstat (limited to 'lib/python')
-rwxr-xr-x | lib/python/apex-python-utils.py | 197 |
1 files changed, 122 insertions, 75 deletions
diff --git a/lib/python/apex-python-utils.py b/lib/python/apex-python-utils.py index 7e947ea6..4eee4f75 100755 --- a/lib/python/apex-python-utils.py +++ b/lib/python/apex-python-utils.py @@ -1,5 +1,5 @@ ############################################################################## -# Copyright (c) 2016 Feng Pan (fpan@redhat.com) +# Copyright (c) 2016 Feng Pan (fpan@redhat.com), Dan Radez (dradez@redhat.com) # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 @@ -15,86 +15,133 @@ import os from jinja2 import Environment, FileSystemLoader -def parse_net_settings(settings_args): - settings = apex.NetworkSettings(settings_args.path, - settings_args.network_isolation) +def parse_net_settings(args): + """ + Parse OPNFV Apex network_settings.yaml config file + and dump bash syntax to set environment variables + + Args: + - file: string + file to network_settings.yaml file + - network_isolation: bool + enable or disable network_isolation + """ + settings = apex.NetworkSettings(args.file, + args.network_isolation) settings.dump_bash() -def parse_deploy_settings(settings_args): - settings = apex.DeploySettings(settings_args.path) +def parse_deploy_settings(args): + settings = apex.DeploySettings(args.file) settings.dump_bash() -def find_ip(int_args): - interface = apex.ip_utils.get_interface(int_args.interface, - int_args.address_family) +def find_ip(args): + """ + Get and print the IP from a specific interface + + Args: + - interface: string + network interface name + - address_family: int + 4 or 6, respective to ipv4 or ipv6 + """ + interface = apex.ip_utils.get_interface(args.interface, + args.address_family) if interface: 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") -subparsers = parser.add_subparsers() - -net_settings = subparsers.add_parser('parse_net_settings', - help='Parse network settings file') -net_settings.add_argument('-n', '--path', default='network_settings.yaml', - help='path to network settings file') -net_settings.add_argument('-i', '--network_isolation', type=bool, default=True, - help='network isolation') -net_settings.set_defaults(func=parse_net_settings) - -get_int_ip = subparsers.add_parser('find_ip', - help='Find interface ip') -get_int_ip.add_argument('-i', '--interface', required=True, - help='Interface name') -get_int_ip.add_argument('-af', '--address_family', default=4, type=int, - choices=[4, 6], - 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) - -deploy_settings = subparsers.add_parser('parse-deploy-settings', - help='Parse deploy settings file') -deploy_settings.add_argument('-f', '--path', default='deploy_settings.yaml', - help='path to deploy settings file') -deploy_settings.set_defaults(func=parse_deploy_settings) - -args = parser.parse_args(sys.argv[1:]) -if args.DEBUG: - logging.basicConfig(level=logging.DEBUG) -else: - apex_log_filename = '/var/log/apex/apex.log' - os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True) - logging.basicConfig(filename=apex_log_filename, - format='%(asctime)s %(levelname)s: %(message)s', - datefmt='%m/%d/%Y %I:%M:%S %p', - level=logging.DEBUG) - -if hasattr(args, 'func'): - args.func(args) -else: - parser.print_help() - exit(1) +def build_nic_template(args): + """ + Build and print a Triple-O nic template from jinja template + + Args: + - template: string + path to jinja template to load + - enabled_networks: comma delimited list + list of networks defined in net_env.py + - ext_net_type: string + interface or br-ex, defines the external network configuration + - address_family: string + 4 or 6, respective to ipv4 or ipv6 + """ + dir, template = args.template.rsplit('/', 1) + + env = Environment(loader=FileSystemLoader(dir)) + template = env.get_template(template) + print(template.render(enabled_networks=args.enabled_networks, + external_net_type=args.ext_net_type, + external_net_af=args.address_family)) + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--debug', action='store_true', default=False, + help="Turn on debug messages") + parser.add_argument('-l', '--log-file', default='/var/log/apex/apex.log', + dest='log_file', help="Log file to log to") + subparsers = parser.add_subparsers() + + net_settings = subparsers.add_parser('parse-net-settings', + help='Parse network settings file') + net_settings.add_argument('-f', '--file', default='network-settings.yaml', + help='path to network settings file') + net_settings.add_argument('-i', '--network-isolation', type=bool, + default=True, dest='network_isolation', + help='network isolation') + net_settings.set_defaults(func=parse_net_settings) + + get_int_ip = subparsers.add_parser('find-ip', + help='Find interface ip') + get_int_ip.add_argument('-i', '--interface', required=True, + help='Interface name') + get_int_ip.add_argument('-af', '--address-family', default=4, type=int, + choices=[4, 6], dest='address_family', + 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('-t', '--template', required=True, + dest='template', + help='Template file to process') + nic_template.add_argument('-n', '--enabled-networks', required=True, + dest='enabled_networks', + help='enabled network list') + nic_template.add_argument('-e', '--ext-net-type', default='interface', + dest='ext_net_type', + choices=['interface', 'br-ex'], + help='External network type') + nic_template.add_argument('-af', '--address-family', type=int, default=4, + dest='address_family', help='IP address family') + nic_template.set_defaults(func=build_nic_template) + + deploy_settings = subparsers.add_parser('parse-deploy-settings', + help='Parse deploy settings file') + deploy_settings.add_argument('-f', '--file', default='deploy_settings.yaml', + help='path to deploy settings file') + deploy_settings.set_defaults(func=parse_deploy_settings) + + args = parser.parse_args(sys.argv[1:]) + if args.debug: + logging.basicConfig(level=logging.DEBUG) + else: + apex_log_filename = args.log_file + os.makedirs(os.path.dirname(apex_log_filename), exist_ok=True) + logging.basicConfig(filename=apex_log_filename, + format='%(asctime)s %(levelname)s: %(message)s', + datefmt='%m/%d/%Y %I:%M:%S %p', + level=logging.DEBUG) + return parser, args + + +def main(parser, args): + if hasattr(args, 'func'): + args.func(args) + else: + parser.print_help() + exit(1) + +if __name__ == "__main__": + parser, args = parse_args() + main(parser, args) |