Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 783a6c0b

History | View | Annotate | Download (363.7 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
_PMigrationLive = ("live", 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):
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
    """
1664
    for node, n_img in node_image.items():
1665
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1666
        # skip non-healthy nodes
1667
        continue
1668
      for volume in n_img.volumes:
1669
        test = (node not in node_vol_should or
1670
                volume not in node_vol_should[node])
1671
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1672
                      "volume %s is unknown", volume)
1673

    
1674
  def _VerifyOrphanInstances(self, instancelist, node_image):
1675
    """Verify the list of running instances.
1676

1677
    This checks what instances are running but unknown to the cluster.
1678

1679
    """
1680
    for node, n_img in node_image.items():
1681
      for o_inst in n_img.instances:
1682
        test = o_inst not in instancelist
1683
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1684
                      "instance %s on node %s should not exist", o_inst, node)
1685

    
1686
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1687
    """Verify N+1 Memory Resilience.
1688

1689
    Check that if one single node dies we can still start all the
1690
    instances it was primary for.
1691

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

    
1713
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1714
                       master_files):
1715
    """Verifies and computes the node required file checksums.
1716

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

1724
    """
1725
    node = ninfo.name
1726
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1727

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

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

    
1757
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_helper,
1758
                      drbd_map):
1759
    """Verifies and the node DRBD status.
1760

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

1769
    """
1770
    node = ninfo.name
1771
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1772

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

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

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

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

    
1820
  def _UpdateNodeOS(self, ninfo, nresult, nimg):
1821
    """Builds the node OS structures.
1822

1823
    @type ninfo: L{objects.Node}
1824
    @param ninfo: the node to check
1825
    @param nresult: the remote results for the node
1826
    @param nimg: the node image object
1827

1828
    """
1829
    node = ninfo.name
1830
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1831

    
1832
    remote_os = nresult.get(constants.NV_OSLIST, None)
1833
    test = (not isinstance(remote_os, list) or
1834
            not compat.all(isinstance(v, list) and len(v) == 7
1835
                           for v in remote_os))
1836

    
1837
    _ErrorIf(test, self.ENODEOS, node,
1838
             "node hasn't returned valid OS data")
1839

    
1840
    nimg.os_fail = test
1841

    
1842
    if test:
1843
      return
1844

    
1845
    os_dict = {}
1846

    
1847
    for (name, os_path, status, diagnose,
1848
         variants, parameters, api_ver) in nresult[constants.NV_OSLIST]:
1849

    
1850
      if name not in os_dict:
1851
        os_dict[name] = []
1852

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

    
1859
    nimg.oslist = os_dict
1860

    
1861
  def _VerifyNodeOS(self, ninfo, nimg, base):
1862
    """Verifies the node OS list.
1863

1864
    @type ninfo: L{objects.Node}
1865
    @param ninfo: the node to check
1866
    @param nimg: the node image object
1867
    @param base: the 'template' node we match against (e.g. from the master)
1868

1869
    """
1870
    node = ninfo.name
1871
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1872

    
1873
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1874

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2003
  def BuildHooksEnv(self):
2004
    """Build hooks env.
2005

2006
    Cluster-Verify hooks just ran in the post phase and their failure makes
2007
    the output be logged in the verify output and the verification to fail.
2008

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

    
2017
    return env, [], all_nodes
2018

    
2019
  def Exec(self, feedback_fn):
2020
    """Verify integrity of cluster, performing various test on nodes.
2021

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

    
2031
    # Check the cluster certificates
2032
    for cert_filename in constants.ALL_CERT_FILES:
2033
      (errcode, msg) = _VerifyCertificate(cert_filename)
2034
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
2035

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

    
2051
    # FIXME: verify OS list
2052
    # do local checksums
2053
    master_files = [constants.CLUSTER_CONF_FILE]
2054
    master_node = self.master_node = self.cfg.GetMasterNode()
2055
    master_ip = self.cfg.GetMasterIP()
2056

    
2057
    file_names = ssconf.SimpleStore().GetFileList()
2058
    file_names.extend(constants.ALL_CERT_FILES)
2059
    file_names.extend(master_files)
2060
    if cluster.modify_etc_hosts:
2061
      file_names.append(constants.ETC_HOSTS)
2062

    
2063
    local_checksums = utils.FingerprintFiles(file_names)
2064

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

    
2083
    if vg_name is not None:
2084
      node_verify_param[constants.NV_VGLIST] = None
2085
      node_verify_param[constants.NV_LVLIST] = vg_name
2086
      node_verify_param[constants.NV_PVLIST] = [vg_name]
2087
      node_verify_param[constants.NV_DRBDLIST] = None
2088

    
2089
    if drbd_helper:
2090
      node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
2091

    
2092
    # Build our expected cluster state
2093
    node_image = dict((node.name, self.NodeImage(offline=node.offline,
2094
                                                 name=node.name))
2095
                      for node in nodeinfo)
2096

    
2097
    for instance in instancelist:
2098
      inst_config = instanceinfo[instance]
2099

    
2100
      for nname in inst_config.all_nodes:
2101
        if nname not in node_image:
2102
          # ghost node
2103
          gnode = self.NodeImage(name=nname)
2104
          gnode.ghost = True
2105
          node_image[nname] = gnode
2106

    
2107
      inst_config.MapLVsByNode(node_vol_should)
2108

    
2109
      pnode = inst_config.primary_node
2110
      node_image[pnode].pinst.append(instance)
2111

    
2112
      for snode in inst_config.secondary_nodes:
2113
        nimg = node_image[snode]
2114
        nimg.sinst.append(instance)
2115
        if pnode not in nimg.sbp:
2116
          nimg.sbp[pnode] = []
2117
        nimg.sbp[pnode].append(instance)
