diff options
-rwxr-xr-x | ci/clean.sh | 4 | ||||
-rwxr-xr-x | ci/deploy.sh | 49 | ||||
-rwxr-xr-x | ci/util.sh | 4 | ||||
-rw-r--r-- | lib/common-functions.sh | 4 | ||||
-rwxr-xr-x | lib/python/apex-python-utils.py | 197 | ||||
-rwxr-xr-x | tests/python-coverage.sh | 23 |
6 files changed, 172 insertions, 109 deletions
diff --git a/ci/clean.sh b/ci/clean.sh index 368ce8bc..e5b2f91c 100755 --- a/ci/clean.sh +++ b/ci/clean.sh @@ -14,9 +14,11 @@ # Use default if no param passed CONFIG=${CONFIG:-'/var/opt/opnfv'} +RESOURCES=${RESOURCES:-"$CONFIG/images"} +LIB=${LIB:-"$CONFIG/lib"} ##LIBRARIES -source $CONFIG/lib/common-functions.sh +source $LIB/common-functions.sh vm_index=4 ovs_bridges="br-admin br-private br-public br-storage" diff --git a/ci/deploy.sh b/ci/deploy.sh index a3b17baf..b99a5693 100755 --- a/ci/deploy.sh +++ b/ci/deploy.sh @@ -37,9 +37,11 @@ declare -A NET_MAP SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error) DEPLOY_OPTIONS="" -RESOURCES=${RESOURCES:-'/var/opt/opnfv/images'} CONFIG=${CONFIG:-'/var/opt/opnfv'} +RESOURCES=${RESOURCES:-"$CONFIG/images"} +LIB=${LIB:-"$CONFIG/lib"} OPNFV_NETWORK_TYPES="admin_network private_network public_network storage_network" + VM_CPUS=4 VM_RAM=8 VM_COMPUTES=2 @@ -52,6 +54,11 @@ NET_MAP['storage_network']="br-storage" ext_net_type="interface" ip_address_family=4 +# Libraries +source $LIB/common-functions.sh +source $LIB/utility-functions.sh +source $LIB/installer/onos/onos_gw_mac_update.sh + ##FUNCTIONS ##translates yaml into variables ##params: filename, prefix (ex. "config_") @@ -108,7 +115,7 @@ parse_setting_value() { ##parses network settings yaml into globals parse_network_settings() { local output - if output=$(python3.4 -B $CONFIG/lib/python/apex-python-utils.py parse_net_settings -n $NETSETS -i $net_isolation_enabled); then + if output=$(python3.4 -B $LIB/python/apex-python-utils.py parse-net-settings -f $NETSETS -i $net_isolation_enabled); then echo -e "${blue}${output}${reset}" eval "$output" else @@ -120,7 +127,7 @@ parse_network_settings() { ##parses deploy settings yaml into globals parse_deploy_settings() { local output - if output=$(python3.4 -B $CONFIG/lib/python/apex-python-utils.py parse-deploy-settings -f $DEPLOY_SETTINGS_FILE); then + if output=$(python3.4 -B $LIB/python/apex-python-utils.py parse-deploy-settings -f $DEPLOY_SETTINGS_FILE); then echo -e "${blue}${output}${reset}" eval "$output" else @@ -660,12 +667,12 @@ function configure_undercloud { echo -e "${blue}Network Environment set for Deployment: ${reset}" cat $CONFIG/network-environment.yaml scp ${SSH_OPTIONS[@]} $CONFIG/network-environment.yaml "stack@$UNDERCLOUD": - if ! controller_nic_template=$(python3.4 -B $CONFIG/lib/python/apex-python-utils.py nic_template -d $CONFIG -f nics-controller.yaml.jinja2 -n "$enabled_network_list" -e $ext_net_type -af $ip_addr_family); then + if ! controller_nic_template=$(python3.4 -B $LIB/python/apex-python-utils.py nic-template -t $CONFIG/nics-controller.yaml.jinja2 -n "$enabled_network_list" -e $ext_net_type -af $ip_addr_family); then echo -e "${red}ERROR: Failed to generate controller NIC heat template ${reset}" exit 1 fi - if ! compute_nic_template=$(python3.4 -B $CONFIG/lib/python/apex-python-utils.py nic_template -d $CONFIG -f nics-compute.yaml.jinja2 -n "$enabled_network_list" -e $ext_net_type -af $ip_addr_family); then + if ! compute_nic_template=$(python3.4 -B $LIB/python/apex-python-utils.py nic-template -t $CONFIG/nics-compute.yaml.jinja2 -n "$enabled_network_list" -e $ext_net_type -af $ip_addr_family); then echo -e "${red}ERROR: Failed to generate compute NIC heat template ${reset}" exit 1 fi @@ -1053,15 +1060,13 @@ EOI display_usage() { echo -e "Usage:\n$0 [arguments] \n" - echo -e " -c|--config : Directory to configuration files. Optional. Defaults to /var/opt/opnfv/ \n" - echo -e " -d|--deploy-settings : Full path to deploy settings yaml file. Optional. Defaults to null \n" - echo -e " -i|--inventory : Full path to inventory yaml file. Required only for baremetal \n" - echo -e " -n|--net-settings : Full path to network settings file. Optional. \n" - echo -e " -p|--ping-site : site to use to verify IP connectivity. Optional. Defaults to 8.8.8.8 \n" - echo -e " -r|--resources : Directory to deployment resources. Optional. Defaults to /var/opt/opnfv/stack \n" - echo -e " -v|--virtual : Virtualize overcloud nodes instead of using baremetal. \n" - echo -e " --no-ha : disable High Availability deployment scheme, this assumes a single controller and single compute node \n" - echo -e " --flat : disable Network Isolation and use a single flat network for the underlay network.\n" + echo -e " -d|--deploy-settings : Full path to deploy settings yaml file. Optional. Defaults to null" + echo -e " -i|--inventory : Full path to inventory yaml file. Required only for baremetal" + echo -e " -n|--net-settings : Full path to network settings file. Optional." + echo -e " -p|--ping-site : site to use to verify IP connectivity. Optional. Defaults to 8.8.8.8" + echo -e " -v|--virtual : Virtualize overcloud nodes instead of using baremetal." + echo -e " --no-ha : disable High Availability deployment scheme, this assumes a single controller and single compute node" + echo -e " --flat : disable Network Isolation and use a single flat network for the underlay network." echo -e " --no-post-config : disable Post Install configuration." echo -e " --debug : enable debug output." echo -e " --interactive : enable interactive deployment mode which requires user to confirm steps of deployment." @@ -1084,11 +1089,6 @@ parse_cmdline() { display_usage exit 0 ;; - -c|--config) - CONFIG=$2 - echo "Deployment Configuration Directory Overridden to: $2" - shift 2 - ;; -d|--deploy-settings) DEPLOY_SETTINGS_FILE=$2 echo "Deployment Configuration file: $2" @@ -1108,11 +1108,6 @@ parse_cmdline() { echo "Using $2 as the ping site" shift 2 ;; - -r|--resources) - RESOURCES=$2 - echo "Deployment Resources Directory Overridden to: $2" - shift 2 - ;; -v|--virtual) virtual="TRUE" echo "Executing a Virtual Deployment" @@ -1202,12 +1197,6 @@ parse_cmdline() { post_config="FALSE" fi - ##LIBRARIES - # Do this after cli parse so that $CONFIG is set properly - source $CONFIG/lib/common-functions.sh - source $CONFIG/lib/utility-functions.sh - source $CONFIG/lib/installer/onos/onos_gw_mac_update.sh - } ##END FUNCTIONS @@ -3,9 +3,11 @@ # @author Tim Rozet (trozet@redhat.com) CONFIG=${CONFIG:-'/var/opt/opnfv'} +RESOURCES=${RESOURCES:-"$CONFIG/images"} +LIB=${LIB:-"$CONFIG/lib"} VALID_CMDS="undercloud debug-stack -h --help" -source $CONFIG/lib/utility-functions.sh +source $LIB/utility-functions.sh resolve_cmd() { local given=$1 diff --git a/lib/common-functions.sh b/lib/common-functions.sh index dfac008e..6b259ac1 100644 --- a/lib/common-functions.sh +++ b/lib/common-functions.sh @@ -18,7 +18,7 @@ function find_ip { return 1 fi - python3.4 -B $CONFIG/lib/python/apex-python-utils.py find_ip -i $1 + python3.4 -B $LIB/python/apex-python-utils.py find-ip -i $1 } ##attach interface to OVS and set the network config correctly @@ -154,7 +154,7 @@ function detach_interface_from_ovs { if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge}) if [ -z "$if_mask" ]; then - if_prefix=$(sed -n 's/^PREFIX=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge}) + if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${net_path}/ifcfg-${bridge}) if_mask=$(prefix2mask ${if_prefix}) fi 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) diff --git a/tests/python-coverage.sh b/tests/python-coverage.sh new file mode 100755 index 00000000..2814363e --- /dev/null +++ b/tests/python-coverage.sh @@ -0,0 +1,23 @@ +#!/bin/bash +set -x +all_networks="admin_network private_network storage_network external_network" + +# exercise help +coverage-3.4 run ../lib/python/apex-python-utils.py -l /dev/null > /dev/null + +# exercise parse-net-settings +# throw debug on the first to exercise it +coverage-3.4 run -a ../lib/python/apex-python-utils.py --debug parse-net-settings -f ../config/network/network_settings.yaml -i True > /dev/null + +# exercise proper nic-template runs +coverage-3.4 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e interface -af 4 > /dev/null +coverage-3.4 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e interface -af 6 > /dev/null +coverage-3.4 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e br-ex -af 4 > /dev/null +coverage-3.4 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e br-ex -af 6 > /dev/null + +# exercise find-ip +coverage-3.4 run -a ../lib/python/apex-python-utils.py -l /dev/null find-ip -i $(ip a | grep 2: | cut -d \ -f 2 | head -n 1 | cut -d : -f 1) > /dev/null + +# generate reports +coverage-3.4 report --include '*lib/python/*' -m +coverage-3.4 html --include '*lib/python/*' |