Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 999b183c

History | View | Annotate | Download (364.1 kB)

1
#
2
#
3

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

    
21

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

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

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

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

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

    
43
from ganeti import ssh
44
from ganeti import utils
45
from ganeti import errors
46
from ganeti import hypervisor
47
from ganeti import locking
48
from ganeti import constants
49
from ganeti import objects
50
from ganeti import serializer
51
from ganeti import ssconf
52
from ganeti import uidpool
53
from ganeti import compat
54
from ganeti import masterd
55
from ganeti import netutils
56

    
57
import ganeti.masterd.instance # pylint: disable-msg=W0611
58

    
59

    
60
# Modifiable default values; need to define these here before the
61
# actual LUs
62

    
63
def _EmptyList():
64
  """Returns an empty list.
65

66
  """
67
  return []
68

    
69

    
70
def _EmptyDict():
71
  """Returns an empty dict.
72

73
  """
74
  return {}
75

    
76

    
77
#: The without-default default value
78
_NoDefault = object()
79

    
80

    
81
#: The no-type (value to complex to check it in the type system)
82
_NoType = object()
83

    
84

    
85
# Some basic types
86
def _TNotNone(val):
87
  """Checks if the given value is not None.
88

89
  """
90
  return val is not None
91

    
92

    
93
def _TNone(val):
94
  """Checks if the given value is None.
95

96
  """
97
  return val is None
98

    
99

    
100
def _TBool(val):
101
  """Checks if the given value is a boolean.
102

103
  """
104
  return isinstance(val, bool)
105

    
106

    
107
def _TInt(val):
108
  """Checks if the given value is an integer.
109

110
  """
111
  return isinstance(val, int)
112

    
113

    
114
def _TFloat(val):
115
  """Checks if the given value is a float.
116

117
  """
118
  return isinstance(val, float)
119

    
120

    
121
def _TString(val):
122
  """Checks if the given value is a string.
123

124
  """
125
  return isinstance(val, basestring)
126

    
127

    
128
def _TTrue(val):
129
  """Checks if a given value evaluates to a boolean True value.
130

131
  """
132
  return bool(val)
133

    
134

    
135
def _TElemOf(target_list):
136
  """Builds a function that checks if a given value is a member of a list.
137

138
  """
139
  return lambda val: val in target_list
140

    
141

    
142
# Container types
143
def _TList(val):
144
  """Checks if the given value is a list.
145

146
  """
147
  return isinstance(val, list)
148

    
149

    
150
def _TDict(val):
151
  """Checks if the given value is a dictionary.
152

153
  """
154
  return isinstance(val, dict)
155

    
156

    
157
# Combinator types
158
def _TAnd(*args):
159
  """Combine multiple functions using an AND operation.
160

161
  """
162
  def fn(val):
163
    return compat.all(t(val) for t in args)
164
  return fn
165

    
166

    
167
def _TOr(*args):
168
  """Combine multiple functions using an AND operation.
169

170
  """
171
  def fn(val):
172
    return compat.any(t(val) for t in args)
173
  return fn
174

    
175

    
176
# Type aliases
177

    
178
#: a non-empty string
179
_TNonEmptyString = _TAnd(_TString, _TTrue)
180

    
181

    
182
#: a maybe non-empty string
183
_TMaybeString = _TOr(_TNonEmptyString, _TNone)
184

    
185

    
186
#: a maybe boolean (bool or none)
187
_TMaybeBool = _TOr(_TBool, _TNone)
188

    
189

    
190
#: a positive integer
191
_TPositiveInt = _TAnd(_TInt, lambda v: v >= 0)
192

    
193
#: a strictly positive integer
194
_TStrictPositiveInt = _TAnd(_TInt, lambda v: v > 0)
195

    
196

    
197
def _TListOf(my_type):
198
  """Checks if a given value is a list with all elements of the same type.
199

200
  """
201
  return _TAnd(_TList,
202
               lambda lst: compat.all(my_type(v) for v in lst))
203

    
204

    
205
def _TDictOf(key_type, val_type):
206
  """Checks a dict type for the type of its key/values.
207

208
  """
209
  return _TAnd(_TDict,
210
               lambda my_dict: (compat.all(key_type(v) for v in my_dict.keys())
211
                                and compat.all(val_type(v)
212
                                               for v in my_dict.values())))
213

    
214

    
215
# Common opcode attributes
216

    
217
#: output fields for a query operation
218
_POutputFields = ("output_fields", _NoDefault, _TListOf(_TNonEmptyString))
219

    
220

    
221
#: the shutdown timeout
222
_PShutdownTimeout = ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
223
                     _TPositiveInt)
224

    
225
#: the force parameter
226
_PForce = ("force", False, _TBool)
227

    
228
#: a required instance name (for single-instance LUs)
229
_PInstanceName = ("instance_name", _NoDefault, _TNonEmptyString)
230

    
231

    
232
#: a required node name (for single-node LUs)
233
_PNodeName = ("node_name", _NoDefault, _TNonEmptyString)
234

    
235
#: the migration type (live/non-live)
236
_PMigrationMode = ("mode", None, _TOr(_TNone,
237
                                      _TElemOf(constants.HT_MIGRATION_MODES)))
238

    
239

    
240
# End types
241
class LogicalUnit(object):
242
  """Logical Unit base class.
243

244
  Subclasses must follow these rules:
245
    - implement ExpandNames
246
    - implement CheckPrereq (except when tasklets are used)
247
    - implement Exec (except when tasklets are used)
248
    - implement BuildHooksEnv
249
    - redefine HPATH and HTYPE
250
    - optionally redefine their run requirements:
251
        REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
252

253
  Note that all commands require root permissions.
254

255
  @ivar dry_run_result: the value (if any) that will be returned to the caller
256
      in dry-run mode (signalled by opcode dry_run parameter)
257
  @cvar _OP_PARAMS: a list of opcode attributes, their defaults values
258
      they should get if not already defined, and types they must match
259

260
  """
261
  HPATH = None
262
  HTYPE = None
263
  _OP_PARAMS = []
264
  REQ_BGL = True
265

    
266
  def __init__(self, processor, op, context, rpc):
267
    """Constructor for LogicalUnit.
268

269
    This needs to be overridden in derived classes in order to check op
270
    validity.
271

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

    
299
    # Tasklets
300
    self.tasklets = None
301

    
302
    # The new kind-of-type-system
303
    op_id = self.op.OP_ID
304
    for attr_name, aval, test in self._OP_PARAMS:
305
      if not hasattr(op, attr_name):
306
        if aval == _NoDefault:
307
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
308
                                     (op_id, attr_name), errors.ECODE_INVAL)
309
        else:
310
          if callable(aval):
311
            dval = aval()
312
          else:
313
            dval = aval
314
          setattr(self.op, attr_name, dval)
315
      attr_val = getattr(op, attr_name)
316
      if test == _NoType:
317
        # no tests here
318
        continue
319
      if not callable(test):
320
        raise errors.ProgrammerError("Validation for parameter '%s.%s' failed,"
321
                                     " given type is not a proper type (%s)" %
322
                                     (op_id, attr_name, test))
323
      if not test(attr_val):
324
        logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
325
                      self.op.OP_ID, attr_name, type(attr_val), attr_val)
326
        raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
327
                                   (op_id, attr_name), errors.ECODE_INVAL)
328

    
329
    self.CheckArguments()
330

    
331
  def __GetSSH(self):
332
    """Returns the SshRunner object
333

334
    """
335
    if not self.__ssh:
336
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
337
    return self.__ssh
338

    
339
  ssh = property(fget=__GetSSH)
340

    
341
  def CheckArguments(self):
342
    """Check syntactic validity for the opcode arguments.
343

344
    This method is for doing a simple syntactic check and ensure
345
    validity of opcode parameters, without any cluster-related
346
    checks. While the same can be accomplished in ExpandNames and/or
347
    CheckPrereq, doing these separate is better because:
348

349
      - ExpandNames is left as as purely a lock-related function
350
      - CheckPrereq is run after we have acquired locks (and possible
351
        waited for them)
352

353
    The function is allowed to change the self.op attribute so that
354
    later methods can no longer worry about missing parameters.
355

356
    """
357
    pass
358

    
359
  def ExpandNames(self):
360
    """Expand names for this LU.
361

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

367
    LUs which implement this method must also populate the self.needed_locks
368
    member, as a dict with lock levels as keys, and a list of needed lock names
369
    as values. Rules:
370

371
      - use an empty dict if you don't need any lock
372
      - if you don't need any lock at a particular level omit that level
373
      - don't put anything for the BGL level
374
      - if you want all locks at a level use locking.ALL_SET as a value
375

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

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

384
    Examples::
385

386
      # Acquire all nodes and one instance
387
      self.needed_locks = {
388
        locking.LEVEL_NODE: locking.ALL_SET,
389
        locking.LEVEL_INSTANCE: ['instance1.example.com'],
390
      }
391
      # Acquire just two nodes
392
      self.needed_locks = {
393
        locking.LEVEL_NODE: ['node1.example.com', 'node2.example.com'],
394
      }
395
      # Acquire no locks
396
      self.needed_locks = {} # No, you can't leave it to the default value None
397

398
    """
399
    # The implementation of this method is mandatory only if the new LU is
400
    # concurrent, so that old LUs don't need to be changed all at the same
401
    # time.
402
    if self.REQ_BGL:
403
      self.needed_locks = {} # Exclusive LUs don't need locks.
404
    else:
405
      raise NotImplementedError
406

    
407
  def DeclareLocks(self, level):
408
    """Declare LU locking needs for a level
409

410
    While most LUs can just declare their locking needs at ExpandNames time,
411
    sometimes there's the need to calculate some locks after having acquired
412
    the ones before. This function is called just before acquiring locks at a
413
    particular level, but after acquiring the ones at lower levels, and permits
414
    such calculations. It can be used to modify self.needed_locks, and by
415
    default it does nothing.
416

417
    This function is only called if you have something already set in
418
    self.needed_locks for the level.
419

420
    @param level: Locking level which is going to be locked
421
    @type level: member of ganeti.locking.LEVELS
422

423
    """
424

    
425
  def CheckPrereq(self):
426
    """Check prerequisites for this LU.
427

428
    This method should check that the prerequisites for the execution
429
    of this LU are fulfilled. It can do internode communication, but
430
    it should be idempotent - no cluster or system changes are
431
    allowed.
432

433
    The method should raise errors.OpPrereqError in case something is
434
    not fulfilled. Its return value is ignored.
435

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

439
    """
440
    if self.tasklets is not None:
441
      for (idx, tl) in enumerate(self.tasklets):
442
        logging.debug("Checking prerequisites for tasklet %s/%s",
443
                      idx + 1, len(self.tasklets))
444
        tl.CheckPrereq()
445
    else:
446
      pass
447

    
448
  def Exec(self, feedback_fn):
449
    """Execute the LU.
450

451
    This method should implement the actual work. It should raise
452
    errors.OpExecError for failures that are somewhat dealt with in
453
    code, or expected.
454

455
    """
456
    if self.tasklets is not None:
457
      for (idx, tl) in enumerate(self.tasklets):
458
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
459
        tl.Exec(feedback_fn)
460
    else:
461
      raise NotImplementedError
462

    
463
  def BuildHooksEnv(self):
464
    """Build hooks environment for this LU.
465

466
    This method should return a three-node tuple consisting of: a dict
467
    containing the environment that will be used for running the
468
    specific hook for this LU, a list of node names on which the hook
469
    should run before the execution, and a list of node names on which
470
    the hook should run after the execution.
471

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

477
    No nodes should be returned as an empty list (and not None).
478

479
    Note that if the HPATH for a LU class is None, this function will
480
    not be called.
481

482
    """
483
    raise NotImplementedError
484

    
485
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
486
    """Notify the LU about the results of its hooks.
487

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

494
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
495
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
496
    @param hook_results: the results of the multi-node hooks rpc call
497
    @param feedback_fn: function used send feedback back to the caller
498
    @param lu_result: the previous Exec result this LU had, or None
499
        in the PRE phase
500
    @return: the new Exec result, based on the previous result
501
        and hook results
502

503
    """
504
    # API must be kept, thus we ignore the unused argument and could
505
    # be a function warnings
506
    # pylint: disable-msg=W0613,R0201
507
    return lu_result
508

    
509
  def _ExpandAndLockInstance(self):
510
    """Helper function to expand and lock an instance.
511

512
    Many LUs that work on an instance take its name in self.op.instance_name
513
    and need to expand it and then declare the expanded name for locking. This
514
    function does it, and then updates self.op.instance_name to the expanded
515
    name. It also initializes needed_locks as a dict, if this hasn't been done
516
    before.
517

518
    """
519
    if self.needed_locks is None:
520
      self.needed_locks = {}
521
    else:
522
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
523
        "_ExpandAndLockInstance called with instance-level locks set"
524
    self.op.instance_name = _ExpandInstanceName(self.cfg,
525
                                                self.op.instance_name)
526
    self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
527

    
528
  def _LockInstancesNodes(self, primary_only=False):
529
    """Helper function to declare instances' nodes for locking.
530

531
    This function should be called after locking one or more instances to lock
532
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
533
    with all primary or secondary nodes for instances already locked and
534
    present in self.needed_locks[locking.LEVEL_INSTANCE].
535

536
    It should be called from DeclareLocks, and for safety only works if
537
    self.recalculate_locks[locking.LEVEL_NODE] is set.
538

539
    In the future it may grow parameters to just lock some instance's nodes, or
540
    to just lock primaries or secondary nodes, if needed.
541

542
    If should be called in DeclareLocks in a way similar to::
543

544
      if level == locking.LEVEL_NODE:
545
        self._LockInstancesNodes()
546

547
    @type primary_only: boolean
548
    @param primary_only: only lock primary nodes of locked instances
549

550
    """
551
    assert locking.LEVEL_NODE in self.recalculate_locks, \
552
      "_LockInstancesNodes helper function called with no nodes to recalculate"
553

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

    
556
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
557
    # future we might want to have different behaviors depending on the value
558
    # of self.recalculate_locks[locking.LEVEL_NODE]
559
    wanted_nodes = []
560
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
561
      instance = self.context.cfg.GetInstanceInfo(instance_name)
562
      wanted_nodes.append(instance.primary_node)
563
      if not primary_only:
564
        wanted_nodes.extend(instance.secondary_nodes)
565

    
566
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
567
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
568
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
569
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
570

    
571
    del self.recalculate_locks[locking.LEVEL_NODE]
572

    
573

    
574
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
575
  """Simple LU which runs no hooks.
576

577
  This LU is intended as a parent for other LogicalUnits which will
578
  run no hooks, in order to reduce duplicate code.
579

580
  """
581
  HPATH = None
582
  HTYPE = None
583

    
584
  def BuildHooksEnv(self):
585
    """Empty BuildHooksEnv for NoHooksLu.
586

587
    This just raises an error.
588

589
    """
590
    assert False, "BuildHooksEnv called for NoHooksLUs"
591

    
592

    
593
class Tasklet:
594
  """Tasklet base class.
595

596
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
597
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
598
  tasklets know nothing about locks.
599

600
  Subclasses must follow these rules:
601
    - Implement CheckPrereq
602
    - Implement Exec
603

604
  """
605
  def __init__(self, lu):
606
    self.lu = lu
607

    
608
    # Shortcuts
609
    self.cfg = lu.cfg
610
    self.rpc = lu.rpc
611

    
612
  def CheckPrereq(self):
613
    """Check prerequisites for this tasklets.
614

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

619
    The method should raise errors.OpPrereqError in case something is not
620
    fulfilled. Its return value is ignored.
621

622
    This method should also update all parameters to their canonical form if it
623
    hasn't been done before.
624

625
    """
626
    pass
627

    
628
  def Exec(self, feedback_fn):
629
    """Execute the tasklet.
630

631
    This method should implement the actual work. It should raise
632
    errors.OpExecError for failures that are somewhat dealt with in code, or
633
    expected.
634

635
    """
636
    raise NotImplementedError
637

    
638

    
639
def _GetWantedNodes(lu, nodes):
640
  """Returns list of checked and expanded node names.
641

642
  @type lu: L{LogicalUnit}
643
  @param lu: the logical unit on whose behalf we execute
644
  @type nodes: list
645
  @param nodes: list of node names or None for all nodes
646
  @rtype: list
647
  @return: the list of nodes, sorted
648
  @raise errors.ProgrammerError: if the nodes parameter is wrong type
649

650
  """
651
  if not nodes:
652
    raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
653
      " non-empty list of nodes whose name is to be expanded.")
654

    
655
  wanted = [_ExpandNodeName(lu.cfg, name) for name in nodes]
656
  return utils.NiceSort(wanted)
657

    
658

    
659
def _GetWantedInstances(lu, instances):
660
  """Returns list of checked and expanded instance names.
661

662
  @type lu: L{LogicalUnit}
663
  @param lu: the logical unit on whose behalf we execute
664
  @type instances: list
665
  @param instances: list of instance names or None for all instances
666
  @rtype: list
667
  @return: the list of instances, sorted
668
  @raise errors.OpPrereqError: if the instances parameter is wrong type
669
  @raise errors.OpPrereqError: if any of the passed instances is not found
670

671
  """
672
  if instances:
673
    wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
674
  else:
675
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
676
  return wanted
677

    
678

    
679
def _GetUpdatedParams(old_params, update_dict,
680
                      use_default=True, use_none=False):
681
  """Return the new version of a parameter dictionary.
682

683
  @type old_params: dict
684
  @param old_params: old parameters
685
  @type update_dict: dict
686
  @param update_dict: dict containing new parameter values, or
687
      constants.VALUE_DEFAULT to reset the parameter to its default
688
      value
689
  @param use_default: boolean
690
  @type use_default: whether to recognise L{constants.VALUE_DEFAULT}
691
      values as 'to be deleted' values
692
  @param use_none: boolean
693
  @type use_none: whether to recognise C{None} values as 'to be
694
      deleted' values
695
  @rtype: dict
696
  @return: the new parameter dictionary
697

698
  """
699
  params_copy = copy.deepcopy(old_params)
700
  for key, val in update_dict.iteritems():
701
    if ((use_default and val == constants.VALUE_DEFAULT) or
702
        (use_none and val is None)):
703
      try:
704
        del params_copy[key]
705
      except KeyError:
706
        pass
707
    else:
708
      params_copy[key] = val
709
  return params_copy
710

    
711

    
712
def _CheckOutputFields(static, dynamic, selected):
713
  """Checks whether all selected fields are valid.
714

715
  @type static: L{utils.FieldSet}
716
  @param static: static fields set
717
  @type dynamic: L{utils.FieldSet}
718
  @param dynamic: dynamic fields set
719

720
  """
721
  f = utils.FieldSet()
722
  f.Extend(static)
723
  f.Extend(dynamic)
724

    
725
  delta = f.NonMatching(selected)
726
  if delta:
727
    raise errors.OpPrereqError("Unknown output fields selected: %s"
728
                               % ",".join(delta), errors.ECODE_INVAL)
729

    
730

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

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

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

    
745

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

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

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

    
758

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

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

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

    
771

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

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

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

    
789

    
790
def _RequireFileStorage():
791
  """Checks that file storage is enabled.
792

793
  @raise errors.OpPrereqError: when file storage is disabled
794

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

    
800

    
801
def _CheckDiskTemplate(template):
802
  """Ensure a given disk template is valid.
803

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

    
813

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

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

    
825

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

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

    
833

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

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

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

    
849

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

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

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

    
866

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

    
871

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

    
876

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

882
  This builds the hook environment from individual variables.
883

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

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

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

    
946
  env["INSTANCE_NIC_COUNT"] = nic_count
947

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

    
956
  env["INSTANCE_DISK_COUNT"] = disk_count
957

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

    
962
  return env
963

    
964

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

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

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

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

    
988

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

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

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

    
1026

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

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

    
1042

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

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

    
1053

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

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

    
1067

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

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

    
1076

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

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

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

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

    
1097

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

    
1101

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

1105
  """
1106

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

    
1109

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

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

    
1117

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

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

    
1125

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

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

    
1135
  return []
1136

    
1137

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

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

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

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

    
1152
  return faulty
1153

    
1154

    
1155
def _CheckIAllocatorOrNode(lu, iallocator_slot, node_slot):
1156
  """Check the sanity of iallocator and node arguments and use the
1157
  cluster-wide iallocator if appropriate.
1158

1159
  Check that at most one of (iallocator, node) is specified. If none is
1160
  specified, then the LU's opcode's iallocator slot is filled with the
1161
  cluster-wide default iallocator.
1162

1163
  @type iallocator_slot: string
1164
  @param iallocator_slot: the name of the opcode iallocator slot
1165
  @type node_slot: string
1166
  @param node_slot: the name of the opcode target node slot
1167

1168
  """
1169
  node = getattr(lu.op, node_slot, None)
1170
  iallocator = getattr(lu.op, iallocator_slot, None)
1171

    
1172
  if node is not None and iallocator is not None:
1173
    raise errors.OpPrereqError("Do not specify both, iallocator and node.",
1174
                               errors.ECODE_INVAL)
1175
  elif node is None and iallocator is None:
1176
    default_iallocator = lu.cfg.GetDefaultIAllocator()
1177
    if default_iallocator:
1178
      setattr(lu.op, iallocator_slot, default_iallocator)
1179
    else:
1180
      raise errors.OpPrereqError("No iallocator or node given and no"
1181
                                 " cluster-wide default iallocator found."
1182
                                 " Please specify either an iallocator or a"
1183
                                 " node, or set a cluster-wide default"
1184
                                 " iallocator.")
1185

    
1186

    
1187
class LUPostInitCluster(LogicalUnit):
1188
  """Logical unit for running hooks after cluster initialization.
1189

1190
  """
1191
  HPATH = "cluster-init"
1192
  HTYPE = constants.HTYPE_CLUSTER
1193

    
1194
  def BuildHooksEnv(self):
1195
    """Build hooks env.
1196

1197
    """
1198
    env = {"OP_TARGET": self.cfg.GetClusterName()}
1199
    mn = self.cfg.GetMasterNode()
1200
    return env, [], [mn]
1201

    
1202
  def Exec(self, feedback_fn):
1203
    """Nothing to do.
1204

1205
    """
1206
    return True
1207

    
1208

    
1209
class LUDestroyCluster(LogicalUnit):
1210
  """Logical unit for destroying the cluster.
1211

1212
  """
1213
  HPATH = "cluster-destroy"
1214
  HTYPE = constants.HTYPE_CLUSTER
1215

    
1216
  def BuildHooksEnv(self):
1217
    """Build hooks env.
1218

1219
    """
1220
    env = {"OP_TARGET": self.cfg.GetClusterName()}
1221
    return env, [], []
1222

    
1223
  def CheckPrereq(self):
1224
    """Check prerequisites.
1225

1226
    This checks whether the cluster is empty.
1227

1228
    Any errors are signaled by raising errors.OpPrereqError.
1229

1230
    """
1231
    master = self.cfg.GetMasterNode()
1232

    
1233
    nodelist = self.cfg.GetNodeList()
1234
    if len(nodelist) != 1 or nodelist[0] != master:
1235
      raise errors.OpPrereqError("There are still %d node(s) in"
1236
                                 " this cluster." % (len(nodelist) - 1),
1237
                                 errors.ECODE_INVAL)
1238
    instancelist = self.cfg.GetInstanceList()
1239
    if instancelist:
1240
      raise errors.OpPrereqError("There are still %d instance(s) in"
1241
                                 " this cluster." % len(instancelist),
1242
                                 errors.ECODE_INVAL)
1243

    
1244
  def Exec(self, feedback_fn):
1245
    """Destroys the cluster.
1246

1247
    """
1248
    master = self.cfg.GetMasterNode()
1249
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
1250

    
1251
    # Run post hooks on master node before it's removed
1252
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
1253
    try:
1254
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
1255
    except:
1256
      # pylint: disable-msg=W0702
1257
      self.LogWarning("Errors occurred running hooks on %s" % master)
1258

    
1259
    result = self.rpc.call_node_stop_master(master, False)
1260
    result.Raise("Could not disable the master role")
1261

    
1262
    if modify_ssh_setup:
1263
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
1264
      utils.CreateBackup(priv_key)
1265
      utils.CreateBackup(pub_key)
1266

    
1267
    return master
1268

    
1269

    
1270
def _VerifyCertificate(filename):
1271
  """Verifies a certificate for LUVerifyCluster.
1272

1273
  @type filename: string
1274
  @param filename: Path to PEM file
1275

1276
  """
1277
  try:
1278
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1279
                                           utils.ReadFile(filename))
1280
  except Exception, err: # pylint: disable-msg=W0703
1281
    return (LUVerifyCluster.ETYPE_ERROR,
1282
            "Failed to load X509 certificate %s: %s" % (filename, err))
1283

    
1284
  (errcode, msg) = \
1285
    utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1286
                                constants.SSL_CERT_EXPIRATION_ERROR)
1287

    
1288
  if msg:
1289
    fnamemsg = "While verifying %s: %s" % (filename, msg)
1290
  else:
1291
    fnamemsg = None
1292

    
1293
  if errcode is None:
1294
    return (None, fnamemsg)
1295
  elif errcode == utils.CERT_WARNING:
1296
    return (LUVerifyCluster.ETYPE_WARNING, fnamemsg)
1297
  elif errcode == utils.CERT_ERROR:
1298
    return (LUVerifyCluster.ETYPE_ERROR, fnamemsg)
1299

    
1300
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1301

    
1302

    
1303
class LUVerifyCluster(LogicalUnit):
1304
  """Verifies the cluster status.
1305

1306
  """
1307
  HPATH = "cluster-verify"
1308
  HTYPE = constants.HTYPE_CLUSTER
1309
  _OP_PARAMS = [
1310
    ("skip_checks", _EmptyList,
1311
     _TListOf(_TElemOf(constants.VERIFY_OPTIONAL_CHECKS))),
1312
    ("verbose", False, _TBool),
1313
    ("error_codes", False, _TBool),
1314
    ("debug_simulate_errors", False, _TBool),
1315
    ]
1316
  REQ_BGL = False
1317

    
1318
  TCLUSTER = "cluster"
1319
  TNODE = "node"
1320
  TINSTANCE = "instance"
1321

    
1322
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1323
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1324
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1325
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1326
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1327
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1328
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1329
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1330
  ENODEDRBD = (TNODE, "ENODEDRBD")
1331
  ENODEDRBDHELPER = (TNODE, "ENODEDRBDHELPER")
1332
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1333
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1334
  ENODEHV = (TNODE, "ENODEHV")
1335
  ENODELVM = (TNODE, "ENODELVM")
1336
  ENODEN1 = (TNODE, "ENODEN1")
1337
  ENODENET = (TNODE, "ENODENET")
1338
  ENODEOS = (TNODE, "ENODEOS")
1339
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1340
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1341
  ENODERPC = (TNODE, "ENODERPC")
1342
  ENODESSH = (TNODE, "ENODESSH")
1343
  ENODEVERSION = (TNODE, "ENODEVERSION")
1344
  ENODESETUP = (TNODE, "ENODESETUP")
1345
  ENODETIME = (TNODE, "ENODETIME")
1346

    
1347
  ETYPE_FIELD = "code"
1348
  ETYPE_ERROR = "ERROR"
1349
  ETYPE_WARNING = "WARNING"
1350

    
1351
  class NodeImage(object):
1352
    """A class representing the logical and physical status of a node.
1353

1354
    @type name: string
1355
    @ivar name: the node name to which this object refers
1356
    @ivar volumes: a structure as returned from
1357
        L{ganeti.backend.GetVolumeList} (runtime)
1358
    @ivar instances: a list of running instances (runtime)
1359
    @ivar pinst: list of configured primary instances (config)
1360
    @ivar sinst: list of configured secondary instances (config)
1361
    @ivar sbp: diction of {secondary-node: list of instances} of all peers
1362
        of this node (config)
1363
    @ivar mfree: free memory, as reported by hypervisor (runtime)
1364
    @ivar dfree: free disk, as reported by the node (runtime)
1365
    @ivar offline: the offline status (config)
1366
    @type rpc_fail: boolean
1367
    @ivar rpc_fail: whether the RPC verify call was successfull (overall,
1368
        not whether the individual keys were correct) (runtime)
1369
    @type lvm_fail: boolean
1370
    @ivar lvm_fail: whether the RPC call didn't return valid LVM data
1371
    @type hyp_fail: boolean
1372
    @ivar hyp_fail: whether the RPC call didn't return the instance list
1373
    @type ghost: boolean
1374
    @ivar ghost: whether this is a known node or not (config)
1375
    @type os_fail: boolean
1376
    @ivar os_fail: whether the RPC call didn't return valid OS data
1377
    @type oslist: list
1378
    @ivar oslist: list of OSes as diagnosed by DiagnoseOS
1379

1380
    """
1381
    def __init__(self, offline=False, name=None):
1382
      self.name = name
1383
      self.volumes = {}
1384
      self.instances = []
1385
      self.pinst = []
1386
      self.sinst = []
1387
      self.sbp = {}
1388
      self.mfree = 0
1389
      self.dfree = 0
1390
      self.offline = offline
1391
      self.rpc_fail = False
1392
      self.lvm_fail = False
1393
      self.hyp_fail = False
1394
      self.ghost = False
1395
      self.os_fail = False
1396
      self.oslist = {}
1397

    
1398
  def ExpandNames(self):
1399
    self.needed_locks = {
1400
      locking.LEVEL_NODE: locking.ALL_SET,
1401
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1402
    }
1403
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1404

    
1405
  def _Error(self, ecode, item, msg, *args, **kwargs):
1406
    """Format an error message.
1407

1408
    Based on the opcode's error_codes parameter, either format a
1409
    parseable error code, or a simpler error string.
1410

1411
    This must be called only from Exec and functions called from Exec.
1412

1413
    """
1414
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1415
    itype, etxt = ecode
1416
    # first complete the msg
1417
    if args:
1418
      msg = msg % args
1419
    # then format the whole message
1420
    if self.op.error_codes:
1421
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1422
    else:
1423
      if item:
1424
        item = " " + item
1425
      else:
1426
        item = ""
1427
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1428
    # and finally report it via the feedback_fn
1429
    self._feedback_fn("  - %s" % msg)
1430

    
1431
  def _ErrorIf(self, cond, *args, **kwargs):
1432
    """Log an error message if the passed condition is True.
1433

1434
    """
1435
    cond = bool(cond) or self.op.debug_simulate_errors
1436
    if cond:
1437
      self._Error(*args, **kwargs)
1438
    # do not mark the operation as failed for WARN cases only
1439
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1440
      self.bad = self.bad or cond
1441

    
1442
  def _VerifyNode(self, ninfo, nresult):
1443
    """Perform some basic validation on data returned from a node.
1444

1445
      - check the result data structure is well formed and has all the
1446
        mandatory fields
1447
      - check ganeti version
1448

1449
    @type ninfo: L{objects.Node}
1450
    @param ninfo: the node to check
1451
    @param nresult: the results from the node
1452
    @rtype: boolean
1453
    @return: whether overall this call was successful (and we can expect
1454
         reasonable values in the respose)
1455

1456
    """
1457
    node = ninfo.name
1458
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1459

    
1460
    # main result, nresult should be a non-empty dict
1461
    test = not nresult or not isinstance(nresult, dict)
1462
    _ErrorIf(test, self.ENODERPC, node,
1463
                  "unable to verify node: no data returned")
1464
    if test:
1465
      return False
1466

    
1467
    # compares ganeti version
1468
    local_version = constants.PROTOCOL_VERSION
1469
    remote_version = nresult.get("version", None)
1470
    test = not (remote_version and
1471
                isinstance(remote_version, (list, tuple)) and
1472
                len(remote_version) == 2)
1473
    _ErrorIf(test, self.ENODERPC, node,
1474
             "connection to node returned invalid data")
1475
    if test:
1476
      return False
1477

    
1478
    test = local_version != remote_version[0]
1479
    _ErrorIf(test, self.ENODEVERSION, node,
1480
             "incompatible protocol versions: master %s,"
1481
             " node %s", local_version, remote_version[0])
1482
    if test:
1483
      return False
1484

    
1485
    # node seems compatible, we can actually try to look into its results
1486

    
1487
    # full package version
1488
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1489
                  self.ENODEVERSION, node,
1490
                  "software version mismatch: master %s, node %s",
1491
                  constants.RELEASE_VERSION, remote_version[1],
1492
                  code=self.ETYPE_WARNING)
1493

    
1494
    hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1495
    if isinstance(hyp_result, dict):
1496
      for hv_name, hv_result in hyp_result.iteritems():
1497
        test = hv_result is not None
1498
        _ErrorIf(test, self.ENODEHV, node,
1499
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1500

    
1501

    
1502
    test = nresult.get(constants.NV_NODESETUP,
1503
                           ["Missing NODESETUP results"])
1504
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1505
             "; ".join(test))
1506

    
1507
    return True
1508

    
1509
  def _VerifyNodeTime(self, ninfo, nresult,
1510
                      nvinfo_starttime, nvinfo_endtime):
1511
    """Check the node time.
1512

1513
    @type ninfo: L{objects.Node}
1514
    @param ninfo: the node to check
1515
    @param nresult: the remote results for the node
1516
    @param nvinfo_starttime: the start time of the RPC call
1517
    @param nvinfo_endtime: the end time of the RPC call
1518

1519
    """
1520
    node = ninfo.name
1521
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1522

    
1523
    ntime = nresult.get(constants.NV_TIME, None)
1524
    try:
1525
      ntime_merged = utils.MergeTime(ntime)
1526
    except (ValueError, TypeError):
1527
      _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1528
      return
1529

    
1530
    if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1531
      ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1532
    elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1533
      ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1534
    else:
1535
      ntime_diff = None
1536

    
1537
    _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1538
             "Node time diverges by at least %s from master node time",
1539
             ntime_diff)
1540

    
1541
  def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1542
    """Check the node time.
1543

1544
    @type ninfo: L{objects.Node}
1545
    @param ninfo: the node to check
1546
    @param nresult: the remote results for the node
1547
    @param vg_name: the configured VG name
1548

1549
    """
1550
    if vg_name is None:
1551
      return
1552

    
1553
    node = ninfo.name
1554
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1555

    
1556
    # checks vg existence and size > 20G
1557
    vglist = nresult.get(constants.NV_VGLIST, None)
1558
    test = not vglist
1559
    _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1560
    if not test:
1561
      vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1562
                                            constants.MIN_VG_SIZE)
1563
      _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1564

    
1565
    # check pv names
1566
    pvlist = nresult.get(constants.NV_PVLIST, None)
1567
    test = pvlist is None
1568
    _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1569
    if not test:
1570
      # check that ':' is not present in PV names, since it's a
1571
      # special character for lvcreate (denotes the range of PEs to
1572
      # use on the PV)
1573
      for _, pvname, owner_vg in pvlist:
1574
        test = ":" in pvname
1575
        _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1576
                 " '%s' of VG '%s'", pvname, owner_vg)
1577

    
1578
  def _VerifyNodeNetwork(self, ninfo, nresult):
1579
    """Check the node time.
1580

1581
    @type ninfo: L{objects.Node}
1582
    @param ninfo: the node to check
1583
    @param nresult: the remote results for the node
1584

1585
    """
1586
    node = ninfo.name
1587
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1588

    
1589
    test = constants.NV_NODELIST not in nresult
1590
    _ErrorIf(test, self.ENODESSH, node,
1591
             "node hasn't returned node ssh connectivity data")
1592
    if not test:
1593
      if nresult[constants.NV_NODELIST]:
1594
        for a_node, a_msg in nresult[constants.NV_NODELIST].items():
1595
          _ErrorIf(True, self.ENODESSH, node,
1596
                   "ssh communication with node '%s': %s", a_node, a_msg)
1597

    
1598
    test = constants.NV_NODENETTEST not in nresult
1599
    _ErrorIf(test, self.ENODENET, node,
1600
             "node hasn't returned node tcp connectivity data")
1601
    if not test:
1602
      if nresult[constants.NV_NODENETTEST]:
1603
        nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys())
1604
        for anode in nlist:
1605
          _ErrorIf(True, self.ENODENET, node,
1606
                   "tcp communication with node '%s': %s",
1607
                   anode, nresult[constants.NV_NODENETTEST][anode])
1608

    
1609
    test = constants.NV_MASTERIP not in nresult
1610
    _ErrorIf(test, self.ENODENET, node,
1611
             "node hasn't returned node master IP reachability data")
1612
    if not test:
1613
      if not nresult[constants.NV_MASTERIP]:
1614
        if node == self.master_node:
1615
          msg = "the master node cannot reach the master IP (not configured?)"
1616
        else:
1617
          msg = "cannot reach the master IP"
1618
        _ErrorIf(True, self.ENODENET, node, msg)
1619

    
1620

    
1621
  def _VerifyInstance(self, instance, instanceconfig, node_image):
1622
    """Verify an instance.
1623

1624
    This function checks to see if the required block devices are
1625
    available on the instance's node.
1626

1627
    """
1628
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1629
    node_current = instanceconfig.primary_node
1630

    
1631
    node_vol_should = {}
1632
    instanceconfig.MapLVsByNode(node_vol_should)
1633

    
1634
    for node in node_vol_should:
1635
      n_img = node_image[node]
1636
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1637
        # ignore missing volumes on offline or broken nodes
1638
        continue
1639
      for volume in node_vol_should[node]:
1640
        test = volume not in n_img.volumes
1641
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1642
                 "volume %s missing on node %s", volume, node)
1643

    
1644
    if instanceconfig.admin_up:
1645
      pri_img = node_image[node_current]
1646
      test = instance not in pri_img.instances and not pri_img.offline
1647
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1648
               "instance not running on its primary node %s",
1649
               node_current)
1650

    
1651
    for node, n_img in node_image.items():
1652
      if (not node == node_current):
1653
        test = instance in n_img.instances
1654
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1655
                 "instance should not run on node %s", node)
1656

    
1657
  def _VerifyOrphanVolumes(self, node_vol_should, node_image, reserved):
1658
    """Verify if there are any unknown volumes in the cluster.
1659

1660
    The .os, .swap and backup volumes are ignored. All other volumes are
1661
    reported as unknown.
1662

1663
    @type reserved: L{ganeti.utils.FieldSet}
1664
    @param reserved: a FieldSet of reserved volume names
1665

1666
    """
1667
    for node, n_img in node_image.items():
1668
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1669
        # skip non-healthy nodes
1670
        continue
1671
      for volume in n_img.volumes:
1672
        test = ((node not in node_vol_should or
1673
                volume not in node_vol_should[node]) and
1674
                not reserved.Matches(volume))
1675
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1676
                      "volume %s is unknown", volume)
1677

    
1678
  def _VerifyOrphanInstances(self, instancelist, node_image):
1679
    """Verify the list of running instances.
1680

1681
    This checks what instances are running but unknown to the cluster.
1682

1683
    """
1684
    for node, n_img in node_image.items():
1685
      for o_inst in n_img.instances:
1686
        test = o_inst not in instancelist
1687
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1688
                      "instance %s on node %s should not exist", o_inst, node)
1689

    
1690
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1691
    """Verify N+1 Memory Resilience.
1692

1693
    Check that if one single node dies we can still start all the
1694
    instances it was primary for.
1695

1696
    """
1697
    for node, n_img in node_image.items():
1698
      # This code checks that every node which is now listed as
1699
      # secondary has enough memory to host all instances it is
1700
      # supposed to should a single other node in the cluster fail.
1701
      # FIXME: not ready for failover to an arbitrary node
1702
      # FIXME: does not support file-backed instances
1703
      # WARNING: we currently take into account down instances as well
1704
      # as up ones, considering that even if they're down someone
1705
      # might want to start them even in the event of a node failure.
1706
      for prinode, instances in n_img.sbp.items():
1707
        needed_mem = 0
1708
        for instance in instances:
1709
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1710
          if bep[constants.BE_AUTO_BALANCE]:
1711
            needed_mem += bep[constants.BE_MEMORY]
1712
        test = n_img.mfree < needed_mem
1713
        self._ErrorIf(test, self.ENODEN1, node,
1714
                      "not enough memory on to accommodate"
1715
                      " failovers should peer node %s fail", prinode)
1716

    
1717
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1718
                       master_files):
1719
    """Verifies and computes the node required file checksums.
1720

1721
    @type ninfo: L{objects.Node}
1722
    @param ninfo: the node to check
1723
    @param nresult: the remote results for the node
1724
    @param file_list: required list of files
1725
    @param local_cksum: dictionary of local files and their checksums
1726
    @param master_files: list of files that only masters should have
1727

1728
    """
1729
    node = ninfo.name
1730
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1731

    
1732
    remote_cksum = nresult.get(constants.NV_FILELIST, None)
1733
    test = not isinstance(remote_cksum, dict)
1734
    _ErrorIf(test, self.ENODEFILECHECK, node,
1735
             "node hasn't returned file checksum data")
1736
    if test:
1737
      return
1738

    
1739
    for file_name in file_list:
1740
      node_is_mc = ninfo.master_candidate
1741
      must_have = (file_name not in master_files) or node_is_mc
1742
      # missing
1743
      test1 = file_name not in remote_cksum
1744
      # invalid checksum
1745
      test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1746
      # existing and good
1747
      test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1748
      _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1749
               "file '%s' missing", file_name)
1750
      _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1751
               "file '%s' has wrong checksum", file_name)
1752
      # not candidate and this is not a must-have file
1753
      _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1754
               "file '%s' should not exist on non master"
1755
               " candidates (and the file is outdated)", file_name)
1756
      # all good, except non-master/non-must have combination
1757
      _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1758
               "file '%s' should not exist"
1759
               " on non master candidates", file_name)
1760

    
1761
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_helper,
1762
                      drbd_map):
1763
    """Verifies and the node DRBD status.
1764

1765
    @type ninfo: L{objects.Node}
1766
    @param ninfo: the node to check
1767
    @param nresult: the remote results for the node
1768
    @param instanceinfo: the dict of instances
1769
    @param drbd_helper: the configured DRBD usermode helper
1770
    @param drbd_map: the DRBD map as returned by
1771
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1772

1773
    """
1774
    node = ninfo.name
1775
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1776

    
1777
    if drbd_helper:
1778
      helper_result = nresult.get(constants.NV_DRBDHELPER, None)
1779
      test = (helper_result == None)
1780
      _ErrorIf(test, self.ENODEDRBDHELPER, node,
1781
               "no drbd usermode helper returned")
1782
      if helper_result:
1783
        status, payload = helper_result
1784
        test = not status
1785
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1786
                 "drbd usermode helper check unsuccessful: %s", payload)
1787
        test = status and (payload != drbd_helper)
1788
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1789
                 "wrong drbd usermode helper: %s", payload)
1790

    
1791
    # compute the DRBD minors
1792
    node_drbd = {}
1793
    for minor, instance in drbd_map[node].items():
1794
      test = instance not in instanceinfo
1795
      _ErrorIf(test, self.ECLUSTERCFG, None,
1796
               "ghost instance '%s' in temporary DRBD map", instance)
1797
        # ghost instance should not be running, but otherwise we
1798
        # don't give double warnings (both ghost instance and
1799
        # unallocated minor in use)
1800
      if test:
1801
        node_drbd[minor] = (instance, False)
1802
      else:
1803
        instance = instanceinfo[instance]
1804
        node_drbd[minor] = (instance.name, instance.admin_up)
1805

    
1806
    # and now check them
1807
    used_minors = nresult.get(constants.NV_DRBDLIST, [])
1808
    test = not isinstance(used_minors, (tuple, list))
1809
    _ErrorIf(test, self.ENODEDRBD, node,
1810
             "cannot parse drbd status file: %s", str(used_minors))
1811
    if test:
1812
      # we cannot check drbd status
1813
      return
1814

    
1815
    for minor, (iname, must_exist) in node_drbd.items():
1816
      test = minor not in used_minors and must_exist
1817
      _ErrorIf(test, self.ENODEDRBD, node,
1818
               "drbd minor %d of instance %s is not active", minor, iname)
1819
    for minor in used_minors:
1820
      test = minor not in node_drbd
1821
      _ErrorIf(test, self.ENODEDRBD, node,
1822
               "unallocated drbd minor %d is in use", minor)
1823

    
1824
  def _UpdateNodeOS(self, ninfo, nresult, nimg):
1825
    """Builds the node OS structures.
1826

1827
    @type ninfo: L{objects.Node}
1828
    @param ninfo: the node to check
1829
    @param nresult: the remote results for the node
1830
    @param nimg: the node image object
1831

1832
    """
1833
    node = ninfo.name
1834
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1835

    
1836
    remote_os = nresult.get(constants.NV_OSLIST, None)
1837
    test = (not isinstance(remote_os, list) or
1838
            not compat.all(isinstance(v, list) and len(v) == 7
1839
                           for v in remote_os))
1840

    
1841
    _ErrorIf(test, self.ENODEOS, node,
1842
             "node hasn't returned valid OS data")
1843

    
1844
    nimg.os_fail = test
1845

    
1846
    if test:
1847
      return
1848

    
1849
    os_dict = {}
1850

    
1851
    for (name, os_path, status, diagnose,
1852
         variants, parameters, api_ver) in nresult[constants.NV_OSLIST]:
1853

    
1854
      if name not in os_dict:
1855
        os_dict[name] = []
1856

    
1857
      # parameters is a list of lists instead of list of tuples due to
1858
      # JSON lacking a real tuple type, fix it:
1859
      parameters = [tuple(v) for v in parameters]
1860
      os_dict[name].append((os_path, status, diagnose,
1861
                            set(variants), set(parameters), set(api_ver)))
1862

    
1863
    nimg.oslist = os_dict
1864

    
1865
  def _VerifyNodeOS(self, ninfo, nimg, base):
1866
    """Verifies the node OS list.
1867

1868
    @type ninfo: L{objects.Node}
1869
    @param ninfo: the node to check
1870
    @param nimg: the node image object
1871
    @param base: the 'template' node we match against (e.g. from the master)
1872

1873
    """
1874
    node = ninfo.name
1875
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1876

    
1877
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1878

    
1879
    for os_name, os_data in nimg.oslist.items():
1880
      assert os_data, "Empty OS status for OS %s?!" % os_name
1881
      f_path, f_status, f_diag, f_var, f_param, f_api = os_data[0]
1882
      _ErrorIf(not f_status, self.ENODEOS, node,
1883
               "Invalid OS %s (located at %s): %s", os_name, f_path, f_diag)
1884
      _ErrorIf(len(os_data) > 1, self.ENODEOS, node,
1885
               "OS '%s' has multiple entries (first one shadows the rest): %s",
1886
               os_name, utils.CommaJoin([v[0] for v in os_data]))
1887
      # this will catched in backend too
1888
      _ErrorIf(compat.any(v >= constants.OS_API_V15 for v in f_api)
1889
               and not f_var, self.ENODEOS, node,
1890
               "OS %s with API at least %d does not declare any variant",
1891
               os_name, constants.OS_API_V15)
1892
      # comparisons with the 'base' image
1893
      test = os_name not in base.oslist
1894
      _ErrorIf(test, self.ENODEOS, node,
1895
               "Extra OS %s not present on reference node (%s)",
1896
               os_name, base.name)
1897
      if test:
1898
        continue
1899
      assert base.oslist[os_name], "Base node has empty OS status?"
1900
      _, b_status, _, b_var, b_param, b_api = base.oslist[os_name][0]
1901
      if not b_status:
1902
        # base OS is invalid, skipping
1903
        continue
1904
      for kind, a, b in [("API version", f_api, b_api),
1905
                         ("variants list", f_var, b_var),
1906
                         ("parameters", f_param, b_param)]:
1907
        _ErrorIf(a != b, self.ENODEOS, node,
1908
                 "OS %s %s differs from reference node %s: %s vs. %s",
1909
                 kind, os_name, base.name,
1910
                 utils.CommaJoin(a), utils.CommaJoin(b))
1911

    
1912
    # check any missing OSes
1913
    missing = set(base.oslist.keys()).difference(nimg.oslist.keys())
1914
    _ErrorIf(missing, self.ENODEOS, node,
1915
             "OSes present on reference node %s but missing on this node: %s",
1916
             base.name, utils.CommaJoin(missing))
1917

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

1921
    This function will update a L{NodeImage}'s internal structures
1922
    with data from the remote call.
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
    nimg.lvm_fail = True
1935
    lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1936
    if vg_name is None:
1937
      pass
1938
    elif isinstance(lvdata, basestring):
1939
      _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1940
               utils.SafeEncode(lvdata))
1941
    elif not isinstance(lvdata, dict):
1942
      _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1943
    else:
1944
      nimg.volumes = lvdata
1945
      nimg.lvm_fail = False
1946

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

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

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

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

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

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

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

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

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

    
2007
  def BuildHooksEnv(self):
2008
    """Build hooks env.
2009

2010
    Cluster-Verify hooks just ran in the post phase and their failure makes
2011
    the output be logged in the verify output and the verification to fail.
2012

2013
    """
2014
    all_nodes = self.cfg.GetNodeList()
2015
    env = {
2016
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
2017
      }
2018
    for node in self.cfg.GetAllNodesInfo().values():
2019
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
2020

    
2021
    return env, [], all_nodes
2022

    
2023
  def Exec(self, feedback_fn):
2024
    """Verify integrity of cluster, performing various test on nodes.
2025

2026
    """
2027
    self.bad = False
2028
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2029
    verbose = self.op.verbose
2030
    self._feedback_fn = feedback_fn
2031
    feedback_fn("* Verifying global settings")
2032
    for msg in self.cfg.VerifyConfig():
2033
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
2034

    
2035
    # Check the cluster certificates
2036
    for cert_filename in constants.ALL_CERT_FILES:
2037
      (errcode, msg) = _VerifyCertificate(cert_filename)
2038
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
2039

    
2040
    vg_name = self.cfg.GetVGName()
2041
    drbd_helper = self.cfg.GetDRBDHelper()
2042
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
2043
    cluster = self.cfg.GetClusterInfo()
2044
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
2045
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
2046
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
2047
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
2048
                        for iname in instancelist)
2049
    i_non_redundant = [] # Non redundant instances
2050
    i_non_a_balanced = [] # Non auto-balanced instances
2051
    n_offline = 0 # Count of offline nodes
2052
    n_drained = 0 # Count of nodes being drained
2053
    node_vol_should = {}
2054

    
2055
    # FIXME: verify OS list
2056
    # do local checksums
2057
    master_files = [constants.CLUSTER_CONF_FILE]
2058
    master_node = self.master_node = self.cfg.GetMasterNode()
2059
    master_ip = self.cfg.GetMasterIP()
2060

    
2061
    file_names = ssconf.SimpleStore().GetFileList()
2062
    file_names.extend(constants.ALL_CERT_FILES)
2063
    file_names.extend(master_files)
2064
    if cluster.modify_etc_hosts:
2065
      file_names.append(constants.ETC_HOSTS)
2066

    
2067
    local_checksums = utils.FingerprintFiles(file_names)
2068

    
2069
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
2070
    node_verify_param = {
2071
      constants.NV_FILELIST: file_names,
2072
      constants.NV_NODELIST: [node.name for node in nodeinfo
2073
                              if not node.offline],
2074
      constants.NV_HYPERVISOR: hypervisors,
2075
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
2076
                                  node.secondary_ip) for node in nodeinfo
2077
                                 if not node.offline],
2078
      constants.NV_INSTANCELIST: hypervisors,
2079
      constants.NV_VERSION: None,
2080
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
2081
      constants.NV_NODESETUP: None,
2082
      constants.NV_TIME: None,
2083
      constants.NV_MASTERIP: (master_node, master_ip),
2084
      constants.NV_OSLIST: None,
2085
      }
2086

    
2087
    if vg_name is not None:
2088
      node_verify_param[constants.NV_VGLIST] = None
2089
      node_verify_param[constants.NV_LVLIST] = vg_name
2090
      node_verify_param[constants.NV_PVLIST] = [vg_name]
2091
      node_verify_param[constants.NV_DRBDLIST] = None
2092

    
2093
    if drbd_helper:
2094
      node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
2095

    
2096
    # Build our expected cluster state
2097
    node_image = dict((node.name, self.NodeImage(offline=node.offline,
2098
                                                 name=node.name))
2099
                      for node in nodeinfo)
2100

    
2101
    for instance in instancelist:
2102
      inst_config = instanceinfo[instance]
2103

    
2104
      for nname in inst_config.all_nodes:
2105
        if nname not in node_image:
2106
          # ghost node
2107
          gnode = self.NodeImage(name=nname)
2108
          gnode.ghost = True
2109
          node_image[nname] = gnode
2110

    
2111
      inst_config.MapLVsByNode(node_vol_should)
2112

    
2113
      pnode = inst_config.primary_node
2114
      node_image[pnode].pinst.append(instance)
2115

    
2116
      for snode in inst_config.secondary_nodes:
2117
        nimg = node_image[snode]
2118
        nimg.sinst.append(instance)
2119
        if pnode not in nimg.sbp:
2120
          nimg.sbp[pnode] = []
2121
        nimg.sbp[pnode].append(instance)
2122

    
2123
    # At this point, we have the in-memory data structures complete,
2124
    # except for the runtime information, which we'll gather next
2125

    
2126
    # Due to the way our RPC system works, exact response times cannot be
2127
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
2128
    # time before and after executing the request, we can at least have a time
2129
    # window.
2130
    nvinfo_starttime = time.time()
2131
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
2132
                                           self.cfg.GetClusterName())
2133
    nvinfo_endtime = time.time()
2134

    
2135
    all_drbd_map = self.cfg.ComputeDRBDMap()
2136

    
2137
    feedback_fn("* Verifying node status")
2138

    
2139
    refos_img = None
2140

    
2141
    for node_i in nodeinfo:
2142
      node = node_i.name
2143
      nimg = node_image[node]
2144

    
2145
      if node_i.offline:
2146
        if verbose:
2147
          feedback_fn("* Skipping offline node %s" % (node,))
2148
        n_offline += 1
2149
        continue
2150

    
2151
      if node == master_node:
2152
        ntype = "master"
2153
      elif node_i.master_candidate:
2154
        ntype = "master candidate"
2155
      elif node_i.drained:
2156
        ntype = "drained"
2157
        n_drained += 1
2158
      else:
2159
        ntype = "regular"
2160
      if verbose:
2161
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
2162

    
2163
      msg = all_nvinfo[node].fail_msg
2164
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
2165
      if msg:
2166
        nimg.rpc_fail = True
2167
        continue
2168

    
2169
      nresult = all_nvinfo[node].payload
2170

    
2171
      nimg.call_ok = self._VerifyNode(node_i, nresult)
2172
      self._VerifyNodeNetwork(node_i, nresult)
2173
      self._VerifyNodeLVM(node_i, nresult, vg_name)
2174
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
2175
                            master_files)
2176
      self._VerifyNodeDrbd(node_i, nresult, instanceinfo, drbd_helper,
2177
                           all_drbd_map)
2178
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
2179

    
2180
      self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
2181
      self._UpdateNodeInstances(node_i, nresult, nimg)
2182
      self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
2183
      self._UpdateNodeOS(node_i, nresult, nimg)
2184
      if not nimg.os_fail:
2185
        if refos_img is None:
2186
          refos_img = nimg
2187
        self._VerifyNodeOS(node_i, nimg, refos_img)
2188

    
2189
    feedback_fn("* Verifying instance status")
2190
    for instance in instancelist:
2191
      if verbose:
2192
        feedback_fn("* Verifying instance %s" % instance)
2193
      inst_config = instanceinfo[instance]
2194
      self._VerifyInstance(instance, inst_config, node_image)
2195
      inst_nodes_offline = []
2196

    
2197
      pnode = inst_config.primary_node
2198
      pnode_img = node_image[pnode]
2199
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
2200
               self.ENODERPC, pnode, "instance %s, connection to"
2201
               " primary node failed", instance)
2202

    
2203
      if pnode_img.offline:
2204
        inst_nodes_offline.append(pnode)
2205

    
2206
      # If the instance is non-redundant we cannot survive losing its primary
2207
      # node, so we are not N+1 compliant. On the other hand we have no disk
2208
      # templates with more than one secondary so that situation is not well
2209
      # supported either.
2210
      # FIXME: does not support file-backed instances
2211
      if not inst_config.secondary_nodes:
2212
        i_non_redundant.append(instance)
2213
      _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
2214
               instance, "instance has multiple secondary nodes: %s",
2215
               utils.CommaJoin(inst_config.secondary_nodes),
2216
               code=self.ETYPE_WARNING)
2217

    
2218
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
2219
        i_non_a_balanced.append(instance)
2220

    
2221
      for snode in inst_config.secondary_nodes:
2222
        s_img = node_image[snode]
2223
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
2224
                 "instance %s, connection to secondary node failed", instance)
2225

    
2226
        if s_img.offline:
2227
          inst_nodes_offline.append(snode)
2228

    
2229
      # warn that the instance lives on offline nodes
2230
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
2231
               "instance lives on offline node(s) %s",
2232
               utils.CommaJoin(inst_nodes_offline))
2233
      # ... or ghost nodes
2234
      for node in inst_config.all_nodes:
2235
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
2236
                 "instance lives on ghost node %s", node)
2237

    
2238
    feedback_fn("* Verifying orphan volumes")
2239
    reserved = utils.FieldSet(*cluster.reserved_lvs)
2240
    self._VerifyOrphanVolumes(node_vol_should, node_image, reserved)
2241

    
2242
    feedback_fn("* Verifying orphan instances")
2243
    self._VerifyOrphanInstances(instancelist, node_image)
2244

    
2245
    if constants.VERIFY_NPLUSONE_MEM not in self.op.skip_checks:
2246
      feedback_fn("* Verifying N+1 Memory redundancy")
2247
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
2248

    
2249
    feedback_fn("* Other Notes")
2250
    if i_non_redundant:
2251
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
2252
                  % len(i_non_redundant))
2253

    
2254
    if i_non_a_balanced:
2255
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
2256
                  % len(i_non_a_balanced))
2257

    
2258
    if n_offline:
2259
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
2260

    
2261
    if n_drained:
2262
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
2263

    
2264
    return not self.bad
2265

    
2266
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
2267
    """Analyze the post-hooks' result
2268

2269
    This method analyses the hook result, handles it, and sends some
2270
    nicely-formatted feedback back to the user.
2271

2272
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
2273
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
2274
    @param hooks_results: the results of the multi-node hooks rpc call
2275
    @param feedback_fn: function used send feedback back to the caller
2276
    @param lu_result: previous Exec result
2277
    @return: the new Exec result, based on the previous result
2278
        and hook results
2279

2280
    """
2281
    # We only really run POST phase hooks, and are only interested in
2282
    # their results
2283
    if phase == constants.HOOKS_PHASE_POST:
2284
      # Used to change hooks' output to proper indentation
2285
      indent_re = re.compile('^', re.M)
2286
      feedback_fn("* Hooks Results")
2287
      assert hooks_results, "invalid result from hooks"
2288

    
2289
      for node_name in hooks_results:
2290
        res = hooks_results[node_name]
2291
        msg = res.fail_msg
2292
        test = msg and not res.offline
2293
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
2294
                      "Communication failure in hooks execution: %s", msg)
2295
        if res.offline or msg:
2296
          # No need to investigate payload if node is offline or gave an error.
2297
          # override manually lu_result here as _ErrorIf only
2298
          # overrides self.bad
2299
          lu_result = 1
2300
          continue
2301
        for script, hkr, output in res.payload:
2302
          test = hkr == constants.HKR_FAIL
2303
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
2304
                        "Script %s failed, output:", script)
2305
          if test:
2306
            output = indent_re.sub('      ', output)
2307
            feedback_fn("%s" % output)
2308
            lu_result = 0
2309

    
2310
      return lu_result
2311

    
2312

    
2313
class LUVerifyDisks(NoHooksLU):
2314
  """Verifies the cluster disks status.
2315

2316
  """
2317
  REQ_BGL = False
2318

    
2319
  def ExpandNames(self):
2320
    self.needed_locks = {
2321
      locking.LEVEL_NODE: locking.ALL_SET,
2322
      locking.LEVEL_INSTANCE: locking.ALL_SET,
2323
    }
2324
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
2325

    
2326
  def Exec(self, feedback_fn):
2327
    """Verify integrity of cluster disks.
2328

2329
    @rtype: tuple of three items
2330
    @return: a tuple of (dict of node-to-node_error, list of instances
2331
        which need activate-disks, dict of instance: (node, volume) for
2332
        missing volumes
2333

2334
    """
2335
    result = res_nodes, res_instances, res_missing = {}, [], {}
2336

    
2337
    vg_name = self.cfg.GetVGName()
2338
    nodes = utils.NiceSort(self.cfg.GetNodeList())
2339
    instances = [self.cfg.GetInstanceInfo(name)
2340
                 for name in self.cfg.GetInstanceList()]
2341

    
2342
    nv_dict = {}
2343
    for inst in instances:
2344
      inst_lvs = {}
2345
      if (not inst.admin_up or
2346
          inst.disk_template not in constants.DTS_NET_MIRROR):
2347
        continue
2348
      inst.MapLVsByNode(inst_lvs)
2349
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
2350
      for node, vol_list in inst_lvs.iteritems():
2351
        for vol in vol_list:
2352
          nv_dict[(node, vol)] = inst
2353

    
2354
    if not nv_dict:
2355
      return result
2356

    
2357
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
2358

    
2359
    for node in nodes:
2360
      # node_volume
2361
      node_res = node_lvs[node]
2362
      if node_res.offline:
2363
        continue
2364
      msg = node_res.fail_msg
2365
      if msg:
2366
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
2367
        res_nodes[node] = msg
2368
        continue
2369

    
2370
      lvs = node_res.payload
2371
      for lv_name, (_, _, lv_online) in lvs.items():
2372
        inst = nv_dict.pop((node, lv_name), None)
2373
        if (not lv_online and inst is not None
2374
            and inst.name not in res_instances):
2375
          res_instances.append(inst.name)
2376

    
2377
    # any leftover items in nv_dict are missing LVs, let's arrange the
2378
    # data better
2379
    for key, inst in nv_dict.iteritems():
2380
      if inst.name not in res_missing:
2381
        res_missing[inst.name] = []
2382
      res_missing[inst.name].append(key)
2383

    
2384
    return result
2385

    
2386

    
2387
class LURepairDiskSizes(NoHooksLU):
2388
  """Verifies the cluster disks sizes.
2389

2390
  """
2391
  _OP_PARAMS = [("instances", _EmptyList, _TListOf(_TNonEmptyString))]
2392
  REQ_BGL = False
2393

    
2394
  def ExpandNames(self):
2395
    if self.op.instances:
2396
      self.wanted_names = []
2397
      for name in self.op.instances:
2398
        full_name = _ExpandInstanceName(self.cfg, name)
2399
        self.wanted_names.append(full_name)
2400
      self.needed_locks = {
2401
        locking.LEVEL_NODE: [],
2402
        locking.LEVEL_INSTANCE: self.wanted_names,
2403
        }
2404
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2405
    else:
2406
      self.wanted_names = None
2407
      self.needed_locks = {
2408
        locking.LEVEL_NODE: locking.ALL_SET,
2409
        locking.LEVEL_INSTANCE: locking.ALL_SET,
2410
        }
2411
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
2412

    
2413
  def DeclareLocks(self, level):
2414
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2415
      self._LockInstancesNodes(primary_only=True)
2416

    
2417
  def CheckPrereq(self):
2418
    """Check prerequisites.
2419

2420
    This only checks the optional instance list against the existing names.
2421

2422
    """
2423
    if self.wanted_names is None:
2424
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2425

    
2426
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2427
                             in self.wanted_names]
2428

    
2429
  def _EnsureChildSizes(self, disk):
2430
    """Ensure children of the disk have the needed disk size.
2431

2432
    This is valid mainly for DRBD8 and fixes an issue where the
2433
    children have smaller disk size.
2434

2435
    @param disk: an L{ganeti.objects.Disk} object
2436

2437
    """
2438
    if disk.dev_type == constants.LD_DRBD8:
2439
      assert disk.children, "Empty children for DRBD8?"
2440
      fchild = disk.children[0]
2441
      mismatch = fchild.size < disk.size
2442
      if mismatch:
2443
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2444
                     fchild.size, disk.size)
2445
        fchild.size = disk.size
2446

    
2447
      # and we recurse on this child only, not on the metadev
2448
      return self._EnsureChildSizes(fchild) or mismatch
2449
    else:
2450
      return False
2451

    
2452
  def Exec(self, feedback_fn):
2453
    """Verify the size of cluster disks.
2454

2455
    """
2456
    # TODO: check child disks too
2457
    # TODO: check differences in size between primary/secondary nodes
2458
    per_node_disks = {}
2459
    for instance in self.wanted_instances:
2460
      pnode = instance.primary_node
2461
      if pnode not in per_node_disks:
2462
        per_node_disks[pnode] = []
2463
      for idx, disk in enumerate(instance.disks):
2464
        per_node_disks[pnode].append((instance, idx, disk))
2465

    
2466
    changed = []
2467
    for node, dskl in per_node_disks.items():
2468
      newl = [v[2].Copy() for v in dskl]
2469
      for dsk in newl:
2470
        self.cfg.SetDiskID(dsk, node)
2471
      result = self.rpc.call_blockdev_getsizes(node, newl)
2472
      if result.fail_msg:
2473
        self.LogWarning("Failure in blockdev_getsizes call to node"
2474
                        " %s, ignoring", node)
2475
        continue
2476
      if len(result.data) != len(dskl):
2477
        self.LogWarning("Invalid result from node %s, ignoring node results",
2478
                        node)
2479
        continue
2480
      for ((instance, idx, disk), size) in zip(dskl, result.data):
2481
        if size is None:
2482
          self.LogWarning("Disk %d of instance %s did not return size"
2483
                          " information, ignoring", idx, instance.name)
2484
          continue
2485
        if not isinstance(size, (int, long)):
2486
          self.LogWarning("Disk %d of instance %s did not return valid"
2487
                          " size information, ignoring", idx, instance.name)
2488
          continue
2489
        size = size >> 20
2490
        if size != disk.size:
2491
          self.LogInfo("Disk %d of instance %s has mismatched size,"
2492
                       " correcting: recorded %d, actual %d", idx,
2493
                       instance.name, disk.size, size)
2494
          disk.size = size
2495
          self.cfg.Update(instance, feedback_fn)
2496
          changed.append((instance.name, idx, size))
2497
        if self._EnsureChildSizes(disk):
2498
          self.cfg.Update(instance, feedback_fn)
2499
          changed.append((instance.name, idx, disk.size))
2500
    return changed
2501

    
2502

    
2503
class LURenameCluster(LogicalUnit):
2504
  """Rename the cluster.
2505

2506
  """
2507
  HPATH = "cluster-rename"
2508
  HTYPE = constants.HTYPE_CLUSTER
2509
  _OP_PARAMS = [("name", _NoDefault, _TNonEmptyString)]
2510

    
2511
  def BuildHooksEnv(self):
2512
    """Build hooks env.
2513

2514
    """
2515
    env = {
2516
      "OP_TARGET": self.cfg.GetClusterName(),
2517
      "NEW_NAME": self.op.name,
2518
      }
2519
    mn = self.cfg.GetMasterNode()
2520
    all_nodes = self.cfg.GetNodeList()
2521
    return env, [mn], all_nodes
2522

    
2523
  def CheckPrereq(self):
2524
    """Verify that the passed name is a valid one.
2525

2526
    """
2527
    hostname = netutils.GetHostInfo(self.op.name)
2528

    
2529
    new_name = hostname.name
2530
    self.ip = new_ip = hostname.ip
2531
    old_name = self.cfg.GetClusterName()
2532
    old_ip = self.cfg.GetMasterIP()
2533
    if new_name == old_name and new_ip == old_ip:
2534
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
2535
                                 " cluster has changed",
2536
                                 errors.ECODE_INVAL)
2537
    if new_ip != old_ip:
2538
      if netutils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
2539
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
2540
                                   " reachable on the network. Aborting." %
2541
                                   new_ip, errors.ECODE_NOTUNIQUE)
2542

    
2543
    self.op.name = new_name
2544

    
2545
  def Exec(self, feedback_fn):
2546
    """Rename the cluster.
2547

2548
    """
2549
    clustername = self.op.name
2550
    ip = self.ip
2551

    
2552
    # shutdown the master IP
2553
    master = self.cfg.GetMasterNode()
2554
    result = self.rpc.call_node_stop_master(master, False)
2555
    result.Raise("Could not disable the master role")
2556

    
2557
    try:
2558
      cluster = self.cfg.GetClusterInfo()
2559
      cluster.cluster_name = clustername
2560
      cluster.master_ip = ip
2561
      self.cfg.Update(cluster, feedback_fn)
2562

    
2563
      # update the known hosts file
2564
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2565
      node_list = self.cfg.GetNodeList()
2566
      try:
2567
        node_list.remove(master)
2568
      except ValueError:
2569
        pass
2570
      result = self.rpc.call_upload_file(node_list,
2571
                                         constants.SSH_KNOWN_HOSTS_FILE)
2572
      for to_node, to_result in result.iteritems():
2573
        msg = to_result.fail_msg
2574
        if msg:
2575
          msg = ("Copy of file %s to node %s failed: %s" %
2576
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
2577
          self.proc.LogWarning(msg)
2578

    
2579
    finally:
2580
      result = self.rpc.call_node_start_master(master, False, False)
2581
      msg = result.fail_msg
2582
      if msg:
2583
        self.LogWarning("Could not re-enable the master role on"
2584
                        " the master, please restart manually: %s", msg)
2585

    
2586

    
2587
class LUSetClusterParams(LogicalUnit):
2588
  """Change the parameters of the cluster.
2589

2590
  """
2591
  HPATH = "cluster-modify"
2592
  HTYPE = constants.HTYPE_CLUSTER
2593
  _OP_PARAMS = [
2594
    ("vg_name", None, _TMaybeString),
2595
    ("enabled_hypervisors", None,
2596
     _TOr(_TAnd(_TListOf(_TElemOf(constants.HYPER_TYPES)), _TTrue), _TNone)),
2597
    ("hvparams", None, _TOr(_TDictOf(_TNonEmptyString, _TDict), _TNone)),
2598
    ("beparams", None, _TOr(_TDictOf(_TNonEmptyString, _TDict), _TNone)),
2599
    ("os_hvp", None, _TOr(_TDictOf(_TNonEmptyString, _TDict), _TNone)),
2600
    ("osparams", None, _TOr(_TDictOf(_TNonEmptyString, _TDict), _TNone)),
2601
    ("candidate_pool_size", None, _TOr(_TStrictPositiveInt, _TNone)),
2602
    ("uid_pool", None, _NoType),
2603
    ("add_uids", None, _NoType),
2604
    ("remove_uids", None, _NoType),
2605
    ("maintain_node_health", None, _TMaybeBool),
2606
    ("nicparams", None, _TOr(_TDict, _TNone)),
2607
    ("drbd_helper", None, _TOr(_TString, _TNone)),
2608
    ("default_iallocator", None, _TMaybeString),
2609
    ]
2610
  REQ_BGL = False
2611

    
2612
  def CheckArguments(self):
2613
    """Check parameters
2614

2615
    """
2616
    if self.op.uid_pool:
2617
      uidpool.CheckUidPool(self.op.uid_pool)
2618

    
2619
    if self.op.add_uids:
2620
      uidpool.CheckUidPool(self.op.add_uids)
2621

    
2622
    if self.op.remove_uids:
2623
      uidpool.CheckUidPool(self.op.remove_uids)
2624

    
2625
  def ExpandNames(self):
2626
    # FIXME: in the future maybe other cluster params won't require checking on
2627
    # all nodes to be modified.
2628
    self.needed_locks = {
2629
      locking.LEVEL_NODE: locking.ALL_SET,
2630
    }
2631
    self.share_locks[locking.LEVEL_NODE] = 1
2632

    
2633
  def BuildHooksEnv(self):
2634
    """Build hooks env.
2635

2636
    """
2637
    env = {
2638
      "OP_TARGET": self.cfg.GetClusterName(),
2639
      "NEW_VG_NAME": self.op.vg_name,
2640
      }
2641
    mn = self.cfg.GetMasterNode()
2642
    return env, [mn], [mn]
2643

    
2644
  def CheckPrereq(self):
2645
    """Check prerequisites.
2646

2647
    This checks whether the given params don't conflict and
2648
    if the given volume group is valid.
2649

2650
    """
2651
    if self.op.vg_name is not None and not self.op.vg_name:
2652
      if self.cfg.HasAnyDiskOfType(constants.LD_LV):
2653
        raise errors.OpPrereqError("Cannot disable lvm storage while lvm-based"
2654
                                   " instances exist", errors.ECODE_INVAL)
2655

    
2656
    if self.op.drbd_helper is not None and not self.op.drbd_helper:
2657
      if self.cfg.HasAnyDiskOfType(constants.LD_DRBD8):
2658
        raise errors.OpPrereqError("Cannot disable drbd helper while"
2659
                                   " drbd-based instances exist",
2660
                                   errors.ECODE_INVAL)
2661

    
2662
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2663

    
2664
    # if vg_name not None, checks given volume group on all nodes
2665
    if self.op.vg_name:
2666
      vglist = self.rpc.call_vg_list(node_list)
2667
      for node in node_list:
2668
        msg = vglist[node].fail_msg
2669
        if msg:
2670
          # ignoring down node
2671
          self.LogWarning("Error while gathering data on node %s"
2672
                          " (ignoring node): %s", node, msg)
2673
          continue
2674
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2675
                                              self.op.vg_name,
2676
                                              constants.MIN_VG_SIZE)
2677
        if vgstatus:
2678
          raise errors.OpPrereqError("Error on node '%s': %s" %
2679
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2680

    
2681
    if self.op.drbd_helper:
2682
      # checks given drbd helper on all nodes
2683
      helpers = self.rpc.call_drbd_helper(node_list)
2684
      for node in node_list:
2685
        ninfo = self.cfg.GetNodeInfo(node)
2686
        if ninfo.offline:
2687
          self.LogInfo("Not checking drbd helper on offline node %s", node)
2688
          continue
2689
        msg = helpers[node].fail_msg
2690
        if msg:
2691
          raise errors.OpPrereqError("Error checking drbd helper on node"
2692
                                     " '%s': %s" % (node, msg),
2693
                                     errors.ECODE_ENVIRON)
2694
        node_helper = helpers[node].payload
2695
        if node_helper != self.op.drbd_helper:
2696
          raise errors.OpPrereqError("Error on node '%s': drbd helper is %s" %
2697
                                     (node, node_helper), errors.ECODE_ENVIRON)
2698

    
2699
    self.cluster = cluster = self.cfg.GetClusterInfo()
2700
    # validate params changes
2701
    if self.op.beparams:
2702
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2703
      self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
2704

    
2705
    if self.op.nicparams:
2706
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2707
      self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
2708
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2709
      nic_errors = []
2710

    
2711
      # check all instances for consistency
2712
      for instance in self.cfg.GetAllInstancesInfo().values():
2713
        for nic_idx, nic in enumerate(instance.nics):
2714
          params_copy = copy.deepcopy(nic.nicparams)
2715
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2716

    
2717
          # check parameter syntax
2718
          try:
2719
            objects.NIC.CheckParameterSyntax(params_filled)
2720
          except errors.ConfigurationError, err:
2721
            nic_errors.append("Instance %s, nic/%d: %s" %
2722
                              (instance.name, nic_idx, err))
2723

    
2724
          # if we're moving instances to routed, check that they have an ip
2725
          target_mode = params_filled[constants.NIC_MODE]
2726
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2727
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2728
                              (instance.name, nic_idx))
2729
      if nic_errors:
2730
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2731
                                   "\n".join(nic_errors))
2732

    
2733
    # hypervisor list/parameters
2734
    self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
2735
    if self.op.hvparams:
2736
      for hv_name, hv_dict in self.op.hvparams.items():
2737
        if hv_name not in self.new_hvparams:
2738
          self.new_hvparams[hv_name] = hv_dict
2739
        else:
2740
          self.new_hvparams[hv_name].update(hv_dict)
2741

    
2742
    # os hypervisor parameters
2743
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2744
    if self.op.os_hvp:
2745
      for os_name, hvs in self.op.os_hvp.items():
2746
        if os_name not in self.new_os_hvp:
2747
          self.new_os_hvp[os_name] = hvs
2748
        else:
2749
          for hv_name, hv_dict in hvs.items():
2750
            if hv_name not in self.new_os_hvp[os_name]:
2751
              self.new_os_hvp[os_name][hv_name] = hv_dict
2752
            else:
2753
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2754

    
2755
    # os parameters
2756
    self.new_osp = objects.FillDict(cluster.osparams, {})
2757
    if self.op.osparams:
2758
      for os_name, osp in self.op.osparams.items():
2759
        if os_name not in self.new_osp:
2760
          self.new_osp[os_name] = {}
2761

    
2762
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
2763
                                                  use_none=True)
2764

    
2765
        if not self.new_osp[os_name]:
2766
          # we removed all parameters
2767
          del self.new_osp[os_name]
2768
        else:
2769
          # check the parameter validity (remote check)
2770
          _CheckOSParams(self, False, [self.cfg.GetMasterNode()],
2771
                         os_name, self.new_osp[os_name])
2772

    
2773
    # changes to the hypervisor list
2774
    if self.op.enabled_hypervisors is not None:
2775
      self.hv_list = self.op.enabled_hypervisors
2776
      for hv in self.hv_list:
2777
        # if the hypervisor doesn't already exist in the cluster
2778
        # hvparams, we initialize it to empty, and then (in both
2779
        # cases) we make sure to fill the defaults, as we might not
2780
        # have a complete defaults list if the hypervisor wasn't
2781
        # enabled before
2782
        if hv not in new_hvp:
2783
          new_hvp[hv] = {}
2784
        new_hvp[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], new_hvp[hv])
2785
        utils.ForceDictType(new_hvp[hv], constants.HVS_PARAMETER_TYPES)
2786
    else:
2787
      self.hv_list = cluster.enabled_hypervisors
2788

    
2789
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2790
      # either the enabled list has changed, or the parameters have, validate
2791
      for hv_name, hv_params in self.new_hvparams.items():
2792
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2793
            (self.op.enabled_hypervisors and
2794
             hv_name in self.op.enabled_hypervisors)):
2795
          # either this is a new hypervisor, or its parameters have changed
2796
          hv_class = hypervisor.GetHypervisor(hv_name)
2797
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2798
          hv_class.CheckParameterSyntax(hv_params)
2799
          _CheckHVParams(self, node_list, hv_name, hv_params)
2800

    
2801
    if self.op.os_hvp:
2802
      # no need to check any newly-enabled hypervisors, since the
2803
      # defaults have already been checked in the above code-block
2804
      for os_name, os_hvp in self.new_os_hvp.items():
2805
        for hv_name, hv_params in os_hvp.items():
2806
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2807
          # we need to fill in the new os_hvp on top of the actual hv_p
2808
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2809
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2810
          hv_class = hypervisor.GetHypervisor(hv_name)
2811
          hv_class.CheckParameterSyntax(new_osp)
2812
          _CheckHVParams(self, node_list, hv_name, new_osp)
2813

    
2814
    if self.op.default_iallocator:
2815
      alloc_script = utils.FindFile(self.op.default_iallocator,
2816
                                    constants.IALLOCATOR_SEARCH_PATH,
2817
                                    os.path.isfile)
2818
      if alloc_script is None:
2819
        raise errors.OpPrereqError("Invalid default iallocator script '%s'"
2820
                                   " specified" % self.op.default_iallocator,
2821
                                   errors.ECODE_INVAL)
2822

    
2823
  def Exec(self, feedback_fn):
2824
    """Change the parameters of the cluster.
2825

2826
    """
2827
    if self.op.vg_name is not None:
2828
      new_volume = self.op.vg_name
2829
      if not new_volume:
2830
        new_volume = None
2831
      if new_volume != self.cfg.GetVGName():
2832
        self.cfg.SetVGName(new_volume)
2833
      else:
2834
        feedback_fn("Cluster LVM configuration already in desired"
2835
                    " state, not changing")
2836
    if self.op.drbd_helper is not None:
2837
      new_helper = self.op.drbd_helper
2838
      if not new_helper:
2839
        new_helper = None
2840
      if new_helper != self.cfg.GetDRBDHelper():
2841
        self.cfg.SetDRBDHelper(new_helper)
2842
      else:
2843
        feedback_fn("Cluster DRBD helper already in desired state,"
2844
                    " not changing")
2845
    if self.op.hvparams:
2846
      self.cluster.hvparams = self.new_hvparams
2847
    if self.op.os_hvp:
2848
      self.cluster.os_hvp = self.new_os_hvp
2849
    if self.op.enabled_hypervisors is not None:
2850
      self.cluster.hvparams = self.new_hvparams
2851
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2852
    if self.op.beparams:
2853
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2854
    if self.op.nicparams:
2855
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2856
    if self.op.osparams:
2857
      self.cluster.osparams = self.new_osp
2858

    
2859
    if self.op.candidate_pool_size is not None:
2860
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2861
      # we need to update the pool size here, otherwise the save will fail
2862
      _AdjustCandidatePool(self, [])
2863

    
2864
    if self.op.maintain_node_health is not None:
2865
      self.cluster.maintain_node_health = self.op.maintain_node_health
2866

    
2867
    if self.op.add_uids is not None:
2868
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
2869

    
2870
    if self.op.remove_uids is not None:
2871
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
2872

    
2873
    if self.op.uid_pool is not None:
2874
      self.cluster.uid_pool = self.op.uid_pool
2875

    
2876
    if self.op.default_iallocator is not None:
2877
      self.cluster.default_iallocator = self.op.default_iallocator
2878

    
2879
    self.cfg.Update(self.cluster, feedback_fn)
2880

    
2881

    
2882
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2883
  """Distribute additional files which are part of the cluster configuration.
2884

2885
  ConfigWriter takes care of distributing the config and ssconf files, but
2886
  there are more files which should be distributed to all nodes. This function
2887
  makes sure those are copied.
2888

2889
  @param lu: calling logical unit
2890
  @param additional_nodes: list of nodes not in the config to distribute to
2891

2892
  """
2893
  # 1. Gather target nodes
2894
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2895
  dist_nodes = lu.cfg.GetOnlineNodeList()
2896
  if additional_nodes is not None:
2897
    dist_nodes.extend(additional_nodes)
2898
  if myself.name in dist_nodes:
2899
    dist_nodes.remove(myself.name)
2900

    
2901
  # 2. Gather files to distribute
2902
  dist_files = set([constants.ETC_HOSTS,
2903
                    constants.SSH_KNOWN_HOSTS_FILE,
2904
                    constants.RAPI_CERT_FILE,
2905
                    constants.RAPI_USERS_FILE,
2906
                    constants.CONFD_HMAC_KEY,
2907
                    constants.CLUSTER_DOMAIN_SECRET_FILE,
2908
                   ])
2909

    
2910
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2911
  for hv_name in enabled_hypervisors:
2912
    hv_class = hypervisor.GetHypervisor(hv_name)
2913
    dist_files.update(hv_class.GetAncillaryFiles())
2914

    
2915
  # 3. Perform the files upload
2916
  for fname in dist_files:
2917
    if os.path.exists(fname):
2918
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2919
      for to_node, to_result in result.items():
2920
        msg = to_result.fail_msg
2921
        if msg:
2922
          msg = ("Copy of file %s to node %s failed: %s" %
2923
                 (fname, to_node, msg))
2924
          lu.proc.LogWarning(msg)
2925

    
2926

    
2927
class LURedistributeConfig(NoHooksLU):
2928
  """Force the redistribution of cluster configuration.
2929

2930
  This is a very simple LU.
2931

2932
  """
2933
  REQ_BGL = False
2934

    
2935
  def ExpandNames(self):
2936
    self.needed_locks = {
2937
      locking.LEVEL_NODE: locking.ALL_SET,
2938
    }
2939
    self.share_locks[locking.LEVEL_NODE] = 1
2940

    
2941
  def Exec(self, feedback_fn):
2942
    """Redistribute the configuration.
2943

2944
    """
2945
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2946
    _RedistributeAncillaryFiles(self)
2947

    
2948

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

2952
  """
2953
  if not instance.disks or disks is not None and not disks:
2954
    return True
2955

    
2956
  disks = _ExpandCheckDisks(instance, disks)
2957

    
2958
  if not oneshot:
2959
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2960

    
2961
  node = instance.primary_node
2962

    
2963
  for dev in disks:
2964
    lu.cfg.SetDiskID(dev, node)
2965

    
2966
  # TODO: Convert to utils.Retry
2967

    
2968
  retries = 0
2969
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2970
  while True:
2971
    max_time = 0
2972
    done = True
2973
    cumul_degraded = False
2974
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, disks)
2975
    msg = rstats.fail_msg
2976
    if msg:
2977
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2978
      retries += 1
2979
      if retries >= 10:
2980
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2981
                                 " aborting." % node)
2982
      time.sleep(6)
2983
      continue
2984
    rstats = rstats.payload
2985
    retries = 0
2986
    for i, mstat in enumerate(rstats):
2987
      if mstat is None:
2988
        lu.LogWarning("Can't compute data for node %s/%s",
2989
                           node, disks[i].iv_name)
2990
        continue
2991

    
2992
      cumul_degraded = (cumul_degraded or
2993
                        (mstat.is_degraded and mstat.sync_percent is None))
2994
      if mstat.sync_percent is not None:
2995
        done = False
2996
        if mstat.estimated_time is not None:
2997
          rem_time = ("%s remaining (estimated)" %
2998
                      utils.FormatSeconds(mstat.estimated_time))
2999
          max_time = mstat.estimated_time
3000
        else:
3001
          rem_time = "no time estimate"
3002
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
3003
                        (disks[i].iv_name, mstat.sync_percent, rem_time))
3004

    
3005
    # if we're done but degraded, let's do a few small retries, to
3006
    # make sure we see a stable and not transient situation; therefore
3007
    # we force restart of the loop
3008
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
3009
      logging.info("Degraded disks found, %d retries left", degr_retries)
3010
      degr_retries -= 1
3011
      time.sleep(1)
3012
      continue
3013

    
3014
    if done or oneshot:
3015
      break
3016

    
3017
    time.sleep(min(60, max_time))
3018

    
3019
  if done:
3020
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
3021
  return not cumul_degraded
3022

    
3023

    
3024
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
3025
  """Check that mirrors are not degraded.
3026

3027
  The ldisk parameter, if True, will change the test from the
3028
  is_degraded attribute (which represents overall non-ok status for
3029
  the device(s)) to the ldisk (representing the local storage status).
3030

3031
  """
3032
  lu.cfg.SetDiskID(dev, node)
3033

    
3034
  result = True
3035

    
3036
  if on_primary or dev.AssembleOnSecondary():
3037
    rstats = lu.rpc.call_blockdev_find(node, dev)
3038
    msg = rstats.fail_msg
3039
    if msg:
3040
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
3041
      result = False
3042
    elif not rstats.payload:
3043
      lu.LogWarning("Can't find disk on node %s", node)
3044
      result = False
3045
    else:
3046
      if ldisk:
3047
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
3048
      else:
3049
        result = result and not rstats.payload.is_degraded
3050

    
3051
  if dev.children:
3052
    for child in dev.children:
3053
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
3054

    
3055
  return result
3056

    
3057

    
3058
class LUDiagnoseOS(NoHooksLU):
3059
  """Logical unit for OS diagnose/query.
3060

3061
  """
3062
  _OP_PARAMS = [
3063
    _POutputFields,
3064
    ("names", _EmptyList, _TListOf(_TNonEmptyString)),
3065
    ]
3066
  REQ_BGL = False
3067
  _FIELDS_STATIC = utils.FieldSet()
3068
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants",
3069
                                   "parameters", "api_versions")
3070

    
3071
  def CheckArguments(self):
3072
    if self.op.names:
3073
      raise errors.OpPrereqError("Selective OS query not supported",
3074
                                 errors.ECODE_INVAL)
3075

    
3076
    _CheckOutputFields(static=self._FIELDS_STATIC,
3077
                       dynamic=self._FIELDS_DYNAMIC,
3078
                       selected=self.op.output_fields)
3079

    
3080
  def ExpandNames(self):
3081
    # Lock all nodes, in shared mode
3082
    # Temporary removal of locks, should be reverted later
3083
    # TODO: reintroduce locks when they are lighter-weight
3084
    self.needed_locks = {}
3085
    #self.share_locks[locking.LEVEL_NODE] = 1
3086
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3087

    
3088
  @staticmethod
3089
  def _DiagnoseByOS(rlist):
3090
    """Remaps a per-node return list into an a per-os per-node dictionary
3091

3092
    @param rlist: a map with node names as keys and OS objects as values
3093

3094
    @rtype: dict
3095
    @return: a dictionary with osnames as keys and as value another
3096
        map, with nodes as keys and tuples of (path, status, diagnose,
3097
        variants, parameters, api_versions) as values, eg::
3098

3099
          {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
3100
                                     (/srv/..., False, "invalid api")],
3101
                           "node2": [(/srv/..., True, "", [], [])]}
3102
          }
3103

3104
    """
3105
    all_os = {}
3106
    # we build here the list of nodes that didn't fail the RPC (at RPC
3107
    # level), so that nodes with a non-responding node daemon don't
3108
    # make all OSes invalid
3109
    good_nodes = [node_name for node_name in rlist
3110
                  if not rlist[node_name].fail_msg]
3111
    for node_name, nr in rlist.items():
3112
      if nr.fail_msg or not nr.payload:
3113
        continue
3114
      for (name, path, status, diagnose, variants,
3115
           params, api_versions) in nr.payload:
3116
        if name not in all_os:
3117
          # build a list of nodes for this os containing empty lists
3118
          # for each node in node_list
3119
          all_os[name] = {}
3120
          for nname in good_nodes:
3121
            all_os[name][nname] = []
3122
        # convert params from [name, help] to (name, help)
3123
        params = [tuple(v) for v in params]
3124
        all_os[name][node_name].append((path, status, diagnose,
3125
                                        variants, params, api_versions))
3126
    return all_os
3127

    
3128
  def Exec(self, feedback_fn):
3129
    """Compute the list of OSes.
3130

3131
    """
3132
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
3133
    node_data = self.rpc.call_os_diagnose(valid_nodes)
3134
    pol = self._DiagnoseByOS(node_data)
3135
    output = []
3136

    
3137
    for os_name, os_data in pol.items():
3138
      row = []
3139
      valid = True
3140
      (variants, params, api_versions) = null_state = (set(), set(), set())
3141
      for idx, osl in enumerate(os_data.values()):
3142
        valid = bool(valid and osl and osl[0][1])
3143
        if not valid:
3144
          (variants, params, api_versions) = null_state
3145
          break
3146
        node_variants, node_params, node_api = osl[0][3:6]
3147
        if idx == 0: # first entry
3148
          variants = set(node_variants)
3149
          params = set(node_params)
3150
          api_versions = set(node_api)
3151
        else: # keep consistency
3152
          variants.intersection_update(node_variants)
3153
          params.intersection_update(node_params)
3154
          api_versions.intersection_update(node_api)
3155

    
3156
      for field in self.op.output_fields:
3157
        if field == "name":
3158
          val = os_name
3159
        elif field == "valid":
3160
          val = valid
3161
        elif field == "node_status":
3162
          # this is just a copy of the dict
3163
          val = {}
3164
          for node_name, nos_list in os_data.items():
3165
            val[node_name] = nos_list
3166
        elif field == "variants":
3167
          val = list(variants)
3168
        elif field == "parameters":
3169
          val = list(params)
3170
        elif field == "api_versions":
3171
          val = list(api_versions)
3172
        else:
3173
          raise errors.ParameterError(field)
3174
        row.append(val)
3175
      output.append(row)
3176

    
3177
    return output
3178

    
3179

    
3180
class LURemoveNode(LogicalUnit):
3181
  """Logical unit for removing a node.
3182

3183
  """
3184
  HPATH = "node-remove"
3185
  HTYPE = constants.HTYPE_NODE
3186
  _OP_PARAMS = [
3187
    _PNodeName,
3188
    ]
3189

    
3190
  def BuildHooksEnv(self):
3191
    """Build hooks env.
3192

3193
    This doesn't run on the target node in the pre phase as a failed
3194
    node would then be impossible to remove.
3195

3196
    """
3197
    env = {
3198
      "OP_TARGET": self.op.node_name,
3199
      "NODE_NAME": self.op.node_name,
3200
      }
3201
    all_nodes = self.cfg.GetNodeList()
3202
    try:
3203
      all_nodes.remove(self.op.node_name)
3204
    except ValueError:
3205
      logging.warning("Node %s which is about to be removed not found"
3206
                      " in the all nodes list", self.op.node_name)
3207
    return env, all_nodes, all_nodes
3208

    
3209
  def CheckPrereq(self):
3210
    """Check prerequisites.
3211

3212
    This checks:
3213
     - the node exists in the configuration
3214
     - it does not have primary or secondary instances
3215
     - it's not the master
3216

3217
    Any errors are signaled by raising errors.OpPrereqError.
3218

3219
    """
3220
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3221
    node = self.cfg.GetNodeInfo(self.op.node_name)
3222
    assert node is not None
3223

    
3224
    instance_list = self.cfg.GetInstanceList()
3225

    
3226
    masternode = self.cfg.GetMasterNode()
3227
    if node.name == masternode:
3228
      raise errors.OpPrereqError("Node is the master node,"
3229
                                 " you need to failover first.",
3230
                                 errors.ECODE_INVAL)
3231

    
3232
    for instance_name in instance_list:
3233
      instance = self.cfg.GetInstanceInfo(instance_name)
3234
      if node.name in instance.all_nodes:
3235
        raise errors.OpPrereqError("Instance %s is still running on the node,"
3236
                                   " please remove first." % instance_name,
3237
                                   errors.ECODE_INVAL)
3238
    self.op.node_name = node.name
3239
    self.node = node
3240

    
3241
  def Exec(self, feedback_fn):
3242
    """Removes the node from the cluster.
3243

3244
    """
3245
    node = self.node
3246
    logging.info("Stopping the node daemon and removing configs from node %s",
3247
                 node.name)
3248

    
3249
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3250

    
3251
    # Promote nodes to master candidate as needed
3252
    _AdjustCandidatePool(self, exceptions=[node.name])
3253
    self.context.RemoveNode(node.name)
3254

    
3255
    # Run post hooks on the node before it's removed
3256
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
3257
    try:
3258
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
3259
    except:
3260
      # pylint: disable-msg=W0702
3261
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
3262

    
3263
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
3264
    msg = result.fail_msg
3265
    if msg:
3266
      self.LogWarning("Errors encountered on the remote node while leaving"
3267
                      " the cluster: %s", msg)
3268

    
3269
    # Remove node from our /etc/hosts
3270
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3271
      # FIXME: this should be done via an rpc call to node daemon
3272
      utils.RemoveHostFromEtcHosts(node.name)
3273
      _RedistributeAncillaryFiles(self)
3274

    
3275

    
3276
class LUQueryNodes(NoHooksLU):
3277
  """Logical unit for querying nodes.
3278

3279
  """
3280
  # pylint: disable-msg=W0142
3281
  _OP_PARAMS = [
3282
    _POutputFields,
3283
    ("names", _EmptyList, _TListOf(_TNonEmptyString)),
3284
    ("use_locking", False, _TBool),
3285
    ]
3286
  REQ_BGL = False
3287

    
3288
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
3289
                    "master_candidate", "offline", "drained"]
3290

    
3291
  _FIELDS_DYNAMIC = utils.FieldSet(
3292
    "dtotal", "dfree",
3293
    "mtotal", "mnode", "mfree",
3294
    "bootid",
3295
    "ctotal", "cnodes", "csockets",
3296
    )
3297

    
3298
  _FIELDS_STATIC = utils.FieldSet(*[
3299
    "pinst_cnt", "sinst_cnt",
3300
    "pinst_list", "sinst_list",
3301
    "pip", "sip", "tags",
3302
    "master",
3303
    "role"] + _SIMPLE_FIELDS
3304
    )
3305

    
3306
  def CheckArguments(self):
3307
    _CheckOutputFields(static=self._FIELDS_STATIC,
3308
                       dynamic=self._FIELDS_DYNAMIC,
3309
                       selected=self.op.output_fields)
3310

    
3311
  def ExpandNames(self):
3312
    self.needed_locks = {}
3313
    self.share_locks[locking.LEVEL_NODE] = 1
3314

    
3315
    if self.op.names:
3316
      self.wanted = _GetWantedNodes(self, self.op.names)
3317
    else:
3318
      self.wanted = locking.ALL_SET
3319

    
3320
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3321
    self.do_locking = self.do_node_query and self.op.use_locking
3322
    if self.do_locking:
3323
      # if we don't request only static fields, we need to lock the nodes
3324
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
3325

    
3326
  def Exec(self, feedback_fn):
3327
    """Computes the list of nodes and their attributes.
3328

3329
    """
3330
    all_info = self.cfg.GetAllNodesInfo()
3331
    if self.do_locking:
3332
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
3333
    elif self.wanted != locking.ALL_SET:
3334
      nodenames = self.wanted
3335
      missing = set(nodenames).difference(all_info.keys())
3336
      if missing:
3337
        raise errors.OpExecError(
3338
          "Some nodes were removed before retrieving their data: %s" % missing)
3339
    else:
3340
      nodenames = all_info.keys()
3341

    
3342
    nodenames = utils.NiceSort(nodenames)
3343
    nodelist = [all_info[name] for name in nodenames]
3344

    
3345
    # begin data gathering
3346

    
3347
    if self.do_node_query:
3348
      live_data = {}
3349
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
3350
                                          self.cfg.GetHypervisorType())
3351
      for name in nodenames:
3352
        nodeinfo = node_data[name]
3353
        if not nodeinfo.fail_msg and nodeinfo.payload:
3354
          nodeinfo = nodeinfo.payload
3355
          fn = utils.TryConvert
3356
          live_data[name] = {
3357
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
3358
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
3359
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
3360
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
3361
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
3362
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
3363
            "bootid": nodeinfo.get('bootid', None),
3364
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
3365
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
3366
            }
3367
        else:
3368
          live_data[name] = {}
3369
    else:
3370
      live_data = dict.fromkeys(nodenames, {})
3371

    
3372
    node_to_primary = dict([(name, set()) for name in nodenames])
3373
    node_to_secondary = dict([(name, set()) for name in nodenames])
3374

    
3375
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
3376
                             "sinst_cnt", "sinst_list"))
3377
    if inst_fields & frozenset(self.op.output_fields):
3378
      inst_data = self.cfg.GetAllInstancesInfo()
3379

    
3380
      for inst in inst_data.values():
3381
        if inst.primary_node in node_to_primary:
3382
          node_to_primary[inst.primary_node].add(inst.name)
3383
        for secnode in inst.secondary_nodes:
3384
          if secnode in node_to_secondary:
3385
            node_to_secondary[secnode].add(inst.name)
3386

    
3387
    master_node = self.cfg.GetMasterNode()
3388

    
3389
    # end data gathering
3390

    
3391
    output = []
3392
    for node in nodelist:
3393
      node_output = []
3394
      for field in self.op.output_fields:
3395
        if field in self._SIMPLE_FIELDS:
3396
          val = getattr(node, field)
3397
        elif field == "pinst_list":
3398
          val = list(node_to_primary[node.name])
3399
        elif field == "sinst_list":
3400
          val = list(node_to_secondary[node.name])
3401
        elif field == "pinst_cnt":
3402
          val = len(node_to_primary[node.name])
3403
        elif field == "sinst_cnt":
3404
          val = len(node_to_secondary[node.name])
3405
        elif field == "pip":
3406
          val = node.primary_ip
3407
        elif field == "sip":
3408
          val = node.secondary_ip
3409
        elif field == "tags":
3410
          val = list(node.GetTags())
3411
        elif field == "master":
3412
          val = node.name == master_node
3413
        elif self._FIELDS_DYNAMIC.Matches(field):
3414
          val = live_data[node.name].get(field, None)
3415
        elif field == "role":
3416
          if node.name == master_node:
3417
            val = "M"
3418
          elif node.master_candidate:
3419
            val = "C"
3420
          elif node.drained:
3421
            val = "D"
3422
          elif node.offline:
3423
            val = "O"
3424
          else:
3425
            val = "R"
3426
        else:
3427
          raise errors.ParameterError(field)
3428
        node_output.append(val)
3429
      output.append(node_output)
3430

    
3431
    return output
3432

    
3433

    
3434
class LUQueryNodeVolumes(NoHooksLU):
3435
  """Logical unit for getting volumes on node(s).
3436

3437
  """
3438
  _OP_PARAMS = [
3439
    ("nodes", _EmptyList, _TListOf(_TNonEmptyString)),
3440
    ("output_fields", _NoDefault, _TListOf(_TNonEmptyString)),
3441
    ]
3442
  REQ_BGL = False
3443
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3444
  _FIELDS_STATIC = utils.FieldSet("node")
3445

    
3446
  def CheckArguments(self):
3447
    _CheckOutputFields(static=self._FIELDS_STATIC,
3448
                       dynamic=self._FIELDS_DYNAMIC,
3449
                       selected=self.op.output_fields)
3450

    
3451
  def ExpandNames(self):
3452
    self.needed_locks = {}
3453
    self.share_locks[locking.LEVEL_NODE] = 1
3454
    if not self.op.nodes:
3455
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3456
    else:
3457
      self.needed_locks[locking.LEVEL_NODE] = \
3458
        _GetWantedNodes(self, self.op.nodes)
3459

    
3460
  def Exec(self, feedback_fn):
3461
    """Computes the list of nodes and their attributes.
3462

3463
    """
3464
    nodenames = self.acquired_locks[locking.LEVEL_NODE]
3465
    volumes = self.rpc.call_node_volumes(nodenames)
3466

    
3467
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3468
             in self.cfg.GetInstanceList()]
3469

    
3470
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3471

    
3472
    output = []
3473
    for node in nodenames:
3474
      nresult = volumes[node]
3475
      if nresult.offline:
3476
        continue
3477
      msg = nresult.fail_msg
3478
      if msg:
3479
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3480
        continue
3481

    
3482
      node_vols = nresult.payload[:]
3483
      node_vols.sort(key=lambda vol: vol['dev'])
3484

    
3485
      for vol in node_vols:
3486
        node_output = []
3487
        for field in self.op.output_fields:
3488
          if field == "node":
3489
            val = node
3490
          elif field == "phys":
3491
            val = vol['dev']
3492
          elif field == "vg":
3493
            val = vol['vg']
3494
          elif field == "name":
3495
            val = vol['name']
3496
          elif field == "size":
3497
            val = int(float(vol['size']))
3498
          elif field == "instance":
3499
            for inst in ilist:
3500
              if node not in lv_by_node[inst]:
3501
                continue
3502
              if vol['name'] in lv_by_node[inst][node]:
3503
                val = inst.name
3504
                break
3505
            else:
3506
              val = '-'
3507
          else:
3508
            raise errors.ParameterError(field)
3509
          node_output.append(str(val))
3510

    
3511
        output.append(node_output)
3512

    
3513
    return output
3514

    
3515

    
3516
class LUQueryNodeStorage(NoHooksLU):
3517
  """Logical unit for getting information on storage units on node(s).
3518

3519
  """
3520
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3521
  _OP_PARAMS = [
3522
    ("nodes", _EmptyList, _TListOf(_TNonEmptyString)),
3523
    ("storage_type", _NoDefault, _CheckStorageType),
3524
    ("output_fields", _NoDefault, _TListOf(_TNonEmptyString)),
3525
    ("name", None, _TMaybeString),
3526
    ]
3527
  REQ_BGL = False
3528

    
3529
  def CheckArguments(self):
3530
    _CheckOutputFields(static=self._FIELDS_STATIC,
3531
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3532
                       selected=self.op.output_fields)
3533

    
3534
  def ExpandNames(self):
3535
    self.needed_locks = {}
3536
    self.share_locks[locking.LEVEL_NODE] = 1
3537

    
3538
    if self.op.nodes:
3539
      self.needed_locks[locking.LEVEL_NODE] = \
3540
        _GetWantedNodes(self, self.op.nodes)
3541
    else:
3542
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3543

    
3544
  def Exec(self, feedback_fn):
3545
    """Computes the list of nodes and their attributes.
3546

3547
    """
3548
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3549

    
3550
    # Always get name to sort by
3551
    if constants.SF_NAME in self.op.output_fields:
3552
      fields = self.op.output_fields[:]
3553
    else:
3554
      fields = [constants.SF_NAME] + self.op.output_fields
3555

    
3556
    # Never ask for node or type as it's only known to the LU
3557
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3558
      while extra in fields:
3559
        fields.remove(extra)
3560

    
3561
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3562
    name_idx = field_idx[constants.SF_NAME]
3563

    
3564
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3565
    data = self.rpc.call_storage_list(self.nodes,
3566
                                      self.op.storage_type, st_args,
3567
                                      self.op.name, fields)
3568

    
3569
    result = []
3570

    
3571
    for node in utils.NiceSort(self.nodes):
3572
      nresult = data[node]
3573
      if nresult.offline:
3574
        continue
3575

    
3576
      msg = nresult.fail_msg
3577
      if msg:
3578
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3579
        continue
3580

    
3581
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3582

    
3583
      for name in utils.NiceSort(rows.keys()):
3584
        row = rows[name]
3585

    
3586
        out = []
3587

    
3588
        for field in self.op.output_fields:
3589
          if field == constants.SF_NODE:
3590
            val = node
3591
          elif field == constants.SF_TYPE:
3592
            val = self.op.storage_type
3593
          elif field in field_idx:
3594
            val = row[field_idx[field]]
3595
          else:
3596
            raise errors.ParameterError(field)
3597

    
3598
          out.append(val)
3599

    
3600
        result.append(out)
3601

    
3602
    return result
3603

    
3604

    
3605
class LUModifyNodeStorage(NoHooksLU):
3606
  """Logical unit for modifying a storage volume on a node.
3607

3608
  """
3609
  _OP_PARAMS = [
3610
    _PNodeName,
3611
    ("storage_type", _NoDefault, _CheckStorageType),
3612
    ("name", _NoDefault, _TNonEmptyString),
3613
    ("changes", _NoDefault, _TDict),
3614
    ]
3615
  REQ_BGL = False
3616

    
3617
  def CheckArguments(self):
3618
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3619

    
3620
    storage_type = self.op.storage_type
3621

    
3622
    try:
3623
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3624
    except KeyError:
3625
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3626
                                 " modified" % storage_type,
3627
                                 errors.ECODE_INVAL)
3628

    
3629
    diff = set(self.op.changes.keys()) - modifiable
3630
    if diff:
3631
      raise errors.OpPrereqError("The following fields can not be modified for"
3632
                                 " storage units of type '%s': %r" %
3633
                                 (storage_type, list(diff)),
3634
                                 errors.ECODE_INVAL)
3635

    
3636
  def ExpandNames(self):
3637
    self.needed_locks = {
3638
      locking.LEVEL_NODE: self.op.node_name,
3639
      }
3640

    
3641
  def Exec(self, feedback_fn):
3642
    """Computes the list of nodes and their attributes.
3643

3644
    """
3645
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3646
    result = self.rpc.call_storage_modify(self.op.node_name,
3647
                                          self.op.storage_type, st_args,
3648
                                          self.op.name, self.op.changes)
3649
    result.Raise("Failed to modify storage unit '%s' on %s" %
3650
                 (self.op.name, self.op.node_name))
3651

    
3652

    
3653
class LUAddNode(LogicalUnit):
3654
  """Logical unit for adding node to the cluster.
3655

3656
  """
3657
  HPATH = "node-add"
3658
  HTYPE = constants.HTYPE_NODE
3659
  _OP_PARAMS = [
3660
    _PNodeName,
3661
    ("primary_ip", None, _NoType),
3662
    ("secondary_ip", None, _TMaybeString),
3663
    ("readd", False, _TBool),
3664
    ]
3665

    
3666
  def CheckArguments(self):
3667
    # validate/normalize the node name
3668
    self.op.node_name = netutils.HostInfo.NormalizeName(self.op.node_name)
3669

    
3670
  def BuildHooksEnv(self):
3671
    """Build hooks env.
3672

3673
    This will run on all nodes before, and on all nodes + the new node after.
3674

3675
    """
3676
    env = {
3677
      "OP_TARGET": self.op.node_name,
3678
      "NODE_NAME": self.op.node_name,
3679
      "NODE_PIP": self.op.primary_ip,
3680
      "NODE_SIP": self.op.secondary_ip,
3681
      }
3682
    nodes_0 = self.cfg.GetNodeList()
3683
    nodes_1 = nodes_0 + [self.op.node_name, ]
3684
    return env, nodes_0, nodes_1
3685

    
3686
  def CheckPrereq(self):
3687
    """Check prerequisites.
3688

3689
    This checks:
3690
     - the new node is not already in the config
3691
     - it is resolvable
3692
     - its parameters (single/dual homed) matches the cluster
3693

3694
    Any errors are signaled by raising errors.OpPrereqError.
3695

3696
    """
3697
    node_name = self.op.node_name
3698
    cfg = self.cfg
3699

    
3700
    dns_data = netutils.GetHostInfo(node_name)
3701

    
3702
    node = dns_data.name
3703
    primary_ip = self.op.primary_ip = dns_data.ip
3704
    if self.op.secondary_ip is None:
3705
      self.op.secondary_ip = primary_ip
3706
    if not netutils.IsValidIP4(self.op.secondary_ip):
3707
      raise errors.OpPrereqError("Invalid secondary IP given",
3708
                                 errors.ECODE_INVAL)
3709
    secondary_ip = self.op.secondary_ip
3710

    
3711
    node_list = cfg.GetNodeList()
3712
    if not self.op.readd and node in node_list:
3713
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3714
                                 node, errors.ECODE_EXISTS)
3715
    elif self.op.readd and node not in node_list:
3716
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3717
                                 errors.ECODE_NOENT)
3718

    
3719
    self.changed_primary_ip = False
3720

    
3721
    for existing_node_name in node_list:
3722
      existing_node = cfg.GetNodeInfo(existing_node_name)
3723

    
3724
      if self.op.readd and node == existing_node_name:
3725
        if existing_node.secondary_ip != secondary_ip:
3726
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3727
                                     " address configuration as before",
3728
                                     errors.ECODE_INVAL)
3729
        if existing_node.primary_ip != primary_ip:
3730
          self.changed_primary_ip = True
3731

    
3732
        continue
3733

    
3734
      if (existing_node.primary_ip == primary_ip or
3735
          existing_node.secondary_ip == primary_ip or
3736
          existing_node.primary_ip == secondary_ip or
3737
          existing_node.secondary_ip == secondary_ip):
3738
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3739
                                   " existing node %s" % existing_node.name,
3740
                                   errors.ECODE_NOTUNIQUE)
3741

    
3742
    # check that the type of the node (single versus dual homed) is the
3743
    # same as for the master
3744
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
3745
    master_singlehomed = myself.secondary_ip == myself.primary_ip
3746
    newbie_singlehomed = secondary_ip == primary_ip
3747
    if master_singlehomed != newbie_singlehomed:
3748
      if master_singlehomed:
3749
        raise errors.OpPrereqError("The master has no private ip but the"
3750
                                   " new node has one",
3751
                                   errors.ECODE_INVAL)
3752
      else:
3753
        raise errors.OpPrereqError("The master has a private ip but the"
3754
                                   " new node doesn't have one",
3755
                                   errors.ECODE_INVAL)
3756

    
3757
    # checks reachability
3758
    if not netutils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3759
      raise errors.OpPrereqError("Node not reachable by ping",
3760
                                 errors.ECODE_ENVIRON)
3761

    
3762
    if not newbie_singlehomed:
3763
      # check reachability from my secondary ip to newbie's secondary ip
3764
      if not netutils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3765
                           source=myself.secondary_ip):
3766
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3767
                                   " based ping to noded port",
3768
                                   errors.ECODE_ENVIRON)
3769

    
3770
    if self.op.readd:
3771
      exceptions = [node]
3772
    else:
3773
      exceptions = []
3774

    
3775
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3776

    
3777
    if self.op.readd:
3778
      self.new_node = self.cfg.GetNodeInfo(node)
3779
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3780
    else:
3781
      self.new_node = objects.Node(name=node,
3782
                                   primary_ip=primary_ip,
3783
                                   secondary_ip=secondary_ip,
3784
                                   master_candidate=self.master_candidate,
3785
                                   offline=False, drained=False)
3786

    
3787
  def Exec(self, feedback_fn):
3788
    """Adds the new node to the cluster.
3789

3790
    """
3791
    new_node = self.new_node
3792
    node = new_node.name
3793

    
3794
    # for re-adds, reset the offline/drained/master-candidate flags;
3795
    # we need to reset here, otherwise offline would prevent RPC calls
3796
    # later in the procedure; this also means that if the re-add
3797
    # fails, we are left with a non-offlined, broken node
3798
    if self.op.readd:
3799
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
3800
      self.LogInfo("Readding a node, the offline/drained flags were reset")
3801
      # if we demote the node, we do cleanup later in the procedure
3802
      new_node.master_candidate = self.master_candidate
3803
      if self.changed_primary_ip:
3804
        new_node.primary_ip = self.op.primary_ip
3805

    
3806
    # notify the user about any possible mc promotion
3807
    if new_node.master_candidate:
3808
      self.LogInfo("Node will be a master candidate")
3809

    
3810
    # check connectivity
3811
    result = self.rpc.call_version([node])[node]
3812
    result.Raise("Can't get version information from node %s" % node)
3813
    if constants.PROTOCOL_VERSION == result.payload:
3814
      logging.info("Communication to node %s fine, sw version %s match",
3815
                   node, result.payload)
3816
    else:
3817
      raise errors.OpExecError("Version mismatch master version %s,"
3818
                               " node version %s" %
3819
                               (constants.PROTOCOL_VERSION, result.payload))
3820

    
3821
    # setup ssh on node
3822
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3823
      logging.info("Copy ssh key to node %s", node)
3824
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3825
      keyarray = []
3826
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3827
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3828
                  priv_key, pub_key]
3829

    
3830
      for i in keyfiles:
3831
        keyarray.append(utils.ReadFile(i))
3832

    
3833
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3834
                                      keyarray[2], keyarray[3], keyarray[4],
3835
                                      keyarray[5])
3836
      result.Raise("Cannot transfer ssh keys to the new node")
3837

    
3838
    # Add node to our /etc/hosts, and add key to known_hosts
3839
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3840
      # FIXME: this should be done via an rpc call to node daemon
3841
      utils.AddHostToEtcHosts(new_node.name)
3842

    
3843
    if new_node.secondary_ip != new_node.primary_ip:
3844
      result = self.rpc.call_node_has_ip_address(new_node.name,
3845
                                                 new_node.secondary_ip)
3846
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3847
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3848
      if not result.payload:
3849
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3850
                                 " you gave (%s). Please fix and re-run this"
3851
                                 " command." % new_node.secondary_ip)
3852

    
3853
    node_verify_list = [self.cfg.GetMasterNode()]
3854
    node_verify_param = {
3855
      constants.NV_NODELIST: [node],
3856
      # TODO: do a node-net-test as well?
3857
    }
3858

    
3859
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3860
                                       self.cfg.GetClusterName())
3861
    for verifier in node_verify_list:
3862
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3863
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3864
      if nl_payload:
3865
        for failed in nl_payload:
3866
          feedback_fn("ssh/hostname verification failed"
3867
                      " (checking from %s): %s" %
3868
                      (verifier, nl_payload[failed]))
3869
        raise errors.OpExecError("ssh/hostname verification failed.")
3870

    
3871
    if self.op.readd:
3872
      _RedistributeAncillaryFiles(self)
3873
      self.context.ReaddNode(new_node)
3874
      # make sure we redistribute the config
3875
      self.cfg.Update(new_node, feedback_fn)
3876
      # and make sure the new node will not have old files around
3877
      if not new_node.master_candidate:
3878
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3879
        msg = result.fail_msg
3880
        if msg:
3881
          self.LogWarning("Node failed to demote itself from master"
3882
                          " candidate status: %s" % msg)
3883
    else:
3884
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3885
      self.context.AddNode(new_node, self.proc.GetECId())
3886

    
3887

    
3888
class LUSetNodeParams(LogicalUnit):
3889
  """Modifies the parameters of a node.
3890

3891
  """
3892
  HPATH = "node-modify"
3893
  HTYPE = constants.HTYPE_NODE
3894
  _OP_PARAMS = [
3895
    _PNodeName,
3896
    ("master_candidate", None, _TMaybeBool),
3897
    ("offline", None, _TMaybeBool),
3898
    ("drained", None, _TMaybeBool),
3899
    ("auto_promote", False, _TBool),
3900
    _PForce,
3901
    ]
3902
  REQ_BGL = False
3903

    
3904
  def CheckArguments(self):
3905
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3906
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3907
    if all_mods.count(None) == 3:
3908
      raise errors.OpPrereqError("Please pass at least one modification",
3909
                                 errors.ECODE_INVAL)
3910
    if all_mods.count(True) > 1:
3911
      raise errors.OpPrereqError("Can't set the node into more than one"
3912
                                 " state at the same time",
3913
                                 errors.ECODE_INVAL)
3914

    
3915
    # Boolean value that tells us whether we're offlining or draining the node
3916
    self.offline_or_drain = (self.op.offline == True or
3917
                             self.op.drained == True)
3918
    self.deoffline_or_drain = (self.op.offline == False or
3919
                               self.op.drained == False)
3920
    self.might_demote = (self.op.master_candidate == False or
3921
                         self.offline_or_drain)
3922

    
3923
    self.lock_all = self.op.auto_promote and self.might_demote
3924

    
3925

    
3926
  def ExpandNames(self):
3927
    if self.lock_all:
3928
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3929
    else:
3930
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3931

    
3932
  def BuildHooksEnv(self):
3933
    """Build hooks env.
3934

3935
    This runs on the master node.
3936

3937
    """
3938
    env = {
3939
      "OP_TARGET": self.op.node_name,
3940
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3941
      "OFFLINE": str(self.op.offline),
3942
      "DRAINED": str(self.op.drained),
3943
      }
3944
    nl = [self.cfg.GetMasterNode(),
3945
          self.op.node_name]
3946
    return env, nl, nl
3947

    
3948
  def CheckPrereq(self):
3949
    """Check prerequisites.
3950

3951
    This only checks the instance list against the existing names.
3952

3953
    """
3954
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3955

    
3956
    if (self.op.master_candidate is not None or
3957
        self.op.drained is not None or
3958
        self.op.offline is not None):
3959
      # we can't change the master's node flags
3960
      if self.op.node_name == self.cfg.GetMasterNode():
3961
        raise errors.OpPrereqError("The master role can be changed"
3962
                                   " only via masterfailover",
3963
                                   errors.ECODE_INVAL)
3964

    
3965

    
3966
    if node.master_candidate and self.might_demote and not self.lock_all:
3967
      assert not self.op.auto_promote, "auto-promote set but lock_all not"
3968
      # check if after removing the current node, we're missing master
3969
      # candidates
3970
      (mc_remaining, mc_should, _) = \
3971
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
3972
      if mc_remaining < mc_should:
3973
        raise errors.OpPrereqError("Not enough master candidates, please"
3974
                                   " pass auto_promote to allow promotion",
3975
                                   errors.ECODE_INVAL)
3976

    
3977
    if (self.op.master_candidate == True and
3978
        ((node.offline and not self.op.offline == False) or
3979
         (node.drained and not self.op.drained == False))):
3980
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3981
                                 " to master_candidate" % node.name,
3982
                                 errors.ECODE_INVAL)
3983

    
3984
    # If we're being deofflined/drained, we'll MC ourself if needed
3985
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3986
        self.op.master_candidate == True and not node.master_candidate):
3987
      self.op.master_candidate = _DecideSelfPromotion(self)
3988
      if self.op.master_candidate:
3989
        self.LogInfo("Autopromoting node to master candidate")
3990

    
3991
    return
3992

    
3993
  def Exec(self, feedback_fn):
3994
    """Modifies a node.
3995

3996
    """
3997
    node = self.node
3998

    
3999
    result = []
4000
    changed_mc = False
4001

    
4002
    if self.op.offline is not None:
4003
      node.offline = self.op.offline
4004
      result.append(("offline", str(self.op.offline)))
4005
      if self.op.offline == True:
4006
        if node.master_candidate:
4007
          node.master_candidate = False
4008
          changed_mc = True
4009
          result.append(("master_candidate", "auto-demotion due to offline"))
4010
        if node.drained:
4011
          node.drained = False
4012
          result.append(("drained", "clear drained status due to offline"))
4013

    
4014
    if self.op.master_candidate is not None:
4015
      node.master_candidate = self.op.master_candidate
4016
      changed_mc = True
4017
      result.append(("master_candidate", str(self.op.master_candidate)))
4018
      if self.op.master_candidate == False:
4019
        rrc = self.rpc.call_node_demote_from_mc(node.name)
4020
        msg = rrc.fail_msg
4021
        if msg:
4022
          self.LogWarning("Node failed to demote itself: %s" % msg)
4023

    
4024
    if self.op.drained is not None:
4025
      node.drained = self.op.drained
4026
      result.append(("drained", str(self.op.drained)))
4027
      if self.op.drained == True:
4028
        if node.master_candidate:
4029
          node.master_candidate = False
4030
          changed_mc = True
4031
          result.append(("master_candidate", "auto-demotion due to drain"))
4032
          rrc = self.rpc.call_node_demote_from_mc(node.name)
4033
          msg = rrc.fail_msg
4034
          if msg:
4035
            self.LogWarning("Node failed to demote itself: %s" % msg)
4036
        if node.offline:
4037
          node.offline = False
4038
          result.append(("offline", "clear offline status due to drain"))
4039

    
4040
    # we locked all nodes, we adjust the CP before updating this node
4041
    if self.lock_all:
4042
      _AdjustCandidatePool(self, [node.name])
4043

    
4044
    # this will trigger configuration file update, if needed
4045
    self.cfg.Update(node, feedback_fn)
4046

    
4047
    # this will trigger job queue propagation or cleanup
4048
    if changed_mc: