Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 3d16a983

History | View | Annotate | Download (354.4 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008 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

    
40
from ganeti import ssh
41
from ganeti import utils
42
from ganeti import errors
43
from ganeti import hypervisor
44
from ganeti import locking
45
from ganeti import constants
46
from ganeti import objects
47
from ganeti import serializer
48
from ganeti import ssconf
49
from ganeti import uidpool
50
from ganeti import compat
51
from ganeti import masterd
52

    
53
import ganeti.masterd.instance # pylint: disable-msg=W0611
54

    
55

    
56
# Modifiable default values; need to define these here before the
57
# actual LUs
58

    
59
def _EmptyList():
60
  """Returns an empty list.
61

62
  """
63
  return []
64

    
65

    
66
def _EmptyDict():
67
  """Returns an empty dict.
68

69
  """
70
  return {}
71

    
72

    
73
#: The without-default default value
74
_NoDefault = object()
75

    
76

    
77
#: The no-type (value to complex to check it in the type system)
78
_NoType = object()
79

    
80

    
81
# Some basic types
82
def _TNotNone(val):
83
  """Checks if the given value is not None.
84

85
  """
86
  return val is not None
87

    
88

    
89
def _TNone(val):
90
  """Checks if the given value is None.
91

92
  """
93
  return val is None
94

    
95

    
96
def _TBool(val):
97
  """Checks if the given value is a boolean.
98

99
  """
100
  return isinstance(val, bool)
101

    
102

    
103
def _TInt(val):
104
  """Checks if the given value is an integer.
105

106
  """
107
  return isinstance(val, int)
108

    
109

    
110
def _TFloat(val):
111
  """Checks if the given value is a float.
112

113
  """
114
  return isinstance(val, float)
115

    
116

    
117
def _TString(val):
118
  """Checks if the given value is a string.
119

120
  """
121
  return isinstance(val, basestring)
122

    
123

    
124
def _TTrue(val):
125
  """Checks if a given value evaluates to a boolean True value.
126

127
  """
128
  return bool(val)
129

    
130

    
131
def _TElemOf(target_list):
132
  """Builds a function that checks if a given value is a member of a list.
133

134
  """
135
  return lambda val: val in target_list
136

    
137

    
138
# Container types
139
def _TList(val):
140
  """Checks if the given value is a list.
141

142
  """
143
  return isinstance(val, list)
144

    
145

    
146
def _TDict(val):
147
  """Checks if the given value is a dictionary.
148

149
  """
150
  return isinstance(val, dict)
151

    
152

    
153
# Combinator types
154
def _TAnd(*args):
155
  """Combine multiple functions using an AND operation.
156

157
  """
158
  def fn(val):
159
    return compat.all(t(val) for t in args)
160
  return fn
161

    
162

    
163
def _TOr(*args):
164
  """Combine multiple functions using an AND operation.
165

166
  """
167
  def fn(val):
168
    return compat.any(t(val) for t in args)
169
  return fn
170

    
171

    
172
# Type aliases
173

    
174
#: a non-empty string
175
_TNonEmptyString = _TAnd(_TString, _TTrue)
176

    
177

    
178
#: a maybe non-empty string
179
_TMaybeString = _TOr(_TNonEmptyString, _TNone)
180

    
181

    
182
#: a maybe boolean (bool or none)
183
_TMaybeBool = _TOr(_TBool, _TNone)
184

    
185

    
186
#: a positive integer
187
_TPositiveInt = _TAnd(_TInt, lambda v: v >= 0)
188

    
189
#: a strictly positive integer
190
_TStrictPositiveInt = _TAnd(_TInt, lambda v: v > 0)
191

    
192

    
193
def _TListOf(my_type):
194
  """Checks if a given value is a list with all elements of the same type.
195

196
  """
197
  return _TAnd(_TList,
198
               lambda lst: compat.all(my_type(v) for v in lst))
199

    
200

    
201
def _TDictOf(key_type, val_type):
202
  """Checks a dict type for the type of its key/values.
203

204
  """
205
  return _TAnd(_TDict,
206
               lambda my_dict: (compat.all(key_type(v) for v in my_dict.keys())
207
                                and compat.all(val_type(v)
208
                                               for v in my_dict.values())))
209

    
210

    
211
# Common opcode attributes
212

    
213
#: output fields for a query operation
214
_POutputFields = ("output_fields", _NoDefault, _TListOf(_TNonEmptyString))
215

    
216

    
217
#: the shutdown timeout
218
_PShutdownTimeout = ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
219
                     _TPositiveInt)
220

    
221
#: the force parameter
222
_PForce = ("force", False, _TBool)
223

    
224
#: a required instance name (for single-instance LUs)
225
_PInstanceName = ("instance_name", _NoDefault, _TNonEmptyString)
226

    
227

    
228
#: a required node name (for single-node LUs)
229
_PNodeName = ("node_name", _NoDefault, _TNonEmptyString)
230

    
231

    
232
# End types
233
class LogicalUnit(object):
234
  """Logical Unit base class.
235

236
  Subclasses must follow these rules:
237
    - implement ExpandNames
238
    - implement CheckPrereq (except when tasklets are used)
239
    - implement Exec (except when tasklets are used)
240
    - implement BuildHooksEnv
241
    - redefine HPATH and HTYPE
242
    - optionally redefine their run requirements:
243
        REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
244

245
  Note that all commands require root permissions.
246

247
  @ivar dry_run_result: the value (if any) that will be returned to the caller
248
      in dry-run mode (signalled by opcode dry_run parameter)
249
  @cvar _OP_DEFS: a list of opcode attributes and the defaults values
250
      they should get if not already existing
251

252
  """
253
  HPATH = None
254
  HTYPE = None
255
  _OP_REQP = []
256
  _OP_DEFS = []
257
  REQ_BGL = True
258

    
259
  def __init__(self, processor, op, context, rpc):
260
    """Constructor for LogicalUnit.
261

262
    This needs to be overridden in derived classes in order to check op
263
    validity.
264

265
    """
266
    self.proc = processor
267
    self.op = op
268
    self.cfg = context.cfg
269
    self.context = context
270
    self.rpc = rpc
271
    # Dicts used to declare locking needs to mcpu
272
    self.needed_locks = None
273
    self.acquired_locks = {}
274
    self.share_locks = dict.fromkeys(locking.LEVELS, 0)
275
    self.add_locks = {}
276
    self.remove_locks = {}
277
    # Used to force good behavior when calling helper functions
278
    self.recalculate_locks = {}
279
    self.__ssh = None
280
    # logging
281
    self.LogWarning = processor.LogWarning # pylint: disable-msg=C0103
282
    self.LogInfo = processor.LogInfo # pylint: disable-msg=C0103
283
    self.LogStep = processor.LogStep # pylint: disable-msg=C0103
284
    # support for dry-run
285
    self.dry_run_result = None
286
    # support for generic debug attribute
287
    if (not hasattr(self.op, "debug_level") or
288
        not isinstance(self.op.debug_level, int)):
289
      self.op.debug_level = 0
290

    
291
    # Tasklets
292
    self.tasklets = None
293

    
294
    for aname, aval in self._OP_DEFS:
295
      if not hasattr(self.op, aname):
296
        if callable(aval):
297
          dval = aval()
298
        else:
299
          dval = aval
300
        setattr(self.op, aname, dval)
301

    
302
    for attr_name, test in self._OP_REQP:
303
      if not hasattr(op, attr_name):
304
        raise errors.OpPrereqError("Required parameter '%s' missing" %
305
                                   attr_name, errors.ECODE_INVAL)
306
      attr_val = getattr(op, attr_name, None)
307
      if not callable(test):
308
        raise errors.ProgrammerError("Validation for parameter '%s' failed,"
309
                                     " given type is not a proper type (%s)" %
310
                                     (attr_name, test))
311
      if not test(attr_val):
312
        logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
313
                      self.op.OP_ID, attr_name, type(attr_val), attr_val)
314
        raise errors.OpPrereqError("Parameter '%s' has invalid type" %
315
                                   attr_name, errors.ECODE_INVAL)
316

    
317
    self.CheckArguments()
318

    
319
  def __GetSSH(self):
320
    """Returns the SshRunner object
321

322
    """
323
    if not self.__ssh:
324
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
325
    return self.__ssh
326

    
327
  ssh = property(fget=__GetSSH)
328

    
329
  def CheckArguments(self):
330
    """Check syntactic validity for the opcode arguments.
331

332
    This method is for doing a simple syntactic check and ensure
333
    validity of opcode parameters, without any cluster-related
334
    checks. While the same can be accomplished in ExpandNames and/or
335
    CheckPrereq, doing these separate is better because:
336

337
      - ExpandNames is left as as purely a lock-related function
338
      - CheckPrereq is run after we have acquired locks (and possible
339
        waited for them)
340

341
    The function is allowed to change the self.op attribute so that
342
    later methods can no longer worry about missing parameters.
343

344
    """
345
    pass
346

    
347
  def ExpandNames(self):
348
    """Expand names for this LU.
349

350
    This method is called before starting to execute the opcode, and it should
351
    update all the parameters of the opcode to their canonical form (e.g. a
352
    short node name must be fully expanded after this method has successfully
353
    completed). This way locking, hooks, logging, ecc. can work correctly.
354

355
    LUs which implement this method must also populate the self.needed_locks
356
    member, as a dict with lock levels as keys, and a list of needed lock names
357
    as values. Rules:
358

359
      - use an empty dict if you don't need any lock
360
      - if you don't need any lock at a particular level omit that level
361
      - don't put anything for the BGL level
362
      - if you want all locks at a level use locking.ALL_SET as a value
363

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

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

372
    Examples::
373

374
      # Acquire all nodes and one instance
375
      self.needed_locks = {
376
        locking.LEVEL_NODE: locking.ALL_SET,
377
        locking.LEVEL_INSTANCE: ['instance1.example.tld'],
378
      }
379
      # Acquire just two nodes
380
      self.needed_locks = {
381
        locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
382
      }
383
      # Acquire no locks
384
      self.needed_locks = {} # No, you can't leave it to the default value None
385

386
    """
387
    # The implementation of this method is mandatory only if the new LU is
388
    # concurrent, so that old LUs don't need to be changed all at the same
389
    # time.
390
    if self.REQ_BGL:
391
      self.needed_locks = {} # Exclusive LUs don't need locks.
392
    else:
393
      raise NotImplementedError
394

    
395
  def DeclareLocks(self, level):
396
    """Declare LU locking needs for a level
397

398
    While most LUs can just declare their locking needs at ExpandNames time,
399
    sometimes there's the need to calculate some locks after having acquired
400
    the ones before. This function is called just before acquiring locks at a
401
    particular level, but after acquiring the ones at lower levels, and permits
402
    such calculations. It can be used to modify self.needed_locks, and by
403
    default it does nothing.
404

405
    This function is only called if you have something already set in
406
    self.needed_locks for the level.
407

408
    @param level: Locking level which is going to be locked
409
    @type level: member of ganeti.locking.LEVELS
410

411
    """
412

    
413
  def CheckPrereq(self):
414
    """Check prerequisites for this LU.
415

416
    This method should check that the prerequisites for the execution
417
    of this LU are fulfilled. It can do internode communication, but
418
    it should be idempotent - no cluster or system changes are
419
    allowed.
420

421
    The method should raise errors.OpPrereqError in case something is
422
    not fulfilled. Its return value is ignored.
423

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

427
    """
428
    if self.tasklets is not None:
429
      for (idx, tl) in enumerate(self.tasklets):
430
        logging.debug("Checking prerequisites for tasklet %s/%s",
431
                      idx + 1, len(self.tasklets))
432
        tl.CheckPrereq()
433
    else:
434
      pass
435

    
436
  def Exec(self, feedback_fn):
437
    """Execute the LU.
438

439
    This method should implement the actual work. It should raise
440
    errors.OpExecError for failures that are somewhat dealt with in
441
    code, or expected.
442

443
    """
444
    if self.tasklets is not None:
445
      for (idx, tl) in enumerate(self.tasklets):
446
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
447
        tl.Exec(feedback_fn)
448
    else:
449
      raise NotImplementedError
450

    
451
  def BuildHooksEnv(self):
452
    """Build hooks environment for this LU.
453

454
    This method should return a three-node tuple consisting of: a dict
455
    containing the environment that will be used for running the
456
    specific hook for this LU, a list of node names on which the hook
457
    should run before the execution, and a list of node names on which
458
    the hook should run after the execution.
459

460
    The keys of the dict must not have 'GANETI_' prefixed as this will
461
    be handled in the hooks runner. Also note additional keys will be
462
    added by the hooks runner. If the LU doesn't define any
463
    environment, an empty dict (and not None) should be returned.
464

465
    No nodes should be returned as an empty list (and not None).
466

467
    Note that if the HPATH for a LU class is None, this function will
468
    not be called.
469

470
    """
471
    raise NotImplementedError
472

    
473
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
474
    """Notify the LU about the results of its hooks.
475

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

482
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
483
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
484
    @param hook_results: the results of the multi-node hooks rpc call
485
    @param feedback_fn: function used send feedback back to the caller
486
    @param lu_result: the previous Exec result this LU had, or None
487
        in the PRE phase
488
    @return: the new Exec result, based on the previous result
489
        and hook results
490

491
    """
492
    # API must be kept, thus we ignore the unused argument and could
493
    # be a function warnings
494
    # pylint: disable-msg=W0613,R0201
495
    return lu_result
496

    
497
  def _ExpandAndLockInstance(self):
498
    """Helper function to expand and lock an instance.
499

500
    Many LUs that work on an instance take its name in self.op.instance_name
501
    and need to expand it and then declare the expanded name for locking. This
502
    function does it, and then updates self.op.instance_name to the expanded
503
    name. It also initializes needed_locks as a dict, if this hasn't been done
504
    before.
505

506
    """
507
    if self.needed_locks is None:
508
      self.needed_locks = {}
509
    else:
510
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
511
        "_ExpandAndLockInstance called with instance-level locks set"
512
    self.op.instance_name = _ExpandInstanceName(self.cfg,
513
                                                self.op.instance_name)
514
    self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
515

    
516
  def _LockInstancesNodes(self, primary_only=False):
517
    """Helper function to declare instances' nodes for locking.
518

519
    This function should be called after locking one or more instances to lock
520
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
521
    with all primary or secondary nodes for instances already locked and
522
    present in self.needed_locks[locking.LEVEL_INSTANCE].
523

524
    It should be called from DeclareLocks, and for safety only works if
525
    self.recalculate_locks[locking.LEVEL_NODE] is set.
526

527
    In the future it may grow parameters to just lock some instance's nodes, or
528
    to just lock primaries or secondary nodes, if needed.
529

530
    If should be called in DeclareLocks in a way similar to::
531

532
      if level == locking.LEVEL_NODE:
533
        self._LockInstancesNodes()
534

535
    @type primary_only: boolean
536
    @param primary_only: only lock primary nodes of locked instances
537

538
    """
539
    assert locking.LEVEL_NODE in self.recalculate_locks, \
540
      "_LockInstancesNodes helper function called with no nodes to recalculate"
541

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

    
544
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
545
    # future we might want to have different behaviors depending on the value
546
    # of self.recalculate_locks[locking.LEVEL_NODE]
547
    wanted_nodes = []
548
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
549
      instance = self.context.cfg.GetInstanceInfo(instance_name)
550
      wanted_nodes.append(instance.primary_node)
551
      if not primary_only:
552
        wanted_nodes.extend(instance.secondary_nodes)
553

    
554
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
555
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
556
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
557
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
558

    
559
    del self.recalculate_locks[locking.LEVEL_NODE]
560

    
561

    
562
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
563
  """Simple LU which runs no hooks.
564

565
  This LU is intended as a parent for other LogicalUnits which will
566
  run no hooks, in order to reduce duplicate code.
567

568
  """
569
  HPATH = None
570
  HTYPE = None
571

    
572
  def BuildHooksEnv(self):
573
    """Empty BuildHooksEnv for NoHooksLu.
574

575
    This just raises an error.
576

577
    """
578
    assert False, "BuildHooksEnv called for NoHooksLUs"
579

    
580

    
581
class Tasklet:
582
  """Tasklet base class.
583

584
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
585
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
586
  tasklets know nothing about locks.
587

588
  Subclasses must follow these rules:
589
    - Implement CheckPrereq
590
    - Implement Exec
591

592
  """
593
  def __init__(self, lu):
594
    self.lu = lu
595

    
596
    # Shortcuts
597
    self.cfg = lu.cfg
598
    self.rpc = lu.rpc
599

    
600
  def CheckPrereq(self):
601
    """Check prerequisites for this tasklets.
602

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

607
    The method should raise errors.OpPrereqError in case something is not
608
    fulfilled. Its return value is ignored.
609

610
    This method should also update all parameters to their canonical form if it
611
    hasn't been done before.
612

613
    """
614
    pass
615

    
616
  def Exec(self, feedback_fn):
617
    """Execute the tasklet.
618

619
    This method should implement the actual work. It should raise
620
    errors.OpExecError for failures that are somewhat dealt with in code, or
621
    expected.
622

623
    """
624
    raise NotImplementedError
625

    
626

    
627
def _GetWantedNodes(lu, nodes):
628
  """Returns list of checked and expanded node names.
629

630
  @type lu: L{LogicalUnit}
631
  @param lu: the logical unit on whose behalf we execute
632
  @type nodes: list
633
  @param nodes: list of node names or None for all nodes
634
  @rtype: list
635
  @return: the list of nodes, sorted
636
  @raise errors.ProgrammerError: if the nodes parameter is wrong type
637

638
  """
639
  if not nodes:
640
    raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
641
      " non-empty list of nodes whose name is to be expanded.")
642

    
643
  wanted = [_ExpandNodeName(lu.cfg, name) for name in nodes]
644
  return utils.NiceSort(wanted)
645

    
646

    
647
def _GetWantedInstances(lu, instances):
648
  """Returns list of checked and expanded instance names.
649

650
  @type lu: L{LogicalUnit}
651
  @param lu: the logical unit on whose behalf we execute
652
  @type instances: list
653
  @param instances: list of instance names or None for all instances
654
  @rtype: list
655
  @return: the list of instances, sorted
656
  @raise errors.OpPrereqError: if the instances parameter is wrong type
657
  @raise errors.OpPrereqError: if any of the passed instances is not found
658

659
  """
660
  if instances:
661
    wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
662
  else:
663
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
664
  return wanted
665

    
666

    
667
def _GetUpdatedParams(old_params, update_dict,
668
                      use_default=True, use_none=False):
669
  """Return the new version of a parameter dictionary.
670

671
  @type old_params: dict
672
  @param old_params: old parameters
673
  @type update_dict: dict
674
  @param update_dict: dict containing new parameter values, or
675
      constants.VALUE_DEFAULT to reset the parameter to its default
676
      value
677
  @param use_default: boolean
678
  @type use_default: whether to recognise L{constants.VALUE_DEFAULT}
679
      values as 'to be deleted' values
680
  @param use_none: boolean
681
  @type use_none: whether to recognise C{None} values as 'to be
682
      deleted' values
683
  @rtype: dict
684
  @return: the new parameter dictionary
685

686
  """
687
  params_copy = copy.deepcopy(old_params)
688
  for key, val in update_dict.iteritems():
689
    if ((use_default and val == constants.VALUE_DEFAULT) or
690
        (use_none and val is None)):
691
      try:
692
        del params_copy[key]
693
      except KeyError:
694
        pass
695
    else:
696
      params_copy[key] = val
697
  return params_copy
698

    
699

    
700
def _CheckOutputFields(static, dynamic, selected):
701
  """Checks whether all selected fields are valid.
702

703
  @type static: L{utils.FieldSet}
704
  @param static: static fields set
705
  @type dynamic: L{utils.FieldSet}
706
  @param dynamic: dynamic fields set
707

708
  """
709
  f = utils.FieldSet()
710
  f.Extend(static)
711
  f.Extend(dynamic)
712

    
713
  delta = f.NonMatching(selected)
714
  if delta:
715
    raise errors.OpPrereqError("Unknown output fields selected: %s"
716
                               % ",".join(delta), errors.ECODE_INVAL)
717

    
718

    
719
def _CheckBooleanOpField(op, name):
720
  """Validates boolean opcode parameters.
721

722
  This will ensure that an opcode parameter is either a boolean value,
723
  or None (but that it always exists).
724

725
  """
726
  val = getattr(op, name, None)
727
  if not (val is None or isinstance(val, bool)):
728
    raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" %
729
                               (name, str(val)), errors.ECODE_INVAL)
730
  setattr(op, name, val)
731

    
732

    
733
def _CheckGlobalHvParams(params):
734
  """Validates that given hypervisor params are not global ones.
735

736
  This will ensure that instances don't get customised versions of
737
  global params.
738

739
  """
740
  used_globals = constants.HVC_GLOBALS.intersection(params)
741
  if used_globals:
742
    msg = ("The following hypervisor parameters are global and cannot"
743
           " be customized at instance level, please modify them at"
744
           " cluster level: %s" % utils.CommaJoin(used_globals))
745
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
746

    
747

    
748
def _CheckNodeOnline(lu, node):
749
  """Ensure that a given node is online.
750

751
  @param lu: the LU on behalf of which we make the check
752
  @param node: the node to check
753
  @raise errors.OpPrereqError: if the node is offline
754

755
  """
756
  if lu.cfg.GetNodeInfo(node).offline:
757
    raise errors.OpPrereqError("Can't use offline node %s" % node,
758
                               errors.ECODE_INVAL)
759

    
760

    
761
def _CheckNodeNotDrained(lu, node):
762
  """Ensure that a given node is not drained.
763

764
  @param lu: the LU on behalf of which we make the check
765
  @param node: the node to check
766
  @raise errors.OpPrereqError: if the node is drained
767

768
  """
769
  if lu.cfg.GetNodeInfo(node).drained:
770
    raise errors.OpPrereqError("Can't use drained node %s" % node,
771
                               errors.ECODE_INVAL)
772

    
773

    
774
def _CheckNodeHasOS(lu, node, os_name, force_variant):
775
  """Ensure that a node supports a given OS.
776

777
  @param lu: the LU on behalf of which we make the check
778
  @param node: the node to check
779
  @param os_name: the OS to query about
780
  @param force_variant: whether to ignore variant errors
781
  @raise errors.OpPrereqError: if the node is not supporting the OS
782

783
  """
784
  result = lu.rpc.call_os_get(node, os_name)
785
  result.Raise("OS '%s' not in supported OS list for node %s" %
786
               (os_name, node),
787
               prereq=True, ecode=errors.ECODE_INVAL)
788
  if not force_variant:
789
    _CheckOSVariant(result.payload, os_name)
790

    
791

    
792
def _RequireFileStorage():
793
  """Checks that file storage is enabled.
794

795
  @raise errors.OpPrereqError: when file storage is disabled
796

797
  """
798
  if not constants.ENABLE_FILE_STORAGE:
799
    raise errors.OpPrereqError("File storage disabled at configure time",
800
                               errors.ECODE_INVAL)
801

    
802

    
803
def _CheckDiskTemplate(template):
804
  """Ensure a given disk template is valid.
805

806
  """
807
  if template not in constants.DISK_TEMPLATES:
808
    msg = ("Invalid disk template name '%s', valid templates are: %s" %
809
           (template, utils.CommaJoin(constants.DISK_TEMPLATES)))
810
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
811
  if template == constants.DT_FILE:
812
    _RequireFileStorage()
813

    
814

    
815
def _CheckStorageType(storage_type):
816
  """Ensure a given storage type is valid.
817

818
  """
819
  if storage_type not in constants.VALID_STORAGE_TYPES:
820
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
821
                               errors.ECODE_INVAL)
822
  if storage_type == constants.ST_FILE:
823
    _RequireFileStorage()
824
  return True
825

    
826

    
827
def _GetClusterDomainSecret():
828
  """Reads the cluster domain secret.
829

830
  """
831
  return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
832
                               strict=True)
833

    
834

    
835
def _CheckInstanceDown(lu, instance, reason):
836
  """Ensure that an instance is not running."""
837
  if instance.admin_up:
838
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
839
                               (instance.name, reason), errors.ECODE_STATE)
840

    
841
  pnode = instance.primary_node
842
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
843
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
844
              prereq=True, ecode=errors.ECODE_ENVIRON)
845

    
846
  if instance.name in ins_l.payload:
847
    raise errors.OpPrereqError("Instance %s is running, %s" %
848
                               (instance.name, reason), errors.ECODE_STATE)
849

    
850

    
851
def _ExpandItemName(fn, name, kind):
852
  """Expand an item name.
853

854
  @param fn: the function to use for expansion
855
  @param name: requested item name
856
  @param kind: text description ('Node' or 'Instance')
857
  @return: the resolved (full) name
858
  @raise errors.OpPrereqError: if the item is not found
859

860
  """
861
  full_name = fn(name)
862
  if full_name is None:
863
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
864
                               errors.ECODE_NOENT)
865
  return full_name
866

    
867

    
868
def _ExpandNodeName(cfg, name):
869
  """Wrapper over L{_ExpandItemName} for nodes."""
870
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
871

    
872

    
873
def _ExpandInstanceName(cfg, name):
874
  """Wrapper over L{_ExpandItemName} for instance."""
875
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
876

    
877

    
878
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
879
                          memory, vcpus, nics, disk_template, disks,
880
                          bep, hvp, hypervisor_name):
881
  """Builds instance related env variables for hooks
882

883
  This builds the hook environment from individual variables.
884

885
  @type name: string
886
  @param name: the name of the instance
887
  @type primary_node: string
888
  @param primary_node: the name of the instance's primary node
889
  @type secondary_nodes: list
890
  @param secondary_nodes: list of secondary nodes as strings
891
  @type os_type: string
892
  @param os_type: the name of the instance's OS
893
  @type status: boolean
894
  @param status: the should_run status of the instance
895
  @type memory: string
896
  @param memory: the memory size of the instance
897
  @type vcpus: string
898
  @param vcpus: the count of VCPUs the instance has
899
  @type nics: list
900
  @param nics: list of tuples (ip, mac, mode, link) representing
901
      the NICs the instance has
902
  @type disk_template: string
903
  @param disk_template: the disk template of the instance
904
  @type disks: list
905
  @param disks: the list of (size, mode) pairs
906
  @type bep: dict
907
  @param bep: the backend parameters for the instance
908
  @type hvp: dict
909
  @param hvp: the hypervisor parameters for the instance
910
  @type hypervisor_name: string
911
  @param hypervisor_name: the hypervisor for the instance
912
  @rtype: dict
913
  @return: the hook environment for this instance
914

915
  """
916
  if status:
917
    str_status = "up"
918
  else:
919
    str_status = "down"
920
  env = {
921
    "OP_TARGET": name,
922
    "INSTANCE_NAME": name,
923
    "INSTANCE_PRIMARY": primary_node,
924
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
925
    "INSTANCE_OS_TYPE": os_type,
926
    "INSTANCE_STATUS": str_status,
927
    "INSTANCE_MEMORY": memory,
928
    "INSTANCE_VCPUS": vcpus,
929
    "INSTANCE_DISK_TEMPLATE": disk_template,
930
    "INSTANCE_HYPERVISOR": hypervisor_name,
931
  }
932

    
933
  if nics:
934
    nic_count = len(nics)
935
    for idx, (ip, mac, mode, link) in enumerate(nics):
936
      if ip is None:
937
        ip = ""
938
      env["INSTANCE_NIC%d_IP" % idx] = ip
939
      env["INSTANCE_NIC%d_MAC" % idx] = mac
940
      env["INSTANCE_NIC%d_MODE" % idx] = mode
941
      env["INSTANCE_NIC%d_LINK" % idx] = link
942
      if mode == constants.NIC_MODE_BRIDGED:
943
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
944
  else:
945
    nic_count = 0
946

    
947
  env["INSTANCE_NIC_COUNT"] = nic_count
948

    
949
  if disks:
950
    disk_count = len(disks)
951
    for idx, (size, mode) in enumerate(disks):
952
      env["INSTANCE_DISK%d_SIZE" % idx] = size
953
      env["INSTANCE_DISK%d_MODE" % idx] = mode
954
  else:
955
    disk_count = 0
956

    
957
  env["INSTANCE_DISK_COUNT"] = disk_count
958

    
959
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
960
    for key, value in source.items():
961
      env["INSTANCE_%s_%s" % (kind, key)] = value
962

    
963
  return env
964

    
965

    
966
def _NICListToTuple(lu, nics):
967
  """Build a list of nic information tuples.
968

969
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
970
  value in LUQueryInstanceData.
971

972
  @type lu:  L{LogicalUnit}
973
  @param lu: the logical unit on whose behalf we execute
974
  @type nics: list of L{objects.NIC}
975
  @param nics: list of nics to convert to hooks tuples
976

977
  """
978
  hooks_nics = []
979
  cluster = lu.cfg.GetClusterInfo()
980
  for nic in nics:
981
    ip = nic.ip
982
    mac = nic.mac
983
    filled_params = cluster.SimpleFillNIC(nic.nicparams)
984
    mode = filled_params[constants.NIC_MODE]
985
    link = filled_params[constants.NIC_LINK]
986
    hooks_nics.append((ip, mac, mode, link))
987
  return hooks_nics
988

    
989

    
990
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
991
  """Builds instance related env variables for hooks from an object.
992

993
  @type lu: L{LogicalUnit}
994
  @param lu: the logical unit on whose behalf we execute
995
  @type instance: L{objects.Instance}
996
  @param instance: the instance for which we should build the
997
      environment
998
  @type override: dict
999
  @param override: dictionary with key/values that will override
1000
      our values
1001
  @rtype: dict
1002
  @return: the hook environment dictionary
1003

1004
  """
1005
  cluster = lu.cfg.GetClusterInfo()
1006
  bep = cluster.FillBE(instance)
1007
  hvp = cluster.FillHV(instance)
1008
  args = {
1009
    'name': instance.name,
1010
    'primary_node': instance.primary_node,
1011
    'secondary_nodes': instance.secondary_nodes,
1012
    'os_type': instance.os,
1013
    'status': instance.admin_up,
1014
    'memory': bep[constants.BE_MEMORY],
1015
    'vcpus': bep[constants.BE_VCPUS],
1016
    'nics': _NICListToTuple(lu, instance.nics),
1017
    'disk_template': instance.disk_template,
1018
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
1019
    'bep': bep,
1020
    'hvp': hvp,
1021
    'hypervisor_name': instance.hypervisor,
1022
  }
1023
  if override:
1024
    args.update(override)
1025
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
1026

    
1027

    
1028
def _AdjustCandidatePool(lu, exceptions):
1029
  """Adjust the candidate pool after node operations.
1030

1031
  """
1032
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
1033
  if mod_list:
1034
    lu.LogInfo("Promoted nodes to master candidate role: %s",
1035
               utils.CommaJoin(node.name for node in mod_list))
1036
    for name in mod_list:
1037
      lu.context.ReaddNode(name)
1038
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
1039
  if mc_now > mc_max:
1040
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
1041
               (mc_now, mc_max))
1042

    
1043

    
1044
def _DecideSelfPromotion(lu, exceptions=None):
1045
  """Decide whether I should promote myself as a master candidate.
1046

1047
  """
1048
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
1049
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
1050
  # the new node will increase mc_max with one, so:
1051
  mc_should = min(mc_should + 1, cp_size)
1052
  return mc_now < mc_should
1053

    
1054

    
1055
def _CheckNicsBridgesExist(lu, target_nics, target_node):
1056
  """Check that the brigdes needed by a list of nics exist.
1057

1058
  """
1059
  cluster = lu.cfg.GetClusterInfo()
1060
  paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics]
