Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 21232d04

History | View | Annotate | Download (371.8 kB)

1
#
2
#
3

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

    
21

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

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

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

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

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

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

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

    
60
# Common opcode attributes
61

    
62
#: output fields for a query operation
63
_POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString))
64

    
65

    
66
#: the shutdown timeout
67
_PShutdownTimeout = ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
68
                     ht.TPositiveInt)
69

    
70
#: the force parameter
71
_PForce = ("force", False, ht.TBool)
72

    
73
#: a required instance name (for single-instance LUs)
74
_PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString)
75

    
76
#: Whether to ignore offline nodes
77
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool)
78

    
79
#: a required node name (for single-node LUs)
80
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString)
81

    
82
#: the migration type (live/non-live)
83
_PMigrationMode = ("mode", None,
84
                   ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)))
85

    
86
#: the obsolete 'live' mode (boolean)
87
_PMigrationLive = ("live", None, ht.TMaybeBool)
88

    
89

    
90
# End types
91
class LogicalUnit(object):
92
  """Logical Unit base class.
93

94
  Subclasses must follow these rules:
95
    - implement ExpandNames
96
    - implement CheckPrereq (except when tasklets are used)
97
    - implement Exec (except when tasklets are used)
98
    - implement BuildHooksEnv
99
    - redefine HPATH and HTYPE
100
    - optionally redefine their run requirements:
101
        REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
102

103
  Note that all commands require root permissions.
104

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

110
  """
111
  HPATH = None
112
  HTYPE = None
113
  _OP_PARAMS = []
114
  REQ_BGL = True
115

    
116
  def __init__(self, processor, op, context, rpc):
117
    """Constructor for LogicalUnit.
118

119
    This needs to be overridden in derived classes in order to check op
120
    validity.
121

122
    """
123
    self.proc = processor
124
    self.op = op
125
    self.cfg = context.cfg
126
    self.context = context
127
    self.rpc = rpc
128
    # Dicts used to declare locking needs to mcpu
129
    self.needed_locks = None
130
    self.acquired_locks = {}
131
    self.share_locks = dict.fromkeys(locking.LEVELS, 0)
132
    self.add_locks = {}
133
    self.remove_locks = {}
134
    # Used to force good behavior when calling helper functions
135
    self.recalculate_locks = {}
136
    self.__ssh = None
137
    # logging
138
    self.Log = processor.Log # pylint: disable-msg=C0103
139
    self.LogWarning = processor.LogWarning # pylint: disable-msg=C0103
140
    self.LogInfo = processor.LogInfo # pylint: disable-msg=C0103
141
    self.LogStep = processor.LogStep # pylint: disable-msg=C0103
142
    # support for dry-run
143
    self.dry_run_result = None
144
    # support for generic debug attribute
145
    if (not hasattr(self.op, "debug_level") or
146
        not isinstance(self.op.debug_level, int)):
147
      self.op.debug_level = 0
148

    
149
    # Tasklets
150
    self.tasklets = None
151

    
152
    # The new kind-of-type-system
153
    op_id = self.op.OP_ID
154
    for attr_name, aval, test in self._OP_PARAMS:
155
      if not hasattr(op, attr_name):
156
        if aval == ht.NoDefault:
157
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
158
                                     (op_id, attr_name), errors.ECODE_INVAL)
159
        else:
160
          if callable(aval):
161
            dval = aval()
162
          else:
163
            dval = aval
164
          setattr(self.op, attr_name, dval)
165
      attr_val = getattr(op, attr_name)
166
      if test == ht.NoType:
167
        # no tests here
168
        continue
169
      if not callable(test):
170
        raise errors.ProgrammerError("Validation for parameter '%s.%s' failed,"
171
                                     " given type is not a proper type (%s)" %
172
                                     (op_id, attr_name, test))
173
      if not test(attr_val):
174
        logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
175
                      self.op.OP_ID, attr_name, type(attr_val), attr_val)
176
        raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
177
                                   (op_id, attr_name), errors.ECODE_INVAL)
178

    
179
    self.CheckArguments()
180

    
181
  def __GetSSH(self):
182
    """Returns the SshRunner object
183

184
    """
185
    if not self.__ssh:
186
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
187
    return self.__ssh
188

    
189
  ssh = property(fget=__GetSSH)
190

    
191
  def CheckArguments(self):
192
    """Check syntactic validity for the opcode arguments.
193

194
    This method is for doing a simple syntactic check and ensure
195
    validity of opcode parameters, without any cluster-related
196
    checks. While the same can be accomplished in ExpandNames and/or
197
    CheckPrereq, doing these separate is better because:
198

199
      - ExpandNames is left as as purely a lock-related function
200
      - CheckPrereq is run after we have acquired locks (and possible
201
        waited for them)
202

203
    The function is allowed to change the self.op attribute so that
204
    later methods can no longer worry about missing parameters.
205

206
    """
207
    pass
208

    
209
  def ExpandNames(self):
210
    """Expand names for this LU.
211

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

217
    LUs which implement this method must also populate the self.needed_locks
218
    member, as a dict with lock levels as keys, and a list of needed lock names
219
    as values. Rules:
220

221
      - use an empty dict if you don't need any lock
222
      - if you don't need any lock at a particular level omit that level
223
      - don't put anything for the BGL level
224
      - if you want all locks at a level use locking.ALL_SET as a value
225

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

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

234
    Examples::
235

236
      # Acquire all nodes and one instance
237
      self.needed_locks = {
238
        locking.LEVEL_NODE: locking.ALL_SET,
239
        locking.LEVEL_INSTANCE: ['instance1.example.com'],
240
      }
241
      # Acquire just two nodes
242
      self.needed_locks = {
243
        locking.LEVEL_NODE: ['node1.example.com', 'node2.example.com'],
244
      }
245
      # Acquire no locks
246
      self.needed_locks = {} # No, you can't leave it to the default value None
247

248
    """
249
    # The implementation of this method is mandatory only if the new LU is
250
    # concurrent, so that old LUs don't need to be changed all at the same
251
    # time.
252
    if self.REQ_BGL:
253
      self.needed_locks = {} # Exclusive LUs don't need locks.
254
    else:
255
      raise NotImplementedError
256

    
257
  def DeclareLocks(self, level):
258
    """Declare LU locking needs for a level
259

260
    While most LUs can just declare their locking needs at ExpandNames time,
261
    sometimes there's the need to calculate some locks after having acquired
262
    the ones before. This function is called just before acquiring locks at a
263
    particular level, but after acquiring the ones at lower levels, and permits
264
    such calculations. It can be used to modify self.needed_locks, and by
265
    default it does nothing.
266

267
    This function is only called if you have something already set in
268
    self.needed_locks for the level.
269

270
    @param level: Locking level which is going to be locked
271
    @type level: member of ganeti.locking.LEVELS
272

273
    """
274

    
275
  def CheckPrereq(self):
276
    """Check prerequisites for this LU.
277

278
    This method should check that the prerequisites for the execution
279
    of this LU are fulfilled. It can do internode communication, but
280
    it should be idempotent - no cluster or system changes are
281
    allowed.
282

283
    The method should raise errors.OpPrereqError in case something is
284
    not fulfilled. Its return value is ignored.
285

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

289
    """
290
    if self.tasklets is not None:
291
      for (idx, tl) in enumerate(self.tasklets):
292
        logging.debug("Checking prerequisites for tasklet %s/%s",
293
                      idx + 1, len(self.tasklets))
294
        tl.CheckPrereq()
295
    else:
296
      pass
297

    
298
  def Exec(self, feedback_fn):
299
    """Execute the LU.
300

301
    This method should implement the actual work. It should raise
302
    errors.OpExecError for failures that are somewhat dealt with in
303
    code, or expected.
304

305
    """
306
    if self.tasklets is not None:
307
      for (idx, tl) in enumerate(self.tasklets):
308
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
309
        tl.Exec(feedback_fn)
310
    else:
311
      raise NotImplementedError
312

    
313
  def BuildHooksEnv(self):
314
    """Build hooks environment for this LU.
315

316
    This method should return a three-node tuple consisting of: a dict
317
    containing the environment that will be used for running the
318
    specific hook for this LU, a list of node names on which the hook
319
    should run before the execution, and a list of node names on which
320
    the hook should run after the execution.
321

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

327
    No nodes should be returned as an empty list (and not None).
328

329
    Note that if the HPATH for a LU class is None, this function will
330
    not be called.
331

332
    """
333
    raise NotImplementedError
334

    
335
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
336
    """Notify the LU about the results of its hooks.
337

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

344
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
345
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
346
    @param hook_results: the results of the multi-node hooks rpc call
347
    @param feedback_fn: function used send feedback back to the caller
348
    @param lu_result: the previous Exec result this LU had, or None
349
        in the PRE phase
350
    @return: the new Exec result, based on the previous result
351
        and hook results
352

353
    """
354
    # API must be kept, thus we ignore the unused argument and could
355
    # be a function warnings
356
    # pylint: disable-msg=W0613,R0201
357
    return lu_result
358

    
359
  def _ExpandAndLockInstance(self):
360
    """Helper function to expand and lock an instance.
361

362
    Many LUs that work on an instance take its name in self.op.instance_name
363
    and need to expand it and then declare the expanded name for locking. This
364
    function does it, and then updates self.op.instance_name to the expanded
365
    name. It also initializes needed_locks as a dict, if this hasn't been done
366
    before.
367

368
    """
369
    if self.needed_locks is None:
370
      self.needed_locks = {}
371
    else:
372
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
373
        "_ExpandAndLockInstance called with instance-level locks set"
374
    self.op.instance_name = _ExpandInstanceName(self.cfg,
375
                                                self.op.instance_name)
376
    self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
377

    
378
  def _LockInstancesNodes(self, primary_only=False):
379
    """Helper function to declare instances' nodes for locking.
380

381
    This function should be called after locking one or more instances to lock
382
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
383
    with all primary or secondary nodes for instances already locked and
384
    present in self.needed_locks[locking.LEVEL_INSTANCE].
385

386
    It should be called from DeclareLocks, and for safety only works if
387
    self.recalculate_locks[locking.LEVEL_NODE] is set.
388

389
    In the future it may grow parameters to just lock some instance's nodes, or
390
    to just lock primaries or secondary nodes, if needed.
391

392
    If should be called in DeclareLocks in a way similar to::
393

394
      if level == locking.LEVEL_NODE:
395
        self._LockInstancesNodes()
396

397
    @type primary_only: boolean
398
    @param primary_only: only lock primary nodes of locked instances
399

400
    """
401
    assert locking.LEVEL_NODE in self.recalculate_locks, \
402
      "_LockInstancesNodes helper function called with no nodes to recalculate"
403

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

    
406
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
407
    # future we might want to have different behaviors depending on the value
408
    # of self.recalculate_locks[locking.LEVEL_NODE]
409
    wanted_nodes = []
410
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
411
      instance = self.context.cfg.GetInstanceInfo(instance_name)
412
      wanted_nodes.append(instance.primary_node)
413
      if not primary_only:
414
        wanted_nodes.extend(instance.secondary_nodes)
415

    
416
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
417
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
418
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
419
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
420

    
421
    del self.recalculate_locks[locking.LEVEL_NODE]
422

    
423

    
424
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
425
  """Simple LU which runs no hooks.
426

427
  This LU is intended as a parent for other LogicalUnits which will
428
  run no hooks, in order to reduce duplicate code.
429

430
  """
431
  HPATH = None
432
  HTYPE = None
433

    
434
  def BuildHooksEnv(self):
435
    """Empty BuildHooksEnv for NoHooksLu.
436

437
    This just raises an error.
438

439
    """
440
    assert False, "BuildHooksEnv called for NoHooksLUs"
441

    
442

    
443
class Tasklet:
444
  """Tasklet base class.
445

446
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
447
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
448
  tasklets know nothing about locks.
449

450
  Subclasses must follow these rules:
451
    - Implement CheckPrereq
452
    - Implement Exec
453

454
  """
455
  def __init__(self, lu):
456
    self.lu = lu
457

    
458
    # Shortcuts
459
    self.cfg = lu.cfg
460
    self.rpc = lu.rpc
461

    
462
  def CheckPrereq(self):
463
    """Check prerequisites for this tasklets.
464

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

469
    The method should raise errors.OpPrereqError in case something is not
470
    fulfilled. Its return value is ignored.
471

472
    This method should also update all parameters to their canonical form if it
473
    hasn't been done before.
474

475
    """
476
    pass
477

    
478
  def Exec(self, feedback_fn):
479
    """Execute the tasklet.
480

481
    This method should implement the actual work. It should raise
482
    errors.OpExecError for failures that are somewhat dealt with in code, or
483
    expected.
484

485
    """
486
    raise NotImplementedError
487

    
488

    
489
def _GetWantedNodes(lu, nodes):
490
  """Returns list of checked and expanded node names.
491

492
  @type lu: L{LogicalUnit}
493
  @param lu: the logical unit on whose behalf we execute
494
  @type nodes: list
495
  @param nodes: list of node names or None for all nodes
496
  @rtype: list
497
  @return: the list of nodes, sorted
498
  @raise errors.ProgrammerError: if the nodes parameter is wrong type
499

500
  """
501
  if not nodes:
502
    raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
503
      " non-empty list of nodes whose name is to be expanded.")
504

    
505
  wanted = [_ExpandNodeName(lu.cfg, name) for name in nodes]
506
  return utils.NiceSort(wanted)
507

    
508

    
509
def _GetWantedInstances(lu, instances):
510
  """Returns list of checked and expanded instance names.
511

512
  @type lu: L{LogicalUnit}
513
  @param lu: the logical unit on whose behalf we execute
514
  @type instances: list
515
  @param instances: list of instance names or None for all instances
516
  @rtype: list
517
  @return: the list of instances, sorted
518
  @raise errors.OpPrereqError: if the instances parameter is wrong type
519
  @raise errors.OpPrereqError: if any of the passed instances is not found
520

521
  """
522
  if instances:
523
    wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
524
  else:
525
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
526
  return wanted
527

    
528

    
529
def _GetUpdatedParams(old_params, update_dict,
530
                      use_default=True, use_none=False):
531
  """Return the new version of a parameter dictionary.
532

533
  @type old_params: dict
534
  @param old_params: old parameters
535
  @type update_dict: dict
536
  @param update_dict: dict containing new parameter values, or
537
      constants.VALUE_DEFAULT to reset the parameter to its default
538
      value
539
  @param use_default: boolean
540
  @type use_default: whether to recognise L{constants.VALUE_DEFAULT}
541
      values as 'to be deleted' values
542
  @param use_none: boolean
543
  @type use_none: whether to recognise C{None} values as 'to be
544
      deleted' values
545
  @rtype: dict
546
  @return: the new parameter dictionary
547

548
  """
549
  params_copy = copy.deepcopy(old_params)
550
  for key, val in update_dict.iteritems():
551
    if ((use_default and val == constants.VALUE_DEFAULT) or
552
        (use_none and val is None)):
553
      try:
554
        del params_copy[key]
555
      except KeyError:
556
        pass
557
    else:
558
      params_copy[key] = val
559
  return params_copy
560

    
561

    
562
def _CheckOutputFields(static, dynamic, selected):
563
  """Checks whether all selected fields are valid.
564

565
  @type static: L{utils.FieldSet}
566
  @param static: static fields set
567
  @type dynamic: L{utils.FieldSet}
568
  @param dynamic: dynamic fields set
569

570
  """
571
  f = utils.FieldSet()
572
  f.Extend(static)
573
  f.Extend(dynamic)
574

    
575
  delta = f.NonMatching(selected)
576
  if delta:
577
    raise errors.OpPrereqError("Unknown output fields selected: %s"
578
                               % ",".join(delta), errors.ECODE_INVAL)
579

    
580

    
581
def _CheckGlobalHvParams(params):
582
  """Validates that given hypervisor params are not global ones.
583

584
  This will ensure that instances don't get customised versions of
585
  global params.
586

587
  """
588
  used_globals = constants.HVC_GLOBALS.intersection(params)
589
  if used_globals:
590
    msg = ("The following hypervisor parameters are global and cannot"
591
           " be customized at instance level, please modify them at"
592
           " cluster level: %s" % utils.CommaJoin(used_globals))
593
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
594

    
595

    
596
def _CheckNodeOnline(lu, node):
597
  """Ensure that a given node is online.
598

599
  @param lu: the LU on behalf of which we make the check
600
  @param node: the node to check
601
  @raise errors.OpPrereqError: if the node is offline
602

603
  """
604
  if lu.cfg.GetNodeInfo(node).offline:
605
    raise errors.OpPrereqError("Can't use offline node %s" % node,
606
                               errors.ECODE_INVAL)
607

    
608

    
609
def _CheckNodeNotDrained(lu, node):
610
  """Ensure that a given node is not drained.
611

612
  @param lu: the LU on behalf of which we make the check
613
  @param node: the node to check
614
  @raise errors.OpPrereqError: if the node is drained
615

616
  """
617
  if lu.cfg.GetNodeInfo(node).drained:
618
    raise errors.OpPrereqError("Can't use drained node %s" % node,
619
                               errors.ECODE_INVAL)
620

    
621

    
622
def _CheckNodeHasOS(lu, node, os_name, force_variant):
623
  """Ensure that a node supports a given OS.
624

625
  @param lu: the LU on behalf of which we make the check
626
  @param node: the node to check
627
  @param os_name: the OS to query about
628
  @param force_variant: whether to ignore variant errors
629
  @raise errors.OpPrereqError: if the node is not supporting the OS
630

631
  """
632
  result = lu.rpc.call_os_get(node, os_name)
633
  result.Raise("OS '%s' not in supported OS list for node %s" %
634
               (os_name, node),
635
               prereq=True, ecode=errors.ECODE_INVAL)
636
  if not force_variant:
637
    _CheckOSVariant(result.payload, os_name)
638

    
639

    
640
def _RequireFileStorage():
641
  """Checks that file storage is enabled.
642

643
  @raise errors.OpPrereqError: when file storage is disabled
644

645
  """
646
  if not constants.ENABLE_FILE_STORAGE:
647
    raise errors.OpPrereqError("File storage disabled at configure time",
648
                               errors.ECODE_INVAL)
649

    
650

    
651
def _CheckDiskTemplate(template):
652
  """Ensure a given disk template is valid.
653

654
  """
655
  if template not in constants.DISK_TEMPLATES:
656
    msg = ("Invalid disk template name '%s', valid templates are: %s" %
657
           (template, utils.CommaJoin(constants.DISK_TEMPLATES)))
658
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
659
  if template == constants.DT_FILE:
660
    _RequireFileStorage()
661
  return True
662

    
663

    
664
def _CheckStorageType(storage_type):
665
  """Ensure a given storage type is valid.
666

667
  """
668
  if storage_type not in constants.VALID_STORAGE_TYPES:
669
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
670
                               errors.ECODE_INVAL)
671
  if storage_type == constants.ST_FILE:
672
    _RequireFileStorage()
673
  return True
674

    
675

    
676
def _GetClusterDomainSecret():
677
  """Reads the cluster domain secret.
678

679
  """
680
  return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
681
                               strict=True)
682

    
683

    
684
def _CheckInstanceDown(lu, instance, reason):
685
  """Ensure that an instance is not running."""
686
  if instance.admin_up:
687
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
688
                               (instance.name, reason), errors.ECODE_STATE)
689

    
690
  pnode = instance.primary_node
691
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
692
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
693
              prereq=True, ecode=errors.ECODE_ENVIRON)
694

    
695
  if instance.name in ins_l.payload:
696
    raise errors.OpPrereqError("Instance %s is running, %s" %
697
                               (instance.name, reason), errors.ECODE_STATE)
698

    
699

    
700
def _ExpandItemName(fn, name, kind):
701
  """Expand an item name.
702

703
  @param fn: the function to use for expansion
704
  @param name: requested item name
705
  @param kind: text description ('Node' or 'Instance')
706
  @return: the resolved (full) name
707
  @raise errors.OpPrereqError: if the item is not found
708

709
  """
710
  full_name = fn(name)
711
  if full_name is None:
712
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
713
                               errors.ECODE_NOENT)
714
  return full_name
715

    
716

    
717
def _ExpandNodeName(cfg, name):
718
  """Wrapper over L{_ExpandItemName} for nodes."""
719
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
720

    
721

    
722
def _ExpandInstanceName(cfg, name):
723
  """Wrapper over L{_ExpandItemName} for instance."""
724
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
725

    
726

    
727
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
728
                          memory, vcpus, nics, disk_template, disks,
729
                          bep, hvp, hypervisor_name):
730
  """Builds instance related env variables for hooks
731

732
  This builds the hook environment from individual variables.
733

734
  @type name: string
735
  @param name: the name of the instance
736
  @type primary_node: string
737
  @param primary_node: the name of the instance's primary node
738
  @type secondary_nodes: list
739
  @param secondary_nodes: list of secondary nodes as strings
740
  @type os_type: string
741
  @param os_type: the name of the instance's OS
742
  @type status: boolean
743
  @param status: the should_run status of the instance
744
  @type memory: string
745
  @param memory: the memory size of the instance
746
  @type vcpus: string
747
  @param vcpus: the count of VCPUs the instance has
748
  @type nics: list
749
  @param nics: list of tuples (ip, mac, mode, link) representing
750
      the NICs the instance has
751
  @type disk_template: string
752
  @param disk_template: the disk template of the instance
753
  @type disks: list
754
  @param disks: the list of (size, mode) pairs
755
  @type bep: dict
756
  @param bep: the backend parameters for the instance
757
  @type hvp: dict
758
  @param hvp: the hypervisor parameters for the instance
759
  @type hypervisor_name: string
760
  @param hypervisor_name: the hypervisor for the instance
761
  @rtype: dict
762
  @return: the hook environment for this instance
763

764
  """
765
  if status:
766
    str_status = "up"
767
  else:
768
    str_status = "down"
769
  env = {
770
    "OP_TARGET": name,
771
    "INSTANCE_NAME": name,
772
    "INSTANCE_PRIMARY": primary_node,
773
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
774
    "INSTANCE_OS_TYPE": os_type,
775
    "INSTANCE_STATUS": str_status,
776
    "INSTANCE_MEMORY": memory,
777
    "INSTANCE_VCPUS": vcpus,
778
    "INSTANCE_DISK_TEMPLATE": disk_template,
779
    "INSTANCE_HYPERVISOR": hypervisor_name,
780
  }
781

    
782
  if nics:
783
    nic_count = len(nics)
784
    for idx, (ip, mac, mode, link) in enumerate(nics):
785
      if ip is None:
786
        ip = ""
787
      env["INSTANCE_NIC%d_IP" % idx] = ip
788
      env["INSTANCE_NIC%d_MAC" % idx] = mac
789
      env["INSTANCE_NIC%d_MODE" % idx] = mode
790
      env["INSTANCE_NIC%d_LINK" % idx] = link
791
      if mode == constants.NIC_MODE_BRIDGED:
792
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
793
  else:
794
    nic_count = 0
795

    
796
  env["INSTANCE_NIC_COUNT"] = nic_count
797

    
798
  if disks:
799
    disk_count = len(disks)
800
    for idx, (size, mode) in enumerate(disks):
801
      env["INSTANCE_DISK%d_SIZE" % idx] = size
802
      env["INSTANCE_DISK%d_MODE" % idx] = mode
803
  else:
804
    disk_count = 0
805

    
806
  env["INSTANCE_DISK_COUNT"] = disk_count
807

    
808
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
809
    for key, value in source.items():
810
      env["INSTANCE_%s_%s" % (kind, key)] = value
811

    
812
  return env
813

    
814

    
815
def _NICListToTuple(lu, nics):
816
  """Build a list of nic information tuples.
817

818
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
819
  value in LUQueryInstanceData.
820

821
  @type lu:  L{LogicalUnit}
822
  @param lu: the logical unit on whose behalf we execute
823
  @type nics: list of L{objects.NIC}
824
  @param nics: list of nics to convert to hooks tuples
825

826
  """
827
  hooks_nics = []
828
  cluster = lu.cfg.GetClusterInfo()
829
  for nic in nics:
830
    ip = nic.ip
831
    mac = nic.mac
832
    filled_params = cluster.SimpleFillNIC(nic.nicparams)
833
    mode = filled_params[constants.NIC_MODE]
834
    link = filled_params[constants.NIC_LINK]
835
    hooks_nics.append((ip, mac, mode, link))
836
  return hooks_nics
837

    
838

    
839
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
840
  """Builds instance related env variables for hooks from an object.
841

842
  @type lu: L{LogicalUnit}
843
  @param lu: the logical unit on whose behalf we execute
844
  @type instance: L{objects.Instance}
845
  @param instance: the instance for which we should build the
846
      environment
847
  @type override: dict
848
  @param override: dictionary with key/values that will override
849
      our values
850
  @rtype: dict
851
  @return: the hook environment dictionary
852

853
  """
854
  cluster = lu.cfg.GetClusterInfo()
855
  bep = cluster.FillBE(instance)
856
  hvp = cluster.FillHV(instance)
857
  args = {
858
    'name': instance.name,
859
    'primary_node': instance.primary_node,
860
    'secondary_nodes': instance.secondary_nodes,
861
    'os_type': instance.os,
862
    'status': instance.admin_up,
863
    'memory': bep[constants.BE_MEMORY],
864
    'vcpus': bep[constants.BE_VCPUS],
865
    'nics': _NICListToTuple(lu, instance.nics),
866
    'disk_template': instance.disk_template,
867
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
868
    'bep': bep,
869
    'hvp': hvp,
870
    'hypervisor_name': instance.hypervisor,
871
  }
872
  if override:
873
    args.update(override)
874
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
875

    
876

    
877
def _AdjustCandidatePool(lu, exceptions):
878
  """Adjust the candidate pool after node operations.
879

880
  """
881
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
882
  if mod_list:
883
    lu.LogInfo("Promoted nodes to master candidate role: %s",
884
               utils.CommaJoin(node.name for node in mod_list))
885
    for name in mod_list:
886
      lu.context.ReaddNode(name)
887
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
888
  if mc_now > mc_max:
889
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
890
               (mc_now, mc_max))
891

    
892

    
893
def _DecideSelfPromotion(lu, exceptions=None):
894
  """Decide whether I should promote myself as a master candidate.
895

896
  """
897
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
898
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
899
  # the new node will increase mc_max with one, so:
900
  mc_should = min(mc_should + 1, cp_size)
901
  return mc_now < mc_should
902

    
903

    
904
def _CheckNicsBridgesExist(lu, target_nics, target_node):
905
  """Check that the brigdes needed by a list of nics exist.
906

907
  """
908
  cluster = lu.cfg.GetClusterInfo()
909
  paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics]
910
  brlist = [params[constants.NIC_LINK] for params in paramslist
911
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
912
  if brlist:
913
    result = lu.rpc.call_bridges_exist(target_node, brlist)
914
    result.Raise("Error checking bridges on destination node '%s'" %
915
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
916

    
917

    
918
def _CheckInstanceBridgesExist(lu, instance, node=None):
919
  """Check that the brigdes needed by an instance exist.
920

921
  """
922
  if node is None:
923
    node = instance.primary_node
924
  _CheckNicsBridgesExist(lu, instance.nics, node)
925

    
926

    
927
def _CheckOSVariant(os_obj, name):
928
  """Check whether an OS name conforms to the os variants specification.
929

930
  @type os_obj: L{objects.OS}
931
  @param os_obj: OS object to check
932
  @type name: string
933
  @param name: OS name passed by the user, to check for validity
934

935
  """
936
  if not os_obj.supported_variants:
937
    return
938
  variant = objects.OS.GetVariant(name)
939
  if not variant:
940
    raise errors.OpPrereqError("OS name must include a variant",
941
                               errors.ECODE_INVAL)
942

    
943
  if variant not in os_obj.supported_variants:
944
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
945

    
946

    
947
def _GetNodeInstancesInner(cfg, fn):
948
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
949

    
950

    
951
def _GetNodeInstances(cfg, node_name):
952
  """Returns a list of all primary and secondary instances on a node.
953

954
  """
955

    
956
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
957

    
958

    
959
def _GetNodePrimaryInstances(cfg, node_name):
960
  """Returns primary instances on a node.
961

962
  """
963
  return _GetNodeInstancesInner(cfg,
964
                                lambda inst: node_name == inst.primary_node)
965

    
966

    
967
def _GetNodeSecondaryInstances(cfg, node_name):
968
  """Returns secondary instances on a node.
969

970
  """
971
  return _GetNodeInstancesInner(cfg,
972
                                lambda inst: node_name in inst.secondary_nodes)
973

    
974

    
975
def _GetStorageTypeArgs(cfg, storage_type):
976
  """Returns the arguments for a storage type.
977

978
  """
979
  # Special case for file storage
980
  if storage_type == constants.ST_FILE:
981
    # storage.FileStorage wants a list of storage directories
982
    return [[cfg.GetFileStorageDir()]]
983

    
984
  return []
985

    
986

    
987
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
988
  faulty = []
989

    
990
  for dev in instance.disks:
991
    cfg.SetDiskID(dev, node_name)
992

    
993
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
994
  result.Raise("Failed to get disk status from node %s" % node_name,
995
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
996

    
997
  for idx, bdev_status in enumerate(result.payload):
998
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
999
      faulty.append(idx)
1000

    
1001
  return faulty
1002

    
1003

    
1004
def _CheckIAllocatorOrNode(lu, iallocator_slot, node_slot):
1005
  """Check the sanity of iallocator and node arguments and use the
1006
  cluster-wide iallocator if appropriate.
1007

1008
  Check that at most one of (iallocator, node) is specified. If none is
1009
  specified, then the LU's opcode's iallocator slot is filled with the
1010
  cluster-wide default iallocator.
1011

1012
  @type iallocator_slot: string
1013
  @param iallocator_slot: the name of the opcode iallocator slot
1014
  @type node_slot: string
1015
  @param node_slot: the name of the opcode target node slot
1016

1017
  """
1018
  node = getattr(lu.op, node_slot, None)
1019
  iallocator = getattr(lu.op, iallocator_slot, None)
1020

    
1021
  if node is not None and iallocator is not None:
1022
    raise errors.OpPrereqError("Do not specify both, iallocator and node.",
1023
                               errors.ECODE_INVAL)
1024
  elif node is None and iallocator is None:
1025
    default_iallocator = lu.cfg.GetDefaultIAllocator()
1026
    if default_iallocator:
1027
      setattr(lu.op, iallocator_slot, default_iallocator)
1028
    else:
1029
      raise errors.OpPrereqError("No iallocator or node given and no"
1030
                                 " cluster-wide default iallocator found."
1031
                                 " Please specify either an iallocator or a"
1032
                                 " node, or set a cluster-wide default"
1033
                                 " iallocator.")
1034

    
1035

    
1036
class LUPostInitCluster(LogicalUnit):
1037
  """Logical unit for running hooks after cluster initialization.
1038

1039
  """
1040
  HPATH = "cluster-init"
1041
  HTYPE = constants.HTYPE_CLUSTER
1042

    
1043
  def BuildHooksEnv(self):
1044
    """Build hooks env.
1045

1046
    """
1047
    env = {"OP_TARGET": self.cfg.GetClusterName()}
1048
    mn = self.cfg.GetMasterNode()
1049
    return env, [], [mn]
1050

    
1051
  def Exec(self, feedback_fn):
1052
    """Nothing to do.
1053

1054
    """
1055
    return True
1056

    
1057

    
1058
class LUDestroyCluster(LogicalUnit):
1059
  """Logical unit for destroying the cluster.
1060

1061
  """
1062
  HPATH = "cluster-destroy"
1063
  HTYPE = constants.HTYPE_CLUSTER
1064

    
1065
  def BuildHooksEnv(self):
1066
    """Build hooks env.
1067

1068
    """
1069
    env = {"OP_TARGET": self.cfg.GetClusterName()}
1070
    return env, [], []
1071

    
1072
  def CheckPrereq(self):
1073
    """Check prerequisites.
1074

1075
    This checks whether the cluster is empty.
1076

1077
    Any errors are signaled by raising errors.OpPrereqError.
1078

1079
    """
1080
    master = self.cfg.GetMasterNode()
1081

    
1082
    nodelist = self.cfg.GetNodeList()
1083
    if len(nodelist) != 1 or nodelist[0] != master:
1084
      raise errors.OpPrereqError("There are still %d node(s) in"
1085
                                 " this cluster." % (len(nodelist) - 1),
1086
                                 errors.ECODE_INVAL)
1087
    instancelist = self.cfg.GetInstanceList()
1088
    if instancelist:
1089
      raise errors.OpPrereqError("There are still %d instance(s) in"
1090
                                 " this cluster." % len(instancelist),
1091
                                 errors.ECODE_INVAL)
1092

    
1093
  def Exec(self, feedback_fn):
1094
    """Destroys the cluster.
1095

1096
    """
1097
    master = self.cfg.GetMasterNode()
1098

    
1099
    # Run post hooks on master node before it's removed
1100
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
1101
    try:
1102
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
1103
    except:
1104
      # pylint: disable-msg=W0702
1105
      self.LogWarning("Errors occurred running hooks on %s" % master)
1106

    
1107
    result = self.rpc.call_node_stop_master(master, False)
1108
    result.Raise("Could not disable the master role")
1109

    
1110
    return master
1111

    
1112

    
1113
def _VerifyCertificate(filename):
1114
  """Verifies a certificate for LUVerifyCluster.
1115

1116
  @type filename: string
1117
  @param filename: Path to PEM file
1118

1119
  """
1120
  try:
1121
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1122
                                           utils.ReadFile(filename))
1123
  except Exception, err: # pylint: disable-msg=W0703
1124
    return (LUVerifyCluster.ETYPE_ERROR,
1125
            "Failed to load X509 certificate %s: %s" % (filename, err))
1126

    
1127
  (errcode, msg) = \
1128
    utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1129
                                constants.SSL_CERT_EXPIRATION_ERROR)
1130

    
1131
  if msg:
1132
    fnamemsg = "While verifying %s: %s" % (filename, msg)
1133
  else:
1134
    fnamemsg = None
1135

    
1136
  if errcode is None:
1137
    return (None, fnamemsg)
1138
  elif errcode == utils.CERT_WARNING:
1139
    return (LUVerifyCluster.ETYPE_WARNING, fnamemsg)
1140
  elif errcode == utils.CERT_ERROR:
1141
    return (LUVerifyCluster.ETYPE_ERROR, fnamemsg)
1142

    
1143
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1144

    
1145

    
1146
class LUVerifyCluster(LogicalUnit):
1147
  """Verifies the cluster status.
1148

1149
  """
1150
  HPATH = "cluster-verify"
1151
  HTYPE = constants.HTYPE_CLUSTER
1152
  _OP_PARAMS = [
1153
    ("skip_checks", ht.EmptyList,
1154
     ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS))),
1155
    ("verbose", False, ht.TBool),
1156
    ("error_codes", False, ht.TBool),
1157
    ("debug_simulate_errors", False, ht.TBool),
1158
    ]
1159
  REQ_BGL = False
1160

    
1161
  TCLUSTER = "cluster"
1162
  TNODE = "node"
1163
  TINSTANCE = "instance"
1164

    
1165
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1166
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1167
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1168
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1169
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1170
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1171
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1172
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1173
  ENODEDRBD = (TNODE, "ENODEDRBD")
1174
  ENODEDRBDHELPER = (TNODE, "ENODEDRBDHELPER")
1175
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1176
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1177
  ENODEHV = (TNODE, "ENODEHV")
1178
  ENODELVM = (TNODE, "ENODELVM")
1179
  ENODEN1 = (TNODE, "ENODEN1")
1180
  ENODENET = (TNODE, "ENODENET")
1181
  ENODEOS = (TNODE, "ENODEOS")
1182
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1183
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1184
  ENODERPC = (TNODE, "ENODERPC")
1185
  ENODESSH = (TNODE, "ENODESSH")
1186
  ENODEVERSION = (TNODE, "ENODEVERSION")
1187
  ENODESETUP = (TNODE, "ENODESETUP")
1188
  ENODETIME = (TNODE, "ENODETIME")
1189

    
1190
  ETYPE_FIELD = "code"
1191
  ETYPE_ERROR = "ERROR"
1192
  ETYPE_WARNING = "WARNING"
1193

    
1194
  class NodeImage(object):
1195
    """A class representing the logical and physical status of a node.
1196

1197
    @type name: string
1198
    @ivar name: the node name to which this object refers
1199
    @ivar volumes: a structure as returned from
1200
        L{ganeti.backend.GetVolumeList} (runtime)
1201
    @ivar instances: a list of running instances (runtime)
1202
    @ivar pinst: list of configured primary instances (config)
1203
    @ivar sinst: list of configured secondary instances (config)
1204
    @ivar sbp: diction of {secondary-node: list of instances} of all peers
1205
        of this node (config)
1206
    @ivar mfree: free memory, as reported by hypervisor (runtime)
1207
    @ivar dfree: free disk, as reported by the node (runtime)
1208
    @ivar offline: the offline status (config)
1209
    @type rpc_fail: boolean
1210
    @ivar rpc_fail: whether the RPC verify call was successfull (overall,
1211
        not whether the individual keys were correct) (runtime)
1212
    @type lvm_fail: boolean
1213
    @ivar lvm_fail: whether the RPC call didn't return valid LVM data
1214
    @type hyp_fail: boolean
1215
    @ivar hyp_fail: whether the RPC call didn't return the instance list
1216
    @type ghost: boolean
1217
    @ivar ghost: whether this is a known node or not (config)
1218
    @type os_fail: boolean
1219
    @ivar os_fail: whether the RPC call didn't return valid OS data
1220
    @type oslist: list
1221
    @ivar oslist: list of OSes as diagnosed by DiagnoseOS
1222

1223
    """
1224
    def __init__(self, offline=False, name=None):
1225
      self.name = name
1226
      self.volumes = {}
1227
      self.instances = []
1228
      self.pinst = []
1229
      self.sinst = []
1230
      self.sbp = {}
1231
      self.mfree = 0
1232
      self.dfree = 0
1233
      self.offline = offline
1234
      self.rpc_fail = False
1235
      self.lvm_fail = False
1236
      self.hyp_fail = False
1237
      self.ghost = False
1238
      self.os_fail = False
1239
      self.oslist = {}
1240

    
1241
  def ExpandNames(self):
1242
    self.needed_locks = {
1243
      locking.LEVEL_NODE: locking.ALL_SET,
1244
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1245
    }
1246
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1247

    
1248
  def _Error(self, ecode, item, msg, *args, **kwargs):
1249
    """Format an error message.
1250

1251
    Based on the opcode's error_codes parameter, either format a
1252
    parseable error code, or a simpler error string.
1253

1254
    This must be called only from Exec and functions called from Exec.
1255

1256
    """
1257
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1258
    itype, etxt = ecode
1259
    # first complete the msg
1260
    if args:
1261
      msg = msg % args
1262
    # then format the whole message
1263
    if self.op.error_codes:
1264
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1265
    else:
1266
      if item:
1267
        item = " " + item
1268
      else:
1269
        item = ""
1270
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1271
    # and finally report it via the feedback_fn
1272
    self._feedback_fn("  - %s" % msg)
1273

    
1274
  def _ErrorIf(self, cond, *args, **kwargs):
1275
    """Log an error message if the passed condition is True.
1276

1277
    """
1278
    cond = bool(cond) or self.op.debug_simulate_errors
1279
    if cond:
1280
      self._Error(*args, **kwargs)
1281
    # do not mark the operation as failed for WARN cases only
1282
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1283
      self.bad = self.bad or cond
1284

    
1285
  def _VerifyNode(self, ninfo, nresult):
1286
    """Perform some basic validation on data returned from a node.
1287

1288
      - check the result data structure is well formed and has all the
1289
        mandatory fields
1290
      - check ganeti version
1291

1292
    @type ninfo: L{objects.Node}
1293
    @param ninfo: the node to check
1294
    @param nresult: the results from the node
1295
    @rtype: boolean
1296
    @return: whether overall this call was successful (and we can expect
1297
         reasonable values in the respose)
1298

1299
    """
1300
    node = ninfo.name
1301
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1302

    
1303
    # main result, nresult should be a non-empty dict
1304
    test = not nresult or not isinstance(nresult, dict)
1305
    _ErrorIf(test, self.ENODERPC, node,
1306
                  "unable to verify node: no data returned")
1307
    if test:
1308
      return False
1309

    
1310
    # compares ganeti version
1311
    local_version = constants.PROTOCOL_VERSION
1312
    remote_version = nresult.get("version", None)
1313
    test = not (remote_version and
1314
                isinstance(remote_version, (list, tuple)) and
1315
                len(remote_version) == 2)
1316
    _ErrorIf(test, self.ENODERPC, node,
1317
             "connection to node returned invalid data")
1318
    if test:
1319
      return False
1320

    
1321
    test = local_version != remote_version[0]
1322
    _ErrorIf(test, self.ENODEVERSION, node,
1323
             "incompatible protocol versions: master %s,"
1324
             " node %s", local_version, remote_version[0])
1325
    if test:
1326
      return False
1327

    
1328
    # node seems compatible, we can actually try to look into its results
1329

    
1330
    # full package version
1331
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1332
                  self.ENODEVERSION, node,
1333
                  "software version mismatch: master %s, node %s",
1334
                  constants.RELEASE_VERSION, remote_version[1],
1335
                  code=self.ETYPE_WARNING)
1336

    
1337
    hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1338
    if isinstance(hyp_result, dict):
1339
      for hv_name, hv_result in hyp_result.iteritems():
1340
        test = hv_result is not None
1341
        _ErrorIf(test, self.ENODEHV, node,
1342
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1343

    
1344

    
1345
    test = nresult.get(constants.NV_NODESETUP,
1346
                           ["Missing NODESETUP results"])
1347
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1348
             "; ".join(test))
1349

    
1350
    return True
1351

    
1352
  def _VerifyNodeTime(self, ninfo, nresult,
1353
                      nvinfo_starttime, nvinfo_endtime):
1354
    """Check the node time.
1355

1356
    @type ninfo: L{objects.Node}
1357
    @param ninfo: the node to check
1358
    @param nresult: the remote results for the node
1359
    @param nvinfo_starttime: the start time of the RPC call
1360
    @param nvinfo_endtime: the end time of the RPC call
1361

1362
    """
1363
    node = ninfo.name
1364
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1365

    
1366
    ntime = nresult.get(constants.NV_TIME, None)
1367
    try:
1368
      ntime_merged = utils.MergeTime(ntime)
1369
    except (ValueError, TypeError):
1370
      _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1371
      return
1372

    
1373
    if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1374
      ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1375
    elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1376
      ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1377
    else:
1378
      ntime_diff = None
1379

    
1380
    _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1381
             "Node time diverges by at least %s from master node time",
1382
             ntime_diff)
1383

    
1384
  def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1385
    """Check the node time.
1386

1387
    @type ninfo: L{objects.Node}
1388
    @param ninfo: the node to check
1389
    @param nresult: the remote results for the node
1390
    @param vg_name: the configured VG name
1391

1392
    """
1393
    if vg_name is None:
1394
      return
1395

    
1396
    node = ninfo.name
1397
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1398

    
1399
    # checks vg existence and size > 20G
1400
    vglist = nresult.get(constants.NV_VGLIST, None)
1401
    test = not vglist
1402
    _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1403
    if not test:
1404
      vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1405
                                            constants.MIN_VG_SIZE)
1406
      _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1407

    
1408
    # check pv names
1409
    pvlist = nresult.get(constants.NV_PVLIST, None)
1410
    test = pvlist is None
1411
    _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1412
    if not test:
1413
      # check that ':' is not present in PV names, since it's a
1414
      # special character for lvcreate (denotes the range of PEs to
1415
      # use on the PV)
1416
      for _, pvname, owner_vg in pvlist:
1417
        test = ":" in pvname
1418
        _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1419
                 " '%s' of VG '%s'", pvname, owner_vg)
1420

    
1421
  def _VerifyNodeNetwork(self, ninfo, nresult):
1422
    """Check the node time.
1423

1424
    @type ninfo: L{objects.Node}
1425
    @param ninfo: the node to check
1426
    @param nresult: the remote results for the node
1427

1428
    """
1429
    node = ninfo.name
1430
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1431

    
1432
    test = constants.NV_NODELIST not in nresult
1433
    _ErrorIf(test, self.ENODESSH, node,
1434
             "node hasn't returned node ssh connectivity data")
1435
    if not test:
1436
      if nresult[constants.NV_NODELIST]:
1437
        for a_node, a_msg in nresult[constants.NV_NODELIST].items():
1438
          _ErrorIf(True, self.ENODESSH, node,
1439
                   "ssh communication with node '%s': %s", a_node, a_msg)
1440

    
1441
    test = constants.NV_NODENETTEST not in nresult
1442
    _ErrorIf(test, self.ENODENET, node,
1443
             "node hasn't returned node tcp connectivity data")
1444
    if not test:
1445
      if nresult[constants.NV_NODENETTEST]:
1446
        nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys())
1447
        for anode in nlist:
1448
          _ErrorIf(True, self.ENODENET, node,
1449
                   "tcp communication with node '%s': %s",
1450
                   anode, nresult[constants.NV_NODENETTEST][anode])
1451

    
1452
    test = constants.NV_MASTERIP not in nresult
1453
    _ErrorIf(test, self.ENODENET, node,
1454
             "node hasn't returned node master IP reachability data")
1455
    if not test:
1456
      if not nresult[constants.NV_MASTERIP]:
1457
        if node == self.master_node:
1458
          msg = "the master node cannot reach the master IP (not configured?)"
1459
        else:
1460
          msg = "cannot reach the master IP"
1461
        _ErrorIf(True, self.ENODENET, node, msg)
1462

    
1463

    
1464
  def _VerifyInstance(self, instance, instanceconfig, node_image):
1465
    """Verify an instance.
1466

1467
    This function checks to see if the required block devices are
1468
    available on the instance's node.
1469

1470
    """
1471
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1472
    node_current = instanceconfig.primary_node
1473

    
1474
    node_vol_should = {}
1475
    instanceconfig.MapLVsByNode(node_vol_should)
1476

    
1477
    for node in node_vol_should:
1478
      n_img = node_image[node]
1479
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1480
        # ignore missing volumes on offline or broken nodes
1481
        continue
1482
      for volume in node_vol_should[node]:
1483
        test = volume not in n_img.volumes
1484
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1485
                 "volume %s missing on node %s", volume, node)
1486

    
1487
    if instanceconfig.admin_up:
1488
      pri_img = node_image[node_current]
1489
      test = instance not in pri_img.instances and not pri_img.offline
1490
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1491
               "instance not running on its primary node %s",
1492
               node_current)
1493

    
1494
    for node, n_img in node_image.items():
1495
      if (not node == node_current):
1496
        test = instance in n_img.instances
1497
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1498
                 "instance should not run on node %s", node)
1499

    
1500
  def _VerifyOrphanVolumes(self, node_vol_should, node_image, reserved):
1501
    """Verify if there are any unknown volumes in the cluster.
1502

1503
    The .os, .swap and backup volumes are ignored. All other volumes are
1504
    reported as unknown.
1505

1506
    @type reserved: L{ganeti.utils.FieldSet}
1507
    @param reserved: a FieldSet of reserved volume names
1508

1509
    """
1510
    for node, n_img in node_image.items():
1511
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1512
        # skip non-healthy nodes
1513
        continue
1514
      for volume in n_img.volumes:
1515
        test = ((node not in node_vol_should or
1516
                volume not in node_vol_should[node]) and
1517
                not reserved.Matches(volume))
1518
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1519
                      "volume %s is unknown", volume)
1520

    
1521
  def _VerifyOrphanInstances(self, instancelist, node_image):
1522
    """Verify the list of running instances.
1523

1524
    This checks what instances are running but unknown to the cluster.
1525

1526
    """
1527
    for node, n_img in node_image.items():
1528
      for o_inst in n_img.instances:
1529
        test = o_inst not in instancelist
1530
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1531
                      "instance %s on node %s should not exist", o_inst, node)
1532

    
1533
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1534
    """Verify N+1 Memory Resilience.
1535

1536
    Check that if one single node dies we can still start all the
1537
    instances it was primary for.
1538

1539
    """
1540
    for node, n_img in node_image.items():
1541
      # This code checks that every node which is now listed as
1542
      # secondary has enough memory to host all instances it is
1543
      # supposed to should a single other node in the cluster fail.
1544
      # FIXME: not ready for failover to an arbitrary node
1545
      # FIXME: does not support file-backed instances
1546
      # WARNING: we currently take into account down instances as well
1547
      # as up ones, considering that even if they're down someone
1548
      # might want to start them even in the event of a node failure.
1549
      for prinode, instances in n_img.sbp.items():
1550
        needed_mem = 0
1551
        for instance in instances:
1552
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1553
          if bep[constants.BE_AUTO_BALANCE]:
1554
            needed_mem += bep[constants.BE_MEMORY]
1555
        test = n_img.mfree < needed_mem
1556
        self._ErrorIf(test, self.ENODEN1, node,
1557
                      "not enough memory on to accommodate"
1558
                      " failovers should peer node %s fail", prinode)
1559

    
1560
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1561
                       master_files):
1562
    """Verifies and computes the node required file checksums.
1563

1564
    @type ninfo: L{objects.Node}
1565
    @param ninfo: the node to check
1566
    @param nresult: the remote results for the node
1567
    @param file_list: required list of files
1568
    @param local_cksum: dictionary of local files and their checksums
1569
    @param master_files: list of files that only masters should have
1570

1571
    """
1572
    node = ninfo.name
1573
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1574

    
1575
    remote_cksum = nresult.get(constants.NV_FILELIST, None)
1576
    test = not isinstance(remote_cksum, dict)
1577
    _ErrorIf(test, self.ENODEFILECHECK, node,
1578
             "node hasn't returned file checksum data")
1579
    if test:
1580
      return
1581

    
1582
    for file_name in file_list:
1583
      node_is_mc = ninfo.master_candidate
1584
      must_have = (file_name not in master_files) or node_is_mc
1585
      # missing
1586
      test1 = file_name not in remote_cksum
1587
      # invalid checksum
1588
      test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1589
      # existing and good
1590
      test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1591
      _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1592
               "file '%s' missing", file_name)
1593
      _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1594
               "file '%s' has wrong checksum", file_name)
1595
      # not candidate and this is not a must-have file
1596
      _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1597
               "file '%s' should not exist on non master"
1598
               " candidates (and the file is outdated)", file_name)
1599
      # all good, except non-master/non-must have combination
1600
      _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1601
               "file '%s' should not exist"
1602
               " on non master candidates", file_name)
1603

    
1604
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_helper,
1605
                      drbd_map):
1606
    """Verifies and the node DRBD status.
1607

1608
    @type ninfo: L{objects.Node}
1609
    @param ninfo: the node to check
1610
    @param nresult: the remote results for the node
1611
    @param instanceinfo: the dict of instances
1612
    @param drbd_helper: the configured DRBD usermode helper
1613
    @param drbd_map: the DRBD map as returned by
1614
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1615

1616
    """
1617
    node = ninfo.name
1618
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1619

    
1620
    if drbd_helper:
1621
      helper_result = nresult.get(constants.NV_DRBDHELPER, None)
1622
      test = (helper_result == None)
1623
      _ErrorIf(test, self.ENODEDRBDHELPER, node,
1624
               "no drbd usermode helper returned")
1625
      if helper_result:
1626
        status, payload = helper_result
1627
        test = not status
1628
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1629
                 "drbd usermode helper check unsuccessful: %s", payload)
1630
        test = status and (payload != drbd_helper)
1631
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1632
                 "wrong drbd usermode helper: %s", payload)
1633

    
1634
    # compute the DRBD minors
1635
    node_drbd = {}
1636
    for minor, instance in drbd_map[node].items():
1637
      test = instance not in instanceinfo
1638
      _ErrorIf(test, self.ECLUSTERCFG, None,
1639
               "ghost instance '%s' in temporary DRBD map", instance)
1640
        # ghost instance should not be running, but otherwise we
1641
        # don't give double warnings (both ghost instance and
1642
        # unallocated minor in use)
1643
      if test:
1644
        node_drbd[minor] = (instance, False)
1645
      else:
1646
        instance = instanceinfo[instance]
1647
        node_drbd[minor] = (instance.name, instance.admin_up)
1648

    
1649
    # and now check them
1650
    used_minors = nresult.get(constants.NV_DRBDLIST, [])
1651
    test = not isinstance(used_minors, (tuple, list))
1652
    _ErrorIf(test, self.ENODEDRBD, node,
1653
             "cannot parse drbd status file: %s", str(used_minors))
