Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 870dc44c

History | View | Annotate | Download (365.8 kB)

1
#
2
#
3

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

    
21

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

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

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

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

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

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

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

    
59

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

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

66
  """
67
  return []
68

    
69

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

73
  """
74
  return {}
75

    
76

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

    
80

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

    
84

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

89
  """
90
  return val is not None
91

    
92

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

96
  """
97
  return val is None
98

    
99

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

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

    
106

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

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

    
113

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

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

    
120

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

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

    
127

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

131
  """
132
  return bool(val)
133

    
134

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

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

    
141

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

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

    
149

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

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

    
156

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

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

    
166

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

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

    
175

    
176
# Type aliases
177

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

    
181

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

    
185

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

    
189

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

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

    
196

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

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

    
204

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

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

    
214

    
215
# Common opcode attributes
216

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

    
220

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

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

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

    
231

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

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

    
239
#: the obsolete 'live' mode (boolean)
240
_PMigrationLive = ("live", None, _TMaybeBool)
241

    
242

    
243
# End types
244
class LogicalUnit(object):
245
  """Logical Unit base class.
246

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

256
  Note that all commands require root permissions.
257

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

263
  """
264
  HPATH = None
265
  HTYPE = None
266
  _OP_PARAMS = []
267
  REQ_BGL = True
268

    
269
  def __init__(self, processor, op, context, rpc):
270
    """Constructor for LogicalUnit.
271

272
    This needs to be overridden in derived classes in order to check op
273
    validity.
274

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

    
302
    # Tasklets
303
    self.tasklets = None
304

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

    
332
    self.CheckArguments()
333

    
334
  def __GetSSH(self):
335
    """Returns the SshRunner object
336

337
    """
338
    if not self.__ssh:
339
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
340
    return self.__ssh
341

    
342
  ssh = property(fget=__GetSSH)
343

    
344
  def CheckArguments(self):
345
    """Check syntactic validity for the opcode arguments.
346

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

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

356
    The function is allowed to change the self.op attribute so that
357
    later methods can no longer worry about missing parameters.
358

359
    """
360
    pass
361

    
362
  def ExpandNames(self):
363
    """Expand names for this LU.
364

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

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

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

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

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

387
    Examples::
388

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

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

    
410
  def DeclareLocks(self, level):
411
    """Declare LU locking needs for a level
412

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

420
    This function is only called if you have something already set in
421
    self.needed_locks for the level.
422

423
    @param level: Locking level which is going to be locked
424
    @type level: member of ganeti.locking.LEVELS
425

426
    """
427

    
428
  def CheckPrereq(self):
429
    """Check prerequisites for this LU.
430

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

436
    The method should raise errors.OpPrereqError in case something is
437
    not fulfilled. Its return value is ignored.
438

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

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

    
451
  def Exec(self, feedback_fn):
452
    """Execute the LU.
453

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

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

    
466
  def BuildHooksEnv(self):
467
    """Build hooks environment for this LU.
468

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

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

480
    No nodes should be returned as an empty list (and not None).
481

482
    Note that if the HPATH for a LU class is None, this function will
483
    not be called.
484

485
    """
486
    raise NotImplementedError
487

    
488
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
489
    """Notify the LU about the results of its hooks.
490

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

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

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

    
512
  def _ExpandAndLockInstance(self):
513
    """Helper function to expand and lock an instance.
514

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

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

    
531
  def _LockInstancesNodes(self, primary_only=False):
532
    """Helper function to declare instances' nodes for locking.
533

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

539
    It should be called from DeclareLocks, and for safety only works if
540
    self.recalculate_locks[locking.LEVEL_NODE] is set.
541

542
    In the future it may grow parameters to just lock some instance's nodes, or
543
    to just lock primaries or secondary nodes, if needed.
544

545
    If should be called in DeclareLocks in a way similar to::
546

547
      if level == locking.LEVEL_NODE:
548
        self._LockInstancesNodes()
549

550
    @type primary_only: boolean
551
    @param primary_only: only lock primary nodes of locked instances
552

553
    """
554
    assert locking.LEVEL_NODE in self.recalculate_locks, \
555
      "_LockInstancesNodes helper function called with no nodes to recalculate"
556

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

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

    
569
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
570
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
571
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
572
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
573

    
574
    del self.recalculate_locks[locking.LEVEL_NODE]
575

    
576

    
577
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
578
  """Simple LU which runs no hooks.
579

580
  This LU is intended as a parent for other LogicalUnits which will
581
  run no hooks, in order to reduce duplicate code.
