matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 1 | #! /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 Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 20 | |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 21 | import logging |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 22 | |
Matt Hart | d73bba2 | 2013-08-21 13:46:03 +0100 | [diff] [blame] | 23 | from driver import PDUDriver |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 24 | |
| 25 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 26 | class apc7952(PDUDriver): |
| 27 | def _pdu_logout(self): |
| 28 | self._back_to_main() |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 29 | logging.debug("Logging out") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 30 | self.connection.send("4\r") |
| 31 | |
| 32 | def _back_to_main(self): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 33 | logging.debug("Returning to main menu") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 34 | self.connection.expect('>') |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 35 | for i in range(1, 20): |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 36 | #print("Sending escape character") |
| 37 | self.connection.send("\x1B") |
| 38 | self.connection.send("\r") |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 39 | res = self.connection.expect(["4- Logout", "> "]) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 40 | if res == 0: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 41 | logging.debug("Back at main menu") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 42 | break |
| 43 | #self.connection.send("\r") |
| 44 | #self.connection.expect("4- Logout") |
| 45 | #self.connection.expect("> ") |
| 46 | |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 47 | 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 Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 53 | self.connection.send("\r") |
| 54 | self.connection.expect("> ") |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 55 | logging.debug("Sending outlet number") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 56 | self.connection.send(outlet) |
| 57 | self.connection.send("\r") |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 58 | logging.debug("Finished entering outlet") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 59 | |
| 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.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 67 | logging.debug("Entering Device Manager") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 68 | self.connection.send("1\r") |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 69 | res = self.connection.expect(["3- Outlet Control/Configuration", "2- Outlet Control", "2- Outlet Management", "------- Device Manager"]) |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 70 | logging.debug("Matched pattern %s", res) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 71 | 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.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 79 | elif res == 3: |
| 80 | logging.debug("Matched ------- Device Manager") |
| 81 | self._enter_outlet(port_number, False) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 82 | res = self.connection.expect(["1- Control Outlet", "1- Outlet Control/Configuration"]) |
| 83 | self.connection.expect("> ") |
| 84 | self.connection.send("1\r") |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 85 | res = self.connection.expect(["> ", "Press <ENTER> to continue..."]) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 86 | if res == 1: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 87 | logging.debug("Stupid paging thingmy detected, pressing enter") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 88 | self.connection.send("\r") |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 89 | self.connection.send("\r") |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 90 | res = self.connection.expect(["Control Outlet %s" % port_number, "Control Outlet"]) |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 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 Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 96 | 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 Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 115 | logging.debug("Unknown command!") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 116 | |
| 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 Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 135 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 136 | class apc8959(PDUDriver): |
| 137 | connection = None |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 138 | pdu_commands = {"off": "olOff", "on": "olOn", "reboot": "olReboot", "delayed": "olDlyReboot"} |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 139 | |
| 140 | def _pdu_logout(self): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 141 | logging.debug("logging out") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 142 | self.connection.send("\r") |
| 143 | self.connection.send("exit") |
| 144 | self.connection.send("\r") |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 145 | logging.debug("done") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 146 | |
| 147 | def _pdu_get_to_prompt(self): |
| 148 | self.connection.send("\r") |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 149 | self.connection.expect('apc>') |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 150 | |
| 151 | def _port_interaction(self, command, port_number): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 152 | logging.debug("Attempting %s on port %i" % (command, port_number)) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 153 | self._pdu_get_to_prompt() |
| 154 | self.connection.sendline(self.pdu_commands[command] + (" %i" % port_number)) |
| 155 | self.connection.expect("E000: Success") |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 156 | logging.debug("done") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 157 | |
| 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.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 168 | self._port_interaction("reboot", port_number) |