| Branch: | Tag: | Revision:

root / lib / @ 13f1af63

History | View | Annotate | Download (245.9 kB)

1 2f31098c Iustin Pop
2 a8083063 Iustin Pop
3 a8083063 Iustin Pop
4 e7c6e02b Michael Hanselmann
# Copyright (C) 2006, 2007, 2008 Google Inc.
5 a8083063 Iustin Pop
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 880478f8 Iustin Pop
"""Module implementing the master-side code."""
23 a8083063 Iustin Pop
24 a8083063 Iustin Pop
# pylint: disable-msg=W0613,W0201
25 a8083063 Iustin Pop
26 a8083063 Iustin Pop
import os
27 a8083063 Iustin Pop
import os.path
28 a8083063 Iustin Pop
import time
29 a8083063 Iustin Pop
import tempfile
30 a8083063 Iustin Pop
import re
31 a8083063 Iustin Pop
import platform
32 ffa1c0dc Iustin Pop
import logging
33 74409b12 Iustin Pop
import copy
34 4b7735f9 Iustin Pop
import random
35 a8083063 Iustin Pop
36 a8083063 Iustin Pop
from ganeti import ssh
37 a8083063 Iustin Pop
from ganeti import utils
38 a8083063 Iustin Pop
from ganeti import errors
39 a8083063 Iustin Pop
from ganeti import hypervisor
40 6048c986 Guido Trotter
from ganeti import locking
41 a8083063 Iustin Pop
from ganeti import constants
42 a8083063 Iustin Pop
from ganeti import objects
43 a8083063 Iustin Pop
from ganeti import opcodes
44 8d14b30d Iustin Pop
from ganeti import serializer
45 112f18a5 Iustin Pop
from ganeti import ssconf
46 d61df03e Iustin Pop
47 d61df03e Iustin Pop
48 a8083063 Iustin Pop
class LogicalUnit(object):
49 396e1b78 Michael Hanselmann
  """Logical Unit base class.
50 a8083063 Iustin Pop

51 a8083063 Iustin Pop
  Subclasses must follow these rules:
52 d465bdc8 Guido Trotter
    - implement ExpandNames
53 d465bdc8 Guido Trotter
    - implement CheckPrereq
54 a8083063 Iustin Pop
    - implement Exec
55 a8083063 Iustin Pop
    - implement BuildHooksEnv
56 a8083063 Iustin Pop
    - redefine HPATH and HTYPE
57 05f86716 Guido Trotter
    - optionally redefine their run requirements:
58 7e55040e Guido Trotter
        REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
59 05f86716 Guido Trotter

60 05f86716 Guido Trotter
  Note that all commands require root permissions.
61 a8083063 Iustin Pop

62 a8083063 Iustin Pop
63 a8083063 Iustin Pop
  HPATH = None
64 a8083063 Iustin Pop
  HTYPE = None
65 a8083063 Iustin Pop
  _OP_REQP = []
66 7e55040e Guido Trotter
  REQ_BGL = True
67 a8083063 Iustin Pop
68 72737a7f Iustin Pop
  def __init__(self, processor, op, context, rpc):
69 a8083063 Iustin Pop
    """Constructor for LogicalUnit.
70 a8083063 Iustin Pop

71 a8083063 Iustin Pop
    This needs to be overriden in derived classes in order to check op
72 a8083063 Iustin Pop
73 a8083063 Iustin Pop

74 a8083063 Iustin Pop
75 5bfac263 Iustin Pop
    self.proc = processor
76 a8083063 Iustin Pop
    self.op = op
77 77b657a3 Guido Trotter
    self.cfg = context.cfg
78 77b657a3 Guido Trotter
    self.context = context
79 72737a7f Iustin Pop
    self.rpc = rpc
80 ca2a79e1 Guido Trotter
    # Dicts used to declare locking needs to mcpu
81 d465bdc8 Guido Trotter
    self.needed_locks = None
82 6683bba2 Guido Trotter
    self.acquired_locks = {}
83 3977a4c1 Guido Trotter
    self.share_locks = dict(((i, 0) for i in locking.LEVELS))
84 ca2a79e1 Guido Trotter
    self.add_locks = {}
85 ca2a79e1 Guido Trotter
    self.remove_locks = {}
86 c4a2fee1 Guido Trotter
    # Used to force good behavior when calling helper functions
87 c4a2fee1 Guido Trotter
    self.recalculate_locks = {}
88 c92b310a Michael Hanselmann
    self.__ssh = None
89 86d9d3bb Iustin Pop
    # logging
90 86d9d3bb Iustin Pop
    self.LogWarning = processor.LogWarning
91 86d9d3bb Iustin Pop
    self.LogInfo = processor.LogInfo
92 c92b310a Michael Hanselmann
93 a8083063 Iustin Pop
    for attr_name in self._OP_REQP:
94 a8083063 Iustin Pop
      attr_val = getattr(op, attr_name, None)
95 a8083063 Iustin Pop
      if attr_val is None:
96 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Required parameter '%s' missing" %
97 3ecf6786 Iustin Pop
98 4be4691d Iustin Pop
99 a8083063 Iustin Pop
100 c92b310a Michael Hanselmann
  def __GetSSH(self):
101 c92b310a Michael Hanselmann
    """Returns the SshRunner object
102 c92b310a Michael Hanselmann

103 c92b310a Michael Hanselmann
104 c92b310a Michael Hanselmann
    if not self.__ssh:
105 6b0469d2 Iustin Pop
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
106 c92b310a Michael Hanselmann
    return self.__ssh
107 c92b310a Michael Hanselmann
108 c92b310a Michael Hanselmann
  ssh = property(fget=__GetSSH)
109 c92b310a Michael Hanselmann
110 4be4691d Iustin Pop
  def CheckArguments(self):
111 4be4691d Iustin Pop
    """Check syntactic validity for the opcode arguments.
112 4be4691d Iustin Pop

113 4be4691d Iustin Pop
    This method is for doing a simple syntactic check and ensure
114 4be4691d Iustin Pop
    validity of opcode parameters, without any cluster-related
115 4be4691d Iustin Pop
    checks. While the same can be accomplished in ExpandNames and/or
116 4be4691d Iustin Pop
    CheckPrereq, doing these separate is better because:
117 4be4691d Iustin Pop

118 4be4691d Iustin Pop
      - ExpandNames is left as as purely a lock-related function
119 4be4691d Iustin Pop
      - CheckPrereq is run after we have aquired locks (and possible
120 4be4691d Iustin Pop
        waited for them)
121 4be4691d Iustin Pop

122 4be4691d Iustin Pop
    The function is allowed to change the self.op attribute so that
123 4be4691d Iustin Pop
    later methods can no longer worry about missing parameters.
124 4be4691d Iustin Pop

125 4be4691d Iustin Pop
126 4be4691d Iustin Pop
127 4be4691d Iustin Pop
128 d465bdc8 Guido Trotter
  def ExpandNames(self):
129 d465bdc8 Guido Trotter
    """Expand names for this LU.
130 d465bdc8 Guido Trotter

131 d465bdc8 Guido Trotter
    This method is called before starting to execute the opcode, and it should
132 d465bdc8 Guido Trotter
    update all the parameters of the opcode to their canonical form (e.g. a
133 d465bdc8 Guido Trotter
    short node name must be fully expanded after this method has successfully
134 d465bdc8 Guido Trotter
    completed). This way locking, hooks, logging, ecc. can work correctly.
135 d465bdc8 Guido Trotter

136 d465bdc8 Guido Trotter
    LUs which implement this method must also populate the self.needed_locks
137 d465bdc8 Guido Trotter
    member, as a dict with lock levels as keys, and a list of needed lock names
138 d465bdc8 Guido Trotter
    as values. Rules:
139 e4376078 Iustin Pop

140 e4376078 Iustin Pop
      - use an empty dict if you don't need any lock
141 e4376078 Iustin Pop
      - if you don't need any lock at a particular level omit that level
142 e4376078 Iustin Pop
      - don't put anything for the BGL level
143 e4376078 Iustin Pop
      - if you want all locks at a level use locking.ALL_SET as a value
144 d465bdc8 Guido Trotter

145 3977a4c1 Guido Trotter
    If you need to share locks (rather than acquire them exclusively) at one
146 3977a4c1 Guido Trotter
    level you can modify self.share_locks, setting a true value (usually 1) for
147 3977a4c1 Guido Trotter
    that level. By default locks are not shared.
148 3977a4c1 Guido Trotter

149 e4376078 Iustin Pop
150 e4376078 Iustin Pop

151 e4376078 Iustin Pop
      # Acquire all nodes and one instance
152 e4376078 Iustin Pop
      self.needed_locks = {
153 e4376078 Iustin Pop
        locking.LEVEL_NODE: locking.ALL_SET,
154 e4376078 Iustin Pop
        locking.LEVEL_INSTANCE: ['instance1.example.tld'],
155 e4376078 Iustin Pop
156 e4376078 Iustin Pop
      # Acquire just two nodes
157 e4376078 Iustin Pop
      self.needed_locks = {
158 e4376078 Iustin Pop
        locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
159 e4376078 Iustin Pop
160 e4376078 Iustin Pop
      # Acquire no locks
161 e4376078 Iustin Pop
      self.needed_locks = {} # No, you can't leave it to the default value None
162 d465bdc8 Guido Trotter

163 d465bdc8 Guido Trotter
164 d465bdc8 Guido Trotter
    # The implementation of this method is mandatory only if the new LU is
165 d465bdc8 Guido Trotter
    # concurrent, so that old LUs don't need to be changed all at the same
166 d465bdc8 Guido Trotter
    # time.
167 d465bdc8 Guido Trotter
    if self.REQ_BGL:
168 d465bdc8 Guido Trotter
      self.needed_locks = {} # Exclusive LUs don't need locks.
169 d465bdc8 Guido Trotter
170 d465bdc8 Guido Trotter
      raise NotImplementedError
171 d465bdc8 Guido Trotter
172 fb8dcb62 Guido Trotter
  def DeclareLocks(self, level):
173 fb8dcb62 Guido Trotter
    """Declare LU locking needs for a level
174 fb8dcb62 Guido Trotter

175 fb8dcb62 Guido Trotter
    While most LUs can just declare their locking needs at ExpandNames time,
176 fb8dcb62 Guido Trotter
    sometimes there's the need to calculate some locks after having acquired
177 fb8dcb62 Guido Trotter
    the ones before. This function is called just before acquiring locks at a
178 fb8dcb62 Guido Trotter
    particular level, but after acquiring the ones at lower levels, and permits
179 fb8dcb62 Guido Trotter
    such calculations. It can be used to modify self.needed_locks, and by
180 fb8dcb62 Guido Trotter
    default it does nothing.
181 fb8dcb62 Guido Trotter

182 fb8dcb62 Guido Trotter
    This function is only called if you have something already set in
183 fb8dcb62 Guido Trotter
    self.needed_locks for the level.
184 fb8dcb62 Guido Trotter

185 fb8dcb62 Guido Trotter
    @param level: Locking level which is going to be locked
186 fb8dcb62 Guido Trotter
    @type level: member of ganeti.locking.LEVELS
187 fb8dcb62 Guido Trotter

188 fb8dcb62 Guido Trotter
189 fb8dcb62 Guido Trotter
190 a8083063 Iustin Pop
  def CheckPrereq(self):
191 a8083063 Iustin Pop
    """Check prerequisites for this LU.
192 a8083063 Iustin Pop

193 a8083063 Iustin Pop
    This method should check that the prerequisites for the execution
194 a8083063 Iustin Pop
    of this LU are fulfilled. It can do internode communication, but
195 a8083063 Iustin Pop
    it should be idempotent - no cluster or system changes are
196 a8083063 Iustin Pop
197 a8083063 Iustin Pop

198 a8083063 Iustin Pop
    The method should raise errors.OpPrereqError in case something is
199 a8083063 Iustin Pop
    not fulfilled. Its return value is ignored.
200 a8083063 Iustin Pop

201 a8083063 Iustin Pop
    This method should also update all the parameters of the opcode to
202 d465bdc8 Guido Trotter
    their canonical form if it hasn't been done by ExpandNames before.
203 a8083063 Iustin Pop

204 a8083063 Iustin Pop
205 a8083063 Iustin Pop
    raise NotImplementedError
206 a8083063 Iustin Pop
207 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
208 a8083063 Iustin Pop
    """Execute the LU.
209 a8083063 Iustin Pop

210 a8083063 Iustin Pop
    This method should implement the actual work. It should raise
211 a8083063 Iustin Pop
    errors.OpExecError for failures that are somewhat dealt with in
212 a8083063 Iustin Pop
    code, or expected.
213 a8083063 Iustin Pop

214 a8083063 Iustin Pop
215 a8083063 Iustin Pop
    raise NotImplementedError
216 a8083063 Iustin Pop
217 a8083063 Iustin Pop
  def BuildHooksEnv(self):
218 a8083063 Iustin Pop
    """Build hooks environment for this LU.
219 a8083063 Iustin Pop

220 a8083063 Iustin Pop
    This method should return a three-node tuple consisting of: a dict
221 a8083063 Iustin Pop
    containing the environment that will be used for running the
222 a8083063 Iustin Pop
    specific hook for this LU, a list of node names on which the hook
223 a8083063 Iustin Pop
    should run before the execution, and a list of node names on which
224 a8083063 Iustin Pop
    the hook should run after the execution.
225 a8083063 Iustin Pop

226 a8083063 Iustin Pop
    The keys of the dict must not have 'GANETI_' prefixed as this will
227 a8083063 Iustin Pop
    be handled in the hooks runner. Also note additional keys will be
228 a8083063 Iustin Pop
    added by the hooks runner. If the LU doesn't define any
229 a8083063 Iustin Pop
    environment, an empty dict (and not None) should be returned.
230 a8083063 Iustin Pop

231 8a3fe350 Guido Trotter
    No nodes should be returned as an empty list (and not None).
232 a8083063 Iustin Pop

233 a8083063 Iustin Pop
    Note that if the HPATH for a LU class is None, this function will
234 a8083063 Iustin Pop
    not be called.
235 a8083063 Iustin Pop

236 a8083063 Iustin Pop
237 a8083063 Iustin Pop
    raise NotImplementedError
238 a8083063 Iustin Pop
239 1fce5219 Guido Trotter
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
240 1fce5219 Guido Trotter
    """Notify the LU about the results of its hooks.
241 1fce5219 Guido Trotter

242 1fce5219 Guido Trotter
    This method is called every time a hooks phase is executed, and notifies
243 1fce5219 Guido Trotter
    the Logical Unit about the hooks' result. The LU can then use it to alter
244 1fce5219 Guido Trotter
    its result based on the hooks.  By default the method does nothing and the
245 1fce5219 Guido Trotter
    previous result is passed back unchanged but any LU can define it if it
246 1fce5219 Guido Trotter
    wants to use the local cluster hook-scripts somehow.
247 1fce5219 Guido Trotter

248 e4376078 Iustin Pop
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
249 e4376078 Iustin Pop
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
250 e4376078 Iustin Pop
    @param hook_results: the results of the multi-node hooks rpc call
251 e4376078 Iustin Pop
    @param feedback_fn: function used send feedback back to the caller
252 e4376078 Iustin Pop
    @param lu_result: the previous Exec result this LU had, or None
253 e4376078 Iustin Pop
        in the PRE phase
254 e4376078 Iustin Pop
    @return: the new Exec result, based on the previous result
255 e4376078 Iustin Pop
        and hook results
256 1fce5219 Guido Trotter

257 1fce5219 Guido Trotter
258 1fce5219 Guido Trotter
    return lu_result
259 1fce5219 Guido Trotter
260 43905206 Guido Trotter
  def _ExpandAndLockInstance(self):
261 43905206 Guido Trotter
    """Helper function to expand and lock an instance.
262 43905206 Guido Trotter

263 43905206 Guido Trotter
    Many LUs that work on an instance take its name in self.op.instance_name
264 43905206 Guido Trotter
    and need to expand it and then declare the expanded name for locking. This
265 43905206 Guido Trotter
    function does it, and then updates self.op.instance_name to the expanded
266 43905206 Guido Trotter
    name. It also initializes needed_locks as a dict, if this hasn't been done
267 43905206 Guido Trotter
268 43905206 Guido Trotter

269 43905206 Guido Trotter
270 43905206 Guido Trotter
    if self.needed_locks is None:
271 43905206 Guido Trotter
      self.needed_locks = {}
272 43905206 Guido Trotter
273 43905206 Guido Trotter
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
274 43905206 Guido Trotter
        "_ExpandAndLockInstance called with instance-level locks set"
275 43905206 Guido Trotter
    expanded_name = self.cfg.ExpandInstanceName(self.op.instance_name)
276 43905206 Guido Trotter
    if expanded_name is None:
277 43905206 Guido Trotter
      raise errors.OpPrereqError("Instance '%s' not known" %
278 43905206 Guido Trotter
279 43905206 Guido Trotter
    self.needed_locks[locking.LEVEL_INSTANCE] = expanded_name
280 43905206 Guido Trotter
    self.op.instance_name = expanded_name
281 43905206 Guido Trotter
282 a82ce292 Guido Trotter
  def _LockInstancesNodes(self, primary_only=False):
283 c4a2fee1 Guido Trotter
    """Helper function to declare instances' nodes for locking.
