Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 3953242f

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

    
48

    
49
class LogicalUnit(object):
50
  """Logical Unit base class.
51

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

61
  Note that all commands require root permissions.
62

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

66
  """
67
  HPATH = None
68
  HTYPE = None
69
  _OP_REQP = []
70
  REQ_BGL = True
71

    
72
  def __init__(self, processor, op, context, rpc):
73
    """Constructor for LogicalUnit.
74

75
    This needs to be overridden in derived classes in order to check op
76
    validity.
77

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

    
104
    # Tasklets
105
    self.tasklets = None
106

    
107
    for attr_name in self._OP_REQP:
108
      attr_val = getattr(op, attr_name, None)
109
      if attr_val is None:
110
        raise errors.OpPrereqError("Required parameter '%s' missing" %
111
                                   attr_name, errors.ECODE_INVAL)
112

    
113
    self.CheckArguments()
114

    
115
  def __GetSSH(self):
116
    """Returns the SshRunner object
117

118
    """
119
    if not self.__ssh:
120
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
121
    return self.__ssh
122

    
123
  ssh = property(fget=__GetSSH)
124

    
125
  def CheckArguments(self):
126
    """Check syntactic validity for the opcode arguments.
127

128
    This method is for doing a simple syntactic check and ensure
129
    validity of opcode parameters, without any cluster-related
130
    checks. While the same can be accomplished in ExpandNames and/or
131
    CheckPrereq, doing these separate is better because:
132

133
      - ExpandNames is left as as purely a lock-related function
134
      - CheckPrereq is run after we have acquired locks (and possible
135
        waited for them)
136

137
    The function is allowed to change the self.op attribute so that
138
    later methods can no longer worry about missing parameters.
139

140
    """
141
    pass
142

    
143
  def ExpandNames(self):
144
    """Expand names for this LU.
145

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

151
    LUs which implement this method must also populate the self.needed_locks
152
    member, as a dict with lock levels as keys, and a list of needed lock names
153
    as values. Rules:
154

155
      - use an empty dict if you don't need any lock
156
      - if you don't need any lock at a particular level omit that level
157
      - don't put anything for the BGL level
158
      - if you want all locks at a level use locking.ALL_SET as a value
159

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

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

168
    Examples::
169

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

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

    
191
  def DeclareLocks(self, level):
192
    """Declare LU locking needs for a level
193

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

201
    This function is only called if you have something already set in
202
    self.needed_locks for the level.
203

204
    @param level: Locking level which is going to be locked
205
    @type level: member of ganeti.locking.LEVELS
206

207
    """
208

    
209
  def CheckPrereq(self):
210
    """Check prerequisites for this LU.
211

212
    This method should check that the prerequisites for the execution
213
    of this LU are fulfilled. It can do internode communication, but
214
    it should be idempotent - no cluster or system changes are
215
    allowed.
216

217
    The method should raise errors.OpPrereqError in case something is
218
    not fulfilled. Its return value is ignored.
219

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

223
    """
224
    if self.tasklets is not None:
225
      for (idx, tl) in enumerate(self.tasklets):
226
        logging.debug("Checking prerequisites for tasklet %s/%s",
227
                      idx + 1, len(self.tasklets))
228
        tl.CheckPrereq()
229
    else:
230
      raise NotImplementedError
231

    
232
  def Exec(self, feedback_fn):
233
    """Execute the LU.
234

235
    This method should implement the actual work. It should raise
236
    errors.OpExecError for failures that are somewhat dealt with in
237
    code, or expected.
238

239
    """
240
    if self.tasklets is not None:
241
      for (idx, tl) in enumerate(self.tasklets):
242
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
243
        tl.Exec(feedback_fn)
244
    else:
245
      raise NotImplementedError
246

    
247
  def BuildHooksEnv(self):
248
    """Build hooks environment for this LU.
249

250
    This method should return a three-node tuple consisting of: a dict
251
    containing the environment that will be used for running the
252
    specific hook for this LU, a list of node names on which the hook
253
    should run before the execution, and a list of node names on which
254
    the hook should run after the execution.
255

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

261
    No nodes should be returned as an empty list (and not None).
262

263
    Note that if the HPATH for a LU class is None, this function will
264
    not be called.
265

266
    """
267
    raise NotImplementedError
268

    
269
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
270
    """Notify the LU about the results of its hooks.
271

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

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

287
    """
288
    # API must be kept, thus we ignore the unused argument and could
289
    # be a function warnings
290
    # pylint: disable-msg=W0613,R0201
291
    return lu_result
292

    
293
  def _ExpandAndLockInstance(self):
294
    """Helper function to expand and lock an instance.
295

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

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

    
312
  def _LockInstancesNodes(self, primary_only=False):
313
    """Helper function to declare instances' nodes for locking.
314

315
    This function should be called after locking one or more instances to lock
316
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
317
    with all primary or secondary nodes for instances already locked and
318
    present in self.needed_locks[locking.LEVEL_INSTANCE].
319

320
    It should be called from DeclareLocks, and for safety only works if
321
    self.recalculate_locks[locking.LEVEL_NODE] is set.
322

323
    In the future it may grow parameters to just lock some instance's nodes, or
324
    to just lock primaries or secondary nodes, if needed.
325

326
    If should be called in DeclareLocks in a way similar to::
327

328
      if level == locking.LEVEL_NODE:
329
        self._LockInstancesNodes()
330

331
    @type primary_only: boolean
332
    @param primary_only: only lock primary nodes of locked instances
333

334
    """
335
    assert locking.LEVEL_NODE in self.recalculate_locks, \
336
      "_LockInstancesNodes helper function called with no nodes to recalculate"
337

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

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

    
350
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
351
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
352
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
353
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
354

    
355
    del self.recalculate_locks[locking.LEVEL_NODE]
356

    
357

    
358
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
359
  """Simple LU which runs no hooks.
360

361
  This LU is intended as a parent for other LogicalUnits which will
362
  run no hooks, in order to reduce duplicate code.
363

364
  """
365
  HPATH = None
366
  HTYPE = None
367

    
368
  def BuildHooksEnv(self):
369
    """Empty BuildHooksEnv for NoHooksLu.
370

371
    This just raises an error.
372

373
    """
374
    assert False, "BuildHooksEnv called for NoHooksLUs"
375

    
376

    
377
class Tasklet:
378
  """Tasklet base class.
379

380
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
381
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
382
  tasklets know nothing about locks.
383

384
  Subclasses must follow these rules:
385
    - Implement CheckPrereq
386
    - Implement Exec
387

388
  """
389
  def __init__(self, lu):
390
    self.lu = lu
391

    
392
    # Shortcuts
393
    self.cfg = lu.cfg
394
    self.rpc = lu.rpc
395

    
396
  def CheckPrereq(self):
397
    """Check prerequisites for this tasklets.
398

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

403
    The method should raise errors.OpPrereqError in case something is not
404
    fulfilled. Its return value is ignored.
405

406
    This method should also update all parameters to their canonical form if it
407
    hasn't been done before.
408

409
    """
410
    raise NotImplementedError
411

    
412
  def Exec(self, feedback_fn):
413
    """Execute the tasklet.
414

415
    This method should implement the actual work. It should raise
416
    errors.OpExecError for failures that are somewhat dealt with in code, or
417
    expected.
418

419
    """
420
    raise NotImplementedError
421

    
422

    
423
def _GetWantedNodes(lu, nodes):
424
  """Returns list of checked and expanded node names.
425

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

434
  """
435
  if not isinstance(nodes, list):
436
    raise errors.OpPrereqError("Invalid argument type 'nodes'",
437
                               errors.ECODE_INVAL)
438

    
439
  if not nodes:
440
    raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
441
      " non-empty list of nodes whose name is to be expanded.")
442

    
443
  wanted = [_ExpandNodeName(lu.cfg, name) for name in nodes]
444
  return utils.NiceSort(wanted)
445

    
446

    
447
def _GetWantedInstances(lu, instances):
448
  """Returns list of checked and expanded instance names.
449

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

459
  """
460
  if not isinstance(instances, list):
461
    raise errors.OpPrereqError("Invalid argument type 'instances'",
462
                               errors.ECODE_INVAL)
463

    
464
  if instances:
465
    wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
466
  else:
467
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
468
  return wanted
469

    
470

    
471
def _CheckOutputFields(static, dynamic, selected):
472
  """Checks whether all selected fields are valid.
473

474
  @type static: L{utils.FieldSet}
475
  @param static: static fields set
476
  @type dynamic: L{utils.FieldSet}
477
  @param dynamic: dynamic fields set
478

479
  """
480
  f = utils.FieldSet()
481
  f.Extend(static)
482
  f.Extend(dynamic)
483

    
484
  delta = f.NonMatching(selected)
485
  if delta:
486
    raise errors.OpPrereqError("Unknown output fields selected: %s"
487
                               % ",".join(delta), errors.ECODE_INVAL)
488

    
489

    
490
def _CheckBooleanOpField(op, name):
491
  """Validates boolean opcode parameters.
492

493
  This will ensure that an opcode parameter is either a boolean value,
494
  or None (but that it always exists).
495

496
  """
497
  val = getattr(op, name, None)
498
  if not (val is None or isinstance(val, bool)):
499
    raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" %
500
                               (name, str(val)), errors.ECODE_INVAL)
501
  setattr(op, name, val)
502

    
503

    
504
def _CheckGlobalHvParams(params):
505
  """Validates that given hypervisor params are not global ones.
506

507
  This will ensure that instances don't get customised versions of
508
  global params.
509

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

    
518

    
519
def _CheckNodeOnline(lu, node):
520
  """Ensure that a given node is online.
521

522
  @param lu: the LU on behalf of which we make the check
523
  @param node: the node to check
524
  @raise errors.OpPrereqError: if the node is offline
525

526
  """
527
  if lu.cfg.GetNodeInfo(node).offline:
528
    raise errors.OpPrereqError("Can't use offline node %s" % node,
529
                               errors.ECODE_INVAL)
530

    
531

    
532
def _CheckNodeNotDrained(lu, node):
533
  """Ensure that a given node is not drained.
534

535
  @param lu: the LU on behalf of which we make the check
536
  @param node: the node to check
537
  @raise errors.OpPrereqError: if the node is drained
538

539
  """
540
  if lu.cfg.GetNodeInfo(node).drained:
541
    raise errors.OpPrereqError("Can't use drained node %s" % node,
542
                               errors.ECODE_INVAL)
543

    
544

    
545
def _CheckNodeHasOS(lu, node, os_name, force_variant):
546
  """Ensure that a node supports a given OS.
547

548
  @param lu: the LU on behalf of which we make the check
549
  @param node: the node to check
550
  @param os_name: the OS to query about
551
  @param force_variant: whether to ignore variant errors
552
  @raise errors.OpPrereqError: if the node is not supporting the OS
553

554
  """
555
  result = lu.rpc.call_os_get(node, os_name)
556
  result.Raise("OS '%s' not in supported OS list for node %s" %
557
               (os_name, node),
558
               prereq=True, ecode=errors.ECODE_INVAL)
559
  if not force_variant:
560
    _CheckOSVariant(result.payload, os_name)
561

    
562

    
563
def _CheckDiskTemplate(template):
564
  """Ensure a given disk template is valid.
565

566
  """
567
  if template not in constants.DISK_TEMPLATES:
568
    msg = ("Invalid disk template name '%s', valid templates are: %s" %
569
           (template, utils.CommaJoin(constants.DISK_TEMPLATES)))
570
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
571

    
572

    
573
def _CheckInstanceDown(lu, instance, reason):
574
  """Ensure that an instance is not running."""
575
  if instance.admin_up:
576
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
577
                               (instance.name, reason), errors.ECODE_STATE)
578

    
579
  pnode = instance.primary_node
580
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
581
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
582
              prereq=True, ecode=errors.ECODE_ENVIRON)
583

    
584
  if instance.name in ins_l.payload:
585
    raise errors.OpPrereqError("Instance %s is running, %s" %
586
                               (instance.name, reason), errors.ECODE_STATE)
587

    
588

    
589
def _ExpandItemName(fn, name, kind):
590
  """Expand an item name.
591

592
  @param fn: the function to use for expansion
593
  @param name: requested item name
594
  @param kind: text description ('Node' or 'Instance')
595
  @return: the resolved (full) name
596
  @raise errors.OpPrereqError: if the item is not found
597

598
  """
599
  full_name = fn(name)
600
  if full_name is None:
601
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
602
                               errors.ECODE_NOENT)
603
  return full_name
604

    
605

    
606
def _ExpandNodeName(cfg, name):
607
  """Wrapper over L{_ExpandItemName} for nodes."""
608
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
609

    
610

    
611
def _ExpandInstanceName(cfg, name):
612
  """Wrapper over L{_ExpandItemName} for instance."""
613
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
614

    
615

    
616
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
617
                          memory, vcpus, nics, disk_template, disks,
618
                          bep, hvp, hypervisor_name):
619
  """Builds instance related env variables for hooks
620

621
  This builds the hook environment from individual variables.
622

623
  @type name: string
624
  @param name: the name of the instance
625
  @type primary_node: string
626
  @param primary_node: the name of the instance's primary node
627
  @type secondary_nodes: list
628
  @param secondary_nodes: list of secondary nodes as strings
629
  @type os_type: string
630
  @param os_type: the name of the instance's OS
631
  @type status: boolean
632
  @param status: the should_run status of the instance
633
  @type memory: string
634
  @param memory: the memory size of the instance
635
  @type vcpus: string
636
  @param vcpus: the count of VCPUs the instance has
637
  @type nics: list
638
  @param nics: list of tuples (ip, mac, mode, link) representing
639
      the NICs the instance has
640
  @type disk_template: string
641
  @param disk_template: the disk template of the instance
642
  @type disks: list
643
  @param disks: the list of (size, mode) pairs
644
  @type bep: dict
645
  @param bep: the backend parameters for the instance
646
  @type hvp: dict
647
  @param hvp: the hypervisor parameters for the instance
648
  @type hypervisor_name: string
649
  @param hypervisor_name: the hypervisor for the instance
650
  @rtype: dict
651
  @return: the hook environment for this instance
652

653
  """
654
  if status:
655
    str_status = "up"
656
  else:
657
    str_status = "down"
