Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 71ca6309

History | View | Annotate | Download (364.6 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
    return clustername
2587

    
2588

    
2589
class LUSetClusterParams(LogicalUnit):
2590
  """Change the parameters of the cluster.
2591

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

    
2615
  def CheckArguments(self):
2616
    """Check parameters
2617

2618
    """
2619
    if self.op.uid_pool:
2620
      uidpool.CheckUidPool(self.op.uid_pool)
2621

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

    
2625
    if self.op.remove_uids:
2626
      uidpool.CheckUidPool(self.op.remove_uids)
2627

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

    
2636
  def BuildHooksEnv(self):
2637
    """Build hooks env.
2638

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

    
2647
  def CheckPrereq(self):
2648
    """Check prerequisites.
2649

2650
    This checks whether the given params don't conflict and
2651
    if the given volume group is valid.
2652

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

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

    
2665
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2666

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

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

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

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

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

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

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

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

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

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

    
2765
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
2766
                                                  use_none=True)
2767

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

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

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

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

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

    
2826
  def Exec(self, feedback_fn):
2827
    """Change the parameters of the cluster.
2828

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

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

    
2867
    if self.op.maintain_node_health is not None:
2868
      self.cluster.maintain_node_health = self.op.maintain_node_health
2869

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

    
2873
    if self.op.remove_uids is not None:
2874
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
2875

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

    
2879
    if self.op.default_iallocator is not None:
2880
      self.cluster.default_iallocator = self.op.default_iallocator
2881

    
2882
    if self.op.reserved_lvs is not None:
2883
      self.cluster.reserved_lvs = self.op.reserved_lvs
2884

    
2885
    self.cfg.Update(self.cluster, feedback_fn)
2886

    
2887

    
2888
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2889
  """Distribute additional files which are part of the cluster configuration.
2890

2891
  ConfigWriter takes care of distributing the config and ssconf files, but
2892
  there are more files which should be distributed to all nodes. This function
2893
  makes sure those are copied.
2894

2895
  @param lu: calling logical unit
2896
  @param additional_nodes: list of nodes not in the config to distribute to
2897

2898
  """
2899
  # 1. Gather target nodes
2900
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2901
  dist_nodes = lu.cfg.GetOnlineNodeList()
2902
  if additional_nodes is not None:
2903
    dist_nodes.extend(additional_nodes)
2904
  if myself.name in dist_nodes:
2905
    dist_nodes.remove(myself.name)
2906

    
2907
  # 2. Gather files to distribute
2908
  dist_files = set([constants.ETC_HOSTS,
2909
                    constants.SSH_KNOWN_HOSTS_FILE,
2910
                    constants.RAPI_CERT_FILE,
2911
                    constants.RAPI_USERS_FILE,
2912
                    constants.CONFD_HMAC_KEY,
2913
                    constants.CLUSTER_DOMAIN_SECRET_FILE,
2914
                   ])
2915

    
2916
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2917
  for hv_name in enabled_hypervisors:
2918
    hv_class = hypervisor.GetHypervisor(hv_name)
2919
    dist_files.update(hv_class.GetAncillaryFiles())
2920

    
2921
  # 3. Perform the files upload
2922
  for fname in dist_files:
2923
    if os.path.exists(fname):
2924
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2925
      for to_node, to_result in result.items():
2926
        msg = to_result.fail_msg
2927
        if msg:
2928
          msg = ("Copy of file %s to node %s failed: %s" %
2929
                 (fname, to_node, msg))
2930
          lu.proc.LogWarning(msg)
2931

    
2932

    
2933
class LURedistributeConfig(NoHooksLU):
2934
  """Force the redistribution of cluster configuration.
2935

2936
  This is a very simple LU.
2937

2938
  """
2939
  REQ_BGL = False
2940

    
2941
  def ExpandNames(self):
2942
    self.needed_locks = {
2943
      locking.LEVEL_NODE: locking.ALL_SET,
2944
    }
2945
    self.share_locks[locking.LEVEL_NODE] = 1
2946

    
2947
  def Exec(self, feedback_fn):
2948
    """Redistribute the configuration.
2949

2950
    """
2951
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2952
    _RedistributeAncillaryFiles(self)
2953

    
2954

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

2958
  """
2959
  if not instance.disks or disks is not None and not disks:
2960
    return True
2961

    
2962
  disks = _ExpandCheckDisks(instance, disks)
2963

    
2964
  if not oneshot:
2965
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2966

    
2967
  node = instance.primary_node
2968

    
2969
  for dev in disks:
2970
    lu.cfg.SetDiskID(dev, node)
2971

    
2972
  # TODO: Convert to utils.Retry
2973

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

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

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

    
3020
    if done or oneshot:
3021
      break
3022

    
3023
    time.sleep(min(60, max_time))
3024

    
3025
  if done:
3026
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
3027
  return not cumul_degraded
3028

    
3029

    
3030
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
3031
  """Check that mirrors are not degraded.
3032

3033
  The ldisk parameter, if True, will change the test from the
3034
  is_degraded attribute (which represents overall non-ok status for
3035
  the device(s)) to the ldisk (representing the local storage status).
3036

3037
  """
3038
  lu.cfg.SetDiskID(dev, node)
3039

    
3040
  result = True
3041

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

    
3057
  if dev.children:
3058
    for child in dev.children:
3059
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
3060

    
3061
  return result
3062

    
3063

    
3064
class LUDiagnoseOS(NoHooksLU):
3065
  """Logical unit for OS diagnose/query.
3066

3067
  """
3068
  _OP_PARAMS = [
3069
    _POutputFields,
3070
    ("names", _EmptyList, _TListOf(_TNonEmptyString)),
3071
    ]
3072
  REQ_BGL = False
3073
  _FIELDS_STATIC = utils.FieldSet()
3074
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants",
3075
                                   "parameters", "api_versions")
3076

    
3077
  def CheckArguments(self):
3078
    if self.op.names:
3079
      raise errors.OpPrereqError("Selective OS query not supported",
3080
                                 errors.ECODE_INVAL)
3081

    
3082
    _CheckOutputFields(static=self._FIELDS_STATIC,
3083
                       dynamic=self._FIELDS_DYNAMIC,
3084
                       selected=self.op.output_fields)
3085

    
3086
  def ExpandNames(self):
3087
    # Lock all nodes, in shared mode
3088
    # Temporary removal of locks, should be reverted later
3089
    # TODO: reintroduce locks when they are lighter-weight
3090
    self.needed_locks = {}
3091
    #self.share_locks[locking.LEVEL_NODE] = 1
3092
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3093

    
3094
  @staticmethod
3095
  def _DiagnoseByOS(rlist):
3096
    """Remaps a per-node return list into an a per-os per-node dictionary
3097

3098
    @param rlist: a map with node names as keys and OS objects as values
3099

3100
    @rtype: dict
3101
    @return: a dictionary with osnames as keys and as value another
3102
        map, with nodes as keys and tuples of (path, status, diagnose,
3103
        variants, parameters, api_versions) as values, eg::
3104

3105
          {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
3106
                                     (/srv/..., False, "invalid api")],
3107
                           "node2": [(/srv/..., True, "", [], [])]}
3108
          }
3109

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

    
3134
  def Exec(self, feedback_fn):
3135
    """Compute the list of OSes.
3136

3137
    """
3138
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
3139
    node_data = self.rpc.call_os_diagnose(valid_nodes)
3140
    pol = self._DiagnoseByOS(node_data)
3141
    output = []
3142

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

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

    
3183
    return output
3184

    
3185

    
3186
class LURemoveNode(LogicalUnit):
3187
  """Logical unit for removing a node.
3188

3189
  """
3190
  HPATH = "node-remove"
3191
  HTYPE = constants.HTYPE_NODE
3192
  _OP_PARAMS = [
3193
    _PNodeName,
3194
    ]
3195

    
3196
  def BuildHooksEnv(self):
3197
    """Build hooks env.
3198

3199
    This doesn't run on the target node in the pre phase as a failed
3200
    node would then be impossible to remove.
3201

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

    
3215
  def CheckPrereq(self):
3216
    """Check prerequisites.
3217

3218
    This checks:
3219
     - the node exists in the configuration
3220
     - it does not have primary or secondary instances
3221
     - it's not the master
3222

3223
    Any errors are signaled by raising errors.OpPrereqError.
3224

3225
    """
3226
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3227
    node = self.cfg.GetNodeInfo(self.op.node_name)
3228
    assert node is not None
3229

    
3230
    instance_list = self.cfg.GetInstanceList()
3231

    
3232
    masternode = self.cfg.GetMasterNode()
3233
    if node.name == masternode:
3234
      raise errors.OpPrereqError("Node is the master node,"
3235
                                 " you need to failover first.",
3236
                                 errors.ECODE_INVAL)
3237

    
3238
    for instance_name in instance_list:
3239
      instance = self.cfg.GetInstanceInfo(instance_name)
3240
      if node.name in instance.all_nodes:
3241
        raise errors.OpPrereqError("Instance %s is still running on the node,"
3242
                                   " please remove first." % instance_name,
3243
                                   errors.ECODE_INVAL)
3244
    self.op.node_name = node.name
3245
    self.node = node
3246

    
3247
  def Exec(self, feedback_fn):
3248
    """Removes the node from the cluster.