284 c4a2fee1 Guido Trotter

285 c4a2fee1 Guido Trotter
    This function should be called after locking one or more instances to lock
286 c4a2fee1 Guido Trotter
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
287 c4a2fee1 Guido Trotter
    with all primary or secondary nodes for instances already locked and
288 c4a2fee1 Guido Trotter
    present in self.needed_locks[locking.LEVEL_INSTANCE].
289 c4a2fee1 Guido Trotter

290 c4a2fee1 Guido Trotter
    It should be called from DeclareLocks, and for safety only works if
291 c4a2fee1 Guido Trotter
    self.recalculate_locks[locking.LEVEL_NODE] is set.
292 c4a2fee1 Guido Trotter

293 c4a2fee1 Guido Trotter
    In the future it may grow parameters to just lock some instance's nodes, or
294 c4a2fee1 Guido Trotter
    to just lock primaries or secondary nodes, if needed.
295 c4a2fee1 Guido Trotter

296 e4376078 Iustin Pop
    If should be called in DeclareLocks in a way similar to::
297 c4a2fee1 Guido Trotter

298 e4376078 Iustin Pop
      if level == locking.LEVEL_NODE:
299 e4376078 Iustin Pop
300 c4a2fee1 Guido Trotter

301 a82ce292 Guido Trotter
    @type primary_only: boolean
302 a82ce292 Guido Trotter
    @param primary_only: only lock primary nodes of locked instances
303 a82ce292 Guido Trotter

304 c4a2fee1 Guido Trotter
305 c4a2fee1 Guido Trotter
    assert locking.LEVEL_NODE in self.recalculate_locks, \
306 c4a2fee1 Guido Trotter
      "_LockInstancesNodes helper function called with no nodes to recalculate"
307 c4a2fee1 Guido Trotter
308 c4a2fee1 Guido Trotter
    # TODO: check if we're really been called with the instance locks held
309 c4a2fee1 Guido Trotter
310 c4a2fee1 Guido Trotter
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
311 c4a2fee1 Guido Trotter
    # future we might want to have different behaviors depending on the value
312 c4a2fee1 Guido Trotter
    # of self.recalculate_locks[locking.LEVEL_NODE]
313 c4a2fee1 Guido Trotter
    wanted_nodes = []
314 6683bba2 Guido Trotter
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
315 c4a2fee1 Guido Trotter
      instance = self.context.cfg.GetInstanceInfo(instance_name)
316 c4a2fee1 Guido Trotter
317 a82ce292 Guido Trotter
      if not primary_only:
318 a82ce292 Guido Trotter
319 9513b6ab Guido Trotter
320 9513b6ab Guido Trotter
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
321 9513b6ab Guido Trotter
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
322 9513b6ab Guido Trotter
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
323 9513b6ab Guido Trotter
324 c4a2fee1 Guido Trotter
325 c4a2fee1 Guido Trotter
    del self.recalculate_locks[locking.LEVEL_NODE]
326 c4a2fee1 Guido Trotter
327 a8083063 Iustin Pop
328 a8083063 Iustin Pop
class NoHooksLU(LogicalUnit):
329 a8083063 Iustin Pop
  """Simple LU which runs no hooks.
330 a8083063 Iustin Pop

331 a8083063 Iustin Pop
  This LU is intended as a parent for other LogicalUnits which will
332 a8083063 Iustin Pop
  run no hooks, in order to reduce duplicate code.
333 a8083063 Iustin Pop

334 a8083063 Iustin Pop
335 a8083063 Iustin Pop
  HPATH = None
336 a8083063 Iustin Pop
  HTYPE = None
337 a8083063 Iustin Pop
338 a8083063 Iustin Pop
339 dcb93971 Michael Hanselmann
def _GetWantedNodes(lu, nodes):
340 a7ba5e53 Iustin Pop
  """Returns list of checked and expanded node names.
341 83120a01 Michael Hanselmann

342 e4376078 Iustin Pop
  @type lu: L{LogicalUnit}
343 e4376078 Iustin Pop
  @param lu: the logical unit on whose behalf we execute
344 e4376078 Iustin Pop
  @type nodes: list
345 e4376078 Iustin Pop
  @param nodes: list of node names or None for all nodes
346 e4376078 Iustin Pop
  @rtype: list
347 e4376078 Iustin Pop
  @return: the list of nodes, sorted
348 e4376078 Iustin Pop
  @raise errors.OpProgrammerError: if the nodes parameter is wrong type
349 83120a01 Michael Hanselmann

350 83120a01 Michael Hanselmann
351 3312b702 Iustin Pop
  if not isinstance(nodes, list):
352 3ecf6786 Iustin Pop
    raise errors.OpPrereqError("Invalid argument type 'nodes'")
353 dcb93971 Michael Hanselmann
354 ea47808a Guido Trotter
  if not nodes:
355 ea47808a Guido Trotter
    raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
356 ea47808a Guido Trotter
      " non-empty list of nodes whose name is to be expanded.")
357 dcb93971 Michael Hanselmann
358 ea47808a Guido Trotter
  wanted = []
359 ea47808a Guido Trotter
  for name in nodes:
360 ea47808a Guido Trotter
    node = lu.cfg.ExpandNodeName(name)
361 ea47808a Guido Trotter
    if node is None:
362 ea47808a Guido Trotter
      raise errors.OpPrereqError("No such node name '%s'" % name)
363 ea47808a Guido Trotter
364 dcb93971 Michael Hanselmann
365 a7ba5e53 Iustin Pop
  return utils.NiceSort(wanted)
366 3312b702 Iustin Pop
367 3312b702 Iustin Pop
368 3312b702 Iustin Pop
def _GetWantedInstances(lu, instances):
369 a7ba5e53 Iustin Pop
  """Returns list of checked and expanded instance names.
370 3312b702 Iustin Pop

371 e4376078 Iustin Pop
  @type lu: L{LogicalUnit}
372 e4376078 Iustin Pop
  @param lu: the logical unit on whose behalf we execute
373 e4376078 Iustin Pop
  @type instances: list
374 e4376078 Iustin Pop
  @param instances: list of instance names or None for all instances
375 e4376078 Iustin Pop
  @rtype: list
376 e4376078 Iustin Pop
  @return: the list of instances, sorted
377 e4376078 Iustin Pop
  @raise errors.OpPrereqError: if the instances parameter is wrong type
378 e4376078 Iustin Pop
  @raise errors.OpPrereqError: if any of the passed instances is not found
379 3312b702 Iustin Pop

380 3312b702 Iustin Pop
381 3312b702 Iustin Pop
  if not isinstance(instances, list):
382 3312b702 Iustin Pop
    raise errors.OpPrereqError("Invalid argument type 'instances'")
383 3312b702 Iustin Pop
384 3312b702 Iustin Pop
  if instances:
385 3312b702 Iustin Pop
    wanted = []
386 3312b702 Iustin Pop
387 3312b702 Iustin Pop
    for name in instances:
388 a7ba5e53 Iustin Pop
      instance = lu.cfg.ExpandInstanceName(name)
389 3312b702 Iustin Pop
      if instance is None:
390 3312b702 Iustin Pop
        raise errors.OpPrereqError("No such instance name '%s'" % name)
391 3312b702 Iustin Pop
392 3312b702 Iustin Pop
393 3312b702 Iustin Pop
394 a7f5dc98 Iustin Pop
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
395 a7f5dc98 Iustin Pop
  return wanted
396 dcb93971 Michael Hanselmann
397 dcb93971 Michael Hanselmann
398 dcb93971 Michael Hanselmann
def _CheckOutputFields(static, dynamic, selected):
399 83120a01 Michael Hanselmann
  """Checks whether all selected fields are valid.
400 83120a01 Michael Hanselmann

401 a2d2e1a7 Iustin Pop
  @type static: L{utils.FieldSet}
402 31bf511f Iustin Pop
  @param static: static fields set
403 a2d2e1a7 Iustin Pop
  @type dynamic: L{utils.FieldSet}
404 31bf511f Iustin Pop
  @param dynamic: dynamic fields set
405 83120a01 Michael Hanselmann

406 83120a01 Michael Hanselmann
407 a2d2e1a7 Iustin Pop
  f = utils.FieldSet()
408 31bf511f Iustin Pop
409 31bf511f Iustin Pop
410 dcb93971 Michael Hanselmann
411 31bf511f Iustin Pop
  delta = f.NonMatching(selected)
412 31bf511f Iustin Pop
  if delta:
413 3ecf6786 Iustin Pop
    raise errors.OpPrereqError("Unknown output fields selected: %s"
414 31bf511f Iustin Pop
                               % ",".join(delta))
415 dcb93971 Michael Hanselmann
416 dcb93971 Michael Hanselmann
417 a5961235 Iustin Pop
def _CheckBooleanOpField(op, name):
418 a5961235 Iustin Pop
  """Validates boolean opcode parameters.
419 a5961235 Iustin Pop

420 a5961235 Iustin Pop
  This will ensure that an opcode parameter is either a boolean value,
421 a5961235 Iustin Pop
  or None (but that it always exists).
422 a5961235 Iustin Pop

423 a5961235 Iustin Pop
424 a5961235 Iustin Pop
  val = getattr(op, name, None)
425 a5961235 Iustin Pop
  if not (val is None or isinstance(val, bool)):
426 a5961235 Iustin Pop
    raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" %
427 a5961235 Iustin Pop
                               (name, str(val)))
428 a5961235 Iustin Pop
  setattr(op, name, val)
429 a5961235 Iustin Pop
430 a5961235 Iustin Pop
431 a5961235 Iustin Pop
def _CheckNodeOnline(lu, node):
432 a5961235 Iustin Pop
  """Ensure that a given node is online.
433 a5961235 Iustin Pop

434 a5961235 Iustin Pop
  @param lu: the LU on behalf of which we make the check
435 a5961235 Iustin Pop
  @param node: the node to check
436 733a2b6a Iustin Pop
  @raise errors.OpPrereqError: if the node is offline
437 a5961235 Iustin Pop

438 a5961235 Iustin Pop
439 a5961235 Iustin Pop
  if lu.cfg.GetNodeInfo(node).offline:
440 a5961235 Iustin Pop
    raise errors.OpPrereqError("Can't use offline node %s" % node)
441 a5961235 Iustin Pop
442 a5961235 Iustin Pop
443 733a2b6a Iustin Pop
def _CheckNodeNotDrained(lu, node):
444 733a2b6a Iustin Pop
  """Ensure that a given node is not drained.
445 733a2b6a Iustin Pop

446 733a2b6a Iustin Pop
  @param lu: the LU on behalf of which we make the check
447 733a2b6a Iustin Pop
  @param node: the node to check
448 733a2b6a Iustin Pop
  @raise errors.OpPrereqError: if the node is drained
449 733a2b6a Iustin Pop

450 733a2b6a Iustin Pop
451 733a2b6a Iustin Pop
  if lu.cfg.GetNodeInfo(node).drained:
452 733a2b6a Iustin Pop
    raise errors.OpPrereqError("Can't use drained node %s" % node)
453 733a2b6a Iustin Pop
454 733a2b6a Iustin Pop
455 ecb215b5 Michael Hanselmann
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
456 2c2690c9 Iustin Pop
                          memory, vcpus, nics, disk_template, disks):
457 e4376078 Iustin Pop
  """Builds instance related env variables for hooks
458 e4376078 Iustin Pop

459 e4376078 Iustin Pop
  This builds the hook environment from individual variables.
460 e4376078 Iustin Pop

461 e4376078 Iustin Pop
  @type name: string
462 e4376078 Iustin Pop
  @param name: the name of the instance
463 e4376078 Iustin Pop
  @type primary_node: string
464 e4376078 Iustin Pop
  @param primary_node: the name of the instance's primary node
465 e4376078 Iustin Pop
  @type secondary_nodes: list
466 e4376078 Iustin Pop
  @param secondary_nodes: list of secondary nodes as strings
467 e4376078 Iustin Pop
  @type os_type: string
468 e4376078 Iustin Pop
  @param os_type: the name of the instance's OS
469 0d68c45d Iustin Pop
  @type status: boolean
470 0d68c45d Iustin Pop
  @param status: the should_run status of the instance
471 e4376078 Iustin Pop
  @type memory: string
472 e4376078 Iustin Pop
  @param memory: the memory size of the instance
473 e4376078 Iustin Pop
  @type vcpus: string
474 e4376078 Iustin Pop
  @param vcpus: the count of VCPUs the instance has
475 e4376078 Iustin Pop
  @type nics: list
476 e4376078 Iustin Pop
  @param nics: list of tuples (ip, bridge, mac) representing
477 e4376078 Iustin Pop
      the NICs the instance  has
478 2c2690c9 Iustin Pop
  @type disk_template: string
479 2c2690c9 Iustin Pop
  @param disk_template: the distk template of the instance
480 2c2690c9 Iustin Pop
  @type disks: list
481 2c2690c9 Iustin Pop
  @param disks: the list of (size, mode) pairs
482 e4376078 Iustin Pop
  @rtype: dict
483 e4376078 Iustin Pop
  @return: the hook environment for this instance
484 ecb215b5 Michael Hanselmann

485 396e1b78 Michael Hanselmann
486 0d68c45d Iustin Pop
  if status:
487 0d68c45d Iustin Pop
    str_status = "up"
488 0d68c45d Iustin Pop
489 0d68c45d Iustin Pop
    str_status = "down"
490 396e1b78 Michael Hanselmann
  env = {
491 0e137c28 Iustin Pop
    "OP_TARGET": name,
492 396e1b78 Michael Hanselmann
    "INSTANCE_NAME": name,
493 396e1b78 Michael Hanselmann
    "INSTANCE_PRIMARY": primary_node,
494 396e1b78 Michael Hanselmann
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
495 ecb215b5 Michael Hanselmann
    "INSTANCE_OS_TYPE": os_type,
496 0d68c45d Iustin Pop
    "INSTANCE_STATUS": str_status,
497 396e1b78 Michael Hanselmann
    "INSTANCE_MEMORY": memory,
498 396e1b78 Michael Hanselmann
    "INSTANCE_VCPUS": vcpus,
499 2c2690c9 Iustin Pop
    "INSTANCE_DISK_TEMPLATE": disk_template,
500 396e1b78 Michael Hanselmann
501 396e1b78 Michael Hanselmann
502 396e1b78 Michael Hanselmann
  if nics:
503 396e1b78 Michael Hanselmann
    nic_count = len(nics)
504 53e4e875 Guido Trotter
    for idx, (ip, bridge, mac) in enumerate(nics):
505 396e1b78 Michael Hanselmann
      if ip is None:
506 396e1b78 Michael Hanselmann
        ip = ""
507 396e1b78 Michael Hanselmann
      env["INSTANCE_NIC%d_IP" % idx] = ip
508 396e1b78 Michael Hanselmann
      env["INSTANCE_NIC%d_BRIDGE" % idx] = bridge
509 2c2690c9 Iustin Pop
      env["INSTANCE_NIC%d_MAC" % idx] = mac
510 396e1b78 Michael Hanselmann
511 396e1b78 Michael Hanselmann
    nic_count = 0
512 396e1b78 Michael Hanselmann
513 396e1b78 Michael Hanselmann
  env["INSTANCE_NIC_COUNT"] = nic_count
514 396e1b78 Michael Hanselmann
515 2c2690c9 Iustin Pop
  if disks:
516 2c2690c9 Iustin Pop
    disk_count = len(disks)
517 2c2690c9 Iustin Pop
    for idx, (size, mode) in enumerate(disks):
518 2c2690c9 Iustin Pop
      env["INSTANCE_DISK%d_SIZE" % idx] = size
519 2c2690c9 Iustin Pop
      env["INSTANCE_DISK%d_MODE" % idx] = mode
520 2c2690c9 Iustin Pop
521 2c2690c9 Iustin Pop
    disk_count = 0
522 2c2690c9 Iustin Pop
523 2c2690c9 Iustin Pop
  env["INSTANCE_DISK_COUNT"] = disk_count
524 2c2690c9 Iustin Pop
525 396e1b78 Michael Hanselmann
  return env
526 396e1b78 Michael Hanselmann
527 396e1b78 Michael Hanselmann
528 338e51e8 Iustin Pop
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
529 ecb215b5 Michael Hanselmann
  """Builds instance related env variables for hooks from an object.
530 ecb215b5 Michael Hanselmann

531 e4376078 Iustin Pop
  @type lu: L{LogicalUnit}
532 e4376078 Iustin Pop
  @param lu: the logical unit on whose behalf we execute
533 e4376078 Iustin Pop
  @type instance: L{objects.Instance}
534 e4376078 Iustin Pop
  @param instance: the instance for which we should build the
535 e4376078 Iustin Pop
536 e4376078 Iustin Pop
  @type override: dict
537 e4376078 Iustin Pop
  @param override: dictionary with key/values that will override
538 e4376078 Iustin Pop
      our values
539 e4376078 Iustin Pop
  @rtype: dict
540 e4376078 Iustin Pop
  @return: the hook environment dictionary
541 e4376078 Iustin Pop

542 ecb215b5 Michael Hanselmann
543 338e51e8 Iustin Pop
  bep = lu.cfg.GetClusterInfo().FillBE(instance)
544 396e1b78 Michael Hanselmann
  args = {
545 396e1b78 Michael Hanselmann
546 396e1b78 Michael Hanselmann
    'primary_node': instance.primary_node,
547 396e1b78 Michael Hanselmann
    'secondary_nodes': instance.secondary_nodes,
548 ecb215b5 Michael Hanselmann
    'os_type': instance.os,
549 0d68c45d Iustin Pop
    'status': instance.admin_up,
550 338e51e8 Iustin Pop
    'memory': bep[constants.BE_MEMORY],
551 338e51e8 Iustin Pop
    'vcpus': bep[constants.BE_VCPUS],
552 53e4e875 Guido Trotter
    'nics': [(nic.ip, nic.bridge, nic.mac) for nic in instance.nics],
553 2c2690c9 Iustin Pop
    'disk_template': instance.disk_template,
554 2c2690c9 Iustin Pop
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
555 396e1b78 Michael Hanselmann
556 396e1b78 Michael Hanselmann
  if override:
557 396e1b78 Michael Hanselmann
558 396e1b78 Michael Hanselmann
  return _BuildInstanceHookEnv(**args)
559 396e1b78 Michael Hanselmann
560 396e1b78 Michael Hanselmann
561 ec0292f1 Iustin Pop
def _AdjustCandidatePool(lu):
562 ec0292f1 Iustin Pop
  """Adjust the candidate pool after node operations.
563 ec0292f1 Iustin Pop

564 ec0292f1 Iustin Pop
565 ec0292f1 Iustin Pop
  mod_list = lu.cfg.MaintainCandidatePool()
566 ec0292f1 Iustin Pop
  if mod_list:
567 ec0292f1 Iustin Pop
    lu.LogInfo("Promoted nodes to master candidate role: %s",
568 ee513a66 Iustin Pop
               ", ".join( for node in mod_list))
569 ec0292f1 Iustin Pop
    for name in mod_list:
570 ec0292f1 Iustin Pop
571 ec0292f1 Iustin Pop
  mc_now, mc_max = lu.cfg.GetMasterCandidateStats()
572 ec0292f1 Iustin Pop
  if mc_now > mc_max:
573 ec0292f1 Iustin Pop
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
574 ec0292f1 Iustin Pop
               (mc_now, mc_max))