2118

    
2119
    # At this point, we have the in-memory data structures complete,
2120
    # except for the runtime information, which we'll gather next
2121

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

    
2131
    all_drbd_map = self.cfg.ComputeDRBDMap()
2132

    
2133
    feedback_fn("* Verifying node status")
2134

    
2135
    refos_img = None
2136

    
2137
    for node_i in nodeinfo:
2138
      node = node_i.name
2139
      nimg = node_image[node]
2140

    
2141
      if node_i.offline:
2142
        if verbose:
2143
          feedback_fn("* Skipping offline node %s" % (node,))
2144
        n_offline += 1
2145
        continue
2146

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

    
2159
      msg = all_nvinfo[node].fail_msg
2160
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
2161
      if msg:
2162
        nimg.rpc_fail = True
2163
        continue
2164

    
2165
      nresult = all_nvinfo[node].payload
2166

    
2167
      nimg.call_ok = self._VerifyNode(node_i, nresult)
2168
      self._VerifyNodeNetwork(node_i, nresult)
2169
      self._VerifyNodeLVM(node_i, nresult, vg_name)
2170
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
2171
                            master_files)
2172
      self._VerifyNodeDrbd(node_i, nresult, instanceinfo, drbd_helper,
2173
                           all_drbd_map)
2174
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
2175

    
2176
      self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
2177
      self._UpdateNodeInstances(node_i, nresult, nimg)
2178
      self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
2179
      self._UpdateNodeOS(node_i, nresult, nimg)
2180
      if not nimg.os_fail:
2181
        if refos_img is None:
2182
          refos_img = nimg
2183
        self._VerifyNodeOS(node_i, nimg, refos_img)
2184

    
2185
    feedback_fn("* Verifying instance status")
2186
    for instance in instancelist:
2187
      if verbose:
2188
        feedback_fn("* Verifying instance %s" % instance)
2189
      inst_config = instanceinfo[instance]
2190
      self._VerifyInstance(instance, inst_config, node_image)
2191
      inst_nodes_offline = []
2192

    
2193
      pnode = inst_config.primary_node
2194
      pnode_img = node_image[pnode]
2195
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
2196
               self.ENODERPC, pnode, "instance %s, connection to"
2197
               " primary node failed", instance)
2198

    
2199
      if pnode_img.offline:
2200
        inst_nodes_offline.append(pnode)
2201

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

    
2214
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
2215
        i_non_a_balanced.append(instance)
2216

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

    
2222
        if s_img.offline:
2223
          inst_nodes_offline.append(snode)
2224

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

    
2234
    feedback_fn("* Verifying orphan volumes")
2235
    self._VerifyOrphanVolumes(node_vol_should, node_image)
2236

    
2237
    feedback_fn("* Verifying orphan instances")
2238
    self._VerifyOrphanInstances(instancelist, node_image)
2239

    
2240
    if constants.VERIFY_NPLUSONE_MEM not in self.op.skip_checks:
2241
      feedback_fn("* Verifying N+1 Memory redundancy")
2242
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
2243

    
2244
    feedback_fn("* Other Notes")
2245
    if i_non_redundant:
2246
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
2247
                  % len(i_non_redundant))
2248

    
2249
    if i_non_a_balanced:
2250
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
2251
                  % len(i_non_a_balanced))
2252

    
2253
    if n_offline:
2254
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
2255

    
2256
    if n_drained:
2257
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
2258

    
2259
    return not self.bad
2260

    
2261
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
2262
    """Analyze the post-hooks' result
2263

2264
    This method analyses the hook result, handles it, and sends some
2265
    nicely-formatted feedback back to the user.
2266

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

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

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

    
2305
      return lu_result
2306

    
2307

    
2308
class LUVerifyDisks(NoHooksLU):
2309
  """Verifies the cluster disks status.
2310

2311
  """
2312
  REQ_BGL = False
2313

    
2314
  def ExpandNames(self):
2315
    self.needed_locks = {
2316
      locking.LEVEL_NODE: locking.ALL_SET,
2317
      locking.LEVEL_INSTANCE: locking.ALL_SET,
2318
    }
2319
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
2320

    
2321
  def Exec(self, feedback_fn):
2322
    """Verify integrity of cluster disks.
2323

2324
    @rtype: tuple of three items
2325
    @return: a tuple of (dict of node-to-node_error, list of instances
2326
        which need activate-disks, dict of instance: (node, volume) for
2327
        missing volumes
2328

2329
    """
2330
    result = res_nodes, res_instances, res_missing = {}, [], {}
2331

    
2332
    vg_name = self.cfg.GetVGName()
2333
    nodes = utils.NiceSort(self.cfg.GetNodeList())
2334
    instances = [self.cfg.GetInstanceInfo(name)
2335
                 for name in self.cfg.GetInstanceList()]
2336

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

    
2349
    if not nv_dict:
2350
      return result
2351

    
2352
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
2353

    
2354
    for node in nodes:
2355
      # node_volume
2356
      node_res = node_lvs[node]
2357
      if node_res.offline:
2358
        continue
2359
      msg = node_res.fail_msg
2360
      if msg:
2361
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
2362
        res_nodes[node] = msg
2363
        continue
2364

    
2365
      lvs = node_res.payload
2366
      for lv_name, (_, _, lv_online) in lvs.items():
2367
        inst = nv_dict.pop((node, lv_name), None)
2368
        if (not lv_online and inst is not None
2369
            and inst.name not in res_instances):
2370
          res_instances.append(inst.name)
2371

    
2372
    # any leftover items in nv_dict are missing LVs, let's arrange the
2373
    # data better
2374
    for key, inst in nv_dict.iteritems():
2375
      if inst.name not in res_missing:
2376
        res_missing[inst.name] = []
2377
      res_missing[inst.name].append(key)
2378

    
2379
    return result
2380

    
2381

    
2382
class LURepairDiskSizes(NoHooksLU):
2383
  """Verifies the cluster disks sizes.
2384

2385
  """
2386
  _OP_PARAMS = [("instances", _EmptyList, _TListOf(_TNonEmptyString))]
2387
  REQ_BGL = False
2388

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

    
2408
  def DeclareLocks(self, level):
2409
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2410
      self._LockInstancesNodes(primary_only=True)
2411

    
2412
  def CheckPrereq(self):
2413
    """Check prerequisites.
2414

2415
    This only checks the optional instance list against the existing names.
2416

2417
    """
2418
    if self.wanted_names is None:
2419
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2420

    
2421
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2422
                             in self.wanted_names]
2423

    
2424
  def _EnsureChildSizes(self, disk):
2425
    """Ensure children of the disk have the needed disk size.
2426

2427
    This is valid mainly for DRBD8 and fixes an issue where the
2428
    children have smaller disk size.
2429

2430
    @param disk: an L{ganeti.objects.Disk} object
2431

2432
    """
2433
    if disk.dev_type == constants.LD_DRBD8:
2434
      assert disk.children, "Empty children for DRBD8?"
2435
      fchild = disk.children[0]
2436
      mismatch = fchild.size < disk.size
2437
      if mismatch:
2438
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2439
                     fchild.size, disk.size)
2440
        fchild.size = disk.size
2441

    
2442
      # and we recurse on this child only, not on the metadev
2443
      return self._EnsureChildSizes(fchild) or mismatch
2444
    else:
2445
      return False
2446

    
2447
  def Exec(self, feedback_fn):
2448
    """Verify the size of cluster disks.
2449

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

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

    
2497

    
2498
class LURenameCluster(LogicalUnit):
2499
  """Rename the cluster.
2500

2501
  """
2502
  HPATH = "cluster-rename"
2503
  HTYPE = constants.HTYPE_CLUSTER
2504
  _OP_PARAMS = [("name", _NoDefault, _TNonEmptyString)]
2505

    
2506
  def BuildHooksEnv(self):
2507
    """Build hooks env.
2508

2509
    """
2510
    env = {
2511
      "OP_TARGET": self.cfg.GetClusterName(),
2512
      "NEW_NAME": self.op.name,
2513
      }
2514
    mn = self.cfg.GetMasterNode()
2515
    all_nodes = self.cfg.GetNodeList()
2516
    return env, [mn], all_nodes
2517

    
2518
  def CheckPrereq(self):
2519
    """Verify that the passed name is a valid one.
2520

2521
    """
2522
    hostname = netutils.GetHostInfo(self.op.name)
2523

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

    
2538
    self.op.name = new_name
2539

    
2540
  def Exec(self, feedback_fn):
2541
    """Rename the cluster.
2542

2543
    """
2544
    clustername = self.op.name
2545
    ip = self.ip
2546

    
2547
    # shutdown the master IP
2548
    master = self.cfg.GetMasterNode()
2549
    result = self.rpc.call_node_stop_master(master, False)
2550
    result.Raise("Could not disable the master role")
2551

    
2552
    try:
2553
      cluster = self.cfg.GetClusterInfo()
2554
      cluster.cluster_name = clustername
2555
      cluster.master_ip = ip
2556
      self.cfg.Update(cluster, feedback_fn)
2557

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

    
2574
    finally:
2575
      result = self.rpc.call_node_start_master(master, False, False)
2576
      msg = result.fail_msg
2577
      if msg:
2578
        self.LogWarning("Could not re-enable the master role on"
2579
                        " the master, please restart manually: %s", msg)
2580

    
2581

    
2582
class LUSetClusterParams(LogicalUnit):
2583
  """Change the parameters of the cluster.
2584

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

    
2607
  def CheckArguments(self):
2608
    """Check parameters
2609

2610
    """
2611
    if self.op.uid_pool:
2612
      uidpool.CheckUidPool(self.op.uid_pool)
2613

    
2614
    if self.op.add_uids:
2615
      uidpool.CheckUidPool(self.op.add_uids)
2616

    
2617
    if self.op.remove_uids:
2618
      uidpool.CheckUidPool(self.op.remove_uids)
2619

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

    
2628
  def BuildHooksEnv(self):
2629
    """Build hooks env.
2630

2631
    """
2632
    env = {
2633
      "OP_TARGET": self.cfg.GetClusterName(),
2634
      "NEW_VG_NAME": self.op.vg_name,
2635
      }
2636
    mn = self.cfg.GetMasterNode()
2637
    return env, [mn], [mn]
2638

    
2639
  def CheckPrereq(self):
2640
    """Check prerequisites.
2641

2642
    This checks whether the given params don't conflict and
2643
    if the given volume group is valid.
2644

2645
    """
2646
    if self.op.vg_name is not None and not self.op.vg_name:
2647
      if self.cfg.HasAnyDiskOfType(constants.LD_LV):
2648
        raise errors.OpPrereqError("Cannot disable lvm storage while lvm-based"
2649
                                   " instances exist", errors.ECODE_INVAL)
2650

    
2651
    if self.op.drbd_helper is not None and not self.op.drbd_helper:
2652
      if self.cfg.HasAnyDiskOfType(constants.LD_DRBD8):
2653
        raise errors.OpPrereqError("Cannot disable drbd helper while"
2654
                                   " drbd-based instances exist",
2655
                                   errors.ECODE_INVAL)
2656

    
2657
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2658

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

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

    
2694
    self.cluster = cluster = self.cfg.GetClusterInfo()
2695
    # validate params changes
2696
    if self.op.beparams:
2697
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2698
      self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
2699

    
2700
    if self.op.nicparams:
2701
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2702
      self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
2703
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2704
      nic_errors = []
2705

    
2706
      # check all instances for consistency
2707
      for instance in self.cfg.GetAllInstancesInfo().values():
2708
        for nic_idx, nic in enumerate(instance.nics):
2709
          params_copy = copy.deepcopy(nic.nicparams)
2710
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2711

    
2712
          # check parameter syntax
2713
          try:
2714
            objects.NIC.CheckParameterSyntax(params_filled)
2715
          except errors.ConfigurationError, err:
2716
            nic_errors.append("Instance %s, nic/%d: %s" %
2717
                              (instance.name, nic_idx, err))
2718

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

    
2728
    # hypervisor list/parameters
2729
    self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
2730
    if self.op.hvparams:
2731
      for hv_name, hv_dict in self.op.hvparams.items():
2732
        if hv_name not in self.new_hvparams:
2733
          self.new_hvparams[hv_name] = hv_dict
2734
        else:
2735
          self.new_hvparams[hv_name].update(hv_dict)
2736

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

    
2750
    # os parameters
2751
    self.new_osp = objects.FillDict(cluster.osparams, {})
2752
    if self.op.osparams:
2753
      for os_name, osp in self.op.osparams.items():
2754
        if os_name not in self.new_osp:
2755
          self.new_osp[os_name] = {}
2756

    
2757
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
2758
                                                  use_none=True)
2759

    
2760
        if not self.new_osp[os_name]:
2761
          # we removed all parameters
2762
          del self.new_osp[os_name]
2763
        else:
2764
          # check the parameter validity (remote check)
2765
          _CheckOSParams(self, False, [self.cfg.GetMasterNode()],
2766
                         os_name, self.new_osp[os_name])
2767

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

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

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

    
2809
    if self.op.default_iallocator:
2810
      alloc_script = utils.FindFile(self.op.default_iallocator,
2811
                                    constants.IALLOCATOR_SEARCH_PATH,
2812
                                    os.path.isfile)
2813
      if alloc_script is None:
2814
        raise errors.OpPrereqError("Invalid default iallocator script '%s'"
2815
                                   " specified" % self.op.default_iallocator,
2816
                                   errors.ECODE_INVAL)
2817

    
2818
  def Exec(self, feedback_fn):
2819
    """Change the parameters of the cluster.
2820

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

    
2854
    if self.op.candidate_pool_size is not None:
2855
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2856
      # we need to update the pool size here, otherwise the save will fail
2857
      _AdjustCandidatePool(self, [])
2858

    
2859
    if self.op.maintain_node_health is not None:
2860
      self.cluster.maintain_node_health = self.op.maintain_node_health
2861

    
2862
    if self.op.add_uids is not None:
2863
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
2864

    
2865
    if self.op.remove_uids is not None:
2866
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
2867

    
2868
    if self.op.uid_pool is not None:
2869
      self.cluster.uid_pool = self.op.uid_pool
2870

    
2871
    if self.op.default_iallocator is not None:
2872
      self.cluster.default_iallocator = self.op.default_iallocator
2873

    
2874
    self.cfg.Update(self.cluster, feedback_fn)
2875

    
2876

    
2877
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2878
  """Distribute additional files which are part of the cluster configuration.
2879

2880
  ConfigWriter takes care of distributing the config and ssconf files, but
2881
  there are more files which should be distributed to all nodes. This function
2882
  makes sure those are copied.
2883

2884
  @param lu: calling logical unit
2885
  @param additional_nodes: list of nodes not in the config to distribute to
2886

2887
  """
2888
  # 1. Gather target nodes
2889
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2890
  dist_nodes = lu.cfg.GetOnlineNodeList()
2891
  if additional_nodes is not None:
2892
    dist_nodes.extend(additional_nodes)
2893
  if myself.name in dist_nodes:
2894
    dist_nodes.remove(myself.name)
2895

    
2896
  # 2. Gather files to distribute
2897
  dist_files = set([constants.ETC_HOSTS,
2898
                    constants.SSH_KNOWN_HOSTS_FILE,
2899
                    constants.RAPI_CERT_FILE,
2900
                    constants.RAPI_USERS_FILE,
2901
                    constants.CONFD_HMAC_KEY,
2902
                    constants.CLUSTER_DOMAIN_SECRET_FILE,
2903
                   ])
2904

    
2905
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2906
  for hv_name in enabled_hypervisors:
2907
    hv_class = hypervisor.GetHypervisor(hv_name)
2908
    dist_files.update(hv_class.GetAncillaryFiles())
2909

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

    
2921

    
2922
class LURedistributeConfig(NoHooksLU):
2923
  """Force the redistribution of cluster configuration.
2924

2925
  This is a very simple LU.
2926

2927
  """
2928
  REQ_BGL = False
2929

    
2930
  def ExpandNames(self):
2931
    self.needed_locks = {
2932
      locking.LEVEL_NODE: locking.ALL_SET,
2933
    }
2934
    self.share_locks[locking.LEVEL_NODE] = 1
2935

    
2936
  def Exec(self, feedback_fn):
2937
    """Redistribute the configuration.
2938

2939
    """
2940
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2941
    _RedistributeAncillaryFiles(self)
2942

    
2943

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

2947
  """
2948
  if not instance.disks or disks is not None and not disks:
2949
    return True
2950

    
2951
  disks = _ExpandCheckDisks(instance, disks)
2952

    
2953
  if not oneshot:
2954
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2955

    
2956
  node = instance.primary_node
2957

    
2958
  for dev in disks:
2959
    lu.cfg.SetDiskID(dev, node)
2960

    
2961
  # TODO: Convert to utils.Retry
2962

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

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

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

    
3009
    if done or oneshot:
3010
      break
3011

    
3012
    time.sleep(min(60, max_time))
3013

    
3014
  if done:
3015
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
3016
  return not cumul_degraded
3017

    
3018

    
3019
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
3020
  """Check that mirrors are not degraded.
3021

3022
  The ldisk parameter, if True, will change the test from the
3023
  is_degraded attribute (which represents overall non-ok status for
3024
  the device(s)) to the ldisk (representing the local storage status).
3025

3026
  """
3027
  lu.cfg.SetDiskID(dev, node)
3028

    
3029
  result = True
3030

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

    
3046
  if dev.children:
3047
    for child in dev.children:
3048
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
3049

    
3050
  return result
3051

    
3052

    
3053
class LUDiagnoseOS(NoHooksLU):
3054
  """Logical unit for OS diagnose/query.
3055

3056
  """
3057
  _OP_PARAMS = [
3058
    _POutputFields,
3059
    ("names", _EmptyList, _TListOf(_TNonEmptyString)),
3060
    ]
3061
  REQ_BGL = False
3062
  _FIELDS_STATIC = utils.FieldSet()
3063
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants",
3064
                                   "parameters", "api_versions")
3065

    
3066
  def CheckArguments(self):
3067
    if self.op.names:
3068
      raise errors.OpPrereqError("Selective OS query not supported",
3069
                                 errors.ECODE_INVAL)
3070

    
3071
    _CheckOutputFields(static=self._FIELDS_STATIC,
3072
                       dynamic=self._FIELDS_DYNAMIC,
3073
                       selected=self.op.output_fields)
3074

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

    
3083
  @staticmethod
3084
  def _DiagnoseByOS(rlist):
3085
    """Remaps a per-node return list into an a per-os per-node dictionary
3086

3087
    @param rlist: a map with node names as keys and OS objects as values
3088

3089
    @rtype: dict
3090
    @return: a dictionary with osnames as keys and as value another
3091
        map, with nodes as keys and tuples of (path, status, diagnose,
3092
        variants, parameters, api_versions) as values, eg::
3093

3094
          {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
3095
                                     (/srv/..., False, "invalid api")],
3096
                           "node2": [(/srv/..., True, "", [], [])]}
3097
          }
3098

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

    
3123
  def Exec(self, feedback_fn):
3124
    """Compute the list of OSes.
3125

3126
    """
3127
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
3128
    node_data = self.rpc.call_os_diagnose(valid_nodes)
3129
    pol = self._DiagnoseByOS(node_data)
3130
    output = []
3131

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

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

    
3172
    return output
3173

    
3174

    
3175
class LURemoveNode(LogicalUnit):
3176
  """Logical unit for removing a node.
3177

3178
  """
3179
  HPATH = "node-remove"
3180
  HTYPE = constants.HTYPE_NODE
3181
  _OP_PARAMS = [
3182
    _PNodeName,
3183
    ]
3184

    
3185
  def BuildHooksEnv(self):
3186
    """Build hooks env.
3187

3188
    This doesn't run on the target node in the pre phase as a failed
3189
    node would then be impossible to remove.
3190

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

    
3204
  def CheckPrereq(self):
3205
    """Check prerequisites.
3206

3207
    This checks:
3208
     - the node exists in the configuration
3209
     - it does not have primary or secondary instances
3210
     - it's not the master
3211

3212
    Any errors are signaled by raising errors.OpPrereqError.
3213

3214
    """
3215
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3216
    node = self.cfg.GetNodeInfo(self.op.node_name)
3217
    assert node is not None
3218

    
3219
    instance_list = self.cfg.GetInstanceList()
3220

    
3221
    masternode = self.cfg.GetMasterNode()
3222
    if node.name == masternode:
3223
      raise errors.OpPrereqError("Node is the master node,"
3224
                                 " you need to failover first.",
3225
                                 errors.ECODE_INVAL)
3226

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

    
3236
  def Exec(self, feedback_fn):
3237
    """Removes the node from the cluster.
3238

3239
    """
3240
    node = self.node
3241
    logging.info("Stopping the node daemon and removing configs from node %s",
3242
                 node.name)
3243

    
3244
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3245

    
3246
    # Promote nodes to master candidate as needed
3247
    _AdjustCandidatePool(self, exceptions=[node.name])
3248
    self.context.RemoveNode(node.name)
3249

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

    
3258
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
3259
    msg = result.fail_msg
3260
    if msg:
3261
      self.LogWarning("Errors encountered on the remote node while leaving"
3262
                      " the cluster: %s", msg)
3263

    
3264
    # Remove node from our /etc/hosts
3265
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3266
      # FIXME: this should be done via an rpc call to node daemon
3267
      utils.RemoveHostFromEtcHosts(node.name)
3268
      _RedistributeAncillaryFiles(self)
3269

    
3270

    
3271
class LUQueryNodes(NoHooksLU):
3272
  """Logical unit for querying nodes.
3273

3274
  """
3275
  # pylint: disable-msg=W0142
3276
  _OP_PARAMS = [
3277
    _POutputFields,
3278
    ("names", _EmptyList, _TListOf(_TNonEmptyString)),
3279
    ("use_locking", False, _TBool),
3280
    ]
3281
  REQ_BGL = False
3282

    
3283
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
3284
                    "master_candidate", "offline", "drained"]
3285

    
3286
  _FIELDS_DYNAMIC = utils.FieldSet(
3287
    "dtotal", "dfree",
3288
    "mtotal", "mnode", "mfree",
3289
    "bootid",
3290
    "ctotal", "cnodes", "csockets",
3291
    )
3292

    
3293
  _FIELDS_STATIC = utils.FieldSet(*[
3294
    "pinst_cnt", "sinst_cnt",
3295
    "pinst_list", "sinst_list",
3296
    "pip", "sip", "tags",
3297
    "master",
3298
    "role"] + _SIMPLE_FIELDS
3299
    )
3300

    
3301
  def CheckArguments(self):
3302
    _CheckOutputFields(static=self._FIELDS_STATIC,
3303
                       dynamic=self._FIELDS_DYNAMIC,
3304
                       selected=self.op.output_fields)
3305

    
3306
  def ExpandNames(self):
3307
    self.needed_locks = {}
3308
    self.share_locks[locking.LEVEL_NODE] = 1
3309

    
3310
    if self.op.names:
3311
      self.wanted = _GetWantedNodes(self, self.op.names)
3312
    else:
3313
      self.wanted = locking.ALL_SET
3314

    
3315
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3316
    self.do_locking = self.do_node_query and self.op.use_locking
3317
    if self.do_locking:
3318
      # if we don't request only static fields, we need to lock the nodes
3319
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
3320

    
3321
  def Exec(self, feedback_fn):
3322
    """Computes the list of nodes and their attributes.
3323

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

    
3337
    nodenames = utils.NiceSort(nodenames)
3338
    nodelist = [all_info[name] for name in nodenames]
3339

    
3340
    # begin data gathering
3341

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

    
3367
    node_to_primary = dict([(name, set()) for name in nodenames])
3368
    node_to_secondary = dict([(name, set()) for name in nodenames])
3369

    
3370
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
3371
                             "sinst_cnt", "sinst_list"))
3372
    if inst_fields & frozenset(self.op.output_fields):
3373
      inst_data = self.cfg.GetAllInstancesInfo()
3374

    
3375
      for inst in inst_data.values():
3376
        if inst.primary_node in node_to_primary:
3377
          node_to_primary[inst.primary_node].add(inst.name)
3378
        for secnode in inst.secondary_nodes:
3379
          if secnode in node_to_secondary:
3380
            node_to_secondary[secnode].add(inst.name)
3381

    
3382
    master_node = self.cfg.GetMasterNode()
3383

    
3384
    # end data gathering
3385

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

    
3426
    return output
3427

    
3428

    
3429
class LUQueryNodeVolumes(NoHooksLU):
3430
  """Logical unit for getting volumes on node(s).
3431

3432
  """
3433
  _OP_PARAMS = [
3434
    ("nodes", _EmptyList, _TListOf(_TNonEmptyString)),
3435
    ("output_fields", _NoDefault, _TListOf(_TNonEmptyString)),
3436
    ]
3437
  REQ_BGL = False
3438
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3439
  _FIELDS_STATIC = utils.FieldSet("node")
3440

    
3441
  def CheckArguments(self):
3442
    _CheckOutputFields(static=self._FIELDS_STATIC,
3443
                       dynamic=self._FIELDS_DYNAMIC,
3444
                       selected=self.op.output_fields)
3445

    
3446
  def ExpandNames(self):
3447
    self.needed_locks = {}
3448
    self.share_locks[locking.LEVEL_NODE] = 1
3449
    if not self.op.nodes:
3450
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3451
    else:
3452
      self.needed_locks[locking.LEVEL_NODE] = \
3453
        _GetWantedNodes(self, self.op.nodes)
3454

    
3455
  def Exec(self, feedback_fn):
3456
    """Computes the list of nodes and their attributes.
3457

3458
    """
3459
    nodenames = self.acquired_locks[locking.LEVEL_NODE]
3460
    volumes = self.rpc.call_node_volumes(nodenames)
3461

    
3462
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3463
             in self.cfg.GetInstanceList()]
3464

    
3465
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3466

    
3467
    output = []
3468
    for node in nodenames:
3469
      nresult = volumes[node]
3470
      if nresult.offline:
3471
        continue
3472
      msg = nresult.fail_msg
3473
      if msg:
3474
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3475
        continue
3476

    
3477
      node_vols = nresult.payload[:]
3478
      node_vols.sort(key=lambda vol: vol['dev'])
3479

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

    
3506
        output.append(node_output)
3507

    
3508
    return output
3509

    
3510

    
3511
class LUQueryNodeStorage(NoHooksLU):
3512
  """Logical unit for getting information on storage units on node(s).
3513

3514
  """
3515
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3516
  _OP_PARAMS = [
3517
    ("nodes", _EmptyList, _TListOf(_TNonEmptyString)),
3518
    ("storage_type", _NoDefault, _CheckStorageType),
3519
    ("output_fields", _NoDefault, _TListOf(_TNonEmptyString)),
3520
    ("name", None, _TMaybeString),
3521
    ]
3522
  REQ_BGL = False
3523

    
3524
  def CheckArguments(self):
3525
    _CheckOutputFields(static=self._FIELDS_STATIC,
3526
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3527
                       selected=self.op.output_fields)
3528

    
3529
  def ExpandNames(self):
3530
    self.needed_locks = {}
3531
    self.share_locks[locking.LEVEL_NODE] = 1
3532

    
3533
    if self.op.nodes:
3534
      self.needed_locks[locking.LEVEL_NODE] = \
3535
        _GetWantedNodes(self, self.op.nodes)
3536
    else:
3537
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3538

    
3539
  def Exec(self, feedback_fn):
3540
    """Computes the list of nodes and their attributes.
3541

3542
    """
3543
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3544

    
3545
    # Always get name to sort by
3546
    if constants.SF_NAME in self.op.output_fields:
3547
      fields = self.op.output_fields[:]
3548
    else:
3549
      fields = [constants.SF_NAME] + self.op.output_fields
3550

    
3551
    # Never ask for node or type as it's only known to the LU
3552
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3553
      while extra in fields:
3554
        fields.remove(extra)
3555

    
3556
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3557
    name_idx = field_idx[constants.SF_NAME]
3558

    
3559
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3560
    data = self.rpc.call_storage_list(self.nodes,
3561
                                      self.op.storage_type, st_args,
3562
                                      self.op.name, fields)
3563

    
3564
    result = []
3565

    
3566
    for node in utils.NiceSort(self.nodes):
3567
      nresult = data[node]
3568
      if nresult.offline:
3569
        continue
3570

    
3571
      msg = nresult.fail_msg
3572
      if msg:
3573
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3574
        continue
3575

    
3576
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3577

    
3578
      for name in utils.NiceSort(rows.keys()):
3579
        row = rows[name]
3580

    
3581
        out = []
3582

    
3583
        for field in self.op.output_fields:
3584
          if field == constants.SF_NODE:
3585
            val = node
3586
          elif field == constants.SF_TYPE:
3587
            val = self.op.storage_type
3588
          elif field in field_idx:
3589
            val = row[field_idx[field]]
3590
          else:
3591
            raise errors.ParameterError(field)
3592

    
3593
          out.append(val)
3594

    
3595
        result.append(out)
3596

    
3597
    return result
3598

    
3599

    
3600
class LUModifyNodeStorage(NoHooksLU):
3601
  """Logical unit for modifying a storage volume on a node.
3602

3603
  """
3604
  _OP_PARAMS = [
3605
    _PNodeName,
3606
    ("storage_type", _NoDefault, _CheckStorageType),
3607
    ("name", _NoDefault, _TNonEmptyString),
3608
    ("changes", _NoDefault, _TDict),
3609
    ]
3610
  REQ_BGL = False
3611

    
3612
  def CheckArguments(self):
3613
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3614

    
3615
    storage_type = self.op.storage_type
3616

    
3617
    try:
3618
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3619
    except KeyError:
3620
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3621
                                 " modified" % storage_type,
3622
                                 errors.ECODE_INVAL)
3623

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

    
3631
  def ExpandNames(self):
3632
    self.needed_locks = {
3633
      locking.LEVEL_NODE: self.op.node_name,
3634
      }
3635

    
3636
  def Exec(self, feedback_fn):
3637
    """Computes the list of nodes and their attributes.
3638

3639
    """
3640
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3641
    result = self.rpc.call_storage_modify(self.op.node_name,
3642
                                          self.op.storage_type, st_args,
3643
                                          self.op.name, self.op.changes)
3644
    result.Raise("Failed to modify storage unit '%s' on %s" %
3645
                 (self.op.name, self.op.node_name))
3646

    
3647

    
3648
class LUAddNode(LogicalUnit):
3649
  """Logical unit for adding node to the cluster.
3650

3651
  """
3652
  HPATH = "node-add"
3653
  HTYPE = constants.HTYPE_NODE
3654
  _OP_PARAMS = [
3655
    _PNodeName,
3656
    ("primary_ip", None, _NoType),
3657
    ("secondary_ip", None, _TMaybeString),
3658
    ("readd", False, _TBool),
3659
    ]
3660

    
3661
  def CheckArguments(self):
3662
    # validate/normalize the node name
3663
    self.op.node_name = netutils.HostInfo.NormalizeName(self.op.node_name)
3664

    
3665
  def BuildHooksEnv(self):
3666
    """Build hooks env.
3667

3668
    This will run on all nodes before, and on all nodes + the new node after.
3669

3670
    """
3671
    env = {
3672
      "OP_TARGET": self.op.node_name,
3673
      "NODE_NAME": self.op.node_name,
3674
      "NODE_PIP": self.op.primary_ip,
3675
      "NODE_SIP": self.op.secondary_ip,
3676
      }
3677
    nodes_0 = self.cfg.GetNodeList()
3678
    nodes_1 = nodes_0 + [self.op.node_name, ]
3679
    return env, nodes_0, nodes_1
3680

    
3681
  def CheckPrereq(self):
3682
    """Check prerequisites.
3683

3684
    This checks:
3685
     - the new node is not already in the config
3686
     - it is resolvable
3687
     - its parameters (single/dual homed) matches the cluster
3688

3689
    Any errors are signaled by raising errors.OpPrereqError.
3690

3691
    """
3692
    node_name = self.op.node_name
3693
    cfg = self.cfg
3694

    
3695
    dns_data = netutils.GetHostInfo(node_name)
3696

    
3697
    node = dns_data.name
3698
    primary_ip = self.op.primary_ip = dns_data.ip
3699
    if self.op.secondary_ip is None:
3700
      self.op.secondary_ip = primary_ip
3701
    if not netutils.IsValidIP4(self.op.secondary_ip):
3702
      raise errors.OpPrereqError("Invalid secondary IP given",
3703
                                 errors.ECODE_INVAL)
3704
    secondary_ip = self.op.secondary_ip
3705

    
3706
    node_list = cfg.GetNodeList()
3707
    if not self.op.readd and node in node_list:
3708
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3709
                                 node, errors.ECODE_EXISTS)
3710
    elif self.op.readd and node not in node_list:
3711
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3712
                                 errors.ECODE_NOENT)
3713

    
3714
    self.changed_primary_ip = False
3715

    
3716
    for existing_node_name in node_list:
3717
      existing_node = cfg.GetNodeInfo(existing_node_name)
3718

    
3719
      if self.op.readd and node == existing_node_name:
3720
        if existing_node.secondary_ip != secondary_ip:
3721
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3722
                                     " address configuration as before",
3723
                                     errors.ECODE_INVAL)
3724
        if existing_node.primary_ip != primary_ip:
3725
          self.changed_primary_ip = True