582

583
  """
584
  HPATH = None
585
  HTYPE = None
586

    
587
  def BuildHooksEnv(self):
588
    """Empty BuildHooksEnv for NoHooksLu.
589

590
    This just raises an error.
591

592
    """
593
    assert False, "BuildHooksEnv called for NoHooksLUs"
594

    
595

    
596
class Tasklet:
597
  """Tasklet base class.
598

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

603
  Subclasses must follow these rules:
604
    - Implement CheckPrereq
605
    - Implement Exec
606

607
  """
608
  def __init__(self, lu):
609
    self.lu = lu
610

    
611
    # Shortcuts
612
    self.cfg = lu.cfg
613
    self.rpc = lu.rpc
614

    
615
  def CheckPrereq(self):
616
    """Check prerequisites for this tasklets.
617

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

622
    The method should raise errors.OpPrereqError in case something is not
623
    fulfilled. Its return value is ignored.
624

625
    This method should also update all parameters to their canonical form if it
626
    hasn't been done before.
627

628
    """
629
    pass
630

    
631
  def Exec(self, feedback_fn):
632
    """Execute the tasklet.
633

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

638
    """
639
    raise NotImplementedError
640

    
641

    
642
def _GetWantedNodes(lu, nodes):
643
  """Returns list of checked and expanded node names.
644

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

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

    
658
  wanted = [_ExpandNodeName(lu.cfg, name) for name in nodes]
659
  return utils.NiceSort(wanted)
660

    
661

    
662
def _GetWantedInstances(lu, instances):
663
  """Returns list of checked and expanded instance names.
664

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

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

    
681

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

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

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

    
714

    
715
def _CheckOutputFields(static, dynamic, selected):
716
  """Checks whether all selected fields are valid.
717

718
  @type static: L{utils.FieldSet}
719
  @param static: static fields set
720
  @type dynamic: L{utils.FieldSet}
721
  @param dynamic: dynamic fields set
722

723
  """
724
  f = utils.FieldSet()
725
  f.Extend(static)
726
  f.Extend(dynamic)
727

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

    
733

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

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

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

    
748

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

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

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

    
761

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

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

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

    
774

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

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

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

    
792

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

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

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

    
803

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

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

    
816

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

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

    
828

    
829
def _GetClusterDomainSecret():
830
  """Reads the cluster domain secret.
831

832
  """
833
  return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
834
                               strict=True)
835

    
836

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

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

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

    
852

    
853
def _ExpandItemName(fn, name, kind):
854
  """Expand an item name.
855

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

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

    
869

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

    
874

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

    
879

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

885
  This builds the hook environment from individual variables.
886

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

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

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

    
949
  env["INSTANCE_NIC_COUNT"] = nic_count
950

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

    
959
  env["INSTANCE_DISK_COUNT"] = disk_count
960

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

    
965
  return env
966

    
967

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

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

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

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

    
991

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

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

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

    
1029

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

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

    
1045

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

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

    
1056

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

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

    
1070

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

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

    
1079

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

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

1088
  """
1089
  if not os_obj.supported_variants:
1090
    return
1091
  variant = objects.OS.GetVariant(name)
1092
  if not variant:
1093
    raise errors.OpPrereqError("OS name must include a variant",
1094
                               errors.ECODE_INVAL)
1095

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

    
1099

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

    
1103

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

1107
  """
1108

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

    
1111

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

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

    
1119

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

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

    
1127

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

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

    
1137
  return []
1138

    
1139

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

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

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

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

    
1154
  return faulty
1155

    
1156

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

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

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

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

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

    
1188

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

1192
  """
1193
  HPATH = "cluster-init"
1194
  HTYPE = constants.HTYPE_CLUSTER
1195

    
1196
  def BuildHooksEnv(self):
1197
    """Build hooks env.
1198

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

    
1204
  def Exec(self, feedback_fn):
1205
    """Nothing to do.
1206

1207
    """
1208
    return True
1209

    
1210

    
1211
class LUDestroyCluster(LogicalUnit):
1212
  """Logical unit for destroying the cluster.
1213

1214
  """
1215
  HPATH = "cluster-destroy"
1216
  HTYPE = constants.HTYPE_CLUSTER
1217

    
1218
  def BuildHooksEnv(self):
1219
    """Build hooks env.
1220

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

    
1225
  def CheckPrereq(self):
1226
    """Check prerequisites.
1227

1228
    This checks whether the cluster is empty.
1229

1230
    Any errors are signaled by raising errors.OpPrereqError.
1231

