iotlab driver fixed for OneLab in Production
[sfa.git] / sfa / iotlab / iotlabslices.py
index 9449627..7206c92 100644 (file)
@@ -135,7 +135,6 @@ class IotlabSlices:
                 * self.driver.testbed_shell.GetLeaseGranularity())
             if job['duration'] < \
                     self.driver.testbed_shell.GetLeaseGranularity():
-                logger.debug("JORDAN removed too short lease !!!! %r" % (requested_jobs_dict[job['start_time']],))
                 del requested_jobs_dict[job['start_time']]
 
         #Requested jobs
@@ -214,6 +213,7 @@ class IotlabSlices:
                     job['hostname'],
                     sfa_slice, int(job['start_time']),
                     int(job['duration']))
+
                 # Removed by jordan
                 #if job_id is not None:
                 #    new_leases = self.driver.GetLeases(login=
@@ -232,7 +232,6 @@ class IotlabSlices:
                          % (sfa_slice, deleted_leases))
 
         if reschedule_jobs_dict:
-            logger.debug("JORDAN re-schedule jobs: %r" % (reschedule_jobs_dict,))
             for start_time in reschedule_jobs_dict:
                 job = reschedule_jobs_dict[start_time]
                 self.driver.AddLeases(
@@ -320,90 +319,55 @@ class IotlabSlices:
         if slicename.startswith("iotlab"):
             slices_list = self.driver.GetSlices(slice_filter=slicename,
                                                 slice_filter_type='slice_hrn')
-
-
+    
             if slices_list:
                 for sl in slices_list:
-
+    
                     logger.debug("IOTLABSLICES \t verify_slice slicename %s \
                                     slices_list %s sl %s \r slice_record %s"
                                  % (slicename, slices_list, sl, slice_record))
                     sfa_slice = sl
                     sfa_slice.update(slice_record)
 
-            else:
-                #Search for user in ldap based on email SA 14/11/12
-                ldap_user = self.driver.testbed_shell.ldap.LdapFindUser(\
-                                                        slice_record['user'])
-                logger.debug(" IOTLABSLICES \tverify_slice Oups \
-                            slice_record %s sfa_peer %s ldap_user %s"
-                            % (slice_record, sfa_peer, ldap_user))
-                #User already registered in ldap, meaning user should be in SFA db
-                #and hrn = sfa_auth+ uid
-                sfa_slice = {'hrn': slicename,
-                             'node_list': [],
-                             'authority': slice_record['authority'],
-                             'gid': slice_record['gid'],
-                             'slice_id': slice_record['record_id'],
-                             'urn': hrn_to_urn(slicename,'slice'),
-                             'reg-researchers': slice_record['reg-researchers'],
-                             #'peer_authority': str(sfa_peer)
-                             }
-
-                if ldap_user:
-                    hrn = self.driver.testbed_shell.root_auth + '.' \
-                                                    + ldap_user['uid']
-                    # Registry returns a user record from get_user_record 
-                    user = self.driver.get_user_record(hrn)
-
-                    logger.debug(" IOTLABSLICES \tverify_slice hrn %s USER %s"
-                                 % (hrn, user))
-
-                     # add the external slice to the local SFA iotlab DB
-                    if sfa_slice:
-                        self.driver.AddSlice(sfa_slice, user)
-
-                logger.debug("IOTLABSLICES \tverify_slice ADDSLICE OK")
         else:
+            #Search for user in ldap based on email SA 14/11/12
+            ldap_user = self.driver.testbed_shell.ldap.LdapFindUser(\
+                                                    slice_record['user'])
+            logger.debug(" IOTLABSLICES \tverify_slice Oups \
+                        slice_record %s sfa_peer %s ldap_user %s"
+                        % (slice_record, sfa_peer, ldap_user))
+            #User already registered in ldap, meaning user should be in SFA db
+            #and hrn = sfa_auth+ uid
             sfa_slice = {'hrn': slicename,
-             'node_list': [],
-             'authority': slice_record['authority'],
-             'gid': slice_record['gid'],
-             'urn': hrn_to_urn(slicename,'slice'),
-             #'slice_id': slice_record['record_id'],
-             'reg-researchers': slice_record['reg-researchers'],
-             #'peer_authority': str(sfa_peer)
-            }
-
-
-            # JORDAN
-            logger.debug("JORDAN ADDSLICE")
-            logger.debug("ADDSLICE user hrn = %s" % slice_record['user']['hrn'])
-            # XXX LOIC !!! searching in IOTLAB DB because has been added in verify_persons
-            user = self.driver.get_user_record(slice_record['user']['hrn'])
-
-            logger.debug("LOIC ADDSLICE Search in IOTLAB DB for user = %s" % user)
-            # XXX LOIC !!! not searching in LDAP because this has been done in verify_persons
-            #ldap_user = self.driver.testbed_shell.ldap.LdapFindUser(\
-            #                                        slice_record['user'])
-            #if ldap_user:
-            #    hrn = self.driver.testbed_shell.root_auth + '.' \
-            #                                    + ldap_user['uid']
-            #    user = self.driver.get_user_record(hrn)
-            #    logger.debug(" IOTLAB SLICES JORDAN user: %r %r " % (user, hrn))
+                         'node_list': [],
+                         'authority': slice_record['authority'],
+                         'gid': slice_record['gid'],
+                         #'slice_id': slice_record['record_id'],
+                         'reg-researchers': slice_record['reg-researchers'],
+                         'urn': hrn_to_urn(slicename,'slice'),
+                         #'peer_authority': str(sfa_peer)
+                         }
+
+            if ldap_user:
+#                hrn = self.driver.testbed_shell.root_auth + '.' \
+#                                                + ldap_user['uid']
+                for hrn in slice_record['reg-researchers']:
+                    user = self.driver.get_user_record(hrn)
+                    if user:
+                        break
 
