svn keywords
[plcapi.git] / PLC / Methods / GetDummyBoxMedium.py
index 2fb4e55..3ba8fd4 100644 (file)
@@ -1,6 +1,7 @@
 #
 # Marta Carbone - UniPi
 # $Id$
+# $URL$
 #
 # This class requires the rpm package containing
 # the picobsd image to be installed
@@ -17,24 +18,21 @@ from PLC.Parameter import Parameter               # use the Parameter wrapper
 from PLC.Auth import Auth                         # import the Auth parameter
 from PLC.Nodes import *                           # nodes functions
 from PLC.Methods.GetBootMedium import compute_key # key generation function
-from PLC.Accessors.Accessors_dummynetbox import *                       # import dummynet accessors
+from PLC.Accessors.Accessors_dummynetbox import * # import dummynet accessors
 
 WORK_DIR = "/var/tmp/DummynetBoxMedium"
 BASE_IMAGE = "/usr/share/dummynet/picobsd"
 
 class GetDummyBoxMedium(Method):
     """
-    This method is used to get a boot image of the DummyNetBox
-    equipped with the configuration file.
+    This method is used to fetch a boot image for the DummyNetBox
+    with its configuration file embedded.
 
-    We need to provide the dummybox_id of the DummyNetBox
-    we want to generate.
-    Since every time a new configuration file will be generater,
-    THIS OPERATION WILL INVALIDATE ANY PREVIOUSLY DUMMYNETBOX BOOT MEDIUM.
-    # XXX add checks for picobsd.bin existence
+    Every time this method is called, a new key is generated in the configuration file,
+    SO THIS OPERATION WILL INVALIDATE ANY PREVIOUSLY DUMMYNETBOX BOOT MEDIUM.
 
-    Returns the iso image customized for the DummyNetBox with the new
-    key integrated in the image, and update the key fields in the database.
+    This method updates the key fields in the database.
+    It returns a base64-encoded boot image.
     """
     # I added the session role, because this method should be called from the web
     roles = ['admin', 'pi', 'tech', 'session']
@@ -47,6 +45,8 @@ class GetDummyBoxMedium(Method):
 
     returns = Parameter(str, "DummynetBox boot medium")
 
+    # XXX add checks for picobsd.bin existence
+
     # Generate a new configuration file in the working directory
     # input parameters follows:
     # self is used to access instance data,
@@ -59,7 +59,7 @@ class GetDummyBoxMedium(Method):
         today = datetime.date.today()
         file = ""
         file += "# This is the dummynetbox configuration file\n"
-        file += "# and was generated the %s\n" % str(today)
+        file += "# and was generated on %s\n" % str(today)
         
         host_domain = dummybox['hostname']
         host_domain = host_domain.split('.', 1)
@@ -109,6 +109,7 @@ class GetDummyBoxMedium(Method):
             raise PLCInvalidArgument, "No primary network configured on %s" % dummybox_hostname
 
        dummybox = interface_info
+        dummybox['hostname']=dummybox_hostname
 
        # Select the base image, default to bin image
         if type != 'iso':
@@ -120,10 +121,6 @@ class GetDummyBoxMedium(Method):
 
         # Permission checks
         assert self.caller is not None
-        if 'admin' not in self.caller['roles']:
-            if dummybox['site_id'] not in self.caller['site_ids']:
-                raise PLCPermissionDenied, "Not allowed to generate an iso image for %s %s" % \
-                               (dummybox['hostname'], dummybox_id)
 
         # Start the generation of the image
         # Generate a new key
@@ -152,7 +149,7 @@ class GetDummyBoxMedium(Method):
        shell_script += " chmod u+w %s; chmod u+w %s; " % (IMAGE_NAME, configfile)
 
         # cat the configuration file in the raw image
-        shell_script += "cat %s | dd of=%s seek=$MATCH conv=notrunc bs=1)" \
+        shell_script += " cat %s | dd of=%s seek=$MATCH conv=notrunc bs=1)" \
                            % (configfile, IMAGE_NAME)
 
         # check returned values, 0 means OK, remove the lock file
@@ -160,9 +157,13 @@ class GetDummyBoxMedium(Method):
         os.system("rm %s" % (lockfile))
 
         # if all goes fine store the key in the database
-        dummybox['key'] = new_key
-        dummybox.sync()
-
-        # return the file
-        return IMAGE_NAME
-        return base64.b64encode(file(IMAGE_NAME).read())
+        nodes = Nodes(self.api, dummybox_id)
+        if not nodes:
+            raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
+        nodes[0]['key'] = new_key
+        nodes.sync()
+
+        # return the file's content base64-encoded
+        result = file(IMAGE_NAME).read()
+        os.unlink(IMAGE_NAME)
+        return base64.b64encode(result)