3 # Reboot specified nodes
9 import errno, time, traceback
12 import threading, popen2
15 from subprocess import PIPE, Popen
16 import pcucontrol.transports.ssh.pxssh as pxssh
17 import pcucontrol.transports.ssh.pexpect as pexpect
22 # Use our versions of telnetlib and pyssh
23 sys.path.insert(0, os.path.dirname(sys.argv[0]))
24 import pcucontrol.transports.telnetlib as telnetlib
25 sys.path.insert(0, os.path.dirname(sys.argv[0]) + "/pyssh")
26 import pcucontrol.transports.pyssh as pyssh
28 # Event class ID from pcu events
29 #NODE_POWER_CONTROL = 3
32 #MONITOR_USER_ID = 11142
38 class ExceptionNoTransport(Exception): pass
39 class ExceptionNotFound(Exception): pass
40 class ExceptionPassword(Exception): pass
41 class ExceptionTimeout(Exception): pass
42 class ExceptionPrompt(Exception): pass
43 class ExceptionSequence(Exception): pass
44 class ExceptionReset(Exception): pass
45 class ExceptionPort(Exception): pass
46 class ExceptionUsername(Exception): pass
50 # PCU has model, host, preferred-port, user, passwd,
52 # This is an object derived directly form the PLCAPI DB fields
54 def __init__(self, plc_pcu_dict):
55 for field in ['username', 'password', 'site_id',
58 'node_ids', 'ports', ]:
59 if field in plc_pcu_dict:
60 self.__setattr__(field, plc_pcu_dict[field])
62 raise Exception("No such field %s in PCU object" % field)
64 # These are the convenience functions build around the PCU object.
66 def __init__(self, plc_pcu_dict):
67 PCU.__init__(self, plc_pcu_dict)
68 self.host = self.pcu_name()
71 if self.hostname is not None and self.hostname is not "":
73 elif self.ip is not None and self.ip is not "":
78 def nodeidToPort(self, node_id):
79 if node_id in self.node_ids:
80 for i in range(0, len(self.node_ids)):
81 if node_id == self.node_ids[i]:
84 raise Exception("No such Node ID: %d" % node_id)
86 # This class captures the observed pcu records from FindBadPCUs.py
88 def __init__(self, pcu_record_dict):
89 for field in ['port_status',
92 if field in pcu_record_dict:
94 self.__setattr__("reboot_str", pcu_record_dict[field])
96 self.__setattr__(field, pcu_record_dict[field])
98 # raise Exception("No such field %s in pcu record dict" % field)
121 def __init__(self, type, verbose):
123 self.verbose = verbose
124 self.transport = None
126 def open(self, host, username=None, password=None, prompt="User Name"):
129 if self.type == self.TELNET:
130 transport = telnetlib.Telnet(host, timeout=self.TELNET_TIMEOUT)
131 transport.set_debuglevel(self.verbose)
132 if username is not None:
133 self.transport = transport
134 self.ifThenSend(prompt, username, ExceptionUsername)
136 elif self.type == self.SSH:
137 if username is not None:
138 transport = pyssh.Ssh(username, host)
139 transport.set_debuglevel(self.verbose)
141 # TODO: have an ssh set_debuglevel() also...
143 raise Exception("Username cannot be None for ssh transport.")
144 elif self.type == self.HTTP:
145 # NOTE: this does not work for all web-based services...
146 self.url = "http://%s:%d/" % (host,80)
147 uri = "%s:%d" % (host,80)
150 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
151 authinfo.add_password (None, uri, username, password)
152 authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
154 transport = urllib2.build_opener(authhandler)
156 raise Exception("Unknown transport type: %s" % self.type)
158 self.transport = transport
162 if self.type == self.TELNET:
163 self.transport.close()
164 elif self.type == self.SSH:
165 self.transport.close()
166 elif self.type == self.HTTP:
169 raise Exception("Unknown transport type %s" % self.type)
170 self.transport = None
172 def write(self, msg):
173 return self.send(msg)
176 if self.transport == None:
177 raise ExceptionNoTransport("transport object is type None")
179 return self.transport.write(msg)
181 def sendPassword(self, password, prompt=None):
182 if self.type == self.TELNET:
184 self.ifThenSend("Password", password, ExceptionPassword)
186 self.ifThenSend(prompt, password, ExceptionPassword)
187 elif self.type == self.SSH:
188 self.ifThenSend("password:", password, ExceptionPassword)
189 elif self.type == self.HTTP:
192 raise Exception("Unknown transport type: %s" % self.type)
194 def sendHTTP(self, resource, data):
196 print "POSTing '%s' to %s" % (data,self.url + resource)
199 f = self.transport.open(self.url + resource ,data)
204 except urllib2.URLError,err:
205 print 'Could not open http connection', err
206 return "http transport error"
210 def ifThenSend(self, expected, buffer, ErrorClass=ExceptionPrompt):
212 if self.transport != None:
213 output = self.transport.read_until(expected, self.TELNET_TIMEOUT)
214 if output.find(expected) == -1:
215 print "OUTPUT: --%s--" % output
216 raise ErrorClass, "'%s' not found" % expected
218 self.transport.write(buffer + "\r\n")
220 raise ExceptionNoTransport("transport object is type None")
222 def ifElse(self, expected, ErrorClass):
224 self.transport.read_until(expected, self.TELNET_TIMEOUT)
226 raise ErrorClass("Could not find '%s' within timeout" % expected)
228 class PCUControl(PCUModel,PCURecord):
231 There are three cases:
232 1) the pcu_record passed below includes port_status from an
234 2) the external probe failed, and the values are empty
235 3) this call is made independent of port_status.
237 In the first case, the first open port is used.
238 In the third case, the ports are tried in sequence.
240 In this way, the port_status value serves only as an optimization,
241 because closed ports are avoided. The supported_ports value should
242 order ports by their preferred usage.
247 def __init__(self, plc_pcu_record, verbose, ignored=None):
248 PCUModel.__init__(self, plc_pcu_record)
249 PCURecord.__init__(self, plc_pcu_record)
251 def reboot(self, node_port, dryrun):
254 # There are two sources of potential ports. Those that are open and
255 # those that are part of the PCU's supported_ports.
256 # I think we should start with supported_ports and then filter that
259 port_list = self.supported_ports
261 if hasattr(self, 'port_status') and self.port_status:
262 # get out the open ports
263 port_list = filter(lambda x: self.port_status[x] == "open" , self.port_status.keys())
264 port_list = [ int(x) for x in port_list ]
265 # take only the open ports that are supported_ports
266 port_list = filter(lambda x: x in self.supported_ports, port_list)
268 raise ExceptionPort("No Open Port: No transport from open ports")
272 ret = "No implementation for open ports on selected PCU model"
273 for port in port_list:
274 if port not in Transport.porttypemap:
277 type = Transport.porttypemap[port]
278 self.transport = Transport(type, verbose)
280 print "checking for run_%s" % type
281 if hasattr(self, "run_%s" % type):
282 print "found run_%s" % type
283 fxn = getattr(self, "run_%s" % type)
284 ret = self.catcherror(fxn, node_port, dryrun)
285 if ret == 0: # NOTE: success!, so stop
292 def run(self, node_port, dryrun):
293 """ This function is to be defined by the specific PCU instance. """
294 raise Exception("This function is not implemented")
297 #def reboot(self, node_port, dryrun):
299 def catcherror(self, function, node_port, dryrun):
301 return function(node_port, dryrun)
302 except ExceptionNotFound, err:
303 return "error: " + str(err)
304 except ExceptionPassword, err:
305 return "Password exception: " + str(err)
306 except ExceptionTimeout, err:
307 return "Timeout exception: " + str(err)
308 except ExceptionUsername, err:
309 return "No username prompt: " + str(err)
310 except ExceptionSequence, err:
311 return "Sequence error: " + str(err)
312 except ExceptionPrompt, err:
313 return "Prompt exception: " + str(err)
314 except ExceptionNoTransport, err:
315 return "No Transport: " + str(err)
316 except ExceptionPort, err:
317 return "No ports exception: " + str(err)
318 except socket.error, err:
319 return "socket error: timeout: " + str(err)
320 except urllib2.HTTPError, err:
321 return "HTTPError: " + str(err)
322 except urllib2.URLError, err:
323 return "URLError: " + str(err)
324 except EOFError, err:
325 self.transport.close()
327 traceback.print_exc()
328 return "EOF connection reset" + str(err)
329 except Exception, err:
330 from monitor.common import email_exception
331 email_exception(self.host)
334 from pcucontrol.util import command
335 from pcucontrol.models import *
338 if pcu['hostname'] is not None and pcu['hostname'] is not "":
339 return pcu['hostname']
340 elif pcu['ip'] is not None and pcu['ip'] is not "":
345 class Unknown(PCUControl):
346 supported_ports = [22,23,80,443,5869,9100,16992]
348 def model_to_object(modelname):
349 if modelname is None:
351 if "AMT" in modelname:
353 elif "BayTech" in modelname:
355 elif "HPiLO" in modelname:
357 elif "IPAL" in modelname:
359 elif "APC" in modelname:
361 elif "DRAC" in modelname:
363 elif "WTI" in modelname:
365 elif "ePowerSwitch" in modelname:
366 return ePowerSwitchNew
367 elif "IPMI" in modelname:
369 elif "BlackBoxPSMaverick" in modelname:
370 return BlackBoxPSMaverick
371 elif "PM211MIP" in modelname:
373 elif "ManualPCU" in modelname:
376 print "UNKNOWN model %s"%modelname
379 def reboot_api(node, pcu):
383 modelname = pcu['model']
385 # get object instance
386 instance = eval('%s(pcu, verbose)' % modelname)
388 i = pcu['node_ids'].index(node['node_id'])
391 rb_ret = instance.reboot(p, False)
393 rb_ret = "No modelname in PCU record."
394 # TODO: how to handle the weird, georgetown pcus, the drac faults, and ilo faults
395 except Exception, err:
396 rb_ret = "Exception Model(%s): " % modelname
401 def convert_oldmodelname_to_newmodelname(oldmodelname, pcu_id):
403 update = { 'AP79xx' : 'APCControl13p13',
404 'Masterswitch' : 'APCControl13p13',
405 'DS4-RPC' : 'BayTech',
406 'IP-41x_IP-81x' : 'IPAL',
409 'ePowerSwitch' : 'ePowerSwitchOld',
412 'PM211-MIP' : 'PM211MIP',
413 'AMT2.5' : 'IntelAMT',
414 'AMT3.0' : 'IntelAMT',
415 'WTI_IPS-4' : 'WTIIPS4',
416 'unknown' : 'ManualPCU',
419 'bbsemaverick' : 'BlackBoxPSMaverick',
420 'manualadmin' : 'ManualPCU',
423 if oldmodelname in update:
424 newmodelname = update[oldmodelname]
426 newmodelname = oldmodelname
428 if pcu_id in [1102,1163,1055,1111,1231,1113,1127,1128,1148]:
429 newmodelname = 'APCControl12p3'
430 elif pcu_id in [1110,86]:
431 newmodelname = 'APCControl1p4'
432 elif pcu_id in [1221,1225,1220,1192]:
433 newmodelname = 'APCControl121p3'
434 elif pcu_id in [1173,1240,47,1363,1405,1401,1372,1371]:
435 newmodelname = 'APCControl121p1'
436 elif pcu_id in [1056,1237,1052,1209,1002,1008,1013,1022]:
437 newmodelname = 'BayTechCtrlC'
439 newmodelname = 'BayTechRPC3NC'
440 elif pcu_id in [1057]:
441 newmodelname = 'BayTechCtrlCUnibe'
442 elif pcu_id in [1012]:
443 newmodelname = 'BayTechRPC16'
444 elif pcu_id in [1089, 1071, 1046, 1035, 1118]:
445 newmodelname = 'ePowerSwitchNew'
449 def reboot_test_new(nodename, values, verbose, dryrun):
451 if 'plc_pcu_stats' in values:
452 values.update(values['plc_pcu_stats'])
455 modelname = convert_oldmodelname_to_newmodelname(values['model'], values['pcu_id'])
457 object = eval('%s(values, verbose)' % modelname)
458 rb_ret = object.reboot(values[nodename], dryrun)
461 # TODO: how to handle the weird, georgetown pcus, the drac faults, and ilo faults
462 except ExceptionPort, err:
464 except NameError, err:
470 print "this does not work."
472 if __name__ == '__main__':