1654
    if test:
1655
      # we cannot check drbd status
1656
      return
1657

    
1658
    for minor, (iname, must_exist) in node_drbd.items():
1659
      test = minor not in used_minors and must_exist
1660
      _ErrorIf(test, self.ENODEDRBD, node,
1661
               "drbd minor %d of instance %s is not active", minor, iname)
1662
    for minor in used_minors:
1663
      test = minor not in node_drbd
1664
      _ErrorIf(test, self.ENODEDRBD, node,
1665
               "unallocated drbd minor %d is in use", minor)
1666

    
1667
  def _UpdateNodeOS(self, ninfo, nresult, nimg):
1668
    """Builds the node OS structures.
1669

1670
    @type ninfo: L{objects.Node}
1671
    @param ninfo: the node to check
1672
    @param nresult: the remote results for the node
1673
    @param nimg: the node image object
1674

1675
    """
1676
    node = ninfo.name
1677
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1678

    
1679
    remote_os = nresult.get(constants.NV_OSLIST, None)
1680
    test = (not isinstance(remote_os, list) or
1681
            not compat.all(isinstance(v, list) and len(v) == 7
1682
                           for v in remote_os))
1683

    
1684
    _ErrorIf(test, self.ENODEOS, node,
1685
             "node hasn't returned valid OS data")
1686

    
1687
    nimg.os_fail = test
1688

    
1689
    if test:
1690
      return
1691

    
1692
    os_dict = {}
1693

    
1694
    for (name, os_path, status, diagnose,
1695
         variants, parameters, api_ver) in nresult[constants.NV_OSLIST]:
1696

    
1697
      if name not in os_dict:
1698
        os_dict[name] = []
1699

    
1700
      # parameters is a list of lists instead of list of tuples due to
1701
      # JSON lacking a real tuple type, fix it:
1702
      parameters = [tuple(v) for v in parameters]
1703
      os_dict[name].append((os_path, status, diagnose,
1704
                            set(variants), set(parameters), set(api_ver)))
1705

    
1706
    nimg.oslist = os_dict
1707

    
1708
  def _VerifyNodeOS(self, ninfo, nimg, base):
1709
    """Verifies the node OS list.
1710

1711
    @type ninfo: L{objects.Node}
1712
    @param ninfo: the node to check
1713
    @param nimg: the node image object
1714
    @param base: the 'template' node we match against (e.g. from the master)
1715

1716
    """
1717
    node = ninfo.name
1718
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1719

    
1720
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1721

    
1722
    for os_name, os_data in nimg.oslist.items():
1723
      assert os_data, "Empty OS status for OS %s?!" % os_name
1724
      f_path, f_status, f_diag, f_var, f_param, f_api = os_data[0]
1725
      _ErrorIf(not f_status, self.ENODEOS, node,
1726
               "Invalid OS %s (located at %s): %s", os_name, f_path, f_diag)
1727
      _ErrorIf(len(os_data) > 1, self.ENODEOS, node,
1728
               "OS '%s' has multiple entries (first one shadows the rest): %s",
1729
               os_name, utils.CommaJoin([v[0] for v in os_data]))
1730
      # this will catched in backend too
1731
      _ErrorIf(compat.any(v >= constants.OS_API_V15 for v in f_api)
1732
               and not f_var, self.ENODEOS, node,
1733
               "OS %s with API at least %d does not declare any variant",
1734
               os_name, constants.OS_API_V15)
1735
      # comparisons with the 'base' image
1736
      test = os_name not in base.oslist
1737
      _ErrorIf(test, self.ENODEOS, node,
1738
               "Extra OS %s not present on reference node (%s)",
1739
               os_name, base.name)
1740
      if test:
1741
        continue
1742
      assert base.oslist[os_name], "Base node has empty OS status?"
1743
      _, b_status, _, b_var, b_param, b_api = base.oslist[os_name][0]
1744
      if not b_status:
1745
        # base OS is invalid, skipping
1746
        continue
1747
      for kind, a, b in [("API version", f_api, b_api),
1748
                         ("variants list", f_var, b_var),
1749
                         ("parameters", f_param, b_param)]:
1750
        _ErrorIf(a != b, self.ENODEOS, node,
1751
                 "OS %s %s differs from reference node %s: %s vs. %s",
1752
                 kind, os_name, base.name,
1753
                 utils.CommaJoin(a), utils.CommaJoin(b))
1754

    
1755
    # check any missing OSes
1756
    missing = set(base.oslist.keys()).difference(nimg.oslist.keys())
1757
    _ErrorIf(missing, self.ENODEOS, node,
1758
             "OSes present on reference node %s but missing on this node: %s",
1759
             base.name, utils.CommaJoin(missing))
1760

    
1761
  def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
1762
    """Verifies and updates the node volume data.
1763

1764
    This function will update a L{NodeImage}'s internal structures
1765
    with data from the remote call.
1766

1767
    @type ninfo: L{objects.Node}
1768
    @param ninfo: the node to check
1769
    @param nresult: the remote results for the node
1770
    @param nimg: the node image object
1771
    @param vg_name: the configured VG name
1772

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

    
1777
    nimg.lvm_fail = True
1778
    lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1779
    if vg_name is None:
1780
      pass
1781
    elif isinstance(lvdata, basestring):
1782
      _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1783
               utils.SafeEncode(lvdata))
1784
    elif not isinstance(lvdata, dict):
1785
      _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1786
    else:
1787
      nimg.volumes = lvdata
1788
      nimg.lvm_fail = False
1789

    
1790
  def _UpdateNodeInstances(self, ninfo, nresult, nimg):
1791
    """Verifies and updates the node instance list.
1792

1793
    If the listing was successful, then updates this node's instance
1794
    list. Otherwise, it marks the RPC call as failed for the instance
1795
    list key.
1796

1797
    @type ninfo: L{objects.Node}
1798
    @param ninfo: the node to check
1799
    @param nresult: the remote results for the node
1800
    @param nimg: the node image object
1801

1802
    """
1803
    idata = nresult.get(constants.NV_INSTANCELIST, None)
1804
    test = not isinstance(idata, list)
1805
    self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed"
1806
                  " (instancelist): %s", utils.SafeEncode(str(idata)))
1807
    if test:
1808
      nimg.hyp_fail = True
1809
    else:
1810
      nimg.instances = idata
1811

    
1812
  def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
1813
    """Verifies and computes a node information map
1814

1815
    @type ninfo: L{objects.Node}
1816
    @param ninfo: the node to check
1817
    @param nresult: the remote results for the node
1818
    @param nimg: the node image object
1819
    @param vg_name: the configured VG name
1820

1821
    """
1822
    node = ninfo.name
1823
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1824

    
1825
    # try to read free memory (from the hypervisor)
1826
    hv_info = nresult.get(constants.NV_HVINFO, None)
1827
    test = not isinstance(hv_info, dict) or "memory_free" not in hv_info
1828
    _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1829
    if not test:
1830
      try:
1831
        nimg.mfree = int(hv_info["memory_free"])
1832
      except (ValueError, TypeError):
1833
        _ErrorIf(True, self.ENODERPC, node,
1834
                 "node returned invalid nodeinfo, check hypervisor")
1835

    
1836
    # FIXME: devise a free space model for file based instances as well
1837
    if vg_name is not None:
1838
      test = (constants.NV_VGLIST not in nresult or
1839
              vg_name not in nresult[constants.NV_VGLIST])
1840
      _ErrorIf(test, self.ENODELVM, node,
1841
               "node didn't return data for the volume group '%s'"
1842
               " - it is either missing or broken", vg_name)
1843
      if not test:
1844
        try:
1845
          nimg.dfree = int(nresult[constants.NV_VGLIST][vg_name])
1846
        except (ValueError, TypeError):
1847
          _ErrorIf(True, self.ENODERPC, node,
1848
                   "node returned invalid LVM info, check LVM status")
1849

    
1850
  def BuildHooksEnv(self):
1851
    """Build hooks env.
1852

1853
    Cluster-Verify hooks just ran in the post phase and their failure makes
1854
    the output be logged in the verify output and the verification to fail.
1855

1856
    """
1857
    all_nodes = self.cfg.GetNodeList()
1858
    env = {
1859
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1860
      }
1861
    for node in self.cfg.GetAllNodesInfo().values():
1862
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1863

    
1864
    return env, [], all_nodes
1865

    
1866
  def Exec(self, feedback_fn):
1867
    """Verify integrity of cluster, performing various test on nodes.
1868

1869
    """
1870
    self.bad = False
1871
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1872
    verbose = self.op.verbose
1873
    self._feedback_fn = feedback_fn
1874
    feedback_fn("* Verifying global settings")
1875
    for msg in self.cfg.VerifyConfig():
1876
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1877

    
1878
    # Check the cluster certificates
1879
    for cert_filename in constants.ALL_CERT_FILES:
1880
      (errcode, msg) = _VerifyCertificate(cert_filename)
1881
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1882

    
1883
    vg_name = self.cfg.GetVGName()
1884
    drbd_helper = self.cfg.GetDRBDHelper()
1885
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1886
    cluster = self.cfg.GetClusterInfo()
1887
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1888
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1889
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1890
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1891
                        for iname in instancelist)
1892
    i_non_redundant = [] # Non redundant instances
1893
    i_non_a_balanced = [] # Non auto-balanced instances
1894
    n_offline = 0 # Count of offline nodes
1895
    n_drained = 0 # Count of nodes being drained
1896
    node_vol_should = {}
1897

    
1898
    # FIXME: verify OS list
1899
    # do local checksums
1900
    master_files = [constants.CLUSTER_CONF_FILE]
1901
    master_node = self.master_node = self.cfg.GetMasterNode()
1902
    master_ip = self.cfg.GetMasterIP()
1903

    
1904
    file_names = ssconf.SimpleStore().GetFileList()
1905
    file_names.extend(constants.ALL_CERT_FILES)
1906
    file_names.extend(master_files)
1907
    if cluster.modify_etc_hosts:
1908
      file_names.append(constants.ETC_HOSTS)
1909

    
1910
    local_checksums = utils.FingerprintFiles(file_names)
1911

    
1912
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1913
    node_verify_param = {
1914
      constants.NV_FILELIST: file_names,
1915
      constants.NV_NODELIST: [node.name for node in nodeinfo
1916
                              if not node.offline],
1917
      constants.NV_HYPERVISOR: hypervisors,
1918
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1919
                                  node.secondary_ip) for node in nodeinfo
1920
                                 if not node.offline],
1921
      constants.NV_INSTANCELIST: hypervisors,
1922
      constants.NV_VERSION: None,
1923
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1924
      constants.NV_NODESETUP: None,
1925
      constants.NV_TIME: None,
1926
      constants.NV_MASTERIP: (master_node, master_ip),
1927
      constants.NV_OSLIST: None,
1928
      }
1929

    
1930
    if vg_name is not None:
1931
      node_verify_param[constants.NV_VGLIST] = None
1932
      node_verify_param[constants.NV_LVLIST] = vg_name
1933
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1934
      node_verify_param[constants.NV_DRBDLIST] = None
1935

    
1936
    if drbd_helper:
1937
      node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
1938

    
1939
    # Build our expected cluster state
1940
    node_image = dict((node.name, self.NodeImage(offline=node.offline,
1941
                                                 name=node.name))
1942
                      for node in nodeinfo)
1943

    
1944
    for instance in instancelist:
1945
      inst_config = instanceinfo[instance]
1946

    
1947
      for nname in inst_config.all_nodes:
1948
        if nname not in node_image:
1949
          # ghost node
1950
          gnode = self.NodeImage(name=nname)
1951
          gnode.ghost = True
1952
          node_image[nname] = gnode
1953

    
1954
      inst_config.MapLVsByNode(node_vol_should)
1955

    
1956
      pnode = inst_config.primary_node
1957
      node_image[pnode].pinst.append(instance)
1958

    
1959
      for snode in inst_config.secondary_nodes:
1960
        nimg = node_image[snode]
1961
        nimg.sinst.append(instance)
1962
        if pnode not in nimg.sbp:
1963
          nimg.sbp[pnode] = []
1964
        nimg.sbp[pnode].append(instance)
1965

    
1966
    # At this point, we have the in-memory data structures complete,
1967
    # except for the runtime information, which we'll gather next
1968

    
1969
    # Due to the way our RPC system works, exact response times cannot be
1970
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
1971
    # time before and after executing the request, we can at least have a time
1972
    # window.
1973
    nvinfo_starttime = time.time()
1974
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1975
                                           self.cfg.GetClusterName())
1976
    nvinfo_endtime = time.time()
1977

    
1978
    all_drbd_map = self.cfg.ComputeDRBDMap()
1979

    
1980
    feedback_fn("* Verifying node status")
1981

    
1982
    refos_img = None
1983

    
1984
    for node_i in nodeinfo:
1985
      node = node_i.name
1986
      nimg = node_image[node]
1987

    
1988
      if node_i.offline:
1989
        if verbose:
1990
          feedback_fn("* Skipping offline node %s" % (node,))
1991
        n_offline += 1
1992
        continue
1993

    
1994
      if node == master_node:
1995
        ntype = "master"
1996
      elif node_i.master_candidate:
1997
        ntype = "master candidate"
1998
      elif node_i.drained:
1999
        ntype = "drained"
2000
        n_drained += 1
2001
      else:
2002
        ntype = "regular"
2003
      if verbose:
2004
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
2005

    
2006
      msg = all_nvinfo[node].fail_msg
2007
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
2008
      if msg:
2009
        nimg.rpc_fail = True
2010
        continue
2011

    
2012
      nresult = all_nvinfo[node].payload
2013

    
2014
      nimg.call_ok = self._VerifyNode(node_i, nresult)
2015
      self._VerifyNodeNetwork(node_i, nresult)
2016
      self._VerifyNodeLVM(node_i, nresult, vg_name)
2017
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
2018
                            master_files)
2019
      self._VerifyNodeDrbd(node_i, nresult, instanceinfo, drbd_helper,
2020
                           all_drbd_map)
2021
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
2022

    
2023
      self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
2024
      self._UpdateNodeInstances(node_i, nresult, nimg)
2025
      self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
2026
      self._UpdateNodeOS(node_i, nresult, nimg)
2027
      if not nimg.os_fail:
2028
        if refos_img is None:
2029
          refos_img = nimg
2030
        self._VerifyNodeOS(node_i, nimg, refos_img)
2031

    
2032
    feedback_fn("* Verifying instance status")
2033
    for instance in instancelist:
2034
      if verbose:
2035
        feedback_fn("* Verifying instance %s" % instance)
2036
      inst_config = instanceinfo[instance]
2037
      self._VerifyInstance(instance, inst_config, node_image)
2038
      inst_nodes_offline = []
2039

    
2040
      pnode = inst_config.primary_node
2041
      pnode_img = node_image[pnode]
2042
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
2043
               self.ENODERPC, pnode, "instance %s, connection to"
2044
               " primary node failed", instance)
2045

    
2046
      if pnode_img.offline:
2047
        inst_nodes_offline.append(pnode)
2048

    
2049
      # If the instance is non-redundant we cannot survive losing its primary
2050
      # node, so we are not N+1 compliant. On the other hand we have no disk
2051
      # templates with more than one secondary so that situation is not well
2052
      # supported either.
2053
      # FIXME: does not support file-backed instances
2054
      if not inst_config.secondary_nodes:
2055
        i_non_redundant.append(instance)
2056
      _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
2057
               instance, "instance has multiple secondary nodes: %s",
2058
               utils.CommaJoin(inst_config.secondary_nodes),
2059
               code=self.ETYPE_WARNING)
2060

    
2061
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
2062
        i_non_a_balanced.append(instance)
2063

    
2064
      for snode in inst_config.secondary_nodes:
2065
        s_img = node_image[snode]
2066
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
2067
                 "instance %s, connection to secondary node failed", instance)
2068

    
2069
        if s_img.offline:
2070
          inst_nodes_offline.append(snode)
2071

    
2072
      # warn that the instance lives on offline nodes
2073
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
2074
               "instance lives on offline node(s) %s",
2075
               utils.CommaJoin(inst_nodes_offline))
2076
      # ... or ghost nodes
2077
      for node in inst_config.all_nodes:
2078
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
2079
                 "instance lives on ghost node %s", node)
2080

    
2081
    feedback_fn("* Verifying orphan volumes")
2082
    reserved = utils.FieldSet(*cluster.reserved_lvs)
2083
    self._VerifyOrphanVolumes(node_vol_should, node_image, reserved)
2084

    
2085
    feedback_fn("* Verifying orphan instances")
2086
    self._VerifyOrphanInstances(instancelist, node_image)
2087

    
2088
    if constants.VERIFY_NPLUSONE_MEM not in self.op.skip_checks:
2089
      feedback_fn("* Verifying N+1 Memory redundancy")
2090
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
2091

    
2092
    feedback_fn("* Other Notes")
2093
    if i_non_redundant:
2094
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
2095
                  % len(i_non_redundant))
2096

    
2097
    if i_non_a_balanced:
2098
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
2099
                  % len(i_non_a_balanced))
2100

    
2101
    if n_offline:
2102
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
2103

    
2104
    if n_drained:
2105
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
2106

    
2107
    return not self.bad
2108

    
2109
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
2110
    """Analyze the post-hooks' result
2111

2112
    This method analyses the hook result, handles it, and sends some
2113
    nicely-formatted feedback back to the user.
2114

2115
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
2116
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
2117
    @param hooks_results: the results of the multi-node hooks rpc call
2118
    @param feedback_fn: function used send feedback back to the caller
2119
    @param lu_result: previous Exec result
2120
    @return: the new Exec result, based on the previous result
2121
        and hook results
2122

