Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 099c52ad

History | View | Annotate | Download (304.5 kB)

1
#
2
#
3

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

    
21

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

    
24
# pylint: disable-msg=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, errors.ECODE_INVAL)
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, errors.ECODE_NOENT)
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
                               errors.ECODE_INVAL)
422

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

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

    
435
  return utils.NiceSort(wanted)
436

    
437

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

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

450
  """
451
  if not isinstance(instances, list):
452
    raise errors.OpPrereqError("Invalid argument type 'instances'",
453
                               errors.ECODE_INVAL)
454

    
455
  if instances:
456
    wanted = []
457

    
458
    for name in instances:
459
      instance = lu.cfg.ExpandInstanceName(name)
460
      if instance is None:
461
        raise errors.OpPrereqError("No such instance name '%s'" % name,
462
                                   errors.ECODE_NOENT)
463
      wanted.append(instance)
464

    
465
  else:
466
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
467
  return wanted
468

    
469

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

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

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

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

    
488

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

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

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

    
502

    
503
def _CheckNodeOnline(lu, node):
504
  """Ensure that a given node is online.
505

506
  @param lu: the LU on behalf of which we make the check
507
  @param node: the node to check
508
  @raise errors.OpPrereqError: if the node is offline
509

510
  """
511
  if lu.cfg.GetNodeInfo(node).offline:
512
    raise errors.OpPrereqError("Can't use offline node %s" % node,
513
                               errors.ECODE_INVAL)
514

    
515

    
516
def _CheckNodeNotDrained(lu, node):
517
  """Ensure that a given node is not drained.
518

519
  @param lu: the LU on behalf of which we make the check
520
  @param node: the node to check
521
  @raise errors.OpPrereqError: if the node is drained
522

523
  """
524
  if lu.cfg.GetNodeInfo(node).drained:
525
    raise errors.OpPrereqError("Can't use drained node %s" % node,
526
                               errors.ECODE_INVAL)
527

    
528

    
529
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
530
                          memory, vcpus, nics, disk_template, disks,
531
                          bep, hvp, hypervisor_name):
532
  """Builds instance related env variables for hooks
533

534
  This builds the hook environment from individual variables.
535

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

566
  """
567
  if status:
568
    str_status = "up"
569
  else:
570
    str_status = "down"
571
  env = {
572
    "OP_TARGET": name,
573
    "INSTANCE_NAME": name,
574
    "INSTANCE_PRIMARY": primary_node,
575
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
576
    "INSTANCE_OS_TYPE": os_type,
577
    "INSTANCE_STATUS": str_status,
578
    "INSTANCE_MEMORY": memory,
579
    "INSTANCE_VCPUS": vcpus,
580
    "INSTANCE_DISK_TEMPLATE": disk_template,
581
    "INSTANCE_HYPERVISOR": hypervisor_name,
582
  }
583

    
584
  if nics:
585
    nic_count = len(nics)
586
    for idx, (ip, mac, mode, link) in enumerate(nics):
587
      if ip is None:
588
        ip = ""
589
      env["INSTANCE_NIC%d_IP" % idx] = ip
590
      env["INSTANCE_NIC%d_MAC" % idx] = mac
591
      env["INSTANCE_NIC%d_MODE" % idx] = mode
592
      env["INSTANCE_NIC%d_LINK" % idx] = link
593
      if mode == constants.NIC_MODE_BRIDGED:
594
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
595
  else:
596
    nic_count = 0
597

    
598
  env["INSTANCE_NIC_COUNT"] = nic_count
599

    
600
  if disks:
601
    disk_count = len(disks)
602
    for idx, (size, mode) in enumerate(disks):
603
      env["INSTANCE_DISK%d_SIZE" % idx] = size
604
      env["INSTANCE_DISK%d_MODE" % idx] = mode
605
  else:
606
    disk_count = 0
607

    
608
  env["INSTANCE_DISK_COUNT"] = disk_count
609

    
610
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
611
    for key, value in source.items():
612
      env["INSTANCE_%s_%s" % (kind, key)] = value
613

    
614
  return env
615

    
616

    
617
def _NICListToTuple(lu, nics):
618
  """Build a list of nic information tuples.
619

620
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
621
  value in LUQueryInstanceData.
622

623
  @type lu:  L{LogicalUnit}
624
  @param lu: the logical unit on whose behalf we execute
625
  @type nics: list of L{objects.NIC}
626
  @param nics: list of nics to convert to hooks tuples
627

628
  """
629
  hooks_nics = []
630
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
631
  for nic in nics:
632
    ip = nic.ip
633
    mac = nic.mac
634
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
635
    mode = filled_params[constants.NIC_MODE]
636
    link = filled_params[constants.NIC_LINK]
637
    hooks_nics.append((ip, mac, mode, link))
638
  return hooks_nics
639

    
640

    
641
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
642
  """Builds instance related env variables for hooks from an object.
643

644
  @type lu: L{LogicalUnit}
645
  @param lu: the logical unit on whose behalf we execute
646
  @type instance: L{objects.Instance}
647
  @param instance: the instance for which we should build the
648
      environment
649
  @type override: dict
650
  @param override: dictionary with key/values that will override
651
      our values
652
  @rtype: dict
653
  @return: the hook environment dictionary
654

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

    
678

    
679
def _AdjustCandidatePool(lu, exceptions):
680
  """Adjust the candidate pool after node operations.
681

682
  """
683
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
684
  if mod_list:
685
    lu.LogInfo("Promoted nodes to master candidate role: %s",
686
               ", ".join(node.name for node in mod_list))
687
    for name in mod_list:
688
      lu.context.ReaddNode(name)
689
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
690
  if mc_now > mc_max:
691
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
692
               (mc_now, mc_max))
693

    
694

    
695
def _DecideSelfPromotion(lu, exceptions=None):
696
  """Decide whether I should promote myself as a master candidate.
697

698
  """
699
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
700
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
701
  # the new node will increase mc_max with one, so:
702
  mc_should = min(mc_should + 1, cp_size)
703
  return mc_now < mc_should
704

    
705

    
706
def _CheckNicsBridgesExist(lu, target_nics, target_node,
707
                               profile=constants.PP_DEFAULT):
708
  """Check that the brigdes needed by a list of nics exist.
709

710
  """
711
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
712
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
713
                for nic in target_nics]
714
  brlist = [params[constants.NIC_LINK] for params in paramslist
715
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
716
  if brlist:
717
    result = lu.rpc.call_bridges_exist(target_node, brlist)
718
    result.Raise("Error checking bridges on destination node '%s'" %
719
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
720

    
721

    
722
def _CheckInstanceBridgesExist(lu, instance, node=None):
723
  """Check that the brigdes needed by an instance exist.
724

725
  """
726
  if node is None:
727
    node = instance.primary_node
728
  _CheckNicsBridgesExist(lu, instance.nics, node)
729

    
730

    
731
def _CheckOSVariant(os_obj, name):
732
  """Check whether an OS name conforms to the os variants specification.
733

734
  @type os_obj: L{objects.OS}
735
  @param os_obj: OS object to check
736
  @type name: string
737
  @param name: OS name passed by the user, to check for validity
738

739
  """
740
  if not os_obj.supported_variants:
741
    return
742
  try:
743
    variant = name.split("+", 1)[1]
744
  except IndexError:
745
    raise errors.OpPrereqError("OS name must include a variant",
746
                               errors.ECODE_INVAL)
747

    
748
  if variant not in os_obj.supported_variants:
749
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
750

    
751

    
752
def _GetNodeInstancesInner(cfg, fn):
753
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
754

    
755

    
756
def _GetNodeInstances(cfg, node_name):
757
  """Returns a list of all primary and secondary instances on a node.
758

759
  """
760

    
761
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
762

    
763

    
764
def _GetNodePrimaryInstances(cfg, node_name):
765
  """Returns primary instances on a node.
766

767
  """
768
  return _GetNodeInstancesInner(cfg,
769
                                lambda inst: node_name == inst.primary_node)
770

    
771

    
772
def _GetNodeSecondaryInstances(cfg, node_name):
773
  """Returns secondary instances on a node.
774

775
  """
776
  return _GetNodeInstancesInner(cfg,
777
                                lambda inst: node_name in inst.secondary_nodes)
778

    
779

    
780
def _GetStorageTypeArgs(cfg, storage_type):
781
  """Returns the arguments for a storage type.
782

783
  """
784
  # Special case for file storage
785
  if storage_type == constants.ST_FILE:
786
    # storage.FileStorage wants a list of storage directories
787
    return [[cfg.GetFileStorageDir()]]
788

    
789
  return []
790

    
791

    
792
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
793
  faulty = []
794

    
795
  for dev in instance.disks:
796
    cfg.SetDiskID(dev, node_name)
797

    
798
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
799
  result.Raise("Failed to get disk status from node %s" % node_name,
800
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
801

    
802
  for idx, bdev_status in enumerate(result.payload):
803
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
804
      faulty.append(idx)
805

    
806
  return faulty
807

    
808

    
809
class LUPostInitCluster(LogicalUnit):
810
  """Logical unit for running hooks after cluster initialization.
811

812
  """
813
  HPATH = "cluster-init"
814
  HTYPE = constants.HTYPE_CLUSTER
815
  _OP_REQP = []
816

    
817
  def BuildHooksEnv(self):
818
    """Build hooks env.
819

820
    """
821
    env = {"OP_TARGET": self.cfg.GetClusterName()}
822
    mn = self.cfg.GetMasterNode()
823
    return env, [], [mn]
824

    
825
  def CheckPrereq(self):
826
    """No prerequisites to check.
827

828
    """
829
    return True
830

    
831
  def Exec(self, feedback_fn):
832
    """Nothing to do.
833

834
    """
835
    return True
836

    
837

    
838
class LUDestroyCluster(LogicalUnit):
839
  """Logical unit for destroying the cluster.
840

841
  """
842
  HPATH = "cluster-destroy"
843
  HTYPE = constants.HTYPE_CLUSTER
844
  _OP_REQP = []
845

    
846
  def BuildHooksEnv(self):
847
    """Build hooks env.
848

849
    """
850
    env = {"OP_TARGET": self.cfg.GetClusterName()}
851
    return env, [], []
852

    
853
  def CheckPrereq(self):
854
    """Check prerequisites.
855

856
    This checks whether the cluster is empty.
857

858
    Any errors are signaled by raising errors.OpPrereqError.
859

860
    """
861
    master = self.cfg.GetMasterNode()
862

    
863
    nodelist = self.cfg.GetNodeList()
864
    if len(nodelist) != 1 or nodelist[0] != master:
865
      raise errors.OpPrereqError("There are still %d node(s) in"
866
                                 " this cluster." % (len(nodelist) - 1),
867
                                 errors.ECODE_INVAL)
868
    instancelist = self.cfg.GetInstanceList()
869
    if instancelist:
870
      raise errors.OpPrereqError("There are still %d instance(s) in"
871
                                 " this cluster." % len(instancelist),
872
                                 errors.ECODE_INVAL)
873

    
874
  def Exec(self, feedback_fn):
875
    """Destroys the cluster.
876

877
    """
878
    master = self.cfg.GetMasterNode()
879
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
880

    
881
    # Run post hooks on master node before it's removed
882
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
883
    try:
884
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
885
    except:
886
      self.LogWarning("Errors occurred running hooks on %s" % master)
887

    
888
    result = self.rpc.call_node_stop_master(master, False)
889
    result.Raise("Could not disable the master role")
890

    
891
    if modify_ssh_setup:
892
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
893
      utils.CreateBackup(priv_key)
894
      utils.CreateBackup(pub_key)
895

    
896
    return master
897

    
898

    
899
class LUVerifyCluster(LogicalUnit):
900
  """Verifies the cluster status.
901

902
  """
903
  HPATH = "cluster-verify"
904
  HTYPE = constants.HTYPE_CLUSTER
905
  _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"]
906
  REQ_BGL = False
907

    
908
  TCLUSTER = "cluster"
909
  TNODE = "node"
910
  TINSTANCE = "instance"
911

    
912
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
913
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
914
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
915
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
916
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
917
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
918
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
919
  ENODEDRBD = (TNODE, "ENODEDRBD")
920
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
921
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
922
  ENODEHV = (TNODE, "ENODEHV")
923
  ENODELVM = (TNODE, "ENODELVM")
924
  ENODEN1 = (TNODE, "ENODEN1")
925
  ENODENET = (TNODE, "ENODENET")
926
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
927
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
928
  ENODERPC = (TNODE, "ENODERPC")
929
  ENODESSH = (TNODE, "ENODESSH")
930
  ENODEVERSION = (TNODE, "ENODEVERSION")
931
  ENODESETUP = (TNODE, "ENODESETUP")
932

    
933
  ETYPE_FIELD = "code"
934
  ETYPE_ERROR = "ERROR"
935
  ETYPE_WARNING = "WARNING"
936

    
937
  def ExpandNames(self):
938
    self.needed_locks = {
939
      locking.LEVEL_NODE: locking.ALL_SET,
940
      locking.LEVEL_INSTANCE: locking.ALL_SET,
941
    }
942
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
943

    
944
  def _Error(self, ecode, item, msg, *args, **kwargs):
945
    """Format an error message.
946

947
    Based on the opcode's error_codes parameter, either format a
948
    parseable error code, or a simpler error string.
949

950
    This must be called only from Exec and functions called from Exec.
951

952
    """
953
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
954
    itype, etxt = ecode
955
    # first complete the msg
956
    if args:
957
      msg = msg % args
958
    # then format the whole message
959
    if self.op.error_codes:
960
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
961
    else:
962
      if item:
963
        item = " " + item
964
      else:
965
        item = ""
966
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
967
    # and finally report it via the feedback_fn
968
    self._feedback_fn("  - %s" % msg)
969

    
970
  def _ErrorIf(self, cond, *args, **kwargs):
971
    """Log an error message if the passed condition is True.
972

973
    """
974
    cond = bool(cond) or self.op.debug_simulate_errors
975
    if cond:
976
      self._Error(*args, **kwargs)
977
    # do not mark the operation as failed for WARN cases only
978
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
979
      self.bad = self.bad or cond
980

    
981
  def _VerifyNode(self, nodeinfo, file_list, local_cksum,
982
                  node_result, master_files, drbd_map, vg_name):
983
    """Run multiple tests against a node.
984

985
    Test list:
986

987
      - compares ganeti version
988
      - checks vg existence and size > 20G
989
      - checks config file checksum
990
      - checks ssh to other nodes
991

992
    @type nodeinfo: L{objects.Node}
993
    @param nodeinfo: the node to check
994
    @param file_list: required list of files
995
    @param local_cksum: dictionary of local files and their checksums
996
    @param node_result: the results from the node
997
    @param master_files: list of files that only masters should have
998
    @param drbd_map: the useddrbd minors for this node, in
999
        form of minor: (instance, must_exist) which correspond to instances
1000
        and their running status
1001
    @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
1002

1003
    """
1004
    node = nodeinfo.name
1005
    _ErrorIf = self._ErrorIf
1006

    
1007
    # main result, node_result should be a non-empty dict
1008
    test = not node_result or not isinstance(node_result, dict)
1009
    _ErrorIf(test, self.ENODERPC, node,
1010
                  "unable to verify node: no data returned")
1011
    if test:
1012
      return
1013

    
1014
    # compares ganeti version
1015
    local_version = constants.PROTOCOL_VERSION
1016
    remote_version = node_result.get('version', None)
1017
    test = not (remote_version and
1018
                isinstance(remote_version, (list, tuple)) and
1019
                len(remote_version) == 2)
1020
    _ErrorIf(test, self.ENODERPC, node,
1021
             "connection to node returned invalid data")
1022
    if test:
1023
      return
1024

    
1025
    test = local_version != remote_version[0]
1026
    _ErrorIf(test, self.ENODEVERSION, node,
1027
             "incompatible protocol versions: master %s,"
1028
             " node %s", local_version, remote_version[0])
1029
    if test:
1030
      return
1031

    
1032
    # node seems compatible, we can actually try to look into its results
1033

    
1034
    # full package version
1035
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1036
                  self.ENODEVERSION, node,
1037
                  "software version mismatch: master %s, node %s",
1038
                  constants.RELEASE_VERSION, remote_version[1],
1039
                  code=self.ETYPE_WARNING)
1040

    
1041
    # checks vg existence and size > 20G
1042
    if vg_name is not None:
1043
      vglist = node_result.get(constants.NV_VGLIST, None)
1044
      test = not vglist
1045
      _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1046
      if not test:
1047
        vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1048
                                              constants.MIN_VG_SIZE)
1049
        _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1050

    
1051
    # checks config file checksum
1052

    
1053
    remote_cksum = node_result.get(constants.NV_FILELIST, None)
1054
    test = not isinstance(remote_cksum, dict)
1055
    _ErrorIf(test, self.ENODEFILECHECK, node,
1056
             "node hasn't returned file checksum data")
1057
    if not test:
1058
      for file_name in file_list:
1059
        node_is_mc = nodeinfo.master_candidate
1060
        must_have = (file_name not in master_files) or node_is_mc
1061
        # missing
1062
        test1 = file_name not in remote_cksum
1063
        # invalid checksum
1064
        test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1065
        # existing and good
1066
        test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1067
        _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1068
                 "file '%s' missing", file_name)
