Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ e9f7d26a

History | View | Annotate | Download (415.3 kB)

1
#
2
#
3

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

    
21

    
22
"""Module implementing the master-side code."""
23

    
24
# pylint: disable-msg=W0201,C0302
25

    
26
# W0201 since most LU attributes are defined in CheckPrereq or similar
27
# functions
28

    
29
# C0302: since we have waaaay to many lines in this module
30

    
31
import os
32
import os.path
33
import time
34
import re
35
import platform
36
import logging
37
import copy
38
import OpenSSL
39
import socket
40
import tempfile
41
import shutil
42
import itertools
43

    
44
from ganeti import ssh
45
from ganeti import utils
46
from ganeti import errors
47
from ganeti import hypervisor
48
from ganeti import locking
49
from ganeti import constants
50
from ganeti import objects
51
from ganeti import serializer
52
from ganeti import ssconf
53
from ganeti import uidpool
54
from ganeti import compat
55
from ganeti import masterd
56
from ganeti import netutils
57
from ganeti import query
58
from ganeti import qlang
59
from ganeti import opcodes
60

    
61
import ganeti.masterd.instance # pylint: disable-msg=W0611
62

    
63

    
64
def _SupportsOob(cfg, node):
65
  """Tells if node supports OOB.
66

67
  @type cfg: L{config.ConfigWriter}
68
  @param cfg: The cluster configuration
69
  @type node: L{objects.Node}
70
  @param node: The node
71
  @return: The OOB script if supported or an empty string otherwise
72

73
  """
74
  return cfg.GetNdParams(node)[constants.ND_OOB_PROGRAM]
75

    
76

    
77
class ResultWithJobs:
78
  """Data container for LU results with jobs.
79

80
  Instances of this class returned from L{LogicalUnit.Exec} will be recognized
81
  by L{mcpu.Processor._ProcessResult}. The latter will then submit the jobs
82
  contained in the C{jobs} attribute and include the job IDs in the opcode
83
  result.
84

85
  """
86
  def __init__(self, jobs, **kwargs):
87
    """Initializes this class.
88

89
    Additional return values can be specified as keyword arguments.
90

91
    @type jobs: list of lists of L{opcode.OpCode}
92
    @param jobs: A list of lists of opcode objects
93

94
    """
95
    self.jobs = jobs
96
    self.other = kwargs
97

    
98

    
99
class LogicalUnit(object):
100
  """Logical Unit base class.
101

102
  Subclasses must follow these rules:
103
    - implement ExpandNames
104
    - implement CheckPrereq (except when tasklets are used)
105
    - implement Exec (except when tasklets are used)
106
    - implement BuildHooksEnv
107
    - implement BuildHooksNodes
108
    - redefine HPATH and HTYPE
109
    - optionally redefine their run requirements:
110
        REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
111

112
  Note that all commands require root permissions.
113

114
  @ivar dry_run_result: the value (if any) that will be returned to the caller
115
      in dry-run mode (signalled by opcode dry_run parameter)
116

117
  """
118
  HPATH = None
119
  HTYPE = None
120
  REQ_BGL = True
121

    
122
  def __init__(self, processor, op, context, rpc):
123
    """Constructor for LogicalUnit.
124

125
    This needs to be overridden in derived classes in order to check op
126
    validity.
127

128
    """
129
    self.proc = processor
130
    self.op = op
131
    self.cfg = context.cfg
132
    self.context = context
133
    self.rpc = rpc
134
    # Dicts used to declare locking needs to mcpu
135
    self.needed_locks = None
136
    self.acquired_locks = {}
137
    self.share_locks = dict.fromkeys(locking.LEVELS, 0)
138
    self.add_locks = {}
139
    self.remove_locks = {}
140
    # Used to force good behavior when calling helper functions
141
    self.recalculate_locks = {}
142
    self.__ssh = None
143
    # logging
144
    self.Log = processor.Log # pylint: disable-msg=C0103
145
    self.LogWarning = processor.LogWarning # pylint: disable-msg=C0103
146
    self.LogInfo = processor.LogInfo # pylint: disable-msg=C0103
147
    self.LogStep = processor.LogStep # pylint: disable-msg=C0103
148
    # support for dry-run
149
    self.dry_run_result = None
150
    # support for generic debug attribute
151
    if (not hasattr(self.op, "debug_level") or
152
        not isinstance(self.op.debug_level, int)):
153
      self.op.debug_level = 0
154

    
155
    # Tasklets
156
    self.tasklets = None
157

    
158
    # Validate opcode parameters and set defaults
159
    self.op.Validate(True)
160

    
161
    self.CheckArguments()
162

    
163
  def __GetSSH(self):
164
    """Returns the SshRunner object
165

166
    """
167
    if not self.__ssh:
168
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
169
    return self.__ssh
170

    
171
  ssh = property(fget=__GetSSH)
172

    
173
  def CheckArguments(self):
174
    """Check syntactic validity for the opcode arguments.
175

176
    This method is for doing a simple syntactic check and ensure
177
    validity of opcode parameters, without any cluster-related
178
    checks. While the same can be accomplished in ExpandNames and/or
179
    CheckPrereq, doing these separate is better because:
180

181
      - ExpandNames is left as as purely a lock-related function
182
      - CheckPrereq is run after we have acquired locks (and possible
183
        waited for them)
184

185
    The function is allowed to change the self.op attribute so that
186
    later methods can no longer worry about missing parameters.
187

188
    """
189
    pass
190

    
191
  def ExpandNames(self):
192
    """Expand names for this LU.
193

194
    This method is called before starting to execute the opcode, and it should
195
    update all the parameters of the opcode to their canonical form (e.g. a
196
    short node name must be fully expanded after this method has successfully
197
    completed). This way locking, hooks, logging, etc. can work correctly.
198

199
    LUs which implement this method must also populate the self.needed_locks
200
    member, as a dict with lock levels as keys, and a list of needed lock names
201
    as values. Rules:
202

203
      - use an empty dict if you don't need any lock
204
      - if you don't need any lock at a particular level omit that level
205
      - don't put anything for the BGL level
206
      - if you want all locks at a level use locking.ALL_SET as a value
207

208
    If you need to share locks (rather than acquire them exclusively) at one
209
    level you can modify self.share_locks, setting a true value (usually 1) for
210
    that level. By default locks are not shared.
211

212
    This function can also define a list of tasklets, which then will be
213
    executed in order instead of the usual LU-level CheckPrereq and Exec
214
    functions, if those are not defined by the LU.
215

216
    Examples::
217

218
      # Acquire all nodes and one instance
219
      self.needed_locks = {
220
        locking.LEVEL_NODE: locking.ALL_SET,
221
        locking.LEVEL_INSTANCE: ['instance1.example.com'],
222
      }
223
      # Acquire just two nodes
224
      self.needed_locks = {
225
        locking.LEVEL_NODE: ['node1.example.com', 'node2.example.com'],
226
      }
227
      # Acquire no locks
228
      self.needed_locks = {} # No, you can't leave it to the default value None
229

230
    """
231
    # The implementation of this method is mandatory only if the new LU is
232
    # concurrent, so that old LUs don't need to be changed all at the same
233
    # time.
234
    if self.REQ_BGL:
235
      self.needed_locks = {} # Exclusive LUs don't need locks.
236
    else:
237
      raise NotImplementedError
238

    
239
  def DeclareLocks(self, level):
240
    """Declare LU locking needs for a level
241

242
    While most LUs can just declare their locking needs at ExpandNames time,
243
    sometimes there's the need to calculate some locks after having acquired
244
    the ones before. This function is called just before acquiring locks at a
245
    particular level, but after acquiring the ones at lower levels, and permits
246
    such calculations. It can be used to modify self.needed_locks, and by
247
    default it does nothing.
248

249
    This function is only called if you have something already set in
250
    self.needed_locks for the level.
251

252
    @param level: Locking level which is going to be locked
253
    @type level: member of ganeti.locking.LEVELS
254

255
    """
256

    
257
  def CheckPrereq(self):
258
    """Check prerequisites for this LU.
259

260
    This method should check that the prerequisites for the execution
261
    of this LU are fulfilled. It can do internode communication, but
262
    it should be idempotent - no cluster or system changes are
263
    allowed.
264

265
    The method should raise errors.OpPrereqError in case something is
266
    not fulfilled. Its return value is ignored.
267

268
    This method should also update all the parameters of the opcode to
269
    their canonical form if it hasn't been done by ExpandNames before.
270

271
    """
272
    if self.tasklets is not None:
273
      for (idx, tl) in enumerate(self.tasklets):
274
        logging.debug("Checking prerequisites for tasklet %s/%s",
275
                      idx + 1, len(self.tasklets))
276
        tl.CheckPrereq()
277
    else:
278
      pass
279

    
280
  def Exec(self, feedback_fn):
281
    """Execute the LU.
282

283
    This method should implement the actual work. It should raise
284
    errors.OpExecError for failures that are somewhat dealt with in
285
    code, or expected.
286

287
    """
288
    if self.tasklets is not None:
289
      for (idx, tl) in enumerate(self.tasklets):
290
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
291
        tl.Exec(feedback_fn)
292
    else:
293
      raise NotImplementedError
294

    
295
  def BuildHooksEnv(self):
296
    """Build hooks environment for this LU.
297

298
    @rtype: dict
299
    @return: Dictionary containing the environment that will be used for
300
      running the hooks for this LU. The keys of the dict must not be prefixed
301
      with "GANETI_"--that'll be added by the hooks runner. The hooks runner
302
      will extend the environment with additional variables. If no environment
303
      should be defined, an empty dictionary should be returned (not C{None}).
304
    @note: If the C{HPATH} attribute of the LU class is C{None}, this function
305
      will not be called.
306

307
    """
308
    raise NotImplementedError
309

    
310
  def BuildHooksNodes(self):
311
    """Build list of nodes to run LU's hooks.
312

313
    @rtype: tuple; (list, list)
314
    @return: Tuple containing a list of node names on which the hook
315
      should run before the execution and a list of node names on which the
316
      hook should run after the execution. No nodes should be returned as an
317
      empty list (and not None).
318
    @note: If the C{HPATH} attribute of the LU class is C{None}, this function
319
      will not be called.
320

321
    """
322
    raise NotImplementedError
323

    
324
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
325
    """Notify the LU about the results of its hooks.
326

327
    This method is called every time a hooks phase is executed, and notifies
328
    the Logical Unit about the hooks' result. The LU can then use it to alter
329
    its result based on the hooks.  By default the method does nothing and the
330
    previous result is passed back unchanged but any LU can define it if it
331
    wants to use the local cluster hook-scripts somehow.
332

333
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
334
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
335
    @param hook_results: the results of the multi-node hooks rpc call
336
    @param feedback_fn: function used send feedback back to the caller
337
    @param lu_result: the previous Exec result this LU had, or None
338
        in the PRE phase
339
    @return: the new Exec result, based on the previous result
340
        and hook results
341

342
    """
343
    # API must be kept, thus we ignore the unused argument and could
344
    # be a function warnings
345
    # pylint: disable-msg=W0613,R0201
346
    return lu_result
347

    
348
  def _ExpandAndLockInstance(self):
349
    """Helper function to expand and lock an instance.
350

351
    Many LUs that work on an instance take its name in self.op.instance_name
352
    and need to expand it and then declare the expanded name for locking. This
353
    function does it, and then updates self.op.instance_name to the expanded
354
    name. It also initializes needed_locks as a dict, if this hasn't been done
355
    before.
356

357
    """
358
    if self.needed_locks is None:
359
      self.needed_locks = {}
360
    else:
361
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
362
        "_ExpandAndLockInstance called with instance-level locks set"
363
    self.op.instance_name = _ExpandInstanceName(self.cfg,
364
                                                self.op.instance_name)
365
    self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
366

    
367
  def _LockInstancesNodes(self, primary_only=False):
368
    """Helper function to declare instances' nodes for locking.
369

370
    This function should be called after locking one or more instances to lock
371
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
372
    with all primary or secondary nodes for instances already locked and
373
    present in self.needed_locks[locking.LEVEL_INSTANCE].
374

375
    It should be called from DeclareLocks, and for safety only works if
376
    self.recalculate_locks[locking.LEVEL_NODE] is set.
377

378
    In the future it may grow parameters to just lock some instance's nodes, or
379
    to just lock primaries or secondary nodes, if needed.
380

381
    If should be called in DeclareLocks in a way similar to::
382

383
      if level == locking.LEVEL_NODE:
384
        self._LockInstancesNodes()
385

386
    @type primary_only: boolean
387
    @param primary_only: only lock primary nodes of locked instances
388

389
    """
390
    assert locking.LEVEL_NODE in self.recalculate_locks, \
391
      "_LockInstancesNodes helper function called with no nodes to recalculate"
392

    
393
    # TODO: check if we're really been called with the instance locks held
394

    
395
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
396
    # future we might want to have different behaviors depending on the value
397
    # of self.recalculate_locks[locking.LEVEL_NODE]
398
    wanted_nodes = []
399
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
400
      instance = self.context.cfg.GetInstanceInfo(instance_name)
401
      wanted_nodes.append(instance.primary_node)
402
      if not primary_only:
403
        wanted_nodes.extend(instance.secondary_nodes)
404

    
405
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
406
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
407
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
408
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
409

    
410
    del self.recalculate_locks[locking.LEVEL_NODE]
411

    
412

    
413
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
414
  """Simple LU which runs no hooks.
415

416
  This LU is intended as a parent for other LogicalUnits which will
417
  run no hooks, in order to reduce duplicate code.
418

419
  """
420
  HPATH = None
421
  HTYPE = None
422

    
423
  def BuildHooksEnv(self):
424
    """Empty BuildHooksEnv for NoHooksLu.
425

426
    This just raises an error.
427

428
    """
429
    raise AssertionError("BuildHooksEnv called for NoHooksLUs")
430

    
431
  def BuildHooksNodes(self):
432
    """Empty BuildHooksNodes for NoHooksLU.
433

434
    """
435
    raise AssertionError("BuildHooksNodes called for NoHooksLU")
436

    
437

    
438
class Tasklet:
439
  """Tasklet base class.
440

441
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
442
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
443
  tasklets know nothing about locks.
444

445
  Subclasses must follow these rules:
446
    - Implement CheckPrereq
447
    - Implement Exec
448

449
  """
450
  def __init__(self, lu):
451
    self.lu = lu
452

    
453
    # Shortcuts
454
    self.cfg = lu.cfg
455
    self.rpc = lu.rpc
456

    
457
  def CheckPrereq(self):
458
    """Check prerequisites for this tasklets.
459

460
    This method should check whether the prerequisites for the execution of
461
    this tasklet are fulfilled. It can do internode communication, but it
462
    should be idempotent - no cluster or system changes are allowed.
463

464
    The method should raise errors.OpPrereqError in case something is not
465
    fulfilled. Its return value is ignored.
466

467
    This method should also update all parameters to their canonical form if it
468
    hasn't been done before.
469

470
    """
471
    pass
472

    
473
  def Exec(self, feedback_fn):
474
    """Execute the tasklet.
475

476
    This method should implement the actual work. It should raise
477
    errors.OpExecError for failures that are somewhat dealt with in code, or
478
    expected.
479

480
    """
481
    raise NotImplementedError
482

    
483

    
484
class _QueryBase:
485
  """Base for query utility classes.
486

487
  """
488
  #: Attribute holding field definitions
489
  FIELDS = None
490

    
491
  def __init__(self, filter_, fields, use_locking):
492
    """Initializes this class.
493

494
    """
495
    self.use_locking = use_locking
496

    
497
    self.query = query.Query(self.FIELDS, fields, filter_=filter_,
498
                             namefield="name")
499
    self.requested_data = self.query.RequestedData()
500
    self.names = self.query.RequestedNames()
501

    
502
    # Sort only if no names were requested
503
    self.sort_by_name = not self.names
504

    
505
    self.do_locking = None
506
    self.wanted = None
507

    
508
  def _GetNames(self, lu, all_names, lock_level):
509
    """Helper function to determine names asked for in the query.
510

511
    """
512
    if self.do_locking:
513
      names = lu.acquired_locks[lock_level]
514
    else:
515
      names = all_names
516

    
517
    if self.wanted == locking.ALL_SET:
518
      assert not self.names
519
      # caller didn't specify names, so ordering is not important
520
      return utils.NiceSort(names)
521

    
522
    # caller specified names and we must keep the same order
523
    assert self.names
524
    assert not self.do_locking or lu.acquired_locks[lock_level]
525

    
526
    missing = set(self.wanted).difference(names)
527
    if missing:
528
      raise errors.OpExecError("Some items were removed before retrieving"
529
                               " their data: %s" % missing)
530

    
531
    # Return expanded names
532
    return self.wanted
533

    
534
  def ExpandNames(self, lu):
535
    """Expand names for this query.
536

537
    See L{LogicalUnit.ExpandNames}.
538

539
    """
540
    raise NotImplementedError()
541

    
542
  def DeclareLocks(self, lu, level):
543
    """Declare locks for this query.
544

545
    See L{LogicalUnit.DeclareLocks}.
546

547
    """
548
    raise NotImplementedError()
549

    
550
  def _GetQueryData(self, lu):
551
    """Collects all data for this query.
552

553
    @return: Query data object
554

555
    """
556
    raise NotImplementedError()
557

    
558
  def NewStyleQuery(self, lu):
559
    """Collect data and execute query.
560

561
    """
562
    return query.GetQueryResponse(self.query, self._GetQueryData(lu),
563
                                  sort_by_name=self.sort_by_name)
564

    
565
  def OldStyleQuery(self, lu):
566
    """Collect data and execute query.
567

568
    """
569
    return self.query.OldStyleQuery(self._GetQueryData(lu),
570
                                    sort_by_name=self.sort_by_name)
571

    
572

    
573
def _GetWantedNodes(lu, nodes):
574
  """Returns list of checked and expanded node names.
575

576
  @type lu: L{LogicalUnit}
577
  @param lu: the logical unit on whose behalf we execute
578
  @type nodes: list
579
  @param nodes: list of node names or None for all nodes
580
  @rtype: list
581
  @return: the list of nodes, sorted
582
  @raise errors.ProgrammerError: if the nodes parameter is wrong type
583

584
  """
585
  if nodes:
586
    return [_ExpandNodeName(lu.cfg, name) for name in nodes]
587

    
588
  return utils.NiceSort(lu.cfg.GetNodeList())
