Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 895eb320

History | View | Annotate | Download (338.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

    
603
def _CheckInstanceDown(lu, instance, reason):
604
  """Ensure that an instance is not running."""
605
  if instance.admin_up:
606
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
607
                               (instance.name, reason), errors.ECODE_STATE)
608

    
609
  pnode = instance.primary_node
610
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
611
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
612
              prereq=True, ecode=errors.ECODE_ENVIRON)
613

    
614
  if instance.name in ins_l.payload:
615
    raise errors.OpPrereqError("Instance %s is running, %s" %
616
                               (instance.name, reason), errors.ECODE_STATE)
617

    
618

    
619
def _ExpandItemName(fn, name, kind):
620
  """Expand an item name.
621

622
  @param fn: the function to use for expansion
623
  @param name: requested item name
624
  @param kind: text description ('Node' or 'Instance')
625
  @return: the resolved (full) name
626
  @raise errors.OpPrereqError: if the item is not found
627

628
  """
629
  full_name = fn(name)
630
  if full_name is None:
631
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
632
                               errors.ECODE_NOENT)
633
  return full_name
634

    
635

    
636
def _ExpandNodeName(cfg, name):
637
  """Wrapper over L{_ExpandItemName} for nodes."""
638
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
639

    
640

    
641
def _ExpandInstanceName(cfg, name):
642
  """Wrapper over L{_ExpandItemName} for instance."""
643
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
644

    
645

    
646
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
647
                          memory, vcpus, nics, disk_template, disks,
648
                          bep, hvp, hypervisor_name):
649
  """Builds instance related env variables for hooks
650

651
  This builds the hook environment from individual variables.
652

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

683
  """
684
  if status:
685
    str_status = "up"
686
  else:
687
    str_status = "down"
688
  env = {
689
    "OP_TARGET": name,
690
    "INSTANCE_NAME": name,
691
    "INSTANCE_PRIMARY": primary_node,
692
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
693
    "INSTANCE_OS_TYPE": os_type,
694
    "INSTANCE_STATUS": str_status,
695
    "INSTANCE_MEMORY": memory,
696
    "INSTANCE_VCPUS": vcpus,
697
    "INSTANCE_DISK_TEMPLATE": disk_template,
698
    "INSTANCE_HYPERVISOR": hypervisor_name,
699
  }
700

    
701
  if nics:
702
    nic_count = len(nics)
703
    for idx, (ip, mac, mode, link) in enumerate(nics):
704
      if ip is None:
705
        ip = ""
706
      env["INSTANCE_NIC%d_IP" % idx] = ip
707
      env["INSTANCE_NIC%d_MAC" % idx] = mac
708
      env["INSTANCE_NIC%d_MODE" % idx] = mode
709
      env["INSTANCE_NIC%d_LINK" % idx] = link
710
      if mode == constants.NIC_MODE_BRIDGED:
711
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
712
  else:
713
    nic_count = 0
714

    
715
  env["INSTANCE_NIC_COUNT"] = nic_count
716

    
717
  if disks:
718
    disk_count = len(disks)
719
    for idx, (size, mode) in enumerate(disks):
720
      env["INSTANCE_DISK%d_SIZE" % idx] = size
721
      env["INSTANCE_DISK%d_MODE" % idx] = mode
722
  else:
723
    disk_count = 0
724

    
725
  env["INSTANCE_DISK_COUNT"] = disk_count
726

    
727
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
728
    for key, value in source.items():
729
      env["INSTANCE_%s_%s" % (kind, key)] = value
730

    
731
  return env
732

    
733

    
734
def _NICListToTuple(lu, nics):
735
  """Build a list of nic information tuples.
736

737
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
738
  value in LUQueryInstanceData.
739

740
  @type lu:  L{LogicalUnit}
741
  @param lu: the logical unit on whose behalf we execute
742
  @type nics: list of L{objects.NIC}
743
  @param nics: list of nics to convert to hooks tuples
744

745
  """
746
  hooks_nics = []
747
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
748
  for nic in nics:
749
    ip = nic.ip
750
    mac = nic.mac
751
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
752
    mode = filled_params[constants.NIC_MODE]
753
    link = filled_params[constants.NIC_LINK]
754
    hooks_nics.append((ip, mac, mode, link))
755
  return hooks_nics
756

    
757

    
758
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
759
  """Builds instance related env variables for hooks from an object.
760

761
  @type lu: L{LogicalUnit}
762
  @param lu: the logical unit on whose behalf we execute
763
  @type instance: L{objects.Instance}
764
  @param instance: the instance for which we should build the
765
      environment
766
  @type override: dict
767
  @param override: dictionary with key/values that will override
768
      our values
769
  @rtype: dict
770
  @return: the hook environment dictionary
771

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

    
795

    
796
def _AdjustCandidatePool(lu, exceptions):
797
  """Adjust the candidate pool after node operations.
798

799
  """
800
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
801
  if mod_list:
802
    lu.LogInfo("Promoted nodes to master candidate role: %s",
803
               utils.CommaJoin(node.name for node in mod_list))
804
    for name in mod_list:
805
      lu.context.ReaddNode(name)
806
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
807
  if mc_now > mc_max:
808
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
809
               (mc_now, mc_max))
810

    
811

    
812
def _DecideSelfPromotion(lu, exceptions=None):
813
  """Decide whether I should promote myself as a master candidate.
814

815
  """
816
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
817
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
818
  # the new node will increase mc_max with one, so:
819
  mc_should = min(mc_should + 1, cp_size)
820
  return mc_now < mc_should
821

    
822

    
823
def _CheckNicsBridgesExist(lu, target_nics, target_node,
824
                               profile=constants.PP_DEFAULT):
825
  """Check that the brigdes needed by a list of nics exist.
826

827
  """
828
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
829
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
830
                for nic in target_nics]
831
  brlist = [params[constants.NIC_LINK] for params in paramslist
832
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
833
  if brlist:
834
    result = lu.rpc.call_bridges_exist(target_node, brlist)
835
    result.Raise("Error checking bridges on destination node '%s'" %
836
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
837

    
838

    
839
def _CheckInstanceBridgesExist(lu, instance, node=None):
840
  """Check that the brigdes needed by an instance exist.
841

842
  """
843
  if node is None:
844
    node = instance.primary_node
845
  _CheckNicsBridgesExist(lu, instance.nics, node)
846

    
847

    
848
def _CheckOSVariant(os_obj, name):
849
  """Check whether an OS name conforms to the os variants specification.
850

851
  @type os_obj: L{objects.OS}
852
  @param os_obj: OS object to check
853
  @type name: string
854
  @param name: OS name passed by the user, to check for validity
855

856
  """
857
  if not os_obj.supported_variants:
858
    return
859
  try:
860
    variant = name.split("+", 1)[1]
861
  except IndexError:
862
    raise errors.OpPrereqError("OS name must include a variant",
863
                               errors.ECODE_INVAL)
864

    
865
  if variant not in os_obj.supported_variants:
866
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
867

    
868

    
869
def _GetNodeInstancesInner(cfg, fn):
870
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
871

    
872

    
873
def _GetNodeInstances(cfg, node_name):
874
  """Returns a list of all primary and secondary instances on a node.
875

876
  """
877

    
878
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
879

    
880

    
881
def _GetNodePrimaryInstances(cfg, node_name):
882
  """Returns primary instances on a node.
883

884
  """
885
  return _GetNodeInstancesInner(cfg,
886
                                lambda inst: node_name == inst.primary_node)
887

    
888

    
889
def _GetNodeSecondaryInstances(cfg, node_name):
890
  """Returns secondary instances on a node.
891

892
  """
893
  return _GetNodeInstancesInner(cfg,
894
                                lambda inst: node_name in inst.secondary_nodes)
895

    
896

    
897
def _GetStorageTypeArgs(cfg, storage_type):
898
  """Returns the arguments for a storage type.
899

900
  """
901
  # Special case for file storage
902
  if storage_type == constants.ST_FILE:
903
    # storage.FileStorage wants a list of storage directories
904
    return [[cfg.GetFileStorageDir()]]
905

    
906
  return []
907

    
908

    
909
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
910
  faulty = []
911

    
912
  for dev in instance.disks:
913
    cfg.SetDiskID(dev, node_name)
914

    
915
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
916
  result.Raise("Failed to get disk status from node %s" % node_name,
917
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
918

    
919
  for idx, bdev_status in enumerate(result.payload):
920
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
921
      faulty.append(idx)
922

    
923
  return faulty
924

    
925

    
926
class LUPostInitCluster(LogicalUnit):
927
  """Logical unit for running hooks after cluster initialization.
928

929
  """
930
  HPATH = "cluster-init"
931
  HTYPE = constants.HTYPE_CLUSTER
932
  _OP_REQP = []
933

    
934
  def BuildHooksEnv(self):
935
    """Build hooks env.
936

937
    """
938
    env = {"OP_TARGET": self.cfg.GetClusterName()}
939
    mn = self.cfg.GetMasterNode()
940
    return env, [], [mn]
941

    
942
  def CheckPrereq(self):
943
    """No prerequisites to check.
944

945
    """
946
    return True
947

    
948
  def Exec(self, feedback_fn):
949
    """Nothing to do.
950

951
    """
952
    return True
953

    
954

    
955
class LUDestroyCluster(LogicalUnit):
956
  """Logical unit for destroying the cluster.
957

958
  """
959
  HPATH = "cluster-destroy"
960
  HTYPE = constants.HTYPE_CLUSTER
961
  _OP_REQP = []
962

    
963
  def BuildHooksEnv(self):
964
    """Build hooks env.
965

