forked from vrnetlab/vrnetlab
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathlaunch.py
More file actions
executable file
·268 lines (220 loc) · 8.19 KB
/
Copy pathlaunch.py
File metadata and controls
executable file
·268 lines (220 loc) · 8.19 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
import datetime
import logging
import os
import random
import re
import signal
import string
import subprocess
import sys
import vrnetlab
STARTUP_CONFIG_FILE = "/config/startup-config.cfg"
def handle_SIGCHLD(signal, frame):
os.waitpid(-1, os.WNOHANG)
def handle_SIGTERM(signal, frame):
sys.exit(0)
signal.signal(signal.SIGINT, handle_SIGTERM)
signal.signal(signal.SIGTERM, handle_SIGTERM)
signal.signal(signal.SIGCHLD, handle_SIGCHLD)
TRACE_LEVEL_NUM = 9
logging.addLevelName(TRACE_LEVEL_NUM, "TRACE")
def trace(self, message, *args, **kws):
# Yes, logger takes its '*args' as 'args'.
if self.isEnabledFor(TRACE_LEVEL_NUM):
self._log(TRACE_LEVEL_NUM, message, args, **kws)
logging.Logger.trace = trace
class cat9kv_vm(vrnetlab.VM):
def __init__(self, hostname, username, password, conn_mode, vcpu, ram):
disk_image = None
for e in sorted(os.listdir("/")):
if not disk_image and re.search(".qcow2$", e):
disk_image = "/" + e
super().__init__(
username,
password,
disk_image=disk_image,
smp=f"cores={vcpu},threads=1,sockets=1",
ram=ram,
min_dp_nics=8,
)
self.hostname = hostname
self.conn_mode = conn_mode
self.num_nics = 9
self.nic_type = "virtio-net-pci"
self.image_name = "config.img"
self.qemu_args.extend(
[
"-overcommit mem-lock=off",
f"-boot order=cd -cdrom /{self.image_name}",
]
)
# create .img which is mounted for startup config and contains ASIC emulation in 'conf/vswitch.xml' dir.
self.create_boot_image()
def create_boot_image(self):
"""Creates a iso image with a bootstrap configuration"""
try:
os.makedirs("/img_dir/conf")
except:
self.logger.error(
"Unable to make '/img_dir'. Does the directory already exist?"
)
try:
# Load vswitch.xml and randomize serial number
if os.path.exists("/vswitch.xml"):
with open("/vswitch.xml", "r") as f:
vswitch_content = f.read()
random_serial = ''.join(random.choices(string.ascii_uppercase + string.digits, k=7))
vswitch_content = re.sub(
r'<prod_serial_number>.*?</prod_serial_number>',
f'<prod_serial_number>{random_serial}</prod_serial_number>',
vswitch_content
)
with open("/img_dir/conf/vswitch.xml", "w") as f:
f.write(vswitch_content)
self.logger.info(f"Generated vswitch.xml with randomized serial number: {random_serial}")
else:
self.logger.debug("No vswitch.xml file provided.")
except Exception as e:
self.logger.error(f"Error processing vswitch.xml: {e}")
v4_mgmt_address = vrnetlab.cidr_to_ddn(self.mgmt_address_ipv4)
cat9kv_config = f"""hostname {self.hostname}
username {self.username} privilege 15 password {self.password}
ip domain name example.com
no ip domain lookup
!
crypto key generate rsa modulus 2048
!
line con 0
logging synchronous
!
line vty 0 4
logging synchronous
login local
transport input all
!
ip route vrf Mgmt-vrf 0.0.0.0 0.0.0.0 {self.mgmt_gw_ipv4}
ipv6 route vrf Mgmt-vrf ::/0 {self.mgmt_gw_ipv6}
!
interface GigabitEthernet0/0
description Containerlab management interface
ip address {v4_mgmt_address[0]} {v4_mgmt_address[1]}
ipv6 address {self.mgmt_address_ipv6}
no shut
exit
!
restconf
netconf-yang
netconf max-sessions 16
netconf detailed-error
!
ip ssh server algorithm mac hmac-sha2-512
!
"""
if os.path.exists(STARTUP_CONFIG_FILE):
self.logger.info("Startup configuration file found")
with open(STARTUP_CONFIG_FILE, "r") as startup_config:
# Strip any address the appended startup-config sets on the mgmt
# interface so a stale saved/hand-written mgmt address can never
# re-set it after the GigabitEthernet0/0 stanza configured above
# (IOS-XE applies day0 config top to bottom, so a later "ip
# address"/"ipv6 address" under the same interface wins) --
# otherwise the switch comes up "healthy" but unreachable at the
# address clab/DNS expect. Keyed on interface name, not value.
cat9kv_config += vrnetlab.strip_mgmt_interface_config(
startup_config.read(), "GigabitEthernet0/0", "ios"
)
else:
self.logger.warning(f"User provided startup configuration is not found.")
with open("/img_dir/iosxe_config.txt", "w") as cfg_file:
cfg_file.write(cat9kv_config)
genisoimage_args = [
"genisoimage",
"-l",
"-o",
"/" + self.image_name,
"/img_dir",
]
self.logger.debug("Generating boot ISO")
subprocess.Popen(genisoimage_args).wait()
def bootstrap_spin(self):
"""This function should be called periodically to do work."""
if self.spins > 300:
# too many spins with no result -> give up
self.stop()
self.start()
return
(ridx, match, res) = self.con_expect(
[
b"CVAC-4-CONFIG_DONE",
b"IOSXEBOOT-4-FACTORY_RESET",
],
)
if match: # got a match!
if ridx == 0: # configuration applied
self.logger.info("CVAC Configuration has been applied.")
# close telnet connection
self.scrapli_tn.close()
# startup time?
startup_time = datetime.datetime.now() - self.start_time
self.logger.info("Startup complete in: %s", startup_time)
# mark as running
self.running = True
return
elif ridx == 1: # IOSXEBOOT-4-FACTORY_RESET
self.logger.warning("Unexpected reload while running")
# no match, if we saw some output from the router it's probably
# booting, so let's give it some more time
if res != b"":
self.write_to_stdout(res)
# reset spins if we saw some output
self.spins = 0
self.spins += 1
return
class cat9kv(vrnetlab.VR):
def __init__(self, hostname, username, password, conn_mode, vcpu, ram):
super(cat9kv, self).__init__(username, password)
self.vms = [cat9kv_vm(hostname, username, password, conn_mode, vcpu, ram)]
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"--trace", action="store_true", help="enable trace level logging"
)
parser.add_argument("--username", default="vrnetlab", help="Username")
parser.add_argument("--password", default="VR-netlab9", help="Password")
parser.add_argument("--hostname", default="", help="Router hostname")
parser.add_argument(
"--connection-mode",
default="vrxcon",
help="Connection mode to use in the datapath",
)
parser.add_argument("--vcpu", type=int, default=4, help="Allocated vCPUs")
parser.add_argument("--ram", type=int, default=18432, help="Allocaetd RAM in MB")
args = parser.parse_args()
LOG_FORMAT = "%(asctime)s: %(module)-10s %(levelname)-8s %(message)s"
logging.basicConfig(format=LOG_FORMAT)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
if args.trace:
logger.setLevel(1)
# Auto-detect hostname from image filename if not provided
if not args.hostname:
for e in os.listdir("/"):
if re.search(r"\.qcow2$", e):
if re.search(r"c9800", e, re.IGNORECASE):
args.hostname = "c9800cl"
else:
args.hostname = "cat9kv"
break
if not args.hostname:
args.hostname = "cat9kv"
vr = cat9kv(
args.hostname,
args.username,
args.password,
args.connection_mode,
args.vcpu,
args.ram,
)
vr.start()