blob: 1946b62802756e5d737324846c20d6245b183997 [file] [log] [blame]
Matt Hart2af71142014-07-22 10:40:41 +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.
20
21import logging
22import pexpect
Matt Hart132d43c2015-02-25 18:19:53 +000023from lavapdu.drivers.driver import PDUDriver
Matt Hart2af71142014-07-22 10:40:41 +010024import sys
25
Matt Hart132d43c2015-02-25 18:19:53 +000026
Matt Hart2af71142014-07-22 10:40:41 +010027class APCBase(PDUDriver):
28 connection = None
29
Matt Hart132d43c2015-02-25 18:19:53 +000030 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 Hart2af71142014-07-22 10:40:41 +010038 self.get_connection()
Matt Hart132d43c2015-02-25 18:19:53 +000039 super(APCBase, self).__init__()
Matt Hart2af71142014-07-22 10:40:41 +010040
Matt Hart132d43c2015-02-25 18:19:53 +000041 @classmethod
42 def accepts(cls, drivername):
Matt Hartfe042212015-07-09 14:13:29 +010043 logging.debug(drivername)
Matt Hart132d43c2015-02-25 18:19:53 +000044 return False
Matt Hart2af71142014-07-22 10:40:41 +010045
46 def port_interaction(self, command, port_number):
47 logging.debug("Running port_interaction from APCBase")
Matt Hartfe042212015-07-09 14:13:29 +010048 self._port_interaction(command, # pylint: disable=no-member
49 port_number)
Matt Hart2af71142014-07-22 10:40:41 +010050
51 def get_connection(self):
Matt Hartfe042212015-07-09 14:13:29 +010052 logging.debug("Connecting to APC PDU with: %s", self.exec_string)
Matt Hart132d43c2015-02-25 18:19:53 +000053 if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
Matt Hartfe042212015-07-09 14:13:29 +010054 self.connection = pexpect.spawn(self.exec_string,
55 logfile=sys.stdout)
Matt Hart132d43c2015-02-25 18:19:53 +000056 else:
57 self.connection = pexpect.spawn(self.exec_string)
Matt Hartfe042212015-07-09 14:13:29 +010058 self._pdu_login("apc", "apc")
Matt Hart2af71142014-07-22 10:40:41 +010059
60 def _cleanup(self):
Matt Hartfe042212015-07-09 14:13:29 +010061 self._pdu_logout() # pylint: disable=no-member
Matt Hart132d43c2015-02-25 18:19:53 +000062
63 def _bombout(self):
Matt Hartfe042212015-07-09 14:13:29 +010064 logging.debug("Bombing out of driver: %s", self.connection)
Matt Hart132d43c2015-02-25 18:19:53 +000065 self.connection.close(force=True)
Matt Hartfe042212015-07-09 14:13:29 +010066 del self
Matt Hart2af71142014-07-22 10:40:41 +010067
68 def _pdu_login(self, username, password):
Matt Hartfe042212015-07-09 14:13:29 +010069 logging.debug("attempting login with username %s, password %s",
70 username, password)
Matt Hart2af71142014-07-22 10:40:41 +010071 self.connection.send("\r")
72 self.connection.expect("User Name :")
73 self.connection.send("%s\r" % username)
74 self.connection.expect("Password :")
75 self.connection.send("%s\r" % password)