2123
    """
2124
    # We only really run POST phase hooks, and are only interested in
2125
    # their results
2126
    if phase == constants.HOOKS_PHASE_POST:
2127
      # Used to change hooks' output to proper indentation
2128
      indent_re = re.compile('^', re.M)
2129
      feedback_fn("* Hooks Results")
2130
      assert hooks_results, "invalid result from hooks"
2131

    
2132
      for node_name in hooks_results:
2133
        res = hooks_results[node_name]
2134
        msg = res.fail_msg
2135
        test = msg and not res.offline
2136
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
2137
                      "Communication failure in hooks execution: %s", msg)
2138
        if res.offline or msg:
2139
          # No need to investigate payload if node is offline or gave an error.
2140
          # override manually lu_result here as _ErrorIf only
2141
          # overrides self.bad
2142
          lu_result = 1
2143
          continue
2144
        for script, hkr, output in res.payload:
2145
          test = hkr == constants.HKR_FAIL
2146
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
2147
                        "Script %s failed, output:", script)
2148
          if test:
2149
            output = indent_re.sub('      ', output)
2150
            feedback_fn("%s" % output)
2151
            lu_result = 0
2152

    
2153
      return lu_result
2154

    
2155

    
2156
class LUVerifyDisks(NoHooksLU):
2157
  """Verifies the cluster disks status.
2158

2159
  """
2160
  REQ_BGL = False
2161

    
2162
  def ExpandNames(self):
2163
    self.needed_locks = {
2164
      locking.LEVEL_NODE: locking.ALL_SET,
2165
      locking.LEVEL_INSTANCE: locking.ALL_SET,
2166
    }
2167
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
2168

    
2169
  def Exec(self, feedback_fn):
2170
    """Verify integrity of cluster disks.
2171

2172
    @rtype: tuple of three items
2173
    @return: a tuple of (dict of node-to-node_error, list of instances
2174
        which need activate-disks, dict of instance: (node, volume) for
2175
        missing volumes
2176

2177
    """
2178
    result = res_nodes, res_instances, res_missing = {}, [], {}
2179

    
2180
    vg_name = self.cfg.GetVGName()
2181
    nodes = utils.NiceSort(self.cfg.GetNodeList())
2182
    instances = [self.cfg.GetInstanceInfo(name)
2183
                 for name in self.cfg.GetInstanceList()]
2184

    
2185
    nv_dict = {}
2186
    for inst in instances:
2187
      inst_lvs = {}
2188
      if (not inst.admin_up or
2189
          inst.disk_template not in constants.DTS_NET_MIRROR):
2190
        continue
2191
      inst.MapLVsByNode(inst_lvs)
2192
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
2193
      for node, vol_list in inst_lvs.iteritems():
2194
        for vol in vol_list:
2195
          nv_dict[(node, vol)] = inst
2196

    
2197
    if not nv_dict:
2198
      return result
2199

    
2200
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
2201

    
2202
    for node in nodes:
2203
      # node_volume
2204
      node_res = node_lvs[node]
2205
      if node_res.offline:
2206
        continue
2207
      msg = node_res.fail_msg
2208
      if msg:
2209
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
2210
        res_nodes[node] = msg
2211
        continue
2212

    
2213
      lvs = node_res.payload
2214
      for lv_name, (_, _, lv_online) in lvs.items():
2215
        inst = nv_dict.pop((node, lv_name), None)
2216
        if (not lv_online and inst is not None
2217
            and inst.name not in res_instances):
2218
          res_instances.append(inst.name)
2219

    
2220
    # any leftover items in nv_dict are missing LVs, let's arrange the
2221
    # data better
2222
    for key, inst in nv_dict.iteritems():
2223
      if inst.name not in res_missing:
2224
        res_missing[inst.name] = []
2225
      res_missing[inst.name].append(key)
2226

    
2227
    return result
2228

    
2229

    
2230
class LURepairDiskSizes(NoHooksLU):
2231
  """Verifies the cluster disks sizes.
2232

2233
  """
2234
  _OP_PARAMS = [("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString))]
2235
  REQ_BGL = False
2236

    
2237
  def ExpandNames(self):
2238
    if self.op.instances:
2239
      self.wanted_names = []
2240
      for name in self.op.instances:
2241
        full_name = _ExpandInstanceName(self.cfg, name)
2242
        self.wanted_names.append(full_name)
2243
      self.needed_locks = {
2244
        locking.LEVEL_NODE: [],
2245
        locking.LEVEL_INSTANCE: self.wanted_names,
2246
        }
2247
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2248
    else:
2249
      self.wanted_names = None
2250
      self.needed_locks = {
2251
        locking.LEVEL_NODE: locking.ALL_SET,
2252
        locking.LEVEL_INSTANCE: locking.ALL_SET,
2253
        }
2254
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
2255

    
2256
  def DeclareLocks(self, level):
2257
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2258
      self._LockInstancesNodes(primary_only=True)
2259

    
2260
  def CheckPrereq(self):
2261
    """Check prerequisites.
2262

2263
    This only checks the optional instance list against the existing names.
2264

2265
    """
2266
    if self.wanted_names is None:
2267
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2268

    
2269
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2270
                             in self.wanted_names]
2271

    
2272
  def _EnsureChildSizes(self, disk):
2273
    """Ensure children of the disk have the needed disk size.
2274

2275
    This is valid mainly for DRBD8 and fixes an issue where the
2276
    children have smaller disk size.
2277

2278
    @param disk: an L{ganeti.objects.Disk} object
2279

2280
    """
2281
    if disk.dev_type == constants.LD_DRBD8:
2282
      assert disk.children, "Empty children for DRBD8?"
2283
      fchild = disk.children[0]
2284
      mismatch = fchild.size < disk.size
2285
      if mismatch:
2286
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2287
                     fchild.size, disk.size)
2288
        fchild.size = disk.size
2289

    
2290
      # and we recurse on this child only, not on the metadev
2291
      return self._EnsureChildSizes(fchild) or mismatch
2292
    else:
2293
      return False
2294

    
2295
  def Exec(self, feedback_fn):
2296
    """Verify the size of cluster disks.
2297

2298
    """
2299
    # TODO: check child disks too
2300
    # TODO: check differences in size between primary/secondary nodes
2301
    per_node_disks = {}
2302
    for instance in self.wanted_instances:
2303
      pnode = instance.primary_node
2304
      if pnode not in per_node_disks:
2305
        per_node_disks[pnode] = []
2306
      for idx, disk in enumerate(instance.disks):
2307
        per_node_disks[pnode].append((instance, idx, disk))
2308

    
2309
    changed = []
2310
    for node, dskl in per_node_disks.items():
2311
      newl = [v[2].Copy() for v in dskl]
2312
      for dsk in newl:
2313
        self.cfg.SetDiskID(dsk, node)
2314
      result = self.rpc.call_blockdev_getsizes(node, newl)
2315
      if result.fail_msg:
2316
        self.LogWarning("Failure in blockdev_getsizes call to node"
2317
                        " %s, ignoring", node)
2318
        continue
2319
      if len(result.data) != len(dskl):
2320
        self.LogWarning("Invalid result from node %s, ignoring node results",
2321
                        node)
2322
        continue
2323
      for ((instance, idx, disk), size) in zip(dskl, result.data):
2324
        if size is None:
2325
          self.LogWarning("Disk %d of instance %s did not return size"
2326
                          " information, ignoring", idx, instance.name)
2327
          continue
2328
        if not isinstance(size, (int, long)):
2329
          self.LogWarning("Disk %d of instance %s did not return valid"
2330
                          " size information, ignoring", idx, instance.name)
2331
          continue
2332
        size = size >> 20
2333
        if size != disk.size:
2334
          self.LogInfo("Disk %d of instance %s has mismatched size,"
2335
                       " correcting: recorded %d, actual %d", idx,
2336
                       instance.name, disk.size, size)
2337
          disk.size = size
2338
          self.cfg.Update(instance, feedback_fn)
2339
          changed.append((instance.name, idx, size))
2340
        if self._EnsureChildSizes(disk):
2341
          self.cfg.Update(instance, feedback_fn)
2342
          changed.append((instance.name, idx, disk.size))
2343
    return changed
2344

    
2345

    
2346
class LURenameCluster(LogicalUnit):
2347
  """Rename the cluster.
2348

2349
  """
2350
  HPATH = "cluster-rename"
2351
  HTYPE = constants.HTYPE_CLUSTER
2352
  _OP_PARAMS = [("name", ht.NoDefault, ht.TNonEmptyString)]
2353

    
2354
  def BuildHooksEnv(self):
2355
    """Build hooks env.
2356

2357
    """
2358
    env = {
2359
      "OP_TARGET": self.cfg.GetClusterName(),
2360
      "NEW_NAME": self.op.name,
2361
      }
2362
    mn = self.cfg.GetMasterNode()
2363
    all_nodes = self.cfg.GetNodeList()
2364
    return env, [mn], all_nodes
2365

    
2366
  def CheckPrereq(self):
2367
    """Verify that the passed name is a valid one.
2368

2369
    """
2370
    hostname = netutils.GetHostname(name=self.op.name,
2371
                                    family=self.cfg.GetPrimaryIPFamily())
2372

    
2373
    new_name = hostname.name
2374
    self.ip = new_ip = hostname.ip
2375
    old_name = self.cfg.GetClusterName()
2376
    old_ip = self.cfg.GetMasterIP()
2377
    if new_name == old_name and new_ip == old_ip:
2378
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
2379
                                 " cluster has changed",
2380
                                 errors.ECODE_INVAL)
2381
    if new_ip != old_ip:
2382
      if netutils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
2383
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
2384
                                   " reachable on the network" %
2385
                                   new_ip, errors.ECODE_NOTUNIQUE)
2386

    
2387
    self.op.name = new_name
2388

    
2389
  def Exec(self, feedback_fn):
2390
    """Rename the cluster.
2391

2392
    """
2393
    clustername = self.op.name
2394
    ip = self.ip
2395

    
2396
    # shutdown the master IP
2397
    master = self.cfg.GetMasterNode()
2398
    result = self.rpc.call_node_stop_master(master, False)
2399
    result.Raise("Could not disable the master role")
2400

    
2401
    try:
2402
      cluster = self.cfg.GetClusterInfo()
2403
      cluster.cluster_name = clustername
2404
      cluster.master_ip = ip
2405
      self.cfg.Update(cluster, feedback_fn)
2406

    
2407
      # update the known hosts file
2408
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2409
      node_list = self.cfg.GetNodeList()
2410
      try:
2411
        node_list.remove(master)
2412
      except ValueError:
2413
        pass
2414
      result = self.rpc.call_upload_file(node_list,
2415
                                         constants.SSH_KNOWN_HOSTS_FILE)
2416
      for to_node, to_result in result.iteritems():
2417
        msg = to_result.fail_msg
2418
        if msg:
2419
          msg = ("Copy of file %s to node %s failed: %s" %
2420
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
2421
          self.proc.LogWarning(msg)
2422

    
2423
    finally:
2424
      result = self.rpc.call_node_start_master(master, False, False)
2425
      msg = result.fail_msg
2426
      if msg:
2427
        self.LogWarning("Could not re-enable the master role on"
2428
                        " the master, please restart manually: %s", msg)
2429

    
2430
    return clustername
2431

    
2432

    
2433
class LUSetClusterParams(LogicalUnit):
2434
  """Change the parameters of the cluster.
2435

2436
  """
2437
  HPATH = "cluster-modify"
2438
  HTYPE = constants.HTYPE_CLUSTER
2439
  _OP_PARAMS = [
2440
    ("vg_name", None, ht.TMaybeString),
2441
    ("enabled_hypervisors", None,
2442
     ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
2443
            ht.TNone)),
2444
    ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
2445
                              ht.TNone)),
2446
    ("beparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
2447
                              ht.TNone)),
2448
    ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
2449
                            ht.TNone)),
2450
    ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
2451
                              ht.TNone)),
2452
    ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone)),
2453
    ("uid_pool", None, ht.NoType),
2454
    ("add_uids", None, ht.NoType),
2455
    ("remove_uids", None, ht.NoType),
2456
    ("maintain_node_health", None, ht.TMaybeBool),
2457
    ("prealloc_wipe_disks", None, ht.TMaybeBool),
2458
    ("nicparams", None, ht.TOr(ht.TDict, ht.TNone)),
2459
    ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone)),
2460
    ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone)),
2461
    ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone)),
2462
    ("hidden_os", None, ht.TOr(ht.TListOf(\
2463
          ht.TAnd(ht.TList,
2464
                ht.TIsLength(2),
2465
                ht.TMap(lambda v: v[0], ht.TElemOf(constants.DDMS_VALUES)))),
2466
          ht.TNone)),
2467
    ("blacklisted_os", None, ht.TOr(ht.TListOf(\
2468
          ht.TAnd(ht.TList,
2469
                ht.TIsLength(2),
2470
                ht.TMap(lambda v: v[0], ht.TElemOf(constants.DDMS_VALUES)))),
2471
          ht.TNone)),
2472
    ]
2473
  REQ_BGL = False
2474

    
2475
  def CheckArguments(self):
2476
    """Check parameters
2477

2478
    """
2479
    if self.op.uid_pool:
2480
      uidpool.CheckUidPool(self.op.uid_pool)
2481

    
2482
    if self.op.add_uids:
2483
      uidpool.CheckUidPool(self.op.add_uids)
2484

    
2485
    if self.op.remove_uids:
2486
      uidpool.CheckUidPool(self.op.remove_uids)
2487

    
2488
  def ExpandNames(self):
2489
    # FIXME: in the future maybe other cluster params won't require checking on
2490
    # all nodes to be modified.
2491
    self.needed_locks = {
2492
      locking.LEVEL_NODE: locking.ALL_SET,
2493
    }
2494
    self.share_locks[locking.LEVEL_NODE] = 1
2495

    
2496
  def BuildHooksEnv(self):
2497
    """Build hooks env.
2498

2499
    """
2500
    env = {
2501
      "OP_TARGET": self.cfg.GetClusterName(),
2502
      "NEW_VG_NAME": self.op.vg_name,
2503
      }
2504
    mn = self.cfg.GetMasterNode()
2505
    return env, [mn], [mn]
2506

    
2507
  def CheckPrereq(self):
2508
    """Check prerequisites.
2509

2510
    This checks whether the given params don't conflict and
2511
    if the given volume group is valid.
2512

2513
    """
2514
    if self.op.vg_name is not None and not self.op.vg_name:
2515
      if self.cfg.HasAnyDiskOfType(constants.LD_LV):
2516
        raise errors.OpPrereqError("Cannot disable lvm storage while lvm-based"
2517
                                   " instances exist", errors.ECODE_INVAL)
2518

    
2519
    if self.op.drbd_helper is not None and not self.op.drbd_helper:
2520
      if self.cfg.HasAnyDiskOfType(constants.LD_DRBD8):
2521
        raise errors.OpPrereqError("Cannot disable drbd helper while"
2522
                                   " drbd-based instances exist",
2523
                                   errors.ECODE_INVAL)
2524

    
2525
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2526

    
2527
    # if vg_name not None, checks given volume group on all nodes
2528
    if self.op.vg_name:
2529
      vglist = self.rpc.call_vg_list(node_list)
2530
      for node in node_list:
2531
        msg = vglist[node].fail_msg
2532
        if msg:
2533
          # ignoring down node
2534
          self.LogWarning("Error while gathering data on node %s"
2535
                          " (ignoring node): %s", node, msg)
2536
          continue
2537
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2538
                                              self.op.vg_name,
2539
                                              constants.MIN_VG_SIZE)
2540
        if vgstatus:
2541
          raise errors.OpPrereqError("Error on node '%s': %s" %
2542
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2543

    
2544
    if self.op.drbd_helper:
2545
      # checks given drbd helper on all nodes
2546
      helpers = self.rpc.call_drbd_helper(node_list)
2547
      for node in node_list:
2548
        ninfo = self.cfg.GetNodeInfo(node)
2549
        if ninfo.offline:
2550
          self.LogInfo("Not checking drbd helper on offline node %s", node)
2551
          continue
2552
        msg = helpers[node].fail_msg
2553
        if msg:
2554
          raise errors.OpPrereqError("Error checking drbd helper on node"
2555
                                     " '%s': %s" % (node, msg),
2556
                                     errors.ECODE_ENVIRON)
2557
        node_helper = helpers[node].payload
2558
        if node_helper != self.op.drbd_helper:
2559
          raise errors.OpPrereqError("Error on node '%s': drbd helper is %s" %
2560
                                     (node, node_helper), errors.ECODE_ENVIRON)
2561

    
2562
    self.cluster = cluster = self.cfg.GetClusterInfo()
2563
    # validate params changes
2564
    if self.op.beparams:
2565
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2566
      self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
2567

    
2568
    if self.op.nicparams:
2569
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2570
      self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
2571
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2572
      nic_errors = []
2573

    
2574
      # check all instances for consistency
2575
      for instance in self.cfg.GetAllInstancesInfo().values():
2576
        for nic_idx, nic in enumerate(instance.nics):
2577
          params_copy = copy.deepcopy(nic.nicparams)
2578
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2579

    
2580
          # check parameter syntax
2581
          try:
2582
            objects.NIC.CheckParameterSyntax(params_filled)
2583
          except errors.ConfigurationError, err:
2584
            nic_errors.append("Instance %s, nic/%d: %s" %
2585
                              (instance.name, nic_idx, err))
2586

    
2587
          # if we're moving instances to routed, check that they have an ip
2588
          target_mode = params_filled[constants.NIC_MODE]
2589
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2590
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2591
                              (instance.name, nic_idx))
2592
      if nic_errors:
2593
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2594
                                   "\n".join(nic_errors))
2595

    
2596
    # hypervisor list/parameters
2597
    self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
2598
    if self.op.hvparams:
2599
      for hv_name, hv_dict in self.op.hvparams.items():
2600
        if hv_name not in self.new_hvparams:
2601
          self.new_hvparams[hv_name] = hv_dict
2602
        else:
2603
          self.new_hvparams[hv_name].update(hv_dict)
2604

    
2605
    # os hypervisor parameters
2606
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2607
    if self.op.os_hvp:
2608
      for os_name, hvs in self.op.os_hvp.items():
2609
        if os_name not in self.new_os_hvp:
2610
          self.new_os_hvp[os_name] = hvs
2611
        else:
2612
          for hv_name, hv_dict in hvs.items():
2613
            if hv_name not in self.new_os_hvp[os_name]:
2614
              self.new_os_hvp[os_name][hv_name] = hv_dict
2615
            else:
2616
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2617

    
2618
    # os parameters
2619
    self.new_osp = objects.FillDict(cluster.osparams, {})
2620
    if self.op.osparams:
2621
      for os_name, osp in self.op.osparams.items():
2622
        if os_name not in self.new_osp:
2623
          self.new_osp[os_name] = {}
2624

    
2625
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
2626
                                                  use_none=True)
2627

    
2628
        if not self.new_osp[os_name]:
2629
          # we removed all parameters
2630
          del self.new_osp[os_name]
2631
        else:
2632
          # check the parameter validity (remote check)
2633
          _CheckOSParams(self, False, [self.cfg.GetMasterNode()],
2634
                         os_name, self.new_osp[os_name])
2635

    
2636
    # changes to the hypervisor list
2637
    if self.op.enabled_hypervisors is not None:
2638
      self.hv_list = self.op.enabled_hypervisors
2639
      for hv in self.hv_list:
2640
        # if the hypervisor doesn't already exist in the cluster
2641
        # hvparams, we initialize it to empty, and then (in both
2642
        # cases) we make sure to fill the defaults, as we might not
2643
        # have a complete defaults list if the hypervisor wasn't
2644
        # enabled before
2645
        if hv not in new_hvp:
2646
          new_hvp[hv] = {}
2647
        new_hvp[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], new_hvp[hv])
2648
        utils.ForceDictType(new_hvp[hv], constants.HVS_PARAMETER_TYPES)
2649
    else:
2650
      self.hv_list = cluster.enabled_hypervisors
2651

    
2652
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2653
      # either the enabled list has changed, or the parameters have, validate
2654
      for hv_name, hv_params in self.new_hvparams.items():
2655
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2656
            (self.op.enabled_hypervisors and
2657
             hv_name in self.op.enabled_hypervisors)):
2658
          # either this is a new hypervisor, or its parameters have changed
2659
          hv_class = hypervisor.GetHypervisor(hv_name)
2660
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2661
          hv_class.CheckParameterSyntax(hv_params)
2662
          _CheckHVParams(self, node_list, hv_name, hv_params)
2663

    
2664
    if self.op.os_hvp:
2665
      # no need to check any newly-enabled hypervisors, since the
2666
      # defaults have already been checked in the above code-block
2667
      for os_name, os_hvp in self.new_os_hvp.items():
2668
        for hv_name, hv_params in os_hvp.items():
2669
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2670
          # we need to fill in the new os_hvp on top of the actual hv_p
2671
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2672
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2673
          hv_class = hypervisor.GetHypervisor(hv_name)
2674
          hv_class.CheckParameterSyntax(new_osp)
2675
          _CheckHVParams(self, node_list, hv_name, new_osp)
2676

    
2677
    if self.op.default_iallocator:
2678
      alloc_script = utils.FindFile(self.op.default_iallocator,
2679
                                    constants.IALLOCATOR_SEARCH_PATH,
2680
                                    os.path.isfile)
2681
      if alloc_script is None:
2682
        raise errors.OpPrereqError("Invalid default iallocator script '%s'"
2683
                                   " specified" % self.op.default_iallocator,
2684
                                   errors.ECODE_INVAL)
2685

    
2686
  def Exec(self, feedback_fn):
2687
    """Change the parameters of the cluster.
2688

2689
    """
2690
    if self.op.vg_name is not None:
2691
      new_volume = self.op.vg_name
2692
      if not new_volume:
2693
        new_volume = None
2694
      if new_volume != self.cfg.GetVGName():
2695
        self.cfg.SetVGName(new_volume)
2696
      else:
2697
        feedback_fn("Cluster LVM configuration already in desired"
2698
                    " state, not changing")
2699
    if self.op.drbd_helper is not None:
2700
      new_helper = self.op.drbd_helper
2701
      if not new_helper:
2702
        new_helper = None
2703
      if new_helper != self.cfg.GetDRBDHelper():
2704
        self.cfg.SetDRBDHelper(new_helper)
2705
      else:
2706
        feedback_fn("Cluster DRBD helper already in desired state,"
2707
                    " not changing")
2708
    if self.op.hvparams:
2709
      self.cluster.hvparams = self.new_hvparams
2710
    if self.op.os_hvp:
2711
      self.cluster.os_hvp = self.new_os_hvp
2712
    if self.op.enabled_hypervisors is not None:
2713
      self.cluster.hvparams = self.new_hvparams
2714
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2715
    if self.op.beparams:
2716
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2717
    if self.op.nicparams:
2718
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2719
    if self.op.osparams:
2720
      self.cluster.osparams = self.new_osp
2721

    
2722
    if self.op.candidate_pool_size is not None:
2723
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2724
      # we need to update the pool size here, otherwise the save will fail
2725
      _AdjustCandidatePool(self, [])
2726

    
2727
    if self.op.maintain_node_health is not None:
2728
      self.cluster.maintain_node_health = self.op.maintain_node_health
2729

    
2730
    if self.op.prealloc_wipe_disks is not None:
2731
      self.cluster.prealloc_wipe_disks = self.op.prealloc_wipe_disks
2732

    
2733
    if self.op.add_uids is not None:
2734
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
2735

    
2736
    if self.op.remove_uids is not None:
2737
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
2738

    
2739
    if self.op.uid_pool is not None:
2740
      self.cluster.uid_pool = self.op.uid_pool
2741

    
2742
    if self.op.default_iallocator is not None:
2743
      self.cluster.default_iallocator = self.op.default_iallocator
2744

    
2745
    if self.op.reserved_lvs is not None:
2746
      self.cluster.reserved_lvs = self.op.reserved_lvs
2747

    
2748
    def helper_os(aname, mods, desc):
2749
      desc += " OS list"
2750
      lst = getattr(self.cluster, aname)
2751
      for key, val in mods:
2752
        if key == constants.DDM_ADD:
2753
          if val in lst:
2754
            feedback_fn("OS %s already in %s, ignoring", val, desc)
2755
          else:
2756
            lst.append(val)
2757
        elif key == constants.DDM_REMOVE:
2758
          if val in lst:
2759
            lst.remove(val)
2760
          else:
2761
            feedback_fn("OS %s not found in %s, ignoring", val, desc)
2762
        else:
2763
          raise errors.ProgrammerError("Invalid modification '%s'" % key)
2764

    
2765
    if self.op.hidden_os:
2766
      helper_os("hidden_os", self.op.hidden_os, "hidden")
2767

    
2768
    if self.op.blacklisted_os:
2769
      helper_os("blacklisted_os", self.op.blacklisted_os, "blacklisted")
2770

    
2771
    self.cfg.Update(self.cluster, feedback_fn)
2772

    
2773

    
2774
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2775
  """Distribute additional files which are part of the cluster configuration.
2776

2777
  ConfigWriter takes care of distributing the config and ssconf files, but
2778
  there are more files which should be distributed to all nodes. This function
2779
  makes sure those are copied.
2780

2781
  @param lu: calling logical unit
2782
  @param additional_nodes: list of nodes not in the config to distribute to
2783

2784
  """
2785
  # 1. Gather target nodes
2786
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2787
  dist_nodes = lu.cfg.GetOnlineNodeList()
2788
  if additional_nodes is not None:
2789
    dist_nodes.extend(additional_nodes)
2790
  if myself.name in dist_nodes:
2791
    dist_nodes.remove(myself.name)
2792

    
2793
  # 2. Gather files to distribute
2794
  dist_files = set([constants.ETC_HOSTS,
2795
                    constants.SSH_KNOWN_HOSTS_FILE,
2796
                    constants.RAPI_CERT_FILE,
2797
                    constants.RAPI_USERS_FILE,
2798
                    constants.CONFD_HMAC_KEY,
2799
                    constants.CLUSTER_DOMAIN_SECRET_FILE,
2800
                   ])
2801

    
2802
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2803
  for hv_name in enabled_hypervisors:
2804
    hv_class = hypervisor.GetHypervisor(hv_name)
2805
    dist_files.update(hv_class.GetAncillaryFiles())
2806

    
2807
  # 3. Perform the files upload
2808
  for fname in dist_files:
2809
    if os.path.exists(fname):
2810
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2811
      for to_node, to_result in result.items():
2812
        msg = to_result.fail_msg
2813
        if msg:
2814
          msg = ("Copy of file %s to node %s failed: %s" %
2815
                 (fname, to_node, msg))
2816
          lu.proc.LogWarning(msg)
2817

    
2818

    
2819
class LURedistributeConfig(NoHooksLU):
2820
  """Force the redistribution of cluster configuration.
2821

2822
  This is a very simple LU.
2823

2824
  """
2825
  REQ_BGL = False
2826

    
2827
  def ExpandNames(self):
2828
    self.needed_locks = {
2829
      locking.LEVEL_NODE: locking.ALL_SET,
2830
    }
2831
    self.share_locks[locking.LEVEL_NODE] = 1
2832

    
2833
  def Exec(self, feedback_fn):
2834
    """Redistribute the configuration.
2835

2836
    """
2837
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2838
    _RedistributeAncillaryFiles(self)
2839

    
2840

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

2844
  """
2845
  if not instance.disks or disks is not None and not disks:
2846
    return True
2847

    
2848
  disks = _ExpandCheckDisks(instance, disks)
2849

    
2850
  if not oneshot:
2851
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2852

    
2853
  node = instance.primary_node
2854

    
2855
  for dev in disks:
2856
    lu.cfg.SetDiskID(dev, node)
2857

    
2858
  # TODO: Convert to utils.Retry
2859

    
2860
  retries = 0
2861
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2862
  while True:
2863
    max_time = 0
2864
    done = True
2865
    cumul_degraded = False
2866
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, disks)
2867
    msg = rstats.fail_msg
