Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 6b7d5878

History | View | Annotate | Download (326.5 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 _CheckDiskTemplate(template):
546
  """Ensure a given disk template is valid.
547

548
  """
549
  if template not in constants.DISK_TEMPLATES:
550
    msg = ("Invalid disk template name '%s', valid templates are: %s" %
551
           (template, utils.CommaJoin(constants.DISK_TEMPLATES)))
552
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
553

    
554

    
555
def _CheckInstanceDown(lu, instance, reason):
556
  """Ensure that an instance is not running."""
557
  if instance.admin_up:
558
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
559
                               (instance.name, reason), errors.ECODE_STATE)
560

    
561
  pnode = instance.primary_node
562
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
563
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
564
              prereq=True, ecode=errors.ECODE_ENVIRON)
565

    
566
  if instance.name in ins_l.payload:
567
    raise errors.OpPrereqError("Instance %s is running, %s" %
568
                               (instance.name, reason), errors.ECODE_STATE)
569

    
570

    
571
def _ExpandItemName(fn, name, kind):
572
  """Expand an item name.
573

574
  @param fn: the function to use for expansion
575
  @param name: requested item name
576
  @param kind: text description ('Node' or 'Instance')
577
  @return: the resolved (full) name
578
  @raise errors.OpPrereqError: if the item is not found
579

580
  """
581
  full_name = fn(name)
582
  if full_name is None:
583
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
584
                               errors.ECODE_NOENT)
585
  return full_name
586

    
587

    
588
def _ExpandNodeName(cfg, name):
589
  """Wrapper over L{_ExpandItemName} for nodes."""
590
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
591

    
592

    
593
def _ExpandInstanceName(cfg, name):
594
  """Wrapper over L{_ExpandItemName} for instance."""
595
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
596

    
597

    
598
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
599
                          memory, vcpus, nics, disk_template, disks,
600
                          bep, hvp, hypervisor_name):
601
  """Builds instance related env variables for hooks
602

603
  This builds the hook environment from individual variables.
604

605
  @type name: string
606
  @param name: the name of the instance
607
  @type primary_node: string
608
  @param primary_node: the name of the instance's primary node
609
  @type secondary_nodes: list
610
  @param secondary_nodes: list of secondary nodes as strings
611
  @type os_type: string
612
  @param os_type: the name of the instance's OS
613
  @type status: boolean
614
  @param status: the should_run status of the instance
615
  @type memory: string
616
  @param memory: the memory size of the instance
617
  @type vcpus: string
618
  @param vcpus: the count of VCPUs the instance has
619
  @type nics: list
620
  @param nics: list of tuples (ip, mac, mode, link) representing
621
      the NICs the instance has
622
  @type disk_template: string
623
  @param disk_template: the disk template of the instance
624
  @type disks: list
625
  @param disks: the list of (size, mode) pairs
626
  @type bep: dict
627
  @param bep: the backend parameters for the instance
628
  @type hvp: dict
629
  @param hvp: the hypervisor parameters for the instance
630
  @type hypervisor_name: string
631
  @param hypervisor_name: the hypervisor for the instance
632
  @rtype: dict
633
  @return: the hook environment for this instance
634

635
  """
636
  if status:
637
    str_status = "up"
638
  else:
639
    str_status = "down"
640
  env = {
641
    "OP_TARGET": name,
642
    "INSTANCE_NAME": name,
643
    "INSTANCE_PRIMARY": primary_node,
644
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
645
    "INSTANCE_OS_TYPE": os_type,
646
    "INSTANCE_STATUS": str_status,
647
    "INSTANCE_MEMORY": memory,
648
    "INSTANCE_VCPUS": vcpus,
649
    "INSTANCE_DISK_TEMPLATE": disk_template,
650
    "INSTANCE_HYPERVISOR": hypervisor_name,
651
  }
652

    
653
  if nics:
654
    nic_count = len(nics)
655
    for idx, (ip, mac, mode, link) in enumerate(nics):
656
      if ip is None:
657
        ip = ""
658
      env["INSTANCE_NIC%d_IP" % idx] = ip
659
      env["INSTANCE_NIC%d_MAC" % idx] = mac
660
      env["INSTANCE_NIC%d_MODE" % idx] = mode
661
      env["INSTANCE_NIC%d_LINK" % idx] = link
662
      if mode == constants.NIC_MODE_BRIDGED:
663
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
664
  else:
665
    nic_count = 0
666

    
667
  env["INSTANCE_NIC_COUNT"] = nic_count
668

    
669
  if disks:
670
    disk_count = len(disks)
671
    for idx, (size, mode) in enumerate(disks):
672
      env["INSTANCE_DISK%d_SIZE" % idx] = size
673
      env["INSTANCE_DISK%d_MODE" % idx] = mode
674
  else:
675
    disk_count = 0
676

    
677
  env["INSTANCE_DISK_COUNT"] = disk_count
678

    
679
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
680
    for key, value in source.items():
681
      env["INSTANCE_%s_%s" % (kind, key)] = value
682

    
683
  return env
684

    
685

    
686
def _NICListToTuple(lu, nics):
687
  """Build a list of nic information tuples.
688

689
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
690
  value in LUQueryInstanceData.
691

692
  @type lu:  L{LogicalUnit}
693
  @param lu: the logical unit on whose behalf we execute
694
  @type nics: list of L{objects.NIC}
695
  @param nics: list of nics to convert to hooks tuples
696

697
  """
698
  hooks_nics = []
699
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
700
  for nic in nics:
701
    ip = nic.ip
702
    mac = nic.mac
703
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
704
    mode = filled_params[constants.NIC_MODE]
705
    link = filled_params[constants.NIC_LINK]
706
    hooks_nics.append((ip, mac, mode, link))
707
  return hooks_nics
708

    
709

    
710
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
711
  """Builds instance related env variables for hooks from an object.
712

713
  @type lu: L{LogicalUnit}
714
  @param lu: the logical unit on whose behalf we execute
715
  @type instance: L{objects.Instance}
716
  @param instance: the instance for which we should build the
717
      environment
718
  @type override: dict
719
  @param override: dictionary with key/values that will override
720
      our values
721
  @rtype: dict
722
  @return: the hook environment dictionary
723

724
  """
725
  cluster = lu.cfg.GetClusterInfo()
726
  bep = cluster.FillBE(instance)
727
  hvp = cluster.FillHV(instance)
728
  args = {
729
    'name': instance.name,
730
    'primary_node': instance.primary_node,
731
    'secondary_nodes': instance.secondary_nodes,
732
    'os_type': instance.os,
733
    'status': instance.admin_up,
734
    'memory': bep[constants.BE_MEMORY],
735
    'vcpus': bep[constants.BE_VCPUS],
736
    'nics': _NICListToTuple(lu, instance.nics),
737
    'disk_template': instance.disk_template,
738
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
739
    'bep': bep,
740
    'hvp': hvp,
741
    'hypervisor_name': instance.hypervisor,
742
  }
743
  if override:
744
    args.update(override)
745
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
746

    
747

    
748
def _AdjustCandidatePool(lu, exceptions):
749
  """Adjust the candidate pool after node operations.
750

751
  """
752
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
753
  if mod_list:
754
    lu.LogInfo("Promoted nodes to master candidate role: %s",
755
               utils.CommaJoin(node.name for node in mod_list))
756
    for name in mod_list:
757
      lu.context.ReaddNode(name)
758
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
759
  if mc_now > mc_max:
760
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
761
               (mc_now, mc_max))
762

    
763

    
764
def _DecideSelfPromotion(lu, exceptions=None):
765
  """Decide whether I should promote myself as a master candidate.
766

767
  """
768
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
769
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
770
  # the new node will increase mc_max with one, so:
771
  mc_should = min(mc_should + 1, cp_size)
772
  return mc_now < mc_should
773

    
774

    
775
def _CheckNicsBridgesExist(lu, target_nics, target_node,
776
                               profile=constants.PP_DEFAULT):
777
  """Check that the brigdes needed by a list of nics exist.
778

779
  """
780
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
781
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
782
                for nic in target_nics]
783
  brlist = [params[constants.NIC_LINK] for params in paramslist
784
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
785
  if brlist:
786
    result = lu.rpc.call_bridges_exist(target_node, brlist)
787
    result.Raise("Error checking bridges on destination node '%s'" %
788
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
789

    
790

    
791
def _CheckInstanceBridgesExist(lu, instance, node=None):
792
  """Check that the brigdes needed by an instance exist.
793

794
  """
795
  if node is None:
796
    node = instance.primary_node
797
  _CheckNicsBridgesExist(lu, instance.nics, node)
798

    
799

    
800
def _CheckOSVariant(os_obj, name):
801
  """Check whether an OS name conforms to the os variants specification.
802

803
  @type os_obj: L{objects.OS}
804
  @param os_obj: OS object to check
805
  @type name: string
806
  @param name: OS name passed by the user, to check for validity
807

808
  """
809
  if not os_obj.supported_variants:
810
    return
811
  try:
812
    variant = name.split("+", 1)[1]
813
  except IndexError:
814
    raise errors.OpPrereqError("OS name must include a variant",
815
                               errors.ECODE_INVAL)
816

    
817
  if variant not in os_obj.supported_variants:
818
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
819

    
820

    
821
def _GetNodeInstancesInner(cfg, fn):
822
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
823

    
824

    
825
def _GetNodeInstances(cfg, node_name):
826
  """Returns a list of all primary and secondary instances on a node.
827

828
  """
829

    
830
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
831

    
832

    
833
def _GetNodePrimaryInstances(cfg, node_name):
834
  """Returns primary instances on a node.
835

836
  """
837
  return _GetNodeInstancesInner(cfg,
838
                                lambda inst: node_name == inst.primary_node)
839

    
840

    
841
def _GetNodeSecondaryInstances(cfg, node_name):
842
  """Returns secondary instances on a node.
843

844
  """
845
  return _GetNodeInstancesInner(cfg,
846
                                lambda inst: node_name in inst.secondary_nodes)
847

    
848

    
849
def _GetStorageTypeArgs(cfg, storage_type):
850
  """Returns the arguments for a storage type.
851

852
  """
853
  # Special case for file storage
854
  if storage_type == constants.ST_FILE:
855
    # storage.FileStorage wants a list of storage directories
856
    return [[cfg.GetFileStorageDir()]]
857

    
858
  return []
859

    
860

    
861
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
862
  faulty = []
863

    
864
  for dev in instance.disks:
865
    cfg.SetDiskID(dev, node_name)
866

    
867
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
868
  result.Raise("Failed to get disk status from node %s" % node_name,
869
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
870

    
871
  for idx, bdev_status in enumerate(result.payload):
872
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
873
      faulty.append(idx)
874

    
875
  return faulty
876

    
877

    
878
def _FormatTimestamp(secs):
879
  """Formats a Unix timestamp with the local timezone.
880

881
  """
882
  return time.strftime("%F %T %Z", time.gmtime(secs))
883

    
884

    
885
class LUPostInitCluster(LogicalUnit):
886
  """Logical unit for running hooks after cluster initialization.
887

888
  """
889
  HPATH = "cluster-init"
890
  HTYPE = constants.HTYPE_CLUSTER
891
  _OP_REQP = []
892

    
893
  def BuildHooksEnv(self):
894
    """Build hooks env.
895

896
    """
897
    env = {"OP_TARGET": self.cfg.GetClusterName()}
898
    mn = self.cfg.GetMasterNode()
899
    return env, [], [mn]
900

    
901
  def CheckPrereq(self):
902
    """No prerequisites to check.
903

904
    """
905
    return True
906

    
907
  def Exec(self, feedback_fn):
908
    """Nothing to do.
909

910
    """
911
    return True
912

    
913

    
914
class LUDestroyCluster(LogicalUnit):
915
  """Logical unit for destroying the cluster.
916

917
  """
918
  HPATH = "cluster-destroy"
919
  HTYPE = constants.HTYPE_CLUSTER
920
  _OP_REQP = []
921

    
922
  def BuildHooksEnv(self):
923
    """Build hooks env.
924

925
    """
926
    env = {"OP_TARGET": self.cfg.GetClusterName()}
927
    return env, [], []
928

    
929
  def CheckPrereq(self):
930
    """Check prerequisites.
931

932
    This checks whether the cluster is empty.
933

934
    Any errors are signaled by raising errors.OpPrereqError.
935

936
    """
937
    master = self.cfg.GetMasterNode()
938

    
939
    nodelist = self.cfg.GetNodeList()
940
    if len(nodelist) != 1 or nodelist[0] != master:
941
      raise errors.OpPrereqError("There are still %d node(s) in"
942
                                 " this cluster." % (len(nodelist) - 1),
943
                                 errors.ECODE_INVAL)
944
    instancelist = self.cfg.GetInstanceList()
945
    if instancelist:
946
      raise errors.OpPrereqError("There are still %d instance(s) in"
947
                                 " this cluster." % len(instancelist),
948
                                 errors.ECODE_INVAL)
949

    
950
  def Exec(self, feedback_fn):
951
    """Destroys the cluster.
952

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

    
957
    # Run post hooks on master node before it's removed
958
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
959
    try:
960
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
961
    except:
962
      # pylint: disable-msg=W0702
963
      self.LogWarning("Errors occurred running hooks on %s" % master)
964

    
965
    result = self.rpc.call_node_stop_master(master, False)
966
    result.Raise("Could not disable the master role")
967

    
968
    if modify_ssh_setup:
969
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
970
      utils.CreateBackup(priv_key)
971
      utils.CreateBackup(pub_key)
972

    
973
    return master
974

    
975

    
976
def _VerifyCertificateInner(filename, expired, not_before, not_after, now,
977
                            warn_days=constants.SSL_CERT_EXPIRATION_WARN,
978
                            error_days=constants.SSL_CERT_EXPIRATION_ERROR):
979
  """Verifies certificate details for LUVerifyCluster.
980

981
  """
982
  if expired:
983
    msg = "Certificate %s is expired" % filename
984

    
985
    if not_before is not None and not_after is not None:
986
      msg += (" (valid from %s to %s)" %
987
              (_FormatTimestamp(not_before),
988
               _FormatTimestamp(not_after)))
989
    elif not_before is not None:
990
      msg += " (valid from %s)" % _FormatTimestamp(not_before)
991
    elif not_after is not None:
992
      msg += " (valid until %s)" % _FormatTimestamp(not_after)
993

    
994
    return (LUVerifyCluster.ETYPE_ERROR, msg)
995

    
996
  elif not_before is not None and not_before > now:
997
    return (LUVerifyCluster.ETYPE_WARNING,
998
            "Certificate %s not yet valid (valid from %s)" %
999
            (filename, _FormatTimestamp(not_before)))
1000

    
1001
  elif not_after is not None:
1002
    remaining_days = int((not_after - now) / (24 * 3600))
1003

    
1004
    msg = ("Certificate %s expires in %d days" % (filename, remaining_days))
1005

    
1006
    if remaining_days <= error_days:
1007
      return (LUVerifyCluster.ETYPE_ERROR, msg)
1008

    
1009
    if remaining_days <= warn_days:
1010
      return (LUVerifyCluster.ETYPE_WARNING, msg)
1011

    
1012
  return (None, None)
1013

    
1014

    
1015
def _VerifyCertificate(filename):
1016
  """Verifies a certificate for LUVerifyCluster.
1017

1018
  @type filename: string
1019
  @param filename: Path to PEM file
1020

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

    
1029
  # Depending on the pyOpenSSL version, this can just return (None, None)
1030
  (not_before, not_after) = utils.GetX509CertValidity(cert)
1031

    
1032
  return _VerifyCertificateInner(filename, cert.has_expired(),
1033
                                 not_before, not_after, time.time())
1034

    
1035

    
1036
class LUVerifyCluster(LogicalUnit):
1037
  """Verifies the cluster status.
1038

1039
  """
1040
  HPATH = "cluster-verify"
1041
  HTYPE = constants.HTYPE_CLUSTER
1042
  _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"]
1043
  REQ_BGL = False
1044

    
1045
  TCLUSTER = "cluster"
1046
  TNODE = "node"
1047
  TINSTANCE = "instance"
1048

    
1049
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1050
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1051
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1052
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1053
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1054
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1055
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1056
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1057
  ENODEDRBD = (TNODE, "ENODEDRBD")
1058
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1059
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1060
  ENODEHV = (TNODE, "ENODEHV")
1061
  ENODELVM = (TNODE, "ENODELVM")
1062
  ENODEN1 = (TNODE, "ENODEN1")
1063
  ENODENET = (TNODE, "ENODENET")
1064
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1065
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1066
  ENODERPC = (TNODE, "ENODERPC")
1067
  ENODESSH = (TNODE, "ENODESSH")
1068
  ENODEVERSION = (TNODE, "ENODEVERSION")
1069
  ENODESETUP = (TNODE, "ENODESETUP")
1070
  ENODETIME = (TNODE, "ENODETIME")
1071

    
1072
  ETYPE_FIELD = "code"
1073
  ETYPE_ERROR = "ERROR"
1074
  ETYPE_WARNING = "WARNING"
1075

    
1076
  def ExpandNames(self):
1077
    self.needed_locks = {
1078
      locking.LEVEL_NODE: locking.ALL_SET,
1079
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1080
    }
1081
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1082

    
1083
  def _Error(self, ecode, item, msg, *args, **kwargs):
1084
    """Format an error message.
1085

1086
    Based on the opcode's error_codes parameter, either format a
1087
    parseable error code, or a simpler error string.
1088

1089
    This must be called only from Exec and functions called from Exec.
1090

1091
    """
1092
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1093
    itype, etxt = ecode
1094
    # first complete the msg
1095
    if args:
1096
      msg = msg % args
1097
    # then format the whole message
1098
    if self.op.error_codes:
1099
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1100
    else:
1101
      if item:
1102
        item = " " + item
1103
      else:
1104
        item = ""
1105
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1106
    # and finally report it via the feedback_fn
1107
    self._feedback_fn("  - %s" % msg)
1108

    
1109
  def _ErrorIf(self, cond, *args, **kwargs):
1110
    """Log an error message if the passed condition is True.
1111

1112
    """
1113
    cond = bool(cond) or self.op.debug_simulate_errors
1114
    if cond:
1115
      self._Error(*args, **kwargs)
1116
    # do not mark the operation as failed for WARN cases only
1117
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1118
      self.bad = self.bad or cond
1119

    
1120
  def _VerifyNode(self, nodeinfo, file_list, local_cksum,
1121
                  node_result, master_files, drbd_map, vg_name):
1122
    """Run multiple tests against a node.
1123

1124
    Test list:
1125

1126
      - compares ganeti version
1127
      - checks vg existence and size > 20G
1128
      - checks config file checksum
1129
      - checks ssh to other nodes
1130

1131
    @type nodeinfo: L{objects.Node}
1132
    @param nodeinfo: the node to check
1133
    @param file_list: required list of files
1134
    @param local_cksum: dictionary of local files and their checksums
1135
    @param node_result: the results from the node
1136
    @param master_files: list of files that only masters should have
1137
    @param drbd_map: the useddrbd minors for this node, in
1138
        form of minor: (instance, must_exist) which correspond to instances
1139
        and their running status
1140
    @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
1141

1142
    """
1143
    node = nodeinfo.name
1144
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1145

    
1146
    # main result, node_result should be a non-empty dict
1147
    test = not node_result or not isinstance(node_result, dict)
1148
    _ErrorIf(test, self.ENODERPC, node,
1149
                  "unable to verify node: no data returned")
1150
    if test:
1151
      return
1152

    
1153
    # compares ganeti version
1154
    local_version = constants.PROTOCOL_VERSION
1155
    remote_version = node_result.get('version', None)
1156
    test = not (remote_version and
1157
                isinstance(remote_version, (list, tuple)) and
1158
                len(remote_version) == 2)
1159
    _ErrorIf(test, self.ENODERPC, node,
1160
             "connection to node returned invalid data")
1161
    if test:
1162
      return
1163

    
1164
    test = local_version != remote_version[0]
1165
    _ErrorIf(test, self.ENODEVERSION, node,
1166
             "incompatible protocol versions: master %s,"
1167
             " node %s", local_version, remote_version[0])
1168
    if test:
1169
      return
1170

    
1171
    # node seems compatible, we can actually try to look into its results
1172

    
1173
    # full package version
1174
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1175
                  self.ENODEVERSION, node,
1176
                  "software version mismatch: master %s, node %s",
1177
                  constants.RELEASE_VERSION, remote_version[1],
1178
                  code=self.ETYPE_WARNING)
1179

    
1180
    # checks vg existence and size > 20G
1181
    if vg_name is not None:
1182
      vglist = node_result.get(constants.NV_VGLIST, None)
1183
      test = not vglist
1184
      _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1185
      if not test:
1186
        vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1187
                                              constants.MIN_VG_SIZE)
1188
        _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1189

    
1190
    # checks config file checksum
1191

    
1192
    remote_cksum = node_result.get(constants.NV_FILELIST, None)
1193
    test = not isinstance(remote_cksum, dict)
1194
    _ErrorIf(test, self.ENODEFILECHECK, node,
1195
             "node hasn't returned file checksum data")
1196
    if not test:
1197
      for file_name in file_list:
1198
        node_is_mc = nodeinfo.master_candidate
1199
        must_have = (file_name not in master_files) or node_is_mc
1200
        # missing
1201
        test1 = file_name not in remote_cksum
1202
        # invalid checksum
1203
        test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1204
        # existing and good
1205
        test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1206
        _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1207
                 "file '%s' missing", file_name)