575 ec0292f1 Iustin Pop
576 ec0292f1 Iustin Pop
577 b9bddb6b Iustin Pop
def _CheckInstanceBridgesExist(lu, instance):
578 bf6929a2 Alexander Schreiber
  """Check that the brigdes needed by an instance exist.
579 bf6929a2 Alexander Schreiber

580 bf6929a2 Alexander Schreiber
581 bf6929a2 Alexander Schreiber
  # check bridges existance
582 bf6929a2 Alexander Schreiber
  brlist = [nic.bridge for nic in instance.nics]
583 781de953 Iustin Pop
  result = lu.rpc.call_bridges_exist(instance.primary_node, brlist)
584 781de953 Iustin Pop
585 781de953 Iustin Pop
  if not
586 781de953 Iustin Pop
    raise errors.OpPrereqError("One or more target bridges %s does not"
587 bf6929a2 Alexander Schreiber
                               " exist on destination node '%s'" %
588 bf6929a2 Alexander Schreiber
                               (brlist, instance.primary_node))
589 bf6929a2 Alexander Schreiber
590 bf6929a2 Alexander Schreiber
591 a8083063 Iustin Pop
class LUDestroyCluster(NoHooksLU):
592 a8083063 Iustin Pop
  """Logical unit for destroying the cluster.
593 a8083063 Iustin Pop

594 a8083063 Iustin Pop
595 a8083063 Iustin Pop
  _OP_REQP = []
596 a8083063 Iustin Pop
597 a8083063 Iustin Pop
  def CheckPrereq(self):
598 a8083063 Iustin Pop
    """Check prerequisites.
599 a8083063 Iustin Pop

600 a8083063 Iustin Pop
    This checks whether the cluster is empty.
601 a8083063 Iustin Pop

602 a8083063 Iustin Pop
    Any errors are signalled by raising errors.OpPrereqError.
603 a8083063 Iustin Pop

604 a8083063 Iustin Pop
605 d6a02168 Michael Hanselmann
    master = self.cfg.GetMasterNode()
606 a8083063 Iustin Pop
607 a8083063 Iustin Pop
    nodelist = self.cfg.GetNodeList()
608 db915bd1 Michael Hanselmann
    if len(nodelist) != 1 or nodelist[0] != master:
609 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("There are still %d node(s) in"
610 3ecf6786 Iustin Pop
                                 " this cluster." % (len(nodelist) - 1))
611 db915bd1 Michael Hanselmann
    instancelist = self.cfg.GetInstanceList()
612 db915bd1 Michael Hanselmann
    if instancelist:
613 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("There are still %d instance(s) in"
614 3ecf6786 Iustin Pop
                                 " this cluster." % len(instancelist))
615 a8083063 Iustin Pop
616 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
617 a8083063 Iustin Pop
    """Destroys the cluster.
618 a8083063 Iustin Pop

619 a8083063 Iustin Pop
620 d6a02168 Michael Hanselmann
    master = self.cfg.GetMasterNode()
621 781de953 Iustin Pop
    result = self.rpc.call_node_stop_master(master, False)
622 781de953 Iustin Pop
623 781de953 Iustin Pop
    if not
624 c9064964 Iustin Pop
      raise errors.OpExecError("Could not disable the master role")
625 70d9e3d8 Iustin Pop
    priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
626 70d9e3d8 Iustin Pop
627 70d9e3d8 Iustin Pop
628 140aa4a8 Iustin Pop
    return master
629 a8083063 Iustin Pop
630 a8083063 Iustin Pop
631 d8fff41c Guido Trotter
class LUVerifyCluster(LogicalUnit):
632 a8083063 Iustin Pop
  """Verifies the cluster status.
633 a8083063 Iustin Pop

634 a8083063 Iustin Pop
635 d8fff41c Guido Trotter
  HPATH = "cluster-verify"
636 d8fff41c Guido Trotter
637 e54c4c5e Guido Trotter
  _OP_REQP = ["skip_checks"]
638 d4b9d97f Guido Trotter
  REQ_BGL = False
639 d4b9d97f Guido Trotter
640 d4b9d97f Guido Trotter
  def ExpandNames(self):
641 d4b9d97f Guido Trotter
    self.needed_locks = {
642 d4b9d97f Guido Trotter
      locking.LEVEL_NODE: locking.ALL_SET,
643 d4b9d97f Guido Trotter
      locking.LEVEL_INSTANCE: locking.ALL_SET,
644 d4b9d97f Guido Trotter
645 d4b9d97f Guido Trotter
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
646 a8083063 Iustin Pop
647 25361b9a Iustin Pop
  def _VerifyNode(self, nodeinfo, file_list, local_cksum,
648 6d2e83d5 Iustin Pop
                  node_result, feedback_fn, master_files,
649 cc9e1230 Guido Trotter
                  drbd_map, vg_name):
650 a8083063 Iustin Pop
    """Run multiple tests against a node.
651 a8083063 Iustin Pop

652 112f18a5 Iustin Pop
    Test list:
653 e4376078 Iustin Pop

654 a8083063 Iustin Pop
      - compares ganeti version
655 a8083063 Iustin Pop
      - checks vg existance and size > 20G
656 a8083063 Iustin Pop
      - checks config file checksum
657 a8083063 Iustin Pop
      - checks ssh to other nodes
658 a8083063 Iustin Pop

659 112f18a5 Iustin Pop
    @type nodeinfo: L{objects.Node}
660 112f18a5 Iustin Pop
    @param nodeinfo: the node to check
661 e4376078 Iustin Pop
    @param file_list: required list of files
662 e4376078 Iustin Pop
    @param local_cksum: dictionary of local files and their checksums
663 e4376078 Iustin Pop
    @param node_result: the results from the node
664 e4376078 Iustin Pop
    @param feedback_fn: function used to accumulate results
665 112f18a5 Iustin Pop
    @param master_files: list of files that only masters should have
666 6d2e83d5 Iustin Pop
    @param drbd_map: the useddrbd minors for this node, in
667 6d2e83d5 Iustin Pop
        form of minor: (instance, must_exist) which correspond to instances
668 6d2e83d5 Iustin Pop
        and their running status
669 cc9e1230 Guido Trotter
    @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
670 098c0958 Michael Hanselmann

671 a8083063 Iustin Pop
672 112f18a5 Iustin Pop
    node =
673 25361b9a Iustin Pop
674 25361b9a Iustin Pop
    # main result, node_result should be a non-empty dict
675 25361b9a Iustin Pop
    if not node_result or not isinstance(node_result, dict):
676 25361b9a Iustin Pop
      feedback_fn("  - ERROR: unable to verify node %s." % (node,))
677 25361b9a Iustin Pop
      return True
678 25361b9a Iustin Pop
679 a8083063 Iustin Pop
    # compares ganeti version
680 a8083063 Iustin Pop
    local_version = constants.PROTOCOL_VERSION
681 25361b9a Iustin Pop
    remote_version = node_result.get('version', None)
682 e9ce0a64 Iustin Pop
    if not (remote_version and isinstance(remote_version, (list, tuple)) and
683 e9ce0a64 Iustin Pop
            len(remote_version) == 2):
684 c840ae6f Guido Trotter
      feedback_fn("  - ERROR: connection to %s failed" % (node))
685 a8083063 Iustin Pop
      return True
686 a8083063 Iustin Pop
687 e9ce0a64 Iustin Pop
    if local_version != remote_version[0]:
688 e9ce0a64 Iustin Pop
      feedback_fn("  - ERROR: incompatible protocol versions: master %s,"
689 e9ce0a64 Iustin Pop
                  " node %s %s" % (local_version, node, remote_version[0]))
690 a8083063 Iustin Pop
      return True
691 a8083063 Iustin Pop
692 e9ce0a64 Iustin Pop
    # node seems compatible, we can actually try to look into its results
693 a8083063 Iustin Pop
694 a8083063 Iustin Pop
    bad = False
695 e9ce0a64 Iustin Pop
696 e9ce0a64 Iustin Pop
    # full package version
697 e9ce0a64 Iustin Pop
    if constants.RELEASE_VERSION != remote_version[1]:
698 e9ce0a64 Iustin Pop
      feedback_fn("  - WARNING: software version mismatch: master %s,"
699 e9ce0a64 Iustin Pop
                  " node %s %s" %
700 e9ce0a64 Iustin Pop
                  (constants.RELEASE_VERSION, node, remote_version[1]))
701 e9ce0a64 Iustin Pop
702 e9ce0a64 Iustin Pop
    # checks vg existence and size > 20G
703 cc9e1230 Guido Trotter
    if vg_name is not None:
704 cc9e1230 Guido Trotter
      vglist = node_result.get(constants.NV_VGLIST, None)
705 cc9e1230 Guido Trotter
      if not vglist:
706 cc9e1230 Guido Trotter
        feedback_fn("  - ERROR: unable to check volume groups on node %s." %
707 cc9e1230 Guido Trotter
708 a8083063 Iustin Pop
        bad = True
709 cc9e1230 Guido Trotter
710 cc9e1230 Guido Trotter
        vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
711 cc9e1230 Guido Trotter
712 cc9e1230 Guido Trotter
        if vgstatus:
713 cc9e1230 Guido Trotter
          feedback_fn("  - ERROR: %s on node %s" % (vgstatus, node))
714 cc9e1230 Guido Trotter
          bad = True
715 a8083063 Iustin Pop
716 a8083063 Iustin Pop
    # checks config file checksum
717 a8083063 Iustin Pop
718 25361b9a Iustin Pop
    remote_cksum = node_result.get(constants.NV_FILELIST, None)
719 25361b9a Iustin Pop
    if not isinstance(remote_cksum, dict):
720 a8083063 Iustin Pop
      bad = True
721 a8083063 Iustin Pop
      feedback_fn("  - ERROR: node hasn't returned file checksum data")
722 a8083063 Iustin Pop
723 a8083063 Iustin Pop
      for file_name in file_list:
724 112f18a5 Iustin Pop
        node_is_mc = nodeinfo.master_candidate
725 112f18a5 Iustin Pop
        must_have_file = file_name not in master_files
726 a8083063 Iustin Pop
        if file_name not in remote_cksum:
727 112f18a5 Iustin Pop
          if node_is_mc or must_have_file:
728 112f18a5 Iustin Pop
            bad = True
729 112f18a5 Iustin Pop
            feedback_fn("  - ERROR: file '%s' missing" % file_name)
730 a8083063 Iustin Pop
        elif remote_cksum[file_name] != local_cksum[file_name]:
731 112f18a5 Iustin Pop
          if node_is_mc or must_have_file:
732 112f18a5 Iustin Pop
            bad = True
733 112f18a5 Iustin Pop
            feedback_fn("  - ERROR: file '%s' has wrong checksum" % file_name)
734 112f18a5 Iustin Pop
735 112f18a5 Iustin Pop
            # not candidate and this is not a must-have file
736 112f18a5 Iustin Pop
            bad = True
737 112f18a5 Iustin Pop
            feedback_fn("  - ERROR: non master-candidate has old/wrong file"
738 112f18a5 Iustin Pop
                        " '%s'" % file_name)
739 112f18a5 Iustin Pop
740 112f18a5 Iustin Pop
          # all good, except non-master/non-must have combination
741 112f18a5 Iustin Pop
          if not node_is_mc and not must_have_file:
742 112f18a5 Iustin Pop
            feedback_fn("  - ERROR: file '%s' should not exist on non master"
743 112f18a5 Iustin Pop
                        " candidates" % file_name)
744 a8083063 Iustin Pop
745 25361b9a Iustin Pop
    # checks ssh to any
746 25361b9a Iustin Pop
747 25361b9a Iustin Pop
    if constants.NV_NODELIST not in node_result:
748 a8083063 Iustin Pop
      bad = True
749 9d4bfc96 Iustin Pop
      feedback_fn("  - ERROR: node hasn't returned node ssh connectivity data")
750 a8083063 Iustin Pop
751 25361b9a Iustin Pop
      if node_result[constants.NV_NODELIST]:
752 a8083063 Iustin Pop
        bad = True
753 25361b9a Iustin Pop
        for node in node_result[constants.NV_NODELIST]:
754 9d4bfc96 Iustin Pop
          feedback_fn("  - ERROR: ssh communication with node '%s': %s" %
755 25361b9a Iustin Pop
                          (node, node_result[constants.NV_NODELIST][node]))
756 25361b9a Iustin Pop
757 25361b9a Iustin Pop
    if constants.NV_NODENETTEST not in node_result:
758 9d4bfc96 Iustin Pop
      bad = True
759 9d4bfc96 Iustin Pop
      feedback_fn("  - ERROR: node hasn't returned node tcp connectivity data")
760 9d4bfc96 Iustin Pop
761 25361b9a Iustin Pop
      if node_result[constants.NV_NODENETTEST]:
762 9d4bfc96 Iustin Pop
        bad = True
763 25361b9a Iustin Pop
        nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
764 9d4bfc96 Iustin Pop
        for node in nlist:
765 9d4bfc96 Iustin Pop
          feedback_fn("  - ERROR: tcp communication with node '%s': %s" %
766 25361b9a Iustin Pop
                          (node, node_result[constants.NV_NODENETTEST][node]))
767 9d4bfc96 Iustin Pop
768 25361b9a Iustin Pop
    hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
769 e69d05fd Iustin Pop
    if isinstance(hyp_result, dict):
770 e69d05fd Iustin Pop
      for hv_name, hv_result in hyp_result.iteritems():
771 e69d05fd Iustin Pop
        if hv_result is not None:
772 e69d05fd Iustin Pop
          feedback_fn("  - ERROR: hypervisor %s verify failure: '%s'" %
773 e69d05fd Iustin Pop
                      (hv_name, hv_result))
774 6d2e83d5 Iustin Pop
775 6d2e83d5 Iustin Pop
    # check used drbd list
776 cc9e1230 Guido Trotter
    if vg_name is not None:
777 cc9e1230 Guido Trotter
      used_minors = node_result.get(constants.NV_DRBDLIST, [])
778 cc9e1230 Guido Trotter
      if not isinstance(used_minors, (tuple, list)):
779 cc9e1230 Guido Trotter
        feedback_fn("  - ERROR: cannot parse drbd status file: %s" %
780 cc9e1230 Guido Trotter
781 cc9e1230 Guido Trotter
782 cc9e1230 Guido Trotter
        for minor, (iname, must_exist) in drbd_map.items():
783 cc9e1230 Guido Trotter
          if minor not in used_minors and must_exist:
784 35e994e9 Iustin Pop
            feedback_fn("  - ERROR: drbd minor %d of instance %s is"
785 35e994e9 Iustin Pop
                        " not active" % (minor, iname))
786 cc9e1230 Guido Trotter
            bad = True
787 cc9e1230 Guido Trotter
        for minor in used_minors:
788 cc9e1230 Guido Trotter
          if minor not in drbd_map:
789 35e994e9 Iustin Pop
            feedback_fn("  - ERROR: unallocated drbd minor %d is in use" %
790 35e994e9 Iustin Pop
791 cc9e1230 Guido Trotter
            bad = True
792 6d2e83d5 Iustin Pop
793 a8083063 Iustin Pop
    return bad
794 a8083063 Iustin Pop
795 c5705f58 Guido Trotter
  def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
796 0a66c968 Iustin Pop
                      node_instance, feedback_fn, n_offline):
797 a8083063 Iustin Pop
    """Verify an instance.
798 a8083063 Iustin Pop

799 a8083063 Iustin Pop
    This function checks to see if the required block devices are
800 a8083063 Iustin Pop
    available on the instance's node.
801 a8083063 Iustin Pop

802 a8083063 Iustin Pop
803 a8083063 Iustin Pop
    bad = False
804 a8083063 Iustin Pop
805 a8083063 Iustin Pop
    node_current = instanceconfig.primary_node
806 a8083063 Iustin Pop
807 a8083063 Iustin Pop
    node_vol_should = {}
808 a8083063 Iustin Pop
809 a8083063 Iustin Pop
810 a8083063 Iustin Pop
    for node in node_vol_should:
811 0a66c968 Iustin Pop
      if node in n_offline:
812 0a66c968 Iustin Pop
        # ignore missing volumes on offline nodes
813 0a66c968 Iustin Pop
814 a8083063 Iustin Pop
      for volume in node_vol_should[node]:
815 a8083063 Iustin Pop
        if node not in node_vol_is or volume not in node_vol_is[node]:
816 a8083063 Iustin Pop
          feedback_fn("  - ERROR: volume %s missing on node %s" %
817 a8083063 Iustin Pop
                          (volume, node))
818 a8083063 Iustin Pop
          bad = True
819 a8083063 Iustin Pop
820 0d68c45d Iustin Pop
    if instanceconfig.admin_up:
821 0a66c968 Iustin Pop
      if ((node_current not in node_instance or
822 0a66c968 Iustin Pop
          not instance in node_instance[node_current]) and
823 0a66c968 Iustin Pop
          node_current not in n_offline):
824 a8083063 Iustin Pop
        feedback_fn("  - ERROR: instance %s not running on node %s" %
825 a8083063 Iustin Pop
                        (instance, node_current))
826 a8083063 Iustin Pop
        bad = True
827 a8083063 Iustin Pop
828 a8083063 Iustin Pop
    for node in node_instance:
829 a8083063 Iustin Pop
      if (not node == node_current):
830 a8083063 Iustin Pop
        if instance in node_instance[node]:
831 a8083063 Iustin Pop
          feedback_fn("  - ERROR: instance %s should not run on node %s" %
832 a8083063 Iustin Pop
                          (instance, node))
833 a8083063 Iustin Pop
          bad = True
834 a8083063 Iustin Pop
835 6a438c98 Michael Hanselmann
    return bad
836 a8083063 Iustin Pop
837 a8083063 Iustin Pop
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is, feedback_fn):
838 a8083063 Iustin Pop
    """Verify if there are any unknown volumes in the cluster.
839 a8083063 Iustin Pop

840 a8083063 Iustin Pop
    The .os, .swap and backup volumes are ignored. All other volumes are
841 a8083063 Iustin Pop
    reported as unknown.
842 a8083063 Iustin Pop

843 a8083063 Iustin Pop
844 a8083063 Iustin Pop
    bad = False
845 a8083063 Iustin Pop
846 a8083063 Iustin Pop
    for node in node_vol_is:
847 a8083063 Iustin Pop
      for volume in node_vol_is[node]:
848 a8083063 Iustin Pop
        if node not in node_vol_should or volume not in node_vol_should[node]:
849 a8083063 Iustin Pop
          feedback_fn("  - ERROR: volume %s on node %s should not exist" %
850 a8083063 Iustin Pop
                      (volume, node))
851 a8083063 Iustin Pop
          bad = True
852 a8083063 Iustin Pop
    return bad
853 a8083063 Iustin Pop
854 a8083063 Iustin Pop
  def _VerifyOrphanInstances(self, instancelist, node_instance, feedback_fn):
855 a8083063 Iustin Pop
    """Verify the list of running instances.
856 a8083063 Iustin Pop

857 a8083063 Iustin Pop
    This checks what instances are running but unknown to the cluster.
858 a8083063 Iustin Pop

859 a8083063 Iustin Pop
860 a8083063 Iustin Pop
    bad = False
861 a8083063 Iustin Pop
    for node in node_instance:
862 a8083063 Iustin Pop
      for runninginstance in node_instance[node]:
863 a8083063 Iustin Pop
        if runninginstance not in instancelist:
864 a8083063 Iustin Pop
          feedback_fn("  - ERROR: instance %s on node %s should not exist" %
865 a8083063 Iustin Pop
                          (runninginstance, node))
866 a8083063 Iustin Pop
          bad = True
867 a8083063 Iustin Pop
    return bad
868 a8083063 Iustin Pop
869 2b3b6ddd Guido Trotter
  def _VerifyNPlusOneMemory(self, node_info, instance_cfg, feedback_fn):
870 2b3b6ddd Guido Trotter
    """Verify N+1 Memory Resilience.
871 2b3b6ddd Guido Trotter

872 2b3b6ddd Guido Trotter
    Check that if one single node dies we can still start all the instances it
873 2b3b6ddd Guido Trotter
    was primary for.
874 2b3b6ddd Guido Trotter

875 2b3b6ddd Guido Trotter
876 2b3b6ddd Guido Trotter
    bad = False
877 2b3b6ddd Guido Trotter
878 2b3b6ddd Guido Trotter
    for node, nodeinfo in node_info.iteritems():
879 2b3b6ddd Guido Trotter
      # This code checks that every node which is now listed as secondary has
880 2b3b6ddd Guido Trotter
      # enough memory to host all instances it is supposed to should a single
881 2b3b6ddd Guido Trotter
      # other node in the cluster fail.
882 2b3b6ddd Guido Trotter
      # FIXME: not ready for failover to an arbitrary node
883 2b3b6ddd Guido Trotter
      # FIXME: does not support file-backed instances
884 2b3b6ddd Guido Trotter
      # WARNING: we currently take into account down instances as well as up
885 2b3b6ddd Guido Trotter
      # ones, considering that even if they're down someone might want to start
886 2b3b6ddd Guido Trotter
      # them even in the event of a node failure.
887 2b3b6ddd Guido Trotter
      for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
888 2b3b6ddd Guido Trotter
        needed_mem = 0
889 2b3b6ddd Guido Trotter
        for instance in instances:
890 338e51e8 Iustin Pop
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
891 c0f2b229 Iustin Pop
          if bep[constants.BE_AUTO_BALANCE]:
892 3924700f Iustin Pop
            needed_mem += bep[constants.BE_MEMORY]
893 2b3b6ddd Guido Trotter
        if nodeinfo['mfree'] < needed_mem:
894 2b3b6ddd Guido Trotter
          feedback_fn("  - ERROR: not enough memory on node %s to accomodate"
895 2b3b6ddd Guido Trotter
                      " failovers should node %s fail" % (node, prinode))
896 2b3b6ddd Guido Trotter
          bad = True
897 2b3b6ddd Guido Trotter
    return bad
898 2b3b6ddd Guido Trotter
899 a8083063 Iustin Pop
  def CheckPrereq(self):
900 a8083063 Iustin Pop
    """Check prerequisites.
901 a8083063 Iustin Pop

902 e54c4c5e Guido Trotter
    Transform the list of checks we're going to skip into a set and check that
903 e54c4c5e Guido Trotter
    all its members are valid.
904 a8083063 Iustin Pop

905 a8083063 Iustin Pop
906 e54c4c5e Guido Trotter
    self.skip_set = frozenset(self.op.skip_checks)
907 e54c4c5e Guido Trotter
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
908 e54c4c5e Guido Trotter
      raise errors.OpPrereqError("Invalid checks to be skipped specified")
909 a8083063 Iustin Pop
910 d8fff41c Guido Trotter
  def BuildHooksEnv(self):
911 d8fff41c Guido Trotter
    """Build hooks env.
912 d8fff41c Guido Trotter

913 d8fff41c Guido Trotter
    Cluster-Verify hooks just rone in the post phase and their failure makes
914 d8fff41c Guido Trotter
    the output be logged in the verify output and the verification to fail.
915 d8fff41c Guido Trotter

916 d8fff41c Guido Trotter
917 d8fff41c Guido Trotter
    all_nodes = self.cfg.GetNodeList()
918 35e994e9 Iustin Pop
    env = {
919 35e994e9 Iustin Pop
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
920 35e994e9 Iustin Pop
921 35e994e9 Iustin Pop
    for node in self.cfg.GetAllNodesInfo().values():
922 35e994e9 Iustin Pop
      env["NODE_TAGS_%s" %] = " ".join(node.GetTags())
923 35e994e9 Iustin Pop
924 d8fff41c Guido Trotter
    return env, [], all_nodes
925 d8fff41c Guido Trotter
926 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
927 a8083063 Iustin Pop
    """Verify integrity of cluster, performing various test on nodes.
928 a8083063 Iustin Pop

929 a8083063 Iustin Pop
930 a8083063 Iustin Pop
    bad = False
931 a8083063 Iustin Pop
    feedback_fn("* Verifying global settings")
932 8522ceeb Iustin Pop
    for msg in self.cfg.VerifyConfig():
933 8522ceeb Iustin Pop
      feedback_fn("  - ERROR: %s" % msg)
934 a8083063 Iustin Pop
935 a8083063 Iustin Pop
    vg_name = self.cfg.GetVGName()
936 e69d05fd Iustin Pop
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
937 a8083063 Iustin Pop
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
938 9d4bfc96 Iustin Pop
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
939 a8083063 Iustin Pop
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
940 6d2e83d5 Iustin Pop
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
941 6d2e83d5 Iustin Pop
                        for iname in instancelist)
942 93e4c50b Guido Trotter
    i_non_redundant = [] # Non redundant instances
943 3924700f Iustin Pop
    i_non_a_balanced = [] # Non auto-balanced instances
944 0a66c968 Iustin Pop
    n_offline = [] # List of offline nodes
945 22f0f71d Iustin Pop
    n_drained = [] # List of nodes being drained
946 a8083063 Iustin Pop
    node_volume = {}
947 a8083063 Iustin Pop
    node_instance = {}
948 9c9c7d30 Guido Trotter
    node_info = {}
949 26b6af5e Guido Trotter
    instance_cfg = {}
950 a8083063 Iustin Pop
951 a8083063 Iustin Pop
    # FIXME: verify OS list
952 a8083063 Iustin Pop
    # do local checksums
953 112f18a5 Iustin Pop
    master_files = [constants.CLUSTER_CONF_FILE]
954 112f18a5 Iustin Pop
955 112f18a5 Iustin Pop
    file_names = ssconf.SimpleStore().GetFileList()
956 cb91d46e Iustin Pop
957 699777f2 Michael Hanselmann
958 112f18a5 Iustin Pop
959 112f18a5 Iustin Pop
960 a8083063 Iustin Pop
    local_checksums = utils.FingerprintFiles(file_names)
961 a8083063 Iustin Pop
962 a8083063 Iustin Pop
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
963 a8083063 Iustin Pop
    node_verify_param = {
964 25361b9a Iustin Pop
      constants.NV_FILELIST: file_names,
965 82e37788 Iustin Pop
      constants.NV_NODELIST: [ for node in nodeinfo
966 82e37788 Iustin Pop
                              if not node.offline],
967 25361b9a Iustin Pop
      constants.NV_HYPERVISOR: hypervisors,
968 25361b9a Iustin Pop
      constants.NV_NODENETTEST: [(, node.primary_ip,
969 82e37788 Iustin Pop
                                  node.secondary_ip) for node in nodeinfo
970 82e37788 Iustin Pop
                                 if not node.offline],
971 25361b9a Iustin Pop
      constants.NV_INSTANCELIST: hypervisors,
972 25361b9a Iustin Pop
      constants.NV_VERSION: None,
973 25361b9a Iustin Pop
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
974 a8083063 Iustin Pop
975 cc9e1230 Guido Trotter
    if vg_name is not None:
976 cc9e1230 Guido Trotter
      node_verify_param[constants.NV_VGLIST] = None
977 cc9e1230 Guido Trotter
      node_verify_param[constants.NV_LVLIST] = vg_name
978 cc9e1230 Guido Trotter
      node_verify_param[constants.NV_DRBDLIST] = None
979 72737a7f Iustin Pop
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
980 72737a7f Iustin Pop
981 a8083063 Iustin Pop
982 3924700f Iustin Pop
    cluster = self.cfg.GetClusterInfo()
983 112f18a5 Iustin Pop
    master_node = self.cfg.GetMasterNode()
984 6d2e83d5 Iustin Pop
    all_drbd_map = self.cfg.ComputeDRBDMap()
985 6d2e83d5 Iustin Pop
986 112f18a5 Iustin Pop
    for node_i in nodeinfo:
987 112f18a5 Iustin Pop
      node =
988 25361b9a Iustin Pop
      nresult = all_nvinfo[node].data
989 25361b9a Iustin Pop
990 0a66c968 Iustin Pop
      if node_i.offline:
991 0a66c968 Iustin Pop
        feedback_fn("* Skipping offline node %s" % (node,))
992 0a66c968 Iustin Pop
993 0a66c968 Iustin Pop
994 0a66c968 Iustin Pop
995 112f18a5 Iustin Pop
      if node == master_node:
996 25361b9a Iustin Pop
        ntype = "master"
997 112f18a5 Iustin Pop
      elif node_i.master_candidate:
998 25361b9a Iustin Pop
        ntype = "master candidate"
999 22f0f71d Iustin Pop
      elif node_i.drained:
1000 22f0f71d Iustin Pop
        ntype = "drained"
1001 22f0f71d Iustin Pop
1002 112f18a5 Iustin Pop
1003 25361b9a Iustin Pop
        ntype = "regular"
1004 112f18a5 Iustin Pop
      feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1005 25361b9a Iustin Pop
1006 25361b9a Iustin Pop
      if all_nvinfo[node].failed or not isinstance(nresult, dict):
1007 25361b9a Iustin Pop
        feedback_fn("  - ERROR: connection to %s failed" % (node,))
1008 25361b9a Iustin Pop
        bad = True
1009 25361b9a Iustin Pop
1010 25361b9a Iustin Pop
1011 6d2e83d5 Iustin Pop
      node_drbd = {}
1012 6d2e83d5 Iustin Pop
      for minor, instance in all_drbd_map[node].items():
1013 c614e5fb Iustin Pop
        if instance not in instanceinfo:
1014 c614e5fb Iustin Pop
          feedback_fn("  - ERROR: ghost instance '%s' in temporary DRBD map" %
1015 c614e5fb Iustin Pop
1016 c614e5fb Iustin Pop
          # ghost instance should not be running, but otherwise we
1017 c614e5fb Iustin Pop
          # don't give double warnings (both ghost instance and
1018 c614e5fb Iustin Pop
          # unallocated minor in use)
1019 c614e5fb Iustin Pop
          node_drbd[minor] = (instance, False)
1020 c614e5fb Iustin Pop
1021 c614e5fb Iustin Pop
          instance = instanceinfo[instance]
1022 c614e5fb Iustin Pop
          node_drbd[minor] = (, instance.admin_up)
1023 112f18a5 Iustin Pop
      result = self._VerifyNode(node_i, file_names, local_checksums,
1024 6d2e83d5 Iustin Pop
                                nresult, feedback_fn, master_files,
1025 cc9e1230 Guido Trotter
                                node_drbd, vg_name)
1026 a8083063 Iustin Pop
      bad = bad or result
1027 a8083063 Iustin Pop
1028 25361b9a Iustin Pop
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1029 cc9e1230 Guido Trotter
      if vg_name is None:
1030 cc9e1230 Guido Trotter
        node_volume[node] = {}
1031 cc9e1230 Guido Trotter
      elif isinstance(lvdata, basestring):
1032 b63ed789 Iustin Pop
        feedback_fn("  - ERROR: LVM problem on node %s: %s" %
1033 26f15862 Iustin Pop
                    (node, utils.SafeEncode(lvdata)))
1034 b63ed789 Iustin Pop
        bad = True
1035 b63ed789 Iustin Pop
        node_volume[node] = {}
1036 25361b9a Iustin Pop
      elif not isinstance(lvdata, dict):
1037 25361b9a Iustin Pop
        feedback_fn("  - ERROR: connection to %s failed (lvlist)" % (node,))
1038 a8083063 Iustin Pop
        bad = True
1039 a8083063 Iustin Pop
1040 b63ed789 Iustin Pop
1041 25361b9a Iustin Pop
        node_volume[node] = lvdata
1042 a8083063 Iustin Pop
1043 a8083063 Iustin Pop
      # node_instance
1044 25361b9a Iustin Pop
      idata = nresult.get(constants.NV_INSTANCELIST, None)
1045 25361b9a Iustin Pop
      if not isinstance(idata, list):
1046 25361b9a Iustin Pop
        feedback_fn("  - ERROR: connection to %s failed (instancelist)" %
1047 25361b9a Iustin Pop
1048 a8083063 Iustin Pop
        bad = True
1049 a8083063 Iustin Pop
1050 a8083063 Iustin Pop
1051 25361b9a Iustin Pop
      node_instance[node] = idata
1052 a8083063 Iustin Pop
1053 9c9c7d30 Guido Trotter
      # node_info
1054 25361b9a Iustin Pop
      nodeinfo = nresult.get(constants.NV_HVINFO, None)
1055 9c9c7d30 Guido Trotter
      if not isinstance(nodeinfo, dict):
1056 25361b9a Iustin Pop
        feedback_fn("  - ERROR: connection to %s failed (hvinfo)" % (node,))
1057 9c9c7d30 Guido Trotter
        bad = True
1058 9c9c7d30 Guido Trotter
1059 9c9c7d30 Guido Trotter
1060 9c9c7d30 Guido Trotter
1061 9c9c7d30 Guido Trotter
        node_info[node] = {
1062 9c9c7d30 Guido Trotter
          "mfree": int(nodeinfo['memory_free']),
1063 93e4c50b Guido Trotter
          "pinst": [],
1064 93e4c50b Guido Trotter
          "sinst": [],
1065 36e7da50 Guido Trotter
          # dictionary holding all instances this node is secondary for,
1066 36e7da50 Guido Trotter
          # grouped by their primary node. Each key is a cluster node, and each
1067 36e7da50 Guido Trotter
          # value is a list of instances which have the key as primary and the
1068 36e7da50 Guido Trotter
          # current node as secondary.  this is handy to calculate N+1 memory
1069 36e7da50 Guido Trotter
          # availability if you can only failover from a primary to its
1070 36e7da50 Guido Trotter
          # secondary.
1071 36e7da50 Guido Trotter
          "sinst-by-pnode": {},
1072 9c9c7d30 Guido Trotter
1073 cc9e1230 Guido Trotter
        # FIXME: devise a free space model for file based instances as well
1074 cc9e1230 Guido Trotter
        if vg_name is not None:
1075 9a198532 Iustin Pop
          if (constants.NV_VGLIST not in nresult or
1076 9a198532 Iustin Pop
              vg_name not in nresult[constants.NV_VGLIST]):
1077 9a198532 Iustin Pop
            feedback_fn("  - ERROR: node %s didn't return data for the"
1078 9a198532 Iustin Pop
                        " volume group '%s' - it is either missing or broken" %
1079 9a198532 Iustin Pop
                        (node, vg_name))
1080 9a198532 Iustin Pop
            bad = True
1081 9a198532 Iustin Pop
1082 cc9e1230 Guido Trotter
          node_info[node]["dfree"] = int(nresult[constants.NV_VGLIST][vg_name])
1083 9a198532 Iustin Pop
      except (ValueError, KeyError):
1084 9a198532 Iustin Pop
        feedback_fn("  - ERROR: invalid nodeinfo value returned"
1085 9a198532 Iustin Pop
                    " from node %s" % (node,))
1086 9c9c7d30 Guido Trotter
        bad = True
1087 9c9c7d30 Guido Trotter
1088 9c9c7d30 Guido Trotter
1089 a8083063 Iustin Pop
    node_vol_should = {}
1090 a8083063 Iustin Pop
1091 a8083063 Iustin Pop
    for instance in instancelist:
1092 a8083063 Iustin Pop
      feedback_fn("* Verifying instance %s" % instance)
1093 6d2e83d5 Iustin Pop
      inst_config = instanceinfo[instance]
1094 c5705f58 Guido Trotter
      result =  self._VerifyInstance(instance, inst_config, node_volume,
1095 0a66c968 Iustin Pop
                                     node_instance, feedback_fn, n_offline)
1096 c5705f58 Guido Trotter
      bad = bad or result
1097 832261fd Iustin Pop
      inst_nodes_offline = []
1098 a8083063 Iustin Pop
1099 a8083063 Iustin Pop
1100 a8083063 Iustin Pop
1101 26b6af5e Guido Trotter
      instance_cfg[instance] = inst_config
1102 26b6af5e Guido Trotter
1103 93e4c50b Guido Trotter
      pnode = inst_config.primary_node
1104 93e4c50b Guido Trotter
      if pnode in node_info:
1105 93e4c50b Guido Trotter
1106 0a66c968 Iustin Pop
      elif pnode not in n_offline:
1107 93e4c50b Guido Trotter
        feedback_fn("  - ERROR: instance %s, connection to primary node"
1108 93e4c50b Guido Trotter
                    " %s failed" % (instance, pnode))
1109 93e4c50b Guido Trotter
        bad = True
1110 93e4c50b Guido Trotter
1111 832261fd Iustin Pop
      if pnode in n_offline:
1112 832261fd Iustin Pop
1113 832261fd Iustin Pop
1114 93e4c50b Guido Trotter
      # If the instance is non-redundant we cannot survive losing its primary
1115 93e4c50b Guido Trotter
      # node, so we are not N+1 compliant. On the other hand we have no disk
1116 93e4c50b Guido Trotter
      # templates with more than one secondary so that situation is not well
1117 93e4c50b Guido Trotter
      # supported either.
1118 93e4c50b Guido Trotter
      # FIXME: does not support file-backed instances
1119 93e4c50b Guido Trotter
      if len(inst_config.secondary_nodes) == 0:
1120 93e4c50b Guido Trotter
1121 93e4c50b Guido Trotter
      elif len(inst_config.secondary_nodes) > 1:
1122 93e4c50b Guido Trotter
        feedback_fn("  - WARNING: multiple secondaries for instance %s"
1123 93e4c50b Guido Trotter
                    % instance)
1124 93e4c50b Guido Trotter
1125 c0f2b229 Iustin Pop
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1126 3924700f Iustin Pop
1127 3924700f Iustin Pop
1128 93e4c50b Guido Trotter
      for snode in inst_config.secondary_nodes:
1129 93e4c50b Guido Trotter
        if snode in node_info:
1130 93e4c50b Guido Trotter
1131 36e7da50 Guido Trotter
          if pnode not in node_info[snode]['sinst-by-pnode']:
1132 36e7da50 Guido Trotter
            node_info[snode]['sinst-by-pnode'][pnode] = []
1133 36e7da50 Guido Trotter
1134 0a66c968 Iustin Pop
        elif snode not in n_offline:
1135 93e4c50b Guido Trotter
          feedback_fn("  - ERROR: instance %s, connection to secondary node"
1136 93e4c50b Guido Trotter
                      " %s failed" % (instance, snode))
1137 832261fd Iustin Pop
          bad = True
1138 832261fd Iustin Pop
        if snode in n_offline:
1139 832261fd Iustin Pop
1140 832261fd Iustin Pop
1141 832261fd Iustin Pop
      if inst_nodes_offline:
1142 832261fd Iustin Pop
        # warn that the instance lives on offline nodes, and set bad=True
1143 832261fd Iustin Pop
        feedback_fn("  - ERROR: instance lives on offline node(s) %s" %
1144 832261fd Iustin Pop
                    ", ".join(inst_nodes_offline))
1145 832261fd Iustin Pop
        bad = True
1146 93e4c50b Guido Trotter
1147 a8083063 Iustin Pop
    feedback_fn("* Verifying orphan volumes")
1148 a8083063 Iustin Pop
    result = self._VerifyOrphanVolumes(node_vol_should, node_volume,
1149 a8083063 Iustin Pop
1150 a8083063 Iustin Pop
    bad = bad or result
1151 a8083063 Iustin Pop
1152 a8083063 Iustin Pop
    feedback_fn("* Verifying remaining instances")
1153 a8083063 Iustin Pop
    result = self._VerifyOrphanInstances(instancelist, node_instance,
1154 a8083063 Iustin Pop
1155 a8083063 Iustin Pop
    bad = bad or result
1156 a8083063 Iustin Pop
1157 e54c4c5e Guido Trotter
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1158 e54c4c5e Guido Trotter
      feedback_fn("* Verifying N+1 Memory redundancy")
1159 e54c4c5e Guido Trotter
      result = self._VerifyNPlusOneMemory(node_info, instance_cfg, feedback_fn)
1160 e54c4c5e Guido Trotter
      bad = bad or result
1161 2b3b6ddd Guido Trotter
1162 2b3b6ddd Guido Trotter
    feedback_fn("* Other Notes")
1163 2b3b6ddd Guido Trotter
    if i_non_redundant:
1164 2b3b6ddd Guido Trotter
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1165 2b3b6ddd Guido Trotter
                  % len(i_non_redundant))
1166 2b3b6ddd Guido Trotter
1167 3924700f Iustin Pop
    if i_non_a_balanced:
1168 3924700f Iustin Pop
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1169 3924700f Iustin Pop
                  % len(i_non_a_balanced))
1170 3924700f Iustin Pop
1171 0a66c968 Iustin Pop
    if n_offline:
1172 0a66c968 Iustin Pop
      feedback_fn("  - NOTICE: %d offline node(s) found." % len(n_offline))
1173 0a66c968 Iustin Pop
1174 22f0f71d Iustin Pop
    if n_drained:
1175 22f0f71d Iustin Pop
      feedback_fn("  - NOTICE: %d drained node(s) found." % len(n_drained))
1176 22f0f71d Iustin Pop
1177 34290825 Michael Hanselmann
    return not bad
1178 a8083063 Iustin Pop
1179 d8fff41c Guido Trotter
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1180 e4376078 Iustin Pop
    """Analize the post-hooks' result
1181 e4376078 Iustin Pop

1182 e4376078 Iustin Pop
    This method analyses the hook result, handles it, and sends some
1183 d8fff41c Guido Trotter
    nicely-formatted feedback back to the user.
1184 d8fff41c Guido Trotter

1185 e4376078 Iustin Pop
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1186 e4376078 Iustin Pop
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1187 e4376078 Iustin Pop
    @param hooks_results: the results of the multi-node hooks rpc call
1188 e4376078 Iustin Pop
    @param feedback_fn: function used send feedback back to the caller
1189 e4376078 Iustin Pop
    @param lu_result: previous Exec result
1190 e4376078 Iustin Pop
    @return: the new Exec result, based on the previous result
1191 e4376078 Iustin Pop
        and hook results
1192 d8fff41c Guido Trotter

1193 d8fff41c Guido Trotter
1194 38206f3c Iustin Pop
    # We only really run POST phase hooks, and are only interested in
1195 38206f3c Iustin Pop
    # their results
1196 d8fff41c Guido Trotter
    if phase == constants.HOOKS_PHASE_POST:
1197 d8fff41c Guido Trotter
      # Used to change hooks' output to proper indentation
1198 d8fff41c Guido Trotter
      indent_re = re.compile('^', re.M)
1199 d8fff41c Guido Trotter
      feedback_fn("* Hooks Results")
1200 d8fff41c Guido Trotter
      if not hooks_results:
1201 d8fff41c Guido Trotter
        feedback_fn("  - ERROR: general communication failure")
1202 d8fff41c Guido Trotter
        lu_result = 1
1203 d8fff41c Guido Trotter
1204 d8fff41c Guido Trotter
        for node_name in hooks_results:
1205 d8fff41c Guido Trotter
          show_node_header = True
1206 d8fff41c Guido Trotter
          res = hooks_results[node_name]
1207 25361b9a Iustin Pop
          if res.failed or is False or not isinstance(, list):
1208 0a66c968 Iustin Pop
            if res.offline:
1209 0a66c968 Iustin Pop
              # no need to warn or set fail return value
1210 0a66c968 Iustin Pop
1211 25361b9a Iustin Pop
            feedback_fn("    Communication failure in hooks execution")
1212 d8fff41c Guido Trotter
            lu_result = 1
1213 d8fff41c Guido Trotter
1214 25361b9a Iustin Pop
          for script, hkr, output in
1215 d8fff41c Guido Trotter
            if hkr == constants.HKR_FAIL:
1216 d8fff41c Guido Trotter
              # The node header is only shown once, if there are
1217 d8fff41c Guido Trotter
              # failing hooks on that node
1218 d8fff41c Guido Trotter
              if show_node_header:
1219 d8fff41c Guido Trotter
                feedback_fn("  Node %s:" % node_name)
1220 d8fff41c Guido Trotter
                show_node_header = False
1221 d8fff41c Guido Trotter
              feedback_fn("    ERROR: Script %s failed, output:" % script)
1222 d8fff41c Guido Trotter
              output = indent_re.sub('      ', output)
1223 d8fff41c Guido Trotter
              feedback_fn("%s" % output)
1224 d8fff41c Guido Trotter
              lu_result = 1
1225 d8fff41c Guido Trotter
1226 d8fff41c Guido Trotter
      return lu_result
1227 d8fff41c Guido Trotter
1228 a8083063 Iustin Pop
1229 2c95a8d4 Iustin Pop
class LUVerifyDisks(NoHooksLU):
1230 2c95a8d4 Iustin Pop
  """Verifies the cluster disks status.
1231 2c95a8d4 Iustin Pop

1232 2c95a8d4 Iustin Pop
1233 2c95a8d4 Iustin Pop
  _OP_REQP = []
1234 d4b9d97f Guido Trotter
  REQ_BGL = False
1235 d4b9d97f Guido Trotter
1236 d4b9d97f Guido Trotter
  def ExpandNames(self):
1237 d4b9d97f Guido Trotter
    self.needed_locks = {
1238 d4b9d97f Guido Trotter
      locking.LEVEL_NODE: locking.ALL_SET,
1239 d4b9d97f Guido Trotter
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1240 d4b9d97f Guido Trotter
1241 d4b9d97f Guido Trotter
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1242 2c95a8d4 Iustin Pop
1243 2c95a8d4 Iustin Pop
  def CheckPrereq(self):
1244 2c95a8d4 Iustin Pop
    """Check prerequisites.
1245 2c95a8d4 Iustin Pop

1246 2c95a8d4 Iustin Pop
    This has no prerequisites.
1247 2c95a8d4 Iustin Pop

1248 2c95a8d4 Iustin Pop
1249 2c95a8d4 Iustin Pop
1250 2c95a8d4 Iustin Pop
1251 2c95a8d4 Iustin Pop
  def Exec(self, feedback_fn):
1252 2c95a8d4 Iustin Pop
    """Verify integrity of cluster disks.
1253 2c95a8d4 Iustin Pop

1254 2c95a8d4 Iustin Pop
1255 b63ed789 Iustin Pop
    result = res_nodes, res_nlvm, res_instances, res_missing = [], {}, [], {}
1256 2c95a8d4 Iustin Pop
1257 2c95a8d4 Iustin Pop
    vg_name = self.cfg.GetVGName()
1258 2c95a8d4 Iustin Pop
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1259 2c95a8d4 Iustin Pop
    instances = [self.cfg.GetInstanceInfo(name)
1260 2c95a8d4 Iustin Pop
                 for name in self.cfg.GetInstanceList()]
1261 2c95a8d4 Iustin Pop
1262 2c95a8d4 Iustin Pop
    nv_dict = {}
1263 2c95a8d4 Iustin Pop
    for inst in instances:
1264 2c95a8d4 Iustin Pop
      inst_lvs = {}
1265 0d68c45d Iustin Pop
      if (not inst.admin_up or
1266 2c95a8d4 Iustin Pop
          inst.disk_template not in constants.DTS_NET_MIRROR):
1267 2c95a8d4 Iustin Pop
1268 2c95a8d4 Iustin Pop
1269 2c95a8d4 Iustin Pop
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1270 2c95a8d4 Iustin Pop
      for node, vol_list in inst_lvs.iteritems():
1271 2c95a8d4 Iustin Pop
        for vol in vol_list:
1272 2c95a8d4 Iustin Pop
          nv_dict[(node, vol)] = inst
1273 2c95a8d4 Iustin Pop
1274 2c95a8d4 Iustin Pop
    if not nv_dict:
1275 2c95a8d4 Iustin Pop
      return result
1276 2c95a8d4 Iustin Pop
1277 72737a7f Iustin Pop
    node_lvs = self.rpc.call_volume_list(nodes, vg_name)
1278 2c95a8d4 Iustin Pop
1279 2c95a8d4 Iustin Pop
    to_act = set()
1280 2c95a8d4 Iustin Pop
    for node in nodes:
1281 2c95a8d4 Iustin Pop
      # node_volume
1282 2c95a8d4 Iustin Pop
      lvs = node_lvs[node]
1283 781de953 Iustin Pop
      if lvs.failed:
1284 0a66c968 Iustin Pop
        if not lvs.offline:
1285 0a66c968 Iustin Pop
          self.LogWarning("Connection to node %s failed: %s" %
1286 0a66c968 Iustin Pop
1287 781de953 Iustin Pop
1288 781de953 Iustin Pop
      lvs =
1289 b63ed789 Iustin Pop
      if isinstance(lvs, basestring):
1290 9a4f63d1 Iustin Pop
        logging.warning("Error enumerating LVs on node %s: %s", node, lvs)
1291 b63ed789 Iustin Pop
        res_nlvm[node] = lvs
1292 ea9ddc07 Iustin Pop
1293 b63ed789 Iustin Pop
      elif not isinstance(lvs, dict):
1294 9a4f63d1 Iustin Pop
        logging.warning("Connection to node %s failed or invalid data"
1295 9a4f63d1 Iustin Pop
                        " returned", node)
1296 2c95a8d4 Iustin Pop
1297 2c95a8d4 Iustin Pop
1298 2c95a8d4 Iustin Pop
1299 2c95a8d4 Iustin Pop
      for lv_name, (_, lv_inactive, lv_online) in lvs.iteritems():
1300 b63ed789 Iustin Pop
        inst = nv_dict.pop((node, lv_name), None)
1301 b63ed789 Iustin Pop
        if (not lv_online and inst is not None
1302 b63ed789 Iustin Pop
            and not in res_instances):
1303 b08d5a87 Iustin Pop
1304 2c95a8d4 Iustin Pop
1305 b63ed789 Iustin Pop
    # any leftover items in nv_dict are missing LVs, let's arrange the
1306 b63ed789 Iustin Pop
    # data better
1307 b63ed789 Iustin Pop
    for key, inst in nv_dict.iteritems():
1308 b63ed789 Iustin Pop
      if not in res_missing:
1309 b63ed789 Iustin Pop
        res_missing[] = []
1310 b63ed789 Iustin Pop
1311 b63ed789 Iustin Pop
1312 2c95a8d4 Iustin Pop
    return result
1313 2c95a8d4 Iustin Pop
1314 2c95a8d4 Iustin Pop
1315 07bd8a51 Iustin Pop
class LURenameCluster(LogicalUnit):
1316 07bd8a51 Iustin Pop
  """Rename the cluster.
1317 07bd8a51 Iustin Pop

1318 07bd8a51 Iustin Pop
1319 07bd8a51 Iustin Pop
  HPATH = "cluster-rename"
1320 07bd8a51 Iustin Pop
1321 07bd8a51 Iustin Pop
  _OP_REQP = ["name"]
1322 07bd8a51 Iustin Pop
1323 07bd8a51 Iustin Pop
  def BuildHooksEnv(self):
1324 07bd8a51 Iustin Pop
    """Build hooks env.
1325 07bd8a51 Iustin Pop

1326 07bd8a51 Iustin Pop
1327 07bd8a51 Iustin Pop
    env = {
1328 d6a02168 Michael Hanselmann
      "OP_TARGET": self.cfg.GetClusterName(),
1329 07bd8a51 Iustin Pop
1330 07bd8a51 Iustin Pop
1331 d6a02168 Michael Hanselmann
    mn = self.cfg.GetMasterNode()
1332 07bd8a51 Iustin Pop
    return env, [mn], [mn]
1333 07bd8a51 Iustin Pop
1334 07bd8a51 Iustin Pop
  def CheckPrereq(self):
1335 07bd8a51 Iustin Pop
    """Verify that the passed name is a valid one.
1336 07bd8a51 Iustin Pop

1337 07bd8a51 Iustin Pop
1338 89e1fc26 Iustin Pop
    hostname = utils.HostInfo(
1339 07bd8a51 Iustin Pop
1340 bcf043c9 Iustin Pop
    new_name =
1341 bcf043c9 Iustin Pop
    self.ip = new_ip = hostname.ip
1342 d6a02168 Michael Hanselmann
    old_name = self.cfg.GetClusterName()
1343 d6a02168 Michael Hanselmann
    old_ip = self.cfg.GetMasterIP()
1344 07bd8a51 Iustin Pop
    if new_name == old_name and new_ip == old_ip:
1345 07bd8a51 Iustin Pop
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
1346 07bd8a51 Iustin Pop
                                 " cluster has changed")
1347 07bd8a51 Iustin Pop
    if new_ip != old_ip:
1348 937f983d Guido Trotter
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1349 07bd8a51 Iustin Pop
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
1350 07bd8a51 Iustin Pop
                                   " reachable on the network. Aborting." %
1351 07bd8a51 Iustin Pop
1352 07bd8a51 Iustin Pop
1353 07bd8a51 Iustin Pop = new_name
1354 07bd8a51 Iustin Pop
1355 07bd8a51 Iustin Pop
  def Exec(self, feedback_fn):
1356 07bd8a51 Iustin Pop
    """Rename the cluster.
1357 07bd8a51 Iustin Pop

1358 07bd8a51 Iustin Pop
1359 07bd8a51 Iustin Pop
    clustername =
1360 07bd8a51 Iustin Pop
    ip = self.ip
1361 07bd8a51 Iustin Pop
1362 07bd8a51 Iustin Pop
    # shutdown the master IP
1363 d6a02168 Michael Hanselmann
    master = self.cfg.GetMasterNode()
1364 781de953 Iustin Pop
    result = self.rpc.call_node_stop_master(master, False)
1365 781de953 Iustin Pop
    if result.failed or not
1366 07bd8a51 Iustin Pop
      raise errors.OpExecError("Could not disable the master role")
1367 07bd8a51 Iustin Pop
1368 07bd8a51 Iustin Pop
1369 55cf7d83 Iustin Pop
      cluster = self.cfg.GetClusterInfo()
1370 55cf7d83 Iustin Pop
      cluster.cluster_name = clustername
1371 55cf7d83 Iustin Pop
      cluster.master_ip = ip
1372 55cf7d83 Iustin Pop
1373 ec85e3d5 Iustin Pop
1374 ec85e3d5 Iustin Pop
      # update the known hosts file
1375 ec85e3d5 Iustin Pop
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
1376 ec85e3d5 Iustin Pop
      node_list = self.cfg.GetNodeList()
1377 ec85e3d5 Iustin Pop
1378 ec85e3d5 Iustin Pop
1379 ec85e3d5 Iustin Pop
      except ValueError:
1380 ec85e3d5 Iustin Pop
1381 ec85e3d5 Iustin Pop
      result = self.rpc.call_upload_file(node_list,
1382 ec85e3d5 Iustin Pop
1383 ec85e3d5 Iustin Pop
      for to_node, to_result in result.iteritems():
1384 1b54fc6c Guido Trotter
         msg = to_result.RemoteFailMsg()
1385 1b54fc6c Guido Trotter
         if msg:
1386 1b54fc6c Guido Trotter
           msg = ("Copy of file %s to node %s failed: %s" %
1387 1b54fc6c Guido Trotter
                   (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
1388 1b54fc6c Guido Trotter
1389 ec85e3d5 Iustin Pop
1390 07bd8a51 Iustin Pop
1391 781de953 Iustin Pop
      result = self.rpc.call_node_start_master(master, False)
1392 781de953 Iustin Pop
      if result.failed or not
1393 86d9d3bb Iustin Pop
        self.LogWarning("Could not re-enable the master role on"
1394 86d9d3bb Iustin Pop
                        " the master, please restart manually.")
1395 07bd8a51 Iustin Pop
1396 07bd8a51 Iustin Pop
1397 8084f9f6 Manuel Franceschini
def _RecursiveCheckIfLVMBased(disk):
1398 8084f9f6 Manuel Franceschini
  """Check if the given disk or its children are lvm-based.
1399 8084f9f6 Manuel Franceschini

1400 e4376078 Iustin Pop
  @type disk: L{objects.Disk}
1401 e4376078 Iustin Pop
  @param disk: the disk to check
1402 e4376078 Iustin Pop
  @rtype: booleean
1403 e4376078 Iustin Pop
  @return: boolean indicating whether a LD_LV dev_type was found or not
1404 8084f9f6 Manuel Franceschini

1405 8084f9f6 Manuel Franceschini
1406 8084f9f6 Manuel Franceschini
  if disk.children:
1407 8084f9f6 Manuel Franceschini
    for chdisk in disk.children:
1408 8084f9f6 Manuel Franceschini
      if _RecursiveCheckIfLVMBased(chdisk):
1409 8084f9f6 Manuel Franceschini
        return True
1410 8084f9f6 Manuel Franceschini
  return disk.dev_type == constants.LD_LV
1411 8084f9f6 Manuel Franceschini
1412 8084f9f6 Manuel Franceschini
1413 8084f9f6 Manuel Franceschini
class LUSetClusterParams(LogicalUnit):
1414 8084f9f6 Manuel Franceschini
  """Change the parameters of the cluster.
1415 8084f9f6 Manuel Franceschini

1416 8084f9f6 Manuel Franceschini
1417 8084f9f6 Manuel Franceschini
  HPATH = "cluster-modify"
1418 8084f9f6 Manuel Franceschini
1419 8084f9f6 Manuel Franceschini
  _OP_REQP = []
1420 c53279cf Guido Trotter
  REQ_BGL = False
1421 c53279cf Guido Trotter
1422 3994f455 Iustin Pop
  def CheckArguments(self):
1423 4b7735f9 Iustin Pop
    """Check parameters
1424 4b7735f9 Iustin Pop

1425 4b7735f9 Iustin Pop
1426 4b7735f9 Iustin Pop
    if not hasattr(self.op, "candidate_pool_size"):
1427 4b7735f9 Iustin Pop
      self.op.candidate_pool_size = None
1428 4b7735f9 Iustin Pop
    if self.op.candidate_pool_size is not None:
1429 4b7735f9 Iustin Pop
1430 4b7735f9 Iustin Pop
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
1431 3994f455 Iustin Pop
      except (ValueError, TypeError), err:
1432 4b7735f9 Iustin Pop
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
1433 4b7735f9 Iustin Pop
1434 4b7735f9 Iustin Pop
      if self.op.candidate_pool_size < 1:
1435 4b7735f9 Iustin Pop
        raise errors.OpPrereqError("At least one master candidate needed")
1436 4b7735f9 Iustin Pop
1437 c53279cf Guido Trotter
  def ExpandNames(self):
1438 c53279cf Guido Trotter
    # FIXME: in the future maybe other cluster params won't require checking on
1439 c53279cf Guido Trotter
    # all nodes to be modified.
1440 c53279cf Guido Trotter
    self.needed_locks = {
1441 c53279cf Guido Trotter
      locking.LEVEL_NODE: locking.ALL_SET,
1442 c53279cf Guido Trotter
1443 c53279cf Guido Trotter
    self.share_locks[locking.LEVEL_NODE] = 1
1444 8084f9f6 Manuel Franceschini
1445 8084f9f6 Manuel Franceschini
  def BuildHooksEnv(self):
1446 8084f9f6 Manuel Franceschini
    """Build hooks env.
1447 8084f9f6 Manuel Franceschini

1448 8084f9f6 Manuel Franceschini
1449 8084f9f6 Manuel Franceschini
    env = {
1450 d6a02168 Michael Hanselmann
      "OP_TARGET": self.cfg.GetClusterName(),
1451 8084f9f6 Manuel Franceschini
      "NEW_VG_NAME": self.op.vg_name,
1452 8084f9f6 Manuel Franceschini
1453 d6a02168 Michael Hanselmann
    mn = self.cfg.GetMasterNode()
1454 8084f9f6 Manuel Franceschini
    return env, [mn], [mn]
1455 8084f9f6 Manuel Franceschini
1456 8084f9f6 Manuel Franceschini
  def CheckPrereq(self):
1457 8084f9f6 Manuel Franceschini
    """Check prerequisites.
1458 8084f9f6 Manuel Franceschini

1459 8084f9f6 Manuel Franceschini
    This checks whether the given params don't conflict and
1460 5f83e263 Iustin Pop
    if the given volume group is valid.
1461 8084f9f6 Manuel Franceschini

1462 8084f9f6 Manuel Franceschini
1463 779c15bb Iustin Pop
    if self.op.vg_name is not None and not self.op.vg_name:
1464 c53279cf Guido Trotter
      instances = self.cfg.GetAllInstancesInfo().values()
1465 8084f9f6 Manuel Franceschini
      for inst in instances:
1466 8084f9f6 Manuel Franceschini
        for disk in inst.disks:
1467 8084f9f6 Manuel Franceschini
          if _RecursiveCheckIfLVMBased(disk):
1468 8084f9f6 Manuel Franceschini
            raise errors.OpPrereqError("Cannot disable lvm storage while"
1469 8084f9f6 Manuel Franceschini
                                       " lvm-based instances exist")
1470 8084f9f6 Manuel Franceschini
1471 779c15bb Iustin Pop
    node_list = self.acquired_locks[locking.LEVEL_NODE]
1472 779c15bb Iustin Pop
1473 8084f9f6 Manuel Franceschini
    # if vg_name not None, checks given volume group on all nodes
1474 8084f9f6 Manuel Franceschini
    if self.op.vg_name:
1475 72737a7f Iustin Pop
      vglist = self.rpc.call_vg_list(node_list)
1476 8084f9f6 Manuel Franceschini
      for node in node_list:
1477 781de953 Iustin Pop
        if vglist[node].failed:
1478 781de953 Iustin Pop
          # ignoring down node
1479 781de953 Iustin Pop
          self.LogWarning("Node %s unreachable/error, ignoring" % node)
1480 781de953 Iustin Pop
1481 781de953 Iustin Pop
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].data,
1482 781de953 Iustin Pop
1483 8d1a2a64 Michael Hanselmann
1484 8084f9f6 Manuel Franceschini
        if vgstatus:
1485 8084f9f6 Manuel Franceschini
          raise errors.OpPrereqError("Error on node '%s': %s" %
1486 8084f9f6 Manuel Franceschini
                                     (node, vgstatus))
1487 8084f9f6 Manuel Franceschini
1488 779c15bb Iustin Pop
    self.cluster = cluster = self.cfg.GetClusterInfo()
1489 5af3da74 Guido Trotter
    # validate params changes
1490 779c15bb Iustin Pop
    if self.op.beparams:
1491 a5728081 Guido Trotter
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
1492 abe609b2 Guido Trotter
      self.new_beparams = objects.FillDict(
1493 4ef7f423 Guido Trotter
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
1494 779c15bb Iustin Pop
1495 5af3da74 Guido Trotter
    if self.op.nicparams:
1496 5af3da74 Guido Trotter
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
1497 5af3da74 Guido Trotter
      self.new_nicparams = objects.FillDict(
1498 5af3da74 Guido Trotter
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
1499 5af3da74 Guido Trotter
1500 5af3da74 Guido Trotter
1501 779c15bb Iustin Pop
    # hypervisor list/parameters
1502 abe609b2 Guido Trotter
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
1503 779c15bb Iustin Pop
    if self.op.hvparams:
1504 779c15bb Iustin Pop
      if not isinstance(self.op.hvparams, dict):
1505 779c15bb Iustin Pop
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input")
1506 779c15bb Iustin Pop
      for hv_name, hv_dict in self.op.hvparams.items():
1507 779c15bb Iustin Pop
        if hv_name not in self.new_hvparams:
1508 779c15bb Iustin Pop
          self.new_hvparams[hv_name] = hv_dict
1509 779c15bb Iustin Pop
1510 779c15bb Iustin Pop
1511 779c15bb Iustin Pop
1512 779c15bb Iustin Pop
    if self.op.enabled_hypervisors is not None:
1513 779c15bb Iustin Pop
      self.hv_list = self.op.enabled_hypervisors
1514 779c15bb Iustin Pop
1515 779c15bb Iustin Pop
      self.hv_list = cluster.enabled_hypervisors
1516 779c15bb Iustin Pop
1517 779c15bb Iustin Pop
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
1518 779c15bb Iustin Pop
      # either the enabled list has changed, or the parameters have, validate
1519 779c15bb Iustin Pop
      for hv_name, hv_params in self.new_hvparams.items():
1520 779c15bb Iustin Pop
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
1521 779c15bb Iustin Pop
            (self.op.enabled_hypervisors and
1522 779c15bb Iustin Pop
             hv_name in self.op.enabled_hypervisors)):
1523 779c15bb Iustin Pop
          # either this is a new hypervisor, or its parameters have changed
1524 779c15bb Iustin Pop
          hv_class = hypervisor.GetHypervisor(hv_name)
1525 a5728081 Guido Trotter
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
1526 779c15bb Iustin Pop
1527 779c15bb Iustin Pop
          _CheckHVParams(self, node_list, hv_name, hv_params)
1528 779c15bb Iustin Pop
1529 8084f9f6 Manuel Franceschini
  def Exec(self, feedback_fn):
1530 8084f9f6 Manuel Franceschini
    """Change the parameters of the cluster.
1531 8084f9f6 Manuel Franceschini

1532 8084f9f6 Manuel Franceschini
1533 779c15bb Iustin Pop
    if self.op.vg_name is not None:
1534 b2482333 Guido Trotter
      new_volume = self.op.vg_name
1535 b2482333 Guido Trotter
      if not new_volume:
1536 b2482333 Guido Trotter
        new_volume = None
1537 b2482333 Guido Trotter
      if new_volume != self.cfg.GetVGName():
1538 b2482333 Guido Trotter
1539 779c15bb Iustin Pop
1540 779c15bb Iustin Pop
        feedback_fn("Cluster LVM configuration already in desired"
1541 779c15bb Iustin Pop
                    " state, not changing")
1542 779c15bb Iustin Pop
    if self.op.hvparams:
1543 779c15bb Iustin Pop
      self.cluster.hvparams = self.new_hvparams
1544 779c15bb Iustin Pop
    if self.op.enabled_hypervisors is not None:
1545 779c15bb Iustin Pop
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
1546 779c15bb Iustin Pop
    if self.op.beparams:
1547 4ef7f423 Guido Trotter
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
1548 5af3da74 Guido Trotter
    if self.op.nicparams:
1549 5af3da74 Guido Trotter
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
1550 5af3da74 Guido Trotter
1551 4b7735f9 Iustin Pop
    if self.op.candidate_pool_size is not None:
1552 4b7735f9 Iustin Pop
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
1553 4b7735f9 Iustin Pop
1554 779c15bb Iustin Pop
1555 8084f9f6 Manuel Franceschini
1556 4b7735f9 Iustin Pop
    # we want to update nodes after the cluster so that if any errors
1557 4b7735f9 Iustin Pop
    # happen, we have recorded and saved the cluster info
1558 4b7735f9 Iustin Pop
    if self.op.candidate_pool_size is not None:
1559 ec0292f1 Iustin Pop
1560 4b7735f9 Iustin Pop
1561 8084f9f6 Manuel Franceschini
1562 28eddce5 Guido Trotter
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
1563 28eddce5 Guido Trotter
  """Distribute additional files which are part of the cluster configuration.
1564 28eddce5 Guido Trotter

1565 28eddce5 Guido Trotter
  ConfigWriter takes care of distributing the config and ssconf files, but
1566 28eddce5 Guido Trotter
  there are more files which should be distributed to all nodes. This function
1567 28eddce5 Guido Trotter
  makes sure those are copied.
1568 28eddce5 Guido Trotter

1569 28eddce5 Guido Trotter
  @param lu: calling logical unit
1570 28eddce5 Guido Trotter
  @param additional_nodes: list of nodes not in the config to distribute to
1571 28eddce5 Guido Trotter

1572 28eddce5 Guido Trotter
1573 28eddce5 Guido Trotter
  # 1. Gather target nodes
1574 28eddce5 Guido Trotter
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
1575 28eddce5 Guido Trotter
  dist_nodes = lu.cfg.GetNodeList()
1576 28eddce5 Guido Trotter
  if additional_nodes is not None:
1577 28eddce5 Guido Trotter
1578 28eddce5 Guido Trotter
  if in dist_nodes:
1579 28eddce5 Guido Trotter
1580 28eddce5 Guido Trotter
  # 2. Gather files to distribute
1581 28eddce5 Guido Trotter
  dist_files = set([constants.ETC_HOSTS,
1582 28eddce5 Guido Trotter
1583 28eddce5 Guido Trotter
1584 28eddce5 Guido Trotter
1585 28eddce5 Guido Trotter
1586 e1b8653f Guido Trotter
1587 e1b8653f Guido Trotter
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
1588 e1b8653f Guido Trotter
  for hv_name in enabled_hypervisors:
1589 e1b8653f Guido Trotter
    hv_class = hypervisor.GetHypervisor(hv_name)
1590 e1b8653f Guido Trotter
1591 e1b8653f Guido Trotter
1592 28eddce5 Guido Trotter
  # 3. Perform the files upload
1593 28eddce5 Guido Trotter
  for fname in dist_files:
1594 28eddce5 Guido Trotter
    if os.path.exists(fname):
1595 28eddce5 Guido Trotter
      result = lu.rpc.call_upload_file(dist_nodes, fname)
1596 28eddce5 Guido Trotter
      for to_node, to_result in result.items():
1597 1b54fc6c Guido Trotter
         msg = to_result.RemoteFailMsg()
1598 1b54fc6c Guido Trotter
         if msg:
1599 1b54fc6c Guido Trotter
           msg = ("Copy of file %s to node %s failed: %s" %
1600 1b54fc6c Guido Trotter
                   (fname, to_node, msg))
1601 1b54fc6c Guido Trotter
1602 28eddce5 Guido Trotter
1603 28eddce5 Guido Trotter
1604 afee0879 Iustin Pop
class LURedistributeConfig(NoHooksLU):
1605 afee0879 Iustin Pop
  """Force the redistribution of cluster configuration.
1606 afee0879 Iustin Pop

1607 afee0879 Iustin Pop
  This is a very simple LU.
1608 afee0879 Iustin Pop

1609 afee0879 Iustin Pop
1610 afee0879 Iustin Pop
  _OP_REQP = []
1611 afee0879 Iustin Pop
  REQ_BGL = False
1612 afee0879 Iustin Pop
1613 afee0879 Iustin Pop
  def ExpandNames(self):
1614 afee0879 Iustin Pop
    self.needed_locks = {
1615 afee0879 Iustin Pop
      locking.LEVEL_NODE: locking.ALL_SET,
1616 afee0879 Iustin Pop
1617 afee0879 Iustin Pop
    self.share_locks[locking.LEVEL_NODE] = 1
1618 afee0879 Iustin Pop
1619 afee0879 Iustin Pop
  def CheckPrereq(self):
1620 afee0879 Iustin Pop
    """Check prerequisites.
1621 afee0879 Iustin Pop

1622 afee0879 Iustin Pop
1623 afee0879 Iustin Pop
1624 afee0879 Iustin Pop
  def Exec(self, feedback_fn):
1625 afee0879 Iustin Pop
    """Redistribute the configuration.
1626 afee0879 Iustin Pop

1627 afee0879 Iustin Pop
1628 afee0879 Iustin Pop
1629 28eddce5 Guido Trotter
1630 afee0879 Iustin Pop
1631 afee0879 Iustin Pop
1632 b9bddb6b Iustin Pop
def _WaitForSync(lu, instance, oneshot=False, unlock=False):
1633 a8083063 Iustin Pop
  """Sleep and poll for an instance's disk to sync.
1634 a8083063 Iustin Pop

1635 a8083063 Iustin Pop
1636 a8083063 Iustin Pop
  if not instance.disks:
1637 a8083063 Iustin Pop
    return True
1638 a8083063 Iustin Pop
1639 a8083063 Iustin Pop
  if not oneshot:
1640 b9bddb6b Iustin Pop
    lu.proc.LogInfo("Waiting for instance %s to sync disks." %
1641 a8083063 Iustin Pop
1642 a8083063 Iustin Pop
  node = instance.primary_node
1643 a8083063 Iustin Pop
1644 a8083063 Iustin Pop
  for dev in instance.disks:
1645 b9bddb6b Iustin Pop
    lu.cfg.SetDiskID(dev, node)
1646 a8083063 Iustin Pop
1647 a8083063 Iustin Pop
  retries = 0
1648 a8083063 Iustin Pop
  while True:
1649 a8083063 Iustin Pop
    max_time = 0
1650 a8083063 Iustin Pop
    done = True
1651 a8083063 Iustin Pop
    cumul_degraded = False
1652 72737a7f Iustin Pop
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
1653 781de953 Iustin Pop
    if rstats.failed or not
1654 86d9d3bb Iustin Pop
      lu.LogWarning("Can't get any data from node %s", node)
1655 a8083063 Iustin Pop
      retries += 1
1656 a8083063 Iustin Pop
      if retries >= 10:
1657 3ecf6786 Iustin Pop
        raise errors.RemoteError("Can't contact node %s for mirror data,"
1658 3ecf6786 Iustin Pop
                                 " aborting." % node)
1659 a8083063 Iustin Pop
1660 a8083063 Iustin Pop
1661 781de953 Iustin Pop
    rstats =
1662 a8083063 Iustin Pop
    retries = 0
1663 1492cca7 Iustin Pop
    for i, mstat in enumerate(rstats):
1664 a8083063 Iustin Pop
      if mstat is None:
1665 86d9d3bb Iustin Pop
        lu.LogWarning("Can't compute data for node %s/%s",
1666 86d9d3bb Iustin Pop
                           node, instance.disks[i].iv_name)
1667 a8083063 Iustin Pop
1668 0834c866 Iustin Pop
      # we ignore the ldisk parameter
1669 0834c866 Iustin Pop
      perc_done, est_time, is_degraded, _ = mstat
1670 a8083063 Iustin Pop
      cumul_degraded = cumul_degraded or (is_degraded and perc_done is None)
1671 a8083063 Iustin Pop
      if perc_done is not None:
1672 a8083063 Iustin Pop
        done = False
1673 a8083063 Iustin Pop
        if est_time is not None:
1674 a8083063 Iustin Pop
          rem_time = "%d estimated seconds remaining" % est_time
1675 a8083063 Iustin Pop
          max_time = est_time
1676 a8083063 Iustin Pop
1677 a8083063 Iustin Pop
          rem_time = "no time estimate"
1678 b9bddb6b Iustin Pop
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
1679 b9bddb6b Iustin Pop
                        (instance.disks[i].iv_name, perc_done, rem_time))
1680 a8083063 Iustin Pop
    if done or oneshot:
1681 a8083063 Iustin Pop
1682 a8083063 Iustin Pop
1683 d4fa5c23 Iustin Pop
    time.sleep(min(60, max_time))
1684 a8083063 Iustin Pop
1685 a8083063 Iustin Pop
  if done:
1686 b9bddb6b Iustin Pop
    lu.proc.LogInfo("Instance %s's disks are in sync." %
1687 a8083063 Iustin Pop
  return not cumul_degraded
1688 a8083063 Iustin Pop
1689 a8083063 Iustin Pop
1690 b9bddb6b Iustin Pop
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
1691 a8083063 Iustin Pop
  """Check that mirrors are not degraded.
1692 a8083063 Iustin Pop

1693 0834c866 Iustin Pop
  The ldisk parameter, if True, will change the test from the
1694 0834c866 Iustin Pop
  is_degraded attribute (which represents overall non-ok status for
1695 0834c866 Iustin Pop
  the device(s)) to the ldisk (representing the local storage status).
1696 0834c866 Iustin Pop

1697 a8083063 Iustin Pop
1698 b9bddb6b Iustin Pop
  lu.cfg.SetDiskID(dev, node)
1699 0834c866 Iustin Pop
  if ldisk:
1700 0834c866 Iustin Pop
    idx = 6
1701 0834c866 Iustin Pop
1702 0834c866 Iustin Pop
    idx = 5
1703 a8083063 Iustin Pop
1704 a8083063 Iustin Pop
  result = True
1705 a8083063 Iustin Pop
  if on_primary or dev.AssembleOnSecondary():
1706 72737a7f Iustin Pop
    rstats = lu.rpc.call_blockdev_find(node, dev)
1707 23829f6f Iustin Pop
    msg = rstats.RemoteFailMsg()
1708 23829f6f Iustin Pop
    if msg:
1709 23829f6f Iustin Pop
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
1710 23829f6f Iustin Pop
      result = False
1711 23829f6f Iustin Pop
    elif not rstats.payload:
1712 23829f6f Iustin Pop
      lu.LogWarning("Can't find disk on node %s", node)
1713 a8083063 Iustin Pop
      result = False
1714 a8083063 Iustin Pop
1715 23829f6f Iustin Pop
      result = result and (not rstats.payload[idx])
1716 a8083063 Iustin Pop
  if dev.children:
1717 a8083063 Iustin Pop
    for child in dev.children:
1718 b9bddb6b Iustin Pop
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
1719 a8083063 Iustin Pop
1720 a8083063 Iustin Pop
  return result
1721 a8083063 Iustin Pop
1722 a8083063 Iustin Pop
1723 a8083063 Iustin Pop
class LUDiagnoseOS(NoHooksLU):
1724 a8083063 Iustin Pop
  """Logical unit for OS diagnose/query.
1725 a8083063 Iustin Pop

1726 a8083063 Iustin Pop
1727 1f9430d6 Iustin Pop
  _OP_REQP = ["output_fields", "names"]
1728 6bf01bbb Guido Trotter
  REQ_BGL = False
1729 a2d2e1a7 Iustin Pop
  _FIELDS_STATIC = utils.FieldSet()
1730 a2d2e1a7 Iustin Pop
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status")
1731 a8083063 Iustin Pop
1732 6bf01bbb Guido Trotter
  def ExpandNames(self):
1733 1f9430d6 Iustin Pop
    if self.op.names:
1734 1f9430d6 Iustin Pop
      raise errors.OpPrereqError("Selective OS query not supported")
1735 1f9430d6 Iustin Pop
1736 31bf511f Iustin Pop
1737 31bf511f Iustin Pop
1738 1f9430d6 Iustin Pop
1739 1f9430d6 Iustin Pop
1740 6bf01bbb Guido Trotter
    # Lock all nodes, in shared mode
1741 a6ab004b Iustin Pop
    # Temporary removal of locks, should be reverted later
1742 a6ab004b Iustin Pop
    # TODO: reintroduce locks when they are lighter-weight
1743 6bf01bbb Guido Trotter
    self.needed_locks = {}
1744 a6ab004b Iustin Pop
    #self.share_locks[locking.LEVEL_NODE] = 1
1745 a6ab004b Iustin Pop
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
1746 6bf01bbb Guido Trotter
1747 6bf01bbb Guido Trotter
  def CheckPrereq(self):
1748 6bf01bbb Guido Trotter
    """Check prerequisites.
1749 6bf01bbb Guido Trotter

1750 6bf01bbb Guido Trotter
1751 6bf01bbb Guido Trotter
1752 1f9430d6 Iustin Pop
1753 1f9430d6 Iustin Pop
  def _DiagnoseByOS(node_list, rlist):
1754 1f9430d6 Iustin Pop
    """Remaps a per-node return list into an a per-os per-node dictionary
1755 1f9430d6 Iustin Pop

1756 e4376078 Iustin Pop
    @param node_list: a list with the names of all nodes
1757 e4376078 Iustin Pop
    @param rlist: a map with node names as keys and OS objects as values
1758 1f9430d6 Iustin Pop

1759 e4376078 Iustin Pop
    @rtype: dict
1760 5fcc718f Iustin Pop
    @return: a dictionary with osnames as keys and as value another map, with
1761 e4376078 Iustin Pop
        nodes as keys and list of OS objects as values, eg::
1762 e4376078 Iustin Pop

1763 e4376078 Iustin Pop
          {"debian-etch": {"node1": [<object>,...],
1764 e4376078 Iustin Pop
                           "node2": [<object>,]}
1765 e4376078 Iustin Pop
1766 1f9430d6 Iustin Pop

1767 1f9430d6 Iustin Pop
1768 1f9430d6 Iustin Pop
    all_os = {}
1769 a6ab004b Iustin Pop
    # we build here the list of nodes that didn't fail the RPC (at RPC
1770 a6ab004b Iustin Pop
    # level), so that nodes with a non-responding node daemon don't
1771 a6ab004b Iustin Pop
    # make all OSes invalid
1772 a6ab004b Iustin Pop
    good_nodes = [node_name for node_name in rlist
1773 a6ab004b Iustin Pop
                  if not rlist[node_name].failed]
1774 1f9430d6 Iustin Pop
    for node_name, nr in rlist.iteritems():
1775 781de953 Iustin Pop
      if nr.failed or not
1776 1f9430d6 Iustin Pop
1777 781de953 Iustin Pop
      for os_obj in
1778 b4de68a9 Iustin Pop
        if not in all_os:
1779 1f9430d6 Iustin Pop
          # build a list of nodes for this os containing empty lists
1780 1f9430d6 Iustin Pop
          # for each node in node_list
1781 b4de68a9 Iustin Pop
          all_os[] = {}
1782 a6ab004b Iustin Pop
          for nname in good_nodes:
1783 b4de68a9 Iustin Pop
            all_os[][nname] = []
1784 b4de68a9 Iustin Pop
1785 1f9430d6 Iustin Pop
    return all_os
1786 a8083063 Iustin Pop
1787 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1788 a8083063 Iustin Pop
    """Compute the list of OSes.
1789 a8083063 Iustin Pop

1790 a8083063 Iustin Pop
1791 a6ab004b Iustin Pop
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
1792 94a02bb5 Iustin Pop
    node_data = self.rpc.call_os_diagnose(valid_nodes)
1793 a8083063 Iustin Pop
    if node_data == False:
1794 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't gather the list of OSes")
1795 94a02bb5 Iustin Pop
    pol = self._DiagnoseByOS(valid_nodes, node_data)
1796 1f9430d6 Iustin Pop
    output = []
1797 1f9430d6 Iustin Pop
    for os_name, os_data in pol.iteritems():
1798 1f9430d6 Iustin Pop
      row = []
1799 1f9430d6 Iustin Pop
      for field in self.op.output_fields:
1800 1f9430d6 Iustin Pop
        if field == "name":
1801 1f9430d6 Iustin Pop
          val = os_name
1802 1f9430d6 Iustin Pop
        elif field == "valid":
1803 1f9430d6 Iustin Pop
          val = utils.all([osl and osl[0] for osl in os_data.values()])
1804 1f9430d6 Iustin Pop
        elif field == "node_status":
1805 1f9430d6 Iustin Pop
          val = {}
1806 1f9430d6 Iustin Pop
          for node_name, nos_list in os_data.iteritems():
1807 1f9430d6 Iustin Pop
            val[node_name] = [(v.status, v.path) for v in nos_list]
1808 1f9430d6 Iustin Pop
1809 1f9430d6 Iustin Pop
          raise errors.ParameterError(field)
1810 1f9430d6 Iustin Pop
1811 1f9430d6 Iustin Pop
1812 1f9430d6 Iustin Pop
1813 1f9430d6 Iustin Pop
    return output
1814 a8083063 Iustin Pop
1815 a8083063 Iustin Pop
1816 a8083063 Iustin Pop
class LURemoveNode(LogicalUnit):
1817 a8083063 Iustin Pop
  """Logical unit for removing a node.
1818 a8083063 Iustin Pop

1819 a8083063 Iustin Pop
1820 a8083063 Iustin Pop
  HPATH = "node-remove"
1821 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_NODE
1822 a8083063 Iustin Pop
  _OP_REQP = ["node_name"]
1823 a8083063 Iustin Pop
1824 a8083063 Iustin Pop
  def BuildHooksEnv(self):
1825 a8083063 Iustin Pop
    """Build hooks env.
1826 a8083063 Iustin Pop

1827 a8083063 Iustin Pop
    This doesn't run on the target node in the pre phase as a failed
1828 d08869ee Guido Trotter
    node would then be impossible to remove.
1829 a8083063 Iustin Pop

1830 a8083063 Iustin Pop
1831 396e1b78 Michael Hanselmann
    env = {
1832 0e137c28 Iustin Pop
      "OP_TARGET": self.op.node_name,
1833 396e1b78 Michael Hanselmann
      "NODE_NAME": self.op.node_name,
1834 396e1b78 Michael Hanselmann
1835 a8083063 Iustin Pop
    all_nodes = self.cfg.GetNodeList()
1836 a8083063 Iustin Pop
1837 396e1b78 Michael Hanselmann
    return env, all_nodes, all_nodes
1838 a8083063 Iustin Pop
1839 a8083063 Iustin Pop
  def CheckPrereq(self):
1840 a8083063 Iustin Pop
    """Check prerequisites.
1841 a8083063 Iustin Pop

1842 a8083063 Iustin Pop
    This checks:
1843 a8083063 Iustin Pop
     - the node exists in the configuration
1844 a8083063 Iustin Pop
     - it does not have primary or secondary instances
1845 a8083063 Iustin Pop
     - it's not the master
1846 a8083063 Iustin Pop

1847 a8083063 Iustin Pop
    Any errors are signalled by raising errors.OpPrereqError.
1848 a8083063 Iustin Pop

1849 a8083063 Iustin Pop
1850 a8083063 Iustin Pop
    node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
1851 a8083063 Iustin Pop
    if node is None:
1852 a02bc76e Iustin Pop
      raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name)
1853 a8083063 Iustin Pop
1854 a8083063 Iustin Pop
    instance_list = self.cfg.GetInstanceList()
1855 a8083063 Iustin Pop
1856 d6a02168 Michael Hanselmann
    masternode = self.cfg.GetMasterNode()
1857 a8083063 Iustin Pop
    if == masternode:
1858 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Node is the master node,"
1859 3ecf6786 Iustin Pop
                                 " you need to failover first.")
1860 a8083063 Iustin Pop
1861 a8083063 Iustin Pop
    for instance_name in instance_list:
1862 a8083063 Iustin Pop
      instance = self.cfg.GetInstanceInfo(instance_name)
1863 6b12959c Iustin Pop
      if in instance.all_nodes:
1864 6b12959c Iustin Pop
        raise errors.OpPrereqError("Instance %s is still running on the node,"
1865 3ecf6786 Iustin Pop
                                   " please remove first." % instance_name)
1866 a8083063 Iustin Pop
    self.op.node_name =
1867 a8083063 Iustin Pop
    self.node = node
1868 a8083063 Iustin Pop
1869 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1870 a8083063 Iustin Pop
    """Removes the node from the cluster.
1871 a8083063 Iustin Pop

1872 a8083063 Iustin Pop
1873 a8083063 Iustin Pop
    node = self.node
1874 9a4f63d1 Iustin Pop"Stopping the node daemon and removing configs from node %s",
1875 9a4f63d1 Iustin Pop
1876 a8083063 Iustin Pop
1877 d8470559 Michael Hanselmann
1878 a8083063 Iustin Pop
1879 72737a7f Iustin Pop
1880 c8a0948f Michael Hanselmann
1881 eb1742d5 Guido Trotter
    # Promote nodes to master candidate as needed
1882 ec0292f1 Iustin Pop
1883 eb1742d5 Guido Trotter
1884 a8083063 Iustin Pop
1885 a8083063 Iustin Pop
class LUQueryNodes(NoHooksLU):
1886 a8083063 Iustin Pop
  """Logical unit for querying nodes.
1887 a8083063 Iustin Pop

1888 a8083063 Iustin Pop
1889 bc8e4a1a Iustin Pop
  _OP_REQP = ["output_fields", "names", "use_locking"]
1890 35705d8f Guido Trotter
  REQ_BGL = False
1891 a2d2e1a7 Iustin Pop
  _FIELDS_DYNAMIC = utils.FieldSet(
1892 31bf511f Iustin Pop
    "dtotal", "dfree",
1893 31bf511f Iustin Pop
    "mtotal", "mnode", "mfree",
1894 31bf511f Iustin Pop
1895 0105bad3 Iustin Pop
    "ctotal", "cnodes", "csockets",
1896 31bf511f Iustin Pop
1897 31bf511f Iustin Pop
1898 a2d2e1a7 Iustin Pop
  _FIELDS_STATIC = utils.FieldSet(
1899 31bf511f Iustin Pop
    "name", "pinst_cnt", "sinst_cnt",
1900 31bf511f Iustin Pop
    "pinst_list", "sinst_list",
1901 31bf511f Iustin Pop
    "pip", "sip", "tags",
1902 31bf511f Iustin Pop
1903 0e67cdbe Iustin Pop
1904 0e67cdbe Iustin Pop
1905 9ddb5e45 Iustin Pop
1906 0b2454b9 Iustin Pop
1907 31bf511f Iustin Pop
1908 a8083063 Iustin Pop
1909 35705d8f Guido Trotter
  def ExpandNames(self):
1910 31bf511f Iustin Pop
1911 31bf511f Iustin Pop
1912 dcb93971 Michael Hanselmann
1913 a8083063 Iustin Pop
1914 35705d8f Guido Trotter
    self.needed_locks = {}
1915 35705d8f Guido Trotter
    self.share_locks[locking.LEVEL_NODE] = 1
1916 c8d8b4c8 Iustin Pop
1917 c8d8b4c8 Iustin Pop
    if self.op.names:
1918 c8d8b4c8 Iustin Pop
      self.wanted = _GetWantedNodes(self, self.op.names)
1919 35705d8f Guido Trotter
1920 c8d8b4c8 Iustin Pop
      self.wanted = locking.ALL_SET
1921 c8d8b4c8 Iustin Pop
1922 bc8e4a1a Iustin Pop
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
1923 bc8e4a1a Iustin Pop
    self.do_locking = self.do_node_query and self.op.use_locking
1924 c8d8b4c8 Iustin Pop
    if self.do_locking:
1925 c8d8b4c8 Iustin Pop
      # if we don't request only static fields, we need to lock the nodes
1926 c8d8b4c8 Iustin Pop
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
1927 c8d8b4c8 Iustin Pop
1928 35705d8f Guido Trotter
1929 35705d8f Guido Trotter
  def CheckPrereq(self):
1930 35705d8f Guido Trotter
    """Check prerequisites.
1931 35705d8f Guido Trotter

1932 35705d8f Guido Trotter
1933 c8d8b4c8 Iustin Pop
    # The validation of the node list is done in the _GetWantedNodes,
1934 c8d8b4c8 Iustin Pop
    # if non empty, and if empty, there's no validation to do
1935 c8d8b4c8 Iustin Pop
1936 a8083063 Iustin Pop
1937 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1938 a8083063 Iustin Pop
    """Computes the list of nodes and their attributes.
1939 a8083063 Iustin Pop

1940 a8083063 Iustin Pop
1941 c8d8b4c8 Iustin Pop
    all_info = self.cfg.GetAllNodesInfo()
1942 c8d8b4c8 Iustin Pop
    if self.do_locking:
1943 c8d8b4c8 Iustin Pop
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
1944 3fa93523 Guido Trotter
    elif self.wanted != locking.ALL_SET:
1945 3fa93523 Guido Trotter
      nodenames = self.wanted
1946 3fa93523 Guido Trotter
      missing = set(nodenames).difference(all_info.keys())
1947 3fa93523 Guido Trotter
      if missing:
1948 7b3a8fb5 Iustin Pop
        raise errors.OpExecError(
1949 3fa93523 Guido Trotter
          "Some nodes were removed before retrieving their data: %s" % missing)
1950 c8d8b4c8 Iustin Pop
1951 c8d8b4c8 Iustin Pop
      nodenames = all_info.keys()
1952 c1f1cbb2 Iustin Pop
1953 c1f1cbb2 Iustin Pop
    nodenames = utils.NiceSort(nodenames)
1954 c8d8b4c8 Iustin Pop
    nodelist = [all_info[name] for name in nodenames]
1955 a8083063 Iustin Pop
1956 a8083063 Iustin Pop
    # begin data gathering
1957 a8083063 Iustin Pop
1958 bc8e4a1a Iustin Pop
    if self.do_node_query:
1959 a8083063 Iustin Pop
      live_data = {}
1960 72737a7f Iustin Pop
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
1961 72737a7f Iustin Pop
1962 a8083063 Iustin Pop
      for name in nodenames:
1963 781de953 Iustin Pop
        nodeinfo = node_data[name]
1964 781de953 Iustin Pop
        if not nodeinfo.failed and
1965 781de953 Iustin Pop
          nodeinfo =
1966 d599d686 Iustin Pop
          fn = utils.TryConvert
1967 a8083063 Iustin Pop
          live_data[name] = {
1968 d599d686 Iustin Pop
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
1969 d599d686 Iustin Pop
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
1970 d599d686 Iustin Pop
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
1971 d599d686 Iustin Pop
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
1972 d599d686 Iustin Pop
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
1973 d599d686 Iustin Pop
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
1974 d599d686 Iustin Pop
            "bootid": nodeinfo.get('bootid', None),
1975 0105bad3 Iustin Pop
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
1976 0105bad3 Iustin Pop
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
1977 a8083063 Iustin Pop
1978 a8083063 Iustin Pop
1979 a8083063 Iustin Pop
          live_data[name] = {}
1980 a8083063 Iustin Pop
1981 a8083063 Iustin Pop
      live_data = dict.fromkeys(nodenames, {})
1982 a8083063 Iustin Pop
1983 ec223efb Iustin Pop
    node_to_primary = dict([(name, set()) for name in nodenames])
1984 ec223efb Iustin Pop
    node_to_secondary = dict([(name, set()) for name in nodenames])
1985 a8083063 Iustin Pop
1986 ec223efb Iustin Pop
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
1987 ec223efb Iustin Pop
                             "sinst_cnt", "sinst_list"))
1988 ec223efb Iustin Pop
    if inst_fields & frozenset(self.op.output_fields):
1989 a8083063 Iustin Pop
      instancelist = self.cfg.GetInstanceList()
1990 a8083063 Iustin Pop
1991 ec223efb Iustin Pop
      for instance_name in instancelist:
1992 ec223efb Iustin Pop
        inst = self.cfg.GetInstanceInfo(instance_name)
1993 ec223efb Iustin Pop
        if inst.primary_node in node_to_primary:
1994 ec223efb Iustin Pop
1995 ec223efb Iustin Pop
        for secnode in inst.secondary_nodes:
1996 ec223efb Iustin Pop
          if secnode in node_to_secondary:
1997 ec223efb Iustin Pop
1998 a8083063 Iustin Pop
1999 0e67cdbe Iustin Pop
    master_node = self.cfg.GetMasterNode()
2000 0e67cdbe Iustin Pop
2001 a8083063 Iustin Pop
    # end data gathering
2002 a8083063 Iustin Pop
2003 a8083063 Iustin Pop
    output = []
2004 a8083063 Iustin Pop
    for node in nodelist:
2005 a8083063 Iustin Pop
      node_output = []
2006 a8083063 Iustin Pop
      for field in self.op.output_fields:
2007 a8083063 Iustin Pop
        if field == "name":
2008 a8083063 Iustin Pop
          val =
2009 ec223efb Iustin Pop
        elif field == "pinst_list":
2010 ec223efb Iustin Pop
          val = list(node_to_primary[])
2011 ec223efb Iustin Pop
        elif field == "sinst_list":
2012 ec223efb Iustin Pop
          val = list(node_to_secondary[])
2013 ec223efb Iustin Pop
        elif field == "pinst_cnt":
2014 ec223efb Iustin Pop
          val = len(node_to_primary[])
2015 ec223efb Iustin Pop
        elif field == "sinst_cnt":
2016 ec223efb Iustin Pop
          val = len(node_to_secondary[])
2017 a8083063 Iustin Pop
        elif field == "pip":
2018 a8083063 Iustin Pop
          val = node.primary_ip
2019 a8083063 Iustin Pop
        elif field == "sip":
2020 a8083063 Iustin Pop
          val = node.secondary_ip
2021 130a6a6f Iustin Pop
        elif field == "tags":
2022 130a6a6f Iustin Pop
          val = list(node.GetTags())
2023 38d7239a Iustin Pop
        elif field == "serial_no":
2024 38d7239a Iustin Pop
          val = node.serial_no
2025 0e67cdbe Iustin Pop
        elif field == "master_candidate":
2026 0e67cdbe Iustin Pop
          val = node.master_candidate
2027 0e67cdbe Iustin Pop
        elif field == "master":
2028 0e67cdbe Iustin Pop
          val = == master_node
2029 9ddb5e45 Iustin Pop
        elif field == "offline":
2030 9ddb5e45 Iustin Pop
          val = node.offline
2031 0b2454b9 Iustin Pop
        elif field == "drained":
2032 0b2454b9 Iustin Pop
          val = node.drained
2033 31bf511f Iustin Pop
        elif self._FIELDS_DYNAMIC.Matches(field):
2034 ec223efb Iustin Pop
          val = live_data[].get(field, None)
2035 a8083063 Iustin Pop
2036 3ecf6786 Iustin Pop
          raise errors.ParameterError(field)
2037 a8083063 Iustin Pop
2038 a8083063 Iustin Pop
2039 a8083063 Iustin Pop
2040 a8083063 Iustin Pop
    return output
2041 a8083063 Iustin Pop
2042 a8083063 Iustin Pop
2043 dcb93971 Michael Hanselmann
class LUQueryNodeVolumes(NoHooksLU):
2044 dcb93971 Michael Hanselmann
  """Logical unit for getting volumes on node(s).
2045 dcb93971 Michael Hanselmann

2046 dcb93971 Michael Hanselmann
2047 dcb93971 Michael Hanselmann
  _OP_REQP = ["nodes", "output_fields"]
2048 21a15682 Guido Trotter
  REQ_BGL = False
2049 a2d2e1a7 Iustin Pop
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2050 a2d2e1a7 Iustin Pop
  _FIELDS_STATIC = utils.FieldSet("node")
2051 21a15682 Guido Trotter
2052 21a15682 Guido Trotter
  def ExpandNames(self):
2053 31bf511f Iustin Pop
2054 31bf511f Iustin Pop
2055 21a15682 Guido Trotter
2056 21a15682 Guido Trotter
2057 21a15682 Guido Trotter
    self.needed_locks = {}
2058 21a15682 Guido Trotter
    self.share_locks[locking.LEVEL_NODE] = 1
2059 21a15682 Guido Trotter
    if not self.op.nodes:
2060 e310b019 Guido Trotter
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2061 21a15682 Guido Trotter
2062 21a15682 Guido Trotter
      self.needed_locks[locking.LEVEL_NODE] = \
2063 21a15682 Guido Trotter
        _GetWantedNodes(self, self.op.nodes)
2064 dcb93971 Michael Hanselmann
2065 dcb93971 Michael Hanselmann
  def CheckPrereq(self):
2066 dcb93971 Michael Hanselmann
    """Check prerequisites.
2067 dcb93971 Michael Hanselmann

2068 dcb93971 Michael Hanselmann
    This checks that the fields required are valid output fields.
2069 dcb93971 Michael Hanselmann

2070 dcb93971 Michael Hanselmann
2071 21a15682 Guido Trotter
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2072 dcb93971 Michael Hanselmann
2073 dcb93971 Michael Hanselmann
  def Exec(self, feedback_fn):