-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_ipvpn.py
executable file
·227 lines (199 loc) · 8.89 KB
/
add_ipvpn.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
# encoding: utf-8
import yaml
import argparse
from ipcalc import Network
from glob import glob
from jinja2 import Template, Environment, FileSystemLoader
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from pprint import pprint as pp
from define import BASEFOLDER, TEMPLATES
def parse_args(args=None):
"""
Parse the arguments/options passed to the program on the command line.
"""
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('devices', type=str, metavar='devices')
parser.add_argument('-pp', '--pprint-only', dest='pp', action='store_true')
subparsers = parser.add_subparsers(help='sub-command help')
ipvpn = subparsers.add_parser('ipvpn', help='ipvpn only')
ipvpn.set_defaults(which='ipvpn')
ipvpn.add_argument('-n', '--number-of-vpns', type=int, dest='nvpns')
ipvpn.add_argument('-vid', '--first-vpn-id', type=int, dest='vid')
ipvpn.add_argument('-nu', '--number-of-units', type=int, dest='nu')
ipvpn.add_argument('-nid', '--first-unit-id', type=int, dest='nid')
vpws = subparsers.add_parser('vpws', help='vpws only')
vpws.set_defaults(which='vpws')
vpws.add_argument('-n', '--number-of-vpns', type=int, dest='nvpns')
vpws.add_argument('-vid', '--first-vpn-id', type=int, dest='vid')
vpls = subparsers.add_parser('vpls', help='vpls only')
vpls.set_defaults(which='vpls')
vpls.add_argument('-n', '--number-of-vpns', type=int, dest='nvpns')
vpls.add_argument('-vid', '--first-vpn-id', type=int, dest='vid')
vpls.add_argument('-nid', '--first-unit-id', type=int, dest='nid')
vpls.add_argument('-oid', '--first-outer-vlan-id', type=int, dest='oid')
(args, rest_args) = parser.parse_known_args()
return args
def show_diff(cu):
"""
show configuration | compare
"""
return cu.diff()
def commit(cu):
"""
issues commit check follofs commit if everything is fine,
prints error otherwise
"""
if cu.commit_check():
if cu.commit():
print("Done")
else:
print("Something is wrong with commit")
return
def push(hostname, config):
if args.pp:
print(config)
else:
dev = Device(hostname, user='admin', password='admin')
dev.open(gather_facts=False)
dev.timeout = 60
dev.bind(cu=Config)
with Config(dev, mode='private') as cu:
rsp = cu.load(config, format='text', merge=True)
diff = show_diff(cu)
if diff:
print(diff)
# commit(cu)
cu.rollback()
def do_ipvpn(device, **kwargs):
def _do_routing_insatnce():
template = env.get_template('ipvpn/routing-instances.j2')
print(kwargs['name'])
vpns = range(args.vid, args.vid+args.nvpns)
units = range(args.nid, args.nid+args.nu)
return template.render({'vpns': vpns,
'units': units,
'base_interface': kwargs['base_interface'],
'rd': kwargs['rd'],
'number_of_units': args.nu
})
def _do_interfaces():
net = Network('{}'.format(kwargs['ipvpn_ipnet']))
ipv6net = Network(u'{}'.format(kwargs['ipvpn_ipv6net']))
template = env.get_template('ipvpn/interfaces.j2')
vpns = [ str(vpn) for vpn in range(args.vid, args.vid+args.nvpns)]
outer_offest, inner_offset = kwargs['vlan_offsets'].split(' ')
return template.render({'vpns':vpns,
'units': range(args.nid, args.nid+args.nu),
'base_interface': kwargs['base_interface'],
'subnets': [ net+i*256 for i in range(1, args.nu+1)],
'v6subnets': [ ipv6net+i*65536 for i in range(1, args.nu+1)],
'gw': 1,
'inner_vlans': list(range(1, args.nu+1)),
'nu': args.nu,
'inner_offset': int(inner_offset),
'outer_offset': int(outer_offest)
})
device_config = ''
device_config += _do_routing_insatnce() + '\n'
device_config += _do_interfaces() + '\n'
push(device, device_config)
def do_vpws(device, **kwargs):
def _do_routing_insatnce():
template = env.get_template('vpws/routing-instances.j2')
print(kwargs['name'])
vpns = range(args.vid, args.vid+args.nvpns)
return template.render({'vpns': vpns,
'base_interface': kwargs['base_interface'],
'rd': kwargs['rd'],
'side': kwargs['side']
})
def _do_interfaces():
template = env.get_template('vpws/interfaces.j2')
vpns = [ str(vpn) for vpn in range(args.vid, args.vid+args.nvpns)]
outer_offest, inner_offset = kwargs['vlan_offsets'].split(' ')
inner_vlans = list(range(1, 11))
esis = []
esi = True if kwargs['side'] == 'right' else None
for i in range(args.vid, args.vid+len(vpns)+1):
s = "{0:020d}".format(i)
esis.append(":".join(a+b for a,b in zip(s[::2], s[1::2])))
return template.render({'vpns':vpns,
'esi_exists': esi,
'esis': esis,
'inner_vlans': len(vpns)*inner_vlans,
'base_interface': kwargs['base_interface'],
'inner_offset': int(inner_offset),
'outer_offset': int(outer_offest)
})
device_config = ''
device_config += _do_routing_insatnce() + '\n'
device_config += _do_interfaces() + '\n'
push(device, device_config)
def do_vpls(device, **kwargs):
def _do_routing_insatnce():
template = env.get_template('vpls/routing-instances.j2')
print(kwargs['name'])
vpns = range(args.vid, args.vid+args.nvpns)
units = list(range(30))
return template.render({'vpns': vpns,
'base_interface': kwargs['base_interface'],
'rd': kwargs['rd'],
'side': kwargs['side'],
'units': units,
'base_unit': args.nid
})
def _do_interfaces():
template = env.get_template('vpls/interfaces.j2')
vpns = [ str(vpn) for vpn in range(args.vid, args.vid+args.nvpns)]
outer_offest, inner_offset = kwargs['vlan_offsets'].split(' ')
inner_vlans = list(range(1, 31))
outer_vlans = [ i for i in list(range(args.oid, args.oid+args.nvpns+1)) for y in inner_vlans ]
left_vpns = [ i for i in vpns for y in inner_vlans ]
esis = []
esi = True if kwargs['side'] == 'right' else None
left_units = list(range(args.nid, args.nid+args.nvpns*30))
right_units = list(range(args.nid, args.nid+args.nvpns*30, 30))
for i in right_units:
s = "{0:020d}".format(i)
esis.append(":".join(a+b for a,b in zip(s[::2], s[1::2])))
return template.render({'vpns': vpns,
'left_vpns': left_vpns,
'left_units': left_units,
'right_units': right_units,
'esi_exists': esi,
'esis': esis,
'inner_vlans': len(vpns)*inner_vlans,
'outer_vlans': outer_vlans,
'base_interface': kwargs['base_interface'],
'inner_offset': int(inner_offset),
'outer_offset': int(outer_offest),
'side': kwargs['side']
})
device_config = ''
device_config += _do_routing_insatnce() + '\n'
device_config += _do_interfaces() + '\n'
push(device, device_config)
if __name__ == '__main__':
args = parse_args()
env = Environment(loader=FileSystemLoader(TEMPLATES))
env.globals.update(zip=zip)
env.globals.update(enum=enumerate)
with open(args.devices, 'r') as f:
device_dict = yaml.safe_load(f)
devices = device_dict['PEs']
if args.which == 'ipvpn':
for device in devices.items():
if 'ipvpn' in device[1]['vpn_types']:
do_ipvpn(device[0], **device[1])
elif args.which == 'vpws':
for device in devices.items():
if 'vpws' in device[1]['vpn_types']:
do_vpws(device[0], **device[1])
elif args.which == 'vpls':
for device in devices.items():
if 'vpls' in device[1]['vpn_types']:
do_vpls(device[0], **device[1])