1061
  brlist = [params[constants.NIC_LINK] for params in paramslist
1062
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
1063
  if brlist:
1064
    result = lu.rpc.call_bridges_exist(target_node, brlist)
1065
    result.Raise("Error checking bridges on destination node '%s'" %
1066
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
1067

    
1068

    
1069
def _CheckInstanceBridgesExist(lu, instance, node=None):
1070
  """Check that the brigdes needed by an instance exist.
1071

1072
  """
1073
  if node is None:
1074
    node = instance.primary_node
1075
  _CheckNicsBridgesExist(lu, instance.nics, node)
1076

    
1077

    
1078
def _CheckOSVariant(os_obj, name):
1079
  """Check whether an OS name conforms to the os variants specification.
1080

1081
  @type os_obj: L{objects.OS}
1082
  @param os_obj: OS object to check
1083
  @type name: string
1084
  @param name: OS name passed by the user, to check for validity
1085

1086
  """
1087
  if not os_obj.supported_variants:
1088
    return
1089
  try:
1090
    variant = name.split("+", 1)[1]
1091
  except IndexError:
1092
    raise errors.OpPrereqError("OS name must include a variant",
1093
                               errors.ECODE_INVAL)
1094

    
1095
  if variant not in os_obj.supported_variants:
1096
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
1097

    
1098

    
1099
def _GetNodeInstancesInner(cfg, fn):
1100
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
1101

    
1102

    
1103
def _GetNodeInstances(cfg, node_name):
1104
  """Returns a list of all primary and secondary instances on a node.
1105

1106
  """
1107

    
1108
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
1109

    
1110

    
1111
def _GetNodePrimaryInstances(cfg, node_name):
1112
  """Returns primary instances on a node.
1113

1114
  """
1115
  return _GetNodeInstancesInner(cfg,
1116
                                lambda inst: node_name == inst.primary_node)
1117

    
1118

    
1119
def _GetNodeSecondaryInstances(cfg, node_name):
1120
  """Returns secondary instances on a node.
1121

1122
  """
1123
  return _GetNodeInstancesInner(cfg,
1124
                                lambda inst: node_name in inst.secondary_nodes)
1125

    
1126

    
1127
def _GetStorageTypeArgs(cfg, storage_type):
1128
  """Returns the arguments for a storage type.
1129

1130
  """
1131
  # Special case for file storage
1132
  if storage_type == constants.ST_FILE:
1133
    # storage.FileStorage wants a list of storage directories
1134
    return [[cfg.GetFileStorageDir()]]
1135

    
1136
  return []
1137

    
1138

    
1139
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
1140
  faulty = []
1141

    
1142
  for dev in instance.disks:
1143
    cfg.SetDiskID(dev, node_name)
1144

    
1145
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
1146
  result.Raise("Failed to get disk status from node %s" % node_name,
1147
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
1148

    
1149
  for idx, bdev_status in enumerate(result.payload):
1150
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
1151
      faulty.append(idx)
1152

    
1153
  return faulty
1154

    
1155

    
1156
class LUPostInitCluster(LogicalUnit):
1157
  """Logical unit for running hooks after cluster initialization.
1158

1159
  """
1160
  HPATH = "cluster-init"
1161
  HTYPE = constants.HTYPE_CLUSTER
1162
  _OP_REQP = []
1163

    
1164
  def BuildHooksEnv(self):
1165
    """Build hooks env.
1166

1167
    """
1168
    env = {"OP_TARGET": self.cfg.GetClusterName()}
1169
    mn = self.cfg.GetMasterNode()
1170
    return env, [], [mn]
1171

    
1172
  def Exec(self, feedback_fn):
1173
    """Nothing to do.
1174

1175
    """
1176
    return True
1177

    
1178

    
1179
class LUDestroyCluster(LogicalUnit):
1180
  """Logical unit for destroying the cluster.
1181

1182
  """
1183
  HPATH = "cluster-destroy"
1184
  HTYPE = constants.HTYPE_CLUSTER
1185
  _OP_REQP = []
1186

    
1187
  def BuildHooksEnv(self):
1188
    """Build hooks env.
1189

1190
    """
1191
    env = {"OP_TARGET": self.cfg.GetClusterName()}
1192
    return env, [], []
1193

    
1194
  def CheckPrereq(self):
1195
    """Check prerequisites.
1196

1197
    This checks whether the cluster is empty.
1198

1199
    Any errors are signaled by raising errors.OpPrereqError.
1200

1201
    """
1202
    master = self.cfg.GetMasterNode()
1203

    
1204
    nodelist = self.cfg.GetNodeList()
1205
    if len(nodelist) != 1 or nodelist[0] != master:
1206
      raise errors.OpPrereqError("There are still %d node(s) in"
1207
                                 " this cluster." % (len(nodelist) - 1),
1208
                                 errors.ECODE_INVAL)
1209
    instancelist = self.cfg.GetInstanceList()
1210
    if instancelist:
1211
      raise errors.OpPrereqError("There are still %d instance(s) in"
1212
                                 " this cluster." % len(instancelist),
1213
                                 errors.ECODE_INVAL)
1214

    
1215
  def Exec(self, feedback_fn):
1216
    """Destroys the cluster.
1217

1218
    """
1219
    master = self.cfg.GetMasterNode()
1220
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
1221

    
1222
    # Run post hooks on master node before it's removed
1223
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
1224
    try:
1225
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
1226
    except:
1227
      # pylint: disable-msg=W0702
1228
      self.LogWarning("Errors occurred running hooks on %s" % master)
1229

    
1230
    result = self.rpc.call_node_stop_master(master, False)
1231
    result.Raise("Could not disable the master role")
1232

    
1233
    if modify_ssh_setup:
1234
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
1235
      utils.CreateBackup(priv_key)
1236
      utils.CreateBackup(pub_key)
1237

    
1238
    return master
1239

    
1240

    
1241
def _VerifyCertificate(filename):
1242
  """Verifies a certificate for LUVerifyCluster.
1243

1244
  @type filename: string
1245
  @param filename: Path to PEM file
1246

1247
  """
1248
  try:
1249
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1250
                                           utils.ReadFile(filename))
1251
  except Exception, err: # pylint: disable-msg=W0703
1252
    return (LUVerifyCluster.ETYPE_ERROR,
1253
            "Failed to load X509 certificate %s: %s" % (filename, err))
1254

    
1255
  (errcode, msg) = \
1256
    utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1257
                                constants.SSL_CERT_EXPIRATION_ERROR)
1258

    
1259
  if msg:
1260
    fnamemsg = "While verifying %s: %s" % (filename, msg)
1261
  else:
1262
    fnamemsg = None
1263

    
1264
  if errcode is None:
1265
    return (None, fnamemsg)
1266
  elif errcode == utils.CERT_WARNING:
1267
    return (LUVerifyCluster.ETYPE_WARNING, fnamemsg)
1268
  elif errcode == utils.CERT_ERROR:
1269
    return (LUVerifyCluster.ETYPE_ERROR, fnamemsg)
1270

    
1271
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1272

    
1273

    
1274
class LUVerifyCluster(LogicalUnit):
1275
  """Verifies the cluster status.
1276

1277
  """
1278
  HPATH = "cluster-verify"
1279
  HTYPE = constants.HTYPE_CLUSTER
1280
  _OP_REQP = [
1281
    ("skip_checks", _TListOf(_TElemOf(constants.VERIFY_OPTIONAL_CHECKS))),
1282
    ("verbose", _TBool),
1283
    ("error_codes", _TBool),
1284
    ("debug_simulate_errors", _TBool),
1285
    ]
1286
  REQ_BGL = False
1287

    
1288
  TCLUSTER = "cluster"
1289
  TNODE = "node"
1290
  TINSTANCE = "instance"
1291

    
1292
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1293
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1294
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1295
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1296
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1297
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1298
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1299
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1300
  ENODEDRBD = (TNODE, "ENODEDRBD")
1301
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1302
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1303
  ENODEHV = (TNODE, "ENODEHV")
1304
  ENODELVM = (TNODE, "ENODELVM")
1305
  ENODEN1 = (TNODE, "ENODEN1")
1306
  ENODENET = (TNODE, "ENODENET")
1307
  ENODEOS = (TNODE, "ENODEOS")
1308
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1309
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1310
  ENODERPC = (TNODE, "ENODERPC")
1311
  ENODESSH = (TNODE, "ENODESSH")
1312
  ENODEVERSION = (TNODE, "ENODEVERSION")
1313
  ENODESETUP = (TNODE, "ENODESETUP")
1314
  ENODETIME = (TNODE, "ENODETIME")
1315

    
1316
  ETYPE_FIELD = "code"
1317
  ETYPE_ERROR = "ERROR"
1318
  ETYPE_WARNING = "WARNING"
1319

    
1320
  class NodeImage(object):
1321
    """A class representing the logical and physical status of a node.
1322

1323
    @type name: string
1324
    @ivar name: the node name to which this object refers
1325
    @ivar volumes: a structure as returned from
1326
        L{ganeti.backend.GetVolumeList} (runtime)
1327
    @ivar instances: a list of running instances (runtime)
1328
    @ivar pinst: list of configured primary instances (config)
1329
    @ivar sinst: list of configured secondary instances (config)
1330
    @ivar sbp: diction of {secondary-node: list of instances} of all peers
1331
        of this node (config)
1332
    @ivar mfree: free memory, as reported by hypervisor (runtime)
1333
    @ivar dfree: free disk, as reported by the node (runtime)
1334
    @ivar offline: the offline status (config)
1335
    @type rpc_fail: boolean
1336
    @ivar rpc_fail: whether the RPC verify call was successfull (overall,
1337
        not whether the individual keys were correct) (runtime)
1338
    @type lvm_fail: boolean
1339
    @ivar lvm_fail: whether the RPC call didn't return valid LVM data
1340
    @type hyp_fail: boolean
1341
    @ivar hyp_fail: whether the RPC call didn't return the instance list
1342
    @type ghost: boolean
1343
    @ivar ghost: whether this is a known node or not (config)
1344
    @type os_fail: boolean
1345
    @ivar os_fail: whether the RPC call didn't return valid OS data
1346
    @type oslist: list
1347
    @ivar oslist: list of OSes as diagnosed by DiagnoseOS
1348

1349
    """
1350
    def __init__(self, offline=False, name=None):
1351
      self.name = name
1352
      self.volumes = {}
1353
      self.instances = []
1354
      self.pinst = []
1355
      self.sinst = []
1356
      self.sbp = {}
1357
      self.mfree = 0
1358
      self.dfree = 0
1359
      self.offline = offline
1360
      self.rpc_fail = False
1361
      self.lvm_fail = False
1362
      self.hyp_fail = False
1363
      self.ghost = False
1364
      self.os_fail = False
1365
      self.oslist = {}
1366

    
1367
  def ExpandNames(self):
1368
    self.needed_locks = {
1369
      locking.LEVEL_NODE: locking.ALL_SET,
1370
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1371
    }
1372
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1373

    
1374
  def _Error(self, ecode, item, msg, *args, **kwargs):
1375
    """Format an error message.
1376

1377
    Based on the opcode's error_codes parameter, either format a
1378
    parseable error code, or a simpler error string.
1379

1380
    This must be called only from Exec and functions called from Exec.
1381

1382
    """
1383
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1384
    itype, etxt = ecode
1385
    # first complete the msg
1386
    if args:
1387
      msg = msg % args
1388
    # then format the whole message
1389
    if self.op.error_codes:
1390
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1391
    else:
1392
      if item:
1393
        item = " " + item
1394
      else:
1395
        item = ""
1396
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1397
    # and finally report it via the feedback_fn
1398
    self._feedback_fn("  - %s" % msg)
1399

    
1400
  def _ErrorIf(self, cond, *args, **kwargs):
1401
    """Log an error message if the passed condition is True.
1402

1403
    """
1404
    cond = bool(cond) or self.op.debug_simulate_errors
1405
    if cond:
1406
      self._Error(*args, **kwargs)
1407
    # do not mark the operation as failed for WARN cases only
1408
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1409
      self.bad = self.bad or cond
1410

    
1411
  def _VerifyNode(self, ninfo, nresult):
1412
    """Run multiple tests against a node.
1413

1414
    Test list:
1415

1416
      - compares ganeti version
1417
      - checks vg existence and size > 20G
1418
      - checks config file checksum
1419
      - checks ssh to other nodes
1420

1421
    @type ninfo: L{objects.Node}
1422
    @param ninfo: the node to check
1423
    @param nresult: the results from the node
1424
    @rtype: boolean
1425
    @return: whether overall this call was successful (and we can expect
1426
         reasonable values in the respose)
1427

1428
    """
1429
    node = ninfo.name
1430
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1431

    
1432
    # main result, nresult should be a non-empty dict
1433
    test = not nresult or not isinstance(nresult, dict)
1434
    _ErrorIf(test, self.ENODERPC, node,
1435
                  "unable to verify node: no data returned")
1436
    if test:
1437
      return False
1438

    
1439
    # compares ganeti version
1440
    local_version = constants.PROTOCOL_VERSION
1441
    remote_version = nresult.get("version", None)
1442
    test = not (remote_version and
1443
                isinstance(remote_version, (list, tuple)) and
1444
                len(remote_version) == 2)
1445
    _ErrorIf(test, self.ENODERPC, node,
1446
             "connection to node returned invalid data")
1447
    if test:
1448
      return False
1449

    
1450
    test = local_version != remote_version[0]
1451
    _ErrorIf(test, self.ENODEVERSION, node,
1452
             "incompatible protocol versions: master %s,"
1453
             " node %s", local_version, remote_version[0])
1454
    if test:
1455
      return False
1456

    
1457
    # node seems compatible, we can actually try to look into its results
1458

    
1459
    # full package version
1460
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1461
                  self.ENODEVERSION, node,
1462
                  "software version mismatch: master %s, node %s",
1463
                  constants.RELEASE_VERSION, remote_version[1],
1464
                  code=self.ETYPE_WARNING)
1465

    
1466
    hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1467
    if isinstance(hyp_result, dict):
1468
      for hv_name, hv_result in hyp_result.iteritems():
1469
        test = hv_result is not None
1470
        _ErrorIf(test, self.ENODEHV, node,
1471
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1472

    
1473

    
1474
    test = nresult.get(constants.NV_NODESETUP,
1475
                           ["Missing NODESETUP results"])
1476
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1477
             "; ".join(test))
1478

    
1479
    return True
1480

    
1481
  def _VerifyNodeTime(self, ninfo, nresult,
1482
                      nvinfo_starttime, nvinfo_endtime):
1483
    """Check the node time.
1484

1485
    @type ninfo: L{objects.Node}
1486
    @param ninfo: the node to check
1487
    @param nresult: the remote results for the node
1488
    @param nvinfo_starttime: the start time of the RPC call
1489
    @param nvinfo_endtime: the end time of the RPC call
1490

1491
    """
1492
    node = ninfo.name
1493
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1494

    
1495
    ntime = nresult.get(constants.NV_TIME, None)
1496
    try:
1497
      ntime_merged = utils.MergeTime(ntime)
1498
    except (ValueError, TypeError):
1499
      _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1500
      return
1501

    
1502
    if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1503
      ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1504
    elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1505
      ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1506
    else:
1507
      ntime_diff = None
1508

    
1509
    _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1510
             "Node time diverges by at least %s from master node time",
1511
             ntime_diff)
1512

    
1513
  def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1514
    """Check the node time.
1515

1516
    @type ninfo: L{objects.Node}
1517
    @param ninfo: the node to check
1518
    @param nresult: the remote results for the node
1519
    @param vg_name: the configured VG name
1520

1521
    """
1522
    if vg_name is None:
1523
      return
1524

    
1525
    node = ninfo.name
1526
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1527

    
1528
    # checks vg existence and size > 20G
1529
    vglist = nresult.get(constants.NV_VGLIST, None)
1530
    test = not vglist
1531
    _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1532
    if not test:
1533
      vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1534
                                            constants.MIN_VG_SIZE)
1535
      _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1536

    
1537
    # check pv names
1538
    pvlist = nresult.get(constants.NV_PVLIST, None)
1539
    test = pvlist is None
1540
    _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1541
    if not test:
1542
      # check that ':' is not present in PV names, since it's a
1543
      # special character for lvcreate (denotes the range of PEs to
1544
      # use on the PV)
1545
      for _, pvname, owner_vg in pvlist:
1546
        test = ":" in pvname
1547
        _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1548
                 " '%s' of VG '%s'", pvname, owner_vg)
1549

    
1550
  def _VerifyNodeNetwork(self, ninfo, nresult):
1551
    """Check the node time.
1552

1553
    @type ninfo: L{objects.Node}
1554
    @param ninfo: the node to check
1555
    @param nresult: the remote results for the node
1556

1557
    """
1558
    node = ninfo.name
1559
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1560

    
1561
    test = constants.NV_NODELIST not in nresult
1562
    _ErrorIf(test, self.ENODESSH, node,
1563
             "node hasn't returned node ssh connectivity data")
1564
    if not test:
1565
      if nresult[constants.NV_NODELIST]:
1566
        for a_node, a_msg in nresult[constants.NV_NODELIST].items():
1567
          _ErrorIf(True, self.ENODESSH, node,
1568
                   "ssh communication with node '%s': %s", a_node, a_msg)
1569

    
1570
    test = constants.NV_NODENETTEST not in nresult
1571
    _ErrorIf(test, self.ENODENET, node,
1572
             "node hasn't returned node tcp connectivity data")
1573
    if not test:
1574
      if nresult[constants.NV_NODENETTEST]:
1575
        nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys())
1576
        for anode in nlist:
1577
          _ErrorIf(True, self.ENODENET, node,
1578
                   "tcp communication with node '%s': %s",
1579
                   anode, nresult[constants.NV_NODENETTEST][anode])
1580

    
1581
    test = constants.NV_MASTERIP not in nresult
1582
    _ErrorIf(test, self.ENODENET, node,
1583
             "node hasn't returned node master IP reachability data")
1584
    if not test:
1585
      if not nresult[constants.NV_MASTERIP]:
1586
        if node == self.master_node:
1587
          msg = "the master node cannot reach the master IP (not configured?)"
1588
        else:
1589
          msg = "cannot reach the master IP"
1590
        _ErrorIf(True, self.ENODENET, node, msg)
1591

    
1592

    
1593
  def _VerifyInstance(self, instance, instanceconfig, node_image):
1594
    """Verify an instance.
1595

1596
    This function checks to see if the required block devices are
1597
    available on the instance's node.
1598

1599
    """
1600
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1601
    node_current = instanceconfig.primary_node
1602

    
1603
    node_vol_should = {}
1604
    instanceconfig.MapLVsByNode(node_vol_should)
1605

    
1606
    for node in node_vol_should:
1607
      n_img = node_image[node]
1608
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1609
        # ignore missing volumes on offline or broken nodes
1610
        continue
1611
      for volume in node_vol_should[node]:
1612
        test = volume not in n_img.volumes
1613
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1614
                 "volume %s missing on node %s", volume, node)
1615

    
1616
    if instanceconfig.admin_up:
1617
      pri_img = node_image[node_current]
1618
      test = instance not in pri_img.instances and not pri_img.offline
1619
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1620
               "instance not running on its primary node %s",
1621
               node_current)
1622

    
1623
    for node, n_img in node_image.items():
1624
      if (not node == node_current):
1625
        test = instance in n_img.instances
1626
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1627
                 "instance should not run on node %s", node)
1628

    
1629
  def _VerifyOrphanVolumes(self, node_vol_should, node_image):
1630
    """Verify if there are any unknown volumes in the cluster.
1631

1632
    The .os, .swap and backup volumes are ignored. All other volumes are
1633
    reported as unknown.
1634

1635
    """
1636
    for node, n_img in node_image.items():
1637
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1638
        # skip non-healthy nodes
1639
        continue
1640
      for volume in n_img.volumes:
1641
        test = (node not in node_vol_should or
1642
                volume not in node_vol_should[node])
1643
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1644
                      "volume %s is unknown", volume)
1645

    
1646
  def _VerifyOrphanInstances(self, instancelist, node_image):
1647
    """Verify the list of running instances.
1648

1649
    This checks what instances are running but unknown to the cluster.
1650

1651
    """
1652
    for node, n_img in node_image.items():
1653
      for o_inst in n_img.instances:
1654
        test = o_inst not in instancelist
1655
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1656
                      "instance %s on node %s should not exist", o_inst, node)
1657

    
1658
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1659
    """Verify N+1 Memory Resilience.
1660

1661
    Check that if one single node dies we can still start all the
1662
    instances it was primary for.
1663

1664
    """
