iotlab driver: wip fixing Timur's bug when a user already has an account in LDAP...
[sfa.git] / sfa / iotlab / iotlabslices.py
index f5ebfa7..710f613 100644 (file)
@@ -2,7 +2,7 @@
 This file defines the IotlabSlices class by which all the slice checkings
 upon lease creation are done.
 """
-from sfa.util.xrn import get_authority, urn_to_hrn
+from sfa.util.xrn import get_authority, urn_to_hrn, hrn_to_urn
 from sfa.util.sfalogging import logger
 
 MAXINT = 2L**31-1
@@ -133,7 +133,8 @@ class IotlabSlices:
             job['duration'] = \
                 str(int(job['duration']) \
                 * self.driver.testbed_shell.GetLeaseGranularity())
-            if job['duration'] < self.driver.testbed_shell.GetLeaseGranularity():
+            if job['duration'] < \
+                    self.driver.testbed_shell.GetLeaseGranularity():
                 del requested_jobs_dict[job['start_time']]
 
         #Requested jobs
@@ -164,6 +165,7 @@ class IotlabSlices:
                          "%( start_time))
             if start_time in current_nodes_reserved_by_start_time:
 
+                # JORDAN : if we request the same nodes: do nothing
                 if requested_nodes_by_start_time[start_time] == \
                     current_nodes_reserved_by_start_time[start_time]:
                     continue
@@ -211,16 +213,20 @@ class IotlabSlices:
                     job['hostname'],
                     sfa_slice, int(job['start_time']),
                     int(job['duration']))
-                if job_id is not None:
-                    new_leases = self.driver.GetLeases(login=
-                        sfa_slice['login'])
-                    for new_lease in new_leases:
-                        leases.append(new_lease)
+
+                # Removed by jordan
+                #if job_id is not None:
+                #    new_leases = self.driver.GetLeases(login=
+                #        sfa_slice['login'])
+                #    for new_lease in new_leases:
+                #        leases.append(new_lease)
 
         #Deleted leases are the ones with lease id not declared in the Rspec
         if deleted_leases:
             self.driver.testbed_shell.DeleteLeases(deleted_leases,
-                                                sfa_slice['user']['uid'])
+                                                sfa_slice['login'])
+            #self.driver.testbed_shell.DeleteLeases(deleted_leases,
+            #                                    sfa_slice['user']['uid'])
             logger.debug("IOTLABSLICES \
                           verify_slice_leases slice %s deleted_leases %s"
                          % (sfa_slice, deleted_leases))
@@ -232,7 +238,10 @@ class IotlabSlices:
                     job['hostname'],
                     sfa_slice, int(job['start_time']),
                     int(job['duration']))
-        return leases
+
+        # Added by Jordan: until we find a better solution, always update the list of leases
+        return self.driver.GetLeases(login= sfa_slice['login'])
+        #return leases
 
     def verify_slice_nodes(self, sfa_slice, requested_slivers, peer):
         """Check for wanted and unwanted nodes in the slice.
@@ -304,20 +313,21 @@ class IotlabSlices:
         """
 
         slicename = slice_hrn
-        # check if slice belongs to Iotlab
-        slices_list = self.driver.GetSlices(slice_filter=slicename,
-                                            slice_filter_type='slice_hrn')
-
         sfa_slice = None
 
-        if slices_list:
-            for sl in slices_list:
-
-                logger.debug("SLABSLICE \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)
+        # check if slice belongs to Iotlab
+        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
@@ -332,13 +342,15 @@ class IotlabSlices:
                          'node_list': [],
                          'authority': slice_record['authority'],
                          'gid': slice_record['gid'],
-                         'slice_id': slice_record['record_id'],
+                         #'slice_id': slice_record['record_id'],
                          'reg-researchers': slice_record['reg-researchers'],
-                         'peer_authority': str(sfa_peer)
+                         'urn': hrn_to_urn(slicename,'slice'),
+                         #'peer_authority': str(sfa_peer)
                          }
 
             if ldap_user:
-                hrn = self.driver.testbed_shell.root_auth + '.' + ldap_user['uid']
+                hrn = self.driver.testbed_shell.root_auth + '.' \
+                                                + ldap_user['uid']
                 user = self.driver.get_user_record(hrn)
 
                 logger.debug(" IOTLABSLICES \tverify_slice hrn %s USER %s"
@@ -352,9 +364,9 @@ class IotlabSlices:
         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.
+        person if needed (AddPerson).
 
         Checking that a user exist is based on the user's email. If the user is
         still not found in the LDAP, it means that the user comes from another
@@ -366,8 +378,8 @@ class IotlabSlices:
         :param slice_record: record of the slice_hrn
         :param users: users is a record list. Records can either be
             local records or users records from known and trusted federated
-            sites.If the user is from another site that iotlab doesn't trust yet,
-            then Resolve will raise an error before getting to create_sliver.
+            sites.If the user is from another site that iotlab doesn't trust
+            yet, then Resolve will raise an error before getting to allocate.
 
         :type slice_hrn: string
         :type slice_record: string
@@ -378,8 +390,9 @@ class IotlabSlices:
 
 
         """
