blob: beb954f6c0e7132dbda50114f9ba458e9cbc410c [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
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000024import sys
Matt Hart96ca8662013-08-21 13:37:35 +010025
Matt Hartd73bba22013-08-21 13:46:03 +010026from apcdrivers import apc8959
27from apcdrivers import apc7952
Matt Hart96ca8662013-08-21 13:37:35 +010028
Matt Hart1306d822013-08-09 12:08:22 +010029
30class PDUEngine():
31 connection = None
Matt Hart1306d822013-08-09 12:08:22 +010032 prompt = 0
33 driver = None
34
Neil Williams16579dc2014-02-13 11:20:04 +000035 def __init__(self, pdu_hostname, pdu_telnetport=23):
Matt Hart40d6c312013-08-20 14:47:31 +010036 self.exec_string = "/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport)
37 logging.debug("Created new PDUEngine: %s" % self.exec_string)
Matt Hart1306d822013-08-09 12:08:22 +010038 #self.connection.logfile_read = sys.stdout
Neil Williams16579dc2014-02-13 11:20:04 +000039 prompt = self._pdu_login("apc", "apc")
Matt Hart40d6c312013-08-20 14:47:31 +010040 if prompt == 0:
Matt Hart63ed9112013-08-20 13:31:50 +010041 logging.debug("Found a v5 prompt")
Matt Hart1306d822013-08-09 12:08:22 +010042 self.driver = apc8959(self.connection)
Matt Hart40d6c312013-08-20 14:47:31 +010043 elif prompt == 1:
Matt Hart63ed9112013-08-20 13:31:50 +010044 logging.debug("Found a v3 prompt")
Matt Hart1306d822013-08-09 12:08:22 +010045 self.driver = apc7952(self.connection)
46 else:
Matt Hart63ed9112013-08-20 13:31:50 +010047 logging.debug("Unknown prompt!")
Matt Hart1306d822013-08-09 12:08:22 +010048
Matt Hart40d6c312013-08-20 14:47:31 +010049 def pduconnect(self):
50 self.connection = self.get_connection(self.exec_string)
51
52 def pduclose(self):
53 self.connection.close(True)
54
55 def pdureconnect(self):
56 self.pduclose()
57 self.pduconnect()
58
59 def get_connection(self, exec_string):
60 connection = pexpect.spawn(exec_string)
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000061 connection.logfile = sys.stdout
Matt Hart40d6c312013-08-20 14:47:31 +010062 return connection
63
Matt Hart1306d822013-08-09 12:08:22 +010064 def is_busy(self):
65 if os.path.exists("/proc/%i" % self.connection.pid):
66 return True
67 return False
68
69 def close(self):
70 self.driver._pdu_logout()
71 self.connection.close(True)
72
73 def _pdu_login(self, username, password):
Neil Williams16579dc2014-02-13 11:20:04 +000074 logging.debug("attempting login with username %s, password %s" % (username, password))
Matt Hart40d6c312013-08-20 14:47:31 +010075 self.pduconnect()
Matt Hart1306d822013-08-09 12:08:22 +010076 self.connection.send("\r")
Neil Williams16579dc2014-02-13 11:20:04 +000077 self.connection.expect("User Name :")
matthew.hart@linaro.org93a573b2013-08-27 11:12:31 +010078 self.connection.send("apc\r")
Matt Hart1306d822013-08-09 12:08:22 +010079 self.connection.expect("Password :")
matthew.hart@linaro.org93a573b2013-08-27 11:12:31 +010080 self.connection.send("apc\r")
Matt Hart40d6c312013-08-20 14:47:31 +010081 return self.connection.expect(["apc>", ">"])
Matt Hart1306d822013-08-09 12:08:22 +010082
83
84if __name__ == "__main__":
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000085 #pe1 = PDUEngine("pdu15")
86 #pe1.driver.port_off(22)
87 #pe1.driver.port_on(22)
88 #pe1.close()
89 #pe2 = PDUEngine("pdu14")
90 #pe2.driver.port_off(6)
91 #pe2.driver.port_on(6)
92 #pe2.close()
93 #pe3 = PDUEngine("pdu01")
94 #pe3.driver.port_reboot(1)
95 #pe3.driver.port_off(1)
96 #pe3.driver.port_on(1)
97 #pe3.close()
98 logging.basicConfig(level=logging.DEBUG)
99 logging.getLogger().setLevel(logging.DEBUG)
100 pe4 = PDUEngine("192.168.1.153")
101 pe4.driver.port_reboot(1)
102 pe4.driver.port_reboot(2)
103 pe4.driver.port_reboot(3)
104 pe4.driver.port_reboot(4)
105 pe4.driver.port_reboot(5)
106 pe4.driver.port_reboot(6)
107 pe4.driver.port_reboot(7)
Matt Hart1306d822013-08-09 12:08:22 +0100108 pe4.driver.port_reboot(8)
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +0000109 #pe4.driver.port_off(8)
110 #pe4.driver.port_on(8)
Matt Hart1306d822013-08-09 12:08:22 +0100111 pe4.close()