-            #    logger.debug(" IOTLABSLICES \tverify_slice hrn %s USER %s"
-            #                 % (hrn, user))
+                logger.debug(" IOTLABSLICES \tverify_slice hrn %s USER %s"
+                             % (hrn, user))
 
-            # add the external slice to the local SFA iotlab DB
-            if sfa_slice:
-                self.driver.AddSlice(sfa_slice, user)
-                logger.debug("IOTLABSLICES \tverify_slice ADDSLICE OK")
+                 # add the external slice to the local SFA iotlab DB
+                if sfa_slice:
+                    self.driver.AddSlice(sfa_slice, user)
 
+            logger.debug("IOTLABSLICES \tverify_slice ADDSLICE OK")
         return sfa_slice
 
 
-    def verify_persons(self, slice_hrn, slice_record, users, options={}):
+    def verify_persons(self, slice_hrn, slice_record, users, options=None):
         """Ensures the users in users list exist and are enabled in LDAP. Adds
         person if needed (AddPerson).
 
@@ -429,6 +393,9 @@ class IotlabSlices:
 
 
         """
+        slice_user = slice_record['user']['hrn']
+
+        if options is None: options={}
         logger.debug("IOTLABSLICES \tverify_persons \tslice_hrn  %s  \
                     \t slice_record %s\r\n users %s \t  "
                      % (slice_hrn, slice_record, users))
@@ -437,7 +404,9 @@ class IotlabSlices:
         #users_dict : dict whose keys can either be the user's hrn or its id.
         #Values contains only id and hrn
         users_dict = {}
-
+        
+        # XXX LOIC !!! Fix: Only 1 user per slice in iotlab
+        users = [slice_record['user']]
         #First create dicts by hrn and id for each user in the user record list:
         for info in users:
             # if 'slice_record' in info:
@@ -449,12 +418,12 @@ class IotlabSlices:
                 users_by_email[info['email']] = info
                 users_dict[info['email']] = info
 
-        logger.debug("IOTLABSLICES.PY \t verify_person  \
-                        users_dict %s \r\n user_by_email %s \r\n  "
-                     % (users_dict, users_by_email))
+        #logger.debug("IOTLABSLICES.PY \t verify_person  \
+        #                users_dict %s \r\n user_by_email %s \r\n  "
+        #             % (users_dict, users_by_email))
 
         existing_user_ids = []
-        existing_user_emails = []
+        existing_users_by_email = dict()
         existing_users = []
         # Check if user is in Iotlab LDAP using its hrn.
         # Assuming Iotlab is centralised :  one LDAP for all sites,
@@ -468,18 +437,20 @@ class IotlabSlices:
             #Check user i in LDAP with GetPersons
             #Needed because what if the user has been deleted in LDAP but
             #is still in SFA?
+            # GetPersons -> LdapFindUser -> _process_ldap_info_for_one_user
+            # XXX LOIC Fix in _process_ldap_info_for_one_user not to update user with hrn=None
             existing_users = self.driver.testbed_shell.GetPersons(filter_user)
             logger.debug(" \r\n IOTLABSLICES.PY \tverify_person  filter_user %s\
                        existing_users %s  "
                         % (filter_user, existing_users))