-        #TODO SA 21/08/12 verify_persons Needs review
+        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))
@@ -388,7 +401,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:
@@ -400,9 +415,9 @@ class IotlabSlices:
                 users_by_email[info['email']] = info
                 users_dict[info['email']] = info
 
-        logger.debug("SLABSLICE.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 = []
@@ -419,6 +434,8 @@ 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  "
@@ -427,10 +444,11 @@ class IotlabSlices:
             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_user_emails.append(
+                    #    users_dict[user['email']]['email'])
+                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.
@@ -441,34 +459,34 @@ class IotlabSlices:
                 else:
                     req += users['email']
                 ldap_reslt = self.driver.testbed_shell.ldap.LdapSearch(req)
-
+                logger.debug("LdapSearch slice_record[user] = %s" % slice_user)
                 if ldap_reslt:
-                    logger.debug(" SLABSLICE.PY \tverify_person users \
+                    logger.debug(" IOTLABSLICES.PY \tverify_person users \
                                 USER already in Iotlab \t ldap_reslt %s \
                                 " % (ldap_reslt))
                     existing_users.append(ldap_reslt[1])
-
+                    logger.debug("ldap_reslt slice_record[user] = %s" % slice_user)
                 else:
                     #User not existing in LDAP
-                    logger.debug("SLABSLICE.PY \tverify_person users \
+                    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]
-        logger.debug("SLABSLICE.PY \tverify_person  \
-                       users_by_email  %s " % (users_by_email))
+        requested_user_hrns = \
+            [users_by_email[user]['hrn'] for user in users_by_email]
+        # logger.debug("IOTLABSLICES.PY \tverify_person  \
+                       users_by_email  %s " % (users_by_email))
 
-        #Check that the user of the slice in the slice record
-        #matches one of the existing users
-        try:
-            if slice_record['reg-researchers'][0] in requested_user_hrns:
-                logger.debug(" SLABSLICE  \tverify_person ['PI']\
-                                slice_record %s" % (slice_record))
+        # #Check that the user of the slice in the slice record
+        # #matches one of the existing users
+        try:
+            if slice_record['reg-researchers'][0] in requested_user_hrns:
+        #         logger.debug(" IOTLABSLICES  \tverify_person ['PI']\
+                                slice_record %s" % (slice_record))
 
-        except KeyError:
-            pass
+        except KeyError:
+            pass
 
         # users to be added, removed or updated
         #One user in one iotlab slice : there should be no need
@@ -482,14 +500,18 @@ class IotlabSlices:
         #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(" SLABSLICE  \tverify_person QUICK DIRTY %s"
+            slice_record['login'] = existing_users[0]['uid']
+            #slice_record['login'] = users_dict[requested_user_emails[0]]['uid']
+            logger.debug(" IOTLABSLICES  \tverify_person QUICK DIRTY %s"
                          % (slice_record))
+            # XXX JORDAN uid == 'register'
 
+        # 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 \
@@ -500,13 +522,14 @@ class IotlabSlices:
             for k in k_list:
                 if k in added_user:
                     person[k] = added_user[k]
-
-            person['pkey'] = added_user['keys'][0]
+            # 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.testbed_shell.AddPerson(person)
+            ret = self.driver.AddPerson(person)
             if 'uid' in ret:
                 # meaning bool is True and the AddPerson was successful
                 person['uid'] = ret['uid']
@@ -515,7 +538,7 @@ class IotlabSlices:
                 # error message in ret
                 logger.debug(" IOTLABSLICES ret message %s" %(ret))
 
-            logger.debug(" SLABSLICE \r\n \r\n  \t THE SECOND verify_person\
+            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
 
@@ -524,10 +547,11 @@ class IotlabSlices:
         return added_persons
 
 
-    def verify_keys(self, persons, users, peer, options={}):
+    def verify_keys(self, persons, users, peer, options=None):
         """
         .. warning:: unused
         """
+        if options is None: options={}
         # existing keys
         key_ids = []
         for person in persons:
@@ -570,12 +594,6 @@ class IotlabSlices:
                                             #key['key_id'], peer['shortname'], \
                                             #remote_key_id)
 
-                    #finally:
-                        #if peer:
-                            #self.driver.testbed_shell.BindObjectToPeer('person', \
-                                    #person['person_id'], peer['shortname'], \
-                                    #user['person_id'])
-
         # remove old keys (only if we are not appending)
         append = options.get('append', True)
         if append is False: