From 68fc16c4fc98d26bbb58cda6ce7945c2ac6d79bc Mon Sep 17 00:00:00 2001 From: Tim Rozet Date: Tue, 31 May 2016 17:40:11 -0400 Subject: Migrates network environment parsing to python Change-Id: Ibaf20a2960a9f4b5e3f256fbed12a61d7606a967 Signed-off-by: Tim Rozet --- build/opnfv-apex-common.spec | 5 +- ci/deploy.sh | 60 ++------ lib/python/apex-python-utils.py | 25 +++- lib/python/apex/__init__.py | 3 +- lib/python/apex/common/__init__.py | 0 lib/python/apex/common/constants.py | 16 ++ lib/python/apex/common/utils.py | 15 ++ lib/python/apex/net_env.py | 256 ------------------------------- lib/python/apex/network_environment.py | 135 +++++++++++++++++ lib/python/apex/network_settings.py | 266 +++++++++++++++++++++++++++++++++ tests/python-coverage.sh | 2 +- 11 files changed, 468 insertions(+), 315 deletions(-) create mode 100644 lib/python/apex/common/__init__.py create mode 100644 lib/python/apex/common/constants.py create mode 100644 lib/python/apex/common/utils.py delete mode 100644 lib/python/apex/net_env.py create mode 100644 lib/python/apex/network_environment.py create mode 100644 lib/python/apex/network_settings.py diff --git a/build/opnfv-apex-common.spec b/build/opnfv-apex-common.spec index 6915b795..464bae91 100644 --- a/build/opnfv-apex-common.spec +++ b/build/opnfv-apex-common.spec @@ -47,10 +47,7 @@ install lib/common-functions.sh %{buildroot}%{_var}/opt/opnfv/lib/ install lib/utility-functions.sh %{buildroot}%{_var}/opt/opnfv/lib/ install lib/python/apex-python-utils.py %{buildroot}%{_var}/opt/opnfv/lib/python/ mkdir -p %{buildroot}%{python3_sitelib}/apex/ -install lib/python/apex/__init__.py %{buildroot}%{python3_sitelib}/apex/ -install lib/python/apex/ip_utils.py %{buildroot}%{python3_sitelib}/apex/ -install lib/python/apex/net_env.py %{buildroot}%{python3_sitelib}/apex/ -install lib/python/apex/deploy_env.py %{buildroot}%{python3_sitelib}/apex/ +install lib/python/apex/ %{buildroot}%{python3_sitelib}/apex/ mkdir -p %{buildroot}%{_var}/opt/opnfv/lib/installer/onos/ install lib/installer/onos/onos_gw_mac_update.sh %{buildroot}%{_var}/opt/opnfv/lib/installer/onos/ diff --git a/ci/deploy.sh b/ci/deploy.sh index bb4c1f81..a40b92a2 100755 --- a/ci/deploy.sh +++ b/ci/deploy.sh @@ -115,7 +115,7 @@ parse_setting_value() { ##parses network settings yaml into globals parse_network_settings() { local output - if output=$(python3.4 -B $LIB/python/apex-python-utils.py parse-net-settings -f $NETSETS -i $net_isolation_enabled); then + if output=$(python3.4 -B $LIB/python/apex-python-utils.py parse-net-settings -s $NETSETS -i $net_isolation_enabled -e $CONFIG/network-environment.yaml); then echo -e "${blue}${output}${reset}" eval "$output" else @@ -609,53 +609,6 @@ function define_vm () { --baremetal-interface $4 } -##Set network-environment settings -##params: network-environment file to edit -function configure_network_environment { - local tht_dir - tht_dir=/usr/share/openstack-tripleo-heat-templates/network - - sed -i '/ControlPlaneSubnetCidr/c\\ ControlPlaneSubnetCidr: "'${admin_network_cidr##*/}'"' $1 - sed -i '/ControlPlaneDefaultRoute/c\\ ControlPlaneDefaultRoute: '${admin_network_provisioner_ip}'' $1 - sed -i '/ExternalNetCidr/c\\ ExternalNetCidr: '${public_network_cidr}'' $1 - sed -i "/ExternalAllocationPools/c\\ ExternalAllocationPools: [{'start': '${public_network_usable_ip_range%%,*}', 'end': '${public_network_usable_ip_range##*,}'}]" $1 - sed -i '/ExternalInterfaceDefaultRoute/c\\ ExternalInterfaceDefaultRoute: '${public_network_gateway}'' $1 - sed -i '/EC2MetadataIp/c\\ EC2MetadataIp: '${admin_network_provisioner_ip}'' $1 - - # check for private network - if [[ ! -z "$private_network_enabled" && "$private_network_enabled" == "True" ]]; then - sed -i 's#^.*Network::Tenant.*$# OS::TripleO::Network::Tenant: '${tht_dir}'/tenant.yaml#' $1 - sed -i 's#^.*Controller::Ports::TenantPort:.*$# OS::TripleO::Controller::Ports::TenantPort: '${tht_dir}'/ports/tenant.yaml#' $1 - sed -i 's#^.*Compute::Ports::TenantPort:.*$# OS::TripleO::Compute::Ports::TenantPort: '${tht_dir}'/ports/tenant.yaml#' $1 - sed -i "/TenantAllocationPools/c\\ TenantAllocationPools: [{'start': '${private_network_usable_ip_range%%,*}', 'end': '${private_network_usable_ip_range##*,}'}]" $1 - sed -i '/TenantNetCidr/c\\ TenantNetCidr: '${private_network_cidr}'' $1 - else - sed -i 's#^.*Network::Tenant.*$# OS::TripleO::Network::Tenant: '${tht_dir}'/noop.yaml#' $1 - sed -i 's#^.*Controller::Ports::TenantPort:.*$# OS::TripleO::Controller::Ports::TenantPort: '${tht_dir}'/ports/noop.yaml#' $1 - sed -i 's#^.*Compute::Ports::TenantPort:.*$# OS::TripleO::Compute::Ports::TenantPort: '${tht_dir}'/ports/noop.yaml#' $1 - fi - - # check for storage network - if [[ ! -z "$storage_network_enabled" && "$storage_network_enabled" == "True" ]]; then - sed -i 's#^.*Network::Storage:.*$# OS::TripleO::Network::Storage: '${tht_dir}'/storage.yaml#' $1 - sed -i 's#^.*Network::Ports::StorageVipPort:.*$# OS::TripleO::Network::Ports::StorageVipPort: '${tht_dir}'/ports/storage.yaml#' $1 - sed -i 's#^.*Controller::Ports::StoragePort:.*$# OS::TripleO::Controller::Ports::StoragePort: '${tht_dir}'/ports/storage.yaml#' $1 - sed -i 's#^.*Compute::Ports::StoragePort:.*$# OS::TripleO::Compute::Ports::StoragePort: '${tht_dir}'/ports/storage.yaml#' $1 - sed -i "/StorageAllocationPools/c\\ StorageAllocationPools: [{'start': '${storage_network_usable_ip_range%%,*}', 'end': '${storage_network_usable_ip_range##*,}'}]" $1 - sed -i '/StorageNetCidr/c\\ StorageNetCidr: '${storage_network_cidr}'' $1 - else - sed -i 's#^.*Network::Storage:.*$# OS::TripleO::Network::Storage: '${tht_dir}'/noop.yaml#' $1 - sed -i 's#^.*Network::Ports::StorageVipPort:.*$# OS::TripleO::Network::Ports::StorageVipPort: '${tht_dir}'/ports/noop.yaml#' $1 - sed -i 's#^.*Controller::Ports::StoragePort:.*$# OS::TripleO::Controller::Ports::StoragePort: '${tht_dir}'/ports/noop.yaml#' $1 - sed -i 's#^.*Compute::Ports::StoragePort:.*$# OS::TripleO::Compute::Ports::StoragePort: '${tht_dir}'/ports/noop.yaml#' $1 - fi - - # check for ODL L3 - if [ "${deploy_options_array['sdn_l3']}" == 'True' ]; then - ext_net_type=br-ex - fi - -} ##Copy over the glance images and instackenv json file ##params: none function configure_undercloud { @@ -663,10 +616,15 @@ function configure_undercloud { echo echo "Copying configuration files to Undercloud" if [[ "$net_isolation_enabled" == "TRUE" ]]; then - configure_network_environment $CONFIG/network-environment.yaml echo -e "${blue}Network Environment set for Deployment: ${reset}" - cat $CONFIG/network-environment.yaml - scp ${SSH_OPTIONS[@]} $CONFIG/network-environment.yaml "stack@$UNDERCLOUD": + cat /tmp/network-environment.yaml + scp ${SSH_OPTIONS[@]} /tmp/network-environment.yaml "stack@$UNDERCLOUD": + + # check for ODL L3/ONOS + if [ "${deploy_options_array['sdn_l3']}" == 'True' ]; then + ext_net_type=br-ex + fi + 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 diff --git a/lib/python/apex-python-utils.py b/lib/python/apex-python-utils.py index 4eee4f75..6c76cb13 100755 --- a/lib/python/apex-python-utils.py +++ b/lib/python/apex-python-utils.py @@ -12,6 +12,7 @@ import sys import apex import logging import os +import yaml from jinja2 import Environment, FileSystemLoader @@ -26,10 +27,24 @@ def parse_net_settings(args): - network_isolation: bool enable or disable network_isolation """ - settings = apex.NetworkSettings(args.file, + settings = apex.NetworkSettings(args.net_settings_file, args.network_isolation) + net_env = apex.NetworkEnvironment(settings, args.net_env_file) + dump_yaml(net_env.get_netenv_settings(), '/tmp/network-environment.yaml') settings.dump_bash() + +def dump_yaml(data, file): + """ + Dumps data to a file as yaml + :param data: yaml to be written to file + :param file: filename to write to + :return: + """ + with open(file, "w") as fh: + yaml.dump(data, fh, default_flow_style=False) + + def parse_deploy_settings(args): settings = apex.DeploySettings(args.file) settings.dump_bash() @@ -84,11 +99,17 @@ def parse_args(): net_settings = subparsers.add_parser('parse-net-settings', help='Parse network settings file') - net_settings.add_argument('-f', '--file', default='network-settings.yaml', + net_settings.add_argument('-s', '--net-settings-file', + default='network-settings.yaml', + dest='net_settings_file', 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.add_argument('-e', '--net-env-file', + default="network-environment.yaml", + dest='net_env_file', + help='path to network environment file') net_settings.set_defaults(func=parse_net_settings) get_int_ip = subparsers.add_parser('find-ip', diff --git a/lib/python/apex/__init__.py b/lib/python/apex/__init__.py index 2efc64f4..5b158501 100644 --- a/lib/python/apex/__init__.py +++ b/lib/python/apex/__init__.py @@ -8,5 +8,6 @@ ############################################################################## -from .net_env import NetworkSettings +from .network_settings import NetworkSettings from .deploy_env import DeploySettings +from .network_environment import NetworkEnvironment diff --git a/lib/python/apex/common/__init__.py b/lib/python/apex/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lib/python/apex/common/constants.py b/lib/python/apex/common/constants.py new file mode 100644 index 00000000..506b7b2b --- /dev/null +++ b/lib/python/apex/common/constants.py @@ -0,0 +1,16 @@ +############################################################################## +# Copyright (c) 2016 Tim Rozet (trozet@redhat.com) and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +ADMIN_NETWORK = 'admin_network' +PRIVATE_NETWORK = 'private_network' +PUBLIC_NETWORK = 'public_network' +STORAGE_NETWORK = 'storage_network' +API_NETWORK = 'api_network' +OPNFV_NETWORK_TYPES = [ADMIN_NETWORK, PRIVATE_NETWORK, PUBLIC_NETWORK, + STORAGE_NETWORK, API_NETWORK] diff --git a/lib/python/apex/common/utils.py b/lib/python/apex/common/utils.py new file mode 100644 index 00000000..b7678a20 --- /dev/null +++ b/lib/python/apex/common/utils.py @@ -0,0 +1,15 @@ +############################################################################## +# Copyright (c) 2016 Tim Rozet (trozet@redhat.com) and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +def str2bool(var): + if isinstance(var, bool): + return var + else: + return var.lower() in ("true", "yes") diff --git a/lib/python/apex/net_env.py b/lib/python/apex/net_env.py deleted file mode 100644 index 3ca28f8a..00000000 --- a/lib/python/apex/net_env.py +++ /dev/null @@ -1,256 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Feng Pan (fpan@redhat.com) and others. -# -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## - - -import yaml -import logging -import ipaddress -from . import ip_utils - - -ADMIN_NETWORK = 'admin_network' -PRIVATE_NETWORK = 'private_network' -PUBLIC_NETWORK = 'public_network' -STORAGE_NETWORK = 'storage_network' -API_NETWORK = 'api_network' -OPNFV_NETWORK_TYPES = [ADMIN_NETWORK, PRIVATE_NETWORK, PUBLIC_NETWORK, - STORAGE_NETWORK, API_NETWORK] - - -class NetworkSettings: - """ - This class parses APEX network settings yaml file into an object. It - generates or detects all missing fields for deployment. - - The resulting object will be used later to generate network environment file - as well as configuring post deployment networks. - - Currently the parsed object is dumped into a bash global definition file - for deploy.sh consumption. This object will later be used directly as - deployment script move to python. - """ - def __init__(self, filename, network_isolation): - with open(filename, 'r') as network_settings_file: - self.settings_obj = yaml.load(network_settings_file) - self.network_isolation = network_isolation - self.enabled_network_list = [] - self._validate_input() - - def _validate_input(self): - """ - Validates the network settings file and populates all fields. - - NetworkSettingsException will be raised if validation fails. - """ - if ADMIN_NETWORK not in self.settings_obj or \ - self.settings_obj[ADMIN_NETWORK].get('enabled') != True: - raise NetworkSettingsException("You must enable admin_network " - "and configure it explicitly or " - "use auto-detection") - if self.network_isolation and \ - (PUBLIC_NETWORK not in self.settings_obj or - self.settings_obj[PUBLIC_NETWORK].get('enabled') != True): - raise NetworkSettingsException("You must enable public_network " - "and configure it explicitly or " - "use auto-detection") - - for network in OPNFV_NETWORK_TYPES: - if network in self.settings_obj: - if self.settings_obj[network].get('enabled') == True: - logging.info("{} enabled".format(network)) - self._config_required_settings(network) - self._config_ip_range(network=network, - setting='usable_ip_range', - start_offset=21, end_offset=21) - self._config_optional_settings(network) - self.enabled_network_list.append(network) - else: - logging.info("{} disabled, will collapse with " - "admin_network".format(network)) - else: - logging.info("{} is not in specified, will collapse with " - "admin_network".format(network)) - - def _config_required_settings(self, network): - """ - Configures either CIDR or bridged_interface setting - - cidr takes precedence if both cidr and bridged_interface are specified - for a given network. - - When using bridged_interface, we will detect network setting on the - given NIC in the system. The resulting config in settings object will - be an ipaddress.network object, replacing the NIC name. - """ - cidr = self.settings_obj[network].get('cidr') - nic_name = self.settings_obj[network].get('bridged_interface') - - if cidr: - cidr = ipaddress.ip_network(self.settings_obj[network]['cidr']) - self.settings_obj[network]['cidr'] = cidr - logging.info("{}_cidr: {}".format(network, cidr)) - return 0 - elif nic_name: - # If cidr is not specified, we need to know if we should find - # IPv6 or IPv4 address on the interface - if self.settings_obj[network].get('ipv6') == True: - address_family = 6 - else: - address_family = 4 - nic_interface = ip_utils.get_interface(nic_name, address_family) - if nic_interface: - self.settings_obj[network]['bridged_interface'] = nic_interface - logging.info("{}_bridged_interface: {}". - format(network, nic_interface)) - return 0 - else: - raise NetworkSettingsException("Auto detection failed for {}: " - "Unable to find valid ip for " - "interface {}" - .format(network, nic_name)) - - else: - raise NetworkSettingsException("Auto detection failed for {}: " - "either bridge_interface or cidr " - "must be specified" - .format(network)) - - def _config_ip_range(self, network, setting, start_offset=None, - end_offset=None, count=None): - """ - Configures IP range for a given setting. - - If the setting is already specified, no change will be made. - - The spec for start_offset, end_offset and count are identical to - ip_utils.get_ip_range. - """ - ip_range = self.settings_obj[network].get(setting) - interface = self.settings_obj[network].get('bridged_interface') - - if not ip_range: - cidr = self.settings_obj[network].get('cidr') - ip_range = ip_utils.get_ip_range(start_offset=start_offset, - end_offset=end_offset, - count=count, - cidr=cidr, - interface=interface) - self.settings_obj[network][setting] = ip_range - - logging.info("{}_{}: {}".format(network, setting, ip_range)) - - def _config_ip(self, network, setting, offset): - """ - Configures IP for a given setting. - - If the setting is already specified, no change will be made. - - The spec for offset is identical to ip_utils.get_ip - """ - ip = self.settings_obj[network].get(setting) - interface = self.settings_obj[network].get('bridged_interface') - - if not ip: - cidr = self.settings_obj[network].get('cidr') - ip = ip_utils.get_ip(offset, cidr, interface) - self.settings_obj[network][setting] = ip - - logging.info("{}_{}: {}".format(network, setting, ip)) - - def _config_optional_settings(self, network): - """ - Configures optional settings: - - admin_network: - - provisioner_ip - - dhcp_range - - introspection_range - - public_network: - - provisioner_ip - - floating_ip - - gateway - """ - if network == ADMIN_NETWORK: - self._config_ip(network, 'provisioner_ip', 1) - self._config_ip_range(network=network, setting='dhcp_range', - start_offset=2, count=9) - self._config_ip_range(network=network, - setting='introspection_range', - start_offset=11, count=9) - elif network == PUBLIC_NETWORK: - self._config_ip(network, 'provisioner_ip', 1) - self._config_ip_range(network=network, - setting='floating_ip', - end_offset=2, count=20) - self._config_gateway(network) - - def _config_gateway(self, network): - """ - Configures gateway setting for a given network. - - If cidr is specified, we always use the first address in the address - space for gateway. Otherwise, we detect the system gateway. - """ - gateway = self.settings_obj[network].get('gateway') - interface = self.settings_obj[network].get('bridged_interface') - - if not gateway: - cidr = self.settings_obj[network].get('cidr') - if cidr: - gateway = ip_utils.get_ip(1, cidr) - else: - gateway = ip_utils.find_gateway(interface) - - if gateway: - self.settings_obj[network]['gateway'] = gateway - else: - raise NetworkSettingsException("Failed to set gateway") - - logging.info("{}_gateway: {}".format(network, gateway)) - - - def dump_bash(self, path=None): - """ - Prints settings for bash consumption. - - If optional path is provided, bash string will be written to the file - instead of stdout. - """ - bash_str = '' - for network in self.enabled_network_list: - for key, value in self.settings_obj[network].items(): - 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): - self.value = value - - def __str__(self): - return self.value diff --git a/lib/python/apex/network_environment.py b/lib/python/apex/network_environment.py new file mode 100644 index 00000000..e6f0135a --- /dev/null +++ b/lib/python/apex/network_environment.py @@ -0,0 +1,135 @@ +############################################################################## +# Copyright (c) 2016 Tim Rozet (trozet@redhat.com) and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import yaml +import re +from .common import constants + +PORTS = '/ports' +# Resources defined by : +TENANT_RESOURCES = {'OS::TripleO::Network::Tenant': None, + 'OS::TripleO::Controller::Ports::TenantPort': PORTS, + 'OS::TripleO::Compute::Ports::TenantPort': PORTS} +STORAGE_RESOURCES = {'OS::TripleO::Network::Storage': None, + 'OS::TripleO::Network::Ports::StorageVipPort': PORTS, + 'OS::TripleO::Controller::Ports::StoragePort': PORTS, + 'OS::TripleO::Compute::Ports::StoragePort': PORTS} + + +class NetworkEnvironment: + """ + This class creates a Network Environment to be used in TripleO Heat + Templates. + + The class builds upon an existing network-environment file and modifies + based on a NetworkSettings object. + """ + def __init__(self, net_settings, filename): + with open(filename, 'r') as net_env_fh: + self.netenv_obj = yaml.load(net_env_fh) + if net_settings: + settings_obj = net_settings.get_network_settings() + enabled_networks = net_settings.get_enabled_networks() + self.netenv_obj = \ + self._update_net_environment(settings_obj, + enabled_networks) + else: + raise NetworkEnvException("Network Settings does not exist") + + def _update_net_environment(self, net_settings, enabled_networks): + """ + Updates Network Environment according to Network Settings + :param: network settings dictionary + :param: enabled network list + :return: None + """ + param_def = 'parameter_defaults' + reg = 'resource_registry' + for key, prefix in TENANT_RESOURCES.items(): + if prefix is None: + prefix = '' + m = re.split('%s/\w+\.yaml' % prefix, self.netenv_obj[reg][key]) + if m is not None: + tht_dir = m[0] + break + if not tht_dir: + raise NetworkEnvException('Unable to parse THT Directory') + admin_cidr = net_settings[constants.ADMIN_NETWORK]['cidr'] + admin_prefix = str(admin_cidr.prefixlen) + self.netenv_obj[param_def]['ControlPlaneSubnetCidr'] = admin_prefix + self.netenv_obj[param_def]['ControlPlaneDefaultRoute'] = \ + net_settings[constants.ADMIN_NETWORK]['provisioner_ip'] + public_cidr = net_settings[constants.PUBLIC_NETWORK]['cidr'] + self.netenv_obj[param_def]['ExternalNetCidr'] = str(public_cidr) + public_range = net_settings[constants.PUBLIC_NETWORK][ + 'usable_ip_range'].split(',') + self.netenv_obj[param_def]['ExternalAllocationPools'] = \ + [{'start': + public_range[0], + 'end': public_range[1] + }] + self.netenv_obj[param_def]['ExternalInterfaceDefaultRoute'] = \ + net_settings[constants.PUBLIC_NETWORK]['gateway'] + self.netenv_obj[param_def]['EC2MetadataIp'] = \ + net_settings[constants.ADMIN_NETWORK]['provisioner_ip'] + + if constants.PRIVATE_NETWORK in enabled_networks: + priv_range = net_settings[constants.PRIVATE_NETWORK][ + 'usable_ip_range'].split(',') + self.netenv_obj[param_def]['TenantAllocationPools'] = \ + [{'start': + priv_range[0], + 'end': priv_range[1] + }] + priv_cidr = net_settings[constants.PRIVATE_NETWORK]['cidr'] + self.netenv_obj[param_def]['TenantNetCidr'] = str(priv_cidr) + postfix = '/tenant.yaml' + else: + postfix = '/noop.yaml' + + for key, prefix in TENANT_RESOURCES.items(): + if prefix is None: + prefix = '' + self.netenv_obj[reg][key] = tht_dir + prefix + postfix + + if constants.STORAGE_NETWORK in enabled_networks: + storage_range = net_settings[constants.STORAGE_NETWORK][ + 'usable_ip_range'].split(',') + self.netenv_obj[param_def]['StorageAllocationPools'] = \ + [{'start': + storage_range[0], + 'end': + storage_range[1] + }] + storage_cidr = net_settings[constants.STORAGE_NETWORK]['cidr'] + self.netenv_obj[param_def]['StorageNetCidr'] = str(storage_cidr) + postfix = '/storage.yaml' + else: + postfix = '/noop.yaml' + + for key, prefix in STORAGE_RESOURCES.items(): + if prefix is None: + prefix = '' + self.netenv_obj[reg][key] = tht_dir + prefix + postfix + return self.netenv_obj + + def get_netenv_settings(self): + """ + Getter for netenv settings + :return: Dictionary of network environment settings + """ + return self.netenv_obj + + +class NetworkEnvException(Exception): + def __init__(self, value): + self.value = value + + def __str__(self): + return self.value diff --git a/lib/python/apex/network_settings.py b/lib/python/apex/network_settings.py new file mode 100644 index 00000000..ea7f4ca9 --- /dev/null +++ b/lib/python/apex/network_settings.py @@ -0,0 +1,266 @@ +############################################################################## +# Copyright (c) 2016 Feng Pan (fpan@redhat.com) and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import yaml +import logging +import ipaddress +from . import ip_utils +from .common import constants, utils + + +class NetworkSettings: + """ + This class parses APEX network settings yaml file into an object. It + generates or detects all missing fields for deployment. + + The resulting object will be used later to generate network environment file + as well as configuring post deployment networks. + + Currently the parsed object is dumped into a bash global definition file + for deploy.sh consumption. This object will later be used directly as + deployment script move to python. + """ + def __init__(self, filename, network_isolation): + with open(filename, 'r') as network_settings_file: + self.settings_obj = yaml.load(network_settings_file) + self.network_isolation = network_isolation + self.enabled_network_list = [] + self._validate_input() + + def _validate_input(self): + """ + Validates the network settings file and populates all fields. + + NetworkSettingsException will be raised if validation fails. + """ + if constants.ADMIN_NETWORK not in self.settings_obj or \ + not utils.str2bool(self.settings_obj[constants.ADMIN_NETWORK].get( + 'enabled')): + raise NetworkSettingsException("You must enable admin_network " + "and configure it explicitly or " + "use auto-detection") + if self.network_isolation and \ + (constants.PUBLIC_NETWORK not in self.settings_obj or not + utils.str2bool(self.settings_obj[constants.PUBLIC_NETWORK].get( + 'enabled'))): + raise NetworkSettingsException("You must enable public_network " + "and configure it explicitly or " + "use auto-detection") + + for network in constants.OPNFV_NETWORK_TYPES: + if network in self.settings_obj: + if utils.str2bool(self.settings_obj[network].get('enabled')): + logging.info("{} enabled".format(network)) + self._config_required_settings(network) + self._config_ip_range(network=network, + setting='usable_ip_range', + start_offset=21, end_offset=21) + self._config_optional_settings(network) + self.enabled_network_list.append(network) + else: + logging.info("{} disabled, will collapse with " + "admin_network".format(network)) + else: + logging.info("{} is not in specified, will collapse with " + "admin_network".format(network)) + + def _config_required_settings(self, network): + """ + Configures either CIDR or bridged_interface setting + + cidr takes precedence if both cidr and bridged_interface are specified + for a given network. + + When using bridged_interface, we will detect network setting on the + given NIC in the system. The resulting config in settings object will + be an ipaddress.network object, replacing the NIC name. + """ + cidr = self.settings_obj[network].get('cidr') + nic_name = self.settings_obj[network].get('bridged_interface') + + if cidr: + cidr = ipaddress.ip_network(self.settings_obj[network]['cidr']) + self.settings_obj[network]['cidr'] = cidr + logging.info("{}_cidr: {}".format(network, cidr)) + return 0 + elif nic_name: + # If cidr is not specified, we need to know if we should find + # IPv6 or IPv4 address on the interface + if utils.str2bool(self.settings_obj[network].get('ipv6')): + address_family = 6 + else: + address_family = 4 + nic_interface = ip_utils.get_interface(nic_name, address_family) + if nic_interface: + self.settings_obj[network]['bridged_interface'] = nic_interface + logging.info("{}_bridged_interface: {}". + format(network, nic_interface)) + return 0 + else: + raise NetworkSettingsException("Auto detection failed for {}: " + "Unable to find valid ip for " + "interface {}" + .format(network, nic_name)) + + else: + raise NetworkSettingsException("Auto detection failed for {}: " + "either bridge_interface or cidr " + "must be specified" + .format(network)) + + def _config_ip_range(self, network, setting, start_offset=None, + end_offset=None, count=None): + """ + Configures IP range for a given setting. + + If the setting is already specified, no change will be made. + + The spec for start_offset, end_offset and count are identical to + ip_utils.get_ip_range. + """ + ip_range = self.settings_obj[network].get(setting) + interface = self.settings_obj[network].get('bridged_interface') + + if not ip_range: + cidr = self.settings_obj[network].get('cidr') + ip_range = ip_utils.get_ip_range(start_offset=start_offset, + end_offset=end_offset, + count=count, + cidr=cidr, + interface=interface) + self.settings_obj[network][setting] = ip_range + + logging.info("{}_{}: {}".format(network, setting, ip_range)) + + def _config_ip(self, network, setting, offset): + """ + Configures IP for a given setting. + + If the setting is already specified, no change will be made. + + The spec for offset is identical to ip_utils.get_ip + """ + ip = self.settings_obj[network].get(setting) + interface = self.settings_obj[network].get('bridged_interface') + + if not ip: + cidr = self.settings_obj[network].get('cidr') + ip = ip_utils.get_ip(offset, cidr, interface) + self.settings_obj[network][setting] = ip + + logging.info("{}_{}: {}".format(network, setting, ip)) + + def _config_optional_settings(self, network): + """ + Configures optional settings: + - admin_network: + - provisioner_ip + - dhcp_range + - introspection_range + - public_network: + - provisioner_ip + - floating_ip + - gateway + """ + if network == constants.ADMIN_NETWORK: + self._config_ip(network, 'provisioner_ip', 1) + self._config_ip_range(network=network, setting='dhcp_range', + start_offset=2, count=9) + self._config_ip_range(network=network, + setting='introspection_range', + start_offset=11, count=9) + elif network == constants.PUBLIC_NETWORK: + self._config_ip(network, 'provisioner_ip', 1) + self._config_ip_range(network=network, + setting='floating_ip', + end_offset=2, count=20) + self._config_gateway(network) + + def _config_gateway(self, network): + """ + Configures gateway setting for a given network. + + If cidr is specified, we always use the first address in the address + space for gateway. Otherwise, we detect the system gateway. + """ + gateway = self.settings_obj[network].get('gateway') + interface = self.settings_obj[network].get('bridged_interface') + + if not gateway: + cidr = self.settings_obj[network].get('cidr') + if cidr: + gateway = ip_utils.get_ip(1, cidr) + else: + gateway = ip_utils.find_gateway(interface) + + if gateway: + self.settings_obj[network]['gateway'] = gateway + else: + raise NetworkSettingsException("Failed to set gateway") + + logging.info("{}_gateway: {}".format(network, gateway)) + + def dump_bash(self, path=None): + """ + Prints settings for bash consumption. + + If optional path is provided, bash string will be written to the file + instead of stdout. + """ + bash_str = '' + for network in self.enabled_network_list: + for key, value in self.settings_obj[network].items(): + 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 + + def get_network_settings(self): + """ + Getter for network settings + :return: network settings dictionary + """ + return self.settings_obj + + def get_enabled_networks(self): + """ + Getter for enabled network list + :return: list of enabled networks + """ + return self.enabled_network_list + + +class NetworkSettingsException(Exception): + def __init__(self, value): + self.value = value + + def __str__(self): + return self.value + + + diff --git a/tests/python-coverage.sh b/tests/python-coverage.sh index 2814363e..ee471446 100755 --- a/tests/python-coverage.sh +++ b/tests/python-coverage.sh @@ -7,7 +7,7 @@ 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 +coverage-3.4 run -a ../lib/python/apex-python-utils.py --debug parse-net-settings -s ../config/network/network_settings.yaml -i True -e ../build/network-environment.yaml> /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 -- cgit 1.2.3-korg