1232
    """
1233
    master = self.cfg.GetMasterNode()
1234

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

    
1246
  def Exec(self, feedback_fn):
1247
    """Destroys the cluster.
1248

1249
    """
1250
    master = self.cfg.GetMasterNode()
1251
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
1252

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

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

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

    
1269
    return master
1270

    
1271

    
1272
def _VerifyCertificate(filename):
1273
  """Verifies a certificate for LUVerifyCluster.
1274

1275
  @type filename: string
1276
  @param filename: Path to PEM file
1277

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

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

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

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

    
1302
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1303

    
1304

    
1305
class LUVerifyCluster(LogicalUnit):
1306
  """Verifies the cluster status.
1307

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

    
1320
  TCLUSTER = "cluster"
1321
  TNODE = "node"
1322
  TINSTANCE = "instance"
1323

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

    
1349
  ETYPE_FIELD = "code"
1350
  ETYPE_ERROR = "ERROR"
1351
  ETYPE_WARNING = "WARNING"
1352

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

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

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

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

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

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

1413
    This must be called only from Exec and functions called from Exec.
1414

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

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

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

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

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

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

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

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

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

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

    
1487
    # node seems compatible, we can actually try to look into its results
1488

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

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

    
1503

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

    
1509
    return True
1510

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

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

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

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

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

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

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

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

1551
    """
1552
    if vg_name is None:
1553
      return
1554

    
1555
    node = ninfo.name
1556
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1557

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

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

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

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

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

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

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

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

    
1622

    
1623
  def _VerifyInstance(self, instance, instanceconfig, node_image):
1624
    """Verify an instance.
1625

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

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

    
1633
    node_vol_should = {}
1634
    instanceconfig.MapLVsByNode(node_vol_should)
1635

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

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

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

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

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

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

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

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

1683
    This checks what instances are running but unknown to the cluster.
1684

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1846
    nimg.os_fail = test
1847

    
1848
    if test:
1849
      return
1850

    
1851
    os_dict = {}
1852

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

    
1856
      if name not in os_dict:
1857
        os_dict[name] = []
1858

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

    
1865
    nimg.oslist = os_dict
1866

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

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

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

    
1879
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1880

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

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

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

1923
    This function will update a L{NodeImage}'s internal structures
1924
    with data from the remote call.
1925

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

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

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

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

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

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

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

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

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

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

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

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

    
2009
  def BuildHooksEnv(self):
2010
    """Build hooks env.
2011

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

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

    
2023
    return env, [], all_nodes
2024

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

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

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

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

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

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

    
2069
    local_checksums = utils.FingerprintFiles(file_names)
2070

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

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

    
2095
    if drbd_helper:
2096
      node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
2097

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

    
2103
    for instance in instancelist:
2104
      inst_config = instanceinfo[instance]
2105

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

    
2113
      inst_config.MapLVsByNode(node_vol_should)
2114

    
2115
      pnode = inst_config.primary_node
2116
      node_image[pnode].pinst.append(instance)
2117

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

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

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

    
2137
    all_drbd_map = self.cfg.ComputeDRBDMap()
2138

    
2139
    feedback_fn("* Verifying node status")
2140

    
2141
    refos_img = None
2142

    
2143
    for node_i in nodeinfo:
2144
      node = node_i.name
2145
      nimg = node_image[node]
2146

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

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

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

    
2171
      nresult = all_nvinfo[node].payload
2172

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

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

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

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

    
2205
      if pnode_img.offline:
2206
        inst_nodes_offline.append(pnode)
2207

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

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

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

    
2228
        if s_img.offline:
2229
          inst_nodes_offline.append(snode)
2230

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

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

    
2244
    feedback_fn("* Verifying orphan instances")
2245
    self._VerifyOrphanInstances(instancelist, node_image)
2246

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

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

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

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

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

    
2266
    return not self.bad
2267

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

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

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

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

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

    
2312
      return lu_result
2313

    
2314

    
2315
class LUVerifyDisks(NoHooksLU):
2316
  """Verifies the cluster disks status.
2317

2318
  """
2319
  REQ_BGL = False
2320

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

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

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

2336
    """
2337
    result = res_nodes, res_instances, res_missing = {}, [], {}
2338

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

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

    
2356
    if not nv_dict:
2357
      return result
2358

    
2359
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
2360

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

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

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

    
2386
    return result
2387

    
2388

    
2389
class LURepairDiskSizes(NoHooksLU):
2390
  """Verifies the cluster disks sizes.
2391

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

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

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

    
2419
  def CheckPrereq(self):
