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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#!/usr/bin/env python3
# De-nest PPPoE interfaces
# Migrate boolean nodes to valueless
import sys
from vyos.configtree import ConfigTree
def migrate_dialer(config, tree, intf):
for pppoe in config.list_nodes(tree):
# assemble string, 0 -> pppoe0
new_base = ['interfaces', 'pppoe', 'pppoe' + pppoe]
config.set(new_base)
# format as tag node to avoid loading problems
config.set_tag(['interfaces', 'pppoe'])
#
# Firewall migrieren
#
firewall = tree + [pppoe, 'firewall']
if config.exists(firewall):
config.copy(firewall, new_base + ['firewall'])
policy = tree + [pppoe, 'policy']
if config.exists(policy):
config.copy(policy, new_base + ['policy'])
#
# QoS migrieren
#
redirect = tree + [pppoe, 'redirect']
if config.exists(redirect):
config.copy(redirect, new_base + ['redirect'])
traffic_policy = tree + [pppoe, 'traffic-policy']
if config.exists(traffic_policy):
config.copy(traffic_policy, new_base + ['traffic-policy'])
#
# Quagga migrieren
#
ip = tree + [pppoe, 'ip']
if config.exists(ip):
config.copy(ip, new_base + ['ip'])
ipv6 = tree + [pppoe, 'ipv6']
if config.exists(ipv6):
config.copy(ipv6, new_base + ['ipv6'])
#
# Interface migration
#
config.set(new_base + ['link'], value=intf)
default_route = tree + [pppoe, 'default-route']
if config.exists(default_route):
tmp = config.return_value(default_route)
config.set(new_base + ['default-route'], value=tmp)
disable = tree + [pppoe, 'disable']
if config.exists(disable):
tmp = config.return_value(disable)
config.set(new_base + ['disable'], value=tmp)
mtu = tree + [pppoe, 'mtu']
if config.exists(mtu):
tmp = config.return_value(mtu)
config.set(new_base + ['mtu'], value=tmp)
nameserver = tree + [pppoe, 'name-server']
if config.exists(nameserver):
tmp = config.return_value(nameserver)
config.set(new_base + ['name-server'], value=tmp)
password = tree + [pppoe, 'password']
if config.exists(password):
tmp = config.return_value(password)
config.set(new_base + ['password'], value=tmp)
userid = tree + [pppoe, 'user-id']
if config.exists(userid):
tmp = config.return_value(userid)
config.set(new_base + ['user-id'], value=tmp)
# remove enable-ipv6 node and rather place it under ipv6 node
ipv6_enable = tree + [pppoe, 'enable-ipv6']
if config.exists(ipv6_enable):
config.set(new_base + ['ipv6', 'enable'])
ipv6_slaac = tree + [pppoe, 'ipv6', 'address', 'autoconf']
if config.exists(ipv6_slaac):
config.set(new_base + ['ipv6', 'address', 'autoconf'])
if __name__ == '__main__':
if (len(sys.argv) < 1):
print("Must specify file name!")
exit(1)
file_name = sys.argv[1]
with open(file_name, 'r') as f:
config_file = f.read()
config = ConfigTree(config_file)
pppoe_links = ['bonding', 'ethernet']
for link_type in pppoe_links:
if not config.exists(['interfaces', link_type]):
continue
for interface in config.list_nodes(['interfaces', link_type]):
# check if PPPoE exists
base_if = ['interfaces', link_type, interface]
pppoe_if = base_if + ['pppoe']
if config.exists(pppoe_if):
for dialer in config.list_nodes(pppoe_if):
migrate_dialer(config, pppoe_if, interface)
# Delete old PPPoE interface
config.delete(pppoe_if)
# bail out early if there are no VLAN interfaces to migrate
if not config.exists(base_if + ['vif']):
continue
# Migrate PPPoE interfaces attached to a VLAN
for vlan in config.list_nodes(base_if + ['vif']):
vlan_if = base_if + ['vif', vlan]
pppoe_if = vlan_if + ['pppoe']
if config.exists(pppoe_if):
for dialer in config.list_nodes(pppoe_if):
intf = "{}.{}".format(interface, vlan)
migrate_dialer(config, pppoe_if, intf)
# Delete old PPPoE interface
config.delete(pppoe_if)
# Add interface description that this is required for PPPoE
if not config.exists(vlan_if + ['description']):
config.set(vlan_if + ['description'], value='PPPoE link interface')
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))
sys.exit(1)
|