966
    """
967
    env = {"OP_TARGET": self.cfg.GetClusterName()}
968
    return env, [], []
969

    
970
  def CheckPrereq(self):
971
    """Check prerequisites.
972

973
    This checks whether the cluster is empty.
974

975
    Any errors are signaled by raising errors.OpPrereqError.
976

977
    """
978
    master = self.cfg.GetMasterNode()
979

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

    
991
  def Exec(self, feedback_fn):
992
    """Destroys the cluster.
993

994
    """
995
    master = self.cfg.GetMasterNode()
996
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
997

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

    
1006
    result = self.rpc.call_node_stop_master(master, False)
1007
    result.Raise("Could not disable the master role")
1008

    
1009
    if modify_ssh_setup:
1010
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
1011
      utils.CreateBackup(priv_key)
1012
      utils.CreateBackup(pub_key)
1013

    
1014
    return master
1015

    
1016

    
1017
def _VerifyCertificate(filename):
1018
  """Verifies a certificate for LUVerifyCluster.
1019

1020
  @type filename: string
1021
  @param filename: Path to PEM file
1022

1023
  """
1024
  try:
1025
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1026
                                           utils.ReadFile(filename))
1027
  except Exception, err: # pylint: disable-msg=W0703
1028
    return (LUVerifyCluster.ETYPE_ERROR,
1029
            "Failed to load X509 certificate %s: %s" % (filename, err))
1030

    
1031
  (errcode, msg) = \
1032
    utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1033
                                constants.SSL_CERT_EXPIRATION_ERROR)
1034

    
1035
  if msg:
1036
    fnamemsg = "While verifying %s: %s" % (filename, msg)
1037
  else:
1038
    fnamemsg = None
1039

    
1040
  if errcode is None:
1041
    return (None, fnamemsg)
1042
  elif errcode == utils.CERT_WARNING:
1043
    return (LUVerifyCluster.ETYPE_WARNING, fnamemsg)
1044
  elif errcode == utils.CERT_ERROR:
1045
    return (LUVerifyCluster.ETYPE_ERROR, fnamemsg)
1046

    
1047
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1048

    
1049

    
1050
class LUVerifyCluster(LogicalUnit):
1051
  """Verifies the cluster status.
1052

1053
  """
1054
  HPATH = "cluster-verify"
1055
  HTYPE = constants.HTYPE_CLUSTER
1056
  _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"]
1057
  REQ_BGL = False
1058

    
1059
  TCLUSTER = "cluster"
1060
  TNODE = "node"
1061
  TINSTANCE = "instance"
1062

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

    
1086
  ETYPE_FIELD = "code"
1087
  ETYPE_ERROR = "ERROR"
1088
  ETYPE_WARNING = "WARNING"
1089

    
1090
  class NodeImage(object):
1091
    """A class representing the logical and physical status of a node.
1092

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

1113
    """
1114
    def __init__(self, offline=False):
1115
      self.volumes = {}
1116
      self.instances = []
1117
      self.pinst = []
1118
      self.sinst = []
1119
      self.sbp = {}
1120
      self.mfree = 0
1121
      self.dfree = 0
1122
      self.offline = offline
1123
      self.rpc_fail = False
1124
      self.lvm_fail = False
1125
      self.hyp_fail = False
1126
      self.ghost = False
1127

    
1128
  def ExpandNames(self):
1129
    self.needed_locks = {
1130
      locking.LEVEL_NODE: locking.ALL_SET,
1131
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1132
    }
1133
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1134

    
1135
  def _Error(self, ecode, item, msg, *args, **kwargs):
1136
    """Format an error message.
1137

1138
    Based on the opcode's error_codes parameter, either format a
1139
    parseable error code, or a simpler error string.
1140

1141
    This must be called only from Exec and functions called from Exec.
1142

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

    
1161
  def _ErrorIf(self, cond, *args, **kwargs):
1162
    """Log an error message if the passed condition is True.
1163

1164
    """
1165
    cond = bool(cond) or self.op.debug_simulate_errors
1166
    if cond:
1167
      self._Error(*args, **kwargs)
1168
    # do not mark the operation as failed for WARN cases only
1169
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1170
      self.bad = self.bad or cond
1171

    
1172
  def _VerifyNode(self, ninfo, nresult):
1173
    """Run multiple tests against a node.
1174

1175
    Test list:
1176

1177
      - compares ganeti version
1178
      - checks vg existence and size > 20G
1179
      - checks config file checksum
1180
      - checks ssh to other nodes
1181

1182
    @type ninfo: L{objects.Node}
1183
    @param ninfo: the node to check
1184
    @param nresult: the results from the node
1185
    @rtype: boolean
1186
    @return: whether overall this call was successful (and we can expect
1187
         reasonable values in the respose)
1188

1189
    """
1190
    node = ninfo.name
1191
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1192

    
1193
    # main result, nresult should be a non-empty dict
1194
    test = not nresult or not isinstance(nresult, dict)
1195
    _ErrorIf(test, self.ENODERPC, node,
1196
                  "unable to verify node: no data returned")
1197
    if test:
1198
      return False
1199

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

    
1211
    test = local_version != remote_version[0]
1212
    _ErrorIf(test, self.ENODEVERSION, node,
1213
             "incompatible protocol versions: master %s,"
1214
             " node %s", local_version, remote_version[0])
1215
    if test:
1216
      return False
1217

    
1218
    # node seems compatible, we can actually try to look into its results
1219

    
1220
    # full package version
1221
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1222
                  self.ENODEVERSION, node,
1223
                  "software version mismatch: master %s, node %s",
1224
                  constants.RELEASE_VERSION, remote_version[1],
1225
                  code=self.ETYPE_WARNING)
1226

    
1227
    hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1228
    if isinstance(hyp_result, dict):
1229
      for hv_name, hv_result in hyp_result.iteritems():
1230
        test = hv_result is not None
1231
        _ErrorIf(test, self.ENODEHV, node,
1232
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1233

    
1234

    
1235
    test = nresult.get(constants.NV_NODESETUP,
1236
                           ["Missing NODESETUP results"])
1237
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1238
             "; ".join(test))
1239

    
1240
    return True
1241

    
1242
  def _VerifyNodeTime(self, ninfo, nresult,
1243
                      nvinfo_starttime, nvinfo_endtime):
1244
    """Check the node time.
1245

1246
    @type ninfo: L{objects.Node}
1247
    @param ninfo: the node to check
1248
    @param nresult: the remote results for the node
1249
    @param nvinfo_starttime: the start time of the RPC call
1250
    @param nvinfo_endtime: the end time of the RPC call
1251

1252
    """
1253
    node = ninfo.name
1254
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1255

    
1256
    ntime = nresult.get(constants.NV_TIME, None)
1257
    try:
1258
      ntime_merged = utils.MergeTime(ntime)
1259
    except (ValueError, TypeError):
1260
      _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1261
      return
1262

    
1263
    if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1264
      ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1265
    elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1266
      ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1267
    else:
1268
      ntime_diff = None
1269

    
1270
    _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1271
             "Node time diverges by at least %s from master node time",
1272
             ntime_diff)
1273

    
1274
  def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1275
    """Check the node time.
1276

1277
    @type ninfo: L{objects.Node}
1278
    @param ninfo: the node to check
1279
    @param nresult: the remote results for the node
1280
    @param vg_name: the configured VG name
1281

1282
    """
1283
    if vg_name is None:
1284
      return
1285

    
1286
    node = ninfo.name
1287
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1288

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

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

    
1311
  def _VerifyNodeNetwork(self, ninfo, nresult):
1312
    """Check the node time.
1313

1314
    @type ninfo: L{objects.Node}
1315
    @param ninfo: the node to check
1316
    @param nresult: the remote results for the node
1317

1318
    """
1319
    node = ninfo.name
1320
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1321

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

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

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

    
1353

    
1354
  def _VerifyInstance(self, instance, instanceconfig, node_image):
1355
    """Verify an instance.
1356

1357
    This function checks to see if the required block devices are
1358
    available on the instance's node.
1359

1360
    """
1361
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1362
    node_current = instanceconfig.primary_node
1363

    
1364
    node_vol_should = {}
1365
    instanceconfig.MapLVsByNode(node_vol_should)
1366

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

    
1377
    if instanceconfig.admin_up:
1378
      pri_img = node_image[node_current]
1379
      test = instance not in pri_img.instances and not pri_img.offline
1380
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1381
               "instance not running on its primary node %s",
1382
               node_current)
1383

    
1384
    for node, n_img in node_image.items():
1385
      if (not node == node_current):
1386
        test = instance in n_img.instances
1387
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1388
                 "instance should not run on node %s", node)
1389

    
1390
  def _VerifyOrphanVolumes(self, node_vol_should, node_image):
1391
    """Verify if there are any unknown volumes in the cluster.
1392

1393
    The .os, .swap and backup volumes are ignored. All other volumes are
1394
    reported as unknown.
1395

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

    
1407
  def _VerifyOrphanInstances(self, instancelist, node_image):
1408
    """Verify the list of running instances.
1409

1410
    This checks what instances are running but unknown to the cluster.
1411

1412
    """
1413
    for node, n_img in node_image.items():
1414
      for o_inst in n_img.instances:
1415
        test = o_inst not in instancelist
1416
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1417
                      "instance %s on node %s should not exist", o_inst, node)
1418

    
1419
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1420
    """Verify N+1 Memory Resilience.
1421

1422
    Check that if one single node dies we can still start all the
1423
    instances it was primary for.
1424

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

    
1446
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1447
                       master_files):
1448
    """Verifies and computes the node required file checksums.
1449

1450
    @type ninfo: L{objects.Node}
1451
    @param ninfo: the node to check
1452
    @param nresult: the remote results for the node
1453
    @param file_list: required list of files
1454
    @param local_cksum: dictionary of local files and their checksums
1455
    @param master_files: list of files that only masters should have
1456

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

    
1461
    remote_cksum = nresult.get(constants.NV_FILELIST, None)
1462
    test = not isinstance(remote_cksum, dict)
1463
    _ErrorIf(test, self.ENODEFILECHECK, node,
1464
             "node hasn't returned file checksum data")
1465
    if test:
1466
      return
1467

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

    
1490
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_map):
1491
    """Verifies and the node DRBD status.
1492

1493
    @type ninfo: L{objects.Node}
1494
    @param ninfo: the node to check
1495
    @param nresult: the remote results for the node
1496
    @param instanceinfo: the dict of instances
1497
    @param drbd_map: the DRBD map as returned by
1498
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1499

1500
    """
1501
    node = ninfo.name
1502
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1503

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

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

    
1528
    for minor, (iname, must_exist) in node_drbd.items():
1529
      test = minor not in used_minors and must_exist
1530
      _ErrorIf(test, self.ENODEDRBD, node,
1531
               "drbd minor %d of instance %s is not active", minor, iname)
1532
    for minor in used_minors:
1533
      test = minor not in node_drbd
1534
      _ErrorIf(test, self.ENODEDRBD, node,
1535
               "unallocated drbd minor %d is in use", minor)
1536

    
1537
  def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
1538
    """Verifies and updates the node volume data.
1539

1540
    This function will update a L{NodeImage}'s internal structures
1541
    with data from the remote call.
1542

1543
    @type ninfo: L{objects.Node}
1544
    @param ninfo: the node to check
1545
    @param nresult: the remote results for the node
1546
    @param nimg: the node image object
1547
    @param vg_name: the configured VG name
1548

1549
    """
1550
    node = ninfo.name
1551
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1552

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

    
1566
  def _UpdateNodeInstances(self, ninfo, nresult, nimg):
1567
    """Verifies and updates the node instance list.
1568

1569
    If the listing was successful, then updates this node's instance
1570
    list. Otherwise, it marks the RPC call as failed for the instance
1571
    list key.
1572

1573
    @type ninfo: L{objects.Node}
1574
    @param ninfo: the node to check
1575
    @param nresult: the remote results for the node
1576
    @param nimg: the node image object
1577

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

    
1588
  def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
1589
    """Verifies and computes a node information map
1590

1591
    @type ninfo: L{objects.Node}
1592
    @param ninfo: the node to check
1593
    @param nresult: the remote results for the node
1594
    @param nimg: the node image object
1595
    @param vg_name: the configured VG name
1596

1597
    """
1598
    node = ninfo.name
1599
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1600

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

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

    
1626
  def CheckPrereq(self):
1627
    """Check prerequisites.
1628

1629
    Transform the list of checks we're going to skip into a set and check that
1630
    all its members are valid.
1631

1632
    """
1633
    self.skip_set = frozenset(self.op.skip_checks)
1634
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1635
      raise errors.OpPrereqError("Invalid checks to be skipped specified",
1636
                                 errors.ECODE_INVAL)
1637

    
1638
  def BuildHooksEnv(self):
1639
    """Build hooks env.
1640

1641
    Cluster-Verify hooks just ran in the post phase and their failure makes
1642
    the output be logged in the verify output and the verification to fail.
1643

1644
    """
1645
    all_nodes = self.cfg.GetNodeList()
1646
    env = {
1647
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1648
      }
1649
    for node in self.cfg.GetAllNodesInfo().values():
1650
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1651

    
1652
    return env, [], all_nodes
1653

    
1654
  def Exec(self, feedback_fn):
1655
    """Verify integrity of cluster, performing various test on nodes.
1656

1657
    """
1658
    self.bad = False
1659
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1660
    verbose = self.op.verbose
1661
    self._feedback_fn = feedback_fn
1662
    feedback_fn("* Verifying global settings")
1663
    for msg in self.cfg.VerifyConfig():
1664
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1665

    
1666
    # Check the cluster certificates
1667
    for cert_filename in constants.ALL_CERT_FILES:
1668
      (errcode, msg) = _VerifyCertificate(cert_filename)
1669
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1670

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

    
1685
    # FIXME: verify OS list
1686
    # do local checksums
1687
    master_files = [constants.CLUSTER_CONF_FILE]
1688
    master_node = self.master_node = self.cfg.GetMasterNode()
1689
    master_ip = self.cfg.GetMasterIP()
1690

    
1691
    file_names = ssconf.SimpleStore().GetFileList()
1692
    file_names.extend(constants.ALL_CERT_FILES)
1693
    file_names.extend(master_files)
1694
    if cluster.modify_etc_hosts:
1695
      file_names.append(constants.ETC_HOSTS)
1696

    
1697
    local_checksums = utils.FingerprintFiles(file_names)
1698

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

    
1716
    if vg_name is not None:
1717
      node_verify_param[constants.NV_VGLIST] = None
1718
      node_verify_param[constants.NV_LVLIST] = vg_name
1719
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1720
      node_verify_param[constants.NV_DRBDLIST] = None
1721

    
1722
    # Build our expected cluster state
1723
    node_image = dict((node.name, self.NodeImage(offline=node.offline))
1724
                      for node in nodeinfo)
1725

    
1726
    for instance in instancelist:
1727
      inst_config = instanceinfo[instance]
1728

    
1729
      for nname in inst_config.all_nodes:
1730
        if nname not in node_image:
1731
          # ghost node
1732
          gnode = self.NodeImage()
1733
          gnode.ghost = True
1734
          node_image[nname] = gnode
1735

    
1736
      inst_config.MapLVsByNode(node_vol_should)
1737

    
1738
      pnode = inst_config.primary_node
1739
      node_image[pnode].pinst.append(instance)
1740

    
1741
      for snode in inst_config.secondary_nodes:
1742
        nimg = node_image[snode]
1743
        nimg.sinst.append(instance)
1744
        if pnode not in nimg.sbp:
1745
          nimg.sbp[pnode] = []
1746
        nimg.sbp[pnode].append(instance)
1747

    
1748
    # At this point, we have the in-memory data structures complete,
1749
    # except for the runtime information, which we'll gather next
1750

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

    
1760
    all_drbd_map = self.cfg.ComputeDRBDMap()
1761

    
1762
    feedback_fn("* Verifying node status")
1763
    for node_i in nodeinfo:
1764
      node = node_i.name
1765
      nimg = node_image[node]
1766

    
1767
      if node_i.offline:
1768
        if verbose:
1769
          feedback_fn("* Skipping offline node %s" % (node,))
1770
        n_offline += 1
1771
        continue
1772

    
1773
      if node == master_node:
1774
        ntype = "master"
1775
      elif node_i.master_candidate:
1776
        ntype = "master candidate"
1777
      elif node_i.drained:
1778
        ntype = "drained"
1779
        n_drained += 1
1780
      else:
1781
        ntype = "regular"
1782
      if verbose:
1783
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1784

    
1785
      msg = all_nvinfo[node].fail_msg
1786
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
1787
      if msg:
1788
        nimg.rpc_fail = True
1789
        continue
1790

    
1791
      nresult = all_nvinfo[node].payload
1792

    
1793
      nimg.call_ok = self._VerifyNode(node_i, nresult)
1794
      self._VerifyNodeNetwork(node_i, nresult)
1795
      self._VerifyNodeLVM(node_i, nresult, vg_name)
1796
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
1797
                            master_files)
1798
      self._VerifyNodeDrbd(node_i, nresult, instanceinfo, all_drbd_map)
1799
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
1800

    
1801
      self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
1802
      self._UpdateNodeInstances(node_i, nresult, nimg)
1803
      self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
1804

    
1805
    feedback_fn("* Verifying instance status")
1806
    for instance in instancelist:
1807
      if verbose:
1808
        feedback_fn("* Verifying instance %s" % instance)
1809
      inst_config = instanceinfo[instance]
1810
      self._VerifyInstance(instance, inst_config, node_image)
1811
      inst_nodes_offline = []
1812

    
1813
      pnode = inst_config.primary_node
1814
      pnode_img = node_image[pnode]
1815
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
1816
               self.ENODERPC, pnode, "instance %s, connection to"
1817
               " primary node failed", instance)
1818

    
1819
      if pnode_img.offline:
1820
        inst_nodes_offline.append(pnode)
1821

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

    
1834
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1835
        i_non_a_balanced.append(instance)
1836

    
1837
      for snode in inst_config.secondary_nodes:
1838
        s_img = node_image[snode]
1839
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
1840
                 "instance %s, connection to secondary node failed", instance)
1841

    
1842
        if s_img.offline:
1843
          inst_nodes_offline.append(snode)
1844

    
1845
      # warn that the instance lives on offline nodes
1846
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
1847
               "instance lives on offline node(s) %s",
1848
               utils.CommaJoin(inst_nodes_offline))
1849
      # ... or ghost nodes
1850
      for node in inst_config.all_nodes:
1851
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
1852
                 "instance lives on ghost node %s", node)
1853

    
1854
    feedback_fn("* Verifying orphan volumes")
1855
    self._VerifyOrphanVolumes(node_vol_should, node_image)
1856

    
1857
    feedback_fn("* Verifying orphan instances")
1858
    self._VerifyOrphanInstances(instancelist, node_image)
1859

    
1860
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1861
      feedback_fn("* Verifying N+1 Memory redundancy")
1862
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
1863

    
1864
    feedback_fn("* Other Notes")
1865
    if i_non_redundant:
1866
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1867
                  % len(i_non_redundant))
1868

    
1869
    if i_non_a_balanced:
