Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T5941: Migration policy delete orphaned interface policy #2890

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/migration-scripts/policy/4-to-5
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2022 VyOS maintainers and contributors
# Copyright (C) 2022-2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
Expand Down Expand Up @@ -37,7 +37,53 @@ base4 = ['policy', 'route']
base6 = ['policy', 'route6']
config = ConfigTree(config_file)


def delete_orphaned_interface_policy(config, iftype, ifname, vif=None, vifs=None, vifc=None):
"""Delete unexpected policy on interfaces in cases when
policy does not exist but inreface has a policy configuration
Example T5941:
set interfaces bonding bond0 vif 995 policy
"""
if_path = ['interfaces', iftype, ifname]

if vif:
if_path += ['vif', vif]
elif vifs:
if_path += ['vif-s', vifs]
if vifc:
if_path += ['vif-c', vifc]

if not config.exists(if_path + ['policy']):
return

config.delete(if_path + ['policy'])


if not config.exists(base4) and not config.exists(base6):
# Delete orphaned nodes on interfaces T5941
for iftype in config.list_nodes(['interfaces']):
for ifname in config.list_nodes(['interfaces', iftype]):
delete_orphaned_interface_policy(config, iftype, ifname)

if config.exists(['interfaces', iftype, ifname, 'vif']):
for vif in config.list_nodes(['interfaces', iftype, ifname, 'vif']):
delete_orphaned_interface_policy(config, iftype, ifname, vif=vif)

if config.exists(['interfaces', iftype, ifname, 'vif-s']):
for vifs in config.list_nodes(['interfaces', iftype, ifname, 'vif-s']):
delete_orphaned_interface_policy(config, iftype, ifname, vifs=vifs)

if config.exists(['interfaces', iftype, ifname, 'vif-s', vifs, 'vif-c']):
for vifc in config.list_nodes(['interfaces', iftype, ifname, 'vif-s', vifs, 'vif-c']):
delete_orphaned_interface_policy(config, iftype, ifname, vifs=vifs, vifc=vifc)

try:
with open(file_name, 'w') as f:
f.write(config.to_string())
except OSError as e:
print("Failed to save the modified config: {}".format(e))
exit(1)

# Nothing to do
exit(0)

Expand Down