Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 2945fd2d

History | View | Annotate | Download (273.6 kB)

1
#
2
#
3

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

    
21

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

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

    
26
import os
27
import os.path
28
import time
29
import re
30
import platform
31
import logging
32
import copy
33

    
34
from ganeti import ssh
35
from ganeti import utils
36
from ganeti import errors
37
from ganeti import hypervisor
38
from ganeti import locking
39
from ganeti import constants
40
from ganeti import objects
41
from ganeti import serializer
42
from ganeti import ssconf
43

    
44

    
45
class LogicalUnit(object):
46
  """Logical Unit base class.
47

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

57
  Note that all commands require root permissions.
58

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

62
  """
63
  HPATH = None
64
  HTYPE = None
65
  _OP_REQP = []
66
  REQ_BGL = True
67

    
68
  def __init__(self, processor, op, context, rpc):
69
    """Constructor for LogicalUnit.
70

71
    This needs to be overridden in derived classes in order to check op
72
    validity.
73

74
    """
75
    self.proc = processor
76
    self.op = op
77
    self.cfg = context.cfg
78
    self.context = context
79
    self.rpc = rpc
80
    # Dicts used to declare locking needs to mcpu
81
    self.needed_locks = None
82
    self.acquired_locks = {}
83
    self.share_locks = dict.fromkeys(locking.LEVELS, 0)
84
    self.add_locks = {}
85
    self.remove_locks = {}
86
    # Used to force good behavior when calling helper functions
87
    self.recalculate_locks = {}
88
    self.__ssh = None
89
    # logging
90
    self.LogWarning = processor.LogWarning
91
    self.LogInfo = processor.LogInfo
92
    self.LogStep = processor.LogStep
93
    # support for dry-run
94
    self.dry_run_result = None
95

    
96
    # Tasklets
97
    self.tasklets = None
98

    
99
    for attr_name in self._OP_REQP:
100
      attr_val = getattr(op, attr_name, None)
101
      if attr_val is None:
102
        raise errors.OpPrereqError("Required parameter '%s' missing" %
103
                                   attr_name)
104

    
105
    self.CheckArguments()
106

    
107
  def __GetSSH(self):
108
    """Returns the SshRunner object
109

110
    """
111
    if not self.__ssh:
112
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
113
    return self.__ssh
114

    
115
  ssh = property(fget=__GetSSH)
116

    
117
  def CheckArguments(self):
118
    """Check syntactic validity for the opcode arguments.
119

120
    This method is for doing a simple syntactic check and ensure
121
    validity of opcode parameters, without any cluster-related
122
    checks. While the same can be accomplished in ExpandNames and/or
123
    CheckPrereq, doing these separate is better because:
124

125
      - ExpandNames is left as as purely a lock-related function
126
      - CheckPrereq is run after we have acquired locks (and possible
127
        waited for them)
128

129
    The function is allowed to change the self.op attribute so that
130
    later methods can no longer worry about missing parameters.
131

132
    """
133
    pass
134

    
135
  def ExpandNames(self):
136
    """Expand names for this LU.
137

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

143
    LUs which implement this method must also populate the self.needed_locks
144
    member, as a dict with lock levels as keys, and a list of needed lock names
145
    as values. Rules:
146

147
      - use an empty dict if you don't need any lock
148
      - if you don't need any lock at a particular level omit that level
149
      - don't put anything for the BGL level
150
      - if you want all locks at a level use locking.ALL_SET as a value
151

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

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

160
    Examples::
161

162
      # Acquire all nodes and one instance
163
      self.needed_locks = {
164
        locking.LEVEL_NODE: locking.ALL_SET,
165
        locking.LEVEL_INSTANCE: ['instance1.example.tld'],
166
      }
167
      # Acquire just two nodes
168
      self.needed_locks = {
169
        locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
170
      }
171
      # Acquire no locks
172
      self.needed_locks = {} # No, you can't leave it to the default value None
173

174
    """
175
    # The implementation of this method is mandatory only if the new LU is
176
    # concurrent, so that old LUs don't need to be changed all at the same
177
    # time.
178
    if self.REQ_BGL:
179
      self.needed_locks = {} # Exclusive LUs don't need locks.
180
    else:
181
      raise NotImplementedError
182

    
183
  def DeclareLocks(self, level):
184
    """Declare LU locking needs for a level
185

186
    While most LUs can just declare their locking needs at ExpandNames time,
187
    sometimes there's the need to calculate some locks after having acquired
188
    the ones before. This function is called just before acquiring locks at a
189
    particular level, but after acquiring the ones at lower levels, and permits
190
    such calculations. It can be used to modify self.needed_locks, and by
191
    default it does nothing.
192

193
    This function is only called if you have something already set in
194
    self.needed_locks for the level.
195

196
    @param level: Locking level which is going to be locked
197
    @type level: member of ganeti.locking.LEVELS
198

199
    """
200

    
201
  def CheckPrereq(self):
202
    """Check prerequisites for this LU.
203

204
    This method should check that the prerequisites for the execution
205
    of this LU are fulfilled. It can do internode communication, but
206
    it should be idempotent - no cluster or system changes are
207
    allowed.
208

209
    The method should raise errors.OpPrereqError in case something is
210
    not fulfilled. Its return value is ignored.
211

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

215
    """
216
    if self.tasklets is not None:
217
      for (idx, tl) in enumerate(self.tasklets):
218
        logging.debug("Checking prerequisites for tasklet %s/%s",
219
                      idx + 1, len(self.tasklets))
220
        tl.CheckPrereq()
221
    else:
222
      raise NotImplementedError
223

    
224
  def Exec(self, feedback_fn):
225
    """Execute the LU.
226

227
    This method should implement the actual work. It should raise
228
    errors.OpExecError for failures that are somewhat dealt with in
229
    code, or expected.
230

231
    """
232
    if self.tasklets is not None:
233
      for (idx, tl) in enumerate(self.tasklets):
234
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
235
        tl.Exec(feedback_fn)
236
    else:
237
      raise NotImplementedError
238

    
239
  def BuildHooksEnv(self):
240
    """Build hooks environment for this LU.
241

242
    This method should return a three-node tuple consisting of: a dict
243
    containing the environment that will be used for running the
244
    specific hook for this LU, a list of node names on which the hook
245
    should run before the execution, and a list of node names on which
246
    the hook should run after the execution.
247

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

253
    No nodes should be returned as an empty list (and not None).
254

255
    Note that if the HPATH for a LU class is None, this function will
256
    not be called.
257

258
    """
259
    raise NotImplementedError
260

    
261
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
262
    """Notify the LU about the results of its hooks.
263

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

270
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
271
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
272
    @param hook_results: the results of the multi-node hooks rpc call
273
    @param feedback_fn: function used send feedback back to the caller
274
    @param lu_result: the previous Exec result this LU had, or None
275
        in the PRE phase
276
    @return: the new Exec result, based on the previous result
277
        and hook results
278

279
    """
280
    return lu_result
281

    
282
  def _ExpandAndLockInstance(self):
283
    """Helper function to expand and lock an instance.
284

285
    Many LUs that work on an instance take its name in self.op.instance_name
286
    and need to expand it and then declare the expanded name for locking. This
287
    function does it, and then updates self.op.instance_name to the expanded
288
    name. It also initializes needed_locks as a dict, if this hasn't been done
289
    before.
290

291
    """
292
    if self.needed_locks is None:
293
      self.needed_locks = {}
294
    else:
295
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
296
        "_ExpandAndLockInstance called with instance-level locks set"
297
    expanded_name = self.cfg.ExpandInstanceName(self.op.instance_name)
298
    if expanded_name is None:
299
      raise errors.OpPrereqError("Instance '%s' not known" %
300
                                  self.op.instance_name)
301
    self.needed_locks[locking.LEVEL_INSTANCE] = expanded_name
302
    self.op.instance_name = expanded_name
303

    
304
  def _LockInstancesNodes(self, primary_only=False):
305
    """Helper function to declare instances' nodes for locking.
306

307
    This function should be called after locking one or more instances to lock
308
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
309
    with all primary or secondary nodes for instances already locked and
310
    present in self.needed_locks[locking.LEVEL_INSTANCE].
311

312
    It should be called from DeclareLocks, and for safety only works if
313
    self.recalculate_locks[locking.LEVEL_NODE] is set.
314

315
    In the future it may grow parameters to just lock some instance's nodes, or
316
    to just lock primaries or secondary nodes, if needed.
317

318
    If should be called in DeclareLocks in a way similar to::
319

320
      if level == locking.LEVEL_NODE:
321
        self._LockInstancesNodes()
322

323
    @type primary_only: boolean
324
    @param primary_only: only lock primary nodes of locked instances
325

326
    """
327
    assert locking.LEVEL_NODE in self.recalculate_locks, \
328
      "_LockInstancesNodes helper function called with no nodes to recalculate"
329

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

    
332
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
333
    # future we might want to have different behaviors depending on the value
334
    # of self.recalculate_locks[locking.LEVEL_NODE]
335
    wanted_nodes = []
336
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
337
      instance = self.context.cfg.GetInstanceInfo(instance_name)
338
      wanted_nodes.append(instance.primary_node)
339
      if not primary_only:
340
        wanted_nodes.extend(instance.secondary_nodes)
341

    
342
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
343
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
344
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
345
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
346

    
347
    del self.recalculate_locks[locking.LEVEL_NODE]
348

    
349

    
350
class NoHooksLU(LogicalUnit):
351
  """Simple LU which runs no hooks.
352

353
  This LU is intended as a parent for other LogicalUnits which will
354
  run no hooks, in order to reduce duplicate code.
355

356
  """
357
  HPATH = None
358
  HTYPE = None
359

    
360

    
361
class Tasklet:
362
  """Tasklet base class.
363

364
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
365
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
366
  tasklets know nothing about locks.
367

368
  Subclasses must follow these rules:
369
    - Implement CheckPrereq
370
    - Implement Exec
371

372
  """
373
  def __init__(self, lu):
374
    self.lu = lu
375

    
376
    # Shortcuts
377
    self.cfg = lu.cfg
378
    self.rpc = lu.rpc
379

    
380
  def CheckPrereq(self):
381
    """Check prerequisites for this tasklets.
382

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

387
    The method should raise errors.OpPrereqError in case something is not
388
    fulfilled. Its return value is ignored.
389

390
    This method should also update all parameters to their canonical form if it
391
    hasn't been done before.
392

393
    """
394
    raise NotImplementedError
395

    
396
  def Exec(self, feedback_fn):
397
    """Execute the tasklet.
398

399
    This method should implement the actual work. It should raise
400
    errors.OpExecError for failures that are somewhat dealt with in code, or
401
    expected.
402

403
    """
404
    raise NotImplementedError
405

    
406

    
407
def _GetWantedNodes(lu, nodes):
408
  """Returns list of checked and expanded node names.
409

410
  @type lu: L{LogicalUnit}
411
  @param lu: the logical unit on whose behalf we execute
412
  @type nodes: list
413
  @param nodes: list of node names or None for all nodes
414
  @rtype: list
415
  @return: the list of nodes, sorted
416
  @raise errors.OpProgrammerError: if the nodes parameter is wrong type
417

418
  """
419
  if not isinstance(nodes, list):
420
    raise errors.OpPrereqError("Invalid argument type 'nodes'")
421

    
422
  if not nodes:
423
    raise errors.ProgrammerError("_GetWantedNodes should only be called with a"
424
      " non-empty list of nodes whose name is to be expanded.")
425

    
426
  wanted = []
427
  for name in nodes:
428
    node = lu.cfg.ExpandNodeName(name)
429
    if node is None:
430
      raise errors.OpPrereqError("No such node name '%s'" % name)
431
    wanted.append(node)
432

    
433
  return utils.NiceSort(wanted)
434

    
435

    
436
def _GetWantedInstances(lu, instances):
437
  """Returns list of checked and expanded instance names.
438

439
  @type lu: L{LogicalUnit}
440
  @param lu: the logical unit on whose behalf we execute
441
  @type instances: list
442
  @param instances: list of instance names or None for all instances
443
  @rtype: list
444
  @return: the list of instances, sorted
445
  @raise errors.OpPrereqError: if the instances parameter is wrong type
446
  @raise errors.OpPrereqError: if any of the passed instances is not found
447

448
  """
449
  if not isinstance(instances, list):
450
    raise errors.OpPrereqError("Invalid argument type 'instances'")
451

    
452
  if instances:
453
    wanted = []
454

    
455
    for name in instances:
456
      instance = lu.cfg.ExpandInstanceName(name)
457
      if instance is None:
458
        raise errors.OpPrereqError("No such instance name '%s'" % name)
459
      wanted.append(instance)
460

    
461
  else:
462
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
463
  return wanted
464

    
465

    
466
def _CheckOutputFields(static, dynamic, selected):
467
  """Checks whether all selected fields are valid.
468

469
  @type static: L{utils.FieldSet}
470
  @param static: static fields set
471
  @type dynamic: L{utils.FieldSet}
472
  @param dynamic: dynamic fields set
473

474
  """
475
  f = utils.FieldSet()
476
  f.Extend(static)
477
  f.Extend(dynamic)
478

    
479
  delta = f.NonMatching(selected)
480
  if delta:
481
    raise errors.OpPrereqError("Unknown output fields selected: %s"
482
                               % ",".join(delta))
483

    
484

    
485
def _CheckBooleanOpField(op, name):
486
  """Validates boolean opcode parameters.
487

488
  This will ensure that an opcode parameter is either a boolean value,
489
  or None (but that it always exists).
490

491
  """
492
  val = getattr(op, name, None)
493
  if not (val is None or isinstance(val, bool)):
494
    raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" %
495
                               (name, str(val)))
496
  setattr(op, name, val)
497

    
498

    
499
def _CheckNodeOnline(lu, node):
500
  """Ensure that a given node is online.
501

502
  @param lu: the LU on behalf of which we make the check
503
  @param node: the node to check
504
  @raise errors.OpPrereqError: if the node is offline
505

506
  """
507
  if lu.cfg.GetNodeInfo(node).offline:
508
    raise errors.OpPrereqError("Can't use offline node %s" % node)
509

    
510

    
511
def _CheckNodeNotDrained(lu, node):
512
  """Ensure that a given node is not drained.
513

514
  @param lu: the LU on behalf of which we make the check
515
  @param node: the node to check
516
  @raise errors.OpPrereqError: if the node is drained
517

518
  """
519
  if lu.cfg.GetNodeInfo(node).drained:
520
    raise errors.OpPrereqError("Can't use drained node %s" % node)
521

    
522

    
523
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
524
                          memory, vcpus, nics, disk_template, disks,
525
                          bep, hvp, hypervisor_name):
526
  """Builds instance related env variables for hooks
527

528
  This builds the hook environment from individual variables.
529

530
  @type name: string
531
  @param name: the name of the instance
532
  @type primary_node: string
533
  @param primary_node: the name of the instance's primary node
534
  @type secondary_nodes: list
535
  @param secondary_nodes: list of secondary nodes as strings
536
  @type os_type: string
537
  @param os_type: the name of the instance's OS
538
  @type status: boolean
539
  @param status: the should_run status of the instance
540
  @type memory: string
541
  @param memory: the memory size of the instance
542
  @type vcpus: string
543
  @param vcpus: the count of VCPUs the instance has
544
  @type nics: list
545
  @param nics: list of tuples (ip, mac, mode, link) representing
546
      the NICs the instance has
547
  @type disk_template: string
548
  @param disk_template: the disk template of the instance
549
  @type disks: list
550
  @param disks: the list of (size, mode) pairs
551
  @type bep: dict
552
  @param bep: the backend parameters for the instance
553
  @type hvp: dict
554
  @param hvp: the hypervisor parameters for the instance
555
  @type hypervisor_name: string
556
  @param hypervisor_name: the hypervisor for the instance
557
  @rtype: dict
558
  @return: the hook environment for this instance
559

560
  """
561
  if status:
562
    str_status = "up"
563
  else:
564
    str_status = "down"
565
  env = {
566
    "OP_TARGET": name,
567
    "INSTANCE_NAME": name,
568
    "INSTANCE_PRIMARY": primary_node,
569
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
570
    "INSTANCE_OS_TYPE": os_type,
571
    "INSTANCE_STATUS": str_status,
572
    "INSTANCE_MEMORY": memory,
573
    "INSTANCE_VCPUS": vcpus,
574
    "INSTANCE_DISK_TEMPLATE": disk_template,
575
    "INSTANCE_HYPERVISOR": hypervisor_name,
576
  }
577

    
578
  if nics:
579
    nic_count = len(nics)
580
    for idx, (ip, mac, mode, link) in enumerate(nics):
581
      if ip is None:
582
        ip = ""
583
      env["INSTANCE_NIC%d_IP" % idx] = ip
584
      env["INSTANCE_NIC%d_MAC" % idx] = mac
585
      env["INSTANCE_NIC%d_MODE" % idx] = mode
586
      env["INSTANCE_NIC%d_LINK" % idx] = link
587
      if mode == constants.NIC_MODE_BRIDGED:
588
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
589
  else:
590
    nic_count = 0
591

    
592
  env["INSTANCE_NIC_COUNT"] = nic_count
593

    
594
  if disks:
595
    disk_count = len(disks)
596
    for idx, (size, mode) in enumerate(disks):
597
      env["INSTANCE_DISK%d_SIZE" % idx] = size
598
      env["INSTANCE_DISK%d_MODE" % idx] = mode
599
  else:
600
    disk_count = 0
601

    
602
  env["INSTANCE_DISK_COUNT"] = disk_count
603

    
604
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
605
    for key, value in source.items():
606
      env["INSTANCE_%s_%s" % (kind, key)] = value
607

    
608
  return env
609

    
610

    
611
def _NICListToTuple(lu, nics):
612
  """Build a list of nic information tuples.
613

614
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
615
  value in LUQueryInstanceData.
616

617
  @type lu:  L{LogicalUnit}
618
  @param lu: the logical unit on whose behalf we execute
619
  @type nics: list of L{objects.NIC}
620
  @param nics: list of nics to convert to hooks tuples
621

622
  """
623
  hooks_nics = []
624
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
625
  for nic in nics:
626
    ip = nic.ip
627
    mac = nic.mac
628
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
629
    mode = filled_params[constants.NIC_MODE]
630
    link = filled_params[constants.NIC_LINK]
631
    hooks_nics.append((ip, mac, mode, link))
632
  return hooks_nics
633

    
634

    
635
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
636
  """Builds instance related env variables for hooks from an object.
637

638
  @type lu: L{LogicalUnit}
639
  @param lu: the logical unit on whose behalf we execute
640
  @type instance: L{objects.Instance}
641
  @param instance: the instance for which we should build the
642
      environment
643
  @type override: dict
644
  @param override: dictionary with key/values that will override
645
      our values
646
  @rtype: dict
647
  @return: the hook environment dictionary
648

649
  """
650
  cluster = lu.cfg.GetClusterInfo()
651
  bep = cluster.FillBE(instance)
652
  hvp = cluster.FillHV(instance)
653
  args = {
654
    'name': instance.name,
655
    'primary_node': instance.primary_node,
656
    'secondary_nodes': instance.secondary_nodes,
657
    'os_type': instance.os,
658
    'status': instance.admin_up,
659
    'memory': bep[constants.BE_MEMORY],
660
    'vcpus': bep[constants.BE_VCPUS],
661
    'nics': _NICListToTuple(lu, instance.nics),
662
    'disk_template': instance.disk_template,
663
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
664
    'bep': bep,
665
    'hvp': hvp,
666
    'hypervisor_name': instance.hypervisor,
667
  }
668
  if override:
669
    args.update(override)
670
  return _BuildInstanceHookEnv(**args)
671

    
672

    
673
def _AdjustCandidatePool(lu):
674
  """Adjust the candidate pool after node operations.
675

676
  """
677
  mod_list = lu.cfg.MaintainCandidatePool()
678
  if mod_list:
679
    lu.LogInfo("Promoted nodes to master candidate role: %s",
680
               ", ".join(node.name for node in mod_list))
681
    for name in mod_list:
682
      lu.context.ReaddNode(name)
683
  mc_now, mc_max = lu.cfg.GetMasterCandidateStats()
684
  if mc_now > mc_max:
685
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
686
               (mc_now, mc_max))
687

    
688

    
689
def _CheckNicsBridgesExist(lu, target_nics, target_node,
690
                               profile=constants.PP_DEFAULT):
691
  """Check that the brigdes needed by a list of nics exist.
692

693
  """
694
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
695
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
696
                for nic in target_nics]
697
  brlist = [params[constants.NIC_LINK] for params in paramslist
698
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
699
  if brlist:
700
    result = lu.rpc.call_bridges_exist(target_node, brlist)
701
    result.Raise("Error checking bridges on destination node '%s'" %
702
                 target_node, prereq=True)
703

    
704

    
705
def _CheckInstanceBridgesExist(lu, instance, node=None):
706
  """Check that the brigdes needed by an instance exist.
707

708
  """
709
  if node is None:
710
    node = instance.primary_node
711
  _CheckNicsBridgesExist(lu, instance.nics, node)
712

    
713

    
714
def _GetNodePrimaryInstances(cfg, node_name):
715
  """Returns primary instances on a node.
716

717
  """
718
  instances = []
719

    
720
  for (_, inst) in cfg.GetAllInstancesInfo().iteritems():
721
    if node_name == inst.primary_node:
722
      instances.append(inst)
723

    
724
  return instances
725

    
726

    
727
def _GetNodeSecondaryInstances(cfg, node_name):
728
  """Returns secondary instances on a node.
729

730
  """
731
  instances = []
732

    
733
  for (_, inst) in cfg.GetAllInstancesInfo().iteritems():
734
    if node_name in inst.secondary_nodes:
735
      instances.append(inst)
736

    
737
  return instances
738

    
739

    
740
def _GetStorageTypeArgs(cfg, storage_type):
741
  """Returns the arguments for a storage type.
742

743
  """
744
  # Special case for file storage
745
  if storage_type == constants.ST_FILE:
746
    # storage.FileStorage wants a list of storage directories
747
    return [[cfg.GetFileStorageDir()]]
748

    
749
  return []
750

    
751

    
752
class LUPostInitCluster(LogicalUnit):
753
  """Logical unit for running hooks after cluster initialization.
754

755
  """
756
  HPATH = "cluster-init"
757
  HTYPE = constants.HTYPE_CLUSTER
758
  _OP_REQP = []
759

    
760
  def BuildHooksEnv(self):
761
    """Build hooks env.
762

763
    """
764
    env = {"OP_TARGET": self.cfg.GetClusterName()}
765
    mn = self.cfg.GetMasterNode()
766
    return env, [], [mn]
767

    
768
  def CheckPrereq(self):
769
    """No prerequisites to check.
770

771
    """
772
    return True
773

    
774
  def Exec(self, feedback_fn):
775
    """Nothing to do.
776

777
    """
778
    return True
779

    
780

    
781
class LUDestroyCluster(NoHooksLU):
782
  """Logical unit for destroying the cluster.
783

784
  """
785
  _OP_REQP = []
786

    
787
  def CheckPrereq(self):
788
    """Check prerequisites.
789

790
    This checks whether the cluster is empty.
791

792
    Any errors are signaled by raising errors.OpPrereqError.
793

794
    """
795
    master = self.cfg.GetMasterNode()
796

    
797
    nodelist = self.cfg.GetNodeList()
798
    if len(nodelist) != 1 or nodelist[0] != master:
799
      raise errors.OpPrereqError("There are still %d node(s) in"
800
                                 " this cluster." % (len(nodelist) - 1))
801
    instancelist = self.cfg.GetInstanceList()
802
    if instancelist:
803
      raise errors.OpPrereqError("There are still %d instance(s) in"
804
                                 " this cluster." % len(instancelist))
805

    
806
  def Exec(self, feedback_fn):
807
    """Destroys the cluster.
808

809
    """
810
    master = self.cfg.GetMasterNode()
811
    result = self.rpc.call_node_stop_master(master, False)
812
    result.Raise("Could not disable the master role")
813
    priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
814
    utils.CreateBackup(priv_key)
815
    utils.CreateBackup(pub_key)
816
    return master
817

    
818

    
819
class LUVerifyCluster(LogicalUnit):
820
  """Verifies the cluster status.
821

822
  """
823
  HPATH = "cluster-verify"
824
  HTYPE = constants.HTYPE_CLUSTER
825
  _OP_REQP = ["skip_checks"]
826
  REQ_BGL = False
827

    
828
  def ExpandNames(self):
829
    self.needed_locks = {
830
      locking.LEVEL_NODE: locking.ALL_SET,
831
      locking.LEVEL_INSTANCE: locking.ALL_SET,
832
    }
833
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
834

    
835
  def _VerifyNode(self, nodeinfo, file_list, local_cksum,
836
                  node_result, feedback_fn, master_files,
837
                  drbd_map, vg_name):
838
    """Run multiple tests against a node.
839

840
    Test list:
841

842
      - compares ganeti version
843
      - checks vg existence and size > 20G
844
      - checks config file checksum
845
      - checks ssh to other nodes
846

847
    @type nodeinfo: L{objects.Node}
848
    @param nodeinfo: the node to check
849
    @param file_list: required list of files
850
    @param local_cksum: dictionary of local files and their checksums
851
    @param node_result: the results from the node
852
    @param feedback_fn: function used to accumulate results
853
    @param master_files: list of files that only masters should have
854
    @param drbd_map: the useddrbd minors for this node, in
855
        form of minor: (instance, must_exist) which correspond to instances
856
        and their running status
857
    @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
858

859
    """
860
    node = nodeinfo.name
861

    
862
    # main result, node_result should be a non-empty dict
863
    if not node_result or not isinstance(node_result, dict):
864
      feedback_fn("  - ERROR: unable to verify node %s." % (node,))
865
      return True
866

    
867
    # compares ganeti version
868
    local_version = constants.PROTOCOL_VERSION
869
    remote_version = node_result.get('version', None)
870
    if not (remote_version and isinstance(remote_version, (list, tuple)) and
871
            len(remote_version) == 2):
872
      feedback_fn("  - ERROR: connection to %s failed" % (node))
873
      return True
874

    
875
    if local_version != remote_version[0]:
876
      feedback_fn("  - ERROR: incompatible protocol versions: master %s,"
877
                  " node %s %s" % (local_version, node, remote_version[0]))
878
      return True
879

    
880
    # node seems compatible, we can actually try to look into its results
881

    
882
    bad = False
883

    
884
    # full package version
885
    if constants.RELEASE_VERSION != remote_version[1]:
886
      feedback_fn("  - WARNING: software version mismatch: master %s,"
887
                  " node %s %s" %
888
                  (constants.RELEASE_VERSION, node, remote_version[1]))
889

    
890
    # checks vg existence and size > 20G
891
    if vg_name is not None:
892
      vglist = node_result.get(constants.NV_VGLIST, None)
893
      if not vglist:
894
        feedback_fn("  - ERROR: unable to check volume groups on node %s." %
895
                        (node,))
896
        bad = True
897
      else:
898
        vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
899
                                              constants.MIN_VG_SIZE)
900
        if vgstatus:
901
          feedback_fn("  - ERROR: %s on node %s" % (vgstatus, node))
902
          bad = True
903

    
904
    # checks config file checksum
905

    
906
    remote_cksum = node_result.get(constants.NV_FILELIST, None)
907
    if not isinstance(remote_cksum, dict):
908
      bad = True
909
      feedback_fn("  - ERROR: node hasn't returned file checksum data")
910
    else:
911
      for file_name in file_list:
912
        node_is_mc = nodeinfo.master_candidate
913
        must_have_file = file_name not in master_files
914
        if file_name not in remote_cksum:
915
          if node_is_mc or must_have_file:
916
            bad = True
917
            feedback_fn("  - ERROR: file '%s' missing" % file_name)
918
        elif remote_cksum[file_name] != local_cksum[file_name]:
919
          if node_is_mc or must_have_file:
920
            bad = True
921
            feedback_fn("  - ERROR: file '%s' has wrong checksum" % file_name)
922
          else:
923
            # not candidate and this is not a must-have file
924
            bad = True
925
            feedback_fn("  - ERROR: file '%s' should not exist on non master"
926
                        " candidates (and the file is outdated)" % file_name)
927
        else:
928
          # all good, except non-master/non-must have combination
929
          if not node_is_mc and not must_have_file:
930
            feedback_fn("  - ERROR: file '%s' should not exist on non master"
931
                        " candidates" % file_name)
932

    
933
    # checks ssh to any
934

    
935
    if constants.NV_NODELIST not in node_result:
936
      bad = True
937
      feedback_fn("  - ERROR: node hasn't returned node ssh connectivity data")
938
    else:
939
      if node_result[constants.NV_NODELIST]:
940
        bad = True
941
        for node in node_result[constants.NV_NODELIST]:
942
          feedback_fn("  - ERROR: ssh communication with node '%s': %s" %
943
                          (node, node_result[constants.NV_NODELIST][node]))
944

    
945
    if constants.NV_NODENETTEST not in node_result:
946
      bad = True
947
      feedback_fn("  - ERROR: node hasn't returned node tcp connectivity data")
948
    else:
949
      if node_result[constants.NV_NODENETTEST]:
950
        bad = True
951
        nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
952
        for node in nlist:
953
          feedback_fn("  - ERROR: tcp communication with node '%s': %s" %
954
                          (node, node_result[constants.NV_NODENETTEST][node]))
955

    
956
    hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
957
    if isinstance(hyp_result, dict):
958
      for hv_name, hv_result in hyp_result.iteritems():
959
        if hv_result is not None:
960
          feedback_fn("  - ERROR: hypervisor %s verify failure: '%s'" %
961
                      (hv_name, hv_result))
962

    
963
    # check used drbd list
964
    if vg_name is not None:
965
      used_minors = node_result.get(constants.NV_DRBDLIST, [])
966
      if not isinstance(used_minors, (tuple, list)):
967
        feedback_fn("  - ERROR: cannot parse drbd status file: %s" %
968
                    str(used_minors))
969
      else:
970
        for minor, (iname, must_exist) in drbd_map.items():
971
          if minor not in used_minors and must_exist:
972
            feedback_fn("  - ERROR: drbd minor %d of instance %s is"
973
                        " not active" % (minor, iname))
974
            bad = True
975
        for minor in used_minors:
976
          if minor not in drbd_map:
977
            feedback_fn("  - ERROR: unallocated drbd minor %d is in use" %
978
                        minor)
979
            bad = True
980

    
981
    return bad
982

    
983
  def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
984
                      node_instance, feedback_fn, n_offline):
985
    """Verify an instance.
986

987
    This function checks to see if the required block devices are
988
    available on the instance's node.
989

990
    """
991
    bad = False
992

    
993
    node_current = instanceconfig.primary_node
994

    
995
    node_vol_should = {}
996
    instanceconfig.MapLVsByNode(node_vol_should)
997

    
998
    for node in node_vol_should:
999
      if node in n_offline:
1000
        # ignore missing volumes on offline nodes
1001
        continue
1002
      for volume in node_vol_should[node]:
1003
        if node not in node_vol_is or volume not in node_vol_is[node]:
1004
          feedback_fn("  - ERROR: volume %s missing on node %s" %
1005
                          (volume, node))
1006
          bad = True
1007

    
1008
    if instanceconfig.admin_up:
1009
      if ((node_current not in node_instance or
1010
          not instance in node_instance[node_current]) and
1011
          node_current not in n_offline):
1012
        feedback_fn("  - ERROR: instance %s not running on node %s" %
1013
                        (instance, node_current))
1014
        bad = True
1015

    
1016
    for node in node_instance:
1017
      if (not node == node_current):
1018
        if instance in node_instance[node]:
1019
          feedback_fn("  - ERROR: instance %s should not run on node %s" %
1020
                          (instance, node))
1021
          bad = True
1022

    
1023
    return bad
1024

    
1025
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is, feedback_fn):
1026
    """Verify if there are any unknown volumes in the cluster.
1027

1028
    The .os, .swap and backup volumes are ignored. All other volumes are
1029
    reported as unknown.
1030

1031
    """
1032
    bad = False
1033

    
1034
    for node in node_vol_is:
1035
      for volume in node_vol_is[node]:
1036
        if node not in node_vol_should or volume not in node_vol_should[node]:
1037
          feedback_fn("  - ERROR: volume %s on node %s should not exist" %
1038
                      (volume, node))
1039
          bad = True
1040
    return bad
1041

    
1042
  def _VerifyOrphanInstances(self, instancelist, node_instance, feedback_fn):
1043
    """Verify the list of running instances.
1044

1045
    This checks what instances are running but unknown to the cluster.
1046

1047
    """
1048
    bad = False
1049
    for node in node_instance:
1050
      for runninginstance in node_instance[node]:
1051
        if runninginstance not in instancelist:
1052
          feedback_fn("  - ERROR: instance %s on node %s should not exist" %
1053
                          (runninginstance, node))
1054
          bad = True
1055
    return bad
1056

    
1057
  def _VerifyNPlusOneMemory(self, node_info, instance_cfg, feedback_fn):
1058
    """Verify N+1 Memory Resilience.
1059

1060
    Check that if one single node dies we can still start all the instances it
1061
    was primary for.
1062

1063
    """
1064
    bad = False
1065

    
1066
    for node, nodeinfo in node_info.iteritems():
1067
      # This code checks that every node which is now listed as secondary has
1068
      # enough memory to host all instances it is supposed to should a single
1069
      # other node in the cluster fail.
1070
      # FIXME: not ready for failover to an arbitrary node
1071
      # FIXME: does not support file-backed instances
1072
      # WARNING: we currently take into account down instances as well as up
1073
      # ones, considering that even if they're down someone might want to start
1074
      # them even in the event of a node failure.
1075
      for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
1076
        needed_mem = 0
1077
        for instance in instances:
1078
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1079
          if bep[constants.BE_AUTO_BALANCE]:
1080
            needed_mem += bep[constants.BE_MEMORY]
1081
        if nodeinfo['mfree'] < needed_mem:
1082
          feedback_fn("  - ERROR: not enough memory on node %s to accommodate"
1083
                      " failovers should node %s fail" % (node, prinode))
1084
          bad = True
1085
    return bad
1086

    
1087
  def CheckPrereq(self):
1088
    """Check prerequisites.
1089

1090
    Transform the list of checks we're going to skip into a set and check that
1091
    all its members are valid.
1092

1093
    """
1094
    self.skip_set = frozenset(self.op.skip_checks)
1095
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1096
      raise errors.OpPrereqError("Invalid checks to be skipped specified")
1097

    
1098
  def BuildHooksEnv(self):
1099
    """Build hooks env.
1100

1101
    Cluster-Verify hooks just ran in the post phase and their failure makes
1102
    the output be logged in the verify output and the verification to fail.
1103

1104
    """
1105
    all_nodes = self.cfg.GetNodeList()
1106
    env = {
1107
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1108
      }
1109
    for node in self.cfg.GetAllNodesInfo().values():
1110
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1111

    
1112
    return env, [], all_nodes
1113

    
1114
  def Exec(self, feedback_fn):
1115
    """Verify integrity of cluster, performing various test on nodes.
1116

1117
    """
1118
    bad = False
1119
    feedback_fn("* Verifying global settings")
1120
    for msg in self.cfg.VerifyConfig():
1121
      feedback_fn("  - ERROR: %s" % msg)
1122

    
1123
    vg_name = self.cfg.GetVGName()
1124
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1125
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1126
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1127
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1128
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1129
                        for iname in instancelist)
1130
    i_non_redundant = [] # Non redundant instances
1131
    i_non_a_balanced = [] # Non auto-balanced instances
1132
    n_offline = [] # List of offline nodes
1133
    n_drained = [] # List of nodes being drained
1134
    node_volume = {}
1135
    node_instance = {}
1136
    node_info = {}
1137
    instance_cfg = {}
1138

    
1139
    # FIXME: verify OS list
1140
    # do local checksums
1141
    master_files = [constants.CLUSTER_CONF_FILE]
1142

    
1143
    file_names = ssconf.SimpleStore().GetFileList()
1144
    file_names.append(constants.SSL_CERT_FILE)
1145
    file_names.append(constants.RAPI_CERT_FILE)
1146
    file_names.extend(master_files)
1147

    
1148
    local_checksums = utils.FingerprintFiles(file_names)
1149

    
1150
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1151
    node_verify_param = {
1152
      constants.NV_FILELIST: file_names,
1153
      constants.NV_NODELIST: [node.name for node in nodeinfo
1154
                              if not node.offline],
1155
      constants.NV_HYPERVISOR: hypervisors,
1156
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1157
                                  node.secondary_ip) for node in nodeinfo
1158
                                 if not node.offline],
1159
      constants.NV_INSTANCELIST: hypervisors,
1160
      constants.NV_VERSION: None,
1161
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1162
      }
1163
    if vg_name is not None:
1164
      node_verify_param[constants.NV_VGLIST] = None
1165
      node_verify_param[constants.NV_LVLIST] = vg_name
1166
      node_verify_param[constants.NV_DRBDLIST] = None
1167
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1168
                                           self.cfg.GetClusterName())
1169

    
1170
    cluster = self.cfg.GetClusterInfo()
1171
    master_node = self.cfg.GetMasterNode()
1172
    all_drbd_map = self.cfg.ComputeDRBDMap()
1173

    
1174
    for node_i in nodeinfo:
1175
      node = node_i.name
1176

    
1177
      if node_i.offline:
1178
        feedback_fn("* Skipping offline node %s" % (node,))
1179
        n_offline.append(node)
1180
        continue
1181

    
1182
      if node == master_node:
1183
        ntype = "master"
1184
      elif node_i.master_candidate:
1185
        ntype = "master candidate"
1186
      elif node_i.drained:
1187
        ntype = "drained"
1188
        n_drained.append(node)
1189
      else:
1190
        ntype = "regular"
1191
      feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1192

    
1193
      msg = all_nvinfo[node].fail_msg
1194
      if msg:
1195
        feedback_fn("  - ERROR: while contacting node %s: %s" % (node, msg))
1196
        bad = True
1197
        continue
1198

    
1199
      nresult = all_nvinfo[node].payload
1200
      node_drbd = {}
1201
      for minor, instance in all_drbd_map[node].items():
1202
        if instance not in instanceinfo:
1203
          feedback_fn("  - ERROR: ghost instance '%s' in temporary DRBD map" %
1204
                      instance)
1205
          # ghost instance should not be running, but otherwise we
1206
          # don't give double warnings (both ghost instance and
1207
          # unallocated minor in use)
1208
          node_drbd[minor] = (instance, False)
1209
        else:
1210
          instance = instanceinfo[instance]
1211
          node_drbd[minor] = (instance.name, instance.admin_up)
1212
      result = self._VerifyNode(node_i, file_names, local_checksums,
1213
                                nresult, feedback_fn, master_files,
1214
                                node_drbd, vg_name)
1215
      bad = bad or result
1216

    
1217
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1218
      if vg_name is None:
1219
        node_volume[node] = {}
1220
      elif isinstance(lvdata, basestring):
1221
        feedback_fn("  - ERROR: LVM problem on node %s: %s" %
1222
                    (node, utils.SafeEncode(lvdata)))
1223
        bad = True
1224
        node_volume[node] = {}
1225
      elif not isinstance(lvdata, dict):
1226
        feedback_fn("  - ERROR: connection to %s failed (lvlist)" % (node,))
1227
        bad = True
1228
        continue
1229
      else:
1230
        node_volume[node] = lvdata
1231

    
1232
      # node_instance
1233
      idata = nresult.get(constants.NV_INSTANCELIST, None)
1234
      if not isinstance(idata, list):
1235
        feedback_fn("  - ERROR: connection to %s failed (instancelist)" %
1236
                    (node,))
1237
        bad = True
1238
        continue
1239

    
1240
      node_instance[node] = idata
1241

    
1242
      # node_info
1243
      nodeinfo = nresult.get(constants.NV_HVINFO, None)
1244
      if not isinstance(nodeinfo, dict):
1245
        feedback_fn("  - ERROR: connection to %s failed (hvinfo)" % (node,))
1246
        bad = True
1247
        continue
1248

    
1249
      try:
1250
        node_info[node] = {
1251
          "mfree": int(nodeinfo['memory_free']),
1252
          "pinst": [],
1253
          "sinst": [],
1254
          # dictionary holding all instances this node is secondary for,
1255
          # grouped by their primary node. Each key is a cluster node, and each
1256
          # value is a list of instances which have the key as primary and the
1257
          # current node as secondary.  this is handy to calculate N+1 memory
1258
          # availability if you can only failover from a primary to its
1259
          # secondary.
1260
          "sinst-by-pnode": {},
1261
        }
1262
        # FIXME: devise a free space model for file based instances as well
1263
        if vg_name is not None:
1264
          if (constants.NV_VGLIST not in nresult or
1265
              vg_name not in nresult[constants.NV_VGLIST]):
1266
            feedback_fn("  - ERROR: node %s didn't return data for the"
1267
                        " volume group '%s' - it is either missing or broken" %
1268
                        (node, vg_name))
1269
            bad = True
1270
            continue
1271
          node_info[node]["dfree"] = int(nresult[constants.NV_VGLIST][vg_name])
1272
      except (ValueError, KeyError):
1273
        feedback_fn("  - ERROR: invalid nodeinfo value returned"
1274
                    " from node %s" % (node,))
1275
        bad = True
1276
        continue
1277

    
1278
    node_vol_should = {}
1279

    
1280
    for instance in instancelist:
1281
      feedback_fn("* Verifying instance %s" % instance)
1282
      inst_config = instanceinfo[instance]
1283
      result =  self._VerifyInstance(instance, inst_config, node_volume,
1284
                                     node_instance, feedback_fn, n_offline)
1285
      bad = bad or result
1286
      inst_nodes_offline = []
1287

    
1288
      inst_config.MapLVsByNode(node_vol_should)
1289

    
1290
      instance_cfg[instance] = inst_config
1291

    
1292
      pnode = inst_config.primary_node
1293
      if pnode in node_info:
1294
        node_info[pnode]['pinst'].append(instance)
1295
      elif pnode not in n_offline:
1296
        feedback_fn("  - ERROR: instance %s, connection to primary node"
1297
                    " %s failed" % (instance, pnode))
1298
        bad = True
1299

    
1300
      if pnode in n_offline:
1301
        inst_nodes_offline.append(pnode)
1302

    
1303
      # If the instance is non-redundant we cannot survive losing its primary
1304
      # node, so we are not N+1 compliant. On the other hand we have no disk
1305
      # templates with more than one secondary so that situation is not well
1306
      # supported either.
1307
      # FIXME: does not support file-backed instances
1308
      if len(inst_config.secondary_nodes) == 0:
1309
        i_non_redundant.append(instance)
1310
      elif len(inst_config.secondary_nodes) > 1:
1311
        feedback_fn("  - WARNING: multiple secondaries for instance %s"
1312
                    % instance)
1313

    
1314
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1315
        i_non_a_balanced.append(instance)
1316

    
1317
      for snode in inst_config.secondary_nodes:
1318
        if snode in node_info:
1319
          node_info[snode]['sinst'].append(instance)
1320
          if pnode not in node_info[snode]['sinst-by-pnode']:
1321
            node_info[snode]['sinst-by-pnode'][pnode] = []
1322
          node_info[snode]['sinst-by-pnode'][pnode].append(instance)
1323
        elif snode not in n_offline:
1324
          feedback_fn("  - ERROR: instance %s, connection to secondary node"
1325
                      " %s failed" % (instance, snode))
1326
          bad = True
1327
        if snode in n_offline:
1328
          inst_nodes_offline.append(snode)
1329

    
1330
      if inst_nodes_offline:
1331
        # warn that the instance lives on offline nodes, and set bad=True
1332
        feedback_fn("  - ERROR: instance lives on offline node(s) %s" %
1333
                    ", ".join(inst_nodes_offline))
1334
        bad = True
1335

    
1336
    feedback_fn("* Verifying orphan volumes")
1337
    result = self._VerifyOrphanVolumes(node_vol_should, node_volume,
1338
                                       feedback_fn)
1339
    bad = bad or result
1340

    
1341
    feedback_fn("* Verifying remaining instances")
1342
    result = self._VerifyOrphanInstances(instancelist, node_instance,
1343
                                         feedback_fn)
1344
    bad = bad or result
1345

    
1346
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1347
      feedback_fn("* Verifying N+1 Memory redundancy")
1348
      result = self._VerifyNPlusOneMemory(node_info, instance_cfg, feedback_fn)
1349
      bad = bad or result
1350

    
1351
    feedback_fn("* Other Notes")
1352
    if i_non_redundant:
1353
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1354
                  % len(i_non_redundant))
1355

    
1356
    if i_non_a_balanced:
1357
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1358
                  % len(i_non_a_balanced))
1359

    
1360
    if n_offline:
1361
      feedback_fn("  - NOTICE: %d offline node(s) found." % len(n_offline))
1362

    
1363
    if n_drained:
1364
      feedback_fn("  - NOTICE: %d drained node(s) found." % len(n_drained))
1365

    
1366
    return not bad
1367

    
1368
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1369
    """Analyze the post-hooks' result
1370

1371
    This method analyses the hook result, handles it, and sends some
1372
    nicely-formatted feedback back to the user.
1373

1374
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1375
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1376
    @param hooks_results: the results of the multi-node hooks rpc call
1377
    @param feedback_fn: function used send feedback back to the caller
1378
    @param lu_result: previous Exec result
1379
    @return: the new Exec result, based on the previous result
1380
        and hook results
1381

1382
    """
1383
    # We only really run POST phase hooks, and are only interested in
1384
    # their results
1385
    if phase == constants.HOOKS_PHASE_POST:
1386
      # Used to change hooks' output to proper indentation
1387
      indent_re = re.compile('^', re.M)
1388
      feedback_fn("* Hooks Results")
1389
      if not hooks_results:
1390
        feedback_fn("  - ERROR: general communication failure")
1391
        lu_result = 1
1392
      else:
1393
        for node_name in hooks_results:
1394
          show_node_header = True
1395
          res = hooks_results[node_name]
1396
          msg = res.fail_msg
1397
          if msg:
1398
            if res.offline:
1399
              # no need to warn or set fail return value
1400
              continue
1401
            feedback_fn("    Communication failure in hooks execution: %s" %
1402
                        msg)
1403
            lu_result = 1
1404
            continue
1405
          for script, hkr, output in res.payload:
1406
            if hkr == constants.HKR_FAIL:
1407
              # The node header is only shown once, if there are
1408
              # failing hooks on that node
1409
              if show_node_header:
1410
                feedback_fn("  Node %s:" % node_name)
1411
                show_node_header = False
1412
              feedback_fn("    ERROR: Script %s failed, output:" % script)
1413
              output = indent_re.sub('      ', output)
1414
              feedback_fn("%s" % output)
1415
              lu_result = 1
1416

    
1417
      return lu_result
1418

    
1419

    
1420
class LUVerifyDisks(NoHooksLU):
1421
  """Verifies the cluster disks status.
1422

1423
  """
1424
  _OP_REQP = []
1425
  REQ_BGL = False
1426

    
1427
  def ExpandNames(self):
1428
    self.needed_locks = {
1429
      locking.LEVEL_NODE: locking.ALL_SET,
1430
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1431
    }
1432
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1433

    
1434
  def CheckPrereq(self):
1435
    """Check prerequisites.
1436

1437
    This has no prerequisites.
1438

1439
    """
1440
    pass
1441

    
1442
  def Exec(self, feedback_fn):
1443
    """Verify integrity of cluster disks.
1444

1445
    @rtype: tuple of three items
1446
    @return: a tuple of (dict of node-to-node_error, list of instances
1447
        which need activate-disks, dict of instance: (node, volume) for
1448
        missing volumes
1449

1450
    """
1451
    result = res_nodes, res_instances, res_missing = {}, [], {}
1452

    
1453
    vg_name = self.cfg.GetVGName()
1454
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1455
    instances = [self.cfg.GetInstanceInfo(name)
1456
                 for name in self.cfg.GetInstanceList()]
1457

    
1458
    nv_dict = {}
1459
    for inst in instances:
1460
      inst_lvs = {}
1461
      if (not inst.admin_up or
1462
          inst.disk_template not in constants.DTS_NET_MIRROR):
1463
        continue
1464
      inst.MapLVsByNode(inst_lvs)
1465
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1466
      for node, vol_list in inst_lvs.iteritems():
1467
        for vol in vol_list:
1468
          nv_dict[(node, vol)] = inst
1469

    
1470
    if not nv_dict:
1471
      return result
1472

    
1473
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1474

    
1475
    for node in nodes:
1476
      # node_volume
1477
      node_res = node_lvs[node]
1478
      if node_res.offline:
1479
        continue
1480
      msg = node_res.fail_msg
1481
      if msg:
1482
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1483
        res_nodes[node] = msg
1484
        continue
1485

    
1486
      lvs = node_res.payload
1487
      for lv_name, (_, lv_inactive, lv_online) in lvs.items():
1488
        inst = nv_dict.pop((node, lv_name), None)
1489
        if (not lv_online and inst is not None
1490
            and inst.name not in res_instances):
1491
          res_instances.append(inst.name)
1492

    
1493
    # any leftover items in nv_dict are missing LVs, let's arrange the
1494
    # data better
1495
    for key, inst in nv_dict.iteritems():
1496
      if inst.name not in res_missing:
1497
        res_missing[inst.name] = []
1498
      res_missing[inst.name].append(key)
1499

    
1500
    return result
1501

    
1502

    
1503
class LURepairDiskSizes(NoHooksLU):
1504
  """Verifies the cluster disks sizes.
1505

1506
  """
1507
  _OP_REQP = ["instances"]
1508
  REQ_BGL = False
1509

    
1510
  def ExpandNames(self):
1511

    
1512
    if not isinstance(self.op.instances, list):
1513
      raise errors.OpPrereqError("Invalid argument type 'instances'")
1514

    
1515
    if self.op.instances:
1516
      self.wanted_names = []
1517
      for name in self.op.instances:
1518
        full_name = self.cfg.ExpandInstanceName(name)
1519
        if full_name is None:
1520
          raise errors.OpPrereqError("Instance '%s' not known" % name)
1521
        self.wanted_names.append(full_name)
1522
      self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names
1523
      self.needed_locks = {
1524
        locking.LEVEL_NODE: [],
1525
        locking.LEVEL_INSTANCE: self.wanted_names,
1526
        }
1527
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1528
    else:
1529
      self.wanted_names = None
1530
      self.needed_locks = {
1531
        locking.LEVEL_NODE: locking.ALL_SET,
1532
        locking.LEVEL_INSTANCE: locking.ALL_SET,
1533
        }
1534
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1535

    
1536
  def DeclareLocks(self, level):
1537
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
1538
      self._LockInstancesNodes(primary_only=True)
1539

    
1540
  def CheckPrereq(self):
1541
    """Check prerequisites.
1542

1543
    This only checks the optional instance list against the existing names.
1544

1545
    """
1546
    if self.wanted_names is None:
1547
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
1548

    
1549
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
1550
                             in self.wanted_names]
1551

    
1552
  def Exec(self, feedback_fn):
1553
    """Verify the size of cluster disks.
1554

1555
    """
1556
    # TODO: check child disks too
1557
    # TODO: check differences in size between primary/secondary nodes
1558
    per_node_disks = {}
1559
    for instance in self.wanted_instances:
1560
      pnode = instance.primary_node
1561
      if pnode not in per_node_disks:
1562
        per_node_disks[pnode] = []
1563
      for idx, disk in enumerate(instance.disks):
1564
        per_node_disks[pnode].append((instance, idx, disk))
1565

    
1566
    changed = []
1567
    for node, dskl in per_node_disks.items():
1568
      result = self.rpc.call_blockdev_getsizes(node, [v[2] for v in dskl])
1569
      if result.failed:
1570
        self.LogWarning("Failure in blockdev_getsizes call to node"
1571
                        " %s, ignoring", node)
1572
        continue
1573
      if len(result.data) != len(dskl):
1574
        self.LogWarning("Invalid result from node %s, ignoring node results",
1575
                        node)
1576
        continue
1577
      for ((instance, idx, disk), size) in zip(dskl, result.data):
1578
        if size is None:
1579
          self.LogWarning("Disk %d of instance %s did not return size"
1580
                          " information, ignoring", idx, instance.name)
1581
          continue
1582
        if not isinstance(size, (int, long)):
1583
          self.LogWarning("Disk %d of instance %s did not return valid"
1584
                          " size information, ignoring", idx, instance.name)
1585
          continue
1586
        size = size >> 20
1587
        if size != disk.size:
1588
          self.LogInfo("Disk %d of instance %s has mismatched size,"
1589
                       " correcting: recorded %d, actual %d", idx,
1590
                       instance.name, disk.size, size)
1591
          disk.size = size
1592
          self.cfg.Update(instance)
1593
          changed.append((instance.name, idx, size))
1594
    return changed
1595

    
1596

    
1597
class LURenameCluster(LogicalUnit):
1598
  """Rename the cluster.
1599

1600
  """
1601
  HPATH = "cluster-rename"
1602
  HTYPE = constants.HTYPE_CLUSTER
1603
  _OP_REQP = ["name"]
1604

    
1605
  def BuildHooksEnv(self):
1606
    """Build hooks env.
1607

1608
    """
1609
    env = {
1610
      "OP_TARGET": self.cfg.GetClusterName(),
1611
      "NEW_NAME": self.op.name,
1612
      }
1613
    mn = self.cfg.GetMasterNode()
1614
    return env, [mn], [mn]
1615

    
1616
  def CheckPrereq(self):
1617
    """Verify that the passed name is a valid one.
1618

1619
    """
1620
    hostname = utils.HostInfo(self.op.name)
1621

    
1622
    new_name = hostname.name
1623
    self.ip = new_ip = hostname.ip
1624
    old_name = self.cfg.GetClusterName()
1625
    old_ip = self.cfg.GetMasterIP()
1626
    if new_name == old_name and new_ip == old_ip:
1627
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
1628
                                 " cluster has changed")
1629
    if new_ip != old_ip:
1630
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1631
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
1632
                                   " reachable on the network. Aborting." %
1633
                                   new_ip)
1634

    
1635
    self.op.name = new_name
1636

    
1637
  def Exec(self, feedback_fn):
1638
    """Rename the cluster.
1639

1640
    """
1641
    clustername = self.op.name
1642
    ip = self.ip
1643

    
1644
    # shutdown the master IP
1645
    master = self.cfg.GetMasterNode()
1646
    result = self.rpc.call_node_stop_master(master, False)
1647
    result.Raise("Could not disable the master role")
1648

    
1649
    try:
1650
      cluster = self.cfg.GetClusterInfo()
1651
      cluster.cluster_name = clustername
1652
      cluster.master_ip = ip
1653
      self.cfg.Update(cluster)
1654

    
1655
      # update the known hosts file
1656
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
1657
      node_list = self.cfg.GetNodeList()
1658
      try:
1659
        node_list.remove(master)
1660
      except ValueError:
1661
        pass
1662
      result = self.rpc.call_upload_file(node_list,
1663
                                         constants.SSH_KNOWN_HOSTS_FILE)
1664
      for to_node, to_result in result.iteritems():
1665
        msg = to_result.fail_msg
1666
        if msg:
1667
          msg = ("Copy of file %s to node %s failed: %s" %
1668
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
1669
          self.proc.LogWarning(msg)
1670

    
1671
    finally:
1672
      result = self.rpc.call_node_start_master(master, False, False)
1673
      msg = result.fail_msg
1674
      if msg:
1675
        self.LogWarning("Could not re-enable the master role on"
1676
                        " the master, please restart manually: %s", msg)
1677

    
1678

    
1679
def _RecursiveCheckIfLVMBased(disk):
1680
  """Check if the given disk or its children are lvm-based.
1681

1682
  @type disk: L{objects.Disk}
1683
  @param disk: the disk to check
1684
  @rtype: boolean
1685
  @return: boolean indicating whether a LD_LV dev_type was found or not
1686

1687
  """
1688
  if disk.children:
1689
    for chdisk in disk.children:
1690
      if _RecursiveCheckIfLVMBased(chdisk):
1691
        return True
1692
  return disk.dev_type == constants.LD_LV
1693

    
1694

    
1695
class LUSetClusterParams(LogicalUnit):
1696
  """Change the parameters of the cluster.
1697

1698
  """
1699
  HPATH = "cluster-modify"
1700
  HTYPE = constants.HTYPE_CLUSTER
1701
  _OP_REQP = []
1702
  REQ_BGL = False
1703

    
1704
  def CheckArguments(self):
1705
    """Check parameters
1706

1707
    """
1708
    if not hasattr(self.op, "candidate_pool_size"):
1709
      self.op.candidate_pool_size = None
1710
    if self.op.candidate_pool_size is not None:
1711
      try:
1712
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
1713
      except (ValueError, TypeError), err:
1714
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
1715
                                   str(err))
1716
      if self.op.candidate_pool_size < 1:
1717
        raise errors.OpPrereqError("At least one master candidate needed")
1718

    
1719
  def ExpandNames(self):
1720
    # FIXME: in the future maybe other cluster params won't require checking on
1721
    # all nodes to be modified.
1722
    self.needed_locks = {
1723
      locking.LEVEL_NODE: locking.ALL_SET,
1724
    }
1725
    self.share_locks[locking.LEVEL_NODE] = 1
1726

    
1727
  def BuildHooksEnv(self):
1728
    """Build hooks env.
1729

1730
    """
1731
    env = {
1732
      "OP_TARGET": self.cfg.GetClusterName(),
1733
      "NEW_VG_NAME": self.op.vg_name,
1734
      }
1735
    mn = self.cfg.GetMasterNode()
1736
    return env, [mn], [mn]
1737

    
1738
  def CheckPrereq(self):
1739
    """Check prerequisites.
1740

1741
    This checks whether the given params don't conflict and
1742
    if the given volume group is valid.
1743

1744
    """
1745
    if self.op.vg_name is not None and not self.op.vg_name:
1746
      instances = self.cfg.GetAllInstancesInfo().values()
1747
      for inst in instances:
1748
        for disk in inst.disks:
1749
          if _RecursiveCheckIfLVMBased(disk):
1750
            raise errors.OpPrereqError("Cannot disable lvm storage while"
1751
                                       " lvm-based instances exist")
1752

    
1753
    node_list = self.acquired_locks[locking.LEVEL_NODE]
1754

    
1755
    # if vg_name not None, checks given volume group on all nodes
1756
    if self.op.vg_name:
1757
      vglist = self.rpc.call_vg_list(node_list)
1758
      for node in node_list:
1759
        msg = vglist[node].fail_msg
1760
        if msg:
1761
          # ignoring down node
1762
          self.LogWarning("Error while gathering data on node %s"
1763
                          " (ignoring node): %s", node, msg)
1764
          continue
1765
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
1766
                                              self.op.vg_name,
1767
                                              constants.MIN_VG_SIZE)
1768
        if vgstatus:
1769
          raise errors.OpPrereqError("Error on node '%s': %s" %
1770
                                     (node, vgstatus))
1771

    
1772
    self.cluster = cluster = self.cfg.GetClusterInfo()
1773
    # validate params changes
1774
    if self.op.beparams:
1775
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
1776
      self.new_beparams = objects.FillDict(
1777
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
1778

    
1779
    if self.op.nicparams:
1780
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
1781
      self.new_nicparams = objects.FillDict(
1782
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
1783
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
1784

    
1785
    # hypervisor list/parameters
1786
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
1787
    if self.op.hvparams:
1788
      if not isinstance(self.op.hvparams, dict):
1789
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input")
1790
      for hv_name, hv_dict in self.op.hvparams.items():
1791
        if hv_name not in self.new_hvparams:
1792
          self.new_hvparams[hv_name] = hv_dict
1793
        else:
1794
          self.new_hvparams[hv_name].update(hv_dict)
1795

    
1796
    if self.op.enabled_hypervisors is not None:
1797
      self.hv_list = self.op.enabled_hypervisors
1798
      if not self.hv_list:
1799
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
1800
                                   " least one member")
1801
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
1802
      if invalid_hvs:
1803
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
1804
                                   " entries: %s" % invalid_hvs)
1805
    else:
1806
      self.hv_list = cluster.enabled_hypervisors
1807

    
1808
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
1809
      # either the enabled list has changed, or the parameters have, validate
1810
      for hv_name, hv_params in self.new_hvparams.items():
1811
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
1812
            (self.op.enabled_hypervisors and
1813
             hv_name in self.op.enabled_hypervisors)):
1814
          # either this is a new hypervisor, or its parameters have changed
1815
          hv_class = hypervisor.GetHypervisor(hv_name)
1816
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
1817
          hv_class.CheckParameterSyntax(hv_params)
1818
          _CheckHVParams(self, node_list, hv_name, hv_params)
1819

    
1820
  def Exec(self, feedback_fn):
1821
    """Change the parameters of the cluster.
1822

1823
    """
1824
    if self.op.vg_name is not None:
1825
      new_volume = self.op.vg_name
1826
      if not new_volume:
1827
        new_volume = None
1828
      if new_volume != self.cfg.GetVGName():
1829
        self.cfg.SetVGName(new_volume)
1830
      else:
1831
        feedback_fn("Cluster LVM configuration already in desired"
1832
                    " state, not changing")
1833
    if self.op.hvparams:
1834
      self.cluster.hvparams = self.new_hvparams
1835
    if self.op.enabled_hypervisors is not None:
1836
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
1837
    if self.op.beparams:
1838
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
1839
    if self.op.nicparams:
1840
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
1841

    
1842
    if self.op.candidate_pool_size is not None:
1843
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
1844
      # we need to update the pool size here, otherwise the save will fail
1845
      _AdjustCandidatePool(self)
1846

    
1847
    self.cfg.Update(self.cluster)
1848

    
1849

    
1850
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
1851
  """Distribute additional files which are part of the cluster configuration.
1852

1853
  ConfigWriter takes care of distributing the config and ssconf files, but
1854
  there are more files which should be distributed to all nodes. This function
1855
  makes sure those are copied.
1856

1857
  @param lu: calling logical unit
1858
  @param additional_nodes: list of nodes not in the config to distribute to
1859

1860
  """
1861
  # 1. Gather target nodes
1862
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
1863
  dist_nodes = lu.cfg.GetNodeList()
1864
  if additional_nodes is not None:
1865
    dist_nodes.extend(additional_nodes)
1866
  if myself.name in dist_nodes:
1867
    dist_nodes.remove(myself.name)
1868
  # 2. Gather files to distribute
1869
  dist_files = set([constants.ETC_HOSTS,
1870
                    constants.SSH_KNOWN_HOSTS_FILE,
1871
                    constants.RAPI_CERT_FILE,
1872
                    constants.RAPI_USERS_FILE,
1873
                    constants.HMAC_CLUSTER_KEY,
1874
                   ])
1875

    
1876
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
1877
  for hv_name in enabled_hypervisors:
1878
    hv_class = hypervisor.GetHypervisor(hv_name)
1879
    dist_files.update(hv_class.GetAncillaryFiles())
1880

    
1881
  # 3. Perform the files upload
1882
  for fname in dist_files:
1883
    if os.path.exists(fname):
1884
      result = lu.rpc.call_upload_file(dist_nodes, fname)
1885
      for to_node, to_result in result.items():
1886
        msg = to_result.fail_msg
1887
        if msg:
1888
          msg = ("Copy of file %s to node %s failed: %s" %
1889
                 (fname, to_node, msg))
1890
          lu.proc.LogWarning(msg)
1891

    
1892

    
1893
class LURedistributeConfig(NoHooksLU):
1894
  """Force the redistribution of cluster configuration.
1895

1896
  This is a very simple LU.
1897

1898
  """
1899
  _OP_REQP = []
1900
  REQ_BGL = False
1901

    
1902
  def ExpandNames(self):
1903
    self.needed_locks = {
1904
      locking.LEVEL_NODE: locking.ALL_SET,
1905
    }
1906
    self.share_locks[locking.LEVEL_NODE] = 1
1907

    
1908
  def CheckPrereq(self):
1909
    """Check prerequisites.
1910

1911
    """
1912

    
1913
  def Exec(self, feedback_fn):
1914
    """Redistribute the configuration.
1915

1916
    """
1917
    self.cfg.Update(self.cfg.GetClusterInfo())
1918
    _RedistributeAncillaryFiles(self)
1919

    
1920

    
1921
def _WaitForSync(lu, instance, oneshot=False, unlock=False):
1922
  """Sleep and poll for an instance's disk to sync.
1923

1924
  """
1925
  if not instance.disks:
1926
    return True
1927

    
1928
  if not oneshot:
1929
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
1930

    
1931
  node = instance.primary_node
1932

    
1933
  for dev in instance.disks:
1934
    lu.cfg.SetDiskID(dev, node)
1935

    
1936
  retries = 0
1937
  degr_retries = 10 # in seconds, as we sleep 1 second each time
1938
  while True:
1939
    max_time = 0
1940
    done = True
1941
    cumul_degraded = False
1942
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
1943
    msg = rstats.fail_msg
1944
    if msg:
1945
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
1946
      retries += 1
1947
      if retries >= 10:
1948
        raise errors.RemoteError("Can't contact node %s for mirror data,"
1949
                                 " aborting." % node)
1950
      time.sleep(6)
1951
      continue
1952
    rstats = rstats.payload
1953
    retries = 0
1954
    for i, mstat in enumerate(rstats):
1955
      if mstat is None:
1956
        lu.LogWarning("Can't compute data for node %s/%s",
1957
                           node, instance.disks[i].iv_name)
1958
        continue
1959

    
1960
      cumul_degraded = (cumul_degraded or
1961
                        (mstat.is_degraded and mstat.sync_percent is None))
1962
      if mstat.sync_percent is not None:
1963
        done = False
1964
        if mstat.estimated_time is not None:
1965
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
1966
          max_time = mstat.estimated_time
1967
        else:
1968
          rem_time = "no time estimate"
1969
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
1970
                        (instance.disks[i].iv_name, mstat.sync_percent, rem_time))
1971

    
1972
    # if we're done but degraded, let's do a few small retries, to
1973
    # make sure we see a stable and not transient situation; therefore
1974
    # we force restart of the loop
1975
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
1976
      logging.info("Degraded disks found, %d retries left", degr_retries)
1977
      degr_retries -= 1
1978
      time.sleep(1)
1979
      continue
1980

    
1981
    if done or oneshot:
1982
      break
1983

    
1984
    time.sleep(min(60, max_time))
1985

    
1986
  if done:
1987
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
1988
  return not cumul_degraded
1989

    
1990

    
1991
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
1992
  """Check that mirrors are not degraded.
1993

1994
  The ldisk parameter, if True, will change the test from the
1995
  is_degraded attribute (which represents overall non-ok status for
1996
  the device(s)) to the ldisk (representing the local storage status).
1997

1998
  """
1999
  lu.cfg.SetDiskID(dev, node)
2000

    
2001
  result = True
2002

    
2003
  if on_primary or dev.AssembleOnSecondary():
2004
    rstats = lu.rpc.call_blockdev_find(node, dev)
2005
    msg = rstats.fail_msg
2006
    if msg:
2007
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2008
      result = False
2009
    elif not rstats.payload:
2010
      lu.LogWarning("Can't find disk on node %s", node)
2011
      result = False
2012
    else:
2013
      if ldisk:
2014
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2015
      else:
2016
        result = result and not rstats.payload.is_degraded
2017

    
2018
  if dev.children:
2019
    for child in dev.children:
2020
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2021

    
2022
  return result
2023

    
2024

    
2025
class LUDiagnoseOS(NoHooksLU):
2026
  """Logical unit for OS diagnose/query.
2027

2028
  """
2029
  _OP_REQP = ["output_fields", "names"]
2030
  REQ_BGL = False
2031
  _FIELDS_STATIC = utils.FieldSet()
2032
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status")
2033

    
2034
  def ExpandNames(self):
2035
    if self.op.names:
2036
      raise errors.OpPrereqError("Selective OS query not supported")
2037

    
2038
    _CheckOutputFields(static=self._FIELDS_STATIC,
2039
                       dynamic=self._FIELDS_DYNAMIC,
2040
                       selected=self.op.output_fields)
2041

    
2042
    # Lock all nodes, in shared mode
2043
    # Temporary removal of locks, should be reverted later
2044
    # TODO: reintroduce locks when they are lighter-weight
2045
    self.needed_locks = {}
2046
    #self.share_locks[locking.LEVEL_NODE] = 1
2047
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2048

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

2052
    """
2053

    
2054
  @staticmethod
2055
  def _DiagnoseByOS(node_list, rlist):
2056
    """Remaps a per-node return list into an a per-os per-node dictionary
2057

2058
    @param node_list: a list with the names of all nodes
2059
    @param rlist: a map with node names as keys and OS objects as values
2060

2061
    @rtype: dict
2062
    @return: a dictionary with osnames as keys and as value another map, with
2063
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2064

2065
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2066
                                     (/srv/..., False, "invalid api")],
2067
                           "node2": [(/srv/..., True, "")]}
2068
          }
2069

2070
    """
2071
    all_os = {}
2072
    # we build here the list of nodes that didn't fail the RPC (at RPC
2073
    # level), so that nodes with a non-responding node daemon don't
2074
    # make all OSes invalid
2075
    good_nodes = [node_name for node_name in rlist
2076
                  if not rlist[node_name].fail_msg]
2077
    for node_name, nr in rlist.items():
2078
      if nr.fail_msg or not nr.payload:
2079
        continue
2080
      for name, path, status, diagnose in nr.payload:
2081
        if name not in all_os:
2082
          # build a list of nodes for this os containing empty lists
2083
          # for each node in node_list
2084
          all_os[name] = {}
2085
          for nname in good_nodes:
2086
            all_os[name][nname] = []
2087
        all_os[name][node_name].append((path, status, diagnose))
2088
    return all_os
2089

    
2090
  def Exec(self, feedback_fn):
2091
    """Compute the list of OSes.
2092

2093
    """
2094
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2095
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2096
    pol = self._DiagnoseByOS(valid_nodes, node_data)
2097
    output = []
2098
    for os_name, os_data in pol.items():
2099
      row = []
2100
      for field in self.op.output_fields:
2101
        if field == "name":
2102
          val = os_name
2103
        elif field == "valid":
2104
          val = utils.all([osl and osl[0][1] for osl in os_data.values()])
2105
        elif field == "node_status":
2106
          # this is just a copy of the dict
2107
          val = {}
2108
          for node_name, nos_list in os_data.items():
2109
            val[node_name] = nos_list
2110
        else:
2111
          raise errors.ParameterError(field)
2112
        row.append(val)
2113
      output.append(row)
2114

    
2115
    return output
2116

    
2117

    
2118
class LURemoveNode(LogicalUnit):
2119
  """Logical unit for removing a node.
2120

2121
  """
2122
  HPATH = "node-remove"
2123
  HTYPE = constants.HTYPE_NODE
2124
  _OP_REQP = ["node_name"]
2125

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

2129
    This doesn't run on the target node in the pre phase as a failed
2130
    node would then be impossible to remove.
2131

2132
    """
2133
    env = {
2134
      "OP_TARGET": self.op.node_name,
2135
      "NODE_NAME": self.op.node_name,
2136
      }
2137
    all_nodes = self.cfg.GetNodeList()
2138
    all_nodes.remove(self.op.node_name)
2139
    return env, all_nodes, all_nodes
2140

    
2141
  def CheckPrereq(self):
2142
    """Check prerequisites.
2143

2144
    This checks:
2145
     - the node exists in the configuration
2146
     - it does not have primary or secondary instances
2147
     - it's not the master
2148

2149
    Any errors are signaled by raising errors.OpPrereqError.
2150

2151
    """
2152
    node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
2153
    if node is None:
2154
      raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name)
2155

    
2156
    instance_list = self.cfg.GetInstanceList()
2157

    
2158
    masternode = self.cfg.GetMasterNode()
2159
    if node.name == masternode:
2160
      raise errors.OpPrereqError("Node is the master node,"
2161
                                 " you need to failover first.")
2162

    
2163
    for instance_name in instance_list:
2164
      instance = self.cfg.GetInstanceInfo(instance_name)
2165
      if node.name in instance.all_nodes:
2166
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2167
                                   " please remove first." % instance_name)
2168
    self.op.node_name = node.name
2169
    self.node = node
2170

    
2171
  def Exec(self, feedback_fn):
2172
    """Removes the node from the cluster.
2173

2174
    """
2175
    node = self.node
2176
    logging.info("Stopping the node daemon and removing configs from node %s",
2177
                 node.name)
2178

    
2179
    self.context.RemoveNode(node.name)
2180

    
2181
    result = self.rpc.call_node_leave_cluster(node.name)
2182
    msg = result.fail_msg
2183
    if msg:
2184
      self.LogWarning("Errors encountered on the remote node while leaving"
2185
                      " the cluster: %s", msg)
2186

    
2187
    # Promote nodes to master candidate as needed
2188
    _AdjustCandidatePool(self)
2189

    
2190

    
2191
class LUQueryNodes(NoHooksLU):
2192
  """Logical unit for querying nodes.
2193

2194
  """
2195
  _OP_REQP = ["output_fields", "names", "use_locking"]
2196
  REQ_BGL = False
2197
  _FIELDS_DYNAMIC = utils.FieldSet(
2198
    "dtotal", "dfree",
2199
    "mtotal", "mnode", "mfree",
2200
    "bootid",
2201
    "ctotal", "cnodes", "csockets",
2202
    )
2203

    
2204
  _FIELDS_STATIC = utils.FieldSet(
2205
    "name", "pinst_cnt", "sinst_cnt",
2206
    "pinst_list", "sinst_list",
2207
    "pip", "sip", "tags",
2208
    "serial_no",
2209
    "master_candidate",
2210
    "master",
2211
    "offline",
2212
    "drained",
2213
    "role",
2214
    )
2215

    
2216
  def ExpandNames(self):
2217
    _CheckOutputFields(static=self._FIELDS_STATIC,
2218
                       dynamic=self._FIELDS_DYNAMIC,
2219
                       selected=self.op.output_fields)
2220

    
2221
    self.needed_locks = {}
2222
    self.share_locks[locking.LEVEL_NODE] = 1
2223

    
2224
    if self.op.names:
2225
      self.wanted = _GetWantedNodes(self, self.op.names)
2226
    else:
2227
      self.wanted = locking.ALL_SET
2228

    
2229
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2230
    self.do_locking = self.do_node_query and self.op.use_locking
2231
    if self.do_locking:
2232
      # if we don't request only static fields, we need to lock the nodes
2233
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2234

    
2235

    
2236
  def CheckPrereq(self):
2237
    """Check prerequisites.
2238

2239
    """
2240
    # The validation of the node list is done in the _GetWantedNodes,
2241
    # if non empty, and if empty, there's no validation to do
2242
    pass
2243

    
2244
  def Exec(self, feedback_fn):
2245
    """Computes the list of nodes and their attributes.
2246

2247
    """
2248
    all_info = self.cfg.GetAllNodesInfo()
2249
    if self.do_locking:
2250
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2251
    elif self.wanted != locking.ALL_SET:
2252
      nodenames = self.wanted
2253
      missing = set(nodenames).difference(all_info.keys())
2254
      if missing:
2255
        raise errors.OpExecError(
2256
          "Some nodes were removed before retrieving their data: %s" % missing)
2257
    else:
2258
      nodenames = all_info.keys()
2259

    
2260
    nodenames = utils.NiceSort(nodenames)
2261
    nodelist = [all_info[name] for name in nodenames]
2262

    
2263
    # begin data gathering
2264

    
2265
    if self.do_node_query:
2266
      live_data = {}
2267
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2268
                                          self.cfg.GetHypervisorType())
2269
      for name in nodenames:
2270
        nodeinfo = node_data[name]
2271
        if not nodeinfo.fail_msg and nodeinfo.payload:
2272
          nodeinfo = nodeinfo.payload
2273
          fn = utils.TryConvert
2274
          live_data[name] = {
2275
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2276
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2277
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2278
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2279
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2280
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2281
            "bootid": nodeinfo.get('bootid', None),
2282
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2283
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2284
            }
2285
        else:
2286
          live_data[name] = {}
2287
    else:
2288
      live_data = dict.fromkeys(nodenames, {})
2289

    
2290
    node_to_primary = dict([(name, set()) for name in nodenames])
2291
    node_to_secondary = dict([(name, set()) for name in nodenames])
2292

    
2293
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2294
                             "sinst_cnt", "sinst_list"))
2295
    if inst_fields & frozenset(self.op.output_fields):
2296
      instancelist = self.cfg.GetInstanceList()
2297

    
2298
      for instance_name in instancelist:
2299
        inst = self.cfg.GetInstanceInfo(instance_name)
2300
        if inst.primary_node in node_to_primary:
2301
          node_to_primary[inst.primary_node].add(inst.name)
2302
        for secnode in inst.secondary_nodes:
2303
          if secnode in node_to_secondary:
2304
            node_to_secondary[secnode].add(inst.name)
2305

    
2306
    master_node = self.cfg.GetMasterNode()
2307

    
2308
    # end data gathering
2309

    
2310
    output = []
2311
    for node in nodelist:
2312
      node_output = []
2313
      for field in self.op.output_fields:
2314
        if field == "name":
2315
          val = node.name
2316
        elif field == "pinst_list":
2317
          val = list(node_to_primary[node.name])
2318
        elif field == "sinst_list":
2319
          val = list(node_to_secondary[node.name])
2320
        elif field == "pinst_cnt":
2321
          val = len(node_to_primary[node.name])
2322
        elif field == "sinst_cnt":
2323
          val = len(node_to_secondary[node.name])
2324
        elif field == "pip":
2325
          val = node.primary_ip
2326
        elif field == "sip":
2327
          val = node.secondary_ip
2328
        elif field == "tags":
2329
          val = list(node.GetTags())
2330
        elif field == "serial_no":
2331
          val = node.serial_no
2332
        elif field == "master_candidate":
2333
          val = node.master_candidate
2334
        elif field == "master":
2335
          val = node.name == master_node
2336
        elif field == "offline":
2337
          val = node.offline
2338
        elif field == "drained":
2339
          val = node.drained
2340
        elif self._FIELDS_DYNAMIC.Matches(field):
2341
          val = live_data[node.name].get(field, None)
2342
        elif field == "role":
2343
          if node.name == master_node:
2344
            val = "M"
2345
          elif node.master_candidate:
2346
            val = "C"
2347
          elif node.drained:
2348
            val = "D"
2349
          elif node.offline:
2350
            val = "O"
2351
          else:
2352
            val = "R"
2353
        else:
2354
          raise errors.ParameterError(field)
2355
        node_output.append(val)
2356
      output.append(node_output)
2357

    
2358
    return output
2359

    
2360

    
2361
class LUQueryNodeVolumes(NoHooksLU):
2362
  """Logical unit for getting volumes on node(s).
2363

2364
  """
2365
  _OP_REQP = ["nodes", "output_fields"]
2366
  REQ_BGL = False
2367
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2368
  _FIELDS_STATIC = utils.FieldSet("node")
2369

    
2370
  def ExpandNames(self):
2371
    _CheckOutputFields(static=self._FIELDS_STATIC,
2372
                       dynamic=self._FIELDS_DYNAMIC,
2373
                       selected=self.op.output_fields)
2374

    
2375
    self.needed_locks = {}
2376
    self.share_locks[locking.LEVEL_NODE] = 1
2377
    if not self.op.nodes:
2378
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2379
    else:
2380
      self.needed_locks[locking.LEVEL_NODE] = \
2381
        _GetWantedNodes(self, self.op.nodes)
2382

    
2383
  def CheckPrereq(self):
2384
    """Check prerequisites.
2385

2386
    This checks that the fields required are valid output fields.
2387

2388
    """
2389
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2390

    
2391
  def Exec(self, feedback_fn):
2392
    """Computes the list of nodes and their attributes.
2393

2394
    """
2395
    nodenames = self.nodes
2396
    volumes = self.rpc.call_node_volumes(nodenames)
2397

    
2398
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
2399
             in self.cfg.GetInstanceList()]
2400

    
2401
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
2402

    
2403
    output = []
2404
    for node in nodenames:
2405
      nresult = volumes[node]
2406
      if nresult.offline:
2407
        continue
2408
      msg = nresult.fail_msg
2409
      if msg:
2410
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
2411
        continue
2412

    
2413
      node_vols = nresult.payload[:]
2414
      node_vols.sort(key=lambda vol: vol['dev'])
2415

    
2416
      for vol in node_vols:
2417
        node_output = []
2418
        for field in self.op.output_fields:
2419
          if field == "node":
2420
            val = node
2421
          elif field == "phys":
2422
            val = vol['dev']
2423
          elif field == "vg":
2424
            val = vol['vg']
2425
          elif field == "name":
2426
            val = vol['name']
2427
          elif field == "size":
2428
            val = int(float(vol['size']))
2429
          elif field == "instance":
2430
            for inst in ilist:
2431
              if node not in lv_by_node[inst]:
2432
                continue
2433
              if vol['name'] in lv_by_node[inst][node]:
2434
                val = inst.name
2435
                break
2436
            else:
2437
              val = '-'
2438
          else:
2439
            raise errors.ParameterError(field)
2440
          node_output.append(str(val))
2441

    
2442
        output.append(node_output)
2443

    
2444
    return output
2445

    
2446

    
2447
class LUQueryNodeStorage(NoHooksLU):
2448
  """Logical unit for getting information on storage units on node(s).
2449

2450
  """
2451
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
2452
  REQ_BGL = False
2453
  _FIELDS_STATIC = utils.FieldSet("node")
2454

    
2455
  def ExpandNames(self):
2456
    storage_type = self.op.storage_type
2457

    
2458
    if storage_type not in constants.VALID_STORAGE_FIELDS:
2459
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type)
2460

    
2461
    dynamic_fields = constants.VALID_STORAGE_FIELDS[storage_type]
2462

    
2463
    _CheckOutputFields(static=self._FIELDS_STATIC,
2464
                       dynamic=utils.FieldSet(*dynamic_fields),
2465
                       selected=self.op.output_fields)
2466

    
2467
    self.needed_locks = {}
2468
    self.share_locks[locking.LEVEL_NODE] = 1
2469

    
2470
    if self.op.nodes:
2471
      self.needed_locks[locking.LEVEL_NODE] = \
2472
        _GetWantedNodes(self, self.op.nodes)
2473
    else:
2474
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2475

    
2476
  def CheckPrereq(self):
2477
    """Check prerequisites.
2478

2479
    This checks that the fields required are valid output fields.
2480

2481
    """
2482
    self.op.name = getattr(self.op, "name", None)
2483

    
2484
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2485

    
2486
  def Exec(self, feedback_fn):
2487
    """Computes the list of nodes and their attributes.
2488

2489
    """
2490
    # Always get name to sort by
2491
    if constants.SF_NAME in self.op.output_fields:
2492
      fields = self.op.output_fields[:]
2493
    else:
2494
      fields = [constants.SF_NAME] + self.op.output_fields
2495

    
2496
    # Never ask for node as it's only known to the LU
2497
    while "node" in fields:
2498
      fields.remove("node")
2499

    
2500
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
2501
    name_idx = field_idx[constants.SF_NAME]
2502

    
2503
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2504
    data = self.rpc.call_storage_list(self.nodes,
2505
                                      self.op.storage_type, st_args,
2506
                                      self.op.name, fields)
2507

    
2508
    result = []
2509

    
2510
    for node in utils.NiceSort(self.nodes):
2511
      nresult = data[node]
2512
      if nresult.offline:
2513
        continue
2514

    
2515
      msg = nresult.fail_msg
2516
      if msg:
2517
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
2518
        continue
2519

    
2520
      rows = dict([(row[name_idx], row) for row in nresult.payload])
2521

    
2522
      for name in utils.NiceSort(rows.keys()):
2523
        row = rows[name]
2524

    
2525
        out = []
2526

    
2527
        for field in self.op.output_fields:
2528
          if field == "node":
2529
            val = node
2530
          elif field in field_idx:
2531
            val = row[field_idx[field]]
2532
          else:
2533
            raise errors.ParameterError(field)
2534

    
2535
          out.append(val)
2536

    
2537
        result.append(out)
2538

    
2539
    return result
2540

    
2541

    
2542
class LUModifyNodeStorage(NoHooksLU):
2543
  """Logical unit for modifying a storage volume on a node.
2544

2545
  """
2546
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
2547
  REQ_BGL = False
2548

    
2549
  def CheckArguments(self):
2550
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2551
    if node_name is None:
2552
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name)
2553

    
2554
    self.op.node_name = node_name
2555

    
2556
    storage_type = self.op.storage_type
2557
    if storage_type not in constants.VALID_STORAGE_FIELDS:
2558
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type)
2559

    
2560
  def ExpandNames(self):
2561
    self.needed_locks = {
2562
      locking.LEVEL_NODE: self.op.node_name,
2563
      }
2564

    
2565
  def CheckPrereq(self):
2566
    """Check prerequisites.
2567

2568
    """
2569
    storage_type = self.op.storage_type
2570

    
2571
    try:
2572
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
2573
    except KeyError:
2574
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
2575
                                 " modified" % storage_type)
2576

    
2577
    diff = set(self.op.changes.keys()) - modifiable
2578
    if diff:
2579
      raise errors.OpPrereqError("The following fields can not be modified for"
2580
                                 " storage units of type '%s': %r" %
2581
                                 (storage_type, list(diff)))
2582

    
2583
  def Exec(self, feedback_fn):
2584
    """Computes the list of nodes and their attributes.
2585

2586
    """
2587
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2588
    result = self.rpc.call_storage_modify(self.op.node_name,
2589
                                          self.op.storage_type, st_args,
2590
                                          self.op.name, self.op.changes)
2591
    result.Raise("Failed to modify storage unit '%s' on %s" %
2592
                 (self.op.name, self.op.node_name))
2593

    
2594

    
2595
class LUAddNode(LogicalUnit):
2596
  """Logical unit for adding node to the cluster.
2597

2598
  """
2599
  HPATH = "node-add"
2600
  HTYPE = constants.HTYPE_NODE
2601
  _OP_REQP = ["node_name"]
2602

    
2603
  def BuildHooksEnv(self):
2604
    """Build hooks env.
2605

2606
    This will run on all nodes before, and on all nodes + the new node after.
2607

2608
    """
2609
    env = {
2610
      "OP_TARGET": self.op.node_name,
2611
      "NODE_NAME": self.op.node_name,
2612
      "NODE_PIP": self.op.primary_ip,
2613
      "NODE_SIP": self.op.secondary_ip,
2614
      }
2615
    nodes_0 = self.cfg.GetNodeList()
2616
    nodes_1 = nodes_0 + [self.op.node_name, ]
2617
    return env, nodes_0, nodes_1
2618

    
2619
  def CheckPrereq(self):
2620
    """Check prerequisites.
2621

2622
    This checks:
2623
     - the new node is not already in the config
2624
     - it is resolvable
2625
     - its parameters (single/dual homed) matches the cluster
2626

2627
    Any errors are signaled by raising errors.OpPrereqError.
2628

2629
    """
2630
    node_name = self.op.node_name
2631
    cfg = self.cfg
2632

    
2633
    dns_data = utils.HostInfo(node_name)
2634

    
2635
    node = dns_data.name
2636
    primary_ip = self.op.primary_ip = dns_data.ip
2637
    secondary_ip = getattr(self.op, "secondary_ip", None)
2638
    if secondary_ip is None:
2639
      secondary_ip = primary_ip
2640
    if not utils.IsValidIP(secondary_ip):
2641
      raise errors.OpPrereqError("Invalid secondary IP given")
2642
    self.op.secondary_ip = secondary_ip
2643

    
2644
    node_list = cfg.GetNodeList()
2645
    if not self.op.readd and node in node_list:
2646
      raise errors.OpPrereqError("Node %s is already in the configuration" %
2647
                                 node)
2648
    elif self.op.readd and node not in node_list:
2649
      raise errors.OpPrereqError("Node %s is not in the configuration" % node)
2650

    
2651
    for existing_node_name in node_list:
2652
      existing_node = cfg.GetNodeInfo(existing_node_name)
2653

    
2654
      if self.op.readd and node == existing_node_name:
2655
        if (existing_node.primary_ip != primary_ip or
2656
            existing_node.secondary_ip != secondary_ip):
2657
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
2658
                                     " address configuration as before")
2659
        continue
2660

    
2661
      if (existing_node.primary_ip == primary_ip or
2662
          existing_node.secondary_ip == primary_ip or
2663
          existing_node.primary_ip == secondary_ip or
2664
          existing_node.secondary_ip == secondary_ip):
2665
        raise errors.OpPrereqError("New node ip address(es) conflict with"
2666
                                   " existing node %s" % existing_node.name)
2667

    
2668
    # check that the type of the node (single versus dual homed) is the
2669
    # same as for the master
2670
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
2671
    master_singlehomed = myself.secondary_ip == myself.primary_ip
2672
    newbie_singlehomed = secondary_ip == primary_ip
2673
    if master_singlehomed != newbie_singlehomed:
2674
      if master_singlehomed:
2675
        raise errors.OpPrereqError("The master has no private ip but the"
2676
                                   " new node has one")
2677
      else:
2678
        raise errors.OpPrereqError("The master has a private ip but the"
2679
                                   " new node doesn't have one")
2680

    
2681
    # checks reachability
2682
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
2683
      raise errors.OpPrereqError("Node not reachable by ping")
2684

    
2685
    if not newbie_singlehomed:
2686
      # check reachability from my secondary ip to newbie's secondary ip
2687
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
2688
                           source=myself.secondary_ip):
2689
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
2690
                                   " based ping to noded port")
2691

    
2692
    cp_size = self.cfg.GetClusterInfo().candidate_pool_size
2693
    if self.op.readd:
2694
      exceptions = [node]
2695
    else:
2696
      exceptions = []
2697
    mc_now, mc_max = self.cfg.GetMasterCandidateStats(exceptions)
2698
    # the new node will increase mc_max with one, so:
2699
    mc_max = min(mc_max + 1, cp_size)
2700
    self.master_candidate = mc_now < mc_max
2701

    
2702
    if self.op.readd:
2703
      self.new_node = self.cfg.GetNodeInfo(node)
2704
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
2705
    else:
2706
      self.new_node = objects.Node(name=node,
2707
                                   primary_ip=primary_ip,
2708
                                   secondary_ip=secondary_ip,
2709
                                   master_candidate=self.master_candidate,
2710
                                   offline=False, drained=False)
2711

    
2712
  def Exec(self, feedback_fn):
2713
    """Adds the new node to the cluster.
2714

2715
    """
2716
    new_node = self.new_node
2717
    node = new_node.name
2718

    
2719
    # for re-adds, reset the offline/drained/master-candidate flags;
2720
    # we need to reset here, otherwise offline would prevent RPC calls
2721
    # later in the procedure; this also means that if the re-add
2722
    # fails, we are left with a non-offlined, broken node
2723
    if self.op.readd:
2724
      new_node.drained = new_node.offline = False
2725
      self.LogInfo("Readding a node, the offline/drained flags were reset")
2726
      # if we demote the node, we do cleanup later in the procedure
2727
      new_node.master_candidate = self.master_candidate
2728

    
2729
    # notify the user about any possible mc promotion
2730
    if new_node.master_candidate:
2731
      self.LogInfo("Node will be a master candidate")
2732

    
2733
    # check connectivity
2734
    result = self.rpc.call_version([node])[node]
2735
    result.Raise("Can't get version information from node %s" % node)
2736
    if constants.PROTOCOL_VERSION == result.payload:
2737
      logging.info("Communication to node %s fine, sw version %s match",
2738
                   node, result.payload)
2739
    else:
2740
      raise errors.OpExecError("Version mismatch master version %s,"
2741
                               " node version %s" %
2742
                               (constants.PROTOCOL_VERSION, result.payload))
2743

    
2744
    # setup ssh on node
2745
    logging.info("Copy ssh key to node %s", node)
2746
    priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
2747
    keyarray = []
2748
    keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
2749
                constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
2750
                priv_key, pub_key]
2751

    
2752
    for i in keyfiles:
2753
      f = open(i, 'r')
2754
      try:
2755
        keyarray.append(f.read())
2756
      finally:
2757
        f.close()
2758

    
2759
    result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
2760
                                    keyarray[2],
2761
                                    keyarray[3], keyarray[4], keyarray[5])
2762
    result.Raise("Cannot transfer ssh keys to the new node")
2763

    
2764
    # Add node to our /etc/hosts, and add key to known_hosts
2765
    if self.cfg.GetClusterInfo().modify_etc_hosts:
2766
      utils.AddHostToEtcHosts(new_node.name)
2767

    
2768
    if new_node.secondary_ip != new_node.primary_ip:
2769
      result = self.rpc.call_node_has_ip_address(new_node.name,
2770
                                                 new_node.secondary_ip)
2771
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
2772
                   prereq=True)
2773
      if not result.payload:
2774
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
2775
                                 " you gave (%s). Please fix and re-run this"
2776
                                 " command." % new_node.secondary_ip)
2777

    
2778
    node_verify_list = [self.cfg.GetMasterNode()]
2779
    node_verify_param = {
2780
      'nodelist': [node],
2781
      # TODO: do a node-net-test as well?
2782
    }
2783

    
2784
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
2785
                                       self.cfg.GetClusterName())
2786
    for verifier in node_verify_list:
2787
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
2788
      nl_payload = result[verifier].payload['nodelist']
2789
      if nl_payload:
2790
        for failed in nl_payload:
2791
          feedback_fn("ssh/hostname verification failed %s -> %s" %
2792
                      (verifier, nl_payload[failed]))
2793
        raise errors.OpExecError("ssh/hostname verification failed.")
2794

    
2795
    if self.op.readd:
2796
      _RedistributeAncillaryFiles(self)
2797
      self.context.ReaddNode(new_node)
2798
      # make sure we redistribute the config
2799
      self.cfg.Update(new_node)
2800
      # and make sure the new node will not have old files around
2801
      if not new_node.master_candidate:
2802
        result = self.rpc.call_node_demote_from_mc(new_node.name)
2803
        msg = result.RemoteFailMsg()
2804
        if msg:
2805
          self.LogWarning("Node failed to demote itself from master"
2806
                          " candidate status: %s" % msg)
2807
    else:
2808
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
2809
      self.context.AddNode(new_node)
2810

    
2811

    
2812
class LUSetNodeParams(LogicalUnit):
2813
  """Modifies the parameters of a node.
2814

2815
  """
2816
  HPATH = "node-modify"
2817
  HTYPE = constants.HTYPE_NODE
2818
  _OP_REQP = ["node_name"]
2819
  REQ_BGL = False
2820

    
2821
  def CheckArguments(self):
2822
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2823
    if node_name is None:
2824
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name)
2825
    self.op.node_name = node_name
2826
    _CheckBooleanOpField(self.op, 'master_candidate')
2827
    _CheckBooleanOpField(self.op, 'offline')
2828
    _CheckBooleanOpField(self.op, 'drained')
2829
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
2830
    if all_mods.count(None) == 3:
2831
      raise errors.OpPrereqError("Please pass at least one modification")
2832
    if all_mods.count(True) > 1:
2833
      raise errors.OpPrereqError("Can't set the node into more than one"
2834
                                 " state at the same time")
2835

    
2836
  def ExpandNames(self):
2837
    self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
2838

    
2839
  def BuildHooksEnv(self):
2840
    """Build hooks env.
2841

2842
    This runs on the master node.
2843

2844
    """
2845
    env = {
2846
      "OP_TARGET": self.op.node_name,
2847
      "MASTER_CANDIDATE": str(self.op.master_candidate),
2848
      "OFFLINE": str(self.op.offline),
2849
      "DRAINED": str(self.op.drained),
2850
      }
2851
    nl = [self.cfg.GetMasterNode(),
2852
          self.op.node_name]
2853
    return env, nl, nl
2854

    
2855
  def CheckPrereq(self):
2856
    """Check prerequisites.
2857

2858
    This only checks the instance list against the existing names.
2859

2860
    """
2861
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
2862

    
2863
    if ((self.op.master_candidate == False or self.op.offline == True or
2864
         self.op.drained == True) and node.master_candidate):
2865
      # we will demote the node from master_candidate
2866
      if self.op.node_name == self.cfg.GetMasterNode():
2867
        raise errors.OpPrereqError("The master node has to be a"
2868
                                   " master candidate, online and not drained")
2869
      cp_size = self.cfg.GetClusterInfo().candidate_pool_size
2870
      num_candidates, _ = self.cfg.GetMasterCandidateStats()
2871
      if num_candidates <= cp_size:
2872
        msg = ("Not enough master candidates (desired"
2873
               " %d, new value will be %d)" % (cp_size, num_candidates-1))
2874
        if self.op.force:
2875
          self.LogWarning(msg)
2876
        else:
2877
          raise errors.OpPrereqError(msg)
2878

    
2879
    if (self.op.master_candidate == True and
2880
        ((node.offline and not self.op.offline == False) or
2881
         (node.drained and not self.op.drained == False))):
2882
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
2883
                                 " to master_candidate" % node.name)
2884

    
2885
    return
2886

    
2887
  def Exec(self, feedback_fn):
2888
    """Modifies a node.
2889

2890
    """
2891
    node = self.node
2892

    
2893
    result = []
2894
    changed_mc = False
2895

    
2896
    if self.op.offline is not None:
2897
      node.offline = self.op.offline
2898
      result.append(("offline", str(self.op.offline)))
2899
      if self.op.offline == True:
2900
        if node.master_candidate:
2901
          node.master_candidate = False
2902
          changed_mc = True
2903
          result.append(("master_candidate", "auto-demotion due to offline"))
2904
        if node.drained:
2905
          node.drained = False
2906
          result.append(("drained", "clear drained status due to offline"))
2907

    
2908
    if self.op.master_candidate is not None:
2909
      node.master_candidate = self.op.master_candidate
2910
      changed_mc = True
2911
      result.append(("master_candidate", str(self.op.master_candidate)))
2912
      if self.op.master_candidate == False:
2913
        rrc = self.rpc.call_node_demote_from_mc(node.name)
2914
        msg = rrc.fail_msg
2915
        if msg:
2916
          self.LogWarning("Node failed to demote itself: %s" % msg)
2917

    
2918
    if self.op.drained is not None:
2919
      node.drained = self.op.drained
2920
      result.append(("drained", str(self.op.drained)))
2921
      if self.op.drained == True:
2922
        if node.master_candidate:
2923
          node.master_candidate = False
2924
          changed_mc = True
2925
          result.append(("master_candidate", "auto-demotion due to drain"))
2926
          rrc = self.rpc.call_node_demote_from_mc(node.name)
2927
          msg = rrc.RemoteFailMsg()
2928
          if msg:
2929
            self.LogWarning("Node failed to demote itself: %s" % msg)
2930
        if node.offline:
2931
          node.offline = False
2932
          result.append(("offline", "clear offline status due to drain"))
2933

    
2934
    # this will trigger configuration file update, if needed
2935
    self.cfg.Update(node)
2936
    # this will trigger job queue propagation or cleanup
2937
    if changed_mc:
2938
      self.context.ReaddNode(node)
2939

    
2940
    return result
2941

    
2942

    
2943
class LUPowercycleNode(NoHooksLU):
2944
  """Powercycles a node.
2945

2946
  """
2947
  _OP_REQP = ["node_name", "force"]
2948
  REQ_BGL = False
2949

    
2950
  def CheckArguments(self):
2951
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2952
    if node_name is None:
2953
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name)
2954
    self.op.node_name = node_name
2955
    if node_name == self.cfg.GetMasterNode() and not self.op.force:
2956
      raise errors.OpPrereqError("The node is the master and the force"
2957
                                 " parameter was not set")
2958

    
2959
  def ExpandNames(self):
2960
    """Locking for PowercycleNode.
2961

2962
    This is a last-resort option and shouldn't block on other
2963
    jobs. Therefore, we grab no locks.
2964

2965
    """
2966
    self.needed_locks = {}
2967

    
2968
  def CheckPrereq(self):
2969
    """Check prerequisites.
2970

2971
    This LU has no prereqs.
2972

2973
    """
2974
    pass
2975

    
2976
  def Exec(self, feedback_fn):
2977
    """Reboots a node.
2978

2979
    """
2980
    result = self.rpc.call_node_powercycle(self.op.node_name,
2981
                                           self.cfg.GetHypervisorType())
2982
    result.Raise("Failed to schedule the reboot")
2983
    return result.payload
2984

    
2985

    
2986
class LUQueryClusterInfo(NoHooksLU):
2987
  """Query cluster configuration.
2988

2989
  """
2990
  _OP_REQP = []
2991
  REQ_BGL = False
2992

    
2993
  def ExpandNames(self):
2994
    self.needed_locks = {}
2995

    
2996
  def CheckPrereq(self):
2997
    """No prerequsites needed for this LU.
2998

2999
    """
3000
    pass
3001

    
3002
  def Exec(self, feedback_fn):
3003
    """Return cluster config.
3004

3005
    """
3006
    cluster = self.cfg.GetClusterInfo()
3007
    result = {
3008
      "software_version": constants.RELEASE_VERSION,
3009
      "protocol_version": constants.PROTOCOL_VERSION,
3010
      "config_version": constants.CONFIG_VERSION,
3011
      "os_api_version": max(constants.OS_API_VERSIONS),
3012
      "export_version": constants.EXPORT_VERSION,
3013
      "architecture": (platform.architecture()[0], platform.machine()),
3014
      "name": cluster.cluster_name,
3015
      "master": cluster.master_node,
3016
      "default_hypervisor": cluster.enabled_hypervisors[0],
3017
      "enabled_hypervisors": cluster.enabled_hypervisors,
3018
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3019
                        for hypervisor_name in cluster.enabled_hypervisors]),
3020
      "beparams": cluster.beparams,
3021
      "nicparams": cluster.nicparams,
3022
      "candidate_pool_size": cluster.candidate_pool_size,
3023
      "master_netdev": cluster.master_netdev,
3024
      "volume_group_name": cluster.volume_group_name,
3025
      "file_storage_dir": cluster.file_storage_dir,
3026
      }
3027

    
3028
    return result
3029

    
3030

    
3031
class LUQueryConfigValues(NoHooksLU):
3032
  """Return configuration values.
3033

3034
  """
3035
  _OP_REQP = []
3036
  REQ_BGL = False
3037
  _FIELDS_DYNAMIC = utils.FieldSet()
3038
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag")
3039

    
3040
  def ExpandNames(self):
3041
    self.needed_locks = {}
3042

    
3043
    _CheckOutputFields(static=self._FIELDS_STATIC,
3044
                       dynamic=self._FIELDS_DYNAMIC,
3045
                       selected=self.op.output_fields)
3046

    
3047
  def CheckPrereq(self):
3048
    """No prerequisites.
3049

3050
    """
3051
    pass
3052

    
3053
  def Exec(self, feedback_fn):
3054
    """Dump a representation of the cluster config to the standard output.
3055

3056
    """
3057
    values = []
3058
    for field in self.op.output_fields:
3059
      if field == "cluster_name":
3060
        entry = self.cfg.GetClusterName()
3061
      elif field == "master_node":
3062
        entry = self.cfg.GetMasterNode()
3063
      elif field == "drain_flag":
3064
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3065
      else:
3066
        raise errors.ParameterError(field)
3067
      values.append(entry)
3068
    return values
3069

    
3070

    
3071
class LUActivateInstanceDisks(NoHooksLU):
3072
  """Bring up an instance's disks.
3073

3074
  """
3075
  _OP_REQP = ["instance_name"]
3076
  REQ_BGL = False
3077

    
3078
  def ExpandNames(self):
3079
    self._ExpandAndLockInstance()
3080
    self.needed_locks[locking.LEVEL_NODE] = []
3081
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3082

    
3083
  def DeclareLocks(self, level):
3084
    if level == locking.LEVEL_NODE:
3085
      self._LockInstancesNodes()
3086

    
3087
  def CheckPrereq(self):
3088
    """Check prerequisites.
3089

3090
    This checks that the instance is in the cluster.
3091

3092
    """
3093
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3094
    assert self.instance is not None, \
3095
      "Cannot retrieve locked instance %s" % self.op.instance_name
3096
    _CheckNodeOnline(self, self.instance.primary_node)
3097
    if not hasattr(self.op, "ignore_size"):
3098
      self.op.ignore_size = False
3099

    
3100
  def Exec(self, feedback_fn):
3101
    """Activate the disks.
3102

3103
    """
3104
    disks_ok, disks_info = \
3105
              _AssembleInstanceDisks(self, self.instance,
3106
                                     ignore_size=self.op.ignore_size)
3107
    if not disks_ok:
3108
      raise errors.OpExecError("Cannot activate block devices")
3109

    
3110
    return disks_info
3111

    
3112

    
3113
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3114
                           ignore_size=False):
3115
  """Prepare the block devices for an instance.
3116

3117
  This sets up the block devices on all nodes.
3118

3119
  @type lu: L{LogicalUnit}
3120
  @param lu: the logical unit on whose behalf we execute
3121
  @type instance: L{objects.Instance}
3122
  @param instance: the instance for whose disks we assemble
3123
  @type ignore_secondaries: boolean
3124
  @param ignore_secondaries: if true, errors on secondary nodes
3125
      won't result in an error return from the function
3126
  @type ignore_size: boolean
3127
  @param ignore_size: if true, the current known size of the disk
3128
      will not be used during the disk activation, useful for cases
3129
      when the size is wrong
3130
  @return: False if the operation failed, otherwise a list of
3131
      (host, instance_visible_name, node_visible_name)
3132
      with the mapping from node devices to instance devices
3133

3134
  """
3135
  device_info = []
3136
  disks_ok = True
3137
  iname = instance.name
3138
  # With the two passes mechanism we try to reduce the window of
3139
  # opportunity for the race condition of switching DRBD to primary
3140
  # before handshaking occured, but we do not eliminate it
3141

    
3142
  # The proper fix would be to wait (with some limits) until the
3143
  # connection has been made and drbd transitions from WFConnection
3144
  # into any other network-connected state (Connected, SyncTarget,
3145
  # SyncSource, etc.)
3146

    
3147
  # 1st pass, assemble on all nodes in secondary mode
3148
  for inst_disk in instance.disks:
3149
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3150
      if ignore_size:
3151
        node_disk = node_disk.Copy()
3152
        node_disk.UnsetSize()
3153
      lu.cfg.SetDiskID(node_disk, node)
3154
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3155
      msg = result.fail_msg
3156
      if msg:
3157
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3158
                           " (is_primary=False, pass=1): %s",
3159
                           inst_disk.iv_name, node, msg)
3160
        if not ignore_secondaries:
3161
          disks_ok = False
3162

    
3163
  # FIXME: race condition on drbd migration to primary
3164

    
3165
  # 2nd pass, do only the primary node
3166
  for inst_disk in instance.disks:
3167
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3168
      if node != instance.primary_node:
3169
        continue
3170
      if ignore_size:
3171
        node_disk = node_disk.Copy()
3172
        node_disk.UnsetSize()
3173
      lu.cfg.SetDiskID(node_disk, node)
3174
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3175
      msg = result.fail_msg
3176
      if msg:
3177
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3178
                           " (is_primary=True, pass=2): %s",
3179
                           inst_disk.iv_name, node, msg)
3180
        disks_ok = False
3181
    device_info.append((instance.primary_node, inst_disk.iv_name,
3182
                        result.payload))
3183

    
3184
  # leave the disks configured for the primary node
3185
  # this is a workaround that would be fixed better by
3186
  # improving the logical/physical id handling
3187
  for disk in instance.disks:
3188
    lu.cfg.SetDiskID(disk, instance.primary_node)
3189

    
3190
  return disks_ok, device_info
3191

    
3192

    
3193
def _StartInstanceDisks(lu, instance, force):
3194
  """Start the disks of an instance.
3195

3196
  """
3197
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3198
                                           ignore_secondaries=force)
3199
  if not disks_ok:
3200
    _ShutdownInstanceDisks(lu, instance)
3201
    if force is not None and not force:
3202
      lu.proc.LogWarning("", hint="If the message above refers to a"
3203
                         " secondary node,"
3204
                         " you can retry the operation using '--force'.")
3205
    raise errors.OpExecError("Disk consistency error")
3206

    
3207

    
3208
class LUDeactivateInstanceDisks(NoHooksLU):
3209
  """Shutdown an instance's disks.
3210

3211
  """
3212
  _OP_REQP = ["instance_name"]
3213
  REQ_BGL = False
3214

    
3215
  def ExpandNames(self):
3216
    self._ExpandAndLockInstance()
3217
    self.needed_locks[locking.LEVEL_NODE] = []
3218
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3219

    
3220
  def DeclareLocks(self, level):
3221
    if level == locking.LEVEL_NODE:
3222
      self._LockInstancesNodes()
3223

    
3224
  def CheckPrereq(self):
3225
    """Check prerequisites.
3226

3227
    This checks that the instance is in the cluster.
3228

3229
    """
3230
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3231
    assert self.instance is not None, \
3232
      "Cannot retrieve locked instance %s" % self.op.instance_name
3233

    
3234
  def Exec(self, feedback_fn):
3235
    """Deactivate the disks
3236

3237
    """
3238
    instance = self.instance
3239
    _SafeShutdownInstanceDisks(self, instance)
3240

    
3241

    
3242
def _SafeShutdownInstanceDisks(lu, instance):
3243
  """Shutdown block devices of an instance.
3244

3245
  This function checks if an instance is running, before calling
3246
  _ShutdownInstanceDisks.
3247

3248
  """
3249
  pnode = instance.primary_node
3250
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
3251
  ins_l.Raise("Can't contact node %s" % pnode)
3252

    
3253
  if instance.name in ins_l.payload:
3254
    raise errors.OpExecError("Instance is running, can't shutdown"
3255
                             " block devices.")
3256

    
3257
  _ShutdownInstanceDisks(lu, instance)
3258

    
3259

    
3260
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3261
  """Shutdown block devices of an instance.
3262

3263
  This does the shutdown on all nodes of the instance.
3264

3265
  If the ignore_primary is false, errors on the primary node are
3266
  ignored.
3267

3268
  """
3269
  all_result = True
3270
  for disk in instance.disks:
3271
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
3272
      lu.cfg.SetDiskID(top_disk, node)
3273
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
3274
      msg = result.fail_msg
3275
      if msg:
3276
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
3277
                      disk.iv_name, node, msg)
3278
        if not ignore_primary or node != instance.primary_node:
3279
          all_result = False
3280
  return all_result
3281

    
3282

    
3283
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
3284
  """Checks if a node has enough free memory.
3285

3286
  This function check if a given node has the needed amount of free
3287
  memory. In case the node has less memory or we cannot get the
3288
  information from the node, this function raise an OpPrereqError
3289
  exception.
3290

3291
  @type lu: C{LogicalUnit}
3292
  @param lu: a logical unit from which we get configuration data
3293
  @type node: C{str}
3294
  @param node: the node to check
3295
  @type reason: C{str}
3296
  @param reason: string to use in the error message
3297
  @type requested: C{int}
3298
  @param requested: the amount of memory in MiB to check for
3299
  @type hypervisor_name: C{str}
3300
  @param hypervisor_name: the hypervisor to ask for memory stats
3301
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
3302
      we cannot check the node
3303

3304
  """
3305
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
3306
  nodeinfo[node].Raise("Can't get data from node %s" % node, prereq=True)
3307
  free_mem = nodeinfo[node].payload.get('memory_free', None)
3308
  if not isinstance(free_mem, int):
3309
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
3310
                               " was '%s'" % (node, free_mem))
3311
  if requested > free_mem:
3312
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
3313
                               " needed %s MiB, available %s MiB" %
3314
                               (node, reason, requested, free_mem))
3315

    
3316

    
3317
class LUStartupInstance(LogicalUnit):
3318
  """Starts an instance.
3319

3320
  """
3321
  HPATH = "instance-start"
3322
  HTYPE = constants.HTYPE_INSTANCE
3323
  _OP_REQP = ["instance_name", "force"]
3324
  REQ_BGL = False
3325

    
3326
  def ExpandNames(self):
3327
    self._ExpandAndLockInstance()
3328

    
3329
  def BuildHooksEnv(self):
3330
    """Build hooks env.
3331

3332
    This runs on master, primary and secondary nodes of the instance.
3333

3334
    """
3335
    env = {
3336
      "FORCE": self.op.force,
3337
      }
3338
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3339
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3340
    return env, nl, nl
3341

    
3342
  def CheckPrereq(self):
3343
    """Check prerequisites.
3344

3345
    This checks that the instance is in the cluster.
3346

3347
    """
3348
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3349
    assert self.instance is not None, \
3350
      "Cannot retrieve locked instance %s" % self.op.instance_name
3351

    
3352
    # extra beparams
3353
    self.beparams = getattr(self.op, "beparams", {})
3354
    if self.beparams:
3355
      if not isinstance(self.beparams, dict):
3356
        raise errors.OpPrereqError("Invalid beparams passed: %s, expected"
3357
                                   " dict" % (type(self.beparams), ))
3358
      # fill the beparams dict
3359
      utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
3360
      self.op.beparams = self.beparams
3361

    
3362
    # extra hvparams
3363
    self.hvparams = getattr(self.op, "hvparams", {})
3364
    if self.hvparams:
3365
      if not isinstance(self.hvparams, dict):
3366
        raise errors.OpPrereqError("Invalid hvparams passed: %s, expected"
3367
                                   " dict" % (type(self.hvparams), ))
3368

    
3369
      # check hypervisor parameter syntax (locally)
3370
      cluster = self.cfg.GetClusterInfo()
3371
      utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
3372
      filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor],
3373
                                    instance.hvparams)
3374
      filled_hvp.update(self.hvparams)
3375
      hv_type = hypervisor.GetHypervisor(instance.hypervisor)
3376
      hv_type.CheckParameterSyntax(filled_hvp)
3377
      _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
3378
      self.op.hvparams = self.hvparams
3379

    
3380
    _CheckNodeOnline(self, instance.primary_node)
3381

    
3382
    bep = self.cfg.GetClusterInfo().FillBE(instance)
3383
    # check bridges existence
3384
    _CheckInstanceBridgesExist(self, instance)
3385

    
3386
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3387
                                              instance.name,
3388
                                              instance.hypervisor)
3389
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3390
                      prereq=True)
3391
    if not remote_info.payload: # not running already
3392
      _CheckNodeFreeMemory(self, instance.primary_node,
3393
                           "starting instance %s" % instance.name,
3394
                           bep[constants.BE_MEMORY], instance.hypervisor)
3395

    
3396
  def Exec(self, feedback_fn):
3397
    """Start the instance.
3398

3399
    """
3400
    instance = self.instance
3401
    force = self.op.force
3402

    
3403
    self.cfg.MarkInstanceUp(instance.name)
3404

    
3405
    node_current = instance.primary_node
3406

    
3407
    _StartInstanceDisks(self, instance, force)
3408

    
3409
    result = self.rpc.call_instance_start(node_current, instance,
3410
                                          self.hvparams, self.beparams)
3411
    msg = result.fail_msg
3412
    if msg:
3413
      _ShutdownInstanceDisks(self, instance)
3414
      raise errors.OpExecError("Could not start instance: %s" % msg)
3415

    
3416

    
3417
class LURebootInstance(LogicalUnit):
3418
  """Reboot an instance.
3419

3420
  """
3421
  HPATH = "instance-reboot"
3422
  HTYPE = constants.HTYPE_INSTANCE
3423
  _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
3424
  REQ_BGL = False
3425

    
3426
  def ExpandNames(self):
3427
    if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
3428
                                   constants.INSTANCE_REBOOT_HARD,
3429
                                   constants.INSTANCE_REBOOT_FULL]:
3430
      raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
3431
                                  (constants.INSTANCE_REBOOT_SOFT,
3432
                                   constants.INSTANCE_REBOOT_HARD,
3433
                                   constants.INSTANCE_REBOOT_FULL))
3434
    self._ExpandAndLockInstance()
3435

    
3436
  def BuildHooksEnv(self):
3437
    """Build hooks env.
3438

3439
    This runs on master, primary and secondary nodes of the instance.
3440

3441
    """
3442
    env = {
3443
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
3444
      "REBOOT_TYPE": self.op.reboot_type,
3445
      }
3446
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3447
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3448
    return env, nl, nl
3449

    
3450
  def CheckPrereq(self):
3451
    """Check prerequisites.
3452

3453
    This checks that the instance is in the cluster.
3454

3455
    """
3456
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3457
    assert self.instance is not None, \
3458
      "Cannot retrieve locked instance %s" % self.op.instance_name
3459

    
3460
    _CheckNodeOnline(self, instance.primary_node)
3461

    
3462
    # check bridges existence
3463
    _CheckInstanceBridgesExist(self, instance)
3464

    
3465
  def Exec(self, feedback_fn):
3466
    """Reboot the instance.
3467

3468
    """
3469
    instance = self.instance
3470
    ignore_secondaries = self.op.ignore_secondaries
3471
    reboot_type = self.op.reboot_type
3472

    
3473
    node_current = instance.primary_node
3474

    
3475
    if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
3476
                       constants.INSTANCE_REBOOT_HARD]:
3477
      for disk in instance.disks:
3478
        self.cfg.SetDiskID(disk, node_current)
3479
      result = self.rpc.call_instance_reboot(node_current, instance,
3480
                                             reboot_type)
3481
      result.Raise("Could not reboot instance")
3482
    else:
3483
      result = self.rpc.call_instance_shutdown(node_current, instance)
3484
      result.Raise("Could not shutdown instance for full reboot")
3485
      _ShutdownInstanceDisks(self, instance)
3486
      _StartInstanceDisks(self, instance, ignore_secondaries)
3487
      result = self.rpc.call_instance_start(node_current, instance, None, None)
3488
      msg = result.fail_msg
3489
      if msg:
3490
        _ShutdownInstanceDisks(self, instance)
3491
        raise errors.OpExecError("Could not start instance for"
3492
                                 " full reboot: %s" % msg)
3493

    
3494
    self.cfg.MarkInstanceUp(instance.name)
3495

    
3496

    
3497
class LUShutdownInstance(LogicalUnit):
3498
  """Shutdown an instance.
3499

3500
  """
3501
  HPATH = "instance-stop"
3502
  HTYPE = constants.HTYPE_INSTANCE
3503
  _OP_REQP = ["instance_name"]
3504
  REQ_BGL = False
3505

    
3506
  def ExpandNames(self):
3507
    self._ExpandAndLockInstance()
3508

    
3509
  def BuildHooksEnv(self):
3510
    """Build hooks env.
3511

3512
    This runs on master, primary and secondary nodes of the instance.
3513

3514
    """
3515
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3516
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3517
    return env, nl, nl
3518

    
3519
  def CheckPrereq(self):
3520
    """Check prerequisites.
3521

3522
    This checks that the instance is in the cluster.
3523

3524
    """
3525
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3526
    assert self.instance is not None, \
3527
      "Cannot retrieve locked instance %s" % self.op.instance_name
3528
    _CheckNodeOnline(self, self.instance.primary_node)
3529

    
3530
  def Exec(self, feedback_fn):
3531
    """Shutdown the instance.
3532

3533
    """
3534
    instance = self.instance
3535
    node_current = instance.primary_node
3536
    self.cfg.MarkInstanceDown(instance.name)
3537
    result = self.rpc.call_instance_shutdown(node_current, instance)
3538
    msg = result.fail_msg
3539
    if msg:
3540
      self.proc.LogWarning("Could not shutdown instance: %s" % msg)
3541

    
3542
    _ShutdownInstanceDisks(self, instance)
3543

    
3544

    
3545
class LUReinstallInstance(LogicalUnit):
3546
  """Reinstall an instance.
3547

3548
  """
3549
  HPATH = "instance-reinstall"
3550
  HTYPE = constants.HTYPE_INSTANCE
3551
  _OP_REQP = ["instance_name"]
3552
  REQ_BGL = False
3553

    
3554
  def ExpandNames(self):
3555
    self._ExpandAndLockInstance()
3556

    
3557
  def BuildHooksEnv(self):
3558
    """Build hooks env.
3559

3560
    This runs on master, primary and secondary nodes of the instance.
3561

3562
    """
3563
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3564
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3565
    return env, nl, nl
3566

    
3567
  def CheckPrereq(self):
3568
    """Check prerequisites.
3569

3570
    This checks that the instance is in the cluster and is not running.
3571

3572
    """
3573
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3574
    assert instance is not None, \
3575
      "Cannot retrieve locked instance %s" % self.op.instance_name
3576
    _CheckNodeOnline(self, instance.primary_node)
3577

    
3578
    if instance.disk_template == constants.DT_DISKLESS:
3579
      raise errors.OpPrereqError("Instance '%s' has no disks" %
3580
                                 self.op.instance_name)
3581
    if instance.admin_up:
3582
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3583
                                 self.op.instance_name)
3584
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3585
                                              instance.name,
3586
                                              instance.hypervisor)
3587
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3588
                      prereq=True)
3589
    if remote_info.payload:
3590
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3591
                                 (self.op.instance_name,
3592
                                  instance.primary_node))
3593

    
3594
    self.op.os_type = getattr(self.op, "os_type", None)
3595
    if self.op.os_type is not None:
3596
      # OS verification
3597
      pnode = self.cfg.GetNodeInfo(
3598
        self.cfg.ExpandNodeName(instance.primary_node))
3599
      if pnode is None:
3600
        raise errors.OpPrereqError("Primary node '%s' is unknown" %
3601
                                   self.op.pnode)
3602
      result = self.rpc.call_os_get(pnode.name, self.op.os_type)
3603
      result.Raise("OS '%s' not in supported OS list for primary node %s" %
3604
                   (self.op.os_type, pnode.name), prereq=True)
3605

    
3606
    self.instance = instance
3607

    
3608
  def Exec(self, feedback_fn):
3609
    """Reinstall the instance.
3610

3611
    """
3612
    inst = self.instance
3613

    
3614
    if self.op.os_type is not None:
3615
      feedback_fn("Changing OS to '%s'..." % self.op.os_type)
3616
      inst.os = self.op.os_type
3617
      self.cfg.Update(inst)
3618

    
3619
    _StartInstanceDisks(self, inst, None)
3620
    try:
3621
      feedback_fn("Running the instance OS create scripts...")
3622
      result = self.rpc.call_instance_os_add(inst.primary_node, inst, True)
3623
      result.Raise("Could not install OS for instance %s on node %s" %
3624
                   (inst.name, inst.primary_node))
3625
    finally:
3626
      _ShutdownInstanceDisks(self, inst)
3627

    
3628

    
3629
class LURenameInstance(LogicalUnit):
3630
  """Rename an instance.
3631

3632
  """
3633
  HPATH = "instance-rename"
3634
  HTYPE = constants.HTYPE_INSTANCE
3635
  _OP_REQP = ["instance_name", "new_name"]
3636

    
3637
  def BuildHooksEnv(self):
3638
    """Build hooks env.
3639

3640
    This runs on master, primary and secondary nodes of the instance.
3641

3642
    """
3643
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3644
    env["INSTANCE_NEW_NAME"] = self.op.new_name
3645
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3646
    return env, nl, nl
3647

    
3648
  def CheckPrereq(self):
3649
    """Check prerequisites.
3650

3651
    This checks that the instance is in the cluster and is not running.
3652

3653
    """
3654
    instance = self.cfg.GetInstanceInfo(
3655
      self.cfg.ExpandInstanceName(self.op.instance_name))
3656
    if instance is None:
3657
      raise errors.OpPrereqError("Instance '%s' not known" %
3658
                                 self.op.instance_name)
3659
    _CheckNodeOnline(self, instance.primary_node)
3660

    
3661
    if instance.admin_up:
3662
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3663
                                 self.op.instance_name)
3664
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3665
                                              instance.name,
3666
                                              instance.hypervisor)
3667
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3668
                      prereq=True)
3669
    if remote_info.payload:
3670
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3671
                                 (self.op.instance_name,
3672
                                  instance.primary_node))
3673
    self.instance = instance
3674

    
3675
    # new name verification
3676
    name_info = utils.HostInfo(self.op.new_name)
3677

    
3678
    self.op.new_name = new_name = name_info.name
3679
    instance_list = self.cfg.GetInstanceList()
3680
    if new_name in instance_list:
3681
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
3682
                                 new_name)
3683

    
3684
    if not getattr(self.op, "ignore_ip", False):
3685
      if utils.TcpPing(name_info.ip, constants.DEFAULT_NODED_PORT):
3686
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
3687
                                   (name_info.ip, new_name))
3688

    
3689

    
3690
  def Exec(self, feedback_fn):
3691
    """Reinstall the instance.
3692

3693
    """
3694
    inst = self.instance
3695
    old_name = inst.name
3696

    
3697
    if inst.disk_template == constants.DT_FILE:
3698
      old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
3699

    
3700
    self.cfg.RenameInstance(inst.name, self.op.new_name)
3701
    # Change the instance lock. This is definitely safe while we hold the BGL
3702
    self.context.glm.remove(locking.LEVEL_INSTANCE, old_name)
3703
    self.context.glm.add(locking.LEVEL_INSTANCE, self.op.new_name)
3704

    
3705
    # re-read the instance from the configuration after rename
3706
    inst = self.cfg.GetInstanceInfo(self.op.new_name)
3707

    
3708
    if inst.disk_template == constants.DT_FILE:
3709
      new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
3710
      result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
3711
                                                     old_file_storage_dir,
3712
                                                     new_file_storage_dir)
3713
      result.Raise("Could not rename on node %s directory '%s' to '%s'"
3714
                   " (but the instance has been renamed in Ganeti)" %
3715
                   (inst.primary_node, old_file_storage_dir,
3716
                    new_file_storage_dir))
3717

    
3718
    _StartInstanceDisks(self, inst, None)
3719
    try:
3720
      result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
3721
                                                 old_name)
3722
      msg = result.fail_msg
3723
      if msg:
3724
        msg = ("Could not run OS rename script for instance %s on node %s"
3725
               " (but the instance has been renamed in Ganeti): %s" %
3726
               (inst.name, inst.primary_node, msg))
3727
        self.proc.LogWarning(msg)
3728
    finally:
3729
      _ShutdownInstanceDisks(self, inst)
3730

    
3731

    
3732
class LURemoveInstance(LogicalUnit):
3733
  """Remove an instance.
3734

3735
  """
3736
  HPATH = "instance-remove"
3737
  HTYPE = constants.HTYPE_INSTANCE
3738
  _OP_REQP = ["instance_name", "ignore_failures"]
3739
  REQ_BGL = False
3740

    
3741
  def ExpandNames(self):
3742
    self._ExpandAndLockInstance()
3743
    self.needed_locks[locking.LEVEL_NODE] = []
3744
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3745

    
3746
  def DeclareLocks(self, level):
3747
    if level == locking.LEVEL_NODE:
3748
      self._LockInstancesNodes()
3749

    
3750
  def BuildHooksEnv(self):
3751
    """Build hooks env.
3752

3753
    This runs on master, primary and secondary nodes of the instance.
3754

3755
    """
3756
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3757
    nl = [self.cfg.GetMasterNode()]
3758
    return env, nl, nl
3759

    
3760
  def CheckPrereq(self):
3761
    """Check prerequisites.
3762

3763
    This checks that the instance is in the cluster.
3764

3765
    """
3766
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3767
    assert self.instance is not None, \
3768
      "Cannot retrieve locked instance %s" % self.op.instance_name
3769

    
3770
  def Exec(self, feedback_fn):
3771
    """Remove the instance.
3772

3773
    """
3774
    instance = self.instance
3775
    logging.info("Shutting down instance %s on node %s",
3776
                 instance.name, instance.primary_node)
3777

    
3778
    result = self.rpc.call_instance_shutdown(instance.primary_node, instance)
3779
    msg = result.fail_msg
3780
    if msg:
3781
      if self.op.ignore_failures:
3782
        feedback_fn("Warning: can't shutdown instance: %s" % msg)
3783
      else:
3784
        raise errors.OpExecError("Could not shutdown instance %s on"
3785
                                 " node %s: %s" %
3786
                                 (instance.name, instance.primary_node, msg))
3787

    
3788
    logging.info("Removing block devices for instance %s", instance.name)
3789

    
3790
    if not _RemoveDisks(self, instance):
3791
      if self.op.ignore_failures:
3792
        feedback_fn("Warning: can't remove instance's disks")
3793
      else:
3794
        raise errors.OpExecError("Can't remove instance's disks")
3795

    
3796
    logging.info("Removing instance %s out of cluster config", instance.name)
3797

    
3798
    self.cfg.RemoveInstance(instance.name)
3799
    self.remove_locks[locking.LEVEL_INSTANCE] = instance.name
3800

    
3801

    
3802
class LUQueryInstances(NoHooksLU):
3803
  """Logical unit for querying instances.
3804

3805
  """
3806
  _OP_REQP = ["output_fields", "names", "use_locking"]
3807
  REQ_BGL = False
3808
  _FIELDS_STATIC = utils.FieldSet(*["name", "os", "pnode", "snodes",
3809
                                    "admin_state",
3810
                                    "disk_template", "ip", "mac", "bridge",
3811
                                    "nic_mode", "nic_link",
3812
                                    "sda_size", "sdb_size", "vcpus", "tags",
3813
                                    "network_port", "beparams",
3814
                                    r"(disk)\.(size)/([0-9]+)",
3815
                                    r"(disk)\.(sizes)", "disk_usage",
3816
                                    r"(nic)\.(mac|ip|mode|link)/([0-9]+)",
3817
                                    r"(nic)\.(bridge)/([0-9]+)",
3818
                                    r"(nic)\.(macs|ips|modes|links|bridges)",
3819
                                    r"(disk|nic)\.(count)",
3820
                                    "serial_no", "hypervisor", "hvparams",] +
3821
                                  ["hv/%s" % name
3822
                                   for name in constants.HVS_PARAMETERS] +
3823
                                  ["be/%s" % name
3824
                                   for name in constants.BES_PARAMETERS])
3825
  _FIELDS_DYNAMIC = utils.FieldSet("oper_state", "oper_ram", "status")
3826

    
3827

    
3828
  def ExpandNames(self):
3829
    _CheckOutputFields(static=self._FIELDS_STATIC,
3830
                       dynamic=self._FIELDS_DYNAMIC,
3831
                       selected=self.op.output_fields)
3832

    
3833
    self.needed_locks = {}
3834
    self.share_locks[locking.LEVEL_INSTANCE] = 1
3835
    self.share_locks[locking.LEVEL_NODE] = 1
3836

    
3837
    if self.op.names:
3838
      self.wanted = _GetWantedInstances(self, self.op.names)
3839
    else:
3840
      self.wanted = locking.ALL_SET
3841

    
3842
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3843
    self.do_locking = self.do_node_query and self.op.use_locking
3844
    if self.do_locking:
3845
      self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
3846
      self.needed_locks[locking.LEVEL_NODE] = []
3847
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3848

    
3849
  def DeclareLocks(self, level):
3850
    if level == locking.LEVEL_NODE and self.do_locking:
3851
      self._LockInstancesNodes()
3852

    
3853
  def CheckPrereq(self):
3854
    """Check prerequisites.
3855

3856
    """
3857
    pass
3858

    
3859
  def Exec(self, feedback_fn):
3860
    """Computes the list of nodes and their attributes.
3861

3862
    """
3863
    all_info = self.cfg.GetAllInstancesInfo()
3864
    if self.wanted == locking.ALL_SET:
3865
      # caller didn't specify instance names, so ordering is not important
3866
      if self.do_locking:
3867
        instance_names = self.acquired_locks[locking.LEVEL_INSTANCE]
3868
      else:
3869
        instance_names = all_info.keys()
3870
      instance_names = utils.NiceSort(instance_names)
3871
    else:
3872
      # caller did specify names, so we must keep the ordering
3873
      if self.do_locking:
3874
        tgt_set = self.acquired_locks[locking.LEVEL_INSTANCE]
3875
      else:
3876
        tgt_set = all_info.keys()
3877
      missing = set(self.wanted).difference(tgt_set)
3878
      if missing:
3879
        raise errors.OpExecError("Some instances were removed before"
3880
                                 " retrieving their data: %s" % missing)
3881
      instance_names = self.wanted
3882

    
3883
    instance_list = [all_info[iname] for iname in instance_names]
3884

    
3885
    # begin data gathering
3886

    
3887
    nodes = frozenset([inst.primary_node for inst in instance_list])
3888
    hv_list = list(set([inst.hypervisor for inst in instance_list]))
3889

    
3890
    bad_nodes = []
3891
    off_nodes = []
3892
    if self.do_node_query:
3893
      live_data = {}
3894
      node_data = self.rpc.call_all_instances_info(nodes, hv_list)
3895
      for name in nodes:
3896
        result = node_data[name]
3897
        if result.offline:
3898
          # offline nodes will be in both lists
3899
          off_nodes.append(name)
3900
        if result.failed or result.fail_msg:
3901
          bad_nodes.append(name)
3902
        else:
3903
          if result.payload:
3904
            live_data.update(result.payload)
3905
          # else no instance is alive
3906
    else:
3907
      live_data = dict([(name, {}) for name in instance_names])
3908

    
3909
    # end data gathering
3910

    
3911
    HVPREFIX = "hv/"
3912
    BEPREFIX = "be/"
3913
    output = []
3914
    cluster = self.cfg.GetClusterInfo()
3915
    for instance in instance_list:
3916
      iout = []
3917
      i_hv = cluster.FillHV(instance)
3918
      i_be = cluster.FillBE(instance)
3919
      i_nicp = [objects.FillDict(cluster.nicparams[constants.PP_DEFAULT],
3920
                                 nic.nicparams) for nic in instance.nics]
3921
      for field in self.op.output_fields:
3922
        st_match = self._FIELDS_STATIC.Matches(field)
3923
        if field == "name":
3924
          val = instance.name
3925
        elif field == "os":
3926
          val = instance.os
3927
        elif field == "pnode":
3928
          val = instance.primary_node
3929
        elif field == "snodes":
3930
          val = list(instance.secondary_nodes)
3931
        elif field == "admin_state":
3932
          val = instance.admin_up
3933
        elif field == "oper_state":
3934
          if instance.primary_node in bad_nodes:
3935
            val = None
3936
          else:
3937
            val = bool(live_data.get(instance.name))
3938
        elif field == "status":
3939
          if instance.primary_node in off_nodes:
3940
            val = "ERROR_nodeoffline"
3941
          elif instance.primary_node in bad_nodes:
3942
            val = "ERROR_nodedown"
3943
          else:
3944
            running = bool(live_data.get(instance.name))
3945
            if running:
3946
              if instance.admin_up:
3947
                val = "running"
3948
              else:
3949
                val = "ERROR_up"
3950
            else:
3951
              if instance.admin_up:
3952
                val = "ERROR_down"
3953
              else:
3954
                val = "ADMIN_down"
3955
        elif field == "oper_ram":
3956
          if instance.primary_node in bad_nodes:
3957
            val = None
3958
          elif instance.name in live_data:
3959
            val = live_data[instance.name].get("memory", "?")
3960
          else:
3961
            val = "-"
3962
        elif field == "vcpus":
3963
          val = i_be[constants.BE_VCPUS]
3964
        elif field == "disk_template":
3965
          val = instance.disk_template
3966
        elif field == "ip":
3967
          if instance.nics:
3968
            val = instance.nics[0].ip
3969
          else:
3970
            val = None
3971
        elif field == "nic_mode":
3972
          if instance.nics:
3973
            val = i_nicp[0][constants.NIC_MODE]
3974
          else:
3975
            val = None
3976
        elif field == "nic_link":
3977
          if instance.nics:
3978
            val = i_nicp[0][constants.NIC_LINK]
3979
          else:
3980
            val = None
3981
        elif field == "bridge":
3982
          if (instance.nics and
3983
              i_nicp[0][constants.NIC_MODE] == constants.NIC_MODE_BRIDGED):
3984
            val = i_nicp[0][constants.NIC_LINK]
3985
          else:
3986
            val = None
3987
        elif field == "mac":
3988
          if instance.nics:
3989
            val = instance.nics[0].mac
3990
          else:
3991
            val = None
3992
        elif field == "sda_size" or field == "sdb_size":
3993
          idx = ord(field[2]) - ord('a')
3994
          try:
3995
            val = instance.FindDisk(idx).size
3996
          except errors.OpPrereqError:
3997
            val = None
3998
        elif field == "disk_usage": # total disk usage per node
3999
          disk_sizes = [{'size': disk.size} for disk in instance.disks]
4000
          val = _ComputeDiskSize(instance.disk_template, disk_sizes)
4001
        elif field == "tags":
4002
          val = list(instance.GetTags())
4003
        elif field == "serial_no":
4004
          val = instance.serial_no
4005
        elif field == "network_port":
4006
          val = instance.network_port
4007
        elif field == "hypervisor":
4008
          val = instance.hypervisor
4009
        elif field == "hvparams":
4010
          val = i_hv
4011
        elif (field.startswith(HVPREFIX) and
4012
              field[len(HVPREFIX):] in constants.HVS_PARAMETERS):
4013
          val = i_hv.get(field[len(HVPREFIX):], None)
4014
        elif field == "beparams":
4015
          val = i_be
4016
        elif (field.startswith(BEPREFIX) and
4017
              field[len(BEPREFIX):] in constants.BES_PARAMETERS):
4018
          val = i_be.get(field[len(BEPREFIX):], None)
4019
        elif st_match and st_match.groups():
4020
          # matches a variable list
4021
          st_groups = st_match.groups()
4022
          if st_groups and st_groups[0] == "disk":
4023
            if st_groups[1] == "count":
4024
              val = len(instance.disks)
4025
            elif st_groups[1] == "sizes":
4026
              val = [disk.size for disk in instance.disks]
4027
            elif st_groups[1] == "size":