-
Notifications
You must be signed in to change notification settings - Fork 24
/
DevNet_Sandbox_CSR_Config_Replace_lancelot.py
272 lines (239 loc) · 13.2 KB
/
DevNet_Sandbox_CSR_Config_Replace_lancelot.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
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
269
270
271
272
# ----------------
# Copyright
# ----------------
# Written by John Capobianco, March 2021
# Copyright (c) 2021 John Capobianco
# ----------------
# Python
# ----------------
import os
import sys
import yaml
import time
import json
import shutil
import logging
import requests
import ftplib
import re
from rich import print
from rich.panel import Panel
from rich.text import Text
from pyats import aetest
from pyats import topology
from pyats.log.utils import banner
from genie.utils.diff import Diff
from jinja2 import Environment, FileSystemLoader
from ascii_art import GREETING, LEARN, RUNNING, WRITING, PUSH_INTENT, DIFF, NO_DIFF
from general_functionalities import ParseConfigFunction, ParseLearnFunction
from datetime import datetime
from contextlib import redirect_stdout
log = logging.getLogger(__name__)
template_dir = 'templates/cisco/ios_xe'
env = Environment(loader=FileSystemLoader(template_dir))
timestr = datetime.now().strftime("%Y%m%d_%H%M%S")
# ----------------
# AE Test Setup
# ----------------
class common_setup(aetest.CommonSetup):
"""Common Setup section"""
@aetest.subsection
def connect_to_devices(self, testbed):
"""Connect to all the devices"""
print(Panel.fit(Text.from_markup(GREETING)))
testbed.connect()
# ----------------
# Test Case #1
# ----------------
class Collect_Information(aetest.Testcase):
"""Parse all the commands"""
@aetest.test
def parse(self, testbed, section, steps):
""" Testcase Setup section """
# ---------------------------------------
# Loop over devices
# ---------------------------------------
for device in testbed:
# ---------------------------------------
# Load Data Model
# ---------------------------------------
with open("data_models/%s.yaml" % device.alias) as stream:
data_model = yaml.safe_load(stream)
# ---------------------------------------
# Genie learn('config').info for pre-change state
# ---------------------------------------
print(Panel.fit(Text.from_markup(LEARN)))
self.learned_config = ParseConfigFunction.parse_learn(steps, device, "config")
original_learned_config = self.learned_config
# Routing
self.learned_routing = ParseLearnFunction.parse_learn(steps, device, "routing")
pre_change_default_route = self.learned_routing['vrf']['default']['address_family']['ipv4']['routes']['0.0.0.0/0']['next_hop']['next_hop_list'][1]['next_hop']
print (pre_change_default_route)
#----------------------------------------
# Confirm All Interfaces Are Up First
#----------------------------------------
with steps.start('Pre-Change Interface Check',continue_=True) as step:
all_interfaces_up = "true"
interface_list = ("GigabitEthernet1","GigabitEthernet2","GigabitEthernet3")
for interface in interface_list:
if "shutdown" in original_learned_config['interface %s' % interface]:
all_interfaces_up = "false"
log.info("Interface is shutdown")
else:
log.info("Interface is up")
if all_interfaces_up == "true":
log.info("The Device is Healthy: Proceeding with Deployment")
#----------------------------------------
# Write Pre-Change File
#----------------------------------------
with steps.start('Store Original Golden Image',continue_=True) as step:
print(Panel.fit(Text.from_markup(WRITING)))
original_config_filename = "%s_Original_Golden_SSH_Image_%s.json" % (timestr,device.alias)
# Write Original Learned Config as JSON
if hasattr(self, 'learned_config'):
with open("Camelot/Cisco/DevNet_Sandbox/Lancelot/Golden_Image/%s" % original_config_filename, "w") as fid:
json.dump(self.learned_config, fid, indent=4, sort_keys=True)
fid.close()
# ---------------------------------------
# Create Intent from Template and Data Models
# ---------------------------------------
with steps.start('Generating Intent From Data Model and Template',continue_=True) as step:
print(Panel.fit(Text.from_markup(RUNNING)))
intended_config_template = env.get_template('intended_ssh_config.j2')
rendered_intended_config = intended_config_template.render(host_data_model=data_model)
with open("Camelot/Cisco/DevNet_Sandbox/Lancelot/Intended_Config/%s_Intended_SSH_Config.txt" % timestr, "w") as fid:
fid.write(rendered_intended_config)
fid.close()
# ---------------------------------------
# Push Intent to FTP Server
# ---------------------------------------
with steps.start('Push Intent to FTP Server',continue_=True) as step:
ftp = ftplib.FTP()
host = "10.10.20.50"
port = 2121
ftp.connect(host, port)
print (ftp.getwelcome())
try:
print ("Logging in...")
ftp.login()
except:
"failed to login"
print ("Storing file")
file = open("Camelot/Cisco/DevNet_Sandbox/Lancelot/Intended_Config/%s_Intended_SSH_Config.txt" % timestr, "rb")
ftp.storbinary("STOR Intended_Config", file)
file.close()
ftp.close()
# ---------------------------------------
# Copy file from FTP server to Bootflash
# ---------------------------------------
device.api.copy_to_device(
protocol="ftp",
server="10.10.20.50:2121",
remote_path="Intended_Config",
local_path="bootflash:/")
# ---------------------------------------
# Trigger Config Replace
# ---------------------------------------
device.execute("configure replace bootflash:/Intended_Config force")
# ---------------------------------------
# Re-capture state
# ---------------------------------------
print(Panel.fit(Text.from_markup(LEARN)))
self.learned_config = ParseConfigFunction.parse_learn(steps, device, "config")
new_learned_config = self.learned_config
# Routing
self.learned_routing = ParseLearnFunction.parse_learn(steps, device, "routing")
post_change_default_route = self.learned_routing['vrf']['default']['address_family']['ipv4']['routes']['0.0.0.0/0']['next_hop']['next_hop_list'][1]['next_hop']
print (pre_change_default_route)
## ---------------------------------------
## Write post-change state
## ---------------------------------------
with steps.start('Store New Golden Image',continue_=True) as step:
print(Panel.fit(Text.from_markup(WRITING)))
new_config_filename = "%s_Golden_SSH_Image_%s.json" % (timestr,device.alias)
## Write New Learned Config as JSON
if hasattr(self, 'learned_config'):
with open("Camelot/Cisco/DevNet_Sandbox/Lancelot/Golden_Image/%s" % new_config_filename, "w") as fid:
json.dump(self.learned_config, fid, indent=4, sort_keys=True)
fid.close()
## ---------------------------------------
## Show the differential
## ---------------------------------------
with steps.start('Show Differential',continue_=True) as step:
config_diff = Diff(original_learned_config, new_learned_config)
config_diff.findDiff()
if config_diff.diffs:
print(Panel.fit(Text.from_markup(DIFF)))
print(config_diff)
with open('Camelot/Cisco/DevNet_Sandbox/Lancelot/Changes/%s_SSH_Changes.txt' % timestr, 'w') as f:
with redirect_stdout(f):
print(config_diff)
f.close()
else:
print(Panel.fit(Text.from_markup(NO_DIFF)))
with open('Camelot/Cisco/DevNet_Sandbox/Lancelot/Changes/%s_SSH_Changes.txt' % timestr, 'w') as f:
f.write("IDEMPOTENT - NO CHANGES")
f.close()
## ---------------------------------------
## Ping Test
## ---------------------------------------
with steps.start('PING Test',continue_=True) as step:
destination_list = ("10.10.20.254","10.10.100.100")
failed = "false"
for destination in destination_list:
try:
result = device.ping(destination)
log.info(result)
except Exception as e:
log.info("We could NOT ping the loopback so we rolling back to startup-config")
device.execute("configure replace nvram:startup-config force")
self.failed('Ping {} from device {} failed with error: {}'.format(
destination,
device,
str(e),
)
)
else:
match = re.search(r'Success rate is (?P<rate>\d+) percent', result)
success_rate = match.group('rate')
log.info('Ping {} with success rate of {}%'.format(
destination,
success_rate,
)
)
if success_rate != "100":
failed = "true"
#----------------------------------------
# Confirm All Interfaces Are Up First
#----------------------------------------
with steps.start('POST-Change Interface Check',continue_=True) as step:
all_interfaces_up = "true"
interface_list = ("GigabitEthernet1","GigabitEthernet2","GigabitEthernet3","Loopback100")
for interface in interface_list:
if "shutdown" in new_learned_config['interface %s' % interface]:
all_interfaces_up = "false"
log.info("Interface is shutdown")
else:
log.info("Interface is up")
#----------------------------------------
# Confirm the default route is unchanged
#----------------------------------------
with steps.start('POST-Change Default Route Test',continue_=True) as step:
bad_default_route = "false"
if pre_change_default_route != post_change_default_route:
bad_default_route = "true"
log.info("Bad Default Route")
else:
log.info("Unchanged Default Route")
## ---------------------------------------
## Commit or Rollback
## ---------------------------------------
with steps.start('Commit or Rollback',continue_=True) as step:
if failed == "true" or all_interfaces_up == "false" or bad_default_route == "true":
log.info("We could NOT ping or Interfaces are DOWN so we rolling back to startup-config")
device.execute("configure replace nvram:startup-config force")
else:
log.info("We could ping,all Interfaces are UP, and default route is unchanged so we are saving the configuration")
device.execute("write mem")
else:
log.info("Sorry all of the Interfaces were NOT UP so we cancelled the deployment")