1870
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1871
                  % len(i_non_a_balanced))
1872

    
1873
    if n_offline:
1874
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
1875

    
1876
    if n_drained:
1877
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
1878

    
1879
    return not self.bad
1880

    
1881
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1882
    """Analyze the post-hooks' result
1883

1884
    This method analyses the hook result, handles it, and sends some
1885
    nicely-formatted feedback back to the user.
1886

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

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

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

    
1925
      return lu_result
1926

    
1927

    
1928
class LUVerifyDisks(NoHooksLU):
1929
  """Verifies the cluster disks status.
1930

1931
  """
1932
  _OP_REQP = []
1933
  REQ_BGL = False
1934

    
1935
  def ExpandNames(self):
1936
    self.needed_locks = {
1937
      locking.LEVEL_NODE: locking.ALL_SET,
1938
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1939
    }
1940
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1941

    
1942
  def CheckPrereq(self):
1943
    """Check prerequisites.
1944

1945
    This has no prerequisites.
1946

1947
    """
1948
    pass
1949

    
1950
  def Exec(self, feedback_fn):
1951
    """Verify integrity of cluster disks.
1952

1953
    @rtype: tuple of three items
1954
    @return: a tuple of (dict of node-to-node_error, list of instances
1955
        which need activate-disks, dict of instance: (node, volume) for
1956
        missing volumes
1957

1958
    """
1959
    result = res_nodes, res_instances, res_missing = {}, [], {}
1960

    
1961
    vg_name = self.cfg.GetVGName()
1962
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1963
    instances = [self.cfg.GetInstanceInfo(name)
1964
                 for name in self.cfg.GetInstanceList()]
1965

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

    
1978
    if not nv_dict:
1979
      return result
1980

    
1981
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1982

    
1983
    for node in nodes:
1984
      # node_volume
1985
      node_res = node_lvs[node]
1986
      if node_res.offline:
1987
        continue
1988
      msg = node_res.fail_msg
1989
      if msg:
1990
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1991
        res_nodes[node] = msg
1992
        continue
1993

    
1994
      lvs = node_res.payload
1995
      for lv_name, (_, _, lv_online) in lvs.items():
1996
        inst = nv_dict.pop((node, lv_name), None)
1997
        if (not lv_online and inst is not None
1998
            and inst.name not in res_instances):
1999
          res_instances.append(inst.name)
2000

    
2001
    # any leftover items in nv_dict are missing LVs, let's arrange the
2002
    # data better
2003
    for key, inst in nv_dict.iteritems():
2004
      if inst.name not in res_missing:
2005
        res_missing[inst.name] = []
2006
      res_missing[inst.name].append(key)
2007

    
2008
    return result
2009

    
2010

    
2011
class LURepairDiskSizes(NoHooksLU):
2012
  """Verifies the cluster disks sizes.
2013

2014
  """
2015
  _OP_REQP = ["instances"]
2016
  REQ_BGL = False
2017

    
2018
  def ExpandNames(self):
2019
    if not isinstance(self.op.instances, list):
2020
      raise errors.OpPrereqError("Invalid argument type 'instances'",
2021
                                 errors.ECODE_INVAL)
2022

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

    
2041
  def DeclareLocks(self, level):
2042
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2043
      self._LockInstancesNodes(primary_only=True)
2044

    
2045
  def CheckPrereq(self):
2046
    """Check prerequisites.
2047

2048
    This only checks the optional instance list against the existing names.
2049

2050
    """
2051
    if self.wanted_names is None:
2052
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2053

    
2054
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2055
                             in self.wanted_names]
2056

    
2057
  def _EnsureChildSizes(self, disk):
2058
    """Ensure children of the disk have the needed disk size.
2059

2060
    This is valid mainly for DRBD8 and fixes an issue where the
2061
    children have smaller disk size.
2062

2063
    @param disk: an L{ganeti.objects.Disk} object
2064

2065
    """
2066
    if disk.dev_type == constants.LD_DRBD8:
2067
      assert disk.children, "Empty children for DRBD8?"
2068
      fchild = disk.children[0]
2069
      mismatch = fchild.size < disk.size
2070
      if mismatch:
2071
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2072
                     fchild.size, disk.size)
2073
        fchild.size = disk.size
2074

    
2075
      # and we recurse on this child only, not on the metadev
2076
      return self._EnsureChildSizes(fchild) or mismatch
2077
    else:
2078
      return False
2079

    
2080
  def Exec(self, feedback_fn):
2081
    """Verify the size of cluster disks.
2082

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

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

    
2130

    
2131
class LURenameCluster(LogicalUnit):
2132
  """Rename the cluster.
2133

2134
  """
2135
  HPATH = "cluster-rename"
2136
  HTYPE = constants.HTYPE_CLUSTER
2137
  _OP_REQP = ["name"]
2138

    
2139
  def BuildHooksEnv(self):
2140
    """Build hooks env.
2141

2142
    """
2143
    env = {
2144
      "OP_TARGET": self.cfg.GetClusterName(),
2145
      "NEW_NAME": self.op.name,
2146
      }
2147
    mn = self.cfg.GetMasterNode()
2148
    all_nodes = self.cfg.GetNodeList()
2149
    return env, [mn], all_nodes
2150

    
2151
  def CheckPrereq(self):
2152
    """Verify that the passed name is a valid one.
2153

2154
    """
2155
    hostname = utils.GetHostInfo(self.op.name)
2156

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

    
2171
    self.op.name = new_name
2172

    
2173
  def Exec(self, feedback_fn):
2174
    """Rename the cluster.
2175

2176
    """
2177
    clustername = self.op.name
2178
    ip = self.ip
2179

    
2180
    # shutdown the master IP
2181
    master = self.cfg.GetMasterNode()
2182
    result = self.rpc.call_node_stop_master(master, False)
2183
    result.Raise("Could not disable the master role")
2184

    
2185
    try:
2186
      cluster = self.cfg.GetClusterInfo()
2187
      cluster.cluster_name = clustername
2188
      cluster.master_ip = ip
2189
      self.cfg.Update(cluster, feedback_fn)
2190

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

    
2207
    finally:
2208
      result = self.rpc.call_node_start_master(master, False, False)
2209
      msg = result.fail_msg
2210
      if msg:
2211
        self.LogWarning("Could not re-enable the master role on"
2212
                        " the master, please restart manually: %s", msg)
2213

    
2214

    
2215
def _RecursiveCheckIfLVMBased(disk):
2216
  """Check if the given disk or its children are lvm-based.
2217

2218
  @type disk: L{objects.Disk}
2219
  @param disk: the disk to check
2220
  @rtype: boolean
2221
  @return: boolean indicating whether a LD_LV dev_type was found or not
2222

2223
  """
2224
  if disk.children:
2225
    for chdisk in disk.children:
2226
      if _RecursiveCheckIfLVMBased(chdisk):
2227
        return True
2228
  return disk.dev_type == constants.LD_LV
2229

    
2230

    
2231
class LUSetClusterParams(LogicalUnit):
2232
  """Change the parameters of the cluster.
2233

2234
  """
2235
  HPATH = "cluster-modify"
2236
  HTYPE = constants.HTYPE_CLUSTER
2237
  _OP_REQP = []
2238
  REQ_BGL = False
2239

    
2240
  def CheckArguments(self):
2241
    """Check parameters
2242

2243
    """
2244
    for attr in ["candidate_pool_size",
2245
                 "uid_pool", "add_uids", "remove_uids"]:
2246
      if not hasattr(self.op, attr):
2247
        setattr(self.op, attr, None)
2248

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

    
2259
    _CheckBooleanOpField(self.op, "maintain_node_health")
2260

    
2261
    if self.op.uid_pool:
2262
      uidpool.CheckUidPool(self.op.uid_pool)
2263

    
2264
    if self.op.add_uids:
2265
      uidpool.CheckUidPool(self.op.add_uids)
2266

    
2267
    if self.op.remove_uids:
2268
      uidpool.CheckUidPool(self.op.remove_uids)
2269

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

    
2278
  def BuildHooksEnv(self):
2279
    """Build hooks env.
2280

2281
    """
2282
    env = {
2283
      "OP_TARGET": self.cfg.GetClusterName(),
2284
      "NEW_VG_NAME": self.op.vg_name,
2285
      }
2286
    mn = self.cfg.GetMasterNode()
2287
    return env, [mn], [mn]
2288

    
2289
  def CheckPrereq(self):
2290
    """Check prerequisites.
2291

2292
    This checks whether the given params don't conflict and
2293
    if the given volume group is valid.
2294

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

    
2305
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2306

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

    
2324
    self.cluster = cluster = self.cfg.GetClusterInfo()
2325
    # validate params changes
2326
    if self.op.beparams:
2327
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2328
      self.new_beparams = objects.FillDict(
2329
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
2330

    
2331
    if self.op.nicparams:
2332
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2333
      self.new_nicparams = objects.FillDict(
2334
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
2335
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2336
      nic_errors = []
2337

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

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

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

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

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

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

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

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

    
2442

    
2443
  def Exec(self, feedback_fn):
2444
    """Change the parameters of the cluster.
2445

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

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

    
2473
    if self.op.maintain_node_health is not None:
2474
      self.cluster.maintain_node_health = self.op.maintain_node_health
2475

    
2476
    if self.op.add_uids is not None:
2477
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
2478

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

    
2482
    if self.op.uid_pool is not None:
2483
      self.cluster.uid_pool = self.op.uid_pool
2484

    
2485
    self.cfg.Update(self.cluster, feedback_fn)
2486

    
2487

    
2488
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2489
  """Distribute additional files which are part of the cluster configuration.
2490

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

2495
  @param lu: calling logical unit
2496
  @param additional_nodes: list of nodes not in the config to distribute to
2497

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

    
2507
  # 2. Gather files to distribute
2508
  dist_files = set([constants.ETC_HOSTS,
2509
                    constants.SSH_KNOWN_HOSTS_FILE,
2510
                    constants.RAPI_CERT_FILE,
2511
                    constants.RAPI_USERS_FILE,
2512
                    constants.CONFD_HMAC_KEY,
2513
                   ])
2514

    
2515
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2516
  for hv_name in enabled_hypervisors:
2517
    hv_class = hypervisor.GetHypervisor(hv_name)
2518
    dist_files.update(hv_class.GetAncillaryFiles())
2519

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

    
2531

    
2532
class LURedistributeConfig(NoHooksLU):
2533
  """Force the redistribution of cluster configuration.
2534

2535
  This is a very simple LU.
2536

2537
  """
2538
  _OP_REQP = []
2539
  REQ_BGL = False
2540

    
2541
  def ExpandNames(self):
2542
    self.needed_locks = {
2543
      locking.LEVEL_NODE: locking.ALL_SET,
2544
    }
2545
    self.share_locks[locking.LEVEL_NODE] = 1
2546

    
2547
  def CheckPrereq(self):
2548
    """Check prerequisites.
2549

2550
    """
2551

    
2552
  def Exec(self, feedback_fn):
2553
    """Redistribute the configuration.
2554

2555
    """
2556
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2557
    _RedistributeAncillaryFiles(self)
2558

    
2559

    
2560
def _WaitForSync(lu, instance, oneshot=False):
2561
  """Sleep and poll for an instance's disk to sync.
2562

2563
  """
2564
  if not instance.disks:
2565
    return True
2566

    
2567
  if not oneshot:
2568
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2569

    
2570
  node = instance.primary_node
2571

    
2572
  for dev in instance.disks:
2573
    lu.cfg.SetDiskID(dev, node)
2574

    
2575
  # TODO: Convert to utils.Retry
2576

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

    
2601
      cumul_degraded = (cumul_degraded or
2602
                        (mstat.is_degraded and mstat.sync_percent is None))
2603
      if mstat.sync_percent is not None:
2604
        done = False
2605
        if mstat.estimated_time is not None:
2606
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
2607
          max_time = mstat.estimated_time
2608
        else:
2609
          rem_time = "no time estimate"
2610
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2611
                        (instance.disks[i].iv_name, mstat.sync_percent,
2612
                         rem_time))
2613

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

    
2623
    if done or oneshot:
2624
      break
2625

    
2626
    time.sleep(min(60, max_time))
2627

    
2628
  if done:
2629
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2630
  return not cumul_degraded
2631

    
2632

    
2633
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2634
  """Check that mirrors are not degraded.
2635

2636
  The ldisk parameter, if True, will change the test from the
2637
  is_degraded attribute (which represents overall non-ok status for
2638
  the device(s)) to the ldisk (representing the local storage status).
2639

2640
  """
2641
  lu.cfg.SetDiskID(dev, node)
2642

    
2643
  result = True
2644

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

    
2660
  if dev.children:
2661
    for child in dev.children:
2662
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2663

    
2664
  return result
2665

    
2666

    
2667
class LUDiagnoseOS(NoHooksLU):
2668
  """Logical unit for OS diagnose/query.
2669

2670
  """
2671
  _OP_REQP = ["output_fields", "names"]
2672
  REQ_BGL = False
2673
  _FIELDS_STATIC = utils.FieldSet()
2674
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants")
2675
  # Fields that need calculation of global os validity
2676
  _FIELDS_NEEDVALID = frozenset(["valid", "variants"])
2677

    
2678
  def ExpandNames(self):
2679
    if self.op.names:
2680
      raise errors.OpPrereqError("Selective OS query not supported",
2681
                                 errors.ECODE_INVAL)
2682

    
2683
    _CheckOutputFields(static=self._FIELDS_STATIC,
2684
                       dynamic=self._FIELDS_DYNAMIC,
2685
                       selected=self.op.output_fields)
2686

    
2687
    # Lock all nodes, in shared mode
2688
    # Temporary removal of locks, should be reverted later
2689
    # TODO: reintroduce locks when they are lighter-weight
2690
    self.needed_locks = {}
2691
    #self.share_locks[locking.LEVEL_NODE] = 1
2692
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2693

    
2694
  def CheckPrereq(self):
2695
    """Check prerequisites.
2696

2697
    """
2698

    
2699
  @staticmethod
2700
  def _DiagnoseByOS(rlist):
2701
    """Remaps a per-node return list into an a per-os per-node dictionary
2702

2703
    @param rlist: a map with node names as keys and OS objects as values
2704

2705
    @rtype: dict
2706
    @return: a dictionary with osnames as keys and as value another map, with
2707
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2708

2709
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2710
                                     (/srv/..., False, "invalid api")],
2711
                           "node2": [(/srv/..., True, "")]}
2712
          }
2713

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

    
2734
  def Exec(self, feedback_fn):
2735
    """Compute the list of OSes.
2736

2737
    """
2738
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2739
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2740
    pol = self._DiagnoseByOS(node_data)
2741
    output = []
2742
    calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields)
2743
    calc_variants = "variants" in self.op.output_fields
2744

    
2745
    for os_name, os_data in pol.items():
2746
      row = []
2747
      if calc_valid:
2748
        valid = True
2749
        variants = None
2750
        for osl in os_data.values():
2751
          valid = valid and osl and osl[0][1]
2752
          if not valid:
2753
            variants = None
2754
            break
2755
          if calc_variants:
2756
            node_variants = osl[0][3]
2757
            if variants is None:
2758
              variants = node_variants
2759
            else:
2760
              variants = [v for v in variants if v in node_variants]
2761

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

    
2779
    return output
2780

    
2781

    
2782
class LURemoveNode(LogicalUnit):
2783
  """Logical unit for removing a node.
2784

2785
  """
2786
  HPATH = "node-remove"
2787
  HTYPE = constants.HTYPE_NODE
2788
  _OP_REQP = ["node_name"]
2789

    
2790
  def BuildHooksEnv(self):
2791
    """Build hooks env.
2792

2793
    This doesn't run on the target node in the pre phase as a failed
2794
    node would then be impossible to remove.
2795

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

    
2809
  def CheckPrereq(self):
2810
    """Check prerequisites.
2811

2812
    This checks:
2813
     - the node exists in the configuration
2814
     - it does not have primary or secondary instances
2815
     - it's not the master
2816

2817
    Any errors are signaled by raising errors.OpPrereqError.
2818

2819
    """
2820
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
2821
    node = self.cfg.GetNodeInfo(self.op.node_name)
2822
    assert node is not None
2823

    
2824
    instance_list = self.cfg.GetInstanceList()
2825

    
2826
    masternode = self.cfg.GetMasterNode()
2827
    if node.name == masternode:
2828
      raise errors.OpPrereqError("Node is the master node,"
2829
                                 " you need to failover first.",
2830
                                 errors.ECODE_INVAL)
2831

    
2832
    for instance_name in instance_list:
2833
      instance = self.cfg.GetInstanceInfo(instance_name)
2834
      if node.name in instance.all_nodes:
2835
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2836
                                   " please remove first." % instance_name,
2837
                                   errors.ECODE_INVAL)
2838
    self.op.node_name = node.name
2839
    self.node = node
2840

    
2841
  def Exec(self, feedback_fn):
2842
    """Removes the node from the cluster.
2843

2844
    """
2845
    node = self.node
2846
    logging.info("Stopping the node daemon and removing configs from node %s",
2847
                 node.name)
2848

    
2849
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
2850

    
2851
    # Promote nodes to master candidate as needed
2852
    _AdjustCandidatePool(self, exceptions=[node.name])
2853
    self.context.RemoveNode(node.name)
2854

    
2855
    # Run post hooks on the node before it's removed
2856
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
2857
    try:
2858
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
2859
    except:
2860
      # pylint: disable-msg=W0702
2861
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
2862

    
2863
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
2864
    msg = result.fail_msg
2865
    if msg:
2866
      self.LogWarning("Errors encountered on the remote node while leaving"
2867
                      " the cluster: %s", msg)
2868

    
2869
    # Remove node from our /etc/hosts
2870
    if self.cfg.GetClusterInfo().modify_etc_hosts:
2871
      # FIXME: this should be done via an rpc call to node daemon
2872
      utils.RemoveHostFromEtcHosts(node.name)
2873
      _RedistributeAncillaryFiles(self)
2874

    
2875

    
2876
class LUQueryNodes(NoHooksLU):
2877
  """Logical unit for querying nodes.
2878

2879
  """
2880
  # pylint: disable-msg=W0142
2881
  _OP_REQP = ["output_fields", "names", "use_locking"]
2882
  REQ_BGL = False
2883

    
2884
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
2885
                    "master_candidate", "offline", "drained"]
2886

    
2887
  _FIELDS_DYNAMIC = utils.FieldSet(
2888
    "dtotal", "dfree",
2889
    "mtotal", "mnode", "mfree",
2890
    "bootid",
2891
    "ctotal", "cnodes", "csockets",
2892
    )
2893

    
2894
  _FIELDS_STATIC = utils.FieldSet(*[
2895
    "pinst_cnt", "sinst_cnt",
2896
    "pinst_list", "sinst_list",
2897
    "pip", "sip", "tags",
2898
    "master",
2899
    "role"] + _SIMPLE_FIELDS
2900
    )
2901

    
2902
  def ExpandNames(self):
2903
    _CheckOutputFields(static=self._FIELDS_STATIC,
2904
                       dynamic=self._FIELDS_DYNAMIC,
2905
                       selected=self.op.output_fields)
2906

    
2907
    self.needed_locks = {}
2908
    self.share_locks[locking.LEVEL_NODE] = 1
2909

    
2910
    if self.op.names:
2911
      self.wanted = _GetWantedNodes(self, self.op.names)
2912
    else:
2913
      self.wanted = locking.ALL_SET
2914

    
2915
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2916
    self.do_locking = self.do_node_query and self.op.use_locking
2917
    if self.do_locking:
2918
      # if we don't request only static fields, we need to lock the nodes
2919
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2920

    
2921
  def CheckPrereq(self):
2922
    """Check prerequisites.
2923

2924
    """
2925
    # The validation of the node list is done in the _GetWantedNodes,
2926
    # if non empty, and if empty, there's no validation to do
2927
    pass
2928

    
2929
  def Exec(self, feedback_fn):
2930
    """Computes the list of nodes and their attributes.
2931

2932
    """
2933
    all_info = self.cfg.GetAllNodesInfo()
2934
    if self.do_locking:
2935
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2936
    elif self.wanted != locking.ALL_SET:
2937
      nodenames = self.wanted
2938
      missing = set(nodenames).difference(all_info.keys())
2939
      if missing:
2940
        raise errors.OpExecError(
2941
          "Some nodes were removed before retrieving their data: %s" % missing)
2942
    else:
2943
      nodenames = all_info.keys()
2944

    
2945
    nodenames = utils.NiceSort(nodenames)
2946
    nodelist = [all_info[name] for name in nodenames]
2947

    
2948
    # begin data gathering
2949

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

    
2975
    node_to_primary = dict([(name, set()) for name in nodenames])
2976
    node_to_secondary = dict([(name, set()) for name in nodenames])
2977

    
2978
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2979
                             "sinst_cnt", "sinst_list"))
2980
    if inst_fields & frozenset(self.op.output_fields):
2981
      inst_data = self.cfg.GetAllInstancesInfo()
2982

    
2983
      for inst in inst_data.values():
2984
        if inst.primary_node in node_to_primary:
2985
          node_to_primary[inst.primary_node].add(inst.name)
2986
        for secnode in inst.secondary_nodes:
2987
          if secnode in node_to_secondary:
2988
            node_to_secondary[secnode].add(inst.name)
2989

    
2990
    master_node = self.cfg.GetMasterNode()
2991

    
2992
    # end data gathering
2993

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

    
3034
    return output
3035

    
3036

    
3037
class LUQueryNodeVolumes(NoHooksLU):
3038
  """Logical unit for getting volumes on node(s).
3039

3040
  """
3041
  _OP_REQP = ["nodes", "output_fields"]
3042
  REQ_BGL = False
3043
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3044
  _FIELDS_STATIC = utils.FieldSet("node")
3045

    
3046
  def ExpandNames(self):
3047
    _CheckOutputFields(static=self._FIELDS_STATIC,
3048
                       dynamic=self._FIELDS_DYNAMIC,
3049
                       selected=self.op.output_fields)
3050

    
3051
    self.needed_locks = {}
3052
    self.share_locks[locking.LEVEL_NODE] = 1
3053
    if not self.op.nodes:
3054
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3055
    else:
3056
      self.needed_locks[locking.LEVEL_NODE] = \
3057
        _GetWantedNodes(self, self.op.nodes)
3058

    
3059
  def CheckPrereq(self):
3060
    """Check prerequisites.
3061

3062
    This checks that the fields required are valid output fields.
3063

3064
    """
3065
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3066

    
3067
  def Exec(self, feedback_fn):
3068
    """Computes the list of nodes and their attributes.
3069

3070
    """
3071
    nodenames = self.nodes
3072
    volumes = self.rpc.call_node_volumes(nodenames)
3073

    
3074
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3075
             in self.cfg.GetInstanceList()]
3076

    
3077
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3078

    
3079
    output = []
3080
    for node in nodenames:
3081
      nresult = volumes[node]
3082
      if nresult.offline:
3083
        continue
3084
      msg = nresult.fail_msg
3085
      if msg:
3086
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3087
        continue
3088

    
3089
      node_vols = nresult.payload[:]
3090
      node_vols.sort(key=lambda vol: vol['dev'])
3091

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

    
3118
        output.append(node_output)
3119

    
3120
    return output
3121

    
3122

    
3123
class LUQueryNodeStorage(NoHooksLU):
3124
  """Logical unit for getting information on storage units on node(s).
3125

3126
  """
3127
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
3128
  REQ_BGL = False
3129
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3130

    
3131
  def CheckArguments(self):
3132
    _CheckStorageType(self.op.storage_type)
3133

    
3134
    _CheckOutputFields(static=self._FIELDS_STATIC,
3135
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3136
                       selected=self.op.output_fields)
3137

    
3138
  def ExpandNames(self):
3139
    self.needed_locks = {}
3140
    self.share_locks[locking.LEVEL_NODE] = 1
3141

    
3142
    if self.op.nodes:
3143
      self.needed_locks[locking.LEVEL_NODE] = \
3144
        _GetWantedNodes(self, self.op.nodes)
3145
    else:
3146
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3147

    
3148
  def CheckPrereq(self):
3149
    """Check prerequisites.
3150

3151
    This checks that the fields required are valid output fields.
3152

3153
    """
3154
    self.op.name = getattr(self.op, "name", None)
3155

    
3156
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3157

    
3158
  def Exec(self, feedback_fn):
3159
    """Computes the list of nodes and their attributes.
3160

3161
    """
3162
    # Always get name to sort by
3163
    if constants.SF_NAME in self.op.output_fields:
3164
      fields = self.op.output_fields[:]
3165
    else:
3166
      fields = [constants.SF_NAME] + self.op.output_fields
3167

    
3168
    # Never ask for node or type as it's only known to the LU
3169
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3170
      while extra in fields:
3171
        fields.remove(extra)
3172

    
3173
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3174
    name_idx = field_idx[constants.SF_NAME]
3175

    
3176
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3177
    data = self.rpc.call_storage_list(self.nodes,
3178
                                      self.op.storage_type, st_args,
3179
                                      self.op.name, fields)
3180

    
3181
    result = []
3182

    
3183
    for node in utils.NiceSort(self.nodes):
3184
      nresult = data[node]
3185
      if nresult.offline:
3186
        continue
3187

    
3188
      msg = nresult.fail_msg
3189
      if msg:
3190
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3191
        continue
3192

    
3193
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3194

    
3195
      for name in utils.NiceSort(rows.keys()):
3196
        row = rows[name]
3197

    
3198
        out = []
3199

    
3200
        for field in self.op.output_fields:
3201
          if field == constants.SF_NODE:
3202
            val = node
3203
          elif field == constants.SF_TYPE:
3204
            val = self.op.storage_type
3205
          elif field in field_idx:
3206
            val = row[field_idx[field]]
3207
          else:
3208
            raise errors.ParameterError(field)
3209

    
3210
          out.append(val)
3211

    
3212
        result.append(out)
3213

    
3214
    return result
3215

    
3216

    
3217
class LUModifyNodeStorage(NoHooksLU):
3218
  """Logical unit for modifying a storage volume on a node.
3219

3220
  """
3221
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
3222
  REQ_BGL = False
3223

    
3224
  def CheckArguments(self):
3225
    self.opnode_name = _ExpandNodeName(self.cfg, self.op.node_name)
3226

    
3227
    _CheckStorageType(self.op.storage_type)
3228

    
3229
  def ExpandNames(self):
3230
    self.needed_locks = {
3231
      locking.LEVEL_NODE: self.op.node_name,
3232
      }
3233

    
3234
  def CheckPrereq(self):
3235
    """Check prerequisites.
3236

3237
    """
3238
    storage_type = self.op.storage_type
3239

    
3240
    try:
3241
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3242
    except KeyError:
3243
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3244
                                 " modified" % storage_type,
3245
                                 errors.ECODE_INVAL)
3246

    
3247
    diff = set(self.op.changes.keys()) - modifiable
3248
    if diff:
3249
      raise errors.OpPrereqError("The following fields can not be modified for"
3250
                                 " storage units of type '%s': %r" %
3251
                                 (storage_type, list(diff)),
3252
                                 errors.ECODE_INVAL)
3253

    
3254
  def Exec(self, feedback_fn):
3255
    """Computes the list of nodes and their attributes.
3256

3257
    """
3258
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3259
    result = self.rpc.call_storage_modify(self.op.node_name,
3260
                                          self.op.storage_type, st_args,
3261
                                          self.op.name, self.op.changes)
3262
    result.Raise("Failed to modify storage unit '%s' on %s" %
3263
                 (self.op.name, self.op.node_name))
3264

    
3265

    
3266
class LUAddNode(LogicalUnit):
3267
  """Logical unit for adding node to the cluster.
3268

3269
  """
3270
  HPATH = "node-add"
3271
  HTYPE = constants.HTYPE_NODE
3272
  _OP_REQP = ["node_name"]
3273

    
3274
  def CheckArguments(self):
3275
    # validate/normalize the node name
3276
    self.op.node_name = utils.HostInfo.NormalizeName(self.op.node_name)
3277

    
3278
  def BuildHooksEnv(self):
3279
    """Build hooks env.
3280

3281
    This will run on all nodes before, and on all nodes + the new node after.
3282

3283
    """
3284
    env = {
3285
      "OP_TARGET": self.op.node_name,
3286
      "NODE_NAME": self.op.node_name,
3287
      "NODE_PIP": self.op.primary_ip,
3288
      "NODE_SIP": self.op.secondary_ip,
3289
      }
3290
    nodes_0 = self.cfg.GetNodeList()
3291
    nodes_1 = nodes_0 + [self.op.node_name, ]
3292
    return env, nodes_0, nodes_1
3293

    
3294
  def CheckPrereq(self):
3295
    """Check prerequisites.
3296

3297
    This checks:
3298
     - the new node is not already in the config
3299
     - it is resolvable
3300
     - its parameters (single/dual homed) matches the cluster
3301

3302
    Any errors are signaled by raising errors.OpPrereqError.
3303

3304
    """
3305
    node_name = self.op.node_name
3306
    cfg = self.cfg
3307

    
3308
    dns_data = utils.GetHostInfo(node_name)
3309

    
3310
    node = dns_data.name
3311
    primary_ip = self.op.primary_ip = dns_data.ip
3312
    secondary_ip = getattr(self.op, "secondary_ip", None)
3313
    if secondary_ip is None:
3314
      secondary_ip = primary_ip
3315
    if not utils.IsValidIP(secondary_ip):
3316
      raise errors.OpPrereqError("Invalid secondary IP given",
3317
                                 errors.ECODE_INVAL)
3318
    self.op.secondary_ip = secondary_ip
3319

    
3320
    node_list = cfg.GetNodeList()
3321
    if not self.op.readd and node in node_list:
3322
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3323
                                 node, errors.ECODE_EXISTS)
3324
    elif self.op.readd and node not in node_list:
3325
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3326
                                 errors.ECODE_NOENT)
3327

    
3328
    self.changed_primary_ip = False
3329

    
3330
    for existing_node_name in node_list:
3331
      existing_node = cfg.GetNodeInfo(existing_node_name)
3332

    
3333
      if self.op.readd and node == existing_node_name:
3334
        if existing_node.secondary_ip != secondary_ip:
3335
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3336
                                     " address configuration as before",
3337
                                     errors.ECODE_INVAL)
3338
        if existing_node.primary_ip != primary_ip:
3339
          self.changed_primary_ip = True
3340

    
3341
        continue
3342

    
3343
      if (existing_node.primary_ip == primary_ip or
3344
          existing_node.secondary_ip == primary_ip or
3345
          existing_node.primary_ip == secondary_ip or
3346
          existing_node.secondary_ip == secondary_ip):
3347
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3348
                                   " existing node %s" % existing_node.name,
3349
                                   errors.ECODE_NOTUNIQUE)
3350

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

    
3366
    # checks reachability
3367
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3368
      raise errors.OpPrereqError("Node not reachable by ping",
3369
                                 errors.ECODE_ENVIRON)
3370

    
3371
    if not newbie_singlehomed:
3372
      # check reachability from my secondary ip to newbie's secondary ip
3373
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3374
                           source=myself.secondary_ip):
3375
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3376
                                   " based ping to noded port",
3377
                                   errors.ECODE_ENVIRON)
3378

    
3379
    if self.op.readd:
3380
      exceptions = [node]
3381
    else:
3382
      exceptions = []
3383

    
3384
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3385

    
3386
    if self.op.readd:
3387
      self.new_node = self.cfg.GetNodeInfo(node)
3388
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3389
    else:
3390
      self.new_node = objects.Node(name=node,
3391
                                   primary_ip=primary_ip,
3392
                                   secondary_ip=secondary_ip,
3393
                                   master_candidate=self.master_candidate,
3394
                                   offline=False, drained=False)
3395

    
3396
  def Exec(self, feedback_fn):
3397
    """Adds the new node to the cluster.
3398

3399
    """
3400
    new_node = self.new_node
3401
    node = new_node.name
3402

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

    
3415
    # notify the user about any possible mc promotion
3416
    if new_node.master_candidate:
3417
      self.LogInfo("Node will be a master candidate")
3418

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

    
3430
    # setup ssh on node
3431
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3432
      logging.info("Copy ssh key to node %s", node)
3433
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3434
      keyarray = []
3435
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3436
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3437
                  priv_key, pub_key]
3438

    
3439
      for i in keyfiles:
3440
        keyarray.append(utils.ReadFile(i))
3441

    
3442
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3443
                                      keyarray[2], keyarray[3], keyarray[4],
3444
                                      keyarray[5])
3445
      result.Raise("Cannot transfer ssh keys to the new node")
3446

    
3447
    # Add node to our /etc/hosts, and add key to known_hosts
3448
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3449
      # FIXME: this should be done via an rpc call to node daemon
3450
      utils.AddHostToEtcHosts(new_node.name)
3451

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

    
3462
    node_verify_list = [self.cfg.GetMasterNode()]
3463
    node_verify_param = {
3464
      constants.NV_NODELIST: [node],
3465
      # TODO: do a node-net-test as well?
3466
    }
3467

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

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

    
3496

    
3497
class LUSetNodeParams(LogicalUnit):
3498
  """Modifies the parameters of a node.
3499

3500
  """
3501
  HPATH = "node-modify"
3502
  HTYPE = constants.HTYPE_NODE
3503
  _OP_REQP = ["node_name"]
3504
  REQ_BGL = False
3505

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

    
3521
    # Boolean value that tells us whether we're offlining or draining the node
3522
    self.offline_or_drain = (self.op.offline == True or
3523
                             self.op.drained == True)
3524
    self.deoffline_or_drain = (self.op.offline == False or
3525
                               self.op.drained == False)
3526
    self.might_demote = (self.op.master_candidate == False or
3527
                         self.offline_or_drain)
3528

    
3529
    self.lock_all = self.op.auto_promote and self.might_demote
3530

    
3531

    
3532
  def ExpandNames(self):
3533
    if self.lock_all:
3534
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3535
    else:
3536
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3537

    
3538
  def BuildHooksEnv(self):
3539
    """Build hooks env.
3540

3541
    This runs on the master node.
3542

3543
    """
3544
    env = {
3545
      "OP_TARGET": self.op.node_name,
3546
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3547
      "OFFLINE": str(self.op.offline),
3548
      "DRAINED": str(self.op.drained),
3549
      }
3550
    nl = [self.cfg.GetMasterNode(),
3551
          self.op.node_name]
3552
    return env, nl, nl
3553

    
3554
  def CheckPrereq(self):
3555
    """Check prerequisites.
3556

3557
    This only checks the instance list against the existing names.
3558

3559
    """
3560
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3561

    
3562
    if (self.op.master_candidate is not None or
3563
        self.op.drained is not None or
3564
        self.op.offline is not None):
3565
      # we can't change the master's node flags
3566
      if self.op.node_name == self.cfg.GetMasterNode():
3567
        raise errors.OpPrereqError("The master role can be changed"
3568
                                   " only via masterfailover",
3569
                                   errors.ECODE_INVAL)
3570

    
3571

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

    
3583
    if (self.op.master_candidate == True and
3584
        ((node.offline and not self.op.offline == False) or
3585
         (node.drained and not self.op.drained == False))):
3586
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3587
                                 " to master_candidate" % node.name,
3588
                                 errors.ECODE_INVAL)
3589

    
3590
    # If we're being deofflined/drained, we'll MC ourself if needed
3591
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3592
        self.op.master_candidate == True and not node.master_candidate):
3593
      self.op.master_candidate = _DecideSelfPromotion(self)
3594
      if self.op.master_candidate:
3595
        self.LogInfo("Autopromoting node to master candidate")
3596

    
3597
    return
3598

    
3599
  def Exec(self, feedback_fn):
3600
    """Modifies a node.
3601

3602
    """
3603
    node = self.node
3604

    
3605
    result = []
3606
    changed_mc = False
3607

    
3608
    if self.op.offline is not None:
3609
      node.offline = self.op.offline
3610
      result.append(("offline", str(self.op.offline)))
3611
      if self.op.offline == True:
3612
        if node.master_candidate:
3613
          node.master_candidate = False
3614
          changed_mc = True
3615
          result.append(("master_candidate", "auto-demotion due to offline"))
3616
        if node.drained:
3617
          node.drained = False
3618
          result.append(("drained", "clear drained status due to offline"))
3619

    
3620
    if self.op.master_candidate is not None:
3621
      node.master_candidate = self.op.master_candidate
3622
      changed_mc = True
3623
      result.append(("master_candidate", str(self.op.master_candidate)))
3624
      if self.op.master_candidate == False:
3625
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3626
        msg = rrc.fail_msg
3627
        if msg:
3628
          self.LogWarning("Node failed to demote itself: %s" % msg)
3629

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

    
3646
    # we locked all nodes, we adjust the CP before updating this node
3647
    if self.lock_all:
3648
      _AdjustCandidatePool(self, [node.name])
3649

    
3650
    # this will trigger configuration file update, if needed
3651
    self.cfg.Update(node, feedback_fn)
3652

    
3653
    # this will trigger job queue propagation or cleanup
3654
    if changed_mc:
3655
      self.context.ReaddNode(node)
3656

    
3657
    return result
3658

    
3659

    
3660
class LUPowercycleNode(NoHooksLU):
3661
  """Powercycles a node.
3662

3663
  """
3664
  _OP_REQP = ["node_name", "force"]
3665
  REQ_BGL = False
3666

    
3667
  def CheckArguments(self):
3668
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3669
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
3670
      raise errors.OpPrereqError("The node is the master and the force"
3671
                                 " parameter was not set",
3672
                                 errors.ECODE_INVAL)
3673

    
3674
  def ExpandNames(self):
3675
    """Locking for PowercycleNode.
3676

3677
    This is a last-resort option and shouldn't block on other
3678
    jobs. Therefore, we grab no locks.
3679

3680
    """
3681
    self.needed_locks = {}
3682

    
3683
  def CheckPrereq(self):
3684
    """Check prerequisites.
3685

3686
    This LU has no prereqs.
