Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 21bcb9aa

History | View | Annotate | Download (307 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 _CheckGlobalHvParams(params):
504
  """Validates that given hypervisor params are not global ones.
505

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

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

    
517

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

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

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

    
530

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

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

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

    
543

    
544
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
545
                          memory, vcpus, nics, disk_template, disks,
546
                          bep, hvp, hypervisor_name):
547
  """Builds instance related env variables for hooks
548

549
  This builds the hook environment from individual variables.
550

551
  @type name: string
552
  @param name: the name of the instance
553
  @type primary_node: string
554
  @param primary_node: the name of the instance's primary node
555
  @type secondary_nodes: list
556
  @param secondary_nodes: list of secondary nodes as strings
557
  @type os_type: string
558
  @param os_type: the name of the instance's OS
559
  @type status: boolean
560
  @param status: the should_run status of the instance
561
  @type memory: string
562
  @param memory: the memory size of the instance
563
  @type vcpus: string
564
  @param vcpus: the count of VCPUs the instance has
565
  @type nics: list
566
  @param nics: list of tuples (ip, mac, mode, link) representing
567
      the NICs the instance has
568
  @type disk_template: string
569
  @param disk_template: the disk template of the instance
570
  @type disks: list
571
  @param disks: the list of (size, mode) pairs
572
  @type bep: dict
573
  @param bep: the backend parameters for the instance
574
  @type hvp: dict
575
  @param hvp: the hypervisor parameters for the instance
576
  @type hypervisor_name: string
577
  @param hypervisor_name: the hypervisor for the instance
578
  @rtype: dict
579
  @return: the hook environment for this instance
580

581
  """
582
  if status:
583
    str_status = "up"
584
  else:
585
    str_status = "down"
586
  env = {
587
    "OP_TARGET": name,
588
    "INSTANCE_NAME": name,
589
    "INSTANCE_PRIMARY": primary_node,
590
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
591
    "INSTANCE_OS_TYPE": os_type,
592
    "INSTANCE_STATUS": str_status,
593
    "INSTANCE_MEMORY": memory,
594
    "INSTANCE_VCPUS": vcpus,
595
    "INSTANCE_DISK_TEMPLATE": disk_template,
596
    "INSTANCE_HYPERVISOR": hypervisor_name,
597
  }
598

    
599
  if nics:
600
    nic_count = len(nics)
601
    for idx, (ip, mac, mode, link) in enumerate(nics):
602
      if ip is None:
603
        ip = ""
604
      env["INSTANCE_NIC%d_IP" % idx] = ip
605
      env["INSTANCE_NIC%d_MAC" % idx] = mac
606
      env["INSTANCE_NIC%d_MODE" % idx] = mode
607
      env["INSTANCE_NIC%d_LINK" % idx] = link
608
      if mode == constants.NIC_MODE_BRIDGED:
609
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
610
  else:
611
    nic_count = 0
612

    
613
  env["INSTANCE_NIC_COUNT"] = nic_count
614

    
615
  if disks:
616
    disk_count = len(disks)
617
    for idx, (size, mode) in enumerate(disks):
618
      env["INSTANCE_DISK%d_SIZE" % idx] = size
619
      env["INSTANCE_DISK%d_MODE" % idx] = mode
620
  else:
621
    disk_count = 0
622

    
623
  env["INSTANCE_DISK_COUNT"] = disk_count
624

    
625
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
626
    for key, value in source.items():
627
      env["INSTANCE_%s_%s" % (kind, key)] = value
628

    
629
  return env
630

    
631

    
632
def _NICListToTuple(lu, nics):
633
  """Build a list of nic information tuples.
634

635
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
636
  value in LUQueryInstanceData.
637

638
  @type lu:  L{LogicalUnit}
639
  @param lu: the logical unit on whose behalf we execute
640
  @type nics: list of L{objects.NIC}
641
  @param nics: list of nics to convert to hooks tuples
642

643
  """
644
  hooks_nics = []
645
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
646
  for nic in nics:
647
    ip = nic.ip
648
    mac = nic.mac
649
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
650
    mode = filled_params[constants.NIC_MODE]
651
    link = filled_params[constants.NIC_LINK]
652
    hooks_nics.append((ip, mac, mode, link))
653
  return hooks_nics
654

    
655

    
656
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
657
  """Builds instance related env variables for hooks from an object.
658

659
  @type lu: L{LogicalUnit}
660
  @param lu: the logical unit on whose behalf we execute
661
  @type instance: L{objects.Instance}
662
  @param instance: the instance for which we should build the
663
      environment
664
  @type override: dict
665
  @param override: dictionary with key/values that will override
666
      our values
667
  @rtype: dict
668
  @return: the hook environment dictionary
669

670
  """
671
  cluster = lu.cfg.GetClusterInfo()
672
  bep = cluster.FillBE(instance)
673
  hvp = cluster.FillHV(instance)
674
  args = {
675
    'name': instance.name,
676
    'primary_node': instance.primary_node,
677
    'secondary_nodes': instance.secondary_nodes,
678
    'os_type': instance.os,
679
    'status': instance.admin_up,
680
    'memory': bep[constants.BE_MEMORY],
681
    'vcpus': bep[constants.BE_VCPUS],
682
    'nics': _NICListToTuple(lu, instance.nics),
683
    'disk_template': instance.disk_template,
684
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
685
    'bep': bep,
686
    'hvp': hvp,
687
    'hypervisor_name': instance.hypervisor,
688
  }
689
  if override:
690
    args.update(override)
691
  return _BuildInstanceHookEnv(**args)
692

    
693

    
694
def _AdjustCandidatePool(lu, exceptions):
695
  """Adjust the candidate pool after node operations.
696

697
  """
698
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
699
  if mod_list:
700
    lu.LogInfo("Promoted nodes to master candidate role: %s",
701
               ", ".join(node.name for node in mod_list))
702
    for name in mod_list:
703
      lu.context.ReaddNode(name)
704
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
705
  if mc_now > mc_max:
706
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
707
               (mc_now, mc_max))
708

    
709

    
710
def _DecideSelfPromotion(lu, exceptions=None):
711
  """Decide whether I should promote myself as a master candidate.
712

713
  """
714
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
715
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
716
  # the new node will increase mc_max with one, so:
717
  mc_should = min(mc_should + 1, cp_size)
718
  return mc_now < mc_should
719

    
720

    
721
def _CheckNicsBridgesExist(lu, target_nics, target_node,
722
                               profile=constants.PP_DEFAULT):
723
  """Check that the brigdes needed by a list of nics exist.
724

725
  """
726
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
727
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
728
                for nic in target_nics]
729
  brlist = [params[constants.NIC_LINK] for params in paramslist
730
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
731
  if brlist:
732
    result = lu.rpc.call_bridges_exist(target_node, brlist)
733
    result.Raise("Error checking bridges on destination node '%s'" %
734
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
735

    
736

    
737
def _CheckInstanceBridgesExist(lu, instance, node=None):
738
  """Check that the brigdes needed by an instance exist.
739

740
  """
741
  if node is None:
742
    node = instance.primary_node
743
  _CheckNicsBridgesExist(lu, instance.nics, node)
744

    
745

    
746
def _CheckOSVariant(os_obj, name):
747
  """Check whether an OS name conforms to the os variants specification.
748

749
  @type os_obj: L{objects.OS}
750
  @param os_obj: OS object to check
751
  @type name: string
752
  @param name: OS name passed by the user, to check for validity
753

754
  """
755
  if not os_obj.supported_variants:
756
    return
757
  try:
758
    variant = name.split("+", 1)[1]
759
  except IndexError:
760
    raise errors.OpPrereqError("OS name must include a variant",
761
                               errors.ECODE_INVAL)
762

    
763
  if variant not in os_obj.supported_variants:
764
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
765

    
766

    
767
def _GetNodeInstancesInner(cfg, fn):
768
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
769

    
770

    
771
def _GetNodeInstances(cfg, node_name):
772
  """Returns a list of all primary and secondary instances on a node.
773

774
  """
775

    
776
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
777

    
778

    
779
def _GetNodePrimaryInstances(cfg, node_name):
780
  """Returns primary instances on a node.
781

782
  """
783
  return _GetNodeInstancesInner(cfg,
784
                                lambda inst: node_name == inst.primary_node)
785

    
786

    
787
def _GetNodeSecondaryInstances(cfg, node_name):
788
  """Returns secondary instances on a node.
789

790
  """
791
  return _GetNodeInstancesInner(cfg,
792
                                lambda inst: node_name in inst.secondary_nodes)
793

    
794

    
795
def _GetStorageTypeArgs(cfg, storage_type):
796
  """Returns the arguments for a storage type.
797

798
  """
799
  # Special case for file storage
800
  if storage_type == constants.ST_FILE:
801
    # storage.FileStorage wants a list of storage directories
802
    return [[cfg.GetFileStorageDir()]]
803

    
804
  return []
805

    
806

    
807
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
808
  faulty = []
809

    
810
  for dev in instance.disks:
811
    cfg.SetDiskID(dev, node_name)
812

    
813
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
814
  result.Raise("Failed to get disk status from node %s" % node_name,
815
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
816

    
817
  for idx, bdev_status in enumerate(result.payload):
818
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
819
      faulty.append(idx)
820

    
821
  return faulty
822

    
823

    
824
class LUPostInitCluster(LogicalUnit):
825
  """Logical unit for running hooks after cluster initialization.
826

827
  """
828
  HPATH = "cluster-init"
829
  HTYPE = constants.HTYPE_CLUSTER
830
  _OP_REQP = []
831

    
832
  def BuildHooksEnv(self):
833
    """Build hooks env.
834

835
    """
836
    env = {"OP_TARGET": self.cfg.GetClusterName()}
837
    mn = self.cfg.GetMasterNode()
838
    return env, [], [mn]
839

    
840
  def CheckPrereq(self):
841
    """No prerequisites to check.
842

843
    """
844
    return True
845

    
846
  def Exec(self, feedback_fn):
847
    """Nothing to do.
848

849
    """
850
    return True
851

    
852

    
853
class LUDestroyCluster(LogicalUnit):
854
  """Logical unit for destroying the cluster.
855

856
  """
857
  HPATH = "cluster-destroy"
858
  HTYPE = constants.HTYPE_CLUSTER
859
  _OP_REQP = []
860

    
861
  def BuildHooksEnv(self):
862
    """Build hooks env.
863

864
    """
865
    env = {"OP_TARGET": self.cfg.GetClusterName()}
866
    return env, [], []
867

    
868
  def CheckPrereq(self):
869
    """Check prerequisites.
870

871
    This checks whether the cluster is empty.
872

873
    Any errors are signaled by raising errors.OpPrereqError.
874

875
    """
876
    master = self.cfg.GetMasterNode()
877

    
878
    nodelist = self.cfg.GetNodeList()
879
    if len(nodelist) != 1 or nodelist[0] != master:
880
      raise errors.OpPrereqError("There are still %d node(s) in"
881
                                 " this cluster." % (len(nodelist) - 1),
882
                                 errors.ECODE_INVAL)
883
    instancelist = self.cfg.GetInstanceList()
884
    if instancelist:
885
      raise errors.OpPrereqError("There are still %d instance(s) in"
886
                                 " this cluster." % len(instancelist),
887
                                 errors.ECODE_INVAL)
888

    
889
  def Exec(self, feedback_fn):
890
    """Destroys the cluster.
891

892
    """
893
    master = self.cfg.GetMasterNode()
894
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
895

    
896
    # Run post hooks on master node before it's removed
897
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
898
    try:
899
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
900
    except:
901
      self.LogWarning("Errors occurred running hooks on %s" % master)
902

    
903
    result = self.rpc.call_node_stop_master(master, False)
904
    result.Raise("Could not disable the master role")
905

    
906
    if modify_ssh_setup:
907
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
908
      utils.CreateBackup(priv_key)
909
      utils.CreateBackup(pub_key)
910

    
911
    return master
912

    
913

    
914
class LUVerifyCluster(LogicalUnit):
915
  """Verifies the cluster status.
916

917
  """
918
  HPATH = "cluster-verify"
919
  HTYPE = constants.HTYPE_CLUSTER
920
  _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"]
921
  REQ_BGL = False
922

    
923
  TCLUSTER = "cluster"
924
  TNODE = "node"
925
  TINSTANCE = "instance"
926

    
927
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
928
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
929
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
930
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
931
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
932
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
933
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
934
  ENODEDRBD = (TNODE, "ENODEDRBD")
935
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
936
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
937
  ENODEHV = (TNODE, "ENODEHV")
938
  ENODELVM = (TNODE, "ENODELVM")
939
  ENODEN1 = (TNODE, "ENODEN1")
940
  ENODENET = (TNODE, "ENODENET")
941
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
942
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
943
  ENODERPC = (TNODE, "ENODERPC")
944
  ENODESSH = (TNODE, "ENODESSH")
945
  ENODEVERSION = (TNODE, "ENODEVERSION")
946
  ENODESETUP = (TNODE, "ENODESETUP")
947

    
948
  ETYPE_FIELD = "code"
949
  ETYPE_ERROR = "ERROR"
950
  ETYPE_WARNING = "WARNING"
951

    
952
  def ExpandNames(self):
953
    self.needed_locks = {
954
      locking.LEVEL_NODE: locking.ALL_SET,
955
      locking.LEVEL_INSTANCE: locking.ALL_SET,
956
    }
957
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
958

    
959
  def _Error(self, ecode, item, msg, *args, **kwargs):
960
    """Format an error message.
961

962
    Based on the opcode's error_codes parameter, either format a
963
    parseable error code, or a simpler error string.
964

965
    This must be called only from Exec and functions called from Exec.
966

967
    """
968
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
969
    itype, etxt = ecode
970
    # first complete the msg
971
    if args:
972
      msg = msg % args
973
    # then format the whole message
974
    if self.op.error_codes:
975
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
976
    else:
977
      if item:
978
        item = " " + item
979
      else:
980
        item = ""
981
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
982
    # and finally report it via the feedback_fn
983
    self._feedback_fn("  - %s" % msg)
984

    
985
  def _ErrorIf(self, cond, *args, **kwargs):
986
    """Log an error message if the passed condition is True.
987

988
    """
989
    cond = bool(cond) or self.op.debug_simulate_errors
990
    if cond:
991
      self._Error(*args, **kwargs)
992
    # do not mark the operation as failed for WARN cases only
993
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
994
      self.bad = self.bad or cond
995

    
996
  def _VerifyNode(self, nodeinfo, file_list, local_cksum,
997
                  node_result, master_files, drbd_map, vg_name):
998
    """Run multiple tests against a node.
999

1000
    Test list:
1001

1002
      - compares ganeti version
1003
      - checks vg existence and size > 20G
1004
      - checks config file checksum
1005
      - checks ssh to other nodes
1006

1007
    @type nodeinfo: L{objects.Node}
1008
    @param nodeinfo: the node to check
1009
    @param file_list: required list of files
1010
    @param local_cksum: dictionary of local files and their checksums
1011
    @param node_result: the results from the node
1012
    @param master_files: list of files that only masters should have
1013
    @param drbd_map: the useddrbd minors for this node, in
1014
        form of minor: (instance, must_exist) which correspond to instances
1015
        and their running status
1016
    @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
1017

1018
    """
1019
    node = nodeinfo.name
1020
    _ErrorIf = self._ErrorIf
1021

    
1022
    # main result, node_result should be a non-empty dict
1023
    test = not node_result or not isinstance(node_result, dict)
1024
    _ErrorIf(test, self.ENODERPC, node,
1025
                  "unable to verify node: no data returned")
1026
    if test:
1027
      return
1028

    
1029
    # compares ganeti version
1030
    local_version = constants.PROTOCOL_VERSION
1031
    remote_version = node_result.get('version', None)
1032
    test = not (remote_version and
1033
                isinstance(remote_version, (list, tuple)) and
1034
                len(remote_version) == 2)
1035
    _ErrorIf(test, self.ENODERPC, node,
1036
             "connection to node returned invalid data")
1037
    if test:
1038
      return
1039

    
1040
    test = local_version != remote_version[0]
1041
    _ErrorIf(test, self.ENODEVERSION, node,
1042
             "incompatible protocol versions: master %s,"
1043
             " node %s", local_version, remote_version[0])
1044
    if test:
1045
      return
1046

    
1047
    # node seems compatible, we can actually try to look into its results
1048

    
1049
    # full package version
1050
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1051
                  self.ENODEVERSION, node,
1052
                  "software version mismatch: master %s, node %s",
1053
                  constants.RELEASE_VERSION, remote_version[1],
1054
                  code=self.ETYPE_WARNING)
1055

    
1056
    # checks vg existence and size > 20G
1057
    if vg_name is not None:
1058
      vglist = node_result.get(constants.NV_VGLIST, None)
1059
      test = not vglist
1060
      _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1061
      if not test:
1062
        vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1063
                                              constants.MIN_VG_SIZE)
1064
        _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1065

    
1066
    # checks config file checksum
1067

    
1068
    remote_cksum = node_result.get(constants.NV_FILELIST, None)
1069
    test = not isinstance(remote_cksum, dict)
1070
    _ErrorIf(test, self.ENODEFILECHECK, node,
1071
             "node hasn't returned file checksum data")
1072
    if not test:
1073
      for file_name in file_list:
1074
        node_is_mc = nodeinfo.master_candidate
1075
        must_have = (file_name not in master_files) or node_is_mc
1076
        # missing
1077
        test1 = file_name not in remote_cksum
1078
        # invalid checksum
1079
        test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1080
        # existing and good
1081
        test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1082
        _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1083
                 "file '%s' missing", file_name)
1084
        _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1085
                 "file '%s' has wrong checksum", file_name)
1086
        # not candidate and this is not a must-have file
1087
        _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1088
                 "file '%s' should not exist on non master"
1089
                 " candidates (and the file is outdated)", file_name)
1090
        # all good, except non-master/non-must have combination
1091
        _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1092
                 "file '%s' should not exist"
1093
                 " on non master candidates", file_name)
1094

    
1095
    # checks ssh to any
1096

    
1097
    test = constants.NV_NODELIST not in node_result
1098
    _ErrorIf(test, self.ENODESSH, node,
1099
             "node hasn't returned node ssh connectivity data")
1100
    if not test:
1101
      if node_result[constants.NV_NODELIST]:
1102
        for a_node, a_msg in node_result[constants.NV_NODELIST].items():
1103
          _ErrorIf(True, self.ENODESSH, node,
1104
                   "ssh communication with node '%s': %s", a_node, a_msg)
1105

    
1106
    test = constants.NV_NODENETTEST not in node_result
1107
    _ErrorIf(test, self.ENODENET, node,
1108
             "node hasn't returned node tcp connectivity data")
1109
    if not test:
1110
      if node_result[constants.NV_NODENETTEST]:
1111
        nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
1112
        for anode in nlist:
1113
          _ErrorIf(True, self.ENODENET, node,
1114
                   "tcp communication with node '%s': %s",
1115
                   anode, node_result[constants.NV_NODENETTEST][anode])
1116

    
1117
    hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
1118
    if isinstance(hyp_result, dict):
1119
      for hv_name, hv_result in hyp_result.iteritems():
1120
        test = hv_result is not None
1121
        _ErrorIf(test, self.ENODEHV, node,
1122
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1123

    
1124
    # check used drbd list
1125
    if vg_name is not None:
1126
      used_minors = node_result.get(constants.NV_DRBDLIST, [])
1127
      test = not isinstance(used_minors, (tuple, list))
1128
      _ErrorIf(test, self.ENODEDRBD, node,
1129
               "cannot parse drbd status file: %s", str(used_minors))
1130
      if not test:
1131
        for minor, (iname, must_exist) in drbd_map.items():
1132
          test = minor not in used_minors and must_exist
1133
          _ErrorIf(test, self.ENODEDRBD, node,
1134
                   "drbd minor %d of instance %s is not active",
1135
                   minor, iname)
1136
        for minor in used_minors:
1137
          test = minor not in drbd_map
1138
          _ErrorIf(test, self.ENODEDRBD, node,
1139
                   "unallocated drbd minor %d is in use", minor)
1140
    test = node_result.get(constants.NV_NODESETUP,
1141
                           ["Missing NODESETUP results"])
1142
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1143
             "; ".join(test))
1144

    
1145
    # check pv names
1146
    if vg_name is not None:
1147
      pvlist = node_result.get(constants.NV_PVLIST, None)
1148
      test = pvlist is None
1149
      _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1150
      if not test:
1151
        # check that ':' is not present in PV names, since it's a
1152
        # special character for lvcreate (denotes the range of PEs to
1153
        # use on the PV)
1154
        for size, pvname, owner_vg in pvlist:
1155
          test = ":" in pvname
1156
          _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1157
                   " '%s' of VG '%s'", pvname, owner_vg)
1158

    
1159
  def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
1160
                      node_instance, n_offline):
1161
    """Verify an instance.
1162

1163
    This function checks to see if the required block devices are
1164
    available on the instance's node.
1165

1166
    """
1167
    _ErrorIf = self._ErrorIf
1168
    node_current = instanceconfig.primary_node
1169

    
1170
    node_vol_should = {}
1171
    instanceconfig.MapLVsByNode(node_vol_should)
1172

    
1173
    for node in node_vol_should:
1174
      if node in n_offline:
1175
        # ignore missing volumes on offline nodes
1176
        continue
1177
      for volume in node_vol_should[node]:
1178
        test = node not in node_vol_is or volume not in node_vol_is[node]
1179
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1180
                 "volume %s missing on node %s", volume, node)
1181

    
1182
    if instanceconfig.admin_up:
1183
      test = ((node_current not in node_instance or
1184
               not instance in node_instance[node_current]) and
1185
              node_current not in n_offline)
1186
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1187
               "instance not running on its primary node %s",
1188
               node_current)
1189

    
1190
    for node in node_instance:
1191
      if (not node == node_current):
1192
        test = instance in node_instance[node]
1193
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1194
                 "instance should not run on node %s", node)
1195

    
1196
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is):
1197
    """Verify if there are any unknown volumes in the cluster.
1198

1199
    The .os, .swap and backup volumes are ignored. All other volumes are
1200
    reported as unknown.
1201

1202
    """
1203
    for node in node_vol_is:
1204
      for volume in node_vol_is[node]:
1205
        test = (node not in node_vol_should or
1206
                volume not in node_vol_should[node])
1207
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1208
                      "volume %s is unknown", volume)
1209

    
1210
  def _VerifyOrphanInstances(self, instancelist, node_instance):
1211
    """Verify the list of running instances.
1212

1213
    This checks what instances are running but unknown to the cluster.
1214

1215
    """
1216
    for node in node_instance:
1217
      for o_inst in node_instance[node]:
1218
        test = o_inst not in instancelist
1219
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1220
                      "instance %s on node %s should not exist", o_inst, node)
1221

    
1222
  def _VerifyNPlusOneMemory(self, node_info, instance_cfg):
1223
    """Verify N+1 Memory Resilience.
1224

1225
    Check that if one single node dies we can still start all the instances it
1226
    was primary for.
1227

1228
    """
1229
    for node, nodeinfo in node_info.iteritems():
1230
      # This code checks that every node which is now listed as secondary has
1231
      # enough memory to host all instances it is supposed to should a single
1232
      # other node in the cluster fail.
1233
      # FIXME: not ready for failover to an arbitrary node
1234
      # FIXME: does not support file-backed instances
1235
      # WARNING: we currently take into account down instances as well as up
1236
      # ones, considering that even if they're down someone might want to start
1237
      # them even in the event of a node failure.
1238
      for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
1239
        needed_mem = 0
1240
        for instance in instances:
1241
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1242
          if bep[constants.BE_AUTO_BALANCE]:
1243
            needed_mem += bep[constants.BE_MEMORY]
1244
        test = nodeinfo['mfree'] < needed_mem
1245
        self._ErrorIf(test, self.ENODEN1, node,
1246
                      "not enough memory on to accommodate"
1247
                      " failovers should peer node %s fail", prinode)
1248

    
1249
  def CheckPrereq(self):
1250
    """Check prerequisites.
1251

1252
    Transform the list of checks we're going to skip into a set and check that
1253
    all its members are valid.
1254

1255
    """
1256
    self.skip_set = frozenset(self.op.skip_checks)
1257
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1258
      raise errors.OpPrereqError("Invalid checks to be skipped specified",
1259
                                 errors.ECODE_INVAL)
1260

    
1261
  def BuildHooksEnv(self):
1262
    """Build hooks env.
1263

1264
    Cluster-Verify hooks just ran in the post phase and their failure makes
1265
    the output be logged in the verify output and the verification to fail.
1266

1267
    """
1268
    all_nodes = self.cfg.GetNodeList()
1269
    env = {
1270
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1271
      }
1272
    for node in self.cfg.GetAllNodesInfo().values():
1273
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1274

    
1275
    return env, [], all_nodes
1276

    
1277
  def Exec(self, feedback_fn):
1278
    """Verify integrity of cluster, performing various test on nodes.
1279

1280
    """
1281
    self.bad = False
1282
    _ErrorIf = self._ErrorIf
1283
    verbose = self.op.verbose
1284
    self._feedback_fn = feedback_fn
1285
    feedback_fn("* Verifying global settings")
1286
    for msg in self.cfg.VerifyConfig():
1287
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1288

    
1289
    vg_name = self.cfg.GetVGName()
1290
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1291
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1292
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1293
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1294
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1295
                        for iname in instancelist)
1296
    i_non_redundant = [] # Non redundant instances
1297
    i_non_a_balanced = [] # Non auto-balanced instances
1298
    n_offline = [] # List of offline nodes
1299
    n_drained = [] # List of nodes being drained
1300
    node_volume = {}
1301
    node_instance = {}
1302
    node_info = {}
1303
    instance_cfg = {}
1304

    
1305
    # FIXME: verify OS list
1306
    # do local checksums
1307
    master_files = [constants.CLUSTER_CONF_FILE]
1308

    
1309
    file_names = ssconf.SimpleStore().GetFileList()
1310
    file_names.append(constants.SSL_CERT_FILE)
1311
    file_names.append(constants.RAPI_CERT_FILE)
1312
    file_names.extend(master_files)
1313

    
1314
    local_checksums = utils.FingerprintFiles(file_names)
1315

    
1316
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1317
    node_verify_param = {
1318
      constants.NV_FILELIST: file_names,
1319
      constants.NV_NODELIST: [node.name for node in nodeinfo
1320
                              if not node.offline],
1321
      constants.NV_HYPERVISOR: hypervisors,
1322
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1323
                                  node.secondary_ip) for node in nodeinfo
1324
                                 if not node.offline],
1325
      constants.NV_INSTANCELIST: hypervisors,
1326
      constants.NV_VERSION: None,
1327
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1328
      constants.NV_NODESETUP: None,
1329
      }
1330
    if vg_name is not None:
1331
      node_verify_param[constants.NV_VGLIST] = None
1332
      node_verify_param[constants.NV_LVLIST] = vg_name
1333
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1334
      node_verify_param[constants.NV_DRBDLIST] = None
1335
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1336
                                           self.cfg.GetClusterName())
1337

    
1338
    cluster = self.cfg.GetClusterInfo()
1339
    master_node = self.cfg.GetMasterNode()
1340
    all_drbd_map = self.cfg.ComputeDRBDMap()
1341

    
1342
    feedback_fn("* Verifying node status")
1343
    for node_i in nodeinfo:
1344
      node = node_i.name
1345

    
1346
      if node_i.offline:
1347
        if verbose:
1348
          feedback_fn("* Skipping offline node %s" % (node,))
1349
        n_offline.append(node)
1350
        continue
1351

    
1352
      if node == master_node:
1353
        ntype = "master"
1354
      elif node_i.master_candidate:
1355
        ntype = "master candidate"
1356
      elif node_i.drained:
1357
        ntype = "drained"
1358
        n_drained.append(node)
1359
      else:
1360
        ntype = "regular"
1361
      if verbose:
1362
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1363

    
1364
      msg = all_nvinfo[node].fail_msg
1365
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
1366
      if msg:
1367
        continue
1368

    
1369
      nresult = all_nvinfo[node].payload
1370
      node_drbd = {}
1371
      for minor, instance in all_drbd_map[node].items():
1372
        test = instance not in instanceinfo
1373
        _ErrorIf(test, self.ECLUSTERCFG, None,
1374
                 "ghost instance '%s' in temporary DRBD map", instance)
1375
          # ghost instance should not be running, but otherwise we
1376
          # don't give double warnings (both ghost instance and
1377
          # unallocated minor in use)
1378
        if test:
1379
          node_drbd[minor] = (instance, False)
1380
        else:
1381
          instance = instanceinfo[instance]
1382
          node_drbd[minor] = (instance.name, instance.admin_up)
1383
      self._VerifyNode(node_i, file_names, local_checksums,
1384
                       nresult, master_files, node_drbd, vg_name)
1385

    
1386
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1387
      if vg_name is None:
1388
        node_volume[node] = {}
1389
      elif isinstance(lvdata, basestring):
1390
        _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1391
                 utils.SafeEncode(lvdata))
1392
        node_volume[node] = {}
1393
      elif not isinstance(lvdata, dict):
1394
        _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1395
        continue
1396
      else:
1397
        node_volume[node] = lvdata
1398

    
1399
      # node_instance
1400
      idata = nresult.get(constants.NV_INSTANCELIST, None)
1401
      test = not isinstance(idata, list)
1402
      _ErrorIf(test, self.ENODEHV, node,
1403
               "rpc call to node failed (instancelist)")
1404
      if test:
1405
        continue
1406

    
1407
      node_instance[node] = idata
1408

    
1409
      # node_info
1410
      nodeinfo = nresult.get(constants.NV_HVINFO, None)
1411
      test = not isinstance(nodeinfo, dict)
1412
      _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1413
      if test:
1414
        continue
1415

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

    
1444
    node_vol_should = {}
1445

    
1446
    feedback_fn("* Verifying instance status")
1447
    for instance in instancelist:
1448
      if verbose:
1449
        feedback_fn("* Verifying instance %s" % instance)
1450
      inst_config = instanceinfo[instance]
1451
      self._VerifyInstance(instance, inst_config, node_volume,
1452
                           node_instance, n_offline)
1453
      inst_nodes_offline = []
1454

    
1455
      inst_config.MapLVsByNode(node_vol_should)
1456

    
1457
      instance_cfg[instance] = inst_config
1458

    
1459
      pnode = inst_config.primary_node
1460
      _ErrorIf(pnode not in node_info and pnode not in n_offline,
1461
               self.ENODERPC, pnode, "instance %s, connection to"
1462
               " primary node failed", instance)
1463
      if pnode in node_info:
1464
        node_info[pnode]['pinst'].append(instance)
1465

    
1466
      if pnode in n_offline:
1467
        inst_nodes_offline.append(pnode)
1468

    
1469
      # If the instance is non-redundant we cannot survive losing its primary
1470
      # node, so we are not N+1 compliant. On the other hand we have no disk
1471
      # templates with more than one secondary so that situation is not well
1472
      # supported either.
1473
      # FIXME: does not support file-backed instances
1474
      if len(inst_config.secondary_nodes) == 0:
1475
        i_non_redundant.append(instance)
1476
      _ErrorIf(len(inst_config.secondary_nodes) > 1,
1477
               self.EINSTANCELAYOUT, instance,
1478
               "instance has multiple secondary nodes", code="WARNING")
1479

    
1480
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1481
        i_non_a_balanced.append(instance)
1482

    
1483
      for snode in inst_config.secondary_nodes:
1484
        _ErrorIf(snode not in node_info and snode not in n_offline,
1485
                 self.ENODERPC, snode,
1486
                 "instance %s, connection to secondary node"
1487
                 "failed", instance)
1488

    
1489
        if snode in node_info:
1490
          node_info[snode]['sinst'].append(instance)
1491
          if pnode not in node_info[snode]['sinst-by-pnode']:
1492
            node_info[snode]['sinst-by-pnode'][pnode] = []
1493
          node_info[snode]['sinst-by-pnode'][pnode].append(instance)
1494

    
1495
        if snode in n_offline:
1496
          inst_nodes_offline.append(snode)
1497

    
1498
      # warn that the instance lives on offline nodes
1499
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
1500
               "instance lives on offline node(s) %s",
1501
               ", ".join(inst_nodes_offline))
1502

    
1503
    feedback_fn("* Verifying orphan volumes")
1504
    self._VerifyOrphanVolumes(node_vol_should, node_volume)
1505

    
1506
    feedback_fn("* Verifying remaining instances")
1507
    self._VerifyOrphanInstances(instancelist, node_instance)
1508

    
1509
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1510
      feedback_fn("* Verifying N+1 Memory redundancy")
1511
      self._VerifyNPlusOneMemory(node_info, instance_cfg)
1512

    
1513
    feedback_fn("* Other Notes")
1514
    if i_non_redundant:
1515
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1516
                  % len(i_non_redundant))
1517

    
1518
    if i_non_a_balanced:
1519
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1520
                  % len(i_non_a_balanced))
1521

    
1522
    if n_offline:
1523
      feedback_fn("  - NOTICE: %d offline node(s) found." % len(n_offline))
1524

    
1525
    if n_drained:
1526
      feedback_fn("  - NOTICE: %d drained node(s) found." % len(n_drained))
1527

    
1528
    return not self.bad
1529

    
1530
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1531
    """Analyze the post-hooks' result
1532

1533
    This method analyses the hook result, handles it, and sends some
1534
    nicely-formatted feedback back to the user.
1535

1536
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1537
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1538
    @param hooks_results: the results of the multi-node hooks rpc call
1539
    @param feedback_fn: function used send feedback back to the caller
1540
    @param lu_result: previous Exec result
1541
    @return: the new Exec result, based on the previous result
1542
        and hook results
1543

1544
    """
1545
    # We only really run POST phase hooks, and are only interested in
1546
    # their results
1547
    if phase == constants.HOOKS_PHASE_POST:
1548
      # Used to change hooks' output to proper indentation
1549
      indent_re = re.compile('^', re.M)
1550
      feedback_fn("* Hooks Results")
1551
      assert hooks_results, "invalid result from hooks"
1552

    
1553
      for node_name in hooks_results:
1554
        show_node_header = True
1555
        res = hooks_results[node_name]
1556
        msg = res.fail_msg
1557
        test = msg and not res.offline
1558
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
1559
                      "Communication failure in hooks execution: %s", msg)
1560
        if test:
1561
          # override manually lu_result here as _ErrorIf only
1562
          # overrides self.bad
1563
          lu_result = 1
1564
          continue
1565
        for script, hkr, output in res.payload:
1566
          test = hkr == constants.HKR_FAIL
1567
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
1568
                        "Script %s failed, output:", script)
1569
          if test:
1570
            output = indent_re.sub('      ', output)
1571
            feedback_fn("%s" % output)
1572
            lu_result = 1
1573

    
1574
      return lu_result
1575

    
1576

    
1577
class LUVerifyDisks(NoHooksLU):
1578
  """Verifies the cluster disks status.
1579

1580
  """
1581
  _OP_REQP = []
1582
  REQ_BGL = False
1583

    
1584
  def ExpandNames(self):
1585
    self.needed_locks = {
1586
      locking.LEVEL_NODE: locking.ALL_SET,
1587
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1588
    }
1589
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1590

    
1591
  def CheckPrereq(self):
1592
    """Check prerequisites.
1593

1594
    This has no prerequisites.
1595

1596
    """
1597
    pass
1598

    
1599
  def Exec(self, feedback_fn):
1600
    """Verify integrity of cluster disks.
1601

1602
    @rtype: tuple of three items
1603
    @return: a tuple of (dict of node-to-node_error, list of instances
1604
        which need activate-disks, dict of instance: (node, volume) for
1605
        missing volumes
1606

1607
    """
1608
    result = res_nodes, res_instances, res_missing = {}, [], {}
1609

    
1610
    vg_name = self.cfg.GetVGName()
1611
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1612
    instances = [self.cfg.GetInstanceInfo(name)
1613
                 for name in self.cfg.GetInstanceList()]
1614

    
1615
    nv_dict = {}
1616
    for inst in instances:
1617
      inst_lvs = {}
1618
      if (not inst.admin_up or
1619
          inst.disk_template not in constants.DTS_NET_MIRROR):
1620
        continue
1621
      inst.MapLVsByNode(inst_lvs)
1622
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1623
      for node, vol_list in inst_lvs.iteritems():
1624
        for vol in vol_list:
1625
          nv_dict[(node, vol)] = inst
1626

    
1627
    if not nv_dict:
1628
      return result
1629

    
1630
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1631

    
1632
    for node in nodes:
1633
      # node_volume
1634
      node_res = node_lvs[node]
1635
      if node_res.offline:
1636
        continue
1637
      msg = node_res.fail_msg
1638
      if msg:
1639
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1640
        res_nodes[node] = msg
1641
        continue
1642

    
1643
      lvs = node_res.payload
1644
      for lv_name, (_, lv_inactive, lv_online) in lvs.items():
1645
        inst = nv_dict.pop((node, lv_name), None)
1646
        if (not lv_online and inst is not None
1647
            and inst.name not in res_instances):
1648
          res_instances.append(inst.name)
1649

    
1650
    # any leftover items in nv_dict are missing LVs, let's arrange the
1651
    # data better
1652
    for key, inst in nv_dict.iteritems():
1653
      if inst.name not in res_missing:
1654
        res_missing[inst.name] = []
1655
      res_missing[inst.name].append(key)
1656

    
1657
    return result
1658

    
1659

    
1660
class LURepairDiskSizes(NoHooksLU):
1661
  """Verifies the cluster disks sizes.
1662

1663
  """
1664
  _OP_REQP = ["instances"]
1665
  REQ_BGL = False
1666

    
1667
  def ExpandNames(self):
1668
    if not isinstance(self.op.instances, list):
1669
      raise errors.OpPrereqError("Invalid argument type 'instances'",
1670
                                 errors.ECODE_INVAL)
1671

    
1672
    if self.op.instances:
1673
      self.wanted_names = []
1674
      for name in self.op.instances:
1675
        full_name = self.cfg.ExpandInstanceName(name)
1676
        if full_name is None:
1677
          raise errors.OpPrereqError("Instance '%s' not known" % name,
1678
                                     errors.ECODE_NOENT)
1679
        self.wanted_names.append(full_name)
1680
      self.needed_locks = {
1681
        locking.LEVEL_NODE: [],
1682
        locking.LEVEL_INSTANCE: self.wanted_names,
1683
        }
1684
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1685
    else:
1686
      self.wanted_names = None
1687
      self.needed_locks = {
1688
        locking.LEVEL_NODE: locking.ALL_SET,
1689
        locking.LEVEL_INSTANCE: locking.ALL_SET,
1690
        }
1691
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1692

    
1693
  def DeclareLocks(self, level):
1694
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
1695
      self._LockInstancesNodes(primary_only=True)
1696

    
1697
  def CheckPrereq(self):
1698
    """Check prerequisites.
1699

1700
    This only checks the optional instance list against the existing names.
1701

1702
    """
1703
    if self.wanted_names is None:
1704
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
1705

    
1706
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
1707
                             in self.wanted_names]
1708

    
1709
  def _EnsureChildSizes(self, disk):
1710
    """Ensure children of the disk have the needed disk size.
1711

1712
    This is valid mainly for DRBD8 and fixes an issue where the
1713
    children have smaller disk size.
1714

1715
    @param disk: an L{ganeti.objects.Disk} object
1716

1717
    """
1718
    if disk.dev_type == constants.LD_DRBD8:
1719
      assert disk.children, "Empty children for DRBD8?"
1720
      fchild = disk.children[0]
1721
      mismatch = fchild.size < disk.size
1722
      if mismatch:
1723
        self.LogInfo("Child disk has size %d, parent %d, fixing",
1724
                     fchild.size, disk.size)
1725
        fchild.size = disk.size
1726

    
1727
      # and we recurse on this child only, not on the metadev
1728
      return self._EnsureChildSizes(fchild) or mismatch
1729
    else:
1730
      return False
1731

    
1732
  def Exec(self, feedback_fn):
1733
    """Verify the size of cluster disks.
1734

1735
    """
1736
    # TODO: check child disks too
1737
    # TODO: check differences in size between primary/secondary nodes
1738
    per_node_disks = {}
1739
    for instance in self.wanted_instances:
1740
      pnode = instance.primary_node
1741
      if pnode not in per_node_disks:
1742
        per_node_disks[pnode] = []
1743
      for idx, disk in enumerate(instance.disks):
1744
        per_node_disks[pnode].append((instance, idx, disk))
1745

    
1746
    changed = []
1747
    for node, dskl in per_node_disks.items():
1748
      newl = [v[2].Copy() for v in dskl]
1749
      for dsk in newl:
1750
        self.cfg.SetDiskID(dsk, node)
1751
      result = self.rpc.call_blockdev_getsizes(node, newl)
1752
      if result.fail_msg:
1753
        self.LogWarning("Failure in blockdev_getsizes call to node"
1754
                        " %s, ignoring", node)
1755
        continue
1756
      if len(result.data) != len(dskl):
1757
        self.LogWarning("Invalid result from node %s, ignoring node results",
1758
                        node)
1759
        continue
1760
      for ((instance, idx, disk), size) in zip(dskl, result.data):
1761
        if size is None:
1762
          self.LogWarning("Disk %d of instance %s did not return size"
1763
                          " information, ignoring", idx, instance.name)
1764
          continue
1765
        if not isinstance(size, (int, long)):
1766
          self.LogWarning("Disk %d of instance %s did not return valid"
1767
                          " size information, ignoring", idx, instance.name)
1768
          continue
1769
        size = size >> 20
1770
        if size != disk.size:
1771
          self.LogInfo("Disk %d of instance %s has mismatched size,"
1772
                       " correcting: recorded %d, actual %d", idx,
1773
                       instance.name, disk.size, size)
1774
          disk.size = size
1775
          self.cfg.Update(instance, feedback_fn)
1776
          changed.append((instance.name, idx, size))
1777
        if self._EnsureChildSizes(disk):
1778
          self.cfg.Update(instance, feedback_fn)
1779
          changed.append((instance.name, idx, disk.size))
1780
    return changed
1781

    
1782

    
1783
class LURenameCluster(LogicalUnit):
1784
  """Rename the cluster.
1785

1786
  """
1787
  HPATH = "cluster-rename"
1788
  HTYPE = constants.HTYPE_CLUSTER
1789
  _OP_REQP = ["name"]
1790

    
1791
  def BuildHooksEnv(self):
1792
    """Build hooks env.
1793

1794
    """
1795
    env = {
1796
      "OP_TARGET": self.cfg.GetClusterName(),
1797
      "NEW_NAME": self.op.name,
1798
      }
1799
    mn = self.cfg.GetMasterNode()
1800
    return env, [mn], [mn]
1801

    
1802
  def CheckPrereq(self):
1803
    """Verify that the passed name is a valid one.
1804

1805
    """
1806
    hostname = utils.GetHostInfo(self.op.name)
1807

    
1808
    new_name = hostname.name
1809
    self.ip = new_ip = hostname.ip
1810
    old_name = self.cfg.GetClusterName()
1811
    old_ip = self.cfg.GetMasterIP()
1812
    if new_name == old_name and new_ip == old_ip:
1813
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
1814
                                 " cluster has changed",
1815
                                 errors.ECODE_INVAL)
1816
    if new_ip != old_ip:
1817
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1818
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
1819
                                   " reachable on the network. Aborting." %
1820
                                   new_ip, errors.ECODE_NOTUNIQUE)
1821

    
1822
    self.op.name = new_name
1823

    
1824
  def Exec(self, feedback_fn):
1825
    """Rename the cluster.
1826

1827
    """
1828
    clustername = self.op.name
1829
    ip = self.ip
1830

    
1831
    # shutdown the master IP
1832
    master = self.cfg.GetMasterNode()
1833
    result = self.rpc.call_node_stop_master(master, False)
1834
    result.Raise("Could not disable the master role")
1835

    
1836
    try:
1837
      cluster = self.cfg.GetClusterInfo()
1838
      cluster.cluster_name = clustername
1839
      cluster.master_ip = ip
1840
      self.cfg.Update(cluster, feedback_fn)
1841

    
1842
      # update the known hosts file
1843
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
1844
      node_list = self.cfg.GetNodeList()
1845
      try:
1846
        node_list.remove(master)
1847
      except ValueError:
1848
        pass
1849
      result = self.rpc.call_upload_file(node_list,
1850
                                         constants.SSH_KNOWN_HOSTS_FILE)
1851
      for to_node, to_result in result.iteritems():
1852
        msg = to_result.fail_msg
1853
        if msg:
1854
          msg = ("Copy of file %s to node %s failed: %s" %
1855
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
1856
          self.proc.LogWarning(msg)
1857

    
1858
    finally:
1859
      result = self.rpc.call_node_start_master(master, False, False)
1860
      msg = result.fail_msg
1861
      if msg:
1862
        self.LogWarning("Could not re-enable the master role on"
1863
                        " the master, please restart manually: %s", msg)
1864

    
1865

    
1866
def _RecursiveCheckIfLVMBased(disk):
1867
  """Check if the given disk or its children are lvm-based.
1868

1869
  @type disk: L{objects.Disk}
1870
  @param disk: the disk to check
1871
  @rtype: boolean
1872
  @return: boolean indicating whether a LD_LV dev_type was found or not
1873

1874
  """
1875
  if disk.children:
1876
    for chdisk in disk.children:
1877
      if _RecursiveCheckIfLVMBased(chdisk):
1878
        return True
1879
  return disk.dev_type == constants.LD_LV
1880

    
1881

    
1882
class LUSetClusterParams(LogicalUnit):
1883
  """Change the parameters of the cluster.
1884

1885
  """
1886
  HPATH = "cluster-modify"
1887
  HTYPE = constants.HTYPE_CLUSTER
1888
  _OP_REQP = []
1889
  REQ_BGL = False
1890

    
1891
  def CheckArguments(self):
1892
    """Check parameters
1893

1894
    """
1895
    if not hasattr(self.op, "candidate_pool_size"):
1896
      self.op.candidate_pool_size = None
1897
    if self.op.candidate_pool_size is not None:
1898
      try:
1899
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
1900
      except (ValueError, TypeError), err:
1901
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
1902
                                   str(err), errors.ECODE_INVAL)
1903
      if self.op.candidate_pool_size < 1:
1904
        raise errors.OpPrereqError("At least one master candidate needed",
1905
                                   errors.ECODE_INVAL)
1906

    
1907
  def ExpandNames(self):
1908
    # FIXME: in the future maybe other cluster params won't require checking on
1909
    # all nodes to be modified.
1910
    self.needed_locks = {
1911
      locking.LEVEL_NODE: locking.ALL_SET,
1912
    }
1913
    self.share_locks[locking.LEVEL_NODE] = 1
1914

    
1915
  def BuildHooksEnv(self):
1916
    """Build hooks env.
1917

1918
    """
1919
    env = {
1920
      "OP_TARGET": self.cfg.GetClusterName(),
1921
      "NEW_VG_NAME": self.op.vg_name,
1922
      }
1923
    mn = self.cfg.GetMasterNode()
1924
    return env, [mn], [mn]
1925

    
1926
  def CheckPrereq(self):
1927
    """Check prerequisites.
1928

1929
    This checks whether the given params don't conflict and
1930
    if the given volume group is valid.
1931

1932
    """
1933
    if self.op.vg_name is not None and not self.op.vg_name:
1934
      instances = self.cfg.GetAllInstancesInfo().values()
1935
      for inst in instances:
1936
        for disk in inst.disks:
1937
          if _RecursiveCheckIfLVMBased(disk):
1938
            raise errors.OpPrereqError("Cannot disable lvm storage while"
1939
                                       " lvm-based instances exist",
1940
                                       errors.ECODE_INVAL)
1941

    
1942
    node_list = self.acquired_locks[locking.LEVEL_NODE]
1943

    
1944
    # if vg_name not None, checks given volume group on all nodes
1945
    if self.op.vg_name:
1946
      vglist = self.rpc.call_vg_list(node_list)
1947
      for node in node_list:
1948
        msg = vglist[node].fail_msg
1949
        if msg:
1950
          # ignoring down node
1951
          self.LogWarning("Error while gathering data on node %s"
1952
                          " (ignoring node): %s", node, msg)
1953
          continue
1954
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
1955
                                              self.op.vg_name,
1956
                                              constants.MIN_VG_SIZE)
1957
        if vgstatus:
1958
          raise errors.OpPrereqError("Error on node '%s': %s" %
1959
                                     (node, vgstatus), errors.ECODE_ENVIRON)
1960

    
1961
    self.cluster = cluster = self.cfg.GetClusterInfo()
1962
    # validate params changes
1963
    if self.op.beparams:
1964
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
1965
      self.new_beparams = objects.FillDict(
1966
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
1967

    
1968
    if self.op.nicparams:
1969
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
1970
      self.new_nicparams = objects.FillDict(
1971
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
1972
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
1973
      nic_errors = []
1974

    
1975
      # check all instances for consistency
1976
      for instance in self.cfg.GetAllInstancesInfo().values():
1977
        for nic_idx, nic in enumerate(instance.nics):
1978
          params_copy = copy.deepcopy(nic.nicparams)
1979
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
1980

    
1981
          # check parameter syntax
1982
          try:
1983
            objects.NIC.CheckParameterSyntax(params_filled)
1984
          except errors.ConfigurationError, err:
1985
            nic_errors.append("Instance %s, nic/%d: %s" %
1986
                              (instance.name, nic_idx, err))
1987

    
1988
          # if we're moving instances to routed, check that they have an ip
1989
          target_mode = params_filled[constants.NIC_MODE]
1990
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
1991
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
1992
                              (instance.name, nic_idx))
1993
      if nic_errors:
1994
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
1995
                                   "\n".join(nic_errors))
1996

    
1997
    # hypervisor list/parameters
1998
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
1999
    if self.op.hvparams:
2000
      if not isinstance(self.op.hvparams, dict):
2001
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input",
2002
                                   errors.ECODE_INVAL)
2003
      for hv_name, hv_dict in self.op.hvparams.items():
2004
        if hv_name not in self.new_hvparams:
2005
          self.new_hvparams[hv_name] = hv_dict
2006
        else:
2007
          self.new_hvparams[hv_name].update(hv_dict)
2008

    
2009
    if self.op.enabled_hypervisors is not None:
2010
      self.hv_list = self.op.enabled_hypervisors
2011
      if not self.hv_list:
2012
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
2013
                                   " least one member",
2014
                                   errors.ECODE_INVAL)
2015
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
2016
      if invalid_hvs:
2017
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
2018
                                   " entries: %s" % " ,".join(invalid_hvs),
2019
                                   errors.ECODE_INVAL)
2020
    else:
2021
      self.hv_list = cluster.enabled_hypervisors
2022

    
2023
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2024
      # either the enabled list has changed, or the parameters have, validate
2025
      for hv_name, hv_params in self.new_hvparams.items():
2026
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2027
            (self.op.enabled_hypervisors and
2028
             hv_name in self.op.enabled_hypervisors)):
2029
          # either this is a new hypervisor, or its parameters have changed
2030
          hv_class = hypervisor.GetHypervisor(hv_name)
2031
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2032
          hv_class.CheckParameterSyntax(hv_params)
2033
          _CheckHVParams(self, node_list, hv_name, hv_params)
2034

    
2035
  def Exec(self, feedback_fn):
2036
    """Change the parameters of the cluster.
2037

2038
    """
2039
    if self.op.vg_name is not None:
2040
      new_volume = self.op.vg_name
2041
      if not new_volume:
2042
        new_volume = None
2043
      if new_volume != self.cfg.GetVGName():
2044
        self.cfg.SetVGName(new_volume)
2045
      else:
2046
        feedback_fn("Cluster LVM configuration already in desired"
2047
                    " state, not changing")
2048
    if self.op.hvparams:
2049
      self.cluster.hvparams = self.new_hvparams
2050
    if self.op.enabled_hypervisors is not None:
2051
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2052
    if self.op.beparams:
2053
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2054
    if self.op.nicparams:
2055
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2056

    
2057
    if self.op.candidate_pool_size is not None:
2058
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2059
      # we need to update the pool size here, otherwise the save will fail
2060
      _AdjustCandidatePool(self, [])
2061

    
2062
    self.cfg.Update(self.cluster, feedback_fn)
2063

    
2064

    
2065
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2066
  """Distribute additional files which are part of the cluster configuration.
2067

2068
  ConfigWriter takes care of distributing the config and ssconf files, but
2069
  there are more files which should be distributed to all nodes. This function
2070
  makes sure those are copied.
2071

2072
  @param lu: calling logical unit
2073
  @param additional_nodes: list of nodes not in the config to distribute to
2074

2075
  """
2076
  # 1. Gather target nodes
2077
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2078
  dist_nodes = lu.cfg.GetNodeList()
2079
  if additional_nodes is not None:
2080
    dist_nodes.extend(additional_nodes)
2081
  if myself.name in dist_nodes:
2082
    dist_nodes.remove(myself.name)
2083

    
2084
  # 2. Gather files to distribute
2085
  dist_files = set([constants.ETC_HOSTS,
2086
                    constants.SSH_KNOWN_HOSTS_FILE,
2087
                    constants.RAPI_CERT_FILE,
2088
                    constants.RAPI_USERS_FILE,
2089
                    constants.HMAC_CLUSTER_KEY,
2090
                   ])
2091

    
2092
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2093
  for hv_name in enabled_hypervisors:
2094
    hv_class = hypervisor.GetHypervisor(hv_name)
2095
    dist_files.update(hv_class.GetAncillaryFiles())
2096

    
2097
  # 3. Perform the files upload
2098
  for fname in dist_files:
2099
    if os.path.exists(fname):
2100
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2101
      for to_node, to_result in result.items():
2102
        msg = to_result.fail_msg
2103
        if msg:
2104
          msg = ("Copy of file %s to node %s failed: %s" %
2105
                 (fname, to_node, msg))
2106
          lu.proc.LogWarning(msg)
2107

    
2108

    
2109
class LURedistributeConfig(NoHooksLU):
2110
  """Force the redistribution of cluster configuration.
2111

2112
  This is a very simple LU.
2113

2114
  """
2115
  _OP_REQP = []
2116
  REQ_BGL = False
2117

    
2118
  def ExpandNames(self):
2119
    self.needed_locks = {
2120
      locking.LEVEL_NODE: locking.ALL_SET,
2121
    }
2122
    self.share_locks[locking.LEVEL_NODE] = 1
2123

    
2124
  def CheckPrereq(self):
2125
    """Check prerequisites.
2126

2127
    """
2128

    
2129
  def Exec(self, feedback_fn):
2130
    """Redistribute the configuration.
2131

2132
    """
2133
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2134
    _RedistributeAncillaryFiles(self)
2135

    
2136

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

2140
  """
2141
  if not instance.disks:
2142
    return True
2143

    
2144
  if not oneshot:
2145
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2146

    
2147
  node = instance.primary_node
2148

    
2149
  for dev in instance.disks:
2150
    lu.cfg.SetDiskID(dev, node)
2151

    
2152
  # TODO: Convert to utils.Retry
2153

    
2154
  retries = 0
2155
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2156
  while True:
2157
    max_time = 0
2158
    done = True
2159
    cumul_degraded = False
2160
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
2161
    msg = rstats.fail_msg
2162
    if msg:
2163
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2164
      retries += 1
2165
      if retries >= 10:
2166
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2167
                                 " aborting." % node)
2168
      time.sleep(6)
2169
      continue
2170
    rstats = rstats.payload
2171
    retries = 0
2172
    for i, mstat in enumerate(rstats):
2173
      if mstat is None:
2174
        lu.LogWarning("Can't compute data for node %s/%s",
2175
                           node, instance.disks[i].iv_name)
2176
        continue
2177

    
2178
      cumul_degraded = (cumul_degraded or
2179
                        (mstat.is_degraded and mstat.sync_percent is None))
2180
      if mstat.sync_percent is not None:
2181
        done = False
2182
        if mstat.estimated_time is not None:
2183
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
2184
          max_time = mstat.estimated_time
2185
        else:
2186
          rem_time = "no time estimate"
2187
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2188
                        (instance.disks[i].iv_name, mstat.sync_percent,
2189
                         rem_time))
