Better logging and exception handling
diff --git a/engine.py b/engine.py
index 0287eff..4809606 100644
--- a/engine.py
+++ b/engine.py
@@ -4,26 +4,41 @@
from apcdrivers import apc8959
from apcdrivers import apc7952
import logging
+import time
class PDUEngine():
connection = None
- pdu_commands = {"off":"olOff","on":"olOn","reboot":"olReboot","delayed":"olDlyReboot"}
prompt = 0
driver = None
def __init__(self, pdu_hostname, pdu_telnetport = 23):
- self.connection = pexpect.spawn("/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport))
+ self.exec_string = "/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport)
+ logging.debug("Created new PDUEngine: %s" % self.exec_string)
#self.connection.logfile_read = sys.stdout
- self._pdu_login("apc","apc")
- if self.prompt == 0:
+ prompt = self._pdu_login("apc","apc")
+ if prompt == 0:
logging.debug("Found a v5 prompt")
self.driver = apc8959(self.connection)
- elif self.prompt == 1:
+ elif prompt == 1:
logging.debug("Found a v3 prompt")
self.driver = apc7952(self.connection)
else:
logging.debug("Unknown prompt!")
+ def pduconnect(self):
+ self.connection = self.get_connection(self.exec_string)
+
+ def pduclose(self):
+ self.connection.close(True)
+
+ def pdureconnect(self):
+ self.pduclose()
+ self.pduconnect()
+
+ def get_connection(self, exec_string):
+ connection = pexpect.spawn(exec_string)
+ return connection
+
def is_busy(self):
if os.path.exists("/proc/%i" % self.connection.pid):
return True
@@ -35,13 +50,13 @@
def _pdu_login(self, username, password):
logging.debug("attempting login with username %s, password %s" % (username,password))
+ self.pduconnect()
self.connection.send("\r")
self.connection.expect ("User Name :")
self.connection.send("apc\r")
self.connection.expect("Password :")
self.connection.send("apc\r")
- self.prompt = self.connection.expect(["apc>", ">"])
- #print("prompt: %s" % self.prompt)
+ return self.connection.expect(["apc>", ">"])
if __name__ == "__main__":