blob: 72d0d5462f61623188f9cd4fca262c05e55dd4bf [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('>')
35 for i in range(1,20):
36 #print("Sending escape character")
37 self.connection.send("\x1B")
38 self.connection.send("\r")
39 res = self.connection.expect(["4- Logout","> "])
40 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")
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +000069 res = self.connection.expect(["3- Outlet Control/Configuration","2- Outlet Control","2- Outlet Management","------- Device Manager"])
70 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")
85 res = self.connection.expect(["> ","Press <ENTER> to continue..."])
86 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")
90 res = self.connection.expect(["Control Outlet %s" % port_number,"Control Outlet"])
91 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
135class apc8959(PDUDriver):
136 connection = None
Matt Hart40d6c312013-08-20 14:47:31 +0100137 pdu_commands = {"off":"olOff","on":"olOn","reboot":"olReboot","delayed":"olDlyReboot"}
Matt Hart1306d822013-08-09 12:08:22 +0100138
139 def _pdu_logout(self):
Matt Hart63ed9112013-08-20 13:31:50 +0100140 logging.debug("logging out")
Matt Hart1306d822013-08-09 12:08:22 +0100141 self.connection.send("\r")
142 self.connection.send("exit")
143 self.connection.send("\r")
Matt Hart63ed9112013-08-20 13:31:50 +0100144 logging.debug("done")
Matt Hart1306d822013-08-09 12:08:22 +0100145
146 def _pdu_get_to_prompt(self):
147 self.connection.send("\r")
148 self.connection.expect ('apc>')
149
150 def _port_interaction(self, command, port_number):
Matt Hart63ed9112013-08-20 13:31:50 +0100151 logging.debug("Attempting %s on port %i" % (command, port_number))
Matt Hart1306d822013-08-09 12:08:22 +0100152 self._pdu_get_to_prompt()
153 self.connection.sendline(self.pdu_commands[command] + (" %i" % port_number))
154 self.connection.expect("E000: Success")
Matt Hart63ed9112013-08-20 13:31:50 +0100155 logging.debug("done")
Matt Hart1306d822013-08-09 12:08:22 +0100156
157 def port_delayed(self, port_number):
158 self._port_interaction("delayed", port_number)
159
160 def port_on(self, port_number):
161 self._port_interaction("on", port_number)
162
163 def port_off(self, port_number):
164 self._port_interaction("off", port_number)
165
166 def port_reboot(self, port_number):
matthew.hart@linaro.org753a90c2014-01-20 09:45:25 +0000167 self._port_interaction("reboot", port_number)