2190

    
2191
    # if we're done but degraded, let's do a few small retries, to
2192
    # make sure we see a stable and not transient situation; therefore
2193
    # we force restart of the loop
2194
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2195
      logging.info("Degraded disks found, %d retries left", degr_retries)
2196
      degr_retries -= 1
2197
      time.sleep(1)
2198
      continue
2199

    
2200
    if done or oneshot:
2201
      break
2202

    
2203
    time.sleep(min(60, max_time))
2204

    
2205
  if done:
2206
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2207
  return not cumul_degraded
2208

    
2209

    
2210
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2211
  """Check that mirrors are not degraded.
2212

2213
  The ldisk parameter, if True, will change the test from the
2214
  is_degraded attribute (which represents overall non-ok status for
2215
  the device(s)) to the ldisk (representing the local storage status).
2216

2217
  """
2218
  lu.cfg.SetDiskID(dev, node)
2219

    
2220
  result = True
2221

    
2222
  if on_primary or dev.AssembleOnSecondary():
2223
    rstats = lu.rpc.call_blockdev_find(node, dev)
2224
    msg = rstats.fail_msg
2225
    if msg:
2226
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2227
      result = False
2228
    elif not rstats.payload:
2229
      lu.LogWarning("Can't find disk on node %s", node)
2230
      result = False