658
  env = {
659
    "OP_TARGET": name,
660
    "INSTANCE_NAME": name,
661
    "INSTANCE_PRIMARY": primary_node,
662
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
663
    "INSTANCE_OS_TYPE": os_type,
664
    "INSTANCE_STATUS": str_status,
665
    "INSTANCE_MEMORY": memory,
666
    "INSTANCE_VCPUS": vcpus,
667
    "INSTANCE_DISK_TEMPLATE": disk_template,
668
    "INSTANCE_HYPERVISOR": hypervisor_name,
669
  }
670

    
671
  if nics:
672
    nic_count = len(nics)
673
    for idx, (ip, mac, mode, link) in enumerate(nics):
674
      if ip is None:
675
        ip = ""
676
      env["INSTANCE_NIC%d_IP" % idx] = ip
677
      env["INSTANCE_NIC%d_MAC" % idx] = mac
678
      env["INSTANCE_NIC%d_MODE" % idx] = mode
679
      env["INSTANCE_NIC%d_LINK" % idx] = link
680
      if mode == constants.NIC_MODE_BRIDGED:
681
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
682
  else:
683
    nic_count = 0
684

    
685
  env["INSTANCE_NIC_COUNT"] = nic_count
686

    
687
  if disks:
688
    disk_count = len(disks)
689
    for idx, (size, mode) in enumerate(disks):
690
      env["INSTANCE_DISK%d_SIZE" % idx] = size
691
      env["INSTANCE_DISK%d_MODE" % idx] = mode
692
  else:
693
    disk_count = 0
694

    
695
  env["INSTANCE_DISK_COUNT"] = disk_count
696

    
697
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
698
    for key, value in source.items():
699
      env["INSTANCE_%s_%s" % (kind, key)] = value
700

    
701
  return env
702

    
703

    
704
def _NICListToTuple(lu, nics):
705
  """Build a list of nic information tuples.
706

707
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
708
  value in LUQueryInstanceData.
709

710
  @type lu:  L{LogicalUnit}
711
  @param lu: the logical unit on whose behalf we execute
712
  @type nics: list of L{objects.NIC}
713
  @param nics: list of nics to convert to hooks tuples
714

715
  """
716
  hooks_nics = []
717
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
718
  for nic in nics:
719
    ip = nic.ip
720
    mac = nic.mac
721
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
722
    mode = filled_params[constants.NIC_MODE]
723
    link = filled_params[constants.NIC_LINK]
724
    hooks_nics.append((ip, mac, mode, link))
725
  return hooks_nics
726

    
727

    
728
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
729
  """Builds instance related env variables for hooks from an object.
730

731
  @type lu: L{LogicalUnit}
732
  @param lu: the logical unit on whose behalf we execute
733
  @type instance: L{objects.Instance}
734
  @param instance: the instance for which we should build the
735
      environment
736
  @type override: dict
737
  @param override: dictionary with key/values that will override
738
      our values
739
  @rtype: dict
740
  @return: the hook environment dictionary
741

742
  """
743
  cluster = lu.cfg.GetClusterInfo()
744
  bep = cluster.FillBE(instance)
745
  hvp = cluster.FillHV(instance)
746
  args = {
747
    'name': instance.name,
748
    'primary_node': instance.primary_node,
749
    'secondary_nodes': instance.secondary_nodes,
750
    'os_type': instance.os,
751
    'status': instance.admin_up,
752
    'memory': bep[constants.BE_MEMORY],
753
    'vcpus': bep[constants.BE_VCPUS],
754
    'nics': _NICListToTuple(lu, instance.nics),
755
    'disk_template': instance.disk_template,
756
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
757
    'bep': bep,
758
    'hvp': hvp,
759
    'hypervisor_name': instance.hypervisor,
760
  }
761
  if override:
762
    args.update(override)
763
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
764

    
765

    
766
def _AdjustCandidatePool(lu, exceptions):
767
  """Adjust the candidate pool after node operations.
768

769
  """
770
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
771
  if mod_list:
772
    lu.LogInfo("Promoted nodes to master candidate role: %s",
773
               utils.CommaJoin(node.name for node in mod_list))
774
    for name in mod_list:
775
      lu.context.ReaddNode(name)
776
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
777
  if mc_now > mc_max:
778
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
779
               (mc_now, mc_max))
780

    
781

    
782
def _DecideSelfPromotion(lu, exceptions=None):
783
  """Decide whether I should promote myself as a master candidate.
784

785
  """
786
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
787
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
788
  # the new node will increase mc_max with one, so:
789
  mc_should = min(mc_should + 1, cp_size)
790
  return mc_now < mc_should
791

    
792

    
793
def _CheckNicsBridgesExist(lu, target_nics, target_node,
794
                               profile=constants.PP_DEFAULT):
795
  """Check that the brigdes needed by a list of nics exist.
796

797
  """
798
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
799
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
800
                for nic in target_nics]
801
  brlist = [params[constants.NIC_LINK] for params in paramslist
802
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
803
  if brlist:
804
    result = lu.rpc.call_bridges_exist(target_node, brlist)
805
    result.Raise("Error checking bridges on destination node '%s'" %
806
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
807

    
808

    
809
def _CheckInstanceBridgesExist(lu, instance, node=None):
810
  """Check that the brigdes needed by an instance exist.
811

812
  """
813
  if node is None:
814
    node = instance.primary_node
815
  _CheckNicsBridgesExist(lu, instance.nics, node)
816

    
817

    
818
def _CheckOSVariant(os_obj, name):
819
  """Check whether an OS name conforms to the os variants specification.
820

821
  @type os_obj: L{objects.OS}
822
  @param os_obj: OS object to check
823
  @type name: string
824
  @param name: OS name passed by the user, to check for validity
825

826
  """
827
  if not os_obj.supported_variants:
828
    return
829
  try:
830
    variant = name.split("+", 1)[1]
831
  except IndexError:
832
    raise errors.OpPrereqError("OS name must include a variant",
833
                               errors.ECODE_INVAL)
834

    
835
  if variant not in os_obj.supported_variants:
836
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
837

    
838

    
839
def _GetNodeInstancesInner(cfg, fn):
840
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
841

    
842

    
843
def _GetNodeInstances(cfg, node_name):
844
  """Returns a list of all primary and secondary instances on a node.
845

846
  """
847

    
848
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
849

    
850

    
851
def _GetNodePrimaryInstances(cfg, node_name):
852
  """Returns primary instances on a node.
853

854
  """
855
  return _GetNodeInstancesInner(cfg,
856
                                lambda inst: node_name == inst.primary_node)
857

    
858

    
859
def _GetNodeSecondaryInstances(cfg, node_name):
860
  """Returns secondary instances on a node.
861

862
  """
863
  return _GetNodeInstancesInner(cfg,
864
                                lambda inst: node_name in inst.secondary_nodes)
865

    
866

    
867
def _GetStorageTypeArgs(cfg, storage_type):
868
  """Returns the arguments for a storage type.
869

870
  """
871
  # Special case for file storage
872
  if storage_type == constants.ST_FILE:
873
    # storage.FileStorage wants a list of storage directories
874
    return [[cfg.GetFileStorageDir()]]
875

    
876
  return []
877

    
878

    
879
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
880
  faulty = []
881

    
882
  for dev in instance.disks:
883
    cfg.SetDiskID(dev, node_name)
884

    
885
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
886
  result.Raise("Failed to get disk status from node %s" % node_name,
887
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
888

    
889
  for idx, bdev_status in enumerate(result.payload):
890
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
891
      faulty.append(idx)
892

    
893
  return faulty
894

    
895

    
896
def _FormatTimestamp(secs):
897
  """Formats a Unix timestamp with the local timezone.
898

899
  """
900
  return time.strftime("%F %T %Z", time.gmtime(secs))
901

    
902

    
903
class LUPostInitCluster(LogicalUnit):
904
  """Logical unit for running hooks after cluster initialization.
905

906
  """
907
  HPATH = "cluster-init"
908
  HTYPE = constants.HTYPE_CLUSTER
909
  _OP_REQP = []
910

    
911
  def BuildHooksEnv(self):
912
    """Build hooks env.
913

914
    """
915
    env = {"OP_TARGET": self.cfg.GetClusterName()}
916
    mn = self.cfg.GetMasterNode()
917
    return env, [], [mn]
918

    
919
  def CheckPrereq(self):
920
    """No prerequisites to check.
921

922
    """
923
    return True
924

    
925
  def Exec(self, feedback_fn):
926
    """Nothing to do.
927

928
    """
929
    return True
930

    
931

    
932
class LUDestroyCluster(LogicalUnit):
933
  """Logical unit for destroying the cluster.
934

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

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

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

    
947
  def CheckPrereq(self):
948
    """Check prerequisites.
949

950
    This checks whether the cluster is empty.
951

952
    Any errors are signaled by raising errors.OpPrereqError.
953

954
    """
955
    master = self.cfg.GetMasterNode()
956

    
957
    nodelist = self.cfg.GetNodeList()
958
    if len(nodelist) != 1 or nodelist[0] != master:
959
      raise errors.OpPrereqError("There are still %d node(s) in"
960
                                 " this cluster." % (len(nodelist) - 1),
961
                                 errors.ECODE_INVAL)
962
    instancelist = self.cfg.GetInstanceList()
963
    if instancelist:
964
      raise errors.OpPrereqError("There are still %d instance(s) in"
965
                                 " this cluster." % len(instancelist),
966
                                 errors.ECODE_INVAL)
967

    
968
  def Exec(self, feedback_fn):
969
    """Destroys the cluster.
970

971
    """
972
    master = self.cfg.GetMasterNode()
973
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
974

    
975
    # Run post hooks on master node before it's removed
976
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
977
    try:
978
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
979
    except:
980
      # pylint: disable-msg=W0702
981
      self.LogWarning("Errors occurred running hooks on %s" % master)
982

    
983
    result = self.rpc.call_node_stop_master(master, False)
984
    result.Raise("Could not disable the master role")
985

    
986
    if modify_ssh_setup:
987
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
988
      utils.CreateBackup(priv_key)
989
      utils.CreateBackup(pub_key)
990

    
991
    return master
992

    
993

    
994
def _VerifyCertificateInner(filename, expired, not_before, not_after, now,
995
                            warn_days=constants.SSL_CERT_EXPIRATION_WARN,
996
                            error_days=constants.SSL_CERT_EXPIRATION_ERROR):
997
  """Verifies certificate details for LUVerifyCluster.
998

999
  """
1000
  if expired:
1001
    msg = "Certificate %s is expired" % filename
1002

    
1003
    if not_before is not None and not_after is not None:
1004
      msg += (" (valid from %s to %s)" %
1005
              (_FormatTimestamp(not_before),
1006
               _FormatTimestamp(not_after)))
1007
    elif not_before is not None:
1008
      msg += " (valid from %s)" % _FormatTimestamp(not_before)
1009
    elif not_after is not None:
1010
      msg += " (valid until %s)" % _FormatTimestamp(not_after)
1011

    
1012
    return (LUVerifyCluster.ETYPE_ERROR, msg)
1013

    
1014
  elif not_before is not None and not_before > now:
1015
    return (LUVerifyCluster.ETYPE_WARNING,
1016
            "Certificate %s not yet valid (valid from %s)" %
1017
            (filename, _FormatTimestamp(not_before)))
1018

    
1019
  elif not_after is not None:
1020
    remaining_days = int((not_after - now) / (24 * 3600))
1021

    
1022
    msg = ("Certificate %s expires in %d days" % (filename, remaining_days))
1023

    
1024
    if remaining_days <= error_days:
1025
      return (LUVerifyCluster.ETYPE_ERROR, msg)
1026

    
1027
    if remaining_days <= warn_days:
1028
      return (LUVerifyCluster.ETYPE_WARNING, msg)
1029

    
1030
  return (None, None)
1031

    
1032

    
1033
def _VerifyCertificate(filename):
1034
  """Verifies a certificate for LUVerifyCluster.
1035

1036
  @type filename: string
1037
  @param filename: Path to PEM file
1038

1039
  """
1040
  try:
1041
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1042
                                           utils.ReadFile(filename))
1043
  except Exception, err: # pylint: disable-msg=W0703
1044
    return (LUVerifyCluster.ETYPE_ERROR,
1045
            "Failed to load X509 certificate %s: %s" % (filename, err))
1046

    
1047
  # Depending on the pyOpenSSL version, this can just return (None, None)
1048
  (not_before, not_after) = utils.GetX509CertValidity(cert)
1049

    
1050
  return _VerifyCertificateInner(filename, cert.has_expired(),
1051
                                 not_before, not_after, time.time())
1052

    
1053

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1179
    Test list:
1180

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

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

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

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

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

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

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

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

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

    
1238

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

    
1244
    return True
1245

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1346
  def _VerifyInstance(self, instance, instanceconfig, node_image):
1347
    """Verify an instance.
1348

1349
    This function checks to see if the required block devices are
1350
    available on the instance's node.
1351

1352
    """
1353
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1354
    node_current = instanceconfig.primary_node
1355

    
1356
    node_vol_should = {}
1357
    instanceconfig.MapLVsByNode(node_vol_should)
1358

    
1359
    for node in node_vol_should:
1360
      n_img = node_image[node]
1361
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1362
        # ignore missing volumes on offline or broken nodes
1363
        continue
1364
      for volume in node_vol_should[node]:
1365
        test = volume not in n_img.volumes
1366
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1367
                 "volume %s missing on node %s", volume, node)
1368

    
1369
    if instanceconfig.admin_up:
1370
      pri_img = node_image[node_current]
1371
      test = instance not in pri_img.instances and not pri_img.offline
1372
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1373
               "instance not running on its primary node %s",
1374
               node_current)
1375

    
1376
    for node, n_img in node_image.items():
1377
      if (not node == node_current):
1378
        test = instance in n_img.instances
1379
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1380
                 "instance should not run on node %s", node)
1381

    
1382
  def _VerifyOrphanVolumes(self, node_vol_should, node_image):
1383
    """Verify if there are any unknown volumes in the cluster.
1384

1385
    The .os, .swap and backup volumes are ignored. All other volumes are
1386
    reported as unknown.
1387

1388
    """
1389
    for node, n_img in node_image.items():
1390
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1391
        # skip non-healthy nodes
1392
        continue
1393
      for volume in n_img.volumes:
1394
        test = (node not in node_vol_should or
1395
                volume not in node_vol_should[node])
1396
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1397
                      "volume %s is unknown", volume)
1398

    
1399
  def _VerifyOrphanInstances(self, instancelist, node_image):
1400
    """Verify the list of running instances.
1401

1402
    This checks what instances are running but unknown to the cluster.
1403

1404
    """
1405
    for node, n_img in node_image.items():
1406
      for o_inst in n_img.instances:
1407
        test = o_inst not in instancelist
1408
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1409
                      "instance %s on node %s should not exist", o_inst, node)
1410

    
1411
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1412
    """Verify N+1 Memory Resilience.
1413

1414
    Check that if one single node dies we can still start all the
1415
    instances it was primary for.
1416

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

    
1438
  def _VerifyNodeFiles(self, ninfo, nresult, file_list, local_cksum,
1439
                       master_files):
1440
    """Verifies and computes the node required file checksums.
1441

1442
    @type ninfo: L{objects.Node}
1443
    @param ninfo: the node to check
1444
    @param nresult: the remote results for the node
1445
    @param file_list: required list of files
1446
    @param local_cksum: dictionary of local files and their checksums
1447
    @param master_files: list of files that only masters should have
1448

1449
    """
1450
    node = ninfo.name
1451
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1452

    
1453
    remote_cksum = nresult.get(constants.NV_FILELIST, None)
1454
    test = not isinstance(remote_cksum, dict)
1455
    _ErrorIf(test, self.ENODEFILECHECK, node,
1456
             "node hasn't returned file checksum data")
1457
    if test:
1458
      return
1459

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

    
1482
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_map):
1483
    """Verifies and the node DRBD status.
1484

1485
    @type ninfo: L{objects.Node}
1486
    @param ninfo: the node to check
1487
    @param nresult: the remote results for the node
1488
    @param instanceinfo: the dict of instances
1489
    @param drbd_map: the DRBD map as returned by
1490
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1491

1492
    """
1493
    node = ninfo.name
1494
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1495

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

    
1511
    # and now check them
1512
    used_minors = nresult.get(constants.NV_DRBDLIST, [])
1513
    test = not isinstance(used_minors, (tuple, list))
1514
    _ErrorIf(test, self.ENODEDRBD, node,
1515
             "cannot parse drbd status file: %s", str(used_minors))
1516
    if test:
1517
      # we cannot check drbd status
1518
      return
1519

    
1520
    for minor, (iname, must_exist) in node_drbd.items():
1521
      test = minor not in used_minors and must_exist
1522
      _ErrorIf(test, self.ENODEDRBD, node,
1523
               "drbd minor %d of instance %s is not active", minor, iname)
1524
    for minor in used_minors:
1525
      test = minor not in node_drbd
1526
      _ErrorIf(test, self.ENODEDRBD, node,
1527
               "unallocated drbd minor %d is in use", minor)
1528

    
1529
  def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
1530
    """Verifies and updates the node volume data.
1531

1532
    This function will update a L{NodeImage}'s internal structures
1533
    with data from the remote call.
1534

1535
    @type ninfo: L{objects.Node}
1536
    @param ninfo: the node to check
1537
    @param nresult: the remote results for the node
1538
    @param nimg: the node image object
1539
    @param vg_name: the configured VG name
1540

1541
    """
1542
    node = ninfo.name
1543
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1544

    
1545
    nimg.lvm_fail = True
1546
    lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1547
    if vg_name is None:
1548
      pass
1549
    elif isinstance(lvdata, basestring):
1550
      _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1551
               utils.SafeEncode(lvdata))
1552
    elif not isinstance(lvdata, dict):
1553
      _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1554
    else:
1555
      nimg.volumes = lvdata
1556
      nimg.lvm_fail = False
1557

    
1558
  def _UpdateNodeInstances(self, ninfo, nresult, nimg):
1559
    """Verifies and updates the node instance list.
1560

1561
    If the listing was successful, then updates this node's instance
1562
    list. Otherwise, it marks the RPC call as failed for the instance
1563
    list key.
1564

1565
    @type ninfo: L{objects.Node}
1566
    @param ninfo: the node to check
1567
    @param nresult: the remote results for the node
1568
    @param nimg: the node image object
1569

1570
    """
1571
    idata = nresult.get(constants.NV_INSTANCELIST, None)
1572
    test = not isinstance(idata, list)
1573
    self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed"
1574
                  " (instancelist): %s", utils.SafeEncode(str(idata)))
1575
    if test:
1576
      nimg.hyp_fail = True
1577
    else:
1578
      nimg.instances = idata
1579

    
1580
  def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
1581
    """Verifies and computes a node information map
1582

1583
    @type ninfo: L{objects.Node}
1584
    @param ninfo: the node to check
1585
    @param nresult: the remote results for the node
1586
    @param nimg: the node image object
1587
    @param vg_name: the configured VG name
1588

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

    
1593
    # try to read free memory (from the hypervisor)
1594
    hv_info = nresult.get(constants.NV_HVINFO, None)
1595
    test = not isinstance(hv_info, dict) or "memory_free" not in hv_info
1596
    _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1597
    if not test:
1598
      try:
1599
        nimg.mfree = int(hv_info["memory_free"])
1600
      except (ValueError, TypeError):
1601
        _ErrorIf(True, self.ENODERPC, node,
1602
                 "node returned invalid nodeinfo, check hypervisor")
1603

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

    
1618
  def CheckPrereq(self):
1619
    """Check prerequisites.
1620

1621
    Transform the list of checks we're going to skip into a set and check that
1622
    all its members are valid.
1623

1624
    """
1625
    self.skip_set = frozenset(self.op.skip_checks)
1626
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1627
      raise errors.OpPrereqError("Invalid checks to be skipped specified",
1628
                                 errors.ECODE_INVAL)
1629

    
1630
  def BuildHooksEnv(self):
1631
    """Build hooks env.
1632

1633
    Cluster-Verify hooks just ran in the post phase and their failure makes
1634
    the output be logged in the verify output and the verification to fail.
1635

1636
    """
1637
    all_nodes = self.cfg.GetNodeList()
1638
    env = {
1639
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1640
      }
1641
    for node in self.cfg.GetAllNodesInfo().values():
1642
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1643

    
1644
    return env, [], all_nodes
1645

    
1646
  def Exec(self, feedback_fn):
1647
    """Verify integrity of cluster, performing various test on nodes.
1648

1649
    """
1650
    self.bad = False
1651
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1652
    verbose = self.op.verbose
1653
    self._feedback_fn = feedback_fn
1654
    feedback_fn("* Verifying global settings")
1655
    for msg in self.cfg.VerifyConfig():
1656
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1657

    
1658
    # Check the cluster certificates
1659
    for cert_filename in constants.ALL_CERT_FILES:
1660
      (errcode, msg) = _VerifyCertificate(cert_filename)
1661
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1662

    
1663
    vg_name = self.cfg.GetVGName()
1664
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1665
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1666
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1667
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1668
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1669
                        for iname in instancelist)
1670
    i_non_redundant = [] # Non redundant instances
1671
    i_non_a_balanced = [] # Non auto-balanced instances
1672
    n_offline = 0 # Count of offline nodes
1673
    n_drained = 0 # Count of nodes being drained
1674
    node_vol_should = {}
1675

    
1676
    # FIXME: verify OS list
1677
    # do local checksums
1678
    master_files = [constants.CLUSTER_CONF_FILE]
1679

    
1680
    file_names = ssconf.SimpleStore().GetFileList()
1681
    file_names.extend(constants.ALL_CERT_FILES)
1682
    file_names.extend(master_files)
1683

    
1684
    local_checksums = utils.FingerprintFiles(file_names)
1685

    
1686
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1687
    node_verify_param = {
1688
      constants.NV_FILELIST: file_names,
1689
      constants.NV_NODELIST: [node.name for node in nodeinfo
1690
                              if not node.offline],
1691
      constants.NV_HYPERVISOR: hypervisors,
1692
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1693
                                  node.secondary_ip) for node in nodeinfo
1694
                                 if not node.offline],
1695
      constants.NV_INSTANCELIST: hypervisors,
1696
      constants.NV_VERSION: None,
1697
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1698
      constants.NV_NODESETUP: None,
1699
      constants.NV_TIME: None,
1700
      }
1701

    
1702
    if vg_name is not None:
1703
      node_verify_param[constants.NV_VGLIST] = None
1704
      node_verify_param[constants.NV_LVLIST] = vg_name
1705
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1706
      node_verify_param[constants.NV_DRBDLIST] = None
1707

    
1708
    # Build our expected cluster state
1709
    node_image = dict((node.name, self.NodeImage(offline=node.offline))
1710
                      for node in nodeinfo)
1711

    
1712
    for instance in instancelist:
1713
      inst_config = instanceinfo[instance]
1714

    
1715
      for nname in inst_config.all_nodes:
1716
        if nname not in node_image:
1717
          # ghost node
1718
          gnode = self.NodeImage()
1719
          gnode.ghost = True
1720
          node_image[nname] = gnode
1721

    
1722
      inst_config.MapLVsByNode(node_vol_should)
1723

    
1724
      pnode = inst_config.primary_node
1725
      node_image[pnode].pinst.append(instance)
1726

    
1727
      for snode in inst_config.secondary_nodes:
1728
        nimg = node_image[snode]
1729
        nimg.sinst.append(instance)
1730
        if pnode not in nimg.sbp:
1731
          nimg.sbp[pnode] = []
1732
        nimg.sbp[pnode].append(instance)
1733

    
1734
    # At this point, we have the in-memory data structures complete,
1735
    # except for the runtime information, which we'll gather next
1736

    
1737
    # Due to the way our RPC system works, exact response times cannot be
1738
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
1739
    # time before and after executing the request, we can at least have a time
1740
    # window.
1741
    nvinfo_starttime = time.time()
1742
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1743
                                           self.cfg.GetClusterName())
1744
    nvinfo_endtime = time.time()
1745

    
1746
    cluster = self.cfg.GetClusterInfo()
1747
    master_node = self.cfg.GetMasterNode()
1748
    all_drbd_map = self.cfg.ComputeDRBDMap()
1749

    
1750
    feedback_fn("* Verifying node status")
1751
    for node_i in nodeinfo:
1752
      node = node_i.name
1753
      nimg = node_image[node]
1754

    
1755
      if node_i.offline:
1756
        if verbose:
1757
          feedback_fn("* Skipping offline node %s" % (node,))
1758
        n_offline += 1
1759
        continue
1760

    
1761
      if node == master_node:
1762
        ntype = "master"
1763
      elif node_i.master_candidate:
1764
        ntype = "master candidate"
1765
      elif node_i.drained:
1766
        ntype = "drained"
1767
        n_drained += 1
1768
      else:
1769
        ntype = "regular"
1770
      if verbose:
1771
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1772

    
1773
      msg = all_nvinfo[node].fail_msg
1774
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
1775
      if msg:
1776
        nimg.rpc_fail = True
1777
        continue
1778

    
1779
      nresult = all_nvinfo[node].payload
1780

    
1781
      nimg.call_ok = self._VerifyNode(node_i, nresult)
1782
      self._VerifyNodeNetwork(node_i, nresult)
1783
      self._VerifyNodeLVM(node_i, nresult, vg_name)
1784
      self._VerifyNodeFiles(node_i, nresult, file_names, local_checksums,
1785
                            master_files)
1786
      self._VerifyNodeDrbd(node_i, nresult, instanceinfo, all_drbd_map)
1787
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
1788

    
1789
      self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
1790
      self._UpdateNodeInstances(node_i, nresult, nimg)
1791
      self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
1792

    
1793
    feedback_fn("* Verifying instance status")
1794
    for instance in instancelist:
1795
      if verbose:
1796
        feedback_fn("* Verifying instance %s" % instance)
1797
      inst_config = instanceinfo[instance]
1798
      self._VerifyInstance(instance, inst_config, node_image)
1799
      inst_nodes_offline = []
1800

    
1801
      pnode = inst_config.primary_node
1802
      pnode_img = node_image[pnode]
1803
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
1804
               self.ENODERPC, pnode, "instance %s, connection to"
1805
               " primary node failed", instance)
1806

    
1807
      if pnode_img.offline:
1808
        inst_nodes_offline.append(pnode)
1809

    
1810
      # If the instance is non-redundant we cannot survive losing its primary
1811
      # node, so we are not N+1 compliant. On the other hand we have no disk
1812
      # templates with more than one secondary so that situation is not well
1813
      # supported either.
1814
      # FIXME: does not support file-backed instances
1815
      if not inst_config.secondary_nodes:
1816
        i_non_redundant.append(instance)
1817
      _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
1818
               instance, "instance has multiple secondary nodes: %s",
1819
               utils.CommaJoin(inst_config.secondary_nodes),
1820
               code=self.ETYPE_WARNING)
1821

    
1822
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1823
        i_non_a_balanced.append(instance)
1824

    
1825
      for snode in inst_config.secondary_nodes:
1826
        s_img = node_image[snode]
1827
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
1828
                 "instance %s, connection to secondary node failed", instance)
1829

    
1830
        if s_img.offline:
1831
          inst_nodes_offline.append(snode)
1832

    
1833
      # warn that the instance lives on offline nodes
1834
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
1835
               "instance lives on offline node(s) %s",
1836
               utils.CommaJoin(inst_nodes_offline))
1837
      # ... or ghost nodes
1838
      for node in inst_config.all_nodes:
1839
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
1840
                 "instance lives on ghost node %s", node)
1841

    
1842
    feedback_fn("* Verifying orphan volumes")
1843
    self._VerifyOrphanVolumes(node_vol_should, node_image)
1844

    
1845
    feedback_fn("* Verifying oprhan instances")
1846
    self._VerifyOrphanInstances(instancelist, node_image)
1847

    
1848
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1849
      feedback_fn("* Verifying N+1 Memory redundancy")
1850
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
1851

    
1852
    feedback_fn("* Other Notes")
1853
    if i_non_redundant:
1854
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1855
                  % len(i_non_redundant))
1856

    
1857
    if i_non_a_balanced:
1858
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1859
                  % len(i_non_a_balanced))
1860

    
1861
    if n_offline:
1862
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
1863

    
1864
    if n_drained:
1865
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
1866

    
1867
    return not self.bad
1868

    
1869
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1870
    """Analyze the post-hooks' result
1871

1872
    This method analyses the hook result, handles it, and sends some
1873
    nicely-formatted feedback back to the user.
1874

1875
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1876
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1877
    @param hooks_results: the results of the multi-node hooks rpc call
1878
    @param feedback_fn: function used send feedback back to the caller
1879
    @param lu_result: previous Exec result
1880
    @return: the new Exec result, based on the previous result
1881
        and hook results
1882

1883
    """
1884
    # We only really run POST phase hooks, and are only interested in
1885
    # their results
1886
    if phase == constants.HOOKS_PHASE_POST:
1887
      # Used to change hooks' output to proper indentation
1888
      indent_re = re.compile('^', re.M)
1889
      feedback_fn("* Hooks Results")
1890
      assert hooks_results, "invalid result from hooks"
1891

    
1892
      for node_name in hooks_results:
1893
        res = hooks_results[node_name]
1894
        msg = res.fail_msg
1895
        test = msg and not res.offline
1896
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
1897
                      "Communication failure in hooks execution: %s", msg)
1898
        if res.offline or msg:
1899
          # No need to investigate payload if node is offline or gave an error.
1900
          # override manually lu_result here as _ErrorIf only
1901
          # overrides self.bad
1902
          lu_result = 1
1903
          continue
1904
        for script, hkr, output in res.payload:
1905
          test = hkr == constants.HKR_FAIL
1906
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
1907
                        "Script %s failed, output:", script)
1908
          if test:
1909
            output = indent_re.sub('      ', output)
1910
            feedback_fn("%s" % output)
1911
            lu_result = 0
1912

    
1913
      return lu_result
1914

    
1915

    
1916
class LUVerifyDisks(NoHooksLU):
1917
  """Verifies the cluster disks status.
1918

1919
  """
1920
  _OP_REQP = []
1921
  REQ_BGL = False
1922

    
1923
  def ExpandNames(self):
1924
    self.needed_locks = {
1925
      locking.LEVEL_NODE: locking.ALL_SET,
1926
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1927
    }
1928
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1929

    
1930
  def CheckPrereq(self):
1931
    """Check prerequisites.
1932

1933
    This has no prerequisites.
1934

1935
    """
1936
    pass
1937

    
1938
  def Exec(self, feedback_fn):
1939
    """Verify integrity of cluster disks.
1940

1941
    @rtype: tuple of three items
1942
    @return: a tuple of (dict of node-to-node_error, list of instances
1943
        which need activate-disks, dict of instance: (node, volume) for
1944
        missing volumes
1945

1946
    """
1947
    result = res_nodes, res_instances, res_missing = {}, [], {}
1948

    
1949
    vg_name = self.cfg.GetVGName()
1950
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1951
    instances = [self.cfg.GetInstanceInfo(name)
1952
                 for name in self.cfg.GetInstanceList()]
1953

    
1954
    nv_dict = {}
1955
    for inst in instances:
1956
      inst_lvs = {}
1957
      if (not inst.admin_up or
1958
          inst.disk_template not in constants.DTS_NET_MIRROR):
1959
        continue
1960
      inst.MapLVsByNode(inst_lvs)
1961
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1962
      for node, vol_list in inst_lvs.iteritems():
1963
        for vol in vol_list:
1964
          nv_dict[(node, vol)] = inst
1965

    
1966
    if not nv_dict:
1967
      return result
1968

    
1969
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1970

    
1971
    for node in nodes:
1972
      # node_volume
1973
      node_res = node_lvs[node]
1974
      if node_res.offline:
1975
        continue
1976
      msg = node_res.fail_msg
1977
      if msg:
1978
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1979
        res_nodes[node] = msg
1980
        continue
1981

    
1982
      lvs = node_res.payload
1983
      for lv_name, (_, _, lv_online) in lvs.items():
1984
        inst = nv_dict.pop((node, lv_name), None)
1985
        if (not lv_online and inst is not None
1986
            and inst.name not in res_instances):
1987
          res_instances.append(inst.name)
1988

    
1989
    # any leftover items in nv_dict are missing LVs, let's arrange the
1990
    # data better
1991
    for key, inst in nv_dict.iteritems():
1992
      if inst.name not in res_missing:
1993
        res_missing[inst.name] = []
1994
      res_missing[inst.name].append(key)
1995

    
1996
    return result
1997

    
1998

    
1999
class LURepairDiskSizes(NoHooksLU):
2000
  """Verifies the cluster disks sizes.
2001

2002
  """
2003
  _OP_REQP = ["instances"]
2004
  REQ_BGL = False
2005

    
2006
  def ExpandNames(self):
2007
    if not isinstance(self.op.instances, list):
2008
      raise errors.OpPrereqError("Invalid argument type 'instances'",
2009
                                 errors.ECODE_INVAL)
2010

    
2011
    if self.op.instances:
2012
      self.wanted_names = []
2013
      for name in self.op.instances:
2014
        full_name = _ExpandInstanceName(self.cfg, name)
2015
        self.wanted_names.append(full_name)
2016
      self.needed_locks = {
2017
        locking.LEVEL_NODE: [],
2018
        locking.LEVEL_INSTANCE: self.wanted_names,
2019
        }
2020
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2021
    else:
2022
      self.wanted_names = None
2023
      self.needed_locks = {
2024
        locking.LEVEL_NODE: locking.ALL_SET,
2025
        locking.LEVEL_INSTANCE: locking.ALL_SET,
2026
        }
2027
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
2028

    
2029
  def DeclareLocks(self, level):
2030
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2031
      self._LockInstancesNodes(primary_only=True)
2032

    
2033
  def CheckPrereq(self):
2034
    """Check prerequisites.
2035

2036
    This only checks the optional instance list against the existing names.
2037

2038
    """
2039
    if self.wanted_names is None:
2040
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2041

    
2042
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2043
                             in self.wanted_names]
2044

    
2045
  def _EnsureChildSizes(self, disk):
2046
    """Ensure children of the disk have the needed disk size.
2047

2048
    This is valid mainly for DRBD8 and fixes an issue where the
2049
    children have smaller disk size.
2050

2051
    @param disk: an L{ganeti.objects.Disk} object
2052

2053
    """
2054
    if disk.dev_type == constants.LD_DRBD8:
2055
      assert disk.children, "Empty children for DRBD8?"
2056
      fchild = disk.children[0]
2057
      mismatch = fchild.size < disk.size
2058
      if mismatch:
2059
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2060
                     fchild.size, disk.size)
2061
        fchild.size = disk.size
2062

    
2063
      # and we recurse on this child only, not on the metadev
2064
      return self._EnsureChildSizes(fchild) or mismatch
2065
    else:
2066
      return False
2067

    
2068
  def Exec(self, feedback_fn):
2069
    """Verify the size of cluster disks.
2070

2071
    """
2072
    # TODO: check child disks too
2073
    # TODO: check differences in size between primary/secondary nodes
2074
    per_node_disks = {}
2075
    for instance in self.wanted_instances:
2076
      pnode = instance.primary_node
2077
      if pnode not in per_node_disks:
2078
        per_node_disks[pnode] = []
2079
      for idx, disk in enumerate(instance.disks):
2080
        per_node_disks[pnode].append((instance, idx, disk))
2081

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

    
2118

    
2119
class LURenameCluster(LogicalUnit):
2120
  """Rename the cluster.
2121

2122
  """
2123
  HPATH = "cluster-rename"
2124
  HTYPE = constants.HTYPE_CLUSTER
2125
  _OP_REQP = ["name"]
2126

    
2127
  def BuildHooksEnv(self):
2128
    """Build hooks env.
2129

2130
    """
2131
    env = {
2132
      "OP_TARGET": self.cfg.GetClusterName(),
2133
      "NEW_NAME": self.op.name,
2134
      }
2135
    mn = self.cfg.GetMasterNode()
2136
    all_nodes = self.cfg.GetNodeList()
2137
    return env, [mn], all_nodes
2138

    
2139
  def CheckPrereq(self):
2140
    """Verify that the passed name is a valid one.
2141

2142
    """
2143
    hostname = utils.GetHostInfo(self.op.name)
2144

    
2145
    new_name = hostname.name
2146
    self.ip = new_ip = hostname.ip
2147
    old_name = self.cfg.GetClusterName()
2148
    old_ip = self.cfg.GetMasterIP()
2149
    if new_name == old_name and new_ip == old_ip:
2150
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
2151
                                 " cluster has changed",
2152
                                 errors.ECODE_INVAL)
2153
    if new_ip != old_ip:
2154
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
2155
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
2156
                                   " reachable on the network. Aborting." %
2157
                                   new_ip, errors.ECODE_NOTUNIQUE)
2158

    
2159
    self.op.name = new_name
2160

    
2161
  def Exec(self, feedback_fn):
2162
    """Rename the cluster.
2163

2164
    """
2165
    clustername = self.op.name
2166
    ip = self.ip
2167

    
2168
    # shutdown the master IP
2169
    master = self.cfg.GetMasterNode()
2170
    result = self.rpc.call_node_stop_master(master, False)
2171
    result.Raise("Could not disable the master role")
2172

    
2173
    try:
2174
      cluster = self.cfg.GetClusterInfo()
2175
      cluster.cluster_name = clustername
2176
      cluster.master_ip = ip
2177
      self.cfg.Update(cluster, feedback_fn)
2178

    
2179
      # update the known hosts file
2180
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2181
      node_list = self.cfg.GetNodeList()
2182
      try:
2183
        node_list.remove(master)
2184
      except ValueError:
2185
        pass
2186
      result = self.rpc.call_upload_file(node_list,
2187
                                         constants.SSH_KNOWN_HOSTS_FILE)
2188
      for to_node, to_result in result.iteritems():
2189
        msg = to_result.fail_msg
2190
        if msg:
2191
          msg = ("Copy of file %s to node %s failed: %s" %
2192
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
2193
          self.proc.LogWarning(msg)
2194

    
2195
    finally:
2196
      result = self.rpc.call_node_start_master(master, False, False)
2197
      msg = result.fail_msg
2198
      if msg:
2199
        self.LogWarning("Could not re-enable the master role on"
2200
                        " the master, please restart manually: %s", msg)
2201

    
2202

    
2203
def _RecursiveCheckIfLVMBased(disk):
2204
  """Check if the given disk or its children are lvm-based.
2205

2206
  @type disk: L{objects.Disk}
2207
  @param disk: the disk to check
2208
  @rtype: boolean
2209
  @return: boolean indicating whether a LD_LV dev_type was found or not
2210

2211
  """
2212
  if disk.children:
2213
    for chdisk in disk.children:
2214
      if _RecursiveCheckIfLVMBased(chdisk):
2215
        return True
2216
  return disk.dev_type == constants.LD_LV
2217

    
2218

    
2219
class LUSetClusterParams(LogicalUnit):
2220
  """Change the parameters of the cluster.
2221

2222
  """
2223
  HPATH = "cluster-modify"
2224
  HTYPE = constants.HTYPE_CLUSTER
2225
  _OP_REQP = []
2226
  REQ_BGL = False
2227

    
2228
  def CheckArguments(self):
2229
    """Check parameters
2230

2231
    """
2232
    if not hasattr(self.op, "candidate_pool_size"):
2233
      self.op.candidate_pool_size = None
2234
    if self.op.candidate_pool_size is not None:
2235
      try:
2236
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
2237
      except (ValueError, TypeError), err:
2238
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
2239
                                   str(err), errors.ECODE_INVAL)
2240
      if self.op.candidate_pool_size < 1:
2241
        raise errors.OpPrereqError("At least one master candidate needed",
2242
                                   errors.ECODE_INVAL)
2243
    _CheckBooleanOpField(self.op, "maintain_node_health")
2244

    
2245
  def ExpandNames(self):
2246
    # FIXME: in the future maybe other cluster params won't require checking on
2247
    # all nodes to be modified.
2248
    self.needed_locks = {
2249
      locking.LEVEL_NODE: locking.ALL_SET,
2250
    }
2251
    self.share_locks[locking.LEVEL_NODE] = 1
2252

    
2253
  def BuildHooksEnv(self):
2254
    """Build hooks env.
2255

2256
    """
2257
    env = {
2258
      "OP_TARGET": self.cfg.GetClusterName(),
2259
      "NEW_VG_NAME": self.op.vg_name,
2260
      }
2261
    mn = self.cfg.GetMasterNode()
2262
    return env, [mn], [mn]
2263

    
2264
  def CheckPrereq(self):
2265
    """Check prerequisites.
2266

2267
    This checks whether the given params don't conflict and
2268
    if the given volume group is valid.
2269

2270
    """
2271
    if self.op.vg_name is not None and not self.op.vg_name:
2272
      instances = self.cfg.GetAllInstancesInfo().values()
2273
      for inst in instances:
2274
        for disk in inst.disks:
2275
          if _RecursiveCheckIfLVMBased(disk):
2276
            raise errors.OpPrereqError("Cannot disable lvm storage while"
2277
                                       " lvm-based instances exist",
2278
                                       errors.ECODE_INVAL)
2279

    
2280
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2281

    
2282
    # if vg_name not None, checks given volume group on all nodes
2283
    if self.op.vg_name:
2284
      vglist = self.rpc.call_vg_list(node_list)
2285
      for node in node_list:
2286
        msg = vglist[node].fail_msg
2287
        if msg:
2288
          # ignoring down node
2289
          self.LogWarning("Error while gathering data on node %s"
2290
                          " (ignoring node): %s", node, msg)
2291
          continue
2292
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2293
                                              self.op.vg_name,
2294
                                              constants.MIN_VG_SIZE)
2295
        if vgstatus:
2296
          raise errors.OpPrereqError("Error on node '%s': %s" %
2297
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2298

    
2299
    self.cluster = cluster = self.cfg.GetClusterInfo()
2300
    # validate params changes
2301
    if self.op.beparams:
2302
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2303
      self.new_beparams = objects.FillDict(
2304
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
2305

    
2306
    if self.op.nicparams:
2307
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2308
      self.new_nicparams = objects.FillDict(
2309
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
2310
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2311
      nic_errors = []
2312

    
2313
      # check all instances for consistency
2314
      for instance in self.cfg.GetAllInstancesInfo().values():
2315
        for nic_idx, nic in enumerate(instance.nics):
2316
          params_copy = copy.deepcopy(nic.nicparams)
2317
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2318

    
2319
          # check parameter syntax
2320
          try:
2321
            objects.NIC.CheckParameterSyntax(params_filled)
2322
          except errors.ConfigurationError, err:
2323
            nic_errors.append("Instance %s, nic/%d: %s" %
2324
                              (instance.name, nic_idx, err))
2325

    
2326
          # if we're moving instances to routed, check that they have an ip
2327
          target_mode = params_filled[constants.NIC_MODE]
2328
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2329
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2330
                              (instance.name, nic_idx))
2331
      if nic_errors:
2332
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2333
                                   "\n".join(nic_errors))
2334

    
2335
    # hypervisor list/parameters
2336
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
2337
    if self.op.hvparams:
2338
      if not isinstance(self.op.hvparams, dict):
2339
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input",
2340
                                   errors.ECODE_INVAL)
2341
      for hv_name, hv_dict in self.op.hvparams.items():
2342
        if hv_name not in self.new_hvparams:
2343
          self.new_hvparams[hv_name] = hv_dict
2344
        else:
2345
          self.new_hvparams[hv_name].update(hv_dict)
2346

    
2347
    # os hypervisor parameters
2348
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2349
    if self.op.os_hvp:
2350
      if not isinstance(self.op.os_hvp, dict):
2351
        raise errors.OpPrereqError("Invalid 'os_hvp' parameter on input",
2352
                                   errors.ECODE_INVAL)
2353
      for os_name, hvs in self.op.os_hvp.items():
2354
        if not isinstance(hvs, dict):
2355
          raise errors.OpPrereqError(("Invalid 'os_hvp' parameter on"
2356
                                      " input"), errors.ECODE_INVAL)
2357
        if os_name not in self.new_os_hvp:
2358
          self.new_os_hvp[os_name] = hvs
2359
        else:
2360
          for hv_name, hv_dict in hvs.items():
2361
            if hv_name not in self.new_os_hvp[os_name]:
2362
              self.new_os_hvp[os_name][hv_name] = hv_dict
2363
            else:
2364
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2365

    
2366
    if self.op.enabled_hypervisors is not None:
2367
      self.hv_list = self.op.enabled_hypervisors
2368
      if not self.hv_list:
2369
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
2370
                                   " least one member",
2371
                                   errors.ECODE_INVAL)
2372
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
2373
      if invalid_hvs:
2374
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
2375
                                   " entries: %s" %
2376
                                   utils.CommaJoin(invalid_hvs),
2377
                                   errors.ECODE_INVAL)
2378
    else:
2379
      self.hv_list = cluster.enabled_hypervisors
2380

    
2381
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2382
      # either the enabled list has changed, or the parameters have, validate
2383
      for hv_name, hv_params in self.new_hvparams.items():
2384
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2385
            (self.op.enabled_hypervisors and
2386
             hv_name in self.op.enabled_hypervisors)):
2387
          # either this is a new hypervisor, or its parameters have changed
2388
          hv_class = hypervisor.GetHypervisor(hv_name)
2389
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2390
          hv_class.CheckParameterSyntax(hv_params)
2391
          _CheckHVParams(self, node_list, hv_name, hv_params)
2392

    
2393
    if self.op.os_hvp:
2394
      # no need to check any newly-enabled hypervisors, since the
2395
      # defaults have already been checked in the above code-block
2396
      for os_name, os_hvp in self.new_os_hvp.items():
2397
        for hv_name, hv_params in os_hvp.items():
2398
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2399
          # we need to fill in the new os_hvp on top of the actual hv_p
2400
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2401
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2402
          hv_class = hypervisor.GetHypervisor(hv_name)
2403
          hv_class.CheckParameterSyntax(new_osp)
2404
          _CheckHVParams(self, node_list, hv_name, new_osp)
2405

    
2406

    
2407
  def Exec(self, feedback_fn):
2408
    """Change the parameters of the cluster.
2409

2410
    """
2411
    if self.op.vg_name is not None:
2412
      new_volume = self.op.vg_name
2413
      if not new_volume:
2414
        new_volume = None
2415
      if new_volume != self.cfg.GetVGName():
2416
        self.cfg.SetVGName(new_volume)
2417
      else:
2418
        feedback_fn("Cluster LVM configuration already in desired"
2419
                    " state, not changing")
2420
    if self.op.hvparams:
2421
      self.cluster.hvparams = self.new_hvparams
2422
    if self.op.os_hvp:
2423
      self.cluster.os_hvp = self.new_os_hvp
2424
    if self.op.enabled_hypervisors is not None:
2425
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2426
    if self.op.beparams:
2427
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2428
    if self.op.nicparams:
2429
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2430

    
2431
    if self.op.candidate_pool_size is not None:
2432
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2433
      # we need to update the pool size here, otherwise the save will fail
2434
      _AdjustCandidatePool(self, [])
2435

    
2436
    if self.op.maintain_node_health is not None:
2437
      self.cluster.maintain_node_health = self.op.maintain_node_health
2438

    
2439
    self.cfg.Update(self.cluster, feedback_fn)
2440

    
2441

    
2442
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2443
  """Distribute additional files which are part of the cluster configuration.
2444

2445
  ConfigWriter takes care of distributing the config and ssconf files, but
2446
  there are more files which should be distributed to all nodes. This function
2447
  makes sure those are copied.
2448

2449
  @param lu: calling logical unit
2450
  @param additional_nodes: list of nodes not in the config to distribute to
2451

2452
  """
2453
  # 1. Gather target nodes
2454
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2455
  dist_nodes = lu.cfg.GetOnlineNodeList()
2456
  if additional_nodes is not None:
2457
    dist_nodes.extend(additional_nodes)
2458
  if myself.name in dist_nodes:
2459
    dist_nodes.remove(myself.name)
2460

    
2461
  # 2. Gather files to distribute
2462
  dist_files = set([constants.ETC_HOSTS,
2463
                    constants.SSH_KNOWN_HOSTS_FILE,
2464
                    constants.RAPI_CERT_FILE,
2465
                    constants.RAPI_USERS_FILE,
2466
                    constants.CONFD_HMAC_KEY,
2467
                   ])
2468

    
2469
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2470
  for hv_name in enabled_hypervisors:
2471
    hv_class = hypervisor.GetHypervisor(hv_name)
2472
    dist_files.update(hv_class.GetAncillaryFiles())
2473

    
2474
  # 3. Perform the files upload
2475
  for fname in dist_files:
2476
    if os.path.exists(fname):
2477
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2478
      for to_node, to_result in result.items():
2479
        msg = to_result.fail_msg
2480
        if msg:
2481
          msg = ("Copy of file %s to node %s failed: %s" %
2482
                 (fname, to_node, msg))
2483
          lu.proc.LogWarning(msg)
2484

    
2485

    
2486
class LURedistributeConfig(NoHooksLU):
2487
  """Force the redistribution of cluster configuration.
2488

2489
  This is a very simple LU.
2490

2491
  """
2492
  _OP_REQP = []
2493
  REQ_BGL = False
2494

    
2495
  def ExpandNames(self):
2496
    self.needed_locks = {
2497
      locking.LEVEL_NODE: locking.ALL_SET,
2498
    }
2499
    self.share_locks[locking.LEVEL_NODE] = 1
2500

    
2501
  def CheckPrereq(self):
2502
    """Check prerequisites.
2503

2504
    """
2505

    
2506
  def Exec(self, feedback_fn):
2507
    """Redistribute the configuration.
2508

2509
    """
2510
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2511
    _RedistributeAncillaryFiles(self)
2512

    
2513

    
2514
def _WaitForSync(lu, instance, oneshot=False):
2515
  """Sleep and poll for an instance's disk to sync.
2516

2517
  """
2518
  if not instance.disks:
2519
    return True
2520

    
2521
  if not oneshot:
2522
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2523

    
2524
  node = instance.primary_node
2525

    
2526
  for dev in instance.disks:
2527
    lu.cfg.SetDiskID(dev, node)
2528

    
2529
  # TODO: Convert to utils.Retry
2530

    
2531
  retries = 0
2532
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2533
  while True:
2534
    max_time = 0
2535
    done = True
2536
    cumul_degraded = False
2537
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
2538
    msg = rstats.fail_msg
2539
    if msg:
2540
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2541
      retries += 1
2542
      if retries >= 10:
2543
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2544
                                 " aborting." % node)
2545
      time.sleep(6)
2546
      continue
2547
    rstats = rstats.payload
2548
    retries = 0
2549
    for i, mstat in enumerate(rstats):
2550
      if mstat is None:
2551
        lu.LogWarning("Can't compute data for node %s/%s",
2552
                           node, instance.disks[i].iv_name)
2553
        continue
2554

    
2555
      cumul_degraded = (cumul_degraded or
2556
                        (mstat.is_degraded and mstat.sync_percent is None))
2557
      if mstat.sync_percent is not None:
2558
        done = False
2559
        if mstat.estimated_time is not None:
2560
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
2561
          max_time = mstat.estimated_time
2562
        else:
2563
          rem_time = "no time estimate"
2564
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2565
                        (instance.disks[i].iv_name, mstat.sync_percent,
2566
                         rem_time))
2567

    
2568
    # if we're done but degraded, let's do a few small retries, to
2569
    # make sure we see a stable and not transient situation; therefore
2570
    # we force restart of the loop
2571
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2572
      logging.info("Degraded disks found, %d retries left", degr_retries)
2573
      degr_retries -= 1
2574
      time.sleep(1)
2575
      continue
2576

    
2577
    if done or oneshot:
2578
      break
2579

    
2580
    time.sleep(min(60, max_time))
2581

    
2582
  if done:
2583
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2584
  return not cumul_degraded
2585

    
2586

    
2587
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2588
  """Check that mirrors are not degraded.
2589

2590
  The ldisk parameter, if True, will change the test from the
2591
  is_degraded attribute (which represents overall non-ok status for
2592
  the device(s)) to the ldisk (representing the local storage status).
2593

2594
  """
2595
  lu.cfg.SetDiskID(dev, node)
2596

    
2597
  result = True
2598

    
2599
  if on_primary or dev.AssembleOnSecondary():
2600
    rstats = lu.rpc.call_blockdev_find(node, dev)
2601
    msg = rstats.fail_msg
2602
    if msg:
2603
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2604
      result = False
2605
    elif not rstats.payload:
2606
      lu.LogWarning("Can't find disk on node %s", node)
2607
      result = False
2608
    else:
2609
      if ldisk:
2610
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2611
      else:
2612
        result = result and not rstats.payload.is_degraded
2613

    
2614
  if dev.children:
2615
    for child in dev.children:
2616
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2617

    
2618
  return result
2619

    
2620

    
2621
class LUDiagnoseOS(NoHooksLU):
2622
  """Logical unit for OS diagnose/query.
2623

2624
  """
2625
  _OP_REQP = ["output_fields", "names"]
2626
  REQ_BGL = False
2627
  _FIELDS_STATIC = utils.FieldSet()
2628
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants")
2629
  # Fields that need calculation of global os validity
2630
  _FIELDS_NEEDVALID = frozenset(["valid", "variants"])
2631

    
2632
  def ExpandNames(self):
2633
    if self.op.names:
2634
      raise errors.OpPrereqError("Selective OS query not supported",
2635
                                 errors.ECODE_INVAL)
2636

    
2637
    _CheckOutputFields(static=self._FIELDS_STATIC,
2638
                       dynamic=self._FIELDS_DYNAMIC,
2639
                       selected=self.op.output_fields)
2640

    
2641
    # Lock all nodes, in shared mode
2642
    # Temporary removal of locks, should be reverted later
2643
    # TODO: reintroduce locks when they are lighter-weight
2644
    self.needed_locks = {}
2645
    #self.share_locks[locking.LEVEL_NODE] = 1
2646
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2647

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

2651
    """
2652

    
2653
  @staticmethod
2654
  def _DiagnoseByOS(rlist):
2655
    """Remaps a per-node return list into an a per-os per-node dictionary
2656

2657
    @param rlist: a map with node names as keys and OS objects as values
2658

2659
    @rtype: dict
2660
    @return: a dictionary with osnames as keys and as value another map, with
2661
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2662

2663
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2664
                                     (/srv/..., False, "invalid api")],
2665
                           "node2": [(/srv/..., True, "")]}
2666
          }
2667

2668
    """
2669
    all_os = {}
2670
    # we build here the list of nodes that didn't fail the RPC (at RPC
2671
    # level), so that nodes with a non-responding node daemon don't
2672
    # make all OSes invalid
2673
    good_nodes = [node_name for node_name in rlist
2674
                  if not rlist[node_name].fail_msg]
2675
    for node_name, nr in rlist.items():
2676
      if nr.fail_msg or not nr.payload:
2677
        continue
2678
      for name, path, status, diagnose, variants in nr.payload:
2679
        if name not in all_os:
2680
          # build a list of nodes for this os containing empty lists
2681
          # for each node in node_list
2682
          all_os[name] = {}
2683
          for nname in good_nodes:
2684
            all_os[name][nname] = []
2685
        all_os[name][node_name].append((path, status, diagnose, variants))
2686
    return all_os
2687

    
2688
  def Exec(self, feedback_fn):
2689
    """Compute the list of OSes.
2690

2691
    """
2692
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2693
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2694
    pol = self._DiagnoseByOS(node_data)
2695
    output = []
2696
    calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields)
2697
    calc_variants = "variants" in self.op.output_fields
2698

    
2699
    for os_name, os_data in pol.items():
2700
      row = []
2701
      if calc_valid:
2702
        valid = True
2703
        variants = None
2704
        for osl in os_data.values():
2705
          valid = valid and osl and osl[0][1]
2706
          if not valid:
2707
            variants = None
2708
            break
2709
          if calc_variants:
2710
            node_variants = osl[0][3]
2711
            if variants is None:
2712
              variants = node_variants
2713
            else:
2714
              variants = [v for v in variants if v in node_variants]
2715

    
2716
      for field in self.op.output_fields:
2717
        if field == "name":
2718
          val = os_name
2719
        elif field == "valid":
2720
          val = valid
2721
        elif field == "node_status":
2722
          # this is just a copy of the dict
2723
          val = {}
2724
          for node_name, nos_list in os_data.items():
2725
            val[node_name] = nos_list
2726
        elif field == "variants":
2727
          val =  variants
2728
        else:
2729
          raise errors.ParameterError(field)
2730
        row.append(val)
2731
      output.append(row)
2732

    
2733
    return output
2734

    
2735

    
2736
class LURemoveNode(LogicalUnit):
2737
  """Logical unit for removing a node.
2738

2739
  """
2740
  HPATH = "node-remove"
2741
  HTYPE = constants.HTYPE_NODE
2742
  _OP_REQP = ["node_name"]
2743

    
2744
  def BuildHooksEnv(self):
2745
    """Build hooks env.
2746

2747
    This doesn't run on the target node in the pre phase as a failed
2748
    node would then be impossible to remove.
2749

2750
    """
2751
    env = {
2752
      "OP_TARGET": self.op.node_name,
2753
      "NODE_NAME": self.op.node_name,
2754
      }
2755
    all_nodes = self.cfg.GetNodeList()
2756
    try:
2757
      all_nodes.remove(self.op.node_name)
2758
    except ValueError:
2759
      logging.warning("Node %s which is about to be removed not found"
2760
                      " in the all nodes list", self.op.node_name)
2761
    return env, all_nodes, all_nodes
2762

    
2763
  def CheckPrereq(self):
2764
    """Check prerequisites.
2765

2766
    This checks:
2767
     - the node exists in the configuration
2768
     - it does not have primary or secondary instances
2769
     - it's not the master
2770

2771
    Any errors are signaled by raising errors.OpPrereqError.
2772

2773
    """
2774
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
2775
    node = self.cfg.GetNodeInfo(self.op.node_name)
2776
    assert node is not None
2777

    
2778
    instance_list = self.cfg.GetInstanceList()
2779

    
2780
    masternode = self.cfg.GetMasterNode()
2781
    if node.name == masternode:
2782
      raise errors.OpPrereqError("Node is the master node,"
2783
                                 " you need to failover first.",
2784
                                 errors.ECODE_INVAL)
2785

    
2786
    for instance_name in instance_list:
2787
      instance = self.cfg.GetInstanceInfo(instance_name)
2788
      if node.name in instance.all_nodes:
2789
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2790
                                   " please remove first." % instance_name,
2791
                                   errors.ECODE_INVAL)
2792
    self.op.node_name = node.name
2793
    self.node = node
2794

    
2795
  def Exec(self, feedback_fn):
2796
    """Removes the node from the cluster.
2797

2798
    """
2799
    node = self.node
2800
    logging.info("Stopping the node daemon and removing configs from node %s",
2801
                 node.name)
2802

    
2803
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
2804

    
2805
    # Promote nodes to master candidate as needed
2806
    _AdjustCandidatePool(self, exceptions=[node.name])
2807
    self.context.RemoveNode(node.name)
2808

    
2809
    # Run post hooks on the node before it's removed
2810
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
2811
    try:
2812
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
2813
    except:
2814
      # pylint: disable-msg=W0702
2815
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
2816

    
2817
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
2818
    msg = result.fail_msg
2819
    if msg:
2820
      self.LogWarning("Errors encountered on the remote node while leaving"
2821
                      " the cluster: %s", msg)
2822

    
2823

    
2824
class LUQueryNodes(NoHooksLU):
2825
  """Logical unit for querying nodes.
2826

2827
  """
2828
  # pylint: disable-msg=W0142
2829
  _OP_REQP = ["output_fields", "names", "use_locking"]
2830
  REQ_BGL = False
2831

    
2832
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
2833
                    "master_candidate", "offline", "drained"]
2834

    
2835
  _FIELDS_DYNAMIC = utils.FieldSet(
2836
    "dtotal", "dfree",
2837
    "mtotal", "mnode", "mfree",
2838
    "bootid",
2839
    "ctotal", "cnodes", "csockets",
2840
    )
2841

    
2842
  _FIELDS_STATIC = utils.FieldSet(*[
2843
    "pinst_cnt", "sinst_cnt",
2844
    "pinst_list", "sinst_list",
2845
    "pip", "sip", "tags",
2846
    "master",
2847
    "role"] + _SIMPLE_FIELDS
2848
    )
2849

    
2850
  def ExpandNames(self):
2851
    _CheckOutputFields(static=self._FIELDS_STATIC,
2852
                       dynamic=self._FIELDS_DYNAMIC,
2853
                       selected=self.op.output_fields)
2854

    
2855
    self.needed_locks = {}
2856
    self.share_locks[locking.LEVEL_NODE] = 1
2857

    
2858
    if self.op.names:
2859
      self.wanted = _GetWantedNodes(self, self.op.names)
2860
    else:
2861
      self.wanted = locking.ALL_SET
2862

    
2863
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2864
    self.do_locking = self.do_node_query and self.op.use_locking
2865
    if self.do_locking:
2866
      # if we don't request only static fields, we need to lock the nodes
2867
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2868

    
2869
  def CheckPrereq(self):
2870
    """Check prerequisites.
2871

2872
    """
2873
    # The validation of the node list is done in the _GetWantedNodes,
2874
    # if non empty, and if empty, there's no validation to do
2875
    pass
2876

    
2877
  def Exec(self, feedback_fn):
2878
    """Computes the list of nodes and their attributes.
2879

2880
    """
2881
    all_info = self.cfg.GetAllNodesInfo()
2882
    if self.do_locking:
2883
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2884
    elif self.wanted != locking.ALL_SET:
2885
      nodenames = self.wanted
2886
      missing = set(nodenames).difference(all_info.keys())
2887
      if missing:
2888
        raise errors.OpExecError(
2889
          "Some nodes were removed before retrieving their data: %s" % missing)
2890
    else:
2891
      nodenames = all_info.keys()
2892

    
2893
    nodenames = utils.NiceSort(nodenames)
2894
    nodelist = [all_info[name] for name in nodenames]
2895

    
2896
    # begin data gathering
2897

    
2898
    if self.do_node_query:
2899
      live_data = {}
2900
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2901
                                          self.cfg.GetHypervisorType())
2902
      for name in nodenames:
2903
        nodeinfo = node_data[name]
2904
        if not nodeinfo.fail_msg and nodeinfo.payload:
2905
          nodeinfo = nodeinfo.payload
2906
          fn = utils.TryConvert
2907
          live_data[name] = {
2908
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2909
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2910
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2911
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2912
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2913
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2914
            "bootid": nodeinfo.get('bootid', None),
2915
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2916
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2917
            }
2918
        else:
2919
          live_data[name] = {}
2920
    else:
2921
      live_data = dict.fromkeys(nodenames, {})
2922

    
2923
    node_to_primary = dict([(name, set()) for name in nodenames])
2924
    node_to_secondary = dict([(name, set()) for name in nodenames])
2925

    
2926
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2927
                             "sinst_cnt", "sinst_list"))
2928
    if inst_fields & frozenset(self.op.output_fields):
2929
      inst_data = self.cfg.GetAllInstancesInfo()
2930

    
2931
      for inst in inst_data.values():
2932
        if inst.primary_node in node_to_primary:
2933
          node_to_primary[inst.primary_node].add(inst.name)
2934
        for secnode in inst.secondary_nodes:
2935
          if secnode in node_to_secondary:
2936
            node_to_secondary[secnode].add(inst.name)
2937

    
2938
    master_node = self.cfg.GetMasterNode()
2939

    
2940
    # end data gathering
2941

    
2942
    output = []
2943
    for node in nodelist:
2944
      node_output = []
2945
      for field in self.op.output_fields:
2946
        if field in self._SIMPLE_FIELDS:
2947
          val = getattr(node, field)
2948
        elif field == "pinst_list":
2949
          val = list(node_to_primary[node.name])
2950
        elif field == "sinst_list":
2951
          val = list(node_to_secondary[node.name])
2952
        elif field == "pinst_cnt":
2953
          val = len(node_to_primary[node.name])
2954
        elif field == "sinst_cnt":
2955
          val = len(node_to_secondary[node.name])
2956
        elif field == "pip":
2957
          val = node.primary_ip
2958
        elif field == "sip":
2959
          val = node.secondary_ip
2960
        elif field == "tags":
2961
          val = list(node.GetTags())
2962
        elif field == "master":
2963
          val = node.name == master_node
2964
        elif self._FIELDS_DYNAMIC.Matches(field):
2965
          val = live_data[node.name].get(field, None)
2966
        elif field == "role":
2967
          if node.name == master_node:
2968
            val = "M"
2969
          elif node.master_candidate:
2970
            val = "C"
2971
          elif node.drained:
2972
            val = "D"
2973
          elif node.offline:
2974
            val = "O"
2975
          else:
2976
            val = "R"
2977
        else:
2978
          raise errors.ParameterError(field)
2979
        node_output.append(val)
2980
      output.append(node_output)
2981

    
2982
    return output
2983

    
2984

    
2985
class LUQueryNodeVolumes(NoHooksLU):
2986
  """Logical unit for getting volumes on node(s).
2987

2988
  """
2989
  _OP_REQP = ["nodes", "output_fields"]
2990
  REQ_BGL = False
2991
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2992
  _FIELDS_STATIC = utils.FieldSet("node")
2993

    
2994
  def ExpandNames(self):
2995
    _CheckOutputFields(static=self._FIELDS_STATIC,
2996
                       dynamic=self._FIELDS_DYNAMIC,
2997
                       selected=self.op.output_fields)
2998

    
2999
    self.needed_locks = {}
3000
    self.share_locks[locking.LEVEL_NODE] = 1
3001
    if not self.op.nodes:
3002
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3003
    else:
3004
      self.needed_locks[locking.LEVEL_NODE] = \
3005
        _GetWantedNodes(self, self.op.nodes)
3006

    
3007
  def CheckPrereq(self):
3008
    """Check prerequisites.
3009

3010
    This checks that the fields required are valid output fields.
3011

3012
    """
3013
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3014

    
3015
  def Exec(self, feedback_fn):
3016
    """Computes the list of nodes and their attributes.
3017

3018
    """
3019
    nodenames = self.nodes
3020
    volumes = self.rpc.call_node_volumes(nodenames)
3021

    
3022
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3023
             in self.cfg.GetInstanceList()]
3024

    
3025
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3026

    
3027
    output = []
3028
    for node in nodenames:
3029
      nresult = volumes[node]
3030
      if nresult.offline:
3031
        continue
3032
      msg = nresult.fail_msg
3033
      if msg:
3034
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3035
        continue
3036

    
3037
      node_vols = nresult.payload[:]
3038
      node_vols.sort(key=lambda vol: vol['dev'])
3039

    
3040
      for vol in node_vols:
3041
        node_output = []
3042
        for field in self.op.output_fields:
3043
          if field == "node":
3044
            val = node
3045
          elif field == "phys":
3046
            val = vol['dev']
3047
          elif field == "vg":
3048
            val = vol['vg']
3049
          elif field == "name":
3050
            val = vol['name']
3051
          elif field == "size":
3052
            val = int(float(vol['size']))
3053
          elif field == "instance":
3054
            for inst in ilist:
3055
              if node not in lv_by_node[inst]:
3056
                continue
3057
              if vol['name'] in lv_by_node[inst][node]:
3058
                val = inst.name
3059
                break
3060
            else:
3061
              val = '-'
3062
          else:
3063
            raise errors.ParameterError(field)
3064
          node_output.append(str(val))
3065

    
3066
        output.append(node_output)
3067

    
3068
    return output
3069

    
3070

    
3071
class LUQueryNodeStorage(NoHooksLU):
3072
  """Logical unit for getting information on storage units on node(s).
3073

3074
  """
3075
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
3076
  REQ_BGL = False
3077
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
3078

    
3079
  def ExpandNames(self):
3080
    storage_type = self.op.storage_type
3081

    
3082
    if storage_type not in constants.VALID_STORAGE_TYPES:
3083
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
3084
                                 errors.ECODE_INVAL)
3085

    
3086
    _CheckOutputFields(static=self._FIELDS_STATIC,
3087
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
3088
                       selected=self.op.output_fields)
3089

    
3090
    self.needed_locks = {}
3091
    self.share_locks[locking.LEVEL_NODE] = 1
3092

    
3093
    if self.op.nodes:
3094
      self.needed_locks[locking.LEVEL_NODE] = \
3095
        _GetWantedNodes(self, self.op.nodes)
3096
    else:
3097
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3098

    
3099
  def CheckPrereq(self):
3100
    """Check prerequisites.
3101

3102
    This checks that the fields required are valid output fields.
3103

3104
    """
3105
    self.op.name = getattr(self.op, "name", None)
3106

    
3107
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
3108

    
3109
  def Exec(self, feedback_fn):
3110
    """Computes the list of nodes and their attributes.
3111

3112
    """
3113
    # Always get name to sort by
3114
    if constants.SF_NAME in self.op.output_fields:
3115
      fields = self.op.output_fields[:]
3116
    else:
3117
      fields = [constants.SF_NAME] + self.op.output_fields
3118

    
3119
    # Never ask for node or type as it's only known to the LU
3120
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
3121
      while extra in fields:
3122
        fields.remove(extra)
3123

    
3124
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
3125
    name_idx = field_idx[constants.SF_NAME]
3126

    
3127
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3128
    data = self.rpc.call_storage_list(self.nodes,
3129
                                      self.op.storage_type, st_args,
3130
                                      self.op.name, fields)
3131

    
3132
    result = []
3133

    
3134
    for node in utils.NiceSort(self.nodes):
3135
      nresult = data[node]
3136
      if nresult.offline:
3137
        continue
3138

    
3139
      msg = nresult.fail_msg
3140
      if msg:
3141
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
3142
        continue
3143

    
3144
      rows = dict([(row[name_idx], row) for row in nresult.payload])
3145

    
3146
      for name in utils.NiceSort(rows.keys()):
3147
        row = rows[name]
3148

    
3149
        out = []
3150

    
3151
        for field in self.op.output_fields:
3152
          if field == constants.SF_NODE:
3153
            val = node
3154
          elif field == constants.SF_TYPE:
3155
            val = self.op.storage_type
3156
          elif field in field_idx:
3157
            val = row[field_idx[field]]
3158
          else:
3159
            raise errors.ParameterError(field)
3160

    
3161
          out.append(val)
3162

    
3163
        result.append(out)
3164

    
3165
    return result
3166

    
3167

    
3168
class LUModifyNodeStorage(NoHooksLU):
3169
  """Logical unit for modifying a storage volume on a node.
3170

3171
  """
3172
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
3173
  REQ_BGL = False
3174

    
3175
  def CheckArguments(self):
3176
    self.opnode_name = _ExpandNodeName(self.cfg, self.op.node_name)
3177

    
3178
    storage_type = self.op.storage_type
3179
    if storage_type not in constants.VALID_STORAGE_TYPES:
3180
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
3181
                                 errors.ECODE_INVAL)
3182

    
3183
  def ExpandNames(self):
3184
    self.needed_locks = {
3185
      locking.LEVEL_NODE: self.op.node_name,
3186
      }
3187

    
3188
  def CheckPrereq(self):
3189
    """Check prerequisites.
3190

3191
    """
3192
    storage_type = self.op.storage_type
3193

    
3194
    try:
3195
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3196
    except KeyError:
3197
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3198
                                 " modified" % storage_type,
3199
                                 errors.ECODE_INVAL)
3200

    
3201
    diff = set(self.op.changes.keys()) - modifiable
3202
    if diff:
3203
      raise errors.OpPrereqError("The following fields can not be modified for"
3204
                                 " storage units of type '%s': %r" %
3205
                                 (storage_type, list(diff)),
3206
                                 errors.ECODE_INVAL)
3207

    
3208
  def Exec(self, feedback_fn):
3209
    """Computes the list of nodes and their attributes.
3210

3211
    """
3212
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3213
    result = self.rpc.call_storage_modify(self.op.node_name,
3214
                                          self.op.storage_type, st_args,
3215
                                          self.op.name, self.op.changes)
3216
    result.Raise("Failed to modify storage unit '%s' on %s" %
3217
                 (self.op.name, self.op.node_name))
3218

    
3219

    
3220
class LUAddNode(LogicalUnit):
3221
  """Logical unit for adding node to the cluster.
3222

3223
  """
3224
  HPATH = "node-add"
3225
  HTYPE = constants.HTYPE_NODE
3226
  _OP_REQP = ["node_name"]
3227

    
3228
  def CheckArguments(self):
3229
    # validate/normalize the node name
3230
    self.op.node_name = utils.HostInfo.NormalizeName(self.op.node_name)
3231

    
3232
  def BuildHooksEnv(self):
3233
    """Build hooks env.
3234

3235
    This will run on all nodes before, and on all nodes + the new node after.
3236

3237
    """
3238
    env = {
3239
      "OP_TARGET": self.op.node_name,
3240
      "NODE_NAME": self.op.node_name,
3241
      "NODE_PIP": self.op.primary_ip,
3242
      "NODE_SIP": self.op.secondary_ip,
3243
      }
3244
    nodes_0 = self.cfg.GetNodeList()
3245
    nodes_1 = nodes_0 + [self.op.node_name, ]
3246
    return env, nodes_0, nodes_1
3247

    
3248
  def CheckPrereq(self):
3249
    """Check prerequisites.
3250

3251
    This checks:
3252
     - the new node is not already in the config
3253
     - it is resolvable
3254
     - its parameters (single/dual homed) matches the cluster
3255

3256
    Any errors are signaled by raising errors.OpPrereqError.
3257

3258
    """
3259
    node_name = self.op.node_name
3260
    cfg = self.cfg
3261

    
3262
    dns_data = utils.GetHostInfo(node_name)
3263

    
3264
    node = dns_data.name
3265
    primary_ip = self.op.primary_ip = dns_data.ip
3266
    secondary_ip = getattr(self.op, "secondary_ip", None)
3267
    if secondary_ip is None:
3268
      secondary_ip = primary_ip
3269
    if not utils.IsValidIP(secondary_ip):
3270
      raise errors.OpPrereqError("Invalid secondary IP given",
3271
                                 errors.ECODE_INVAL)
3272
    self.op.secondary_ip = secondary_ip
3273

    
3274
    node_list = cfg.GetNodeList()
3275
    if not self.op.readd and node in node_list:
3276
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3277
                                 node, errors.ECODE_EXISTS)
3278
    elif self.op.readd and node not in node_list:
3279
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3280
                                 errors.ECODE_NOENT)
3281

    
3282
    for existing_node_name in node_list:
3283
      existing_node = cfg.GetNodeInfo(existing_node_name)
3284

    
3285
      if self.op.readd and node == existing_node_name:
3286
        if (existing_node.primary_ip != primary_ip or
3287
            existing_node.secondary_ip != secondary_ip):
3288
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3289
                                     " address configuration as before",
3290
                                     errors.ECODE_INVAL)
3291
        continue
3292

    
3293
      if (existing_node.primary_ip == primary_ip or
3294
          existing_node.secondary_ip == primary_ip or
3295
          existing_node.primary_ip == secondary_ip or
3296
          existing_node.secondary_ip == secondary_ip):
3297
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3298
                                   " existing node %s" % existing_node.name,
3299
                                   errors.ECODE_NOTUNIQUE)
3300

    
3301
    # check that the type of the node (single versus dual homed) is the
3302
    # same as for the master
3303
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
3304
    master_singlehomed = myself.secondary_ip == myself.primary_ip
3305
    newbie_singlehomed = secondary_ip == primary_ip
3306
    if master_singlehomed != newbie_singlehomed:
3307
      if master_singlehomed:
3308
        raise errors.OpPrereqError("The master has no private ip but the"
3309
                                   " new node has one",
3310
                                   errors.ECODE_INVAL)
3311
      else:
3312
        raise errors.OpPrereqError("The master has a private ip but the"
3313
                                   " new node doesn't have one",
3314
                                   errors.ECODE_INVAL)
3315

    
3316
    # checks reachability
3317
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3318
      raise errors.OpPrereqError("Node not reachable by ping",
3319
                                 errors.ECODE_ENVIRON)
3320

    
3321
    if not newbie_singlehomed:
3322
      # check reachability from my secondary ip to newbie's secondary ip
3323
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3324
                           source=myself.secondary_ip):
3325
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3326
                                   " based ping to noded port",
3327
                                   errors.ECODE_ENVIRON)
3328

    
3329
    if self.op.readd:
3330
      exceptions = [node]
3331
    else:
3332
      exceptions = []
3333

    
3334
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3335

    
3336
    if self.op.readd:
3337
      self.new_node = self.cfg.GetNodeInfo(node)
3338
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3339
    else:
3340
      self.new_node = objects.Node(name=node,
3341
                                   primary_ip=primary_ip,
3342
                                   secondary_ip=secondary_ip,
3343
                                   master_candidate=self.master_candidate,
3344
                                   offline=False, drained=False)
3345

    
3346
  def Exec(self, feedback_fn):
3347
    """Adds the new node to the cluster.
3348

3349
    """
3350
    new_node = self.new_node
3351
    node = new_node.name
3352

    
3353
    # for re-adds, reset the offline/drained/master-candidate flags;
3354
    # we need to reset here, otherwise offline would prevent RPC calls
3355
    # later in the procedure; this also means that if the re-add
3356
    # fails, we are left with a non-offlined, broken node
3357
    if self.op.readd:
3358
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
3359
      self.LogInfo("Readding a node, the offline/drained flags were reset")
3360
      # if we demote the node, we do cleanup later in the procedure
3361
      new_node.master_candidate = self.master_candidate
3362

    
3363
    # notify the user about any possible mc promotion
3364
    if new_node.master_candidate:
3365
      self.LogInfo("Node will be a master candidate")
3366

    
3367
    # check connectivity
3368
    result = self.rpc.call_version([node])[node]
3369
    result.Raise("Can't get version information from node %s" % node)
3370
    if constants.PROTOCOL_VERSION == result.payload:
3371
      logging.info("Communication to node %s fine, sw version %s match",
3372
                   node, result.payload)
3373
    else:
3374
      raise errors.OpExecError("Version mismatch master version %s,"
3375
                               " node version %s" %
3376
                               (constants.PROTOCOL_VERSION, result.payload))
3377

    
3378
    # setup ssh on node
3379
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3380
      logging.info("Copy ssh key to node %s", node)
3381
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3382
      keyarray = []
3383
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3384
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3385
                  priv_key, pub_key]
3386

    
3387
      for i in keyfiles:
3388
        keyarray.append(utils.ReadFile(i))
3389

    
3390
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3391
                                      keyarray[2], keyarray[3], keyarray[4],
3392
                                      keyarray[5])
3393
      result.Raise("Cannot transfer ssh keys to the new node")
3394

    
3395
    # Add node to our /etc/hosts, and add key to known_hosts
3396
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3397
      utils.AddHostToEtcHosts(new_node.name)
3398

    
3399
    if new_node.secondary_ip != new_node.primary_ip:
3400
      result = self.rpc.call_node_has_ip_address(new_node.name,
3401
                                                 new_node.secondary_ip)
3402
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3403
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3404
      if not result.payload:
3405
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3406
                                 " you gave (%s). Please fix and re-run this"
3407
                                 " command." % new_node.secondary_ip)
3408

    
3409
    node_verify_list = [self.cfg.GetMasterNode()]
3410
    node_verify_param = {
3411
      constants.NV_NODELIST: [node],
3412
      # TODO: do a node-net-test as well?
3413
    }
3414

    
3415
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3416
                                       self.cfg.GetClusterName())
3417
    for verifier in node_verify_list:
3418
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3419
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3420
      if nl_payload:
3421
        for failed in nl_payload:
3422
          feedback_fn("ssh/hostname verification failed"
3423
                      " (checking from %s): %s" %
3424
                      (verifier, nl_payload[failed]))
3425
        raise errors.OpExecError("ssh/hostname verification failed.")
3426

    
3427
    if self.op.readd:
3428
      _RedistributeAncillaryFiles(self)
3429
      self.context.ReaddNode(new_node)
3430
      # make sure we redistribute the config
3431
      self.cfg.Update(new_node, feedback_fn)
3432
      # and make sure the new node will not have old files around
3433
      if not new_node.master_candidate:
3434
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3435
        msg = result.fail_msg
3436
        if msg:
3437
          self.LogWarning("Node failed to demote itself from master"
3438
                          " candidate status: %s" % msg)
3439
    else:
3440
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3441
      self.context.AddNode(new_node, self.proc.GetECId())
3442

    
3443

    
3444
class LUSetNodeParams(LogicalUnit):
3445
  """Modifies the parameters of a node.
3446

3447
  """
3448
  HPATH = "node-modify"
3449
  HTYPE = constants.HTYPE_NODE
3450
  _OP_REQP = ["node_name"]
3451
  REQ_BGL = False
3452

    
3453
  def CheckArguments(self):
3454
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3455
    _CheckBooleanOpField(self.op, 'master_candidate')
3456
    _CheckBooleanOpField(self.op, 'offline')
3457
    _CheckBooleanOpField(self.op, 'drained')
3458
    _CheckBooleanOpField(self.op, 'auto_promote')
3459
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3460
    if all_mods.count(None) == 3:
3461
      raise errors.OpPrereqError("Please pass at least one modification",
3462
                                 errors.ECODE_INVAL)
3463
    if all_mods.count(True) > 1:
3464
      raise errors.OpPrereqError("Can't set the node into more than one"
3465
                                 " state at the same time",
3466
                                 errors.ECODE_INVAL)
3467

    
3468
    # Boolean value that tells us whether we're offlining or draining the node
3469
    self.offline_or_drain = (self.op.offline == True or
3470
                             self.op.drained == True)
3471
    self.deoffline_or_drain = (self.op.offline == False or
3472
                               self.op.drained == False)
3473
    self.might_demote = (self.op.master_candidate == False or
3474
                         self.offline_or_drain)
3475

    
3476
    self.lock_all = self.op.auto_promote and self.might_demote
3477

    
3478

    
3479
  def ExpandNames(self):
3480
    if self.lock_all:
3481
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3482
    else:
3483
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3484

    
3485
  def BuildHooksEnv(self):
3486
    """Build hooks env.
3487

3488
    This runs on the master node.
3489

3490
    """
3491
    env = {
3492
      "OP_TARGET": self.op.node_name,
3493
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3494
      "OFFLINE": str(self.op.offline),
3495
      "DRAINED": str(self.op.drained),
3496
      }
3497
    nl = [self.cfg.GetMasterNode(),
3498
          self.op.node_name]
3499
    return env, nl, nl
3500

    
3501
  def CheckPrereq(self):
3502
    """Check prerequisites.
3503

3504
    This only checks the instance list against the existing names.
3505

3506
    """
3507
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3508

    
3509
    if (self.op.master_candidate is not None or
3510
        self.op.drained is not None or
3511
        self.op.offline is not None):
3512
      # we can't change the master's node flags
3513
      if self.op.node_name == self.cfg.GetMasterNode():
3514
        raise errors.OpPrereqError("The master role can be changed"
3515
                                   " only via masterfailover",
3516
                                   errors.ECODE_INVAL)
3517

    
3518

    
3519
    if node.master_candidate and self.might_demote and not self.lock_all:
3520
      assert not self.op.auto_promote, "auto-promote set but lock_all not"
3521
      # check if after removing the current node, we're missing master
3522
      # candidates
3523
      (mc_remaining, mc_should, _) = \
3524
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
3525
      if mc_remaining < mc_should:
3526
        raise errors.OpPrereqError("Not enough master candidates, please"
3527
                                   " pass auto_promote to allow promotion",
3528
                                   errors.ECODE_INVAL)
3529

    
3530
    if (self.op.master_candidate == True and
3531
        ((node.offline and not self.op.offline == False) or
3532
         (node.drained and not self.op.drained == False))):
3533
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3534
                                 " to master_candidate" % node.name,
3535
                                 errors.ECODE_INVAL)
3536

    
3537
    # If we're being deofflined/drained, we'll MC ourself if needed
3538
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3539
        self.op.master_candidate == True and not node.master_candidate):
3540
      self.op.master_candidate = _DecideSelfPromotion(self)
3541
      if self.op.master_candidate:
3542
        self.LogInfo("Autopromoting node to master candidate")
3543

    
3544
    return
3545

    
3546
  def Exec(self, feedback_fn):
3547
    """Modifies a node.
3548

3549
    """
3550
    node = self.node
3551

    
3552
    result = []
3553
    changed_mc = False
3554

    
3555
    if self.op.offline is not None:
3556
      node.offline = self.op.offline
3557
      result.append(("offline", str(self.op.offline)))
3558
      if self.op.offline == True:
3559
        if node.master_candidate:
3560
          node.master_candidate = False
3561
          changed_mc = True
3562
          result.append(("master_candidate", "auto-demotion due to offline"))
3563
        if node.drained:
3564
          node.drained = False
3565
          result.append(("drained", "clear drained status due to offline"))
3566

    
3567
    if self.op.master_candidate is not None:
3568
      node.master_candidate = self.op.master_candidate
3569
      changed_mc = True
3570
      result.append(("master_candidate", str(self.op.master_candidate)))
3571
      if self.op.master_candidate == False:
3572
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3573
        msg = rrc.fail_msg
3574
        if msg:
3575
          self.LogWarning("Node failed to demote itself: %s" % msg)
3576

    
3577
    if self.op.drained is not None:
3578
      node.drained = self.op.drained
3579
      result.append(("drained", str(self.op.drained)))
3580
      if self.op.drained == True:
3581
        if node.master_candidate:
3582
          node.master_candidate = False
3583
          changed_mc = True
3584
          result.append(("master_candidate", "auto-demotion due to drain"))
3585
          rrc = self.rpc.call_node_demote_from_mc(node.name)
3586
          msg = rrc.fail_msg
3587
          if msg:
3588
            self.LogWarning("Node failed to demote itself: %s" % msg)
3589
        if node.offline:
3590
          node.offline = False
3591
          result.append(("offline", "clear offline status due to drain"))
3592

    
3593
    # we locked all nodes, we adjust the CP before updating this node
3594
    if self.lock_all:
3595
      _AdjustCandidatePool(self, [node.name])
3596

    
3597
    # this will trigger configuration file update, if needed
3598
    self.cfg.Update(node, feedback_fn)
3599

    
3600
    # this will trigger job queue propagation or cleanup
3601
    if changed_mc:
3602
      self.context.ReaddNode(node)
3603

    
3604
    return result
3605

    
3606

    
3607
class LUPowercycleNode(NoHooksLU):
3608
  """Powercycles a node.
3609

3610
  """
3611
  _OP_REQP = ["node_name", "force"]
3612
  REQ_BGL = False
3613

    
3614
  def CheckArguments(self):
3615
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3616
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
3617
      raise errors.OpPrereqError("The node is the master and the force"
3618
                                 " parameter was not set",
3619
                                 errors.ECODE_INVAL)
3620

    
3621
  def ExpandNames(self):
3622
    """Locking for PowercycleNode.
3623

3624
    This is a last-resort option and shouldn't block on other
3625
    jobs. Therefore, we grab no locks.
3626

3627
    """
3628
    self.needed_locks = {}
3629

    
3630
  def CheckPrereq(self):
3631
    """Check prerequisites.
3632

3633
    This LU has no prereqs.
3634

3635
    """
3636
    pass
3637

    
3638
  def Exec(self, feedback_fn):
3639
    """Reboots a node.
3640

3641
    """
3642
    result = self.rpc.call_node_powercycle(self.op.node_name,
3643
                                           self.cfg.GetHypervisorType())
3644
    result.Raise("Failed to schedule the reboot")
3645
    return result.payload
3646

    
3647

    
3648
class LUQueryClusterInfo(NoHooksLU):
3649
  """Query cluster configuration.
3650

3651
  """
3652
  _OP_REQP = []
3653
  REQ_BGL = False
3654

    
3655
  def ExpandNames(self):
3656
    self.needed_locks = {}
3657

    
3658
  def CheckPrereq(self):
3659
    """No prerequsites needed for this LU.
3660

3661
    """
3662
    pass
3663

    
3664
  def Exec(self, feedback_fn):
3665
    """Return cluster config.
3666

3667
    """
3668
    cluster = self.cfg.GetClusterInfo()
3669
    os_hvp = {}
3670

    
3671
    # Filter just for enabled hypervisors
3672
    for os_name, hv_dict in cluster.os_hvp.items():
3673
      os_hvp[os_name] = {}
3674
      for hv_name, hv_params in hv_dict.items():
3675
        if hv_name in cluster.enabled_hypervisors:
3676
          os_hvp[os_name][hv_name] = hv_params
3677

    
3678
    result = {
3679
      "software_version": constants.RELEASE_VERSION,
3680
      "protocol_version": constants.PROTOCOL_VERSION,
3681
      "config_version": constants.CONFIG_VERSION,
3682
      "os_api_version": max(constants.OS_API_VERSIONS),
3683
      "export_version": constants.EXPORT_VERSION,
3684
      "architecture": (platform.architecture()[0], platform.machine()),
3685
      "name": cluster.cluster_name,
3686
      "master": cluster.master_node,
3687
      "default_hypervisor": cluster.enabled_hypervisors[0],
3688
      "enabled_hypervisors": cluster.enabled_hypervisors,
3689
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3690
                        for hypervisor_name in cluster.enabled_hypervisors]),
3691
      "os_hvp": os_hvp,
3692
      "beparams": cluster.beparams,
3693
      "nicparams": cluster.nicparams,
3694
      "candidate_pool_size": cluster.candidate_pool_size,
3695
      "master_netdev": cluster.master_netdev,
3696
      "volume_group_name": cluster.volume_group_name,
3697
      "file_storage_dir": cluster.file_storage_dir,
3698
      "maintain_node_health": cluster.maintain_node_health,
3699
      "ctime": cluster.ctime,
3700
      "mtime": cluster.mtime,
3701
      "uuid": cluster.uuid,
3702
      "tags": list(cluster.GetTags()),
3703
      }
3704

    
3705
    return result
3706

    
3707

    
3708
class LUQueryConfigValues(NoHooksLU):
3709
  """Return configuration values.
3710

3711
  """
3712
  _OP_REQP = []
3713
  REQ_BGL = False
3714
  _FIELDS_DYNAMIC = utils.FieldSet()
3715
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
3716
                                  "watcher_pause")
3717

    
3718
  def ExpandNames(self):
3719
    self.needed_locks = {}
3720

    
3721
    _CheckOutputFields(static=self._FIELDS_STATIC,
3722
                       dynamic=self._FIELDS_DYNAMIC,
3723
                       selected=self.op.output_fields)
3724

    
3725
  def CheckPrereq(self):
3726
    """No prerequisites.
3727

3728
    """
3729
    pass
3730

    
3731
  def Exec(self, feedback_fn):
3732
    """Dump a representation of the cluster config to the standard output.
3733

3734
    """
3735
    values = []
3736
    for field in self.op.output_fields:
3737
      if field == "cluster_name":
3738
        entry = self.cfg.GetClusterName()
3739
      elif field == "master_node":
3740
        entry = self.cfg.GetMasterNode()
3741
      elif field == "drain_flag":
3742
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3743
      elif field == "watcher_pause":
3744
        entry = utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
3745
      else:
3746
        raise errors.ParameterError(field)
3747
      values.append(entry)
3748
    return values
3749

    
3750

    
3751
class LUActivateInstanceDisks(NoHooksLU):
3752
  """Bring up an instance's disks.
3753

3754
  """
3755
  _OP_REQP = ["instance_name"]
3756
  REQ_BGL = False
3757

    
3758
  def ExpandNames(self):
3759
    self._ExpandAndLockInstance()
3760
    self.needed_locks[locking.LEVEL_NODE] = []
3761
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3762

    
3763
  def DeclareLocks(self, level):
3764
    if level == locking.LEVEL_NODE:
3765
      self._LockInstancesNodes()
3766

    
3767
  def CheckPrereq(self):
3768
    """Check prerequisites.
3769

3770
    This checks that the instance is in the cluster.
3771

3772
    """
3773
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3774
    assert self.instance is not None, \
3775
      "Cannot retrieve locked instance %s" % self.op.instance_name
3776
    _CheckNodeOnline(self, self.instance.primary_node)
3777
    if not hasattr(self.op, "ignore_size"):
3778
      self.op.ignore_size = False
3779

    
3780
  def Exec(self, feedback_fn):
3781
    """Activate the disks.
3782

3783
    """
3784
    disks_ok, disks_info = \
3785
              _AssembleInstanceDisks(self, self.instance,
3786
                                     ignore_size=self.op.ignore_size)
3787
    if not disks_ok:
3788
      raise errors.OpExecError("Cannot activate block devices")
3789

    
3790
    return disks_info
3791

    
3792

    
3793
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3794
                           ignore_size=False):
3795
  """Prepare the block devices for an instance.
3796

3797
  This sets up the block devices on all nodes.
3798

3799
  @type lu: L{LogicalUnit}
3800
  @param lu: the logical unit on whose behalf we execute
3801
  @type instance: L{objects.Instance}
3802
  @param instance: the instance for whose disks we assemble
3803
  @type ignore_secondaries: boolean
3804
  @param ignore_secondaries: if true, errors on secondary nodes
3805
      won't result in an error return from the function
3806
  @type ignore_size: boolean
3807
  @param ignore_size: if true, the current known size of the disk
3808
      will not be used during the disk activation, useful for cases
3809
      when the size is wrong
3810
  @return: False if the operation failed, otherwise a list of
3811
      (host, instance_visible_name, node_visible_name)
3812
      with the mapping from node devices to instance devices
3813

3814
  """
3815
  device_info = []
3816
  disks_ok = True
3817
  iname = instance.name
3818
  # With the two passes mechanism we try to reduce the window of
3819
  # opportunity for the race condition of switching DRBD to primary
3820
  # before handshaking occured, but we do not eliminate it
3821

    
3822
  # The proper fix would be to wait (with some limits) until the
3823
  # connection has been made and drbd transitions from WFConnection
3824
  # into any other network-connected state (Connected, SyncTarget,
3825
  # SyncSource, etc.)
3826

    
3827
  # 1st pass, assemble on all nodes in secondary mode
3828
  for inst_disk in instance.disks:
3829
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3830
      if ignore_size:
3831
        node_disk = node_disk.Copy()
3832
        node_disk.UnsetSize()
3833
      lu.cfg.SetDiskID(node_disk, node)
3834
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3835
      msg = result.fail_msg
3836
      if msg:
3837
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3838
                           " (is_primary=False, pass=1): %s",
3839
                           inst_disk.iv_name, node, msg)
3840
        if not ignore_secondaries:
3841
          disks_ok = False
3842

    
3843
  # FIXME: race condition on drbd migration to primary
3844

    
3845
  # 2nd pass, do only the primary node
3846
  for inst_disk in instance.disks:
3847
    dev_path = None
3848

    
3849
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3850
      if node != instance.primary_node:
3851
        continue
3852
      if ignore_size:
3853
        node_disk = node_disk.Copy()
3854
        node_disk.UnsetSize()
3855
      lu.cfg.SetDiskID(node_disk, node)
3856
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3857
      msg = result.fail_msg
3858
      if msg:
3859
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3860
                           " (is_primary=True, pass=2): %s",
3861
                           inst_disk.iv_name, node, msg)
3862
        disks_ok = False
3863
      else:
3864
        dev_path = result.payload
3865

    
3866
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
3867

    
3868
  # leave the disks configured for the primary node
3869
  # this is a workaround that would be fixed better by
3870
  # improving the logical/physical id handling
3871
  for disk in instance.disks:
3872
    lu.cfg.SetDiskID(disk, instance.primary_node)
3873

    
3874
  return disks_ok, device_info
3875

    
3876

    
3877
def _StartInstanceDisks(lu, instance, force):
3878
  """Start the disks of an instance.
3879

3880
  """
3881
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3882
                                           ignore_secondaries=force)
3883
  if not disks_ok:
3884
    _ShutdownInstanceDisks(lu, instance)
3885
    if force is not None and not force:
3886
      lu.proc.LogWarning("", hint="If the message above refers to a"
3887
                         " secondary node,"
3888
                         " you can retry the operation using '--force'.")
3889
    raise errors.OpExecError("Disk consistency error")
3890

    
3891

    
3892
class LUDeactivateInstanceDisks(NoHooksLU):
3893
  """Shutdown an instance's disks.
3894

3895
  """
3896
  _OP_REQP = ["instance_name"]
3897
  REQ_BGL = False
3898

    
3899
  def ExpandNames(self):
3900
    self._ExpandAndLockInstance()
3901
    self.needed_locks[locking.LEVEL_NODE] = []
3902
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3903

    
3904
  def DeclareLocks(self, level):
3905
    if level == locking.LEVEL_NODE:
3906
      self._LockInstancesNodes()
3907

    
3908
  def CheckPrereq(self):
3909
    """Check prerequisites.
3910

3911
    This checks that the instance is in the cluster.
3912

3913
    """
3914
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3915
    assert self.instance is not None, \
3916
      "Cannot retrieve locked instance %s" % self.op.instance_name
3917

    
3918
  def Exec(self, feedback_fn):
3919
    """Deactivate the disks
3920

3921
    """
3922
    instance = self.instance
3923
    _SafeShutdownInstanceDisks(self, instance)
3924

    
3925

    
3926
def _SafeShutdownInstanceDisks(lu, instance):
3927
  """Shutdown block devices of an instance.
3928

3929
  This function checks if an instance is running, before calling
3930
  _ShutdownInstanceDisks.
3931

3932
  """
3933
  _CheckInstanceDown(lu, instance, "cannot shutdown disks")
3934
  _ShutdownInstanceDisks(lu, instance)
3935

    
3936

    
3937
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3938
  """Shutdown block devices of an instance.
3939

3940
  This does the shutdown on all nodes of the instance.
3941

3942
  If the ignore_primary is false, errors on the primary node are
3943
  ignored.
3944

3945
  """
3946
  all_result = True
3947
  for disk in instance.disks:
3948
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
3949
      lu.cfg.SetDiskID(top_disk, node)
3950
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
3951
      msg = result.fail_msg
3952
      if msg:
3953
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
3954
                      disk.iv_name, node, msg)
3955
        if not ignore_primary or node != instance.primary_node:
3956
          all_result = False
3957
  return all_result
3958

    
3959

    
3960
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
3961
  """Checks if a node has enough free memory.
3962

3963
  This function check if a given node has the needed amount of free
3964
  memory. In case the node has less memory or we cannot get the
3965
  information from the node, this function raise an OpPrereqError
3966
  exception.
3967

3968
  @type lu: C{LogicalUnit}
3969
  @param lu: a logical unit from which we get configuration data
3970
  @type node: C{str}
3971
  @param node: the node to check
3972
  @type reason: C{str}
3973
  @param reason: string to use in the error message
3974
  @type requested: C{int}
3975
  @param requested: the amount of memory in MiB to check for
3976
  @type hypervisor_name: C{str}
3977
  @param hypervisor_name: the hypervisor to ask for memory stats
3978
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
3979
      we cannot check the node
3980

3981
  """
3982
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
3983
  nodeinfo[node].Raise("Can't get data from node %s" % node,
3984
                       prereq=True, ecode=errors.ECODE_ENVIRON)
3985
  free_mem = nodeinfo[node].payload.get('memory_free', None)
3986
  if not isinstance(free_mem, int):
3987
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
3988
                               " was '%s'" % (node, free_mem),
3989
                               errors.ECODE_ENVIRON)
3990
  if requested > free_mem:
3991
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
3992
                               " needed %s MiB, available %s MiB" %
3993
                               (node, reason, requested, free_mem),
3994
                               errors.ECODE_NORES)
3995

    
3996

    
3997
def _CheckNodesFreeDisk(lu, nodenames, requested):
3998
  """Checks if nodes have enough free disk space in the default VG.
3999

4000
  This function check if all given nodes have the needed amount of
4001
  free disk. In case any node has less disk or we cannot get the
4002
  information from the node, this function raise an OpPrereqError
4003
  exception.
4004

4005
  @type lu: C{LogicalUnit}
4006
  @param lu: a logical unit from which we get configuration data
4007
  @type nodenames: C{list}
4008
  @param nodenames: the list of node names to check
4009
  @type requested: C{int}