3687

3688
    """
3689
    pass
3690

    
3691
  def Exec(self, feedback_fn):
3692
    """Reboots a node.
3693

3694
    """
3695
    result = self.rpc.call_node_powercycle(self.op.node_name,
3696
                                           self.cfg.GetHypervisorType())
3697
    result.Raise("Failed to schedule the reboot")
3698
    return result.payload
3699

    
3700

    
3701
class LUQueryClusterInfo(NoHooksLU):
3702
  """Query cluster configuration.
3703

3704
  """
3705
  _OP_REQP = []
3706
  REQ_BGL = False
3707

    
3708
  def ExpandNames(self):
3709
    self.needed_locks = {}
3710

    
3711
  def CheckPrereq(self):
3712
    """No prerequsites needed for this LU.
3713

3714
    """
3715
    pass
3716

    
3717
  def Exec(self, feedback_fn):
3718
    """Return cluster config.
3719

3720
    """
3721
    cluster = self.cfg.GetClusterInfo()
3722
    os_hvp = {}
3723

    
3724
    # Filter just for enabled hypervisors
3725
    for os_name, hv_dict in cluster.os_hvp.items():
3726
      os_hvp[os_name] = {}
3727
      for hv_name, hv_params in hv_dict.items():
3728
        if hv_name in cluster.enabled_hypervisors:
3729
          os_hvp[os_name][hv_name] = hv_params
3730

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

    
3759
    return result
3760

    
3761

    
3762
class LUQueryConfigValues(NoHooksLU):
3763
  """Return configuration values.
3764

3765
  """
3766
  _OP_REQP = []
3767
  REQ_BGL = False
3768
  _FIELDS_DYNAMIC = utils.FieldSet()
3769
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
3770
                                  "watcher_pause")
3771

    
3772
  def ExpandNames(self):
3773
    self.needed_locks = {}
3774

    
3775
    _CheckOutputFields(static=self._FIELDS_STATIC,
3776
                       dynamic=self._FIELDS_DYNAMIC,
3777
                       selected=self.op.output_fields)
3778

    
3779
  def CheckPrereq(self):
3780
    """No prerequisites.
3781

3782
    """
3783
    pass
3784

    
3785
  def Exec(self, feedback_fn):
3786
    """Dump a representation of the cluster config to the standard output.
3787

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

    
3804

    
3805
class LUActivateInstanceDisks(NoHooksLU):
3806
  """Bring up an instance's disks.
3807

3808
  """
3809
  _OP_REQP = ["instance_name"]
3810
  REQ_BGL = False
3811

    
3812
  def ExpandNames(self):
3813
    self._ExpandAndLockInstance()
3814
    self.needed_locks[locking.LEVEL_NODE] = []
3815
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3816

    
3817
  def DeclareLocks(self, level):
3818
    if level == locking.LEVEL_NODE:
3819
      self._LockInstancesNodes()
3820

    
3821
  def CheckPrereq(self):
3822
    """Check prerequisites.
3823

3824
    This checks that the instance is in the cluster.
3825

3826
    """
3827
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3828
    assert self.instance is not None, \
3829
      "Cannot retrieve locked instance %s" % self.op.instance_name
3830
    _CheckNodeOnline(self, self.instance.primary_node)
3831
    if not hasattr(self.op, "ignore_size"):
3832
      self.op.ignore_size = False
3833

    
3834
  def Exec(self, feedback_fn):
3835
    """Activate the disks.
3836

3837
    """
3838
    disks_ok, disks_info = \
3839
              _AssembleInstanceDisks(self, self.instance,
3840
                                     ignore_size=self.op.ignore_size)
3841
    if not disks_ok:
3842
      raise errors.OpExecError("Cannot activate block devices")
3843

    
3844
    return disks_info
3845

    
3846

    
3847
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3848
                           ignore_size=False):
3849
  """Prepare the block devices for an instance.
3850

3851
  This sets up the block devices on all nodes.
3852

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

3868
  """
3869
  device_info = []
3870
  disks_ok = True
3871
  iname = instance.name
3872
  # With the two passes mechanism we try to reduce the window of
3873
  # opportunity for the race condition of switching DRBD to primary
3874
  # before handshaking occured, but we do not eliminate it
3875

    
3876
  # The proper fix would be to wait (with some limits) until the
3877
  # connection has been made and drbd transitions from WFConnection
3878
  # into any other network-connected state (Connected, SyncTarget,
3879
  # SyncSource, etc.)
3880

    
3881
  # 1st pass, assemble on all nodes in secondary mode
3882
  for inst_disk in instance.disks:
3883
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3884
      if ignore_size:
3885
        node_disk = node_disk.Copy()
3886
        node_disk.UnsetSize()
3887
      lu.cfg.SetDiskID(node_disk, node)
3888
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3889
      msg = result.fail_msg
3890
      if msg:
3891
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3892
                           " (is_primary=False, pass=1): %s",
3893
                           inst_disk.iv_name, node, msg)
3894
        if not ignore_secondaries:
3895
          disks_ok = False
3896

    
3897
  # FIXME: race condition on drbd migration to primary
3898

    
3899
  # 2nd pass, do only the primary node
3900
  for inst_disk in instance.disks:
3901
    dev_path = None
3902

    
3903
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3904
      if node != instance.primary_node:
3905
        continue
3906
      if ignore_size:
3907
        node_disk = node_disk.Copy()
3908
        node_disk.UnsetSize()
3909
      lu.cfg.SetDiskID(node_disk, node)
3910
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3911
      msg = result.fail_msg
3912
      if msg:
3913
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3914
                           " (is_primary=True, pass=2): %s",
3915
                           inst_disk.iv_name, node, msg)
3916
        disks_ok = False
3917
      else:
3918
        dev_path = result.payload
3919

    
3920
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
3921

    
3922
  # leave the disks configured for the primary node
3923
  # this is a workaround that would be fixed better by
3924
  # improving the logical/physical id handling
3925
  for disk in instance.disks:
3926
    lu.cfg.SetDiskID(disk, instance.primary_node)
3927

    
3928
  return disks_ok, device_info
3929

    
3930

    
3931
def _StartInstanceDisks(lu, instance, force):
3932
  """Start the disks of an instance.
3933

3934
  """
3935
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3936
                                           ignore_secondaries=force)
3937
  if not disks_ok:
3938
    _ShutdownInstanceDisks(lu, instance)
3939
    if force is not None and not force:
3940
      lu.proc.LogWarning("", hint="If the message above refers to a"
3941
                         " secondary node,"
3942
                         " you can retry the operation using '--force'.")
3943
    raise errors.OpExecError("Disk consistency error")
3944

    
3945

    
3946
class LUDeactivateInstanceDisks(NoHooksLU):
3947
  """Shutdown an instance's disks.
3948

3949
  """
3950
  _OP_REQP = ["instance_name"]
3951
  REQ_BGL = False
3952

    
3953
  def ExpandNames(self):
3954
    self._ExpandAndLockInstance()
3955
    self.needed_locks[locking.LEVEL_NODE] = []
3956
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3957

    
3958
  def DeclareLocks(self, level):
3959
    if level == locking.LEVEL_NODE:
3960
      self._LockInstancesNodes()
3961

    
3962
  def CheckPrereq(self):
3963
    """Check prerequisites.
3964

3965
    This checks that the instance is in the cluster.
3966

3967
    """
3968
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3969
    assert self.instance is not None, \
3970
      "Cannot retrieve locked instance %s" % self.op.instance_name
3971

    
3972
  def Exec(self, feedback_fn):
3973
    """Deactivate the disks
3974

3975
    """
3976
    instance = self.instance
3977
    _SafeShutdownInstanceDisks(self, instance)
3978

    
3979

    
3980
def _SafeShutdownInstanceDisks(lu, instance):
3981
  """Shutdown block devices of an instance.
3982

3983
  This function checks if an instance is running, before calling
3984
  _ShutdownInstanceDisks.
3985

3986
  """
3987
  _CheckInstanceDown(lu, instance, "cannot shutdown disks")
3988
  _ShutdownInstanceDisks(lu, instance)
3989

    
3990

    
3991
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3992
  """Shutdown block devices of an instance.
3993

3994
  This does the shutdown on all nodes of the instance.
3995

3996
  If the ignore_primary is false, errors on the primary node are
3997
  ignored.
3998

3999
  """
4000
  all_result = True
4001
  for disk in instance.disks:
4002
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
4003
      lu.cfg.SetDiskID(top_disk, node)
4004
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
4005
      msg = result.fail_msg
4006
      if msg:
4007
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
4008
                      disk.iv_name, node, msg)
4009
        if not ignore_primary or node != instance.primary_node:
4010
          all_result = False
4011
  return all_result
4012

    
4013

    
4014
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
4015
  """Checks if a node has enough free memory.
4016

4017
  This function check if a given node has the needed amount of free
4018
  memory. In case the node has less memory or we cannot get the
4019
  information from the node, this function raise an OpPrereqError
4020
  exception.
4021

4022
  @type lu: C{LogicalUnit}
4023
  @param lu: a logical unit from which we get configuration data
4024
  @type node: C{str}
4025
  @param node: the node to check
4026
  @type reason: C{str}
4027
  @param reason: string to use in the error message
4028
  @type requested: C{int}
4029
  @param requested: the amount of memory in MiB to check for
4030
  @type hypervisor_name: C{str}
4031
  @param hypervisor_name: the hypervisor to ask for memory stats
4032
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
4033
      we cannot check the node
4034

4035
  """
4036
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
4037
  nodeinfo[node].Raise("Can't get data from node %s" % node,
4038
                       prereq=True, ecode=errors.ECODE_ENVIRON)
4039