summaryrefslogtreecommitdiff
path: root/src/migration-scripts/system/23-to-24
blob: 97fe82462dd46bc0c8e3c3a90828fd805ad853ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
#
# Copyright (C) 2022 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
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

from ipaddress import ip_interface
from ipaddress import ip_address
from sys import exit, argv
from vyos.configtree import ConfigTree
from vyos.template import is_ipv4

if (len(argv) < 1):
    print("Must specify file name!")
    exit(1)

file_name = argv[1]
with open(file_name, 'r') as f:
    config_file = f.read()

base = ['protocols', 'static', 'arp']
tmp_base = ['protocols', 'static', 'arp-tmp']
config = ConfigTree(config_file)

def fixup_cli(config, path, interface):
    if config.exists(path + ['address']):
        for address in config.return_values(path + ['address']):
            tmp = ip_interface(address)
            # ARP is only available for IPv4 ;-)
            if not is_ipv4(tmp):
                continue
            if ip_address(host) in tmp.network.hosts():
                mac = config.return_value(tmp_base + [host, 'hwaddr'])
                iface_path = ['protocols', 'static', 'arp', 'interface']
                config.set(iface_path + [interface, 'address', host, 'mac'], value=mac)
                config.set_tag(iface_path)
                config.set_tag(iface_path + [interface, 'address'])
                continue

if not config.exists(base):
    # Nothing to do
    exit(0)

# We need a temporary copy of the config tree as the original one needs to be
# deleted first due to a change iun thge tagNode structure.
config.copy(base, tmp_base)
config.delete(base)

for host in config.list_nodes(tmp_base):
    for type in config.list_nodes(['interfaces']):
        for interface in config.list_nodes(['interfaces', type]):
            if_base = ['interfaces', type, interface]
            fixup_cli(config, if_base, interface)

            if config.exists(if_base + ['vif']):
                for vif in config.list_nodes(if_base + ['vif']):
                    vif_base = ['interfaces', type, interface, 'vif', vif]
                    fixup_cli(config, vif_base, f'{interface}.{vif}')

            if config.exists(if_base + ['vif-s']):
                for vif_s in config.list_nodes(if_base + ['vif-s']):
                    vif_s_base = ['interfaces', type, interface, 'vif-s', vif_s]
                    fixup_cli(config, vif_s_base, f'{interface}.{vif_s}')

                    if config.exists(if_base + ['vif-s', vif_s, 'vif-c']):
                        for vif_c in config.list_nodes(if_base + ['vif-s', vif_s, 'vif-c']):
                            vif_c_base = ['interfaces', type, interface, 'vif-s', vif_s, 'vif-c', vif_c]
                            fixup_cli(config, vif_c_base, f'{interface}.{vif_s}.{vif_c}')

config.delete(tmp_base)

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