1665
    for node, n_img in node_image.items():
1666
      # This code checks that every node which is now listed as
1667
      # secondary has enough memory to host all instances it is
1668
      # supposed to should a single other node in the cluster fail.
1669
      # FIXME: not ready for failover to an arbitrary node
1670
      # FIXME: does not support file-backed instances
1671
      # WARNING: we currently take into account down instances as well
1672
      # as up ones, considering that even if they're down someone
1673
      # might want to start them even in the event of a node failure.
1674
      for prinode, instances in n_img.sbp.items():
1675
        needed_mem = 0
1676
        for instance in instances:
1677
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1678
          if bep[constants.BE_AUTO_BALANCE]:
1679
            needed_mem += bep[constants.BE_MEMORY]
1680
        test = n_img.mfree < needed_mem
1681
        self._ErrorIf(test, self.ENODEN1, node,
1682
                      "not enough memory on to accommodate"
1683
                      " failovers should peer node %s fail", prinode)
1684

    
1685
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1686
                       master_files):
1687
    """Verifies and computes the node required file checksums.
1688

1689
    @type ninfo: L{objects.Node}
1690
    @param ninfo: the node to check
1691
    @param nresult: the remote results for the node
1692
    @param file_list: required list of files
1693
    @param local_cksum: dictionary of local files and their checksums
1694
    @param master_files: list of files that only masters should have
1695

1696
    """
1697
    node = ninfo.name
1698
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1699

    
1700
    remote_cksum = nresult.get(constants.NV_FILELIST, None)
1701
    test = not isinstance(remote_cksum, dict)
1702
    _ErrorIf(test, self.ENODEFILECHECK, node,
1703
             "node hasn't returned file checksum data")
1704
    if test:
1705
      return
1706

    
1707
    for file_name in file_list:
1708
      node_is_mc = ninfo.master_candidate
1709
      must_have = (file_name not in master_files) or node_is_mc
1710
      # missing
1711
      test1 = file_name not in remote_cksum
1712
      # invalid checksum
1713
      test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1714
      # existing and good
1715
      test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1716
      _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1717
               "file '%s' missing", file_name)
1718
      _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1719
               "file '%s' has wrong checksum", file_name)
1720
      # not candidate and this is not a must-have file
1721
      _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1722
               "file '%s' should not exist on non master"
1723
               " candidates (and the file is outdated)", file_name)
1724
      # all good, except non-master/non-must have combination
1725
      _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1726
               "file '%s' should not exist"
1727
               " on non master candidates", file_name)
1728

    
1729
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_map):
1730
    """Verifies and the node DRBD status.
1731

1732
    @type ninfo: L{objects.Node}
1733
    @param ninfo: the node to check
1734
    @param nresult: the remote results for the node
1735
    @param instanceinfo: the dict of instances
1736
    @param drbd_map: the DRBD map as returned by
1737
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1738

1739
    """
1740
    node = ninfo.name
1741
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1742

    
1743
    # compute the DRBD minors
1744
    node_drbd = {}
1745
    for minor, instance in drbd_map[node].items():
1746
      test = instance not in instanceinfo
1747
      _ErrorIf(test, self.ECLUSTERCFG, None,
1748
               "ghost instance '%s' in temporary DRBD map", instance)
1749
        # ghost instance should not be running, but otherwise we
1750
        # don't give double warnings (both ghost instance and
1751
        # unallocated minor in use)
1752
      if test:
1753
        node_drbd[minor] = (instance, False)
1754
      else:
1755
        instance = instanceinfo[instance]
1756
        node_drbd[minor] = (instance.name, instance.admin_up)
1757

    
1758
    # and now check them
1759
    used_minors = nresult.get(constants.NV_DRBDLIST, [])
1760
    test = not isinstance(used_minors, (tuple, list))
1761
    _ErrorIf(test, self.ENODEDRBD, node,
1762
             "cannot parse drbd status file: %s", str(used_minors))
1763
    if test:
1764
      # we cannot check drbd status
1765
      return
1766

    
1767
    for minor, (iname, must_exist) in node_drbd.items():
1768
      test = minor not in used_minors and must_exist
1769
      _ErrorIf(test, self.ENODEDRBD, node,
1770
               "drbd minor %d of instance %s is not active", minor, iname)
1771
    for minor in used_minors:
1772
      test = minor not in node_drbd
1773
      _ErrorIf(test, self.ENODEDRBD, node,
1774
               "unallocated drbd minor %d is in use", minor)
1775

    
1776
  def _UpdateNodeOS(self, ninfo, nresult, nimg):
1777
    """Builds the node OS structures.
1778

1779
    @type ninfo: L{objects.Node}
1780
    @param ninfo: the node to check
1781
    @param nresult: the remote results for the node
1782
    @param nimg: the node image object
1783

1784
    """
1785
    node = ninfo.name
1786
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1787

    
1788
    remote_os = nresult.get(constants.NV_OSLIST, None)
1789
    test = (not isinstance(remote_os, list) or
1790
            not compat.all(isinstance(v, list) and len(v) == 7
1791
                           for v in remote_os))
1792

    
1793
    _ErrorIf(test, self.ENODEOS, node,
1794
             "node hasn't returned valid OS data")
1795

    
1796
    nimg.os_fail = test
1797

    
1798
    if test:
1799
      return
1800

    
1801
    os_dict = {}
1802

    
1803
    for (name, os_path, status, diagnose,
1804
         variants, parameters, api_ver) in nresult[constants.NV_OSLIST]:
1805

    
1806
      if name not in os_dict:
1807
        os_dict[name] = []
1808

    
1809
      # parameters is a list of lists instead of list of tuples due to
1810
      # JSON lacking a real tuple type, fix it:
1811
      parameters = [tuple(v) for v in parameters]
1812
      os_dict[name].append((os_path, status, diagnose,
1813
                            set(variants), set(parameters), set(api_ver)))
1814

    
1815
    nimg.oslist = os_dict
1816

    
1817
  def _VerifyNodeOS(self, ninfo, nimg, base):
1818
    """Verifies the node OS list.
1819

1820
    @type ninfo: L{objects.Node}
1821
    @param ninfo: the node to check
1822
    @param nimg: the node image object
1823
    @param base: the 'template' node we match against (e.g. from the master)
1824

1825
    """
1826
    node = ninfo.name
1827
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1828

    
1829
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1830

    
1831
    for os_name, os_data in nimg.oslist.items():
1832
      assert os_data, "Empty OS status for OS %s?!" % os_name
1833
      f_path, f_status, f_diag, f_var, f_param, f_api = os_data[0]
1834
      _ErrorIf(not f_status, self.ENODEOS, node,
1835
               "Invalid OS %s (located at %s): %s", os_name, f_path, f_diag)
1836
      _ErrorIf(len(os_data) > 1, self.ENODEOS, node,
1837
               "OS '%s' has multiple entries (first one shadows the rest): %s",
1838
               os_name, utils.CommaJoin([v[0] for v in os_data]))
1839
      # this will catched in backend too
1840
      _ErrorIf(compat.any(v >= constants.OS_API_V15 for v in f_api)
1841
               and not f_var, self.ENODEOS, node,
1842
               "OS %s with API at least %d does not declare any variant",
1843
               os_name, constants.OS_API_V15)
1844
      # comparisons with the 'base' image
1845
      test = os_name not in base.oslist
1846
      _ErrorIf(test, self.ENODEOS, node,
1847
               "Extra OS %s not present on reference node (%s)",
1848
               os_name, base.name)
1849
      if test:
1850
        continue
1851
      assert base.oslist[os_name], "Base node has empty OS status?"
1852
      _, b_status, _, b_var, b_param, b_api = base.oslist[os_name][0]
1853
      if not b_status:
1854
        # base OS is invalid, skipping
1855
        continue
1856
      for kind, a, b in [("API version", f_api, b_api),
1857
                         ("variants list", f_var, b_var),
1858
                         ("parameters", f_param, b_param)]:
1859
        _ErrorIf(a != b, self.ENODEOS, node,
1860
                 "OS %s %s differs from reference node %s: %s vs. %s",
1861
                 kind, os_name, base.name,
1862
                 utils.CommaJoin(a), utils.CommaJoin(b))
1863

    
1864
    # check any missing OSes
1865
    missing = set(base.oslist.keys()).difference(nimg.oslist.keys())
1866
    _ErrorIf(missing, self.ENODEOS, node,
1867
             "OSes present on reference node %s but missing on this node: %s",
1868
             base.name, utils.CommaJoin(missing))
1869

    
1870
  def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
1871
    """Verifies and updates the node volume data.
1872

1873
    This function will update a L{NodeImage}'s internal structures
1874
    with data from the remote call.
1875

1876
    @type ninfo: L{objects.Node}
1877
    @param ninfo: the node to check
1878
    @param nresult: the remote results for the node
1879
    @param nimg: the node image object
1880
    @param vg_name: the configured VG name
1881

1882
    """
1883
    node = ninfo.name
1884
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1885

    
1886
    nimg.lvm_fail = True
1887
    lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1888
    if vg_name is None:
1889
      pass
1890
    elif isinstance(lvdata, basestring):
1891
      _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1892
               utils.SafeEncode(lvdata))
1893
    elif not isinstance(lvdata, dict):
1894
      _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1895
    else:
1896
      nimg.volumes = lvdata
1897
      nimg.lvm_fail = False
1898

    
1899
  def _UpdateNodeInstances(self, ninfo, nresult, nimg):
1900
    """Verifies and updates the node instance list.
1901

1902
    If the listing was successful, then updates this node's instance
1903
    list. Otherwise, it marks the RPC call as failed for the instance
1904
    list key.
1905

1906
    @type ninfo: L{objects.Node}
1907
    @param ninfo: the node to check
1908
    @param nresult: the remote results for the node
1909
    @param nimg: the node image object
1910

1911
    """
1912
    idata = nresult.get(constants.NV_INSTANCELIST, None)
1913
    test = not isinstance(idata, list)
1914
    self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed"
1915
                  " (instancelist): %s", utils.SafeEncode(str(idata)))
1916
    if test:
1917
      nimg.hyp_fail = True
1918
    else:
1919
      nimg.instances = idata
1920

    
1921
  def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
1922
    """Verifies and computes a node information map
1923

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

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

    
1934
    # try to read free memory (from the hypervisor)
1935
    hv_info = nresult.get(constants.NV_HVINFO, None)
1936
    test = not isinstance(hv_info, dict) or "memory_free" not in hv_info
1937
    _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1938
    if not test:
1939
      try:
1940
        nimg.mfree = int(hv_info["memory_free"])
1941
      except (ValueError, TypeError):
1942
        _ErrorIf(True, self.ENODERPC, node,
1943
                 "node returned invalid nodeinfo, check hypervisor")
1944

    
1945
    # FIXME: devise a free space model for file based instances as well
1946
    if vg_name is not None:
1947
      test = (constants.NV_VGLIST not in nresult or
1948
              vg_name not in nresult[constants.NV_VGLIST])
1949
      _ErrorIf(test, self.ENODELVM, node,
1950
               "node didn't return data for the volume group '%s'"
1951
               " - it is either missing or broken", vg_name)
1952
      if not test:
1953
        try:
1954
          nimg.dfree = int(nresult[constants.NV_VGLIST][vg_name])
1955
        except (ValueError, TypeError):
1956
          _ErrorIf(True, self.ENODERPC, node,
1957
                   "node returned invalid LVM info, check LVM status")
1958

    
1959
  def BuildHooksEnv(self):
1960
    """Build hooks env.
1961

1962
    Cluster-Verify hooks just ran in the post phase and their failure makes
1963
    the output be logged in the verify output and the verification to fail.
1964

1965
    """
1966
    all_nodes = self.cfg.GetNodeList()
1967
    env = {
1968
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1969
      }
1970
    for node in self.cfg.GetAllNodesInfo().values():
1971
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1972

    
1973
    return env, [], all_nodes
1974

    
1975
  def Exec(self, feedback_fn):
1976
    """Verify integrity of cluster, performing various test on nodes.
1977

1978
    """
1979
    self.bad = False
1980
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1981
    verbose = self.op.verbose
1982
    self._feedback_fn = feedback_fn
1983
    feedback_fn("* Verifying global settings")
1984
    for msg in self.cfg.VerifyConfig():
1985
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1986

    
1987
    # Check the cluster certificates
1988
    for cert_filename in constants.ALL_CERT_FILES:
1989
      (errcode, msg) = _VerifyCertificate(cert_filename)
1990
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1991

    
1992
    vg_name = self.cfg.GetVGName()
1993
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1994
    cluster = self.cfg.GetClusterInfo()
1995
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1996
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1997
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1998
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1999
                        for iname in instancelist)
2000
    i_non_redundant = [] # Non redundant instances
2001
    i_non_a_balanced = [] # Non auto-balanced instances
2002
    n_offline = 0 # Count of offline nodes
2003
    n_drained = 0 # Count of nodes being drained
2004
    node_vol_should = {}
2005

    
2006
    # FIXME: verify OS list
2007
    # do local checksums
2008
    master_files = [constants.CLUSTER_CONF_FILE]
2009
    master_node = self.master_node = self.cfg.GetMasterNode()
2010
    master_ip = self.cfg.GetMasterIP()
2011

    
2012
    file_names = ssconf.SimpleStore().GetFileList()
2013
    file_names.extend(constants.ALL_CERT_FILES)
2014
    file_names.extend(master_files)
2015
    if cluster.modify_etc_hosts:
2016
      file_names.append(constants.ETC_HOSTS)
2017

    
2018
    local_checksums = utils.FingerprintFiles(file_names)
2019

    
2020
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
2021
    node_verify_param = {
2022
      constants.NV_FILELIST: file_names,
2023
      constants.NV_NODELIST: [node.name for node in nodeinfo
2024
                              if not node.offline],
2025
      constants.NV_HYPERVISOR: hypervisors,
2026
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
2027
                                  node.secondary_ip) for node in nodeinfo
2028
                                 if not node.offline],
2029
      constants.NV_INSTANCELIST: hypervisors,
2030
      constants.NV_VERSION: None,
2031
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
2032
      constants.NV_NODESETUP: None,
2033
      constants.NV_TIME: None,
2034
      constants.NV_MASTERIP: (master_node, master_ip),
2035
      constants.NV_OSLIST: None,
2036
      }
2037

    
2038
    if vg_name is not None:
2039
      node_verify_param[constants.NV_VGLIST] = None
2040
      node_verify_param[constants.NV_LVLIST] = vg_name
2041
      node_verify_param[constants.NV_PVLIST] = [vg_name]
2042
      node_verify_param[constants.NV_DRBDLIST] = None
2043

    
2044
    # Build our expected cluster state
2045
    node_image = dict((node.name, self.NodeImage(offline=node.offline,
2046
                                                 name=node.name))
2047
                      for node in nodeinfo)
2048

    
2049
    for instance in instancelist:
2050
      inst_config = instanceinfo[instance]
2051

    
2052
      for nname in inst_config.all_nodes:
2053
        if nname not in node_image:
2054
          # ghost node
2055
          gnode = self.NodeImage(name=nname)
2056
          gnode.ghost = True
2057
          node_image[nname] = gnode
2058

    
2059
      inst_config.MapLVsByNode(node_vol_should)
2060

    
2061
      pnode = inst_config.primary_node
2062
      node_image[pnode].pinst.append(instance)
2063

    
2064
      for snode in inst_config.secondary_nodes:
2065
        nimg = node_image[snode]
2066
        nimg.sinst.append(instance)
2067
        if pnode not in nimg.sbp:
2068
          nimg.sbp[pnode] = []
2069
        nimg.sbp[pnode].append(instance)
2070

    
2071
    # At this point, we have the in-memory data structures complete,
2072
    # except for the runtime information, which we'll gather next
2073

    
2074
    # Due to the way our RPC system works, exact response times cannot be
2075
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
2076
    # time before and after executing the request, we can at least have a time
2077
    # window.
2078
    nvinfo_starttime = time.time()
2079
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
2080
                                           self.cfg.GetClusterName())
2081
    nvinfo_endtime = time.time()
2082

    
2083
    all_drbd_map = self.cfg.ComputeDRBDMap()
2084

    
2085
    feedback_fn("* Verifying node status")
2086

    
2087
    refos_img = None
2088

    
2089
    for node_i in nodeinfo:
2090
      node = node_i.name
2091
      nimg = node_image[node]
2092

    
2093
      if node_i.offline:
2094
        if verbose:
2095
          feedback_fn("* Skipping offline node %s" % (node,))
2096
        n_offline += 1
2097
        continue
2098

    
2099
      if node == master_node:
2100
        ntype = "master"
2101
      elif node_i.master_candidate:
2102
        ntype = "master candidate"
2103
      elif node_i.drained:
2104
        ntype = "drained"
2105
        n_drained += 1
2106
      else:
2107
        ntype = "regular"
2108
      if verbose:
2109
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
2110

    
2111
      msg = all_nvinfo[node].fail_msg
2112
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
2113
      if msg:
2114
        nimg.rpc_fail = True
2115
        continue
2116

    
2117
      nresult = all_nvinfo[node].payload
2118

    
2119
      nimg.call_ok = self._VerifyNode(node_i, nresult)
2120
      self._VerifyNodeNetwork(node_i, nresult)
2121
      self._VerifyNodeLVM(node_i, nresult, vg_name)
2122
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
2123
                            master_files)
2124
      self._VerifyNodeDrbd(node_i, nresult, instanceinfo, all_drbd_map)
2125
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
2126

    
2127
      self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
2128
      self._UpdateNodeInstances(node_i, nresult, nimg)
2129
      self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
2130
      self._UpdateNodeOS(node_i, nresult, nimg)
2131
      if not nimg.os_fail:
2132
        if refos_img is None:
2133
          refos_img = nimg
2134
        self._VerifyNodeOS(node_i, nimg, refos_img)
2135

    
2136
    feedback_fn("* Verifying instance status")
2137
    for instance in instancelist:
2138
      if verbose:
2139
        feedback_fn("* Verifying instance %s" % instance)
2140
      inst_config = instanceinfo[instance]
2141
      self._VerifyInstance(instance, inst_config, node_image)
2142
      inst_nodes_offline = []
2143

    
2144
      pnode = inst_config.primary_node
2145
      pnode_img = node_image[pnode]
2146
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
2147
               self.ENODERPC, pnode, "instance %s, connection to"
2148
               " primary node failed", instance)
2149

    
2150
      if pnode_img.offline:
2151
        inst_nodes_offline.append(pnode)
2152

    
2153
      # If the instance is non-redundant we cannot survive losing its primary
2154
      # node, so we are not N+1 compliant. On the other hand we have no disk
2155
      # templates with more than one secondary so that situation is not well
2156
      # supported either.
2157
      # FIXME: does not support file-backed instances
2158
      if not inst_config.secondary_nodes:
2159
        i_non_redundant.append(instance)
2160
      _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
2161
               instance, "instance has multiple secondary nodes: %s",
2162
               utils.CommaJoin(inst_config.secondary_nodes),
2163
               code=self.ETYPE_WARNING)
2164

    
2165
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
2166
        i_non_a_balanced.append(instance)
2167

    
2168
      for snode in inst_config.secondary_nodes:
2169
        s_img = node_image[snode]
2170
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
2171
                 "instance %s, connection to secondary node failed", instance)
2172

    
2173
        if s_img.offline:
2174
          inst_nodes_offline.append(snode)
2175

    
2176
      # warn that the instance lives on offline nodes
2177
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
2178
               "instance lives on offline node(s) %s",
2179
               utils.CommaJoin(inst_nodes_offline))
2180
      # ... or ghost nodes
2181
      for node in inst_config.all_nodes:
2182
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
2183
                 "instance lives on ghost node %s", node)
2184

    
2185
    feedback_fn("* Verifying orphan volumes")
2186
    self._VerifyOrphanVolumes(node_vol_should, node_image)
2187

    
2188
    feedback_fn("* Verifying orphan instances")
2189
    self._VerifyOrphanInstances(instancelist, node_image)
2190

    
2191
    if constants.VERIFY_NPLUSONE_MEM not in self.op.skip_checks:
2192
      feedback_fn("* Verifying N+1 Memory redundancy")
2193
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
2194

    
2195
    feedback_fn("* Other Notes")
2196
    if i_non_redundant:
2197
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
2198
                  % len(i_non_redundant))
2199

    
2200
    if i_non_a_balanced:
2201
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
2202
                  % len(i_non_a_balanced))
2203

    
2204
    if n_offline:
2205
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
2206

    
2207
    if n_drained:
2208
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
2209

    
2210
    return not self.bad
2211

    
2212
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
2213
    """Analyze the post-hooks' result
