X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=monitor%2Fwrapper%2Fplc.py;h=76db7d8699dd6052d43930d2b8f621d2550fa8fb;hb=0fabfc8dbe8f1f2c0d12397e1bc8c6ed686fb5ed;hp=783efbc744634df1b9b2a1286861f98fa8bd9546;hpb=e811d83a1664a222bed13266703d2aa4f1a1900d;p=monitor.git diff --git a/monitor/wrapper/plc.py b/monitor/wrapper/plc.py index 783efbc..76db7d8 100644 --- a/monitor/wrapper/plc.py +++ b/monitor/wrapper/plc.py @@ -12,6 +12,8 @@ import xml, xmlrpclib import logging import time import traceback +from monitor import database + try: import config debug = config.debug @@ -61,12 +63,100 @@ class PLC: def __repr__(self): return self.api.__repr__() +class CachedPLC(PLC): + + def _param_to_str(self, name, *params): + fields = len(params) + retstr = "" + retstr += "%s-" % name + for x in params: + retstr += "%s-" % x + return retstr[:-1] + + def __getattr__(self, name): + method = getattr(self.api, name) + if method is None: + raise AssertionError("method does not exist") + + def run_or_returncached(*params): + cachename = self._param_to_str(name, *params) + #print "cachename is %s" % cachename + if 'Get' in name: + if not database.cachedRecently(cachename): + load_old_cache = False + try: + values = method(self.auth, *params) + except: + print "Call %s FAILED: Using old cached data" % cachename + load_old_cache = True + + if load_old_cache: + values = database.dbLoad(cachename) + else: + database.dbDump(cachename, values) + + return values + else: + values = database.dbLoad(cachename) + return values + else: + return method(self.auth, *params) + + return run_or_returncached + + def getAPI(url): return xmlrpclib.Server(url, verbose=False, allow_none=True) def getAuthAPI(): return PLC(auth.auth, auth.server) +def getCachedAuthAPI(): + return CachedPLC(auth.auth, auth.server) + +def getTechEmails(loginbase): + """ + For the given site, return all user email addresses that have the 'tech' role. + """ + api = getAuthAPI() + # get site details. + s = api.GetSites(loginbase)[0] + # get people at site + p = api.GetPersons(s['person_ids']) + # pull out those with the right role. + emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ] + return emails + +def getPIEmails(loginbase): + """ + For the given site, return all user email addresses that have the 'tech' role. + """ + api = getAuthAPI() + # get site details. + s = api.GetSites(loginbase)[0] + # get people at site + p = api.GetPersons(s['person_ids']) + # pull out those with the right role. + emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ] + return emails + +def getSliceUserEmails(loginbase): + """ + For the given site, return all user email addresses that have the 'tech' role. + """ + #api = getAuthAPI() + # get site details. + s = api.GetSites(loginbase)[0] + # get people at site + slices = api.GetSlices(s['slice_ids']) + people = [] + for slice in slices: + people += api.GetPersons(slice['person_ids']) + # pull out those with the right role. + emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ] + unique_emails = [ x for x in set(emails) ] + return unique_emails + ''' Returns list of nodes in dbg as reported by PLC ''' @@ -84,19 +174,20 @@ def nodesDbg(): Returns loginbase for given nodename ''' def siteId(nodename): - api = xmlrpclib.Server(auth.server, verbose=False) - anon = {'AuthMethod': "anonymous"} - site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id']) + api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True) + site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id']) if len(site_id) == 1: - loginbase = api.GetSites (anon, site_id[0], ["login_base"]) + loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"]) return loginbase[0]['login_base'] + else: + print "Not nodes returned!!!!" ''' Returns list of slices for a site. ''' def slices(loginbase): siteslices = [] - api = xmlrpclib.Server(auth.server, verbose=False) + api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True) sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids'] for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]): siteslices.append(slice['name']) @@ -106,7 +197,7 @@ def slices(loginbase): Returns dict of PCU info of a given node. ''' def getpcu(nodename): - api = xmlrpclib.Server(auth.server, verbose=False) + api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True) anon = {'AuthMethod': "anonymous"} nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0] if nodeinfo['pcu_ids']: @@ -138,6 +229,7 @@ def getSiteNodes(loginbase, fields=None): print "getSiteNodes: %s" % exc return nodelist + def getPersons(filter=None, fields=None): api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True) persons = []