# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2005 The Trustees of Princeton University
#
-# $Id: Shell.py,v 1.18 2006/12/15 18:36:16 mlhuang Exp $
+# $Id: Shell.py,v 1.3 2007/01/16 16:25:12 thierry Exp $
#
+import os
import pydoc
import xmlrpclib
config = None,
# XML-RPC server
url = None, xmlrpc = False, cacert = None,
- # API authentication
- method = None, role = None, user = None, password = None):
+ # API authentication method
+ method = None,
+ # Password authentication
+ role = None, user = None, password = None,
+ # Session authentication
+ session = None):
"""
Initialize a new shell instance. Re-initializes globals.
"""
else:
self.api = PLCAPI(config)
self.config = self.api.config
+ self.url = None
self.server = None
except Exception, err:
# Try connecting to the API server via XML-RPC
self.api = PLCAPI(None)
- if config is None:
- self.config = Config()
- else:
- try:
+ try:
+ if config is None:
+ self.config = Config()
+ else:
self.config = Config(config)
- except Exception, err:
- # Try to continue if no configuration file is available
- self.config = None
- pass
+ except Exception, err:
+ # Try to continue if no configuration file is available
+ self.config = None
if url is None:
if self.config is None:
if cacert is None and self.config is not None:
cacert = self.config.PLC_API_CA_SSL_CRT
- self.server = xmlrpclib.ServerProxy(url, PyCurlTransport(url, cacert), allow_none = 1)
+ self.url = url
+ if cacert is not None:
+ self.server = xmlrpclib.ServerProxy(url, PyCurlTransport(url, cacert), allow_none = 1)
+ else:
+ self.server = xmlrpclib.ServerProxy(url, allow_none = 1)
# Set up authentication structure
- # Default is to use capability authentication
+ # Default is to use session or capability authentication
if (method, user, password) == (None, None, None):
- method = "capability"
+ if session is not None or os.path.exists("/etc/planetlab/session"):
+ method = "session"
+ if session is None:
+ session = "/etc/planetlab/session"
+ else:
+ method = "capability"
if method == "capability":
# Load defaults from configuration file if using capability
if role == "anonymous" or method == "anonymous":
self.auth = {'AuthMethod': "anonymous"}
+ elif method == "session":
+ if session is None:
+ raise Exception, "Must specify session"
+
+ if os.path.exists(session):
+ session = file(session).read()
+
+ self.auth = {'AuthMethod': "session", 'session': session}
else:
if user is None:
raise Exception, "Must specify username"