2420
    """Check prerequisites.
2421

2422
    This only checks the optional instance list against the existing names.
2423

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

    
2428
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2429
                             in self.wanted_names]
2430

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

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

2437
    @param disk: an L{ganeti.objects.Disk} object
2438

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

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

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

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

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

    
2504

    
2505
class LURenameCluster(LogicalUnit):
2506
  """Rename the cluster.
2507

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

    
2513
  def BuildHooksEnv(self):
2514
    """Build hooks env.
2515

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

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

2528
    """
2529
    hostname = netutils.GetHostInfo(self.op.name)
2530

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

    
2545
    self.op.name = new_name
2546

    
2547
  def Exec(self, feedback_fn):
2548
    """Rename the cluster.
2549

2550
    """
2551
    clustername = self.op.name
2552
    ip = self.ip
2553

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

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

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

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

    
2588
    return clustername
2589

    
2590

    
2591
class LUSetClusterParams(LogicalUnit):
2592
  """Change the parameters of the cluster.
2593

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

    
2617
  def CheckArguments(self):
2618
    """Check parameters
2619

2620
    """
2621
    if self.op.uid_pool:
2622
      uidpool.CheckUidPool(self.op.uid_pool)
2623

    
2624
    if self.op.add_uids:
2625
      uidpool.CheckUidPool(self.op.add_uids)
2626

    
2627
    if self.op.remove_uids:
2628
      uidpool.CheckUidPool(self.op.remove_uids)
2629

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

    
2638
  def BuildHooksEnv(self):
2639
    """Build hooks env.
2640

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

    
2649
  def CheckPrereq(self):
2650
    """Check prerequisites.
2651

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

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

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

    
2667
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2668

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

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

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

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

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

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

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

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

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

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

    
2767
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
2768
                                                  use_none=True)
2769

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

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

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

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

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

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

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

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

    
2869
    if self.op.maintain_node_health is not None:
2870
      self.cluster.maintain_node_health = self.op.maintain_node_health
2871

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

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

    
2878
    if self.op.uid_pool is not None:
2879
      self.cluster.uid_pool = self.op.uid_pool
2880

    
2881
    if self.op.default_iallocator is not None:
2882
      self.cluster.default_iallocator = self.op.default_iallocator
2883

    
2884
    if self.op.reserved_lvs is not None:
2885
      self.cluster.reserved_lvs = self.op.reserved_lvs
2886

    
2887
    self.cfg.Update(self.cluster, feedback_fn)
2888

    
2889

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

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

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

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

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

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

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

    
2934

    
2935
class LURedistributeConfig(NoHooksLU):
2936
  """Force the redistribution of cluster configuration.
2937

2938
  This is a very simple LU.
2939

2940
  """
2941
  REQ_BGL = False
2942

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

    
2949
  def Exec(self, feedback_fn):
2950
    """Redistribute the configuration.
2951

2952
    """
2953
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2954
    _RedistributeAncillaryFiles(self)
2955

    
2956

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

2960
  """
2961
  if not instance.disks or disks is not None and not disks:
2962
    return True
2963

    
2964
  disks = _ExpandCheckDisks(instance, disks)
2965

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

    
2969
  node = instance.primary_node
2970

    
2971
  for dev in disks:
2972
    lu.cfg.SetDiskID(dev, node)
2973

    
2974
  # TODO: Convert to utils.Retry
2975

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

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

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

    
3022
    if done or oneshot:
3023
      break
3024

    
3025
    time.sleep(min(60, max_time))
3026

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

    
3031

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

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

3039
  """
3040
  lu.cfg.SetDiskID(dev, node)
3041

    
3042
  result = True
3043

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

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

    
3063
  return result
3064

    
3065

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

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

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

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

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

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

3100
    @param rlist: a map with node names as keys and OS objects as values
3101

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

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

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

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

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

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

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

    
3185
    return output
3186

    
3187

    
3188
class LURemoveNode(LogicalUnit):
3189
  """Logical unit for removing a node.
3190

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

    
3198
  def BuildHooksEnv(self):
3199
    """Build hooks env.
3200

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

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

    
3217
  def CheckPrereq(self):
3218
    """Check prerequisites.
3219

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

3225
    Any errors are signaled by raising errors.OpPrereqError.
3226

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

    
3232
    instance_list = self.cfg.GetInstanceList()