589

    
590

    
591
def _GetWantedInstances(lu, instances):
592
  """Returns list of checked and expanded instance names.
593

594
  @type lu: L{LogicalUnit}
595
  @param lu: the logical unit on whose behalf we execute
596
  @type instances: list
597
  @param instances: list of instance names or None for all instances
598
  @rtype: list
599
  @return: the list of instances, sorted
600
  @raise errors.OpPrereqError: if the instances parameter is wrong type
601
  @raise errors.OpPrereqError: if any of the passed instances is not found
602

603
  """
604
  if instances:
605
    wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
606
  else:
607
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
608
  return wanted
609

    
610

    
611
def _GetUpdatedParams(old_params, update_dict,
612
                      use_default=True, use_none=False):
613
  """Return the new version of a parameter dictionary.
614

615
  @type old_params: dict
616
  @param old_params: old parameters
617
  @type update_dict: dict
618
  @param update_dict: dict containing new parameter values, or
619
      constants.VALUE_DEFAULT to reset the parameter to its default
620
      value
621
  @param use_default: boolean
622
  @type use_default: whether to recognise L{constants.VALUE_DEFAULT}
623
      values as 'to be deleted' values
624
  @param use_none: boolean
625
  @type use_none: whether to recognise C{None} values as 'to be
626
      deleted' values
627
  @rtype: dict
628
  @return: the new parameter dictionary
629

630
  """
631
  params_copy = copy.deepcopy(old_params)
632
  for key, val in update_dict.iteritems():
633
    if ((use_default and val == constants.VALUE_DEFAULT) or
634
        (use_none and val is None)):
635
      try:
636
        del params_copy[key]
637
      except KeyError:
638
        pass
639
    else:
640
      params_copy[key] = val
641
  return params_copy
642

    
643

    
644
def _RunPostHook(lu, node_name):
645
  """Runs the post-hook for an opcode on a single node.
646

647
  """
648
  hm = lu.proc.hmclass(lu.rpc.call_hooks_runner, lu)
649
  try:
650
    hm.RunPhase(constants.HOOKS_PHASE_POST, nodes=[node_name])
651
  except:
652
    # pylint: disable-msg=W0702
653
    lu.LogWarning("Errors occurred running hooks on %s" % node_name)
654

    
655

    
656
def _CheckOutputFields(static, dynamic, selected):
657
  """Checks whether all selected fields are valid.
658

659
  @type static: L{utils.FieldSet}
660
  @param static: static fields set
661
  @type dynamic: L{utils.FieldSet}
662
  @param dynamic: dynamic fields set
663

664
  """
665
  f = utils.FieldSet()
666
  f.Extend(static)
667
  f.Extend(dynamic)
668

    
669
  delta = f.NonMatching(selected)
670
  if delta:
671
    raise errors.OpPrereqError("Unknown output fields selected: %s"
672
                               % ",".join(delta), errors.ECODE_INVAL)
673

    
674

    
675
def _CheckGlobalHvParams(params):
676
  """Validates that given hypervisor params are not global ones.
677

678
  This will ensure that instances don't get customised versions of
679
  global params.
680

681
  """
682
  used_globals = constants.HVC_GLOBALS.intersection(params)
683
  if used_globals:
684
    msg = ("The following hypervisor parameters are global and cannot"
685
           " be customized at instance level, please modify them at"
686
           " cluster level: %s" % utils.CommaJoin(used_globals))
687
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
688

    
689

    
690
def _CheckNodeOnline(lu, node, msg=None):
691
  """Ensure that a given node is online.
692

693
  @param lu: the LU on behalf of which we make the check
694
  @param node: the node to check
695
  @param msg: if passed, should be a message to replace the default one
696
  @raise errors.OpPrereqError: if the node is offline
697

698
  """
699
  if msg is None:
700
    msg = "Can't use offline node"
701
  if lu.cfg.GetNodeInfo(node).offline:
702
    raise errors.OpPrereqError("%s: %s" % (msg, node), errors.ECODE_STATE)
703

    
704

    
705
def _CheckNodeNotDrained(lu, node):
706
  """Ensure that a given node is not drained.
707

708
  @param lu: the LU on behalf of which we make the check
709
  @param node: the node to check
710
  @raise errors.OpPrereqError: if the node is drained
711

712
  """
713
  if lu.cfg.GetNodeInfo(node).drained:
714
    raise errors.OpPrereqError("Can't use drained node %s" % node,
715
                               errors.ECODE_STATE)
716

    
717

    
718
def _CheckNodeVmCapable(lu, node):
719
  """Ensure that a given node is vm capable.
720

721
  @param lu: the LU on behalf of which we make the check
722
  @param node: the node to check
723
  @raise errors.OpPrereqError: if the node is not vm capable
724

725
  """
726
  if not lu.cfg.GetNodeInfo(node).vm_capable:
727
    raise errors.OpPrereqError("Can't use non-vm_capable node %s" % node,
728
                               errors.ECODE_STATE)
729

    
730

    
731
def _CheckNodeHasOS(lu, node, os_name, force_variant):
732
  """Ensure that a node supports a given OS.
733

734
  @param lu: the LU on behalf of which we make the check
735
  @param node: the node to check
736
  @param os_name: the OS to query about
737
  @param force_variant: whether to ignore variant errors
738
  @raise errors.OpPrereqError: if the node is not supporting the OS
739

740
  """
741
  result = lu.rpc.call_os_get(node, os_name)
742
  result.Raise("OS '%s' not in supported OS list for node %s" %
743
               (os_name, node),
744
               prereq=True, ecode=errors.ECODE_INVAL)
745
  if not force_variant:
746
    _CheckOSVariant(result.payload, os_name)
747

    
748

    
749
def _CheckNodeHasSecondaryIP(lu, node, secondary_ip, prereq):
750
  """Ensure that a node has the given secondary ip.
751

752
  @type lu: L{LogicalUnit}
753
  @param lu: the LU on behalf of which we make the check
754
  @type node: string
755
  @param node: the node to check
756
  @type secondary_ip: string
757
  @param secondary_ip: the ip to check
758
  @type prereq: boolean
759
  @param prereq: whether to throw a prerequisite or an execute error
760
  @raise errors.OpPrereqError: if the node doesn't have the ip, and prereq=True
761
  @raise errors.OpExecError: if the node doesn't have the ip, and prereq=False
762

763
  """
764
  result = lu.rpc.call_node_has_ip_address(node, secondary_ip)
765
  result.Raise("Failure checking secondary ip on node %s" % node,
766
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
767
  if not result.payload:
768
    msg = ("Node claims it doesn't have the secondary ip you gave (%s),"
769
           " please fix and re-run this command" % secondary_ip)
770
    if prereq:
771
      raise errors.OpPrereqError(msg, errors.ECODE_ENVIRON)
772
    else:
773
      raise errors.OpExecError(msg)
774

    
775

    
776
def _GetClusterDomainSecret():
777
  """Reads the cluster domain secret.
778

779
  """
780
  return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
781
                               strict=True)
782

    
783

    
784
def _CheckInstanceDown(lu, instance, reason):
785
  """Ensure that an instance is not running."""
786
  if instance.admin_up:
787
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
788
                               (instance.name, reason), errors.ECODE_STATE)
789

    
790
  pnode = instance.primary_node
791
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
792
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
793
              prereq=True, ecode=errors.ECODE_ENVIRON)
794

    
795
  if instance.name in ins_l.payload:
796
    raise errors.OpPrereqError("Instance %s is running, %s" %
797
                               (instance.name, reason), errors.ECODE_STATE)
798

    
799

    
800
def _ExpandItemName(fn, name, kind):
801
  """Expand an item name.
802

803
  @param fn: the function to use for expansion
804
  @param name: requested item name
805
  @param kind: text description ('Node' or 'Instance')
806
  @return: the resolved (full) name
807
  @raise errors.OpPrereqError: if the item is not found
808

809
  """
810
  full_name = fn(name)
811
  if full_name is None:
812
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
813
                               errors.ECODE_NOENT)
814
  return full_name
815

    
816

    
817
def _ExpandNodeName(cfg, name):
818
  """Wrapper over L{_ExpandItemName} for nodes."""
819
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
820

    
821

    
822
def _ExpandInstanceName(cfg, name):
823
  """Wrapper over L{_ExpandItemName} for instance."""
824
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
825

    
826

    
827
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
828
                          memory, vcpus, nics, disk_template, disks,
829
                          bep, hvp, hypervisor_name):
830
  """Builds instance related env variables for hooks
831

832
  This builds the hook environment from individual variables.
833

834
  @type name: string
835
  @param name: the name of the instance
836
  @type primary_node: string
837
  @param primary_node: the name of the instance's primary node
838
  @type secondary_nodes: list
839
  @param secondary_nodes: list of secondary nodes as strings
840
  @type os_type: string
841
  @param os_type: the name of the instance's OS
842
  @type status: boolean
843
  @param status: the should_run status of the instance
844
  @type memory: string
845
  @param memory: the memory size of the instance
846
  @type vcpus: string
847
  @param vcpus: the count of VCPUs the instance has
848
  @type nics: list
849
  @param nics: list of tuples (ip, mac, mode, link) representing
850
      the NICs the instance has
851
  @type disk_template: string
852
  @param disk_template: the disk template of the instance
853
  @type disks: list
854
  @param disks: the list of (size, mode) pairs
855
  @type bep: dict
856
  @param bep: the backend parameters for the instance
857
  @type hvp: dict
858
  @param hvp: the hypervisor parameters for the instance
859
  @type hypervisor_name: string
860
  @param hypervisor_name: the hypervisor for the instance
861
  @rtype: dict
862
  @return: the hook environment for this instance
863

864
  """
865
  if status:
866
    str_status = "up"
867
  else:
868
    str_status = "down"
869
  env = {
870
    "OP_TARGET": name,
871
    "INSTANCE_NAME": name,
872
    "INSTANCE_PRIMARY": primary_node,
873
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
874
    "INSTANCE_OS_TYPE": os_type,
875
    "INSTANCE_STATUS": str_status,
876
    "INSTANCE_MEMORY": memory,
877
    "INSTANCE_VCPUS": vcpus,
878
    "INSTANCE_DISK_TEMPLATE": disk_template,
879
    "INSTANCE_HYPERVISOR": hypervisor_name,
880
  }
881

    
882
  if nics:
883
    nic_count = len(nics)
884
    for idx, (ip, mac, mode, link) in enumerate(nics):
885
      if ip is None:
886
        ip = ""
887
      env["INSTANCE_NIC%d_IP" % idx] = ip
888
      env["INSTANCE_NIC%d_MAC" % idx] = mac
889
      env["INSTANCE_NIC%d_MODE" % idx] = mode
890
      env["INSTANCE_NIC%d_LINK" % idx] = link
891
      if mode == constants.NIC_MODE_BRIDGED:
892
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
893
  else:
894
    nic_count = 0
895

    
896
  env["INSTANCE_NIC_COUNT"] = nic_count
897

    
898
  if disks:
899
    disk_count = len(disks)
900
    for idx, (size, mode) in enumerate(disks):
901
      env["INSTANCE_DISK%d_SIZE" % idx] = size
902
      env["INSTANCE_DISK%d_MODE" % idx] = mode
903
  else:
904
    disk_count = 0
905

    
906
  env["INSTANCE_DISK_COUNT"] = disk_count
907

    
908
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
909
    for key, value in source.items():
910
      env["INSTANCE_%s_%s" % (kind, key)] = value
911

    
912
  return env
913

    
914

    
915
def _NICListToTuple(lu, nics):
916
  """Build a list of nic information tuples.
917

918
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
919
  value in LUInstanceQueryData.
920

921
  @type lu:  L{LogicalUnit}
922
  @param lu: the logical unit on whose behalf we execute
923
  @type nics: list of L{objects.NIC}
924
  @param nics: list of nics to convert to hooks tuples
925

926
  """
927
  hooks_nics = []
928
  cluster = lu.cfg.GetClusterInfo()
929
  for nic in nics:
930
    ip = nic.ip
931
    mac = nic.mac
932
    filled_params = cluster.SimpleFillNIC(nic.nicparams)
933
    mode = filled_params[constants.NIC_MODE]
934
    link = filled_params[constants.NIC_LINK]
935
    hooks_nics.append((ip, mac, mode, link))
936
  return hooks_nics
937

    
938

    
939
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
940
  """Builds instance related env variables for hooks from an object.
941

942
  @type lu: L{LogicalUnit}
943
  @param lu: the logical unit on whose behalf we execute
944
  @type instance: L{objects.Instance}
945
  @param instance: the instance for which we should build the
946
      environment
947
  @type override: dict
948
  @param override: dictionary with key/values that will override
949
      our values
950
  @rtype: dict
951
  @return: the hook environment dictionary
952

953
  """
954
  cluster = lu.cfg.GetClusterInfo()
955
  bep = cluster.FillBE(instance)
956
  hvp = cluster.FillHV(instance)
957
  args = {
958
    'name': instance.name,
959
    'primary_node': instance.primary_node,
960
    'secondary_nodes': instance.secondary_nodes,
961
    'os_type': instance.os,
962
    'status': instance.admin_up,
963
    'memory': bep[constants.BE_MEMORY],
964
    'vcpus': bep[constants.BE_VCPUS],
965
    'nics': _NICListToTuple(lu, instance.nics),
966
    'disk_template': instance.disk_template,
967
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
968
    'bep': bep,
969
    'hvp': hvp,
970
    'hypervisor_name': instance.hypervisor,
971
  }
972
  if override:
973
    args.update(override)
974
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
975

    
976

    
977
def _AdjustCandidatePool(lu, exceptions):
978
  """Adjust the candidate pool after node operations.
979

980
  """
981
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
982
  if mod_list:
983
    lu.LogInfo("Promoted nodes to master candidate role: %s",
984
               utils.CommaJoin(node.name for node in mod_list))
985
    for name in mod_list:
986
      lu.context.ReaddNode(name)
987
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
988
  if mc_now > mc_max:
989
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
990
               (mc_now, mc_max))
991

    
992

    
993
def _DecideSelfPromotion(lu, exceptions=None):
994
  """Decide whether I should promote myself as a master candidate.
995

996
  """
997
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
998
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
999
  # the new node will increase mc_max with one, so:
1000
  mc_should = min(mc_should + 1, cp_size)
1001
  return mc_now < mc_should
1002

    
1003

    
1004
def _CheckNicsBridgesExist(lu, target_nics, target_node):
1005
  """Check that the brigdes needed by a list of nics exist.
1006

1007
  """
1008
  cluster = lu.cfg.GetClusterInfo()
1009
  paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics]
