blob: 6cb08a4caded3e525dbb66dc16ed81aed6f9a8d2 [file] [log] [blame]
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +01001#! /usr/bin/python
2
3# Copyright 2013 Linaro Limited
4# Author Matt Hart <matthew.hart@linaro.org>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19# MA 02110-1301, USA.
20
Matt Hart1306d822013-08-09 12:08:22 +010021import pexpect
Matt Hart1306d822013-08-09 12:08:22 +010022import os
Matt Hart63ed9112013-08-20 13:31:50 +010023import logging
Matt Hart96ca8662013-08-21 13:37:35 +010024
Matt Hartd73bba22013-08-21 13:46:03 +010025from apcdrivers import apc8959
26from apcdrivers import apc7952
Matt Hart96ca8662013-08-21 13:37:35 +010027
Matt Hart1306d822013-08-09 12:08:22 +010028
29class PDUEngine():
30 connection = None
Matt Hart1306d822013-08-09 12:08:22 +010031 prompt = 0
32 driver = None
33
34 def __init__(self, pdu_hostname, pdu_telnetport = 23):
Matt Hart40d6c312013-08-20 14:47:31 +010035 self.exec_string = "/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport)
36 logging.debug("Created new PDUEngine: %s" % self.exec_string)
Matt Hart1306d822013-08-09 12:08:22 +010037 #self.connection.logfile_read = sys.stdout
Matt Hart40d6c312013-08-20 14:47:31 +010038 prompt = self._pdu_login("apc","apc")
39 if prompt == 0:
Matt Hart63ed9112013-08-20 13:31:50 +010040 logging.debug("Found a v5 prompt")
Matt Hart1306d822013-08-09 12:08:22 +010041 self.driver = apc8959(self.connection)
Matt Hart40d6c312013-08-20 14:47:31 +010042 elif prompt == 1:
Matt Hart63ed9112013-08-20 13:31:50 +010043 logging.debug("Found a v3 prompt")
Matt Hart1306d822013-08-09 12:08:22 +010044 self.driver = apc7952(self.connection)
45 else:
Matt Hart63ed9112013-08-20 13:31:50 +010046 logging.debug("Unknown prompt!")
Matt Hart1306d822013-08-09 12:08:22 +010047
Matt Hart40d6c312013-08-20 14:47:31 +010048 def pduconnect(self):
49 self.connection = self.get_connection(self.exec_string)
50
51 def pduclose(self):
52 self.connection.close(True)
53
54 def pdureconnect(self):
55 self.pduclose()
56 self.pduconnect()
57
58 def get_connection(self, exec_string):
59 connection = pexpect.spawn(exec_string)
60 return connection
61
Matt Hart1306d822013-08-09 12:08:22 +010062 def is_busy(self):
63 if os.path.exists("/proc/%i" % self.connection.pid):
64 return True
65 return False
66
67 def close(self):
68 self.driver._pdu_logout()
69 self.connection.close(True)
70
71 def _pdu_login(self, username, password):
Matt Hart63ed9112013-08-20 13:31:50 +010072 logging.debug("attempting login with username %s, password %s" % (username,password))
Matt Hart40d6c312013-08-20 14:47:31 +010073 self.pduconnect()
Matt Hart1306d822013-08-09 12:08:22 +010074 self.connection.send("\r")
75 self.connection.expect ("User Name :")
76 self.connection.send("apc\r")
77 self.connection.expect("Password :")
78 self.connection.send("apc\r")
Matt Hart40d6c312013-08-20 14:47:31 +010079 return self.connection.expect(["apc>", ">"])
Matt Hart1306d822013-08-09 12:08:22 +010080
81
82if __name__ == "__main__":
83 pe1 = PDUEngine("pdu15")
84 pe1.driver.port_off(22)
85 pe1.driver.port_on(22)
86 pe1.close()
87 pe2 = PDUEngine("pdu14")
88 pe2.driver.port_off(6)
89 pe2.driver.port_on(6)
90 pe2.close()
91 pe3 = PDUEngine("pdu01")
92 pe3.driver.port_reboot(1)
93 pe3.driver.port_off(1)
94 pe3.driver.port_on(1)
95 pe3.close()
96 pe4 = PDUEngine("pdu02")
97 pe4.driver.port_reboot(8)
98 pe4.driver.port_off(8)
99 pe4.driver.port_on(8)
100 pe4.close()
101