3233

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

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

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

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

    
3257
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3258

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

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

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

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

    
3283

    
3284
class LUQueryNodes(NoHooksLU):
3285
  """Logical unit for querying nodes.
3286

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

    
3296
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
3297
                    "master_candidate", "offline", "drained"]
3298

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

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

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

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

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

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

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

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

    
3350
    nodenames = utils.NiceSort(nodenames)
3351
    nodelist = [all_info[name] for name in nodenames]
3352

    
3353
    # begin data gathering
3354

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

    
3380
    node_to_primary = dict([(name, set()) for name in nodenames])
3381
    node_to_secondary = dict([(name, set()) for name in nodenames])
3382

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

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

    
3395
    master_node = self.cfg.GetMasterNode()
3396

    
3397
    # end data gathering
3398

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

    
3439
    return output
3440

    
3441

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

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

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

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

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

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

    
3475
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3476
             in self.cfg.GetInstanceList()]
3477

    
3478
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3479

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

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

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

    
3519
        output.append(node_output)
3520

    
3521
    return output
3522

    
3523

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

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

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

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

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

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

3555
    """
3556
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3557

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

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

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

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

    
3577
    result = []
3578

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

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

    
3589
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3590

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

    
3594
        out = []
3595

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

    
3606
          out.append(val)
3607

    
3608
        result.append(out)
3609

    
3610
    return result
3611

    
3612

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

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

    
3625
  def CheckArguments(self):
3626
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3627

    
3628
    storage_type = self.op.storage_type
3629

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

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

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

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

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

    
3660

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

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

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

    
3678
  def BuildHooksEnv(self):
3679
    """Build hooks env.
3680

3681
    This will run on all nodes before, and on all nodes + the new node after.
3682

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

    
3694
  def CheckPrereq(self):
3695
    """Check prerequisites.
3696

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

3702
    Any errors are signaled by raising errors.OpPrereqError.
3703

3704
    """
3705
    node_name = self.op.node_name
3706
    cfg = self.cfg
3707

    
3708
    dns_data = netutils.GetHostInfo(node_name)
3709

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

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

    
3727
    self.changed_primary_ip = False
3728

    
3729
    for existing_node_name in node_list:
3730
      existing_node = cfg.GetNodeInfo(existing_node_name)
3731

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

    
3740
        continue
3741

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

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

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

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

    
3778
    if self.op.readd:
3779
      exceptions = [node]
3780
    else:
3781
      exceptions = []
3782

    
3783
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3784

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

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

3798
    """
3799
    new_node = self.new_node
3800
    node = new_node.name
3801

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

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

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

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

    
3838
      for i in keyfiles:
3839
        keyarray.append(utils.ReadFile(i))
3840

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

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

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

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

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

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

    
3895

    
3896
class LUSetNodeParams(LogicalUnit):
3897
  """Modifies the parameters of a node.
3898

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

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

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

    
3931
    self.lock_all = self.op.auto_promote and self.might_demote
3932

    
3933

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

    
3940
  def BuildHooksEnv(self):
3941
    """Build hooks env.
3942

3943
    This runs on the master node.
3944

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

    
3956
  def CheckPrereq(self):
3957
    """Check prerequisites.
3958

3959
    This only checks the instance list against the existing names.
3960

3961
    """
3962
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3963

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

    
3973

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

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

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

    
3999
    return
4000

    
4001
  def Exec(self, feedback_fn):
4002
    """Modifies a node.
4003

4004
    """
4005
    node = self.node
4006

    
4007
    result = []
4008
    changed_mc = False
4009

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

    
4022
    if self.op.master_candidate is not None:
4023
      node.master_candidate = self.op.master_candidate
4024
      changed_mc = True
4025
      result.append(("master_candidate", str(self.op.master_candidate)))
4026
      if self.op.master_candidate == False:
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

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

    
4048
    # we locked all nodes, we adjust the CP before updating this node
4049
    if self.lock_all:
4050
      _AdjustCandidatePool(self, [node.name])
4051

    
4052
    # this will trigger configuration file update, if needed
4053
    self.cfg.Update(node, feedback_fn)
4054

    
4055
    # this will trigger job queue propagation or cleanup
4056
    if changed_mc:
4057
      self.context.ReaddNode(node)
4058

    
4059
    return result
4060

    
4061

    
4062
class LUPowercycleNode(NoHooksLU):
4063
  """Powercycles a node.
4064

4065
  """
4066
  _OP_PARAMS = [
4067
    _PNodeName,
4068
    _PForce,
4069
    ]
4070
  REQ_BGL = False
4071

    
4072
  def CheckArguments(self):
4073
    self.op.node_name = _ExpandNodeName(self.cfg,