Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 73e0328b

History | View | Annotate | Download (349.2 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008 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
25

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

    
29
import os
30
import os.path
31
import time
32
import re
33
import platform
34
import logging
35
import copy
36
import OpenSSL
37

    
38
from ganeti import ssh
39
from ganeti import utils
40
from ganeti import errors
41
from ganeti import hypervisor
42
from ganeti import locking
43
from ganeti import constants
44
from ganeti import objects
45
from ganeti import serializer
46
from ganeti import ssconf
47
from ganeti import uidpool
48
from ganeti import compat
49
from ganeti import masterd
50

    
51
import ganeti.masterd.instance # pylint: disable-msg=W0611
52

    
53

    
54
class LogicalUnit(object):
55
  """Logical Unit base class.
56

57
  Subclasses must follow these rules:
58
    - implement ExpandNames
59
    - implement CheckPrereq (except when tasklets are used)
60
    - implement Exec (except when tasklets are used)
61
    - implement BuildHooksEnv
62
    - redefine HPATH and HTYPE
63
    - optionally redefine their run requirements:
64
        REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
65

66
  Note that all commands require root permissions.
67

68
  @ivar dry_run_result: the value (if any) that will be returned to the caller
69
      in dry-run mode (signalled by opcode dry_run parameter)
70

71
  """
72
  HPATH = None
73
  HTYPE = None
74
  _OP_REQP = []
75
  REQ_BGL = True
76

    
77
  def __init__(self, processor, op, context, rpc):
78
    """Constructor for LogicalUnit.
79

80
    This needs to be overridden in derived classes in order to check op
81
    validity.
82

83
    """
84
    self.proc = processor
85
    self.op = op
86
    self.cfg = context.cfg
87
    self.context = context
88
    self.rpc = rpc
89
    # Dicts used to declare locking needs to mcpu
90
    self.needed_locks = None
91
    self.acquired_locks = {}
92
    self.share_locks = dict.fromkeys(locking.LEVELS, 0)
93
    self.add_locks = {}
94
    self.remove_locks = {}
95
    # Used to force good behavior when calling helper functions
96
    self.recalculate_locks = {}
97
    self.__ssh = None
98
    # logging
99
    self.LogWarning = processor.LogWarning # pylint: disable-msg=C0103
100
    self.LogInfo = processor.LogInfo # pylint: disable-msg=C0103
101
    self.LogStep = processor.LogStep # pylint: disable-msg=C0103
102
    # support for dry-run
103
    self.dry_run_result = None
104
    # support for generic debug attribute
105
    if (not hasattr(self.op, "debug_level") or
106
        not isinstance(self.op.debug_level, int)):
107
      self.op.debug_level = 0
108

    
109
    # Tasklets
110
    self.tasklets = None
111

    
112
    for attr_name in self._OP_REQP:
113
      attr_val = getattr(op, attr_name, None)
114
      if attr_val is None:
115
        raise errors.OpPrereqError("Required parameter '%s' missing" %
116
                                   attr_name, errors.ECODE_INVAL)
117

    
118
    self.CheckArguments()
119

    
120
  def __GetSSH(self):
121
    """Returns the SshRunner object
122

123
    """
124
    if not self.__ssh:
125
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
126
    return self.__ssh
127

    
128
  ssh = property(fget=__GetSSH)
129

    
130
  def CheckArguments(self):
131
    """Check syntactic validity for the opcode arguments.
132

133
    This method is for doing a simple syntactic check and ensure
134
    validity of opcode parameters, without any cluster-related
135
    checks. While the same can be accomplished in ExpandNames and/or
136
    CheckPrereq, doing these separate is better because:
137

138
      - ExpandNames is left as as purely a lock-related function
139
      - CheckPrereq is run after we have acquired locks (and possible
140
        waited for them)
141

142
    The function is allowed to change the self.op attribute so that
143
    later methods can no longer worry about missing parameters.
144

145
    """
146
    pass
147

    
148
  def ExpandNames(self):
149
    """Expand names for this LU.
150

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

156
    LUs which implement this method must also populate the self.needed_locks
157
    member, as a dict with lock levels as keys, and a list of needed lock names
158
    as values. Rules:
159

160
      - use an empty dict if you don't need any lock
161
      - if you don't need any lock at a particular level omit that level
162
      - don't put anything for the BGL level
163
      - if you want all locks at a level use locking.ALL_SET as a value
164

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

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

173
    Examples::
174

175
      # Acquire all nodes and one instance
176
      self.needed_locks = {
177
        locking.LEVEL_NODE: locking.ALL_SET,
178
        locking.LEVEL_INSTANCE: ['instance1.example.tld'],
179
      }
180
      # Acquire just two nodes
181
      self.needed_locks = {
182
        locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
183
      }
184
      # Acquire no locks
185
      self.needed_locks = {} # No, you can't leave it to the default value None
186

187
    """
188
    # The implementation of this method is mandatory only if the new LU is
189
    # concurrent, so that old LUs don't need to be changed all at the same
190
    # time.
191
    if self.REQ_BGL:
192
      self.needed_locks = {} # Exclusive LUs don't need locks.
193
    else:
194
      raise NotImplementedError
195

    
196
  def DeclareLocks(self, level):
197
    """Declare LU locking needs for a level
198

199
    While most LUs can just declare their locking needs at ExpandNames time,
200
    sometimes there's the need to calculate some locks after having acquired
201
    the ones before. This function is called just before acquiring locks at a
202
    particular level, but after acquiring the ones at lower levels, and permits
203
    such calculations. It can be used to modify self.needed_locks, and by
204
    default it does nothing.
205

206
    This function is only called if you have something already set in
207
    self.needed_locks for the level.
208

209
    @param level: Locking level which is going to be locked
210
    @type level: member of ganeti.locking.LEVELS
211

212
    """
213

    
214
  def CheckPrereq(self):
215
    """Check prerequisites for this LU.
216

217
    This method should check that the prerequisites for the execution
218
    of this LU are fulfilled. It can do internode communication, but
219
    it should be idempotent - no cluster or system changes are
220
    allowed.
221

222
    The method should raise errors.OpPrereqError in case something is
223
    not fulfilled. Its return value is ignored.
224

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

228
    """
229
    if self.tasklets is not None:
230
      for (idx, tl) in enumerate(self.tasklets):
231
        logging.debug("Checking prerequisites for tasklet %s/%s",
232
                      idx + 1, len(self.tasklets))
233
        tl.CheckPrereq()
234
    else:
235
      raise NotImplementedError
236

    
237
  def Exec(self, feedback_fn):
238
    """Execute the LU.
239

240
    This method should implement the actual work. It should raise
241
    errors.OpExecError for failures that are somewhat dealt with in
242
    code, or expected.
243

244
    """
245
    if self.tasklets is not None:
246
      for (idx, tl) in enumerate(self.tasklets):
247
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
248
        tl.Exec(feedback_fn)
249
    else:
250
      raise NotImplementedError
251

    
252
  def BuildHooksEnv(self):
253
    """Build hooks environment for this LU.
254

255
    This method should return a three-node tuple consisting of: a dict
256
    containing the environment that will be used for running the
257
    specific hook for this LU, a list of node names on which the hook
258
    should run before the execution, and a list of node names on which
259
    the hook should run after the execution.
260

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

266
    No nodes should be returned as an empty list (and not None).
267

268
    Note that if the HPATH for a LU class is None, this function will
269
    not be called.
270

271
    """
272
    raise NotImplementedError
273

    
274
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
275
    """Notify the LU about the results of its hooks.
276

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

283
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
284
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
285
    @param hook_results: the results of the multi-node hooks rpc call
286
    @param feedback_fn: function used send feedback back to the caller
287
    @param lu_result: the previous Exec result this LU had, or None
288
        in the PRE phase
289
    @return: the new Exec result, based on the previous result
290
        and hook results
291

292
    """
293
    # API must be kept, thus we ignore the unused argument and could
294
    # be a function warnings
295
    # pylint: disable-msg=W0613,R0201
296
    return lu_result
297

    
298
  def _ExpandAndLockInstance(self):
299
    """Helper function to expand and lock an instance.
300

301
    Many LUs that work on an instance take its name in self.op.instance_name
302
    and need to expand it and then declare the expanded name for locking. This
303
    function does it, and then updates self.op.instance_name to the expanded
304
    name. It also initializes needed_locks as a dict, if this hasn't been done
305
    before.
306

307
    """
308
    if self.needed_locks is None:
309
      self.needed_locks = {}
310
    else:
311
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
312
        "_ExpandAndLockInstance called with instance-level locks set"
313
    self.op.instance_name = _ExpandInstanceName(self.cfg,
314
                                                self.op.instance_name)
315
    self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
316

    
317
  def _LockInstancesNodes(self, primary_only=False):
318
    """Helper function to declare instances' nodes for locking.
319

320
    This function should be called after locking one or more instances to lock
321
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
322
    with all primary or secondary nodes for instances already locked and
323
    present in self.needed_locks[locking.LEVEL_INSTANCE].
324

325
    It should be called from DeclareLocks, and for safety only works if
326
    self.recalculate_locks[locking.LEVEL_NODE] is set.
327

328
    In the future it may grow parameters to just lock some instance's nodes, or
329
    to just lock primaries or secondary nodes, if needed.
330

331
    If should be called in DeclareLocks in a way similar to::
332

333
      if level == locking.LEVEL_NODE:
334
        self._LockInstancesNodes()
335

336
    @type primary_only: boolean
337
    @param primary_only: only lock primary nodes of locked instances
338

339
    """
340
    assert locking.LEVEL_NODE in self.recalculate_locks, \
341
      "_LockInstancesNodes helper function called with no nodes to recalculate"
342

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

    
345
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
346
    # future we might want to have different behaviors depending on the value
347
    # of self.recalculate_locks[locking.LEVEL_NODE]
348
    wanted_nodes = []
349
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
350
      instance = self.context.cfg.GetInstanceInfo(instance_name)
351
      wanted_nodes.append(instance.primary_node)
352
      if not primary_only:
353
        wanted_nodes.extend(instance.secondary_nodes)
354

    
355
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
356
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
357
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
358
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
359

    
360
    del self.recalculate_locks[locking.LEVEL_NODE]
361

    
362

    
363
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
364
  """Simple LU which runs no hooks.
365

366
  This LU is intended as a parent for other LogicalUnits which will
367
  run no hooks, in order to reduce duplicate code.
368

369
  """
370
  HPATH = None
371
  HTYPE = None
372

    
373
  def BuildHooksEnv(self):
374
    """Empty BuildHooksEnv for NoHooksLu.
375

376
    This just raises an error.
377

378
    """
379
    assert False, "BuildHooksEnv called for NoHooksLUs"
380

    
381

    
382
class Tasklet:
383
  """Tasklet base class.
384

385
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
386
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
387
  tasklets know nothing about locks.
388

389
  Subclasses must follow these rules:
390
    - Implement CheckPrereq
391
    - Implement Exec
392

393
  """
394
  def __init__(self, lu):
395
    self.lu = lu
396

    
397
    # Shortcuts
398
    self.cfg = lu.cfg
399
    self.rpc = lu.rpc
400

    
401
  def CheckPrereq(self):
402
    """Check prerequisites for this tasklets.
403

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

408
    The method should raise errors.OpPrereqError in case something is not
409
    fulfilled. Its return value is ignored.
410

411
    This method should also update all parameters to their canonical form if it
412
    hasn't been done before.
413

414
    """
415
    raise NotImplementedError
416

    
417
  def Exec(self, feedback_fn):
418
    """Execute the tasklet.
419

420
    This method should implement the actual work. It should raise
421
    errors.OpExecError for failures that are somewhat dealt with in code, or
422
    expected.
423

424
    """
425
    raise NotImplementedError
426

    
427

    
428
def _GetWantedNodes(lu, nodes):
429
  """Returns list of checked and expanded node names.
430

431
  @type lu: L{LogicalUnit}
432
  @param lu: the logical unit on whose behalf we execute
433
  @type nodes: list
434
  @param nodes: list of node names or None for all nodes
435
  @rtype: list
436
  @return: the list of nodes, sorted
437
  @raise errors.ProgrammerError: if the nodes parameter is wrong type
438

439
  """
440
  if not isinstance(nodes, list):
441
    raise errors.OpPrereqError("Invalid argument type 'nodes'",
442
                               errors.ECODE_INVAL)
443

    
444
  if not nodes:
445
    raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
446
      " non-empty list of nodes whose name is to be expanded.")
447

    
448
  wanted = [_ExpandNodeName(lu.cfg, name) for name in nodes]
449
  return utils.NiceSort(wanted)
450

    
451

    
452
def _GetWantedInstances(lu, instances):
453
  """Returns list of checked and expanded instance names.
454

455
  @type lu: L{LogicalUnit}
456
  @param lu: the logical unit on whose behalf we execute
457
  @type instances: list
458
  @param instances: list of instance names or None for all instances
459
  @rtype: list
460
  @return: the list of instances, sorted
461
  @raise errors.OpPrereqError: if the instances parameter is wrong type
462
  @raise errors.OpPrereqError: if any of the passed instances is not found
463

464
  """
465
  if not isinstance(instances, list):
466
    raise errors.OpPrereqError("Invalid argument type 'instances'",
467
                               errors.ECODE_INVAL)
468

    
469
  if instances:
470
    wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
471
  else:
472
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
473
  return wanted
474

    
475

    
476
def _CheckOutputFields(static, dynamic, selected):
477
  """Checks whether all selected fields are valid.
478

479
  @type static: L{utils.FieldSet}
480
  @param static: static fields set
481
  @type dynamic: L{utils.FieldSet}
482
  @param dynamic: dynamic fields set
483

484
  """
485
  f = utils.FieldSet()
486
  f.Extend(static)
487
  f.Extend(dynamic)
488

    
489
  delta = f.NonMatching(selected)
490
  if delta:
491
    raise errors.OpPrereqError("Unknown output fields selected: %s"
492
                               % ",".join(delta), errors.ECODE_INVAL)
493

    
494

    
495
def _CheckBooleanOpField(op, name):
496
  """Validates boolean opcode parameters.
497

498
  This will ensure that an opcode parameter is either a boolean value,
499
  or None (but that it always exists).
500

501
  """
502
  val = getattr(op, name, None)
503
  if not (val is None or isinstance(val, bool)):
504
    raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" %
505
                               (name, str(val)), errors.ECODE_INVAL)
506
  setattr(op, name, val)
507

    
508

    
509
def _CheckGlobalHvParams(params):
510
  """Validates that given hypervisor params are not global ones.
511

512
  This will ensure that instances don't get customised versions of
513
  global params.
514

515
  """
516
  used_globals = constants.HVC_GLOBALS.intersection(params)
517
  if used_globals:
518
    msg = ("The following hypervisor parameters are global and cannot"
519
           " be customized at instance level, please modify them at"
520
           " cluster level: %s" % utils.CommaJoin(used_globals))
521
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
522

    
523

    
524
def _CheckNodeOnline(lu, node):
525
  """Ensure that a given node is online.
526

527
  @param lu: the LU on behalf of which we make the check
528
  @param node: the node to check
529
  @raise errors.OpPrereqError: if the node is offline
530

531
  """
532
  if lu.cfg.GetNodeInfo(node).offline:
533
    raise errors.OpPrereqError("Can't use offline node %s" % node,
534
                               errors.ECODE_INVAL)
535

    
536

    
537
def _CheckNodeNotDrained(lu, node):
538
  """Ensure that a given node is not drained.
539

540
  @param lu: the LU on behalf of which we make the check
541
  @param node: the node to check
542
  @raise errors.OpPrereqError: if the node is drained
543

544
  """
545
  if lu.cfg.GetNodeInfo(node).drained:
546
    raise errors.OpPrereqError("Can't use drained node %s" % node,
547
                               errors.ECODE_INVAL)
548

    
549

    
550
def _CheckNodeHasOS(lu, node, os_name, force_variant):
551
  """Ensure that a node supports a given OS.
552

553
  @param lu: the LU on behalf of which we make the check
554
  @param node: the node to check
555
  @param os_name: the OS to query about
556
  @param force_variant: whether to ignore variant errors
557
  @raise errors.OpPrereqError: if the node is not supporting the OS
558

559
  """
560
  result = lu.rpc.call_os_get(node, os_name)
561
  result.Raise("OS '%s' not in supported OS list for node %s" %
562
               (os_name, node),
563
               prereq=True, ecode=errors.ECODE_INVAL)
564
  if not force_variant:
565
    _CheckOSVariant(result.payload, os_name)
566

    
567

    
568
def _RequireFileStorage():
569
  """Checks that file storage is enabled.
570

571
  @raise errors.OpPrereqError: when file storage is disabled
572

573
  """
574
  if not constants.ENABLE_FILE_STORAGE:
575
    raise errors.OpPrereqError("File storage disabled at configure time",
576
                               errors.ECODE_INVAL)
577

    
578

    
579
def _CheckDiskTemplate(template):
580
  """Ensure a given disk template is valid.
581

582
  """
