forked from DevNetSandbox/sbx_nxos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestart-sbx.py
82 lines (68 loc) · 2.78 KB
/
restart-sbx.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
#!/usr/bin/env python
'''
This script stops, and then starts all the nodes in the
sandbox using the VIRL APIs.
'''
from time import sleep
from virlutils import *
from builtins import input
import sys
if __name__ == "__main__":
# Get simulation list
nx_os_simulation = get_simulations()
nx_os_simulation_name = list(nx_os_simulation.keys())[0]
print("VIRL Simulation Name: {}\n".format(nx_os_simulation_name))
# Get Simulation Node List and determine which to restart
print("Which node would you like to restart?")
nodes = get_nodes(nx_os_simulation_name)
node_key = []
for i, node in enumerate(nodes.keys()):
node_key.append(node)
print(" {} - {}: Status {}".format(i, node, nodes[node]["state"]))
print(" a - Restart All Nodes ")
print("Enter 0 - {} to choose a node, or a for all".format(len(node_key)-1))
# Ask user which to start
choice = input()
# Validate Users Choice.
try:
if int(choice) not in range(0, len(node_key)-1):
print("Invalid Choice")
print(" Enter 0 - {} to choose a node, or a for all").format(len(node_key)-1)
sys.exit(1)
else:
choice = int(choice)
# A single node was picked, update working nodes dictionary
nodes = {node_key[choice]: nodes[node_key[choice]]}
except ValueError:
# A non-Integer was entered, verify it was the letter "a".
if str.lower(choice) != "a":
print("Invalid Choice")
print(" Enter 0 - {} to choose a node, or a for all").format(len(node_key)-1)
sys.exit(1)
# Stop Nodes
print("Stopping Nodes")
action = stop_nodes(nx_os_simulation_name, nodes)
print(action["stopped"])
print("")
# Wait for nodes to be fully stopped (state = ABSENT)
while not test_node_state(nx_os_simulation_name, "ABSENT", test_nodes=nodes):
print("Nodes not stopped yet")
sleep(10)
# Start Nodes
print("Starting Nodes")
action = start_nodes(nx_os_simulation_name, nodes)
print(action["started"])
print("")
# Wait for nodes to be fully started (state = ACTIVE)
while not test_node_state(nx_os_simulation_name, "ACTIVE", test_nodes=nodes):
print("Nodes not started yet")
sleep(10)
# Done
print("Nodes have been restarted, however it can take up to 15 minutes for all switches to fully boot and be ready.")
print("You can monitor the startup activity at: ")
for node in nodes.keys():
console = get_node_console(nx_os_simulation_name, node)
try:
print(" Console to {} -> `telnet {} {}`".format(node, console["host"], console["console_port"]))
except TypeError:
pass