tear down omf aspects as this is not needed any more with OMFv6
[plcapi.git] / PLC / Methods / GetSlivers.py
index 25ea00a..9e3b73d 100644 (file)
@@ -19,14 +19,10 @@ from PLC.InitScripts import InitScript, InitScripts
 from PLC.Leases import Lease, Leases
 from PLC.Timestamp import Duration
 from PLC.Methods.GetSliceFamily import GetSliceFamily
+from PLC.PersonTags import PersonTag,PersonTags
 
 from PLC.Accessors.Accessors_standard import *
 
-# Caching
-import os
-os.environ['DJANGO_SETTINGS_MODULE']='plc_django_settings'
-from cache_utils.decorators import cached
-
 # XXX used to check if slice expiration time is sane
 MAXINT =  2L**31-1
 
@@ -95,11 +91,12 @@ def get_slivers(api, caller, auth, slice_filter, node = None):
                 # Do not set any nodegroup slice attributes for
                 # which there is at least one sliver attribute
                 # already set.
-                if slice_tag not in slice_tags:
+                if slice_tag['tagname'] not in sliver_attributes:
+                    sliver_attributes.append(slice_tag['tagname'])
                     attributes.append({'tagname': slice_tag['tagname'],
-                                   'value': slice_tag['value']})
+                                       'value': slice_tag['value']})
 
-        for slice_tag in [ a for a in slice_tags if a['node_id'] is None ]:
+        for slice_tag in [ a for a in slice_tags if a['node_id'] is None and a['nodegroup_id'] is None ]:
             # Do not set any global slice attributes for
             # which there is at least one sliver attribute
             # already set.
@@ -210,20 +207,9 @@ class GetSlivers(Method):
     }
 
     def call(self, auth, node_id_or_hostname = None):
-        try:
-            cache_opt = self.api.config.PLC_GETSLIVERS_CACHE
-        with AttributeError:
-            cache_opt = False
-
-        if (cache_opt):
-            return self.cacheable_call(auth, node_id_or_hostname)
-        else:
-            return self.raw_call(auth, node_id_or_hostname)
-
-    @cached(7200)
-    def cacheable_call(self, auth, node_id_or_hostname):
         return self.raw_call(auth, node_id_or_hostname)
 
+
     def raw_call(self, auth, node_id_or_hostname):
         timestamp = int(time.time())
 
@@ -304,6 +290,21 @@ class GetSlivers(Method):
         # reduce ( reduce_flatten_list, [ [1] , [2,3] ], []) => [ 1,2,3 ]
         def reduce_flatten_list (x,y): return x+y
 
+        # root users are users marked with the tag 'isrootonsite'. Hack for Mlab and other sites in which admins participate in diagnosing problems.
+        def get_site_root_user_keys(api,site_id_or_name):
+           site = Sites (api,site_id_or_name,['person_ids'])[0]
+           all_site_persons = site['person_ids']
+           all_site_person_tags = PersonTags(self.api,{'person_id':all_site_persons,'tagname':'isrootonsite'},['value','person_id'])
+           site_root_person_tags = filter(lambda r:r['value']=='true',all_site_person_tags)
+           site_root_person_ids = map(lambda r:r['person_id'],site_root_person_tags)
+           key_ids = reduce (reduce_flatten_list,
+                             [ p['key_ids'] for p in \
+                                   Persons(api,{ 'person_id':site_root_person_ids,
+                                                 'enabled':True, '|role_ids' : [20, 40] },
+                                           ['key_ids']) ],
+                             [])
+           return [ key['key'] for key in Keys (api, key_ids) if key['key_type']=='ssh']
+
         # power users are pis and techs
         def get_site_power_user_keys(api,site_id_or_name):
             site = Sites (api,site_id_or_name,['person_ids'])[0]
@@ -328,8 +329,11 @@ class GetSlivers(Method):
         personsitekeys=get_site_power_user_keys(self.api,node['site_id'])
         accounts.append({'name':'site_admin','keys':personsitekeys})
 
-        # 'root' account setup on nodes from all 'admin' users
+        # 'root' account setup on nodes from all 'admin' users and ones marked with 'isrootonsite' for this site
+        siterootkeys=get_site_root_user_keys(self.api,node['site_id'])
         personsitekeys=get_all_admin_keys(self.api)
+        personsitekeys.extend(siterootkeys)
+
         accounts.append({'name':'root','keys':personsitekeys})
 
         hrn = GetNodeHrn(self.api,self.caller).call(auth,node['node_id'])
@@ -337,13 +341,10 @@ class GetSlivers(Method):
         # XMPP config for omf federation
         try:
             if not self.api.config.PLC_OMF_ENABLED:
-                raise Exception,"OMF disabled"
-            xmpp={'server':self.api.config.PLC_OMF_XMPP_SERVER,
-                  'user':self.api.config.PLC_OMF_XMPP_USER,
-                  'password':self.api.config.PLC_OMF_XMPP_PASSWORD,
-                  }
+                raise Exception,"OMF not enabled"
+            xmpp={'server':self.api.config.PLC_OMF_XMPP_SERVER}
         except:
-            xmpp={'server':None,'user':None,'password':None}
+            xmpp={'server':None}
 
         node.update_last_contact()