querylib.py 5.92 KB
Newer Older
Michael Hanselmann's avatar
Michael Hanselmann committed
1
#
Guido Trotter's avatar
Guido Trotter committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#

# 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

Michael Hanselmann's avatar
Michael Hanselmann committed
30

31
32
33
34
35
# 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
36

Michael Hanselmann's avatar
Michael Hanselmann committed
37

Guido Trotter's avatar
Guido Trotter committed
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
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.

71
72
  It will return success on an empty argument, and an error on any other
  argument.
Guido Trotter's avatar
Guido Trotter committed
73
74
75

  """
  def Exec(self, query):
Guido Trotter's avatar
Guido Trotter committed
76
    """PingQuery main execution.
Guido Trotter's avatar
Guido Trotter committed
77
78
79
80
81
82
83
84
85
86
87

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

Michael Hanselmann's avatar
Michael Hanselmann committed
88

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class ClusterMasterQuery(ConfdQuery):
  """Cluster master query.

  It accepts no arguments, and returns the current cluster master.

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

    """
    if query is None:
      status = constants.CONFD_REPL_STATUS_OK
      answer = self.reader.GetMasterNode()
    else:
      status = constants.CONFD_REPL_STATUS_ERROR
      answer = 'master query accepts no query argument'

    return status, answer

Guido Trotter's avatar
Guido Trotter committed
108
109

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

Guido Trotter's avatar
Guido Trotter committed
112
  It will return one of CONFD_NODE_ROLE_*, or an error for non-existing nodes.
Guido Trotter's avatar
Guido Trotter committed
113
114
115
116
117
118
119
120
121
122
123
124
125

  """
  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:
126
      return QUERY_UNKNOWN_ENTRY_ERROR
Guido Trotter's avatar
Guido Trotter committed
127
128
129
130
131
132
133
134
135
136
137
138
139

    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
140
141

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

Guido Trotter's avatar
Guido Trotter committed
144
145
  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
146
147
148

  """
  def Exec(self, query):
Guido Trotter's avatar
Guido Trotter committed
149
    """InstanceIpToNodePrimaryIpQuery main execution.
Guido Trotter's avatar
Guido Trotter committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

    """
    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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210


class NodesPipsQuery(ConfdQuery):
  """A query for nodes primary IPs.

  It returns the list of nodes primary IPs.

  """
  def Exec(self, query):
    """NodesPipsQuery main execution.

    """
    if query is None:
      status = constants.CONFD_REPL_STATUS_OK
      answer = self.reader.GetNodesPrimaryIps()
    else:
      status = constants.CONFD_REPL_STATUS_ERROR
      answer = "non-empty node primary IPs query"

    return status, answer


class MasterCandidatesPipsQuery(ConfdQuery):
  """A query for master candidates primary IPs.

  It returns the list of master candidates primary IPs.

  """
  def Exec(self, query):
    """MasterCandidatesPipsQuery main execution.

    """
    if query is None:
      status = constants.CONFD_REPL_STATUS_OK
      answer = self.reader.GetMasterCandidatesPrimaryIps()
    else:
      status = constants.CONFD_REPL_STATUS_ERROR
      answer = "non-empty master candidates primary IPs query"

    return status, answer

211
212
213
214

class InstancesIpsQuery(ConfdQuery):
  """A query for instances IPs.

215
216
  It returns the list of IPs of NICs connected to the requested link or all the
  instances IPs if no link is submitted.
217
218
219
220
221
222

  """
  def Exec(self, query):
    """InstancesIpsQuery main execution.

    """
223
224
225
    link = query
    status = constants.CONFD_REPL_STATUS_OK
    answer = self.reader.GetInstancesIps(link)
226
227
228

    return status, answer