From 248ba4d331fa5bcd90b840d2efb7c16992a3f77b Mon Sep 17 00:00:00 2001 From: asarfaty Date: Wed, 20 Jan 2021 14:18:17 +0200 Subject: [PATCH] V2T migration: Admin utility for enable/disable route redistribution + fixing constant typo in the admin utiities + fixing file handling issue in another v2t migration utility + fixing nsxv migration validate admin utility loading azs Change-Id: I38cdc227b513b5ce6bd271dcc688a68334fa7906 --- doc/source/admin_util.rst | 2 + .../admin/plugins/nsxp/resources/migration.py | 97 +++++++++++++++++++ .../plugins/nsxv/resources/dhcp_binding.py | 2 +- .../admin/plugins/nsxv/resources/migration.py | 3 + .../admin/plugins/nsxv/resources/networks.py | 4 +- .../admin/plugins/nsxv/resources/routers.py | 2 +- vmware_nsx/shell/resources.py | 9 +- 7 files changed, 111 insertions(+), 8 deletions(-) diff --git a/doc/source/admin_util.rst b/doc/source/admin_util.rst index 16f020c9c0..11188f5da2 100644 --- a/doc/source/admin_util.rst +++ b/doc/source/admin_util.rst @@ -708,6 +708,8 @@ Client Certificate nsxadmin -r nsx-migrate-v2t -o clean-all +- Disable/Restore Tier0 redistribution of tier1 routes during the migration:: + nsxadmin -r nsx-migrate-v2t -o nsx-redistribute --property action=disable/restore --property tier0s=a,b,c Steps to create a TVD admin user ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/vmware_nsx/shell/admin/plugins/nsxp/resources/migration.py b/vmware_nsx/shell/admin/plugins/nsxp/resources/migration.py index 759872a5c0..f8b0362c9a 100644 --- a/vmware_nsx/shell/admin/plugins/nsxp/resources/migration.py +++ b/vmware_nsx/shell/admin/plugins/nsxp/resources/migration.py @@ -12,8 +12,11 @@ # License for the specific language governing permissions and limitations # under the License. +import copy + from neutron_lib.callbacks import registry from oslo_log import log as logging +from oslo_serialization import jsonutils from vmware_nsx.shell.admin.plugins.common import constants from vmware_nsx.shell.admin.plugins.common import utils as admin_utils @@ -48,6 +51,96 @@ def post_v2t_migration_cleanups(resource, event, trigger, **kwargs): continue +@admin_utils.output_header +def migration_tier0_redistribute(resource, event, trigger, **kwargs): + """Disable/Restore tier0s route redistribution during V2T migration""" + errmsg = ("Need to specify --property action=disable/restore and a comma " + "separated tier0 list as --property tier0s") + if not kwargs.get('property'): + LOG.error("%s", errmsg) + return + properties = admin_utils.parse_multi_keyval_opt(kwargs['property']) + action = properties.get('action') + tier0string = properties.get('tier0s') + if not tier0string or not action: + LOG.error("%s", errmsg) + return + + tier0s = tier0string.split(",") + nsxpolicy = p_utils.get_connected_nsxpolicy() + file_name = "tier0_redistribution_conf.json" + + if action.lower() == 'disable': + orig_conf_map = {} + for tier0 in tier0s: + # get the current config + try: + orig_conf = nsxpolicy.tier0.get_route_redistribution_config( + tier0) + except Exception: + LOG.error("Did not find Tier0 %s", tier0) + return + fixed_conf = copy.deepcopy(orig_conf) + if ((not orig_conf['bgp_enabled'] and + not orig_conf['ospf_enabled']) or + not orig_conf.get('redistribution_rules')): + # Already disabled + LOG.info("Tier0 %s route redistribution config was not " + "changed because it is disabled", tier0) + continue + # Check if any of the rules have tier1 flags enabled + found = False + rule_num = 0 + for rule in orig_conf['redistribution_rules']: + fixed_types = [] + for route_type in rule['route_redistribution_types']: + if route_type.startswith('TIER1'): + found = True + else: + fixed_types.append(route_type) + fixed_conf['redistribution_rules'][rule_num][ + 'route_redistribution_types'] = fixed_types + rule_num = rule_num + 1 + if not found: + LOG.info("Tier0 %s route redistribution config was not " + "changed because there are no Tier1 types", tier0) + continue + # Save the original config so it can be reverted later + orig_conf_map[tier0] = orig_conf + nsxpolicy.tier0.update_route_redistribution_config( + tier0, fixed_conf) + LOG.info("Disabled Tier0 %s route redistribution config for " + "Tier1 routes", tier0) + f = open(file_name, "w") + f.write("%s" % jsonutils.dumps(orig_conf_map)) + f.close() + + elif action.lower() == 'restore': + try: + f = open(file_name, "r") + orig_conf_map = jsonutils.loads(f.read()) + f.close() + except Exception: + LOG.error("Didn't find input file %s", file_name) + return + for tier0 in tier0s: + if tier0 in orig_conf_map: + # Restore its original config: + try: + nsxpolicy.tier0.update_route_redistribution_config( + tier0, orig_conf_map[tier0]) + LOG.info("Restored Tier0 %s original route redistribution " + "config", tier0) + except Exception: + LOG.error("Failed to update redistribution of Tier0 %s", + tier0) + else: + LOG.info("Tier0 %s route redistribution config was not " + "changed", tier0) + else: + LOG.error("%s", errmsg) + + registry.subscribe(cleanup_db_mappings, constants.NSX_MIGRATE_T_P, shell.Operations.CLEAN_ALL.value) @@ -55,3 +148,7 @@ registry.subscribe(cleanup_db_mappings, registry.subscribe(post_v2t_migration_cleanups, constants.NSX_MIGRATE_V_T, shell.Operations.CLEAN_ALL.value) + +registry.subscribe(migration_tier0_redistribute, + constants.NSX_MIGRATE_V_T, + shell.Operations.NSX_REDISTRIBUTE.value) diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py index b0c0b0ceda..bb4372efe6 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py @@ -353,4 +353,4 @@ registry.subscribe(nsx_recreate_dhcp_edge, shell.Operations.NSX_RECREATE.value) registry.subscribe(nsx_redistribute_dhcp_edges, constants.DHCP_BINDING, - shell.Operations.NSX_REDISTRIBURE.value) + shell.Operations.NSX_REDISTRIBUTE.value) diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py index 33faf3b973..c66ea8ed15 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py @@ -13,6 +13,7 @@ # under the License. import netaddr +from oslo_config import cfg from oslo_log import log as logging from networking_l2gw.db.l2gateway import l2gateway_models @@ -23,6 +24,7 @@ from neutron_lib.callbacks import registry from neutron_lib import constants as nl_constants from neutron_lib import context as n_context +from vmware_nsx.common import config from vmware_nsx.common import nsxv_constants from vmware_nsx.common import utils as c_utils from vmware_nsx.db import nsxv_db @@ -70,6 +72,7 @@ def validate_config_for_migration(resource, event, trigger, **kwargs): n_errors = 0 # General config options / per AZ which are unsupported + config.register_nsxv_azs(cfg.CONF, cfg.CONF.nsxv.availability_zones) zones = nsx_az.NsxVAvailabilityZones() unsupported_configs = ['edge_ha', 'edge_host_groups'] for az in zones.list_availability_zones_objects(): diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py index bdd0a35027..1a1b35feae 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py @@ -335,8 +335,8 @@ def list_nsx_virtual_wires(resource, event, trigger, **kwargs): table_results, ['neutron_id', 'nsx_id', 'vni'])) if filename: - f = open(filename, "a") - f.write("%s" % map_results) + f = open(filename, "w") + f.write("%s" % jsonutils.dumps(map_results)) f.close() LOG.info("Mapping data saved into %s", filename) diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/routers.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/routers.py index efb4740caf..f1a422454a 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/routers.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/routers.py @@ -409,7 +409,7 @@ registry.subscribe(migrate_distributed_routers_dhcp, registry.subscribe(redistribute_routers, constants.ROUTERS, - shell.Operations.NSX_REDISTRIBURE.value) + shell.Operations.NSX_REDISTRIBUTE.value) registry.subscribe(update_edge_firewalls, constants.ROUTERS, diff --git a/vmware_nsx/shell/resources.py b/vmware_nsx/shell/resources.py index 491a674545..c4c67a0cd5 100644 --- a/vmware_nsx/shell/resources.py +++ b/vmware_nsx/shell/resources.py @@ -58,7 +58,7 @@ class Operations(enum.Enum): NSX_UPDATE_TAGS = 'nsx-update-tags' NSX_UPDATE_FW = 'nsx-update-fw' NSX_RECREATE = 'nsx-recreate' - NSX_REDISTRIBURE = 'nsx-redistribute' + NSX_REDISTRIBUTE = 'nsx-redistribute' NSX_REORDER = 'nsx-reorder' NSX_DISCONNECT = 'nsx-disconnect' NSX_RECONNECT = 'nsx-reconnect' @@ -196,7 +196,7 @@ nsxv_resources = { constants.DHCP_BINDING: Resource(constants.DHCP_BINDING, [Operations.LIST.value, Operations.NSX_UPDATE.value, - Operations.NSX_REDISTRIBURE.value, + Operations.NSX_REDISTRIBUTE.value, Operations.NSX_RECREATE.value]), constants.NETWORKS: Resource(constants.NETWORKS, [Operations.LIST.value, @@ -236,7 +236,7 @@ nsxv_resources = { Operations.STATUS.value]), constants.ROUTERS: Resource(constants.ROUTERS, [Operations.NSX_RECREATE.value, - Operations.NSX_REDISTRIBURE.value, + Operations.NSX_REDISTRIBUTE.value, Operations.MIGRATE_VDR_DHCP.value, Operations.NSX_UPDATE_FW.value]), constants.ORPHANED_VNICS: Resource(constants.ORPHANED_VNICS, @@ -291,7 +291,8 @@ nsxp_resources = { constants.NSX_MIGRATE_T_P: Resource(constants.NSX_MIGRATE_T_P, [Operations.CLEAN_ALL.value]), constants.NSX_MIGRATE_V_T: Resource(constants.NSX_MIGRATE_V_T, - [Operations.CLEAN_ALL.value]), + [Operations.CLEAN_ALL.value, + Operations.NSX_REDISTRIBUTE.value]), } nsxv3_resources_names = list(nsxv3_resources.keys())