-            # User is in iotlab LDAP
-            # XXX LOIC !!! user is existing in iotlab LDAP but coming from OneLab portal
-            if existing_users and slice_hrn.startswith("iotlab"):
+            #User is in iotlab LDAP
+            if existing_users:
                 for user in existing_users:
                     user['login'] = user['uid']
-                    users_dict[user['email']].update(user)
-                    existing_user_emails.append(
-                        users_dict[user['email']]['email'])
+                    # XXX LOIC Fix we already have all informations comming from Allocate
+                    #users_dict[user['email']].update(user)
+                    existing_users_by_email[user['email']] = user
+                logger.debug("User is in iotlab LDAP slice_record[user] = %s" % slice_user)
 
             # User from another known trusted federated site. Check
             # if a iotlab account matching the email has already been created.
@@ -487,45 +458,22 @@ class IotlabSlices:
                 req = 'mail='
                 if isinstance(users, list):
                     req += users[0]['email']
-                    user = users[0]
                 else:
                     req += users['email']
-                    user = users
                 ldap_reslt = self.driver.testbed_shell.ldap.LdapSearch(req)
-
+                logger.debug("LdapSearch slice_record[user] = %s" % slice_user)
                 if ldap_reslt:
                     logger.debug(" IOTLABSLICES.PY \tverify_person users \
                                 USER already in Iotlab \t ldap_reslt %s \
                                 " % (ldap_reslt))
-                    
-                    #existing_users.append(ldap_reslt[1])
-                    # XXX LOIC !!! Not sure why we use to take the element 1
-                    if len(ldap_reslt)>0:
-                        ldap_reslt = ldap_reslt[0]
-                        logger.debug(ldap_reslt)
-                        if len(ldap_reslt)>1:
-                            ldap_reslt = ldap_reslt[1]
-                            logger.debug("LOIC - iotlabslices.py - ldap_reslt = %s" % (ldap_reslt))
-                            existing_users.append(ldap_reslt)
-                            existing_user_emails.append(ldap_reslt['mail'][0])
-                    
-                    # XXX LOIC !!! This login is required 
-                    # sfa/iotlab/iotlabdriver.py", line 523, in AddLeases 
-                    if 'uid' in ldap_reslt:
-                        # meaning that the Person was found in LDAP
-                        slice_record['login'] = ldap_reslt['uid'][0]
-
-                    # XXX LOIC !!! Add the user to IOTLAB DB Registry???
-                    #if 'keys' in user:
-                    #    user['pkey'] = user['keys'][0]
-
-                    #ret = self.driver.AddPerson(user,add_to_ldap=False)
-                    #logger.debug("LOIC verify_persons AddPerson ret = %s" % ret)
+                    existing_users.append(ldap_reslt[1])
+                    logger.debug("ldap_reslt slice_record[user] = %s" % slice_user)
                 else:
                     #User not existing in LDAP
                     logger.debug("IOTLABSLICES.PY \tverify_person users \
                                 not in ldap ...NEW ACCOUNT NEEDED %s \r\n \t \
                                 ldap_reslt %s " % (users, ldap_reslt))
+
         requested_user_emails = users_by_email.keys()
         # requested_user_hrns = \
         #     [users_by_email[user]['hrn'] for user in users_by_email]
@@ -542,107 +490,128 @@ class IotlabSlices:
         # except KeyError:
         #     pass
 
