X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plcapi.py;h=be77171d9116b68b41b820b7ad7bead07c2cc2bc;hb=refs%2Fheads%2F1.8;hp=e89663d5644960534fa015b0e555edb28f31f3c2;hpb=b0deb4f91ea673181a2db5ee432f8d9c36fc7312;p=nodemanager.git diff --git a/plcapi.py b/plcapi.py index e89663d..be77171 100644 --- a/plcapi.py +++ b/plcapi.py @@ -1,5 +1,8 @@ +# $Id$ + import safexmlrpc import hmac, sha +import logger class PLCAPI: """ @@ -15,15 +18,46 @@ class PLCAPI: the new session-based method. """ - def __init__(self, uri, auth, **kwds): + def __init__(self, uri, cacert, auth, timeout = 90, **kwds): + self.uri = uri + self.cacert = cacert + self.timeout = timeout + if isinstance(auth, (tuple, list)): (self.node_id, self.key) = auth self.session = None - else: + elif isinstance(auth, (str, unicode)): self.node_id = self.key = None self.session = auth + else: + self.node_id = self.key = self.session = None + + self.server = safexmlrpc.ServerProxy(self.uri, self.cacert, self.timeout, allow_none = 1, **kwds) + + + def update_session(self, f="/usr/boot/plnode.txt"): + # try authenticatipopulate /etc.planetlab/session + def plnode(key): + try: + return [i[:-1].split('=') for i in open(f).readlines() if i.startswith(key)][0][1].strip('"') + except: + return None + + auth = (int(plnode("NODE_ID")), plnode("NODE_KEY")) + plc = PLCAPI(self.uri, self.cacert, auth, self.timeout) + open("/etc/planetlab/session", 'w').write(plc.GetSession().strip()) + self.session = open("/etc/planetlab/session").read().strip() + + + def check_authentication(self): + authstatus = False + if self.key or self.session: + try: + authstatus = self.AuthCheck() + except: + logger.log_exc() + return authstatus - self.server = safexmlrpc.ServerProxy(uri, allow_none = 1, **kwds) def add_auth(self, function): """ @@ -64,7 +98,8 @@ class PLCAPI: if self.session is not None: # Use session authentication - auth = {'session': self.session} + auth = {'AuthMethod': "session", + 'session': self.session} else: # Yes, this is the "canonicalization" method used. args = canonicalize(params)