#!/usr/bin/env python # Copyright (C) 2012 Peter Hatina # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . import sys from cura.cura_options import CuraBasicOptions from cura.cura_address import CuraIpv4Addr, CuraIpv4AddrGenerator from cura.cura_client_power import CuraPowerClient class CuraPowerOptions(CuraBasicOptions): def __init__(self): super(self.__class__, self).__init__( "Available actions:\n" " poweroff, reboot, suspend, hibernate\n" " force-poweroff, force-reboot\n") self.m_parser.set_usage("Usage %prog [options] action") @property def good(self): return len(self.m_pos_options) == 1 @property def action(self): return self.m_pos_options[0] if self.good and self.m_pos_options[0] else "" if __name__ == "__main__": options = CuraPowerOptions() options.parse(sys.argv) if not options.good: sys.stderr.write( "Wrong tool usage. Run " + __file__ + " --help for detailed help.\n") sys.exit(1) client_failed = False client_hostnames = CuraIpv4AddrGenerator(options.hostname).enumerate() client_action = options.action.lower() for client_hostname in client_hostnames: if not len(client_hostname): continue client = CuraPowerClient(client_hostname, options.username, options.password) actions = { "reboot": (client.reboot, "Reboot: "), "poweroff": (client.poweroff, "Poweroff: "), "suspend": (client.suspend, "Suspend: "), "hibernate": (client.hibernate, "Hibernate: ") } if not client_action in actions: sys.stdout.write("No such action to perform!\n") sys.exit(1) (rval, rparam) = actions[client_action][0]() if rval: sys.stdout.write("%s: %s\n" % (client_hostname, actions[client_action][1] + rparam if rparam else "ok")) else: sys.stderr.write("%s: %s\n" % (client_hostname, rparam)) client_failed = True sys.exit(client_failed)