#!/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_service import CuraServiceClient class CuraServiceOptions(CuraBasicOptions): def __init__(self): super(self.__class__, self).__init__( "Available actions:\n" " start, stop, restart, enable, disable, reload,\n" " try-restart, cond-restart, reload-or-restart,\n" " reload-or-try-restart, status\n") self.m_parser.set_usage("Usage: %prog [options] action service") @property def good(self): return len(self.m_pos_options) == 2 @property def action(self): return self.m_pos_options[0] if self.good and self.m_pos_options[0] else "" @property def service(self): if not self.good: return "" return self.m_pos_options[1] if self.good and self.m_pos_options[1] else "" if __name__ == "__main__": options = CuraServiceOptions() 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 = CuraServiceClient(client_hostname, options.username, options.password) (service, rparam) = client.serviceFind(options.service) if not service: sys.stderr.write("%s: %s\n" % (client_hostname, rparam)) continue actions = { "start": (service.start, "Start: "), "stop": (service.stop, "Stop: "), "restart": (service.restart, "Restart: "), "enable": (service.enable, "Enabling: "), "disable": (service.disable, "Disabling: "), "reload": (service.reload, "Reload: "), "try-restart": (service.tryRestart, "Try to restart: "), "cond-restart": (service.condRestart, "Conditional restart: "), "reload-or-restart": (service.reloadRestart, "Reload or restart: "), "reload-or-try-restart": (service.reloadTryRestart, "Reload or try restart: "), "status": (service.status, "Service status: ") } if not client_action in actions: sys.stderr.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)