blob: 143da8e12be38022f8753e65c7d6fd9fe31f8b1a [file] [log] [blame]
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +01001#!/usr/bin/python
2
Matt Hart76fb2542014-06-01 14:24:56 +01003# 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
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010021import socket
22import optparse
23
24if __name__ == '__main__':
Neil Williams424fbbb2014-05-16 20:34:00 +010025 usage = "Usage: %prog --daemon deamonhostname --hostname pduhostname --port pduportnum --command pducommand"
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010026 description = "LAVA PDU daemon client"
Matt Hart76fb2542014-06-01 14:24:56 +010027 commands = ["reboot", "on", "off"]
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010028 parser = optparse.OptionParser(usage=usage, description=description)
Matt Hart132d43c2015-02-25 18:19:53 +000029 parser.add_option("--daemon", dest="pdudaemonhostname", action="store", type="string", help="LAVAPDU Daemon hostname (ex: localhost)")
Neil Williams16579dc2014-02-13 11:20:04 +000030 parser.add_option("--hostname", dest="pduhostname", action="store", type="string", help="PDU Hostname (ex: pdu05)")
31 parser.add_option("--port", dest="pduportnum", action="store", type="string", help="PDU Portnumber (ex: 04)")
Matt Hart76fb2542014-06-01 14:24:56 +010032 parser.add_option("--command", dest="pducommand", action="store", type="string", help="PDU command (ex: reboot|on|off)")
Matt Hart132d43c2015-02-25 18:19:53 +000033 parser.add_option("--delay", dest="pdudelay", action="store", type="int", help="Delay before command runs, or between off/on when rebooting (ex: 5)")
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010034 (options, args) = parser.parse_args()
35 if (not (options.pdudaemonhostname) or not(options.pduhostname) or not (options.pduportnum) or not (options.pducommand)):
36 print("Missing option, try -h for help")
37 exit(1)
38 if not (options.pducommand in commands):
39 print("Unknown pdu command: %s" % options.pducommand)
40 exit(1)
Matt Hart76fb2542014-06-01 14:24:56 +010041 if options.pdudelay:
Matt Hart132d43c2015-02-25 18:19:53 +000042 string = ("%s %s %s %s" % (options.pduhostname, options.pduportnum, options.pducommand, options.pdudelay))
Matt Hart76fb2542014-06-01 14:24:56 +010043 else:
44 string = ("%s %s %s" % (options.pduhostname, options.pduportnum, options.pducommand))
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010045 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46 #sock.setblocking(0) # optional non-blocking
Neil Williams6877b1f2014-02-13 11:16:51 +000047 reply = ""
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010048 try:
49 sock.connect((options.pdudaemonhostname, 16421))
50 sock.send(string)
51 reply = sock.recv(16384).strip() # limit reply to 16K
52 sock.close()
53 except Exception:
Matt Hart132d43c2015-02-25 18:19:53 +000054 print ("Error sending command, wrong daemon hostname?")
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010055 exit(1)
56 if reply == "ack":
57 print("Command sent successfully.")
58 exit(0)
59 else:
Neil Williams6877b1f2014-02-13 11:16:51 +000060 print("Unknown error sending command! %s replied: %s" % (options.pdudaemonhostname, reply))
61 exit(127)