7add5bf741e7aa082f5936d15df9b5d265747eec
[certmaster.git] / certmaster / overlord / cmd_modules / call.py
1 """
2 call func method invoker
3
4 Copyright 2007, Red Hat, Inc
5 see AUTHORS
6
7 This software may be freely redistributed under the terms of the GNU
8 general public license.
9
10 You should have received a copy of the GNU General Public License
11 along with this program; if not, write to the Free Software
12 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
13 """
14
15
16 import optparse
17 import pprint
18 import xmlrpclib
19
20 from func.overlord import command
21 from func.overlord import client
22
23 DEFAULT_PORT = 51234
24 DEFAULT_FORKS = 1
25
26 class Call(client.command.Command):
27 name = "call"
28 usage = "call module method name arg1 arg2..."
29 def addOptions(self):
30 self.parser.add_option("-v", "--verbose", dest="verbose",
31 action="store_true")
32 self.parser.add_option("-x", "--xmlrpc", dest="xmlrpc",
33 help="output return data in XMLRPC format",
34 action="store_true")
35 self.parser.add_option("", "--raw", dest="rawprint",
36 help="output return data using Python print",
37 action="store_true")
38 self.parser.add_option("-j", "--json", dest="json",
39 help="output return data using JSON",
40 action="store_true")
41 self.parser.add_option("-p", "--port", dest="port",
42 default=DEFAULT_PORT)
43 self.parser.add_option("-f", "--forks", dest="forks",
44 help="how many parallel processes? (default 1)",
45 default=DEFAULT_FORKS)
46
47 def handleOptions(self, options):
48 self.options = options
49
50 self.verbose = options.verbose
51 self.port = options.port
52
53 # I'm not really a fan of the "module methodname" approach
54 # but we'll keep it for now -akl
55
56 def parse(self, argv):
57 self.argv = argv
58
59 return command.Command.parse(self, argv)
60
61
62 def format_return(self, data):
63 """
64 The call module supports multiple output return types, the default is pprint.
65 """
66
67 if self.options.xmlrpc:
68 return xmlrpclib.dumps((data,""))
69
70 if self.options.json:
71 try:
72 import simplejson
73 return simplejson.dumps(data)
74 except ImportError:
75 print "WARNING: json support not found, install python-simplejson"
76 return data
77
78 if self.options.rawprint:
79 return data
80
81 return pprint.pformat(data)
82
83 def do(self, args):
84
85 # I'm not really a fan of the "module methodname" approach
86 # but we'll keep it for now -akl
87
88 # I kind of feel like we shouldn't be parsing args here, but I'm
89 # not sure what the write place is -al;
90 self.module = args[0]
91 if len(args) > 1:
92 self.method = args[1]
93 else:
94 self.method = None
95 if len(args) > 2:
96 self.method_args = args[2:]
97 else:
98 self.method_args = []
99
100 # this could get weird, sub sub classes might be calling this
101 # this with multiple.parentCommand.parentCommands...
102 # maybe command.py needs a way to set attrs on subCommands?
103 # or some sort of shared datastruct?
104 self.server_spec = self.parentCommand.server_spec
105
106 client_obj = client.Client(self.server_spec,port=self.port,interactive=True,
107 verbose=self.verbose, config=self.config, nforks=self.options.forks)
108 results = client_obj.run(self.module, self.method, self.method_args)
109
110 # TO DO: add multiplexer support
111 # probably as a higher level module.
112
113 # dump the return code stuff atm till we figure out the right place for it
114 return self.format_return(results)