2868
    if msg:
2869
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2870
      retries += 1
2871
      if retries >= 10:
2872
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2873
                                 " aborting." % node)
2874
      time.sleep(6)
2875
      continue
2876
    rstats = rstats.payload
2877
    retries = 0
2878
    for i, mstat in enumerate(rstats):
2879
      if mstat is None:
2880
        lu.LogWarning("Can't compute data for node %s/%s",
2881
                           node, disks[i].iv_name)
2882
        continue
2883

    
2884
      cumul_degraded = (cumul_degraded or
2885
                        (mstat.is_degraded and mstat.sync_percent is None))
2886
      if mstat.sync_percent is not None:
2887
        done = False
2888
        if mstat.estimated_time is not None:
2889
          rem_time = ("%s remaining (estimated)" %
2890
                      utils.FormatSeconds(mstat.estimated_time))
2891
          max_time = mstat.estimated_time
2892
        else:
2893
          rem_time = "no time estimate"
2894
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2895
                        (disks[i].iv_name, mstat.sync_percent, rem_time))
2896

    
2897
    # if we're done but degraded, let's do a few small retries, to
2898
    # make sure we see a stable and not transient situation; therefore
2899
    # we force restart of the loop
2900
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2901
      logging.info("Degraded disks found, %d retries left", degr_retries)
2902
      degr_retries -= 1
2903
      time.sleep(1)
2904
      continue
2905

    
2906
    if done or oneshot:
2907
      break
2908

    
2909
    time.sleep(min(60, max_time))
2910

    
2911
  if done:
2912
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2913
  return not cumul_degraded
2914

    
2915

    
2916
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2917
  """Check that mirrors are not degraded.
2918

2919
  The ldisk parameter, if True, will change the test from the
2920
  is_degraded attribute (which represents overall non-ok status for
2921
  the device(s)) to the ldisk (representing the local storage status).
2922

2923
  """
2924
  lu.cfg.SetDiskID(dev, node)
2925

    
2926
  result = True
2927

    
2928
  if on_primary or dev.AssembleOnSecondary():
2929
    rstats = lu.rpc.call_blockdev_find(node, dev)
2930
    msg = rstats.fail_msg
2931
    if msg:
2932
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2933
      result = False
2934
    elif not rstats.payload:
2935
      lu.LogWarning("Can't find disk on node %s", node)
2936
      result = False
2937
    else:
2938
      if ldisk:
2939
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2940
      else:
2941
        result = result and not rstats.payload.is_degraded
2942

    
2943
  if dev.children:
2944
    for child in dev.children:
2945
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2946

    
2947
  return result
2948

    
2949

    
2950
class LUDiagnoseOS(NoHooksLU):
2951
  """Logical unit for OS diagnose/query.
2952

2953
  """
2954
  _OP_PARAMS = [
2955
    _POutputFields,
2956
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
2957
    ]
2958
  REQ_BGL = False
2959
  _HID = "hidden"
2960
  _BLK = "blacklisted"
2961
  _VLD = "valid"
2962
  _FIELDS_STATIC = utils.FieldSet()
2963
  _FIELDS_DYNAMIC = utils.FieldSet("name", _VLD, "node_status", "variants",
2964
                                   "parameters", "api_versions", _HID, _BLK)
2965

    
2966
  def CheckArguments(self):
2967
    if self.op.names:
2968
      raise errors.OpPrereqError("Selective OS query not supported",
2969
                                 errors.ECODE_INVAL)
2970

    
2971
    _CheckOutputFields(static=self._FIELDS_STATIC,
2972
                       dynamic=self._FIELDS_DYNAMIC,
2973
                       selected=self.op.output_fields)
2974

    
2975
  def ExpandNames(self):
2976
    # Lock all nodes, in shared mode
2977
    # Temporary removal of locks, should be reverted later
2978
    # TODO: reintroduce locks when they are lighter-weight
2979
    self.needed_locks = {}
2980
    #self.share_locks[locking.LEVEL_NODE] = 1
2981
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2982

    
2983
  @staticmethod
2984
  def _DiagnoseByOS(rlist):
2985
    """Remaps a per-node return list into an a per-os per-node dictionary
2986

2987
    @param rlist: a map with node names as keys and OS objects as values
2988

2989
    @rtype: dict
2990
    @return: a dictionary with osnames as keys and as value another
2991
        map, with nodes as keys and tuples of (path, status, diagnose,
2992
        variants, parameters, api_versions) as values, eg::
2993

2994
          {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
2995
                                     (/srv/..., False, "invalid api")],
2996
                           "node2": [(/srv/..., True, "", [], [])]}
2997
          }
2998

2999
    """
3000
    all_os = {}
3001
    # we build here the list of nodes that didn't fail the RPC (at RPC
3002
    # level), so that nodes with a non-responding node daemon don't
3003
    # make all OSes invalid
3004
    good_nodes = [node_name for node_name in rlist
3005
                  if not rlist[node_name].fail_msg]
3006
    for node_name, nr in rlist.items():
3007
      if nr.fail_msg or not nr.payload:
3008
        continue
3009
      for (name, path, status, diagnose, variants,
3010
           params, api_versions) in nr.payload:
3011
        if name not in all_os:
3012
          # build a list of nodes for this os containing empty lists
3013
          # for each node in node_list
3014
          all_os[name] = {}
3015
          for nname in good_nodes:
3016
            all_os[name][nname] = []
3017
        # convert params from [name, help] to (name, help)
3018
        params = [tuple(v) for v in params]
3019
        all_os[name][node_name].append((path, status, diagnose,
3020
                                        variants, params, api_versions))
3021
    return all_os
3022

    
3023
  def Exec(self, feedback_fn):
3024
    """Compute the list of OSes.
3025

3026
    """
3027
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
3028
    node_data = self.rpc.call_os_diagnose(valid_nodes)
3029
    pol = self._DiagnoseByOS(node_data)
3030
    output = []
3031
    cluster = self.cfg.GetClusterInfo()
3032

    
3033
    for os_name in utils.NiceSort(pol.keys()):
3034
      os_data = pol[os_name]
3035
      row = []
3036
      valid = True
3037
      (variants, params, api_versions) = null_state = (set(), set(), set())
3038
      for idx, osl in enumerate(os_data.values()):
3039
        valid = bool(valid and osl and osl[0][1])
3040
        if not valid:
3041
          (variants, params, api_versions) = null_state
3042
          break
3043
        node_variants, node_params, node_api = osl[0][3:6]
3044
        if idx == 0: # first entry
3045
          variants = set(node_variants)
3046
          params = set(node_params)
3047
          api_versions = set(node_api)
3048
        else: # keep consistency
3049
          variants.intersection_update(node_variants)
3050
          params.intersection_update(node_params)
3051
          api_versions.intersection_update(node_api)
3052

    
3053
      is_hid = os_name in cluster.hidden_os
3054
      is_blk = os_name in cluster.blacklisted_os
3055
      if ((self._HID not in self.op.output_fields and is_hid) or
3056
          (self._BLK not in self.op.output_fields and is_blk) or
3057
          (self._VLD not in self.op.output_fields and not valid)):
3058
        continue
3059

    
3060
      for field in self.op.output_fields:
3061
        if field == "name":
3062
          val = os_name
3063
        elif field == self._VLD:
3064
          val = valid
3065
        elif field == "node_status":
3066
          # this is just a copy of the dict
3067
          val = {}
