changes for 3.0
[monitor.git] / plc.py
diff --git a/plc.py b/plc.py
index 0970eaa..5be15c8 100644 (file)
--- a/plc.py
+++ b/plc.py
@@ -13,8 +13,7 @@ import logging
 import time
 import traceback
 try:
-       from config import config
-       config = config()
+       import config
        debug = config.debug
 except:
        debug = False
@@ -31,18 +30,23 @@ XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
 #       configured, use them, with their auth definitions.
 auth = Auth()
 try:
-       import monitorconfig
-       auth.auth = monitorconfig.API_AUTH
-       auth.server = monitorconfig.API_SERVER
+       from monitor import config
+       auth.auth = {'Username' : config.API_AUTH_USER,
+                    'AuthMethod' : 'password',
+                                'AuthString' : config.API_AUTH_PASSWORD}
+       auth.server = config.API_SERVER
 except:
        try:
                import auth
                auth.server = auth.plc
        except:
+               auth = Auth()
                auth.server = XMLRPC_SERVER
 
 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 
+global_error_count = 0
+
 class PLC:
        def __init__(self, auth, url):
                self.auth = auth
@@ -54,7 +58,17 @@ class PLC:
                if method is None:
                        raise AssertionError("method does not exist")
 
-               return lambda *params : method(self.auth, *params)
+               try:
+                       return lambda *params : method(self.auth, *params)
+               except ProtocolError:
+                       traceback.print_exc()
+                       global_error_count += 1
+                       if global_error_count >= 10:
+                               print "maximum error count exceeded; exiting..."
+                               sys.exit(1)
+                       else:
+                               print "%s errors have occurred" % global_error_count
+                       raise Exception("ProtocolError continuing")
 
        def __repr__(self):
                return self.api.__repr__()
@@ -63,7 +77,51 @@ def getAPI(url):
        return xmlrpclib.Server(url, verbose=False, allow_none=True)
 
 def getAuthAPI():
-       return PLC(monitorconfig.API_AUTH, monitorconfig.API_SERVER)
+       return PLC(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
@@ -82,19 +140,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'])
@@ -104,7 +163,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']:
@@ -136,6 +195,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 = []
@@ -172,14 +232,14 @@ def getSiteNodes2(loginbase):
 
 def getNodeNetworks(filter=None):
        api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
-       nodenetworks = api.GetNodeNetworks(auth.auth, filter, None)
+       nodenetworks = api.GetInterfaces(auth.auth, filter, None)
        return nodenetworks
 
 def getNodes(filter=None, fields=None):
        api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
        nodes = api.GetNodes(auth.auth, filter, fields) 
                        #['boot_state', 'hostname', 
-                       #'site_id', 'date_created', 'node_id', 'version', 'nodenetwork_ids',
+                       #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
                        #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
        return nodes