Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 46d2d8a2

History | View | Annotate | Download (365.3 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
  try:
1092
    variant = name.split("+", 1)[1]
1093
  except IndexError:
1094
    raise errors.OpPrereqError("OS name must include a variant",
1095
                               errors.ECODE_INVAL)
1096

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

    
1100

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

    
1104

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

1108
  """
1109

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

    
1112

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

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

    
1120

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

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

    
1128

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

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

    
1138
  return []
1139

    
1140

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

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

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

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

    
1155
  return faulty
1156

    
1157

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

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

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

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

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

    
1189

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

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

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

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

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

1208
    """
1209
    return True
1210

    
1211

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

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

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

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

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

1229
    This checks whether the cluster is empty.
1230

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

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

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

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

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

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

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

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

    
1270
    return master
1271

    
1272

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

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

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

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

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

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

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

    
1305

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1504

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

    
1510
    return True
1511

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1623

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1847
    nimg.os_fail = test
1848

    
1849
    if test:
1850
      return
1851

    
1852
    os_dict = {}
1853

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

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

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

    
1866
    nimg.oslist = os_dict
1867

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2024
    return env, [], all_nodes
2025

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

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

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

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

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

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

    
2070
    local_checksums = utils.FingerprintFiles(file_names)
2071

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

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

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

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

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

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

    
2114
      inst_config.MapLVsByNode(node_vol_should)
2115

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

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

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

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

    
2138
    all_drbd_map = self.cfg.ComputeDRBDMap()
2139

    
2140
    feedback_fn("* Verifying node status")
2141

    
2142
    refos_img = None
2143

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

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

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

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

    
2172
      nresult = all_nvinfo[node].payload
2173

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2267
    return not self.bad
2268

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

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

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

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

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

    
2313
      return lu_result
2314

    
2315

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

2319
  """
2320
  REQ_BGL = False
2321

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

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

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

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

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

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

    
2357
    if not nv_dict:
2358
      return result
2359

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

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

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

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

    
2387
    return result
2388

    
2389

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2505

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

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

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

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

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

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

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

    
2546
    self.op.name = new_name
2547

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

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

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

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

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

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

    
2589
    return clustername
2590

    
2591

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2890

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

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

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

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

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

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

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

    
2935

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

2939
  This is a very simple LU.
2940

2941
  """
2942
  REQ_BGL = False
2943

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

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

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

    
2957

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

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

    
2965
  disks = _ExpandCheckDisks(instance, disks)
2966

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

    
2970
  node = instance.primary_node
2971

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

    
2975
  # TODO: Convert to utils.Retry
2976

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

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

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

    
3023
    if done or oneshot:
3024
      break
3025

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

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

    
3032

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

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

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

    
3043
  result = True
3044

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

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

    
3064
  return result
3065

    
3066

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3186
    return output
3187

    
3188

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

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

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

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

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

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

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

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

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

    
3233
    instance_list = self.cfg.GetInstanceList()
3234

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

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

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

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

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

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

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

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

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

    
3284

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

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

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

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

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

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

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

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

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

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

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

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

    
3354
    # begin data gathering
3355

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

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

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

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

    
3396
    master_node = self.cfg.GetMasterNode()
3397

    
3398
    # end data gathering
3399

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

    
3440
    return output
3441

    
3442

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

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

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

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

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

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

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

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

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

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

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

    
3520
        output.append(node_output)
3521

    
3522
    return output
3523

    
3524

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

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

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

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

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

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

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

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

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

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

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

    
3578
    result = []
3579

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

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

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

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

    
3595
        out = []
3596

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

    
3607
          out.append(val)
3608

    
3609
        result.append(out)
3610

    
3611
    return result
3612

    
3613

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

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

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

    
3629
    storage_type = self.op.storage_type
3630

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

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

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

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

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

    
3661

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

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

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

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

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

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

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

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

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

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

    
3709
    dns_data = netutils.GetHostInfo(node_name)
3710

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

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

    
3728
    self.changed_primary_ip = False
3729

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

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

    
3741
        continue
3742

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3896

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

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

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

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

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

    
3934

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

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

3944
    This runs on the master node.
3945

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

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

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

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

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

    
3974

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

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

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

    
4000
    return
4001

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

4005
    """
4006
    node = self.node
4007

    
4008
    result = []
4009
    changed_mc = False
4010

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

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

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