583
  if template not in constants.DISK_TEMPLATES:
584
    msg = ("Invalid disk template name '%s', valid templates are: %s" %
585
           (template, utils.CommaJoin(constants.DISK_TEMPLATES)))
586
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
587
  if template == constants.DT_FILE:
588
    _RequireFileStorage()
589

    
590

    
591
def _CheckStorageType(storage_type):
592
  """Ensure a given storage type is valid.
593

594
  """
595
  if storage_type not in constants.VALID_STORAGE_TYPES:
596
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
597
                               errors.ECODE_INVAL)
598
  if storage_type == constants.ST_FILE:
599
    _RequireFileStorage()
600

    
601

    
602
def _GetClusterDomainSecret():
603
  """Reads the cluster domain secret.
604

605
  """
606
  return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
607
                               strict=True)
608

    
609

    
610
def _CheckInstanceDown(lu, instance, reason):
611
  """Ensure that an instance is not running."""
612
  if instance.admin_up:
613
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
614
                               (instance.name, reason), errors.ECODE_STATE)
615

    
616
  pnode = instance.primary_node
617
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
618
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
619
              prereq=True, ecode=errors.ECODE_ENVIRON)
620

    
621
  if instance.name in ins_l.payload:
622
    raise errors.OpPrereqError("Instance %s is running, %s" %
623
                               (instance.name, reason), errors.ECODE_STATE)
624

    
625

    
626
def _ExpandItemName(fn, name, kind):
627
  """Expand an item name.
628

629
  @param fn: the function to use for expansion
630
  @param name: requested item name
631
  @param kind: text description ('Node' or 'Instance')
632
  @return: the resolved (full) name
633
  @raise errors.OpPrereqError: if the item is not found
634

635
  """
636
  full_name = fn(name)
637
  if full_name is None:
638
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
639
                               errors.ECODE_NOENT)
640
  return full_name
641

    
642

    
643
def _ExpandNodeName(cfg, name):
644
  """Wrapper over L{_ExpandItemName} for nodes."""
645
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
646

    
647

    
648
def _ExpandInstanceName(cfg, name):
649
  """Wrapper over L{_ExpandItemName} for instance."""
650
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
651

    
652

    
653
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
654
                          memory, vcpus, nics, disk_template, disks,
655
                          bep, hvp, hypervisor_name):
656
  """Builds instance related env variables for hooks
657

658
  This builds the hook environment from individual variables.
659

660
  @type name: string
661
  @param name: the name of the instance
662
  @type primary_node: string
663
  @param primary_node: the name of the instance's primary node
664
  @type secondary_nodes: list
665
  @param secondary_nodes: list of secondary nodes as strings
666
  @type os_type: string
667
  @param os_type: the name of the instance's OS
668
  @type status: boolean
669
  @param status: the should_run status of the instance
670
  @type memory: string
671
  @param memory: the memory size of the instance
672
  @type vcpus: string
673
  @param vcpus: the count of VCPUs the instance has
674
  @type nics: list
675
  @param nics: list of tuples (ip, mac, mode, link) representing
676
      the NICs the instance has
677
  @type disk_template: string
678
  @param disk_template: the disk template of the instance
679
  @type disks: list
680
  @param disks: the list of (size, mode) pairs
681
  @type bep: dict
682
  @param bep: the backend parameters for the instance
683
  @type hvp: dict
684
  @param hvp: the hypervisor parameters for the instance
685
  @type hypervisor_name: string
686
  @param hypervisor_name: the hypervisor for the instance
687
  @rtype: dict
688
  @return: the hook environment for this instance
689

690
  """
691
  if status:
692
    str_status = "up"
693
  else:
694
    str_status = "down"
695
  env = {
696
    "OP_TARGET": name,
697
    "INSTANCE_NAME": name,
698
    "INSTANCE_PRIMARY": primary_node,
699
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
700
    "INSTANCE_OS_TYPE": os_type,
701
    "INSTANCE_STATUS": str_status,
702
    "INSTANCE_MEMORY": memory,
703
    "INSTANCE_VCPUS": vcpus,
704
    "INSTANCE_DISK_TEMPLATE": disk_template,
705
    "INSTANCE_HYPERVISOR": hypervisor_name,
706
  }
707

    
708
  if nics:
709
    nic_count = len(nics)
710
    for idx, (ip, mac, mode, link) in enumerate(nics):
711
      if ip is None:
712
        ip = ""
713
      env["INSTANCE_NIC%d_IP" % idx] = ip
714
      env["INSTANCE_NIC%d_MAC" % idx] = mac
715
      env["INSTANCE_NIC%d_MODE" % idx] = mode
716
      env["INSTANCE_NIC%d_LINK" % idx] = link
717
      if mode == constants.NIC_MODE_BRIDGED:
718
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
719
  else:
720
    nic_count = 0
721

    
722
  env["INSTANCE_NIC_COUNT"] = nic_count
723

    
724
  if disks:
725
    disk_count = len(disks)
726
    for idx, (size, mode) in enumerate(disks):
727
      env["INSTANCE_DISK%d_SIZE" % idx] = size
728
      env["INSTANCE_DISK%d_MODE" % idx] = mode
729
  else:
730
    disk_count = 0
731

    
732
  env["INSTANCE_DISK_COUNT"] = disk_count
733

    
734
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
735
    for key, value in source.items():
736
      env["INSTANCE_%s_%s" % (kind, key)] = value
737

    
738
  return env
739

    
740

    
741
def _NICListToTuple(lu, nics):
742
  """Build a list of nic information tuples.
743

744
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
745
  value in LUQueryInstanceData.
746

747
  @type lu:  L{LogicalUnit}
748
  @param lu: the logical unit on whose behalf we execute
749
  @type nics: list of L{objects.NIC}
750
  @param nics: list of nics to convert to hooks tuples
751

752
  """
753
  hooks_nics = []
754
  cluster = lu.cfg.GetClusterInfo()
755
  for nic in nics:
756
    ip = nic.ip
757
    mac = nic.mac
758
    filled_params = cluster.SimpleFillNIC(nic.nicparams)
759
    mode = filled_params[constants.NIC_MODE]
760
    link = filled_params[constants.NIC_LINK]
761
    hooks_nics.append((ip, mac, mode, link))
762
  return hooks_nics
763

    
764

    
765
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
766
  """Builds instance related env variables for hooks from an object.
767

768
  @type lu: L{LogicalUnit}
769
  @param lu: the logical unit on whose behalf we execute
770
  @type instance: L{objects.Instance}
771
  @param instance: the instance for which we should build the
772
      environment
773
  @type override: dict
774
  @param override: dictionary with key/values that will override
775
      our values
776
  @rtype: dict
777
  @return: the hook environment dictionary
778

779
  """
780
  cluster = lu.cfg.GetClusterInfo()
781
  bep = cluster.FillBE(instance)
782
  hvp = cluster.FillHV(instance)
783
  args = {
784
    'name': instance.name,
785
    'primary_node': instance.primary_node,
786
    'secondary_nodes': instance.secondary_nodes,
787
    'os_type': instance.os,
788
    'status': instance.admin_up,
789
    'memory': bep[constants.BE_MEMORY],
790
    'vcpus': bep[constants.BE_VCPUS],
791
    'nics': _NICListToTuple(lu, instance.nics),
792
    'disk_template': instance.disk_template,
793
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
794
    'bep': bep,
795
    'hvp': hvp,
796
    'hypervisor_name': instance.hypervisor,
797
  }
798
  if override:
799
    args.update(override)
800
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
801

    
802

    
803
def _AdjustCandidatePool(lu, exceptions):
804
  """Adjust the candidate pool after node operations.
805

806
  """
807
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
808
  if mod_list:
809
    lu.LogInfo("Promoted nodes to master candidate role: %s",
810
               utils.CommaJoin(node.name for node in mod_list))
811
    for name in mod_list:
812
      lu.context.ReaddNode(name)
813
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
814
  if mc_now > mc_max:
815
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
816
               (mc_now, mc_max))
817

    
818

    
819
def _DecideSelfPromotion(lu, exceptions=None):
820
  """Decide whether I should promote myself as a master candidate.
821

822
  """
823
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
824
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
825
  # the new node will increase mc_max with one, so:
826
  mc_should = min(mc_should + 1, cp_size)
827
  return mc_now < mc_should
828

    
829

    
830
def _CheckNicsBridgesExist(lu, target_nics, target_node):
831
  """Check that the brigdes needed by a list of nics exist.
832

833
  """
834
  cluster = lu.cfg.GetClusterInfo()
835
  paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics]
836
  brlist = [params[constants.NIC_LINK] for params in paramslist
837
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
838
  if brlist:
839
    result = lu.rpc.call_bridges_exist(target_node, brlist)
840
    result.Raise("Error checking bridges on destination node '%s'" %
841
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
842

    
843

    
844
def _CheckInstanceBridgesExist(lu, instance, node=None):
845
  """Check that the brigdes needed by an instance exist.
846

847
  """
848
  if node is None:
849
    node = instance.primary_node
850
  _CheckNicsBridgesExist(lu, instance.nics, node)
851

    
852

    
853
def _CheckOSVariant(os_obj, name):
854
  """Check whether an OS name conforms to the os variants specification.
855

856
  @type os_obj: L{objects.OS}
857
  @param os_obj: OS object to check
858
  @type name: string
859
  @param name: OS name passed by the user, to check for validity
860

861
  """
862
  if not os_obj.supported_variants:
863
    return
864
  try:
865
    variant = name.split("+", 1)[1]
866
  except IndexError:
867
    raise errors.OpPrereqError("OS name must include a variant",
868
                               errors.ECODE_INVAL)
869

    
870
  if variant not in os_obj.supported_variants:
871
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
872

    
873

    
874
def _GetNodeInstancesInner(cfg, fn):
875
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
876

    
877

    
878
def _GetNodeInstances(cfg, node_name):
879
  """Returns a list of all primary and secondary instances on a node.
880

881
  """
882

    
883
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
884

    
885

    
886
def _GetNodePrimaryInstances(cfg, node_name):
887
  """Returns primary instances on a node.
888

889
  """
890
  return _GetNodeInstancesInner(cfg,
891
                                lambda inst: node_name == inst.primary_node)
892

    
893

    
894
def _GetNodeSecondaryInstances(cfg, node_name):
895
  """Returns secondary instances on a node.
896

897
  """
898
  return _GetNodeInstancesInner(cfg,
899
                                lambda inst: node_name in inst.secondary_nodes)
900

    
901

    
902
def _GetStorageTypeArgs(cfg, storage_type):
903
  """Returns the arguments for a storage type.
904

905
  """
906
  # Special case for file storage
907
  if storage_type == constants.ST_FILE:
908
    # storage.FileStorage wants a list of storage directories
909
    return [[cfg.GetFileStorageDir()]]
910

    
911
  return []
912

    
913

    
914
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
915
  faulty = []
916

    
917
  for dev in instance.disks:
918
    cfg.SetDiskID(dev, node_name)
919

    
920
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
921
  result.Raise("Failed to get disk status from node %s" % node_name,
922
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
923

    
924
  for idx, bdev_status in enumerate(result.payload):
925
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
926
      faulty.append(idx)
927

    
928
  return faulty
929

    
930

    
931
class LUPostInitCluster(LogicalUnit):
932
  """Logical unit for running hooks after cluster initialization.
933

934
  """
935
  HPATH = "cluster-init"
936
  HTYPE = constants.HTYPE_CLUSTER
937
  _OP_REQP = []
938

    
939
  def BuildHooksEnv(self):
940
    """Build hooks env.
941

942
    """
943
    env = {"OP_TARGET": self.cfg.GetClusterName()}
944
    mn = self.cfg.GetMasterNode()
945
    return env, [], [mn]
946

    
947
  def CheckPrereq(self):
948
    """No prerequisites to check.
949

950
    """
951
    return True
952

    
953
  def Exec(self, feedback_fn):
954
    """Nothing to do.
955

956
    """
957
    return True
958

    
959

    
960
class LUDestroyCluster(LogicalUnit):
961
  """Logical unit for destroying the cluster.
962

963
  """
964
  HPATH = "cluster-destroy"
965
  HTYPE = constants.HTYPE_CLUSTER
966
  _OP_REQP = []
967

    
968
  def BuildHooksEnv(self):
969
    """Build hooks env.
970

971
    """
972
    env = {"OP_TARGET": self.cfg.GetClusterName()}
973
    return env, [], []
974

    
975
  def CheckPrereq(self):
976
    """Check prerequisites.
977

978
    This checks whether the cluster is empty.
979

980
    Any errors are signaled by raising errors.OpPrereqError.
981

982
    """
983
    master = self.cfg.GetMasterNode()
984

    
985
    nodelist = self.cfg.GetNodeList()
986
    if len(nodelist) != 1 or nodelist[0] != master:
987
      raise errors.OpPrereqError("There are still %d node(s) in"
988
                                 " this cluster." % (len(nodelist) - 1),
989
                                 errors.ECODE_INVAL)
990
    instancelist = self.cfg.GetInstanceList()
991
    if instancelist:
992
      raise errors.OpPrereqError("There are still %d instance(s) in"
993
                                 " this cluster." % len(instancelist),
994
                                 errors.ECODE_INVAL)
995

    
996
  def Exec(self, feedback_fn):
997
    """Destroys the cluster.
998

999
    """
1000
    master = self.cfg.GetMasterNode()
1001
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
1002

    
1003
    # Run post hooks on master node before it's removed
1004
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
1005
    try:
1006
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
1007
    except:
1008
      # pylint: disable-msg=W0702
1009
      self.LogWarning("Errors occurred running hooks on %s" % master)
1010

    
1011
    result = self.rpc.call_node_stop_master(master, False)
1012
    result.Raise("Could not disable the master role")
1013

    
1014
    if modify_ssh_setup:
1015
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
1016
      utils.CreateBackup(priv_key)
1017
      utils.CreateBackup(pub_key)
1018

    
1019
    return master
1020

    
1021

    
1022
def _VerifyCertificate(filename):
1023
  """Verifies a certificate for LUVerifyCluster.
1024

1025
  @type filename: string
1026
  @param filename: Path to PEM file
1027

1028
  """
1029
  try:
1030
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1031
                                           utils.ReadFile(filename))
1032
  except Exception, err: # pylint: disable-msg=W0703
1033
    return (LUVerifyCluster.ETYPE_ERROR,
1034
            "Failed to load X509 certificate %s: %s" % (filename, err))
1035

    
1036
  (errcode, msg) = \
1037
    utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1038
                                constants.SSL_CERT_EXPIRATION_ERROR)
1039

    
1040
  if msg:
1041
    fnamemsg = "While verifying %s: %s" % (filename, msg)
1042
  else:
1043
    fnamemsg = None
1044

    
1045
  if errcode is None:
1046
    return (None, fnamemsg)
1047
  elif errcode == utils.CERT_WARNING:
1048
    return (LUVerifyCluster.ETYPE_WARNING, fnamemsg)
1049
  elif errcode == utils.CERT_ERROR:
1050
    return (LUVerifyCluster.ETYPE_ERROR, fnamemsg)
1051

    
1052
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1053

    
1054

    
1055
class LUVerifyCluster(LogicalUnit):
1056
  """Verifies the cluster status.
1057

1058
  """
1059
  HPATH = "cluster-verify"
1060
  HTYPE = constants.HTYPE_CLUSTER
1061
  _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"]
1062
  REQ_BGL = False
1063

    
1064
  TCLUSTER = "cluster"
1065
  TNODE = "node"
1066
  TINSTANCE = "instance"
1067

    
1068
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1069
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1070
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1071
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1072
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1073
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1074
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1075
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1076
  ENODEDRBD = (TNODE, "ENODEDRBD")
1077
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1078
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1079
  ENODEHV = (TNODE, "ENODEHV")
1080
  ENODELVM = (TNODE, "ENODELVM")
1081
  ENODEN1 = (TNODE, "ENODEN1")
1082
  ENODENET = (TNODE, "ENODENET")
1083
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1084
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1085
  ENODERPC = (TNODE, "ENODERPC")
1086
  ENODESSH = (TNODE, "ENODESSH")
1087
  ENODEVERSION = (TNODE, "ENODEVERSION")
1088
  ENODESETUP = (TNODE, "ENODESETUP")
1089
  ENODETIME = (TNODE, "ENODETIME")
1090

    
1091
  ETYPE_FIELD = "code"
1092
  ETYPE_ERROR = "ERROR"
1093
  ETYPE_WARNING = "WARNING"
1094

    
1095
  class NodeImage(object):
1096
    """A class representing the logical and physical status of a node.
1097

1098
    @ivar volumes: a structure as returned from
1099
        L{ganeti.backend.GetVolumeList} (runtime)
1100
    @ivar instances: a list of running instances (runtime)
1101
    @ivar pinst: list of configured primary instances (config)
1102
    @ivar sinst: list of configured secondary instances (config)
1103
    @ivar sbp: diction of {secondary-node: list of instances} of all peers
1104
        of this node (config)
1105
    @ivar mfree: free memory, as reported by hypervisor (runtime)
1106
    @ivar dfree: free disk, as reported by the node (runtime)
1107
    @ivar offline: the offline status (config)
1108
    @type rpc_fail: boolean
1109
    @ivar rpc_fail: whether the RPC verify call was successfull (overall,
1110
        not whether the individual keys were correct) (runtime)
1111
    @type lvm_fail: boolean
1112
    @ivar lvm_fail: whether the RPC call didn't return valid LVM data
1113
    @type hyp_fail: boolean
1114
    @ivar hyp_fail: whether the RPC call didn't return the instance list
1115
    @type ghost: boolean
1116
    @ivar ghost: whether this is a known node or not (config)
1117

1118
    """
1119
    def __init__(self, offline=False):
1120
      self.volumes = {}
1121
      self.instances = []
1122
      self.pinst = []
1123
      self.sinst = []
1124
      self.sbp = {}
1125
      self.mfree = 0
1126
      self.dfree = 0
1127
      self.offline = offline
1128
      self.rpc_fail = False
1129
      self.lvm_fail = False
1130
      self.hyp_fail = False
1131
      self.ghost = False
1132

    
1133
  def ExpandNames(self):
1134
    self.needed_locks = {
1135
      locking.LEVEL_NODE: locking.ALL_SET,
1136
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1137
    }
1138
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1139

    
1140
  def _Error(self, ecode, item, msg, *args, **kwargs):
1141
    """Format an error message.
1142

1143
    Based on the opcode's error_codes parameter, either format a
1144
    parseable error code, or a simpler error string.
1145

1146
    This must be called only from Exec and functions called from Exec.
1147

1148
    """
1149
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1150
    itype, etxt = ecode
1151
    # first complete the msg
1152
    if args:
1153
      msg = msg % args
1154
    # then format the whole message
1155
    if self.op.error_codes:
1156
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1157
    else:
1158
      if item:
1159
        item = " " + item
1160
      else:
1161
        item = ""
1162
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1163
    # and finally report it via the feedback_fn
1164
    self._feedback_fn("  - %s" % msg)
1165

    
1166
  def _ErrorIf(self, cond, *args, **kwargs):
1167
    """Log an error message if the passed condition is True.
1168

1169
    """
1170
    cond = bool(cond) or self.op.debug_simulate_errors
1171
    if cond:
1172
      self._Error(*args, **kwargs)
1173
    # do not mark the operation as failed for WARN cases only
1174
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1175
      self.bad = self.bad or cond
1176

    
1177
  def _VerifyNode(self, ninfo, nresult):
1178
    """Run multiple tests against a node.
1179

1180
    Test list:
1181

1182
      - compares ganeti version
1183
      - checks vg existence and size > 20G
1184
      - checks config file checksum
1185
      - checks ssh to other nodes
1186

1187
    @type ninfo: L{objects.Node}
1188
    @param ninfo: the node to check
1189
    @param nresult: the results from the node
1190
    @rtype: boolean
1191
    @return: whether overall this call was successful (and we can expect
1192
         reasonable values in the respose)
1193

1194
    """
1195
    node = ninfo.name
1196
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1197

    
1198
    # main result, nresult should be a non-empty dict
1199
    test = not nresult or not isinstance(nresult, dict)
1200
    _ErrorIf(test, self.ENODERPC, node,
1201
                  "unable to verify node: no data returned")
1202
    if test:
1203
      return False
1204

    
1205
    # compares ganeti version
1206
    local_version = constants.PROTOCOL_VERSION
1207
    remote_version = nresult.get("version", None)
1208
    test = not (remote_version and
1209
                isinstance(remote_version, (list, tuple)) and
1210
                len(remote_version) == 2)
1211
    _ErrorIf(test, self.ENODERPC, node,
1212
             "connection to node returned invalid data")
1213
    if test:
1214
      return False
1215

    
1216
    test = local_version != remote_version[0]
1217
    _ErrorIf(test, self.ENODEVERSION, node,
1218
             "incompatible protocol versions: master %s,"
1219
             " node %s", local_version, remote_version[0])
1220
    if test:
1221
      return False
1222

    
1223
    # node seems compatible, we can actually try to look into its results
1224

    
1225
    # full package version
1226
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1227
                  self.ENODEVERSION, node,
1228
                  "software version mismatch: master %s, node %s",
1229
                  constants.RELEASE_VERSION, remote_version[1],
1230
                  code=self.ETYPE_WARNING)