1208
        _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1209
                 "file '%s' has wrong checksum", file_name)
1210
        # not candidate and this is not a must-have file
1211
        _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1212
                 "file '%s' should not exist on non master"
1213
                 " candidates (and the file is outdated)", file_name)
1214
        # all good, except non-master/non-must have combination
1215
        _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1216
                 "file '%s' should not exist"
1217
                 " on non master candidates", file_name)
1218

    
1219
    # checks ssh to any
1220

    
1221
    test = constants.NV_NODELIST not in node_result
1222
    _ErrorIf(test, self.ENODESSH, node,
1223
             "node hasn't returned node ssh connectivity data")
1224
    if not test:
1225
      if node_result[constants.NV_NODELIST]:
1226
        for a_node, a_msg in node_result[constants.NV_NODELIST].items():
1227
          _ErrorIf(True, self.ENODESSH, node,
1228
                   "ssh communication with node '%s': %s", a_node, a_msg)
1229

    
1230
    test = constants.NV_NODENETTEST not in node_result
1231
    _ErrorIf(test, self.ENODENET, node,
1232
             "node hasn't returned node tcp connectivity data")
1233
    if not test:
1234
      if node_result[constants.NV_NODENETTEST]:
1235
        nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
1236
        for anode in nlist:
1237
          _ErrorIf(True, self.ENODENET, node,
1238
                   "tcp communication with node '%s': %s",
1239
                   anode, node_result[constants.NV_NODENETTEST][anode])
1240

    
1241
    hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
1242
    if isinstance(hyp_result, dict):
1243
      for hv_name, hv_result in hyp_result.iteritems():
1244
        test = hv_result is not None
1245
        _ErrorIf(test, self.ENODEHV, node,
1246
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1247

    
1248
    # check used drbd list
1249
    if vg_name is not None:
1250
      used_minors = node_result.get(constants.NV_DRBDLIST, [])
1251
      test = not isinstance(used_minors, (tuple, list))
1252
      _ErrorIf(test, self.ENODEDRBD, node,
1253
               "cannot parse drbd status file: %s", str(used_minors))
1254
      if not test:
1255
        for minor, (iname, must_exist) in drbd_map.items():
1256
          test = minor not in used_minors and must_exist
1257
          _ErrorIf(test, self.ENODEDRBD, node,
1258
                   "drbd minor %d of instance %s is not active",
1259
                   minor, iname)
1260
        for minor in used_minors:
1261
          test = minor not in drbd_map
1262
          _ErrorIf(test, self.ENODEDRBD, node,
1263
                   "unallocated drbd minor %d is in use", minor)
1264
    test = node_result.get(constants.NV_NODESETUP,
1265
                           ["Missing NODESETUP results"])
1266
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1267
             "; ".join(test))
1268

    
1269
    # check pv names
1270
    if vg_name is not None:
1271
      pvlist = node_result.get(constants.NV_PVLIST, None)
1272
      test = pvlist is None
1273
      _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1274
      if not test:
1275
        # check that ':' is not present in PV names, since it's a
1276
        # special character for lvcreate (denotes the range of PEs to
1277
        # use on the PV)
1278
        for _, pvname, owner_vg in pvlist:
1279
          test = ":" in pvname
1280
          _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1281
                   " '%s' of VG '%s'", pvname, owner_vg)
1282

    
1283
  def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
1284
                      node_instance, n_offline):
1285
    """Verify an instance.
1286

1287
    This function checks to see if the required block devices are
1288
    available on the instance's node.
1289

1290
    """
1291
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1292
    node_current = instanceconfig.primary_node
1293

    
1294
    node_vol_should = {}
1295
    instanceconfig.MapLVsByNode(node_vol_should)
1296

    
1297
    for node in node_vol_should:
1298
      if node in n_offline:
1299
        # ignore missing volumes on offline nodes
1300
        continue
1301
      for volume in node_vol_should[node]:
1302
        test = node not in node_vol_is or volume not in node_vol_is[node]
1303
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1304
                 "volume %s missing on node %s", volume, node)
1305

    
1306
    if instanceconfig.admin_up:
1307
      test = ((node_current not in node_instance or
1308
               not instance in node_instance[node_current]) and
1309
              node_current not in n_offline)
1310
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1311
               "instance not running on its primary node %s",
1312
               node_current)
1313

    
1314
    for node in node_instance:
1315
      if (not node == node_current):
1316
        test = instance in node_instance[node]
1317
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1318
                 "instance should not run on node %s", node)
1319

    
1320
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is):
1321
    """Verify if there are any unknown volumes in the cluster.
1322

1323
    The .os, .swap and backup volumes are ignored. All other volumes are
1324
    reported as unknown.
1325

1326
    """
1327
    for node in node_vol_is:
1328
      for volume in node_vol_is[node]:
1329
        test = (node not in node_vol_should or
1330
                volume not in node_vol_should[node])
1331
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1332
                      "volume %s is unknown", volume)
1333

    
1334
  def _VerifyOrphanInstances(self, instancelist, node_instance):
1335
    """Verify the list of running instances.
1336

1337
    This checks what instances are running but unknown to the cluster.
1338

1339
    """
1340
    for node in node_instance:
1341
      for o_inst in node_instance[node]:
1342
        test = o_inst not in instancelist
1343
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1344
                      "instance %s on node %s should not exist", o_inst, node)
1345

    
1346
  def _VerifyNPlusOneMemory(self, node_info, instance_cfg):
1347
    """Verify N+1 Memory Resilience.
1348

1349
    Check that if one single node dies we can still start all the instances it
1350
    was primary for.
1351

1352
    """
1353
    for node, nodeinfo in node_info.iteritems():
1354
      # This code checks that every node which is now listed as secondary has
1355
      # enough memory to host all instances it is supposed to should a single
1356
      # other node in the cluster fail.
1357
      # FIXME: not ready for failover to an arbitrary node
1358
      # FIXME: does not support file-backed instances
1359
      # WARNING: we currently take into account down instances as well as up
1360
      # ones, considering that even if they're down someone might want to start
1361
      # them even in the event of a node failure.
1362
      for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
1363
        needed_mem = 0
1364
        for instance in instances:
1365
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1366
          if bep[constants.BE_AUTO_BALANCE]:
1367
            needed_mem += bep[constants.BE_MEMORY]
1368
        test = nodeinfo['mfree'] < needed_mem
1369
        self._ErrorIf(test, self.ENODEN1, node,
1370
                      "not enough memory on to accommodate"
1371
                      " failovers should peer node %s fail", prinode)
1372

    
1373
  def CheckPrereq(self):
1374
    """Check prerequisites.
1375

1376
    Transform the list of checks we're going to skip into a set and check that
1377
    all its members are valid.
1378

1379
    """
1380
    self.skip_set = frozenset(self.op.skip_checks)
1381
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1382
      raise errors.OpPrereqError("Invalid checks to be skipped specified",
1383
                                 errors.ECODE_INVAL)
1384

    
1385
  def BuildHooksEnv(self):
1386
    """Build hooks env.
1387

1388
    Cluster-Verify hooks just ran in the post phase and their failure makes
1389
    the output be logged in the verify output and the verification to fail.
1390

1391
    """
1392
    all_nodes = self.cfg.GetNodeList()
1393
    env = {
1394
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1395
      }
1396
    for node in self.cfg.GetAllNodesInfo().values():
1397
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1398

    
1399
    return env, [], all_nodes
1400

    
1401
  def Exec(self, feedback_fn):
1402
    """Verify integrity of cluster, performing various test on nodes.
1403

1404
    """
1405
    self.bad = False
1406
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1407
    verbose = self.op.verbose
1408
    self._feedback_fn = feedback_fn
1409
    feedback_fn("* Verifying global settings")
1410
    for msg in self.cfg.VerifyConfig():
1411
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1412

    
1413
    # Check the cluster certificates
1414
    for cert_filename in constants.ALL_CERT_FILES:
1415
      (errcode, msg) = _VerifyCertificate(cert_filename)
1416
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1417

    
1418
    vg_name = self.cfg.GetVGName()
1419
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1420
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1421
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1422
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1423
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1424
                        for iname in instancelist)
1425
    i_non_redundant = [] # Non redundant instances
1426
    i_non_a_balanced = [] # Non auto-balanced instances
1427
    n_offline = [] # List of offline nodes
1428
    n_drained = [] # List of nodes being drained
1429
    node_volume = {}
1430
    node_instance = {}
1431
    node_info = {}
1432
    instance_cfg = {}
1433

    
1434
    # FIXME: verify OS list
1435
    # do local checksums
1436
    master_files = [constants.CLUSTER_CONF_FILE]
1437

    
1438
    file_names = ssconf.SimpleStore().GetFileList()
1439
    file_names.extend(constants.ALL_CERT_FILES)
1440
    file_names.extend(master_files)