2231
    else:
2232
      if ldisk:
2233
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2234
      else:
2235
        result = result and not rstats.payload.is_degraded
2236

    
2237
  if dev.children:
2238
    for child in dev.children:
2239
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2240

    
2241
  return result
2242

    
2243

    
2244
class LUDiagnoseOS(NoHooksLU):
2245
  """Logical unit for OS diagnose/query.
2246

2247
  """
2248
  _OP_REQP = ["output_fields", "names"]
2249
  REQ_BGL = False
2250
  _FIELDS_STATIC = utils.FieldSet()
2251
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants")
2252
  # Fields that need calculation of global os validity
2253
  _FIELDS_NEEDVALID = frozenset(["valid", "variants"])
2254

    
2255
  def ExpandNames(self):
2256
    if self.op.names:
2257
      raise errors.OpPrereqError("Selective OS query not supported",
2258
                                 errors.ECODE_INVAL)
2259

    
2260
    _CheckOutputFields(static=self._FIELDS_STATIC,
2261
                       dynamic=self._FIELDS_DYNAMIC,
2262
                       selected=self.op.output_fields)
2263

    
2264
    # Lock all nodes, in shared mode
2265
    # Temporary removal of locks, should be reverted later
2266
    # TODO: reintroduce locks when they are lighter-weight
2267
    self.needed_locks = {}
2268
    #self.share_locks[locking.LEVEL_NODE] = 1
2269
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2270

    
2271
  def CheckPrereq(self):
2272
    """Check prerequisites.
2273

2274
    """
2275

    
2276
  @staticmethod
2277
  def _DiagnoseByOS(node_list, rlist):
2278
    """Remaps a per-node return list into an a per-os per-node dictionary
2279

2280
    @param node_list: a list with the names of all nodes
2281
    @param rlist: a map with node names as keys and OS objects as values
2282

2283
    @rtype: dict
2284
    @return: a dictionary with osnames as keys and as value another map, with
2285
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2286

2287
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2288
                                     (/srv/..., False, "invalid api")],
2289
                           "node2": [(/srv/..., True, "")]}
2290
          }
2291

2292
    """
2293
    all_os = {}
2294
    # we build here the list of nodes that didn't fail the RPC (at RPC
2295
    # level), so that nodes with a non-responding node daemon don't
2296
    # make all OSes invalid
2297
    good_nodes = [node_name for node_name in rlist
2298
                  if not rlist[node_name].fail_msg]
2299
    for node_name, nr in rlist.items():
2300
      if nr.fail_msg or not nr.payload:
2301
        continue
2302
      for name, path, status, diagnose, variants in nr.payload:
