diff --git a/Makefile.am b/Makefile.am index 9e4038fdb142f57f034f3f09cd56d33821ac2a8b..ca838e3cda8713fd0d909c01806caba8523ed538 100644 --- a/Makefile.am +++ b/Makefile.am @@ -114,7 +114,8 @@ http_PYTHON = \ confd_PYTHON = \ lib/confd/__init__.py \ - lib/confd/server.py + lib/confd/server.py \ + lib/confd/querylib.py docrst = \ doc/admin.rst \ diff --git a/lib/confd/querylib.py b/lib/confd/querylib.py new file mode 100644 index 0000000000000000000000000000000000000000..d442fb697870308165b10096d9de0a9fd2f94b38 --- /dev/null +++ b/lib/confd/querylib.py @@ -0,0 +1,77 @@ +#!/usr/bin/python +# + +# Copyright (C) 2009, Google Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + + +"""Ganeti configuration daemon queries library. + +""" + +from ganeti import constants + + +class ConfdQuery(object): + """Confd Query base class. + + """ + def __init__(self, reader): + """Constructor for Confd Query + + @type reader: L{ssconf.SimpleConfigReader} + @param reader: ConfigReader to use to access the config + + """ + self.reader = reader + + def Exec(self, query): + """Process a single UDP request from a client. + + Different queries should override this function, which by defaults returns + a "non-implemented" answer. + + @type query: (undefined) + @param query: ConfdRequest 'query' field + @rtype: (integer, undefined) + @return: status and answer to give to the client + + """ + status = constants.CONFD_REPL_STATUS_NOTIMPLEMENTED + answer = 'not implemented' + return status, answer + + +class PingQuery(ConfdQuery): + """An empty confd query. + + It will return success on an empty argument, and an error on any other argument. + + """ + def Exec(self, query): + """EmptyQuery main execution + + """ + if query is None: + status = constants.CONFD_REPL_STATUS_OK + answer = 'ok' + else: + status = constants.CONFD_REPL_STATUS_ERROR + answer = 'non-empty ping query' + + return status, answer + diff --git a/lib/confd/server.py b/lib/confd/server.py index 94ebd82a0c4a0509f9fbd07bd5c164952f4c817f..3d1a2ee0c2e240217b72153e8334a088aa5567cb 100644 --- a/lib/confd/server.py +++ b/lib/confd/server.py @@ -35,12 +35,17 @@ from ganeti import errors from ganeti import utils from ganeti import serializer +from ganeti.confd import querylib + class ConfdProcessor(object): """A processor for confd requests. """ DISPATCH_TABLE = { + constants.CONFD_REQ_PING: querylib.PingQuery, + constants.CONFD_REQ_NODE_ROLE_BYNAME: querylib.ConfdQuery, + constants.CONFD_REQ_NODE_PIP_BY_INSTANCE_IP: querylib.ConfdQuery, } def __init__(self, reader): @@ -127,16 +132,17 @@ class ConfdProcessor(object): raise errors.ConfdRequestError(msg) if request.type not in self.DISPATCH_TABLE: - answer = 'not implemented' - status = constants.CONFD_REPL_STATUS_NOTIMPLEMENTED - reply = objects.ConfdReply( - protocol=constants.CONFD_PROTOCOL_VERSION, - status=status, - answer=answer, - ) - else: - # TODO: actually dispatch queries to some classes to handle them - assert False, "DISPATCH_TABLE is populated but handler is not" + msg = "Valid request %d not in DISPATCH_TABLE" % request.type + raise errors.ProgrammerError(msg) + + query_object = self.DISPATCH_TABLE[request.type](self.reader) + status, answer = query_object.Exec(request.query) + reply = objects.ConfdReply( + protocol=constants.CONFD_PROTOCOL_VERSION, + status=status, + answer=answer, + serial=self.reader.GetConfigSerialNo(), + ) logging.debug("Sending reply: %s" % reply)