3249

3250
    """
3251
    node = self.node
3252
    logging.info("Stopping the node daemon and removing configs from node %s",
3253
                 node.name)
3254

    
3255
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3256

    
3257
    # Promote nodes to master candidate as needed
3258
    _AdjustCandidatePool(self, exceptions=[node.name])
3259
    self.context.RemoveNode(node.name)
3260

    
3261
    # Run post hooks on the node before it's removed
3262
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
3263
    try:
3264
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
3265
    except:
3266
      # pylint: disable-msg=W0702
3267
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
3268

    
3269
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
3270
    msg = result.fail_msg
3271
    if msg:
3272
      self.LogWarning("Errors encountered on the remote node while leaving"
3273
                      " the cluster: %s", msg)
3274

    
3275
    # Remove node from our /etc/hosts
3276
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3277
      # FIXME: this should be done via an rpc call to node daemon
3278
      utils.RemoveHostFromEtcHosts(node.name)
3279
      _RedistributeAncillaryFiles(self)
3280

    
3281

    
3282
class LUQueryNodes(NoHooksLU):
3283
  """Logical unit for querying nodes.
3284

3285
  """
3286
  # pylint: disable-msg=W0142
3287
  _OP_PARAMS = [
3288
    _POutputFields,
3289
    ("names", _EmptyList, _TListOf(_TNonEmptyString)),
3290
    ("use_locking", False, _TBool),
3291
    ]
3292
  REQ_BGL = False
3293

    
3294
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
3295
                    "master_candidate", "offline", "drained"]
3296

    
3297
  _FIELDS_DYNAMIC = utils.FieldSet(
3298
    "dtotal", "dfree",
3299
    "mtotal", "mnode", "mfree",
3300
    "bootid",
3301
    "ctotal", "cnodes", "csockets",
3302
    )
3303

    
3304
  _FIELDS_STATIC = utils.FieldSet(*[
3305
    "pinst_cnt", "sinst_cnt",
3306
    "pinst_list", "sinst_list",
3307
    "pip", "sip", "tags",
3308
    "master",
3309
    "role"] + _SIMPLE_FIELDS
3310
    )
3311

    
3312
  def CheckArguments(self):
3313
    _CheckOutputFields(static=self._FIELDS_STATIC,
3314
                       dynamic=self._FIELDS_DYNAMIC,
3315
                       selected=self.op.output_fields)
3316

    
3317
  def ExpandNames(self):
3318
    self.needed_locks = {}
3319
    self.share_locks[locking.LEVEL_NODE] = 1
3320

    
3321
    if self.op.names:
3322
      self.wanted = _GetWantedNodes(self, self.op.names)
3323
    else:
3324
      self.wanted = locking.ALL_SET
3325

    
3326
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3327
    self.do_locking = self.do_node_query and self.op.use_locking
3328
    if self.do_locking:
3329
      # if we don't request only static fields, we need to lock the nodes
3330
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
3331

    
3332
  def Exec(self, feedback_fn):
3333
    """Computes the list of nodes and their attributes.
3334

3335
    """
3336
    all_info = self.cfg.GetAllNodesInfo()
3337
    if self.do_locking:
3338
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
3339
    elif self.wanted != locking.ALL_SET:
3340
      nodenames = self.wanted
3341
      missing = set(nodenames).difference(all_info.keys())
3342
      if missing:
3343
        raise errors.OpExecError(
3344
          "Some nodes were removed before retrieving their data: %s" % missing)
3345
    else:
3346
      nodenames = all_info.keys()
3347

    
3348
    nodenames = utils.NiceSort(nodenames)
3349
    nodelist = [all_info[name] for name in nodenames]
3350

    
3351
    # begin data gathering
3352

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

    
3378
    node_to_primary = dict([(name, set()) for name in nodenames])
3379
    node_to_secondary = dict([(name, set()) for name in nodenames])
3380

    
3381
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
3382
                             "sinst_cnt", "sinst_list"))
3383
    if inst_fields & frozenset(self.op.output_fields):
3384
      inst_data = self.cfg.GetAllInstancesInfo()
3385

    
3386
      for inst in inst_data.values():
3387
        if inst.primary_node in node_to_primary:
3388
          node_to_primary[inst.primary_node].add(inst.name)
3389
        for secnode in inst.secondary_nodes:
3390
          if secnode in node_to_secondary:
3391
            node_to_secondary[secnode].add(inst.name)
3392

    
3393
    master_node = self.cfg.GetMasterNode()
3394

    
3395
    # end data gathering
3396

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

    
3437
    return output
3438

    
3439

    
3440
class LUQueryNodeVolumes(NoHooksLU):
3441
  """Logical unit for getting volumes on node(s).
3442

3443
  """
3444
  _OP_PARAMS = [
3445
    ("nodes", _EmptyList, _TListOf(_TNonEmptyString)),
3446
    ("output_fields", _NoDefault, _TListOf(_TNonEmptyString)),
3447
    ]
3448
  REQ_BGL = False
3449
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3450
  _FIELDS_STATIC = utils.FieldSet("node")
3451

    
3452
  def CheckArguments(self):
3453
    _CheckOutputFields(static=self._FIELDS_STATIC,
3454
                       dynamic=self._FIELDS_DYNAMIC,
3455
                       selected=self.op.output_fields)
3456

    
3457
  def ExpandNames(self):
3458
    self.needed_locks = {}
3459
    self.share_locks[locking.LEVEL_NODE] = 1
3460
    if not self.op.nodes:
3461
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3462
    else:
3463
      self.needed_locks[locking.LEVEL_NODE] = \
3464
        _GetWantedNodes(self, self.op.nodes)
3465

    
3466
  def Exec(self, feedback_fn):
3467
    """Computes the list of nodes and their attributes.
3468

3469
    """
3470
    nodenames = self.acquired_locks[locking.LEVEL_NODE]
3471
    volumes = self.rpc.call_node_volumes(nodenames)
3472

    
3473
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3474
             in self.cfg.GetInstanceList()]
3475

    
3476
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3477

    
3478
    output = []
3479
    for node in nodenames:
3480
      nresult = volumes[node]
3481
      if nresult.offline:
3482
        continue
3483
      msg = nresult.fail_msg
3484
      if msg:
3485
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3486
        continue
3487

    
3488
      node_vols = nresult.payload[:]
3489
      node_vols.sort(key=lambda vol: vol['dev'])
3490

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

    
3517
        output.append(node_output)
3518

    
3519
    return output
3520

    
3521

    
3522
class LUQueryNodeStorage(NoHooksLU):
3523
  """Logical unit for getting information on storage units on node(s).
3524

3525
  """
3526
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3527
  _OP_PARAMS = [
3528
    ("nodes", _EmptyList, _TListOf(_TNonEmptyString)),
3529
    ("storage_type", _NoDefault, _CheckStorageType),
3530
    ("output_fields", _NoDefault, _TListOf(_TNonEmptyString)),
3531
    ("name", None, _TMaybeString),
3532
    ]
3533
  REQ_BGL = False
3534

    
3535
  def CheckArguments(self):
3536
    _CheckOutputFields(static=self._FIELDS_STATIC,
3537
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3538
                       selected=self.op.output_fields)
3539

    
3540
  def ExpandNames(self):
3541
    self.needed_locks = {}
3542
    self.share_locks[locking.LEVEL_NODE] = 1
3543

    
3544
    if self.op.nodes:
3545
      self.needed_locks[locking.LEVEL_NODE] = \
3546
        _GetWantedNodes(self, self.op.nodes)
3547
    else:
3548
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3549

    
3550
  def Exec(self, feedback_fn):
3551
    """Computes the list of nodes and their attributes.
3552

3553
    """
3554
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3555

    
3556
    # Always get name to sort by
3557
    if constants.SF_NAME in self.op.output_fields:
3558
      fields = self.op.output_fields[:]
3559
    else:
3560
      fields = [constants.SF_NAME] + self.op.output_fields
3561

    
3562
    # Never ask for node or type as it's only known to the LU
3563
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3564
      while extra in fields:
3565
        fields.remove(extra)
3566

    
3567
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3568
    name_idx = field_idx[constants.SF_NAME]
3569

    
3570
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3571
    data = self.rpc.call_storage_list(self.nodes,
3572
                                      self.op.storage_type, st_args,
3573
                                      self.op.name, fields)
3574

    
3575
    result = []
3576

    
3577
    for node in utils.NiceSort(self.nodes):
3578
      nresult = data[node]
3579
      if nresult.offline:
3580
        continue
3581

    
3582
      msg = nresult.fail_msg
3583
      if msg:
3584
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3585
        continue
3586

    
3587
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3588

    
3589
      for name in utils.NiceSort(rows.keys()):
3590
        row = rows[name]
3591

    
3592
        out = []
3593

    
3594
        for field in self.op.output_fields:
3595
          if field == constants.SF_NODE:
3596
            val = node
3597
          elif field == constants.SF_TYPE:
3598
            val = self.op.storage_type
3599
          elif field in field_idx:
3600
            val = row[field_idx[field]]
3601
          else:
3602
            raise errors.ParameterError(field)
3603

    
3604
          out.append(val)
3605

    
3606
        result.append(out)
3607

    
3608
    return result
3609

    
3610

    
3611
class LUModifyNodeStorage(NoHooksLU):
3612
  """Logical unit for modifying a storage volume on a node.
3613

3614
  """
3615
  _OP_PARAMS = [
3616
    _PNodeName,
3617
    ("storage_type", _NoDefault, _CheckStorageType),
3618
    ("name", _NoDefault, _TNonEmptyString),
3619
    ("changes", _NoDefault, _TDict),
3620
    ]
3621
  REQ_BGL = False
3622

    
3623
  def CheckArguments(self):
3624
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3625

    
3626
    storage_type = self.op.storage_type
3627

    
3628
    try:
3629
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3630
    except KeyError:
3631
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3632
                                 " modified" % storage_type,
3633
                                 errors.ECODE_INVAL)
3634

    
3635
    diff = set(self.op.changes.keys()) - modifiable
3636
    if diff:
3637
      raise errors.OpPrereqError("The following fields can not be modified for"
3638
                                 " storage units of type '%s': %r" %
3639
                                 (storage_type, list(diff)),
3640
                                 errors.ECODE_INVAL)
3641

    
3642
  def ExpandNames(self):
3643
    self.needed_locks = {
3644
      locking.LEVEL_NODE: self.op.node_name,
3645
      }
3646

    
3647
  def Exec(self, feedback_fn):
3648
    """Computes the list of nodes and their attributes.
3649

3650
    """
3651
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3652
    result = self.rpc.call_storage_modify(self.op.node_name,
3653
                                          self.op.storage_type, st_args,
3654
                                          self.op.name, self.op.changes)
3655
    result.Raise("Failed to modify storage unit '%s' on %s" %
3656
                 (self.op.name, self.op.node_name))
3657

    
3658

    
3659
class LUAddNode(LogicalUnit):
3660
  """Logical unit for adding node to the cluster.
3661

3662
  """
3663
  HPATH = "node-add"
3664
  HTYPE = constants.HTYPE_NODE
3665
  _OP_PARAMS = [
3666
    _PNodeName,
3667
    ("primary_ip", None, _NoType),
3668
    ("secondary_ip", None, _TMaybeString),
3669
    ("readd", False, _TBool),
3670
    ]
3671

    
3672
  def CheckArguments(self):
3673
    # validate/normalize the node name
3674
    self.op.node_name = netutils.HostInfo.NormalizeName(self.op.node_name)
3675

    
3676
  def BuildHooksEnv(self):
3677
    """Build hooks env.
3678

3679
    This will run on all nodes before, and on all nodes + the new node after.
3680

3681
    """
3682
    env = {
3683
      "OP_TARGET": self.op.node_name,
3684
      "NODE_NAME": self.op.node_name,
3685
      "NODE_PIP": self.op.primary_ip,
3686
      "NODE_SIP": self.op.secondary_ip,
3687
      }
3688
    nodes_0 = self.cfg.GetNodeList()
3689
    nodes_1 = nodes_0 + [self.op.node_name, ]
3690
    return env, nodes_0, nodes_1
3691

    
3692
  def CheckPrereq(self):
3693
    """Check prerequisites.
3694

3695
    This checks:
3696
     - the new node is not already in the config
3697
     - it is resolvable
3698
     - its parameters (single/dual homed) matches the cluster
3699

3700
    Any errors are signaled by raising errors.OpPrereqError.
3701

3702
    """
3703
    node_name = self.op.node_name
3704
    cfg = self.cfg
3705

    
3706
    dns_data = netutils.GetHostInfo(node_name)
3707

    
3708
    node = dns_data.name
3709
    primary_ip = self.op.primary_ip = dns_data.ip
3710
    if self.op.secondary_ip is None:
3711
      self.op.secondary_ip = primary_ip
3712
    if not netutils.IsValidIP4(self.op.secondary_ip):
3713
      raise errors.OpPrereqError("Invalid secondary IP given",
3714
                                 errors.ECODE_INVAL)
3715
    secondary_ip = self.op.secondary_ip
3716

    
3717
    node_list = cfg.GetNodeList()
3718
    if not self.op.readd and node in node_list:
3719
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3720
                                 node, errors.ECODE_EXISTS)
3721
    elif self.op.readd and node not in node_list:
3722
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3723
                                 errors.ECODE_NOENT)
3724

    
3725
    self.changed_primary_ip = False
3726

    
3727
    for existing_node_name in node_list:
3728
      existing_node = cfg.GetNodeInfo(existing_node_name)
3729

    
3730
      if self.op.readd and node == existing_node_name:
3731
        if existing_node.secondary_ip != secondary_ip:
3732
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3733
                                     " address configuration as before",
3734
                                     errors.ECODE_INVAL)
3735
        if existing_node.primary_ip != primary_ip:
3736
          self.changed_primary_ip = True
3737

    
3738
        continue
3739

    
3740
      if (existing_node.primary_ip == primary_ip or
3741
          existing_node.secondary_ip == primary_ip or
3742
          existing_node.primary_ip == secondary_ip or
3743
          existing_node.secondary_ip == secondary_ip):
3744
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3745
                                   " existing node %s" % existing_node.name,
3746
                                   errors.ECODE_NOTUNIQUE)
3747

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

    
3763
    # checks reachability
3764
    if not netutils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3765
      raise errors.OpPrereqError("Node not reachable by ping",
3766
                                 errors.ECODE_ENVIRON)
3767

    
3768
    if not newbie_singlehomed:
3769
      # check reachability from my secondary ip to newbie's secondary ip
3770
      if not netutils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3771
                           source=myself.secondary_ip):
3772
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3773
                                   " based ping to noded port",
3774
                                   errors.ECODE_ENVIRON)
3775

    
3776
    if self.op.readd:
3777
      exceptions = [node]
3778
    else:
3779
      exceptions = []
3780

    
3781
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3782

    
3783
    if self.op.readd:
3784
      self.new_node = self.cfg.GetNodeInfo(node)
3785
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3786
    else:
3787
      self.new_node = objects.Node(name=node,
3788
                                   primary_ip=primary_ip,
3789
                                   secondary_ip=secondary_ip,
3790
                                   master_candidate=self.master_candidate,
3791
                                   offline=False, drained=False)
3792

    
3793
  def Exec(self, feedback_fn):
3794
    """Adds the new node to the cluster.
3795

3796
    """
3797
    new_node = self.new_node
3798
    node = new_node.name
3799

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

    
3812
    # notify the user about any possible mc promotion
3813
    if new_node.master_candidate:
3814
      self.LogInfo("Node will be a master candidate")
3815

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

    
3827
    # setup ssh on node
3828
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3829
      logging.info("Copy ssh key to node %s", node)
3830
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3831
      keyarray = []
3832
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3833
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3834
                  priv_key, pub_key]
3835

    
3836
      for i in keyfiles:
3837
        keyarray.append(utils.ReadFile(i))
3838

    
3839
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3840
                                      keyarray[2], keyarray[3], keyarray[4],
3841
                                      keyarray[5])
3842
      result.Raise("Cannot transfer ssh keys to the new node")
3843

    
3844
    # Add node to our /etc/hosts, and add key to known_hosts
3845
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3846
      # FIXME: this should be done via an rpc call to node daemon
3847
      utils.AddHostToEtcHosts(new_node.name)
3848

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

    
3859
    node_verify_list = [self.cfg.GetMasterNode()]
3860
    node_verify_param = {
3861
      constants.NV_NODELIST: [node],
3862
      # TODO: do a node-net-test as well?
3863
    }
3864

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

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

    
3893

    
3894
class LUSetNodeParams(LogicalUnit):
3895
  """Modifies the parameters of a node.
3896

3897
  """
3898
  HPATH = "node-modify"
3899
  HTYPE = constants.HTYPE_NODE
3900
  _OP_PARAMS = [
3901
    _PNodeName,
3902
    ("master_candidate", None, _TMaybeBool),
3903
    ("offline", None, _TMaybeBool),
3904
    ("drained", None, _TMaybeBool),
3905
    ("auto_promote", False, _TBool),
3906
    _PForce,
3907
    ]
3908
  REQ_BGL = False
3909

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

    
3921
    # Boolean value that tells us whether we're offlining or draining the node
3922
    self.offline_or_drain = (self.op.offline == True or
3923
                             self.op.drained == True)
3924
    self.deoffline_or_drain = (self.op.offline == False or
3925
                               self.op.drained == False)
3926
    self.might_demote = (self.op.master_candidate == False or
3927
                         self.offline_or_drain)
3928

    
3929
    self.lock_all = self.op.auto_promote and self.might_demote
3930

    
3931

    
3932
  def ExpandNames(self):
3933
    if self.lock_all:
3934
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3935
    else:
3936
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3937

    
3938
  def BuildHooksEnv(self):
3939
    """Build hooks env.
3940

3941
    This runs on the master node.
3942

3943
    """
3944
    env = {
3945
      "OP_TARGET": self.op.node_name,
3946
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3947
      "OFFLINE": str(self.op.offline),
3948
      "DRAINED": str(self.op.drained),
3949
      }
3950
    nl = [self.cfg.GetMasterNode(),
3951
          self.op.node_name]
3952
    return env, nl, nl
3953

    
3954
  def CheckPrereq(self):
3955
    """Check prerequisites.
3956

3957
    This only checks the instance list against the existing names.
3958

3959
    """
3960
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3961

    
3962
    if (self.op.master_candidate is not None or
3963
        self.op.drained is not None or
3964
        self.op.offline is not None):
3965
      # we can't change the master's node flags
3966
      if self.op.node_name == self.cfg.GetMasterNode():
3967
        raise errors.OpPrereqError("The master role can be changed"
3968
                                   " only via master-failover",
3969
                                   errors.ECODE_INVAL)
3970

    
3971

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

    
3983
    if (self.op.master_candidate == True and
3984
        ((node.offline and not self.op.offline == False) or
3985
         (node.drained and not self.op.drained == False))):
3986
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3987
                                 " to master_candidate" % node.name,
3988
                                 errors.ECODE_INVAL)
3989

    
3990
    # If we're being deofflined/drained, we'll MC ourself if needed
3991
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3992
        self.op.master_candidate == True and not node.master_candidate):
3993
      self.op.master_candidate = _DecideSelfPromotion(self)
3994
      if self.op.master_candidate:
3995
        self.LogInfo("Autopromoting node to master candidate")
3996

    
3997
    return
3998

    
3999
  def Exec(self, feedback_fn):
4000
    """Modifies a node.
4001

4002
    """
4003
    node = self.node
4004

    
4005
    result = []
4006
    changed_mc = False
4007

    
4008
    if self.op.offline is not None:
4009
      node.offline = self.op.offline
4010
      result.append(("offline", str(self.op.offline)))
4011
      if self.op.offline == True:
4012
        if node.master_candidate:
4013
          node.master_candidate = False
4014
          changed_mc = True
4015
          result.append(("master_candidate", "auto-demotion due to offline"))
4016
        if node.drained:
4017
          node.drained = False
4018
          result.append(("drained", "clear drained status due to offline"))
4019

    
4020
    if self.op.master_candidate is not None:
4021
      node.master_candidate = self.op.master_candidate
4022
      changed_mc = True
4023
      result.append(("master_candidate", str(self.op.master_candidate)))
4024
      if self.op.master_candidate == False:
4025
        rrc = self.rpc.call_node_demote_from_mc(node.name)
4026
        msg = rrc.fail_msg
4027
        if msg:
4028
          self.LogWarning("Node failed to demote itself: %s" % msg)
4029

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

    
4046
    # we locked all nodes, we adjust the CP before updating this node
4047
    <