trashing existing tmp file if older than 5 minutes
[plcapi.git] / PLC / Methods / GetBootMedium.py
index 848796d..f050840 100644 (file)
@@ -1,5 +1,3 @@
-# $Id$
-# $URL$
 import random
 import base64
 import os
@@ -199,6 +197,7 @@ class GetBootMedium(Method):
         # renew the key and save it on the database
         if renew_key:
             node['key'] = compute_key()
+            node.update_last_download(commit=False)
             node.sync()
 
         # Generate node configuration file suitable for BootCD
@@ -263,13 +262,13 @@ class GetBootMedium(Method):
         node_id=node['node_id']
 
         # no support for deployment-based BootCD's, use kvariants instead
-        node_pldistro = GetNodePldistro (self.api).call(auth, node_id)
+        node_pldistro = GetNodePldistro (self.api,self.caller).call(auth, node_id)
         if node_pldistro: pldistro = node_pldistro
 
-        node_fcdistro = GetNodeFcdistro (self.api).call(auth, node_id)
+        node_fcdistro = GetNodeFcdistro (self.api,self.caller).call(auth, node_id)
         if node_fcdistro: fcdistro = node_fcdistro
 
-        node_arch = GetNodeArch (self.api).call(auth,node_id)
+        node_arch = GetNodeArch (self.api,self.caller).call(auth,node_id)
         if node_arch: arch = node_arch
 
         return (pldistro,fcdistro,arch)
@@ -313,8 +312,16 @@ class GetBootMedium(Method):
                     raise PLCInvalidArgument, "File %s not under %s"%(filename,self.WORKDIR)
 
             ### output should not exist (concurrent runs ..)
+            # numerous reports of issues with this policy
+            # looks like people sometime suspend/cancel their download
+            # and this leads to the old file sitting in there forever
+            # so, if the file is older than 5 minutes, we just trash
+            grace=5
+            if os.path.exists(filename) and os.path.getmtime(filename)-time.time() >= (grace*60):
+                os.unlink(filename)
             if os.path.exists(filename):
-                raise PLCInvalidArgument, "Resulting file %s already exists"%filename
+                raise PLCInvalidArgument, "Resulting file %s already exists - please try again in %d minutes"%\
+                    (filename,grace)
 
             ### we can now safely create the file,
             ### either we are admin or under a controlled location