1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#!/usr/bin/env python
# Copyright (C) 2012 Peter Hatina <phatina@redhat.com>
#
# 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 <http://www.gnu.org/licenses/>.
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)
|