1231

    
1232
    hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1233
    if isinstance(hyp_result, dict):
1234
      for hv_name, hv_result in hyp_result.iteritems():
1235
        test = hv_result is not None
1236
        _ErrorIf(test, self.ENODEHV, node,
1237
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1238

    
1239

    
1240
    test = nresult.get(constants.NV_NODESETUP,
1241
                           ["Missing NODESETUP results"])
1242
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1243
             "; ".join(test))
1244

    
1245
    return True
1246

    
1247
  def _VerifyNodeTime(self, ninfo, nresult,
1248
                      nvinfo_starttime, nvinfo_endtime):
1249
    """Check the node time.
1250

1251
    @type ninfo: L{objects.Node}
1252
    @param ninfo: the node to check
1253
    @param nresult: the remote results for the node
1254
    @param nvinfo_starttime: the start time of the RPC call
1255
    @param nvinfo_endtime: the end time of the RPC call
1256

1257
    """
1258
    node = ninfo.name
1259
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1260

    
1261
    ntime = nresult.get(constants.NV_TIME, None)
1262
    try:
1263
      ntime_merged = utils.MergeTime(ntime)
1264
    except (ValueError, TypeError):
1265
      _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1266
      return
1267

    
1268
    if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1269
      ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1270
    elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1271
      ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1272
    else:
1273
      ntime_diff = None
1274

    
1275
    _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1276
             "Node time diverges by at least %s from master node time",
1277
             ntime_diff)
1278

    
1279
  def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1280
    """Check the node time.
1281

1282
    @type ninfo: L{objects.Node}
1283
    @param ninfo: the node to check
1284
    @param nresult: the remote results for the node
1285
    @param vg_name: the configured VG name
1286

1287
    """
1288
    if vg_name is None:
1289
      return
1290

    
1291
    node = ninfo.name
1292
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1293

    
1294
    # checks vg existence and size > 20G
1295
    vglist = nresult.get(constants.NV_VGLIST, None)
1296
    test = not vglist
1297
    _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1298
    if not test:
1299
      vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1300
                                            constants.MIN_VG_SIZE)
1301
      _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1302

    
1303
    # check pv names
1304
    pvlist = nresult.get(constants.NV_PVLIST, None)
1305
    test = pvlist is None
1306
    _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1307
    if not test:
1308
      # check that ':' is not present in PV names, since it's a
1309
      # special character for lvcreate (denotes the range of PEs to
1310
      # use on the PV)
1311
      for _, pvname, owner_vg in pvlist:
1312
        test = ":" in pvname
1313
        _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1314
                 " '%s' of VG '%s'", pvname, owner_vg)
1315

    
1316
  def _VerifyNodeNetwork(self, ninfo, nresult):
1317
    """Check the node time.
1318

1319
    @type ninfo: L{objects.Node}
1320
    @param ninfo: the node to check
1321
    @param nresult: the remote results for the node
1322

1323
    """
1324
    node = ninfo.name
1325
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1326

    
1327
    test = constants.NV_NODELIST not in nresult
1328
    _ErrorIf(test, self.ENODESSH, node,
1329
             "node hasn't returned node ssh connectivity data")
1330
    if not test:
1331
      if nresult[constants.NV_NODELIST]:
1332
        for a_node, a_msg in nresult[constants.NV_NODELIST].items():
1333
          _ErrorIf(True, self.ENODESSH, node,
1334
                   "ssh communication with node '%s': %s", a_node, a_msg)
1335

    
1336
    test = constants.NV_NODENETTEST not in nresult
1337
    _ErrorIf(test, self.ENODENET, node,
1338
             "node hasn't returned node tcp connectivity data")
1339
    if not test:
1340
      if nresult[constants.NV_NODENETTEST]:
1341
        nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys())
1342
        for anode in nlist:
1343
          _ErrorIf(True, self.ENODENET, node,
1344
                   "tcp communication with node '%s': %s",
1345
                   anode, nresult[constants.NV_NODENETTEST][anode])
1346

    
1347
    test = constants.NV_MASTERIP not in nresult
1348
    _ErrorIf(test, self.ENODENET, node,
1349
             "node hasn't returned node master IP reachability data")
1350
    if not test:
1351
      if not nresult[constants.NV_MASTERIP]:
1352
        if node == self.master_node:
1353
          msg = "the master node cannot reach the master IP (not configured?)"
1354
        else:
1355
          msg = "cannot reach the master IP"
1356
        _ErrorIf(True, self.ENODENET, node, msg)
1357

    
1358

    
1359
  def _VerifyInstance(self, instance, instanceconfig, node_image):
1360
    """Verify an instance.
1361

1362
    This function checks to see if the required block devices are
1363
    available on the instance's node.
1364

1365
    """
1366
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1367
    node_current = instanceconfig.primary_node
1368

    
1369
    node_vol_should = {}
1370
    instanceconfig.MapLVsByNode(node_vol_should)
1371

    
1372
    for node in node_vol_should:
1373
      n_img = node_image[node]
1374
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1375
        # ignore missing volumes on offline or broken nodes
1376
        continue
1377
      for volume in node_vol_should[node]:
1378
        test = volume not in n_img.volumes
1379
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1380
                 "volume %s missing on node %s", volume, node)
1381

    
1382
    if instanceconfig.admin_up:
1383
      pri_img = node_image[node_current]
1384
      test = instance not in pri_img.instances and not pri_img.offline
1385
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1386
               "instance not running on its primary node %s",
1387
               node_current)
1388

    
1389
    for node, n_img in node_image.items():
1390
      if (not node == node_current):
1391
        test = instance in n_img.instances
1392
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1393
                 "instance should not run on node %s", node)
1394

    
1395
  def _VerifyOrphanVolumes(self, node_vol_should, node_image):
1396
    """Verify if there are any unknown volumes in the cluster.
1397

1398
    The .os, .swap and backup volumes are ignored. All other volumes are
1399
    reported as unknown.
1400

1401
    """
1402
    for node, n_img in node_image.items():
1403
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1404
        # skip non-healthy nodes
1405
        continue
1406
      for volume in n_img.volumes:
1407
        test = (node not in node_vol_should or
1408
                volume not in node_vol_should[node])
1409
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1410
                      "volume %s is unknown", volume)
1411

    
1412
  def _VerifyOrphanInstances(self, instancelist, node_image):
1413
    """Verify the list of running instances.
1414

1415
    This checks what instances are running but unknown to the cluster.
1416

1417
    """
1418
    for node, n_img in node_image.items():
1419
      for o_inst in n_img.instances:
1420
        test = o_inst not in instancelist
1421
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1422
                      "instance %s on node %s should not exist", o_inst, node)
1423

    
1424
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1425
    """Verify N+1 Memory Resilience.
1426

1427
    Check that if one single node dies we can still start all the
1428
    instances it was primary for.
1429

1430
    """
1431
    for node, n_img in node_image.items():
1432
      # This code checks that every node which is now listed as
1433
      # secondary has enough memory to host all instances it is
1434
      # supposed to should a single other node in the cluster fail.
1435
      # FIXME: not ready for failover to an arbitrary node
1436
      # FIXME: does not support file-backed instances
1437
      # WARNING: we currently take into account down instances as well
1438
      # as up ones, considering that even if they're down someone
1439
      # might want to start them even in the event of a node failure.
1440
      for prinode, instances in n_img.sbp.items():
1441
        needed_mem = 0
1442
        for instance in instances:
1443
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1444
          if bep[constants.BE_AUTO_BALANCE]:
1445
            needed_mem += bep[constants.BE_MEMORY]
1446
        test = n_img.mfree < needed_mem
1447
        self._ErrorIf(test, self.ENODEN1, node,
1448
                      "not enough memory on to accommodate"
1449
                      " failovers should peer node %s fail", prinode)
1450

    
1451
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1452
                       master_files):
1453
    """Verifies and computes the node required file checksums.
1454

1455
    @type ninfo: L{objects.Node}
1456
    @param ninfo: the node to check
1457
    @param nresult: the remote results for the node
1458
    @param file_list: required list of files
1459
    @param local_cksum: dictionary of local files and their checksums
1460
    @param master_files: list of files that only masters should have
1461

1462
    """
1463
    node = ninfo.name
1464
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1465

    
1466
    remote_cksum = nresult.get(constants.NV_FILELIST, None)
1467
    test = not isinstance(remote_cksum, dict)
1468
    _ErrorIf(test, self.ENODEFILECHECK, node,
1469
             "node hasn't returned file checksum data")
1470
    if test:
1471
      return
1472

    
1473
    for file_name in file_list:
1474
      node_is_mc = ninfo.master_candidate
1475
      must_have = (file_name not in master_files) or node_is_mc
1476
      # missing
1477
      test1 = file_name not in remote_cksum
1478
      # invalid checksum
1479
      test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1480
      # existing and good
1481
      test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1482
      _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1483
               "file '%s' missing", file_name)
1484
      _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1485
               "file '%s' has wrong checksum", file_name)
1486
      # not candidate and this is not a must-have file
1487
      _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1488
               "file '%s' should not exist on non master"
1489
               " candidates (and the file is outdated)", file_name)
1490
      # all good, except non-master/non-must have combination
1491
      _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1492
               "file '%s' should not exist"
1493
               " on non master candidates", file_name)
1494

    
1495
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_map):
1496
    """Verifies and the node DRBD status.
1497

1498
    @type ninfo: L{objects.Node}
1499
    @param ninfo: the node to check
1500
    @param nresult: the remote results for the node
1501
    @param instanceinfo: the dict of instances
1502
    @param drbd_map: the DRBD map as returned by
1503
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1504

1505
    """
1506
    node = ninfo.name
1507
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1508

    
1509
    # compute the DRBD minors
1510
    node_drbd = {}
1511
    for minor, instance in drbd_map[node].items():
1512
      test = instance not in instanceinfo
1513
      _ErrorIf(test, self.ECLUSTERCFG, None,
1514
               "ghost instance '%s' in temporary DRBD map", instance)
1515
        # ghost instance should not be running, but otherwise we
1516
        # don't give double warnings (both ghost instance and
1517
        # unallocated minor in use)
1518
      if test:
1519
        node_drbd[minor] = (instance, False)
1520
      else:
1521
        instance = instanceinfo[instance]
1522
        node_drbd[minor] = (instance.name, instance.admin_up)
1523

    
1524
    # and now check them
1525
    used_minors = nresult.get(constants.NV_DRBDLIST, [])
1526
    test = not isinstance(used_minors, (tuple, list))
1527
    _ErrorIf(test, self.ENODEDRBD, node,
1528
             "cannot parse drbd status file: %s", str(used_minors))
1529
    if test:
1530
      # we cannot check drbd status
1531
      return
1532

    
1533
    for minor, (iname, must_exist) in node_drbd.items():
1534
      test = minor not in used_minors and must_exist
1535
      _ErrorIf(test, self.ENODEDRBD, node,
1536
               "drbd minor %d of instance %s is not active", minor, iname)
1537
    for minor in used_minors:
1538
      test = minor not in node_drbd
1539
      _ErrorIf(test, self.ENODEDRBD, node,
1540
               "unallocated drbd minor %d is in use", minor)
1541

    
1542
  def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
1543
    """Verifies and updates the node volume data.
1544

1545
    This function will update a L{NodeImage}'s internal structures
1546
    with data from the remote call.
1547

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

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

    
1558
    nimg.lvm_fail = True
1559
    lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1560
    if vg_name is None:
1561
      pass
1562
    elif isinstance(lvdata, basestring):
1563
      _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1564
               utils.SafeEncode(lvdata))
1565
    elif not isinstance(lvdata, dict):
1566
      _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1567
    else:
1568
      nimg.volumes = lvdata
1569
      nimg.lvm_fail = False
1570

    
1571
  def _UpdateNodeInstances(self, ninfo, nresult, nimg):
1572
    """Verifies and updates the node instance list.
1573

1574
    If the listing was successful, then updates this node's instance
1575
    list. Otherwise, it marks the RPC call as failed for the instance
1576
    list key.
1577

1578
    @type ninfo: L{objects.Node}
1579
    @param ninfo: the node to check
1580
    @param nresult: the remote results for the node
1581
    @param nimg: the node image object
1582

1583
    """
1584
    idata = nresult.get(constants.NV_INSTANCELIST, None)
1585
    test = not isinstance(idata, list)
1586
    self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed"
1587
                  " (instancelist): %s", utils.SafeEncode(str(idata)))
1588
    if test:
1589
      nimg.hyp_fail = True
1590
    else:
1591
      nimg.instances = idata
1592

    
1593
  def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
1594
    """Verifies and computes a node information map
1595

1596
    @type ninfo: L{objects.Node}
1597
    @param ninfo: the node to check
1598
    @param nresult: the remote results for the node
1599
    @param nimg: the node image object
