blob: aaa19c5ae5a53d5a55e3da385262ea185412a08f [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):
43 return False
Matt Hart2af71142014-07-22 10:40:41 +010044
45 def port_interaction(self, command, port_number):
46 logging.debug("Running port_interaction from APCBase")
47 self._port_interaction(command, port_number)
Matt Hart132d43c2015-02-25 18:19:53 +000048 #self._cleanup()
Matt Hart2af71142014-07-22 10:40:41 +010049
50 def get_connection(self):
51 logging.debug("Connecting to APC PDU with: %s" % self.exec_string)
Matt Hart132d43c2015-02-25 18:19:53 +000052 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 Hart2af71142014-07-22 10:40:41 +010056 self._pdu_login("apc","apc")
57
58 def _cleanup(self):
59 self._pdu_logout()
Matt Hart132d43c2015-02-25 18:19:53 +000060
61 def _bombout(self):
62 logging.debug("Bombing out of driver: %s" % self.connection)
63 self.connection.close(force=True)
Matt Hart2af71142014-07-22 10:40:41 +010064 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)