From 998a245512831746c21d63d4264a6e487afed1c5 Mon Sep 17 00:00:00 2001 From: yuyangbj Date: Thu, 19 Apr 2018 11:20:56 +0800 Subject: [PATCH] Add script to clean up all backup edges owned by Neutron When Neutron server is deleted, we need to clean up all backup edges created by neutron server. The current clean-all cannot address the scenario that many neutron servers are using same NSXv backend. Change-Id: I4f4d19adf7293c2c91c2cd8b52359bb1eb338b84 --- doc/source/admin_util.rst | 4 +-- .../plugins/nsxv/resources/backup_edges.py | 28 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/doc/source/admin_util.rst b/doc/source/admin_util.rst index 8b90b3126f..5347c693be 100644 --- a/doc/source/admin_util.rst +++ b/doc/source/admin_util.rst @@ -127,9 +127,9 @@ Backup Edges nsxadmin -r backup-edges -o clean --property edge-id=edge-9 [--force] -- Delete all backup edges:: +- Delete all backup edges existing in both neutron and backend when scope is neutron, else backend only:: - nsxadmin -r backup-edges -o clean-all [--force] + nsxadmin -r backup-edges -o clean-all --property scope=[neutron/all] [--force] - List Edge name mismatches between DB and backend, and backup edges that are missing from the backend:: diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/backup_edges.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/backup_edges.py index 5a7e3efc17..0c85539bd2 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/backup_edges.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/backup_edges.py @@ -37,7 +37,7 @@ nsxv = utils.get_nsxv_client() _uuid = uuidutils.generate_uuid -def get_nsxv_backup_edges(): +def get_nsxv_backup_edges(scope="all"): edges = utils.get_nsxv_backend_edges() backup_edges = [] edgeapi = utils.NeutronDbClient() @@ -46,9 +46,19 @@ def get_nsxv_backup_edges(): # Make sure it is really a backup edge edge_vnic_binds = nsxv_db.get_edge_vnic_bindings_by_edge( edgeapi.context.session, edge['id']) - if not edge_vnic_binds: - extend_edge_info(edge) - backup_edges.append(edge) + if scope != "all": + # Make sure the backup edge exists in neutron + # Return backup edges existing in both neutron and backend + # when scope != all + edge_in_neutron = nsxv_db.get_nsxv_router_binding_by_edge( + edgeapi.context.session, edge['id']) + if not edge_vnic_binds and edge_in_neutron: + extend_edge_info(edge) + backup_edges.append(edge) + else: + if not edge_vnic_binds: + extend_edge_info(edge) + backup_edges.append(edge) return backup_edges @@ -138,7 +148,15 @@ def nsx_clean_backup_edge(resource, event, trigger, **kwargs): def nsx_clean_all_backup_edges(resource, event, trigger, **kwargs): """Delete all backup edges""" - backup_edges = get_nsxv_backup_edges() + scope = "all" + if kwargs.get('property'): + properties = admin_utils.parse_multi_keyval_opt(kwargs['property']) + scope = properties.get("scope", "all") + if scope not in ["neutron", "all"]: + LOG.error("Need to specify the scope in ['neutron', 'all']") + return + + backup_edges = get_nsxv_backup_edges(scope=scope) if not kwargs.get('force'): #ask for the user confirmation