1600
    @param vg_name: the configured VG name
1601

1602
    """
1603
    node = ninfo.name
1604
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1605

    
1606
    # try to read free memory (from the hypervisor)
1607
    hv_info = nresult.get(constants.NV_HVINFO, None)
1608
    test = not isinstance(hv_info, dict) or "memory_free" not in hv_info
1609
    _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1610
    if not test:
1611
      try:
1612
        nimg.mfree = int(hv_info["memory_free"])
1613
      except (ValueError, TypeError):
1614
        _ErrorIf(True, self.ENODERPC, node,
1615
                 "node returned invalid nodeinfo, check hypervisor")
1616

    
1617
    # FIXME: devise a free space model for file based instances as well
1618
    if vg_name is not None:
1619
      test = (constants.NV_VGLIST not in nresult or
1620
              vg_name not in nresult[constants.NV_VGLIST])
1621
      _ErrorIf(test, self.ENODELVM, node,
1622
               "node didn't return data for the volume group '%s'"
1623
               " - it is either missing or broken", vg_name)
1624
      if not test:
1625
        try:
1626
          nimg.dfree = int(nresult[constants.NV_VGLIST][vg_name])
1627
        except (ValueError, TypeError):
1628
          _ErrorIf(True, self.ENODERPC, node,
1629
                   "node returned invalid LVM info, check LVM status")
1630

    
1631
  def CheckPrereq(self):
1632
    """Check prerequisites.
1633

1634
    Transform the list of checks we're going to skip into a set and check that
1635
    all its members are valid.
1636

1637
    """
1638
    self.skip_set = frozenset(self.op.skip_checks)
1639
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1640
      raise errors.OpPrereqError("Invalid checks to be skipped specified",
1641
                                 errors.ECODE_INVAL)
1642

    
1643
  def BuildHooksEnv(self):
1644
    """Build hooks env.
1645

1646
    Cluster-Verify hooks just ran in the post phase and their failure makes
1647
    the output be logged in the verify output and the verification to fail.
1648

1649
    """
1650
    all_nodes = self.cfg.GetNodeList()
1651
    env = {
1652
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1653
      }
1654
    for node in self.cfg.GetAllNodesInfo().values():
1655
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1656

    
1657
    return env, [], all_nodes
1658

    
1659
  def Exec(self, feedback_fn):
1660
    """Verify integrity of cluster, performing various test on nodes.
1661

1662
    """
1663
    self.bad = False
1664
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1665
    verbose = self.op.verbose
1666
    self._feedback_fn = feedback_fn
1667
    feedback_fn("* Verifying global settings")
1668
    for msg in self.cfg.VerifyConfig():
1669
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1670

    
1671
    # Check the cluster certificates
1672
    for cert_filename in constants.ALL_CERT_FILES:
1673
      (errcode, msg) = _VerifyCertificate(cert_filename)
1674
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1675

    
1676
    vg_name = self.cfg.GetVGName()
1677
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1678
    cluster = self.cfg.GetClusterInfo()
1679
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1680
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1681
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1682
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1683
                        for iname in instancelist)
1684
    i_non_redundant = [] # Non redundant instances
1685
    i_non_a_balanced = [] # Non auto-balanced instances
1686
    n_offline = 0 # Count of offline nodes
1687
    n_drained = 0 # Count of nodes being drained
1688
    node_vol_should = {}
1689

    
1690
    # FIXME: verify OS list
1691
    # do local checksums
1692
    master_files = [constants.CLUSTER_CONF_FILE]
1693
    master_node = self.master_node = self.cfg.GetMasterNode()
1694
    master_ip = self.cfg.GetMasterIP()
1695

    
1696
    file_names = ssconf.SimpleStore().GetFileList()
1697
    file_names.extend(constants.ALL_CERT_FILES)
1698
    file_names.extend(master_files)
1699
    if cluster.modify_etc_hosts:
1700
      file_names.append(constants.ETC_HOSTS)
1701

    
1702
    local_checksums = utils.FingerprintFiles(file_names)
1703

    
1704
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1705
    node_verify_param = {
1706
      constants.NV_FILELIST: file_names,
1707
      constants.NV_NODELIST: [node.name for node in nodeinfo
1708
                              if not node.offline],
1709
      constants.NV_HYPERVISOR: hypervisors,
1710
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1711
                                  node.secondary_ip) for node in nodeinfo
1712
                                 if not node.offline],
1713
      constants.NV_INSTANCELIST: hypervisors,
1714
      constants.NV_VERSION: None,
1715
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1716
      constants.NV_NODESETUP: None,
1717
      constants.NV_TIME: None,
1718
      constants.NV_MASTERIP: (master_node, master_ip),
1719
      }
1720

    
1721
    if vg_name is not None:
1722
      node_verify_param[constants.NV_VGLIST] = None
1723
      node_verify_param[constants.NV_LVLIST] = vg_name
1724
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1725
      node_verify_param[constants.NV_DRBDLIST] = None
1726

    
1727
    # Build our expected cluster state
1728
    node_image = dict((node.name, self.NodeImage(offline=node.offline))
1729
                      for node in nodeinfo)
1730

    
1731
    for instance in instancelist:
1732
      inst_config = instanceinfo[instance]
1733

    
1734
      for nname in inst_config.all_nodes:
1735
        if nname not in node_image:
1736
          # ghost node
1737
          gnode = self.NodeImage()
1738
          gnode.ghost = True
1739
          node_image[nname] = gnode
1740

    
1741
      inst_config.MapLVsByNode(node_vol_should)
1742

    
1743
      pnode = inst_config.primary_node
1744
      node_image[pnode].pinst.append(instance)
1745

    
1746
      for snode in inst_config.secondary_nodes:
1747
        nimg = node_image[snode]
1748
        nimg.sinst.append(instance)
1749
        if pnode not in nimg.sbp:
1750
          nimg.sbp[pnode] = []
1751
        nimg.sbp[pnode].append(instance)
1752

    
1753
    # At this point, we have the in-memory data structures complete,
1754
    # except for the runtime information, which we'll gather next
1755

    
1756
    # Due to the way our RPC system works, exact response times cannot be
1757
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
1758
    # time before and after executing the request, we can at least have a time
1759
    # window.
1760
    nvinfo_starttime = time.time()
1761
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1762
                                           self.cfg.GetClusterName())
1763
    nvinfo_endtime = time.time()
1764

    
1765
    all_drbd_map = self.cfg.ComputeDRBDMap()
1766

    
1767
    feedback_fn("* Verifying node status")
1768
    for node_i in nodeinfo:
1769
      node = node_i.name
1770
      nimg = node_image[node]
1771

    
1772
      if node_i.offline:
1773
        if verbose:
1774
          feedback_fn("* Skipping offline node %s" % (node,))
1775
        n_offline += 1
1776
        continue
1777

    
1778
      if node == master_node:
1779
        ntype = "master"
1780
      elif node_i.master_candidate:
1781
        ntype = "master candidate"
1782
      elif node_i.drained:
1783
        ntype = "drained"
1784
        n_drained += 1
1785
      else:
1786
        ntype = "regular"
1787
      if verbose:
1788
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1789

    
1790
      msg = all_nvinfo[node].fail_msg
1791
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
1792
      if msg:
1793
        nimg.rpc_fail = True
1794
        continue
1795

    
1796
      nresult = all_nvinfo[node].payload
1797

    
1798
      nimg.call_ok = self._VerifyNode(node_i, nresult)
1799
      self._VerifyNodeNetwork(node_i, nresult)
1800
      self._VerifyNodeLVM(node_i, nresult, vg_name)
1801
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
1802
                            master_files)
1803
      self._VerifyNodeDrbd(node_i, nresult, instanceinfo, all_drbd_map)
1804
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
1805

    
1806
      self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
1807
      self._UpdateNodeInstances(node_i, nresult, nimg)
1808
      self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
1809

    
1810
    feedback_fn("* Verifying instance status")
1811
    for instance in instancelist:
1812
      if verbose:
1813
        feedback_fn("* Verifying instance %s" % instance)
1814
      inst_config = instanceinfo[instance]
1815
      self._VerifyInstance(instance, inst_config, node_image)
1816
      inst_nodes_offline = []
1817

    
1818
      pnode = inst_config.primary_node
1819
      pnode_img = node_image[pnode]
1820
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
1821
               self.ENODERPC, pnode, "instance %s, connection to"
1822
               " primary node failed", instance)
1823

    
1824
      if pnode_img.offline:
1825
        inst_nodes_offline.append(pnode)
1826

    
1827
      # If the instance is non-redundant we cannot survive losing its primary
1828
      # node, so we are not N+1 compliant. On the other hand we have no disk
1829
      # templates with more than one secondary so that situation is not well
1830
      # supported either.
1831
      # FIXME: does not support file-backed instances
1832
      if not inst_config.secondary_nodes:
1833
        i_non_redundant.append(instance)
1834
      _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
1835
               instance, "instance has multiple secondary nodes: %s",
1836
               utils.CommaJoin(inst_config.secondary_nodes),
1837
               code=self.ETYPE_WARNING)
1838

    
1839
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1840
        i_non_a_balanced.append(instance)
1841

    
1842
      for snode in inst_config.secondary_nodes:
1843
        s_img = node_image[snode]
1844
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
1845
                 "instance %s, connection to secondary node failed", instance)
1846

    
1847
        if s_img.offline:
1848
          inst_nodes_offline.append(snode)
1849

    
1850
      # warn that the instance lives on offline nodes
1851
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
1852
               "instance lives on offline node(s) %s",
1853
               utils.CommaJoin(inst_nodes_offline))
1854
      # ... or ghost nodes
1855
      for node in inst_config.all_nodes:
1856
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
1857
                 "instance lives on ghost node %s", node)
1858

    
1859
    feedback_fn("* Verifying orphan volumes")
1860
    self._VerifyOrphanVolumes(node_vol_should, node_image)
1861

    
1862
    feedback_fn("* Verifying orphan instances")
1863
    self._VerifyOrphanInstances(instancelist, node_image)
1864

    
1865
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1866
      feedback_fn("* Verifying N+1 Memory redundancy")
1867
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
1868

    
1869
    feedback_fn("* Other Notes")
1870
    if i_non_redundant:
1871
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1872
                  % len(i_non_redundant))
1873

    
1874
    if i_non_a_balanced:
1875
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1876
                  % len(i_non_a_balanced))
1877

    
1878
    if n_offline:
1879
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
1880

    
1881
    if n_drained:
1882
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
1883

    
1884
    return not self.bad
1885

    
1886
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1887
    """Analyze the post-hooks' result
1888

1889
    This method analyses the hook result, handles it, and sends some
1890
    nicely-formatted feedback back to the user.
1891

1892
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1893
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1894
    @param hooks_results: the results of the multi-node hooks rpc call
1895
    @param feedback_fn: function used send feedback back to the caller
1896
    @param lu_result: previous Exec result
1897
    @return: the new Exec result, based on the previous result
1898
        and hook results
1899

1900
    """
1901
    # We only really run POST phase hooks, and are only interested in
1902
    # their results
1903
    if phase == constants.HOOKS_PHASE_POST:
1904
      # Used to change hooks' output to proper indentation
1905
      indent_re = re.compile('^', re.M)
1906
      feedback_fn("* Hooks Results")
1907
      assert hooks_results, "invalid result from hooks"
1908

    
1909
      for node_name in hooks_results:
1910
        res = hooks_results[node_name]
1911
        msg = res.fail_msg
1912
        test = msg and not res.offline
1913
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
1914
                      "Communication failure in hooks execution: %s", msg)
1915
        if res.offline or msg:
1916
          # No need to investigate payload if node is offline or gave an error.
1917
          # override manually lu_result here as _ErrorIf only
1918
          # overrides self.bad
1919
          lu_result = 1
1920
          continue
1921
        for script, hkr, output in res.payload:
1922
          test = hkr == constants.HKR_FAIL
1923
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
1924
                        "Script %s failed, output:", script)
1925
          if test:
1926
            output = indent_re.sub('      ', output)
1927
            feedback_fn("%s" % output)
1928
            lu_result = 0
1929

    
1930
      return lu_result
1931

    
1932

    
1933
class LUVerifyDisks(NoHooksLU):
1934
  """Verifies the cluster disks status.
1935

1936
  """
1937
  _OP_REQP = []
1938
  REQ_BGL = False
1939

    
1940
  def ExpandNames(self):
1941
    self.needed_locks = {
1942
      locking.LEVEL_NODE: locking.ALL_SET,
1943
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1944
    }
1945
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1946

    
1947
  def CheckPrereq(self):
1948
    """Check prerequisites.
1949

1950
    This has no prerequisites.
1951

1952
    """
1953
    pass
1954

    
1955
  def Exec(self, feedback_fn):
1956
    """Verify integrity of cluster disks.
1957

1958
    @rtype: tuple of three items