1069
        _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1070
                 "file '%s' has wrong checksum", file_name)
1071
        # not candidate and this is not a must-have file
1072
        _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1073
                 "file '%s' should not exist on non master"
1074
                 " candidates (and the file is outdated)", file_name)
1075
        # all good, except non-master/non-must have combination
1076
        _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1077
                 "file '%s' should not exist"
1078
                 " on non master candidates", file_name)
1079

    
1080
    # checks ssh to any
1081

    
1082
    test = constants.NV_NODELIST not in node_result
1083
    _ErrorIf(test, self.ENODESSH, node,
1084
             "node hasn't returned node ssh connectivity data")
1085
    if not test:
1086
      if node_result[constants.NV_NODELIST]:
1087
        for a_node, a_msg in node_result[constants.NV_NODELIST].items():
1088
          _ErrorIf(True, self.ENODESSH, node,
1089
                   "ssh communication with node '%s': %s", a_node, a_msg)
1090

    
1091
    test = constants.NV_NODENETTEST not in node_result
1092
    _ErrorIf(test, self.ENODENET, node,
1093
             "node hasn't returned node tcp connectivity data")
1094
    if not test:
1095
      if node_result[constants.NV_NODENETTEST]:
1096
        nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
1097
        for anode in nlist:
1098
          _ErrorIf(True, self.ENODENET, node,
1099
                   "tcp communication with node '%s': %s",
1100
                   anode, node_result[constants.NV_NODENETTEST][anode])
1101

    
1102
    hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
1103
    if isinstance(hyp_result, dict):
1104
      for hv_name, hv_result in hyp_result.iteritems():
1105
        test = hv_result is not None
1106
        _ErrorIf(test, self.ENODEHV, node,
1107
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1108

    
1109
    # check used drbd list
1110
    if vg_name is not None:
1111
      used_minors = node_result.get(constants.NV_DRBDLIST, [])
1112
      test = not isinstance(used_minors, (tuple, list))
1113
      _ErrorIf(test, self.ENODEDRBD, node,
1114
               "cannot parse drbd status file: %s", str(used_minors))
1115
      if not test:
1116
        for minor, (iname, must_exist) in drbd_map.items():
1117
          test = minor not in used_minors and must_exist
1118
          _ErrorIf(test, self.ENODEDRBD, node,
1119
                   "drbd minor %d of instance %s is not active",
1120
                   minor, iname)
1121
        for minor in used_minors:
1122
          test = minor not in drbd_map
1123
          _ErrorIf(test, self.ENODEDRBD, node,
1124
                   "unallocated drbd minor %d is in use", minor)
1125
    test = node_result.get(constants.NV_NODESETUP,
1126
                           ["Missing NODESETUP results"])
1127
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1128
             "; ".join(test))
1129

    
1130
    # check pv names
1131
    if vg_name is not None:
1132
      pvlist = node_result.get(constants.NV_PVLIST, None)
1133
      test = pvlist is None
1134
      _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1135
      if not test:
1136
        # check that ':' is not present in PV names, since it's a
1137
        # special character for lvcreate (denotes the range of PEs to
1138
        # use on the PV)
1139
        for size, pvname, owner_vg in pvlist:
1140
          test = ":" in pvname
1141
          _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1142
                   " '%s' of VG '%s'", pvname, owner_vg)
1143

    
1144
  def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
1145
                      node_instance, n_offline):
1146
    """Verify an instance.
1147

1148
    This function checks to see if the required block devices are
1149
    available on the instance's node.
1150

1151
    """
1152
    _ErrorIf = self._ErrorIf
1153
    node_current = instanceconfig.primary_node
1154

    
1155
    node_vol_should = {}
1156
    instanceconfig.MapLVsByNode(node_vol_should)
1157

    
1158
    for node in node_vol_should:
1159
      if node in n_offline:
1160
        # ignore missing volumes on offline nodes
1161
        continue
1162
      for volume in node_vol_should[node]:
1163
        test = node not in node_vol_is or volume not in node_vol_is[node]
1164
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1165
                 "volume %s missing on node %s", volume, node)
1166

    
1167
    if instanceconfig.admin_up:
1168
      test = ((node_current not in node_instance or
1169
               not instance in node_instance[node_current]) and
1170
              node_current not in n_offline)
1171
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1172
               "instance not running on its primary node %s",
1173
               node_current)
1174

    
1175
    for node in node_instance:
1176
      if (not node == node_current):
1177
        test = instance in node_instance[node]
1178
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1179
                 "instance should not run on node %s", node)
1180

    
1181
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is):
1182
    """Verify if there are any unknown volumes in the cluster.
1183

1184
    The .os, .swap and backup volumes are ignored. All other volumes are
1185
    reported as unknown.
1186

1187
    """
1188
    for node in node_vol_is:
1189
      for volume in node_vol_is[node]:
1190
        test = (node not in node_vol_should or
1191
                volume not in node_vol_should[node])
1192
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1193
                      "volume %s is unknown", volume)
1194

    
1195
  def _VerifyOrphanInstances(self, instancelist, node_instance):
1196
    """Verify the list of running instances.
1197

1198
    This checks what instances are running but unknown to the cluster.
1199

1200
    """
1201
    for node in node_instance:
1202
      for o_inst in node_instance[node]:
1203
        test = o_inst not in instancelist
1204
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1205
                      "instance %s on node %s should not exist", o_inst, node)
1206

    
1207
  def _VerifyNPlusOneMemory(self, node_info, instance_cfg):
1208
    """Verify N+1 Memory Resilience.
1209

1210
    Check that if one single node dies we can still start all the instances it
1211
    was primary for.
1212

1213
    """
1214
    for node, nodeinfo in node_info.iteritems():
1215
      # This code checks that every node which is now listed as secondary has
1216
      # enough memory to host all instances it is supposed to should a single
1217
      # other node in the cluster fail.
1218
      # FIXME: not ready for failover to an arbitrary node
1219
      # FIXME: does not support file-backed instances
1220
      # WARNING: we currently take into account down instances as well as up
1221
      # ones, considering that even if they're down someone might want to start
1222
      # them even in the event of a node failure.
1223
      for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
1224
        needed_mem = 0
1225
        for instance in instances:
1226
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1227
          if bep[constants.BE_AUTO_BALANCE]:
1228
            needed_mem += bep[constants.BE_MEMORY]
1229
        test = nodeinfo['mfree'] < needed_mem
1230
        self._ErrorIf(test, self.ENODEN1, node,
1231
                      "not enough memory on to accommodate"
1232
                      " failovers should peer node %s fail", prinode)
1233

    
1234
  def CheckPrereq(self):
1235
    """Check prerequisites.
1236

1237
    Transform the list of checks we're going to skip into a set and check that
1238
    all its members are valid.
1239

1240
    """
1241
    self.skip_set = frozenset(self.op.skip_checks)
1242
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1243
      raise errors.OpPrereqError("Invalid checks to be skipped specified",
1244
                                 errors.ECODE_INVAL)
1245

    
1246
  def BuildHooksEnv(self):
1247
    """Build hooks env.
1248

1249
    Cluster-Verify hooks just ran in the post phase and their failure makes
1250
    the output be logged in the verify output and the verification to fail.
1251

1252
    """
1253
    all_nodes = self.cfg.GetNodeList()
1254
    env = {
1255
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1256
      }
1257
    for node in self.cfg.GetAllNodesInfo().values():
1258
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1259

    
1260
    return env, [], all_nodes
1261

    
1262
  def Exec(self, feedback_fn):
1263
    """Verify integrity of cluster, performing various test on nodes.
1264

1265
    """
1266
    self.bad = False
1267
    _ErrorIf = self._ErrorIf
1268
    verbose = self.op.verbose
1269
    self._feedback_fn = feedback_fn
1270
    feedback_fn("* Verifying global settings")
1271
    for msg in self.cfg.VerifyConfig():
1272
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1273

    
1274
    vg_name = self.cfg.GetVGName()
1275
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1276
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1277
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1278
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1279
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1280
                        for iname in instancelist)
1281
    i_non_redundant = [] # Non redundant instances
1282
    i_non_a_balanced = [] # Non auto-balanced instances
1283
    n_offline = [] # List of offline nodes
1284
    n_drained = [] # List of nodes being drained
1285
    node_volume = {}
1286
    node_instance = {}
1287
    node_info = {}
1288
    instance_cfg = {}
1289

    
1290
    # FIXME: verify OS list
1291
    # do local checksums
1292
    master_files = [constants.CLUSTER_CONF_FILE]
1293

    
1294
    file_names = ssconf.SimpleStore().GetFileList()
1295
    file_names.append(constants.SSL_CERT_FILE)
1296
    file_names.append(constants.RAPI_CERT_FILE)
1297
    file_names.extend(master_files)
1298

    
1299
    local_checksums = utils.FingerprintFiles(file_names)
1300

    
1301
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1302
    node_verify_param = {
1303
      constants.NV_FILELIST: file_names,
1304
      constants.NV_NODELIST: [node.name for node in nodeinfo
1305
                              if not node.offline],
1306
      constants.NV_HYPERVISOR: hypervisors,
1307
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1308
                                  node.secondary_ip) for node in nodeinfo
1309
                                 if not node.offline],
1310
      constants.NV_INSTANCELIST: hypervisors,
1311
      constants.NV_VERSION: None,
1312
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1313
      constants.NV_NODESETUP: None,
1314
      }
1315
    if vg_name is not None:
1316
      node_verify_param[constants.NV_VGLIST] = None
1317
      node_verify_param[constants.NV_LVLIST] = vg_name
1318
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1319
      node_verify_param[constants.NV_DRBDLIST] = None
1320
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1321
                                           self.cfg.GetClusterName())
1322

    
1323
    cluster = self.cfg.GetClusterInfo()
1324
    master_node = self.cfg.GetMasterNode()
1325
    all_drbd_map = self.cfg.ComputeDRBDMap()
1326

    
1327
    feedback_fn("* Verifying node status")
1328
    for node_i in nodeinfo:
1329
      node = node_i.name
1330

    
1331
      if node_i.offline:
1332
        if verbose:
1333
          feedback_fn("* Skipping offline node %s" % (node,))
1334
        n_offline.append(node)
1335
        continue
1336

    
1337
      if node == master_node:
1338
        ntype = "master"
1339
      elif node_i.master_candidate:
1340
        ntype = "master candidate"
1341
      elif node_i.drained:
1342
        ntype = "drained"
1343
        n_drained.append(node)
1344
      else:
1345
        ntype = "regular"
1346
      if verbose:
1347
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1348

    
1349
      msg = all_nvinfo[node].fail_msg
1350
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
1351
      if msg:
1352
        continue
1353

    
1354
      nresult = all_nvinfo[node].payload
1355
      node_drbd = {}
1356
      for minor, instance in all_drbd_map[node].items():
1357
        test = instance not in instanceinfo
1358
        _ErrorIf(test, self.ECLUSTERCFG, None,
1359
                 "ghost instance '%s' in temporary DRBD map", instance)
1360
          # ghost instance should not be running, but otherwise we
1361
          # don't give double warnings (both ghost instance and
1362
          # unallocated minor in use)
1363
        if test:
1364
          node_drbd[minor] = (instance, False)
1365
        else:
1366
          instance = instanceinfo[instance]
1367
          node_drbd[minor] = (instance.name, instance.admin_up)
1368
      self._VerifyNode(node_i, file_names, local_checksums,
1369
                       nresult, master_files, node_drbd, vg_name)
1370

    
1371
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1372
      if vg_name is None:
1373
        node_volume[node] = {}
1374
      elif isinstance(lvdata, basestring):
1375
        _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1376
                 utils.SafeEncode(lvdata))
1377
        node_volume[node] = {}
1378
      elif not isinstance(lvdata, dict):
1379
        _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1380
        continue
1381
      else:
1382
        node_volume[node] = lvdata
1383

    
1384
      # node_instance
1385
      idata = nresult.get(constants.NV_INSTANCELIST, None)
1386
      test = not isinstance(idata, list)
1387
      _ErrorIf(test, self.ENODEHV, node,
1388
               "rpc call to node failed (instancelist)")
1389
      if test:
1390
        continue
1391

    
1392
      node_instance[node] = idata
1393

    
1394
      # node_info
1395
      nodeinfo = nresult.get(constants.NV_HVINFO, None)
1396
      test = not isinstance(nodeinfo, dict)
1397
      _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1398
      if test:
1399
        continue
1400

    
1401
      try:
1402
        node_info[node] = {
1403
          "mfree": int(nodeinfo['memory_free']),
1404
          "pinst": [],
1405
          "sinst": [],
1406
          # dictionary holding all instances this node is secondary for,
1407
          # grouped by their primary node. Each key is a cluster node, and each
1408
          # value is a list of instances which have the key as primary and the
1409
          # current node as secondary.  this is handy to calculate N+1 memory
1410
          # availability if you can only failover from a primary to its
1411
          # secondary.
1412
          "sinst-by-pnode": {},
1413
        }
1414
        # FIXME: devise a free space model for file based instances as well
1415
        if vg_name is not None:
1416
          test = (constants.NV_VGLIST not in nresult or
1417
                  vg_name not in nresult[constants.NV_VGLIST])
1418
          _ErrorIf(test, self.ENODELVM, node,
1419
                   "node didn't return data for the volume group '%s'"
1420
                   " - it is either missing or broken", vg_name)
1421
          if test:
1422
            continue
1423
          node_info[node]["dfree"] = int(nresult[constants.NV_VGLIST][vg_name])
1424
      except (ValueError, KeyError):
1425
        _ErrorIf(True, self.ENODERPC, node,
1426
                 "node returned invalid nodeinfo, check lvm/hypervisor")
1427
        continue
1428

    
1429
    node_vol_should = {}
1430

    
1431
    feedback_fn("* Verifying instance status")
1432
    for instance in instancelist:
1433
      if verbose:
1434
        feedback_fn("* Verifying instance %s" % instance)
1435
      inst_config = instanceinfo[instance]
1436
      self._VerifyInstance(instance, inst_config, node_volume,
1437
                           node_instance, n_offline)
1438
      inst_nodes_offline = []
1439

    
1440
      inst_config.MapLVsByNode(node_vol_should)
1441

    
1442
      instance_cfg[instance] = inst_config
1443

    
1444
      pnode = inst_config.primary_node
1445
      _ErrorIf(pnode not in node_info and pnode not in n_offline,
1446
               self.ENODERPC, pnode, "instance %s, connection to"
1447
               " primary node failed", instance)
1448
      if pnode in node_info:
1449
        node_info[pnode]['pinst'].append(instance)
1450

    
1451
      if pnode in n_offline:
1452
        inst_nodes_offline.append(pnode)
1453

    
1454
      # If the instance is non-redundant we cannot survive losing its primary
1455
      # node, so we are not N+1 compliant. On the other hand we have no disk
1456
      # templates with more than one secondary so that situation is not well
1457
      # supported either.
1458
      # FIXME: does not support file-backed instances
1459
      if len(inst_config.secondary_nodes) == 0:
1460
        i_non_redundant.append(instance)
1461
      _ErrorIf(len(inst_config.secondary_nodes) > 1,
1462
               self.EINSTANCELAYOUT, instance,
1463
               "instance has multiple secondary nodes", code="WARNING")
1464

    
1465
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1466
        i_non_a_balanced.append(instance)
1467

    
1468
      for snode in inst_config.secondary_nodes:
1469
        _ErrorIf(snode not in node_info and snode not in n_offline,
1470
                 self.ENODERPC, snode,
1471
                 "instance %s, connection to secondary node"
1472
                 "failed", instance)
1473

    
1474
        if snode in node_info:
1475
          node_info[snode]['sinst'].append(instance)
1476
          if pnode not in node_info[snode]['sinst-by-pnode']:
1477
            node_info[snode]['sinst-by-pnode'][pnode] = []
1478
          node_info[snode]['sinst-by-pnode'][pnode].append(instance)
1479

    
1480
        if snode in n_offline:
1481
          inst_nodes_offline.append(snode)
1482

    
1483
      # warn that the instance lives on offline nodes
1484
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
1485
               "instance lives on offline node(s) %s",
1486
               ", ".join(inst_nodes_offline))
1487

    
1488
    feedback_fn("* Verifying orphan volumes")
1489
    self._VerifyOrphanVolumes(node_vol_should, node_volume)
1490

    
1491
    feedback_fn("* Verifying remaining instances")
1492
    self._VerifyOrphanInstances(instancelist, node_instance)
1493

    
1494
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1495
      feedback_fn("* Verifying N+1 Memory redundancy")
1496
      self._VerifyNPlusOneMemory(node_info, instance_cfg)
1497

    
1498
    feedback_fn("* Other Notes")
1499
    if i_non_redundant:
1500
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1501
                  % len(i_non_redundant))
1502

    
1503
    if i_non_a_balanced:
1504
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1505
                  % len(i_non_a_balanced))
1506

    
1507
    if n_offline:
1508
      feedback_fn("  - NOTICE: %d offline node(s) found." % len(n_offline))
1509

    
1510
    if n_drained:
1511
      feedback_fn("  - NOTICE: %d drained node(s) found." % len(n_drained))
1512

    
1513
    return not self.bad
1514

    
1515
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1516
    """Analyze the post-hooks' result
1517

1518
    This method analyses the hook result, handles it, and sends some
1519
    nicely-formatted feedback back to the user.
1520

1521
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1522
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1523
    @param hooks_results: the results of the multi-node hooks rpc call
1524
    @param feedback_fn: function used send feedback back to the caller
1525
    @param lu_result: previous Exec result
1526
    @return: the new Exec result, based on the previous result
1527
        and hook results
1528

1529
    """
1530
    # We only really run POST phase hooks, and are only interested in
1531
    # their results
1532
    if phase == constants.HOOKS_PHASE_POST:
1533
      # Used to change hooks' output to proper indentation
1534
      indent_re = re.compile('^', re.M)
1535
      feedback_fn("* Hooks Results")
1536
      assert hooks_results, "invalid result from hooks"
1537

    
1538
      for node_name in hooks_results:
1539
        show_node_header = True
1540
        res = hooks_results[node_name]
1541
        msg = res.fail_msg
1542
        test = msg and not res.offline
1543
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
1544
                      "Communication failure in hooks execution: %s", msg)
1545
        if test:
1546
          # override manually lu_result here as _ErrorIf only
1547
          # overrides self.bad
1548
          lu_result = 1
1549
          continue
1550
        for script, hkr, output in res.payload:
1551
          test = hkr == constants.HKR_FAIL
1552
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
1553
                        "Script %s failed, output:", script)
1554
          if test:
1555
            output = indent_re.sub('      ', output)
1556
            feedback_fn("%s" % output)
1557
            lu_result = 1
1558

    
1559
      return lu_result
1560

    
1561

    
1562
class LUVerifyDisks(NoHooksLU):
1563
  """Verifies the cluster disks status.
1564

1565
  """
1566
  _OP_REQP = []
1567
  REQ_BGL = False
1568

    
1569
  def ExpandNames(self):
1570
    self.needed_locks = {
1571
      locking.LEVEL_NODE: locking.ALL_SET,
1572
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1573
    }
1574
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1575

    
1576
  def CheckPrereq(self):
1577
    """Check prerequisites.
1578

1579
    This has no prerequisites.
1580

1581
    """
1582
    pass
1583

    
1584
  def Exec(self, feedback_fn):
1585
    """Verify integrity of cluster disks.
1586

1587
    @rtype: tuple of three items
1588
    @return: a tuple of (dict of node-to-node_error, list of instances
1589
        which need activate-disks, dict of instance: (node, volume) for
1590
        missing volumes
1591

1592
    """
1593
    result = res_nodes, res_instances, res_missing = {}, [], {}
1594

    
1595
    vg_name = self.cfg.GetVGName()
1596
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1597
    instances = [self.cfg.GetInstanceInfo(name)
1598
                 for name in self.cfg.GetInstanceList()]
1599

    
1600
    nv_dict = {}
1601
    for inst in instances:
1602
      inst_lvs = {}
1603
      if (not inst.admin_up or
1604
          inst.disk_template not in constants.DTS_NET_MIRROR):
1605
        continue
1606
      inst.MapLVsByNode(inst_lvs)
1607
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1608
      for node, vol_list in inst_lvs.iteritems():
1609
        for vol in vol_list:
1610
          nv_dict[(node, vol)] = inst
1611

    
1612
    if not nv_dict:
1613
      return result
1614

    
1615
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1616

    
1617
    for node in nodes:
1618
      # node_volume
1619
      node_res = node_lvs[node]
1620
      if node_res.offline:
1621
        continue
1622
      msg = node_res.fail_msg
1623
      if msg:
1624
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1625
        res_nodes[node] = msg
1626
        continue
1627

    
1628
      lvs = node_res.payload
1629
      for lv_name, (_, lv_inactive, lv_online) in lvs.items():
1630
        inst = nv_dict.pop((node, lv_name), None)
1631
        if (not lv_online and inst is not None
1632
            and inst.name not in res_instances):
1633
          res_instances.append(inst.name)
1634

    
1635
    # any leftover items in nv_dict are missing LVs, let's arrange the
1636
    # data better
1637
    for key, inst in nv_dict.iteritems():
1638
      if inst.name not in res_missing:
1639
        res_missing[inst.name] = []
1640
      res_missing[inst.name].append(key)
1641

    
1642
    return result
1643

    
1644

    
1645
class LURepairDiskSizes(NoHooksLU):
1646
  """Verifies the cluster disks sizes.
1647

1648
  """
1649
  _OP_REQP = ["instances"]
1650
  REQ_BGL = False
1651

    
1652
  def ExpandNames(self):
1653
    if not isinstance(self.op.instances, list):
1654
      raise errors.OpPrereqError("Invalid argument type 'instances'",
1655
                                 errors.ECODE_INVAL)
1656

    
1657
    if self.op.instances:
1658
      self.wanted_names = []
1659
      for name in self.op.instances:
1660
        full_name = self.cfg.ExpandInstanceName(name)
1661
        if full_name is None:
1662
          raise errors.OpPrereqError("Instance '%s' not known" % name,
1663
                                     errors.ECODE_NOENT)
1664
        self.wanted_names.append(full_name)
1665
      self.needed_locks = {
1666
        locking.LEVEL_NODE: [],
1667
        locking.LEVEL_INSTANCE: self.wanted_names,
1668
        }
1669
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1670
    else:
1671
      self.wanted_names = None
1672
      self.needed_locks = {
1673
        locking.LEVEL_NODE: locking.ALL_SET,
1674
        locking.LEVEL_INSTANCE: locking.ALL_SET,
1675
        }
1676
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1677

    
1678
  def DeclareLocks(self, level):
1679
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
1680
      self._LockInstancesNodes(primary_only=True)
1681

    
1682
  def CheckPrereq(self):
1683
    """Check prerequisites.
1684

1685
    This only checks the optional instance list against the existing names.
1686

1687
    """
1688
    if self.wanted_names is None:
1689
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
1690

    
1691
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
1692
                             in self.wanted_names]
1693

    
1694
  def _EnsureChildSizes(self, disk):
1695
    """Ensure children of the disk have the needed disk size.
1696

1697
    This is valid mainly for DRBD8 and fixes an issue where the
1698
    children have smaller disk size.
1699

1700
    @param disk: an L{ganeti.objects.Disk} object
1701

1702
    """
1703
    if disk.dev_type == constants.LD_DRBD8:
1704
      assert disk.children, "Empty children for DRBD8?"
1705
      fchild = disk.children[0]
1706
      mismatch = fchild.size < disk.size
1707
      if mismatch:
1708
        self.LogInfo("Child disk has size %d, parent %d, fixing",
1709
                     fchild.size, disk.size)
1710
        fchild.size = disk.size
1711

    
1712
      # and we recurse on this child only, not on the metadev
1713
      return self._EnsureChildSizes(fchild) or mismatch
1714
    else:
1715
      return False
1716

    
1717
  def Exec(self, feedback_fn):
1718
    """Verify the size of cluster disks.
1719

1720
    """
1721
    # TODO: check child disks too
1722
    # TODO: check differences in size between primary/secondary nodes
1723
    per_node_disks = {}
1724
    for instance in self.wanted_instances:
1725
      pnode = instance.primary_node
1726
      if pnode not in per_node_disks:
1727
        per_node_disks[pnode] = []
1728
      for idx, disk in enumerate(instance.disks):
1729
        per_node_disks[pnode].append((instance, idx, disk))
1730

    
1731
    changed = []
1732
    for node, dskl in per_node_disks.items():
1733
      newl = [v[2].Copy() for v in dskl]
1734
      for dsk in newl:
1735
        self.cfg.SetDiskID(dsk, node)
1736
      result = self.rpc.call_blockdev_getsizes(node, newl)
1737
      if result.fail_msg:
1738
        self.LogWarning("Failure in blockdev_getsizes call to node"
1739
                        " %s, ignoring", node)
1740
        continue
1741
      if len(result.data) != len(dskl):
1742
        self.LogWarning("Invalid result from node %s, ignoring node results",
1743
                        node)
1744
        continue
1745
      for ((instance, idx, disk), size) in zip(dskl, result.data):
1746
        if size is None:
1747
          self.LogWarning("Disk %d of instance %s did not return size"
1748
                          " information, ignoring", idx, instance.name)
1749
          continue
1750
        if not isinstance(size, (int, long)):
1751
          self.LogWarning("Disk %d of instance %s did not return valid"
1752
                          " size information, ignoring", idx, instance.name)
1753
          continue
1754
        size = size >> 20
1755
        if size != disk.size:
1756
          self.LogInfo("Disk %d of instance %s has mismatched size,"
1757
                       " correcting: recorded %d, actual %d", idx,
1758
                       instance.name, disk.size, size)
1759
          disk.size = size
1760
          self.cfg.Update(instance, feedback_fn)
1761
          changed.append((instance.name, idx, size))
1762
        if self._EnsureChildSizes(disk):
1763
          self.cfg.Update(instance, feedback_fn)
1764
          changed.append((instance.name, idx, disk.size))
1765
    return changed
1766

    
1767

    
1768
class LURenameCluster(LogicalUnit):
1769
  """Rename the cluster.
1770

1771
  """
1772
  HPATH = "cluster-rename"
1773
  HTYPE = constants.HTYPE_CLUSTER
1774
  _OP_REQP = ["name"]
1775

    
1776
  def BuildHooksEnv(self):
1777
    """Build hooks env.
1778

1779
    """
1780
    env = {
1781
      "OP_TARGET": self.cfg.GetClusterName(),
1782
      "NEW_NAME": self.op.name,
1783
      }
1784
    mn = self.cfg.GetMasterNode()
1785
    return env, [mn], [mn]
1786

    
1787
  def CheckPrereq(self):
1788
    """Verify that the passed name is a valid one.
1789

1790
    """
1791
    hostname = utils.HostInfo(self.op.name)
1792

    
1793
    new_name = hostname.name
1794
    self.ip = new_ip = hostname.ip
1795
    old_name = self.cfg.GetClusterName()
1796
    old_ip = self.cfg.GetMasterIP()
1797
    if new_name == old_name and new_ip == old_ip:
1798
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
1799
                                 " cluster has changed",
1800
                                 errors.ECODE_INVAL)
1801
    if new_ip != old_ip:
1802
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1803
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
1804
                                   " reachable on the network. Aborting." %
1805
                                   new_ip, errors.ECODE_NOTUNIQUE)
1806

    
1807
    self.op.name = new_name
1808

    
1809
  def Exec(self, feedback_fn):
1810
    """Rename the cluster.
1811

1812
    """
1813
    clustername = self.op.name
1814
    ip = self.ip
1815

    
1816
    # shutdown the master IP
1817
    master = self.cfg.GetMasterNode()
1818
    result = self.rpc.call_node_stop_master(master, False)
1819
    result.Raise("Could not disable the master role")
1820

    
1821
    try:
1822
      cluster = self.cfg.GetClusterInfo()
1823
      cluster.cluster_name = clustername
1824
      cluster.master_ip = ip
1825
      self.cfg.Update(cluster, feedback_fn)
1826

    
1827
      # update the known hosts file
1828
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
1829
      node_list = self.cfg.GetNodeList()
1830
      try:
1831
        node_list.remove(master)
1832
      except ValueError:
1833
        pass
1834
      result = self.rpc.call_upload_file(node_list,
1835
                                         constants.SSH_KNOWN_HOSTS_FILE)
1836
      for to_node, to_result in result.iteritems():
1837
        msg = to_result.fail_msg
1838
        if msg:
1839
          msg = ("Copy of file %s to node %s failed: %s" %
1840
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
1841
          self.proc.LogWarning(msg)
1842

    
1843
    finally:
1844
      result = self.rpc.call_node_start_master(master, False, False)
1845
      msg = result.fail_msg
1846
      if msg:
1847
        self.LogWarning("Could not re-enable the master role on"
1848
                        " the master, please restart manually: %s", msg)
1849

    
1850

    
1851
def _RecursiveCheckIfLVMBased(disk):
1852
  """Check if the given disk or its children are lvm-based.
1853

1854
  @type disk: L{objects.Disk}
1855
  @param disk: the disk to check
1856
  @rtype: boolean
1857
  @return: boolean indicating whether a LD_LV dev_type was found or not
1858

1859
  """
1860
  if disk.children:
1861
    for chdisk in disk.children:
1862
      if _RecursiveCheckIfLVMBased(chdisk):
1863
        return True
1864
  return disk.dev_type == constants.LD_LV
1865

    
1866

    
1867
class LUSetClusterParams(LogicalUnit):
1868
  """Change the parameters of the cluster.
1869

1870
  """
1871
  HPATH = "cluster-modify"
1872
  HTYPE = constants.HTYPE_CLUSTER
1873
  _OP_REQP = []
1874
  REQ_BGL = False
1875

    
1876
  def CheckArguments(self):
1877
    """Check parameters
1878

1879
    """
1880
    if not hasattr(self.op, "candidate_pool_size"):
1881
      self.op.candidate_pool_size = None
1882
    if self.op.candidate_pool_size is not None:
1883
      try:
1884
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
1885
      except (ValueError, TypeError), err:
1886
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
1887
                                   str(err), errors.ECODE_INVAL)
1888
      if self.op.candidate_pool_size < 1:
1889
        raise errors.OpPrereqError("At least one master candidate needed",
1890
                                   errors.ECODE_INVAL)
1891

    
1892
  def ExpandNames(self):
1893
    # FIXME: in the future maybe other cluster params won't require checking on
1894
    # all nodes to be modified.
1895
    self.needed_locks = {
1896
      locking.LEVEL_NODE: locking.ALL_SET,
1897
    }
1898
    self.share_locks[locking.LEVEL_NODE] = 1
1899

    
1900
  def BuildHooksEnv(self):
1901
    """Build hooks env.
1902

1903
    """
1904
    env = {
1905
      "OP_TARGET": self.cfg.GetClusterName(),
1906
      "NEW_VG_NAME": self.op.vg_name,
1907
      }
1908
    mn = self.cfg.GetMasterNode()
1909
    return env, [mn], [mn]
1910

    
1911
  def CheckPrereq(self):
1912
    """Check prerequisites.
1913

1914
    This checks whether the given params don't conflict and
1915
    if the given volume group is valid.
1916

1917
    """
1918
    if self.op.vg_name is not None and not self.op.vg_name:
1919
      instances = self.cfg.GetAllInstancesInfo().values()
1920
      for inst in instances:
1921
        for disk in inst.disks:
1922
          if _RecursiveCheckIfLVMBased(disk):
1923
            raise errors.OpPrereqError("Cannot disable lvm storage while"
1924
                                       " lvm-based instances exist",
1925
                                       errors.ECODE_INVAL)
1926

    
1927
    node_list = self.acquired_locks[locking.LEVEL_NODE]
1928

    
1929
    # if vg_name not None, checks given volume group on all nodes
1930
    if self.op.vg_name:
1931
      vglist = self.rpc.call_vg_list(node_list)
1932
      for node in node_list:
1933
        msg = vglist[node].fail_msg
1934
        if msg:
1935
          # ignoring down node
1936
          self.LogWarning("Error while gathering data on node %s"
1937
                          " (ignoring node): %s", node, msg)
1938
          continue
1939
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
1940
                                              self.op.vg_name,
1941
                                              constants.MIN_VG_SIZE)
1942
        if vgstatus:
1943
          raise errors.OpPrereqError("Error on node '%s': %s" %
1944
                                     (node, vgstatus), errors.ECODE_ENVIRON)
1945

    
1946
    self.cluster = cluster = self.cfg.GetClusterInfo()
1947
    # validate params changes
1948
    if self.op.beparams:
1949
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
1950
      self.new_beparams = objects.FillDict(
1951
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
1952

    
1953
    if self.op.nicparams:
1954
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
1955
      self.new_nicparams = objects.FillDict(
1956
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
1957
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
1958

    
1959
    # hypervisor list/parameters
1960
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
1961
    if self.op.hvparams:
1962
      if not isinstance(self.op.hvparams, dict):
1963
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input",
1964
                                   errors.ECODE_INVAL)
1965
      for hv_name, hv_dict in self.op.hvparams.items():
1966
        if hv_name not in self.new_hvparams:
1967
          self.new_hvparams[hv_name] = hv_dict
1968
        else:
1969
          self.new_hvparams[hv_name].update(hv_dict)
1970

    
1971
    if self.op.enabled_hypervisors is not None:
1972
      self.hv_list = self.op.enabled_hypervisors
1973
      if not self.hv_list:
1974
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
1975
                                   " least one member",
1976
                                   errors.ECODE_INVAL)
1977
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
1978
      if invalid_hvs:
1979
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
1980
                                   " entries: %s" % " ,".join(invalid_hvs),
1981
                                   errors.ECODE_INVAL)
1982
    else:
1983
      self.hv_list = cluster.enabled_hypervisors
1984

    
1985
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
1986
      # either the enabled list has changed, or the parameters have, validate
1987
      for hv_name, hv_params in self.new_hvparams.items():
1988
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
1989
            (self.op.enabled_hypervisors and
1990
             hv_name in self.op.enabled_hypervisors)):
1991
          # either this is a new hypervisor, or its parameters have changed
1992
          hv_class = hypervisor.GetHypervisor(hv_name)
1993
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
1994
          hv_class.CheckParameterSyntax(hv_params)
1995
          _CheckHVParams(self, node_list, hv_name, hv_params)
1996

    
1997
  def Exec(self, feedback_fn):
1998
    """Change the parameters of the cluster.
1999

2000
    """
2001
    if self.op.vg_name is not None:
2002
      new_volume = self.op.vg_name
2003
      if not new_volume:
2004
        new_volume = None
2005
      if new_volume != self.cfg.GetVGName():
2006
        self.cfg.SetVGName(new_volume)
2007
      else:
2008
        feedback_fn("Cluster LVM configuration already in desired"
2009
                    " state, not changing")
2010
    if self.op.hvparams:
2011
      self.cluster.hvparams = self.new_hvparams
2012
    if self.op.enabled_hypervisors is not None:
2013
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2014
    if self.op.beparams:
2015
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2016
    if self.op.nicparams:
2017
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2018

    
2019
    if self.op.candidate_pool_size is not None:
2020
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2021
      # we need to update the pool size here, otherwise the save will fail
2022
      _AdjustCandidatePool(self, [])
2023

    
2024
    self.cfg.Update(self.cluster, feedback_fn)
2025

    
2026

    
2027
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2028
  """Distribute additional files which are part of the cluster configuration.
2029

2030
  ConfigWriter takes care of distributing the config and ssconf files, but
2031
  there are more files which should be distributed to all nodes. This function
2032
  makes sure those are copied.
2033

2034
  @param lu: calling logical unit
2035
  @param additional_nodes: list of nodes not in the config to distribute to
2036

2037
  """
2038
  # 1. Gather target nodes
2039
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2040
  dist_nodes = lu.cfg.GetNodeList()
2041
  if additional_nodes is not None:
2042
    dist_nodes.extend(additional_nodes)
2043
  if myself.name in dist_nodes:
2044
    dist_nodes.remove(myself.name)
2045

    
2046
  # 2. Gather files to distribute
2047
  dist_files = set([constants.ETC_HOSTS,
2048
                    constants.SSH_KNOWN_HOSTS_FILE,
2049
                    constants.RAPI_CERT_FILE,
2050
                    constants.RAPI_USERS_FILE,
2051
                    constants.HMAC_CLUSTER_KEY,
2052
                   ])
2053

    
2054
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2055
  for hv_name in enabled_hypervisors:
2056
    hv_class = hypervisor.GetHypervisor(hv_name)
2057
    dist_files.update(hv_class.GetAncillaryFiles())
2058

    
2059
  # 3. Perform the files upload
2060
  for fname in dist_files:
2061
    if os.path.exists(fname):
2062
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2063
      for to_node, to_result in result.items():
2064
        msg = to_result.fail_msg
2065
        if msg:
2066
          msg = ("Copy of file %s to node %s failed: %s" %
2067
                 (fname, to_node, msg))
2068
          lu.proc.LogWarning(msg)
2069

    
2070

    
2071
class LURedistributeConfig(NoHooksLU):
2072
  """Force the redistribution of cluster configuration.
2073

2074
  This is a very simple LU.
2075

2076
  """
2077
  _OP_REQP = []
2078
  REQ_BGL = False
2079

    
2080
  def ExpandNames(self):
2081
    self.needed_locks = {
2082
      locking.LEVEL_NODE: locking.ALL_SET,
2083
    }
2084
    self.share_locks[locking.LEVEL_NODE] = 1
2085

    
2086
  def CheckPrereq(self):
2087
    """Check prerequisites.
2088

2089
    """
2090

    
2091
  def Exec(self, feedback_fn):
2092
    """Redistribute the configuration.
2093

2094
    """
2095
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2096
    _RedistributeAncillaryFiles(self)
2097

    
2098

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

2102
  """
2103
  if not instance.disks:
2104
    return True
2105

    
2106
  if not oneshot:
2107
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2108

    
2109
  node = instance.primary_node
2110

    
2111
  for dev in instance.disks:
2112
    lu.cfg.SetDiskID(dev, node)
2113

    
2114
  # TODO: Convert to utils.Retry
2115

    
2116
  retries = 0
2117
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2118
  while True:
2119
    max_time = 0
2120
    done = True
2121
    cumul_degraded = False
2122
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
2123
    msg = rstats.fail_msg
2124
    if msg:
2125
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2126
      retries += 1
2127
      if retries >= 10:
2128
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2129
                                 " aborting." % node)
2130
      time.sleep(6)
2131
      continue
2132
    rstats = rstats.payload
2133
    retries = 0
2134
    for i, mstat in enumerate(rstats):
2135
      if mstat is None:
2136
        lu.LogWarning("Can't compute data for node %s/%s",
2137
                           node, instance.disks[i].iv_name)
2138
        continue
2139

    
2140
      cumul_degraded = (cumul_degraded or
2141
                        (mstat.is_degraded and mstat.sync_percent is None))
2142
      if mstat.sync_percent is not None:
2143
        done = False
2144
        if mstat.estimated_time is not None:
2145
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
2146
          max_time = mstat.estimated_time
2147
        else:
2148
          rem_time = "no time estimate"
2149
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2150
                        (instance.disks[i].iv_name, mstat.sync_percent,
2151
                         rem_time))
2152

    
2153
    # if we're done but degraded, let's do a few small retries, to
2154
    # make sure we see a stable and not transient situation; therefore
2155
    # we force restart of the loop
2156
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2157
      logging.info("Degraded disks found, %d retries left", degr_retries)
2158
      degr_retries -= 1
2159
      time.sleep(1)
2160
      continue
2161

    
2162
    if done or oneshot:
2163
      break
2164

    
2165
    time.sleep(min(60, max_time))
2166

    
2167
  if done:
2168
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2169
  return not cumul_degraded
2170

    
2171

    
2172
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2173
  """Check that mirrors are not degraded.
2174

2175
  The ldisk parameter, if True, will change the test from the
2176
  is_degraded attribute (which represents overall non-ok status for
2177
  the device(s)) to the ldisk (representing the local storage status).
2178

2179
  """
2180
  lu.cfg.SetDiskID(dev, node)
2181

    
2182
  result = True
2183

    
2184
  if on_primary or dev.AssembleOnSecondary():
2185
    rstats = lu.rpc.call_blockdev_find(node, dev)
2186
    msg = rstats.fail_msg
2187
    if msg:
2188
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2189
      result = False
2190
    elif not rstats.payload:
2191
      lu.LogWarning("Can't find disk on node %s", node)
2192
      result = False
2193
    else:
2194
      if ldisk:
2195
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2196
      else:
2197
        result = result and not rstats.payload.is_degraded
2198

    
2199
  if dev.children:
2200
    for child in dev.children:
2201
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2202

    
2203
  return result
2204

    
2205

    
2206
class LUDiagnoseOS(NoHooksLU):
2207
  """Logical unit for OS diagnose/query.
2208

2209
  """
2210
  _OP_REQP = ["output_fields", "names"]
2211
  REQ_BGL = False
2212
  _FIELDS_STATIC = utils.FieldSet()
2213
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants")
2214
  # Fields that need calculation of global os validity
2215
  _FIELDS_NEEDVALID = frozenset(["valid", "variants"])
2216

    
2217
  def ExpandNames(self):
2218
    if self.op.names:
2219
      raise errors.OpPrereqError("Selective OS query not supported",
2220
                                 errors.ECODE_INVAL)
2221

    
2222
    _CheckOutputFields(static=self._FIELDS_STATIC,
2223
                       dynamic=self._FIELDS_DYNAMIC,
2224
                       selected=self.op.output_fields)
2225

    
2226
    # Lock all nodes, in shared mode
2227
    # Temporary removal of locks, should be reverted later
2228
    # TODO: reintroduce locks when they are lighter-weight
2229
    self.needed_locks = {}
2230
    #self.share_locks[locking.LEVEL_NODE] = 1
2231
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2232

    
2233
  def CheckPrereq(self):
2234
    """Check prerequisites.
2235

2236
    """
2237

    
2238
  @staticmethod
2239
  def _DiagnoseByOS(node_list, rlist):
2240
    """Remaps a per-node return list into an a per-os per-node dictionary
2241

2242
    @param node_list: a list with the names of all nodes
2243
    @param rlist: a map with node names as keys and OS objects as values
2244

2245
    @rtype: dict
2246
    @return: a dictionary with osnames as keys and as value another map, with
2247
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2248

2249
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2250
                                     (/srv/..., False, "invalid api")],
2251
                           "node2": [(/srv/..., True, "")]}
2252
          }
2253

2254
    """
2255
    all_os = {}
2256
    # we build here the list of nodes that didn't fail the RPC (at RPC
2257
    # level), so that nodes with a non-responding node daemon don't
2258
    # make all OSes invalid
2259
    good_nodes = [node_name for node_name in rlist
2260
                  if not rlist[node_name].fail_msg]
2261
    for node_name, nr in rlist.items():
2262
      if nr.fail_msg or not nr.payload:
2263
        continue
2264
      for name, path, status, diagnose, variants in nr.payload:
2265
        if name not in all_os:
2266
          # build a list of nodes for this os containing empty lists
2267
          # for each node in node_list
2268
          all_os[name] = {}
2269
          for nname in good_nodes:
2270
            all_os[name][nname] = []
2271
        all_os[name][node_name].append((path, status, diagnose, variants))
2272
    return all_os
2273

    
2274
  def Exec(self, feedback_fn):
2275
    """Compute the list of OSes.