2303
        if name not in all_os:
2304
          # build a list of nodes for this os containing empty lists
2305
          # for each node in node_list
2306
          all_os[name] = {}
2307
          for nname in good_nodes:
2308
            all_os[name][nname] = []
2309
        all_os[name][node_name].append((path, status, diagnose, variants))
2310
    return all_os
2311

    
2312
  def Exec(self, feedback_fn):
2313
    """Compute the list of OSes.
2314

2315
    """
2316
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2317
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2318
    pol = self._DiagnoseByOS(valid_nodes, node_data)
2319
    output = []
2320
    calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields)
2321
    calc_variants = "variants" in self.op.output_fields
2322

    
2323
    for os_name, os_data in pol.items():
2324
      row = []
2325
      if calc_valid:
2326
        valid = True
2327
        variants = None
2328
        for osl in os_data.values():
2329
          valid = valid and osl and osl[0][1]
2330
          if not valid:
2331
            variants = None
2332
            break
2333
          if calc_variants:
2334
            node_variants = osl[0][3]
2335
            if variants is None:
2336
              variants = node_variants
2337
            else:
2338
              variants = [v for v in variants if v in node_variants]
2339

    
2340
      for field in self.op.output_fields:
2341
        if field == "name":
2342
          val = os_name
2343
        elif field == "valid":
2344
          val = valid
2345
        elif field == "node_status":
2346
          # this is just a copy of the dict
2347
          val = {}
2348
          for node_name, nos_list in os_data.items():
2349
            val[node_name] = nos_list
2350
        elif field == "variants":
2351
          val =  variants
2352
        else:
2353
          raise errors.ParameterError(field)
2354
        row.append(val)
2355
      output.append(row)
2356

    
2357
    return output
2358

    
2359

    
2360
class LURemoveNode(LogicalUnit):
2361
  """Logical unit for removing a node.
2362

2363
  """
2364
  HPATH = "node-remove"
2365
  HTYPE = constants.HTYPE_NODE
2366
  _OP_REQP = ["node_name"]
2367

    
2368
  def BuildHooksEnv(self):
2369
    """Build hooks env.
2370

2371
    This doesn't run on the target node in the pre phase as a failed
2372
    node would then be impossible to remove.
2373

2374
    """
2375
    env = {
2376
      "OP_TARGET": self.op.node_name,
2377
      "NODE_NAME": self.op.node_name,
2378
      }
2379
    all_nodes = self.cfg.GetNodeList()
2380
    if self.op.node_name in all_nodes:
2381
      all_nodes.remove(self.op.node_name)
2382
    return env, all_nodes, all_nodes
2383

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

2387
    This checks:
2388
     - the node exists in the configuration
2389
     - it does not have primary or secondary instances
2390
     - it's not the master
2391

2392
    Any errors are signaled by raising errors.OpPrereqError.
2393

2394
    """
2395
    node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
2396
    if node is None:
2397
      raise errors.OpPrereqError("Node '%s' is unknown." % self.op.node_name,
2398
                                 errors.ECODE_NOENT)
2399

    
2400
    instance_list = self.cfg.GetInstanceList()
2401

    
2402
    masternode = self.cfg.GetMasterNode()
2403
    if node.name == masternode:
2404
      raise errors.OpPrereqError("Node is the master node,"
2405
                                 " you need to failover first.",
2406
                                 errors.ECODE_INVAL)
2407

    
2408
    for instance_name in instance_list:
2409
      instance = self.cfg.GetInstanceInfo(instance_name)
2410
      if node.name in instance.all_nodes:
2411
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2412
                                   " please remove first." % instance_name,
2413
                                   errors.ECODE_INVAL)
2414
    self.op.node_name = node.name
2415
    self.node = node
2416

    
2417
  def Exec(self, feedback_fn):
2418
    """Removes the node from the cluster.
2419

2420
    """
2421
    node = self.node
2422
    logging.info("Stopping the node daemon and removing configs from node %s",
2423
                 node.name)
2424

    
2425
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
2426

    
2427
    # Promote nodes to master candidate as needed
2428
    _AdjustCandidatePool(self, exceptions=[node.name])
2429
    self.context.RemoveNode(node.name)
2430

    
2431
    # Run post hooks on the node before it's removed
2432
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
2433
    try:
2434
      h_results = hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
2435
    except:
2436
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
2437

    
2438
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
2439
    msg = result.fail_msg
2440
    if msg:
2441
      self.LogWarning("Errors encountered on the remote node while leaving"
2442
                      " the cluster: %s", msg)
2443

    
2444

    
2445
class LUQueryNodes(NoHooksLU):
2446
  """Logical unit for querying nodes.
2447

2448
  """
2449
  _OP_REQP = ["output_fields", "names", "use_locking"]
2450
  REQ_BGL = False
2451

    
2452
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
2453
                    "master_candidate", "offline", "drained"]
2454

    
2455
  _FIELDS_DYNAMIC = utils.FieldSet(
2456
    "dtotal", "dfree",
2457
    "mtotal", "mnode", "mfree",
2458
    "bootid",
2459
    "ctotal", "cnodes", "csockets",
2460
    )
2461

    
2462
  _FIELDS_STATIC = utils.FieldSet(*[
2463
    "pinst_cnt", "sinst_cnt",
2464
    "pinst_list", "sinst_list",
2465
    "pip", "sip", "tags",
2466
    "master",
2467
    "role"] + _SIMPLE_FIELDS
2468
    )
2469

    
2470
  def ExpandNames(self):
2471
    _CheckOutputFields(static=self._FIELDS_STATIC,
2472
                       dynamic=self._FIELDS_DYNAMIC,
2473
                       selected=self.op.output_fields)
2474

    
2475
    self.needed_locks = {}
2476
    self.share_locks[locking.LEVEL_NODE] = 1
2477

    
2478
    if self.op.names:
2479
      self.wanted = _GetWantedNodes(self, self.op.names)
2480
    else:
2481
      self.wanted = locking.ALL_SET
2482

    
2483
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2484
    self.do_locking = self.do_node_query and self.op.use_locking
2485
    if self.do_locking:
2486
      # if we don't request only static fields, we need to lock the nodes
2487
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2488

    
2489
  def CheckPrereq(self):
2490
    """Check prerequisites.
2491

2492
    """
2493
    # The validation of the node list is done in the _GetWantedNodes,
2494
    # if non empty, and if empty, there's no validation to do
2495
    pass
2496

    
2497
  def Exec(self, feedback_fn):
2498
    """Computes the list of nodes and their attributes.
2499

2500
    """
2501
    all_info = self.cfg.GetAllNodesInfo()
2502
    if self.do_locking:
2503
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2504
    elif self.wanted != locking.ALL_SET:
2505
      nodenames = self.wanted
2506
      missing = set(nodenames).difference(all_info.keys())
2507
      if missing:
2508
        raise errors.OpExecError(
2509
          "Some nodes were removed before retrieving their data: %s" % missing)
2510
    else:
2511
      nodenames = all_info.keys()
2512

    
2513
    nodenames = utils.NiceSort(nodenames)
2514
    nodelist = [all_info[name] for name in nodenames]
2515

    
2516
    # begin data gathering
2517

    
2518
    if self.do_node_query:
2519
      live_data = {}
2520
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2521
                                          self.cfg.GetHypervisorType())
2522
      for name in nodenames:
2523
        nodeinfo = node_data[name]
2524
        if not nodeinfo.fail_msg and nodeinfo.payload:
2525
          nodeinfo = nodeinfo.payload
2526
          fn = utils.TryConvert
2527
          live_data[name] = {
2528
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2529
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2530
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2531
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2532
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2533
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2534
            "bootid": nodeinfo.get('bootid', None),
2535
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2536
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2537
            }
2538
        else:
2539
          live_data[name] = {}
2540
    else:
2541
      live_data = dict.fromkeys(nodenames, {})
2542

    
2543
    node_to_primary = dict([(name, set()) for name in nodenames])
2544
    node_to_secondary = dict([(name, set()) for name in nodenames])
2545

    
2546
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2547
                             "sinst_cnt", "sinst_list"))
2548
    if inst_fields & frozenset(self.op.output_fields):
2549
      instancelist = self.cfg.GetInstanceList()
2550

    
2551
      for instance_name in instancelist:
2552
        inst = self.cfg.GetInstanceInfo(instance_name)
2553
        if inst.primary_node in node_to_primary:
2554
          node_to_primary[inst.primary_node].add(inst.name)
2555
        for secnode in inst.secondary_nodes:
2556
          if secnode in node_to_secondary:
2557
            node_to_secondary[secnode].add(inst.name)
2558

    
2559
    master_node = self.cfg.GetMasterNode()
2560

    
2561
    # end data gathering
2562

    
2563
    output = []
2564
    for node in nodelist:
2565
      node_output = []
2566
      for field in self.op.output_fields:
2567
        if field in self._SIMPLE_FIELDS:
2568
          val = getattr(node, field)
2569
        elif field == "pinst_list":
2570
          val = list(node_to_primary[node.name])
2571
        elif field == "sinst_list":
2572
          val = list(node_to_secondary[node.name])
2573
        elif field == "pinst_cnt":
2574
          val = len(node_to_primary[node.name])
2575
        elif field == "sinst_cnt":
2576
          val = len(node_to_secondary[node.name])
2577
        elif field == "pip":
2578
          val = node.primary_ip
2579
        elif field == "sip":
2580
          val = node.secondary_ip
2581
        elif field == "tags":
2582
          val = list(node.GetTags())
2583
        elif field == "master":
2584
          val = node.name == master_node
2585
        elif self._FIELDS_DYNAMIC.Matches(field):
2586
          val = live_data[node.name].get(field, None)
2587
        elif field == "role":
2588
          if node.name == master_node:
2589
            val = "M"
2590
          elif node.master_candidate:
2591
            val = "C"
2592
          elif node.drained:
2593
            val = "D"
2594
          elif node.offline:
2595
            val = "O"
2596
          else:
2597
            val = "R"
2598
        else:
2599
          raise errors.ParameterError(field)
2600
        node_output.append(val)
2601
      output.append(node_output)
2602

    
2603
    return output
2604

    
2605

    
2606
class LUQueryNodeVolumes(NoHooksLU):
2607
  """Logical unit for getting volumes on node(s).
2608

2609
  """
2610
  _OP_REQP = ["nodes", "output_fields"]
2611
  REQ_BGL = False
2612
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2613
  _FIELDS_STATIC = utils.FieldSet("node")
2614

    
2615
  def ExpandNames(self):
2616
    _CheckOutputFields(static=self._FIELDS_STATIC,
2617
                       dynamic=self._FIELDS_DYNAMIC,
2618
                       selected=self.op.output_fields)
2619

    
2620
    self.needed_locks = {}
2621
    self.share_locks[locking.LEVEL_NODE] = 1
2622
    if not self.op.nodes:
2623
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2624
    else:
2625
      self.needed_locks[locking.LEVEL_NODE] = \
2626
        _GetWantedNodes(self, self.op.nodes)
2627

    
2628
  def CheckPrereq(self):
2629
    """Check prerequisites.
2630

2631
    This checks that the fields required are valid output fields.
2632

2633
    """
2634
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2635

    
2636
  def Exec(self, feedback_fn):
2637
    """Computes the list of nodes and their attributes.
2638

2639
    """
2640
    nodenames = self.nodes
2641
    volumes = self.rpc.call_node_volumes(nodenames)
2642

    
2643
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
2644
             in self.cfg.GetInstanceList()]
2645

    
2646
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
2647

    
2648
    output = []
2649
    for node in nodenames:
2650
      nresult = volumes[node]
2651
      if nresult.offline:
2652
        continue
2653
      msg = nresult.fail_msg
2654
      if msg:
2655
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
2656
        continue
2657

    
2658
      node_vols = nresult.payload[:]
2659
      node_vols.sort(key=lambda vol: vol['dev'])
2660

    
2661
      for vol in node_vols:
2662
        node_output = []
2663
        for field in self.op.output_fields:
2664
          if field == "node":
2665
            val = node
2666
          elif field == "phys":
2667
            val = vol['dev']
2668
          elif field == "vg":
2669
            val = vol['vg']
2670
          elif field == "name":
2671
            val = vol['name']
2672
          elif field == "size":
2673
            val = int(float(vol['size']))
2674
          elif field == "instance":
2675
            for inst in ilist:
2676
              if node not in lv_by_node[inst]:
2677
                continue
2678
              if vol['name'] in lv_by_node[inst][node]:
2679
                val = inst.name
2680
                break
2681
            else:
2682
              val = '-'
2683
          else:
2684
            raise errors.ParameterError(field)
2685
          node_output.append(str(val))
2686

    
2687
        output.append(node_output)
2688

    
2689
    return output
2690

    
2691

    
2692
class LUQueryNodeStorage(NoHooksLU):
2693
  """Logical unit for getting information on storage units on node(s).
2694

2695
  """
2696
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
2697
  REQ_BGL = False
2698
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
2699

    
2700
  def ExpandNames(self):
2701
    storage_type = self.op.storage_type
2702

    
2703
    if storage_type not in constants.VALID_STORAGE_TYPES:
2704
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2705
                                 errors.ECODE_INVAL)
2706

    
2707
    _CheckOutputFields(static=self._FIELDS_STATIC,
2708
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
2709
                       selected=self.op.output_fields)
2710

    
2711
    self.needed_locks = {}
2712
    self.share_locks[locking.LEVEL_NODE] = 1
2713

    
2714
    if self.op.nodes:
2715
      self.needed_locks[locking.LEVEL_NODE] = \
2716
        _GetWantedNodes(self, self.op.nodes)
2717
    else:
2718
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2719

    
2720
  def CheckPrereq(self):
2721
    """Check prerequisites.
2722

2723
    This checks that the fields required are valid output fields.
2724

2725
    """
2726
    self.op.name = getattr(self.op, "name", None)
2727

    
2728
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2729

    
2730
  def Exec(self, feedback_fn):
2731
    """Computes the list of nodes and their attributes.
2732