1959
    @return: a tuple of (dict of node-to-node_error, list of instances
1960
        which need activate-disks, dict of instance: (node, volume) for
1961
        missing volumes
1962

1963
    """
1964
    result = res_nodes, res_instances, res_missing = {}, [], {}
1965

    
1966
    vg_name = self.cfg.GetVGName()
1967
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1968
    instances = [self.cfg.GetInstanceInfo(name)
1969
                 for name in self.cfg.GetInstanceList()]
1970

    
1971
    nv_dict = {}
1972
    for inst in instances:
1973
      inst_lvs = {}
1974
      if (not inst.admin_up or
1975
          inst.disk_template not in constants.DTS_NET_MIRROR):
1976
        continue
1977
      inst.MapLVsByNode(inst_lvs)
1978
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1979
      for node, vol_list in inst_lvs.iteritems():
1980
        for vol in vol_list:
1981
          nv_dict[(node, vol)] = inst
1982

    
1983
    if not nv_dict:
1984
      return result
1985

    
1986
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1987

    
1988
    for node in nodes:
1989
      # node_volume
1990
      node_res = node_lvs[node]
1991
      if node_res.offline:
1992
        continue
1993
      msg = node_res.fail_msg
1994
      if msg:
1995
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1996
        res_nodes[node] = msg
1997
        continue
1998

    
1999
      lvs = node_res.payload
2000
      for lv_name, (_, _, lv_online) in lvs.items():
2001
        inst = nv_dict.pop((node, lv_name), None)
2002
        if (not lv_online and inst is not None
2003
            and inst.name not in res_instances):
2004
          res_instances.append(inst.name)
2005

    
2006
    # any leftover items in nv_dict are missing LVs, let's arrange the
2007
    # data better
2008
    for key, inst in nv_dict.iteritems():
2009
      if inst.name not in res_missing:
2010
        res_missing[inst.name] = []
2011
      res_missing[inst.name].append(key)
2012

    
2013
    return result
2014

    
2015

    
2016
class LURepairDiskSizes(NoHooksLU):
2017
  """Verifies the cluster disks sizes.
2018

2019
  """
2020
  _OP_REQP = ["instances"]
2021
  REQ_BGL = False
2022

    
2023
  def ExpandNames(self):
2024
    if not isinstance(self.op.instances, list):
2025
      raise errors.OpPrereqError("Invalid argument type 'instances'",
2026
                                 errors.ECODE_INVAL)
2027

    
2028
    if self.op.instances:
2029
      self.wanted_names = []
2030
      for name in self.op.instances:
2031
        full_name = _ExpandInstanceName(self.cfg, name)
2032
        self.wanted_names.append(full_name)
2033
      self.needed_locks = {
2034
        locking.LEVEL_NODE: [],
2035
        locking.LEVEL_INSTANCE: self.wanted_names,
2036
        }
2037
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2038
    else:
2039
      self.wanted_names = None
2040
      self.needed_locks = {
2041
        locking.LEVEL_NODE: locking.ALL_SET,
2042
        locking.LEVEL_INSTANCE: locking.ALL_SET,
2043
        }
2044
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
2045

    
2046
  def DeclareLocks(self, level):
2047
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2048
      self._LockInstancesNodes(primary_only=True)
2049

    
2050
  def CheckPrereq(self):
2051
    """Check prerequisites.
2052

2053
    This only checks the optional instance list against the existing names.
2054

2055
    """
2056
    if self.wanted_names is None:
2057
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2058

    
2059
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2060
                             in self.wanted_names]
2061

    
2062
  def _EnsureChildSizes(self, disk):
2063
    """Ensure children of the disk have the needed disk size.
2064

2065
    This is valid mainly for DRBD8 and fixes an issue where the
2066
    children have smaller disk size.
2067

2068
    @param disk: an L{ganeti.objects.Disk} object
2069

2070
    """
2071
    if disk.dev_type == constants.LD_DRBD8:
2072
      assert disk.children, "Empty children for DRBD8?"
2073
      fchild = disk.children[0]
2074
      mismatch = fchild.size < disk.size
2075
      if mismatch:
2076
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2077
                     fchild.size, disk.size)
2078
        fchild.size = disk.size
2079

    
2080
      # and we recurse on this child only, not on the metadev
2081
      return self._EnsureChildSizes(fchild) or mismatch
2082
    else:
2083
      return False
2084

    
2085
  def Exec(self, feedback_fn):
2086
    """Verify the size of cluster disks.
2087

2088
    """
2089
    # TODO: check child disks too
2090
    # TODO: check differences in size between primary/secondary nodes
2091
    per_node_disks = {}
2092
    for instance in self.wanted_instances:
2093
      pnode = instance.primary_node
2094
      if pnode not in per_node_disks:
2095
        per_node_disks[pnode] = []
2096
      for idx, disk in enumerate(instance.disks):
2097
        per_node_disks[pnode].append((instance, idx, disk))
2098

    
2099
    changed = []
2100
    for node, dskl in per_node_disks.items():
2101
      newl = [v[2].Copy() for v in dskl]
2102
      for dsk in newl:
2103
        self.cfg.SetDiskID(dsk, node)
2104
      result = self.rpc.call_blockdev_getsizes(node, newl)
2105
      if result.fail_msg:
2106
        self.LogWarning("Failure in blockdev_getsizes call to node"
2107
                        " %s, ignoring", node)
2108
        continue
2109
      if len(result.data) != len(dskl):
2110
        self.LogWarning("Invalid result from node %s, ignoring node results",
2111
                        node)
2112
        continue
2113
      for ((instance, idx, disk), size) in zip(dskl, result.data):
2114
        if size is None:
2115
          self.LogWarning("Disk %d of instance %s did not return size"
2116
                          " information, ignoring", idx, instance.name)
2117
          continue
2118
        if not isinstance(size, (int, long)):
2119
          self.LogWarning("Disk %d of instance %s did not return valid"
2120
                          " size information, ignoring", idx, instance.name)
2121
          continue
2122
        size = size >> 20
2123
        if size != disk.size:
2124
          self.LogInfo("Disk %d of instance %s has mismatched size,"
2125
                       " correcting: recorded %d, actual %d", idx,
2126
                       instance.name, disk.size, size)
2127
          disk.size = size
2128
          self.cfg.Update(instance, feedback_fn)
2129
          changed.append((instance.name, idx, size))
2130
        if self._EnsureChildSizes(disk):
2131
          self.cfg.Update(instance, feedback_fn)
2132
          changed.append((instance.name, idx, disk.size))
2133
    return changed
2134

    
2135

    
2136
class LURenameCluster(LogicalUnit):
2137
  """Rename the cluster.
2138

2139
  """
2140
  HPATH = "cluster-rename"
2141
  HTYPE = constants.HTYPE_CLUSTER
2142
  _OP_REQP = ["name"]
2143

    
2144
  def BuildHooksEnv(self):
2145
    """Build hooks env.
2146

2147
    """
2148
    env = {
2149
      "OP_TARGET": self.cfg.GetClusterName(),
2150
      "NEW_NAME": self.op.name,
2151
      }
2152
    mn = self.cfg.GetMasterNode()
2153
    all_nodes = self.cfg.GetNodeList()
2154
    return env, [mn], all_nodes
2155

    
2156
  def CheckPrereq(self):
2157
    """Verify that the passed name is a valid one.
2158

2159
    """
2160
    hostname = utils.GetHostInfo(self.op.name)
2161

    
2162
    new_name = hostname.name
2163
    self.ip = new_ip = hostname.ip
2164
    old_name = self.cfg.GetClusterName()
2165
    old_ip = self.cfg.GetMasterIP()
2166
    if new_name == old_name and new_ip == old_ip:
2167
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
2168
                                 " cluster has changed",
2169
                                 errors.ECODE_INVAL)
2170
    if new_ip != old_ip:
2171
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
2172
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
2173
                                   " reachable on the network. Aborting." %
2174
                                   new_ip, errors.ECODE_NOTUNIQUE)
2175

    
2176
    self.op.name = new_name
2177

    
2178
  def Exec(self, feedback_fn):
2179
    """Rename the cluster.
2180

2181
    """
2182
    clustername = self.op.name
2183
    ip = self.ip
2184

    
2185
    # shutdown the master IP
2186
    master = self.cfg.GetMasterNode()
2187
    result = self.rpc.call_node_stop_master(master, False)
2188
    result.Raise("Could not disable the master role")
2189

    
2190
    try:
2191
      cluster = self.cfg.GetClusterInfo()
2192
      cluster.cluster_name = clustername
2193
      cluster.master_ip = ip
2194
      self.cfg.Update(cluster, feedback_fn)
2195

    
2196
      # update the known hosts file
2197
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2198
      node_list = self.cfg.GetNodeList()
2199
      try:
2200
        node_list.remove(master)
2201
      except ValueError:
2202
        pass
2203
      result = self.rpc.call_upload_file(node_list,
2204
                                         constants.SSH_KNOWN_HOSTS_FILE)
2205
      for to_node, to_result in result.iteritems():
2206
        msg = to_result.fail_msg
2207
        if msg:
2208
          msg = ("Copy of file %s to node %s failed: %s" %
2209
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
2210
          self.proc.LogWarning(msg)
2211

    
2212
    finally:
2213
      result = self.rpc.call_node_start_master(master, False, False)
2214
      msg = result.fail_msg
2215
      if msg:
2216
        self.LogWarning("Could not re-enable the master role on"
2217
                        " the master, please restart manually: %s", msg)
2218

    
2219

    
2220
def _RecursiveCheckIfLVMBased(disk):
2221
  """Check if the given disk or its children are lvm-based.
2222

2223
  @type disk: L{objects.Disk}
2224
  @param disk: the disk to check
2225
  @rtype: boolean
2226
  @return: boolean indicating whether a LD_LV dev_type was found or not
2227

2228
  """
2229
  if disk.children:
2230
    for chdisk in disk.children:
2231
      if _RecursiveCheckIfLVMBased(chdisk):
2232
        return True
2233
  return disk.dev_type == constants.LD_LV
2234

    
2235

    
2236
class LUSetClusterParams(LogicalUnit):
2237
  """Change the parameters of the cluster.
2238

2239
  """
2240
  HPATH = "cluster-modify"
2241
  HTYPE = constants.HTYPE_CLUSTER
2242
  _OP_REQP = []
2243
  REQ_BGL = False
2244

    
2245
  def CheckArguments(self):
2246
    """Check parameters
2247

2248
    """
2249
    for attr in ["candidate_pool_size",
2250
                 "uid_pool", "add_uids", "remove_uids"]:
2251
      if not hasattr(self.op, attr):
2252
        setattr(self.op, attr, None)
2253

    
2254
    if self.op.candidate_pool_size is not None:
2255
      try:
2256
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
2257
      except (ValueError, TypeError), err:
2258
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
2259
                                   str(err), errors.ECODE_INVAL)
2260
      if self.op.candidate_pool_size < 1:
2261
        raise errors.OpPrereqError("At least one master candidate needed",
2262
                                   errors.ECODE_INVAL)
2263

    
2264
    _CheckBooleanOpField(self.op, "maintain_node_health")
2265

    
2266
    if self.op.uid_pool:
2267
      uidpool.CheckUidPool(self.op.uid_pool)
2268

    
2269
    if self.op.add_uids:
2270
      uidpool.CheckUidPool(self.op.add_uids)
2271

    
2272
    if self.op.remove_uids:
2273
      uidpool.CheckUidPool(self.op.remove_uids)
2274

    
2275
  def ExpandNames(self):
2276
    # FIXME: in the future maybe other cluster params won't require checking on
2277
    # all nodes to be modified.
2278
    self.needed_locks = {
2279
      locking.LEVEL_NODE: locking.ALL_SET,
2280
    }
2281
    self.share_locks[locking.LEVEL_NODE] = 1
2282

    
2283
  def BuildHooksEnv(self):
2284
    """Build hooks env.
2285

2286
    """
2287
    env = {
2288
      "OP_TARGET": self.cfg.GetClusterName(),
2289
      "NEW_VG_NAME": self.op.vg_name,
2290
      }
2291
    mn = self.cfg.GetMasterNode()
2292
    return env, [mn], [mn]
2293

    
2294
  def CheckPrereq(self):
2295
    """Check prerequisites.
2296

2297
    This checks whether the given params don't conflict and
2298
    if the given volume group is valid.
2299

2300
    """
2301
    if self.op.vg_name is not None and not self.op.vg_name:
2302
      instances = self.cfg.GetAllInstancesInfo().values()
2303
      for inst in instances:
2304
        for disk in inst.disks:
2305
          if _RecursiveCheckIfLVMBased(disk):
2306
            raise errors.OpPrereqError("Cannot disable lvm storage while"
2307
                                       " lvm-based instances exist",
2308
                                       errors.ECODE_INVAL)
2309

    
2310
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2311

    
2312
    # if vg_name not None, checks given volume group on all nodes
2313
    if self.op.vg_name:
2314
      vglist = self.rpc.call_vg_list(node_list)
2315
      for node in node_list:
2316
        msg = vglist[node].fail_msg
2317
        if msg:
2318
          # ignoring down node
2319
          self.LogWarning("Error while gathering data on node %s"
2320
                          " (ignoring node): %s", node, msg)
2321
          continue
2322
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2323
                                              self.op.vg_name,
2324
                                              constants.MIN_VG_SIZE)
2325
        if vgstatus:
2326
          raise errors.OpPrereqError("Error on node '%s': %s" %
2327
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2328

    
2329
    self.cluster = cluster = self.cfg.GetClusterInfo()
2330
    # validate params changes
2331
    if self.op.beparams:
2332
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2333
      self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
2334

    
2335
    if self.op.nicparams:
2336
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2337
      self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
2338
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2339
      nic_errors = []
2340

    
2341
      # check all instances for consistency
2342
      for instance in self.cfg.GetAllInstancesInfo().values():
2343
        for nic_idx, nic in enumerate(instance.nics):
2344
          params_copy = copy.deepcopy(nic.nicparams)
2345
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2346

    
2347
          # check parameter syntax
2348
          try:
2349
            objects.NIC.CheckParameterSyntax(params_filled)
2350
          except errors.ConfigurationError, err:
2351
            nic_errors.append("Instance %s, nic/%d: %s" %
2352
                              (instance.name, nic_idx, err))
2353

    
2354
          # if we're moving instances to routed, check that they have an ip
2355
          target_mode = params_filled[constants.NIC_MODE]
2356
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2357
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2358
                              (instance.name, nic_idx))
2359
      if nic_errors:
2360
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2361
                                   "\n".join(nic_errors))
2362

    
2363
    # hypervisor list/parameters
2364
    self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
2365
    if self.op.hvparams:
2366
      if not isinstance(self.op.hvparams, dict):
2367
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input",
2368
                                   errors.ECODE_INVAL)
2369
      for hv_name, hv_dict in self.op.hvparams.items():
2370
        if hv_name not in self.new_hvparams:
2371
          self.new_hvparams[hv_name] = hv_dict
2372
        else:
2373
          self.new_hvparams[hv_name].update(hv_dict)
2374

    
2375
    # os hypervisor parameters
2376
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2377
    if self.op.os_hvp:
2378
      if not isinstance(self.op.os_hvp, dict):
2379
        raise errors.OpPrereqError("Invalid 'os_hvp' parameter on input",
2380
                                   errors.ECODE_INVAL)
2381
      for os_name, hvs in self.op.os_hvp.items():
2382
        if not isinstance(hvs, dict):
2383
          raise errors.OpPrereqError(("Invalid 'os_hvp' parameter on"
2384
                                      " input"), errors.ECODE_INVAL)
2385
        if os_name not in self.new_os_hvp:
2386
          self.new_os_hvp[os_name] = hvs
2387
        else:
2388
          for hv_name, hv_dict in hvs.items():
2389
            if hv_name not in self.new_os_hvp[os_name]:
2390
              self.new_os_hvp[os_name][hv_name] = hv_dict
2391
            else:
2392
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2393

    
2394
    # changes to the hypervisor list
2395
    if self.op.enabled_hypervisors is not None:
2396
      self.hv_list = self.op.enabled_hypervisors
2397
      if not self.hv_list:
2398
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
2399
                                   " least one member",
2400
                                   errors.ECODE_INVAL)
2401
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
2402
      if invalid_hvs:
2403
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
2404
                                   " entries: %s" %
2405
                                   utils.CommaJoin(invalid_hvs),
2406
                                   errors.ECODE_INVAL)
2407
      for hv in self.hv_list:
2408
        # if the hypervisor doesn't already exist in the cluster
2409
        # hvparams, we initialize it to empty, and then (in both
2410
        # cases) we make sure to fill the defaults, as we might not
2411
        # have a complete defaults list if the hypervisor wasn't
2412
        # enabled before
2413
        if hv not in new_hvp:
2414
          new_hvp[hv] = {}
2415
        new_hvp[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], new_hvp[hv])
2416
        utils.ForceDictType(new_hvp[hv], constants.HVS_PARAMETER_TYPES)
2417
    else:
2418
      self.hv_list = cluster.enabled_hypervisors
2419

    
2420
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2421
      # either the enabled list has changed, or the parameters have, validate
2422
      for hv_name, hv_params in self.new_hvparams.items():
2423
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2424
            (self.op.enabled_hypervisors and
2425
             hv_name in self.op.enabled_hypervisors)):
2426
          # either this is a new hypervisor, or its parameters have changed
2427
          hv_class = hypervisor.GetHypervisor(hv_name)
2428
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2429
          hv_class.CheckParameterSyntax(hv_params)
2430
          _CheckHVParams(self, node_list, hv_name, hv_params)
2431

    
2432
    if self.op.os_hvp:
2433
      # no need to check any newly-enabled hypervisors, since the
2434
      # defaults have already been checked in the above code-block
2435
      for os_name, os_hvp in self.new_os_hvp.items():
2436
        for hv_name, hv_params in os_hvp.items():
2437
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2438
          # we need to fill in the new os_hvp on top of the actual hv_p
2439
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2440
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2441
          hv_class = hypervisor.GetHypervisor(hv_name)
2442
          hv_class.CheckParameterSyntax(new_osp)
2443
          _CheckHVParams(self, node_list, hv_name, new_osp)
2444

    
2445

    
2446
  def Exec(self, feedback_fn):
2447
    """Change the parameters of the cluster.
2448

2449
    """
2450
    if self.op.vg_name is not None:
2451
      new_volume = self.op.vg_name
2452
      if not new_volume:
2453
        new_volume = None
2454
      if new_volume != self.cfg.GetVGName():
2455
        self.cfg.SetVGName(new_volume)
2456
      else:
2457
        feedback_fn("Cluster LVM configuration already in desired"
2458
                    " state, not changing")
2459
    if self.op.hvparams:
2460
      self.cluster.hvparams = self.new_hvparams
2461
    if self.op.os_hvp:
2462
      self.cluster.os_hvp = self.new_os_hvp
2463
    if self.op.enabled_hypervisors is not None:
2464
      self.cluster.hvparams = self.new_hvparams
2465
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2466
    if self.op.beparams:
2467
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2468
    if self.op.nicparams:
2469
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2470

    
2471
    if self.op.candidate_pool_size is not None:
2472
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2473
      # we need to update the pool size here, otherwise the save will fail
2474
      _AdjustCandidatePool(self, [])
2475

    
2476
    if self.op.maintain_node_health is not None:
2477
      self.cluster.maintain_node_health = self.op.maintain_node_health
2478

    
2479
    if self.op.add_uids is not None:
2480
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
2481

    
2482
    if self.op.remove_uids is not None:
2483
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
2484

    
2485
    if self.op.uid_pool is not None:
2486
      self.cluster.uid_pool = self.op.uid_pool
2487

    
2488
    self.cfg.Update(self.cluster, feedback_fn)
2489

    
2490

    
2491
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2492
  """Distribute additional files which are part of the cluster configuration.
2493

2494
  ConfigWriter takes care of distributing the config and ssconf files, but
2495
  there are more files which should be distributed to all nodes. This function
2496
  makes sure those are copied.
2497

2498
  @param lu: calling logical unit
2499
  @param additional_nodes: list of nodes not in the config to distribute to
2500

2501
  """
2502
  # 1. Gather target nodes
2503
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2504
  dist_nodes = lu.cfg.GetOnlineNodeList()
2505
  if additional_nodes is not None:
2506
    dist_nodes.extend(additional_nodes)
2507
  if myself.name in dist_nodes:
2508
    dist_nodes.remove(myself.name)
2509

    
2510
  # 2. Gather files to distribute
2511
  dist_files = set([constants.ETC_HOSTS,
2512
                    constants.SSH_KNOWN_HOSTS_FILE,
2513
                    constants.RAPI_CERT_FILE,
2514
                    constants.RAPI_USERS_FILE,
2515
                    constants.CONFD_HMAC_KEY,
2516
                    constants.CLUSTER_DOMAIN_SECRET_FILE,
2517
                   ])
2518

    
2519
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2520
  for hv_name in enabled_hypervisors:
2521
    hv_class = hypervisor.GetHypervisor(hv_name)
2522
    dist_files.update(hv_class.GetAncillaryFiles())
2523

    
2524
  # 3. Perform the files upload
2525
  for fname in dist_files:
2526
    if os.path.exists(fname):
2527
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2528
      for to_node, to_result in result.items():
2529
        msg = to_result.fail_msg
2530
        if msg:
2531
          msg = ("Copy of file %s to node %s failed: %s" %
2532
                 (fname, to_node, msg))
2533
          lu.proc.LogWarning(msg)
2534

    
2535

    
2536
class LURedistributeConfig(NoHooksLU):
2537
  """Force the redistribution of cluster configuration.
2538

2539
  This is a very simple LU.
2540

2541
  """
2542
  _OP_REQP = []
2543
  REQ_BGL = False
2544

    
2545
  def ExpandNames(self):
2546
    self.needed_locks = {
2547
      locking.LEVEL_NODE: locking.ALL_SET,
2548
    }
2549
    self.share_locks[locking.LEVEL_NODE] = 1
2550

    
2551
  def CheckPrereq(self):
2552
    """Check prerequisites.
2553

2554
    """
2555

    
2556
  def Exec(self, feedback_fn):
2557
    """Redistribute the configuration.
2558

2559
    """
2560
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2561
    _RedistributeAncillaryFiles(self)
2562

    
2563

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

2567
  """
2568
  if not instance.disks or disks is not None and not disks:
2569
    return True
2570

    
2571
  disks = _ExpandCheckDisks(instance, disks)
2572

    
2573
  if not oneshot:
2574
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2575

    
2576
  node = instance.primary_node
2577

    
2578
  for dev in disks:
2579
    lu.cfg.SetDiskID(dev, node)
2580

    
2581
  # TODO: Convert to utils.Retry
2582

    
2583
  retries = 0
2584
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2585
  while True:
2586
    max_time = 0
2587
    done = True
2588
    cumul_degraded = False
2589
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, disks)
2590
    msg = rstats.fail_msg
