Removed unused import from planetlab.plxrn in slab files.
[sfa.git] / sfa / senslab / slabdriver.py
index 5277441..555cd0f 100644 (file)
@@ -18,7 +18,7 @@ from sfa.rspecs.version_manager import VersionManager
 from sfa.rspecs.rspec import RSpec
 
 from sfa.util.xrn import hrn_to_urn, urn_to_sliver_id, get_leaf
-from sfa.planetlab.plxrn import slicename_to_hrn, hrn_to_pl_slicename, \
+from sfa.planetlab.plxrn import slicename_to_hrn, \
                                         hostname_to_urn, \
                                         xrn_to_hostname
 
@@ -35,46 +35,6 @@ from sfa.senslab.slabaggregate import SlabAggregate
 from sfa.senslab.slabslices import SlabSlices
 
 
-def __process_walltime(duration=None):
-    """ Calculates the walltime in seconds from the duration in H:M:S
-        specified in the RSpec.
-        
-    """
-    if duration:
-        walltime = duration.split(":")
-        # Fixing the walltime by adding a few delays. First put the walltime 
-        # in seconds oarAdditionalDelay = 20; additional delay for 
-        # /bin/sleep command to
-        # take in account  prologue and epilogue scripts execution
-        # int walltimeAdditionalDelay = 120;  additional delay
-
-        desired_walltime = int(walltime[0])*3600 + int(walltime[1]) * 60 +\
-                                                            int(walltime[2])
-        total_walltime = desired_walltime + 140 #+2 min 20
-        sleep_walltime = desired_walltime + 20 #+20 sec
-        logger.debug("SLABDRIVER \t__process_walltime desired_walltime %s\
-                                total_walltime %s sleep_walltime %s  "\
-                                    %(desired_walltime, total_walltime, \
-                                                    sleep_walltime))
-        #Put the walltime back in str form
-        #First get the hours
-        walltime[0] = str(total_walltime / 3600)
-        total_walltime = total_walltime - 3600 * int(walltime[0])
-        #Get the remaining minutes
-        walltime[1] = str(total_walltime / 60)
-        total_walltime = total_walltime - 60 * int(walltime[1])
-        #Get the seconds
-        walltime[2] = str(total_walltime)
-        logger.debug("SLABDRIVER \t__process_walltime walltime %s "\
-                                        %(walltime))
-    else:
-        #automatically set 10min  +2 min 20
-        walltime[0] = '0'
-        walltime[1] = '12' 
-        walltime[2] = '20'
-        sleep_walltime = '620'
-        
-    return walltime, sleep_walltime
 
 
 
@@ -234,8 +194,8 @@ class SlabDriver(Driver):
             if not lease.get('lease_id'):
                 requested_lease['hostname'] = \
                             xrn_to_hostname(lease.get('component_id').strip())
-                requested_lease['t_from'] = lease.get('t_from')
-                requested_lease['t_until'] = lease.get('t_until')
+                requested_lease['start_time'] = lease.get('start_time')
+                requested_lease['duration'] = lease.get('duration')
             else:
                 kept_leases.append(int(lease['lease_id']))
             if requested_lease.get('hostname'):
@@ -306,7 +266,6 @@ class SlabDriver(Driver):
                 #return rspec 
     
         #panos: passing user-defined options
-        logger.debug("SLABDRIVER \tlist_resources rspec " )
         aggregate = SlabAggregate(self)
         origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
         options.update({'origin_hrn':origin_hrn})
@@ -461,18 +420,19 @@ class SlabDriver(Driver):
         hrn = sfa_record['hrn']
         record_id = sfa_record['record_id']
         if sfa_record_type == 'user':
-            #ldap_uid = hrn.split(".")[len(hrn.split(".")) -1]
-            #get user in ldap  
-            persons = self.GetPersons(sfa_record)
-            # only delete this person if he has site ids. if he doesnt, it probably means
-            # he was just removed from a site, not actually deleted
-            if persons and persons[0]['site_ids']:
-                #TODO : delete person in LDAP
+
+            #get user from senslab ldap  
+            person = self.GetPersons(sfa_record)
+            #No registering at a given site in Senslab.
+            #Once registered to the LDAP, all senslab sites are
+            #accesible.
+            if person :
+                #Mark account as disabled in ldap
                 self.DeletePerson(sfa_record)
         elif sfa_record_type == 'slice':
             if self.GetSlices(slice_filter = hrn, \
                                     slice_filter_type = 'slice_hrn'):