1010
  brlist = [params[constants.NIC_LINK] for params in paramslist
1011
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
1012
  if brlist:
1013
    result = lu.rpc.call_bridges_exist(target_node, brlist)
1014
    result.Raise("Error checking bridges on destination node '%s'" %
1015
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
1016

    
1017

    
1018
def _CheckInstanceBridgesExist(lu, instance, node=None):
1019
  """Check that the brigdes needed by an instance exist.
1020

1021
  """
1022
  if node is None:
1023
    node = instance.primary_node
1024
  _CheckNicsBridgesExist(lu, instance.nics, node)
1025

    
1026

    
1027
def _CheckOSVariant(os_obj, name):
1028
  """Check whether an OS name conforms to the os variants specification.
1029

1030
  @type os_obj: L{objects.OS}
1031
  @param os_obj: OS object to check
1032
  @type name: string
1033
  @param name: OS name passed by the user, to check for validity
1034

1035
  """
1036
  if not os_obj.supported_variants:
1037
    return
1038
  variant = objects.OS.GetVariant(name)
1039
  if not variant:
1040
    raise errors.OpPrereqError("OS name must include a variant",
1041
                               errors.ECODE_INVAL)
1042

    
1043
  if variant not in os_obj.supported_variants:
1044
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
1045

    
1046

    
1047
def _GetNodeInstancesInner(cfg, fn):
1048
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
1049

    
1050

    
1051
def _GetNodeInstances(cfg, node_name):
1052
  """Returns a list of all primary and secondary instances on a node.
1053

1054
  """
1055

    
1056
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
1057

    
1058

    
1059
def _GetNodePrimaryInstances(cfg, node_name):
1060
  """Returns primary instances on a node.
1061

1062
  """
1063
  return _GetNodeInstancesInner(cfg,
1064
                                lambda inst: node_name == inst.primary_node)
1065

    
1066

    
1067
def _GetNodeSecondaryInstances(cfg, node_name):
1068
  """Returns secondary instances on a node.
1069

1070
  """
1071
  return _GetNodeInstancesInner(cfg,
1072
                                lambda inst: node_name in inst.secondary_nodes)
1073

    
1074

    
1075
def _GetStorageTypeArgs(cfg, storage_type):
1076
  """Returns the arguments for a storage type.
1077

1078
  """
1079
  # Special case for file storage
1080
  if storage_type == constants.ST_FILE:
1081
    # storage.FileStorage wants a list of storage directories
1082
    return [[cfg.GetFileStorageDir(), cfg.GetSharedFileStorageDir()]]
1083

    
1084
  return []
1085

    
1086

    
1087
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
1088
  faulty = []
1089

    
1090
  for dev in instance.disks:
1091
    cfg.SetDiskID(dev, node_name)
1092

    
1093
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
1094
  result.Raise("Failed to get disk status from node %s" % node_name,
1095
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
1096

    
1097
  for idx, bdev_status in enumerate(result.payload):
1098
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
1099
      faulty.append(idx)
1100

    
1101
  return faulty
1102

    
1103

    
1104
def _CheckIAllocatorOrNode(lu, iallocator_slot, node_slot):
1105
  """Check the sanity of iallocator and node arguments and use the
1106
  cluster-wide iallocator if appropriate.
1107

1108
  Check that at most one of (iallocator, node) is specified. If none is
1109
  specified, then the LU's opcode's iallocator slot is filled with the
1110
  cluster-wide default iallocator.
1111

1112
  @type iallocator_slot: string
1113
  @param iallocator_slot: the name of the opcode iallocator slot
1114
  @type node_slot: string
1115
  @param node_slot: the name of the opcode target node slot
1116

1117
  """
1118
  node = getattr(lu.op, node_slot, None)
1119
  iallocator = getattr(lu.op, iallocator_slot, None)
1120

    
1121
  if node is not None and iallocator is not None:
1122
    raise errors.OpPrereqError("Do not specify both, iallocator and node.",
1123
                               errors.ECODE_INVAL)
1124
  elif node is None and iallocator is None:
1125
    default_iallocator = lu.cfg.GetDefaultIAllocator()
1126
    if default_iallocator:
1127
      setattr(lu.op, iallocator_slot, default_iallocator)
1128
    else:
1129
      raise errors.OpPrereqError("No iallocator or node given and no"
1130
                                 " cluster-wide default iallocator found."
1131
                                 " Please specify either an iallocator or a"
1132
                                 " node, or set a cluster-wide default"
1133
                                 " iallocator.")
1134

    
1135

    
1136
class LUClusterPostInit(LogicalUnit):
1137
  """Logical unit for running hooks after cluster initialization.
1138

1139
  """
1140
  HPATH = "cluster-init"
1141
  HTYPE = constants.HTYPE_CLUSTER
1142

    
1143
  def BuildHooksEnv(self):
1144
    """Build hooks env.
1145

1146
    """
1147
    return {
1148
      "OP_TARGET": self.cfg.GetClusterName(),
1149
      }
1150

    
1151
  def BuildHooksNodes(self):
1152
    """Build hooks nodes.
1153

1154
    """
1155
    return ([], [self.cfg.GetMasterNode()])
1156

    
1157
  def Exec(self, feedback_fn):
1158
    """Nothing to do.
1159

1160
    """
1161
    return True
1162

    
1163

    
1164
class LUClusterDestroy(LogicalUnit):
1165
  """Logical unit for destroying the cluster.
1166

1167
  """
1168
  HPATH = "cluster-destroy"
1169
  HTYPE = constants.HTYPE_CLUSTER
1170

    
1171
  def BuildHooksEnv(self):
1172
    """Build hooks env.
1173

1174
    """
1175
    return {
1176
      "OP_TARGET": self.cfg.GetClusterName(),
1177
      }
1178

    
1179
  def BuildHooksNodes(self):
1180
    """Build hooks nodes.
1181

1182
    """
1183
    return ([], [])
1184

    
1185
  def CheckPrereq(self):
1186
    """Check prerequisites.
1187

1188
    This checks whether the cluster is empty.
1189

1190
    Any errors are signaled by raising errors.OpPrereqError.
1191

1192
    """
1193
    master = self.cfg.GetMasterNode()
1194

    
1195
    nodelist = self.cfg.GetNodeList()
1196
    if len(nodelist) != 1 or nodelist[0] != master:
1197
      raise errors.OpPrereqError("There are still %d node(s) in"
1198
                                 " this cluster." % (len(nodelist) - 1),
1199
                                 errors.ECODE_INVAL)
1200
    instancelist = self.cfg.GetInstanceList()
1201
    if instancelist:
1202
      raise errors.OpPrereqError("There are still %d instance(s) in"
1203
                                 " this cluster." % len(instancelist),
1204
                                 errors.ECODE_INVAL)
1205

    
1206
  def Exec(self, feedback_fn):
1207
    """Destroys the cluster.
1208

1209
    """
1210
    master = self.cfg.GetMasterNode()
1211

    
1212
    # Run post hooks on master node before it's removed
1213
    _RunPostHook(self, master)
1214

    
1215
    result = self.rpc.call_node_stop_master(master, False)
1216
    result.Raise("Could not disable the master role")
1217

    
1218
    return master
1219

    
1220

    
1221
def _VerifyCertificate(filename):
1222
  """Verifies a certificate for LUClusterVerify.
1223

1224
  @type filename: string
1225
  @param filename: Path to PEM file
1226

1227
  """
1228
  try:
1229
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1230
                                           utils.ReadFile(filename))
1231
  except Exception, err: # pylint: disable-msg=W0703
1232
    return (LUClusterVerify.ETYPE_ERROR,
1233
            "Failed to load X509 certificate %s: %s" % (filename, err))
1234

    
1235
  (errcode, msg) = \
1236
    utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1237
                                constants.SSL_CERT_EXPIRATION_ERROR)
1238

    
1239
  if msg:
1240
    fnamemsg = "While verifying %s: %s" % (filename, msg)
1241
  else:
1242
    fnamemsg = None
1243

    
1244
  if errcode is None:
1245
    return (None, fnamemsg)
1246
  elif errcode == utils.CERT_WARNING:
1247
    return (LUClusterVerify.ETYPE_WARNING, fnamemsg)
1248
  elif errcode == utils.CERT_ERROR:
1249
    return (LUClusterVerify.ETYPE_ERROR, fnamemsg)
1250

    
1251
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1252

    
1253

    
1254
class LUClusterVerify(LogicalUnit):
1255
  """Verifies the cluster status.
1256

1257
  """
1258
  HPATH = "cluster-verify"
1259
  HTYPE = constants.HTYPE_CLUSTER
1260
  REQ_BGL = False
1261

    
1262
  TCLUSTER = "cluster"
1263
  TNODE = "node"
1264
  TINSTANCE = "instance"
1265

    
1266
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1267
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1268
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1269
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1270
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1271
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1272
  EINSTANCEFAULTYDISK = (TINSTANCE, "EINSTANCEFAULTYDISK")
1273
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1274
  EINSTANCESPLITGROUPS = (TINSTANCE, "EINSTANCESPLITGROUPS")
1275
  ENODEDRBD = (TNODE, "ENODEDRBD")
1276
  ENODEDRBDHELPER = (TNODE, "ENODEDRBDHELPER")
1277
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1278
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1279
  ENODEHV = (TNODE, "ENODEHV")
1280
  ENODELVM = (TNODE, "ENODELVM")
1281
  ENODEN1 = (TNODE, "ENODEN1")
1282
  ENODENET = (TNODE, "ENODENET")
1283
  ENODEOS = (TNODE, "ENODEOS")
1284
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1285
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1286
  ENODERPC = (TNODE, "ENODERPC")
1287
  ENODESSH = (TNODE, "ENODESSH")
1288
  ENODEVERSION = (TNODE, "ENODEVERSION")
1289
  ENODESETUP = (TNODE, "ENODESETUP")
1290
  ENODETIME = (TNODE, "ENODETIME")
1291
  ENODEOOBPATH = (TNODE, "ENODEOOBPATH")
1292

    
1293
  ETYPE_FIELD = "code"
1294
  ETYPE_ERROR = "ERROR"
1295
  ETYPE_WARNING = "WARNING"
1296

    
1297
  _HOOKS_INDENT_RE = re.compile("^", re.M)
1298

    
1299
  class NodeImage(object):
1300
    """A class representing the logical and physical status of a node.
1301

1302
    @type name: string
1303
    @ivar name: the node name to which this object refers
1304
    @ivar volumes: a structure as returned from
1305
        L{ganeti.backend.GetVolumeList} (runtime)
1306
    @ivar instances: a list of running instances (runtime)
1307
    @ivar pinst: list of configured primary instances (config)
1308
    @ivar sinst: list of configured secondary instances (config)
1309
    @ivar sbp: dictionary of {primary-node: list of instances} for all
1310
        instances for which this node is secondary (config)
1311
    @ivar mfree: free memory, as reported by hypervisor (runtime)
1312
    @ivar dfree: free disk, as reported by the node (runtime)
1313
    @ivar offline: the offline status (config)
1314
    @type rpc_fail: boolean
1315
    @ivar rpc_fail: whether the RPC verify call was successfull (overall,
1316
        not whether the individual keys were correct) (runtime)
1317
    @type lvm_fail: boolean
1318
    @ivar lvm_fail: whether the RPC call didn't return valid LVM data
1319
    @type hyp_fail: boolean
1320
    @ivar hyp_fail: whether the RPC call didn't return the instance list
1321
    @type ghost: boolean
1322
    @ivar ghost: whether this is a known node or not (config)
1323
    @type os_fail: boolean
1324
    @ivar os_fail: whether the RPC call didn't return valid OS data
1325
    @type oslist: list
1326
    @ivar oslist: list of OSes as diagnosed by DiagnoseOS
1327
    @type vm_capable: boolean
1328
    @ivar vm_capable: whether the node can host instances
1329

1330
    """
1331
    def __init__(self, offline=False, name=None, vm_capable=True):
1332
      self.name = name
1333
      self.volumes = {}
1334
      self.instances = []
1335
      self.pinst = []
1336
      self.sinst = []
1337
      self.sbp = {}
1338
      self.mfree = 0
1339
      self.dfree = 0
1340
      self.offline = offline
1341
      self.vm_capable = vm_capable
1342
      self.rpc_fail = False
1343
      self.lvm_fail = False
1344
      self.hyp_fail = False
1345
      self.ghost = False
1346
      self.os_fail = False
1347
      self.oslist = {}
1348

    
1349
  def ExpandNames(self):
1350
    self.needed_locks = {
1351
      locking.LEVEL_NODE: locking.ALL_SET,
1352
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1353
    }
1354
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1355

    
1356
  def _Error(self, ecode, item, msg, *args, **kwargs):
1357
    """Format an error message.
1358

1359
    Based on the opcode's error_codes parameter, either format a
1360
    parseable error code, or a simpler error string.
1361

1362
    This must be called only from Exec and functions called from Exec.
1363

1364
    """
1365
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1366
    itype, etxt = ecode
1367
    # first complete the msg
1368
    if args:
1369
      msg = msg % args
1370
    # then format the whole message
1371
    if self.op.error_codes:
1372
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1373
    else:
1374
      if item:
1375
        item = " " + item
1376
      else:
1377
        item = ""
1378
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1379
    # and finally report it via the feedback_fn
1380
    self._feedback_fn("  - %s" % msg)
1381

    
1382
  def _ErrorIf(self, cond, *args, **kwargs):
1383
    """Log an error message if the passed condition is True.
1384

1385
    """
1386
    cond = bool(cond) or self.op.debug_simulate_errors
1387
    if cond:
1388
      self._Error(*args, **kwargs)
1389
    # do not mark the operation as failed for WARN cases only
1390
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1391
      self.bad = self.bad or cond
1392

    
1393
  def _VerifyNode(self, ninfo, nresult):
1394
    """Perform some basic validation on data returned from a node.
1395

1396
      - check the result data structure is well formed and has all the
1397
        mandatory fields
1398
      - check ganeti version
1399

1400
    @type ninfo: L{objects.Node}
1401
    @param ninfo: the node to check
1402
    @param nresult: the results from the node
1403
    @rtype: boolean
1404
    @return: whether overall this call was successful (and we can expect
1405
         reasonable values in the respose)
1406

1407
    """
1408
    node = ninfo.name
1409
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1410

    
1411
    # main result, nresult should be a non-empty dict
1412
    test = not nresult or not isinstance(nresult, dict)
1413
    _ErrorIf(test, self.ENODERPC, node,
1414
                  "unable to verify node: no data returned")
1415
    if test:
1416
      return False
1417

    
1418
    # compares ganeti version
1419
    local_version = constants.PROTOCOL_VERSION
1420
    remote_version = nresult.get("version", None)
1421
    test = not (remote_version and
1422
                isinstance(remote_version, (list, tuple)) and
1423
                len(remote_version) == 2)
1424
    _ErrorIf(test, self.ENODERPC, node,
1425
             "connection to node returned invalid data")
1426
    if test:
1427
      return False
1428

    
1429
    test = local_version != remote_version[0]
1430
    _ErrorIf(test, self.ENODEVERSION, node,
1431
             "incompatible protocol versions: master %s,"
1432
             " node %s", local_version, remote_version[0])
1433
    if test:
1434
      return False
1435

    
1436
    # node seems compatible, we can actually try to look into its results
1437

    
1438
    # full package version
1439
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1440
                  self.ENODEVERSION, node,
1441
                  "software version mismatch: master %s, node %s",
1442
                  constants.RELEASE_VERSION, remote_version[1],
1443
                  code=self.ETYPE_WARNING)
1444

    
1445
    hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1446
    if ninfo.vm_capable and isinstance(hyp_result, dict):
1447
      for hv_name, hv_result in hyp_result.iteritems():
1448
        test = hv_result is not None
1449
        _ErrorIf(test, self.ENODEHV, node,
1450
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1451

    
1452
    hvp_result = nresult.get(constants.NV_HVPARAMS, None)
1453
    if ninfo.vm_capable and isinstance(hvp_result, list):
1454
      for item, hv_name, hv_result in hvp_result:
1455
        _ErrorIf(True, self.ENODEHV, node,
1456
                 "hypervisor %s parameter verify failure (source %s): %s",
1457
                 hv_name, item, hv_result)
1458

    
1459
    test = nresult.get(constants.NV_NODESETUP,
1460
                           ["Missing NODESETUP results"])
1461
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1462
             "; ".join(test))
1463

    
1464
    return True
1465

    
1466
  def _VerifyNodeTime(self, ninfo, nresult,
1467
                      nvinfo_starttime, nvinfo_endtime):
1468
    """Check the node time.
1469

1470
    @type ninfo: L{objects.Node}
1471
    @param ninfo: the node to check
1472
    @param nresult: the remote results for the node
1473
    @param nvinfo_starttime: the start time of the RPC call
1474
    @param nvinfo_endtime: the end time of the RPC call
1475

1476
    """
1477
    node = ninfo.name
1478
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1479

    
1480
    ntime = nresult.get(constants.NV_TIME, None)
1481
    try:
1482
      ntime_merged = utils.MergeTime(ntime)
1483
    except (ValueError, TypeError):
1484
      _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1485
      return
1486

    
1487
    if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1488
      ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1489
    elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1490
      ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1491
    else:
1492
      ntime_diff = None
1493

    
1494
    _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1495
             "Node time diverges by at least %s from master node time",
1496
             ntime_diff)
1497

    
1498
  def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1499
    """Check the node time.
1500

1501
    @type ninfo: L{objects.Node}
1502
    @param ninfo: the node to check
1503
    @param nresult: the remote results for the node
1504
    @param vg_name: the configured VG name
1505

1506
    """
1507
    if vg_name is None:
1508
      return
1509

    
1510
    node = ninfo.name
1511
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1512

    
1513
    # checks vg existence and size > 20G
1514
    vglist = nresult.get(constants.NV_VGLIST, None)
1515
    test = not vglist
1516
    _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1517
    if not test:
1518
      vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1519
                                            constants.MIN_VG_SIZE)
1520
      _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1521

    
1522
    # check pv names
1523
    pvlist = nresult.get(constants.NV_PVLIST, None)
1524
    test = pvlist is None
1525
    _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1526
    if not test:
1527
      # check that ':' is not present in PV names, since it's a
1528
      # special character for lvcreate (denotes the range of PEs to
1529
      # use on the PV)
1530
      for _, pvname, owner_vg in pvlist:
1531
        test = ":" in pvname
1532
        _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1533
                 " '%s' of VG '%s'", pvname, owner_vg)
1534

    
1535
  def _VerifyNodeNetwork(self, ninfo, nresult):
1536
    """Check the node time.
1537

1538
    @type ninfo: L{objects.Node}
1539
    @param ninfo: the node to check
1540
    @param nresult: the remote results for the node
1541

1542
    """
1543
    node = ninfo.name
1544
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1545

    
1546
    test = constants.NV_NODELIST not in nresult
1547
    _ErrorIf(test, self.ENODESSH, node,
1548
             "node hasn't returned node ssh connectivity data")
1549
    if not test:
1550
      if nresult[constants.NV_NODELIST]:
1551
        for a_node, a_msg in nresult[constants.NV_NODELIST].items():
1552
          _ErrorIf(True, self.ENODESSH, node,
1553
                   "ssh communication with node '%s': %s", a_node, a_msg)
1554

    
1555
    test = constants.NV_NODENETTEST not in nresult
1556
    _ErrorIf(test, self.ENODENET, node,
1557
             "node hasn't returned node tcp connectivity data")
1558
    if not test:
1559
      if nresult[constants.NV_NODENETTEST]:
1560
        nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys())
1561
        for anode in nlist:
1562
          _ErrorIf(True, self.ENODENET, node,
1563
                   "tcp communication with node '%s': %s",
1564
                   anode, nresult[constants.NV_NODENETTEST][anode])
1565

    
1566
    test = constants.NV_MASTERIP not in nresult
1567
    _ErrorIf(test, self.ENODENET, node,
1568
             "node hasn't returned node master IP reachability data")
1569
    if not test:
1570
      if not nresult[constants.NV_MASTERIP]:
1571
        if node == self.master_node:
1572
          msg = "the master node cannot reach the master IP (not configured?)"
1573
        else:
1574
          msg = "cannot reach the master IP"
1575
        _ErrorIf(True, self.ENODENET, node, msg)
1576

    
1577
  def _VerifyInstance(self, instance, instanceconfig, node_image,
1578
                      diskstatus):
1579
    """Verify an instance.
1580

1581
    This function checks to see if the required block devices are
1582
    available on the instance's node.
1583

1584
    """
1585
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1586
    node_current = instanceconfig.primary_node
1587

    
1588
    node_vol_should = {}
1589
    instanceconfig.MapLVsByNode(node_vol_should)
1590

    
1591
    for node in node_vol_should:
1592
      n_img = node_image[node]
1593
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1594
        # ignore missing volumes on offline or broken nodes
1595
        continue
1596
      for volume in node_vol_should[node]:
1597
        test = volume not in n_img.volumes
1598
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1599
                 "volume %s missing on node %s", volume, node)
1600

    
1601
    if instanceconfig.admin_up:
1602
      pri_img = node_image[node_current]
1603
      test = instance not in pri_img.instances and not pri_img.offline
1604
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1605
               "instance not running on its primary node %s",
1606
               node_current)
1607

    
1608
    for node, n_img in node_image.items():
1609
      if node != node_current:
1610
        test = instance in n_img.instances
1611
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1612
                 "instance should not run on node %s", node)
1613

    
1614
    diskdata = [(nname, success, status, idx)
1615
                for (nname, disks) in diskstatus.items()
1616
                for idx, (success, status) in enumerate(disks)]
1617

    
1618
    for nname, success, bdev_status, idx in diskdata:
1619
      # the 'ghost node' construction in Exec() ensures that we have a
1620
      # node here
1621
      snode = node_image[nname]
1622
      bad_snode = snode.ghost or snode.offline
1623
      _ErrorIf(instanceconfig.admin_up and not success and not bad_snode,
1624
               self.EINSTANCEFAULTYDISK, instance,
1625
               "couldn't retrieve status for disk/%s on %s: %s",
1626
               idx, nname, bdev_status)
1627
      _ErrorIf((instanceconfig.admin_up and success and
1628
                bdev_status.ldisk_status == constants.LDS_FAULTY),
1629
               self.EINSTANCEFAULTYDISK, instance,
1630
               "disk/%s on %s is faulty", idx, nname)
1631

    
1632
  def _VerifyOrphanVolumes(self, node_vol_should, node_image, reserved):
1633
    """Verify if there are any unknown volumes in the cluster.
1634

1635
    The .os, .swap and backup volumes are ignored. All other volumes are
1636
    reported as unknown.
1637

1638
    @type reserved: L{ganeti.utils.FieldSet}
1639
    @param reserved: a FieldSet of reserved volume names
1640

1641
    """
1642
    for node, n_img in node_image.items():
1643
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1644
        # skip non-healthy nodes
1645
        continue
1646
      for volume in n_img.volumes:
1647
        test = ((node not in node_vol_should or
1648
                volume not in node_vol_should[node]) and
1649
                not reserved.Matches(volume))
1650
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1651
                      "volume %s is unknown", volume)
1652

    
1653
  def _VerifyOrphanInstances(self, instancelist, node_image):
1654
    """Verify the list of running instances.
1655

1656
    This checks what instances are running but unknown to the cluster.
1657

1658
    """
1659
    for node, n_img in node_image.items():
1660
      for o_inst in n_img.instances:
1661
        test = o_inst not in instancelist
1662
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1663
                      "instance %s on node %s should not exist", o_inst, node)
1664

    
1665
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1666
    """Verify N+1 Memory Resilience.
1667

1668
    Check that if one single node dies we can still start all the
1669
    instances it was primary for.
1670

1671
    """
1672
    cluster_info = self.cfg.GetClusterInfo()
1673
    for node, n_img in node_image.items():
1674
      # This code checks that every node which is now listed as
1675
      # secondary has enough memory to host all instances it is
1676
      # supposed to should a single other node in the cluster fail.
1677
      # FIXME: not ready for failover to an arbitrary node
1678
      # FIXME: does not support file-backed instances
1679
      # WARNING: we currently take into account down instances as well
1680
      # as up ones, considering that even if they're down someone
1681
      # might want to start them even in the event of a node failure.
1682
      if n_img.offline:
1683
        # we're skipping offline nodes from the N+1 warning, since
1684
        # most likely we don't have good memory infromation from them;
1685
        # we already list instances living on such nodes, and that's
1686
        # enough warning
1687
        continue
1688
      for prinode, instances in n_img.sbp.items():
1689
        needed_mem = 0
1690
        for instance in instances:
1691
          bep = cluster_info.FillBE(instance_cfg[instance])
1692
          if bep[constants.BE_AUTO_BALANCE]:
1693
            needed_mem += bep[constants.BE_MEMORY]
1694
        test = n_img.mfree < needed_mem
1695
        self._ErrorIf(test, self.ENODEN1, node,
1696
                      "not enough memory to accomodate instance failovers"
1697
                      " should node %s fail", prinode)
1698

    
1699
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1700
                       master_files):
1701
    """Verifies and computes the node required file checksums.
1702

1703
    @type ninfo: L{objects.Node}
1704
    @param ninfo: the node to check
1705
    @param nresult: the remote results for the node
1706
    @param file_list: required list of files
1707
    @param local_cksum: dictionary of local files and their checksums
1708
    @param master_files: list of files that only masters should have
1709

1710
    """
1711
    node = ninfo.name
1712
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1713

    
1714
    remote_cksum = nresult.get(constants.NV_FILELIST, None)
1715
    test = not isinstance(remote_cksum, dict)
1716
    _ErrorIf(test, self.ENODEFILECHECK, node,
1717
             "node hasn't returned file checksum data")
1718
    if test:
1719
      return
1720

    
1721
    for file_name in file_list:
1722
      node_is_mc = ninfo.master_candidate
1723
      must_have = (file_name not in master_files) or node_is_mc
1724
      # missing
1725
      test1 = file_name not in remote_cksum
1726
      # invalid checksum
1727
      test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1728
      # existing and good
1729
      test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1730
      _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1731
               "file '%s' missing", file_name)
1732
      _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1733
               "file '%s' has wrong checksum", file_name)
1734
      # not candidate and this is not a must-have file
1735
      _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1736
               "file '%s' should not exist on non master"
1737
               " candidates (and the file is outdated)", file_name)
1738
      # all good, except non-master/non-must have combination
1739
      _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1740
               "file '%s' should not exist"
1741
               " on non master candidates", file_name)
1742

    
1743
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_helper,
1744
                      drbd_map):
1745
    """Verifies and the node DRBD status.
1746

1747
    @type ninfo: L{objects.Node}
1748
    @param ninfo: the node to check
1749
    @param nresult: the remote results for the node
1750
    @param instanceinfo: the dict of instances
1751
    @param drbd_helper: the configured DRBD usermode helper
1752
    @param drbd_map: the DRBD map as returned by
1753
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1754

1755
    """
1756
    node = ninfo.name
1757
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1758

    
1759
    if drbd_helper:
1760
      helper_result = nresult.get(constants.NV_DRBDHELPER, None)
1761
      test = (helper_result == None)
1762
      _ErrorIf(test, self.ENODEDRBDHELPER, node,
1763
               "no drbd usermode helper returned")
1764
      if helper_result:
1765
        status, payload = helper_result
1766
        test = not status
1767
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1768
                 "drbd usermode helper check unsuccessful: %s", payload)
1769
        test = status and (payload != drbd_helper)
1770
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1771
                 "wrong drbd usermode helper: %s", payload)
1772

    
1773
    # compute the DRBD minors
1774
    node_drbd = {}
1775
    for minor, instance in drbd_map[node].items():
1776
      test = instance not in instanceinfo
1777
      _ErrorIf(test, self.ECLUSTERCFG, None,
1778
               "ghost instance '%s' in temporary DRBD map", instance)
1779
        # ghost instance should not be running, but otherwise we
1780
        # don't give double warnings (both ghost instance and
1781
        # unallocated minor in use)
1782
      if test:
1783
        node_drbd[minor] = (instance, False)
1784
      else:
1785
        instance = instanceinfo[instance]
1786
        node_drbd[minor] = (instance.name, instance.admin_up)
1787

    
1788
    # and now check them
1789
    used_minors = nresult.get(constants.NV_DRBDLIST, [])
1790
    test = not isinstance(used_minors, (tuple, list))
1791
    _ErrorIf(test, self.ENODEDRBD, node,
1792
             "cannot parse drbd status file: %s", str(used_minors))
1793
    if test:
1794
      # we cannot check drbd status
1795
      return
1796

    
1797
    for minor, (iname, must_exist) in node_drbd.items():
1798
      test = minor not in used_minors and must_exist
1799
      _ErrorIf(test, self.ENODEDRBD, node,
1800
               "drbd minor %d of instance %s is not active", minor, iname)
1801
    for minor in used_minors:
1802
      test = minor not in node_drbd
1803
      _ErrorIf(test, self.ENODEDRBD, node,
1804
               "unallocated drbd minor %d is in use", minor)
1805

    
1806
  def _UpdateNodeOS(self, ninfo, nresult, nimg):
1807
    """Builds the node OS structures.
1808

1809
    @type ninfo: L{objects.Node}
1810
    @param ninfo: the node to check
1811
    @param nresult: the remote results for the node
1812
    @param nimg: the node image object
1813

1814
    """
1815
    node = ninfo.name
1816
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1817

    
1818
    remote_os = nresult.get(constants.NV_OSLIST, None)
1819
    test = (not isinstance(remote_os, list) or
1820
            not compat.all(isinstance(v, list) and len(v) == 7
1821
                           for v in remote_os))
1822

    
1823
    _ErrorIf(test, self.ENODEOS, node,
1824
             "node hasn't returned valid OS data")
1825

    
1826
    nimg.os_fail = test
1827

    
1828
    if test:
1829
      return
1830

    
1831
    os_dict = {}
1832

    
1833
    for (name, os_path, status, diagnose,
1834
         variants, parameters, api_ver) in nresult[constants.NV_OSLIST]:
1835

    
1836
      if name not in os_dict:
1837
        os_dict[name] = []
1838

    
1839
      # parameters is a list of lists instead of list of tuples due to
1840
      # JSON lacking a real tuple type, fix it:
1841
      parameters = [tuple(v) for v in parameters]
1842
      os_dict[name].append((os_path, status, diagnose,
1843
                            set(variants), set(parameters), set(api_ver)))
1844

    
1845
    nimg.oslist = os_dict
1846

    
1847
  def _VerifyNodeOS(self, ninfo, nimg, base):
1848
    """Verifies the node OS list.
1849

1850
    @type ninfo: L{objects.Node}
1851
    @param ninfo: the node to check
1852
    @param nimg: the node image object
1853
    @param base: the 'template' node we match against (e.g. from the master)
1854

1855
    """
1856
    node = ninfo.name
1857
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1858

    
1859
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1860

    
1861
    for os_name, os_data in nimg.oslist.items():
1862
      assert os_data, "Empty OS status for OS %s?!" % os_name
1863
      f_path, f_status, f_diag, f_var, f_param, f_api = os_data[0]
1864
      _ErrorIf(not f_status, self.ENODEOS, node,
1865
               "Invalid OS %s (located at %s): %s", os_name, f_path, f_diag)
1866
      _ErrorIf(len(os_data) > 1, self.ENODEOS, node,
1867
               "OS '%s' has multiple entries (first one shadows the rest): %s",
1868
               os_name, utils.CommaJoin([v[0] for v in os_data]))
1869
      # this will catched in backend too
1870
      _ErrorIf(compat.any(v >= constants.OS_API_V15 for v in f_api)
1871
               and not f_var, self.ENODEOS, node,
1872
               "OS %s with API at least %d does not declare any variant",
1873
               os_name, constants.OS_API_V15)
1874
      # comparisons with the 'base' image
1875
      test = os_name not in base.oslist
1876
      _ErrorIf(test, self.ENODEOS, node,
1877
               "Extra OS %s not present on reference node (%s)",
1878
               os_name, base.name)
1879
      if test:
1880
        continue
1881
      assert base.oslist[os_name], "Base node has empty OS status?"
1882
      _, b_status, _, b_var, b_param, b_api = base.oslist[os_name][0]
1883
      if not b_status:
1884
        # base OS is invalid, skipping
1885
        continue
1886
      for kind, a, b in [("API version", f_api, b_api),
1887
                         ("variants list", f_var, b_var),
1888
                         ("parameters", f_param, b_param)]:
1889
        _ErrorIf(a != b, self.ENODEOS, node,
1890
                 "OS %s %s differs from reference node %s: %s vs. %s",
1891
                 kind, os_name, base.name,
1892
                 utils.CommaJoin(a), utils.CommaJoin(b))
1893

    
1894
    # check any missing OSes
1895
    missing = set(base.oslist.keys()).difference(nimg.oslist.keys())
1896
    _ErrorIf(missing, self.ENODEOS, node,
1897
             "OSes present on reference node %s but missing on this node: %s",
1898
             base.name, utils.CommaJoin(missing))
1899

    
1900
  def _VerifyOob(self, ninfo, nresult):
1901
    """Verifies out of band functionality of a node.
1902

1903
    @type ninfo: L{objects.Node}
1904
    @param ninfo: the node to check
1905
    @param nresult: the remote results for the node
1906

1907
    """
1908
    node = ninfo.name
1909
    # We just have to verify the paths on master and/or master candidates
1910
    # as the oob helper is invoked on the master
1911
    if ((ninfo.master_candidate or ninfo.master_capable) and
1912
        constants.NV_OOB_PATHS in nresult):
1913
      for path_result in nresult[constants.NV_OOB_PATHS]:
1914
        self._ErrorIf(path_result, self.ENODEOOBPATH, node, path_result)
1915

    
1916
  def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
1917
    """Verifies and updates the node volume data.
1918

1919
    This function will update a L{NodeImage}'s internal structures
1920
    with data from the remote call.
1921

1922
    @type ninfo: L{objects.Node}
1923
    @param ninfo: the node to check
1924
    @param nresult: the remote results for the node
1925
    @param nimg: the node image object
1926
    @param vg_name: the configured VG name
1927

1928
    """
1929
    node = ninfo.name
1930
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1931

    
1932
    nimg.lvm_fail = True
1933
    lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1934
    if vg_name is None:
1935
      pass
1936
    elif isinstance(lvdata, basestring):
1937
      _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1938
               utils.SafeEncode(lvdata))
1939
    elif not isinstance(lvdata, dict):
1940
      _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1941
    else:
1942
      nimg.volumes = lvdata
1943
      nimg.lvm_fail = False
1944

    
1945
  def _UpdateNodeInstances(self, ninfo, nresult, nimg):
1946
    """Verifies and updates the node instance list.
1947

1948
    If the listing was successful, then updates this node's instance
1949
    list. Otherwise, it marks the RPC call as failed for the instance
1950
    list key.
1951

1952
    @type ninfo: L{objects.Node}
1953
    @param ninfo: the node to check
1954
    @param nresult: the remote results for the node
1955
    @param nimg: the node image object
1956

1957
    """
1958
    idata = nresult.get(constants.NV_INSTANCELIST, None)
1959
    test = not isinstance(idata, list)
1960
    self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed"
1961
                  " (instancelist): %s", utils.SafeEncode(str(idata)))
1962
    if test:
1963
      nimg.hyp_fail = True
1964
    else:
1965
      nimg.instances = idata
1966

    
1967
  def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
1968
    """Verifies and computes a node information map
1969

1970
    @type ninfo: L{objects.Node}
1971
    @param ninfo: the node to check
1972
    @param nresult: the remote results for the node
1973
    @param nimg: the node image object
1974
    @param vg_name: the configured VG name
1975

1976
    """
1977
    node = ninfo.name
1978
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1979

    
1980
    # try to read free memory (from the hypervisor)
1981
    hv_info = nresult.get(constants.NV_HVINFO, None)
1982
    test = not isinstance(hv_info, dict) or "memory_free" not in hv_info
1983
    _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1984
    if not test:
1985
      try:
1986
        nimg.mfree = int(hv_info["memory_free"])
1987
      except (ValueError, TypeError):
1988
        _ErrorIf(True, self.ENODERPC, node,
1989
                 "node returned invalid nodeinfo, check hypervisor")
1990

    
1991
    # FIXME: devise a free space model for file based instances as well
1992
    if vg_name is not None:
1993
      test = (constants.NV_VGLIST not in nresult or
1994
              vg_name not in nresult[constants.NV_VGLIST])
1995
      _ErrorIf(test, self.ENODELVM, node,
1996
               "node didn't return data for the volume group '%s'"
1997
               " - it is either missing or broken", vg_name)
1998
      if not test:
1999
        try:
2000
          nimg.dfree = int(nresult[constants.NV_VGLIST][vg_name])
2001
        except (ValueError, TypeError):
2002
          _ErrorIf(True, self.ENODERPC, node,
2003
                   "node returned invalid LVM info, check LVM status")
2004

    
2005
  def _CollectDiskInfo(self, nodelist, node_image, instanceinfo):
2006
    """Gets per-disk status information for all instances.
2007

2008
    @type nodelist: list of strings
2009
    @param nodelist: Node names
2010
    @type node_image: dict of (name, L{objects.Node})
2011
    @param node_image: Node objects
2012
    @type instanceinfo: dict of (name, L{objects.Instance})
2013
    @param instanceinfo: Instance objects
2014
    @rtype: {instance: {node: [(succes, payload)]}}
2015
    @return: a dictionary of per-instance dictionaries with nodes as
2016
        keys and disk information as values; the disk information is a
2017
        list of tuples (success, payload)
2018

2019
    """
2020
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2021

    
2022
    node_disks = {}
2023
    node_disks_devonly = {}
2024
    diskless_instances = set()
2025
    diskless = constants.DT_DISKLESS
2026

    
2027
    for nname in nodelist:
2028
      node_instances = list(itertools.chain(node_image[nname].pinst,
2029
                                            node_image[nname].sinst))
2030
      diskless_instances.update(inst for inst in node_instances
2031
                                if instanceinfo[inst].disk_template == diskless)
2032
      disks = [(inst, disk)
2033
               for inst in node_instances
2034
               for disk in instanceinfo[inst].disks]
2035

    
2036
      if not disks:
2037
        # No need to collect data
2038
        continue
2039

    
2040
      node_disks[nname] = disks
2041

    
2042
      # Creating copies as SetDiskID below will modify the objects and that can
2043
      # lead to incorrect data returned from nodes
2044
      devonly = [dev.Copy() for (_, dev) in disks]
2045

    
2046
      for dev in devonly:
2047
        self.cfg.SetDiskID(dev, nname)
2048

    
2049
      node_disks_devonly[nname] = devonly
2050

    
2051
    assert len(node_disks) == len(node_disks_devonly)
2052

    
2053
    # Collect data from all nodes with disks
2054
    result = self.rpc.call_blockdev_getmirrorstatus_multi(node_disks.keys(),
2055
                                                          node_disks_devonly)
2056

    
2057
    assert len(result) == len(node_disks)
2058

    
2059
    instdisk = {}
2060

    
2061
    for (nname, nres) in result.items():
2062
      disks = node_disks[nname]
2063

    
2064
      if nres.offline:
2065
        # No data from this node
2066
        data = len(disks) * [(False, "node offline")]
2067
      else:
2068
        msg = nres.fail_msg
2069
        _ErrorIf(msg, self.ENODERPC, nname,
2070
                 "while getting disk information: %s", msg)
2071
        if msg:
2072
          # No data from this node
2073
          data = len(disks) * [(False, msg)]
2074
        else:
2075
          data = []
2076
          for idx, i in enumerate(nres.payload):
2077
            if isinstance(i, (tuple, list)) and len(i) == 2:
2078
              data.append(i)
2079
            else:
2080
              logging.warning("Invalid result from node %s, entry %d: %s",
2081
                              nname, idx, i)
2082
              data.append((False, "Invalid result from the remote node"))
2083

    
2084
      for ((inst, _), status) in zip(disks, data):
2085
        instdisk.setdefault(inst, {}).setdefault(nname, []).append(status)
2086

    
2087
    # Add empty entries for diskless instances.
2088
    for inst in diskless_instances:
2089
      assert inst not in instdisk
2090
      instdisk[inst] = {}
2091

    
2092
    assert compat.all(len(statuses) == len(instanceinfo[inst].disks) and
2093
                      len(nnames) <= len(instanceinfo[inst].all_nodes) and
2094
                      compat.all(isinstance(s, (tuple, list)) and
2095
                                 len(s) == 2 for s in statuses)
2096
                      for inst, nnames in instdisk.items()
2097
                      for nname, statuses in nnames.items())
2098
    assert set(instdisk) == set(instanceinfo), "instdisk consistency failure"
2099

    
2100
    return instdisk
2101

    
2102
  def _VerifyHVP(self, hvp_data):
2103
    """Verifies locally the syntax of the hypervisor parameters.
2104

2105
    """
2106
    for item, hv_name, hv_params in hvp_data:
2107
      msg = ("hypervisor %s parameters syntax check (source %s): %%s" %
2108
             (item, hv_name))
2109
      try:
2110
        hv_class = hypervisor.GetHypervisor(hv_name)
2111
        utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2112
        hv_class.CheckParameterSyntax(hv_params)
2113
      except errors.GenericError, err:
2114
        self._ErrorIf(True, self.ECLUSTERCFG, None, msg % str(err))
2115

    
2116
  def BuildHooksEnv(self):
2117
    """Build hooks env.
2118

2119
    Cluster-Verify hooks just ran in the post phase and their failure makes
2120
    the output be logged in the verify output and the verification to fail.
2121

2122
    """
2123
    cfg = self.cfg
2124

    
2125
    env = {
2126
      "CLUSTER_TAGS": " ".join(cfg.GetClusterInfo().GetTags())
2127
      }
2128

    
2129
    env.update(("NODE_TAGS_%s" % node.name, " ".join(node.GetTags()))
2130
               for node in cfg.GetAllNodesInfo().values())
2131

    
2132
    return env
2133

    
2134
  def BuildHooksNodes(self):
2135
    """Build hooks nodes.
2136

2137
    """
2138
    return ([], self.cfg.GetNodeList())
2139

    
2140
  def Exec(self, feedback_fn):
2141
    """Verify integrity of cluster, performing various test on nodes.
2142

2143
    """
2144
    # This method has too many local variables. pylint: disable-msg=R0914
2145
    self.bad = False
2146
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2147
    verbose = self.op.verbose
2148
    self._feedback_fn = feedback_fn
2149
    feedback_fn("* Verifying global settings")
2150
    for msg in self.cfg.VerifyConfig():
2151
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
2152

    
2153
    # Check the cluster certificates
2154
    for cert_filename in constants.ALL_CERT_FILES:
2155
      (errcode, msg) = _VerifyCertificate(cert_filename)
2156
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
2157

    
2158
    vg_name = self.cfg.GetVGName()
2159
    drbd_helper = self.cfg.GetDRBDHelper()
2160
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
2161
    cluster = self.cfg.GetClusterInfo()
2162
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
2163
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
2164
    nodeinfo_byname = dict(zip(nodelist, nodeinfo))
2165
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
2166
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
2167
                        for iname in instancelist)
2168
    groupinfo = self.cfg.GetAllNodeGroupsInfo()
2169
    i_non_redundant = [] # Non redundant instances
2170
    i_non_a_balanced = [] # Non auto-balanced instances
2171
    n_offline = 0 # Count of offline nodes
2172
    n_drained = 0 # Count of nodes being drained
2173
    node_vol_should = {}
2174

    
2175
    # FIXME: verify OS list
2176
    # do local checksums
2177
    master_files = [constants.CLUSTER_CONF_FILE]
2178
    master_node = self.master_node = self.cfg.GetMasterNode()
2179
    master_ip = self.cfg.GetMasterIP()
2180

    
2181
    file_names = ssconf.SimpleStore().GetFileList()
2182
    file_names.extend(constants.ALL_CERT_FILES)
2183
    file_names.extend(master_files)
2184
    if cluster.modify_etc_hosts:
2185
      file_names.append(constants.ETC_HOSTS)
2186

    
2187
    local_checksums = utils.FingerprintFiles(file_names)
2188

    
2189
    # Compute the set of hypervisor parameters
2190
    hvp_data = []
2191
    for hv_name in hypervisors:
2192
      hvp_data.append(("cluster", hv_name, cluster.GetHVDefaults(hv_name)))
2193
    for os_name, os_hvp in cluster.os_hvp.items():
2194
      for hv_name, hv_params in os_hvp.items():
2195
        if not hv_params:
2196
          continue
2197
        full_params = cluster.GetHVDefaults(hv_name, os_name=os_name)
2198
        hvp_data.append(("os %s" % os_name, hv_name, full_params))
2199
    # TODO: collapse identical parameter values in a single one
2200
    for instance in instanceinfo.values():
2201
      if not instance.hvparams:
2202
        continue
2203
      hvp_data.append(("instance %s" % instance.name, instance.hypervisor,
2204
                       cluster.FillHV(instance)))
2205
    # and verify them locally
2206
    self._VerifyHVP(hvp_data)
2207

    
2208
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
2209
    node_verify_param = {
2210
      constants.NV_FILELIST: file_names,
2211
      constants.NV_NODELIST: [node.name for node in nodeinfo
2212
                              if not node.offline],
2213
      constants.NV_HYPERVISOR: hypervisors,
2214
      constants.NV_HVPARAMS: hvp_data,
2215
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
2216
                                  node.secondary_ip) for node in nodeinfo
2217
                                 if not node.offline],
2218
      constants.NV_INSTANCELIST: hypervisors,
2219
      constants.NV_VERSION: None,
2220
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
2221
      constants.NV_NODESETUP: None,
2222
      constants.NV_TIME: None,
2223
      constants.NV_MASTERIP: (master_node, master_ip),
2224
      constants.NV_OSLIST: None,
2225
      constants.NV_VMNODES: self.cfg.GetNonVmCapableNodeList(),
2226
      }
2227

    
2228
    if vg_name is not None:
2229
      node_verify_param[constants.NV_VGLIST] = None
2230
      node_verify_param[constants.NV_LVLIST] = vg_name
2231
      node_verify_param[constants.NV_PVLIST] = [vg_name]
2232
      node_verify_param[constants.NV_DRBDLIST] = None
2233

    
2234
    if drbd_helper:
2235
      node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
2236

    
2237
    # Build our expected cluster state
2238
    node_image = dict((node.name, self.NodeImage(offline=node.offline,
2239
                                                 name=node.name,
2240
                                                 vm_capable=node.vm_capable))
2241
                      for node in nodeinfo)
2242

    
2243
    # Gather OOB paths
2244
    oob_paths = []
2245
    for node in nodeinfo:
2246
      path = _SupportsOob(self.cfg, node)
2247
      if path and path not in oob_paths:
2248
        oob_paths.append(path)
2249

    
2250
    if oob_paths:
2251
      node_verify_param[constants.NV_OOB_PATHS] = oob_paths
2252

    
2253
    for instance in instancelist:
2254
      inst_config = instanceinfo[instance]
2255

    
2256
      for nname in inst_config.all_nodes:
2257
        if nname not in node_image:
2258
          # ghost node
2259
          gnode = self.NodeImage(name=nname)
2260
          gnode.ghost = True
2261
          node_image[nname] = gnode
2262

    
2263
      inst_config.MapLVsByNode(node_vol_should)
2264

    
2265
      pnode = inst_config.primary_node
2266
      node_image[pnode].pinst.append(instance)
2267

    
2268
      for snode in inst_config.secondary_nodes:
2269
        nimg = node_image[snode]
2270
        nimg.sinst.append(instance)
2271
        if pnode not in nimg.sbp:
2272
          nimg.sbp[pnode] = []
2273
        nimg.sbp[pnode].append(instance)
2274

    
2275
    # At this point, we have the in-memory data structures complete,
2276
    # except for the runtime information, which we'll gather next
2277

    
2278
    # Due to the way our RPC system works, exact response times cannot be
2279
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
2280
    # time before and after executing the request, we can at least have a time
2281
    # window.
2282
    nvinfo_starttime = time.time()
2283
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
2284
                                           self.cfg.GetClusterName())
2285
    nvinfo_endtime = time.time()
2286

    
2287
    all_drbd_map = self.cfg.ComputeDRBDMap()
2288

    
2289
    feedback_fn("* Gathering disk information (%s nodes)" % len(nodelist))
2290
    instdisk = self._CollectDiskInfo(nodelist, node_image, instanceinfo)
2291

    
2292
    feedback_fn("* Verifying node status")
2293

    
2294
    refos_img = None
2295

    
2296
    for node_i in nodeinfo:
2297
      node = node_i.name
2298
      nimg = node_image[node]
2299

    
2300
      if node_i.offline:
2301
        if verbose:
2302
          feedback_fn("* Skipping offline node %s" % (node,))
2303
        n_offline += 1
2304
        continue
2305

    
2306
      if node == master_node:
2307
        ntype = "master"
2308
      elif node_i.master_candidate:
2309
        ntype = "master candidate"
2310
      elif node_i.drained:
2311
        ntype = "drained"
2312
        n_drained += 1
2313
      else:
2314
        ntype = "regular"
2315
      if verbose:
2316
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
2317

    
2318
      msg = all_nvinfo[node].fail_msg
2319
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
2320
      if msg:
2321
        nimg.rpc_fail = True
2322
        continue
2323

    
2324
      nresult = all_nvinfo[node].payload
2325

    
2326
      nimg.call_ok = self._VerifyNode(node_i, nresult)
2327
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
2328
      self._VerifyNodeNetwork(node_i, nresult)
2329
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
2330
                            master_files)
2331

    
2332
      self._VerifyOob(node_i, nresult)
2333

    
2334
      if nimg.vm_capable:
2335
        self._VerifyNodeLVM(node_i, nresult, vg_name)
2336
        self._VerifyNodeDrbd(node_i, nresult, instanceinfo, drbd_helper,
2337
                             all_drbd_map)
2338

    
2339
        self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
2340
        self._UpdateNodeInstances(node_i, nresult, nimg)
2341
        self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
2342
        self._UpdateNodeOS(node_i, nresult, nimg)
2343
        if not nimg.os_fail:
2344
          if refos_img is None:
2345
            refos_img = nimg
2346
          self._VerifyNodeOS(node_i, nimg, refos_img)
2347

    
2348
    feedback_fn("* Verifying instance status")
2349
    for instance in instancelist:
2350
      if verbose:
2351
        feedback_fn("* Verifying instance %s" % instance)
2352
      inst_config = instanceinfo[instance]
2353
      self._VerifyInstance(instance, inst_config, node_image,
2354
                           instdisk[instance])
2355
      inst_nodes_offline = []
2356

    
2357
      pnode = inst_config.primary_node
2358
      pnode_img = node_image[pnode]
2359
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
2360
               self.ENODERPC, pnode, "instance %s, connection to"
2361
               " primary node failed", instance)
2362

    
2363
      _ErrorIf(inst_config.admin_up and pnode_img.offline,
2364
               self.EINSTANCEBADNODE, instance,
2365
               "instance is marked as running and lives on offline node %s",
2366
               inst_config.primary_node)
2367

    
2368
      # If the instance is non-redundant we cannot survive losing its primary
2369
      # node, so we are not N+1 compliant. On the other hand we have no disk
2370
      # templates with more than one secondary so that situation is not well
2371
      # supported either.
2372
      # FIXME: does not support file-backed instances
2373
      if not inst_config.secondary_nodes:
2374
        i_non_redundant.append(instance)
2375

    
2376
      _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
2377
               instance, "instance has multiple secondary nodes: %s",
2378
               utils.CommaJoin(inst_config.secondary_nodes),
2379
               code=self.ETYPE_WARNING)
2380

    
2381
      if inst_config.disk_template in constants.DTS_INT_MIRROR:
2382
        pnode = inst_config.primary_node
2383
        instance_nodes = utils.NiceSort(inst_config.all_nodes)
2384
        instance_groups = {}
2385

    
2386
        for node in instance_nodes:
2387
          instance_groups.setdefault(nodeinfo_byname[node].group,
2388
                                     []).append(node)
2389

    
2390
        pretty_list = [
2391
          "%s (group %s)" % (utils.CommaJoin(nodes), groupinfo[group].name)
2392
          # Sort so that we always list the primary node first.
2393
          for group, nodes in sorted(instance_groups.items(),
2394
                                     key=lambda (_, nodes): pnode in nodes,
2395
                                     reverse=True)]
2396

    
2397
        self._ErrorIf(len(instance_groups) > 1, self.EINSTANCESPLITGROUPS,
2398
                      instance, "instance has primary and secondary nodes in"
2399
                      " different groups: %s", utils.CommaJoin(pretty_list),
2400
                      code=self.ETYPE_WARNING)
2401

    
2402
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
2403
        i_non_a_balanced.append(instance)
2404

    
2405
      for snode in inst_config.secondary_nodes:
2406
        s_img = node_image[snode]
2407
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
2408
                 "instance %s, connection to secondary node failed", instance)
2409

    
2410
        if s_img.offline:
2411
          inst_nodes_offline.append(snode)
2412

    
2413
      # warn that the instance lives on offline nodes
2414
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
2415
               "instance has offline secondary node(s) %s",
2416
               utils.CommaJoin(inst_nodes_offline))
2417
      # ... or ghost/non-vm_capable nodes
2418
      for node in inst_config.all_nodes:
2419
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
2420
                 "instance lives on ghost node %s", node)
2421
        _ErrorIf(not node_image[node].vm_capable, self.EINSTANCEBADNODE,
2422
                 instance, "instance lives on non-vm_capable node %s", node)
2423

    
2424
    feedback_fn("* Verifying orphan volumes")
2425
    reserved = utils.FieldSet(*cluster.reserved_lvs)
2426
    self._VerifyOrphanVolumes(node_vol_should, node_image, reserved)
2427

    
2428
    feedback_fn("* Verifying orphan instances")
2429
    self._VerifyOrphanInstances(instancelist, node_image)
2430

    
2431
    if constants.VERIFY_NPLUSONE_MEM not in self.op.skip_checks:
2432
      feedback_fn("* Verifying N+1 Memory redundancy")
2433
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
2434

    
2435
    feedback_fn("* Other Notes")
2436
    if i_non_redundant:
2437
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
2438
                  % len(i_non_redundant))
2439

    
2440
    if i_non_a_balanced:
2441
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
2442
                  % len(i_non_a_balanced))
2443

    
2444
    if n_offline:
2445
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
2446

    
2447
    if n_drained:
2448
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
2449

    
2450
    return not self.bad
2451

    
2452
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
2453
    """Analyze the post-hooks' result
2454

2455
    This method analyses the hook result, handles it, and sends some
2456
    nicely-formatted feedback back to the user.
2457

2458
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
2459
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
2460
    @param hooks_results: the results of the multi-node hooks rpc call
2461
    @param feedback_fn: function used send feedback back to the caller
2462
    @param lu_result: previous Exec result
2463
    @return: the new Exec result, based on the previous result
2464
        and hook results
2465

2466
    """
2467
    # We only really run POST phase hooks, and are only interested in
2468
    # their results
2469
    if phase == constants.HOOKS_PHASE_POST:
2470
      # Used to change hooks' output to proper indentation
2471
      feedback_fn("* Hooks Results")
2472
      assert hooks_results, "invalid result from hooks"
2473

    
2474
      for node_name in hooks_results:
2475
        res = hooks_results[node_name]
2476
        msg = res.fail_msg
2477
        test = msg and not res.offline
2478
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
2479
                      "Communication failure in hooks execution: %s", msg)
2480
        if res.offline or msg:
2481
          # No need to investigate payload if node is offline or gave an error.
2482
          # override manually lu_result here as _ErrorIf only
2483
          # overrides self.bad
2484
          lu_result = 1
2485
          continue
2486
        for script, hkr, output in res.payload:
2487
          test = hkr == constants.HKR_FAIL
2488
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
2489
                        "Script %s failed, output:", script)
2490
          if test:
2491
            output = self._HOOKS_INDENT_RE.sub('      ', output)
2492
            feedback_fn("%s" % output)
2493
            lu_result = 0
2494

    
2495
      return lu_result
2496

    
2497

    
2498
class LUClusterVerifyDisks(NoHooksLU):
2499
  """Verifies the cluster disks status.
2500

2501
  """
2502
  REQ_BGL = False
2503

    
2504
  def ExpandNames(self):
2505
    self.needed_locks = {
2506
      locking.LEVEL_NODE: locking.ALL_SET,
2507
      locking.LEVEL_INSTANCE: locking.ALL_SET,
2508
    }
2509
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
2510

    
2511
  def Exec(self, feedback_fn):
2512
    """Verify integrity of cluster disks.
2513

2514
    @rtype: tuple of three items
2515
    @return: a tuple of (dict of node-to-node_error, list of instances
2516
        which need activate-disks, dict of instance: (node, volume) for
2517
        missing volumes
2518

2519
    """
2520
    result = res_nodes, res_instances, res_missing = {}, [], {}
2521

    
2522
    nodes = utils.NiceSort(self.cfg.GetVmCapableNodeList())
2523
    instances = self.cfg.GetAllInstancesInfo().values()
2524

    
2525
    nv_dict = {}
2526
    for inst in instances:
2527
      inst_lvs = {}
2528
      if not inst.admin_up:
2529
        continue
2530
      inst.MapLVsByNode(inst_lvs)
2531
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
2532
      for node, vol_list in inst_lvs.iteritems():
2533
        for vol in vol_list:
2534
          nv_dict[(node, vol)] = inst
2535

    
2536
    if not nv_dict:
2537
      return result
2538

    
2539
    node_lvs = self.rpc.call_lv_list(nodes, [])
2540
    for node, node_res in node_lvs.items():
2541
      if node_res.offline:
2542
        continue
2543
      msg = node_res.fail_msg
2544
      if msg:
2545
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
2546
        res_nodes[node] = msg
2547
        continue
2548

    
2549
      lvs = node_res.payload
2550
      for lv_name, (_, _, lv_online) in lvs.items():
2551
        inst = nv_dict.pop((node, lv_name), None)
2552
        if (not lv_online and inst is not None
2553
            and inst.name not in res_instances):
2554
          res_instances.append(inst.name)
2555

    
2556
    # any leftover items in nv_dict are missing LVs, let's arrange the
2557
    # data better
2558
    for key, inst in nv_dict.iteritems():
2559
      if inst.name not in res_missing:
2560
        res_missing[inst.name] = []
2561
      res_missing[inst.name].append(key)
2562

    
2563
    return result
2564

    
2565

    
2566
class LUClusterRepairDiskSizes(NoHooksLU):
2567
  """Verifies the cluster disks sizes.
2568

2569
  """
2570
  REQ_BGL = False
2571

    
2572
  def ExpandNames(self):
2573
    if self.op.instances:
2574
      self.wanted_names = []
2575
      for name in self.op.instances:
2576
        full_name = _ExpandInstanceName(self.cfg, name)
2577
        self.wanted_names.append(full_name)
2578
      self.needed_locks = {
2579
        locking.LEVEL_NODE: [],
2580
        locking.LEVEL_INSTANCE: self.wanted_names,
2581
        }
2582
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2583
    else:
2584
      self.wanted_names = None
2585
      self.needed_locks = {
2586
        locking.LEVEL_NODE: locking.ALL_SET,
2587
        locking.LEVEL_INSTANCE: locking.ALL_SET,
2588
        }
2589
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
2590

    
2591
  def DeclareLocks(self, level):
2592
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2593
      self._LockInstancesNodes(primary_only=True)
2594

    
2595
  def CheckPrereq(self):
2596
    """Check prerequisites.
2597

2598
    This only checks the optional instance list against the existing names.
2599

2600
    """
2601
    if self.wanted_names is None:
2602
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2603

    
2604
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2605
                             in self.wanted_names]
2606

    
2607
  def _EnsureChildSizes(self, disk):
2608
    """Ensure children of the disk have the needed disk size.
2609

2610
    This is valid mainly for DRBD8 and fixes an issue where the
2611
    children have smaller disk size.
2612

2613
    @param disk: an L{ganeti.objects.Disk} object
2614

2615
    """
2616
    if disk.dev_type == constants.LD_DRBD8:
2617
      assert disk.children, "Empty children for DRBD8?"
2618
      fchild = disk.children[0]
2619
      mismatch = fchild.size < disk.size
2620
      if mismatch:
2621
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2622
                     fchild.size, disk.size)
2623
        fchild.size = disk.size
2624

    
2625
      # and we recurse on this child only, not on the metadev
2626
      return self._EnsureChildSizes(fchild) or mismatch
2627
    else:
2628
      return False
2629

    
2630
  def Exec(self, feedback_fn):
2631
    """Verify the size of cluster disks.
2632

2633
    """
2634
    # TODO: check child disks too
2635
    # TODO: check differences in size between primary/secondary nodes
2636
    per_node_disks = {}
2637
    for instance in self.wanted_instances:
2638
      pnode = instance.primary_node
2639
      if pnode not in per_node_disks:
2640
        per_node_disks[pnode] = []
2641
      for idx, disk in enumerate(instance.disks):
2642
        per_node_disks[pnode].append((instance, idx, disk))
2643

    
2644
    changed = []
2645
    for node, dskl in per_node_disks.items():
2646
      newl = [v[2].Copy() for v in dskl]
2647
      for dsk in newl:
2648
        self.cfg.SetDiskID(dsk, node)
2649
      result = self.rpc.call_blockdev_getsize(node, newl)
2650
      if result.fail_msg:
2651
        self.LogWarning("Failure in blockdev_getsize call to node"
2652
                        " %s, ignoring", node)
2653
        continue
2654
      if len(result.payload) != len(dskl):
2655
        logging.warning("Invalid result from node %s: len(dksl)=%d,"
2656
                        " result.payload=%s", node, len(dskl), result.payload)
2657
        self.LogWarning("Invalid result from node %s, ignoring node results",
2658
                        node)
2659
        continue
2660
      for ((instance, idx, disk), size) in zip(dskl, result.payload):
2661
        if size is None:
2662
          self.LogWarning("Disk %d of instance %s did not return size"
2663
                          " information, ignoring", idx, instance.name)
2664
          continue
2665
        if not isinstance(size, (int, long)):
2666
          self.LogWarning("Disk %d of instance %s did not return valid"
2667
                          " size information, ignoring", idx, instance.name)
2668
          continue
2669
        size = size >> 20
2670
        if size != disk.size:
2671
          self.LogInfo("Disk %d of instance %s has mismatched size,"
2672
                       " correcting: recorded %d, actual %d", idx,
2673
                       instance.name, disk.size, size)
2674
          disk.size = size
2675
          self.cfg.Update(instance, feedback_fn)
2676
          changed.append((instance.name, idx, size))
2677
        if self._EnsureChildSizes(disk):
2678
          self.cfg.Update(instance, feedback_fn)
2679
          changed.append((instance.name, idx, disk.size))
2680
    return changed
2681

    
2682

    
2683
class LUClusterRename(LogicalUnit):
2684
  """Rename the cluster.
2685

2686
  """
2687
  HPATH = "cluster-rename"
2688
  HTYPE = constants.HTYPE_CLUSTER
2689

    
2690
  def BuildHooksEnv(self):
2691
    """Build hooks env.
2692

2693
    """
2694
    return {
2695
      "OP_TARGET": self.cfg.GetClusterName(),
2696
      "NEW_NAME": self.op.name,
2697
      }
2698

    
2699
  def BuildHooksNodes(self):
2700
    """Build hooks nodes.
2701

2702
    """
2703
    return ([self.cfg.GetMasterNode()], self.cfg.GetNodeList())
2704

    
2705
  def CheckPrereq(self):
2706
    """Verify that the passed name is a valid one.
2707

2708
    """
2709
    hostname = netutils.GetHostname(name=self.op.name,
2710
                                    family=self.cfg.GetPrimaryIPFamily())
2711

    
2712
    new_name = hostname.name
2713
    self.ip = new_ip = hostname.ip
2714
    old_name = self.cfg.GetClusterName()
2715
    old_ip = self.cfg.GetMasterIP()
2716
    if new_name == old_name and new_ip == old_ip:
2717
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
2718
                                 " cluster has changed",
2719
                                 errors.ECODE_INVAL)
2720
    if new_ip != old_ip:
2721
      if netutils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
2722
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
2723
                                   " reachable on the network" %
2724
                                   new_ip, errors.ECODE_NOTUNIQUE)
2725

    
2726
    self.op.name = new_name
2727

    
2728
  def Exec(self, feedback_fn):
2729
    """Rename the cluster.
2730

2731
    """
2732
    clustername = self.op.name
2733
    ip = self.ip
2734

    
2735
    # shutdown the master IP
2736
    master = self.cfg.GetMasterNode()
2737
    result = self.rpc.call_node_stop_master(master, False)
2738
    result.Raise("Could not disable the master role")
2739

    
2740
    try:
2741
      cluster = self.cfg.GetClusterInfo()
2742
      cluster.cluster_name = clustername
2743
      cluster.master_ip = ip
2744
      self.cfg.Update(cluster, feedback_fn)
2745

    
2746
      # update the known hosts file
2747
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2748
      node_list = self.cfg.GetOnlineNodeList()
2749
      try:
2750
        node_list.remove(master)
2751
      except ValueError:
2752
        pass
2753
      _UploadHelper(self, node_list, constants.SSH_KNOWN_HOSTS_FILE)
2754
    finally:
2755
      result = self.rpc.call_node_start_master(master, False, False)
2756
      msg = result.fail_msg
2757
      if msg:
2758
        self.LogWarning("Could not re-enable the master role on"
2759
                        " the master, please restart manually: %s", msg)
2760

    
2761
    return clustername
2762

    
2763

    
2764
class LUClusterSetParams(LogicalUnit):
2765
  """Change the parameters of the cluster.
2766

2767
  """
2768
  HPATH = "cluster-modify"
2769
  HTYPE = constants.HTYPE_CLUSTER
2770
  REQ_BGL = False
2771

    
2772
  def CheckArguments(self):
2773
    """Check parameters
2774

2775
    """
2776
    if self.op.uid_pool:
2777
      uidpool.CheckUidPool(self.op.uid_pool)
2778

    
2779
    if self.op.add_uids:
2780
      uidpool.CheckUidPool(self.op.add_uids)
2781

    
2782
    if self.op.remove_uids:
2783
      uidpool.CheckUidPool(self.op.remove_uids)
2784

    
2785
  def ExpandNames(self):
2786
    # FIXME: in the future maybe other cluster params won't require checking on
2787
    # all nodes to be modified.
2788
    self.needed_locks = {
2789
      locking.LEVEL_NODE: locking.ALL_SET,
2790
    }
2791
    self.share_locks[locking.LEVEL_NODE] = 1
2792

    
2793
  def BuildHooksEnv(self):
2794
    """Build hooks env.
2795

2796
    """
2797
    return {
2798
      "OP_TARGET": self.cfg.GetClusterName(),
2799
      "NEW_VG_NAME": self.op.vg_name,
2800
      }
2801

    
2802
  def BuildHooksNodes(self):
2803
    """Build hooks nodes.
2804

2805
    """
2806
    mn = self.cfg.GetMasterNode()
2807
    return ([mn], [mn])
2808

    
2809
  def CheckPrereq(self):
2810
    """Check prerequisites.
2811

2812
    This checks whether the given params don't conflict and
2813
    if the given volume group is valid.
2814

2815
    """
2816
    if self.op.vg_name is not None and not self.op.vg_name:
2817
      if self.cfg.HasAnyDiskOfType(constants.LD_LV):
2818
        raise errors.OpPrereqError("Cannot disable lvm storage while lvm-based"
2819
                                   " instances exist", errors.ECODE_INVAL)
2820

    
2821
    if self.op.drbd_helper is not None and not self.op.drbd_helper:
2822
      if self.cfg.HasAnyDiskOfType(constants.LD_DRBD8):
2823
        raise errors.OpPrereqError("Cannot disable drbd helper while"
2824
                                   " drbd-based instances exist",
2825
                                   errors.ECODE_INVAL)
2826

    
2827
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2828

    
2829
    # if vg_name not None, checks given volume group on all nodes
2830
    if self.op.vg_name:
2831
      vglist = self.rpc.call_vg_list(node_list)
2832
      for node in node_list:
2833
        msg = vglist[node].fail_msg
2834
        if msg:
2835
          # ignoring down node
2836
          self.LogWarning("Error while gathering data on node %s"
2837
                          " (ignoring node): %s", node, msg)
2838
          continue
2839
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2840
                                              self.op.vg_name,
2841
                                              constants.MIN_VG_SIZE)
2842
        if vgstatus:
2843
          raise errors.OpPrereqError("Error on node '%s': %s" %
2844
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2845

    
2846
    if self.op.drbd_helper:
2847
      # checks given drbd helper on all nodes
2848
      helpers = self.rpc.call_drbd_helper(node_list)
2849
      for node in node_list:
2850
        ninfo = self.cfg.GetNodeInfo(node)
2851
        if ninfo.offline:
2852
          self.LogInfo("Not checking drbd helper on offline node %s", node)
2853
          continue
2854
        msg = helpers[node].fail_msg
2855
        if msg:
2856
          raise errors.OpPrereqError("Error checking drbd helper on node"
2857
                                     " '%s': %s" % (node, msg),
2858
                                     errors.ECODE_ENVIRON)
2859
        node_helper = helpers[node].payload
2860
        if node_helper != self.op.drbd_helper:
2861
          raise errors.OpPrereqError("Error on node '%s': drbd helper is %s" %
2862
                                     (node, node_helper), errors.ECODE_ENVIRON)
2863

    
2864
    self.cluster = cluster = self.cfg.GetClusterInfo()
2865
    # validate params changes
2866
    if self.op.beparams:
2867
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2868
      self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
2869

    
2870
    if self.op.ndparams:
2871
      utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
2872
      self.new_ndparams = cluster.SimpleFillND(self.op.ndparams)
2873

    
2874
    if self.op.nicparams:
2875
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2876
      self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
2877
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2878
      nic_errors = []
2879

    
2880
      # check all instances for consistency
2881
      for instance in self.cfg.GetAllInstancesInfo().values():
2882
        for nic_idx, nic in enumerate(instance.nics):
2883
          params_copy = copy.deepcopy(nic.nicparams)
2884
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2885

    
2886
          # check parameter syntax
2887
          try:
2888
            objects.NIC.CheckParameterSyntax(params_filled)
2889
          except errors.ConfigurationError, err:
2890
            nic_errors.append("Instance %s, nic/%d: %s" %
2891
                              (instance.name, nic_idx, err))
2892

    
2893
          # if we're moving instances to routed, check that they have an ip
2894
          target_mode = params_filled[constants.NIC_MODE]
2895
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2896
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2897
                              (instance.name, nic_idx))
2898
      if nic_errors:
2899
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2900
                                   "\n".join(nic_errors))
2901

    
2902
    # hypervisor list/parameters
2903
    self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
2904
    if self.op.hvparams:
2905
      for hv_name, hv_dict in self.op.hvparams.items():
2906
        if hv_name not in self.new_hvparams:
2907
          self.new_hvparams[hv_name] = hv_dict
2908
        else:
2909
          self.new_hvparams[hv_name].update(hv_dict)
2910

    
2911
    # os hypervisor parameters
2912
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2913
    if self.op.os_hvp:
2914
      for os_name, hvs in self.op.os_hvp.items():
2915
        if os_name not in self.new_os_hvp:
2916
          self.new_os_hvp[os_name] = hvs
2917
        else:
2918
          for hv_name, hv_dict in hvs.items():
2919
            if hv_name not in self.new_os_hvp[os_name]:
2920
              self.new_os_hvp[os_name][hv_name] = hv_dict
2921
            else:
2922
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2923

    
2924
    # os parameters
2925
    self.new_osp = objects.FillDict(cluster.osparams, {})
2926
    if self.op.osparams:
2927
      for os_name, osp in self.op.osparams.items():
2928
        if os_name not in self.new_osp:
2929
          self.new_osp[os_name] = {}
2930

    
2931
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
2932
                                                  use_none=True)
2933

    
2934
        if not self.new_osp[os_name]:
2935
          # we removed all parameters
2936
          del self.new_osp[os_name]
2937
        else:
2938
          # check the parameter validity (remote check)
2939
          _CheckOSParams(self, False, [self.cfg.GetMasterNode()],
2940
                         os_name, self.new_osp[os_name])
2941

    
2942
    # changes to the hypervisor list
2943
    if self.op.enabled_hypervisors is not None:
2944
      self.hv_list = self.op.enabled_hypervisors
2945
      for hv in self.hv_list:
2946
        # if the hypervisor doesn't already exist in the cluster
2947
        # hvparams, we initialize it to empty, and then (in both
2948
        # cases) we make sure to fill the defaults, as we might not
2949
        # have a complete defaults list if the hypervisor wasn't
2950
        # enabled before
2951
        if hv not in new_hvp:
2952
          new_hvp[hv] = {}
2953
        new_hvp[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], new_hvp[hv])
2954
        utils.ForceDictType(new_hvp[hv], constants.HVS_PARAMETER_TYPES)
2955
    else:
2956
      self.hv_list = cluster.enabled_hypervisors
2957

    
2958
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2959
      # either the enabled list has changed, or the parameters have, validate
2960
      for hv_name, hv_params in self.new_hvparams.items():
2961
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2962
            (self.op.enabled_hypervisors and
2963
             hv_name in self.op.enabled_hypervisors)):
2964
          # either this is a new hypervisor, or its parameters have changed
2965
          hv_class = hypervisor.GetHypervisor(hv_name)
2966
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2967
          hv_class.CheckParameterSyntax(hv_params)
2968
          _CheckHVParams(self, node_list, hv_name, hv_params)
2969

    
2970
    if self.op.os_hvp:
2971
      # no need to check any newly-enabled hypervisors, since the
2972
      # defaults have already been checked in the above code-block
2973
      for os_name, os_hvp in self.new_os_hvp.items():
2974
        for hv_name, hv_params in os_hvp.items():
2975
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2976
          # we need to fill in the new os_hvp on top of the actual hv_p
2977
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2978
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2979
          hv_class = hypervisor.GetHypervisor(hv_name)
2980
          hv_class.CheckParameterSyntax(new_osp)
2981
          _CheckHVParams(self, node_list, hv_name, new_osp)
2982

    
2983
    if self.op.default_iallocator:
2984
      alloc_script = utils.FindFile(self.op.default_iallocator,
2985
                                    constants.IALLOCATOR_SEARCH_PATH,
2986
                                    os.path.isfile)
2987
      if alloc_script is None:
2988
        raise errors.OpPrereqError("Invalid default iallocator script '%s'"
2989
                                   " specified" % self.op.default_iallocator,
2990
                                   errors.ECODE_INVAL)
2991

    
2992
  def Exec(self, feedback_fn):
2993
    """Change the parameters of the cluster.
2994

2995
    """
2996
    if self.op.vg_name is not None:
2997
      new_volume = self.op.vg_name
2998
      if not new_volume:
2999
        new_volume = None
3000
      if new_volume != self.cfg.GetVGName():
3001
        self.cfg.SetVGName(new_volume)
3002
      else:
3003
        feedback_fn("Cluster LVM configuration already in desired"
3004
                    " state, not changing")
3005
    if self.op.drbd_helper is not None:
3006
      new_helper = self.op.drbd_helper
3007
      if not new_helper:
3008
        new_helper = None
3009
      if new_helper != self.cfg.GetDRBDHelper():
3010
        self.cfg.SetDRBDHelper(new_helper)
3011
      else:
3012
        feedback_fn("Cluster DRBD helper already in desired state,"
3013
                    " not changing")
3014
    if self.op.hvparams:
3015
      self.cluster.hvparams = self.new_hvparams
3016
    if self.op.os_hvp:
3017
      self.cluster.os_hvp = self.new_os_hvp
3018
    if self.op.enabled_hypervisors is not None:
3019
      self.cluster.hvparams = self.new_hvparams
3020
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
3021
    if self.op.beparams:
3022
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
3023
    if self.op.nicparams:
3024
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
3025
    if self.op.osparams:
3026
      self.cluster.osparams = self.new_osp
3027
    if self.op.ndparams:
3028
      self.cluster.ndparams = self.new_ndparams
3029

    
3030
    if self.op.candidate_pool_size is not None:
3031
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
3032
      # we need to update the pool size here, otherwise the save will fail
3033
      _AdjustCandidatePool(self, [])
3034

    
3035
    if self.op.maintain_node_health is not None:
3036
      self.cluster.maintain_node_health = self.op.maintain_node_health
3037

    
3038
    if self.op.prealloc_wipe_disks is not None:
3039
      self.cluster.prealloc_wipe_disks = self.op.prealloc_wipe_disks
3040

    
3041
    if self.op.add_uids is not None:
3042
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
3043

    
3044
    if self.op.remove_uids is not None:
3045
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
3046

    
3047
    if self.op.uid_pool is not None:
3048
      self.cluster.uid_pool = self.op.uid_pool
3049

    
3050
    if self.op.default_iallocator is not None:
3051
      self.cluster.default_iallocator = self.op.default_iallocator
3052

    
3053
    if self.op.reserved_lvs is not None:
3054
      self.cluster.reserved_lvs = self.op.reserved_lvs
3055

    
3056
    def helper_os(aname, mods, desc):
3057
      desc += " OS list"
3058
      lst = getattr(self.cluster, aname)
3059
      for key, val in mods:
3060
        if key == constants.DDM_ADD:
3061
          if val in lst:
3062
            feedback_fn("OS %s already in %s, ignoring" % (val, desc))
3063
          else:
3064
            lst.append(val)
3065
        elif key == constants.DDM_REMOVE:
3066
          if val in lst:
3067
            lst.remove(val)
3068
          else:
3069
            feedback_fn("OS %s not found in %s, ignoring" % (val, desc))
3070
        else:
3071
          raise errors.ProgrammerError("Invalid modification '%s'" % key)
3072

    
3073
    if self.op.hidden_os:
3074
      helper_os("hidden_os", self.op.hidden_os, "hidden")
3075

    
3076
    if self.op.blacklisted_os:
3077
      helper_os("blacklisted_os", self.op.blacklisted_os, "blacklisted")
3078

    
3079
    if self.op.master_netdev:
3080
      master = self.cfg.GetMasterNode()
3081
      feedback_fn("Shutting down master ip on the current netdev (%s)" %
3082
                  self.cluster.master_netdev)
3083
      result = self.rpc.call_node_stop_master(master, False)
3084
      result.Raise("Could not disable the master ip")
3085
      feedback_fn("Changing master_netdev from %s to %s" %
3086
                  (self.cluster.master_netdev, self.op.master_netdev))
3087
      self.cluster.master_netdev = self.op.master_netdev
3088

    
3089
    self.cfg.Update(self.cluster, feedback_fn)
3090

    
3091
    if self.op.master_netdev:
3092
      feedback_fn("Starting the master ip on the new master netdev (%s)" %
3093
                  self.op.master_netdev)
3094
      result = self.rpc.call_node_start_master(master, False, False)
3095
      if result.fail_msg:
3096
        self.LogWarning("Could not re-enable the master ip on"
3097
                        " the master, please restart manually: %s",
3098
                        result.fail_msg)
3099

    
3100

    
3101
def _UploadHelper(lu, nodes, fname):
3102
  """Helper for uploading a file and showing warnings.
3103

3104
  """
3105
  if os.path.exists(fname):
3106
    result = lu.rpc.call_upload_file(nodes, fname)
3107
    for to_node, to_result in result.items():
3108
      msg = to_result.fail_msg
3109
      if msg:
3110
        msg = ("Copy of file %s to node %s failed: %s" %
3111
               (fname, to_node, msg))
3112
        lu.proc.LogWarning(msg)
3113

    
3114

    
3115
def _RedistributeAncillaryFiles(lu, additional_nodes=None, additional_vm=True):
3116
  """Distribute additional files which are part of the cluster configuration.
3117

3118
  ConfigWriter takes care of distributing the config and ssconf files, but
3119
  there are more files which should be distributed to all nodes. This function
3120
  makes sure those are copied.
3121

3122
  @param lu: calling logical unit
3123
  @param additional_nodes: list of nodes not in the config to distribute to
3124
  @type additional_vm: boolean
3125
  @param additional_vm: whether the additional nodes are vm-capable or not
3126

3127
  """
3128
  # 1. Gather target nodes
3129
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
3130
  dist_nodes = lu.cfg.GetOnlineNodeList()
3131
  nvm_nodes = lu.cfg.GetNonVmCapableNodeList()
3132
  vm_nodes = [name for name in dist_nodes if name not in nvm_nodes]
3133
  if additional_nodes is not None:
3134
    dist_nodes.extend(additional_nodes)
3135
    if additional_vm:
3136
      vm_nodes.extend(additional_nodes)
3137
  if myself.name in dist_nodes:
3138
    dist_nodes.remove(myself.name)
3139
  if myself.name in vm_nodes:
3140
    vm_nodes.remove(myself.name)
3141

    
3142
  # 2. Gather files to distribute
3143
  dist_files = set([constants.ETC_HOSTS,
3144
                    constants.SSH_KNOWN_HOSTS_FILE,
3145
                    constants.RAPI_CERT_FILE,
3146
                    constants.RAPI_USERS_FILE,
3147
                    constants.CONFD_HMAC_KEY,
3148
                    constants.CLUSTER_DOMAIN_SECRET_FILE,
3149
                   ])
3150

    
3151
  vm_files = set()
3152
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
3153
  for hv_name in enabled_hypervisors:
3154
    hv_class = hypervisor.GetHypervisor(hv_name)
3155
    vm_files.update(hv_class.GetAncillaryFiles())
3156

    
3157
  # 3. Perform the files upload
3158
  for fname in dist_files:
3159
    _UploadHelper(lu, dist_nodes, fname)
3160
  for fname in vm_files:
3161
    _UploadHelper(lu, vm_nodes, fname)
3162

    
3163

    
3164
class LUClusterRedistConf(NoHooksLU):
3165
  """Force the redistribution of cluster configuration.
3166

3167
  This is a very simple LU.
3168

3169
  """
3170
  REQ_BGL = False
3171

    
3172
  def ExpandNames(self):
3173
    self.needed_locks = {
3174
      locking.LEVEL_NODE: locking.ALL_SET,
3175
    }
3176
    self.share_locks[locking.LEVEL_NODE] = 1
3177

    
3178
  def Exec(self, feedback_fn):
3179
    """Redistribute the configuration.
3180

3181
    """
3182
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
3183
    _RedistributeAncillaryFiles(self)
3184

    
3185

    
3186
def _WaitForSync(lu, instance, disks=None, oneshot=False):
3187
  """Sleep and poll for an instance's disk to sync.
3188

3189
  """
3190
  if not instance.disks or disks is not None and not disks:
3191
    return True
3192

    
3193
  disks = _ExpandCheckDisks(instance, disks)
3194

    
3195
  if not oneshot:
3196
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
3197

    
3198
  node = instance.primary_node
3199

    
3200
  for dev in disks:
3201
    lu.cfg.SetDiskID(dev, node)
3202

    
3203
  # TODO: Convert to utils.Retry
3204

    
3205
  retries = 0
3206
  degr_retries = 10 # in seconds, as we sleep 1 second each time
3207
  while True:
3208
    max_time = 0
3209
    done = True
3210
    cumul_degraded = False
3211
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, disks)
3212
    msg = rstats.fail_msg
3213
    if msg:
3214
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
3215
      retries += 1
3216
      if retries >= 10:
3217
        raise errors.RemoteError("Can't contact node %s for mirror data,"
3218
                                 " aborting." % node)
3219
      time.sleep(6)
3220
      continue
3221
    rstats = rstats.payload
3222
    retries = 0
3223
    for i, mstat in enumerate(rstats):
3224
      if mstat is None:
3225
        lu.LogWarning("Can't compute data for node %s/%s",
3226
                           node, disks[i].iv_name)
3227
        continue
3228

    
3229
      cumul_degraded = (cumul_degraded or
3230
                        (mstat.is_degraded and mstat.sync_percent is None))
3231
      if mstat.sync_percent is not None:
3232
        done = False
3233
        if mstat.estimated_time is not None:
3234
          rem_time = ("%s remaining (estimated)" %
3235
                      utils.FormatSeconds(mstat.estimated_time))
3236
          max_time = mstat.estimated_time
3237
        else:
3238
          rem_time = "no time estimate"
3239
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
3240
                        (disks[i].iv_name, mstat.sync_percent, rem_time))
3241

    
3242
    # if we're done but degraded, let's do a few small retries, to
3243
    # make sure we see a stable and not transient situation; therefore
3244
    # we force restart of the loop
3245
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
3246
      logging.info("Degraded disks found, %d retries left", degr_retries)
3247
      degr_retries -= 1
3248
      time.sleep(1)
3249
      continue
3250

    
3251
    if done or oneshot:
3252
      break
3253

    
3254
    time.sleep(min(60, max_time))
3255

    
3256
  if done:
3257
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
3258
  return not cumul_degraded
3259

    
3260

    
3261
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
3262
  """Check that mirrors are not degraded.
3263

3264
  The ldisk parameter, if True, will change the test from the
3265
  is_degraded attribute (which represents overall non-ok status for
3266
  the device(s)) to the ldisk (representing the local storage status).
3267

3268
  """
3269
  lu.cfg.SetDiskID(dev, node)
3270

    
3271
  result = True
3272

    
3273
  if on_primary or dev.AssembleOnSecondary():
3274
    rstats = lu.rpc.call_blockdev_find(node, dev)
3275
    msg = rstats.fail_msg
3276
    if msg:
3277
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
3278
      result = False
3279
    elif not rstats.payload:
3280
      lu.LogWarning("Can't find disk on node %s", node)
3281
      result = False
3282
    else:
3283
      if ldisk:
3284
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
3285
      else:
3286
        result = result and not rstats.payload.is_degraded
3287

    
3288
  if dev.children:
3289
    for child in dev.children:
3290
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
3291

    
3292
  return result
3293

    
3294

    
3295
class LUOobCommand(NoHooksLU):
3296
  """Logical unit for OOB handling.
3297

3298
  """
3299
  REG_BGL = False
3300
  _SKIP_MASTER = (constants.OOB_POWER_OFF, constants.OOB_POWER_CYCLE)
3301

    
3302
  def CheckPrereq(self):
3303
    """Check prerequisites.
3304

3305
    This checks:
3306
     - the node exists in the configuration
3307
     - OOB is supported
3308

3309
    Any errors are signaled by raising errors.OpPrereqError.
3310

3311
    """
3312
    self.nodes = []
3313
    self.master_node = self.cfg.GetMasterNode()
3314

    
3315
    assert self.op.power_delay >= 0.0
3316

    
3317
    if self.op.node_names:
3318
      if self.op.command in self._SKIP_MASTER:
3319
        if self.master_node in self.op.node_names:
3320
          master_node_obj = self.cfg.GetNodeInfo(self.master_node)
3321
          master_oob_handler = _SupportsOob(self.cfg, master_node_obj)
3322

    
3323
          if master_oob_handler:
3324
            additional_text = ("Run '%s %s %s' if you want to operate on the"
3325
                               " master regardless") % (master_oob_handler,
3326
                                                        self.op.command,
3327
                                                        self.master_node)
3328
          else:
3329
            additional_text = "The master node does not support out-of-band"
3330

    
3331
          raise errors.OpPrereqError(("Operating on the master node %s is not"
3332
                                      " allowed for %s\n%s") %
3333
                                     (self.master_node, self.op.command,
3334
                                      additional_text), errors.ECODE_INVAL)
3335
    else:
3336
      self.op.node_names = self.cfg.GetNodeList()
3337
      if self.op.command in self._SKIP_MASTER:
3338
        self.op.node_names.remove(self.master_node)
3339

    
3340
    if self.op.command in self._SKIP_MASTER:
3341
      assert self.master_node not in self.op.node_names
3342

    
3343
    for node_name in self.op.node_names:
3344
      node = self.cfg.GetNodeInfo(node_name)
3345

    
3346
      if node is None:
3347
        raise errors.OpPrereqError("Node %s not found" % node_name,
3348
                                   errors.ECODE_NOENT)
3349
      else:
3350
        self.nodes.append(node)
3351

    
3352
      if (not self.op.ignore_status and
3353
          (self.op.command == constants.OOB_POWER_OFF and not node.offline)):
3354
        raise errors.OpPrereqError(("Cannot power off node %s because it is"
3355
                                    " not marked offline") % node_name,
3356
                                   errors.ECODE_STATE)
3357

    
3358
  def ExpandNames(self):
3359
    """Gather locks we need.
3360

3361
    """
3362
    if self.op.node_names:
3363
      self.op.node_names = [_ExpandNodeName(self.cfg, name)
3364
                            for name in self.op.node_names]
3365
      lock_names = self.op.node_names
3366
    else:
3367
      lock_names = locking.ALL_SET
3368

    
3369
    self.needed_locks = {
3370
      locking.LEVEL_NODE: lock_names,
3371
      }
3372

    
3373
  def Exec(self, feedback_fn):
3374
    """Execute OOB and return result if we expect any.
3375

3376
    """
3377
    master_node = self.master_node
3378
    ret = []
3379

    
3380
    for idx, node in enumerate(self.nodes):
3381
      node_entry = [(constants.RS_NORMAL, node.name)]
3382
      ret.append(node_entry)
3383

    
3384
      oob_program = _SupportsOob(self.cfg, node)
3385

    
3386
      if not oob_program:
3387
        node_entry.append((constants.RS_UNAVAIL, None))
3388
        continue
3389

    
3390
      logging.info("Executing out-of-band command '%s' using '%s' on %s",
3391
                   self.op.command, oob_program, node.name)
3392
      result = self.rpc.call_run_oob(master_node, oob_program,
3393
                                     self.op.command, node.name,
3394
                                     self.op.timeout)
3395

    
3396
      if result.fail_msg:
3397
        self.LogWarning("On node '%s' out-of-band RPC failed with: %s",
3398
                        node.name, result.fail_msg)
3399
        node_entry.append((constants.RS_NODATA, None))
3400
      else:
3401
        try:
3402
          self._CheckPayload(result)
3403
        except errors.OpExecError, err:
3404
          self.LogWarning("The payload returned by '%s' is not valid: %s",
3405
                          node.name, err)
3406
          node_entry.append((constants.RS_NODATA, None))
3407
        else:
3408
          if self.op.command == constants.OOB_HEALTH:
3409
            # For health we should log important events
3410
            for item, status in result.payload:
3411
              if status in [constants.OOB_STATUS_WARNING,
3412
                            constants.OOB_STATUS_CRITICAL]:
3413
                self.LogWarning("On node '%s' item '%s' has status '%s'",
3414
                                node.name, item, status)
3415

    
3416
          if self.op.command == constants.OOB_POWER_ON:
3417
            node.powered = True
3418
          elif self.op.command == constants.OOB_POWER_OFF:
3419
            node.powered = False
3420
          elif self.op.command == constants.OOB_POWER_STATUS:
3421
            powered = result.payload[constants.OOB_POWER_STATUS_POWERED]
3422
            if powered != node.powered:
3423
              logging.warning(("Recorded power state (%s) of node '%s' does not"
3424
                               " match actual power state (%s)"), node.powered,
3425
                              node.name, powered)
3426

    
3427
          # For configuration changing commands we should update the node
3428
          if self.op.command in (constants.OOB_POWER_ON,
3429
                                 constants.OOB_POWER_OFF):
3430
            self.cfg.Update(node, feedback_fn)
3431

    
3432
          node_entry.append((constants.RS_NORMAL, result.payload))
3433

    
3434
          if (self.op.command == constants.OOB_POWER_ON and
3435
              idx < len(self.nodes) - 1):
3436
            time.sleep(self.op.power_delay)
3437

    
3438
    return ret
3439

    
3440
  def _CheckPayload(self, result):
3441
    """Checks if the payload is valid.
3442

3443
    @param result: RPC result
3444
    @raises errors.OpExecError: If payload is not valid
3445

3446
    """
3447
    errs = []
3448
    if self.op.command == constants.OOB_HEALTH:
3449
      if not isinstance(result.payload, list):
3450
        errs.append("command 'health' is expected to return a list but got %s" %
3451
                    type(result.payload))
3452
      else:
3453
        for item, status in result.payload:
3454
          if status not in constants.OOB_STATUSES:
3455
            errs.append("health item '%s' has invalid status '%s'" %
3456
                        (item, status))
3457

    
3458
    if self.op.command == constants.OOB_POWER_STATUS:
3459
      if not isinstance(result.payload, dict):
3460
        errs.append("power-status is expected to return a dict but got %s" %
3461
                    type(result.payload))
3462

    
3463
    if self.op.command in [
3464
        constants.OOB_POWER_ON,
3465
        constants.OOB_POWER_OFF,
3466
        constants.OOB_POWER_CYCLE,
3467
        ]:
3468
      if result.payload is not None:
3469
        errs.append("%s is expected to not return payload but got '%s'" %
3470
                    (self.op.command, result.payload))
3471

    
3472
    if errs:
3473
      raise errors.OpExecError("Check of out-of-band payload failed due to %s" %
3474
                               utils.CommaJoin(errs))
3475

    
3476
class _OsQuery(_QueryBase):
3477
  FIELDS = query.OS_FIELDS
3478

    
3479
  def ExpandNames(self, lu):
3480
    # Lock all nodes in shared mode
3481
    # Temporary removal of locks, should be reverted later
3482
    # TODO: reintroduce locks when they are lighter-weight
3483
    lu.needed_locks = {}
3484
    #self.share_locks[locking.LEVEL_NODE] = 1
3485
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3486

    
3487
    # The following variables interact with _QueryBase._GetNames
3488
    if self.names:
3489
      self.wanted = self.names
3490
    else:
3491
      self.wanted = locking.ALL_SET
3492

    
3493
    self.do_locking = self.use_locking
3494

    
3495
  def DeclareLocks(self, lu, level):
3496
    pass
3497

    
3498
  @staticmethod
3499
  def _DiagnoseByOS(rlist):
3500
    """Remaps a per-node return list into an a per-os per-node dictionary
3501

3502
    @param rlist: a map with node names as keys and OS objects as values
3503

3504
    @rtype: dict
3505
    @return: a dictionary with osnames as keys and as value another
3506
        map, with nodes as keys and tuples of (path, status, diagnose,
3507
        variants, parameters, api_versions) as values, eg::
3508

3509
          {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
3510
                                     (/srv/..., False, "invalid api")],
3511
                           "node2": [(/srv/..., True, "", [], [])]}
3512
          }
3513

3514
    """
3515
    all_os = {}
3516
    # we build here the list of nodes that didn't fail the RPC (at RPC
3517
    # level), so that nodes with a non-responding node daemon don't
3518
    # make all OSes invalid
3519
    good_nodes = [node_name for node_name in rlist
3520
                  if not rlist[node_name].fail_msg]
3521
    for node_name, nr in rlist.items():
3522
      if nr.fail_msg or not nr.payload:
3523
        continue
3524
      for (name, path, status, diagnose, variants,
3525
           params, api_versions) in nr.payload:
3526
        if name not in all_os:
3527
          # build a list of nodes for this os containing empty lists
3528
          # for each node in node_list
3529
          all_os[name] = {}
3530
          for nname in good_nodes:
3531
            all_os[name][nname] = []
3532
        # convert params from [name, help] to (name, help)
3533
        params = [tuple(v) for v in params]
3534
        all_os[name][node_name].append((path, status, diagnose,
3535
                                        variants, params, api_versions))
3536
    return all_os
3537

    
3538
  def _GetQueryData(self, lu):
3539
    """Computes the list of nodes and their attributes.
3540

3541
    """
3542
    # Locking is not used
3543
    assert not (lu.acquired_locks or self.do_locking or self.use_locking)
3544

    
3545
    valid_nodes = [node.name
3546
                   for node in lu.cfg.GetAllNodesInfo().values()
3547
                   if not node.offline and node.vm_capable]
3548
    pol = self._DiagnoseByOS(lu.rpc.call_os_diagnose(valid_nodes))
3549
    cluster = lu.cfg.GetClusterInfo()
3550

    
3551
    data = {}
3552

    
3553
    for (os_name, os_data) in pol.items():
3554
      info = query.OsInfo(name=os_name, valid=True, node_status=os_data,
3555
                          hidden=(os_name in cluster.hidden_os),
3556
                          blacklisted=(os_name in cluster.blacklisted_os))
3557

    
3558
      variants = set()
3559
      parameters = set()