2733
    """
2734
    # Always get name to sort by
2735
    if constants.SF_NAME in self.op.output_fields:
2736
      fields = self.op.output_fields[:]
2737
    else:
2738
      fields = [constants.SF_NAME] + self.op.output_fields
2739

    
2740
    # Never ask for node or type as it's only known to the LU
2741
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
2742
      while extra in fields:
2743
        fields.remove(extra)
2744

    
2745
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
2746
    name_idx = field_idx[constants.SF_NAME]
2747

    
2748
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2749
    data = self.rpc.call_storage_list(self.nodes,
2750
                                      self.op.storage_type, st_args,
2751
                                      self.op.name, fields)
2752

    
2753
    result = []
2754

    
2755
    for node in utils.NiceSort(self.nodes):
2756
      nresult = data[node]
2757
      if nresult.offline:
2758
        continue
2759

    
2760
      msg = nresult.fail_msg
2761
      if msg:
2762
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
2763
        continue
2764

    
2765
      rows = dict([(row[name_idx], row) for row in nresult.payload])
2766

    
2767
      for name in utils.NiceSort(rows.keys()):
2768
        row = rows[name]
2769

    
2770
        out = []
2771

    
2772
        for field in self.op.output_fields:
2773
          if field == constants.SF_NODE:
2774
            val = node
2775
          elif field == constants.SF_TYPE:
2776
            val = self.op.storage_type
2777
          elif field in field_idx:
2778
            val = row[field_idx[field]]
2779
          else:
2780
            raise errors.ParameterError(field)
2781

    
2782
          out.append(val)
2783

    
2784
        result.append(out)
2785

    
2786
    return result
2787

    
2788

    
2789
class LUModifyNodeStorage(NoHooksLU):
2790
  """Logical unit for modifying a storage volume on a node.
2791

2792
  """
2793
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
2794
  REQ_BGL = False
2795

    
2796
  def CheckArguments(self):
2797
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2798
    if node_name is None:
2799
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
2800
                                 errors.ECODE_NOENT)
2801

    
2802
    self.op.node_name = node_name
2803

    
2804
    storage_type = self.op.storage_type
2805
    if storage_type not in constants.VALID_STORAGE_TYPES:
2806
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2807
                                 errors.ECODE_INVAL)
2808

    
2809
  def ExpandNames(self):
2810
    self.needed_locks = {
2811
      locking.LEVEL_NODE: self.op.node_name,
2812
      }
2813

    
2814
  def CheckPrereq(self):
2815
    """Check prerequisites.
2816

2817
    """
2818
    storage_type = self.op.storage_type
2819

    
2820
    try:
2821
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
2822
    except KeyError:
2823
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
2824
                                 " modified" % storage_type,
2825
                                 errors.ECODE_INVAL)
2826

    
2827
    diff = set(self.op.changes.keys()) - modifiable
2828
    if diff:
2829
      raise errors.OpPrereqError("The following fields can not be modified for"
2830
                                 " storage units of type '%s': %r" %
2831
                                 (storage_type, list(diff)),
2832
                                 errors.ECODE_INVAL)
2833

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

2837
    """
2838
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2839
    result = self.rpc.call_storage_modify(self.op.node_name,
2840
                                          self.op.storage_type, st_args,
2841
                                          self.op.name, self.op.changes)
2842
    result.Raise("Failed to modify storage unit '%s' on %s" %
2843
                 (self.op.name, self.op.node_name))
2844

    
2845

    
2846
class LUAddNode(LogicalUnit):
2847
  """Logical unit for adding node to the cluster.
2848

2849
  """
2850
  HPATH = "node-add"
2851
  HTYPE = constants.HTYPE_NODE
2852
  _OP_REQP = ["node_name"]
2853

    
2854
  def BuildHooksEnv(self):
2855
    """Build hooks env.
2856

2857
    This will run on all nodes before, and on all nodes + the new node after.
2858

2859
    """
2860
    env = {
2861
      "OP_TARGET": self.op.node_name,
2862
      "NODE_NAME": self.op.node_name,
2863
      "NODE_PIP": self.op.primary_ip,
2864
      "NODE_SIP": self.op.secondary_ip,
2865
      }
2866
    nodes_0 = self.cfg.GetNodeList()
2867
    nodes_1 = nodes_0 + [self.op.node_name, ]
2868
    return env, nodes_0, nodes_1
2869

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

2873
    This checks:
2874
     - the new node is not already in the config
2875
     - it is resolvable
2876
     - its parameters (single/dual homed) matches the cluster
2877

2878
    Any errors are signaled by raising errors.OpPrereqError.
2879

2880
    """
2881
    node_name = self.op.node_name
2882
    cfg = self.cfg
2883

    
2884
    dns_data = utils.GetHostInfo(node_name)
2885

    
2886
    node = dns_data.name
2887
    primary_ip = self.op.primary_ip = dns_data.ip
2888
    secondary_ip = getattr(self.op, "secondary_ip", None)
2889
    if secondary_ip is None:
2890
      secondary_ip = primary_ip
2891
    if not utils.IsValidIP(secondary_ip):
2892
      raise errors.OpPrereqError("Invalid secondary IP given",
2893
                                 errors.ECODE_INVAL)
2894
    self.op.secondary_ip = secondary_ip
2895

    
2896
    node_list = cfg.GetNodeList()
2897
    if not self.op.readd and node in node_list:
2898
      raise errors.OpPrereqError("Node %s is already in the configuration" %
2899
                                 node, errors.ECODE_EXISTS)
2900
    elif self.op.readd and node not in node_list:
2901
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
2902
                                 errors.ECODE_NOENT)
2903

    
2904
    for existing_node_name in node_list:
2905
      existing_node = cfg.GetNodeInfo(existing_node_name)
2906

    
2907
      if self.op.readd and node == existing_node_name:
2908
        if (existing_node.primary_ip != primary_ip or
2909
            existing_node.secondary_ip != secondary_ip):
2910
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
2911
                                     " address configuration as before",
2912
                                     errors.ECODE_INVAL)
2913
        continue
2914

    
2915
      if (existing_node.primary_ip == primary_ip or
2916
          existing_node.secondary_ip == primary_ip or
2917
          existing_node.primary_ip == secondary_ip or
2918
          existing_node.secondary_ip == secondary_ip):
2919
        raise errors.OpPrereqError("New node ip address(es) conflict with"
2920
                                   " existing node %s" % existing_node.name,
2921
                                   errors.ECODE_NOTUNIQUE)
2922

    
2923
    # check that the type of the node (single versus dual homed) is the
2924
    # same as for the master
2925
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
2926
    master_singlehomed = myself.secondary_ip == myself.primary_ip
2927
    newbie_singlehomed = secondary_ip == primary_ip
2928
    if master_singlehomed != newbie_singlehomed:
2929
      if master_singlehomed:
2930
        raise errors.OpPrereqError("The master has no private ip but the"
2931
                                   " new node has one",
2932
                                   errors.ECODE_INVAL)
2933
      else:
2934
        raise errors.OpPrereqError("The master has a private ip but the"
2935
                                   " new node doesn't have one",
2936
                                   errors.ECODE_INVAL)
2937

    
2938
    # checks reachability
2939
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
2940
      raise errors.OpPrereqError("Node not reachable by ping",
2941
                                 errors.ECODE_ENVIRON)
2942

    
2943
    if not newbie_singlehomed:
2944
      # check reachability from my secondary ip to newbie's secondary ip
2945
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
2946
                           source=myself.secondary_ip):
2947
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
2948
                                   " based ping to noded port",
2949
                                   errors.ECODE_ENVIRON)
2950

    
2951
    if self.op.readd:
2952
      exceptions = [node]
2953
    else:
2954
      exceptions = []
2955

    
2956
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
2957

    
2958
    if self.op.readd:
2959
      self.new_node = self.cfg.GetNodeInfo(node)
2960
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
2961
    else:
2962
      self.new_node = objects.Node(name=node,
2963
                                   primary_ip=primary_ip,
2964
                                   secondary_ip=secondary_ip,
2965
                                   master_candidate=self.master_candidate,
2966
                                   offline=False, drained=False)
2967

    
2968
  def Exec(self, feedback_fn):
2969
    """Adds the new node to the cluster.
2970

2971
    """
2972
    new_node = self.new_node
2973
    node = new_node.name
2974

    
2975
    # for re-adds, reset the offline/drained/master-candidate flags;
2976
    # we need to reset here, otherwise offline would prevent RPC calls
2977
    # later in the procedure; this also means that if the re-add
2978
    # fails, we are left with a non-offlined, broken node
2979
    if self.op.readd:
2980
      new_node.drained = new_node.offline = False
2981
      self.LogInfo("Readding a node, the offline/drained flags were reset")
2982
      # if we demote the node, we do cleanup later in the procedure
2983
      new_node.master_candidate = self.master_candidate
2984

    
2985
    # notify the user about any possible mc promotion
2986
    if new_node.master_candidate:
2987
      self.LogInfo("Node will be a master candidate")
2988

    
2989
    # check connectivity
2990
    result = self.rpc.call_version([node])[node]
2991
    result.Raise("Can't get version information from node %s" % node)
2992
    if constants.PROTOCOL_VERSION == result.payload:
2993
      logging.info("Communication to node %s fine, sw version %s match",
2994
                   node, result.payload)
2995
    else:
2996
      raise errors.OpExecError("Version mismatch master version %s,"
2997
                               " node version %s" %
2998
                               (constants.PROTOCOL_VERSION, result.payload))
2999

    
3000
    # setup ssh on node
3001
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3002
      logging.info("Copy ssh key to node %s", node)
3003
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3004
      keyarray = []
3005
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3006
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3007
                  priv_key, pub_key]
3008

    
3009
      for i in keyfiles:
3010
        keyarray.append(utils.ReadFile(i))
3011

    
3012
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3013
                                      keyarray[2], keyarray[3], keyarray[4],
3014
                                      keyarray[5])
3015
      result.Raise("Cannot transfer ssh keys to the new node")
3016

    
3017
    # Add node to our /etc/hosts, and add key to known_hosts
3018
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3019
      utils.AddHostToEtcHosts(new_node.name)
3020

    
3021
    if new_node.secondary_ip != new_node.primary_ip:
3022
      result = self.rpc.call_node_has_ip_address(new_node.name,
3023
                                                 new_node.secondary_ip)
3024
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3025
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3026
      if not result.payload:
3027
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3028
                                 " you gave (%s). Please fix and re-run this"
3029
                                 " command." % new_node.secondary_ip)
3030

    
3031
    node_verify_list = [self.cfg.GetMasterNode()]
3032
    node_verify_param = {
3033
      constants.NV_NODELIST: [node],
3034
      # TODO: do a node-net-test as well?
3035
    }
3036

    
3037
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3038
                                       self.cfg.GetClusterName())
3039
    for verifier in node_verify_list:
3040
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3041
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3042
      if nl_payload:
3043
        for failed in nl_payload:
3044
          feedback_fn("ssh/hostname verification failed"
3045
                      " (checking from %s): %s" %
3046
                      (verifier, nl_payload[failed]))
3047
        raise errors.OpExecError("ssh/hostname verification failed.")
3048

    
3049
    if self.op.readd:
3050
      _RedistributeAncillaryFiles(self)
3051
      self.context.ReaddNode(new_node)
3052
      # make sure we redistribute the config
3053
      self.cfg.Update(new_node, feedback_fn)
3054
      # and make sure the new node will not have old files around
3055
      if not new_node.master_candidate:
3056
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3057
        msg = result.fail_msg
3058
        if msg:
3059
          self.LogWarning("Node failed to demote itself from master"
3060
                          " candidate status: %s" % msg)
3061
    else:
3062
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3063
      self.context.AddNode(new_node, self.proc.GetECId())
3064

    
3065

    
3066
class LUSetNodeParams(LogicalUnit):
3067
  """Modifies the parameters of a node.
3068

3069
  """
3070
  HPATH = "node-modify"
3071
  HTYPE = constants.HTYPE_NODE
3072
  _OP_REQP = ["node_name"]
3073
  REQ_BGL = False
3074

    
3075
  def CheckArguments(self):
3076
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
3077
    if node_name is None:
3078
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
3079
                                 errors.ECODE_INVAL)
3080
    self.op.node_name = node_name
3081
    _CheckBooleanOpField(self.op, 'master_candidate')
3082
    _CheckBooleanOpField(self.op, 'offline')
3083
    _CheckBooleanOpField(self.op, 'drained')
3084
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3085
    if all_mods.count(None) == 3:
3086
      raise errors.OpPrereqError("Please pass at least one modification",
3087
                                 errors.ECODE_INVAL)
3088
    if all_mods.count(True) > 1:
3089
      raise errors.OpPrereqError("Can't set the node into more than one"
3090
                                 " state at the same time",
3091
                                 errors.ECODE_INVAL)
3092

    
3093
  def ExpandNames(self):
3094
    self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3095

    
3096
  def BuildHooksEnv(self):
3097
    """Build hooks env.
3098

3099
    This runs on the master node.
3100

3101
    """
3102
    env = {
3103
      "OP_TARGET": self.op.node_name,
3104
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3105
      "OFFLINE": str(self.op.offline),
3106
      "DRAINED": str(self.op.drained),
3107
      }
3108
    nl = [self.cfg.GetMasterNode(),
3109
          self.op.node_name]
3110
    return env, nl, nl
3111

    
3112
  def CheckPrereq(self):
3113
    """Check prerequisites.
3114

3115
    This only checks the instance list against the existing names.
3116

3117
    """
3118
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3119

    
3120
    if (self.op.master_candidate is not None or
3121
        self.op.drained is not None or
3122
        self.op.offline is not None):
3123
      # we can't change the master's node flags
3124
      if self.op.node_name == self.cfg.GetMasterNode():
3125
        raise errors.OpPrereqError("The master role can be changed"
3126
                                   " only via masterfailover",
3127
                                   errors.ECODE_INVAL)
3128

    
3129
    # Boolean value that tells us whether we're offlining or draining the node
3130
    offline_or_drain = self.op.offline == True or self.op.drained == True
3131
    deoffline_or_drain = self.op.offline == False or self.op.drained == False
3132

    
3133
    if (node.master_candidate and
3134
        (self.op.master_candidate == False or offline_or_drain)):
3135
      cp_size = self.cfg.GetClusterInfo().candidate_pool_size
3136
      mc_now, mc_should, mc_max = self.cfg.GetMasterCandidateStats()
3137
      if mc_now <= cp_size:
3138
        msg = ("Not enough master candidates (desired"
3139
               " %d, new value will be %d)" % (cp_size, mc_now-1))
3140
        # Only allow forcing the operation if it's an offline/drain operation,
3141
        # and we could not possibly promote more nodes.
3142
        # FIXME: this can still lead to issues if in any way another node which
3143
        # could be promoted appears in the meantime.
3144
        if self.op.force and offline_or_drain and mc_should == mc_max:
3145
          self.LogWarning(msg)
3146
        else:
3147
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
3148

    
3149
    if (self.op.master_candidate == True and
3150
        ((node.offline and not self.op.offline == False) or
3151
         (node.drained and not self.op.drained == False))):
3152
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3153
                                 " to master_candidate" % node.name,
3154
                                 errors.ECODE_INVAL)
3155

    
3156
    # If we're being deofflined/drained, we'll MC ourself if needed
3157
    if (deoffline_or_drain and not offline_or_drain and not
3158
        self.op.master_candidate == True):
3159
      self.op.master_candidate = _DecideSelfPromotion(self)
3160
      if self.op.master_candidate:
3161
        self.LogInfo("Autopromoting node to master candidate")
3162

    
3163
    return
3164

    
3165
  def Exec(self, feedback_fn):
3166
    """Modifies a node.
