diff options
author | Adrian Likins <alikins@redhat.com> | 2008-07-18 17:34:50 -0400 |
---|---|---|
committer | Adrian Likins <alikins@redhat.com> | 2008-07-18 17:34:50 -0400 |
commit | f471627c772b744e4df5707d5c8db1e3453401b9 (patch) | |
tree | f30ac4a4cd59c4f7ba833c1474c3984531f8ac98 /scripts | |
parent | 6781e5333d14c4503ccc130c161e526fdd11017b (diff) | |
download | func-f471627c772b744e4df5707d5c8db1e3453401b9.tar.gz func-f471627c772b744e4df5707d5c8db1e3453401b9.tar.xz func-f471627c772b744e4df5707d5c8db1e3453401b9.zip |
Do some refactoring of func-transmit.
make JSON the default encoding
reorganize some imports
move everything into a main()
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/func-transmit | 165 |
1 files changed, 86 insertions, 79 deletions
diff --git a/scripts/func-transmit b/scripts/func-transmit index b46a471..4fe3cc3 100644 --- a/scripts/func-transmit +++ b/scripts/func-transmit @@ -10,6 +10,7 @@ ## Copyright 2008, Various ## Marco Mornati <mmornati@byte-code.com> ## Michael DeHaan <mdehaan@redhat.com> +## Adrian Likins <alikins@redhat.com> ## ## This software may be freely redistributed under the terms of the GNU ## general public license. @@ -20,7 +21,7 @@ ## -## Example input file format: +## Example yaml input file format: """ clients: "*" aysnc: False @@ -30,105 +31,111 @@ method: run parameters: "/bin/echo Hello World" """ + import exceptions import string import sys import distutils.sysconfig import optparse -import func.yaml as yaml # FIXME: need to subpackage this as part of Func -import func.overlord.func_command as func_command -import func.overlord.client as fc - -from optparse import OptionParser import simplejson - -# load input parameters -parser = OptionParser() -parser.add_option("-j","--json",help="Json Parser",action="store_true",dest="json",default=False) -parser.add_option("-y","--yaml",help="Yaml Parser",action="store_true",dest="yaml",default=False) - -(language,args)=parser.parse_args() - -# Setting default language if no one is selected by user -if (language.json==False and language.yaml==False): - language.json = True - - -# load input from stdin -input = sys.stdin.read() - -if (language.yaml): - params = yaml.load(input).next() -elif (language.json): - params = simplejson.loads(input) - +import func.overlord.func_command as func_command +import func.overlord.client as fc +import func.yaml as yaml class NoAsyncForListMinionException(exceptions.Exception): - def __init__(self, msg): - self.msg = msg -# scan arguments + def __init__(self, msg): + self.msg = msg + # scan arguments def is_a_list(item): if type(item) in [type([]), type(())]: return True return False -# slightly odd, but we expect clients to be a string we parse -# out later (glob and group expansion, etc). So if it is a list, -# flatten it into a string. -clients = params.get('clients', "*") -if is_a_list(clients): - clients = string.join(clients,';') - -method = params.get('method','unknown') -async = params.get('async', False) - -if method == "list_minions": - # clients will default to * if not specified, and specifing is a good - # way to see who is in what group, etc, so pass it though - - if async == True: - raise NoAsyncForListMinionException("list_minions method can not be run in async") - minion_set = fc.Minions(clients) - servers = minion_set.get_all_hosts() - results = servers +def main(argv): +# load input parameters + parser = optparse.OptionParser() + parser.add_option("-j","--json", + help="Json Parser", + action="store_true", + dest="json", + default=False) + parser.add_option("-y","--yaml", + help="Yaml Parser", + action="store_true", + dest="yaml", + default=False) -else: - # scan more arguments - nforks = params.get('nforks', 1) - module = params.get('module', None) - parameters = params.get('parameters', None) - - # make the call - client = fc.Overlord(clients, async=async, nforks=nforks) - if module is None: - method_handle = getattr(client, method) - else: - module_handle = getattr(client, module) - method_handle = getattr(module_handle, method) - - if parameters is not None: - # listify if we get something thats not a list - if not is_a_list(parameters): - parameters = [parameters] - results = method_handle(*parameters) - else: - results = method_handle() + (options,args) = parser.parse_args() + + # load input from stdin + input = sys.stdin.read() -# convert to selected language (default is YAML) -if (language.json): - output = simplejson.dumps(results) -elif (language.yaml): - output = yaml.dump(results) + # Setting default language if no one is selected by user + if (options.json==False and options.yaml==False): + options.json = True + if (options.yaml): + params = yaml.load(input).next() + elif (options.json): + params = simplejson.loads(input) -# write to stdout -print output + # slightly odd, but we expect clients to be a string we parse + # out later (glob and group expansion, etc). So if it is a list, + # flatten it into a string. + clients = params.get('clients', "*") + if is_a_list(clients): + clients = string.join(clients,';') + method = params.get('method','unknown') + async = params.get('async', False) + if method == "list_minions": + # clients will default to * if not specified, and specifing is a good + # way to see who is in what group, etc, so pass it though -#if __name__ == "__main__": -# main(sys.argv) + if async == True: + raise NoAsyncForListMinionException("list_minions method can not be run in async") + + minion_set = fc.Minions(clients) + servers = minion_set.get_all_hosts() + results = servers + + else: + # scan more arguments + nforks = params.get('nforks', 1) + module = params.get('module', None) + parameters = params.get('parameters', None) + + # make the call + client = fc.Overlord(clients, async=async, nforks=nforks) + if module is None: + method_handle = getattr(client, method) + else: + module_handle = getattr(client, module) + method_handle = getattr(module_handle, method) + + if parameters is not None: + # listify if we get something thats not a list + if not is_a_list(parameters): + parameters = [parameters] + results = method_handle(*parameters) + else: + results = method_handle() + + # convert to selected language (default is JSON) + if (options.json): + output = simplejson.dumps(results) + elif (options.yaml): + output = yaml.dump(results) + + # write to stdout + print output + + + +if __name__ == "__main__": + main(sys.argv) |