1441

    
1442
    local_checksums = utils.FingerprintFiles(file_names)
1443

    
1444
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1445
    node_verify_param = {
1446
      constants.NV_FILELIST: file_names,
1447
      constants.NV_NODELIST: [node.name for node in nodeinfo
1448
                              if not node.offline],
1449
      constants.NV_HYPERVISOR: hypervisors,
1450
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1451
                                  node.secondary_ip) for node in nodeinfo
1452
                                 if not node.offline],
1453
      constants.NV_INSTANCELIST: hypervisors,
1454
      constants.NV_VERSION: None,
1455
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1456
      constants.NV_NODESETUP: None,
1457
      constants.NV_TIME: None,
1458
      }
1459

    
1460
    if vg_name is not None:
1461
      node_verify_param[constants.NV_VGLIST] = None
1462
      node_verify_param[constants.NV_LVLIST] = vg_name
1463
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1464
      node_verify_param[constants.NV_DRBDLIST] = None
1465

    
1466
    # Due to the way our RPC system works, exact response times cannot be
1467
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
1468
    # time before and after executing the request, we can at least have a time
1469
    # window.
1470
    nvinfo_starttime = time.time()
1471
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1472
                                           self.cfg.GetClusterName())
1473
    nvinfo_endtime = time.time()
1474

    
1475
    cluster = self.cfg.GetClusterInfo()
1476
    master_node = self.cfg.GetMasterNode()
1477
    all_drbd_map = self.cfg.ComputeDRBDMap()
1478

    
1479
    feedback_fn("* Verifying node status")
1480
    for node_i in nodeinfo:
1481
      node = node_i.name
1482

    
1483
      if node_i.offline:
1484
        if verbose:
1485
          feedback_fn("* Skipping offline node %s" % (node,))
1486
        n_offline.append(node)
1487
        continue
1488

    
1489
      if node == master_node:
1490
        ntype = "master"
1491
      elif node_i.master_candidate:
1492
        ntype = "master candidate"
1493
      elif node_i.drained:
1494
        ntype = "drained"
1495
        n_drained.append(node)
1496
      else:
1497
        ntype = "regular"
1498
      if verbose:
1499
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1500

    
1501
      msg = all_nvinfo[node].fail_msg
1502
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
1503
      if msg:
1504
        continue
1505

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

    
1521
      self._VerifyNode(node_i, file_names, local_checksums,
1522
                       nresult, master_files, node_drbd, vg_name)
1523

    
1524
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1525
      if vg_name is None:
1526
        node_volume[node] = {}
1527
      elif isinstance(lvdata, basestring):
1528
        _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1529
                 utils.SafeEncode(lvdata))
1530
        node_volume[node] = {}
1531
      elif not isinstance(lvdata, dict):
1532
        _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1533
        continue
1534
      else:
1535
        node_volume[node] = lvdata
1536

    
1537
      # node_instance
1538
      idata = nresult.get(constants.NV_INSTANCELIST, None)
1539
      test = not isinstance(idata, list)
1540
      _ErrorIf(test, self.ENODEHV, node,
1541
               "rpc call to node failed (instancelist): %s",
1542
               utils.SafeEncode(str(idata)))
1543
      if test:
1544
        continue
1545

    
1546
      node_instance[node] = idata
1547

    
1548
      # node_info
1549
      nodeinfo = nresult.get(constants.NV_HVINFO, None)
1550
      test = not isinstance(nodeinfo, dict)
1551
      _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1552
      if test:
1553
        continue
1554

    
1555
      # Node time
1556
      ntime = nresult.get(constants.NV_TIME, None)
1557
      try:
1558
        ntime_merged = utils.MergeTime(ntime)
1559
      except (ValueError, TypeError):
1560
        _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1561

    
1562
      if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1563
        ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1564
      elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1565
        ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1566
      else:
1567
        ntime_diff = None
1568

    
1569
      _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1570
               "Node time diverges by at least %s from master node time",
1571
               ntime_diff)
1572

    
1573
      if ntime_diff is not None:
1574
        continue
1575

    
1576
      try:
1577
        node_info[node] = {
1578
          "mfree": int(nodeinfo['memory_free']),
1579
          "pinst": [],
1580
          "sinst": [],
1581
          # dictionary holding all instances this node is secondary for,
1582
          # grouped by their primary node. Each key is a cluster node, and each
1583
          # value is a list of instances which have the key as primary and the
1584
          # current node as secondary.  this is handy to calculate N+1 memory
1585
          # availability if you can only failover from a primary to its
1586
          # secondary.
1587
          "sinst-by-pnode": {},
1588
        }
1589
        # FIXME: devise a free space model for file based instances as well
1590
        if vg_name is not None:
1591
          test = (constants.NV_VGLIST not in nresult or
1592
                  vg_name not in nresult[constants.NV_VGLIST])
1593
          _ErrorIf(test, self.ENODELVM, node,
1594
                   "node didn't return data for the volume group '%s'"
1595
                   " - it is either missing or broken", vg_name)
1596
          if test:
1597
            continue
1598
          node_info[node]["dfree"] = int(nresult[constants.NV_VGLIST][vg_name])
1599
      except (ValueError, KeyError):
1600
        _ErrorIf(True, self.ENODERPC, node,
1601
                 "node returned invalid nodeinfo, check lvm/hypervisor")
1602
        continue
1603

    
1604
    node_vol_should = {}
1605

    
1606
    feedback_fn("* Verifying instance status")
1607
    for instance in instancelist:
1608
      if verbose:
1609
        feedback_fn("* Verifying instance %s" % instance)
1610
      inst_config = instanceinfo[instance]
1611
      self._VerifyInstance(instance, inst_config, node_volume,
1612
                           node_instance, n_offline)
1613
      inst_nodes_offline = []
1614

    
1615
      inst_config.MapLVsByNode(node_vol_should)
1616

    
1617
      instance_cfg[instance] = inst_config
1618

    
1619
      pnode = inst_config.primary_node
1620
      _ErrorIf(pnode not in node_info and pnode not in n_offline,
1621
               self.ENODERPC, pnode, "instance %s, connection to"
1622
               " primary node failed", instance)
1623
      if pnode in node_info:
1624
        node_info[pnode]['pinst'].append(instance)
1625

    
1626
      if pnode in n_offline:
1627
        inst_nodes_offline.append(pnode)
1628

    
1629
      # If the instance is non-redundant we cannot survive losing its primary
1630
      # node, so we are not N+1 compliant. On the other hand we have no disk
1631
      # templates with more than one secondary so that situation is not well
1632
      # supported either.
1633
      # FIXME: does not support file-backed instances
1634
      if len(inst_config.secondary_nodes) == 0:
1635
        i_non_redundant.append(instance)
1636
      _ErrorIf(len(inst_config.secondary_nodes) > 1,
1637
               self.EINSTANCELAYOUT, instance,
1638
               "instance has multiple secondary nodes", code="WARNING")
1639

    
1640
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1641
        i_non_a_balanced.append(instance)
1642

    
1643
      for snode in inst_config.secondary_nodes:
1644
        _ErrorIf(snode not in node_info and snode not in n_offline,
1645
                 self.ENODERPC, snode,
1646
                 "instance %s, connection to secondary node"
1647
                 " failed", instance)
1648

    
1649
        if snode in node_info:
1650
          node_info[snode]['sinst'].append(instance)
1651
          if pnode not in node_info[snode]['sinst-by-pnode']:
1652
            node_info[snode]['sinst-by-pnode'][pnode] = []
1653
          node_info[snode]['sinst-by-pnode'][pnode].append(instance)
1654

    
1655
        if snode in n_offline:
1656
          inst_nodes_offline.append(snode)
1657

    
1658
      # warn that the instance lives on offline nodes
1659
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
1660
               "instance lives on offline node(s) %s",
1661
               utils.CommaJoin(inst_nodes_offline))
1662

    
1663
    feedback_fn("* Verifying orphan volumes")
1664
    self._VerifyOrphanVolumes(node_vol_should, node_volume)
1665

    
1666
    feedback_fn("* Verifying remaining instances")
1667
    self._VerifyOrphanInstances(instancelist, node_instance)
1668

    
1669
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1670
      feedback_fn("* Verifying N+1 Memory redundancy")
1671
      self._VerifyNPlusOneMemory(node_info, instance_cfg)
1672

    
1673
    feedback_fn("* Other Notes")
1674
    if i_non_redundant:
1675
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1676
                  % len(i_non_redundant))
1677

    
1678
    if i_non_a_balanced:
1679
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1680
                  % len(i_non_a_balanced))
1681

    
1682
    if n_offline:
1683
      feedback_fn("  - NOTICE: %d offline node(s) found." % len(n_offline))
1684

    
1685
    if n_drained:
1686
      feedback_fn("  - NOTICE: %d drained node(s) found." % len(n_drained))
1687

    
1688
    return not self.bad
1689

    
1690
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1691
    """Analyze the post-hooks' result
1692

1693
    This method analyses the hook result, handles it, and sends some
1694
    nicely-formatted feedback back to the user.
1695

1696
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1697
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1698
    @param hooks_results: the results of the multi-node hooks rpc call
1699
    @param feedback_fn: function used send feedback back to the caller
1700
    @param lu_result: previous Exec result
1701
    @return: the new Exec result, based on the previous result
1702
        and hook results
1703

1704
    """
1705
    # We only really run POST phase hooks, and are only interested in
1706
    # their results
1707
    if phase == constants.HOOKS_PHASE_POST:
1708
      # Used to change hooks' output to proper indentation
1709
      indent_re = re.compile('^', re.M)
1710
      feedback_fn("* Hooks Results")
1711
      assert hooks_results, "invalid result from hooks"
1712

    
1713
      for node_name in hooks_results:
1714
        res = hooks_results[node_name]
1715
        msg = res.fail_msg
1716
        test = msg and not res.offline
1717
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
1718
                      "Communication failure in hooks execution: %s", msg)
1719
        if res.offline or msg:
1720
          # No need to investigate payload if node is offline or gave an error.
1721
          # override manually lu_result here as _ErrorIf only
1722
          # overrides self.bad
1723
          lu_result = 1
1724
          continue
1725
        for script, hkr, output in res.payload:
1726
          test = hkr == constants.HKR_FAIL
1727
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
1728
                        "Script %s failed, output:", script)
1729
          if test:
1730
            output = indent_re.sub('      ', output)
1731
            feedback_fn("%s" % output)
1732
            lu_result = 0
1733

    
1734
      return lu_result
1735

    
1736

    
1737
class LUVerifyDisks(NoHooksLU):
1738
  """Verifies the cluster disks status.
1739

1740
  """
1741
  _OP_REQP = []
1742
  REQ_BGL = False
1743

    
1744
  def ExpandNames(self):
1745
    self.needed_locks = {
1746
      locking.LEVEL_NODE: locking.ALL_SET,
1747
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1748
    }
1749
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1750

    
1751
  def CheckPrereq(self):
1752
    """Check prerequisites.
1753

1754
    This has no prerequisites.
1755

1756
    """
1757
    pass
1758

    
1759
  def Exec(self, feedback_fn):
1760
    """Verify integrity of cluster disks.
1761

1762
    @rtype: tuple of three items
1763
    @return: a tuple of (dict of node-to-node_error, list of instances
1764
        which need activate-disks, dict of instance: (node, volume) for
1765
        missing volumes
1766

1767
    """
1768
    result = res_nodes, res_instances, res_missing = {}, [], {}
1769

    
1770
    vg_name = self.cfg.GetVGName()
1771
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1772
    instances = [self.cfg.GetInstanceInfo(name)
1773
                 for name in self.cfg.GetInstanceList()]
1774

    
1775
    nv_dict = {}
1776
    for inst in instances:
1777
      inst_lvs = {}
1778
      if (not inst.admin_up or
1779
          inst.disk_template not in constants.DTS_NET_MIRROR):
1780
        continue
1781
      inst.MapLVsByNode(inst_lvs)
1782
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1783
      for node, vol_list in inst_lvs.iteritems():
1784
        for vol in vol_list:
1785
          nv_dict[(node, vol)] = inst
1786

    
1787
    if not nv_dict:
1788
      return result
1789

    
1790
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1791

    
1792
    for node in nodes:
1793
      # node_volume
1794
      node_res = node_lvs[node]
1795
      if node_res.offline:
1796
        continue
1797
      msg = node_res.fail_msg
1798
      if msg:
1799
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1800
        res_nodes[node] = msg
1801
        continue
1802

    
1803
      lvs = node_res.payload
1804
      for lv_name, (_, _, lv_online) in lvs.items():
1805
        inst = nv_dict.pop((node, lv_name), None)
1806
        if (not lv_online and inst is not None
1807
            and inst.name not in res_instances):
1808
          res_instances.append(inst.name)
1809

    
1810
    # any leftover items in nv_dict are missing LVs, let's arrange the
1811
    # data better
1812
    for key, inst in nv_dict.iteritems():
1813
      if inst.name not in res_missing:
1814
        res_missing[inst.name] = []
1815
      res_missing[inst.name].append(key)
1816

    
1817
    return result
1818

    
1819

    
1820
class LURepairDiskSizes(NoHooksLU):
1821
  """Verifies the cluster disks sizes.
1822

1823
  """
1824
  _OP_REQP = ["instances"]
1825
  REQ_BGL = False
1826

    
1827
  def ExpandNames(self):
1828
    if not isinstance(self.op.instances, list):
1829
      raise errors.OpPrereqError("Invalid argument type 'instances'",
1830
                                 errors.ECODE_INVAL)
1831

    
1832
    if self.op.instances:
1833
      self.wanted_names = []
1834
      for name in self.op.instances:
1835
        full_name = _ExpandInstanceName(self.cfg, name)
1836
        self.wanted_names.append(full_name)
1837
      self.needed_locks = {
1838
        locking.LEVEL_NODE: [],
1839
        locking.LEVEL_INSTANCE: self.wanted_names,
1840
        }
1841
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1842
    else:
1843
      self.wanted_names = None
1844
      self.needed_locks = {
1845
        locking.LEVEL_NODE: locking.ALL_SET,
1846
        locking.LEVEL_INSTANCE: locking.ALL_SET,
1847
        }
1848
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1849

    
1850
  def DeclareLocks(self, level):
1851
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
1852
      self._LockInstancesNodes(primary_only=True)
1853

    
1854
  def CheckPrereq(self):
1855
    """Check prerequisites.
1856

1857
    This only checks the optional instance list against the existing names.
1858

1859
    """
1860
    if self.wanted_names is None:
1861
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
1862

    
1863
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
1864
                             in self.wanted_names]
1865

    
1866
  def _EnsureChildSizes(self, disk):
1867
    """Ensure children of the disk have the needed disk size.
1868

1869
    This is valid mainly for DRBD8 and fixes an issue where the
1870
    children have smaller disk size.
1871

1872
    @param disk: an L{ganeti.objects.Disk} object
1873

1874
    """
1875
    if disk.dev_type == constants.LD_DRBD8:
1876
      assert disk.children, "Empty children for DRBD8?"
1877
      fchild = disk.children[0]
1878
      mismatch = fchild.size < disk.size
1879
      if mismatch:
1880
        self.LogInfo("Child disk has size %d, parent %d, fixing",
1881
                     fchild.size, disk.size)
1882
        fchild.size = disk.size
1883

    
1884
      # and we recurse on this child only, not on the metadev
1885
      return self._EnsureChildSizes(fchild) or mismatch
1886
    else:
1887
      return False
1888

    
1889
  def Exec(self, feedback_fn):
1890
    """Verify the size of cluster disks.
1891

1892
    """
1893
    # TODO: check child disks too
1894
    # TODO: check differences in size between primary/secondary nodes
1895
    per_node_disks = {}
1896
    for instance in self.wanted_instances:
1897
      pnode = instance.primary_node
1898
      if pnode not in per_node_disks:
1899
        per_node_disks[pnode] = []
1900
      for idx, disk in enumerate(instance.disks):
1901
        per_node_disks[pnode].append((instance, idx, disk))
1902

    
1903
    changed = []
1904
    for node, dskl in per_node_disks.items():
1905
      newl = [v[2].Copy() for v in dskl]
1906
      for dsk in newl:
1907
        self.cfg.SetDiskID(dsk, node)
1908
      result = self.rpc.call_blockdev_getsizes(node, newl)
1909
      if result.fail_msg:
1910
        self.LogWarning("Failure in blockdev_getsizes call to node"
1911
                        " %s, ignoring", node)
1912
        continue
1913
      if len(result.data) != len(dskl):
1914
        self.LogWarning("Invalid result from node %s, ignoring node results",
1915
                        node)
1916
        continue
1917
      for ((instance, idx, disk), size) in zip(dskl, result.data):
1918
        if size is None:
1919
          self.LogWarning("Disk %d of instance %s did not return size"
1920
                          " information, ignoring", idx, instance.name)
1921
          continue
1922
        if not isinstance(size, (int, long)):
1923
          self.LogWarning("Disk %d of instance %s did not return valid"
1924
                          " size information, ignoring", idx, instance.name)
1925
          continue
1926
        size = size >> 20
1927
        if size != disk.size:
1928
          self.LogInfo("Disk %d of instance %s has mismatched size,"
1929
                       " correcting: recorded %d, actual %d", idx,
1930
                       instance.name, disk.size, size)
1931
          disk.size = size
1932
          self.cfg.Update(instance, feedback_fn)
1933
          changed.append((instance.name, idx, size))
1934
        if self._EnsureChildSizes(disk):
1935
          self.cfg.Update(instance, feedback_fn)
1936
          changed.append((instance.name, idx, disk.size))
1937
    return changed
1938

    
1939

    
1940
class LURenameCluster(LogicalUnit):
1941
  """Rename the cluster.
1942

1943
  """
1944
  HPATH = "cluster-rename"
1945
  HTYPE = constants.HTYPE_CLUSTER
1946
  _OP_REQP = ["name"]
1947

    
1948
  def BuildHooksEnv(self):
1949
    """Build hooks env.
1950

1951
    """
1952
    env = {
1953
      "OP_TARGET": self.cfg.GetClusterName(),
1954
      "NEW_NAME": self.op.name,
1955
      }
1956
    mn = self.cfg.GetMasterNode()
1957
    all_nodes = self.cfg.GetNodeList()
1958
    return env, [mn], all_nodes
1959

    
1960
  def CheckPrereq(self):
1961
    """Verify that the passed name is a valid one.
1962

1963
    """
1964
    hostname = utils.GetHostInfo(self.op.name)
1965

    
1966
    new_name = hostname.name
1967
    self.ip = new_ip = hostname.ip
1968
    old_name = self.cfg.GetClusterName()
1969
    old_ip = self.cfg.GetMasterIP()
1970
    if new_name == old_name and new_ip == old_ip:
1971
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
1972
                                 " cluster has changed",
1973
                                 errors.ECODE_INVAL)
1974
    if new_ip != old_ip:
1975
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1976
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
1977
                                   " reachable on the network. Aborting." %
1978
                                   new_ip, errors.ECODE_NOTUNIQUE)
1979

    
1980
    self.op.name = new_name
1981

    
1982
  def Exec(self, feedback_fn):
1983
    """Rename the cluster.
1984

1985
    """
1986
    clustername = self.op.name
1987
    ip = self.ip
1988

    
1989
    # shutdown the master IP
1990
    master = self.cfg.GetMasterNode()
1991
    result = self.rpc.call_node_stop_master(master, False)
1992
    result.Raise("Could not disable the master role")
1993

    
1994
    try:
1995
      cluster = self.cfg.GetClusterInfo()
1996
      cluster.cluster_name = clustername
1997
      cluster.master_ip = ip
1998
      self.cfg.Update(cluster, feedback_fn)
1999

    
2000
      # update the known hosts file
2001
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2002
      node_list = self.cfg.GetNodeList()
2003
      try:
2004
        node_list.remove(master)
2005
      except ValueError:
2006
        pass
2007
      result = self.rpc.call_upload_file(node_list,
2008
                                         constants.SSH_KNOWN_HOSTS_FILE)
2009
      for to_node, to_result in result.iteritems():
2010
        msg = to_result.fail_msg
2011
        if msg:
2012
          msg = ("Copy of file %s to node %s failed: %s" %
2013
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
2014
          self.proc.LogWarning(msg)
2015

    
2016
    finally:
2017
      result = self.rpc.call_node_start_master(master, False, False)
2018
      msg = result.fail_msg
2019
      if msg:
2020
        self.LogWarning("Could not re-enable the master role on"
2021
                        " the master, please restart manually: %s", msg)
2022

    
2023

    
2024
def _RecursiveCheckIfLVMBased(disk):
2025
  """Check if the given disk or its children are lvm-based.
2026

2027
  @type disk: L{objects.Disk}
2028
  @param disk: the disk to check
2029
  @rtype: boolean
2030
  @return: boolean indicating whether a LD_LV dev_type was found or not
2031

2032
  """
2033
  if disk.children:
2034
    for chdisk in disk.children:
2035
      if _RecursiveCheckIfLVMBased(chdisk):
2036
        return True
2037
  return disk.dev_type == constants.LD_LV
2038

    
2039

    
2040
class LUSetClusterParams(LogicalUnit):
2041
  """Change the parameters of the cluster.
2042

2043
  """
2044
  HPATH = "cluster-modify"
2045
  HTYPE = constants.HTYPE_CLUSTER
2046
  _OP_REQP = []
2047
  REQ_BGL = False
2048

    
2049
  def CheckArguments(self):
2050
    """Check parameters
2051

2052
    """
2053
    if not hasattr(self.op, "candidate_pool_size"):
2054
      self.op.candidate_pool_size = None
2055
    if self.op.candidate_pool_size is not None:
2056
      try:
2057
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
2058
      except (ValueError, TypeError), err:
2059
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
2060
                                   str(err), errors.ECODE_INVAL)
2061
      if self.op.candidate_pool_size < 1:
2062
        raise errors.OpPrereqError("At least one master candidate needed",
2063
                                   errors.ECODE_INVAL)
2064

    
2065
  def ExpandNames(self):
2066
    # FIXME: in the future maybe other cluster params won't require checking on
2067
    # all nodes to be modified.
2068
    self.needed_locks = {
2069
      locking.LEVEL_NODE: locking.ALL_SET,
2070
    }
2071
    self.share_locks[locking.LEVEL_NODE] = 1
2072

    
2073
  def BuildHooksEnv(self):
2074
    """Build hooks env.
2075

2076
    """
2077
    env = {
2078
      "OP_TARGET": self.cfg.GetClusterName(),
2079
      "NEW_VG_NAME": self.op.vg_name,
2080
      }
2081
    mn = self.cfg.GetMasterNode()
2082
    return env, [mn], [mn]
2083

    
2084
  def CheckPrereq(self):
2085
    """Check prerequisites.
2086

2087
    This checks whether the given params don't conflict and
2088
    if the given volume group is valid.
2089

2090
    """
2091
    if self.op.vg_name is not None and not self.op.vg_name:
2092
      instances = self.cfg.GetAllInstancesInfo().values()
2093
      for inst in instances:
2094
        for disk in inst.disks:
2095
          if _RecursiveCheckIfLVMBased(disk):
2096
            raise errors.OpPrereqError("Cannot disable lvm storage while"
2097
                                       " lvm-based instances exist",
2098
                                       errors.ECODE_INVAL)
2099

    
2100
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2101

    
2102
    # if vg_name not None, checks given volume group on all nodes
2103
    if self.op.vg_name:
2104
      vglist = self.rpc.call_vg_list(node_list)
2105
      for node in node_list:
2106
        msg = vglist[node].fail_msg
2107
        if msg:
2108
          # ignoring down node
2109
          self.LogWarning("Error while gathering data on node %s"
2110
                          " (ignoring node): %s", node, msg)
2111
          continue
2112
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2113
                                              self.op.vg_name,
2114
                                              constants.MIN_VG_SIZE)
2115
        if vgstatus:
2116
          raise errors.OpPrereqError("Error on node '%s': %s" %
2117
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2118

    
2119
    self.cluster = cluster = self.cfg.GetClusterInfo()
2120
    # validate params changes
2121
    if self.op.beparams:
2122
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2123
      self.new_beparams = objects.FillDict(
2124
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
2125

    
2126
    if self.op.nicparams:
2127
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2128
      self.new_nicparams = objects.FillDict(
2129
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
2130
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2131
      nic_errors = []
2132

    
2133
      # check all instances for consistency
2134
      for instance in self.cfg.GetAllInstancesInfo().values():
2135
        for nic_idx, nic in enumerate(instance.nics):
2136
          params_copy = copy.deepcopy(nic.nicparams)
2137
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2138

    
2139
          # check parameter syntax
2140
          try:
2141
            objects.NIC.CheckParameterSyntax(params_filled)
2142
          except errors.ConfigurationError, err:
2143
            nic_errors.append("Instance %s, nic/%d: %s" %
2144
                              (instance.name, nic_idx, err))
2145

    
2146
          # if we're moving instances to routed, check that they have an ip
2147
          target_mode = params_filled[constants.NIC_MODE]
2148
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2149
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2150
                              (instance.name, nic_idx))
2151
      if nic_errors:
2152
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2153
                                   "\n".join(nic_errors))
2154

    
2155
    # hypervisor list/parameters
2156
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
2157
    if self.op.hvparams:
2158
      if not isinstance(self.op.hvparams, dict):
2159
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input",
2160
                                   errors.ECODE_INVAL)
2161
      for hv_name, hv_dict in self.op.hvparams.items():
2162
        if hv_name not in self.new_hvparams:
2163
          self.new_hvparams[hv_name] = hv_dict
2164
        else:
2165
          self.new_hvparams[hv_name].update(hv_dict)
2166

    
2167
    # os hypervisor parameters
2168
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2169
    if self.op.os_hvp:
2170
      if not isinstance(self.op.os_hvp, dict):
2171
        raise errors.OpPrereqError("Invalid 'os_hvp' parameter on input",
2172
                                   errors.ECODE_INVAL)
2173
      for os_name, hvs in self.op.os_hvp.items():
2174
        if not isinstance(hvs, dict):
2175
          raise errors.OpPrereqError(("Invalid 'os_hvp' parameter on"
2176
                                      " input"), errors.ECODE_INVAL)
2177
        if os_name not in self.new_os_hvp:
2178
          self.new_os_hvp[os_name] = hvs
2179
        else:
2180
          for hv_name, hv_dict in hvs.items():
2181
            if hv_name not in self.new_os_hvp[os_name]:
2182
              self.new_os_hvp[os_name][hv_name] = hv_dict
2183
            else:
2184
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2185

    
2186
    if self.op.enabled_hypervisors is not None:
2187
      self.hv_list = self.op.enabled_hypervisors
2188
      if not self.hv_list:
2189
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
2190
                                   " least one member",
2191
                                   errors.ECODE_INVAL)
2192
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
2193
      if invalid_hvs:
2194
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
2195
                                   " entries: %s" %
2196
                                   utils.CommaJoin(invalid_hvs),
2197
                                   errors.ECODE_INVAL)
2198
    else:
2199
      self.hv_list = cluster.enabled_hypervisors
2200

    
2201
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2202
      # either the enabled list has changed, or the parameters have, validate
2203
      for hv_name, hv_params in self.new_hvparams.items():
2204
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2205
            (self.op.enabled_hypervisors and
2206
             hv_name in self.op.enabled_hypervisors)):
2207
          # either this is a new hypervisor, or its parameters have changed
2208
          hv_class = hypervisor.GetHypervisor(hv_name)
2209
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2210
          hv_class.CheckParameterSyntax(hv_params)
2211
          _CheckHVParams(self, node_list, hv_name, hv_params)
2212

    
2213
    if self.op.os_hvp:
2214
      # no need to check any newly-enabled hypervisors, since the
2215
      # defaults have already been checked in the above code-block
2216
      for os_name, os_hvp in self.new_os_hvp.items():
2217
        for hv_name, hv_params in os_hvp.items():
2218
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2219
          # we need to fill in the new os_hvp on top of the actual hv_p
2220
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2221
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2222
          hv_class = hypervisor.GetHypervisor(hv_name)
2223
          hv_class.CheckParameterSyntax(new_osp)
2224
          _CheckHVParams(self, node_list, hv_name, new_osp)
2225

    
2226

    
2227
  def Exec(self, feedback_fn):
2228
    """Change the parameters of the cluster.
2229

2230
    """
2231
    if self.op.vg_name is not None:
2232
      new_volume = self.op.vg_name
2233
      if not new_volume:
2234
        new_volume = None
2235
      if new_volume != self.cfg.GetVGName():
2236
        self.cfg.SetVGName(new_volume)
2237
      else:
2238
        feedback_fn("Cluster LVM configuration already in desired"
2239
                    " state, not changing")
2240
    if self.op.hvparams:
2241
      self.cluster.hvparams = self.new_hvparams
2242
    if self.op.os_hvp:
2243
      self.cluster.os_hvp = self.new_os_hvp
2244
    if self.op.enabled_hypervisors is not None:
2245
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2246
    if self.op.beparams:
2247
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2248
    if self.op.nicparams:
2249
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2250

    
2251
    if self.op.candidate_pool_size is not None:
2252
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2253
      # we need to update the pool size here, otherwise the save will fail
2254
      _AdjustCandidatePool(self, [])
2255

    
2256
    self.cfg.Update(self.cluster, feedback_fn)
2257

    
2258

    
2259
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2260
  """Distribute additional files which are part of the cluster configuration.
2261

2262
  ConfigWriter takes care of distributing the config and ssconf files, but
2263
  there are more files which should be distributed to all nodes. This function
2264
  makes sure those are copied.
2265

2266
  @param lu: calling logical unit
2267
  @param additional_nodes: list of nodes not in the config to distribute to
2268

2269
  """
2270
  # 1. Gather target nodes
2271
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2272
  dist_nodes = lu.cfg.GetOnlineNodeList()
2273
  if additional_nodes is not None:
2274
    dist_nodes.extend(additional_nodes)
2275
  if myself.name in dist_nodes:
2276
    dist_nodes.remove(myself.name)
2277

    
2278
  # 2. Gather files to distribute
2279
  dist_files = set([constants.ETC_HOSTS,
2280
                    constants.SSH_KNOWN_HOSTS_FILE,
2281
                    constants.RAPI_CERT_FILE,
2282
                    constants.RAPI_USERS_FILE,
2283
                    constants.CONFD_HMAC_KEY,
2284
                   ])
2285

    
2286
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2287
  for hv_name in enabled_hypervisors:
2288
    hv_class = hypervisor.GetHypervisor(hv_name)
2289
    dist_files.update(hv_class.GetAncillaryFiles())
2290

    
2291
  # 3. Perform the files upload
2292
  for fname in dist_files:
2293
    if os.path.exists(fname):
2294
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2295
      for to_node, to_result in result.items():
2296
        msg = to_result.fail_msg
2297
        if msg:
2298
          msg = ("Copy of file %s to node %s failed: %s" %
2299
                 (fname, to_node, msg))
2300
          lu.proc.LogWarning(msg)
2301

    
2302

    
2303
class LURedistributeConfig(NoHooksLU):
2304
  """Force the redistribution of cluster configuration.
2305

2306
  This is a very simple LU.
2307

2308
  """
2309
  _OP_REQP = []
2310
  REQ_BGL = False
2311

    
2312
  def ExpandNames(self):
2313
    self.needed_locks = {
2314
      locking.LEVEL_NODE: locking.ALL_SET,
2315
    }
2316
    self.share_locks[locking.LEVEL_NODE] = 1
2317

    
2318
  def CheckPrereq(self):
2319
    """Check prerequisites.
2320

2321
    """
2322

    
2323
  def Exec(self, feedback_fn):
2324
    """Redistribute the configuration.
2325

2326
    """
2327
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2328
    _RedistributeAncillaryFiles(self)
2329

    
2330

    
2331
def _WaitForSync(lu, instance, oneshot=False):
2332
  """Sleep and poll for an instance's disk to sync.
2333

2334
  """
2335
  if not instance.disks:
2336
    return True
2337

    
2338
  if not oneshot:
2339
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2340

    
2341
  node = instance.primary_node
2342

    
2343
  for dev in instance.disks:
2344
    lu.cfg.SetDiskID(dev, node)
2345

    
2346
  # TODO: Convert to utils.Retry
2347

    
2348
  retries = 0
2349
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2350
  while True:
2351
    max_time = 0
2352
    done = True
2353
    cumul_degraded = False
2354
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
2355
    msg = rstats.fail_msg
2356
    if msg:
2357
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2358
      retries += 1
2359
      if retries >= 10:
2360
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2361
                                 " aborting." % node)
2362
      time.sleep(6)
2363
      continue
2364
    rstats = rstats.payload
2365
    retries = 0
2366
    for i, mstat in enumerate(rstats):
2367
      if mstat is None:
2368
        lu.LogWarning("Can't compute data for node %s/%s",
2369
                           node, instance.disks[i].iv_name)
2370
        continue
2371

    
2372
      cumul_degraded = (cumul_degraded or
2373
                        (mstat.is_degraded and mstat.sync_percent is None))
2374
      if mstat.sync_percent is not None:
2375
        done = False
2376
        if mstat.estimated_time is not None:
2377
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
2378
          max_time = mstat.estimated_time
2379
        else:
2380
          rem_time = "no time estimate"
2381
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2382
                        (instance.disks[i].iv_name, mstat.sync_percent,
2383
                         rem_time))
2384

    
2385
    # if we're done but degraded, let's do a few small retries, to
2386
    # make sure we see a stable and not transient situation; therefore
2387
    # we force restart of the loop
2388
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2389
      logging.info("Degraded disks found, %d retries left", degr_retries)
2390
      degr_retries -= 1
2391
      time.sleep(1)
2392
      continue
2393

    
2394
    if done or oneshot:
2395
      break
2396

    
2397
    time.sleep(min(60, max_time))
2398

    
2399
  if done:
2400
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2401
  return not cumul_degraded
2402

    
2403

    
2404
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2405
  """Check that mirrors are not degraded.
2406

2407
  The ldisk parameter, if True, will change the test from the
2408
  is_degraded attribute (which represents overall non-ok status for
2409
  the device(s)) to the ldisk (representing the local storage status).
2410

2411
  """
2412
  lu.cfg.SetDiskID(dev, node)
2413

    
2414
  result = True
2415

    
2416
  if on_primary or dev.AssembleOnSecondary():
2417
    rstats = lu.rpc.call_blockdev_find(node, dev)
2418
    msg = rstats.fail_msg
2419
    if msg:
2420
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2421
      result = False
2422
    elif not rstats.payload:
2423
      lu.LogWarning("Can't find disk on node %s", node)
2424
      result = False
2425
    else:
2426
      if ldisk:
2427
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2428
      else:
2429
        result = result and not rstats.payload.is_degraded
2430

    
2431
  if dev.children:
2432
    for child in dev.children:
2433
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2434

    
2435
  return result
2436

    
2437

    
2438
class LUDiagnoseOS(NoHooksLU):
2439
  """Logical unit for OS diagnose/query.
2440

2441
  """
2442
  _OP_REQP = ["output_fields", "names"]
2443
  REQ_BGL = False
2444
  _FIELDS_STATIC = utils.FieldSet()
2445
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants")
2446
  # Fields that need calculation of global os validity
2447
  _FIELDS_NEEDVALID = frozenset(["valid", "variants"])
2448

    
2449
  def ExpandNames(self):
2450
    if self.op.names:
2451
      raise errors.OpPrereqError("Selective OS query not supported",
2452
                                 errors.ECODE_INVAL)
2453

    
2454
    _CheckOutputFields(static=self._FIELDS_STATIC,
2455
                       dynamic=self._FIELDS_DYNAMIC,
2456
                       selected=self.op.output_fields)
2457

    
2458
    # Lock all nodes, in shared mode
2459
    # Temporary removal of locks, should be reverted later
2460
    # TODO: reintroduce locks when they are lighter-weight
2461
    self.needed_locks = {}
2462
    #self.share_locks[locking.LEVEL_NODE] = 1
2463
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2464

    
2465
  def CheckPrereq(self):
2466
    """Check prerequisites.
2467

2468
    """
2469

    
2470
  @staticmethod
2471
  def _DiagnoseByOS(rlist):
2472
    """Remaps a per-node return list into an a per-os per-node dictionary
2473

2474
    @param rlist: a map with node names as keys and OS objects as values
2475

2476
    @rtype: dict
2477
    @return: a dictionary with osnames as keys and as value another map, with
2478
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2479

2480
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2481
                                     (/srv/..., False, "invalid api")],
2482
                           "node2": [(/srv/..., True, "")]}
2483
          }
2484

2485
    """
2486
    all_os = {}
2487
    # we build here the list of nodes that didn't fail the RPC (at RPC
2488
    # level), so that nodes with a non-responding node daemon don't
2489
    # make all OSes invalid
2490
    good_nodes = [node_name for node_name in rlist
2491
                  if not rlist[node_name].fail_msg]
2492
    for node_name, nr in rlist.items():
2493
      if nr.fail_msg or not nr.payload:
2494
        continue
2495
      for name, path, status, diagnose, variants in nr.payload:
2496
        if name not in all_os:
2497
          # build a list of nodes for this os containing empty lists
2498
          # for each node in node_list
2499
          all_os[name] = {}
2500
          for nname in good_nodes:
2501
            all_os[name][nname] = []
2502
        all_os[name][node_name].append((path, status, diagnose, variants))
2503
    return all_os
2504

    
2505
  def Exec(self, feedback_fn):
2506
    """Compute the list of OSes.
2507

2508
    """
2509
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2510
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2511
    pol = self._DiagnoseByOS(node_data)
2512
    output = []
2513
    calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields)
2514
    calc_variants = "variants" in self.op.output_fields
2515

    
2516
    for os_name, os_data in pol.items():
2517
      row = []
2518
      if calc_valid:
2519
        valid = True
2520
        variants = None
2521
        for osl in os_data.values():
2522
          valid = valid and osl and osl[0][1]
2523
          if not valid:
2524
            variants = None
2525
            break
2526
          if calc_variants:
2527
            node_variants = osl[0][3]
2528
            if variants is None:
2529
              variants = node_variants
2530
            else:
2531
              variants = [v for v in variants if v in node_variants]
2532

    
2533
      for field in self.op.output_fields:
2534
        if field == "name":
2535
          val = os_name
2536
        elif field == "valid":
2537
          val = valid
2538
        elif field == "node_status":
2539
          # this is just a copy of the dict
2540
          val = {}
2541
          for node_name, nos_list in os_data.items():
2542
            val[node_name] = nos_list
2543
        elif field == "variants":
2544
          val =  variants
2545
        else:
2546
          raise errors.ParameterError(field)
2547
        row.append(val)
2548
      output.append(row)
2549

    
2550
    return output
2551

    
2552

    
2553
class LURemoveNode(LogicalUnit):
2554
  """Logical unit for removing a node.
2555

2556
  """
2557
  HPATH = "node-remove"
2558
  HTYPE = constants.HTYPE_NODE
2559
  _OP_REQP = ["node_name"]
2560

    
2561
  def BuildHooksEnv(self):
2562
    """Build hooks env.
2563

2564
    This doesn't run on the target node in the pre phase as a failed
2565
    node would then be impossible to remove.
2566

2567
    """
2568
    env = {
2569
      "OP_TARGET": self.op.node_name,
2570
      "NODE_NAME": self.op.node_name,
2571
      }
2572
    all_nodes = self.cfg.GetNodeList()
2573
    try:
2574
      all_nodes.remove(self.op.node_name)
2575
    except ValueError:
2576
      logging.warning("Node %s which is about to be removed not found"
2577
                      " in the all nodes list", self.op.node_name)
2578
    return env, all_nodes, all_nodes
2579

    
2580
  def CheckPrereq(self):
2581
    """Check prerequisites.
2582

2583
    This checks:
2584
     - the node exists in the configuration
2585
     - it does not have primary or secondary instances
2586
     - it's not the master
2587

2588
    Any errors are signaled by raising errors.OpPrereqError.
2589

2590
    """
2591
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
2592
    node = self.cfg.GetNodeInfo(self.op.node_name)
2593
    assert node is not None
2594

    
2595
    instance_list = self.cfg.GetInstanceList()
2596

    
2597
    masternode = self.cfg.GetMasterNode()
2598
    if node.name == masternode:
2599
      raise errors.OpPrereqError("Node is the master node,"
2600
                                 " you need to failover first.",
2601
                                 errors.ECODE_INVAL)
2602

    
2603
    for instance_name in instance_list:
2604
      instance = self.cfg.GetInstanceInfo(instance_name)
2605
      if node.name in instance.all_nodes:
2606
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2607
                                   " please remove first." % instance_name,
2608
                                   errors.ECODE_INVAL)
2609
    self.op.node_name = node.name
2610
    self.node = node
2611

    
2612
  def Exec(self, feedback_fn):
2613
    """Removes the node from the cluster.
2614

2615
    """
2616
    node = self.node
2617
    logging.info("Stopping the node daemon and removing configs from node %s",
2618
                 node.name)
2619

    
2620
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
2621

    
2622
    # Promote nodes to master candidate as needed
2623
    _AdjustCandidatePool(self, exceptions=[node.name])
2624
    self.context.RemoveNode(node.name)
2625

    
2626
    # Run post hooks on the node before it's removed
2627
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
2628
    try:
2629
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
2630
    except:
2631
      # pylint: disable-msg=W0702
2632
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
2633

    
2634
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
2635
    msg = result.fail_msg
2636
    if msg:
2637
      self.LogWarning("Errors encountered on the remote node while leaving"
2638
                      " the cluster: %s", msg)
2639

    
2640

    
2641
class LUQueryNodes(NoHooksLU):
2642
  """Logical unit for querying nodes.
2643

2644
  """
2645
  # pylint: disable-msg=W0142
2646
  _OP_REQP = ["output_fields", "names", "use_locking"]
2647
  REQ_BGL = False
2648

    
2649
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
2650
                    "master_candidate", "offline", "drained"]
2651

    
2652
  _FIELDS_DYNAMIC = utils.FieldSet(
2653
    "dtotal", "dfree",
2654
    "mtotal", "mnode", "mfree",
2655
    "bootid",
2656
    "ctotal", "cnodes", "csockets",
2657
    )
2658

    
2659
  _FIELDS_STATIC = utils.FieldSet(*[
2660
    "pinst_cnt", "sinst_cnt",
2661
    "pinst_list", "sinst_list",
2662
    "pip", "sip", "tags",
2663
    "master",
2664
    "role"] + _SIMPLE_FIELDS
2665
    )
2666

    
2667
  def ExpandNames(self):
2668
    _CheckOutputFields(static=self._FIELDS_STATIC,
2669
                       dynamic=self._FIELDS_DYNAMIC,
2670
                       selected=self.op.output_fields)
2671

    
2672
    self.needed_locks = {}
2673
    self.share_locks[locking.LEVEL_NODE] = 1
2674

    
2675
    if self.op.names:
2676
      self.wanted = _GetWantedNodes(self, self.op.names)
2677
    else:
2678
      self.wanted = locking.ALL_SET
2679

    
2680
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2681
    self.do_locking = self.do_node_query and self.op.use_locking
2682
    if self.do_locking:
2683
      # if we don't request only static fields, we need to lock the nodes
2684
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2685

    
2686
  def CheckPrereq(self):
2687
    """Check prerequisites.
2688

2689
    """
2690
    # The validation of the node list is done in the _GetWantedNodes,
2691
    # if non empty, and if empty, there's no validation to do
2692
    pass
2693

    
2694
  def Exec(self, feedback_fn):
2695
    """Computes the list of nodes and their attributes.
2696

2697
    """
2698
    all_info = self.cfg.GetAllNodesInfo()
2699
    if self.do_locking:
2700
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2701
    elif self.wanted != locking.ALL_SET:
2702
      nodenames = self.wanted
2703
      missing = set(nodenames).difference(all_info.keys())
2704
      if missing:
2705
        raise errors.OpExecError(
2706
          "Some nodes were removed before retrieving their data: %s" % missing)
2707
    else:
2708
      nodenames = all_info.keys()
2709

    
2710
    nodenames = utils.NiceSort(nodenames)
2711
    nodelist = [all_info[name] for name in nodenames]
2712

    
2713
    # begin data gathering
2714

    
2715
    if self.do_node_query:
2716
      live_data = {}
2717
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2718
                                          self.cfg.GetHypervisorType())
2719
      for name in nodenames:
2720
        nodeinfo = node_data[name]
2721
        if not nodeinfo.fail_msg and nodeinfo.payload:
2722
          nodeinfo = nodeinfo.payload
2723
          fn = utils.TryConvert
2724
          live_data[name] = {
2725
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2726
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2727
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2728
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2729
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2730
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2731
            "bootid": nodeinfo.get('bootid', None),
2732
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2733
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2734
            }
2735
        else:
2736
          live_data[name] = {}
2737
    else:
2738
      live_data = dict.fromkeys(nodenames, {})
2739

    
2740
    node_to_primary = dict([(name, set()) for name in nodenames])
2741
    node_to_secondary = dict([(name, set()) for name in nodenames])
