flexdataclient.in 2.75 KB
Newer Older
1
#!/usr/local/bin/python
2 3
#
# Copyright (c) 2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
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

import sys
import getopt
import xmlrpclib
import traceback
import string

DEF_HOST = 'ops.emulab.net'
DEF_PORT = 3993

def usage(progname):
    print "Usage: %s [-hp <arg>]" % str(progname)
    print "  -h <name>    Connect to this server (default: %s)" % DEF_HOST
    print "  -p <port>    Connect to this port (default: %d)" % DEF_PORT
    sys.exit(2)
    pass

def main():
    try:
        opts,args = getopt.getopt(sys.argv[1:],"h:p:")
    except getopt.GetoptError:
        traceback.print_exc()
        usage(sys.argv[0])
        pass

    (host,port) = (DEF_HOST,DEF_PORT)

    for (o,v) in opts:
        if o == '-h':
            host = v
            pass
        elif o == '-p':
            port = int(v)
            pass
        pass

    if len(args) < 1:
        usage(sys.argv[0])
        pass

    method = 'flexlab.' + args.pop(0)
    params = dict({})
    for param in args:
        plist = string.split(param, "=", 1)
        if len(plist) != 2:
            print ("error: Parameter, '"
                   + param
                   + "', is not of the form: param=value!")
            return -1
        value = plist[1]

        #
        # Evaluate the arg no matter what (so we get int/float/str, in addition
        # to list/dict).
        #
        try:
            value = eval(value)
        except:
            print "Error while parsing argument into python variable:"
            traceback.print_exc()
            sys.exit(3)
            pass
        
        params[plist[0]] = value
        pass

    #
    # Setup the server, grab the method
    #
    surl = "http://%s:%d/" % (host,port)
    server = xmlrpclib.ServerProxy(surl,allow_none=1)

    meth = getattr(server,method)
    
    #
    # Make the call. 
    #
    try:
        response = apply(meth,[params])
        pass
    except xmlrpclib.Fault, e:
        print e.faultString
        return -1

    print str(response)
    
    sys.exit(0)


if __name__ == '__main__':
    main()
    pass