3 # Interactive shell for testing PLCAPI
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
8 # $Id: plcsh,v 1.5 2007/02/02 04:39:03 mlhuang Exp $
13 from socket import gethostname
14 from optparse import OptionParser
15 from getpass import getpass
16 from traceback import print_exc
18 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
19 from PLC.Shell import Shell
21 parser = OptionParser(add_help_option = False)
22 parser.add_option("-f", "--config", help = "PLC configuration file")
23 parser.add_option("-h", "--url", help = "API URL")
24 parser.add_option("-c", "--cacert", help = "API SSL certificate")
25 parser.add_option("-k", "--insecure", help = "Do not check SSL certificate")
26 parser.add_option("-m", "--method", help = "API authentication method")
27 parser.add_option("-s", "--session", help = "API session key")
28 parser.add_option("-u", "--user", help = "API user name")
29 parser.add_option("-p", "--password", help = "API password")
30 parser.add_option("-r", "--role", help = "API role")
31 parser.add_option("-x", "--xmlrpc", action = "store_true", default = False, help = "Use XML-RPC interface")
32 parser.add_option("--help", action = "help", help = "show this help message and exit")
33 (options, args) = parser.parse_args()
35 # If user is specified but password is not
36 if options.user is not None and options.password is None:
38 options.password = getpass()
39 except (EOFError, KeyboardInterrupt):
43 # Initialize a single global instance (scripts may re-initialize
44 # this instance and/or create additional instances).
46 shell = Shell(globals = globals(),
47 config = options.config,
48 url = options.url, xmlrpc = options.xmlrpc, cacert = options.cacert,
49 method = options.method, role = options.role,
50 user = options.user, password = options.password,
51 session = options.session)
52 # Register a few more globals for backward compatibility
56 except Exception, err:
62 # If called by a script
63 if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
64 # Pop us off the argument stack
68 # Otherwise, run an interactive shell environment
70 if shell.server is None:
71 print "PlanetLab Central Direct API Access"
73 elif shell.auth['AuthMethod'] == "anonymous":
74 prompt = "[anonymous]"
75 print "Connected anonymously"
76 elif shell.auth['AuthMethod'] == "session":
77 # XXX No way to tell node and user sessions apart from the
78 # client point of view.
79 prompt = "[%s]" % gethostname()
80 print "%s connected using session authentication" % gethostname()
82 prompt = "[%s]" % shell.auth['Username']
83 print "%s connected using %s authentication" % \
84 (shell.auth['Username'], shell.auth['AuthMethod'])
86 # Readline and tab completion support
91 print 'Type "system.listMethods()" or "help(method)" for more information.'
92 # Load command history
93 history_path = os.path.join(os.environ["HOME"], ".plcapi_history")
95 file(history_path, 'a').close()
96 readline.read_history_file(history_path)
97 atexit.register(readline.write_history_file, history_path)
101 # Enable tab completion
102 readline.parse_and_bind("tab: complete")
114 line = raw_input(prompt + sep)
116 except KeyboardInterrupt:
121 # Build up multi-line command
124 # Blank line or first line does not end in :
125 if line == "" or (command == line and line[-1] != ':'):
128 command += os.linesep
134 elif command in ["q", "quit", "exit"]:
139 # Try evaluating as an expression and printing the result
140 result = eval(command)
141 if result is not None:
144 # Fall back to executing as a statement
146 except Exception, err: