-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswinit.py
executable file
·129 lines (97 loc) · 3.4 KB
/
swinit.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
#!/usr/bin/env python3.6
from absl import app
from absl import flags
import os
import serial
import sys
# Device support
import cisco
import device
FLAGS = flags.FLAGS
flags.DEFINE_string('serial', '/dev/ttyUSB0', 'Serial device to manage')
flags.DEFINE_integer('baud', 9600, 'Serial baud rate')
flags.DEFINE_integer('timeout', 600, 'Device timeout before resetting state')
class Error(Exception):
"""Base exception for this module"""
class UnsupportedDeviceError(Error):
"""The operation is not known how to do on the given device."""
class Events(object):
def __init__(self):
self.last_state = 'timeout'
def detected(self):
"""Play discovery sound to inform operator we have it from here."""
self.last_state = 'detected'
os.system('aplay sounds/detected.wav')
def unsupported(self):
"""Play sound to inform operator we don't support this device."""
self.last_state = 'unsupported'
os.system('aplay sounds/unsupported.wav')
def completed(self):
"""Play sound to inform operator the device has been initialized."""
# Set state to timeout to not trigger a timeout sound if there
# are no more devices
self.last_state = 'timeout'
os.system('aplay sounds/completed.wav')
def timeout(self):
"""Play reset sound to inform operator we're ready again."""
# Never loop this if we haven't detected a device since last time
if self.last_state == 'timeout':
return
self.last_state = 'timeout'
os.system('aplay sounds/reset.wav')
def loop(port, events):
# Start with a unknwon anonymous device and try to learn what device we have
thing = device.Device(port)
# Poke the port in case it's already ready
thing.poke()
# Wait for bootloader prompt (ROMMON or so)
thing.wait_for_bootloader()
print('Detected bootloader')
events.detected()
model = thing.learn_model()
print('Model is', model)
if cisco.Cisco3850.matches_model(model):
thing = cisco.Cisco3850(port)
elif cisco.Cisco2950.matches_model(model):
thing = cisco.Cisco2950(port)
else:
raise UnsupportedDeviceError(model)
if thing.is_potentially_stack():
if thing.is_stack_primary():
print('Switch is no 1')
thing.set_switch_number(1)
else:
# TODO(bluecmd): We only support stacks of two right now
print('Switch is no 2')
thing.set_switch_number(2)
thing.clear_config()
thing.boot()
thing.wait_for_boot_complete()
# Install basic configure for the device for it to continue with
# using swboot for real configuration
thing.configure()
# Return the final thing object for testing frameworks to be able
# to verify state
return thing
def main(argv):
del argv # Unused
print('Using device', FLAGS.serial)
# Why are we using a timeout?
# This means that we will get an interupt exception after a while
# so we can avoid getting stuck if somebody disconnects the switch
# mid-way.
port = serial.Serial(FLAGS.serial, FLAGS.baud, timeout=FLAGS.timeout)
events = Events()
while True:
try:
loop(port, events)
print('#### Configuration done, resetting state for new device ####')
events.completed()
except UnsupportedDeviceError as e:
print('Unsupported device encountered, resetting state: ', str(e))
events.unsupported()
except device.DeviceTimeoutError:
print('Device timed out, resetting state')
events.timeout()
if __name__ == '__main__':
app.run(main)