3068
          for node_name, nos_list in os_data.items():
3069
            val[node_name] = nos_list
3070
        elif field == "variants":
3071
          val = utils.NiceSort(list(variants))
3072
        elif field == "parameters":
3073
          val = list(params)
3074
        elif field == "api_versions":
3075
          val = list(api_versions)
3076
        elif field == self._HID:
3077
          val = is_hid
3078
        elif field == self._BLK:
3079
          val = is_blk
3080
        else:
3081
          raise errors.ParameterError(field)
3082
        row.append(val)
3083
      output.append(row)
3084

    
3085
    return output
3086

    
3087

    
3088
class LURemoveNode(LogicalUnit):
3089
  """Logical unit for removing a node.
3090

3091
  """
3092
  HPATH = "node-remove"
3093
  HTYPE = constants.HTYPE_NODE
3094
  _OP_PARAMS = [
3095
    _PNodeName,
3096
    ]
3097

    
3098
  def BuildHooksEnv(self):
3099
    """Build hooks env.
3100

3101
    This doesn't run on the target node in the pre phase as a failed
3102
    node would then be impossible to remove.
3103

3104
    """
3105
    env = {
3106
      "OP_TARGET": self.op.node_name,
3107
      "NODE_NAME": self.op.node_name,
3108
      }
3109
    all_nodes = self.cfg.GetNodeList()
3110
    try:
3111
      all_nodes.remove(self.op.node_name)
3112
    except ValueError:
3113
      logging.warning("Node %s which is about to be removed not found"
3114
                      " in the all nodes list", self.op.node_name)
3115
    return env, all_nodes, all_nodes
3116

    
3117
  def CheckPrereq(self):
3118
    """Check prerequisites.
3119

3120
    This checks:
3121
     - the node exists in the configuration
3122
     - it does not have primary or secondary instances
3123
     - it's not the master
3124

3125
    Any errors are signaled by raising errors.OpPrereqError.
3126

3127
    """
3128
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3129
    node = self.cfg.GetNodeInfo(self.op.node_name)
3130
    assert node is not None
3131

    
3132
    instance_list = self.cfg.GetInstanceList()
3133

    
3134
    masternode = self.cfg.GetMasterNode()
3135
    if node.name == masternode:
3136
      raise errors.OpPrereqError("Node is the master node,"
3137
                                 " you need to failover first.",
3138
                                 errors.ECODE_INVAL)
3139

    
3140
    for instance_name in instance_list:
3141
      instance = self.cfg.GetInstanceInfo(instance_name)
3142
      if node.name in instance.all_nodes:
3143
        raise errors.OpPrereqError("Instance %s is still running on the node,"
3144
                                   " please remove first." % instance_name,
3145
                                   errors.ECODE_INVAL)
3146
    self.op.node_name = node.name
3147
    self.node = node
3148

    
3149
  def Exec(self, feedback_fn):
3150
    """Removes the node from the cluster.
3151

3152
    """
3153
    node = self.node
3154
    logging.info("Stopping the node daemon and removing configs from node %s",
3155
                 node.name)
3156

    
3157
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3158

    
3159
    # Promote nodes to master candidate as needed
3160
    _AdjustCandidatePool(self, exceptions=[node.name])
3161
    self.context.RemoveNode(node.name)
3162

    
3163
    # Run post hooks on the node before it's removed
3164
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
3165
    try:
3166
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
3167
    except:
3168
      # pylint: disable-msg=W0702
3169
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
3170

    
3171
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
3172
    msg = result.fail_msg
3173
    if msg:
3174
      self.LogWarning("Errors encountered on the remote node while leaving"
3175
                      " the cluster: %s", msg)
3176

    
3177
    # Remove node from our /etc/hosts
3178
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3179
      master_node = self.cfg.GetMasterNode()
3180
      result = self.rpc.call_etc_hosts_modify(master_node,
3181
                                              constants.ETC_HOSTS_REMOVE,
3182
                                              node.name, None)
3183
      result.Raise("Can't update hosts file with new host data")
3184
      _RedistributeAncillaryFiles(self)
3185

    
3186

    
3187
class LUQueryNodes(NoHooksLU):
3188
  """Logical unit for querying nodes.
3189

3190
  """
3191
  # pylint: disable-msg=W0142
3192
  _OP_PARAMS = [
3193
    _POutputFields,
3194
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
3195
    ("use_locking", False, ht.TBool),
3196
    ]
3197
  REQ_BGL = False
3198

    
3199
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
3200
                    "master_candidate", "offline", "drained"]
3201

    
3202
  _FIELDS_DYNAMIC = utils.FieldSet(
3203
    "dtotal", "dfree",
3204
    "mtotal", "mnode", "mfree",
3205
    "bootid",
3206
    "ctotal", "cnodes", "csockets",
3207
    )
3208

    
3209
  _FIELDS_STATIC = utils.FieldSet(*[
3210
    "pinst_cnt", "sinst_cnt",
3211
    "pinst_list", "sinst_list",
3212
    "pip", "sip", "tags",
3213
    "master",
3214
    "role"] + _SIMPLE_FIELDS
3215
    )
3216

    
3217
  def CheckArguments(self):
3218
    _CheckOutputFields(static=self._FIELDS_STATIC,
3219
                       dynamic=self._FIELDS_DYNAMIC,
3220
                       selected=self.op.output_fields)
3221

    
3222
  def ExpandNames(self):
3223
    self.needed_locks = {}
3224
    self.share_locks[locking.LEVEL_NODE] = 1
3225

    
3226
    if self.op.names:
3227
      self.wanted = _GetWantedNodes(self, self.op.names)
3228
    else:
3229
      self.wanted = locking.ALL_SET
3230

    
3231
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3232
    self.do_locking = self.do_node_query and self.op.use_locking
3233
    if self.do_locking:
3234
      # if we don't request only static fields, we need to lock the nodes
3235
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
3236

    
3237
  def Exec(self, feedback_fn):
3238
    """Computes the list of nodes and their attributes.
3239

3240
    """
3241
    all_info = self.cfg.GetAllNodesInfo()
3242
    if self.do_locking:
3243
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
3244
    elif self.wanted != locking.ALL_SET:
3245
      nodenames = self.wanted
3246
      missing = set(nodenames).difference(all_info.keys())
3247
      if missing:
3248
        raise errors.OpExecError(
3249
          "Some nodes were removed before retrieving their data: %s" % missing)
3250
    else:
3251
      nodenames = all_info.keys()
3252

    
3253
    nodenames = utils.NiceSort(nodenames)
3254
    nodelist = [all_info[name] for name in nodenames]
3255

    
3256
    # begin data gathering
3257

    
3258
    if self.do_node_query:
3259
      live_data = {}
3260
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
3261
                                          self.cfg.GetHypervisorType())
3262
      for name in nodenames:
3263
        nodeinfo = node_data[name]
3264
        if not nodeinfo.fail_msg and nodeinfo.payload:
3265
          nodeinfo = nodeinfo.payload
3266
          fn = utils.TryConvert
3267
          live_data[name] = {
3268
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
3269
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
3270
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
3271
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
3272
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
3273
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
3274
            "bootid": nodeinfo.get('bootid', None),
3275
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
3276
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
3277
            }
3278
        else:
3279
          live_data[name] = {}
3280
    else:
3281
      live_data = dict.fromkeys(nodenames, {})
3282

    
3283
    node_to_primary = dict([(name, set()) for name in nodenames])
3284
    node_to_secondary = dict([(name, set()) for name in nodenames])
3285

    
3286
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
3287
                             "sinst_cnt", "sinst_list"))
3288
    if inst_fields & frozenset(self.op.output_fields):
3289
      inst_data = self.cfg.GetAllInstancesInfo()
3290

    
3291
      for inst in inst_data.values():
3292
        if inst.primary_node in node_to_primary:
3293
          node_to_primary[inst.primary_node].add(inst.name)
3294
        for secnode in inst.secondary_nodes:
3295
          if secnode in node_to_secondary:
3296
            node_to_secondary[secnode].add(inst.name)
3297

    
3298
    master_node = self.cfg.GetMasterNode()
3299

    
3300
    # end data gathering
3301

    
3302
    output = []
3303
    for node in nodelist:
3304
      node_output = []
3305
      for field in self.op.output_fields:
3306
        if field in self._SIMPLE_FIELDS:
3307
          val = getattr(node, field)
3308
        elif field == "pinst_list":
3309
          val = list(node_to_primary[node.name])
3310
        elif field == "sinst_list":
3311
          val = list(node_to_secondary[node.name])
3312
        elif field == "pinst_cnt":
3313
          val = len(node_to_primary[node.name])
3314
        elif field == "sinst_cnt":
3315
          val = len(node_to_secondary[node.name])
3316
        elif field == "pip":
3317
          val = node.primary_ip
3318
        elif field == "sip":
3319
          val = node.secondary_ip
3320
        elif field == "tags":
3321
          val = list(node.GetTags())
3322
        elif field == "master":
3323
          val = node.name == master_node
3324
        elif self._FIELDS_DYNAMIC.Matches(field):
3325
          val = live_data[node.name].get(field, None)
3326
        elif field == "role":
3327
          if node.name == master_node:
3328
            val = "M"
3329
          elif node.master_candidate:
3330
            val = "C"
3331
          elif node.drained:
3332
            val = "D"
3333
          elif node.offline:
3334
            val = "O"
3335
          else:
3336
            val = "R"
3337
        else:
3338
          raise errors.ParameterError(field)
3339
        node_output.append(val)
3340
      output.append(node_output)
3341

    
3342
    return output
3343

    
3344

    
3345
class LUQueryNodeVolumes(NoHooksLU):
3346
  """Logical unit for getting volumes on node(s).
3347

3348
  """
3349
  _OP_PARAMS = [
3350
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
3351
    ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString)),
3352
    ]
3353
  REQ_BGL = False
3354
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3355
  _FIELDS_STATIC = utils.FieldSet("node")
3356

    
3357
  def CheckArguments(self):
3358
    _CheckOutputFields(static=self._FIELDS_STATIC,
3359
                       dynamic=self._FIELDS_DYNAMIC,
3360
                       selected=self.op.output_fields)
3361

    
3362
  def ExpandNames(self):
3363
    self.needed_locks = {}
3364
    self.share_locks[locking.LEVEL_NODE] = 1
3365
    if not self.op.nodes:
3366
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3367
    else:
3368
      self.needed_locks[locking.LEVEL_NODE] = \
3369
        _GetWantedNodes(self, self.op.nodes)
3370

    
3371
  def Exec(self, feedback_fn):
3372
    """Computes the list of nodes and their attributes.
3373

3374
    """
3375
    nodenames = self.acquired_locks[locking.LEVEL_NODE]
3376
    volumes = self.rpc.call_node_volumes(nodenames)
3377

    
3378
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3379
             in self.cfg.GetInstanceList()]
3380

    
3381
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3382

    
3383
    output = []
3384
    for node in nodenames:
3385
      nresult = volumes[node]
3386
      if nresult.offline:
3387
        continue
3388
      msg = nresult.fail_msg
3389
      if msg:
3390
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3391
        continue
3392

    
3393
      node_vols = nresult.payload[:]
3394
      node_vols.sort(key=lambda vol: vol['dev'])
3395

    
3396
      for vol in node_vols:
3397
        node_output = []
3398
        for field in self.op.output_fields:
3399
          if field == "node":
3400
            val = node
3401
          elif field == "phys":
3402
            val = vol['dev']
3403
          elif field == "vg":
3404
            val = vol['vg']
3405
          elif field == "name":
3406
            val = vol['name']
3407
          elif field == "size":
3408
            val = int(float(vol['size']))
3409
          elif field == "instance":
3410
            for inst in ilist:
3411
              if node not in lv_by_node[inst]:
3412
                continue
3413
              if vol['name'] in lv_by_node[inst][node]:
3414
                val = inst.name
3415
                break
3416
            else:
3417
              val = '-'
3418
          else:
3419
            raise errors.ParameterError(field)
3420
          node_output.append(str(val))
3421

    
3422
        output.append(node_output)
3423

    
3424
    return output
3425

    
3426

    
3427
class LUQueryNodeStorage(NoHooksLU):
3428
  """Logical unit for getting information on storage units on node(s).
3429

3430
  """
3431
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3432
  _OP_PARAMS = [
3433
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
3434
    ("storage_type", ht.NoDefault, _CheckStorageType),
3435
    ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString)),
3436
    ("name", None, ht.TMaybeString),
3437
    ]
3438
  REQ_BGL = False
3439

    
3440
  def CheckArguments(self):
3441
    _CheckOutputFields(static=self._FIELDS_STATIC,
3442
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3443
                       selected=self.op.output_fields)
3444

    
3445
  def ExpandNames(self):
3446
    self.needed_locks = {}
3447
    self.share_locks[locking.LEVEL_NODE] = 1
3448

    
3449
    if self.op.nodes:
3450
      self.needed_locks[locking.LEVEL_NODE] = \
3451
        _GetWantedNodes(self, self.op.nodes)
3452
    else:
3453
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3454

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

3458
    """
3459
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3460

    
3461
    # Always get name to sort by
3462
    if constants.SF_NAME in self.op.output_fields:
3463
      fields = self.op.output_fields[:]
3464
    else:
3465
      fields = [constants.SF_NAME] + self.op.output_fields
3466

    
3467
    # Never ask for node or type as it's only known to the LU
3468
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3469
      while extra in fields:
3470
        fields.remove(extra)
3471

    
3472
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3473
    name_idx = field_idx[constants.SF_NAME]
3474

    
3475
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3476
    data = self.rpc.call_storage_list(self.nodes,
3477
                                      self.op.storage_type, st_args,
3478
                                      self.op.name, fields)
3479

    
3480
    result = []
3481

    
3482
    for node in utils.NiceSort(self.nodes):
3483
      nresult = data[node]
3484
      if nresult.offline:
3485
        continue
3486

    
3487
      msg = nresult.fail_msg
3488
      if msg:
3489
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3490
        continue
3491

    
3492
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3493

    
3494
      for name in utils.NiceSort(rows.keys()):
3495
        row = rows[name]
3496

    
3497
        out = []
3498

    
3499
        for field in self.op.output_fields:
3500
          if field == constants.SF_NODE:
3501
            val = node
3502
          elif field == constants.SF_TYPE:
3503
            val = self.op.storage_type
3504
          elif field in field_idx:
3505
            val = row[field_idx[field]]
3506
          else:
3507
            raise errors.ParameterError(field)
3508

    
3509
          out.append(val)
3510

    
3511
        result.append(out)
3512

    
3513
    return result
3514

    
3515

    
3516
class LUModifyNodeStorage(NoHooksLU):
3517
  """Logical unit for modifying a storage volume on a node.
3518

3519
  """
3520
  _OP_PARAMS = [
3521
    _PNodeName,
3522
    ("storage_type", ht.NoDefault, _CheckStorageType),
3523
    ("name", ht.NoDefault, ht.TNonEmptyString),
3524
    ("changes", ht.NoDefault, ht.TDict),
3525
    ]
3526
  REQ_BGL = False
3527

    
3528
  def CheckArguments(self):
3529
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3530

    
3531
    storage_type = self.op.storage_type
3532

    
3533
    try:
3534
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3535
    except KeyError:
3536
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3537
                                 " modified" % storage_type,
3538
                                 errors.ECODE_INVAL)
3539

    
3540
    diff = set(self.op.changes.keys()) - modifiable
3541
    if diff:
3542
      raise errors.OpPrereqError("The following fields can not be modified for"
3543
                                 " storage units of type '%s': %r" %
3544
                                 (storage_type, list(diff)),
3545
                                 errors.ECODE_INVAL)
3546

    
3547
  def ExpandNames(self):
3548
    self.needed_locks = {
3549
      locking.LEVEL_NODE: self.op.node_name,
3550
      }
3551

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

3555
    """
3556
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3557
    result = self.rpc.call_storage_modify(self.op.node_name,
3558
                                          self.op.storage_type, st_args,
3559
                                          self.op.name, self.op.changes)
3560
    result.Raise("Failed to modify storage unit '%s' on %s" %
3561
                 (self.op.name, self.op.node_name))
3562

    
3563

    
3564
class LUAddNode(LogicalUnit):
3565
  """Logical unit for adding node to the cluster.
3566

3567
  """
3568
  HPATH = "node-add"
3569
  HTYPE = constants.HTYPE_NODE
3570
  _OP_PARAMS = [
3571
    _PNodeName,
3572
    ("primary_ip", None, ht.NoType),
3573
    ("secondary_ip", None, ht.TMaybeString),
3574
    ("readd", False, ht.TBool),
3575
    ("group", None, ht.TMaybeString)
3576
    ]
3577

    
3578
  def CheckArguments(self):
3579
    self.primary_ip_family = self.cfg.GetPrimaryIPFamily()
3580
    # validate/normalize the node name
3581
    self.hostname = netutils.GetHostname(name=self.op.node_name,
3582
                                         family=self.primary_ip_family)
3583
    self.op.node_name = self.hostname.name
3584
    if self.op.readd and self.op.group:
3585
      raise errors.OpPrereqError("Cannot pass a node group when a node is"
3586
                                 " being readded", errors.ECODE_INVAL)
3587

    
3588
  def BuildHooksEnv(self):
3589
    """Build hooks env.
3590

3591
    This will run on all nodes before, and on all nodes + the new node after.
3592

3593
    """
3594
    env = {
3595
      "OP_TARGET": self.op.node_name,
3596
      "NODE_NAME": self.op.node_name,
3597
      "NODE_PIP": self.op.primary_ip,
3598
      "NODE_SIP": self.op.secondary_ip,
3599
      }
3600
    nodes_0 = self.cfg.GetNodeList()
