blob: 4e278b5f3c72e035886ee92a85f25e563fd3e838 [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__':
Matt Hartfe042212015-07-09 14:13:29 +010025 usage = "Usage: %prog --daemon deamonhostname --hostname pduhostname " \
26 "--port pduportnum --command pducommand"
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010027 description = "LAVA PDU daemon client"
Matt Hart76fb2542014-06-01 14:24:56 +010028 commands = ["reboot", "on", "off"]
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010029 parser = optparse.OptionParser(usage=usage, description=description)
Matt Hartfe042212015-07-09 14:13:29 +010030 parser.add_option("--daemon", dest="pdudaemonhostname", action="store",
31 type="string",
32 help="LAVAPDU Daemon hostname (ex: localhost)")
33 parser.add_option("--hostname", dest="pduhostname", action="store",
34 type="string", help="PDU Hostname (ex: pdu05)")
35 parser.add_option("--port", dest="pduportnum", action="store",
36 type="string", help="PDU Portnumber (ex: 04)")
37 parser.add_option("--command", dest="pducommand", action="store",
38 type="string", help="PDU command (ex: reboot|on|off)")
39 parser.add_option("--delay", dest="pdudelay", action="store", type="int",
40 help="Delay before command runs, or between off/on "
41 "when rebooting (ex: 5)")
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010042 (options, args) = parser.parse_args()
Matt Hartfe042212015-07-09 14:13:29 +010043 if not options.pdudaemonhostname \
44 or not options.pduhostname \
45 or not options.pduportnum \
46 or not options.pducommand:
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010047 print("Missing option, try -h for help")
48 exit(1)
49 if not (options.pducommand in commands):
50 print("Unknown pdu command: %s" % options.pducommand)
51 exit(1)
Matt Hart76fb2542014-06-01 14:24:56 +010052 if options.pdudelay:
Matt Hartfe042212015-07-09 14:13:29 +010053 string = ("%s %s %s %s" % (options.pduhostname, options.pduportnum,
54 options.pducommand, options.pdudelay))
Matt Hart76fb2542014-06-01 14:24:56 +010055 else:
Matt Hartfe042212015-07-09 14:13:29 +010056 string = ("%s %s %s" % (options.pduhostname, options.pduportnum,
57 options.pducommand))
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010058 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Matt Hartfe042212015-07-09 14:13:29 +010059 # sock.setblocking(0) # optional non-blocking
Neil Williams6877b1f2014-02-13 11:16:51 +000060 reply = ""
Matt Hartfe042212015-07-09 14:13:29 +010061 # noinspection PyBroadException
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010062 try:
63 sock.connect((options.pdudaemonhostname, 16421))
64 sock.send(string)
65 reply = sock.recv(16384).strip() # limit reply to 16K
66 sock.close()
67 except Exception:
Matt Hart132d43c2015-02-25 18:19:53 +000068 print ("Error sending command, wrong daemon hostname?")
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010069 exit(1)
70 if reply == "ack":
71 print("Command sent successfully.")
72 exit(0)
73 else:
Matt Hartfe042212015-07-09 14:13:29 +010074 print("Error sending command! %s replied: %s" %
75 (options.pdudaemonhostname, reply))
Neil Williams6877b1f2014-02-13 11:16:51 +000076 exit(127)