review 'print' statements - make sure to use PLC.Debug.log that gets redirected to...
[plcapi.git] / PLC / Methods / GetBootMedium.py
index 53f68a1..90d65e1 100644 (file)
@@ -1,5 +1,3 @@
-# $Id$
-# $URL$
 import random
 import base64
 import os
@@ -16,6 +14,8 @@ from PLC.Interfaces import Interface, Interfaces
 from PLC.InterfaceTags import InterfaceTag, InterfaceTags
 from PLC.NodeTags import NodeTag, NodeTags
 
+from PLC.Debug import log
+
 from PLC.Accessors.Accessors_standard import *                  # import node accessors
 
 # could not define this in the class..
@@ -264,13 +264,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)
@@ -284,7 +284,7 @@ class GetBootMedium(Method):
     def cleantrash (self):
         for file in self.trash:
             if self.DEBUG:
-                print 'DEBUG -- preserving',file
+                print >> log, 'DEBUG -- preserving',file
             else:
                 os.unlink(file)
 
@@ -314,8 +314,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 (time.time()-os.path.getmtime(filename)) >= (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
@@ -361,7 +369,7 @@ class GetBootMedium(Method):
                                                                  log_file)
 
         if self.DEBUG:
-            print "The build command line is %s" % command
+            print >> log, "The build command line is %s" % command
 
         return command
 
@@ -386,7 +394,8 @@ class GetBootMedium(Method):
             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
         node = nodes[0]
 
-        if self.DEBUG: print "%s required on node %s. Node type is: %s" \
+        if self.DEBUG:
+            print >> log, "%s requested on node %s. Node type is: %s" \
                 % (action, node['node_id'], node['node_type'])
 
         # check the required action against the node type
@@ -567,7 +576,11 @@ class GetBootMedium(Method):
                     result = file(node_image).read()
                     self.trash.append(node_image)
                     self.cleantrash()
-                    return base64.b64encode(result)
+                    print >> log, "GetBootMedium - done with build.sh"
+                    encoded_result = base64.b64encode(result)
+                    print >> log, "GetBootMedium - done with base64 encoding - lengths=%s - %s"\
+                        %(len(result),len(encoded_result))
+                    return encoded_result
             except:
                 self.cleantrash()
                 raise