3601
    nodes_1 = nodes_0 + [self.op.node_name, ]
3602
    return env, nodes_0, nodes_1
3603

    
3604
  def CheckPrereq(self):
3605
    """Check prerequisites.
3606

3607
    This checks:
3608
     - the new node is not already in the config
3609
     - it is resolvable
3610
     - its parameters (single/dual homed) matches the cluster
3611

3612
    Any errors are signaled by raising errors.OpPrereqError.
3613

3614
    """
3615
    cfg = self.cfg
3616
    hostname = self.hostname
3617
    node = hostname.name
3618
    primary_ip = self.op.primary_ip = hostname.ip
3619
    if self.op.secondary_ip is None:
3620
      if self.primary_ip_family == netutils.IP6Address.family:
3621
        raise errors.OpPrereqError("When using a IPv6 primary address, a valid"
3622
                                   " IPv4 address must be given as secondary",
3623
                                   errors.ECODE_INVAL)
3624
      self.op.secondary_ip = primary_ip
3625

    
3626
    secondary_ip = self.op.secondary_ip
3627
    if not netutils.IP4Address.IsValid(secondary_ip):
3628
      raise errors.OpPrereqError("Secondary IP (%s) needs to be a valid IPv4"
3629
                                 " address" % secondary_ip, errors.ECODE_INVAL)
3630

    
3631
    node_list = cfg.GetNodeList()
3632
    if not self.op.readd and node in node_list:
3633
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3634
                                 node, errors.ECODE_EXISTS)
3635
    elif self.op.readd and node not in node_list:
3636
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3637
                                 errors.ECODE_NOENT)
3638

    
3639
    self.changed_primary_ip = False
3640

    
3641
    for existing_node_name in node_list:
3642
      existing_node = cfg.GetNodeInfo(existing_node_name)
3643

    
3644
      if self.op.readd and node == existing_node_name:
3645
        if existing_node.secondary_ip != secondary_ip:
3646
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3647
                                     " address configuration as before",
3648
                                     errors.ECODE_INVAL)
3649
        if existing_node.primary_ip != primary_ip:
3650
          self.changed_primary_ip = True
3651

    
3652
        continue
3653

    
3654
      if (existing_node.primary_ip == primary_ip or
3655
          existing_node.secondary_ip == primary_ip or
3656
          existing_node.primary_ip == secondary_ip or
3657
          existing_node.secondary_ip == secondary_ip):
3658
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3659
                                   " existing node %s" % existing_node.name,
3660
                                   errors.ECODE_NOTUNIQUE)
3661

    
3662
    # check that the type of the node (single versus dual homed) is the
3663
    # same as for the master
3664
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
3665
    master_singlehomed = myself.secondary_ip == myself.primary_ip
3666
    newbie_singlehomed = secondary_ip == primary_ip
3667
    if master_singlehomed != newbie_singlehomed:
3668
      if master_singlehomed:
3669
        raise errors.OpPrereqError("The master has no private ip but the"
3670
                                   " new node has one",
3671
                                   errors.ECODE_INVAL)
3672
      else:
3673
        raise errors.OpPrereqError("The master has a private ip but the"
3674
                                   " new node doesn't have one",
3675
                                   errors.ECODE_INVAL)
3676

    
3677
    # checks reachability
3678
    if not netutils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3679
      raise errors.OpPrereqError("Node not reachable by ping",
3680
                                 errors.ECODE_ENVIRON)
3681

    
3682
    if not newbie_singlehomed:
3683
      # check reachability from my secondary ip to newbie's secondary ip
3684
      if not netutils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3685
                           source=myself.secondary_ip):
3686
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3687
                                   " based ping to noded port",
3688
                                   errors.ECODE_ENVIRON)
3689

    
3690
    if self.op.readd:
3691
      exceptions = [node]
3692
    else:
3693
      exceptions = []
3694

    
3695
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3696

    
3697
    if self.op.readd:
3698
      self.new_node = self.cfg.GetNodeInfo(node)
3699
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3700
    else:
3701
      node_group = cfg.LookupNodeGroup(self.op.group)
3702
      self.new_node = objects.Node(name=node,
3703
                                   primary_ip=primary_ip,
3704
                                   secondary_ip=secondary_ip,
3705
                                   master_candidate=self.master_candidate,
3706
                                   offline=False, drained=False,
3707
                                   group=node_group)
3708

    
3709
  def Exec(self, feedback_fn):
3710
    """Adds the new node to the cluster.
3711

3712
    """
3713
    new_node = self.new_node
3714
    node = new_node.name
3715

    
3716
    # for re-adds, reset the offline/drained/master-candidate flags;
3717
    # we need to reset here, otherwise offline would prevent RPC calls
3718
    # later in the procedure; this also means that if the re-add
3719
    # fails, we are left with a non-offlined, broken node
3720
    if self.op.readd:
3721
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
3722
      self.LogInfo("Readding a node, the offline/drained flags were reset")
3723
      # if we demote the node, we do cleanup later in the procedure
3724
      new_node.master_candidate = self.master_candidate
3725
      if self.changed_primary_ip:
3726
        new_node.primary_ip = self.op.primary_ip
3727

    
3728
    # notify the user about any possible mc promotion
3729
    if new_node.master_candidate:
3730
      self.LogInfo("Node will be a master candidate")
3731

    
3732
    # check connectivity
3733
    result = self.rpc.call_version([node])[node]
3734
    result.Raise("Can't get version information from node %s" % node)
3735
    if constants.PROTOCOL_VERSION == result.payload:
3736
      logging.info("Communication to node %s fine, sw version %s match",
3737
                   node, result.payload)
3738
    else:
3739
      raise errors.OpExecError("Version mismatch master version %s,"
3740
                               " node version %s" %
3741
                               (constants.PROTOCOL_VERSION, result.payload))
3742

    
3743
    # Add node to our /etc/hosts, and add key to known_hosts
3744
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3745
      master_node = self.cfg.GetMasterNode()
3746
      result = self.rpc.call_etc_hosts_modify(master_node,
3747
                                              constants.ETC_HOSTS_ADD,
3748
                                              self.hostname.name,
3749
                                              self.hostname.ip)
3750
      result.Raise("Can't update hosts file with new host data")
3751

    
3752
    if new_node.secondary_ip != new_node.primary_ip:
3753
      result = self.rpc.call_node_has_ip_address(new_node.name,
3754
                                                 new_node.secondary_ip)
3755
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3756
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3757
      if not result.payload:
3758
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3759
                                 " you gave (%s). Please fix and re-run this"
3760
                                 " command." % new_node.secondary_ip)
3761

    
3762
    node_verify_list = [self.cfg.GetMasterNode()]
3763
    node_verify_param = {
3764
      constants.NV_NODELIST: [node],
3765
      # TODO: do a node-net-test as well?
3766
    }
3767

    
3768
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3769
                                       self.cfg.GetClusterName())
3770
    for verifier in node_verify_list:
3771
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3772
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3773
      if nl_payload:
3774
        for failed in nl_payload:
3775
          feedback_fn("ssh/hostname verification failed"
3776
                      " (checking from %s): %s" %
3777
                      (verifier, nl_payload[failed]))
3778
        raise errors.OpExecError("ssh/hostname verification failed.")
3779

    
3780
    if self.op.readd:
3781
      _RedistributeAncillaryFiles(self)
3782
      self.context.ReaddNode(new_node)
3783
      # make sure we redistribute the config
3784
      self.cfg.Update(new_node, feedback_fn)
3785
      # and make sure the new node will not have old files around
3786
      if not new_node.master_candidate:
3787
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3788
        msg = result.fail_msg
3789
        if msg:
3790
          self.LogWarning("Node failed to demote itself from master"
3791
                          " candidate status: %s" % msg)
3792
    else:
3793
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3794
      self.context.AddNode(new_node, self.proc.GetECId())
3795

    
3796

    
3797
class LUSetNodeParams(LogicalUnit):
3798
  """Modifies the parameters of a node.
3799

3800
  @cvar _F2R: a dictionary from tuples of flags (mc, drained, offline)
3801
      to the node role (as _ROLE_*)
3802
  @cvar _R2F: a dictionary from node role to tuples of flags
3803
  @cvar _FLAGS: a list of attribute names corresponding to the flags
3804

3805
  """
3806
  HPATH = "node-modify"
3807
  HTYPE = constants.HTYPE_NODE
3808
  _OP_PARAMS = [
3809
    _PNodeName,
3810
    ("master_candidate", None, ht.TMaybeBool),
3811
    ("offline", None, ht.TMaybeBool),
3812
    ("drained", None, ht.TMaybeBool),
3813
    ("auto_promote", False, ht.TBool),
3814
    _PForce,
3815
    ]
3816
  REQ_BGL = False
3817
  (_ROLE_CANDIDATE, _ROLE_DRAINED, _ROLE_OFFLINE, _ROLE_REGULAR) = range(4)
3818
  _F2R = {
3819
    (True, False, False): _ROLE_CANDIDATE,
3820
    (False, True, False): _ROLE_DRAINED,
3821
    (False, False, True): _ROLE_OFFLINE,
3822
    (False, False, False): _ROLE_REGULAR,
3823
    }
3824
  _R2F = dict((v, k) for k, v in _F2R.items())
3825
  _FLAGS = ["master_candidate", "drained", "offline"]
3826

    
3827
  def CheckArguments(self):
3828
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3829
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3830
    if all_mods.count(None) == len(all_mods):
3831
      raise errors.OpPrereqError("Please pass at least one modification",
3832
                                 errors.ECODE_INVAL)
3833
    if all_mods.count(True) > 1:
3834
      raise errors.OpPrereqError("Can't set the node into more than one"
3835
                                 " state at the same time",
3836
                                 errors.ECODE_INVAL)
3837

    
3838
    # Boolean value that tells us whether we might be demoting from MC
3839
    self.might_demote = (self.op.master_candidate == False or
3840
                         self.op.offline == True or
3841
                         self.op.drained == True)
3842

    
3843
    self.lock_all = self.op.auto_promote and self.might_demote
3844

    
3845
  def ExpandNames(self):
3846
    if self.lock_all:
3847
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3848
    else:
3849
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3850

    
3851
  def BuildHooksEnv(self):
3852
    """Build hooks env.
3853

3854
    This runs on the master node.
3855

3856
    """
3857
    env = {
3858
      "OP_TARGET": self.op.node_name,
3859
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3860
      "OFFLINE": str(self.op.offline),
3861
      "DRAINED": str(self.op.drained),
3862
      }
3863
    nl = [self.cfg.GetMasterNode(),
3864
          self.op.node_name]
3865
    return env, nl, nl
3866

    
3867
  def CheckPrereq(self):
3868
    """Check prerequisites.
3869

3870
    This only checks the instance list against the existing names.
3871

3872
    """
3873
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3874

    
3875
    if (self.op.master_candidate is not None or
3876
        self.op.drained is not None or
3877
        self.op.offline is not None):
3878
      # we can't change the master's node flags
3879
      if self.op.node_name == self.cfg.GetMasterNode():
3880
        raise errors.OpPrereqError("The master role can be changed"
3881
                                   " only via master-failover",
3882
                                   errors.ECODE_INVAL)
3883

    
3884

    
3885
    if node.master_candidate and self.might_demote and not self.lock_all:
3886
      assert not self.op.auto_promote, "auto-promote set but lock_all not"
3887
      # check if after removing the current node, we're missing master
3888
      # candidates
3889
      (mc_remaining, mc_should, _) = \
3890
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
3891
      if mc_remaining < mc_should:
3892
        raise errors.OpPrereqError("Not enough master candidates, please"
3893
                                   " pass auto_promote to allow promotion",
3894
                                   errors.ECODE_STATE)
3895

    
3896
    self.old_flags = old_flags = (node.master_candidate,
3897
                                  node.drained, node.offline)
3898
    assert old_flags in self._F2R, "Un-handled old flags  %s" % str(old_flags)
3899
    self.old_role = self._F2R[old_flags]
3900

    
3901
    # Check for ineffective changes
3902
    for attr in self._FLAGS:
3903
      if (getattr(self.op, attr) == False and getattr(node, attr) == False):
3904
        self.LogInfo("Ignoring request to unset flag %s, already unset", attr)
3905
        setattr(self.op, attr, None)
3906

    
3907
    # Past this point, any flag change to False means a transition
3908
    # away from the respective state, as only real changes are kept
3909

    
3910
    # If we're being deofflined/drained, we'll MC ourself if needed
3911
    if self.op.drained == False or self.op.offline == False:
3912
      if _DecideSelfPromotion(self):
3913
        self.op.master_candidate = True
3914
        self.LogInfo("Auto-promoting node to master candidate")
3915

    
3916
  def Exec(self, feedback_fn):
3917
    """Modifies a node.
3918

3919
    """
3920
    node = self.node
3921
    old_role = self.old_role
3922

    
3923
    assert [getattr(self.op, attr) for attr in self._FLAGS].count(True) <= 1
3924

    
3925
    # compute new flags
3926
    if self.op.master_candidate:
3927
      new_role = self._ROLE_CANDIDATE
3928
    elif self.op.drained:
3929
      new_role = self._ROLE_DRAINED
3930
    elif self.op.offline:
3931
      new_role = self._ROLE_OFFLINE
3932
    elif False in [self.op.master_candidate, self.op.drained, self.op.offline]:
3933
      # False is still in new flags, which means we're un-setting (the
3934
      # only) True flag
3935
      new_role = self._ROLE_REGULAR
3936
    else: # no new flags, nothing, keep old role
3937
      new_role = old_role
3938

    
3939
    result = []
3940
    changed_mc = [old_role, new_role].count(self._ROLE_CANDIDATE) == 1
3941

    
3942
    # Tell the node to demote itself, if no longer MC and not offline
3943
    if (old_role == self._ROLE_CANDIDATE and
3944
        new_role != self._ROLE_OFFLINE and new_role != old_role):
3945
      msg = self.rpc.call_node_demote_from_mc(node.name).fail_msg
3946
      if msg:
3947
        self.LogWarning("Node failed to demote itself: %s", msg)
3948

    
3949
    new_flags = self._R2F[new_role]
3950
    for of, nf, desc in zip(self.old_flags, new_flags, self._FLAGS):
3951
      if of != nf:
3952
        result.append((desc, str(nf)))
3953
    (node.master_candidate, node.drained, node.offline) = new_flags
3954

    
3955
    # we locked all nodes, we adjust the CP before updating this node
3956
    if self.lock_all:
3957
      _AdjustCandidatePool(self, [node.name])
3958

    
3959
    # this will trigger configuration file update, if needed
3960
    self.cfg.Update(node, feedback_fn)
3961

    
3962
    # this will trigger job queue propagation or cleanup
3963
    if changed_mc:
3964
      self.context.ReaddNode(node)
3965

    
3966
    return result
3967

    
3968

    
3969
class LUPowercycleNode(NoHooksLU):
3970
  """Powercycles a node.
3971

3972
  """
3973
  _OP_PARAMS = [
3974
    _PNodeName,
3975
    _PForce,
3976
    ]
3977
  REQ_BGL = False
3978

    
3979
  def CheckArguments(self):
3980
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3981
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
3982
      raise errors.OpPrereqError("The node is the master and the force"
3983
                                 " parameter was not set",
3984
                                 errors.ECODE_INVAL)
3985

    
3986
  def ExpandNames(self):
3987
    """Locking for PowercycleNode.
3988

3989
    This is a last-resort option and shouldn't block on other
3990
    jobs. Therefore, we grab no locks.
3991

3992
    """
3993
    self.needed_locks = {}
3994

    
3995
  def Exec(self, feedback_fn):
3996
    """Reboots a node.
3997

3998
    """
3999
    result = self.rpc.call_node_powercycle(self.op.node_name,
4000
                                           self.cfg.GetHypervisorType())
4001
    result.Raise("Failed to schedule the reboot")
4002
    return result.payload
4003

    
4004

    
4005
class LUQueryClusterInfo(NoHooksLU):
4006
  """Query cluster configuration.
4007

4008
  """
4009
  REQ_BGL = False
4010

    
4011
  def ExpandNames(self):
4012
    self.needed_locks = {}
4013

    
4014
  def Exec(self, feedback_fn):
4015
    """Return cluster config.
4016

4017
    """
4018
    cluster = self.cfg.GetClusterInfo()
4019
    os_hvp = {}
4020

    
4021
    # Filter just for enabled hypervisors
4022
    for os_name, hv_dict in cluster.os_hvp.items():
4023
      os_hvp[os_name] = {}
4024
      for hv_name, hv_params in hv_dict.items():
4025
        if hv_name in cluster.enabled_hypervisors:
4026
          os_hvp[os_name][hv_name] = hv_params
4027

    
4028
    # Convert ip_family to ip_version
4029
    primary_ip_version = constants.IP4_VERSION
4030
    if cluster.primary_ip_family == netutils.IP6Address.family:
4031
      primary_ip_version = constants.IP6_VERSION
4032

    
4033
    result = {
4034
      "software_version": constants.RELEASE_VERSION,
4035
      "protocol_version": constants.PROTOCOL_VERSION,
4036
      "config_version": constants.CONFIG_VERSION,
4037
      "os_api_version": max(constants.OS_API_VERSIONS),
4038
      "export_version": constants.EXPORT_VERSION,
4039
      "architecture": (platform.architecture()[0], platform.machine()),
4040
      "name": cluster.cluster_name,
4041
      "master": cluster.master_node,
4042
      "default_hypervisor": cluster.enabled_hypervisors[0],
4043
      "enabled_hypervisors": cluster.enabled_hypervisors,
4044
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
4045
                        for hypervisor_name in cluster.enabled_hypervisors]),
4046
      "os_hvp": os_hvp,
4047
      "beparams": cluster.beparams,