-                self.DeleteSlice(hrn)
+                self.DeleteSlice(sfa_record_type)
 
         #elif type == 'authority':
             #if self.GetSites(pointer):
@@ -489,8 +449,7 @@ class SlabDriver(Driver):
         existing_hrns_by_types = {}
         logger.debug("SLABDRIVER \tGetPeers auth = %s, peer_filter %s, \
                     return_field %s " %(auth , peer_filter, return_fields_list))
-        all_records = dbsession.query(RegRecord).\
-                                filter(RegRecord.type.like('%authority%')).all()
+        all_records = dbsession.query(RegRecord).filter(RegRecord.type.like('%authority%')).all()
         for record in all_records:
             existing_records[(record.hrn, record.type)] = record
             if record.type not in existing_hrns_by_types:
@@ -568,17 +527,20 @@ class SlabDriver(Driver):
     
 
     def DeleteJobs(self, job_id, slice_hrn):
-        if not job_id:
+        if not job_id or job_id is -1:
             return
         username  = slice_hrn.split(".")[-1].rstrip("_slice")
         reqdict = {}
         reqdict['method'] = "delete"
         reqdict['strval'] = str(job_id)
+       
         answer = self.oar.POSTRequestToOARRestAPI('DELETE_jobs_id', \
-                                                        reqdict,username)
-        logger.debug("SLABDRIVER \tDeleteJobs jobid  %s \r\n answer %s "  \
-                                                    %(job_id,answer))
+                                                    reqdict,username)
+        logger.debug("SLABDRIVER \tDeleteJobs jobid  %s \r\n answer %s username %s"  \
+                                                %(job_id,answer, username))
         return answer
+
+            
         
         ##TODO : Unused GetJobsId ? SA 05/07/12
     #def GetJobsId(self, job_id, username = None ):
@@ -787,12 +749,10 @@ class SlabDriver(Driver):
                                                 %(authorized_filter_types_list))
         if slice_filter_type in authorized_filter_types_list:
             if slice_filter_type == 'slice_hrn':
-                slicerec = slab_dbsession.query(SliceSenslab).\
-                                    filter_by(slice_hrn = slice_filter).first()
+                slicerec = slab_dbsession.query(SliceSenslab).filter_by(slice_hrn = slice_filter).first()
                                         
             if slice_filter_type == 'record_id_user':
-                slicerec = slab_dbsession.query(SliceSenslab).\
-                                filter_by(record_id_user = slice_filter).first()
+                slicerec = slab_dbsession.query(SliceSenslab).filter_by(record_id_user = slice_filter).first()
                 
             if slicerec:
                 #warning pylint OK
@@ -897,10 +857,11 @@ class SlabDriver(Driver):
             #instantion used in get_slivers ? 
             if not "instantiation" in slab_record:
                 slab_record["instantiation"] = "senslab-instantiated"
-            slab_record["hrn"] = hrn_to_pl_slicename(hrn)
+            #slab_record["hrn"] = hrn_to_pl_slicename(hrn)     
+            #Unused hrn_to_pl_slicename because Slab's hrn already in the appropriate form SA 23/07/12
+            slab_record["hrn"] = hrn 
             logger.debug("SLABDRIVER.PY sfa_fields_to_slab_fields \
-                        slab_record %s hrn_to_pl_slicename(hrn) hrn %s " \
-                                                %(slab_record['hrn'], hrn))
+                        slab_record %s  " %(slab_record['hrn']))
             if "url" in record:
                 slab_record["url"] = record["url"]
             if "description" in record:
@@ -999,7 +960,48 @@ class SlabDriver(Driver):
         custom_length = len(reqdict['resource'])- 2
         reqdict['resource'] = reqdict['resource'][0:custom_length] + \
                                             ")}/nodes=" + str(len(nodeid_list))
