|
| 1 | +# Copyright (c) 2022 Red Hat, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +from neutron_lib import constants |
| 17 | +from neutron_lib import context |
| 18 | +from neutron_lib.db import api as db_api |
| 19 | +from oslo_config import cfg |
| 20 | +from oslo_db import options as db_options |
| 21 | +from oslo_log import log as logging |
| 22 | + |
| 23 | +from neutron.common import config as common_config |
| 24 | +from neutron.objects import ports as ports_obj |
| 25 | + |
| 26 | + |
| 27 | +LOG = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +def setup_conf(conf): |
| 31 | + common_config.register_common_config_options() |
| 32 | + db_group, neutron_db_opts = db_options.list_opts()[0] |
| 33 | + conf.register_cli_opts(neutron_db_opts, db_group) |
| 34 | + conf() |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + """Main method for removing the duplicated port binding registers. |
| 39 | +
|
| 40 | + This script finds all ``PortBinding`` registers with the same ``port_id``. |
| 41 | + That happens during the live-migration process. Once finished, the inactive |
| 42 | + port binding register is deleted. However, it could happen that during the |
| 43 | + live-migration, an error occurs and this deletion is not executed. The |
| 44 | + related port cannot be migrated anymore. |
| 45 | +
|
| 46 | + This script should not be executed during a live migration process. It will |
| 47 | + remove the inactive port binding and will break the migration. |
| 48 | + """ |
| 49 | + conf = cfg.CONF |
| 50 | + setup_conf(conf) |
| 51 | + _dry_run = conf.cli_script.dry_run |
| 52 | + admin_ctx = context.get_admin_context() |
| 53 | + with db_api.CONTEXT_WRITER.using(admin_ctx): |
| 54 | + dup_pbindings = ports_obj.PortBinding.get_duplicated_port_bindings( |
| 55 | + admin_ctx) |
| 56 | + |
| 57 | + # Clean duplicated port bindings that are INACTIVE (if not in dry-run). |
| 58 | + if not _dry_run: |
| 59 | + for pbinding in dup_pbindings: |
| 60 | + ports_obj.PortBinding.delete_objects( |
| 61 | + admin_ctx, status=constants.INACTIVE, |
| 62 | + port_id=pbinding.port_id) |
| 63 | + |
| 64 | + if dup_pbindings: |
| 65 | + port_ids = [pbinding.port_id for pbinding in dup_pbindings] |
| 66 | + action = 'can be' if _dry_run else 'have been' |
| 67 | + LOG.info('The following duplicated PortBinding registers with ' |
| 68 | + 'status=INACTIVE %s removed, port_ids: %s', |
| 69 | + action, port_ids) |
| 70 | + else: |
| 71 | + LOG.info('No duplicated PortBinding registers has been found.') |
0 commit comments