blob: 4809606911b5380b4adb0b588ced4bd7f1a43d47 [file] [log] [blame]
Matt Hart1306d822013-08-09 12:08:22 +01001import pexpect
2import sys
3import os
4from apcdrivers import apc8959
5from apcdrivers import apc7952
Matt Hart63ed9112013-08-20 13:31:50 +01006import logging
Matt Hart40d6c312013-08-20 14:47:31 +01007import time
Matt Hart1306d822013-08-09 12:08:22 +01008
9class PDUEngine():
10 connection = None
Matt Hart1306d822013-08-09 12:08:22 +010011 prompt = 0
12 driver = None
13
14 def __init__(self, pdu_hostname, pdu_telnetport = 23):
Matt Hart40d6c312013-08-20 14:47:31 +010015 self.exec_string = "/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport)
16 logging.debug("Created new PDUEngine: %s" % self.exec_string)
Matt Hart1306d822013-08-09 12:08:22 +010017 #self.connection.logfile_read = sys.stdout
Matt Hart40d6c312013-08-20 14:47:31 +010018 prompt = self._pdu_login("apc","apc")
19 if prompt == 0:
Matt Hart63ed9112013-08-20 13:31:50 +010020 logging.debug("Found a v5 prompt")
Matt Hart1306d822013-08-09 12:08:22 +010021 self.driver = apc8959(self.connection)
Matt Hart40d6c312013-08-20 14:47:31 +010022 elif prompt == 1:
Matt Hart63ed9112013-08-20 13:31:50 +010023 logging.debug("Found a v3 prompt")
Matt Hart1306d822013-08-09 12:08:22 +010024 self.driver = apc7952(self.connection)
25 else:
Matt Hart63ed9112013-08-20 13:31:50 +010026 logging.debug("Unknown prompt!")
Matt Hart1306d822013-08-09 12:08:22 +010027
Matt Hart40d6c312013-08-20 14:47:31 +010028 def pduconnect(self):
29 self.connection = self.get_connection(self.exec_string)
30
31 def pduclose(self):
32 self.connection.close(True)
33
34 def pdureconnect(self):
35 self.pduclose()
36 self.pduconnect()
37
38 def get_connection(self, exec_string):
39 connection = pexpect.spawn(exec_string)
40 return connection
41
Matt Hart1306d822013-08-09 12:08:22 +010042 def is_busy(self):
43 if os.path.exists("/proc/%i" % self.connection.pid):
44 return True
45 return False
46
47 def close(self):
48 self.driver._pdu_logout()
49 self.connection.close(True)
50
51 def _pdu_login(self, username, password):
Matt Hart63ed9112013-08-20 13:31:50 +010052 logging.debug("attempting login with username %s, password %s" % (username,password))
Matt Hart40d6c312013-08-20 14:47:31 +010053 self.pduconnect()
Matt Hart1306d822013-08-09 12:08:22 +010054 self.connection.send("\r")
55 self.connection.expect ("User Name :")
56 self.connection.send("apc\r")
57 self.connection.expect("Password :")
58 self.connection.send("apc\r")
Matt Hart40d6c312013-08-20 14:47:31 +010059 return self.connection.expect(["apc>", ">"])
Matt Hart1306d822013-08-09 12:08:22 +010060
61
62if __name__ == "__main__":
63 pe1 = PDUEngine("pdu15")
64 pe1.driver.port_off(22)
65 pe1.driver.port_on(22)
66 pe1.close()
67 pe2 = PDUEngine("pdu14")
68 pe2.driver.port_off(6)
69 pe2.driver.port_on(6)
70 pe2.close()
71 pe3 = PDUEngine("pdu01")
72 pe3.driver.port_reboot(1)
73 pe3.driver.port_off(1)
74 pe3.driver.port_on(1)
75 pe3.close()
76 pe4 = PDUEngine("pdu02")
77 pe4.driver.port_reboot(8)
78 pe4.driver.port_off(8)
79 pe4.driver.port_on(8)
80 pe4.close()
81