-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_post.py
201 lines (165 loc) · 6.46 KB
/
pre_post.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
import json
import os
from typing import Dict, List, Callable
from util import config, log, loop_for, login_loop_for, State, Client
Snapshot = Dict[str, List[Dict[str, str]]]
############################################################
# Pre-change data collection
############################################################
def load_snapshot(client: Client) -> Snapshot:
"""Load/create snapshot"""
fn = client.args["snapshot_file"]
data = {"faults": [], "devices": []}
if not os.path.isfile(fn):
with open(fn, "w") as f:
log.info(f"Creating new snapshot {fn}...")
data["faults"] = get_faults(client)
data["devices"] = get_devices(client)
data["isis_routes"] = get_interpod_routes(client)
f.write(json.dumps(data, indent=2))
return data
with open(fn, "r") as f:
log.info(f"Loading snapshot from {fn}...")
return json.loads(f.read())
def get_faults(client: Client) -> List[Dict[str, str]]:
"""Get current fault list"""
return client.get_class("faultInst")
def get_devices(client: Client) -> List[Dict[str, str]]:
"""Get current device list"""
return client.get_class("topSystem")
def get_interpod_routes(client: Client) -> List[Dict[str, str]]:
"""Get current inter-pod routes"""
tepQueries = []
for pod in client.get_class("fabricSetupP"):
if pod.get("podType") == "physical":
tepPool = pod.get("tepPool")
if tepPool:
tepQueries.append(f'eq(isisRoute.pfx,"{tepPool}")')
if len(tepQueries) > 0:
tepQuery = ",".join(tepQueries)
return client.get_class(
"isisRoute",
params={
"rsp-subtree-include": "relations",
"query-target-filter": f"or({tepQuery})",
},
)
return []
############################################################
# Post-change comparison
############################################################
enabled_post_checks = []
CheckFn = Callable[[Client, Snapshot], State]
def enable_post(fn: CheckFn) -> CheckFn:
enabled_post_checks.append(fn)
return fn
def run_post_checks(client: Client, snapshot: Snapshot) -> State:
# Non-zero code indicates an error condition
for check in enabled_post_checks:
log.debug(f"Comparing: {check.__doc__}...")
state = check(client, snapshot)
if state == State.FAIL:
log.debug("Failed on comparison", check=check.__doc__)
return state
return State.OK
@enable_post
def compare_faults(client: Client, snapshot: Snapshot) -> State:
"""faults"""
new_faults = []
has_new_critical_fault = False
for current_fault in get_faults(client):
new_fault = True
for previous_fault in snapshot["faults"]:
previous_dn = previous_fault.get("dn", "")
if previous_dn and previous_dn == current_fault.get("dn", ""):
new_fault = False
if new_fault and current_fault.get("severity") != "cleared":
new_faults.append(current_fault)
if len(new_faults) > 0:
log.debug("%d new faults found" % len(new_faults))
by_code = {}
for fault in new_faults:
if fault["severity"] == "critical":
has_new_critical_fault = True
if client.args["debug"]:
log.debug(
"New fault:",
code=fault["code"],
dn=fault["dn"],
severity=fault["severity"],
description=fault["descr"],
)
else:
code = fault.get("code", "")
if code in by_code:
by_code[code]["count"] += 1
else:
by_code[code] = {
"count": 1,
"severity": fault.get("severity", ""),
"description": fault.get("descr", ""),
}
for code, fault_meta in sorted(by_code.items()):
log.debug("New fault(s)", **fault_meta)
if has_new_critical_fault:
log.info("New critical faults found. Waiting for fabric to converge...")
return State.FAIL
return State.OK
log.debug("No new faults found")
return State.OK
@enable_post
def compare_devices(client: Client, snapshot: Snapshot) -> State:
"""devices"""
has_missing = False
current_dns = [r.get("dn", "") for r in get_devices(client)]
for device in snapshot["devices"]:
snapshot_dn = device.get("dn", "")
if snapshot_dn not in current_dns:
# Device is missing!
log.debug("missing device", dn=device.get("dn"), name=device.get("name"))
has_missing = True
if has_missing:
log.info(
"Some devices are not currently available. Waiting for fabric to converge..."
)
return State.FAIL
return State.OK
@enable_post
def compare_routes(client: Client, snapshot: Snapshot) -> State:
"""ISIS inter-pod routes"""
current_dns = [r.get("dn", "") for r in get_interpod_routes(client)]
has_missing = False
for route in snapshot["isis_routes"]:
dn = route.get("dn", "")
if dn not in current_dns:
# Route is missing!
log.debug("missing ISIS route", dn=route.get("dn"), prefix=route.get("pfx"))
has_missing = True
if has_missing:
log.info("ISIS inter-pod routes are missing. Waiting for fabric to converge...")
return State.FAIL
return State.OK
def init(timeout: int = 3600) -> State:
"""Always create a new snapshot, for the start of the upgrade"""
if os.path.isfile(config.get("snapshot_file")):
os.remove(config.get("snapshot_file"))
return run(timeout=timeout)
def run(timeout: int = 3600) -> State:
client = login_loop_for(timeout, config)
if client is None:
return State.FAIL
snapshot_exists = os.path.isfile(client.args["snapshot_file"])
snapshot = load_snapshot(client)
def _run_checks(client: Client) -> State:
return run_post_checks(client, snapshot)
if snapshot_exists:
state = loop_for(timeout, _run_checks, fail_msg="Post-check unsuccessful")
if state == State.OK:
log.info("Snapshot compare successful.")
elif state == State.FAIL:
log.debug("Snapshot compare failed.")
return state
else:
return State.OK
if __name__ == "__main__":
run(timeout=-1)