Matt Hart | 2af7114 | 2014-07-22 10:40:41 +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. |
| 20 | |
| 21 | import logging |
| 22 | import pexpect |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 23 | from lavapdu.drivers.driver import PDUDriver |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 24 | log = logging.getLogger(__name__) |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 25 | |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 26 | |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 27 | class APCBase(PDUDriver): |
| 28 | connection = None |
| 29 | |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 30 | def __init__(self, hostname, settings): |
| 31 | self.hostname = hostname |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 32 | log.debug(settings) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 33 | self.settings = settings |
| 34 | telnetport = 23 |
| 35 | if "telnetport" in settings: |
| 36 | telnetport = settings["telnetport"] |
| 37 | self.exec_string = "/usr/bin/telnet %s %d" % (hostname, telnetport) |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 38 | self.get_connection() |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 39 | super(APCBase, self).__init__() |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 40 | |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 41 | @classmethod |
| 42 | def accepts(cls, drivername): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 43 | log.debug(drivername) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 44 | return False |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 45 | |
| 46 | def port_interaction(self, command, port_number): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 47 | log.debug("Running port_interaction from APCBase") |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 48 | self._port_interaction(command, # pylint: disable=no-member |
| 49 | port_number) |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 50 | |
| 51 | def get_connection(self): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 52 | log.debug("Connecting to APC PDU with: %s", self.exec_string) |
| 53 | # only uncomment this line for FULL debug when developing |
| 54 | # self.connection = pexpect.spawn(self.exec_string, logfile=sys.stdout) |
| 55 | self.connection = pexpect.spawn(self.exec_string) |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 56 | self._pdu_login("apc", "apc") |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 57 | |
| 58 | def _cleanup(self): |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 59 | self._pdu_logout() # pylint: disable=no-member |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 60 | |
| 61 | def _bombout(self): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 62 | log.debug("Bombing out of driver: %s", self.connection) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 63 | self.connection.close(force=True) |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 64 | del self |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 65 | |
| 66 | def _pdu_login(self, username, password): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 67 | log.debug("attempting login with username %s, password %s", |
| 68 | username, password) |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 69 | self.connection.send("\r") |
| 70 | self.connection.expect("User Name :") |
| 71 | self.connection.send("%s\r" % username) |
| 72 | self.connection.expect("Password :") |
| 73 | self.connection.send("%s\r" % password) |