2214

2215
    This method analyses the hook result, handles it, and sends some
2216
    nicely-formatted feedback back to the user.
2217

2218
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
2219
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
2220
    @param hooks_results: the results of the multi-node hooks rpc call
2221
    @param feedback_fn: function used send feedback back to the caller
2222
    @param lu_result: previous Exec result
2223
    @return: the new Exec result, based on the previous result
2224
        and hook results
2225

2226
    """
2227
    # We only really run POST phase hooks, and are only interested in
2228
    # their results
2229
    if phase == constants.HOOKS_PHASE_POST:
2230
      # Used to change hooks' output to proper indentation
2231
      indent_re = re.compile('^', re.M)
2232
      feedback_fn("* Hooks Results")
2233
      assert hooks_results, "invalid result from hooks"
2234

    
2235
      for node_name in hooks_results:
2236
        res = hooks_results[node_name]
2237
        msg = res.fail_msg
2238
        test = msg and not res.offline
2239
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
2240
                      "Communication failure in hooks execution: %s", msg)
2241
        if res.offline or msg:
2242
          # No need to investigate payload if node is offline or gave an error.
2243
          # override manually lu_result here as _ErrorIf only
2244
          # overrides self.bad
2245
          lu_result = 1
2246
          continue
2247
        for script, hkr, output in res.payload:
2248
          test = hkr == constants.HKR_FAIL
2249
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
2250
                        "Script %s failed, output:", script)
2251
          if test:
2252
            output = indent_re.sub('      ', output)
2253
            feedback_fn("%s" % output)
2254
            lu_result = 0
2255

    
2256
      return lu_result
2257

    
2258

    
2259
class LUVerifyDisks(NoHooksLU):
2260
  """Verifies the cluster disks status.
2261

2262
  """
2263
  _OP_REQP = []
2264
  REQ_BGL = False
2265

    
2266
  def ExpandNames(self):
2267
    self.needed_locks = {
2268
      locking.LEVEL_NODE: locking.ALL_SET,
2269
      locking.LEVEL_INSTANCE: locking.ALL_SET,
2270
    }
2271
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
2272

    
2273
  def Exec(self, feedback_fn):
2274
    """Verify integrity of cluster disks.
2275

2276
    @rtype: tuple of three items
2277
    @return: a tuple of (dict of node-to-node_error, list of instances
2278
        which need activate-disks, dict of instance: (node, volume) for
2279
        missing volumes
2280

2281
    """
2282
    result = res_nodes, res_instances, res_missing = {}, [], {}
2283

    
2284
    vg_name = self.cfg.GetVGName()
2285
    nodes = utils.NiceSort(self.cfg.GetNodeList())
2286
    instances = [self.cfg.GetInstanceInfo(name)
2287
                 for name in self.cfg.GetInstanceList()]
2288

    
2289
    nv_dict = {}
2290
    for inst in instances:
2291
      inst_lvs = {}
2292
      if (not inst.admin_up or
2293
          inst.disk_template not in constants.DTS_NET_MIRROR):
2294
        continue
2295
      inst.MapLVsByNode(inst_lvs)
2296
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
2297
      for node, vol_list in inst_lvs.iteritems():
2298
        for vol in vol_list:
2299
          nv_dict[(node, vol)] = inst
2300

    
2301
    if not nv_dict:
2302
      return result
2303

    
2304
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
2305

    
2306
    for node in nodes:
2307
      # node_volume
2308
      node_res = node_lvs[node]
2309
      if node_res.offline:
2310
        continue
2311
      msg = node_res.fail_msg
2312
      if msg:
2313
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
2314
        res_nodes[node] = msg
2315
        continue
2316

    
2317
      lvs = node_res.payload
2318
      for lv_name, (_, _, lv_online) in lvs.items():
2319
        inst = nv_dict.pop((node, lv_name), None)
2320
        if (not lv_online and inst is not None
2321
            and inst.name not in res_instances):
2322
          res_instances.append(inst.name)
2323

    
2324
    # any leftover items in nv_dict are missing LVs, let's arrange the
2325
    # data better
2326
    for key, inst in nv_dict.iteritems():
2327
      if inst.name not in res_missing:
2328
        res_missing[inst.name] = []
2329
      res_missing[inst.name].append(key)
2330

    
2331
    return result
2332

    
2333

    
2334
class LURepairDiskSizes(NoHooksLU):
2335
  """Verifies the cluster disks sizes.
2336

2337
  """
2338
  _OP_REQP = [("instances", _TListOf(_TNonEmptyString))]
2339
  REQ_BGL = False
2340

    
2341
  def ExpandNames(self):
2342
    if self.op.instances:
2343
      self.wanted_names = []
2344
      for name in self.op.instances:
2345
        full_name = _ExpandInstanceName(self.cfg, name)
2346
        self.wanted_names.append(full_name)
2347
      self.needed_locks = {
2348
        locking.LEVEL_NODE: [],
2349
        locking.LEVEL_INSTANCE: self.wanted_names,
2350
        }
2351
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2352
    else:
2353
      self.wanted_names = None
2354
      self.needed_locks = {
2355
        locking.LEVEL_NODE: locking.ALL_SET,
2356
        locking.LEVEL_INSTANCE: locking.ALL_SET,
2357
        }
2358
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
2359

    
2360
  def DeclareLocks(self, level):
2361
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2362
      self._LockInstancesNodes(primary_only=True)
2363

    
2364
  def CheckPrereq(self):
2365
    """Check prerequisites.
2366

2367
    This only checks the optional instance list against the existing names.
2368

2369
    """
2370
    if self.wanted_names is None:
2371
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2372

    
2373
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2374
                             in self.wanted_names]
2375

    
2376
  def _EnsureChildSizes(self, disk):
2377
    """Ensure children of the disk have the needed disk size.
2378

2379
    This is valid mainly for DRBD8 and fixes an issue where the
2380
    children have smaller disk size.
2381

2382
    @param disk: an L{ganeti.objects.Disk} object
2383

2384
    """
2385
    if disk.dev_type == constants.LD_DRBD8:
2386
      assert disk.children, "Empty children for DRBD8?"
2387
      fchild = disk.children[0]
2388
      mismatch = fchild.size < disk.size
2389
      if mismatch:
2390
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2391
                     fchild.size, disk.size)
2392
        fchild.size = disk.size
2393

    
2394
      # and we recurse on this child only, not on the metadev
2395
      return self._EnsureChildSizes(fchild) or mismatch
2396
    else:
2397
      return False
2398

    
2399
  def Exec(self, feedback_fn):
2400
    """Verify the size of cluster disks.
2401

2402
    """
2403
    # TODO: check child disks too
2404
    # TODO: check differences in size between primary/secondary nodes
2405
    per_node_disks = {}
2406
    for instance in self.wanted_instances:
2407
      pnode = instance.primary_node
2408
      if pnode not in per_node_disks:
2409
        per_node_disks[pnode] = []
2410
      for idx, disk in enumerate(instance.disks):
2411
        per_node_disks[pnode].append((instance, idx, disk))
2412

    
2413
    changed = []
2414
    for node, dskl in per_node_disks.items():
2415
      newl = [v[2].Copy() for v in dskl]
2416
      for dsk in newl:
2417
        self.cfg.SetDiskID(dsk, node)
2418
      result = self.rpc.call_blockdev_getsizes(node, newl)
2419
      if result.fail_msg:
2420
        self.LogWarning("Failure in blockdev_getsizes call to node"
2421
                        " %s, ignoring", node)
2422
        continue
2423
      if len(result.data) != len(dskl):
2424
        self.LogWarning("Invalid result from node %s, ignoring node results",
2425
                        node)
2426
        continue
2427
      for ((instance, idx, disk), size) in zip(dskl, result.data):
2428
        if size is None:
2429
          self.LogWarning("Disk %d of instance %s did not return size"
2430
                          " information, ignoring", idx, instance.name)
2431
          continue
2432
        if not isinstance(size, (int, long)):
2433
          self.LogWarning("Disk %d of instance %s did not return valid"
2434
                          " size information, ignoring", idx, instance.name)
2435
          continue
2436
        size = size >> 20
2437
        if size != disk.size:
2438
          self.LogInfo("Disk %d of instance %s has mismatched size,"
2439
                       " correcting: recorded %d, actual %d", idx,
2440
                       instance.name, disk.size, size)
2441
          disk.size = size
2442
          self.cfg.Update(instance, feedback_fn)
2443
          changed.append((instance.name, idx, size))
2444
        if self._EnsureChildSizes(disk):
2445
          self.cfg.Update(instance, feedback_fn)
2446
          changed.append((instance.name, idx, disk.size))
2447
    return changed
2448

    
2449

    
2450
class LURenameCluster(LogicalUnit):
2451
  """Rename the cluster.
2452

2453
  """
2454
  HPATH = "cluster-rename"
2455
  HTYPE = constants.HTYPE_CLUSTER
2456
  _OP_REQP = [("name", _TNonEmptyString)]
2457

    
2458
  def BuildHooksEnv(self):
2459
    """Build hooks env.
2460

2461
    """
2462
    env = {
2463
      "OP_TARGET": self.cfg.GetClusterName(),
2464
      "NEW_NAME": self.op.name,
2465
      }
2466
    mn = self.cfg.GetMasterNode()
2467
    all_nodes = self.cfg.GetNodeList()
2468
    return env, [mn], all_nodes
2469

    
2470
  def CheckPrereq(self):
2471
    """Verify that the passed name is a valid one.
2472

2473
    """
2474
    hostname = utils.GetHostInfo(self.op.name)
2475

    
2476
    new_name = hostname.name
2477
    self.ip = new_ip = hostname.ip
2478
    old_name = self.cfg.GetClusterName()
2479
    old_ip = self.cfg.GetMasterIP()
2480
    if new_name == old_name and new_ip == old_ip:
2481
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
2482
                                 " cluster has changed",
2483
                                 errors.ECODE_INVAL)
2484
    if new_ip != old_ip:
2485
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
2486
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
2487
                                   " reachable on the network. Aborting." %
2488
                                   new_ip, errors.ECODE_NOTUNIQUE)
2489

    
2490
    self.op.name = new_name
2491

    
2492
  def Exec(self, feedback_fn):
2493
    """Rename the cluster.
2494

2495
    """
2496
    clustername = self.op.name
2497
    ip = self.ip
2498

    
2499
    # shutdown the master IP
2500
    master = self.cfg.GetMasterNode()
2501
    result = self.rpc.call_node_stop_master(master, False)
2502
    result.Raise("Could not disable the master role")
2503

    
2504
    try:
2505
      cluster = self.cfg.GetClusterInfo()
2506
      cluster.cluster_name = clustername
2507
      cluster.master_ip = ip
2508
      self.cfg.Update(cluster, feedback_fn)
2509

    
2510
      # update the known hosts file
2511
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2512
      node_list = self.cfg.GetNodeList()
2513
      try:
2514
        node_list.remove(master)
2515
      except ValueError:
2516
        pass
2517
      result = self.rpc.call_upload_file(node_list,
2518
                                         constants.SSH_KNOWN_HOSTS_FILE)
2519
      for to_node, to_result in result.iteritems():
2520
        msg = to_result.fail_msg
2521
        if msg:
2522
          msg = ("Copy of file %s to node %s failed: %s" %
2523
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
2524
          self.proc.LogWarning(msg)
2525

    
2526
    finally:
2527
      result = self.rpc.call_node_start_master(master, False, False)
2528
      msg = result.fail_msg
2529
      if msg:
2530
        self.LogWarning("Could not re-enable the master role on"
2531
                        " the master, please restart manually: %s", msg)
2532

    
2533

    
2534
def _RecursiveCheckIfLVMBased(disk):
2535
  """Check if the given disk or its children are lvm-based.
2536

2537
  @type disk: L{objects.Disk}
2538
  @param disk: the disk to check
2539
  @rtype: boolean
2540
  @return: boolean indicating whether a LD_LV dev_type was found or not
2541

2542
  """
2543
  if disk.children:
2544
    for chdisk in disk.children:
2545
      if _RecursiveCheckIfLVMBased(chdisk):
2546
        return True
2547
  return disk.dev_type == constants.LD_LV
2548

    
2549

    
2550
class LUSetClusterParams(LogicalUnit):
2551
  """Change the parameters of the cluster.
2552

2553
  """
2554
  HPATH = "cluster-modify"
2555
  HTYPE = constants.HTYPE_CLUSTER
2556
  _OP_REQP = [
2557
    ("hvparams", _TOr(_TDictOf(_TNonEmptyString, _TDict), _TNone)),
2558
    ("os_hvp", _TOr(_TDictOf(_TNonEmptyString, _TDict), _TNone)),
2559
    ("osparams", _TOr(_TDictOf(_TNonEmptyString, _TDict), _TNone)),
2560
    ("enabled_hypervisors",
2561
     _TOr(_TAnd(_TListOf(_TElemOf(constants.HYPER_TYPES)), _TTrue), _TNone)),
2562
    ]
2563
  _OP_DEFS = [
2564
    ("candidate_pool_size", None),
2565
    ("uid_pool", None),
2566
    ("add_uids", None),
2567
    ("remove_uids", None),
2568
    ("hvparams", None),
2569
    ("os_hvp", None),
2570
    ("osparams", None),
2571
    ]
2572
  REQ_BGL = False
2573

    
2574
  def CheckArguments(self):
2575
    """Check parameters
2576

2577
    """
2578
    if self.op.candidate_pool_size is not None:
2579
      try:
2580
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
2581
      except (ValueError, TypeError), err:
2582
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
2583
                                   str(err), errors.ECODE_INVAL)
2584
      if self.op.candidate_pool_size < 1:
2585
        raise errors.OpPrereqError("At least one master candidate needed",
2586
                                   errors.ECODE_INVAL)
2587

    
2588
    _CheckBooleanOpField(self.op, "maintain_node_health")
2589

    
2590
    if self.op.uid_pool:
2591
      uidpool.CheckUidPool(self.op.uid_pool)
2592

    
2593
    if self.op.add_uids:
2594
      uidpool.CheckUidPool(self.op.add_uids)
2595

    
2596
    if self.op.remove_uids:
2597
      uidpool.CheckUidPool(self.op.remove_uids)
2598

    
2599
  def ExpandNames(self):
2600
    # FIXME: in the future maybe other cluster params won't require checking on
2601
    # all nodes to be modified.
2602
    self.needed_locks = {
2603
      locking.LEVEL_NODE: locking.ALL_SET,
2604
    }
2605
    self.share_locks[locking.LEVEL_NODE] = 1
2606

    
2607
  def BuildHooksEnv(self):
2608
    """Build hooks env.
2609

2610
    """
2611
    env = {
2612
      "OP_TARGET": self.cfg.GetClusterName(),
2613
      "NEW_VG_NAME": self.op.vg_name,
2614
      }
2615
    mn = self.cfg.GetMasterNode()
2616
    return env, [mn], [mn]
2617

    
2618
  def CheckPrereq(self):
2619
    """Check prerequisites.
2620

2621
    This checks whether the given params don't conflict and
2622
    if the given volume group is valid.
2623

2624
    """
2625
    if self.op.vg_name is not None and not self.op.vg_name:
2626
      instances = self.cfg.GetAllInstancesInfo().values()
2627
      for inst in instances:
2628
        for disk in inst.disks:
2629
          if _RecursiveCheckIfLVMBased(disk):
2630
            raise errors.OpPrereqError("Cannot disable lvm storage while"
2631
                                       " lvm-based instances exist",
2632
                                       errors.ECODE_INVAL)
2633

    
2634
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2635

    
2636
    # if vg_name not None, checks given volume group on all nodes
2637
    if self.op.vg_name:
2638
      vglist = self.rpc.call_vg_list(node_list)
2639
      for node in node_list:
2640
        msg = vglist[node].fail_msg
2641
        if msg:
2642
          # ignoring down node
2643
          self.LogWarning("Error while gathering data on node %s"
2644
                          " (ignoring node): %s", node, msg)
2645
          continue
2646
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2647
                                              self.op.vg_name,
2648
                                              constants.MIN_VG_SIZE)
2649
        if vgstatus:
2650
          raise errors.OpPrereqError("Error on node '%s': %s" %
2651
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2652

    
2653
    self.cluster = cluster = self.cfg.GetClusterInfo()
2654
    # validate params changes
2655
    if self.op.beparams:
2656
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2657
      self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
2658

    
2659
    if self.op.nicparams:
2660
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2661
      self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
2662
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2663
      nic_errors = []
2664

    
2665
      # check all instances for consistency
2666
      for instance in self.cfg.GetAllInstancesInfo().values():
2667
        for nic_idx, nic in enumerate(instance.nics):
2668
          params_copy = copy.deepcopy(nic.nicparams)
2669
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2670

    
2671
          # check parameter syntax
2672
          try:
2673
            objects.NIC.CheckParameterSyntax(params_filled)
2674
          except errors.ConfigurationError, err:
2675
            nic_errors.append("Instance %s, nic/%d: %s" %
2676
                              (instance.name, nic_idx, err))
2677

    
2678
          # if we're moving instances to routed, check that they have an ip
2679
          target_mode = params_filled[constants.NIC_MODE]
2680
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2681
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2682
                              (instance.name, nic_idx))
2683
      if nic_errors:
2684
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2685
                                   "\n".join(nic_errors))
2686

    
2687
    # hypervisor list/parameters
2688
    self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
2689
    if self.op.hvparams:
2690
      for hv_name, hv_dict in self.op.hvparams.items():
2691
        if hv_name not in self.new_hvparams:
2692
          self.new_hvparams[hv_name] = hv_dict
2693
        else:
2694
          self.new_hvparams[hv_name].update(hv_dict)
2695

    
2696
    # os hypervisor parameters
2697
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2698
    if self.op.os_hvp:
2699
      for os_name, hvs in self.op.os_hvp.items():
2700
        if os_name not in self.new_os_hvp:
2701
          self.new_os_hvp[os_name] = hvs
2702
        else:
2703
          for hv_name, hv_dict in hvs.items():
2704
            if hv_name not in self.new_os_hvp[os_name]:
2705
              self.new_os_hvp[os_name][hv_name] = hv_dict
2706
            else:
2707
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2708

    
2709
    # os parameters
2710
    self.new_osp = objects.FillDict(cluster.osparams, {})
2711
    if self.op.osparams:
2712
      for os_name, osp in self.op.osparams.items():
2713
        if os_name not in self.new_osp:
2714
          self.new_osp[os_name] = {}
2715

    
2716
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
2717
                                                  use_none=True)
2718

    
2719
        if not self.new_osp[os_name]:
2720
          # we removed all parameters
2721
          del self.new_osp[os_name]
2722
        else:
2723
          # check the parameter validity (remote check)
2724
          _CheckOSParams(self, False, [self.cfg.GetMasterNode()],
2725
                         os_name, self.new_osp[os_name])
2726

    
2727
    # changes to the hypervisor list
2728
    if self.op.enabled_hypervisors is not None:
2729
      self.hv_list = self.op.enabled_hypervisors
2730
      for hv in self.hv_list:
2731
        # if the hypervisor doesn't already exist in the cluster
2732
        # hvparams, we initialize it to empty, and then (in both
2733
        # cases) we make sure to fill the defaults, as we might not
2734
        # have a complete defaults list if the hypervisor wasn't
2735
        # enabled before
2736
        if hv not in new_hvp:
2737
          new_hvp[hv] = {}
2738
        new_hvp[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], new_hvp[hv])
2739
        utils.ForceDictType(new_hvp[hv], constants.HVS_PARAMETER_TYPES)
2740
    else:
2741
      self.hv_list = cluster.enabled_hypervisors
2742

    
2743
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2744
      # either the enabled list has changed, or the parameters have, validate
2745
      for hv_name, hv_params in self.new_hvparams.items():
2746
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2747
            (self.op.enabled_hypervisors and
2748
             hv_name in self.op.enabled_hypervisors)):
2749
          # either this is a new hypervisor, or its parameters have changed
2750
          hv_class = hypervisor.GetHypervisor(hv_name)
2751
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2752
          hv_class.CheckParameterSyntax(hv_params)
2753
          _CheckHVParams(self, node_list, hv_name, hv_params)
2754

    
2755
    if self.op.os_hvp:
2756
      # no need to check any newly-enabled hypervisors, since the
2757
      # defaults have already been checked in the above code-block
2758
      for os_name, os_hvp in self.new_os_hvp.items():
2759
        for hv_name, hv_params in os_hvp.items():
2760
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2761
          # we need to fill in the new os_hvp on top of the actual hv_p
2762
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2763
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2764
          hv_class = hypervisor.GetHypervisor(hv_name)
2765
          hv_class.CheckParameterSyntax(new_osp)
2766
          _CheckHVParams(self, node_list, hv_name, new_osp)
2767

    
2768

    
2769
  def Exec(self, feedback_fn):
2770
    """Change the parameters of the cluster.
2771

2772
    """
2773
    if self.op.vg_name is not None:
2774
      new_volume = self.op.vg_name
2775
      if not new_volume:
2776
        new_volume = None
2777
      if new_volume != self.cfg.GetVGName():
2778
        self.cfg.SetVGName(new_volume)
2779
      else:
2780
        feedback_fn("Cluster LVM configuration already in desired"
2781
                    " state, not changing")
2782
    if self.op.hvparams:
2783
      self.cluster.hvparams = self.new_hvparams
2784
    if self.op.os_hvp:
2785
      self.cluster.os_hvp = self.new_os_hvp
2786
    if self.op.enabled_hypervisors is not None:
2787
      self.cluster.hvparams = self.new_hvparams
2788
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2789
    if self.op.beparams:
2790
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2791
    if self.op.nicparams:
2792
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2793
    if self.op.osparams:
2794
      self.cluster.osparams = self.new_osp
2795

    
2796
    if self.op.candidate_pool_size is not None:
2797
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2798
      # we need to update the pool size here, otherwise the save will fail
2799
      _AdjustCandidatePool(self, [])
2800

    
2801
    if self.op.maintain_node_health is not None:
2802
      self.cluster.maintain_node_health = self.op.maintain_node_health
2803

    
2804
    if self.op.add_uids is not None:
2805
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
2806

    
2807
    if self.op.remove_uids is not None:
2808
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
2809

    
2810
    if self.op.uid_pool is not None:
2811
      self.cluster.uid_pool = self.op.uid_pool
2812

    
2813
    self.cfg.Update(self.cluster, feedback_fn)
2814

    
2815

    
2816
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2817
  """Distribute additional files which are part of the cluster configuration.
2818

2819
  ConfigWriter takes care of distributing the config and ssconf files, but
2820
  there are more files which should be distributed to all nodes. This function
2821
  makes sure those are copied.
2822

2823
  @param lu: calling logical unit
2824
  @param additional_nodes: list of nodes not in the config to distribute to
2825

2826
  """
2827
  # 1. Gather target nodes
2828
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2829
  dist_nodes = lu.cfg.GetOnlineNodeList()
2830
  if additional_nodes is not None:
2831
    dist_nodes.extend(additional_nodes)
2832
  if myself.name in dist_nodes:
2833
    dist_nodes.remove(myself.name)
2834

    
2835
  # 2. Gather files to distribute
2836
  dist_files = set([constants.ETC_HOSTS,
2837
                    constants.SSH_KNOWN_HOSTS_FILE,
2838
                    constants.RAPI_CERT_FILE,
2839
                    constants.RAPI_USERS_FILE,
2840
                    constants.CONFD_HMAC_KEY,
2841
                    constants.CLUSTER_DOMAIN_SECRET_FILE,
2842
                   ])
2843

    
2844
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2845
  for hv_name in enabled_hypervisors:
2846
    hv_class = hypervisor.GetHypervisor(hv_name)
2847
    dist_files.update(hv_class.GetAncillaryFiles())
2848

    
2849
  # 3. Perform the files upload
2850
  for fname in dist_files:
2851
    if os.path.exists(fname):
2852
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2853
      for to_node, to_result in result.items():
2854
        msg = to_result.fail_msg
2855
        if msg:
2856
          msg = ("Copy of file %s to node %s failed: %s" %
2857
                 (fname, to_node, msg))
2858
          lu.proc.LogWarning(msg)
2859

    
2860

    
2861
class LURedistributeConfig(NoHooksLU):
2862
  """Force the redistribution of cluster configuration.
2863

2864
  This is a very simple LU.
2865

2866
  """
2867
  _OP_REQP = []
2868
  REQ_BGL = False
2869

    
2870
  def ExpandNames(self):
2871
    self.needed_locks = {
2872
      locking.LEVEL_NODE: locking.ALL_SET,
2873
    }
2874
    self.share_locks[locking.LEVEL_NODE] = 1
2875

    
2876
  def Exec(self, feedback_fn):
2877
    """Redistribute the configuration.
2878

2879
    """
2880
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2881
    _RedistributeAncillaryFiles(self)
2882

    
2883

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

2887
  """
2888
  if not instance.disks or disks is not None and not disks:
2889
    return True
2890

    
2891
  disks = _ExpandCheckDisks(instance, disks)
2892

    
2893
  if not oneshot:
2894
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2895

    
2896
  node = instance.primary_node
2897

    
2898
  for dev in disks:
2899
    lu.cfg.SetDiskID(dev, node)
2900

    
2901
  # TODO: Convert to utils.Retry
2902

    
2903
  retries = 0
2904
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2905
  while True:
2906
    max_time = 0
2907
    done = True
2908
    cumul_degraded = False
2909
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, disks)
2910
    msg = rstats.fail_msg
2911
    if msg:
2912
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2913
      retries += 1
2914
      if retries >= 10:
2915
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2916
                                 " aborting." % node)
2917
      time.sleep(6)
2918
      continue
2919
    rstats = rstats.payload
2920
    retries = 0
2921
    for i, mstat in enumerate(rstats):
2922
      if mstat is None:
2923
        lu.LogWarning("Can't compute data for node %s/%s",
2924
                           node, disks[i].iv_name)
2925
        continue
2926

    
2927
      cumul_degraded = (cumul_degraded or
2928
                        (mstat.is_degraded and mstat.sync_percent is None))
2929
      if mstat.sync_percent is not None:
2930
        done = False
2931
        if mstat.estimated_time is not None:
2932
          rem_time = ("%s remaining (estimated)" %
2933
                      utils.FormatSeconds(mstat.estimated_time))
2934
          max_time = mstat.estimated_time
2935
        else:
2936
          rem_time = "no time estimate"
2937
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2938
                        (disks[i].iv_name, mstat.sync_percent, rem_time))
2939

    
2940
    # if we're done but degraded, let's do a few small retries, to
2941
    # make sure we see a stable and not transient situation; therefore
2942
    # we force restart of the loop
2943
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2944
      logging.info("Degraded disks found, %d retries left", degr_retries)
2945
      degr_retries -= 1
2946
      time.sleep(1)
2947
      continue
2948

    
2949
    if done or oneshot:
2950
      break
2951

    
2952
    time.sleep(min(60, max_time))
2953

    
2954
  if done:
2955
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2956
  return not cumul_degraded
2957

    
2958

    
2959
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2960
  """Check that mirrors are not degraded.
2961

2962
  The ldisk parameter, if True, will change the test from the
2963
  is_degraded attribute (which represents overall non-ok status for
2964
  the device(s)) to the ldisk (representing the local storage status).
2965

2966
  """
2967
  lu.cfg.SetDiskID(dev, node)
2968

    
2969
  result = True
2970

    
2971
  if on_primary or dev.AssembleOnSecondary():
2972
    rstats = lu.rpc.call_blockdev_find(node, dev)
2973
    msg = rstats.fail_msg
2974
    if msg:
2975
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2976
      result = False
2977
    elif not rstats.payload:
2978
      lu.LogWarning("Can't find disk on node %s", node)
2979
      result = False
2980
    else:
2981
      if ldisk:
2982
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2983
      else:
2984
        result = result and not rstats.payload.is_degraded
2985

    
2986
  if dev.children:
2987
    for child in dev.children:
2988
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2989

    
2990
  return result
2991

    
2992

    
2993
class LUDiagnoseOS(NoHooksLU):
2994
  """Logical unit for OS diagnose/query.
2995

2996
  """
2997
  _OP_REQP = [
2998
    ("output_fields", _TListOf(_TNonEmptyString)),
2999
    ("names", _TListOf(_TNonEmptyString)),
3000
    ]
3001
  REQ_BGL = False
3002
  _FIELDS_STATIC = utils.FieldSet()
3003
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants",
3004
                                   "parameters", "api_versions")
3005

    
3006
  def CheckArguments(self):
3007
    if self.op.names:
3008
      raise errors.OpPrereqError("Selective OS query not supported",
3009
                                 errors.ECODE_INVAL)
3010

    
3011
    _CheckOutputFields(static=self._FIELDS_STATIC,
3012
                       dynamic=self._FIELDS_DYNAMIC,
3013
                       selected=self.op.output_fields)
3014

    
3015
  def ExpandNames(self):
3016
    # Lock all nodes, in shared mode
3017
    # Temporary removal of locks, should be reverted later
3018
    # TODO: reintroduce locks when they are lighter-weight
3019
    self.needed_locks = {}
3020
    #self.share_locks[locking.LEVEL_NODE] = 1
3021
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3022

    
3023
  @staticmethod
3024
  def _DiagnoseByOS(rlist):
3025
    """Remaps a per-node return list into an a per-os per-node dictionary
3026

3027
    @param rlist: a map with node names as keys and OS objects as values
3028

3029
    @rtype: dict
3030
    @return: a dictionary with osnames as keys and as value another
3031
        map, with nodes as keys and tuples of (path, status, diagnose,
3032
        variants, parameters, api_versions) as values, eg::
3033

3034
          {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
3035
                                     (/srv/..., False, "invalid api")],
3036
                           "node2": [(/srv/..., True, "", [], [])]}
3037
          }
3038

3039
    """
3040
    all_os = {}
3041
    # we build here the list of nodes that didn't fail the RPC (at RPC
3042
    # level), so that nodes with a non-responding node daemon don't
3043
    # make all OSes invalid
3044
    good_nodes = [node_name for node_name in rlist
3045
                  if not rlist[node_name].fail_msg]
3046
    for node_name, nr in rlist.items():
3047
      if nr.fail_msg or not nr.payload:
3048
        continue
3049
      for (name, path, status, diagnose, variants,
3050
           params, api_versions) in nr.payload:
3051
        if name not in all_os:
3052
          # build a list of nodes for this os containing empty lists
3053
          # for each node in node_list
3054
          all_os[name] = {}
3055
          for nname in good_nodes:
3056
            all_os[name][nname] = []
3057
        # convert params from [name, help] to (name, help)
3058
        params = [tuple(v) for v in params]
3059
        all_os[name][node_name].append((path, status, diagnose,
3060
                                        variants, params, api_versions))
3061
    return all_os
3062

    
3063
  def Exec(self, feedback_fn):
3064
    """Compute the list of OSes.
3065

3066
    """
3067
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
3068
    node_data = self.rpc.call_os_diagnose(valid_nodes)
3069
    pol = self._DiagnoseByOS(node_data)
3070
    output = []
3071

    
3072
    for os_name, os_data in pol.items():
3073
      row = []
3074
      valid = True
3075
      (variants, params, api_versions) = null_state = (set(), set(), set())
3076
      for idx, osl in enumerate(os_data.values()):
3077
        valid = bool(valid and osl and osl[0][1])
3078
        if not valid:
3079
          (variants, params, api_versions) = null_state
3080
          break
3081
        node_variants, node_params, node_api = osl[0][3:6]
3082
        if idx == 0: # first entry
3083
          variants = set(node_variants)
