-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
97 lines (77 loc) · 2.55 KB
/
demo.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
import logging
import time
from nornir import InitNornir
from nornir.core.task import Task, Result
from random import randrange
from time import sleep
nr = InitNornir(
runner={
"plugin": "cachedThreaded",
"options": {
"num_workers": 100,
},
},
inventory={
"plugin": "SimpleInventory",
"options": {
"host_file": "tests/demo_inventory/hosts.yaml",
"group_file": "tests/demo_inventory/groups.yaml",
},
},
)
def hello_world(task: Task) -> Result:
sleep(7)
return Result(host=task.host, result=f"{task.host.name} says hello world!")
def say(task: Task, text: str) -> Result:
return Result(host=task.host, result=f"{task.host.name} says {text}")
def count(task: Task, number: int) -> Result:
count = []
for i in range(0, number):
if randrange(10) == 9:
raise Exception(f"Random exception at number {i}")
count.append(i)
return Result(host=task.host, result=f"{count}")
def greet_and_count(task: Task, number: int) -> Result:
task.run(
name="Greeting is the polite thing to do",
task=say,
text="hi!",
)
task.run(
name="Counting beans", task=count, number=number, severity_level=logging.DEBUG
)
task.run(
name="We should say bye too",
task=say,
text="bye!",
)
# let's inform if we counted even or odd times
even_or_odds = "even" if number % 2 == 1 else "odd"
return Result(host=task.host, result=f"{task.host} counted {even_or_odds} times!")
print("=" * 20, "hello_world 1st run", "=" * 20)
start = time.time()
nr.run(task=hello_world)
print(f"{time.time() - start} seconds")
print("=" * 20, "hello_world 2nd run", "=" * 20)
start = time.time()
nr.run(task=hello_world)
print(f"{time.time() - start} seconds")
print("=" * 20, "count 1st run", "=" * 20)
nr.data.reset_failed_hosts()
result = nr.run(task=count, number=10)
print(f"Hosts {result.failed_hosts.keys()} failed")
for h, r in result.failed_hosts.items():
print(f"{h}: {r[0].exception}")
print("=" * 20, "count 2nd run", "=" * 20)
nr.data.reset_failed_hosts()
result = nr.run(task=count, number=10)
print(f"Hosts {result.failed_hosts.keys()} failed")
for h, r in result.failed_hosts.items():
print(f"{h}: {r[0].exception}")
print("=" * 20, "count 3rd run", "=" * 20)
nr.data.reset_failed_hosts()
result = nr.run(task=count, number=10)
print(f"Hosts {result.failed_hosts.keys()} failed")
for h, r in result.failed_hosts.items():
print(f"{h}: {r[0].exception}")
nr.data.reset_failed_hosts()