X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/83d92ad8a5c653b599fec0f9cb550875012aef25..4ca693ca220138a4bcc47b19b9766c9e3a4c6c1f:/daemons/ganeti-noded diff --git a/daemons/ganeti-noded b/daemons/ganeti-noded index 3e53f2b..ef78513 100755 --- a/daemons/ganeti-noded +++ b/daemons/ganeti-noded @@ -21,14 +21,16 @@ """Ganeti node daemon""" -# functions in this module need to have a given name structure, so: -# pylint: disable-msg=C0103 +# pylint: disable-msg=C0103,W0142 + +# C0103: Functions in this module need to have a given name structure, +# and the name of the daemon doesn't match + +# W0142: Used * or ** magic, since we do use it extensively in this +# module import os import sys -import traceback -import SocketServer -import errno import logging import signal @@ -42,13 +44,34 @@ from ganeti import jstore from ganeti import daemon from ganeti import http from ganeti import utils +from ganeti import storage +from ganeti import serializer -import ganeti.http.server +import ganeti.http.server # pylint: disable-msg=W0611 queue_lock = None +def _PrepareQueueLock(): + """Try to prepare the queue lock. + + @return: None for success, otherwise an exception object + + """ + global queue_lock # pylint: disable-msg=W0603 + + if queue_lock is not None: + return None + + # Prepare job queue + try: + queue_lock = jstore.InitAndVerifyQueue(must_lock=False) + return None + except EnvironmentError, err: + return err + + def _RequireJobQueueLock(fn): """Decorator for job queue manipulating functions. @@ -58,6 +81,9 @@ def _RequireJobQueueLock(fn): def wrapper(*args, **kwargs): # Locking in exclusive, blocking mode because there could be several # children running at the same time. Waiting up to 10 seconds. + if _PrepareQueueLock() is not None: + raise errors.JobQueueError("Job queue failed initialization," + " cannot update jobs") queue_lock.Exclusive(blocking=True, timeout=QUEUE_LOCK_TIMEOUT) try: return fn(*args, **kwargs) @@ -67,12 +93,30 @@ 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. This class holds all methods exposed over the RPC interface. """ + # too many public methods, and unused args - all methods get params + # due to the API + # pylint: disable-msg=R0904,W0613 def __init__(self, *args, **kwargs): http.server.HttpServer.__init__(self, *args, **kwargs) self.noded_pid = os.getpid() @@ -93,12 +137,13 @@ class NodeHttpServer(http.server.HttpServer): raise http.HttpNotFound() try: - return method(req.request_body) + 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", @@ -106,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 - except: + result = err.args + except Exception, err: logging.exception("Error in RPC call") - raise + result = (False, "Error while executing backend function: %s" % str(err)) + + return serializer.DumpJson(result, indent=False) # the new block devices -------------------------- @@ -199,8 +246,9 @@ class NodeHttpServer(http.server.HttpServer): """ disks = [objects.Disk.FromDict(dsk_s) - for dsk_s in params] - return backend.BlockdevGetmirrorstatus(disks) + for dsk_s in params] + return [status.ToDict() + for status in backend.BlockdevGetmirrorstatus(disks)] @staticmethod def perspective_blockdev_find(params): @@ -210,7 +258,12 @@ class NodeHttpServer(http.server.HttpServer): """ disk = objects.Disk.FromDict(params[0]) - return backend.BlockdevFind(disk) + + result = backend.BlockdevFind(disk) + if result is None: + return None + + return result.ToDict() @staticmethod def perspective_blockdev_snapshot(params): @@ -241,6 +294,23 @@ class NodeHttpServer(http.server.HttpServer): disks = [objects.Disk.FromDict(cf) for cf in params[1]] return backend.BlockdevClose(params[0], disks) + @staticmethod + def perspective_blockdev_getsize(params): + """Compute the sizes of the given block devices. + + """ + disks = [objects.Disk.FromDict(cf) for cf in params[0]] + return backend.BlockdevGetsize(disks) + + @staticmethod + def perspective_blockdev_export(params): + """Compute the sizes of the given block devices. + + """ + disk = objects.Disk.FromDict(params[0]) + dest_node, dest_path, cluster_name = params[1:] + return backend.BlockdevExport(disk, dest_node, dest_path, cluster_name) + # blockdev/drbd specific methods ---------- @staticmethod @@ -283,26 +353,19 @@ 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] - return backend.ExportSnapshot(disk, dest_node, instance, - cluster_name, dev_idx) - - @staticmethod def perspective_finalize_export(params): """Expose the finalize export functionality. """ instance = objects.Instance.FromDict(params[0]) - snap_disks = [objects.Disk.FromDict(str_data) - for str_data in params[1]] + + snap_disks = [] + for disk in params[1]: + if isinstance(disk, bool): + snap_disks.append(disk) + else: + snap_disks.append(objects.Disk.FromDict(disk)) + return backend.FinalizeExport(instance, snap_disks) @staticmethod @@ -338,12 +401,12 @@ class NodeHttpServer(http.server.HttpServer): # volume -------------------------- @staticmethod - def perspective_volume_list(params): + def perspective_lv_list(params): """Query the list of logical volumes in a given volume group. """ vgname = params[0] - return True, backend.GetVolumeList(vgname) + return backend.GetVolumeList(vgname) @staticmethod def perspective_vg_list(params): @@ -352,6 +415,32 @@ class NodeHttpServer(http.server.HttpServer): """ return backend.ListVolumeGroups() + # Storage -------------------------- + + @staticmethod + def perspective_storage_list(params): + """Get list of storage units. + + """ + (su_name, su_args, name, fields) = params + return storage.GetStorage(su_name, *su_args).List(name, fields) + + @staticmethod + def perspective_storage_modify(params): + """Modify a storage unit. + + """ + (su_name, su_args, name, changes) = params + return storage.GetStorage(su_name, *su_args).Modify(name, changes) + + @staticmethod + def perspective_storage_execute(params): + """Execute an operation on a storage unit. + + """ + (su_name, su_args, name, op) = params + return storage.GetStorage(su_name, *su_args).Execute(name, op) + # bridge -------------------------- @staticmethod @@ -372,26 +461,17 @@ class NodeHttpServer(http.server.HttpServer): inst_s = params[0] inst = objects.Instance.FromDict(inst_s) reinstall = params[1] - return backend.InstanceOsAdd(inst, reinstall) + debug = params[2] + return backend.InstanceOsAdd(inst, reinstall, debug) @staticmethod def perspective_instance_run_rename(params): """Runs the OS rename script for an instance. """ - inst_s, old_name = params - inst = objects.Instance.FromDict(inst_s) - return backend.RunRenameInstance(inst, old_name) - - @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 = params + inst_s, old_name, debug = params inst = objects.Instance.FromDict(inst_s) - return backend.ImportOSIntoInstance(inst, src_node, src_images, - cluster_name) + return backend.RunRenameInstance(inst, old_name, debug) @staticmethod def perspective_instance_shutdown(params): @@ -399,7 +479,8 @@ class NodeHttpServer(http.server.HttpServer): """ instance = objects.Instance.FromDict(params[0]) - return backend.InstanceShutdown(instance) + timeout = params[1] + return backend.InstanceShutdown(instance, timeout) @staticmethod def perspective_instance_start(params): @@ -451,7 +532,8 @@ class NodeHttpServer(http.server.HttpServer): """ instance = objects.Instance.FromDict(params[0]) reboot_type = params[1] - return backend.InstanceReboot(instance, reboot_type) + shutdown_timeout = params[2] + return backend.InstanceReboot(instance, reboot_type, shutdown_timeout) @staticmethod def perspective_instance_info(params): @@ -480,7 +562,7 @@ class NodeHttpServer(http.server.HttpServer): """Query the list of running instances. """ - return True, backend.GetInstanceList(params[0]) + return backend.GetInstanceList(params[0]) # node -------------------------- @@ -497,7 +579,7 @@ class NodeHttpServer(http.server.HttpServer): """Checks if a node has the given ip address. """ - return True, utils.OwnIpAddress(params[0]) + return utils.OwnIpAddress(params[0]) @staticmethod def perspective_node_info(params): @@ -527,7 +609,7 @@ class NodeHttpServer(http.server.HttpServer): """Promote this node to master status. """ - return backend.StartMaster(params[0]) + return backend.StartMaster(params[0], params[1]) @staticmethod def perspective_node_stop_master(params): @@ -541,7 +623,7 @@ class NodeHttpServer(http.server.HttpServer): """Cleanup after leaving a cluster. """ - return backend.LeaveCluster() + return backend.LeaveCluster(params[0]) @staticmethod def perspective_node_volumes(params): @@ -574,7 +656,7 @@ class NodeHttpServer(http.server.HttpServer): """Query version information. """ - return True, constants.PROTOCOL_VERSION + return constants.PROTOCOL_VERSION @staticmethod def perspective_upload_file(params): @@ -608,7 +690,7 @@ class NodeHttpServer(http.server.HttpServer): """Query detailed information about existing OSes. """ - return True, [os_obj.ToDict() for os_obj in backend.DiagnoseOS()] + return backend.DiagnoseOS() @staticmethod def perspective_os_get(params): @@ -616,10 +698,7 @@ class NodeHttpServer(http.server.HttpServer): """ name = params[0] - try: - os_obj = backend.OSFromDisk(name) - except errors.InvalidOS, err: - os_obj = objects.OS.FromInvalidOS(err) + os_obj = backend.OSFromDisk(name) return os_obj.ToDict() # hooks ----------------------- @@ -652,7 +731,10 @@ class NodeHttpServer(http.server.HttpServer): """ duration = params[0] - return utils.TestDelay(duration) + status, rval = utils.TestDelay(duration) + if not status: + raise backend.RPCFail(rval) + return rval # file storage --------------- @@ -729,87 +811,130 @@ class NodeHttpServer(http.server.HttpServer): (hvname, hvparams) = params return backend.ValidateHVParams(hvname, hvparams) + # Crypto -def ParseOptions(): - """Parse the command line options. + @staticmethod + def perspective_x509_cert_create(params): + """Creates a new X509 certificate for SSL/TLS. - @return: (options, args) as from OptionParser.parse_args() + """ + (validity, ) = params + return backend.CreateX509Certificate(validity) - """ - parser = OptionParser(description="Ganeti node daemon", - usage="%prog [-f] [-d] [-b ADDRESS]", - version="%%prog (ganeti) %s" % - constants.RELEASE_VERSION) + @staticmethod + def perspective_x509_cert_remove(params): + """Removes a X509 certificate. - parser.add_option("-f", "--foreground", dest="fork", - help="Don't detach from the current terminal", - default=True, action="store_false") - parser.add_option("-d", "--debug", dest="debug", - help="Enable some debug messages", - default=False, action="store_true") - parser.add_option("-b", "--bind", dest="bind_address", - help="Bind address", - default="", metavar="ADDRESS") + """ + (name, ) = params + return backend.RemoveX509Certificate(name) - options, args = parser.parse_args() - return options, args + # Import and export + @staticmethod + def perspective_import_start(params): + """Starts an import daemon. -def main(): - """Main function for the node 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. - """ - global queue_lock + """ + (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)) - options, args = ParseOptions() - utils.debug = options.debug + @staticmethod + def perspective_impexp_status(params): + """Retrieves the status of an import or export daemon. - if options.fork: - utils.CloseFDs() + """ + return backend.GetImportExportStatus(params[0]) - for fname in (constants.SSL_CERT_FILE,): - if not os.path.isfile(fname): - print "config %s not there, will not run." % fname - sys.exit(5) + @staticmethod + def perspective_impexp_abort(params): + """Aborts an import or export. - try: - port = utils.GetNodeDaemonPort() - except errors.ConfigurationError, err: - print "Cluster configuration incomplete: '%s'" % str(err) - sys.exit(5) + """ + return backend.AbortImportExport(params[0]) - 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)) - utils.EnsureDirs(dirs) + @staticmethod + def perspective_impexp_cleanup(params): + """Cleans up after an import or export. - # become a daemon - if options.fork: - utils.Daemonize(logfile=constants.LOG_NODESERVER) + """ + return backend.CleanupImportExport(params[0]) - utils.WritePidFile(constants.NODED_PID) - try: - utils.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug, - stderr_logging=not options.fork) - logging.info("ganeti node daemon startup") - # Read SSL certificate - ssl_params = http.HttpSslParams(ssl_key_path=constants.SSL_CERT_FILE, - ssl_cert_path=constants.SSL_CERT_FILE) +def CheckNoded(_, args): + """Initial checks whether to run or exit with a failure. - # Prepare job queue - queue_lock = jstore.InitAndVerifyQueue(must_lock=False) + """ + if args: # noded doesn't take any arguments + print >> sys.stderr, ("Usage: %s [-f] [-d] [-p port] [-b ADDRESS]" % + sys.argv[0]) + sys.exit(constants.EXIT_FAILURE) - mainloop = daemon.Mainloop() - server = NodeHttpServer(mainloop, options.bind_address, port, - ssl_params=ssl_params, ssl_verify_peer=True) - server.Start() - try: - mainloop.Run() - finally: - server.Stop() + +def ExecNoded(options, _): + """Main node daemon function, executed with the PID file held. + + """ + # Read SSL certificate + if options.ssl: + ssl_params = http.HttpSslParams(ssl_key_path=options.ssl_key, + ssl_cert_path=options.ssl_cert) + else: + ssl_params = None + + err = _PrepareQueueLock() + if err is not None: + # this might be some kind of file-system/permission error; while + # this breaks the job queue functionality, we shouldn't prevent + # startup of the whole node daemon because of this + logging.critical("Can't init/verify the queue, proceeding anyway: %s", err) + + mainloop = daemon.Mainloop() + server = NodeHttpServer(mainloop, options.bind_address, options.port, + ssl_params=ssl_params, ssl_verify_peer=True) + server.Start() + try: + mainloop.Run() finally: - utils.RemovePidFile(constants.NODED_PID) + server.Stop() + + +def main(): + """Main function for the node daemon. + + """ + parser = OptionParser(description="Ganeti node daemon", + usage="%prog [-f] [-d] [-p port] [-b ADDRESS]", + version="%%prog (ganeti) %s" % + constants.RELEASE_VERSION) + 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)) + 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__':