Statistics
| Branch: | Tag: | Revision:

root / lib / confd / querylib.py @ 7189e790

History | View | Annotate | Download (3.1 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2009, Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Ganeti configuration daemon queries library.
23

24
"""
25

    
26
from ganeti import constants
27

    
28
# constants for some common errors to return from a query
29
QUERY_UNKNOWN_ENTRY_ERROR = (constants.CONFD_REPL_STATUS_ERROR,
30
                             constants.CONFD_ERROR_UNKNOWN_ENTRY)
31
QUERY_INTERNAL_ERROR = (constants.CONFD_REPL_STATUS_ERROR,
32
                        constants.CONFD_ERROR_INTERNAL)
33

    
34
class ConfdQuery(object):
35
  """Confd Query base class.
36

37
  """
38
  def __init__(self, reader):
39
    """Constructor for Confd Query
40

41
    @type reader: L{ssconf.SimpleConfigReader}
42
    @param reader: ConfigReader to use to access the config
43

44
    """
45
    self.reader = reader
46

    
47
  def Exec(self, query):
48
    """Process a single UDP request from a client.
49

50
    Different queries should override this function, which by defaults returns
51
    a "non-implemented" answer.
52

53
    @type query: (undefined)
54
    @param query: ConfdRequest 'query' field
55
    @rtype: (integer, undefined)
56
    @return: status and answer to give to the client
57

58
    """
59
    status = constants.CONFD_REPL_STATUS_NOTIMPLEMENTED
60
    answer = 'not implemented'
61
    return status, answer
62

    
63

    
64
class PingQuery(ConfdQuery):
65
  """An empty confd query.
66

67
  It will return success on an empty argument, and an error on any other argument.
68

69
  """
70
  def Exec(self, query):
71
    """EmptyQuery main execution
72

73
    """
74
    if query is None:
75
      status = constants.CONFD_REPL_STATUS_OK
76
      answer = 'ok'
77
    else:
78
      status = constants.CONFD_REPL_STATUS_ERROR
79
      answer = 'non-empty ping query'
80

    
81
    return status, answer
82

    
83

    
84
class NodeRoleQuery(ConfdQuery):
85
  """An empty confd query.
86

87
  It will return success on an empty argument, and an error on any other argument.
88

89
  """
90
  def Exec(self, query):
91
    """EmptyQuery main execution
92

93
    """
94
    node = query
95
    if self.reader.GetMasterNode() == node:
96
      status = constants.CONFD_REPL_STATUS_OK
97
      answer = constants.CONFD_NODE_ROLE_MASTER
98
      return status, answer
99
    flags = self.reader.GetNodeStatusFlags(node)
100
    if flags is None:
101
      return QUERY_UNKNOWN_ENTRY_ERROR
102

    
103
    master_candidate, drained, offline = flags
104
    if master_candidate:
105
      answer = constants.CONFD_NODE_ROLE_CANDIDATE
106
    elif drained:
107
      answer = constants.CONFD_NODE_ROLE_DRAINED
108
    elif offline:
109
      answer = constants.CONFD_NODE_ROLE_OFFLINE
110
    else:
111
      answer = constants.CONFD_NODE_ROLE_REGULAR
112

    
113
    return constants.CONFD_REPL_STATUS_OK, answer
114