Statistics
| Branch: | Tag: | Revision:

root / lib / confd / querylib.py @ 6daf26a0

History | View | Annotate | Download (2.9 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

    
29
class ConfdQuery(object):
30
  """Confd Query base class.
31

32
  """
33
  def __init__(self, reader):
34
    """Constructor for Confd Query
35

36
    @type reader: L{ssconf.SimpleConfigReader}
37
    @param reader: ConfigReader to use to access the config
38

39
    """
40
    self.reader = reader
41

    
42
  def Exec(self, query):
43
    """Process a single UDP request from a client.
44

45
    Different queries should override this function, which by defaults returns
46
    a "non-implemented" answer.
47

48
    @type query: (undefined)
49
    @param query: ConfdRequest 'query' field
50
    @rtype: (integer, undefined)
51
    @return: status and answer to give to the client
52

53
    """
54
    status = constants.CONFD_REPL_STATUS_NOTIMPLEMENTED
55
    answer = 'not implemented'
56
    return status, answer
57

    
58

    
59
class PingQuery(ConfdQuery):
60
  """An empty confd query.
61

62
  It will return success on an empty argument, and an error on any other argument.
63

64
  """
65
  def Exec(self, query):
66
    """EmptyQuery main execution
67

68
    """
69
    if query is None:
70
      status = constants.CONFD_REPL_STATUS_OK
71
      answer = 'ok'
72
    else:
73
      status = constants.CONFD_REPL_STATUS_ERROR
74
      answer = 'non-empty ping query'
75

    
76
    return status, answer
77

    
78

    
79
class NodeRoleQuery(ConfdQuery):
80
  """An empty confd query.
81

82
  It will return success on an empty argument, and an error on any other argument.
83

84
  """
85
  def Exec(self, query):
86
    """EmptyQuery main execution
87

88
    """
89
    node = query
90
    if self.reader.GetMasterNode() == node:
91
      status = constants.CONFD_REPL_STATUS_OK
92
      answer = constants.CONFD_NODE_ROLE_MASTER
93
      return status, answer
94
    flags = self.reader.GetNodeStatusFlags(node)
95
    if flags is None:
96
      status = constants.CONFD_REPL_STATUS_ERROR
97
      answer = constants.CONFD_ERROR_UNKNOWN_ENTRY
98
      return status, answer
99

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

    
110
    return constants.CONFD_REPL_STATUS_OK, answer
111