2276

2277
    """
2278
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2279
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2280
    pol = self._DiagnoseByOS(valid_nodes, node_data)
2281
    output = []
2282
    calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields)
2283
    calc_variants = "variants" in self.op.output_fields
2284

    
2285
    for os_name, os_data in pol.items():
2286
      row = []
2287
      if calc_valid:
2288
        valid = True
2289
        variants = None
2290
        for osl in os_data.values():
2291
          valid = valid and osl and osl[0][1]
2292
          if not valid:
2293
            variants = None
2294
            break
2295
          if calc_variants:
2296
            node_variants = osl[0][3]
2297
            if variants is None:
2298
              variants = node_variants
2299
            else:
2300
              variants = [v for v in variants if v in node_variants]
2301

    
2302
      for field in self.op.output_fields:
2303
        if field == "name":
2304
          val = os_name
2305
        elif field == "valid":
2306
          val = valid
2307
        elif field == "node_status":
2308
          # this is just a copy of the dict
2309
          val = {}
2310
          for node_name, nos_list in os_data.items():
2311
            val[node_name] = nos_list
2312
        elif field == "variants":
2313
          val =  variants
2314
        else:
2315
          raise errors.ParameterError(field)
2316
        row.append(val)
2317
      output.append(row)
2318

    
2319
    return output
2320

    
2321

    
2322
class LURemoveNode(LogicalUnit):
2323
  """Logical unit for removing a node.
2324

2325
  """
2326
  HPATH = "node-remove"
2327
  HTYPE = constants.HTYPE_NODE
2328
  _OP_REQP = ["node_name"]
2329

    
2330
  def BuildHooksEnv(self):
2331
    """Build hooks env.
2332

2333
    This doesn't run on the target node in the pre phase as a failed
2334
    node would then be impossible to remove.
2335

2336
    """
2337
    env = {
2338
      "OP_TARGET": self.op.node_name,
2339
      "NODE_NAME": self.op.node_name,
2340
      }
2341
    all_nodes = self.cfg.GetNodeList()
2342
    if self.op.node_name in all_nodes:
2343
      all_nodes.remove(self.op.node_name)
2344
    return env, all_nodes, all_nodes
2345

    
2346
  def CheckPrereq(self):
2347
    """Check prerequisites.
2348

2349
    This checks:
2350
     - the node exists in the configuration
2351
     - it does not have primary or secondary instances
2352
     - it's not the master
2353

2354
    Any errors are signaled by raising errors.OpPrereqError.
2355

2356
    """
2357
    node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
2358
    if node is None:
2359
      raise errors.OpPrereqError("Node '%s' is unknown." % self.op.node_name,
2360
                                 errors.ECODE_NOENT)
2361

    
2362
    instance_list = self.cfg.GetInstanceList()
2363

    
2364
    masternode = self.cfg.GetMasterNode()
2365
    if node.name == masternode:
2366
      raise errors.OpPrereqError("Node is the master node,"
2367
                                 " you need to failover first.",
2368
                                 errors.ECODE_INVAL)
2369

    
2370
    for instance_name in instance_list:
2371
      instance = self.cfg.GetInstanceInfo(instance_name)
2372
      if node.name in instance.all_nodes:
2373
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2374
                                   " please remove first." % instance_name,
2375
                                   errors.ECODE_INVAL)
2376
    self.op.node_name = node.name
2377
    self.node = node
2378

    
2379
  def Exec(self, feedback_fn):
2380
    """Removes the node from the cluster.
2381

2382
    """
2383
    node = self.node
2384
    logging.info("Stopping the node daemon and removing configs from node %s",
2385
                 node.name)
2386

    
2387
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
2388

    
2389
    # Promote nodes to master candidate as needed
2390
    _AdjustCandidatePool(self, exceptions=[node.name])
2391
    self.context.RemoveNode(node.name)
2392

    
2393
    # Run post hooks on the node before it's removed
2394
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
2395
    try:
2396
      h_results = hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
2397
    except:
2398
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
2399

    
2400
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
2401
    msg = result.fail_msg
2402
    if msg:
2403
      self.LogWarning("Errors encountered on the remote node while leaving"
2404
                      " the cluster: %s", msg)
2405

    
2406

    
2407
class LUQueryNodes(NoHooksLU):
2408
  """Logical unit for querying nodes.
2409

2410
  """
2411
  _OP_REQP = ["output_fields", "names", "use_locking"]
2412
  REQ_BGL = False
2413

    
2414
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
2415
                    "master_candidate", "offline", "drained"]
2416

    
2417
  _FIELDS_DYNAMIC = utils.FieldSet(
2418
    "dtotal", "dfree",
2419
    "mtotal", "mnode", "mfree",
2420
    "bootid",
2421
    "ctotal", "cnodes", "csockets",
2422
    )
2423

    
2424
  _FIELDS_STATIC = utils.FieldSet(*[
2425
    "pinst_cnt", "sinst_cnt",
2426
    "pinst_list", "sinst_list",
2427
    "pip", "sip", "tags",
2428
    "master",
2429
    "role"] + _SIMPLE_FIELDS
2430
    )
2431

    
2432
  def ExpandNames(self):
2433
    _CheckOutputFields(static=self._FIELDS_STATIC,
2434
                       dynamic=self._FIELDS_DYNAMIC,
2435
                       selected=self.op.output_fields)
2436

    
2437
    self.needed_locks = {}
2438
    self.share_locks[locking.LEVEL_NODE] = 1
2439

    
2440
    if self.op.names:
2441
      self.wanted = _GetWantedNodes(self, self.op.names)
2442
    else:
2443
      self.wanted = locking.ALL_SET
2444

    
2445
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2446
    self.do_locking = self.do_node_query and self.op.use_locking
2447
    if self.do_locking:
2448
      # if we don't request only static fields, we need to lock the nodes
2449
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2450

    
2451
  def CheckPrereq(self):
2452
    """Check prerequisites.
2453

2454
    """
2455
    # The validation of the node list is done in the _GetWantedNodes,
2456
    # if non empty, and if empty, there's no validation to do
2457
    pass
2458

    
2459
  def Exec(self, feedback_fn):
2460
    """Computes the list of nodes and their attributes.
2461

2462
    """
2463
    all_info = self.cfg.GetAllNodesInfo()
2464
    if self.do_locking:
2465
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2466
    elif self.wanted != locking.ALL_SET:
2467
      nodenames = self.wanted
2468
      missing = set(nodenames).difference(all_info.keys())
2469
      if missing:
2470
        raise errors.OpExecError(
2471
          "Some nodes were removed before retrieving their data: %s" % missing)
2472
    else:
2473
      nodenames = all_info.keys()
2474

    
2475
    nodenames = utils.NiceSort(nodenames)
2476
    nodelist = [all_info[name] for name in nodenames]
2477

    
2478
    # begin data gathering
2479

    
2480
    if self.do_node_query:
2481
      live_data = {}
2482
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2483
                                          self.cfg.GetHypervisorType())
2484
      for name in nodenames:
2485
        nodeinfo = node_data[name]
2486
        if not nodeinfo.fail_msg and nodeinfo.payload:
2487
          nodeinfo = nodeinfo.payload
2488
          fn = utils.TryConvert
2489
          live_data[name] = {
2490
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2491
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2492
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2493
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2494
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2495
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2496
            "bootid": nodeinfo.get('bootid', None),
2497
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2498
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2499
            }
2500
        else:
2501
          live_data[name] = {}
2502
    else:
2503
      live_data = dict.fromkeys(nodenames, {})
2504

    
2505
    node_to_primary = dict([(name, set()) for name in nodenames])
2506
    node_to_secondary = dict([(name, set()) for name in nodenames])
2507

    
2508
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2509
                             "sinst_cnt", "sinst_list"))
2510
    if inst_fields & frozenset(self.op.output_fields):
2511
      instancelist = self.cfg.GetInstanceList()
2512

    
2513
      for instance_name in instancelist:
2514
        inst = self.cfg.GetInstanceInfo(instance_name)
2515
        if inst.primary_node in node_to_primary:
2516
          node_to_primary[inst.primary_node].add(inst.name)
2517
        for secnode in inst.secondary_nodes:
2518
          if secnode in node_to_secondary:
2519
            node_to_secondary[secnode].add(inst.name)
2520

    
2521
    master_node = self.cfg.GetMasterNode()
2522

    
2523
    # end data gathering
2524

    
2525
    output = []
2526
    for node in nodelist:
2527
      node_output = []
2528
      for field in self.op.output_fields:
2529
        if field in self._SIMPLE_FIELDS:
2530
          val = getattr(node, field)
2531
        elif field == "pinst_list":
2532
          val = list(node_to_primary[node.name])
2533
        elif field == "sinst_list":
2534
          val = list(node_to_secondary[node.name])
2535
        elif field == "pinst_cnt":
2536
          val = len(node_to_primary[node.name])
2537
        elif field == "sinst_cnt":
2538
          val = len(node_to_secondary[node.name])
2539
        elif field == "pip":
2540
          val = node.primary_ip
2541
        elif field == "sip":
2542
          val = node.secondary_ip
2543
        elif field == "tags":
2544
          val = list(node.GetTags())
2545
        elif field == "master":
2546
          val = node.name == master_node
2547
        elif self._FIELDS_DYNAMIC.Matches(field):
2548
          val = live_data[node.name].get(field, None)
2549
        elif field == "role":
2550
          if node.name == master_node:
2551
            val = "M"
2552
          elif node.master_candidate:
2553
            val = "C"
2554
          elif node.drained:
2555
            val = "D"
2556
          elif node.offline:
2557
            val = "O"
2558
          else:
2559
            val = "R"
2560
        else:
2561
          raise errors.ParameterError(field)
2562
        node_output.append(val)
2563
      output.append(node_output)
2564

    
2565
    return output
2566

    
2567

    
2568
class LUQueryNodeVolumes(NoHooksLU):
2569
  """Logical unit for getting volumes on node(s).
2570

2571
  """
2572
  _OP_REQP = ["nodes", "output_fields"]
2573
  REQ_BGL = False
2574
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2575
  _FIELDS_STATIC = utils.FieldSet("node")
2576

    
2577
  def ExpandNames(self):
2578
    _CheckOutputFields(static=self._FIELDS_STATIC,
2579
                       dynamic=self._FIELDS_DYNAMIC,
2580
                       selected=self.op.output_fields)
2581

    
2582
    self.needed_locks = {}
2583
    self.share_locks[locking.LEVEL_NODE] = 1
2584
    if not self.op.nodes:
2585
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2586
    else:
2587
      self.needed_locks[locking.LEVEL_NODE] = \
2588
        _GetWantedNodes(self, self.op.nodes)
2589

    
2590
  def CheckPrereq(self):
2591
    """Check prerequisites.
2592

2593
    This checks that the fields required are valid output fields.
2594

2595
    """
2596
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2597

    
2598
  def Exec(self, feedback_fn):
2599
    """Computes the list of nodes and their attributes.
2600

2601
    """
2602
    nodenames = self.nodes
2603
    volumes = self.rpc.call_node_volumes(nodenames)
2604

    
2605
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
2606
             in self.cfg.GetInstanceList()]
2607

    
2608
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
2609

    
2610
    output = []
2611
    for node in nodenames:
2612
      nresult = volumes[node]
2613
      if nresult.offline:
2614
        continue
2615
      msg = nresult.fail_msg
2616
      if msg:
2617
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
2618
        continue
2619

    
2620
      node_vols = nresult.payload[:]
2621
      node_vols.sort(key=lambda vol: vol['dev'])
2622

    
2623
      for vol in node_vols:
2624
        node_output = []
2625
        for field in self.op.output_fields:
2626
          if field == "node":
2627
            val = node
2628
          elif field == "phys":
2629
            val = vol['dev']
2630
          elif field == "vg":
2631
            val = vol['vg']
2632
          elif field == "name":
2633
            val = vol['name']
2634
          elif field == "size":
2635
            val = int(float(vol['size']))
2636
          elif field == "instance":
2637
            for inst in ilist:
2638
              if node not in lv_by_node[inst]:
2639
                continue
2640
              if vol['name'] in lv_by_node[inst][node]:
2641
                val = inst.name
2642
                break
2643
            else:
2644
              val = '-'
2645
          else:
2646
            raise errors.ParameterError(field)
2647
          node_output.append(str(val))
2648

    
2649
        output.append(node_output)
2650

    
2651
    return output
2652

    
2653

    
2654
class LUQueryNodeStorage(NoHooksLU):
2655
  """Logical unit for getting information on storage units on node(s).
2656

2657
  """
2658
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
2659
  REQ_BGL = False
2660
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
2661

    
2662
  def ExpandNames(self):
2663
    storage_type = self.op.storage_type
2664

    
2665
    if storage_type not in constants.VALID_STORAGE_TYPES:
2666
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2667
                                 errors.ECODE_INVAL)
2668

    
2669
    _CheckOutputFields(static=self._FIELDS_STATIC,
2670
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
2671
                       selected=self.op.output_fields)
2672

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

    
2676
    if self.op.nodes:
2677
      self.needed_locks[locking.LEVEL_NODE] = \
2678
        _GetWantedNodes(self, self.op.nodes)
2679
    else:
2680
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2681

    
2682
  def CheckPrereq(self):
2683
    """Check prerequisites.
2684

2685
    This checks that the fields required are valid output fields.
2686

2687
    """
2688
    self.op.name = getattr(self.op, "name", None)
2689

    
2690
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2691

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

2695
    """
2696
    # Always get name to sort by
2697
    if constants.SF_NAME in self.op.output_fields:
2698
      fields = self.op.output_fields[:]
2699
    else:
2700
      fields = [constants.SF_NAME] + self.op.output_fields
2701

    
2702
    # Never ask for node or type as it's only known to the LU
2703
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
2704
      while extra in fields:
2705
        fields.remove(extra)
2706

    
2707
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
2708
    name_idx = field_idx[constants.SF_NAME]
2709

    
2710
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2711
    data = self.rpc.call_storage_list(self.nodes,
2712
                                      self.op.storage_type, st_args,
2713
                                      self.op.name, fields)
2714

    
2715
    result = []
2716

    
2717
    for node in utils.NiceSort(self.nodes):
2718
      nresult = data[node]
2719
      if nresult.offline:
2720
        continue
2721

    
2722
      msg = nresult.fail_msg
2723
      if msg:
2724
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
2725
        continue
2726

    
2727
      rows = dict([(row[name_idx], row) for row in nresult.payload])
2728

    
2729
      for name in utils.NiceSort(rows.keys()):
2730
        row = rows[name]
2731

    
2732
        out = []
2733

    
2734
        for field in self.op.output_fields:
2735
          if field == constants.SF_NODE:
2736
            val = node
2737
          elif field == constants.SF_TYPE:
2738
            val = self.op.storage_type
2739
          elif field in field_idx:
2740
            val = row[field_idx[field]]
2741
          else:
2742
            raise errors.ParameterError(field)
2743

    
2744
          out.append(val)
2745

    
2746
        result.append(out)
2747

    
2748
    return result
2749

    
2750

    
2751
class LUModifyNodeStorage(NoHooksLU):
2752
  """Logical unit for modifying a storage volume on a node.
2753

2754
  """
2755
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
2756
  REQ_BGL = False
2757

    
2758
  def CheckArguments(self):
2759
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2760
    if node_name is None:
2761
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
2762
                                 errors.ECODE_NOENT)
2763

    
2764
    self.op.node_name = node_name
2765

    
2766
    storage_type = self.op.storage_type
2767
    if storage_type not in constants.VALID_STORAGE_TYPES:
2768
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2769
                                 errors.ECODE_INVAL)
2770

    
2771
  def ExpandNames(self):
2772
    self.needed_locks = {
2773
      locking.LEVEL_NODE: self.op.node_name,
2774
      }
2775

    
2776
  def CheckPrereq(self):
2777
    """Check prerequisites.
2778

2779
    """
2780
    storage_type = self.op.storage_type
2781

    
2782
    try:
2783
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
2784
    except KeyError:
2785
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
2786
                                 " modified" % storage_type,
2787
                                 errors.ECODE_INVAL)
2788

    
2789
    diff = set(self.op.changes.keys()) - modifiable
2790
    if diff:
2791
      raise errors.OpPrereqError("The following fields can not be modified for"
2792
                                 " storage units of type '%s': %r" %
2793
                                 (storage_type, list(diff)),
2794
                                 errors.ECODE_INVAL)
2795

    
2796
  def Exec(self, feedback_fn):
2797
    """Computes the list of nodes and their attributes.
2798

2799
    """
2800
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2801
    result = self.rpc.call_storage_modify(self.op.node_name,
2802
                                          self.op.storage_type, st_args,
2803
                                          self.op.name, self.op.changes)
2804
    result.Raise("Failed to modify storage unit '%s' on %s" %
2805
                 (self.op.name, self.op.node_name))
2806

    
2807

    
2808
class LUAddNode(LogicalUnit):
2809
  """Logical unit for adding node to the cluster.
2810

2811
  """
2812
  HPATH = "node-add"
2813
  HTYPE = constants.HTYPE_NODE
2814
  _OP_REQP = ["node_name"]
2815

    
2816
  def BuildHooksEnv(self):
2817
    """Build hooks env.
2818

2819
    This will run on all nodes before, and on all nodes + the new node after.
2820

2821
    """
2822
    env = {
2823
      "OP_TARGET": self.op.node_name,
2824
      "NODE_NAME": self.op.node_name,
2825
      "NODE_PIP": self.op.primary_ip,
2826
      "NODE_SIP": self.op.secondary_ip,
2827
      }
2828
    nodes_0 = self.cfg.GetNodeList()
2829
    nodes_1 = nodes_0 + [self.op.node_name, ]
2830
    return env, nodes_0, nodes_1
2831

    
2832
  def CheckPrereq(self):
2833
    """Check prerequisites.
2834

2835
    This checks:
2836
     - the new node is not already in the config
2837
     - it is resolvable
2838
     - its parameters (single/dual homed) matches the cluster
2839

2840
    Any errors are signaled by raising errors.OpPrereqError.
2841

2842
    """
2843
    node_name = self.op.node_name
2844
    cfg = self.cfg
2845

    
2846
    dns_data = utils.HostInfo(node_name)
2847

    
2848
    node = dns_data.name
2849
    primary_ip = self.op.primary_ip = dns_data.ip
2850
    secondary_ip = getattr(self.op, "secondary_ip", None)
2851
    if secondary_ip is None:
2852
      secondary_ip = primary_ip
2853
    if not utils.IsValidIP(secondary_ip):
2854
      raise errors.OpPrereqError("Invalid secondary IP given",
2855
                                 errors.ECODE_INVAL)
2856
    self.op.secondary_ip = secondary_ip
2857

    
2858
    node_list = cfg.GetNodeList()
2859
    if not self.op.readd and node in node_list:
2860
      raise errors.OpPrereqError("Node %s is already in the configuration" %
2861
                                 node, errors.ECODE_EXISTS)
2862
    elif self.op.readd and node not in node_list:
2863
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
2864
                                 errors.ECODE_NOENT)
2865

    
2866
    for existing_node_name in node_list:
2867
      existing_node = cfg.GetNodeInfo(existing_node_name)
2868

    
2869
      if self.op.readd and node == existing_node_name:
2870
        if (existing_node.primary_ip != primary_ip or
2871
            existing_node.secondary_ip != secondary_ip):
2872
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
2873
                                     " address configuration as before",
2874
                                     errors.ECODE_INVAL)
2875
        continue
2876

    
2877
      if (existing_node.primary_ip == primary_ip or
2878
          existing_node.secondary_ip == primary_ip or
2879
          existing_node.primary_ip == secondary_ip or
2880
          existing_node.secondary_ip == secondary_ip):
2881
        raise errors.OpPrereqError("New node ip address(es) conflict with"
2882
                                   " existing node %s" % existing_node.name,
2883
                                   errors.ECODE_NOTUNIQUE)
2884

    
2885
    # check that the type of the node (single versus dual homed) is the
2886
    # same as for the master
2887
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
2888
    master_singlehomed = myself.secondary_ip == myself.primary_ip
2889
    newbie_singlehomed = secondary_ip == primary_ip
2890
    if master_singlehomed != newbie_singlehomed:
2891
      if master_singlehomed:
2892
        raise errors.OpPrereqError("The master has no private ip but the"
2893
                                   " new node has one",
2894
                                   errors.ECODE_INVAL)
2895
      else:
2896
        raise errors.OpPrereqError("The master has a private ip but the"
2897
                                   " new node doesn't have one",
2898
                                   errors.ECODE_INVAL)
2899

    
2900
    # checks reachability
2901
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
2902
      raise errors.OpPrereqError("Node not reachable by ping",
2903
                                 errors.ECODE_ENVIRON)
2904

    
2905
    if not newbie_singlehomed:
2906
      # check reachability from my secondary ip to newbie's secondary ip
2907
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
2908
                           source=myself.secondary_ip):
2909
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
2910
                                   " based ping to noded port",
2911
                                   errors.ECODE_ENVIRON)
2912

    
2913
    if self.op.readd:
2914
      exceptions = [node]
2915
    else:
2916
      exceptions = []
2917

    
2918
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
2919

    
2920
    if self.op.readd:
2921
      self.new_node = self.cfg.GetNodeInfo(node)
2922
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
2923
    else:
2924
      self.new_node = objects.Node(name=node,
2925
                                   primary_ip=primary_ip,
2926
                                   secondary_ip=secondary_ip,
2927
                                   master_candidate=self.master_candidate,
2928
                                   offline=False, drained=False)
2929

    
2930
  def Exec(self, feedback_fn):
2931
    """Adds the new node to the cluster.
2932

2933
    """
2934
    new_node = self.new_node
2935
    node = new_node.name
2936

    
2937
    # for re-adds, reset the offline/drained/master-candidate flags;
2938
    # we need to reset here, otherwise offline would prevent RPC calls
2939
    # later in the procedure; this also means that if the re-add
2940
    # fails, we are left with a non-offlined, broken node
2941
    if self.op.readd:
2942
      new_node.drained = new_node.offline = False
2943
      self.LogInfo("Readding a node, the offline/drained flags were reset")
2944
      # if we demote the node, we do cleanup later in the procedure
2945
      new_node.master_candidate = self.master_candidate
2946

    
2947
    # notify the user about any possible mc promotion
2948
    if new_node.master_candidate:
2949
      self.LogInfo("Node will be a master candidate")
2950

    
2951
    # check connectivity
2952
    result = self.rpc.call_version([node])[node]
2953
    result.Raise("Can't get version information from node %s" % node)
2954
    if constants.PROTOCOL_VERSION == result.payload:
2955
      logging.info("Communication to node %s fine, sw version %s match",
2956
                   node, result.payload)
2957
    else:
2958
      raise errors.OpExecError("Version mismatch master version %s,"
2959
                               " node version %s" %
2960
                               (constants.PROTOCOL_VERSION, result.payload))
2961

    
2962
    # setup ssh on node
2963
    if self.cfg.GetClusterInfo().modify_ssh_setup:
2964
      logging.info("Copy ssh key to node %s", node)
2965
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
2966
      keyarray = []
2967
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
2968
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
2969
                  priv_key, pub_key]
2970

    
2971
      for i in keyfiles:
2972
        keyarray.append(utils.ReadFile(i))
2973

    
2974
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
2975
                                      keyarray[2], keyarray[3], keyarray[4],
2976
                                      keyarray[5])
2977
      result.Raise("Cannot transfer ssh keys to the new node")
2978

    
2979
    # Add node to our /etc/hosts, and add key to known_hosts
2980
    if self.cfg.GetClusterInfo().modify_etc_hosts:
2981
      utils.AddHostToEtcHosts(new_node.name)
2982

    
2983
    if new_node.secondary_ip != new_node.primary_ip:
2984
      result = self.rpc.call_node_has_ip_address(new_node.name,
2985
                                                 new_node.secondary_ip)
2986
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
2987
                   prereq=True, ecode=errors.ECODE_ENVIRON)
2988
      if not result.payload:
2989
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
2990
                                 " you gave (%s). Please fix and re-run this"
2991
                                 " command." % new_node.secondary_ip)
2992

    
2993
    node_verify_list = [self.cfg.GetMasterNode()]
2994
    node_verify_param = {
2995
      constants.NV_NODELIST: [node],
2996
      # TODO: do a node-net-test as well?
2997
    }
2998

    
2999
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3000
                                       self.cfg.GetClusterName())
3001
    for verifier in node_verify_list:
3002
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3003
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3004
      if nl_payload:
3005
        for failed in nl_payload:
3006
          feedback_fn("ssh/hostname verification failed"
3007
                      " (checking from %s): %s" %
3008
                      (verifier, nl_payload[failed]))
3009
        raise errors.OpExecError("ssh/hostname verification failed.")
3010

    
3011
    if self.op.readd:
3012
      _RedistributeAncillaryFiles(self)
3013
      self.context.ReaddNode(new_node)
3014
      # make sure we redistribute the config
3015
      self.cfg.Update(new_node, feedback_fn)
3016
      # and make sure the new node will not have old files around
3017
      if not new_node.master_candidate:
3018
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3019
        msg = result.fail_msg
3020
        if msg:
3021
          self.LogWarning("Node failed to demote itself from master"
3022
                          " candidate status: %s" % msg)
3023
    else:
3024
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3025
      self.context.AddNode(new_node)
3026

    
3027

    
3028
class LUSetNodeParams(LogicalUnit):
3029
  """Modifies the parameters of a node.
3030

3031
  """
3032
  HPATH = "node-modify"
3033
  HTYPE = constants.HTYPE_NODE
3034
  _OP_REQP = ["node_name"]
3035
  REQ_BGL = False
3036

    
3037
  def CheckArguments(self):
3038
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
3039
    if node_name is None:
3040
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
3041
                                 errors.ECODE_INVAL)
3042
    self.op.node_name = node_name
3043
    _CheckBooleanOpField(self.op, 'master_candidate')
3044
    _CheckBooleanOpField(self.op, 'offline')
3045
    _CheckBooleanOpField(self.op, 'drained')
3046
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3047
    if all_mods.count(None) == 3:
3048
      raise errors.OpPrereqError("Please pass at least one modification",
3049
                                 errors.ECODE_INVAL)
3050
    if all_mods.count(True) > 1:
3051
      raise errors.OpPrereqError("Can't set the node into more than one"
3052
                                 " state at the same time",
3053
                                 errors.ECODE_INVAL)
3054

    
3055
  def ExpandNames(self):
3056
    self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3057

    
3058
  def BuildHooksEnv(self):
3059
    """Build hooks env.
3060

3061
    This runs on the master node.
3062

3063
    """
3064
    env = {
3065
      "OP_TARGET": self.op.node_name,
3066
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3067
      "OFFLINE": str(self.op.offline),
3068
      "DRAINED": str(self.op.drained),
3069
      }
3070
    nl = [self.cfg.GetMasterNode(),
3071
          self.op.node_name]
3072
    return env, nl, nl
3073

    
3074
  def CheckPrereq(self):
3075
    """Check prerequisites.
3076

3077
    This only checks the instance list against the existing names.
3078

3079
    """
3080
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3081

    
3082
    if (self.op.master_candidate is not None or
3083
        self.op.drained is not None or
3084
        self.op.offline is not None):
3085
      # we can't change the master's node flags
3086
      if self.op.node_name == self.cfg.GetMasterNode():
3087
        raise errors.OpPrereqError("The master role can be changed"
3088
                                   " only via masterfailover",
3089
                                   errors.ECODE_INVAL)
3090

    
3091
    # Boolean value that tells us whether we're offlining or draining the node
3092
    offline_or_drain = self.op.offline == True or self.op.drained == True
3093
    deoffline_or_drain = self.op.offline == False or self.op.drained == False
3094

    
3095
    if (node.master_candidate and
3096
        (self.op.master_candidate == False or offline_or_drain)):
3097
      cp_size = self.cfg.GetClusterInfo().candidate_pool_size
3098
      mc_now, mc_should, mc_max = self.cfg.GetMasterCandidateStats()
3099
      if mc_now <= cp_size:
3100
        msg = ("Not enough master candidates (desired"
3101
               " %d, new value will be %d)" % (cp_size, mc_now-1))
3102
        # Only allow forcing the operation if it's an offline/drain operation,
3103
        # and we could not possibly promote more nodes.
3104
        # FIXME: this can still lead to issues if in any way another node which
3105
        # could be promoted appears in the meantime.
3106
        if self.op.force and offline_or_drain and mc_should == mc_max:
3107
          self.LogWarning(msg)
3108
        else:
3109
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
3110

    
3111
    if (self.op.master_candidate == True and
3112
        ((node.offline and not self.op.offline == False) or
3113
         (node.drained and not self.op.drained == False))):
3114
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3115
                                 " to master_candidate" % node.name,
3116
                                 errors.ECODE_INVAL)
3117

    
3118
    # If we're being deofflined/drained, we'll MC ourself if needed
3119
    if (deoffline_or_drain and not offline_or_drain and not
3120
        self.op.master_candidate == True):
3121
      self.op.master_candidate = _DecideSelfPromotion(self)
3122
      if self.op.master_candidate:
3123
        self.LogInfo("Autopromoting node to master candidate")
3124

    
3125
    return
3126

    
3127
  def Exec(self, feedback_fn):
3128
    """Modifies a node.
3129

3130
    """
3131
    node = self.node
3132

    
3133
    result = []
3134
    changed_mc = False
3135

    
3136
    if self.op.offline is not None:
3137
      node.offline = self.op.offline
3138
      result.append(("offline", str(self.op.offline)))
3139
      if self.op.offline == True:
3140
        if node.master_candidate:
3141
          node.master_candidate = False
3142
          changed_mc = True
3143
          result.append(("master_candidate", "auto-demotion due to offline"))
