-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaintenance_tasks.py
116 lines (99 loc) · 2.53 KB
/
maintenance_tasks.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
from logging import DEBUG, INFO
from time import sleep
from random import randint
from nornir.core.task import Task, Result
from nornir_napalm.plugins.tasks import napalm_configure
CFG_MAINTENANCE_ENABLE = """
maintenance
unit System
quiesce
end
"""
CFG_MAINTENANCE_DISABLE = """
no maintenance
end
"""
CFG_OSPF_DISABLE = """
router ospf 1
shutdown
end
"""
CFG_OSPF_ENABLE = """
router ospf 1
no shutdown
end
"""
CFG_ISIS_DISABLE = """
router isis Gandalf
shutdown
end
"""
CFG_ISIS_ENABLE = """
router isis Gandalf
no shutdown
end
"""
def relax(task: Task) -> Result:
seconds = randint(10, 30)
sleep(seconds)
# Every now and then, a router does not want to return to work.
if randint(0, 9) == 9:
raise Exception(f"{task.host.name} is not in the mood to perform its job.")
return Result(task.host, result=f"{seconds} seconds of beauty sleep.")
def spa_day(task: Task) -> Result:
"""
We believe every router deserves some relaxing time.
"""
# Shutdown ospf or|and isis
if "ospf" in task.host.groups:
task.run(
napalm_configure,
configuration=CFG_OSPF_DISABLE,
name="ospf_disalbe",
severity_level=DEBUG,
)
if "isis" in task.host.groups:
task.run(
napalm_configure,
configuration=CFG_ISIS_DISABLE,
name="isis_disalbe",
severity_level=DEBUG,
)
# Enjoy your spa day
task.run(relax, severity_level=INFO)
# Enable ospf or|and isis
if "ospf" in task.host.groups:
task.run(
napalm_configure,
configuration=CFG_OSPF_ENABLE,
name="ospf_enable",
severity_level=DEBUG,
)
if "isis" in task.host.groups:
task.run(
napalm_configure,
configuration=CFG_ISIS_ENABLE,
name="isis_enable",
severity_level=DEBUG,
)
return Result(
task.host, result=f"{task.host.name} is relaxed and happy to perform its job."
)
def maintenance(task: Task) -> Result:
task.run(
napalm_configure,
configuration=CFG_MAINTENANCE_ENABLE,
name="maintenence_enable",
severity_level=DEBUG,
)
sleep(5)
# Enjoy your spa day
task.run(spa_day, severity_level=INFO)
task.run(
napalm_configure,
configuration=CFG_MAINTENANCE_DISABLE,
name="maintenence_disable",
severity_level=DEBUG,
)
sleep(10)
return Result(task.host, result=f"{task.host.name} is back from the spa day")