summaryrefslogtreecommitdiff
path: root/src/migration-scripts/system/23-to-24
blob: feb62bc327844adf77909f4c91219f160c8c3882 (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
# Copyright 2022-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

from ipaddress import ip_interface
from ipaddress import ip_address

from vyos.configtree import ConfigTree
from vyos.template import is_ipv4

base = ['protocols', 'static', 'arp']
tmp_base = ['protocols', 'static', 'arp-tmp']

def fixup_cli(config, path, interface, host):
    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

def migrate(config: ConfigTree) -> None:
    if not config.exists(base):
        # Nothing to do
        return

    # 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, host)

                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}', host)

                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}', host)

                        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}', host)

    config.delete(tmp_base)