3084
          params = set(node_params)
3085
          api_versions = set(node_api)
3086
        else: # keep consistency
3087
          variants.intersection_update(node_variants)
3088
          params.intersection_update(node_params)
3089
          api_versions.intersection_update(node_api)
3090

    
3091
      for field in self.op.output_fields:
3092
        if field == "name":
3093
          val = os_name
3094
        elif field == "valid":
3095
          val = valid
3096
        elif field == "node_status":
3097
          # this is just a copy of the dict
3098
          val = {}
3099
          for node_name, nos_list in os_data.items():
3100
            val[node_name] = nos_list
3101
        elif field == "variants":
3102
          val = list(variants)
3103
        elif field == "parameters":
3104
          val = list(params)
3105
        elif field == "api_versions":
3106
          val = list(api_versions)
3107
        else:
3108
          raise errors.ParameterError(field)
3109
        row.append(val)
3110
      output.append(row)
3111

    
3112
    return output
3113

    
3114

    
3115
class LURemoveNode(LogicalUnit):
3116
  """Logical unit for removing a node.
3117

3118
  """
3119
  HPATH = "node-remove"
3120
  HTYPE = constants.HTYPE_NODE
3121
  _OP_REQP = [("node_name", _TNonEmptyString)]
3122

    
3123
  def BuildHooksEnv(self):
3124
    """Build hooks env.
3125

3126
    This doesn't run on the target node in the pre phase as a failed
3127
    node would then be impossible to remove.
3128

3129
    """
3130
    env = {
3131
      "OP_TARGET": self.op.node_name,
3132
      "NODE_NAME": self.op.node_name,
3133
      }
3134
    all_nodes = self.cfg.GetNodeList()
3135
    try:
3136
      all_nodes.remove(self.op.node_name)
3137
    except ValueError:
3138
      logging.warning("Node %s which is about to be removed not found"
3139
                      " in the all nodes list", self.op.node_name)
3140
    return env, all_nodes, all_nodes
3141

    
3142
  def CheckPrereq(self):
3143
    """Check prerequisites.
3144

3145
    This checks:
3146
     - the node exists in the configuration
3147
     - it does not have primary or secondary instances
3148
     - it's not the master
3149

3150
    Any errors are signaled by raising errors.OpPrereqError.
3151

3152
    """
3153
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3154
    node = self.cfg.GetNodeInfo(self.op.node_name)
3155
    assert node is not None
3156

    
3157
    instance_list = self.cfg.GetInstanceList()
3158

    
3159
    masternode = self.cfg.GetMasterNode()
3160
    if node.name == masternode:
3161
      raise errors.OpPrereqError("Node is the master node,"
3162
                                 " you need to failover first.",
3163
                                 errors.ECODE_INVAL)
3164

    
3165
    for instance_name in instance_list:
3166
      instance = self.cfg.GetInstanceInfo(instance_name)
3167
      if node.name in instance.all_nodes:
3168
        raise errors.OpPrereqError("Instance %s is still running on the node,"
3169
                                   " please remove first." % instance_name,
3170
                                   errors.ECODE_INVAL)
3171
    self.op.node_name = node.name
3172
    self.node = node
3173

    
3174
  def Exec(self, feedback_fn):
3175
    """Removes the node from the cluster.
3176

3177
    """
3178
    node = self.node
3179
    logging.info("Stopping the node daemon and removing configs from node %s",
3180
                 node.name)
3181

    
3182
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3183

    
3184
    # Promote nodes to master candidate as needed
3185
    _AdjustCandidatePool(self, exceptions=[node.name])
3186
    self.context.RemoveNode(node.name)
3187

    
3188
    # Run post hooks on the node before it's removed
3189
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
3190
    try:
3191
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
3192
    except:
3193
      # pylint: disable-msg=W0702
3194
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
3195

    
3196
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
3197
    msg = result.fail_msg
3198
    if msg:
3199
      self.LogWarning("Errors encountered on the remote node while leaving"
3200
                      " the cluster: %s", msg)
3201

    
3202
    # Remove node from our /etc/hosts
3203
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3204
      # FIXME: this should be done via an rpc call to node daemon
3205
      utils.RemoveHostFromEtcHosts(node.name)
3206
      _RedistributeAncillaryFiles(self)
3207

    
3208

    
3209
class LUQueryNodes(NoHooksLU):
3210
  """Logical unit for querying nodes.
3211

3212
  """
3213
  # pylint: disable-msg=W0142
3214
  _OP_REQP = [
3215
    ("output_fields", _TListOf(_TNonEmptyString)),
3216
    ("names", _TListOf(_TNonEmptyString)),
3217
    ("use_locking", _TBool),
3218
    ]
3219
  REQ_BGL = False
3220

    
3221
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
3222
                    "master_candidate", "offline", "drained"]
3223

    
3224
  _FIELDS_DYNAMIC = utils.FieldSet(
3225
    "dtotal", "dfree",
3226
    "mtotal", "mnode", "mfree",
3227
    "bootid",
3228
    "ctotal", "cnodes", "csockets",
3229
    )
3230

    
3231
  _FIELDS_STATIC = utils.FieldSet(*[
3232
    "pinst_cnt", "sinst_cnt",
3233
    "pinst_list", "sinst_list",
3234
    "pip", "sip", "tags",
3235
    "master",
3236
    "role"] + _SIMPLE_FIELDS
3237
    )
3238

    
3239
  def CheckArguments(self):
3240
    _CheckOutputFields(static=self._FIELDS_STATIC,
3241
                       dynamic=self._FIELDS_DYNAMIC,
3242
                       selected=self.op.output_fields)
3243

    
3244
  def ExpandNames(self):
3245
    self.needed_locks = {}
3246
    self.share_locks[locking.LEVEL_NODE] = 1
3247

    
3248
    if self.op.names:
3249
      self.wanted = _GetWantedNodes(self, self.op.names)
3250
    else:
3251
      self.wanted = locking.ALL_SET
3252

    
3253
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3254
    self.do_locking = self.do_node_query and self.op.use_locking
3255
    if self.do_locking:
3256
      # if we don't request only static fields, we need to lock the nodes
3257
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
3258

    
3259
  def Exec(self, feedback_fn):
3260
    """Computes the list of nodes and their attributes.
3261

3262
    """
3263
    all_info = self.cfg.GetAllNodesInfo()
3264
    if self.do_locking:
3265
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
3266
    elif self.wanted != locking.ALL_SET:
3267
      nodenames = self.wanted
3268
      missing = set(nodenames).difference(all_info.keys())
3269
      if missing:
3270
        raise errors.OpExecError(
3271
          "Some nodes were removed before retrieving their data: %s" % missing)
3272
    else:
3273
      nodenames = all_info.keys()
3274

    
3275
    nodenames = utils.NiceSort(nodenames)
3276
    nodelist = [all_info[name] for name in nodenames]
3277

    
3278
    # begin data gathering
3279

    
3280
    if self.do_node_query:
3281
      live_data = {}
3282
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
3283
                                          self.cfg.GetHypervisorType())
3284
      for name in nodenames:
3285
        nodeinfo = node_data[name]
3286
        if not nodeinfo.fail_msg and nodeinfo.payload:
3287
          nodeinfo = nodeinfo.payload
3288
          fn = utils.TryConvert
3289
          live_data[name] = {
3290
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
3291
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
3292
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
3293
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
3294
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
3295
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
3296
            "bootid": nodeinfo.get('bootid', None),
3297
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
3298
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
3299
            }
3300
        else:
3301
          live_data[name] = {}
3302
    else:
3303
      live_data = dict.fromkeys(nodenames, {})
3304

    
3305
    node_to_primary = dict([(name, set()) for name in nodenames])
3306
    node_to_secondary = dict([(name, set()) for name in nodenames])
3307

    
3308
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
3309
                             "sinst_cnt", "sinst_list"))
3310
    if inst_fields & frozenset(self.op.output_fields):
3311
      inst_data = self.cfg.GetAllInstancesInfo()
3312

    
3313
      for inst in inst_data.values():
3314
        if inst.primary_node in node_to_primary:
3315
          node_to_primary[inst.primary_node].add(inst.name)
3316
        for secnode in inst.secondary_nodes:
3317
          if secnode in node_to_secondary:
3318
            node_to_secondary[secnode].add(inst.name)
3319

    
3320
    master_node = self.cfg.GetMasterNode()
3321

    
3322
    # end data gathering
3323

    
3324
    output = []
3325
    for node in nodelist:
3326
      node_output = []
3327
      for field in self.op.output_fields:
3328
        if field in self._SIMPLE_FIELDS:
3329
          val = getattr(node, field)
3330
        elif field == "pinst_list":
3331
          val = list(node_to_primary[node.name])
3332
        elif field == "sinst_list":
3333
          val = list(node_to_secondary[node.name])
3334
        elif field == "pinst_cnt":
3335
          val = len(node_to_primary[node.name])
3336
        elif field == "sinst_cnt":
3337
          val = len(node_to_secondary[node.name])
3338
        elif field == "pip":
3339
          val = node.primary_ip
3340
        elif field == "sip":
3341
          val = node.secondary_ip
3342
        elif field == "tags":
3343
          val = list(node.GetTags())
3344
        elif field == "master":
3345
          val = node.name == master_node
3346
        elif self._FIELDS_DYNAMIC.Matches(field):
3347
          val = live_data[node.name].get(field, None)
3348
        elif field == "role":
3349
          if node.name == master_node:
3350
            val = "M"
3351
          elif node.master_candidate:
3352
            val = "C"
3353
          elif node.drained:
3354
            val = "D"
3355
          elif node.offline:
3356
            val = "O"
3357
          else:
3358
            val = "R"
3359
        else:
3360
          raise errors.ParameterError(field)
3361
        node_output.append(val)
3362
      output.append(node_output)
3363

    
3364
    return output
3365

    
3366

    
3367
class LUQueryNodeVolumes(NoHooksLU):
3368
  """Logical unit for getting volumes on node(s).
3369

3370
  """
3371
  _OP_REQP = [
3372
    ("nodes", _TListOf(_TNonEmptyString)),
3373
    ("output_fields", _TListOf(_TNonEmptyString)),
3374
    ]
3375
  REQ_BGL = False
3376
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3377
  _FIELDS_STATIC = utils.FieldSet("node")
3378

    
3379
  def CheckArguments(self):
3380
    _CheckOutputFields(static=self._FIELDS_STATIC,
3381
                       dynamic=self._FIELDS_DYNAMIC,
3382
                       selected=self.op.output_fields)
3383

    
3384
  def ExpandNames(self):
3385
    self.needed_locks = {}
3386
    self.share_locks[locking.LEVEL_NODE] = 1
3387
    if not self.op.nodes:
3388
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3389
    else:
3390
      self.needed_locks[locking.LEVEL_NODE] = \
3391
        _GetWantedNodes(self, self.op.nodes)
3392

    
3393
  def Exec(self, feedback_fn):
3394
    """Computes the list of nodes and their attributes.
3395

3396
    """
3397
    nodenames = self.acquired_locks[locking.LEVEL_NODE]
3398
    volumes = self.rpc.call_node_volumes(nodenames)
3399

    
3400
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3401
             in self.cfg.GetInstanceList()]
3402

    
3403
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3404

    
3405
    output = []
3406
    for node in nodenames:
3407
      nresult = volumes[node]
3408
      if nresult.offline:
3409
        continue
3410
      msg = nresult.fail_msg
3411
      if msg:
3412
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3413
        continue
3414

    
3415
      node_vols = nresult.payload[:]
3416
      node_vols.sort(key=lambda vol: vol['dev'])
3417

    
3418
      for vol in node_vols:
3419
        node_output = []
3420
        for field in self.op.output_fields:
3421
          if field == "node":
3422
            val = node
3423
          elif field == "phys":
3424
            val = vol['dev']
3425
          elif field == "vg":
3426
            val = vol['vg']
3427
          elif field == "name":
3428
            val = vol['name']
3429
          elif field == "size":
3430
            val = int(float(vol['size']))
3431
          elif field == "instance":
3432
            for inst in ilist:
3433
              if node not in lv_by_node[inst]:
3434
                continue
3435
              if vol['name'] in lv_by_node[inst][node]:
3436
                val = inst.name
3437
                break
3438
            else:
3439
              val = '-'
3440
          else:
3441
            raise errors.ParameterError(field)
3442
          node_output.append(str(val))
3443

    
3444
        output.append(node_output)
3445

    
3446
    return output
3447

    
3448

    
3449
class LUQueryNodeStorage(NoHooksLU):
3450
  """Logical unit for getting information on storage units on node(s).
3451

3452
  """
3453
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3454
  _OP_REQP = [
3455
    ("nodes", _TListOf(_TNonEmptyString)),
3456
    ("storage_type", _CheckStorageType),
3457
    ("output_fields", _TListOf(_TNonEmptyString)),
3458
    ]
3459
  _OP_DEFS = [("name", None)]
3460
  REQ_BGL = False
3461

    
3462
  def CheckArguments(self):
3463
    _CheckOutputFields(static=self._FIELDS_STATIC,
3464
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3465
                       selected=self.op.output_fields)
3466

    
3467
  def ExpandNames(self):
3468
    self.needed_locks = {}
3469
    self.share_locks[locking.LEVEL_NODE] = 1
3470

    
3471
    if self.op.nodes:
3472
      self.needed_locks[locking.LEVEL_NODE] = \
3473
        _GetWantedNodes(self, self.op.nodes)
3474
    else:
3475
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3476

    
3477
  def Exec(self, feedback_fn):
3478
    """Computes the list of nodes and their attributes.
3479

3480
    """
3481
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3482

    
3483
    # Always get name to sort by
3484
    if constants.SF_NAME in self.op.output_fields:
3485
      fields = self.op.output_fields[:]
3486
    else:
3487
      fields = [constants.SF_NAME] + self.op.output_fields
3488

    
3489
    # Never ask for node or type as it's only known to the LU
3490
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3491
      while extra in fields:
3492
        fields.remove(extra)
3493

    
3494
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3495
    name_idx = field_idx[constants.SF_NAME]
3496

    
3497
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3498
    data = self.rpc.call_storage_list(self.nodes,
3499
                                      self.op.storage_type, st_args,
3500
                                      self.op.name, fields)
3501

    
3502
    result = []
3503

    
3504
    for node in utils.NiceSort(self.nodes):
3505
      nresult = data[node]
3506
      if nresult.offline:
3507
        continue
3508

    
3509
      msg = nresult.fail_msg
3510
      if msg:
3511
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3512
        continue
3513

    
3514
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3515

    
3516
      for name in utils.NiceSort(rows.keys()):
3517
        row = rows[name]
3518

    
3519
        out = []
3520

    
3521
        for field in self.op.output_fields:
3522
          if field == constants.SF_NODE:
3523
            val = node
3524
          elif field == constants.SF_TYPE:
3525
            val = self.op.storage_type
3526
          elif field in field_idx:
3527
            val = row[field_idx[field]]
3528
          else:
3529
            raise errors.ParameterError(field)
3530

    
3531
          out.append(val)
3532

    
3533
        result.append(out)
3534

    
3535
    return result
3536

    
3537

    
3538
class LUModifyNodeStorage(NoHooksLU):
3539
  """Logical unit for modifying a storage volume on a node.
3540

3541
  """
3542
  _OP_REQP = [
3543
    ("node_name", _TNonEmptyString),
3544
    ("storage_type", _CheckStorageType),
3545
    ("name", _TNonEmptyString),
3546
    ("changes", _TDict),
3547
    ]
3548
  REQ_BGL = False
3549

    
3550
  def CheckArguments(self):
3551
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3552

    
3553
    storage_type = self.op.storage_type
3554

    
3555
    try:
3556
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3557
    except KeyError:
3558
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3559
                                 " modified" % storage_type,
3560
                                 errors.ECODE_INVAL)
3561

    
3562
    diff = set(self.op.changes.keys()) - modifiable
3563
    if diff:
3564
      raise errors.OpPrereqError("The following fields can not be modified for"
3565
                                 " storage units of type '%s': %r" %
3566
                                 (storage_type, list(diff)),
3567
                                 errors.ECODE_INVAL)
3568

    
3569
  def ExpandNames(self):
3570
    self.needed_locks = {
3571
      locking.LEVEL_NODE: self.op.node_name,
3572
      }
3573

    
3574
  def Exec(self, feedback_fn):
3575
    """Computes the list of nodes and their attributes.
3576

3577
    """
3578
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3579
    result = self.rpc.call_storage_modify(self.op.node_name,
3580
                                          self.op.storage_type, st_args,
3581
                                          self.op.name, self.op.changes)
3582
    result.Raise("Failed to modify storage unit '%s' on %s" %
3583
                 (self.op.name, self.op.node_name))
3584

    
3585

    
3586
class LUAddNode(LogicalUnit):
3587
  """Logical unit for adding node to the cluster.
3588

3589
  """
3590
  HPATH = "node-add"
3591
  HTYPE = constants.HTYPE_NODE
3592
  _OP_REQP = [
3593
    ("node_name", _TNonEmptyString),
3594
    ]
3595
  _OP_DEFS = [("secondary_ip", None)]
3596

    
3597
  def CheckArguments(self):
3598
    # validate/normalize the node name
3599
    self.op.node_name = utils.HostInfo.NormalizeName(self.op.node_name)
3600

    
3601
  def BuildHooksEnv(self):
3602
    """Build hooks env.
3603

3604
    This will run on all nodes before, and on all nodes + the new node after.
3605

3606
    """