2591
    if msg:
2592
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2593
      retries += 1
2594
      if retries >= 10:
2595
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2596
                                 " aborting." % node)
2597
      time.sleep(6)
2598
      continue
2599
    rstats = rstats.payload
2600
    retries = 0
2601
    for i, mstat in enumerate(rstats):
2602
      if mstat is None:
2603
        lu.LogWarning("Can't compute data for node %s/%s",
2604
                           node, disks[i].iv_name)
2605
        continue
2606

    
2607
      cumul_degraded = (cumul_degraded or
2608
                        (mstat.is_degraded and mstat.sync_percent is None))
2609
      if mstat.sync_percent is not None:
2610
        done = False
2611
        if mstat.estimated_time is not None:
2612
          rem_time = ("%s remaining (estimated)" %
2613
                      utils.FormatSeconds(mstat.estimated_time))
2614
          max_time = mstat.estimated_time
2615
        else:
2616
          rem_time = "no time estimate"
2617
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2618
                        (disks[i].iv_name, mstat.sync_percent, rem_time))
2619

    
2620
    # if we're done but degraded, let's do a few small retries, to
2621
    # make sure we see a stable and not transient situation; therefore
2622
    # we force restart of the loop
2623
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2624
      logging.info("Degraded disks found, %d retries left", degr_retries)
2625
      degr_retries -= 1
2626
      time.sleep(1)
2627
      continue
2628

    
2629
    if done or oneshot:
2630
      break
2631

    
2632
    time.sleep(min(60, max_time))
2633

    
2634
  if done:
2635
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2636
  return not cumul_degraded
2637

    
2638

    
2639
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2640
  """Check that mirrors are not degraded.
2641

2642
  The ldisk parameter, if True, will change the test from the
2643
  is_degraded attribute (which represents overall non-ok status for
2644
  the device(s)) to the ldisk (representing the local storage status).
2645

2646
  """
2647
  lu.cfg.SetDiskID(dev, node)
2648

    
2649
  result = True
2650

    
2651
  if on_primary or dev.AssembleOnSecondary():
2652
    rstats = lu.rpc.call_blockdev_find(node, dev)
2653
    msg = rstats.fail_msg
2654
    if msg:
2655
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2656
      result = False
2657
    elif not rstats.payload:
2658
      lu.LogWarning("Can't find disk on node %s", node)
2659
      result = False
2660
    else:
2661
      if ldisk:
2662
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2663
      else:
2664
        result = result and not rstats.payload.is_degraded
2665

    
2666
  if dev.children:
2667
    for child in dev.children:
2668
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2669

    
2670
  return result
2671

    
2672

    
2673
class LUDiagnoseOS(NoHooksLU):
2674
  """Logical unit for OS diagnose/query.
2675

2676
  """
2677
  _OP_REQP = ["output_fields", "names"]
2678
  REQ_BGL = False
2679
  _FIELDS_STATIC = utils.FieldSet()
2680
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants")
2681
  # Fields that need calculation of global os validity
2682
  _FIELDS_NEEDVALID = frozenset(["valid", "variants"])
2683

    
2684
  def ExpandNames(self):
2685
    if self.op.names:
2686
      raise errors.OpPrereqError("Selective OS query not supported",
2687
                                 errors.ECODE_INVAL)
2688

    
2689
    _CheckOutputFields(static=self._FIELDS_STATIC,
2690
                       dynamic=self._FIELDS_DYNAMIC,
2691
                       selected=self.op.output_fields)
2692

    
2693
    # Lock all nodes, in shared mode
2694
    # Temporary removal of locks, should be reverted later
2695
    # TODO: reintroduce locks when they are lighter-weight
2696
    self.needed_locks = {}
2697
    #self.share_locks[locking.LEVEL_NODE] = 1
2698
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2699

    
2700
  def CheckPrereq(self):
2701
    """Check prerequisites.
2702

2703
    """
2704

    
2705
  @staticmethod
2706
  def _DiagnoseByOS(rlist):
2707
    """Remaps a per-node return list into an a per-os per-node dictionary
2708

2709
    @param rlist: a map with node names as keys and OS objects as values
2710

2711
    @rtype: dict
2712
    @return: a dictionary with osnames as keys and as value another map, with
2713
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2714

2715
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2716
                                     (/srv/..., False, "invalid api")],
2717
                           "node2": [(/srv/..., True, "")]}
2718
          }
2719

2720
    """
2721
    all_os = {}
2722
    # we build here the list of nodes that didn't fail the RPC (at RPC
2723
    # level), so that nodes with a non-responding node daemon don't
2724
    # make all OSes invalid
2725
    good_nodes = [node_name for node_name in rlist
2726
                  if not rlist[node_name].fail_msg]
2727
    for node_name, nr in rlist.items():
2728
      if nr.fail_msg or not nr.payload:
2729
        continue
2730
      for name, path, status, diagnose, variants in nr.payload:
2731
        if name not in all_os:
2732
          # build a list of nodes for this os containing empty lists
2733
          # for each node in node_list
2734
          all_os[name] = {}
2735
          for nname in good_nodes:
2736
            all_os[name][nname] = []
2737
        all_os[name][node_name].append((path, status, diagnose, variants))
2738
    return all_os
2739

    
2740
  def Exec(self, feedback_fn):
2741
    """Compute the list of OSes.
2742

2743
    """
2744
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2745
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2746
    pol = self._DiagnoseByOS(node_data)
2747
    output = []
2748
    calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields)
2749
    calc_variants = "variants" in self.op.output_fields
2750

    
2751
    for os_name, os_data in pol.items():
2752
      row = []
2753
      if calc_valid:
2754
        valid = True
2755
        variants = None
2756
        for osl in os_data.values():
2757
          valid = valid and osl and osl[0][1]
2758
          if not valid:
2759
            variants = set()
2760
            break
2761
          if calc_variants:
2762
            node_variants = osl[0][3]
2763
            if variants is None:
2764
              variants = set(node_variants)
2765
            else:
2766
              variants.intersection_update(node_variants)
2767

    
2768
      for field in self.op.output_fields:
2769
        if field == "name":
2770
          val = os_name
2771
        elif field == "valid":
2772
          val = valid
2773
        elif field == "node_status":
2774
          # this is just a copy of the dict
2775
          val = {}
2776
          for node_name, nos_list in os_data.items():
2777
            val[node_name] = nos_list
2778
        elif field == "variants":
2779
          val = list(variants)
2780
        else:
2781
          raise errors.ParameterError(field)
2782
        row.append(val)
2783
      output.append(row)
2784

    
2785
    return output
2786

    
2787

    
2788
class LURemoveNode(LogicalUnit):
2789
  """Logical unit for removing a node.
2790

2791
  """
2792
  HPATH = "node-remove"
2793
  HTYPE = constants.HTYPE_NODE
2794
  _OP_REQP = ["node_name"]
2795

    
2796
  def BuildHooksEnv(self):
2797
    """Build hooks env.
2798

2799
    This doesn't run on the target node in the pre phase as a failed
2800
    node would then be impossible to remove.
2801

2802
    """
2803
    env = {
2804
      "OP_TARGET": self.op.node_name,
2805
      "NODE_NAME": self.op.node_name,
2806
      }
2807
    all_nodes = self.cfg.GetNodeList()
2808
    try:
2809
      all_nodes.remove(self.op.node_name)
2810
    except ValueError:
2811
      logging.warning("Node %s which is about to be removed not found"
2812
                      " in the all nodes list", self.op.node_name)
2813
    return env, all_nodes, all_nodes
2814

    
2815
  def CheckPrereq(self):
2816
    """Check prerequisites.
2817

2818
    This checks:
2819
     - the node exists in the configuration
2820
     - it does not have primary or secondary instances
2821
     - it's not the master
2822

2823
    Any errors are signaled by raising errors.OpPrereqError.
2824

2825
    """
2826
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
2827
    node = self.cfg.GetNodeInfo(self.op.node_name)
2828
    assert node is not None
2829

    
2830
    instance_list = self.cfg.GetInstanceList()
2831

    
2832
    masternode = self.cfg.GetMasterNode()
2833
    if node.name == masternode:
2834
      raise errors.OpPrereqError("Node is the master node,"
2835
                                 " you need to failover first.",
2836
                                 errors.ECODE_INVAL)
2837

    
2838
    for instance_name in instance_list:
2839
      instance = self.cfg.GetInstanceInfo(instance_name)
2840
      if node.name in instance.all_nodes:
2841
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2842
                                   " please remove first." % instance_name,
2843
                                   errors.ECODE_INVAL)
2844
    self.op.node_name = node.name
2845
    self.node = node
2846

    
2847
  def Exec(self, feedback_fn):
2848
    """Removes the node from the cluster.
2849

2850
    """
2851
    node = self.node
2852
    logging.info("Stopping the node daemon and removing configs from node %s",
2853
                 node.name)
2854

    
2855
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
2856

    
2857
    # Promote nodes to master candidate as needed
2858
    _AdjustCandidatePool(self, exceptions=[node.name])
2859
    self.context.RemoveNode(node.name)
2860

    
2861
    # Run post hooks on the node before it's removed
2862
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
2863
    try:
2864
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
2865
    except:
2866
      # pylint: disable-msg=W0702
2867
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
2868

    
2869
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
2870
    msg = result.fail_msg
2871
    if msg:
2872
      self.LogWarning("Errors encountered on the remote node while leaving"
2873
                      " the cluster: %s", msg)
2874

    
2875
    # Remove node from our /etc/hosts
2876
    if self.cfg.GetClusterInfo().modify_etc_hosts:
2877
      # FIXME: this should be done via an rpc call to node daemon
2878
      utils.RemoveHostFromEtcHosts(node.name)
2879
      _RedistributeAncillaryFiles(self)
2880

    
2881

    
2882
class LUQueryNodes(NoHooksLU):
2883
  """Logical unit for querying nodes.
2884

2885
  """
2886
  # pylint: disable-msg=W0142
2887
  _OP_REQP = ["output_fields", "names", "use_locking"]
2888
  REQ_BGL = False
2889

    
2890
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
2891
                    "master_candidate", "offline", "drained"]
2892

    
2893
  _FIELDS_DYNAMIC = utils.FieldSet(
2894
    "dtotal", "dfree",
2895
    "mtotal", "mnode", "mfree",
2896
    "bootid",
2897
    "ctotal", "cnodes", "csockets",
2898
    )
2899

    
2900
  _FIELDS_STATIC = utils.FieldSet(*[
2901
    "pinst_cnt", "sinst_cnt",
2902
    "pinst_list", "sinst_list",
2903
    "pip", "sip", "tags",
2904
    "master",
2905
    "role"] + _SIMPLE_FIELDS
2906
    )
2907

    
2908
  def ExpandNames(self):
2909
    _CheckOutputFields(static=self._FIELDS_STATIC,
2910
                       dynamic=self._FIELDS_DYNAMIC,
2911
                       selected=self.op.output_fields)
2912

    
2913
    self.needed_locks = {}
2914
    self.share_locks[locking.LEVEL_NODE] = 1
2915

    
2916
    if self.op.names:
2917
      self.wanted = _GetWantedNodes(self, self.op.names)
2918
    else:
2919
      self.wanted = locking.ALL_SET
2920

    
2921
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2922
    self.do_locking = self.do_node_query and self.op.use_locking
2923
    if self.do_locking:
2924
      # if we don't request only static fields, we need to lock the nodes
2925
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2926

    
2927
  def CheckPrereq(self):
2928
    """Check prerequisites.
2929

2930
    """
2931
    # The validation of the node list is done in the _GetWantedNodes,
2932
    # if non empty, and if empty, there's no validation to do
2933
    pass
2934

    
2935
  def Exec(self, feedback_fn):
2936
    """Computes the list of nodes and their attributes.
2937

2938
    """
2939
    all_info = self.cfg.GetAllNodesInfo()
2940
    if self.do_locking:
2941
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2942
    elif self.wanted != locking.ALL_SET:
2943
      nodenames = self.wanted
2944
      missing = set(nodenames).difference(all_info.keys())
2945
      if missing:
2946
        raise errors.OpExecError(
2947
          "Some nodes were removed before retrieving their data: %s" % missing)
2948
    else:
2949
      nodenames = all_info.keys()
2950

    
2951
    nodenames = utils.NiceSort(nodenames)
2952
    nodelist = [all_info[name] for name in nodenames]
2953

    
2954
    # begin data gathering
2955

    
2956
    if self.do_node_query:
2957
      live_data = {}
2958
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2959
                                          self.cfg.GetHypervisorType())
2960
      for name in nodenames:
2961
        nodeinfo = node_data[name]
2962
        if not nodeinfo.fail_msg and nodeinfo.payload:
2963
          nodeinfo = nodeinfo.payload
2964
          fn = utils.TryConvert
2965
          live_data[name] = {
2966
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2967
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2968
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2969
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2970
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2971
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2972
            "bootid": nodeinfo.get('bootid', None),
2973
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2974
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2975
            }
2976
        else:
2977
          live_data[name] = {}
2978
    else:
2979
      live_data = dict.fromkeys(nodenames, {})
2980

    
2981
    node_to_primary = dict([(name, set()) for name in nodenames])
2982
    node_to_secondary = dict([(name, set()) for name in nodenames])
2983

    
2984
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2985
                             "sinst_cnt", "sinst_list"))
2986
    if inst_fields & frozenset(self.op.output_fields):
2987
      inst_data = self.cfg.GetAllInstancesInfo()
2988

    
2989
      for inst in inst_data.values():
2990
        if inst.primary_node in node_to_primary:
2991
          node_to_primary[inst.primary_node].add(inst.name)
2992
        for secnode in inst.secondary_nodes:
2993
          if secnode in node_to_secondary:
2994
            node_to_secondary[secnode].add(inst.name)
2995

    
2996
    master_node = self.cfg.GetMasterNode()
2997

    
2998
    # end data gathering
2999

    
3000
    output = []
3001
    for node in nodelist:
3002
      node_output = []
3003
      for field in self.op.output_fields:
3004
        if field in self._SIMPLE_FIELDS:
3005
          val = getattr(node, field)
3006
        elif field == "pinst_list":
3007
          val = list(node_to_primary[node.name])
3008
        elif field == "sinst_list":
3009
          val = list(node_to_secondary[node.name])
3010
        elif field == "pinst_cnt":
3011
          val = len(node_to_primary[node.name])
3012
        elif field == "sinst_cnt":
3013
          val = len(node_to_secondary[node.name])
3014
        elif field == "pip":
3015
          val = node.primary_ip
3016
        elif field == "sip":
3017
          val = node.secondary_ip
3018
        elif field == "tags":
3019
          val = list(node.GetTags())
3020
        elif field == "master":
3021
          val = node.name == master_node
3022
        elif self._FIELDS_DYNAMIC.Matches(field):
3023
          val = live_data[node.name].get(field, None)
3024
        elif field == "role":
3025
          if node.name == master_node:
3026
            val = "M"
3027
          elif node.master_candidate:
3028
            val = "C"
3029
          elif node.drained:
3030
            val = "D"
3031
          elif node.offline:
3032
            val = "O"
3033
          else:
3034
            val = "R"
3035
        else:
3036
          raise errors.ParameterError(field)
3037
        node_output.append(val)
3038
      output.append(node_output)
3039

    
3040
    return output