-        
+                                            
+        def __process_walltime(duration=None):
+            """ Calculates the walltime in seconds from the duration in H:M:S
+                specified in the RSpec.
+                
+            """
+            if duration:
+                walltime = duration.split(":")
+                # Fixing the walltime by adding a few delays. First put the walltime 
+                # in seconds oarAdditionalDelay = 20; additional delay for 
+                # /bin/sleep command to
+                # take in account  prologue and epilogue scripts execution
+                # int walltimeAdditionalDelay = 120;  additional delay
+        
+                desired_walltime = int(walltime[0])*3600 + int(walltime[1]) * 60 +\
+                                                                    int(walltime[2])
+                total_walltime = desired_walltime + 140 #+2 min 20
+                sleep_walltime = desired_walltime + 20 #+20 sec
+                logger.debug("SLABDRIVER \t__process_walltime desired_walltime %s\
+                                        total_walltime %s sleep_walltime %s  "\
+                                            %(desired_walltime, total_walltime, \
+                                                            sleep_walltime))
+                #Put the walltime back in str form
+                #First get the hours
+                walltime[0] = str(total_walltime / 3600)
+                total_walltime = total_walltime - 3600 * int(walltime[0])
+                #Get the remaining minutes
+                walltime[1] = str(total_walltime / 60)
+                total_walltime = total_walltime - 60 * int(walltime[1])
+                #Get the seconds
+                walltime[2] = str(total_walltime)
+                logger.debug("SLABDRIVER \t__process_walltime walltime %s "\
+                                                %(walltime))
+            else:
+                #automatically set 10min  +2 min 20
+                walltime[0] = '0'
+                walltime[1] = '12' 
+                walltime[2] = '20'
+                sleep_walltime = '620'
+                
+            return walltime, sleep_walltime
+                
         #if slot['duration']:
         walltime, sleep_walltime = __process_walltime(duration = \
                                                             slot['duration'])
@@ -1111,7 +1113,12 @@ class SlabDriver(Driver):
         return   
     
  
-
+    def GetLeaseGranularity(self):
+        """ Returns the granularity of Senslab testbed.
+        Defined in seconds. """
+        
+        grain = 60 
+        return grain
     
     def GetLeases(self, lease_filter_dict=None, return_fields_list=None):
         unfiltered_reservation_list = self.GetReservedNodes()
@@ -1124,9 +1131,11 @@ class SlabDriver(Driver):
 
             user = dbsession.query(RegUser).filter_by(email = \
                                                 ldap_info['mail'][0]).first()
-           
-            slice_info = slab_dbsession.query(SliceSenslab).\
-                            filter_by(record_id_user = user.record_id).first()
+            #Separated in case user not in database : record_id not defined SA 17/07//12
+            query_slice_info = slab_dbsession.query(SliceSenslab).filter_by(record_id_user = user.record_id)
+            if query_slice_info:
+                slice_info = query_slice_info.first()
+                
             #Put the slice_urn 
             resa['slice_id'] = hrn_to_urn(slice_info.slice_hrn, 'slice')
             resa['component_id_list'] = []
@@ -1202,8 +1211,7 @@ class SlabDriver(Driver):
                     #therefore fetches user and slice info again(one more loop)
                     #Will update PIs and researcher for the slice
                     recuser = dbsession.query(RegRecord).filter_by(record_id = \
-                                                recslice['record_id_user']).\
-                                                first()
+                                                recslice['record_id_user']).first()
                     recslice.update({'PI':[recuser.hrn],
                     'researcher': [recuser.hrn],
                     'name':record['hrn'], 
@@ -1395,7 +1403,7 @@ class SlabDriver(Driver):
         return
 
     
-
+    #TODO : Check rights to delete person 
     def DeletePerson(self, auth, person_record):
         """ Disable an existing account in senslab LDAP.
         Users and techs can only delete themselves. PIs can only 
@@ -1407,12 +1415,16 @@ class SlabDriver(Driver):
         """
         #Disable user account in senslab LDAP
         ret = self.ldap.LdapMarkUserAsDeleted(person_record)
-        logger.warning("SLABDRIVER DeletePerson EMPTY - DO NOTHING \r\n ")
+        logger.warning("SLABDRIVER DeletePerson %s " %(person_record))
         return ret
     
-    #TODO DeleteSlice 04/07/2012 SA
-    def DeleteSlice(self, auth, slice_id_or_name):
+    #TODO Check DeleteSlice, check rights 05/07/2012 SA
+    def DeleteSlice(self, auth, slice_record):
         """ Deletes the specified slice.
+         Senslab : Kill the job associated with the slice if there is one
+         using DeleteSliceFromNodes.
+         Updates the slice record in slab db to remove the slice nodes.
+         
          Users may only delete slices of which they are members. PIs may 
          delete any of the slices at their sites, or any slices of which 
          they are members. Admins may delete any slice.
@@ -1420,7 +1432,9 @@ class SlabDriver(Driver):
          FROM PLC API DOC
         
         """
-        logger.warning("SLABDRIVER DeleteSlice EMPTY - DO NOTHING \r\n ")
+        self.DeleteSliceFromNodes(slice_record)
+        self.db.update_job(slice_record['hrn'], job_id = -1, nodes = [])
+        logger.warning("SLABDRIVER DeleteSlice %s "%(slice_record))
         return
     
     #TODO AddPerson 04/07/2012 SA