summaryrefslogtreecommitdiffstats
path: root/scripts/func-transmit
blob: 14556dbc2a376effa048cb6418f8b4055d4795ac (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python

##
## func yaml wrapper tool.
## allows usage of func over stdin/stdin using yaml as a marshalling format
## for access to the Overlord() API from non-Python code.
## this should typically be accessed via a pipe, though also works as
##     func-transmit < yamlfile
##
## Copyright 2008, Various
## Marco Mornati <mmornati@byte-code.com>
## Michael DeHaan <mdehaan@redhat.com>
##
## This software may be freely redistributed under the terms of the GNU
## general public license.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##


## Example input file format:
"""
clients: "*"
aysnc: False
nforks: 1
module: command
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)



class NoAsyncForListMinionException(exceptions.Exception):
    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()
    # for marco ;-> -akl
    results =  {'list_minions': 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 YAML)
if (language.json):
    output = simplejson.dumps(results)
elif (language.yaml):
    output = yaml.dump(results)


# write to stdout
print output



#if __name__ == "__main__":
#    main(sys.argv)