83de3a5a60c225d1b11361e03464b96aff90ccf3
[monitor.git] / pcucontrol / models / BayTech.py
1 from pcucontrol.reboot import *
2
3 class BayTechRPC3NC(PCUControl):
4         def run_telnet(self, node_port, dryrun):
5                 return self.run_ssh(node_port, dryrun)
6
7         def run_ssh(self, node_port, dryrun):
8                 self.transport.open(self.host, self.username, None, "Enter user name:")
9                 self.transport.sendPassword(self.password, "Enter Password:")
10
11                 #self.transport.ifThenSend("RPC-16>", "Status")
12                 self.transport.ifThenSend("RPC3-NC>", "Reboot %d" % node_port)
13
14                 # Reboot Outlet  N        (Y/N)?
15                 if dryrun:
16                         self.transport.ifThenSend("(Y/N)?", "N")
17                 else:
18                         self.transport.ifThenSend("(Y/N)?", "Y")
19                 self.transport.ifThenSend("RPC3-NC>", "")
20
21                 self.transport.close()
22                 return 0
23
24 class BayTechRPC16(PCUControl):
25         def run_telnet(self, node_port, dryrun):
26                 return self.run_ssh(node_port, dryrun)
27         def run_ssh(self, node_port, dryrun):
28                 self.transport.open(self.host, self.username, None, "Enter user name:")
29                 self.transport.sendPassword(self.password, "Enter Password:")
30
31                 #self.transport.ifThenSend("RPC-16>", "Status")
32
33                 self.transport.ifThenSend("RPC-16>", "Reboot %d" % node_port)
34
35                 # Reboot Outlet  N        (Y/N)?
36                 if dryrun:
37                         self.transport.ifThenSend("(Y/N)?", "N")
38                 else:
39                         self.transport.ifThenSend("(Y/N)?", "Y")
40                 self.transport.ifThenSend("RPC-16>", "")
41
42                 self.transport.close()
43                 return 0
44
45 class BayTechCtrlCUnibe(PCUControl):
46         """
47                 For some reason, these units let you log in fine, but they hang
48                 indefinitely, unless you send a Ctrl-C after the password.  No idea
49                 why.
50         """
51         def run_ssh(self, node_port, dryrun):
52                 print "BayTechCtrlC %s" % self.host
53
54                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
55                 s = pxssh.pxssh()
56                 if not s.login(self.host, self.username, self.password, ssh_options):
57                         raise ExceptionPassword("Invalid Password")
58                 # Otherwise, the login succeeded.
59
60                 # Send a ctrl-c to the remote process.
61                 print "sending ctrl-c"
62                 s.send(chr(3))
63
64                 # Control Outlets  (5 ,1).........5
65                 try:
66                         #index = s.expect("Enter Request")
67                         index = s.expect(["Enter Request :"])
68
69                         if index == 0:
70                                 print "3"
71                                 s.send("3\r\n")
72                                 index = s.expect(["DS-RPC>", "Enter user name:"])
73                                 if index == 1:
74                                         s.send(self.username + "\r\n")
75                                         index = s.expect(["DS-RPC>"])
76
77                                 if index == 0:
78                                         print "Reboot %d" % node_port
79                                         time.sleep(5)
80                                         s.send("Reboot %d\r\n" % node_port)
81
82                                         time.sleep(5)
83                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
84                                         if index == 0:
85                                                 if dryrun:
86                                                         print "sending N"
87                                                         s.send("N\r\n")
88                                                 else:
89                                                         print "sending Y"
90                                                         s.send("Y\r\n")
91                                         elif index == 1:
92                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
93                                         elif index == 2:
94                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
95
96                                 time.sleep(5)
97                                 index = s.expect(["DS-RPC>"])
98                                 #print "got prompt back"
99
100                         s.close()
101
102                 except pexpect.EOF:
103                         raise ExceptionPrompt("EOF before expected Prompt")
104                 except pexpect.TIMEOUT:
105                         raise ExceptionPrompt("Timeout before expected Prompt")
106
107                 return 0
108
109 class BayTechCtrlC(PCUControl):
110         """
111                 For some reason, these units let you log in fine, but they hang
112                 indefinitely, unless you send a Ctrl-C after the password.  No idea
113                 why.
114         """
115         def run_ssh(self, node_port, dryrun):
116                 print "BayTechCtrlC %s" % self.host
117
118                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
119                 s = pxssh.pxssh()
120                 if not s.login(self.host, self.username, self.password, ssh_options):
121                         raise ExceptionPassword("Invalid Password")
122                 # Otherwise, the login succeeded.
123
124                 # Send a ctrl-c to the remote process.
125                 print "SENDING ctrl-c"
126                 s.send(chr(3))
127
128                 # Control Outlets  (5 ,1).........5
129                 try:
130                         print "EXPECTING: ", "Enter Request :"
131                         index = s.expect(["Enter Request :"])
132
133                         if index == 0:
134                                 print "SENDING: 5"
135                                 s.send("5\r\n")
136                                 print "EXPECTING: ", "DS-RPC>"
137                                 index = s.expect(["DS-RPC>", "Enter user name:", "Port in use."])
138                                 if index == 1:
139                                         print "sending username"
140                                         s.send(self.username + "\r\n")
141                                         index = s.expect(["DS-RPC>"])
142                                 elif index == 2:
143                                         raise ExceptionPrompt("PCU Reported 'Port in use.'")
144
145                                 if index == 0:
146                                         print "SENDING: Reboot %d" % node_port
147                                         s.send("Reboot %d\r\n" % node_port)
148
149                                         print "SLEEPING: 5"
150                                         time.sleep(5)
151                                         print "EXPECTING: ", "Y/N?"
152                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
153                                         if index == 0:
154                                                 if dryrun:
155                                                         print "sending N"
156                                                         s.send("N\r\n")
157                                                 else:
158                                                         print "SENDING: Y"
159                                                         s.send("Y\r\n")
160                                         elif index == 1:
161                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
162                                         elif index == 2:
163                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
164
165                                 # NOTE: for some reason, the script times out with the
166                                 # following line.  In manual tests, it works correctly, but
167                                 # with automated tests, evidently it fails.
168                                 print "SLEEPING: 5"
169                                 time.sleep(5)
170                                 #print "TOTAL--", s.allstr, "--EOT"
171                                 index = s.expect(["DS-RPC>"])
172                                 print "got prompt back"
173
174                         s.close()
175
176                 except pexpect.EOF:
177                         raise ExceptionPrompt("EOF before 'Enter Request' Prompt")
178                 except pexpect.TIMEOUT:
179                         raise ExceptionPrompt("Timeout before Prompt")
180
181                 return 0
182
183 class BayTech(PCUControl):
184         supported_ports = [22,23]
185
186         def run_telnet(self, node_port, dryrun):
187                 return self.run_ssh(node_port, dryrun)
188
189         def run_ssh(self, node_port, dryrun):
190                 self.transport.open(self.host, self.username)
191                 self.transport.sendPassword(self.password)
192
193                 # Control Outlets  (5 ,1).........5
194                 self.transport.ifThenSend("Enter Request :", "5")
195
196                 # Reboot N
197                 try:
198                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port, ExceptionNotFound)
199                 except ExceptionNotFound, msg:
200                         # one machine is configured to ask for a username,
201                         # even after login...
202                         print "msg: %s" % msg
203                         self.transport.write(self.username + "\r\n")
204                         time.sleep(5)
205                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port)
206
207                 # Reboot Outlet  N        (Y/N)?
208                 if dryrun:
209                         self.transport.ifThenSend("(Y/N)?", "N")
210                 else:
211                         self.transport.ifThenSend("(Y/N)?", "Y")
212                 time.sleep(5)
213                 self.transport.ifThenSend("DS-RPC>", "")
214
215                 self.transport.close()
216                 return 0