3167

3168
    """
3169
    node = self.node
3170

    
3171
    result = []
3172
    changed_mc = False
3173

    
3174
    if self.op.offline is not None:
3175
      node.offline = self.op.offline
3176
      result.append(("offline", str(self.op.offline)))
3177
      if self.op.offline == True:
3178
        if node.master_candidate:
3179
          node.master_candidate = False
3180
          changed_mc = True
3181
          result.append(("master_candidate", "auto-demotion due to offline"))
3182
        if node.drained:
3183
          node.drained = False
3184
          result.append(("drained", "clear drained status due to offline"))
3185

    
3186
    if self.op.master_candidate is not None:
3187
      node.master_candidate = self.op.master_candidate
3188
      changed_mc = True
3189
      result.append(("master_candidate", str(self.op.master_candidate)))
3190
      if self.op.master_candidate == False:
3191
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3192
        msg = rrc.fail_msg
3193
        if msg:
3194
          self.LogWarning("Node failed to demote itself: %s" % msg)
3195

    
3196
    if self.op.drained is not None:
3197
      node.drained = self.op.drained
3198
      result.append(("drained", str(self.op.drained)))
3199
      if self.op.drained == True:
3200
        if node.master_candidate:
3201
          node.master_candidate = False
3202
          changed_mc = True
3203
          result.append(("master_candidate", "auto-demotion due to drain"))
3204
          rrc = self.rpc.call_node_demote_from_mc(node.name)
3205
          msg = rrc.fail_msg
3206
          if msg:
3207
            self.LogWarning("Node failed to demote itself: %s" % msg)
3208
        if node.offline:
3209
          node.offline = False
3210
          result.append(("offline", "clear offline status due to drain"))
3211

    
3212
    # this will trigger configuration file update, if needed
3213
    self.cfg.Update(node, feedback_fn)
3214
    # this will trigger job queue propagation or cleanup
3215
    if changed_mc:
3216
      self.context.ReaddNode(node)
3217

    
3218
    return result
3219

    
3220

    
3221
class LUPowercycleNode(NoHooksLU):
3222
  """Powercycles a node.
3223

3224
  """
3225
  _OP_REQP = ["node_name", "force"]
3226
  REQ_BGL = False
3227

    
3228
  def CheckArguments(self):
3229
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
3230
    if node_name is None:
3231
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name,
3232
                                 errors.ECODE_NOENT)
3233
    self.op.node_name = node_name
3234
    if node_name == self.cfg.GetMasterNode() and not self.op.force:
3235
      raise errors.OpPrereqError("The node is the master and the force"
3236
                                 " parameter was not set",
3237
                                 errors.ECODE_INVAL)
3238

    
3239
  def ExpandNames(self):
3240
    """Locking for PowercycleNode.
3241

3242
    This is a last-resort option and shouldn't block on other
3243
    jobs. Therefore, we grab no locks.
3244

3245
    """
3246
    self.needed_locks = {}
3247

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

3251
    This LU has no prereqs.
3252

3253
    """
3254
    pass
3255

    
3256
  def Exec(self, feedback_fn):
3257
    """Reboots a node.
3258

3259
    """
3260
    result = self.rpc.call_node_powercycle(self.op.node_name,
3261
                                           self.cfg.GetHypervisorType())
3262
    result.Raise("Failed to schedule the reboot")
3263
    return result.payload
3264

    
3265

    
3266
class LUQueryClusterInfo(NoHooksLU):
3267
  """Query cluster configuration.
3268

3269
  """
3270
  _OP_REQP = []
3271
  REQ_BGL = False
3272

    
3273
  def ExpandNames(self):
3274
    self.needed_locks = {}
3275

    
3276
  def CheckPrereq(self):
3277
    """No prerequsites needed for this LU.
3278

3279
    """
3280
    pass
3281

    
3282
  def Exec(self, feedback_fn):
3283
    """Return cluster config.
3284

3285
    """
3286
    cluster = self.cfg.GetClusterInfo()
3287
    result = {
3288
      "software_version": constants.RELEASE_VERSION,
3289
      "protocol_version": constants.PROTOCOL_VERSION,
3290
      "config_version": constants.CONFIG_VERSION,
3291
      "os_api_version": max(constants.OS_API_VERSIONS),
3292
      "export_version": constants.EXPORT_VERSION,
3293
      "architecture": (platform.architecture()[0], platform.machine()),
3294
      "name": cluster.cluster_name,
3295
      "master": cluster.master_node,
3296
      "default_hypervisor": cluster.enabled_hypervisors[0],
3297
      "enabled_hypervisors": cluster.enabled_hypervisors,
3298
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3299
                        for hypervisor_name in cluster.enabled_hypervisors]),
3300
      "beparams": cluster.beparams,
3301
      "nicparams": cluster.nicparams,
3302
      "candidate_pool_size": cluster.candidate_pool_size,
3303
      "master_netdev": cluster.master_netdev,
3304
      "volume_group_name": cluster.volume_group_name,
3305
      "file_storage_dir": cluster.file_storage_dir,
3306
      "ctime": cluster.ctime,
3307
      "mtime": cluster.mtime,
3308
      "uuid": cluster.uuid,
3309
      "tags": list(cluster.GetTags()),
3310
      }
3311

    
3312
    return result
3313

    
3314

    
3315
class LUQueryConfigValues(NoHooksLU):
3316
  """Return configuration values.
3317

3318
  """
3319
  _OP_REQP = []
3320
  REQ_BGL = False
3321
  _FIELDS_DYNAMIC = utils.FieldSet()
3322
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
3323
                                  "watcher_pause")
3324

    
3325
  def ExpandNames(self):
3326
    self.needed_locks = {}
3327

    
3328
    _CheckOutputFields(static=self._FIELDS_STATIC,
3329
                       dynamic=self._FIELDS_DYNAMIC,
3330
                       selected=self.op.output_fields)
3331

    
3332
  def CheckPrereq(self):
3333
    """No prerequisites.
3334

3335
    """
3336
    pass
3337

    
3338
  def Exec(self, feedback_fn):
3339
    """Dump a representation of the cluster config to the standard output.
3340

3341
    """
3342
    values = []
3343
    for field in self.op.output_fields:
3344
      if field == "cluster_name":
3345
        entry = self.cfg.GetClusterName()
3346
      elif field == "master_node":
3347
        entry = self.cfg.GetMasterNode()
3348
      elif field == "drain_flag":
3349
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3350
      elif field == "watcher_pause":
3351
        return utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
3352
      else:
3353
        raise errors.ParameterError(field)
3354
      values.append(entry)
3355
    return values
3356

    
3357

    
3358
class LUActivateInstanceDisks(NoHooksLU):
3359
  """Bring up an instance's disks.
3360

3361
  """
3362
  _OP_REQP = ["instance_name"]
3363
  REQ_BGL = False
3364

    
3365
  def ExpandNames(self):
3366
    self._ExpandAndLockInstance()
3367
    self.needed_locks[locking.LEVEL_NODE] = []
3368
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3369

    
3370
  def DeclareLocks(self, level):
3371
    if level == locking.LEVEL_NODE:
3372
      self._LockInstancesNodes()
3373

    
3374
  def CheckPrereq(self):
3375
    """Check prerequisites.
3376

3377
    This checks that the instance is in the cluster.
3378

3379
    """
3380
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3381
    assert self.instance is not None, \
3382
      "Cannot retrieve locked instance %s" % self.op.instance_name
3383
    _CheckNodeOnline(self, self.instance.primary_node)
3384
    if not hasattr(self.op, "ignore_size"):
3385
      self.op.ignore_size = False
3386

    
3387
  def Exec(self, feedback_fn):
3388
    """Activate the disks.
3389

3390
    """
3391
    disks_ok, disks_info = \
3392
              _AssembleInstanceDisks(self, self.instance,
3393
                                     ignore_size=self.op.ignore_size)
3394
    if not disks_ok:
3395
      raise errors.OpExecError("Cannot activate block devices")
3396

    
3397
    return disks_info
3398

    
3399

    
3400
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3401
                           ignore_size=False):
3402
  """Prepare the block devices for an instance.
3403

3404
  This sets up the block devices on all nodes.
3405

3406
  @type lu: L{LogicalUnit}
3407
  @param lu: the logical unit on whose behalf we execute
3408
  @type instance: L{objects.Instance}
3409
  @param instance: the instance for whose disks we assemble
3410
  @type ignore_secondaries: boolean
3411
  @param ignore_secondaries: if true, errors on secondary nodes
3412
      won't result in an error return from the function
3413
  @type ignore_size: boolean
3414
  @param ignore_size: if true, the current known size of the disk
3415
      will not be used during the disk activation, useful for cases
3416
      when the size is wrong
3417
  @return: False if the operation failed, otherwise a list of
3418
      (host, instance_visible_name, node_visible_name)
3419
      with the mapping from node devices to instance devices
3420

3421
  """
3422
  device_info = []
3423
  disks_ok = True
3424
  iname = instance.name
3425
  # With the two passes mechanism we try to reduce the window of
3426
  # opportunity for the race condition of switching DRBD to primary
3427
  # before handshaking occured, but we do not eliminate it
3428

    
3429
  # The proper fix would be to wait (with some limits) until the
3430
  # connection has been made and drbd transitions from WFConnection
3431
  # into any other network-connected state (Connected, SyncTarget,
3432
  # SyncSource, etc.)
3433

    
3434
  # 1st pass, assemble on all nodes in secondary mode
3435
  for inst_disk in instance.disks:
3436
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3437
      if ignore_size:
3438
        node_disk = node_disk.Copy()
3439
        node_disk.UnsetSize()
3440
      lu.cfg.SetDiskID(node_disk, node)
3441
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3442
      msg = result.fail_msg
3443
      if msg:
3444
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3445
                           " (is_primary=False, pass=1): %s",
3446
                           inst_disk.iv_name, node, msg)
3447
        if not ignore_secondaries:
3448
          disks_ok = False
3449

    
3450
  # FIXME: race condition on drbd migration to primary
3451

    
3452
  # 2nd pass, do only the primary node
3453
  for inst_disk in instance.disks:
3454
    dev_path = None
3455

    
3456
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3457
      if node != instance.primary_node:
3458
        continue
3459
      if ignore_size:
3460
        node_disk = node_disk.Copy()
3461
        node_disk.UnsetSize()
3462
      lu.cfg.SetDiskID(node_disk, node)
3463
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3464
      msg = result.fail_msg
3465
      if msg:
3466
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3467
                           " (is_primary=True, pass=2): %s",
3468
                           inst_disk.iv_name, node, msg)
3469
        disks_ok = False
3470
      else:
3471
        dev_path = result.payload
3472

    
3473
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
3474

    
3475
  # leave the disks configured for the primary node
3476
  # this is a workaround that would be fixed better by
3477
  # improving the logical/physical id handling
3478
  for disk in instance.disks:
3479
    lu.cfg.SetDiskID(disk, instance.primary_node)
3480

    
3481
  return disks_ok, device_info
3482

    
3483

    
3484
def _StartInstanceDisks(lu, instance, force):
3485
  """Start the disks of an instance.
3486

3487
  """
3488
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3489
                                           ignore_secondaries=force)
3490
  if not disks_ok:
3491
    _ShutdownInstanceDisks(lu, instance)
3492
    if force is not None and not force:
3493
      lu.proc.LogWarning("", hint="If the message above refers to a"
3494
                         " secondary node,"
3495
                         " you can retry the operation using '--force'.")
3496
    raise errors.OpExecError("Disk consistency error")
3497

    
3498

    
3499
class LUDeactivateInstanceDisks(NoHooksLU):
3500
  """Shutdown an instance's disks.
3501

3502
  """
3503
  _OP_REQP = ["instance_name"]
3504
  REQ_BGL = False
3505

    
3506
  def ExpandNames(self):
3507
    self._ExpandAndLockInstance()
3508
    self.needed_locks[locking.LEVEL_NODE] = []
3509
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3510

    
3511
  def DeclareLocks(self, level):
3512
    if level == locking.LEVEL_NODE:
3513
      self._LockInstancesNodes()
3514

    
3515
  def CheckPrereq(self):
3516
    """Check prerequisites.
3517

3518
    This checks that the instance is in the cluster.
3519

3520
    """
3521
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3522
    assert self.instance is not None, \
3523
      "Cannot retrieve locked instance %s" % self.op.instance_name
3524

    
3525
  def Exec(self, feedback_fn):
3526
    """Deactivate the disks
3527

3528
    """
3529
    instance = self.instance
3530
    _SafeShutdownInstanceDisks(self, instance)
3531

    
3532

    
3533
def _SafeShutdownInstanceDisks(lu, instance):
3534
  """Shutdown block devices of an instance.
3535

3536
  This function checks if an instance is running, before calling
3537
  _ShutdownInstanceDisks.
3538

3539
  """
3540
  pnode = instance.primary_node
3541
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
3542
  ins_l.Raise("Can't contact node %s" % pnode)
3543

    
3544
  if instance.name in ins_l.payload:
3545
    raise errors.OpExecError("Instance is running, can't shutdown"
3546
                             " block devices.")
3547

    
3548
  _ShutdownInstanceDisks(lu, instance)
3549

    
3550

    
3551
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3552
  """Shutdown block devices of an instance.
3553

3554
  This does the shutdown on all nodes of the instance.
3555

3556
  If the ignore_primary is false, errors on the primary node are
3557
  ignored.
3558

3559
  """
3560
  all_result = True
3561
  for disk in instance.disks:
3562
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
3563
      lu.cfg.SetDiskID(top_disk, node)
3564
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
3565
      msg = result.fail_msg
3566
      if msg:
3567
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
3568
                      disk.iv_name, node, msg)
3569
        if not ignore_primary or node != instance.primary_node:
3570
          all_result = False
3571
  return all_result
3572

    
3573

    
3574
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
3575
  """Checks if a node has enough free memory.
3576

3577
  This function check if a given node has the needed amount of free
3578
  memory. In case the node has less memory or we cannot get the
3579
  information from the node, this function raise an OpPrereqError
3580
  exception.
3581

3582
  @type lu: C{LogicalUnit}
3583
  @param lu: a logical unit from which we get configuration data
3584
  @type node: C{str}
3585
  @param node: the node to check
3586
  @type reason: C{str}
3587
  @param reason: string to use in the error message
3588
  @type requested: C{int}
3589
  @param requested: the amount of memory in MiB to check for
3590
  @type hypervisor_name: C{str}
3591
  @param hypervisor_name: the hypervisor to ask for memory stats
3592
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
3593
      we cannot check the node
3594

3595
  """
