blob: 0dad9b647d23730da741af3b2a6a4dec2cd55160 [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.
Matt Hart1306d822013-08-09 12:08:22 +010020
Matt Hart63ed9112013-08-20 13:31:50 +010021import logging
Matt Hart1306d822013-08-09 12:08:22 +010022
Matt Hartd73bba22013-08-21 13:46:03 +010023from driver import PDUDriver
Matt Hart96ca8662013-08-21 13:37:35 +010024
25
Matt Hart1306d822013-08-09 12:08:22 +010026class apc7952(PDUDriver):
27 def _pdu_logout(self):
28 self._back_to_main()
Matt Hart63ed9112013-08-20 13:31:50 +010029 logging.debug("Logging out")
Matt Hart1306d822013-08-09 12:08:22 +010030 self.connection.send("4\r")
31
32 def _back_to_main(self):
Matt Hart63ed9112013-08-20 13:31:50 +010033 logging.debug("Returning to main menu")
Matt Hart1306d822013-08-09 12:08:22 +010034 self.connection.expect('>')
Neil Williams16579dc2014-02-13 11:20:04 +000035 for i in range(1, 20):
Matt Hart1306d822013-08-09 12:08:22 +010036 #print("Sending escape character")
37 self.connection.send("\x1B")
38 self.connection.send("\r")
Neil Williams16579dc2014-02-13 11:20:04 +000039 res = self.connection.expect(["4- Logout", "> "])
Matt Hart1306d822013-08-09 12:08:22 +010040 if res == 0:
Matt Hart63ed9112013-08-20 13:31:50 +010041 logging.debug("Back at main menu")
Matt Hart1306d822013-08-09 12:08:22 +010042 break
43 #self.connection.send("\r")
44 #self.connection.expect("4- Logout")
45 #self.connection.expect("> ")
46
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000047 def _enter_outlet(self, outlet, enter_needed=True):
48 outlet = "%s" % outlet
49 logging.debug("Attempting to enter outlet %s", outlet)
50 if (enter_needed):
51 self.connection.expect("Press <ENTER> to continue...")
52 logging.debug("Sending enter")
Matt Hart1306d822013-08-09 12:08:22 +010053 self.connection.send("\r")
54 self.connection.expect("> ")
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000055 logging.debug("Sending outlet number")
Matt Hart1306d822013-08-09 12:08:22 +010056 self.connection.send(outlet)
57 self.connection.send("\r")
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000058 logging.debug("Finished entering outlet")
Matt Hart1306d822013-08-09 12:08:22 +010059
60 def _port_interaction(self, command, port_number):
61 print("Attempting command: %s port: %i" % (command, port_number))
62 ### make sure in main menu here
63 self._back_to_main()
64 self.connection.send("\r")
65 self.connection.expect("1- Device Manager")
66 self.connection.expect("> ")
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000067 logging.debug("Entering Device Manager")
Matt Hart1306d822013-08-09 12:08:22 +010068 self.connection.send("1\r")
Neil Williams16579dc2014-02-13 11:20:04 +000069 res = self.connection.expect(["3- Outlet Control/Configuration", "2- Outlet Control", "2- Outlet Management", "------- Device Manager"])
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000070 logging.debug("Matched pattern %s", res)
Matt Hart1306d822013-08-09 12:08:22 +010071 if res == 0:
72 self.connection.send("3\r")
73 self._enter_outlet(port_number)
74 elif res == 1:
75 self.connection.send("2\r")
76 self._enter_outlet(port_number)
77 elif res == 2:
78 self.connection.send("2\r")
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000079 elif res == 3:
80 logging.debug("Matched ------- Device Manager")
81 self._enter_outlet(port_number, False)
Matt Hart1306d822013-08-09 12:08:22 +010082 res = self.connection.expect(["1- Control Outlet", "1- Outlet Control/Configuration"])
83 self.connection.expect("> ")
84 self.connection.send("1\r")
Neil Williams16579dc2014-02-13 11:20:04 +000085 res = self.connection.expect(["> ", "Press <ENTER> to continue..."])
Matt Hart1306d822013-08-09 12:08:22 +010086 if res == 1:
Matt Hart63ed9112013-08-20 13:31:50 +010087 logging.debug("Stupid paging thingmy detected, pressing enter")
Matt Hart1306d822013-08-09 12:08:22 +010088 self.connection.send("\r")
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000089 self.connection.send("\r")
Neil Williams16579dc2014-02-13 11:20:04 +000090 res = self.connection.expect(["Control Outlet %s" % port_number, "Control Outlet"])
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000091 if res == 0:
92 logging.debug("Already at the right port")
93 else:
94 self.connection.send("%s\r" % port_number)
95 self.connection.send("1\r")
Matt Hart1306d822013-08-09 12:08:22 +010096 self.connection.expect("3- Immediate Reboot")
97 self.connection.expect("> ")
98 if command == "reboot":
99 self.connection.send("3\r")
100 self.connection.expect("Immediate Reboot")
101 self._do_it()
102 elif command == "delayed":
103 self.connection.send("6\r")
104 self.connection.expect("Delayed Reboot")
105 self._do_it()
106 elif command == "on":
107 self.connection.send("1\r")
108 self.connection.expect("Immediate On")
109 self._do_it()
110 elif command == "off":
111 self.connection.send("2\r")
112 self.connection.expect("Immediate Off")
113 self._do_it()
114 else:
Matt Hart63ed9112013-08-20 13:31:50 +0100115 logging.debug("Unknown command!")
Matt Hart1306d822013-08-09 12:08:22 +0100116
117 def _do_it(self):
118 self.connection.expect("Enter 'YES' to continue or <ENTER> to cancel :")
119 self.connection.send("YES\r")
120 self.connection.expect("Press <ENTER> to continue...")
121 self.connection.send("\r")
122
123 def port_delayed(self, port_number):
124 self._port_interaction("delayed", port_number)
125
126 def port_on(self, port_number):
127 self._port_interaction("on", port_number)
128
129 def port_off(self, port_number):
130 self._port_interaction("off", port_number)
131
132 def port_reboot(self, port_number):
133 self._port_interaction("reboot", port_number)
134
Neil Williams16579dc2014-02-13 11:20:04 +0000135
Matt Hart1306d822013-08-09 12:08:22 +0100136class apc8959(PDUDriver):
137 connection = None
Neil Williams16579dc2014-02-13 11:20:04 +0000138 pdu_commands = {"off": "olOff", "on": "olOn", "reboot": "olReboot", "delayed": "olDlyReboot"}
Matt Hart1306d822013-08-09 12:08:22 +0100139
140 def _pdu_logout(self):
Matt Hart63ed9112013-08-20 13:31:50 +0100141 logging.debug("logging out")
Matt Hart1306d822013-08-09 12:08:22 +0100142 self.connection.send("\r")
143 self.connection.send("exit")
144 self.connection.send("\r")
Matt Hart63ed9112013-08-20 13:31:50 +0100145 logging.debug("done")
Matt Hart1306d822013-08-09 12:08:22 +0100146
147 def _pdu_get_to_prompt(self):
148 self.connection.send("\r")
Neil Williams16579dc2014-02-13 11:20:04 +0000149 self.connection.expect('apc>')
Matt Hart1306d822013-08-09 12:08:22 +0100150
151 def _port_interaction(self, command, port_number):
Matt Hart63ed9112013-08-20 13:31:50 +0100152 logging.debug("Attempting %s on port %i" % (command, port_number))
Matt Hart1306d822013-08-09 12:08:22 +0100153 self._pdu_get_to_prompt()
154 self.connection.sendline(self.pdu_commands[command] + (" %i" % port_number))
155 self.connection.expect("E000: Success")
Matt Hart63ed9112013-08-20 13:31:50 +0100156 logging.debug("done")
Matt Hart1306d822013-08-09 12:08:22 +0100157
158 def port_delayed(self, port_number):
159 self._port_interaction("delayed", port_number)
160
161 def port_on(self, port_number):
162 self._port_interaction("on", port_number)
163
164 def port_off(self, port_number):
165 self._port_interaction("off", port_number)
166
167 def port_reboot(self, port_number):
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +0000168 self._port_interaction("reboot", port_number)