3041

    
3042

    
3043
class LUQueryNodeVolumes(NoHooksLU):
3044
  """Logical unit for getting volumes on node(s).
3045

3046
  """
3047
  _OP_REQP = ["nodes", "output_fields"]
3048
  REQ_BGL = False
3049
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3050
  _FIELDS_STATIC = utils.FieldSet("node")
3051

    
3052
  def ExpandNames(self):
3053
    _CheckOutputFields(static=self._FIELDS_STATIC,
3054
                       dynamic=self._FIELDS_DYNAMIC,
3055
                       selected=self.op.output_fields)
3056

    
3057
    self.needed_locks = {}
3058
    self.share_locks[locking.LEVEL_NODE] = 1
3059
    if not self.op.nodes:
3060
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3061
    else:
3062
      self.needed_locks[locking.LEVEL_NODE] = \
3063
        _GetWantedNodes(self, self.op.nodes)
3064

    
3065
  def CheckPrereq(self):
3066
    """Check prerequisites.
3067

3068
    This checks that the fields required are valid output fields.
3069

3070
    """
3071
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3072

    
3073
  def Exec(self, feedback_fn):
3074
    """Computes the list of nodes and their attributes.
3075

3076
    """
3077
    nodenames = self.nodes
3078
    volumes = self.rpc.call_node_volumes(nodenames)
3079

    
3080
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3081
             in self.cfg.GetInstanceList()]
3082

    
3083
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3084

    
3085
    output = []
3086
    for node in nodenames:
3087
      nresult = volumes[node]
3088
      if nresult.offline:
3089
        continue
3090
      msg = nresult.fail_msg
3091
      if msg:
3092
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3093
        continue
3094

    
3095
      node_vols = nresult.payload[:]
3096
      node_vols.sort(key=lambda vol: vol['dev'])
3097

    
3098
      for vol in node_vols:
3099
        node_output = []
3100
        for field in self.op.output_fields:
3101
          if field == "node":
3102
            val = node
3103
          elif field == "phys":
3104
            val = vol['dev']
3105
          elif field == "vg":
3106
            val = vol['vg']
3107
          elif field == "name":
3108
            val = vol['name']
3109
          elif field == "size":
3110
            val = int(float(vol['size']))
3111
          elif field == "instance":
3112
            for inst in ilist:
3113
              if node not in lv_by_node[inst]:
3114
                continue
3115
              if vol['name'] in lv_by_node[inst][node]:
3116
                val = inst.name
3117
                break
3118
            else:
3119
              val = '-'
3120
          else:
3121
            raise errors.ParameterError(field)
3122
          node_output.append(str(val))
3123

    
3124
        output.append(node_output)
3125

    
3126
    return output
3127

    
3128

    
3129
class LUQueryNodeStorage(NoHooksLU):
3130
  """Logical unit for getting information on storage units on node(s).
3131

3132
  """
3133
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
3134
  REQ_BGL = False
3135
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3136

    
3137
  def CheckArguments(self):
3138
    _CheckStorageType(self.op.storage_type)
3139

    
3140
    _CheckOutputFields(static=self._FIELDS_STATIC,
3141
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3142
                       selected=self.op.output_fields)
3143

    
3144
  def ExpandNames(self):
3145
    self.needed_locks = {}
3146
    self.share_locks[locking.LEVEL_NODE] = 1
3147

    
3148
    if self.op.nodes:
3149
      self.needed_locks[locking.LEVEL_NODE] = \
3150
        _GetWantedNodes(self, self.op.nodes)
3151
    else:
3152
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3153

    
3154
  def CheckPrereq(self):
3155
    """Check prerequisites.
3156

3157
    This checks that the fields required are valid output fields.
3158

3159
    """
3160
    self.op.name = getattr(self.op, "name", None)
3161

    
3162
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3163

    
3164
  def Exec(self, feedback_fn):
3165
    """Computes the list of nodes and their attributes.
3166

3167
    """
3168
    # Always get name to sort by
3169
    if constants.SF_NAME in self.op.output_fields:
3170
      fields = self.op.output_fields[:]
3171
    else:
3172
      fields = [constants.SF_NAME] + self.op.output_fields
3173

    
3174
    # Never ask for node or type as it's only known to the LU
3175
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3176
      while extra in fields:
3177
        fields.remove(extra)
3178

    
3179
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3180
    name_idx = field_idx[constants.SF_NAME]
3181

    
3182
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3183
    data = self.rpc.call_storage_list(self.nodes,
3184
                                      self.op.storage_type, st_args,
3185
                                      self.op.name, fields)
3186

    
3187
    result = []
3188

    
3189
    for node in utils.NiceSort(self.nodes):
3190
      nresult = data[node]
3191
      if nresult.offline:
3192
        continue
3193

    
3194
      msg = nresult.fail_msg
3195
      if msg:
3196
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3197
        continue
3198

    
3199
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3200

    
3201
      for name in utils.NiceSort(rows.keys()):
3202
        row = rows[name]
3203

    
3204
        out = []
3205

    
3206
        for field in self.op.output_fields:
3207
          if field == constants.SF_NODE:
3208
            val = node
3209
          elif field == constants.SF_TYPE:
3210
            val = self.op.storage_type
3211
          elif field in field_idx:
3212
            val = row[field_idx[field]]
3213
          else:
3214
            raise errors.ParameterError(field)
3215

    
3216
          out.append(val)
3217

    
3218
        result.append(out)
3219

    
3220
    return result
3221

    
3222

    
3223
class LUModifyNodeStorage(NoHooksLU):
3224
  """Logical unit for modifying a storage volume on a node.
3225

3226
  """
3227
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
3228
  REQ_BGL = False
3229

    
3230
  def CheckArguments(self):
3231
    self.opnode_name = _ExpandNodeName(self.cfg, self.op.node_name)
3232

    
3233
    _CheckStorageType(self.op.storage_type)
3234

    
3235
  def ExpandNames(self):
3236
    self.needed_locks = {
3237
      locking.LEVEL_NODE: self.op.node_name,
3238
      }
3239

    
3240
  def CheckPrereq(self):
3241
    """Check prerequisites.
3242

3243
    """
3244
    storage_type = self.op.storage_type
3245

    
3246
    try:
3247
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3248
    except KeyError:
3249
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3250
                                 " modified" % storage_type,
3251
                                 errors.ECODE_INVAL)
3252

    
3253
    diff = set(self.op.changes.keys()) - modifiable
3254
    if diff:
3255
      raise errors.OpPrereqError("The following fields can not be modified for"
3256
                                 " storage units of type '%s': %r" %
3257
                                 (storage_type, list(diff)),
3258
                                 errors.ECODE_INVAL)
3259

    
3260
  def Exec(self, feedback_fn):
3261
    """Computes the list of nodes and their attributes.
3262

3263
    """
3264
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3265
    result = self.rpc.call_storage_modify(self.op.node_name,
3266
                                          self.op.storage_type, st_args,
3267
                                          self.op.name, self.op.changes)
3268
    result.Raise("Failed to modify storage unit '%s' on %s" %
3269
                 (self.op.name, self.op.node_name))
3270

    
3271

    
3272
class LUAddNode(LogicalUnit):
3273
  """Logical unit for adding node to the cluster.
3274

3275
  """
3276
  HPATH = "node-add"
3277
  HTYPE = constants.HTYPE_NODE
3278
  _OP_REQP = ["node_name"]
3279

    
3280
  def CheckArguments(self):
3281
    # validate/normalize the node name
3282
    self.op.node_name = utils.HostInfo.NormalizeName(self.op.node_name)
3283

    
3284
  def BuildHooksEnv(self):
3285
    """Build hooks env.
3286

3287
    This will run on all nodes before, and on all nodes + the new node after.
3288

3289
    """
3290
    env = {
3291
      "OP_TARGET": self.op.node_name,
3292
      "NODE_NAME": self.op.node_name,
3293
      "NODE_PIP": self.op.primary_ip,
3294
      "NODE_SIP": self.op.secondary_ip,
3295
      }
3296
    nodes_0 = self.cfg.GetNodeList()
3297
    nodes_1 = nodes_0 + [self.op.node_name, ]
3298
    return env, nodes_0, nodes_1
3299

    
3300
  def CheckPrereq(self):
3301
    """Check prerequisites.
3302

3303
    This checks:
3304
     - the new node is not already in the config
3305
     - it is resolvable
3306
     - its parameters (single/dual homed) matches the cluster
3307

3308
    Any errors are signaled by raising errors.OpPrereqError.
3309

3310
    """
3311
    node_name = self.op.node_name
3312
    cfg = self.cfg
3313

    
3314
    dns_data = utils.GetHostInfo(node_name)
3315

    
3316
    node = dns_data.name
3317
    primary_ip = self.op.primary_ip = dns_data.ip
3318
    secondary_ip = getattr(self.op, "secondary_ip", None)
3319
    if secondary_ip is None:
3320
      secondary_ip = primary_ip
3321
    if not utils.IsValidIP(secondary_ip):
3322
      raise errors.OpPrereqError("Invalid secondary IP given",
3323
                                 errors.ECODE_INVAL)
3324
    self.op.secondary_ip = secondary_ip
3325

    
3326
    node_list = cfg.GetNodeList()
3327
    if not self.op.readd and node in node_list:
3328
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3329
                                 node, errors.ECODE_EXISTS)
3330
    elif self.op.readd and node not in node_list:
3331
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3332
                                 errors.ECODE_NOENT)
3333

    
3334
    self.changed_primary_ip = False
3335

    
3336
    for existing_node_name in node_list:
3337
      existing_node = cfg.GetNodeInfo(existing_node_name)
3338

    
3339
      if self.op.readd and node == existing_node_name:
3340
        if existing_node.secondary_ip != secondary_ip:
3341
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3342
                                     " address configuration as before",
3343
                                     errors.ECODE_INVAL)
3344
        if existing_node.primary_ip != primary_ip:
3345
          self.changed_primary_ip = True
3346

    
3347
        continue
3348

    
3349
      if (existing_node.primary_ip == primary_ip or
3350
          existing_node.secondary_ip == primary_ip or
3351
          existing_node.primary_ip == secondary_ip or
3352
          existing_node.secondary_ip == secondary_ip):
3353
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3354
                                   " existing node %s" % existing_node.name,
3355
                                   errors.ECODE_NOTUNIQUE)
3356

    
3357
    # check that the type of the node (single versus dual homed) is the
3358
    # same as for the master
3359
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
3360
    master_singlehomed = myself.secondary_ip == myself.primary_ip
3361
    newbie_singlehomed = secondary_ip == primary_ip
3362
    if master_singlehomed != newbie_singlehomed:
3363
      if master_singlehomed:
3364
        raise errors.OpPrereqError("The master has no private ip but the"
3365
                                   " new node has one",
3366
                                   errors.ECODE_INVAL)
3367
      else:
3368
        raise errors.OpPrereqError("The master has a private ip but the"
3369
                                   " new node doesn't have one",
3370
                                   errors.ECODE_INVAL)
3371

    
3372
    # checks reachability
3373
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3374
      raise errors.OpPrereqError("Node not reachable by ping",
3375
                                 errors.ECODE_ENVIRON)
3376

    
3377
    if not newbie_singlehomed:
3378
      # check reachability from my secondary ip to newbie's secondary ip
3379
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3380
                           source=myself.secondary_ip):
3381
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3382
                                   " based ping to noded port",
3383
                                   errors.ECODE_ENVIRON)
3384

    
3385
    if self.op.readd:
3386
      exceptions = [node]
3387
    else:
3388
      exceptions = []
3389

    
3390
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3391

    
3392
    if self.op.readd:
3393
      self.new_node = self.cfg.GetNodeInfo(node)
3394
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3395
    else:
3396
      self.new_node = objects.Node(name=node,
3397
                                   primary_ip=primary_ip,
3398
                                   secondary_ip=secondary_ip,
3399
                                   master_candidate=self.master_candidate,
3400
                                   offline=False, drained=False)
3401

    
3402
  def Exec(self, feedback_fn):
3403
    """Adds the new node to the cluster.
3404

3405
    """
3406
    new_node = self.new_node
3407
    node = new_node.name
3408

    
3409
    # for re-adds, reset the offline/drained/master-candidate flags;
3410
    # we need to reset here, otherwise offline would prevent RPC calls
3411
    # later in the procedure; this also means that if the re-add
3412
    # fails, we are left with a non-offlined, broken node
3413
    if self.op.readd:
3414
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
3415
      self.LogInfo("Readding a node, the offline/drained flags were reset")
3416
      # if we demote the node, we do cleanup later in the procedure
3417
      new_node.master_candidate = self.master_candidate
3418
      if self.changed_primary_ip:
3419
        new_node.primary_ip = self.op.primary_ip
3420

    
3421
    # notify the user about any possible mc promotion
3422
    if new_node.master_candidate:
3423
      self.LogInfo("Node will be a master candidate")
3424

    
3425
    # check connectivity
3426
    result = self.rpc.call_version([node])[node]
3427
    result.Raise("Can't get version information from node %s" % node)
3428
    if constants.PROTOCOL_VERSION == result.payload:
3429
      logging.info("Communication to node %s fine, sw version %s match",
3430
                   node, result.payload)
3431
    else:
3432
      raise errors.OpExecError("Version mismatch master version %s,"
3433
                               " node version %s" %
3434
                               (constants.PROTOCOL_VERSION, result.payload))
3435

    
3436
    # setup ssh on node
3437
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3438
      logging.info("Copy ssh key to node %s", node)
3439
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3440
      keyarray = []
3441
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3442
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3443
                  priv_key, pub_key]
3444

    
3445
      for i in keyfiles:
3446
        keyarray.append(utils.ReadFile(i))
3447

    
3448
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3449
                                      keyarray[2], keyarray[3], keyarray[4],
3450
                                      keyarray[5])
3451
      result.Raise("Cannot transfer ssh keys to the new node")
3452

    
3453
    # Add node to our /etc/hosts, and add key to known_hosts
3454
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3455
      # FIXME: this should be done via an rpc call to node daemon
3456
      utils.AddHostToEtcHosts(new_node.name)
3457

    
3458
    if new_node.secondary_ip != new_node.primary_ip:
3459
      result = self.rpc.call_node_has_ip_address(new_node.name,
3460
                                                 new_node.secondary_ip)
3461
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3462
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3463
      if not result.payload:
3464
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3465
                                 " you gave (%s). Please fix and re-run this"
3466
                                 " command." % new_node.secondary_ip)
3467

    
3468
    node_verify_list = [self.cfg.GetMasterNode()]
3469
    node_verify_param = {
3470
      constants.NV_NODELIST: [node],
3471
      # TODO: do a node-net-test as well?
3472
    }
3473

    
3474
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3475
                                       self.cfg.GetClusterName())
3476
    for verifier in node_verify_list:
3477
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3478
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3479
      if nl_payload:
3480
        for failed in nl_payload:
3481
          feedback_fn("ssh/hostname verification failed"
3482
                      " (checking from %s): %s" %
3483
                      (verifier, nl_payload[failed]))
3484
        raise errors.OpExecError("ssh/hostname verification failed.")
3485

    
3486
    if self.op.readd:
3487
      _RedistributeAncillaryFiles(self)
3488
      self.context.ReaddNode(new_node)
3489
      # make sure we redistribute the config
3490
      self.cfg.Update(new_node, feedback_fn)
3491
      # and make sure the new node will not have old files around
3492
      if not new_node.master_candidate:
3493
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3494
        msg = result.fail_msg
3495
        if msg:
3496
          self.LogWarning("Node failed to demote itself from master"
3497
                          " candidate status: %s" % msg)
3498
    else:
3499
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3500
      self.context.AddNode(new_node, self.proc.GetECId())
3501

    
3502

    
3503
class LUSetNodeParams(LogicalUnit):
3504
  """Modifies the parameters of a node.
3505

3506
  """
3507
  HPATH = "node-modify"
3508
  HTYPE = constants.HTYPE_NODE
3509
  _OP_REQP = ["node_name"]
3510
  REQ_BGL = False
3511

    
3512
  def CheckArguments(self):
3513
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3514
    _CheckBooleanOpField(self.op, 'master_candidate')
3515
    _CheckBooleanOpField(self.op, 'offline')
3516
    _CheckBooleanOpField(self.op, 'drained')
3517
    _CheckBooleanOpField(self.op, 'auto_promote')
3518
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3519
    if all_mods.count(None) == 3:
3520
      raise errors.OpPrereqError("Please pass at least one modification",
3521
                                 errors.ECODE_INVAL)
3522
    if all_mods.count(True) > 1:
3523
      raise errors.OpPrereqError("Can't set the node into more than one"
3524
                                 " state at the same time",
3525
                                 errors.ECODE_INVAL)
3526

    
3527
    # Boolean value that tells us whether we're offlining or draining the node
3528
    self.offline_or_drain = (self.op.offline == True or
3529
                             self.op.drained == True)
3530
    self.deoffline_or_drain = (self.op.offline == False or
3531
                               self.op.drained == False)
3532
    self.might_demote = (self.op.master_candidate == False or
3533
                         self.offline_or_drain)
3534

    
3535
    self.lock_all = self.op.auto_promote and self.might_demote
3536

    
3537

    
3538
  def ExpandNames(self):
3539
    if self.lock_all:
3540
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3541
    else:
3542
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3543

    
3544
  def BuildHooksEnv(self):
3545
    """Build hooks env.
3546

3547
    This runs on the master node.
3548

3549
    """
3550
    env = {
3551
      "OP_TARGET": self.op.node_name,
3552
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3553
      "OFFLINE": str(self.op.offline),
3554
      "DRAINED": str(self.op.drained),
3555
      }
3556
    nl = [self.cfg.GetMasterNode(),
3557
          self.op.node_name]
3558
    return env, nl, nl
3559

    
3560
  def CheckPrereq(self):
3561
    """Check prerequisites.
3562

3563
    This only checks the instance list against the existing names.
3564

3565
    """
3566
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3567

    
3568
    if (self.op.master_candidate is not None or
3569
        self.op.drained is not None or
3570
        self.op.offline is not None):
3571
      # we can't change the master's node flags
3572
      if self.op.node_name == self.cfg.GetMasterNode():
3573
        raise errors.OpPrereqError("The master role can be changed"
3574
                                   " only via masterfailover",
3575
                                   errors.ECODE_INVAL)
3576

    
3577

    
3578
    if node.master_candidate and self.might_demote and not self.lock_all:
3579
      assert not self.op.auto_promote, "auto-promote set but lock_all not"
3580
      # check if after removing the current node, we're missing master
3581
      # candidates
3582
      (mc_remaining, mc_should, _) = \
3583
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
3584
      if mc_remaining < mc_should:
3585
        raise errors.OpPrereqError("Not enough master candidates, please"
3586
                                   " pass auto_promote to allow promotion",
3587
                                   errors.ECODE_INVAL)
3588

    
3589
    if (self.op.master_candidate == True and
3590
        ((node.offline and not self.op.offline == False) or
3591
         (node.drained and not self.op.drained == False))):
3592
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3593
                                 " to master_candidate" % node.name,
3594
                                 errors.ECODE_INVAL)
3595

    
3596
    # If we're being deofflined/drained, we'll MC ourself if needed
3597
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3598
        self.op.master_candidate == True and not node.master_candidate):
3599
      self.op.master_candidate = _DecideSelfPromotion(self)
3600
      if self.op.master_candidate:
3601
        self.LogInfo("Autopromoting node to master candidate")
3602

    
3603
    return
3604

    
3605
  def Exec(self, feedback_fn):
3606
    """Modifies a node.
3607

3608
    """
3609
    node = self.node
3610

    
3611
    result = []
3612
    changed_mc = False
3613

    
3614
    if self.op.offline is not None:
3615
      node.offline = self.op.offline
3616
      result.append(("offline", str(self.op.offline)))
3617
      if self.op.offline == True:
3618
        if node.master_candidate:
3619
          node.master_candidate = False
3620
          changed_mc = True
3621
          result.append(("master_candidate", "auto-demotion due to offline"))
3622
        if node.drained:
3623
          node.drained = False
3624
          result.append(("drained", "clear drained status due to offline"))
3625

    
3626
    if self.op.master_candidate is not None:
3627
      node.master_candidate = self.op.master_candidate
3628
      changed_mc = True
3629
      result.append(("master_candidate", str(self.op.master_candidate)))
3630
      if self.op.master_candidate == False:
3631
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3632
        msg = rrc.fail_msg
3633
        if msg:
3634
          self.LogWarning("Node failed to demote itself: %s" % msg)
3635

    
3636
    if self.op.drained is not None:
3637
      node.drained = self.op.drained
3638
      result.append(("drained", str(self.op.drained)))
3639
      if self.op.drained == True:
3640
        if node.master_candidate:
3641
          node.master_candidate = False
3642
          changed_mc = True
3643
          result.append(("master_candidate", "auto-demotion due to drain"))
3644
          rrc = self.rpc.call_node_demote_from_mc(node.name)
3645
          msg = rrc.fail_msg
3646
          if msg:
3647
            self.LogWarning("Node failed to demote itself: %s" % msg)
3648
        if node.offline:
3649
          node.offline = False
3650
          result.append(("offline", "clear offline status due to drain"))
3651

    
3652
    # we locked all nodes, we adjust the CP before updating this node
3653
    if self.lock_all:
3654
      _AdjustCandidatePool(self, [node.name])
3655

    
3656
    # this will trigger configuration file update, if needed
3657
    self.cfg.Update(node, feedback_fn)
3658

    
3659
    # this will trigger job queue propagation or cleanup
3660
    if changed_mc:
3661
      self.context.ReaddNode(node)
3662

    
3663
    return result
3664

    
3665

    
3666
class LUPowercycleNode(NoHooksLU):
3667
  """Powercycles a node.
3668

3669
  """
3670
  _OP_REQP = ["node_name", "force"]
3671
  REQ_BGL = False
3672

    
3673
  def CheckArguments(self):
3674
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3675
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
3676
      raise errors.OpPrereqError("The node is the master and the force"
3677
                                 " parameter was not set",
3678
                                 errors.ECODE_INVAL)
3679

    
3680
  def ExpandNames(self):
3681
    """Locking for PowercycleNode.
3682

3683
    This is a last-resort option and shouldn't block on other
3684
    jobs. Therefore, we grab no locks.
3685

3686
    """
3687
    self.needed_locks = {}
3688

    
3689
  def CheckPrereq(self):
3690
    """Check prerequisites.
3691

3692
    This LU has no prereqs.
3693

3694
    """
3695
    pass
3696

    
3697
  def Exec(self, feedback_fn):
3698
    """Reboots a node.
3699

3700
    """
3701
    result = self.rpc.call_node_powercycle(self.op.node_name,
3702
                                           self.cfg.GetHypervisorType())
3703
    result.Raise("Failed to schedule the reboot")
3704
    return result.payload
3705

    
3706

    
3707
class LUQueryClusterInfo(NoHooksLU):
3708
  """Query cluster configuration.
3709

3710
  """
3711
  _OP_REQP = []
3712
  REQ_BGL = False
3713

    
3714
  def ExpandNames(self):
3715
    self.needed_locks = {}
3716

    
3717
  def CheckPrereq(self):
3718
    """No prerequsites needed for this LU.
3719

3720
    """
3721
    pass
3722

    
3723
  def Exec(self, feedback_fn):
3724
    """Return cluster config.
3725

3726
    """
3727
    cluster = self.cfg.GetClusterInfo()
3728
    os_hvp = {}
3729

    
3730
    # Filter just for enabled hypervisors
3731
    for os_name, hv_dict in cluster.os_hvp.items():
3732
      os_hvp[os_name] = {}
3733
      for hv_name, hv_params in hv_dict.items():
3734
        if hv_name in cluster.enabled_hypervisors:
3735
          os_hvp[os_name][hv_name] = hv_params
3736

    
3737
    result = {
3738
      "software_version": constants.RELEASE_VERSION,
3739
      "protocol_version": constants.PROTOCOL_VERSION,
3740
      "config_version": constants.CONFIG_VERSION,
3741
      "os_api_version": max(constants.OS_API_VERSIONS),
3742
      "export_version": constants.EXPORT_VERSION,
3743
      "architecture": (platform.architecture()[0], platform.machine()),
3744
      "name": cluster.cluster_name,
3745
      "master": cluster.master_node,
3746
      "default_hypervisor": cluster.enabled_hypervisors[0],
3747
      "enabled_hypervisors": cluster.enabled_hypervisors,
3748
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3749
                        for hypervisor_name in cluster.enabled_hypervisors]),
3750
      "os_hvp": os_hvp,
3751
      "beparams": cluster.beparams,
3752
      "nicparams": cluster.nicparams,
3753
      "candidate_pool_size": cluster.candidate_pool_size,
3754
      "master_netdev": cluster.master_netdev,
3755
      "volume_group_name": cluster.volume_group_name,
3756
      "file_storage_dir": cluster.file_storage_dir,
3757
      "maintain_node_health": cluster.maintain_node_health,
3758
      "ctime": cluster.ctime,
3759
      "mtime": cluster.mtime,
3760
      "uuid": cluster.uuid,
3761
      "tags": list(cluster.GetTags()),
3762
      "uid_pool": cluster.uid_pool,
3763
      }
3764

    
3765
    return result
3766

    
3767

    
3768
class LUQueryConfigValues(NoHooksLU):
3769
  """Return configuration values.
3770

3771
  """
3772
  _OP_REQP = []
3773
  REQ_BGL = False
3774
  _FIELDS_DYNAMIC = utils.FieldSet()
3775
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
3776
                                  "watcher_pause")
3777

    
3778
  def ExpandNames(self):
3779
    self.needed_locks = {}
3780

    
3781
    _CheckOutputFields(static=self._FIELDS_STATIC,
3782
                       dynamic=self._FIELDS_DYNAMIC,
3783
                       selected=self.op.output_fields)
3784

    
3785
  def CheckPrereq(self):
3786
    """No prerequisites.
3787

3788
    """
3789
    pass
3790

    
3791
  def Exec(self, feedback_fn):
3792
    """Dump a representation of the cluster config to the standard output.
3793

3794
    """
3795
    values = []
3796
    for field in self.op.output_fields:
3797
      if field == "cluster_name":
3798
        entry = self.cfg.GetClusterName()
3799
      elif field == "master_node":
3800
        entry = self.cfg.GetMasterNode()
3801
      elif field == "drain_flag":
3802
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3803
      elif field == "watcher_pause":
3804
        entry = utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
3805
      else:
3806
        raise errors.ParameterError(field)
3807
      values.append(entry)
3808
    return values
3809

    
3810

    
3811
class LUActivateInstanceDisks(NoHooksLU):
3812
  """Bring up an instance's disks.
3813

3814
  """
3815
  _OP_REQP = ["instance_name"]
3816
  REQ_BGL = False
3817

    
3818
  def ExpandNames(self):
3819
    self._ExpandAndLockInstance()
3820
    self.needed_locks[locking.LEVEL_NODE] = []
3821
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3822

    
3823
  def DeclareLocks(self, level):
3824
    if level == locking.LEVEL_NODE:
3825
      self._LockInstancesNodes()
3826

    
3827
  def CheckPrereq(self):
3828
    """Check prerequisites.
3829

3830
    This checks that the instance is in the cluster.
3831

3832
    """
3833
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3834
    assert self.instance is not None, \
3835
      "Cannot retrieve locked instance %s" % self.op.instance_name
3836
    _CheckNodeOnline(self, self.instance.primary_node)
3837
    if not hasattr(self.op, "ignore_size"):
3838
      self.op.ignore_size = False
3839

    
3840
  def Exec(self, feedback_fn):
3841
    """Activate the disks.
3842

3843
    """
3844
    disks_ok, disks_info = \
3845
              _AssembleInstanceDisks(self, self.instance,
3846
                                     ignore_size=self.op.ignore_size)
3847
    if not disks_ok:
3848
      raise errors.OpExecError("Cannot activate block devices")
3849

    
3850
    return disks_info
3851

    
3852

    
3853
def _AssembleInstanceDisks(lu, instance, disks=None, ignore_secondaries=False,
3854
                           ignore_size=False):
3855
  """Prepare the block devices for an instance.
3856

3857
  This sets up the block devices on all nodes.
3858

3859
  @type lu: L{LogicalUnit}
3860
  @param lu: the logical unit on whose behalf we execute
3861
  @type instance: L{objects.Instance}
3862
  @param instance: the instance for whose disks we assemble
3863
  @type disks: list of L{objects.Disk} or None
3864
  @param disks: which disks to assemble (or all, if None)
3865
  @type ignore_secondaries: boolean
3866
  @param ignore_secondaries: if true, errors on secondary nodes
3867
      won't result in an error return from the function
3868
  @type ignore_size: boolean
3869
  @param ignore_size: if true, the current known size of the disk
3870
      will not be used during the disk activation, useful for cases
3871
      when the size is wrong
3872
  @return: False if the operation failed, otherwise a list of
3873
      (host, instance_visible_name, node_visible_name)
3874
      with the mapping from node devices to instance devices
3875

3876
  """
3877
  device_info = []
3878
  disks_ok = True
3879
  iname = instance.name
3880
  disks = _ExpandCheckDisks(instance, disks)
3881

    
3882
  # With the two passes mechanism we try to reduce the window of
3883
  # opportunity for the race condition of switching DRBD to primary
3884
  # before handshaking occured, but we do not eliminate it
3885

    
3886
  # The proper fix would be to wait (with some limits) until the
3887
  # connection has been made and drbd transitions from WFConnection
3888
  # into any other network-connected state (Connected, SyncTarget,
3889
  # SyncSource, etc.)
3890

    
3891
  # 1st pass, assemble on all nodes in secondary mode
3892
  for inst_disk in disks:
3893
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3894
      if ignore_size:
3895
        node_disk = node_disk.Copy()
3896
        node_disk.UnsetSize()
3897
      lu.cfg.SetDiskID(node_disk, node)
3898
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3899
      msg = result.fail_msg
3900
      if msg:
3901
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3902
                           " (is_primary=False, pass=1): %s",
3903
                           inst_disk.iv_name, node, msg)
3904
        if not ignore_secondaries:
3905
          disks_ok = False
3906

    
3907
  # FIXME: race condition on drbd migration to primary
3908

    
3909
  # 2nd pass, do only the primary node
3910
  for inst_disk in disks:
3911
    dev_path = None
3912

    
3913
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3914
      if node != instance.primary_node:
3915
        continue
3916
      if ignore_size:
3917
        node_disk = node_disk.Copy()
3918
        node_disk.UnsetSize()
3919
      lu.cfg.SetDiskID(node_disk, node)
3920
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3921
      msg = result.fail_msg
3922
      if msg:
3923
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3924
                           " (is_primary=True, pass=2): %s",
3925
                           inst_disk.iv_name, node, msg)
3926
        disks_ok = False
3927
      else:
3928
        dev_path = result.payload
3929

    
3930
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
3931

    
3932
  # leave the disks configured for the primary node
3933
  # this is a workaround that would be fixed better by
3934
  # improving the logical/physical id handling
3935
  for disk in disks:
3936
    lu.cfg.SetDiskID(disk, instance.primary_node)
3937

    
3938
  return disks_ok, device_info
3939

    
3940

    
3941
def _StartInstanceDisks(lu, instance, force):
3942
  """Start the disks of an instance.
3943

3944
  """
3945
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3946
                                           ignore_secondaries=force)
3947
  if not disks_ok:
3948
    _ShutdownInstanceDisks(lu, instance)
3949
    if force is not None and not force:
3950
      lu.proc.LogWarning("", hint="If the message above refers to a"
3951
                         " secondary node,"
3952
                         " you can retry the operation using '--force'.")
3953
    raise errors.OpExecError("Disk consistency error")
3954

    
3955

    
3956
class LUDeactivateInstanceDisks(NoHooksLU):
3957
  """Shutdown an instance's disks.
3958

3959
  """
3960
  _OP_REQP = ["instance_name"]
3961
  REQ_BGL = False
3962

    
3963
  def ExpandNames(self):
3964
    self._ExpandAndLockInstance()
3965
    self.needed_locks[locking.LEVEL_NODE] = []
3966
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3967

    
3968
  def DeclareLocks(self, level):
3969
    if level == locking.LEVEL_NODE:
3970
      self._LockInstancesNodes()
3971

    
3972
  def CheckPrereq(self):
3973
    """Check prerequisites.
3974

3975
    This checks that the instance is in the cluster.
3976

3977
    """
3978
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3979
    assert self.instance is not None, \
3980
      "Cannot retrieve locked instance %s" % self.op.instance_name
3981

    
3982
  def Exec(self, feedback_fn):
3983
    """Deactivate the disks
3984

3985
    """
3986
    instance = self.instance
3987
    _SafeShutdownInstanceDisks(self, instance)
3988

    
3989

    
3990
def _SafeShutdownInstanceDisks(lu, instance, disks=None):
3991
  """Shutdown block devices of an instance.
3992

3993
  This function checks if an instance is running, before calling
3994
  _ShutdownInstanceDisks.
3995

3996
  """
3997
  _CheckInstanceDown(lu, instance, "cannot shutdown disks")
3998
  _ShutdownInstanceDisks(lu, instance, disks=disks)
3999

    
4000

    
4001
def _ExpandCheckDisks(instance, disks):
4002
  """Return the instance disks selected by the disks list
4003

4004
  @type disks: list of L{objects.Disk} or None
4005
  @param disks: selected disks
4006
  @rtype: list of L{objects.Disk}
4007
  @return: selected instance disks to act on
4008

4009
  """
4010
  if disks is None:
4011
    return instance.disks
4012
  else:
4013
    if not set(disks).issubset(instance.disks):
4014
      raise errors.ProgrammerError("Can only act on disks belonging to the"
4015
                                   " target instance")
4016
    return disks
4017

    
4018

    
4019
def _ShutdownInstanceDisks(lu, instance, disks=