- fix previously introduced typo
[plcapi.git] / PLC / Methods / GetBootMedium.py
index 0dc06d6..d143196 100644 (file)
@@ -93,11 +93,15 @@ class GetBootMedium(Method):
         of the result file; it is the caller's responsability to remove 
         this file after use.
 
-        Security:
+    Options: an optional array of keywords. Currently supported are
+        - 'serial'
+        - 'cramfs'
+
+    Security:
         When the user's role is not admin, the provided directory *must* be under
         the %d area
 
-        Housekeeping: 
+   Housekeeping: 
         Whenever needed, the method stores intermediate files in a
         private area, typically not located under the web server's
         accessible area, and are cleaned up by the method.
@@ -112,6 +116,7 @@ class GetBootMedium(Method):
               Node.fields['hostname']),
         Parameter (str, "Action mode, expected in " + "|".join(boot_medium_actions)),
         Parameter (str, "Empty string for verbatim result, resulting file full path otherwise"),
+        Parameter ([str], "Options"),
         ]
 
     returns = Parameter(str, "Node boot medium, either inlined, or filename, depending to the filename parameter")
@@ -230,19 +235,38 @@ class GetBootMedium(Method):
         if not self.DEBUG:
             os.system("rm -rf %s"%tempdir)
 
-    def call(self, auth, node_id_or_hostname, action, filename):
+    def call(self, auth, node_id_or_hostname, action, filename, options = []):
 
         ### check action
-        if action not in boot_medium_actions:
+        found=False
+        for boot_medium_action in boot_medium_actions:
+            if action.startswith(boot_medium_action): 
+                found=True
+                break
+
+        if not found:
             raise PLCInvalidArgument, "Unknown action %s"%action
 
-        ### compute file suffix 
+        ### compute file suffix and type
         if action.find("-iso") >= 0 :
             suffix=".iso"
+            type = ["iso"]
         elif action.find("-usb") >= 0:
             suffix=".usb"
+            type = ["usb"]
         else:
             suffix=".txt"
+            type = ["txt"]
+
+        if type != "txt":
+            if 'serial' in options:
+                suffix = "-serial" + suffix
+                type.insert(1, "serial")
+            if 'cramfs' in options:
+                suffix = "-cramfs" + suffix
+                # XXX must be the same index as above
+                type.insert(1, "cramfs")
+        type = "_".join(type)
 
         ### compute a 8 bytes random number
         tempbytes = random.sample (xrange(0,256), 8);
@@ -309,40 +333,25 @@ class GetBootMedium(Method):
                 ### return the generic medium content as-is, just base64 encoded
                 return base64.b64encode(file(generic_path).read())
 
-        ### floppy preview
-        if action == 'node-preview':
-            floppy = self.floppy_contents (node,False)
-            if filename:
-                try:
-                    file(filename,'w').write(floppy)
-                except:
-                    raise PLCPermissionDenied, "Could not write into %s"%filename
-                return filename
-            else:
-                return floppy
-
-        if action == 'node-floppy':
-            floppy = self.floppy_contents (node,True)
-            if filename:
-                try:
-                    file(filename,'w').write(floppy)
-                except:
-                    raise PLCPermissionDenied, "Could not write into %s"%filename
-                return filename
-            else:
-                return floppy
+       ### config file preview or regenerated
+       if action == 'node-preview' or action == 'node-floppy':
+            if action == 'node-preview': bo=False
+            else: bo=True
+            floppy = self.floppy_contents (node,bo)
+           if filename:
+               try:
+                   file(filename,'w').write(floppy)
+               except:
+                   raise PLCPermissionDenied, "Could not write into %s"%filename
+               return filename
+           else:
+               return floppy
 
         ### we're left with node-iso and node-usb
-        if action == 'node-iso' or action == 'node-usb':
+        if action.startswith('node-iso') or action.startswith('node-usb'):
 
             ### check we've got required material
             version = self.bootcd_version()
-            generic_name = "%s-BootCD-%s%s"%(self.api.config.PLC_NAME,
-                                             version,
-                                             suffix)
-            generic_path = "%s/%s" % (self.GENERICDIR,generic_name)
-            if not os.path.isfile(generic_path):
-                raise PLCAPIError, "Cannot locate generic medium %s"%generic_path
             
             if not os.path.isfile(self.BOOTCDBUILD):
                 raise PLCAPIError, "Cannot locate bootcd/build.sh script %s"%self.BOOTCDBUILD
@@ -367,11 +376,22 @@ class GetBootMedium(Method):
 
                 node_image = "%s/%s"%(tempdir,nodename)
                 # invoke build.sh
-                build_command = '%s -f "%s" -O "%s" -t "%s" &> %s.log' % (self.BOOTCDBUILD,
-                                                                          node_floppy,
-                                                                          node_image,
-                                                                          suffix[1:],
-                                                                          node_image)
+                if action.find("-serial") > 0:
+                    serial_info = action[action.find("-serial")+len("-serial"):]
+                    if len(serial_info) > 0:
+                        serial_info = serial_info[1:]
+                    else:
+                        serial_info = "ttyS0:115200:n:8"
+                    serial_arg='-s "%s"' % serial_info
+                else:
+                    serial_arg=""
+
+                build_command = '%s -f "%s" -O "%s" -t "%s" %s &> %s.log' % (self.BOOTCDBUILD,
+                                                                             node_floppy,
+                                                                             node_image,
+                                                                             type,
+                                                                             serial_arg,
+                                                                             node_image)
                 if self.DEBUG:
                     print 'build command:',build_command
                 ret=os.system(build_command)