-        # users to be added, removed or updated
-        #One user in one iotlab slice : there should be no need
-        #to remove/ add any user from/to a slice.
-        #However a user from SFA which is not registered in Iotlab yet
-        #should be added to the LDAP.
-        logger.debug("LOIC - iotlabslice.py - requested_user_emails = %r" % requested_user_emails)
-        logger.debug("LOIC - iotlabslice.py - existing_user_emails = %r" % existing_user_emails)
-
-        added_user_emails = set(requested_user_emails).\
-                                        difference(set(existing_user_emails))
-        logger.debug("LOIC - iotlabslice.py - added_user_emails = %r" % added_user_emails)
-        logger.debug("LOIC - iotlabslice.py - existing_user_emails = %r" % existing_user_emails)
-        #self.verify_keys(existing_slice_users, updated_users_list, \
-                                                            #peer, append)
-
-        # XXX JORDAN the uid of the user is put in slice_record['login']
-
-        added_persons = []
-        # add new users
-        #requested_user_email is in existing_user_emails
-        if len(added_user_emails) == 0:
-#            slice_record['login'] = users_dict[requested_user_emails[0]]['uid']
-            logger.debug(" IOTLABSLICES  \tverify_person QUICK DIRTY %s"
-                         % (slice_record))
-
-            # XXX JORDAN uid == 'register'
-#            new_hrn = slice_record['user']['hrn']
-#            new_user = self.driver.get_user_record(new_hrn)
-#            if not new_user:
-#                # XXX HERE WE SHOULD CREATE A SFA USER !!!!!!
-#                added_user = users_dict[requested_user_emails[0]]
-#                person = {}
-#                person['peer_person_id'] = None
-#                k_list = ['first_name', 'last_name', 'person_id']
-#                for k in k_list:
-#                    if k in added_user:
-#                        person[k] = added_user[k]
-#                # bug user without key
-#                if added_user['keys']:
-#                    person['pkey'] = added_user['keys'][0]
-#                person['mail'] = added_user['email']
-#                person['email'] = added_user['email']
-#                person['key_ids'] = added_user.get('key_ids', [])
-#                # LOIC !!! il faudrait transformer onelab.upmc.XXX en iotlab.XXX
-#                if new_hrn.startswith("iotlab"):
-#                    person['hrn'] = new_hrn                 
-#                else:
-#                    hrn_hierarchy = new_hrn.split(".")
-#                    person['hrn'] = "iotlab." + hrn_hierarchy[-1]
-#
-#                ret = self.driver.AddPerson(person, add_to_ldap=False)
-#                logger.debug("AddPerson return = %r type = %s" % (ret,type(ret)))
-#                # LOIC !!! XXX Dans un cas ça retourne un dict du LDAP dans l'autre cas pas de LDAP donc ça ne retourne rien
-#                if ret is None:
-#                    person['uid'] = slice_record['login']
-#                else:
-#                    if 'uid' in ret:
-#                        # meaning bool is True and the AddPerson was successful
-#                        person['uid'] = ret['uid']
-#                        slice_record['login'] = person['uid']
-
-        # XXX JORDAN i have no added_user_emails
-        for added_user_email in added_user_emails:
-            added_user = users_dict[added_user_email]
-            logger.debug(" IOTLABSLICES \r\n \r\n  \t  verify_person \
-                         added_user %s" % (added_user))
-            person = {}
-            person['peer_person_id'] = None
-            k_list = ['first_name', 'last_name', 'person_id']
-            for k in k_list:
-                if k in added_user:
-                    person[k] = added_user[k]
-            # bug user without key
-            if added_user['keys']:
-                person['pkey'] = added_user['keys'][0]
-            person['mail'] = added_user['email']
-            person['email'] = added_user['email']
-            person['key_ids'] = added_user.get('key_ids', [])
-
-            ret = self.driver.AddPerson(person)
-            if 'uid' in ret:
-                # meaning bool is True and the AddPerson was successful
-                person['uid'] = ret['uid']
-                slice_record['login'] = person['uid']
+        # The function returns a list of added persons (to the LDAP ?)
+        added_persons = list()
+
+        # We go though each requested user and make sure it exists both in the
+        # LDAP and in the local DB
+        for user_email in requested_user_emails:
+            user = users_by_email[user_email]
+
+            person = {
+                'peer_person_id': None,
+                'mail'      : user['email'],
+                'email'     : user['email'],
+                'key_ids'   : user.get('key_ids', []),
+                'hrn'       : users_by_email[user['email']]['hrn'],
+            }
+            if 'first_name' in user:
+                person['first_name'] = user['first_name']
+            if 'last_name' in user:
+                person['last_name'] = user['last_name']
+            if 'person_id' in user:
+                person['person_id'] = user['person_id']
+            if user['keys']:
+                # XXX Only one key is kept for IoTLAB
+                person['pkey'] = user['keys'][0]
+
+            # LDAP 
+            if users_by_email not in existing_users_by_email.keys():
+                ret = self.driver.AddPerson(person)
+                if 'uid' in ret:
+                    person['uid'] = ret['uid']
+                    added_persons.append(person)
+                else:
+                    logger.debug(" IOTLABSLICES ret message %s" %(ret))
             else:
-                # error message in ret
-                logger.debug(" IOTLABSLICES ret message %s" %(ret))
+                person['uid'] = existing_users_by_email[user['email']]['uid']
 
-            logger.debug(" IOTLABSLICES \r\n \r\n  \t THE SECOND verify_person\
-                           person %s" % (person))
-            #Update slice_Record with the id now known to LDAP
+            # Local DB
+            self.driver.add_person_to_db(person)
 
+            
+        # Set the login in the slice_record XXX
+        slice_record['login'] = existing_users[0]['uid']
 
-            added_persons.append(person)
         return added_persons
 