2742

    
2743
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2744
                             "sinst_cnt", "sinst_list"))
2745
    if inst_fields & frozenset(self.op.output_fields):
2746
      inst_data = self.cfg.GetAllInstancesInfo()
2747

    
2748
      for inst in inst_data.values():
2749
        if inst.primary_node in node_to_primary:
2750
          node_to_primary[inst.primary_node].add(inst.name)
2751
        for secnode in inst.secondary_nodes:
2752
          if secnode in node_to_secondary:
2753
            node_to_secondary[secnode].add(inst.name)
2754

    
2755
    master_node = self.cfg.GetMasterNode()
2756

    
2757
    # end data gathering
2758

    
2759
    output = []
2760
    for node in nodelist:
2761
      node_output = []
2762
      for field in self.op.output_fields:
2763
        if field in self._SIMPLE_FIELDS:
2764
          val = getattr(node, field)
2765
        elif field == "pinst_list":
2766
          val = list(node_to_primary[node.name])
2767
        elif field == "sinst_list":
2768
          val = list(node_to_secondary[node.name])
2769
        elif field == "pinst_cnt":
2770
          val = len(node_to_primary[node.name])
2771
        elif field == "sinst_cnt":
2772
          val = len(node_to_secondary[node.name])
2773
        elif field == "pip":
2774
          val = node.primary_ip
2775
        elif field == "sip":
2776
          val = node.secondary_ip
2777
        elif field == "tags":
2778
          val = list(node.GetTags())
2779
        elif field == "master":
2780
          val = node.name == master_node
2781
        elif self._FIELDS_DYNAMIC.Matches(field):
2782
          val = live_data[node.name].get(field, None)
2783
        elif field == "role":
2784
          if node.name == master_node:
2785
            val = "M"
2786
          elif node.master_candidate:
2787
            val = "C"
2788
          elif node.drained:
2789
            val = "D"
2790
          elif node.offline:
2791
            val = "O"
2792
          else:
2793
            val = "R"
2794
        else:
2795
          raise errors.ParameterError(field)
2796
        node_output.append(val)
2797
      output.append(node_output)
2798

    
2799
    return output
2800

    
2801

    
2802
class LUQueryNodeVolumes(NoHooksLU):
2803
  """Logical unit for getting volumes on node(s).
2804

2805
  """
2806
  _OP_REQP = ["nodes", "output_fields"]
2807
  REQ_BGL = False
2808
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2809
  _FIELDS_STATIC = utils.FieldSet("node")
2810

    
2811
  def ExpandNames(self):
2812
    _CheckOutputFields(static=self._FIELDS_STATIC,
2813
                       dynamic=self._FIELDS_DYNAMIC,
2814
                       selected=self.op.output_fields)
2815

    
2816
    self.needed_locks = {}
2817
    self.share_locks[locking.LEVEL_NODE] = 1
2818
    if not self.op.nodes:
2819
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2820
    else:
2821
      self.needed_locks[locking.LEVEL_NODE] = \
2822
        _GetWantedNodes(self, self.op.nodes)
2823

    
2824
  def CheckPrereq(self):
2825
    """Check prerequisites.
2826

2827
    This checks that the fields required are valid output fields.
2828

2829
    """
2830
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2831

    
2832
  def Exec(self, feedback_fn):
2833
    """Computes the list of nodes and their attributes.
2834

2835
    """
2836
    nodenames = self.nodes
2837
    volumes = self.rpc.call_node_volumes(nodenames)
2838

    
2839
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
2840
             in self.cfg.GetInstanceList()]
2841

    
2842
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
2843

    
2844
    output = []
2845
    for node in nodenames:
2846
      nresult = volumes[node]
2847
      if nresult.offline:
2848
        continue
2849
      msg = nresult.fail_msg
2850
      if msg:
2851
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
2852
        continue
2853

    
2854
      node_vols = nresult.payload[:]
2855
      node_vols.sort(key=lambda vol: vol['dev'])
2856

    
2857
      for vol in node_vols:
2858
        node_output = []
2859
        for field in self.op.output_fields:
2860
          if field == "node":
2861
            val = node
2862
          elif field == "phys":
2863
            val = vol['dev']
2864
          elif field == "vg":
2865
            val = vol['vg']
2866
          elif field == "name":
2867
            val = vol['name']
2868
          elif field == "size":
2869
            val = int(float(vol['size']))
2870
          elif field == "instance":
2871
            for inst in ilist:
2872
              if node not in lv_by_node[inst]:
2873
                continue
2874
              if vol['name'] in lv_by_node[inst][node]:
2875
                val = inst.name
2876
                break
2877
            else:
2878
              val = '-'
2879
          else:
2880
            raise errors.ParameterError(field)
2881
          node_output.append(str(val))
2882

    
2883
        output.append(node_output)
2884

    
2885
    return output
2886

    
2887

    
2888
class LUQueryNodeStorage(NoHooksLU):
2889
  """Logical unit for getting information on storage units on node(s).
2890

2891
  """
2892
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
2893
  REQ_BGL = False
2894
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
2895

    
2896
  def ExpandNames(self):
2897
    storage_type = self.op.storage_type
2898

    
2899
    if storage_type not in constants.VALID_STORAGE_TYPES:
2900
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2901
                                 errors.ECODE_INVAL)
2902

    
2903
    _CheckOutputFields(static=self._FIELDS_STATIC,
2904
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
2905
                       selected=self.op.output_fields)
2906

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

    
2910
    if self.op.nodes:
2911
      self.needed_locks[locking.LEVEL_NODE] = \
2912
        _GetWantedNodes(self, self.op.nodes)
2913
    else:
2914
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2915

    
2916
  def CheckPrereq(self):
2917
    """Check prerequisites.
2918

2919
    This checks that the fields required are valid output fields.
2920

2921
    """
2922
    self.op.name = getattr(self.op, "name", None)
2923

    
2924
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2925

    
2926
  def Exec(self, feedback_fn):
2927
    """Computes the list of nodes and their attributes.
2928

2929
    """
2930
    # Always get name to sort by
2931
    if constants.SF_NAME in self.op.output_fields:
2932
      fields = self.op.output_fields[:]
2933
    else:
2934
      fields = [constants.SF_NAME] + self.op.output_fields
2935

    
2936
    # Never ask for node or type as it's only known to the LU
2937
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
2938
      while extra in fields:
2939
        fields.remove(extra)
2940

    
2941
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
2942
    name_idx = field_idx[constants.SF_NAME]
2943

    
2944
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2945
    data = self.rpc.call_storage_list(self.nodes,
2946
                                      self.op.storage_type, st_args,
2947
                                      self.op.name, fields)
2948

    
2949
    result = []
2950

    
2951
    for node in utils.NiceSort(self.nodes):
2952
      nresult = data[node]
2953
      if nresult.offline:
2954
        continue
2955

    
2956
      msg = nresult.fail_msg
2957
      if msg:
2958
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
2959
        continue
2960

    
2961
      rows = dict([(row[name_idx], row) for row in nresult.payload])
2962

    
2963
      for name in utils.NiceSort(rows.keys()):
2964
        row = rows[name]
2965

    
2966
        out = []
2967

    
2968
        for field in self.op.output_fields:
2969
          if field == constants.SF_NODE:
2970
            val = node
2971
          elif field == constants.SF_TYPE:
2972
            val = self.op.storage_type
2973
          elif field in field_idx:
2974
            val = row[field_idx[field]]
2975
          else:
2976
            raise errors.ParameterError(field)
2977

    
2978
          out.append(val)
2979

    
2980
        result.append(out)
2981

    
2982
    return result
2983

    
2984

    
2985
class LUModifyNodeStorage(NoHooksLU):
2986
  """Logical unit for modifying a storage volume on a node.
2987

2988
  """
2989
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
2990
  REQ_BGL = False
2991

    
2992
  def CheckArguments(self):
2993
    self.opnode_name = _ExpandNodeName(self.cfg, self.op.node_name)
2994

    
2995
    storage_type = self.op.storage_type
2996
    if storage_type not in constants.VALID_STORAGE_TYPES:
2997
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2998
                                 errors.ECODE_INVAL)
2999

    
3000
  def ExpandNames(self):
3001
    self.needed_locks = {
3002
      locking.LEVEL_NODE: self.op.node_name,
3003
      }
3004

    
3005
  def CheckPrereq(self):
3006
    """Check prerequisites.
3007

3008
    """
3009
    storage_type = self.op.storage_type
3010

    
3011
    try:
3012
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
3013
    except KeyError:
3014
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
3015
                                 " modified" % storage_type,
3016
                                 errors.ECODE_INVAL)
3017

    
3018
    diff = set(self.op.changes.keys()) - modifiable
3019
    if diff:
3020
      raise errors.OpPrereqError("The following fields can not be modified for"
3021
                                 " storage units of type '%s': %r" %
3022
                                 (storage_type, list(diff)),
3023
                                 errors.ECODE_INVAL)
3024

    
3025
  def Exec(self, feedback_fn):
3026
    """Computes the list of nodes and their attributes.
3027

3028
    """
3029
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3030
    result = self.rpc.call_storage_modify(self.op.node_name,
3031
                                          self.op.storage_type, st_args,
3032
                                          self.op.name, self.op.changes)
3033
    result.Raise("Failed to modify storage unit '%s' on %s" %
3034
                 (self.op.name, self.op.node_name))
3035

    
3036

    
3037
class LUAddNode(LogicalUnit):
3038
  """Logical unit for adding node to the cluster.
3039

3040
  """
3041
  HPATH = "node-add"
3042
  HTYPE = constants.HTYPE_NODE
3043
  _OP_REQP = ["node_name"]
3044

    
3045
  def CheckArguments(self):
3046
    # validate/normalize the node name
3047
    self.op.node_name = utils.HostInfo.NormalizeName(self.op.node_name)
3048

    
3049
  def BuildHooksEnv(self):
3050
    """Build hooks env.
3051

3052
    This will run on all nodes before, and on all nodes + the new node after.
3053

3054
    """
3055
    env = {
3056
      "OP_TARGET": self.op.node_name,
3057
      "NODE_NAME": self.op.node_name,
3058
      "NODE_PIP": self.op.primary_ip,
3059
      "NODE_SIP": self.op.secondary_ip,
3060
      }
3061
    nodes_0 = self.cfg.GetNodeList()
3062
    nodes_1 = nodes_0 + [self.op.node_name, ]
3063
    return env, nodes_0, nodes_1
3064

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

3068
    This checks:
3069
     - the new node is not already in the config
3070
     - it is resolvable
3071
     - its parameters (single/dual homed) matches the cluster
3072

3073
    Any errors are signaled by raising errors.OpPrereqError.
3074

3075
    """
3076
    node_name = self.op.node_name
3077
    cfg = self.cfg
3078

    
3079
    dns_data = utils.GetHostInfo(node_name)
3080

    
3081
    node = dns_data.name
3082
    primary_ip = self.op.primary_ip = dns_data.ip
3083
    secondary_ip = getattr(self.op, "secondary_ip", None)
3084
    if secondary_ip is None:
3085
      secondary_ip = primary_ip
3086
    if not utils.IsValidIP(secondary_ip):
3087
      raise errors.OpPrereqError("Invalid secondary IP given",
3088
                                 errors.ECODE_INVAL)
3089
    self.op.secondary_ip = secondary_ip
3090

    
3091
    node_list = cfg.GetNodeList()
3092
    if not self.op.readd and node in node_list:
3093
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3094
                                 node, errors.ECODE_EXISTS)
3095
    elif self.op.readd and node not in node_list:
3096
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3097
                                 errors.ECODE_NOENT)
3098

    
3099
    for existing_node_name in node_list:
3100
      existing_node = cfg.GetNodeInfo(existing_node_name)
3101

    
3102
      if self.op.readd and node == existing_node_name:
3103
        if (existing_node.primary_ip != primary_ip or
3104
            existing_node.secondary_ip != secondary_ip):
3105
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3106
                                     " address configuration as before",
3107
                                     errors.ECODE_INVAL)
3108
        continue
3109

    
3110
      if (existing_node.primary_ip == primary_ip or
3111
          existing_node.secondary_ip == primary_ip or
3112
          existing_node.primary_ip == secondary_ip or
3113
          existing_node.secondary_ip == secondary_ip):
3114
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3115
                                   " existing node %s" % existing_node.name,
3116
                                   errors.ECODE_NOTUNIQUE)
3117

    
3118
    # check that the type of the node (single versus dual homed) is the
3119
    # same as for the master
3120
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
3121
    master_singlehomed = myself.secondary_ip == myself.primary_ip
3122
    newbie_singlehomed = secondary_ip == primary_ip
3123
    if master_singlehomed != newbie_singlehomed:
3124
      if master_singlehomed:
3125
        raise errors.OpPrereqError("The master has no private ip but the"
3126
                                   " new node has one",
3127
                                   errors.ECODE_INVAL)
3128
      else:
3129
        raise errors.OpPrereqError("The master has a private ip but the"
3130
                                   " new node doesn't have one",
3131
                                   errors.ECODE_INVAL)
3132

    
3133
    # checks reachability
3134
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3135
      raise errors.OpPrereqError("Node not reachable by ping",
3136
                                 errors.ECODE_ENVIRON)
3137

    
3138
    if not newbie_singlehomed:
3139
      # check reachability from my secondary ip to newbie's secondary ip
3140
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3141
                           source=myself.secondary_ip):
3142
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3143
                                   " based ping to noded port",
3144
                                   errors.ECODE_ENVIRON)
3145

    
3146
    if self.op.readd:
3147
      exceptions = [node]
3148
    else:
3149
      exceptions = []
3150

    
3151
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3152

    
3153
    if self.op.readd:
3154
      self.new_node = self.cfg.GetNodeInfo(node)
3155
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3156
    else:
3157
      self.new_node = objects.Node(name=node,
3158
                                   primary_ip=primary_ip,
3159
                                   secondary_ip=secondary_ip,
3160
                                   master_candidate=self.master_candidate,
3161
                                   offline=False, drained=False)
3162

    
3163
  def Exec(self, feedback_fn):
3164
    """Adds the new node to the cluster.
3165

3166
    """
3167
    new_node = self.new_node
3168
    node = new_node.name
3169

    
3170
    # for re-adds, reset the offline/drained/master-candidate flags;
3171
    # we need to reset here, otherwise offline would prevent RPC calls
3172
    # later in the procedure; this also means that if the re-add
3173
    # fails, we are left with a non-offlined, broken node
3174
    if self.op.readd:
3175
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
3176
      self.LogInfo("Readding a node, the offline/drained flags were reset")
3177
      # if we demote the node, we do cleanup later in the procedure
3178
      new_node.master_candidate = self.master_candidate
3179

    
3180
    # notify the user about any possible mc promotion
3181
    if new_node.master_candidate:
3182
      self.LogInfo("Node will be a master candidate")
3183

    
3184
    # check connectivity
3185
    result = self.rpc.call_version([node])[node]
3186
    result.Raise("Can't get version information from node %s" % node)
3187
    if constants.PROTOCOL_VERSION == result.payload:
3188
      logging.info("Communication to node %s fine, sw version %s match",
3189
                   node, result.payload)
3190
    else:
3191
      raise errors.OpExecError("Version mismatch master version %s,"
3192
                               " node version %s" %
3193
                               (constants.PROTOCOL_VERSION, result.payload))
3194

    
3195
    # setup ssh on node
3196
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3197
      logging.info("Copy ssh key to node %s", node)
3198
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3199
      keyarray = []
3200
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3201
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3202
                  priv_key, pub_key]
3203

    
3204
      for i in keyfiles:
3205
        keyarray.append(utils.ReadFile(i))
3206

    
3207
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3208
                                      keyarray[2], keyarray[3], keyarray[4],
3209
                                      keyarray[5])
3210
      result.Raise("Cannot transfer ssh keys to the new node")
3211

    
3212
    # Add node to our /etc/hosts, and add key to known_hosts
3213
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3214
      utils.AddHostToEtcHosts(new_node.name)
3215

    
3216
    if new_node.secondary_ip != new_node.primary_ip:
3217
      result = self.rpc.call_node_has_ip_address(new_node.name,
3218
                                                 new_node.secondary_ip)
3219
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3220
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3221
      if not result.payload:
3222
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3223
                                 " you gave (%s). Please fix and re-run this"
3224
                                 " command." % new_node.secondary_ip)
3225

    
3226
    node_verify_list = [self.cfg.GetMasterNode()]
3227
    node_verify_param = {
3228
      constants.NV_NODELIST: [node],
3229
      # TODO: do a node-net-test as well?
3230
    }
3231

    
3232
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3233
                                       self.cfg.GetClusterName())
3234
    for verifier in node_verify_list:
3235
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3236
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3237
      if nl_payload:
3238
        for failed in nl_payload:
3239
          feedback_fn("ssh/hostname verification failed"
3240
                      " (checking from %s): %s" %
3241
                      (verifier, nl_payload[failed]))
3242
        raise errors.OpExecError("ssh/hostname verification failed.")
3243

    
3244
    if self.op.readd:
3245
      _RedistributeAncillaryFiles(self)
3246
      self.context.ReaddNode(new_node)
3247
      # make sure we redistribute the config
3248
      self.cfg.Update(new_node, feedback_fn)
3249
      # and make sure the new node will not have old files around
3250
      if not new_node.master_candidate:
3251
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3252
        msg = result.fail_msg
3253
        if msg:
3254
          self.LogWarning("Node failed to demote itself from master"
3255
                          " candidate status: %s" % msg)
3256
    else:
3257
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3258
      self.context.AddNode(new_node, self.proc.GetECId())
3259

    
3260

    
3261
class LUSetNodeParams(LogicalUnit):
3262
  """Modifies the parameters of a node.
3263

3264
  """
3265
  HPATH = "node-modify"
3266
  HTYPE = constants.HTYPE_NODE
3267
  _OP_REQP = ["node_name"]
3268
  REQ_BGL = False
3269

    
3270
  def CheckArguments(self):
3271
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3272
    _CheckBooleanOpField(self.op, 'master_candidate')
3273
    _CheckBooleanOpField(self.op, 'offline')
3274
    _CheckBooleanOpField(self.op, 'drained')
3275
    _CheckBooleanOpField(self.op, 'auto_promote')
3276
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3277
    if all_mods.count(None) == 3:
3278
      raise errors.OpPrereqError("Please pass at least one modification",
3279
                                 errors.ECODE_INVAL)
3280
    if all_mods.count(True) > 1:
3281
      raise errors.OpPrereqError("Can't set the node into more than one"
3282
                                 " state at the same time",
3283
                                 errors.ECODE_INVAL)
3284

    
3285
    # Boolean value that tells us whether we're offlining or draining the node
3286
    self.offline_or_drain = (self.op.offline == True or
3287
                             self.op.drained == True)
3288
    self.deoffline_or_drain = (self.op.offline == False or
3289
                               self.op.drained == False)
3290
    self.might_demote = (self.op.master_candidate == False or
3291
                         self.offline_or_drain)
3292

    
3293
    self.lock_all = self.op.auto_promote and self.might_demote
3294

    
3295

    
3296
  def ExpandNames(self):
3297
    if self.lock_all:
3298
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3299
    else:
3300
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3301

    
3302
  def BuildHooksEnv(self):
3303
    """Build hooks env.
3304

3305
    This runs on the master node.
3306

3307
    """
3308
    env = {
3309
      "OP_TARGET": self.op.node_name,
3310
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3311
      "OFFLINE": str(self.op.offline),
3312
      "DRAINED": str(self.op.drained),
3313
      }
3314
    nl = [self.cfg.GetMasterNode(),
3315
          self.op.node_name]
3316
    return env, nl, nl
3317

    
3318
  def CheckPrereq(self):
3319
    """Check prerequisites.
3320

3321
    This only checks the instance list against the existing names.
3322

3323
    """
3324
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3325

    
3326
    if (self.op.master_candidate is not None or
3327
        self.op.drained is not None or
3328
        self.op.offline is not None):
3329
      # we can't change the master's node flags
3330
      if self.op.node_name == self.cfg.GetMasterNode():
3331
        raise errors.OpPrereqError("The master role can be changed"
3332
                                   " only via masterfailover",
3333
                                   errors.ECODE_INVAL)
3334

    
3335

    
3336
    if node.master_candidate and self.might_demote and not self.lock_all:
3337
      assert not self.op.auto_promote, "auto-promote set but lock_all not"
3338
      # check if after removing the current node, we're missing master
3339
      # candidates
3340
      (mc_remaining, mc_should, _) = \
3341
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
3342
      if mc_remaining != mc_should:
3343
        raise errors.OpPrereqError("Not enough master candidates, please"
3344
                                   " pass auto_promote to allow promotion",
3345
                                   errors.ECODE_INVAL)
3346

    
3347
    if (self.op.master_candidate == True and
3348
        ((node.offline and not self.op.offline == False) or
3349
         (node.drained and not self.op.drained == False))):
3350
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3351
                                 " to master_candidate" % node.name,
3352
                                 errors.ECODE_INVAL)
3353

    
3354
    # If we're being deofflined/drained, we'll MC ourself if needed
3355
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3356
        self.op.master_candidate == True and not node.master_candidate):
3357
      self.op.master_candidate = _DecideSelfPromotion(self)
3358
      if self.op.master_candidate:
3359
        self.LogInfo("Autopromoting node to master candidate")
3360

    
3361
    return
3362

    
3363
  def Exec(self, feedback_fn):
3364
    """Modifies a node.
3365

3366
    """
3367
    node = self.node
3368

    
3369
    result = []
3370
    changed_mc = False
3371

    
3372
    if self.op.offline is not None:
3373
      node.offline = self.op.offline
3374
      result.append(("offline", str(self.op.offline)))
3375
      if self.op.offline == True:
3376
        if node.master_candidate:
3377
          node.master_candidate = False
3378
          changed_mc = True
3379
          result.append(("master_candidate", "auto-demotion due to offline"))
3380
        if node.drained:
3381
          node.drained = False
3382
          result.append(("drained", "clear drained status due to offline"))
3383

    
3384
    if self.op.master_candidate is not None:
3385
      node.master_candidate = self.op.master_candidate
3386
      changed_mc = True
3387
      result.append(("master_candidate", str(self.op.master_candidate)))
3388
      if self.op.master_candidate == False:
3389
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3390
        msg = rrc.fail_msg
3391
        if msg:
3392
          self.LogWarning("Node failed to demote itself: %s" % msg)
3393

    
3394
    if self.op.drained is not None:
3395
      node.drained = self.op.drained
3396
      result.append(("drained", str(self.op.drained)))
3397
      if self.op.drained == True:
3398
        if node.master_candidate:
3399
          node.master_candidate = False
3400
          changed_mc = True
3401
          result.append(("master_candidate", "auto-demotion due to drain"))
3402
          rrc = self.rpc.call_node_demote_from_mc(node.name)
3403
          msg = rrc.fail_msg
3404
          if msg:
3405
            self.LogWarning("Node failed to demote itself: %s" % msg)
3406
        if node.offline:
3407
          node.offline = False
3408
          result.append(("offline", "clear offline status due to drain"))
3409

    
3410
    # we locked all nodes, we adjust the CP before updating this node
3411
    if self.lock_all:
3412
      _AdjustCandidatePool(self, [node.name])
3413

    
3414
    # this will trigger configuration file update, if needed
3415
    self.cfg.Update(node, feedback_fn)
3416

    
3417
    # this will trigger job queue propagation or cleanup
3418
    if changed_mc:
3419
      self.context.ReaddNode(node)
3420

    
3421
    return result
3422

    
3423

    
3424
class LUPowercycleNode(NoHooksLU):
3425
  """Powercycles a node.
3426

3427
  """
3428
  _OP_REQP = ["node_name", "force"]
3429
  REQ_BGL = False
3430

    
3431
  def CheckArguments(self):
3432
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3433
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
3434
      raise errors.OpPrereqError("The node is the master and the force"
3435
                                 " parameter was not set",
3436
                                 errors.ECODE_INVAL)
3437

    
3438
  def ExpandNames(self):
3439
    """Locking for PowercycleNode.
3440

3441
    This is a last-resort option and shouldn't block on other
3442
    jobs. Therefore, we grab no locks.
3443

3444
    """
3445
    self.needed_locks = {}
3446

    
3447
  def CheckPrereq(self):
3448
    """Check prerequisites.
3449

3450
    This LU has no prereqs.
3451

3452
    """
3453
    pass
3454

    
3455
  def Exec(self, feedback_fn):
3456
    """Reboots a node.
3457

3458
    """
3459
    result = self.rpc.call_node_powercycle(self.op.node_name,
3460
                                           self.cfg.GetHypervisorType())
3461
    result.Raise("Failed to schedule the reboot")
3462
    return result.payload
3463

    
3464

    
3465
class LUQueryClusterInfo(NoHooksLU):
3466
  """Query cluster configuration.
3467

3468
  """
3469
  _OP_REQP = []
3470
  REQ_BGL = False
3471

    
3472
  def ExpandNames(self):
3473
    self.needed_locks = {}
3474

    
3475
  def CheckPrereq(self):
3476
    """No prerequsites needed for this LU.
3477

3478
    """
3479
    pass
3480

    
3481
  def Exec(self, feedback_fn):
3482
    """Return cluster config.
3483

3484
    """
3485
    cluster = self.cfg.GetClusterInfo()
3486
    os_hvp = {}
3487

    
3488
    # Filter just for enabled hypervisors
3489
    for os_name, hv_dict in cluster.os_hvp.items():
3490
      os_hvp[os_name] = {}
3491
      for hv_name, hv_params in hv_dict.items():
3492
        if hv_name in cluster.enabled_hypervisors:
3493
          os_hvp[os_name][hv_name] = hv_params
3494

    
3495
    result = {
3496
      "software_version": constants.RELEASE_VERSION,
3497
      "protocol_version": constants.PROTOCOL_VERSION,
3498
      "config_version": constants.CONFIG_VERSION,
3499
      "os_api_version": max(constants.OS_API_VERSIONS),
3500
      "export_version": constants.EXPORT_VERSION,
3501
      "architecture": (platform.architecture()[0], platform.machine()),
3502
      "name": cluster.cluster_name,
3503
      "master": cluster.master_node,
3504
      "default_hypervisor": cluster.enabled_hypervisors[0],
3505
      "enabled_hypervisors": cluster.enabled_hypervisors,
3506
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3507
                        for hypervisor_name in cluster.enabled_hypervisors]),
3508
      "os_hvp": os_hvp,
3509
      "beparams": cluster.beparams,
3510
      "nicparams": cluster.nicparams,
3511
      "candidate_pool_size": cluster.candidate_pool_size,
3512
      "master_netdev": cluster.master_netdev,
3513
      "volume_group_name": cluster.volume_group_name,
3514
      "file_storage_dir": cluster.file_storage_dir,
3515
      "ctime": cluster.ctime,
3516
      "mtime": cluster.mtime,
3517
      "uuid": cluster.uuid,
3518
      "tags": list(cluster.GetTags()),
3519
      }
3520

    
3521
    return result
3522

    
3523

    
3524
class LUQueryConfigValues(NoHooksLU):
3525
  """Return configuration values.
3526

3527
  """
3528
  _OP_REQP = []
3529
  REQ_BGL = False
3530
  _FIELDS_DYNAMIC = utils.FieldSet()
3531
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
3532
                                  "watcher_pause")
3533

    
3534
  def ExpandNames(self):
3535
    self.needed_locks = {}
3536

    
3537
    _CheckOutputFields(static=self._FIELDS_STATIC,
3538
                       dynamic=self._FIELDS_DYNAMIC,
3539
                       selected=self.op.output_fields)
3540

    
3541
  def CheckPrereq(self):
3542
    """No prerequisites.
3543

3544
    """
3545
    pass
3546

    
3547
  def Exec(self, feedback_fn):
3548
    """Dump a representation of the cluster config to the standard output.
3549

3550
    """
3551
    values = []
3552
    for field in self.op.output_fields:
3553
      if field == "cluster_name":
3554
        entry = self.cfg.GetClusterName()
3555
      elif field == "master_node":
3556
        entry = self.cfg.GetMasterNode()
3557
      elif field == "drain_flag":
3558
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3559
      elif field == "watcher_pause":
3560
        entry = utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
3561
      else:
3562
        raise errors.ParameterError(field)
3563
      values.append(entry)
3564
    return values
3565

    
3566

    
3567
class LUActivateInstanceDisks(NoHooksLU):
3568
  """Bring up an instance's disks.
3569

3570
  """
3571
  _OP_REQP = ["instance_name"]
3572
  REQ_BGL = False
3573

    
3574
  def ExpandNames(self):
3575
    self._ExpandAndLockInstance()
3576
    self.needed_locks[locking.LEVEL_NODE] = []
3577
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3578

    
3579
  def DeclareLocks(self, level):
3580
    if level == locking.LEVEL_NODE:
3581
      self._LockInstancesNodes()
3582

    
3583
  def CheckPrereq(self):
3584
    """Check prerequisites.
3585

3586
    This checks that the instance is in the cluster.
3587

3588
    """
3589
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3590
    assert self.instance is not None, \
3591
      "Cannot retrieve locked instance %s" % self.op.instance_name
3592
    _CheckNodeOnline(self, self.instance.primary_node)
3593
    if not hasattr(self.op, "ignore_size"):
3594
      self.op.ignore_size = False
3595

    
3596
  def Exec(self, feedback_fn):
3597
    """Activate the disks.
3598

3599
    """
3600
    disks_ok, disks_info = \
3601
              _AssembleInstanceDisks(self, self.instance,
3602
                                     ignore_size=self.op.ignore_size)
3603
    if not disks_ok:
3604
      raise errors.OpExecError("Cannot activate block devices")
3605

    
3606
    return disks_info
3607

    
3608

    
3609
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3610
                           ignore_size=False):
3611
  """Prepare the block devices for an instance.
3612

3613
  This sets up the block devices on all nodes.
3614

3615
  @type lu: L{LogicalUnit}
3616
  @param lu: the logical unit on whose behalf we execute
3617
  @type instance: L{objects.Instance}
3618
  @param instance: the instance for whose disks we assemble
