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 | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 24 | import sys |
| 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 |
| 32 | logging.debug(settings) |
| 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): |
| 43 | return False |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 44 | |
| 45 | def port_interaction(self, command, port_number): |
| 46 | logging.debug("Running port_interaction from APCBase") |
| 47 | self._port_interaction(command, port_number) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame^] | 48 | #self._cleanup() |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 49 | |
| 50 | def get_connection(self): |
| 51 | logging.debug("Connecting to APC PDU with: %s" % self.exec_string) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame^] | 52 | if logging.getLogger().getEffectiveLevel() == logging.DEBUG: |
| 53 | self.connection = pexpect.spawn(self.exec_string, logfile=sys.stdout) |
| 54 | else: |
| 55 | self.connection = pexpect.spawn(self.exec_string) |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 56 | self._pdu_login("apc","apc") |
| 57 | |
| 58 | def _cleanup(self): |
| 59 | self._pdu_logout() |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame^] | 60 | |
| 61 | def _bombout(self): |
| 62 | logging.debug("Bombing out of driver: %s" % self.connection) |
| 63 | self.connection.close(force=True) |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 64 | del(self) |
| 65 | |
| 66 | def _pdu_login(self, username, password): |
| 67 | logging.debug("attempting login with username %s, password %s" % (username, password)) |
| 68 | self.connection.send("\r") |
| 69 | self.connection.expect("User Name :") |
| 70 | self.connection.send("%s\r" % username) |
| 71 | self.connection.expect("Password :") |
| 72 | self.connection.send("%s\r" % password) |