svn merge -c 8998 ./branches/dummynet-4.2-2 ./trunk/
[plcapi.git] / PLC / Methods / GetBootMedium.py
index bc4c64d..4e66c5c 100644 (file)
@@ -22,6 +22,18 @@ boot_medium_actions = [ 'node-preview',
                         'generic-usb',
                         ]
 
+# compute a new key
+# xxx used by GetDummyBoxMedium
+def compute_key():
+    # Generate 32 random bytes
+    bytes = random.sample(xrange(0, 256), 32)
+    # Base64 encode their string representation
+    key = base64.b64encode("".join(map(chr, bytes)))
+    # Boot Manager cannot handle = in the key
+    # XXX this sounds wrong, as it might prevent proper decoding
+    key = key.replace("=", "")
+    return key
+
 class GetBootMedium(Method):
     """
     This method is a redesign based on former, supposedly dedicated, 
@@ -71,11 +83,13 @@ class GetBootMedium(Method):
         ** WARNING **
         It is the caller's responsability to remove this file after use.
 
-    Options: an optional array of keywords. Currently supported are
-        - 'serial'
+    Options: an optional array of keywords. 
+        options are not supported for generic images
+    Currently supported are
+        - 'partition' - for USB actions only
         - 'cramfs'
-        - 'console:<console_spec>'
-        console_spec is passed as-is to bootcd/build.sh
+        - 'serial' or 'serial:<console_spec>'
+        console_spec (or 'default') is passed as-is to bootcd/build.sh
         it is expected to be a colon separated string denoting
         tty - baudrate - parity - bits
         e.g. ttyS0:115200:n:8
@@ -147,12 +161,7 @@ class GetBootMedium(Method):
         ( host, domain ) = self.split_hostname (node)
 
         if renew_key:
-            # Generate 32 random bytes
-            bytes = random.sample(xrange(0, 256), 32)
-            # Base64 encode their string representation
-            node['key'] = base64.b64encode("".join(map(chr, bytes)))
-            # XXX Boot Manager cannot handle = in the key
-            node['key'] = node['key'].replace("=", "")
+            node['key'] = compute_key()
             # Save it
             node.sync()
 
@@ -230,23 +239,35 @@ class GetBootMedium(Method):
         ### compute file suffix and type
         if action.find("-iso") >= 0 :
             suffix=".iso"
-            type = ["iso"]
+            type = "iso"
         elif action.find("-usb") >= 0:
             suffix=".usb"
-            type = ["usb"]
+            type = "usb"
         else:
             suffix=".txt"
-            type = ["txt"]
-
-        if "txt" not in type:
-            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)
+            type = "txt"
+
+        # handle / caconicalize options
+        if type == "txt":
+            if options:
+                raise PLCInvalidArgument, "Options are not supported for node configs"
+        else:
+            # create a dict for build.sh 
+            optdict={}
+            for option in options:
+                if option == "cramfs":
+                    optdict['cramfs']=True
+                elif option == 'partition':
+                    if type != "usb":
+                        raise PLCInvalidArgument, "option 'partition' is for USB images only"
+                    else:
+                        type="usb_partition"
+                elif option == "serial":
+                    optdict['serial']='default'
+                elif option.find("serial:") == 0:
+                    optdict['serial']=option.replace("serial:","")
+                else:
+                    raise PLCInvalidArgument, "unknown option %s"%option
 
         ### compute a 8 bytes random number
         tempbytes = random.sample (xrange(0,256), 8);
@@ -267,6 +288,8 @@ class GetBootMedium(Method):
             nodename = temp
             
         ### handle filename
+        # allow to set filename to None or any other empty value
+        if not filename: filename=''
         filename = filename.replace ("%d",self.WORKDIR)
         filename = filename.replace ("%n",nodename)
         filename = filename.replace ("%s",suffix)
@@ -287,15 +310,20 @@ class GetBootMedium(Method):
 
             ### we can now safely create the file, 
             ### either we are admin or under a controlled location
-            if not os.path.exists(os.path.dirname(filename)):
-                try:
-                    os.makedirs (os.path.dirname(filename),0777)
-                except:
-                    raise PLCPermissionDenied, "Could not create dir %s"%os.path.dirname(filename)
+            filedir=os.path.dirname(filename)
+            # dirname does not return "." for a local filename like its shell counterpart
+            if filedir:
+                if not os.path.exists(filedir):
+                    try:
+                        os.makedirs (dirname,0777)
+                    except:
+                        raise PLCPermissionDenied, "Could not create dir %s"%dirname
 
         
         ### generic media
         if action == 'generic-iso' or action == 'generic-usb':
+            if options:
+                raise PLCInvalidArgument, "Options are not supported for generic images"
             # this raises an exception if bootcd is missing
             version = self.bootcd_version()
             generic_name = "%s-BootCD-%s%s"%(self.api.config.PLC_NAME,
@@ -355,29 +383,28 @@ class GetBootMedium(Method):
 
                 self.trash.append(floppy_file)
 
-                node_image = "%s/%s"%(self.WORKDIR,nodename)
+                node_image = "%s/%s%s"%(self.WORKDIR,nodename,suffix)
 
-                # handle console
+                # make build's arguments
                 serial_arg=""
-                for option in options:
-                    console=option.replace("console:","")
-                    if option != console:
-                        serial_arg="-s %s"%console
+                if "cramfs" in optdict: type += "_cramfs"
+                if "serial" in optdict: serial_arg = "-s %s"%optdict['serial']
+                log_file="%s.log"%node_image
                 # invoke build.sh
-                build_command = '%s -f "%s" -O "%s" -t "%s" %s &> %s.log' % (self.BOOTCDBUILD,
-                                                                             floppy_file,
-                                                                             node_image,
-                                                                             type,
-                                                                             serial_arg,
-                                                                             node_image)
+                build_command = '%s -f "%s" -o "%s" -t "%s" %s &> %s' % (self.BOOTCDBUILD,
+                                                                         floppy_file,
+                                                                         node_image,
+                                                                         type,
+                                                                         serial_arg,
+                                                                         log_file)
                 if self.DEBUG:
                     print 'build command:',build_command
                 ret=os.system(build_command)
                 if ret != 0:
-                    raise PLCPermissionDenied,"build.sh failed to create node-specific medium"
+                    raise PLCAPIError,"bootcd/build.sh failed\n%s\n%s"%(
+                        build_command,file(log_file).read())
 
-                self.trash.append("%s.log"%node_image)
-                node_image += suffix
+                self.trash.append(log_file)
                 if not os.path.isfile (node_image):
                     raise PLCAPIError,"Unexpected location of build.sh output - %s"%node_image