-
-    def verify_keys(self, persons, users, peer, options={}):
+#DEPRECATED|        # users to be added, removed or updated
+#DEPRECATED|        #One user in one iotlab slice : there should be no need
+#DEPRECATED|        #to remove/ add any user from/to a slice.
+#DEPRECATED|        #However a user from SFA which is not registered in Iotlab yet
+#DEPRECATED|        #should be added to the LDAP.
+#DEPRECATED|        added_user_emails = set(requested_user_emails).\
+#DEPRECATED|                                        difference(set(existing_user_emails))
+#DEPRECATED|
+#DEPRECATED|
+#DEPRECATED|        #self.verify_keys(existing_slice_users, updated_users_list, \
+#DEPRECATED|                                                            #peer, append)
+#DEPRECATED|
+#DEPRECATED|        # XXX JORDAN the uid of the user is put in slice_record['login']
+#DEPRECATED|        added_persons = []
+#DEPRECATED|        # add new users
+#DEPRECATED|        #requested_user_email is in existing_user_emails
+#DEPRECATED|        if len(added_user_emails) == 0:
+#DEPRECATED|            slice_record['login'] = existing_users[0]['uid']
+#DEPRECATED|            #slice_record['login'] = users_dict[requested_user_emails[0]]['uid']
+#DEPRECATED|            logger.debug(" IOTLABSLICES  \tverify_person QUICK DIRTY %s"
+#DEPRECATED|                         % (slice_record))
+#DEPRECATED|            # XXX JORDAN uid == 'register'
+#DEPRECATED|        logger.debug("JORDAN USERS BY EMAIL: %r" % users_by_email)
+#DEPRECATED|
+#DEPRECATED|        # XXX JORDAN i have no added_user_emails
+#DEPRECATED|        logger.debug("JORDAN: added_user_emails: %r" % added_user_emails)
+#DEPRECATED|        for added_user_email in added_user_emails:
+#DEPRECATED|            added_user = users_dict[added_user_email]
+#DEPRECATED|            logger.debug(" IOTLABSLICES \r\n \r\n  \t  verify_person \
+#DEPRECATED|                         added_user %s" % (added_user))
+#DEPRECATED|            person = {}
+#DEPRECATED|            person['peer_person_id'] = None
+#DEPRECATED|            k_list = ['first_name', 'last_name', 'person_id']
+#DEPRECATED|            for k in k_list:
+#DEPRECATED|                if k in added_user:
+#DEPRECATED|                    person[k] = added_user[k]
+#DEPRECATED|            # bug user without key
+#DEPRECATED|            if added_user['keys']:
+#DEPRECATED|                person['pkey'] = added_user['keys'][0]
+#DEPRECATED|            person['mail'] = added_user['email']
+#DEPRECATED|            person['email'] = added_user['email']
+#DEPRECATED|            person['key_ids'] = added_user.get('key_ids', [])
+#DEPRECATED|
+#DEPRECATED|            # JORDAN
+#DEPRECATED|            # This is the only call to AddPerson. We need to be sure to provide
+#DEPRECATED|            # the right hrn, by default it used to be done in the function like
+#DEPRECATED|            # this:
+#DEPRECATED|            # person['hrn'] = self.testbed_shell.root_auth + '.' + ret['uid']
+#DEPRECATED|            person['hrn'] = users_by_email[added_user['email']]['hrn']
+#DEPRECATED|
+#DEPRECATED|            # This only deals with the LDAP (now)
+#DEPRECATED|            ret = self.driver.AddPerson(person)
+#DEPRECATED|            # This will check if we have a record in the local DB and add it if necessary
+#DEPRECATED|            self.__add_person_to_db(person)
+#DEPRECATED|
+#DEPRECATED|            if 'uid' in ret:
+#DEPRECATED|                # meaning bool is True and the AddPerson was successful
+#DEPRECATED|                person['uid'] = ret['uid']
+#DEPRECATED|                slice_record['login'] = person['uid']
+#DEPRECATED|            else:
+#DEPRECATED|                # error message in ret
+#DEPRECATED|                logger.debug(" IOTLABSLICES ret message %s" %(ret))
+#DEPRECATED|
+#DEPRECATED|            logger.debug(" IOTLABSLICES \r\n \r\n  \t THE SECOND verify_person\
+#DEPRECATED|                           person %s" % (person))
+#DEPRECATED|            #Update slice_Record with the id now known to LDAP
+#DEPRECATED|
+#DEPRECATED|
+#DEPRECATED|            added_persons.append(person)
+#DEPRECATED|        return added_persons
+
+
+    def verify_keys(self, persons, users, peer, options=None):
         """
         .. warning:: unused
         """
+        if options is None: options={}
         # existing keys
         key_ids = []
         for person in persons: