blob: 5f16705168023320f4ca955376866cf00ff01826 [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 Hartd4a09982015-07-17 16:13:11 +010024log = logging.getLogger(__name__)
Matt Hart2af71142014-07-22 10:40:41 +010025
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
Matt Hartd4a09982015-07-17 16:13:11 +010032 log.debug(settings)
Matt Hart132d43c2015-02-25 18:19:53 +000033 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 Hartd4a09982015-07-17 16:13:11 +010043 log.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):
Matt Hartd4a09982015-07-17 16:13:11 +010047 log.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 Hartd4a09982015-07-17 16:13:11 +010052 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 Hartfe042212015-07-09 14:13:29 +010056 self._pdu_login("apc", "apc")
Matt Hart2af71142014-07-22 10:40:41 +010057
58 def _cleanup(self):
Matt Hartfe042212015-07-09 14:13:29 +010059 self._pdu_logout() # pylint: disable=no-member
Matt Hart132d43c2015-02-25 18:19:53 +000060
61 def _bombout(self):
Matt Hartd4a09982015-07-17 16:13:11 +010062 log.debug("Bombing out of driver: %s", self.connection)
Matt Hart132d43c2015-02-25 18:19:53 +000063 self.connection.close(force=True)
Matt Hartfe042212015-07-09 14:13:29 +010064 del self
Matt Hart2af71142014-07-22 10:40:41 +010065
66 def _pdu_login(self, username, password):
Matt Hartd4a09982015-07-17 16:13:11 +010067 log.debug("attempting login with username %s, password %s",
68 username, password)
Matt Hart2af71142014-07-22 10:40:41 +010069 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)