3596
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
3597
  nodeinfo[node].Raise("Can't get data from node %s" % node,
3598
                       prereq=True, ecode=errors.ECODE_ENVIRON)
3599
  free_mem = nodeinfo[node].payload.get('memory_free', None)
3600
  if not isinstance(free_mem, int):
3601
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
3602
                               " was '%s'" % (node, free_mem),
3603
                               errors.ECODE_ENVIRON)
3604
  if requested > free_mem:
3605
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
3606
                               " needed %s MiB, available %s MiB" %
3607
                               (node, reason, requested, free_mem),
3608
                               errors.ECODE_NORES)
3609

    
3610

    
3611
class LUStartupInstance(LogicalUnit):
3612
  """Starts an instance.
3613

3614
  """
3615
  HPATH = "instance-start"
3616
  HTYPE = constants.HTYPE_INSTANCE
3617
  _OP_REQP = ["instance_name", "force"]
3618
  REQ_BGL = False
3619

    
3620
  def ExpandNames(self):
3621
    self._ExpandAndLockInstance()
3622

    
3623
  def BuildHooksEnv(self):
3624
    """Build hooks env.
3625

3626
    This runs on master, primary and secondary nodes of the instance.
3627

3628
    """
3629
    env = {
3630
      "FORCE": self.op.force,
3631
      }
3632
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3633
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3634
    return env, nl, nl
3635

    
3636
  def CheckPrereq(self):
3637
    """Check prerequisites.
3638

3639
    This checks that the instance is in the cluster.
3640

3641
    """
3642
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3643
    assert self.instance is not None, \
3644
      "Cannot retrieve locked instance %s" % self.op.instance_name
3645

    
3646
    # extra beparams
3647
    self.beparams = getattr(self.op, "beparams", {})
3648
    if self.beparams:
3649
      if not isinstance(self.beparams, dict):
3650
        raise errors.OpPrereqError("Invalid beparams passed: %s, expected"
3651
                                   " dict" % (type(self.beparams), ),
3652
                                   errors.ECODE_INVAL)
3653
      # fill the beparams dict
3654
      utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
3655
      self.op.beparams = self.beparams
3656

    
3657
    # extra hvparams
3658
    self.hvparams = getattr(self.op, "hvparams", {})
3659
    if self.hvparams:
3660
      if not isinstance(self.hvparams, dict):
3661
        raise errors.OpPrereqError("Invalid hvparams passed: %s, expected"
3662
                                   " dict" % (type(self.hvparams), ),
3663
                                   errors.ECODE_INVAL)
3664

    
3665
      # check hypervisor parameter syntax (locally)
3666
      cluster = self.cfg.GetClusterInfo()
3667
      utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
3668
      filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor],
3669
                                    instance.hvparams)
3670
      filled_hvp.update(self.hvparams)
3671
      hv_type = hypervisor.GetHypervisor(instance.hypervisor)
3672
      hv_type.CheckParameterSyntax(filled_hvp)
3673
      _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
3674
      self.op.hvparams = self.hvparams
3675

    
3676
    _CheckNodeOnline(self, instance.primary_node)
3677

    
3678
    bep = self.cfg.GetClusterInfo().FillBE(instance)
3679
    # check bridges existence
3680
    _CheckInstanceBridgesExist(self, instance)
3681

    
3682
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3683
                                              instance.name,
3684
                                              instance.hypervisor)
3685
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3686
                      prereq=True, ecode=errors.ECODE_ENVIRON)
3687
    if not remote_info.payload: # not running already
3688
      _CheckNodeFreeMemory(self, instance.primary_node,
3689
                           "starting instance %s" % instance.name,
3690
                           bep[constants.BE_MEMORY], instance.hypervisor)
3691

    
3692
  def Exec(self, feedback_fn):
3693
    """Start the instance.
3694

3695
    """
3696
    instance = self.instance
3697
    force = self.op.force
3698

    
3699
    self.cfg.MarkInstanceUp(instance.name)
3700

    
3701
    node_current = instance.primary_node
3702

    
3703
    _StartInstanceDisks(self, instance, force)
3704

    
3705
    result = self.rpc.call_instance_start(node_current, instance,
3706
                                          self.hvparams, self.beparams)
3707
    msg = result.fail_msg
3708
    if msg:
3709
      _ShutdownInstanceDisks(self, instance)
3710
      raise errors.OpExecError("Could not start instance: %s" % msg)
3711

    
3712

    
3713
class LURebootInstance(LogicalUnit):
3714
  """Reboot an instance.
3715

3716
  """
3717
  HPATH = "instance-reboot"
3718
  HTYPE = constants.HTYPE_INSTANCE
3719
  _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
3720
  REQ_BGL = False
3721

    
3722
  def CheckArguments(self):
3723
    """Check the arguments.
3724

3725
    """
3726
    self.shutdown_timeout = getattr(self.op, "shutdown_timeout",
3727
                                    constants.DEFAULT_SHUTDOWN_TIMEOUT)
3728

    
3729
  def ExpandNames(self):
3730
    if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
3731
                                   constants.INSTANCE_REBOOT_HARD,
3732
                                   constants.INSTANCE_REBOOT_FULL]:
3733
      raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
3734
                                  (constants.INSTANCE_REBOOT_SOFT,
3735
                                   constants.INSTANCE_REBOOT_HARD,
3736
                                   constants.INSTANCE_REBOOT_FULL))
3737
    self._ExpandAndLockInstance()
3738

    
3739
  def BuildHooksEnv(self):
3740
    """Build hooks env.
3741

3742
    This runs on master, primary and secondary nodes of the instance.
3743

3744
    """
3745
    env = {
3746
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
3747
      "REBOOT_TYPE": self.op.reboot_type,
3748
      "SHUTDOWN_TIMEOUT": self.shutdown_timeout,
3749
      }
3750
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3751
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3752
    return env, nl, nl
3753

    
3754
  def CheckPrereq(self):
3755
    """Check prerequisites.
3756

3757
    This checks that the instance is in the cluster.
3758

3759
    """
3760
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3761
    assert self.instance is not None, \
3762
      "Cannot retrieve locked instance %s" % self.op.instance_name
3763

    
3764
    _CheckNodeOnline(self, instance.primary_node)
3765

    
3766
    # check bridges existence
3767
    _CheckInstanceBridgesExist(self, instance)
3768

    
3769
  def Exec(self, feedback_fn):
3770
    """Reboot the instance.
3771

3772
    """
3773
    instance = self.instance
3774
    ignore_secondaries = self.op.ignore_secondaries
3775
    reboot_type = self.op.reboot_type
3776

    
3777
    node_current = instance.primary_node
3778

    
3779
    if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
3780
                       constants.INSTANCE_REBOOT_HARD]:
3781
      for disk in instance.disks:
3782
        self.cfg.SetDiskID(disk, node_current)
3783
      result = self.rpc.call_instance_reboot(node_current, instance,
3784
                                             reboot_type,
3785
                                             self.shutdown_timeout)
3786
      result.Raise("Could not reboot instance")
3787
    else:
3788
      result = self.rpc.call_instance_shutdown(node_current, instance,
3789
                                               self.shutdown_timeout)
3790
      result.Raise("Could not shutdown instance for full reboot")
3791
      _ShutdownInstanceDisks(self, instance)
3792
      _StartInstanceDisks(self, instance, ignore_secondaries)
3793
      result = self.rpc.call_instance_start(node_current, instance, None, None)
3794
      msg = result.fail_msg
3795
      if msg:
3796
        _ShutdownInstanceDisks(self, instance)
3797
        raise errors.OpExecError("Could not start instance for"
3798
                                 " full reboot: %s" % msg)
3799

    
3800
    self.cfg.MarkInstanceUp(instance.name)
3801

    
3802

    
3803
class LUShutdownInstance(LogicalUnit):
3804
  """Shutdown an instance.
3805

3806
  """
3807
  HPATH = "instance-stop"
3808
  HTYPE = constants.HTYPE_INSTANCE
3809
  _OP_REQP = ["instance_name"]
3810
  REQ_BGL = False
3811

    
3812
  def CheckArguments(self):
3813
    """Check the arguments.
3814

3815
    """
3816
    self.timeout = getattr(self.op, "timeout",
3817
                           constants.DEFAULT_SHUTDOWN_TIMEOUT)
3818

    
3819
  def ExpandNames(self):
3820
    self._ExpandAndLockInstance()
3821

    
3822
  def BuildHooksEnv(self):
3823
    """Build hooks env.
3824

3825
    This runs on master, primary and secondary nodes of the instance.
3826

3827
    """
3828
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3829
    env["TIMEOUT"] = self.timeout
3830
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3831
    return env, nl, nl
3832

    
3833
  def CheckPrereq(self):
3834
    """Check prerequisites.
3835

3836
    This checks that the instance is in the cluster.
3837

3838
    """
3839
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3840
    assert self.instance is not None, \
3841
      "Cannot retrieve locked instance %s" % self.op.instance_name
3842
    _CheckNodeOnline(self, self.instance.primary_node)
3843

    
3844
  def Exec(self, feedback_fn):
3845
    """Shutdown the instance.
3846

3847
    """
3848
    instance = self.instance
3849
    node_current = instance.primary_node
3850
    timeout = self.timeout
3851
    self.cfg.MarkInstanceDown(instance.name)
3852
    result = self.rpc.call_instance_shutdown(node_current, instance, timeout)
3853
    msg = result.fail_msg
3854
    if msg:
3855
      self.proc.LogWarning("Could not shutdown instance: %s" % msg)
3856

    
3857
    _ShutdownInstanceDisks(self, instance)
3858

    
3859

    
3860
class LUReinstallInstance(LogicalUnit):
3861
  """Reinstall an instance.
3862

3863
  """
3864
  HPATH = "instance-reinstall"
3865
  HTYPE = constants.HTYPE_INSTANCE
3866
  _OP_REQP = ["instance_name"]
3867
  REQ_BGL = False
3868

    
3869
  def ExpandNames(self):
3870
    self._ExpandAndLockInstance()
3871

    
3872
  def BuildHooksEnv(self):
3873
    """Build hooks env.
3874

3875
    This runs on master, primary and secondary nodes of the instance.
3876

3877
    """
3878
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3879
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3880
    return env, nl, nl
3881

    
3882
  def CheckPrereq(self):
3883
    """Check prerequisites.
3884

3885
    This checks that the instance is in the cluster and is not running.
3886

3887
    """
3888
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3889
    assert instance is not None, \
3890
      "Cannot retrieve locked instance %s" % self.op.instance_name
3891
    _CheckNodeOnline(self, instance.primary_node)
3892

    
3893
    if instance.disk_template == constants.DT_DISKLESS:
3894
      raise errors.OpPrereqError("Instance '%s' has no disks" %
3895
                                 self.op.instance_name,
3896
                                 errors.ECODE_INVAL)
3897
    if instance.admin_up:
3898
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3899
                                 self.op.instance_name,
3900
                                 errors.ECODE_STATE)
3901
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3902
                                              instance.name,
3903
                                              instance.hypervisor)
3904
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3905
                      prereq=True, ecode=errors.ECODE_ENVIRON)
3906
    if remote_info.payload:
3907
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3908
                                 (self.op.instance_name,
3909
                                  instance.primary_node),
3910
                                 errors.ECODE_STATE)
3911

    
3912
    self.op.os_type = getattr(self.op, "os_type", None)
3913
    self.op.force_variant = getattr(self.op, "force_variant", False)
3914
    if self.op.os_type is not None:
3915
      # OS verification
3916
      pnode = self.cfg.GetNodeInfo(
3917
        self.cfg.ExpandNodeName(instance.primary_node))
3918
      if pnode is None:
3919
        raise errors.OpPrereqError("Primary node '%s' is unknown" %
3920
                                   self.op.pnode, errors.ECODE_NOENT)
3921
      result = self.rpc.call_os_get(pnode.name, self.op.os_type)
3922
      result.Raise("OS '%s' not in supported OS list for primary node %s" %
3923
                   (self.op.os_type, pnode.name),
3924
                   prereq=True, ecode=errors.ECODE_INVAL)
3925
      if not self.op.force_variant:
3926
        _CheckOSVariant(result.payload, self.op.os_type)
3927

    
3928
    self.instance = instance
3929

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

3933
    """
3934
    inst = self.instance
3935

    
3936
    if self.op.os_type is not None:
3937
      feedback_fn("Changing OS to '%s'..." % self.op.os_type)
3938
      inst.os = self.op.os_type
3939
      self.cfg.Update(inst, feedback_fn)
3940

    
3941
    _StartInstanceDisks(self, inst, None)
3942
    try:
3943
      feedback_fn("Running the instance OS create scripts...")
3944
      result = self.rpc.call_instance_os_add(inst.primary_node, inst, True)
3945
      result.Raise("Could not install OS for instance %s on node %s" %
3946
                   (inst.name, inst.primary_node))
3947
    finally:
3948
      _ShutdownInstanceDisks(self, inst)
3949

    
3950

    
3951
class LURecreateInstanceDisks(LogicalUnit):
3952
  """Recreate an instance's missing disks.
3953

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

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

3963
    """
3964
    if not isinstance(self.op.disks, list):
3965
      raise errors.OpPrereqError("Invalid disks parameter", errors.ECODE_INVAL)
3966
    for item in self.op.disks:
3967
      if (not isinstance(item, int) or
3968
          item < 0):
3969
        raise errors.OpPrereqError("Invalid disk specification '%s'" %
3970
                                   str(item), errors.ECODE_INVAL)
3971

    
3972
  def ExpandNames(self):
3973
    self._ExpandAndLockInstance()
3974

    
3975
  def BuildHooksEnv(self):
3976
    """Build hooks env.
3977

3978
    This runs on master, primary and secondary nodes of the instance.
3979

3980
    """
3981
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3982
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3983
    return env, nl, nl
3984

    
3985
  def CheckPrereq(self):
3986
    """Check prerequisites.
3987

3988
    This checks that the instance is in the cluster and is not running.
3989

3990
    """
3991
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3992
    assert instance is not None, \
3993
      "Cannot retrieve locked instance %s" % self.op.instance_name
3994
    _CheckNodeOnline(self, instance.primary_node)
3995

    
3996
    if instance.disk_template == constants.DT_DISKLESS:
3997
      raise errors.OpPrereqError("Instance '%s' has no disks" %
3998
                                 self.op.instance_name, errors.ECODE_INVAL)
3999
    if instance.admin_up:
4000
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
4001
                                 self.op.instance_name, errors.ECODE_STATE)
4002
    remote_info = self.rpc.call_instance_info(instance.primary_node,