3607
    env = {
3608
      "OP_TARGET": self.op.node_name,
3609
      "NODE_NAME": self.op.node_name,
3610
      "NODE_PIP": self.op.primary_ip,
3611
      "NODE_SIP": self.op.secondary_ip,
3612
      }
3613
    nodes_0 = self.cfg.GetNodeList()
3614
    nodes_1 = nodes_0 + [self.op.node_name, ]
3615
    return env, nodes_0, nodes_1
3616

    
3617
  def CheckPrereq(self):
3618
    """Check prerequisites.
3619

3620
    This checks:
3621
     - the new node is not already in the config
3622
     - it is resolvable
3623
     - its parameters (single/dual homed) matches the cluster
3624

3625
    Any errors are signaled by raising errors.OpPrereqError.
3626

3627
    """
3628
    node_name = self.op.node_name
3629
    cfg = self.cfg
3630

    
3631
    dns_data = utils.GetHostInfo(node_name)
3632

    
3633
    node = dns_data.name
3634
    primary_ip = self.op.primary_ip = dns_data.ip
3635
    if self.op.secondary_ip is None:
3636
      self.op.secondary_ip = primary_ip
3637
    if not utils.IsValidIP4(self.op.secondary_ip):
3638
      raise errors.OpPrereqError("Invalid secondary IP given",
3639
                                 errors.ECODE_INVAL)
3640
    secondary_ip = self.op.secondary_ip
3641

    
3642
    node_list = cfg.GetNodeList()
3643
    if not self.op.readd and node in node_list:
3644
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3645
                                 node, errors.ECODE_EXISTS)
3646
    elif self.op.readd and node not in node_list:
3647
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3648
                                 errors.ECODE_NOENT)
3649

    
3650
    self.changed_primary_ip = False
3651

    
3652
    for existing_node_name in node_list:
3653
      existing_node = cfg.GetNodeInfo(existing_node_name)
3654

    
3655
      if self.op.readd and node == existing_node_name:
3656
        if existing_node.secondary_ip != secondary_ip:
3657
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3658
                                     " address configuration as before",
3659
                                     errors.ECODE_INVAL)
3660
        if existing_node.primary_ip != primary_ip:
3661
          self.changed_primary_ip = True
3662

    
3663
        continue
3664

    
3665
      if (existing_node.primary_ip == primary_ip or
3666
          existing_node.secondary_ip == primary_ip or
3667
          existing_node.primary_ip == secondary_ip or
3668
          existing_node.secondary_ip == secondary_ip):
3669
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3670
                                   " existing node %s" % existing_node.name,
3671
                                   errors.ECODE_NOTUNIQUE)
3672

    
3673
    # check that the type of the node (single versus dual homed) is the
3674
    # same as for the master
3675
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
3676
    master_singlehomed = myself.secondary_ip == myself.primary_ip
3677
    newbie_singlehomed = secondary_ip == primary_ip
3678
    if master_singlehomed != newbie_singlehomed:
3679
      if master_singlehomed:
3680
        raise errors.OpPrereqError("The master has no private ip but the"
3681
                                   " new node has one",
3682
                                   errors.ECODE_INVAL)
3683
      else:
3684
        raise errors.OpPrereqError("The master has a private ip but the"
3685
                                   " new node doesn't have one",
3686
                                   errors.ECODE_INVAL)
3687

    
3688
    # checks reachability
3689
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3690
      raise errors.OpPrereqError("Node not reachable by ping",
3691
                                 errors.ECODE_ENVIRON)
3692

    
3693
    if not newbie_singlehomed:
3694
      # check reachability from my secondary ip to newbie's secondary ip
3695
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3696
                           source=myself.secondary_ip):
3697
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3698
                                   " based ping to noded port",
3699
                                   errors.ECODE_ENVIRON)
3700

    
3701
    if self.op.readd:
3702
      exceptions = [node]
3703
    else:
3704
      exceptions = []
3705

    
3706
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3707

    
3708
    if self.op.readd:
3709
      self.new_node = self.cfg.GetNodeInfo(node)
3710
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3711
    else:
3712
      self.new_node = objects.Node(name=node,
3713
                                   primary_ip=primary_ip,
3714
                                   secondary_ip=secondary_ip,
3715
                                   master_candidate=self.master_candidate,
3716
                                   offline=False, drained=False)
3717

    
3718
  def Exec(self, feedback_fn):
3719
    """Adds the new node to the cluster.
3720

3721
    """
3722
    new_node = self.new_node
3723
    node = new_node.name
3724

    
3725
    # for re-adds, reset the offline/drained/master-candidate flags;
3726
    # we need to reset here, otherwise offline would prevent RPC calls
3727
    # later in the procedure; this also means that if the re-add
3728
    # fails, we are left with a non-offlined, broken node
3729
    if self.op.readd:
3730
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
3731
      self.LogInfo("Readding a node, the offline/drained flags were reset")
3732
      # if we demote the node, we do cleanup later in the procedure
3733
      new_node.master_candidate = self.master_candidate
3734
      if self.changed_primary_ip:
3735
        new_node.primary_ip = self.op.primary_ip
3736

    
3737
    # notify the user about any possible mc promotion
3738
    if new_node.master_candidate:
3739
      self.LogInfo("Node will be a master candidate")
3740

    
3741
    # check connectivity
3742
    result = self.rpc.call_version([node])[node]
3743
    result.Raise("Can't get version information from node %s" % node)
3744
    if constants.PROTOCOL_VERSION == result.payload:
3745
      logging.info("Communication to node %s fine, sw version %s match",
3746
                   node, result.payload)
3747
    else:
3748
      raise errors.OpExecError("Version mismatch master version %s,"
3749
                               " node version %s" %
3750
                               (constants.PROTOCOL_VERSION, result.payload))
3751

    
3752
    # setup ssh on node
3753
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3754
      logging.info("Copy ssh key to node %s", node)
3755
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3756
      keyarray = []
3757
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3758
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3759
                  priv_key, pub_key]
3760

    
3761
      for i in keyfiles:
3762
        keyarray.append(utils.ReadFile(i))
3763

    
3764
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3765
                                      keyarray[2], keyarray[3], keyarray[4],
3766
                                      keyarray[5])
3767
      result.Raise("Cannot transfer ssh keys to the new node")
3768

    
3769
    # Add node to our /etc/hosts, and add key to known_hosts
3770
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3771
      # FIXME: this should be done via an rpc call to node daemon
3772
      utils.AddHostToEtcHosts(new_node.name)
3773

    
3774
    if new_node.secondary_ip != new_node.primary_ip:
3775
      result = self.rpc.call_node_has_ip_address(new_node.name,
3776
                                                 new_node.secondary_ip)
3777
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3778
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3779
      if not result.payload:
3780
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3781
                                 " you gave (%s). Please fix and re-run this"
3782
                                 " command." % new_node.secondary_ip)
3783

    
3784
    node_verify_list = [self.cfg.GetMasterNode()]
3785
    node_verify_param = {
3786
      constants.NV_NODELIST: [node],
3787
      # TODO: do a node-net-test as well?
3788
    }
3789

    
3790
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3791
                                       self.cfg.GetClusterName())
3792
    for verifier in node_verify_list:
3793
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3794
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3795
      if nl_payload:
3796
        for failed in nl_payload:
3797
          feedback_fn("ssh/hostname verification failed"
3798
                      " (checking from %s): %s" %
3799
                      (verifier, nl_payload[failed]))
3800
        raise errors.OpExecError("ssh/hostname verification failed.")
3801

    
3802
    if self.op.readd:
3803
      _RedistributeAncillaryFiles(self)
3804
      self.context.ReaddNode(new_node)
3805
      # make sure we redistribute the config
3806
      self.cfg.Update(new_node, feedback_fn)
3807
      # and make sure the new node will not have old files around
3808
      if not new_node.master_candidate:
3809
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3810
        msg = result.fail_msg
3811
        if msg:
3812
          self.LogWarning("Node failed to demote itself from master"
3813
                          " candidate status: %s" % msg)
3814
    else:
3815
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3816
      self.context.AddNode(new_node, self.proc.GetECId())
3817

    
3818

    
3819
class LUSetNodeParams(LogicalUnit):
3820
  """Modifies the parameters of a node.
3821

3822
  """
3823
  HPATH = "node-modify"
3824
  HTYPE = constants.HTYPE_NODE
3825
  _OP_REQP = [("node_name", _TNonEmptyString)]
3826
  REQ_BGL = False
3827

    
3828
  def CheckArguments(self):
3829
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3830
    _CheckBooleanOpField(self.op, 'master_candidate')
3831
    _CheckBooleanOpField(self.op, 'offline')
3832
    _CheckBooleanOpField(self.op, 'drained')
3833
    _CheckBooleanOpField(self.op, 'auto_promote')
3834
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3835
    if all_mods.count(None) == 3:
3836
      raise errors.OpPrereqError("Please pass at least one modification",
3837
                                 errors.ECODE_INVAL)
3838
    if all_mods.count(True) > 1:
3839
      raise errors.OpPrereqError("Can't set the node into more than one"
3840
                                 " state at the same time",
3841
                                 errors.ECODE_INVAL)
3842

    
3843
    # Boolean value that tells us whether we're offlining or draining the node
3844
    self.offline_or_drain = (self.op.offline == True or
3845
                             self.op.drained == True)
3846
    self.deoffline_or_drain = (self.op.offline == False or
3847
                               self.op.drained == False)
3848
    self.might_demote = (self.op.master_candidate == False or
3849
                         self.offline_or_drain)
3850

    
3851
    self.lock_all = self.op.auto_promote and self.might_demote
3852

    
3853

    
3854
  def ExpandNames(self):
3855
    if self.lock_all:
3856
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3857
    else:
3858
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3859

    
3860
  def BuildHooksEnv(self):
3861
    """Build hooks env.
3862

3863
    This runs on the master node.
3864

3865
    """
3866
    env = {
3867
      "OP_TARGET": self.op.node_name,
3868
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3869
      "OFFLINE": str(self.op.offline),
3870
      "DRAINED": str(self.op.drained),
3871
      }
3872
    nl = [self.cfg.GetMasterNode(),
3873
          self.op.node_name]
3874
    return env, nl, nl
3875

    
3876
  def CheckPrereq(self):
3877
    """Check prerequisites.
3878

3879
    This only checks the instance list against the existing names.
3880

3881
    """
3882
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3883

    
3884
    if (self.op.master_candidate is not None or
3885
        self.op.drained is not None or
3886
        self.op.offline is not None):
3887
      # we can't change the master's node flags
3888
      if self.op.node_name == self.cfg.GetMasterNode():
3889
        raise errors.OpPrereqError("The master role can be changed"
3890
                                   " only via masterfailover",
3891
                                   errors.ECODE_INVAL)
3892

    
3893

    
3894
    if node.master_candidate and self.might_demote and not self.lock_all:
3895
      assert not self.op.auto_promote, "auto-promote set but lock_all not"
3896
      # check if after removing the current node, we're missing master
3897
      # candidates
3898
      (mc_remaining, mc_should, _) = \
3899
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
3900
      if mc_remaining < mc_should:
3901
        raise errors.OpPrereqError("Not enough master candidates, please"
3902
                                   " pass auto_promote to allow promotion",
3903
                                   errors.ECODE_INVAL)
3904

    
3905
    if (self.op.master_candidate == True and
3906
        ((node.offline and not self.op.offline == False) or
3907
         (node.drained and not self.op.drained == False))):
3908
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3909
                                 " to master_candidate" % node.name,
3910
                                 errors.ECODE_INVAL)
3911

    
3912
    # If we're being deofflined/drained, we'll MC ourself if needed
3913
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3914
        self.op.master_candidate == True and not node.master_candidate):
3915
      self.op.master_candidate = _DecideSelfPromotion(self)
3916
      if self.op.master_candidate:
3917
        self.LogInfo("Autopromoting node to master candidate")
3918

    
3919
    return
3920

    
3921
  def Exec(self, feedback_fn):
3922
    """Modifies a node.
3923

3924
    """
3925
    node = self.node
3926

    
3927
    result = []
3928
    changed_mc = False
3929

    
3930
    if self.op.offline is not None:
3931
      node.offline = self.op.offline
3932
      result.append(("offline", str(self.op.offline)))
3933
      if self.op.offline == True:
3934
        if node.master_candidate:
3935
          node.master_candidate = False
3936
          changed_mc = True
3937
          result.append(("master_candidate", "auto-demotion due to offline"))
3938
        if node.drained:
3939
          node.drained = False
3940
          result.append(("drained", "clear drained status due to offline"))
3941

    
3942
    if self.op.master_candidate is not None:
3943
      node.master_candidate = self.op.master_candidate
3944
      changed_mc = True
3945
      result.append(("master_candidate", str(self.op.master_candidate)))
3946
      if self.op.master_candidate == False:
3947
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3948
        msg = rrc.fail_msg
3949
        if msg:
3950
          self.LogWarning("Node failed to demote itself: %s" % msg)
3951

    
3952
    if self.op.drained is not None:
3953
      node.drained = self.op.drained
3954
      result.append(("drained", str(self.op.drained)))
3955
      if self.op.drained == True:
3956
        if node.master_candidate:
3957
          node.master_candidate = False
3958
          changed_mc = True
3959
          result.append(("master_candidate", "auto-demotion due to drain"))
3960
          rrc = self.rpc.call_node_demote_from_mc(node.name)
3961
          msg = rrc.fail_msg
3962
          if msg:
3963
            self.LogWarning("Node failed to demote itself: %s" % msg)
3964
        if node.offline:
3965
          node.offline = False
3966
          result.append(("offline", "clear offline status due to drain"))
3967

    
3968
    # we locked all nodes, we adjust the CP before updating this node
3969
    if self.lock_all:
3970
      _AdjustCandidatePool(self, [node.name])
3971

    
3972
    # this will trigger configuration file update, if needed
3973
    self.cfg.Update(node, feedback_fn)
3974

    
3975
    # this will trigger job queue propagation or cleanup
3976
    if changed_mc:
3977
      self.context.ReaddNode(node)
3978

    
3979
    return result
3980

    
3981

    
3982
class LUPowercycleNode(NoHooksLU):
3983
  """Powercycles a node.
3984

3985
  """
3986
  _OP_REQP = [
3987
    ("node_name", _TNonEmptyString),
3988
    ("force", _TBool),
3989
    ]
3990
  REQ_BGL = False
3991

    
3992
  def CheckArguments(self):
3993
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3994
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
3995
      raise errors.OpPrereqError("The node is the master and the force"
3996
                                 " parameter was not set",
3997
                                 errors.ECODE_INVAL)
3998

    
3999
  def ExpandNames(self):
4000
    """Locking for PowercycleNode.
4001

4002
    This is a last-resort option and shouldn't block on other
4003
    jobs. Therefore, we grab no locks.
4004

4005
    """
4006
    self.needed_locks = {}
4007

    
4008
  def Exec(self, feedback_fn):
4009
    """Reboots a node.
4010

4011
    """
4012
    result = self.rpc.call_node_powercycle(self.op.node_name,
4013
                                           self.cfg.GetHypervisorType())
4014
    result.Raise("Failed to schedule the reboot")
4015
    return result.payload
4016

    
4017

    
4018
class LUQueryClusterInfo(NoHooksLU):
4019
  """Query cluster configuration.
4020

4021
  """
4022
  _OP_REQP = []
4023
  REQ_BGL = False
4024

    
4025
  def ExpandNames(self):
4026
    self.needed_locks = {}
4027

    
4028
  def Exec(self, feedback_fn):
4029
    """Return cluster config.
4030

4031
    """
4032
    cluster = self.cfg.GetClusterInfo()
4033
    os_hvp = {}
4034

    
4035
    # Filter just for enabled hypervisors
4036
    for os_name, hv_dict in cluster.os_hvp.items():
4037
      os_hvp[os_name] = {}
4038
      for hv_name, hv_params in hv_dict.items():
4039
        if hv_name in cluster.enabled_hypervisors:
4040
          os_hvp[os_name][hv_name] = hv_params
4041

    
4042
    result = {
4043
      "software_version": constants.RELEASE_VERSION,
4044
      "protocol_version": constants.PROTOCOL_VERSION,
4045
      "config_version": constants.CONFIG_VERSION,
4046
      "os_api_version": max(constants.OS_API_VERSIONS),
4047
      "export_version": constants.EXPORT_VERSION,
4048
      "architecture": (platform.architecture()[0], platform.machine()),
4049
      "name": cluster.cluster_name,
4050
      "master": cluster.master_node,
4051
      "default_hypervisor": cluster.enabled_hypervisors[0],
4052
      "enabled_hypervisors": cluster.enabled_hypervisors,
4053
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
4054
                        for hypervisor_name in cluster.enabled_hypervisors]),
4055
      "os_hvp": os_hvp,
4056
      "beparams": cluster.beparams,
4057
      "osparams": cluster.osparams,
4058
      "nicparams": cluster.nicparams,
4059
      "candidate_pool_size": cluster.candidate_pool_size,
4060
      "master_netdev": cluster.master_netdev,
4061
      "volume_group_name": cluster.volume_group_name,
4062
      "file_storage_dir": cluster.file_storage_dir,
4063
      "maintain_node_health": cluster.maintain_node_health,
4064
      "ctime": cluster.ctime,
4065
      "mtime": cluster.mtime,
4066
      "uuid": cluster.uuid,
4067
      "tags": list(cluster.GetTags()),
4068
      "uid_pool": cluster.uid_pool,
4069
      }
4070

    <