querylib.py 4.13 KB
Newer Older
Guido Trotter's avatar
Guido Trotter committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/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.

"""

Guido Trotter's avatar
Guido Trotter committed
26
27
import logging

Guido Trotter's avatar
Guido Trotter committed
28
29
from ganeti import constants

30
31
32
33
34
# constants for some common errors to return from a query
QUERY_UNKNOWN_ENTRY_ERROR = (constants.CONFD_REPL_STATUS_ERROR,
                             constants.CONFD_ERROR_UNKNOWN_ENTRY)
QUERY_INTERNAL_ERROR = (constants.CONFD_REPL_STATUS_ERROR,
                        constants.CONFD_ERROR_INTERNAL)
Guido Trotter's avatar
Guido Trotter committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

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):
Guido Trotter's avatar
Guido Trotter committed
73
    """PingQuery main execution.
Guido Trotter's avatar
Guido Trotter committed
74
75
76
77
78
79
80
81
82
83
84

    """
    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

Guido Trotter's avatar
Guido Trotter committed
85
86

class NodeRoleQuery(ConfdQuery):
Guido Trotter's avatar
Guido Trotter committed
87
  """A query for the role of a node.
Guido Trotter's avatar
Guido Trotter committed
88

Guido Trotter's avatar
Guido Trotter committed
89
  It will return one of CONFD_NODE_ROLE_*, or an error for non-existing nodes.
Guido Trotter's avatar
Guido Trotter committed
90
91
92
93
94
95
96
97
98
99
100
101
102

  """
  def Exec(self, query):
    """EmptyQuery main execution

    """
    node = query
    if self.reader.GetMasterNode() == node:
      status = constants.CONFD_REPL_STATUS_OK
      answer = constants.CONFD_NODE_ROLE_MASTER
      return status, answer
    flags = self.reader.GetNodeStatusFlags(node)
    if flags is None:
103
      return QUERY_UNKNOWN_ENTRY_ERROR
Guido Trotter's avatar
Guido Trotter committed
104
105
106
107
108
109
110
111
112
113
114
115
116

    master_candidate, drained, offline = flags
    if master_candidate:
      answer = constants.CONFD_NODE_ROLE_CANDIDATE
    elif drained:
      answer = constants.CONFD_NODE_ROLE_DRAINED
    elif offline:
      answer = constants.CONFD_NODE_ROLE_OFFLINE
    else:
      answer = constants.CONFD_NODE_ROLE_REGULAR

    return constants.CONFD_REPL_STATUS_OK, answer

Guido Trotter's avatar
Guido Trotter committed
117
118

class InstanceIpToNodePrimaryIpQuery(ConfdQuery):
Guido Trotter's avatar
Guido Trotter committed
119
  """A query for the location of an instance's ip.
Guido Trotter's avatar
Guido Trotter committed
120

Guido Trotter's avatar
Guido Trotter committed
121
122
  It returns the primary ip of the node hosting the instance having the
  requested ip address, or an error if no such address is known.
Guido Trotter's avatar
Guido Trotter committed
123
124
125

  """
  def Exec(self, query):
Guido Trotter's avatar
Guido Trotter committed
126
    """InstanceIpToNodePrimaryIpQuery main execution.
Guido Trotter's avatar
Guido Trotter committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

    """
    instance_ip = query
    instance = self.reader.GetInstanceByIp(instance_ip)
    if instance is None:
      return QUERY_UNKNOWN_ENTRY_ERROR

    pnode = self.reader.GetInstancePrimaryNode(instance)
    if pnode is None:
      # this shouldn't happen
      logging.error("Internal configuration inconsistent (instance-to-pnode)")
      return QUERY_INTERNAL_ERROR

    pnode_primary_ip = self.reader.GetNodePrimaryIp(pnode)
    if pnode_primary_ip is None:
      # this shouldn't happen
      logging.error("Internal configuration inconsistent (node-to-primary-ip)")
      return QUERY_INTERNAL_ERROR

    return constants.CONFD_REPL_STATUS_OK, pnode_primary_ip