3560
      api_versions = set()
3561

    
3562
      for idx, osl in enumerate(os_data.values()):
3563
        info.valid = bool(info.valid and osl and osl[0][1])
3564
        if not info.valid:
3565
          break
3566

    
3567
        (node_variants, node_params, node_api) = osl[0][3:6]
3568
        if idx == 0:
3569
          # First entry
3570
          variants.update(node_variants)
3571
          parameters.update(node_params)
3572
          api_versions.update(node_api)
3573
        else:
3574
          # Filter out inconsistent values
3575
          variants.intersection_update(node_variants)
3576
          parameters.intersection_update(node_params)
3577
          api_versions.intersection_update(node_api)
3578

    
3579
      info.variants = list(variants)
3580
      info.parameters = list(parameters)
3581
      info.api_versions = list(api_versions)
3582

    
3583
      data[os_name] = info
3584

    
3585
    # Prepare data in requested order
3586
    return [data[name] for name in self._GetNames(lu, pol.keys(), None)
3587
            if name in data]
3588

    
3589

    
3590
class LUOsDiagnose(NoHooksLU):
3591
  """Logical unit for OS diagnose/query.
3592

3593
  """
3594
  REQ_BGL = False
3595

    
3596
  @staticmethod
3597
  def _BuildFilter(fields, names):
3598
    """Builds a filter for querying OSes.
3599

3600
    """
3601
    name_filter = qlang.MakeSimpleFilter("name", names)
3602

    
3603
    # Legacy behaviour: Hide hidden, blacklisted or invalid OSes if the
3604
    # respective field is not requested
3605
    status_filter = [[qlang.OP_NOT, [qlang.OP_TRUE, fname]]
3606
                     for fname in ["hidden", "blacklisted"]
3607
                     if fname not in fields]
3608
    if "valid" not in fields:
3609
      status_filter.append([qlang.OP_TRUE, "valid"])
3610

    
3611
    if status_filter:
3612
      status_filter.insert(0, qlang.OP_AND)
3613
    else:
3614
      status_filter = None
3615

    
3616
    if name_filter and status_filter:
3617
      return [qlang.OP_AND, name_filter, status_filter]
3618
    elif name_filter:
3619
      return name_filter
3620
    else:
3621
      return status_filter
3622

    
3623
  def CheckArguments(self):
3624
    self.oq = _OsQuery(self._BuildFilter(self.op.output_fields, self.op.names),
3625
                       self.op.output_fields, False)
3626

    
3627
  def ExpandNames(self):
3628
    self.oq.ExpandNames(self)
3629

    
3630
  def Exec(self, feedback_fn):
3631
    return self.oq.OldStyleQuery(self)
3632

    
3633

    
3634
class LUNodeRemove(LogicalUnit):
3635
  """Logical unit for removing a node.
3636

3637
  """
3638
  HPATH = "node-remove"
3639
  HTYPE = constants.HTYPE_NODE
3640

    
3641
  def BuildHooksEnv(self):
3642
    """Build hooks env.
3643

3644
    This doesn't run on the target node in the pre phase as a failed
3645
    node would then be impossible to remove.
3646

3647
    """
3648
    return {
3649
      "OP_TARGET": self.op.node_name,
3650
      "NODE_NAME": self.op.node_name,
3651
      }
3652

    
3653
  def BuildHooksNodes(self):
3654
    """Build hooks nodes.
3655

3656
    """
3657
    all_nodes = self.cfg.GetNodeList()
3658
    try:
3659
      all_nodes.remove(self.op.node_name)
3660
    except ValueError:
3661
      logging.warning("Node '%s', which is about to be removed, was not found"
3662
                      " in the list of all nodes", self.op.node_name)
3663
    return (all_nodes, all_nodes)
3664

    
3665
  def CheckPrereq(self):
3666
    """Check prerequisites.
3667

3668
    This checks:
3669
     - the node exists in the configuration
3670
     - it does not have primary or secondary instances
3671
     - it's not the master
3672

3673
    Any errors are signaled by raising errors.OpPrereqError.
3674

3675
    """
3676
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3677
    node = self.cfg.GetNodeInfo(self.op.node_name)
3678
    assert node is not None
3679

    
3680
    instance_list = self.cfg.GetInstanceList()
3681

    
3682
    masternode = self.cfg.GetMasterNode()
3683
    if node.name == masternode:
3684
      raise errors.OpPrereqError("Node is the master node,"
3685
                                 " you need to failover first.",
3686
                                 errors.ECODE_INVAL)
3687

    
3688
    for instance_name in instance_list:
3689
      instance = self.cfg.GetInstanceInfo(instance_name)
3690
      if node.name in instance.all_nodes:
3691
        raise errors.OpPrereqError("Instance %s is still running on the node,"
3692
                                   " please remove first." % instance_name,
3693
                                   errors.ECODE_INVAL)
3694
    self.op.node_name = node.name
3695
    self.node = node
3696

    
3697
  def Exec(self, feedback_fn):
3698
    """Removes the node from the cluster.
3699

3700
    """
3701
    node = self.node
3702
    logging.info("Stopping the node daemon and removing configs from node %s",
3703
                 node.name)
3704

    
3705
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3706

    
3707
    # Promote nodes to master candidate as needed
3708
    _AdjustCandidatePool(self, exceptions=[node.name])
3709
    self.context.RemoveNode(node.name)
3710

    
3711
    # Run post hooks on the node before it's removed
3712
    _RunPostHook(self, node.name)
3713

    
3714
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
3715
    msg = result.fail_msg
3716
    if msg:
3717
      self.LogWarning("Errors encountered on the remote node while leaving"
3718
                      " the cluster: %s", msg)
3719

    
3720
    # Remove node from our /etc/hosts
3721
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3722
      master_node = self.cfg.GetMasterNode()
3723
      result = self.rpc.call_etc_hosts_modify(master_node,
3724
                                              constants.ETC_HOSTS_REMOVE,
3725
                                              node.name, None)
3726
      result.Raise("Can't update hosts file with new host data")
3727
      _RedistributeAncillaryFiles(self)
3728

    
3729

    
3730
class _NodeQuery(_QueryBase):
3731
  FIELDS = query.NODE_FIELDS
3732

    
3733
  def ExpandNames(self, lu):
3734
    lu.needed_locks = {}
3735
    lu.share_locks[locking.LEVEL_NODE] = 1
3736

    
3737
    if self.names:
3738
      self.wanted = _GetWantedNodes(lu, self.names)
3739
    else:
3740
      self.wanted = locking.ALL_SET
3741

    
3742
    self.do_locking = (self.use_locking and
3743
                       query.NQ_LIVE in self.requested_data)
3744

    
3745
    if self.do_locking:
3746
      # if we don't request only static fields, we need to lock the nodes
3747
      lu.needed_locks[locking.LEVEL_NODE] = self.wanted
3748

    
3749
  def DeclareLocks(self, lu, level):
3750
    pass
3751

    
3752
  def _GetQueryData(self, lu):
3753
    """Computes the list of nodes and their attributes.
3754

3755
    """
3756
    all_info = lu.cfg.GetAllNodesInfo()
3757

    
3758
    nodenames = self._GetNames(lu, all_info.keys(), locking.LEVEL_NODE)
3759

    
3760
    # Gather data as requested
3761
    if query.NQ_LIVE in self.requested_data:
3762
      # filter out non-vm_capable nodes
3763
      toquery_nodes = [name for name in nodenames if all_info[name].vm_capable]
3764

    
3765
      node_data = lu.rpc.call_node_info(toquery_nodes, lu.cfg.GetVGName(),
3766
                                        lu.cfg.GetHypervisorType())
3767
      live_data = dict((name, nresult.payload)
3768
                       for (name, nresult) in node_data.items()
3769
                       if not nresult.fail_msg and nresult.payload)
3770
    else:
3771
      live_data = None
3772

    
3773
    if query.NQ_INST in self.requested_data:
3774
      node_to_primary = dict([(name, set()) for name in nodenames])
3775
      node_to_secondary = dict([(name, set()) for name in nodenames])
3776

    
3777
      inst_data = lu.cfg.GetAllInstancesInfo()
3778

    
3779
      for inst in inst_data.values():
3780
        if inst.primary_node in node_to_primary:
3781
          node_to_primary[inst.primary_node].add(inst.name)
3782
        for secnode in inst.secondary_nodes:
3783
          if secnode in node_to_secondary:
3784
            node_to_secondary[secnode].add(inst.name)
3785
    else:
3786
      node_to_primary = None
3787
      node_to_secondary = None
3788

    
3789
    if query.NQ_OOB in self.requested_data:
3790
      oob_support = dict((name, bool(_SupportsOob(lu.cfg, node)))
3791
                         for name, node in all_info.iteritems())
3792
    else:
3793
      oob_support = None
3794

    
3795
    if query.NQ_GROUP in self.requested_data:
3796
      groups = lu.cfg.GetAllNodeGroupsInfo()
3797
    else:
3798
      groups = {}
3799

    
3800
    return query.NodeQueryData([all_info[name] for name in nodenames],
3801
                               live_data, lu.cfg.GetMasterNode(),
3802
                               node_to_primary, node_to_secondary, groups,
3803
                               oob_support, lu.cfg.GetClusterInfo())
3804

    
3805

    
3806
class LUNodeQuery(NoHooksLU):
3807
  """Logical unit for querying nodes.
3808

3809
  """
3810
  # pylint: disable-msg=W0142
3811
  REQ_BGL = False
3812

    
3813
  def CheckArguments(self):
3814
    self.nq = _NodeQuery(qlang.MakeSimpleFilter("name", self.op.names),
3815
                         self.op.output_fields, self.op.use_locking)
3816

    
3817
  def ExpandNames(self):
3818
    self.nq.ExpandNames(self)
3819

    
3820
  def Exec(self, feedback_fn):
3821
    return self.nq.OldStyleQuery(self)
3822

    
3823

    
3824
class LUNodeQueryvols(NoHooksLU):
3825
  """Logical unit for getting volumes on node(s).
3826

3827
  """
3828
  REQ_BGL = False
3829
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3830
  _FIELDS_STATIC = utils.FieldSet("node")
3831

    
3832
  def CheckArguments(self):
3833
    _CheckOutputFields(static=self._FIELDS_STATIC,
3834
                       dynamic=self._FIELDS_DYNAMIC,
3835
                       selected=self.op.output_fields)
3836

    
3837
  def ExpandNames(self):
3838
    self.needed_locks = {}
3839
    self.share_locks[locking.LEVEL_NODE] = 1
3840
    if not self.op.nodes:
3841
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3842
    else:
3843
      self.needed_locks[locking.LEVEL_NODE] = \
3844
        _GetWantedNodes(self, self.op.nodes)
3845

    
3846
  def Exec(self, feedback_fn):
3847
    """Computes the list of nodes and their attributes.
3848

3849
    """
3850
    nodenames = self.acquired_locks[locking.LEVEL_NODE]
3851
    volumes = self.rpc.call_node_volumes(nodenames)
3852

    
3853
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3854
             in self.cfg.GetInstanceList()]
3855

    
3856
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3857

    
3858
    output = []
3859
    for node in nodenames:
3860
      nresult = volumes[node]
3861
      if nresult.offline:
3862
        continue
3863
      msg = nresult.fail_msg
3864
      if msg:
3865
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3866
        continue
3867

    
3868
      node_vols = nresult.payload[:]
3869
      node_vols.sort(key=lambda vol: vol['dev'])
3870

    
3871
      for vol in node_vols:
3872
        node_output = []
3873
        for field in self.op.output_fields:
3874
          if field == "node":
3875
            val = node
3876
          elif field == "phys":
3877
            val = vol['dev']
3878
          elif field == "vg":
3879
            val = vol['vg']
3880
          elif field == "name":
3881
            val = vol['name']
3882
          elif field == "size":
3883
            val = int(float(vol['size']))
3884
          elif field == "instance":
3885
            for inst in ilist:
3886
              if node not in lv_by_node[inst]:
3887
                continue
3888
              if vol['name'] in lv_by_node[inst][node]:
3889
                val = inst.name
3890
                break
3891
            else:
3892
              val = '-'
3893
          else:
3894
            raise errors.ParameterError(field)
3895
          node_output.append(str(val))
3896

    
3897
        output.append(node_output)
3898

    
3899
    return output
3900

    
3901

    
3902
class LUNodeQueryStorage(NoHooksLU):
3903
  """Logical unit for getting information on storage units on node(s).
3904

3905
  """
3906
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3907
  REQ_BGL = False
3908

    
3909
  def CheckArguments(self):
3910
    _CheckOutputFields(static=self._FIELDS_STATIC,
3911
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3912
                       selected=self.op.output_fields)
3913

    
3914
  def ExpandNames(self):
3915
    self.needed_locks = {}
3916
    self.share_locks[locking.LEVEL_NODE] = 1
3917

    
3918
    if self.op.nodes:
3919
      self.needed_locks[locking.LEVEL_NODE] = \
3920
        _GetWantedNodes(self, self.op.nodes)
3921
    else:
3922
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3923

    
3924
  def Exec(self, feedback_fn):
3925
    """Computes the list of nodes and their attributes.
3926

3927
    """
3928
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3929

    
3930
    # Always get name to sort by
3931
    if constants.SF_NAME in self.op.output_fields:
3932
      fields = self.op.output_fields[:]
3933
    else:
3934
      fields = [constants.SF_NAME] + self.op.output_fields
3935

    
3936
    # Never ask for node or type as it's only known to the LU
3937
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3938
      while extra in fields:
3939
        fields.remove(extra)
3940

    
3941
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3942
    name_idx = field_idx[constants.SF_NAME]
3943

    
3944
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3945
    data = self.rpc.call_storage_list(self.nodes,
3946
                                      self.op.storage_type, st_args,
3947
                                      self.op.name, fields)
3948

    
3949
    result = []
3950

    
3951
    for node in utils.NiceSort(self.nodes):
3952
      nresult = data[node]
3953
      if nresult.offline:
3954
        continue
3955

    
3956
      msg = nresult.fail_msg
3957
      if msg:
3958
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3959
        continue
3960

    
3961
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3962

    
3963
      for name in utils.NiceSort(rows.keys()):
3964
        row = rows[name]
3965

    
3966
        out = []
3967

    
3968
        for field in self.op.output_fields:
3969
          if field == constants.SF_NODE:
3970
            val = node
3971
          elif field == constants.SF_TYPE:
3972
            val = self.op.storage_type
3973
          elif field in field_idx:
3974
            val = row[field_idx[field]]
3975
          else:
3976
            raise errors.ParameterError(field)
3977

    
3978
          out.append(val)
3979

    
3980
        result.append(out)
3981

    
3982
    return result
3983

    
3984

    
3985
class _InstanceQuery(_QueryBase):
3986
  FIELDS = query.INSTANCE_FIELDS
3987

    
3988
  def ExpandNames(self, lu):
3989
    lu.needed_locks = {}
3990
    lu.share_locks[locking.LEVEL_INSTANCE] = 1
3991
    lu.share_locks[locking.LEVEL_NODE] = 1
3992

    
3993
    if self.names:
3994
      self.wanted = _GetWantedInstances(lu, self.names)
3995
    else:
3996
      self.wanted = locking.ALL_SET
3997

    
3998
    self.do_locking = (self.use_locking and
3999
                       query.IQ_LIVE in self.requested_data)
4000
    if self.do_locking:
4001
      lu.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
4002
      lu.needed_locks[locking.LEVEL_NODE] = []
4003
      lu.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
4004

    
4005
  def DeclareLocks(self, lu, level):
4006
    if level == locking.LEVEL_NODE and self.do_locking:
4007
      lu._LockInstancesNodes() # pylint: disable-msg=W0212
4008

    
4009
  def _GetQueryData(self, lu):
4010
    """Computes the list of instances and their attributes.
4011

4012
    """
4013
    cluster = lu.cfg.GetClusterInfo()
4014
    all_info = lu.cfg.GetAllInstancesInfo()
4015

    
4016
    instance_names = self._GetNames(lu, all_info.keys(), locking.LEVEL_INSTANCE)
4017

    
4018
    instance_list = [all_info[name] for name in instance_names]
4019
    nodes = frozenset(itertools.chain(*(inst.all_nodes
4020
                                        for inst in instance_list)))
4021
    hv_list = list(set([inst.hypervisor for inst in instance_list]))
4022
    bad_nodes = []
4023
    offline_nodes = []
4024
    wrongnode_inst = set()
4025

    
4026
    # Gather data as requested
4027
    if self.requested_data & set([query.IQ_LIVE, query.IQ_CONSOLE]):
4028
      live_data = {}
4029
      node_data = lu.rpc.call_all_instances_info(nodes, hv_list)
4030
      for name in nodes:
4031
        result = node_data[name]
4032
        if result.offline:
4033
          # offline nodes will be in both lists
4034
          assert result.fail_msg
4035
          offline_nodes.append(name)
4036
        if result.fail_msg:
4037
          bad_nodes.append(name)
4038
        elif result.payload:
4039
          for inst in result.payload:
4040
            if all_info[inst].primary_node == name:
4041
              live_data.update(result.payload)
4042
            else:
4043
              wrongnode_inst.add(inst)
4044
        # else no instance is alive
4045
    else:
4046
      live_data = {}
4047

    
4048
    if query.IQ_DISKUSAGE in self.requested_data:
4049
      disk_usage = dict((inst.name,
4050
                         _ComputeDiskSize(inst.disk_template,
4051
                                          [{"size": disk.size}
4052
                                           for disk in inst.disks]))
4053
                        for inst in instance_list)
4054
    else:
4055
      disk_usage = None
4056

    
4057
    if query.IQ_CONSOLE in self.requested_data:
4058
      consinfo = {}
4059
      for inst in instance_list:
4060
        if inst.name in live_data:
4061
          # Instance is running
4062
          consinfo[inst.name] = _GetInstanceConsole(cluster, inst)
4063
        else:
4064
          consinfo[inst.name] = None
4065
      assert set(consinfo.keys()) == set(instance_names)
4066
    else:
4067
      consinfo = None
4068

    
4069
    return query.InstanceQueryData(instance_list, lu.cfg.GetClusterInfo(),
4070
                                   disk_usage, offline_nodes, bad_nodes,
4071
                                   live_data, wrongnode_inst, consinfo)
4072

    
4073

    
4074
class LUQuery(NoHooksLU):
4075
  """Query for resources/items of a certain kind.
4076

4077
  """
4078
  # pylint: disable-msg=W0142
4079
  REQ_BGL = False
4080

    
4081
  def CheckArguments(self):
4082
    qcls = _GetQueryImplementation(self.op.what)
4083