3619
  @type ignore_secondaries: boolean
3620
  @param ignore_secondaries: if true, errors on secondary nodes
3621
      won't result in an error return from the function
3622
  @type ignore_size: boolean
3623
  @param ignore_size: if true, the current known size of the disk
3624
      will not be used during the disk activation, useful for cases
3625
      when the size is wrong
3626
  @return: False if the operation failed, otherwise a list of
3627
      (host, instance_visible_name, node_visible_name)
3628
      with the mapping from node devices to instance devices
3629

3630
  """
3631
  device_info = []
3632
  disks_ok = True
3633
  iname = instance.name
3634
  # With the two passes mechanism we try to reduce the window of
3635
  # opportunity for the race condition of switching DRBD to primary
3636
  # before handshaking occured, but we do not eliminate it
3637

    
3638
  # The proper fix would be to wait (with some limits) until the
3639
  # connection has been made and drbd transitions from WFConnection
3640
  # into any other network-connected state (Connected, SyncTarget,
3641
  # SyncSource, etc.)
3642

    
3643
  # 1st pass, assemble on all nodes in secondary mode
3644
  for inst_disk in instance.disks:
3645
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3646
      if ignore_size:
3647
        node_disk = node_disk.Copy()
3648
        node_disk.UnsetSize()
3649
      lu.cfg.SetDiskID(node_disk, node)
3650
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3651
      msg = result.fail_msg
3652
      if msg:
3653
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3654
                           " (is_primary=False, pass=1): %s",
3655
                           inst_disk.iv_name, node, msg)
3656
        if not ignore_secondaries:
3657
          disks_ok = False
3658

    
3659
  # FIXME: race condition on drbd migration to primary
3660

    
3661
  # 2nd pass, do only the primary node
3662
  for inst_disk in instance.disks:
3663
    dev_path = None
3664

    
3665
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3666
      if node != instance.primary_node:
3667
        continue
3668
      if ignore_size:
3669
        node_disk = node_disk.Copy()
3670
        node_disk.UnsetSize()
3671
      lu.cfg.SetDiskID(node_disk, node)
3672
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3673
      msg = result.fail_msg
3674
      if msg:
3675
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3676
                           " (is_primary=True, pass=2): %s",
3677
                           inst_disk.iv_name, node, msg)
3678
        disks_ok = False
3679
      else:
3680
        dev_path = result.payload
3681

    
3682
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
3683

    
3684
  # leave the disks configured for the primary node
3685
  # this is a workaround that would be fixed better by
3686
  # improving the logical/physical id handling
3687
  for disk in instance.disks:
3688
    lu.cfg.SetDiskID(disk, instance.primary_node)
3689

    
3690
  return disks_ok, device_info
3691

    
3692

    
3693
def _StartInstanceDisks(lu, instance, force):
3694
  """Start the disks of an instance.
3695

3696
  """
3697
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3698
                                           ignore_secondaries=force)
3699
  if not disks_ok:
3700
    _ShutdownInstanceDisks(lu, instance)
3701
    if force is not None and not force:
3702
      lu.proc.LogWarning("", hint="If the message above refers to a"
3703
                         " secondary node,"
3704
                         " you can retry the operation using '--force'.")
3705
    raise errors.OpExecError("Disk consistency error")
3706

    
3707

    
3708
class LUDeactivateInstanceDisks(NoHooksLU):
3709
  """Shutdown an instance's disks.
3710

3711
  """
3712
  _OP_REQP = ["instance_name"]
3713
  REQ_BGL = False
3714

    
3715
  def ExpandNames(self):
3716
    self._ExpandAndLockInstance()
3717
    self.needed_locks[locking.LEVEL_NODE] = []
3718
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3719

    
3720
  def DeclareLocks(self, level):
3721
    if level == locking.LEVEL_NODE:
3722
      self._LockInstancesNodes()
3723

    
3724
  def CheckPrereq(self):
3725
    """Check prerequisites.
3726

3727
    This checks that the instance is in the cluster.
3728

3729
    """
3730
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3731
    assert self.instance is not None, \
3732
      "Cannot retrieve locked instance %s" % self.op.instance_name
3733

    
3734
  def Exec(self, feedback_fn):
3735
    """Deactivate the disks
3736

3737
    """
3738
    instance = self.instance
3739
    _SafeShutdownInstanceDisks(self, instance)
3740

    
3741

    
3742
def _SafeShutdownInstanceDisks(lu, instance):
3743
  """Shutdown block devices of an instance.
3744

3745
  This function checks if an instance is running, before calling
3746
  _ShutdownInstanceDisks.
3747

3748
  """
3749
  _CheckInstanceDown(lu, instance, "cannot shutdown disks")
3750
  _ShutdownInstanceDisks(lu, instance)
3751

    
3752

    
3753
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3754
  """Shutdown block devices of an instance.
3755

3756
  This does the shutdown on all nodes of the instance.
3757

3758
  If the ignore_primary is false, errors on the primary node are
3759
  ignored.
3760

3761
  """
3762
  all_result = True
3763
  for disk in instance.disks:
3764
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
3765
      lu.cfg.SetDiskID(top_disk, node)
3766
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
3767
      msg = result.fail_msg
3768
      if msg:
3769
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
3770
                      disk.iv_name, node, msg)
3771
        if not ignore_primary or node != instance.primary_node:
3772
          all_result = False
3773
  return all_result
3774

    
3775

    
3776
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
3777
  """Checks if a node has enough free memory.
3778

3779
  This function check if a given node has the needed amount of free
3780
  memory. In case the node has less memory or we cannot get the
3781
  information from the node, this function raise an OpPrereqError
3782
  exception.
3783

3784
  @type lu: C{LogicalUnit}
3785
  @param lu: a logical unit from which we get configuration data
3786
  @type node: C{str}
3787
  @param node: the node to check
3788
  @type reason: C{str}
3789
  @param reason: string to use in the error message
3790
  @type requested: C{int}
3791
  @param requested: the amount of memory in MiB to check for
3792
  @type hypervisor_name: C{str}
3793
  @param hypervisor_name: the hypervisor to ask for memory stats
3794
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
3795
      we cannot check the node
3796

3797
  """
3798
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
3799
  nodeinfo[node].Raise("Can't get data from node %s" % node,
3800
                       prereq=True, ecode=errors.ECODE_ENVIRON)
3801
  free_mem = nodeinfo[node].payload.get('memory_free', None)
3802
  if not isinstance(free_mem, int):
3803
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
3804
                               " was '%s'" % (node, free_mem),
3805
                               errors.ECODE_ENVIRON)
3806
  if requested > free_mem:
3807
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
3808
                               " needed %s MiB, available %s MiB" %
3809
                               (node, reason, requested, free_mem),
3810
                               errors.ECODE_NORES)
3811

    
3812

    
3813
def _CheckNodesFreeDisk(lu, nodenames, requested):
3814
  """Checks if nodes have enough free disk space in the default VG.
3815

3816
  This function check if all given nodes have the needed amount of
3817
  free disk. In case any node has less disk or we cannot get the
3818
  information from the node, this function raise an OpPrereqError
3819
  exception.
3820

3821
  @type lu: C{LogicalUnit}
3822
  @param lu: a logical unit from which we get configuration data
3823
  @type nodenames: C{list}
3824
  @param node: the list of node names to check
3825
  @type requested: C{int}
3826
  @param requested: the amount of disk in MiB to check for
3827
  @raise errors.OpPrereqError: if the node doesn't have enough disk, or
3828
      we cannot check the node
3829

3830
  """
3831
  nodeinfo = lu.rpc.call_node_info(nodenames, lu.cfg.GetVGName(),
3832
                                   lu.cfg.GetHypervisorType())
3833
  for node in nodenames:
3834
    info = nodeinfo[node]
3835
    info.Raise("Cannot get current information from node %s" % node,
3836
               prereq=True, ecode=errors.ECODE_ENVIRON)
3837
    vg_free = info.payload.get("vg_free", None)
3838
    if not isinstance(vg_free, int):
3839
      raise errors.OpPrereqError("Can't compute free disk space on node %s,"
3840
                                 " result was '%s'" % (node, vg_free),
3841
                                 errors.ECODE_ENVIRON)
3842
    if requested > vg_free:
3843
      raise errors.OpPrereqError("Not enough disk space on target node %s:"
3844
                                 " required %d MiB, available %d MiB" %
3845
                                 (node, requested, vg_free),
3846
                                 errors.ECODE_NORES)
3847

    
3848

    
3849
class LUStartupInstance(LogicalUnit):
3850
  """Starts an instance.
3851

3852
  """
3853
  HPATH = "instance-start"
3854
  HTYPE = constants.HTYPE_INSTANCE
3855
  _OP_REQP = ["instance_name", "force"]
3856
  REQ_BGL = False
3857

    
3858
  def ExpandNames(self):
3859
    self._ExpandAndLockInstance()
3860

    
3861
  def BuildHooksEnv(self):
3862
    """Build hooks env.
3863

3864
    This runs on master, primary and secondary nodes of the instance.
3865

3866
    """
3867
    env = {
3868
      "FORCE": self.op.force,
3869
      }
3870
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3871
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3872
    return env, nl, nl
3873

    
3874
  def CheckPrereq(self):
3875
    """Check prerequisites.
3876

3877
    This checks that the instance is in the cluster.
3878

3879
    """
3880
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3881
    assert self.instance is not None, \
3882
      "Cannot retrieve locked instance %s" % self.op.instance_name
3883

    
3884
    # extra beparams
3885
    self.beparams = getattr(self.op, "beparams", {})
3886
    if self.beparams:
3887
      if not isinstance(self.beparams, dict):
3888
        raise errors.OpPrereqError("Invalid beparams passed: %s, expected"
3889
                                   " dict" % (type(self.beparams), ),
3890
                                   errors.ECODE_INVAL)
3891
      # fill the beparams dict
3892
      utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
3893
      self.op.beparams = self.beparams
3894

    
3895
    # extra hvparams
3896
    self.hvparams = getattr(self.op, "hvparams", {})
3897
    if self.hvparams:
3898
      if not isinstance(self.hvparams, dict):
3899
        raise errors.OpPrereqError("Invalid hvparams passed: %s, expected"
3900
                                   " dict" % (type(self.hvparams), ),
3901
                                   errors.ECODE_INVAL)
3902

    
3903
      # check hypervisor parameter syntax (locally)
3904
      cluster = self.cfg.GetClusterInfo()
3905
      utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
3906
      filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor],
3907
                                    instance.hvparams)
3908
      filled_hvp.update(self.hvparams)
3909
      hv_type = hypervisor.GetHypervisor(instance.hypervisor)
3910
      hv_type.CheckParameterSyntax(filled_hvp)
3911
      _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
3912
      self.op.hvparams = self.hvparams
3913

    
3914
    _CheckNodeOnline(self, instance.primary_node)
3915

    
3916
    bep = self.cfg.GetClusterInfo().FillBE(instance)
3917
    # check bridges existence
3918
    _CheckInstanceBridgesExist(self, instance)
3919

    
3920
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3921
                                              instance.name,
3922
                                              instance.hypervisor)
3923
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3924
                      prereq=True, ecode=errors.ECODE_ENVIRON)
3925
    if not remote_info.payload: # not running already
3926
      _CheckNodeFreeMemory(self, instance.primary_node,
3927
                           "starting instance %s" % instance.name,
3928
                           bep[constants.BE_MEMORY], instance.hypervisor)
3929

    
3930
  def Exec(self, feedback_fn):
3931
    """Start the instance.
3932

3933
    """
3934
    instance = self.instance
3935
    force = self.op.force
3936

    
3937
    self.cfg.MarkInstanceUp(instance.name)
3938

    
3939
    node_current = instance.primary_node
3940

    
3941
    _StartInstanceDisks(self, instance, force)
3942

    
3943
    result = self.rpc.call_instance_start(node_current, instance,
3944
                                          self.hvparams, self.beparams)
3945
    msg = result.fail_msg
3946
    if msg:
3947
      _ShutdownInstanceDisks(self, instance)
3948
      raise errors.OpExecError("Could not start instance: %s" % msg)
3949

    
3950

    
3951
class LURebootInstance(LogicalUnit):
3952
  """Reboot an instance.
3953

3954
  """
3955
  HPATH = "instance-reboot"
3956
  HTYPE = constants.HTYPE_INSTANCE
3957
  _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
3958
  REQ_BGL = False
3959

    
3960
  def CheckArguments(self):
3961
    """Check the arguments.
3962

3963
    """
3964
    self.shutdown_timeout = getattr(self.op, "shutdown_timeout",
3965
                                    constants.DEFAULT_SHUTDOWN_TIMEOUT)
3966

    
3967
  def ExpandNames(self):
3968
    if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
3969
                                   constants.INSTANCE_REBOOT_HARD,
3970
                                   constants.INSTANCE_REBOOT_FULL]:
3971
      raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
3972
                                  (constants.INSTANCE_REBOOT_SOFT,
3973
                                   constants.INSTANCE_REBOOT_HARD,
3974
                                   constants.INSTANCE_REBOOT_FULL))
3975
    self._ExpandAndLockInstance()
3976

    
3977
  def BuildHooksEnv(self):
3978
    """Build hooks env.
3979

3980
    This runs on master, primary and secondary nodes of the instance.
3981

3982
    """
3983
    env = {
3984
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
3985
      "REBOOT_TYPE": self.op.reboot_type,
3986
      "SHUTDOWN_TIMEOUT": self.shutdown_timeout,
3987
      }
3988
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3989
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3990
    return env, nl, nl
3991

    
3992
  def CheckPrereq(self):
3993
    """Check prerequisites.
3994

3995
    This checks that the instance is in the cluster.
3996

3997
    """
3998
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3999
    assert self.instance is not None, \
4000
      "Cannot retrieve locked instance %s" % self.op.instance_name
4001

    
4002
    _CheckNodeOnline(self, instance.primary_node)
4003

    
4004
    # check bridges existence
4005
    _CheckInstanceBridgesExist(self, instance)
4006

    
4007
  def Exec(self, feedback_fn):
4008
    """Reboot the instance.
4009

4010
    """
4011
    instance = self.instance
4012
    ignore_secondaries = self.op.ignore_secondaries
4013
    reboot_type = self.op.reboot_type
4014

    
4015
    node_current = instance.primary_node
4016

    
4017
    if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
4018
                       constants.INSTANCE_REBOOT_HARD]:
4019
      for disk in instance.disks:
4020
        self.cfg.SetDiskID(disk, node_current)
4021
      result = self.rpc.call_instance_reboot(node_current, instance,
4022
                                             reboot_type,
4023
                                             self.shutdown_timeout)
4024
      result.Raise("Could not reboot instance")
4025
    else:
4026
      result = self.rpc.call_instance_shutdown(node_current, instance,
4027
                                               self.shutdown_timeout)
4028
      result.Raise("Could not shutdown instance for full reboot")
4029
      _ShutdownInstanceDisks(self, instance)
4030
      _StartInstanceDisks(self, instance, ignore_secondaries)
4031
      result = self.rpc.call_instance_start(node_current, instance, None, None)
4032
      msg = result.fail_msg
4033<