sshxmlrpc_client.py.in 4.68 KB
Newer Older
1 2 3 4 5 6 7
#! /usr/bin/env python
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004 University of Utah and the Flux Group.
# All rights reserved.
#
import sys
8
sys.path.append("@prefix@/lib")
9
import getopt
10
import os
11 12

from sshxmlrpc import *
13
from emulabclient import *
14 15 16 17

##
# The package version number
#
18
PACKAGE_VERSION = 0.1
19

20 21 22 23 24 25 26 27 28
# Default server
XMLRPC_SERVER   = "@BOSSNODE@"

# User supplied server name.
xmlrpc_server   = XMLRPC_SERVER

# User supplied login ID to use (overrides env variable USER if exists).
login_id        = os.environ["USER"]

29 30 31
# The default RPC module to invoke.
module          = "experiment"

32 33 34
# Debugging output.
debug           = 0

35 36 37 38
# For admin people.
SERVER_PATH     = "/usr/testbed/sbin/"
path            = None

39 40 41 42
##
# Print the usage statement to stdout.
#
def usage():
43 44
    print "Make a request to the Emulab XML-RPC (SSH-based) server."
    print ("Usage: " + sys.argv[0] 
45 46
                     + " [-hV] [-l login] [-s server] [-m module] "
                     + "<method> [param=value ...]")
47 48 49
    print
    print "Options:"
    print "  -h, --help\t\t  Display this help message"
50 51 52
    print "  -V, --version\t\t  Show the version number"
    print "  -s, --server\t\t  Set the server hostname"
    print "  -l, --login\t\t  Set the login id (defaults to $USER)"
53
    print "  -m, --module\t\t  Set the RPC module (defaults to experiment)"
54 55 56
    print
    print "Required arguments:"
    print "  method\t\t  The method to execute on the server"
57
    print "  params\t\t\t  The method arguments in param=value format"
58 59 60 61
    print
    print "Example:"
    print ("  "
           + sys.argv[0]
62
           + " -s boss.emulab.net echo \"Hello World!\"")
63 64
    return

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#
# Process a single command line
#
def do_method(server, method_and_args):
    # Get a pointer to the function we want to invoke.
    meth = getattr(server, method_and_args[0])

    # Pop off the method, and then convert the rest of the arguments.
    # Be sure to add the version.
    method_and_args.pop(0)

    #
    # Convert all params (name=value) into a Dictionary. 
    # 
    params = {}
    for param in method_and_args:
81
        plist = string.split(param, "=", 1)
82 83 84 85 86 87 88 89 90 91 92 93 94 95
        if len(plist) != 2:
            print "Parameters are of the form: param=value!"
            return -1
    
        params[plist[0]] = plist[1]
        pass
    meth_args = [ PACKAGE_VERSION, params ]

    #
    # Make the call. 
    #
    response = apply(meth, meth_args)

    #
96
    # Parse the Response, which is a Dictionary. See EmulabResponse in the
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    # emulabclient.py module. The XML standard converts classes to a plain
    # Dictionary, hence the code below. 
    # 
    if len(response["output"]):
        print response["output"]
        pass

    rval = response["code"]

    #
    # If the code indicates failure, look for a "value". Use that as the
    # return value instead of the code. 
    # 
    if rval != RESPONSE_SUCCESS:
        if response["value"]:
            rval = response["value"]
            pass
        pass
    return rval

#
# Process program arguments.
# 
120 121 122
try:
    # Parse the options,
    opts, req_args = getopt.getopt(sys.argv[1:],
123
                                   "dhVs:l:a",
124
                                   [ "help", "version", "server=", "login=", ])
125 126 127 128 129 130 131 132 133 134
    # ... act on them appropriately, and
    for opt, val in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
            pass
        elif opt in ("-V", "--version"):
            print PACKAGE_VERSION
            sys.exit()
            pass
135 136 137 138 139 140
        elif opt in ("-s", "--server"):
	    xmlrpc_server = val
            pass
        elif opt in ("-l", "--login"):
	    login_id = val
            pass
141 142 143
        elif opt in ("-m", "--module"):
	    module = val
            pass
144
        elif opt in ("-d", "--debug"):
145 146
	    debug = 1
            pass
147 148 149
        elif opt in ("-a", "--admin"):
	    path = SERVER_PATH
            pass
150 151 152 153 154 155 156 157 158
        pass
    pass
except getopt.error, e:
    print e.args[0]
    usage()
    sys.exit(2)
    pass

# Get a handle on the server,
159
server = SSHServerProxy("ssh://" + login_id + "@" + xmlrpc_server +
160
                        "/xmlrpc/" + module, path=path)
161

162 163 164 165 166 167 168 169
if len(req_args):
    # Method and args are on the command line.
    sys.exit(do_method(server, req_args))
else:
    # Prompt the user for input.
    try:
        while True:
            line = raw_input("$ ")
170
            tokens = line.split(" ")
171 172 173 174 175 176 177 178 179 180 181 182 183
            if len(tokens) >= 1 and len(tokens[0]) > 0:
                try:
                    print str(do_method(server, tokens))
                    pass
                except xmlrpclib.Fault, e:
                    print e.faultString
                    pass
                pass
            pass
        pass
    except EOFError:
        pass
    print
184 185
    pass