3726

    
3727
        continue
3728

    
3729
      if (existing_node.primary_ip == primary_ip or
3730
          existing_node.secondary_ip == primary_ip or
3731
          existing_node.primary_ip == secondary_ip or
3732
          existing_node.secondary_ip == secondary_ip):
3733
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3734
                                   " existing node %s" % existing_node.name,
3735
                                   errors.ECODE_NOTUNIQUE)
3736

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

    
3752
    # checks reachability
3753
    if not netutils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3754
      raise errors.OpPrereqError("Node not reachable by ping",
3755
                                 errors.ECODE_ENVIRON)
3756

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

    
3765
    if self.op.readd:
3766
      exceptions = [node]
3767
    else:
3768
      exceptions = []
3769

    
3770
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3771

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

    
3782
  def Exec(self, feedback_fn):
3783
    """Adds the new node to the cluster.
3784

3785
    """
3786
    new_node = self.new_node
3787
    node = new_node.name
3788

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

    
3801
    # notify the user about any possible mc promotion
3802
    if new_node.master_candidate:
3803
      self.LogInfo("Node will be a master candidate")
3804

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

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

    
3825
      for i in keyfiles:
3826
        keyarray.append(utils.ReadFile(i))
3827

    
3828
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3829
                                      keyarray[2], keyarray[3], keyarray[4],
3830
                                      keyarray[5])
3831
      result.Raise("Cannot transfer ssh keys to the new node")
3832

    
3833
    # Add node to our /etc/hosts, and add key to known_hosts
3834
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3835
      # FIXME: this should be done via an rpc call to node daemon
3836
      utils.AddHostToEtcHosts(new_node.name)
3837

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

    
3848
    node_verify_list = [self.cfg.GetMasterNode()]
3849
    node_verify_param = {
3850
      constants.NV_NODELIST: [node],
3851
      # TODO: do a node-net-test as well?
3852
    }
3853

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

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

    
3882

    
3883
class LUSetNodeParams(LogicalUnit):
3884
  """Modifies the parameters of a node.
3885

3886
  """
3887
  HPATH = "node-modify"
3888
  HTYPE = constants.HTYPE_NODE
3889
  _OP_PARAMS = [
3890
    _PNodeName,
3891
    ("master_candidate", None, _TMaybeBool),
3892
    ("offline", None, _TMaybeBool),
3893
    ("drained", None, _TMaybeBool),
3894
    ("auto_promote", False, _TBool),
3895
    _PForce,
3896
    ]
3897
  REQ_BGL = False
3898

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

    
3910
    # Boolean value that tells us whether we're offlining or draining the node
3911
    self.offline_or_drain = (self.op.offline == True or
3912
                             self.op.drained == True)
3913
    self.deoffline_or_drain = (self.op.offline == False or
3914
                               self.op.drained == False)
3915
    self.might_demote = (self.op.master_candidate == False or
3916
                         self.offline_or_drain)
3917

    
3918
    self.lock_all = self.op.auto_promote and self.might_demote
3919

    
3920

    
3921
  def ExpandNames(self):
3922
    if self.lock_all:
3923
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3924
    else:
3925
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3926

    
3927
  def BuildHooksEnv(self):
3928
    """Build hooks env.
3929

3930
    This runs on the master node.
3931

3932
    """
3933
    env = {
3934
      "OP_TARGET": self.op.node_name,
3935
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3936
      "OFFLINE": str(self.op.offline),
3937
      "DRAINED": str(self.op.drained),
3938
      }
3939
    nl = [self.cfg.GetMasterNode(),
3940
          self.op.node_name]
3941
    return env, nl, nl
3942

    
3943
  def CheckPrereq(self):
3944
    """Check prerequisites.
3945

3946
    This only checks the instance list against the existing names.
3947

3948
    """
3949
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3950

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

    
3960

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

    
3972
    if (self.op.master_candidate == True and
3973
        ((node.offline and not self.op.offline == False) or
3974
         (node.drained and not self.op.drained == False))):
3975
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3976
                                 " to master_candidate" % node.name,
3977
                                 errors.ECODE_INVAL)
3978

    
3979
    # If we're being deofflined/drained, we'll MC ourself if needed
3980
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3981
        self.op.master_candidate == True and not node.master_candidate):
3982
      self.op.master_candidate = _DecideSelfPromotion(self)
3983
      if self.op.master_candidate:
3984
        self.LogInfo("Autopromoting node to master candidate")
3985

    
3986
    return
3987

    
3988
  def Exec(self, feedback_fn):
3989
    """Modifies a node.
3990

3991
    """
3992
    node = self.node
3993

    
3994
    result = []
3995
    changed_mc = False
3996

    
3997
    if self.op.offline is not None:
3998
      node.offline = self.op.offline
3999
      result.append(("offline", str(self.op.offline)))
4000
      if self.op.offline == True:
4001
        if node.master_candidate:
4002
          node.master_candidate = False
4003
          changed_mc = True
4004
          result.append(("master_candidate", "auto-demotion due to offline"))
4005
        if node.drained:
4006
          node.drained = False
4007
          result.append(("drained", "clear drained status due to offline"))
4008

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

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

    
4035
    # we locked all nodes, we adjust the CP before updating this node
4036
    if self.lock_all:
4037
      _AdjustCandidatePool(self, [node.name])
4038

    
4039
    # this will trigger configuration file update, if needed
4040
    self.cfg.Update(node, feedback_fn)
4041

    
4042
    # this will trigger job queue propagation or cleanup
4043
    if changed_mc:
4044
      self.context.ReaddNode(node)
4045

    
4046
    return result
4047

    
4048

    
4049
class LUPowercycleNode(NoHooksLU):
4050
  """