3144
        if node.drained:
3145
          node.drained = False
3146
          result.append(("drained", "clear drained status due to offline"))
3147

    
3148
    if self.op.master_candidate is not None:
3149
      node.master_candidate = self.op.master_candidate
3150
      changed_mc = True
3151
      result.append(("master_candidate", str(self.op.master_candidate)))
3152
      if self.op.master_candidate == False:
3153
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3154
        msg = rrc.fail_msg
3155
        if msg:
3156
          self.LogWarning("Node failed to demote itself: %s" % msg)
3157

    
3158
    if self.op.drained is not None:
3159
      node.drained = self.op.drained
3160
      result.append(("drained", str(self.op.drained)))
3161
      if self.op.drained == True:
3162
        if node.master_candidate:
3163
          node.master_candidate = False
3164
          changed_mc = True
3165
          result.append(("master_candidate", "auto-demotion due to drain"))
3166
          rrc = self.rpc.call_node_demote_from_mc(node.name)
3167
          msg = rrc.fail_msg
3168
          if msg:
3169
            self.LogWarning("Node failed to demote itself: %s" % msg)
3170
        if node.offline:
3171
          node.offline = False
3172
          result.append(("offline", "clear offline status due to drain"))
3173

    
3174
    # this will trigger configuration file update, if needed
3175
    self.cfg.Update(node, feedback_fn)
3176
    # this will trigger job queue propagation or cleanup
3177
    if changed_mc:
3178
      self.context.ReaddNode(node)
3179

    
3180
    return result
3181

    
3182

    
3183
class LUPowercycleNode(NoHooksLU):
3184
  """Powercycles a node.
3185

3186
  """
3187
  _OP_REQP = ["node_name", "force"]
3188
  REQ_BGL = False
3189

    
3190
  def CheckArguments(self):
3191
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
3192
    if node_name is None:
3193
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
3194
                                 errors.ECODE_NOENT)
3195
    self.op.node_name = node_name
3196
    if node_name == self.cfg.GetMasterNode() and not self.op.force:
3197
      raise errors.OpPrereqError("The node is the master and the force"
3198
                                 " parameter was not set",
3199
                                 errors.ECODE_INVAL)
3200

    
3201
  def ExpandNames(self):
3202
    """Locking for PowercycleNode.
3203

3204
    This is a last-resort option and shouldn't block on other
3205
    jobs. Therefore, we grab no locks.
3206

3207
    """
3208
    self.needed_locks = {}
3209

    
3210
  def CheckPrereq(self):
3211
    """Check prerequisites.
3212

3213
    This LU has no prereqs.
3214

3215
    """
3216
    pass
3217

    
3218
  def Exec(self, feedback_fn):
3219
    """Reboots a node.
3220

3221
    """
3222
    result = self.rpc.call_node_powercycle(self.op.node_name,
3223
                                           self.cfg.GetHypervisorType())
3224
    result.Raise("Failed to schedule the reboot")
3225
    return result.payload
3226

    
3227

    
3228
class LUQueryClusterInfo(NoHooksLU):
3229
  """Query cluster configuration.
3230

3231
  """
3232
  _OP_REQP = []
3233
  REQ_BGL = False
3234

    
3235
  def ExpandNames(self):
3236
    self.needed_locks = {}
3237

    
3238
  def CheckPrereq(self):
3239
    """No prerequsites needed for this LU.
3240

3241
    """
3242
    pass
3243

    
3244
  def Exec(self, feedback_fn):
3245
    """Return cluster config.
3246

3247
    """
3248
    cluster = self.cfg.GetClusterInfo()
3249
    result = {
3250
      "software_version": constants.RELEASE_VERSION,
3251
      "protocol_version": constants.PROTOCOL_VERSION,
3252
      "config_version": constants.CONFIG_VERSION,
3253
      "os_api_version": max(constants.OS_API_VERSIONS),
3254
      "export_version": constants.EXPORT_VERSION,
3255
      "architecture": (platform.architecture()[0], platform.machine()),
3256
      "name": cluster.cluster_name,
3257
      "master": cluster.master_node,
3258
      "default_hypervisor": cluster.enabled_hypervisors[0],
3259
      "enabled_hypervisors": cluster.enabled_hypervisors,
3260
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3261
                        for hypervisor_name in cluster.enabled_hypervisors]),
3262
      "beparams": cluster.beparams,
3263
      "nicparams": cluster.nicparams,
3264
      "candidate_pool_size": cluster.candidate_pool_size,
3265
      "master_netdev": cluster.master_netdev,
3266
      "volume_group_name": cluster.volume_group_name,
3267
      "file_storage_dir": cluster.file_storage_dir,
3268
      "ctime": cluster.ctime,
3269
      "mtime": cluster.mtime,
3270
      "uuid": cluster.uuid,
3271
      "tags": list(cluster.GetTags()),
3272
      }
3273

    
3274
    return result
3275

    
3276

    
3277
class LUQueryConfigValues(NoHooksLU):
3278
  """Return configuration values.
3279

3280
  """
3281
  _OP_REQP = []
3282
  REQ_BGL = False
3283
  _FIELDS_DYNAMIC = utils.FieldSet()
3284
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
3285
                                  "watcher_pause")
3286

    
3287
  def ExpandNames(self):
3288
    self.needed_locks = {}
3289

    
3290
    _CheckOutputFields(static=self._FIELDS_STATIC,
3291
                       dynamic=self._FIELDS_DYNAMIC,
3292
                       selected=self.op.output_fields)
3293

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

3297
    """
3298
    pass
3299

    
3300
  def Exec(self, feedback_fn):
3301
    """Dump a representation of the cluster config to the standard output.
3302

3303
    """
3304
    values = []
3305
    for field in self.op.output_fields:
3306
      if field == "cluster_name":
3307
        entry = self.cfg.GetClusterName()
3308
      elif field == "master_node":
3309
        entry = self.cfg.GetMasterNode()
3310
      elif field == "drain_flag":
3311
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3312
      elif field == "watcher_pause":
3313
        return utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
3314
      else:
3315
        raise errors.ParameterError(field)
3316
      values.append(entry)
3317
    return values
3318

    
3319

    
3320
class LUActivateInstanceDisks(NoHooksLU):
3321
  """Bring up an instance's disks.
3322

3323
  """
3324
  _OP_REQP = ["instance_name"]
3325
  REQ_BGL = False
3326

    
3327
  def ExpandNames(self):
3328
    self._ExpandAndLockInstance()
3329
    self.needed_locks[locking.LEVEL_NODE] = []
3330
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3331

    
3332
  def DeclareLocks(self, level):
3333
    if level == locking.LEVEL_NODE:
3334
      self._LockInstancesNodes()
3335

    
3336
  def CheckPrereq(self):
3337
    """Check prerequisites.
3338

3339
    This checks that the instance is in the cluster.
3340

3341
    """
3342
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3343
    assert self.instance is not None, \
3344
      "Cannot retrieve locked instance %s" % self.op.instance_name
3345
    _CheckNodeOnline(self, self.instance.primary_node)
3346
    if not hasattr(self.op, "ignore_size"):
3347
      self.op.ignore_size = False
3348

    
3349
  def Exec(self, feedback_fn):
3350
    """Activate the disks.
3351

3352
    """
3353
    disks_ok, disks_info = \
3354
              _AssembleInstanceDisks(self, self.instance,
3355
                                     ignore_size=self.op.ignore_size)
3356
    if not disks_ok:
3357
      raise errors.OpExecError("Cannot activate block devices")
3358

    
3359
    return disks_info
3360

    
3361

    
3362
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3363
                           ignore_size=False):
3364
  """Prepare the block devices for an instance.
3365

3366
  This sets up the block devices on all nodes.
3367

3368
  @type lu: L{LogicalUnit}
3369
  @param lu: the logical unit on whose behalf we execute
3370
  @type instance: L{objects.Instance}
3371
  @param instance: the instance for whose disks we assemble
3372
  @type ignore_secondaries: boolean
3373
  @param ignore_secondaries: if true, errors on secondary nodes
3374
      won't result in an error return from the function
3375
  @type ignore_size: boolean
3376
  @param ignore_size: if true, the current known size of the disk
3377
      will not be used during the disk activation, useful for cases
3378
      when the size is wrong
3379
  @return: False if the operation failed, otherwise a list of
3380
      (host, instance_visible_name, node_visible_name)
3381
      with the mapping from node devices to instance devices
3382

3383
  """
3384
  device_info = []
3385
  disks_ok = True
3386
  iname = instance.name
3387
  # With the two passes mechanism we try to reduce the window of
3388
  # opportunity for the race condition of switching DRBD to primary
3389
  # before handshaking occured, but we do not eliminate it
3390

    
3391
  # The proper fix would be to wait (with some limits) until the
3392
  # connection has been made and drbd transitions from WFConnection
3393
  # into any other network-connected state (Connected, SyncTarget,
3394
  # SyncSource, etc.)
3395

    
3396
  # 1st pass, assemble on all nodes in secondary mode
3397
  for inst_disk in instance.disks:
3398
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3399
      if ignore_size:
3400
        node_disk = node_disk.Copy()
3401
        node_disk.UnsetSize()
3402
      lu.cfg.SetDiskID(node_disk, node)
3403
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3404
      msg = result.fail_msg
3405
      if msg:
3406
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3407
                           " (is_primary=False, pass=1): %s",
3408
                           inst_disk.iv_name, node, msg)
3409
        if not ignore_secondaries:
3410
          disks_ok = False
3411

    
3412
  # FIXME: race condition on drbd migration to primary
3413

    
3414
  # 2nd pass, do only the primary node
3415
  for inst_disk in instance.disks:
3416
    dev_path = None
3417

    
3418
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3419
      if node != instance.primary_node:
3420
        continue
3421
      if ignore_size:
3422
        node_disk = node_disk.Copy()
3423
        node_disk.UnsetSize()
3424
      lu.cfg.SetDiskID(node_disk, node)
3425
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3426
      msg = result.fail_msg
3427
      if msg:
3428
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3429
                           " (is_primary=True, pass=2): %s",
3430
                           inst_disk.iv_name, node, msg)
3431
        disks_ok = False
3432
      else:
3433
        dev_path = result.payload
3434

    
3435
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
3436

    
3437
  # leave the disks configured for the primary node
3438
  # this is a workaround that would be fixed better by
3439
  # improving the logical/physical id handling
3440
  for disk in instance.disks:
3441
    lu.cfg.SetDiskID(disk, instance.primary_node)
3442

    
3443
  return disks_ok, device_info
3444

    
3445

    
3446
def _StartInstanceDisks(lu, instance, force):
3447
  """Start the disks of an instance.
3448

3449
  """
3450
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3451
                                           ignore_secondaries=force)
3452
  if not disks_ok:
3453
    _ShutdownInstanceDisks(lu, instance)
3454
    if force is not None and not force:
3455
      lu.proc.LogWarning("", hint="If the message above refers to a"
3456
                         " secondary node,"
3457
                         " you can retry the operation using '--force'.")
3458
    raise errors.OpExecError("Disk consistency error")
3459

    
3460

    
3461
class LUDeactivateInstanceDisks(NoHooksLU):
3462
  """Shutdown an instance's disks.
3463

3464
  """
3465
  _OP_REQP = ["instance_name"]
3466
  REQ_BGL = False
3467

    
3468
  def ExpandNames(self):
3469
    self._ExpandAndLockInstance()
3470
    self.needed_locks[locking.LEVEL_NODE] = []
3471
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3472

    
3473
  def DeclareLocks(self, level):
3474
    if level == locking.LEVEL_NODE:
3475
      self._LockInstancesNodes()
3476

    
3477
  def CheckPrereq(self):
3478
    """Check prerequisites.
3479

3480
    This checks that the instance is in the cluster.
3481

3482
    """
3483
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3484
    assert self.instance is not None, \
3485
      "Cannot retrieve locked instance %s" % self.op.instance_name
3486

    
3487
  def Exec(self, feedback_fn):
3488
    """Deactivate the disks
3489

3490
    """
3491
    instance = self.instance
3492
    _SafeShutdownInstanceDisks(self, instance)
3493

    
3494

    
3495
def _SafeShutdownInstanceDisks(lu, instance):
3496
  """Shutdown block devices of an instance.
3497

3498
  This function checks if an instance is running, before calling
3499
  _ShutdownInstanceDisks.
3500

3501
  """
3502
  pnode = instance.primary_node
3503
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
3504
  ins_l.Raise("Can't contact node %s" % pnode)
3505

    
3506
  if instance.name in ins_l.payload:
3507
    raise errors.OpExecError("Instance is running, can't shutdown"
3508
                             " block devices.")
3509

    
3510
  _ShutdownInstanceDisks(lu, instance)
3511

    
3512

    
3513
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3514
  """Shutdown block devices of an instance.
3515

3516
  This does the shutdown on all nodes of the instance.
3517

3518
  If the ignore_primary is false, errors on the primary node are
3519
  ignored.
3520

3521
  """
3522
  all_result = True
3523
  for disk in instance.disks:
3524
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
3525
      lu.cfg.SetDiskID(top_disk, node)
3526
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
3527
      msg = result.fail_msg
3528
      if msg:
3529
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
3530
                      disk.iv_name, node, msg)
3531
        if not ignore_primary or node != instance.primary_node:
3532
          all_result = False
3533
  return all_result
3534

    
3535

    
3536
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
3537
  """Checks if a node has enough free memory.
3538

3539
  This function check if a given node has the needed amount of free
3540
  memory. In case the node has less memory or we cannot get the
3541
  information from the node, this function raise an OpPrereqError
3542
  exception.
3543

3544
  @type lu: C{LogicalUnit}
3545
  @param lu: a logical unit from which we get configuration data
3546
  @type node: C{str}
3547
  @param node: the node to check
3548
  @type reason: C{str}
3549
  @param reason: string to use in the error message
3550
  @type requested: C{int}
3551
  @param requested: the amount of memory in MiB to check for
3552
  @type hypervisor_name: C{str}
3553
  @param hypervisor_name: the hypervisor to ask for memory stats
3554
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
3555
      we cannot check the node
3556

3557
  """
3558
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
3559
  nodeinfo[node].Raise("Can't get data from node %s" % node,
3560
                       prereq=True, ecode=errors.ECODE_ENVIRON)
3561
  free_mem = nodeinfo[node].payload.get('memory_free', None)
3562
  if not isinstance(free_mem, int):
3563
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
3564
                               " was '%s'" % (node, free_mem),
3565
                               errors.ECODE_ENVIRON)
3566
  if requested > free_mem:
3567
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
3568
                               " needed %s MiB, available %s MiB" %
3569
                               (node, reason, requested, free_mem),
3570
                               errors.ECODE_NORES)
3571

    
3572

    
3573
class LUStartupInstance(LogicalUnit):
3574
  """Starts an instance.
3575

3576
  """
3577
  HPATH = "instance-start"
3578
  HTYPE = constants.HTYPE_INSTANCE
3579
  _OP_REQP = ["instance_name", "force"]
3580
  REQ_BGL = False
3581

    
3582
  def ExpandNames(self):
3583
    self._ExpandAndLockInstance()
3584

    
3585
  def BuildHooksEnv(self):
3586
    """Build hooks env.
