Move some code into separate class in import/export daemon
[ganeti-local] / daemons / ganeti-noded
index 97feb39..ef78513 100755 (executable)
@@ -45,6 +45,7 @@ from ganeti import daemon
 from ganeti import http
 from ganeti import utils
 from ganeti import storage
+from ganeti import serializer
 
 import ganeti.http.server # pylint: disable-msg=W0611
 
@@ -92,6 +93,21 @@ def _RequireJobQueueLock(fn):
   return wrapper
 
 
+def _DecodeImportExportIO(ieio, ieioargs):
+  """Decodes import/export I/O information.
+
+  """
+  if ieio == constants.IEIO_RAW_DISK:
+    assert len(ieioargs) == 1
+    return (objects.Disk.FromDict(ieioargs[0]), )
+
+  if ieio == constants.IEIO_SCRIPT:
+    assert len(ieioargs) == 2
+    return (objects.Disk.FromDict(ieioargs[0]), ieioargs[1])
+
+  return ieioargs
+
+
 class NodeHttpServer(http.server.HttpServer):
   """The server implementation.
 
@@ -121,14 +137,13 @@ class NodeHttpServer(http.server.HttpServer):
       raise http.HttpNotFound()
 
     try:
-      rvalue = method(req.request_body)
-      return True, rvalue
+      result = (True, method(serializer.LoadJson(req.request_body)))
 
     except backend.RPCFail, err:
       # our custom failure exception; str(err) works fine if the
       # exception was constructed with a single argument, and in
       # this case, err.message == err.args[0] == str(err)
-      return (False, str(err))
+      result = (False, str(err))
     except errors.QuitGanetiException, err:
       # Tell parent to quit
       logging.info("Shutting down the node daemon, arguments: %s",
@@ -136,10 +151,12 @@ class NodeHttpServer(http.server.HttpServer):
       os.kill(self.noded_pid, signal.SIGTERM)
       # And return the error's arguments, which must be already in
       # correct tuple format
-      return err.args
+      result = err.args
     except Exception, err:
       logging.exception("Error in RPC call")
-      return False, "Error while executing backend function: %s" % str(err)
+      result = (False, "Error while executing backend function: %s" % str(err))
+
+    return serializer.DumpJson(result, indent=False)
 
   # the new block devices  --------------------------
 
@@ -336,20 +353,6 @@ class NodeHttpServer(http.server.HttpServer):
   # export/import  --------------------------
 
   @staticmethod
-  def perspective_snapshot_export(params):
-    """Export a given snapshot.
-
-    """
-    disk = objects.Disk.FromDict(params[0])
-    dest_node = params[1]
-    instance = objects.Instance.FromDict(params[2])
-    cluster_name = params[3]
-    dev_idx = params[4]
-    debug = params[5]
-    return backend.ExportSnapshot(disk, dest_node, instance,
-                                  cluster_name, dev_idx, debug)
-
-  @staticmethod
   def perspective_finalize_export(params):
     """Expose the finalize export functionality.
 
@@ -471,16 +474,6 @@ class NodeHttpServer(http.server.HttpServer):
     return backend.RunRenameInstance(inst, old_name, debug)
 
   @staticmethod
-  def perspective_instance_os_import(params):
-    """Run the import function of an OS onto a given instance.
-
-    """
-    inst_s, src_node, src_images, cluster_name, debug = params
-    inst = objects.Instance.FromDict(inst_s)
-    return backend.ImportOSIntoInstance(inst, src_node, src_images,
-                                        cluster_name, debug)
-
-  @staticmethod
   def perspective_instance_shutdown(params):
     """Shutdown an instance.
 
@@ -818,6 +811,75 @@ class NodeHttpServer(http.server.HttpServer):
     (hvname, hvparams) = params
     return backend.ValidateHVParams(hvname, hvparams)
 
+  # Crypto
+
+  @staticmethod
+  def perspective_x509_cert_create(params):
+    """Creates a new X509 certificate for SSL/TLS.
+
+    """
+    (validity, ) = params
+    return backend.CreateX509Certificate(validity)
+
+  @staticmethod
+  def perspective_x509_cert_remove(params):
+    """Removes a X509 certificate.
+
+    """
+    (name, ) = params
+    return backend.RemoveX509Certificate(name)
+
+  # Import and export
+
+  @staticmethod
+  def perspective_import_start(params):
+    """Starts an import daemon.
+
+    """
+    (x509_key_name, source_x509_ca, instance, dest, dest_args) = params
+    return backend.StartImportExportDaemon(constants.IEM_IMPORT,
+                                           x509_key_name, source_x509_ca,
+                                           None, None,
+                                           objects.Instance.FromDict(instance),
+                                           dest,
+                                           _DecodeImportExportIO(dest,
+                                                                 dest_args))
+  @staticmethod
+  def perspective_export_start(params):
+    """Starts an export daemon.
+
+    """
+    (x509_key_name, dest_x509_ca, host, port, instance,
+     source, source_args) = params
+    return backend.StartImportExportDaemon(constants.IEM_EXPORT,
+                                           x509_key_name, dest_x509_ca,
+                                           host, port,
+                                           objects.Instance.FromDict(instance),
+                                           source,
+                                           _DecodeImportExportIO(source,
+                                                                 source_args))
+
+  @staticmethod
+  def perspective_impexp_status(params):
+    """Retrieves the status of an import or export daemon.
+
+    """
+    return backend.GetImportExportStatus(params[0])
+
+  @staticmethod
+  def perspective_impexp_abort(params):
+    """Aborts an import or export.
+
+    """
+    return backend.AbortImportExport(params[0])
+
+  @staticmethod
+  def perspective_impexp_cleanup(params):
+    """Cleans up after an import or export.
+
+    """
+    return backend.CleanupImportExport(params[0])
+
 
 def CheckNoded(_, args):
   """Initial checks whether to run or exit with a failure.
@@ -868,7 +930,11 @@ def main():
   dirs = [(val, constants.RUN_DIRS_MODE) for val in constants.SUB_RUN_DIRS]
   dirs.append((constants.LOG_OS_DIR, 0750))
   dirs.append((constants.LOCK_DIR, 1777))
-  daemon.GenericMain(constants.NODED, parser, dirs, CheckNoded, ExecNoded)
+  dirs.append((constants.CRYPTO_KEYS_DIR, constants.CRYPTO_KEYS_DIR_MODE))
+  dirs.append((constants.IMPORT_EXPORT_DIR, constants.IMPORT_EXPORT_DIR_MODE))
+  daemon.GenericMain(constants.NODED, parser, dirs, CheckNoded, ExecNoded,
+                     default_ssl_cert=constants.NODED_CERT_FILE,
+                     default_ssl_key=constants.NODED_CERT_FILE)
 
 
 if __name__ == '__main__':