3587

3588
    This runs on master, primary and secondary nodes of the instance.
3589

3590
    """
3591
    env = {
3592
      "FORCE": self.op.force,
3593
      }
3594
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3595
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3596
    return env, nl, nl
3597

    
3598
  def CheckPrereq(self):
3599
    """Check prerequisites.
3600

3601
    This checks that the instance is in the cluster.
3602

3603
    """
3604
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3605
    assert self.instance is not None, \
3606
      "Cannot retrieve locked instance %s" % self.op.instance_name
3607

    
3608
    # extra beparams
3609
    self.beparams = getattr(self.op, "beparams", {})
3610
    if self.beparams:
3611
      if not isinstance(self.beparams, dict):
3612
        raise errors.OpPrereqError("Invalid beparams passed: %s, expected"
3613
                                   " dict" % (type(self.beparams), ),
3614
                                   errors.ECODE_INVAL)
3615
      # fill the beparams dict
3616
      utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
3617
      self.op.beparams = self.beparams
3618

    
3619
    # extra hvparams
3620
    self.hvparams = getattr(self.op, "hvparams", {})
3621
    if self.hvparams:
3622
      if not isinstance(self.hvparams, dict):
3623
        raise errors.OpPrereqError("Invalid hvparams passed: %s, expected"
3624
                                   " dict" % (type(self.hvparams), ),
3625
                                   errors.ECODE_INVAL)
3626

    
3627
      # check hypervisor parameter syntax (locally)
3628
      cluster = self.cfg.GetClusterInfo()
3629
      utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
3630
      filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor],
3631
                                    instance.hvparams)
3632
      filled_hvp.update(self.hvparams)
3633
      hv_type = hypervisor.GetHypervisor(instance.hypervisor)
3634
      hv_type.CheckParameterSyntax(filled_hvp)
3635
      _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
3636
      self.op.hvparams = self.hvparams
3637

    
3638
    _CheckNodeOnline(self, instance.primary_node)
3639

    
3640
    bep = self.cfg.GetClusterInfo().FillBE(instance)
3641
    # check bridges existence
3642
    _CheckInstanceBridgesExist(self, instance)
3643

    
3644
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3645
                                              instance.name,
3646
                                              instance.hypervisor)
3647
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3648
                      prereq=True, ecode=errors.ECODE_ENVIRON)
3649
    if not remote_info.payload: # not running already
3650
      _CheckNodeFreeMemory(self, instance.primary_node,
3651
                           "starting instance %s" % instance.name,
3652
                           bep[constants.BE_MEMORY], instance.hypervisor)
3653

    
3654
  def Exec(self, feedback_fn):
3655
    """Start the instance.
3656

3657
    """
3658
    instance = self.instance
3659
    force = self.op.force
3660

    
3661
    self.cfg.MarkInstanceUp(instance.name)
3662

    
3663
    node_current = instance.primary_node
3664

    
3665
    _StartInstanceDisks(self, instance, force)
3666

    
3667
    result = self.rpc.call_instance_start(node_current, instance,
3668
                                          self.hvparams, self.beparams)
3669
    msg = result.fail_msg
3670
    if msg:
3671
      _ShutdownInstanceDisks(self, instance)
3672
      raise errors.OpExecError("Could not start instance: %s" % msg)
3673

    
3674

    
3675
class LURebootInstance(LogicalUnit):
3676
  """Reboot an instance.
3677

3678
  """
3679
  HPATH = "instance-reboot"
3680
  HTYPE = constants.HTYPE_INSTANCE
3681
  _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
3682
  REQ_BGL = False
3683

    
3684
  def CheckArguments(self):
3685
    """Check the arguments.
3686

3687
    """
3688
    self.shutdown_timeout = getattr(self.op, "shutdown_timeout",
3689
                                    constants.DEFAULT_SHUTDOWN_TIMEOUT)
3690

    
3691
  def ExpandNames(self):
3692
    if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
3693
                                   constants.INSTANCE_REBOOT_HARD,
3694
                                   constants.INSTANCE_REBOOT_FULL]:
3695
      raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
3696
                                  (constants.INSTANCE_REBOOT_SOFT,
3697
                                   constants.INSTANCE_REBOOT_HARD,
3698
                                   constants.INSTANCE_REBOOT_FULL))
3699
    self._ExpandAndLockInstance()
3700

    
3701
  def BuildHooksEnv(self):
3702
    """Build hooks env.
3703

3704
    This runs on master, primary and secondary nodes of the instance.
3705

3706
    """
3707
    env = {
3708
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
3709
      "REBOOT_TYPE": self.op.reboot_type,
3710
      "SHUTDOWN_TIMEOUT": self.shutdown_timeout,
3711
      }
3712
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3713
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3714
    return env, nl, nl
3715

    
3716
  def CheckPrereq(self):
3717
    """Check prerequisites.
3718

3719
    This checks that the instance is in the cluster.
3720

3721
    """
3722
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3723
    assert self.instance is not None, \
3724
      "Cannot retrieve locked instance %s" % self.op.instance_name
3725

    
3726
    _CheckNodeOnline(self, instance.primary_node)
3727

    
3728
    # check bridges existence
3729
    _CheckInstanceBridgesExist(self, instance)
3730

    
3731
  def Exec(self, feedback_fn):
3732
    """Reboot the instance.
3733

3734
    """
3735
    instance = self.instance
3736
    ignore_secondaries = self.op.ignore_secondaries
3737
    reboot_type = self.op.reboot_type
3738

    
3739
    node_current = instance.primary_node
3740

    
3741
    if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
3742
                       constants.INSTANCE_REBOOT_HARD]:
3743
      for disk in instance.disks:
3744
        self.cfg.SetDiskID(disk, node_current)
3745
      result = self.rpc.call_instance_reboot(node_current, instance,
3746
                                             reboot_type,
3747
                                             self.shutdown_timeout)
3748
      result.Raise("Could not reboot instance")
3749
    else:
3750
      result = self.rpc.call_instance_shutdown(node_current, instance,
3751
                                               self.shutdown_timeout)
3752
      result.Raise("Could not shutdown instance for full reboot")
3753
      _ShutdownInstanceDisks(self, instance)
3754
      _StartInstanceDisks(self, instance, ignore_secondaries)
3755
      result = self.rpc.call_instance_start(node_current, instance, None, None)
3756
      msg = result.fail_msg
3757
      if msg:
3758
        _ShutdownInstanceDisks(self, instance)
3759
        raise errors.OpExecError("Could not start instance for"
3760
                                 " full reboot: %s" % msg)
3761

    
3762
    self.cfg.MarkInstanceUp(instance.name)
3763

    
3764

    
3765
class LUShutdownInstance(LogicalUnit):
3766
  """Shutdown an instance.
3767

3768
  """
3769
  HPATH = "instance-stop"
3770
  HTYPE = constants.HTYPE_INSTANCE
3771
  _OP_REQP = ["instance_name"]
3772
  REQ_BGL = False
3773

    
3774
  def CheckArguments(self):
3775
    """Check the arguments.
3776

3777
    """
3778
    self.timeout = getattr(self.op, "timeout",
3779
                           constants.DEFAULT_SHUTDOWN_TIMEOUT)
3780

    
3781
  def ExpandNames(self):
3782
    self._ExpandAndLockInstance()
3783

    
3784
  def BuildHooksEnv(self):
3785
    """Build hooks env.
3786

3787
    This runs on master, primary and secondary nodes of the instance.
3788

3789
    """
3790
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3791
    env["TIMEOUT"] = self.timeout
3792
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3793
    return env, nl, nl
3794

    
3795
  def CheckPrereq(self):
3796
    """Check prerequisites.
3797

3798
    This checks that the instance is in the cluster.
3799

3800
    """
3801
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3802
    assert self.instance is not None, \
3803
      "Cannot retrieve locked instance %s" % self.op.instance_name
3804
    _CheckNodeOnline(self, self.instance.primary_node)
3805

    
3806
  def Exec(self, feedback_fn):
3807
    """Shutdown the instance.
3808

3809
    """
3810
    instance = self.instance
3811
    node_current = instance.primary_node
3812
    timeout = self.timeout
3813
    self.cfg.MarkInstanceDown(instance.name)
3814
    result = self.rpc.call_instance_shutdown(node_current, instance, timeout)
3815
    msg = result.fail_msg
3816
    if msg:
3817
      self.proc.LogWarning("Could not shutdown instance: %s" % msg)
3818

    
3819
    _ShutdownInstanceDisks(self, instance)
3820

    
3821

    
3822
class LUReinstallInstance(LogicalUnit):
3823
  """Reinstall an instance.
3824

3825
  """
3826
  HPATH = "instance-reinstall"
3827
  HTYPE = constants.HTYPE_INSTANCE
3828
  _OP_REQP = ["instance_name"]
3829
  REQ_BGL = False
3830

    
3831
  def ExpandNames(self):
3832
    self._ExpandAndLockInstance()
3833

    
3834
  def BuildHooksEnv(self):
3835
    """Build hooks env.
3836

3837
    This runs on master, primary and secondary nodes of the instance.
3838

3839
    """
3840
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3841
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3842
    return env, nl, nl
3843

    
3844
  def CheckPrereq(self):
3845
    """Check prerequisites.
3846

3847
    This checks that the instance is in the cluster and is not running.
3848

3849
    """
3850
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3851
    assert instance is not None, \
3852
      "Cannot retrieve locked instance %s" % self.op.instance_name
3853
    _CheckNodeOnline(self, instance.primary_node)
3854

    
3855
    if instance.disk_template == constants.DT_DISKLESS:
3856
      raise errors.OpPrereqError("Instance '%s' has no disks" %
3857
                                 self.op.instance_name,
3858
                                 errors.ECODE_INVAL)
3859
    if instance.admin_up:
3860
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3861
                                 self.op.instance_name,
3862
                                 errors.ECODE_STATE)
3863
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3864
                                              instance.name,
3865
                                              instance.hypervisor)
3866
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3867
                      prereq=True, ecode=errors.ECODE_ENVIRON)
3868
    if remote_info.payload:
3869
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3870
                                 (self.op.instance_name,
3871
                                  instance.primary_node),
3872
                                 errors.ECODE_STATE)
3873

    
3874
    self.op.os_type = getattr(self.op, "os_type", None)
3875
    self.op.force_variant = getattr(self.op, "force_variant", False)
3876
    if self.op.os_type is not None:
3877
      # OS verification
3878
      pnode = self.cfg.GetNodeInfo(
3879
        self.cfg.ExpandNodeName(instance.primary_node))
3880
      if pnode is None:
3881
        raise errors.OpPrereqError("Primary node '%s' is unknown" %
3882
                                   self.op.pnode, errors.ECODE_NOENT)
3883
      result = self.rpc.call_os_get(pnode.name, self.op.os_type)
3884
      result.Raise("OS '%s' not in supported OS list for primary node %s" %
3885
                   (self.op.os_type, pnode.name),
3886
                   prereq=True, ecode=errors.ECODE_INVAL)
3887
      if not self.op.force_variant:
3888
        _CheckOSVariant(result.payload, self.op.os_type)
3889

    
3890
    self.instance = instance
3891

    
3892
  def Exec(self, feedback_fn):
3893
    """Reinstall the instance.
3894

3895
    """
3896
    inst = self.instance
3897

    
3898
    if self.op.os_type is not None:
3899
      feedback_fn("Changing OS to '%s'..." % self.op.os_type)
3900
      inst.os = self.op.os_type
3901
      self.cfg.Update(inst, feedback_fn)
3902

    
3903
    _StartInstanceDisks(self, inst, None)
3904
    try:
3905
      feedback_fn("Running the instance OS create scripts...")
3906
      result = self.rpc.call_instance_os_add(inst.primary_node, inst, True)
3907
      result.Raise("Could not install OS for instance %s on node %s" %
3908
                   (inst.name, inst.primary_node))
3909
    finally:
3910
      _ShutdownInstanceDisks(self, inst)
3911

    
3912

    
3913
class LURecreateInstanceDisks(LogicalUnit):
3914
  """Recreate an instance's missing disks.
3915

3916
  """
3917
  HPATH = "instance-recreate-disks"
3918
  HTYPE = constants.HTYPE_INSTANCE
3919
  _OP_REQP = ["instance_name", "disks"]
3920
  REQ_BGL = False
3921

    
3922
  def CheckArguments(self):
3923
    """Check the arguments.
3924

3925
    """
3926
    if not isinstance(self.op.disks, list):
3927
      raise errors.OpPrereqError("Invalid disks parameter", errors.ECODE_INVAL)
3928
    for item in self.op.disks:
3929
      if (not isinstance(item, int) or
3930
          item < 0):
3931
        raise errors.OpPrereqError("Invalid disk specification '%s'" %
3932
                                   str(item), errors.ECODE_INVAL)
3933

    
3934
  def ExpandNames(self):
3935
    self._ExpandAndLockInstance()
3936

    
3937
  def BuildHooksEnv(self):
3938
    """Build hooks env.
3939

3940
    This runs on master, primary and secondary nodes of the instance.
3941

3942
    """
3943
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3944
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3945
    return env, nl, nl
3946

    
3947
  def CheckPrereq(self):
3948
    """Check prerequisites.
3949

3950
    This checks that the instance is in the cluster and is not running.
3951

3952
    """
3953
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3954
    assert instance is not None, \
3955
      "Cannot retrieve locked instance %s" % self.op.instance_name
3956
    _CheckNodeOnline(self, instance.primary_node)
3957

    
3958
    if instance.disk_template == constants.DT_DISKLESS:
3959
      raise errors.OpPrereqError("Instance '%s' has no disks" %
3960
                                 self.op.instance_name, errors.ECODE_INVAL)
3961
    if instance.admin_up:
3962
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3963
                                 self.op.instance_name, errors.ECODE_STATE)
3964
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3965
                                              instance.name,
3966
                                              instance.hypervisor)
3967
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3968
                      prereq=True, ecode=errors.ECODE_ENVIRON)
3969
    if remote_info.payload:
3970
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3971
                                 (self.op.instance_name,
3972
                                  instance.primary_node), errors.ECODE_STATE)
3973

    
3974
    if not self.op.disks:
3975
      self.op.disks = range(len(instance.disks))
3976
    else:
3977
      for idx in self.op.disks:
3978
        if idx >= len(instance.disks):
3979
          raise errors.OpPrereqError("Invalid disk index passed '%s'" % idx,
3980
                                     errors.ECODE_INVAL)
3981

    
3982
    self.instance = instance
3983

    
3984
  def Exec(self, feedback_fn):
3985
    """Recreate the disks.
3986

3987
    """
3988
    to_skip = []
3989
    for idx, disk in enumerate(self.instance.disks):
3990
      if idx not in self.op.disks: # disk idx has not been passed in
3991
        to_skip.append(idx)
3992
        continue
3993

    
3994
    _CreateDisks(self, self.instance, to_skip=to_skip)
3995

    
3996

    
3997
class LURenameInstance(LogicalUnit):
3998
  """Rename an instance.
3999

4000
  """
4001
  HPATH = "instance-rename"
4002
  HTYPE = constants.HTYPE_INSTANCE
4003
  _OP_REQP = ["instance_name", "new_name"