Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ c3589cf8

History | View | Annotate | Download (322.1 kB)

1
#
2
#
3

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

    
21

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

    
24
# pylint: disable-msg=W0201
25

    
26
# W0201 since most LU attributes are defined in CheckPrereq or similar
27
# functions
28

    
29
import os
30
import os.path
31
import time
32
import re
33
import platform
34
import logging
35
import copy
36
import OpenSSL
37

    
38
from ganeti import ssh
39
from ganeti import utils
40
from ganeti import errors
41
from ganeti import hypervisor
42
from ganeti import locking
43
from ganeti import constants
44
from ganeti import objects
45
from ganeti import serializer
46
from ganeti import ssconf
47

    
48

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

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

61
  Note that all commands require root permissions.
62

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

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

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

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

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

    
104
    # Tasklets
105
    self.tasklets = None
106

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

    
113
    self.CheckArguments()
114

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

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

    
123
  ssh = property(fget=__GetSSH)
124

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

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

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

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

140
    """
141
    pass
142

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

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

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

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

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

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

168
    Examples::
169

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

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

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

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

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

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

207
    """
208

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

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

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

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

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

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

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

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

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

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

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

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

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

266
    """
267
    raise NotImplementedError
268

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
355
    del self.recalculate_locks[locking.LEVEL_NODE]
356

    
357

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

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

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

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

371
    This just raises an error.
372

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

    
376

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

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

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

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

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

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

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

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

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

409
    """
410
    raise NotImplementedError
411

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

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

419
    """
420
    raise NotImplementedError
421

    
422

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

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

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

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

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

    
446

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

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

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

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

    
470

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

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

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

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

    
489

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

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

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

    
503

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

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

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

    
518

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

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

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

    
531

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

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

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

    
544

    
545
def _ExpandItemName(fn, name, kind):
546
  """Expand an item name.
547

548
  @param fn: the function to use for expansion
549
  @param name: requested item name
550
  @param kind: text description ('Node' or 'Instance')
551
  @return: the resolved (full) name
552
  @raise errors.OpPrereqError: if the item is not found
553

554
  """
555
  full_name = fn(name)
556
  if full_name is None:
557
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
558
                               errors.ECODE_NOENT)
559
  return full_name
560

    
561

    
562
def _ExpandNodeName(cfg, name):
563
  """Wrapper over L{_ExpandItemName} for nodes."""
564
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
565

    
566

    
567
def _ExpandInstanceName(cfg, name):
568
  """Wrapper over L{_ExpandItemName} for instance."""
569
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
570

    
571

    
572
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
573
                          memory, vcpus, nics, disk_template, disks,
574
                          bep, hvp, hypervisor_name):
575
  """Builds instance related env variables for hooks
576

577
  This builds the hook environment from individual variables.
578

579
  @type name: string
580
  @param name: the name of the instance
581
  @type primary_node: string
582
  @param primary_node: the name of the instance's primary node
583
  @type secondary_nodes: list
584
  @param secondary_nodes: list of secondary nodes as strings
585
  @type os_type: string
586
  @param os_type: the name of the instance's OS
587
  @type status: boolean
588
  @param status: the should_run status of the instance
589
  @type memory: string
590
  @param memory: the memory size of the instance
591
  @type vcpus: string
592
  @param vcpus: the count of VCPUs the instance has
593
  @type nics: list
594
  @param nics: list of tuples (ip, mac, mode, link) representing
595
      the NICs the instance has
596
  @type disk_template: string
597
  @param disk_template: the disk template of the instance
598
  @type disks: list
599
  @param disks: the list of (size, mode) pairs
600
  @type bep: dict
601
  @param bep: the backend parameters for the instance
602
  @type hvp: dict
603
  @param hvp: the hypervisor parameters for the instance
604
  @type hypervisor_name: string
605
  @param hypervisor_name: the hypervisor for the instance
606
  @rtype: dict
607
  @return: the hook environment for this instance
608

609
  """
610
  if status:
611
    str_status = "up"
612
  else:
613
    str_status = "down"
614
  env = {
615
    "OP_TARGET": name,
616
    "INSTANCE_NAME": name,
617
    "INSTANCE_PRIMARY": primary_node,
618
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
619
    "INSTANCE_OS_TYPE": os_type,
620
    "INSTANCE_STATUS": str_status,
621
    "INSTANCE_MEMORY": memory,
622
    "INSTANCE_VCPUS": vcpus,
623
    "INSTANCE_DISK_TEMPLATE": disk_template,
624
    "INSTANCE_HYPERVISOR": hypervisor_name,
625
  }
626

    
627
  if nics:
628
    nic_count = len(nics)
629
    for idx, (ip, mac, mode, link) in enumerate(nics):
630
      if ip is None:
631
        ip = ""
632
      env["INSTANCE_NIC%d_IP" % idx] = ip
633
      env["INSTANCE_NIC%d_MAC" % idx] = mac
634
      env["INSTANCE_NIC%d_MODE" % idx] = mode
635
      env["INSTANCE_NIC%d_LINK" % idx] = link
636
      if mode == constants.NIC_MODE_BRIDGED:
637
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
638
  else:
639
    nic_count = 0
640

    
641
  env["INSTANCE_NIC_COUNT"] = nic_count
642

    
643
  if disks:
644
    disk_count = len(disks)
645
    for idx, (size, mode) in enumerate(disks):
646
      env["INSTANCE_DISK%d_SIZE" % idx] = size
647
      env["INSTANCE_DISK%d_MODE" % idx] = mode
648
  else:
649
    disk_count = 0
650

    
651
  env["INSTANCE_DISK_COUNT"] = disk_count
652

    
653
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
654
    for key, value in source.items():
655
      env["INSTANCE_%s_%s" % (kind, key)] = value
656

    
657
  return env
658

    
659

    
660
def _NICListToTuple(lu, nics):
661
  """Build a list of nic information tuples.
662

663
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
664
  value in LUQueryInstanceData.
665

666
  @type lu:  L{LogicalUnit}
667
  @param lu: the logical unit on whose behalf we execute
668
  @type nics: list of L{objects.NIC}
669
  @param nics: list of nics to convert to hooks tuples
670

671
  """
672
  hooks_nics = []
673
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
674
  for nic in nics:
675
    ip = nic.ip
676
    mac = nic.mac
677
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
678
    mode = filled_params[constants.NIC_MODE]
679
    link = filled_params[constants.NIC_LINK]
680
    hooks_nics.append((ip, mac, mode, link))
681
  return hooks_nics
682

    
683

    
684
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
685
  """Builds instance related env variables for hooks from an object.
686

687
  @type lu: L{LogicalUnit}
688
  @param lu: the logical unit on whose behalf we execute
689
  @type instance: L{objects.Instance}
690
  @param instance: the instance for which we should build the
691
      environment
692
  @type override: dict
693
  @param override: dictionary with key/values that will override
694
      our values
695
  @rtype: dict
696
  @return: the hook environment dictionary
697

698
  """
699
  cluster = lu.cfg.GetClusterInfo()
700
  bep = cluster.FillBE(instance)
701
  hvp = cluster.FillHV(instance)
702
  args = {
703
    'name': instance.name,
704
    'primary_node': instance.primary_node,
705
    'secondary_nodes': instance.secondary_nodes,
706
    'os_type': instance.os,
707
    'status': instance.admin_up,
708
    'memory': bep[constants.BE_MEMORY],
709
    'vcpus': bep[constants.BE_VCPUS],
710
    'nics': _NICListToTuple(lu, instance.nics),
711
    'disk_template': instance.disk_template,
712
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
713
    'bep': bep,
714
    'hvp': hvp,
715
    'hypervisor_name': instance.hypervisor,
716
  }
717
  if override:
718
    args.update(override)
719
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
720

    
721

    
722
def _AdjustCandidatePool(lu, exceptions):
723
  """Adjust the candidate pool after node operations.
724

725
  """
726
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
727
  if mod_list:
728
    lu.LogInfo("Promoted nodes to master candidate role: %s",
729
               utils.CommaJoin(node.name for node in mod_list))
730
    for name in mod_list:
731
      lu.context.ReaddNode(name)
732
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
733
  if mc_now > mc_max:
734
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
735
               (mc_now, mc_max))
736

    
737

    
738
def _DecideSelfPromotion(lu, exceptions=None):
739
  """Decide whether I should promote myself as a master candidate.
740

741
  """
742
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
743
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
744
  # the new node will increase mc_max with one, so:
745
  mc_should = min(mc_should + 1, cp_size)
746
  return mc_now < mc_should
747

    
748

    
749
def _CheckNicsBridgesExist(lu, target_nics, target_node,
750
                               profile=constants.PP_DEFAULT):
751
  """Check that the brigdes needed by a list of nics exist.
752

753
  """
754
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
755
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
756
                for nic in target_nics]
757
  brlist = [params[constants.NIC_LINK] for params in paramslist
758
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
759
  if brlist:
760
    result = lu.rpc.call_bridges_exist(target_node, brlist)
761
    result.Raise("Error checking bridges on destination node '%s'" %
762
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
763

    
764

    
765
def _CheckInstanceBridgesExist(lu, instance, node=None):
766
  """Check that the brigdes needed by an instance exist.
767

768
  """
769
  if node is None:
770
    node = instance.primary_node
771
  _CheckNicsBridgesExist(lu, instance.nics, node)
772

    
773

    
774
def _CheckOSVariant(os_obj, name):
775
  """Check whether an OS name conforms to the os variants specification.
776

777
  @type os_obj: L{objects.OS}
778
  @param os_obj: OS object to check
779
  @type name: string
780
  @param name: OS name passed by the user, to check for validity
781

782
  """
783
  if not os_obj.supported_variants:
784
    return
785
  try:
786
    variant = name.split("+", 1)[1]
787
  except IndexError:
788
    raise errors.OpPrereqError("OS name must include a variant",
789
                               errors.ECODE_INVAL)
790

    
791
  if variant not in os_obj.supported_variants:
792
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
793

    
794

    
795
def _GetNodeInstancesInner(cfg, fn):
796
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
797

    
798

    
799
def _GetNodeInstances(cfg, node_name):
800
  """Returns a list of all primary and secondary instances on a node.
801

802
  """
803

    
804
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
805

    
806

    
807
def _GetNodePrimaryInstances(cfg, node_name):
808
  """Returns primary instances on a node.
809

810
  """
811
  return _GetNodeInstancesInner(cfg,
812
                                lambda inst: node_name == inst.primary_node)
813

    
814

    
815
def _GetNodeSecondaryInstances(cfg, node_name):
816
  """Returns secondary instances on a node.
817

818
  """
819
  return _GetNodeInstancesInner(cfg,
820
                                lambda inst: node_name in inst.secondary_nodes)
821

    
822

    
823
def _GetStorageTypeArgs(cfg, storage_type):
824
  """Returns the arguments for a storage type.
825

826
  """
827
  # Special case for file storage
828
  if storage_type == constants.ST_FILE:
829
    # storage.FileStorage wants a list of storage directories
830
    return [[cfg.GetFileStorageDir()]]
831

    
832
  return []
833

    
834

    
835
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
836
  faulty = []
837

    
838
  for dev in instance.disks:
839
    cfg.SetDiskID(dev, node_name)
840

    
841
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
842
  result.Raise("Failed to get disk status from node %s" % node_name,
843
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
844

    
845
  for idx, bdev_status in enumerate(result.payload):
846
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
847
      faulty.append(idx)
848

    
849
  return faulty
850

    
851

    
852
def _FormatTimestamp(secs):
853
  """Formats a Unix timestamp with the local timezone.
854

855
  """
856
  return time.strftime("%F %T %Z", time.gmtime(secs))
857

    
858

    
859
class LUPostInitCluster(LogicalUnit):
860
  """Logical unit for running hooks after cluster initialization.
861

862
  """
863
  HPATH = "cluster-init"
864
  HTYPE = constants.HTYPE_CLUSTER
865
  _OP_REQP = []
866

    
867
  def BuildHooksEnv(self):
868
    """Build hooks env.
869

870
    """
871
    env = {"OP_TARGET": self.cfg.GetClusterName()}
872
    mn = self.cfg.GetMasterNode()
873
    return env, [], [mn]
874

    
875
  def CheckPrereq(self):
876
    """No prerequisites to check.
877

878
    """
879
    return True
880

    
881
  def Exec(self, feedback_fn):
882
    """Nothing to do.
883

884
    """
885
    return True
886

    
887

    
888
class LUDestroyCluster(LogicalUnit):
889
  """Logical unit for destroying the cluster.
890

891
  """
892
  HPATH = "cluster-destroy"
893
  HTYPE = constants.HTYPE_CLUSTER
894
  _OP_REQP = []
895

    
896
  def BuildHooksEnv(self):
897
    """Build hooks env.
898

899
    """
900
    env = {"OP_TARGET": self.cfg.GetClusterName()}
901
    return env, [], []
902

    
903
  def CheckPrereq(self):
904
    """Check prerequisites.
905

906
    This checks whether the cluster is empty.
907

908
    Any errors are signaled by raising errors.OpPrereqError.
909

910
    """
911
    master = self.cfg.GetMasterNode()
912

    
913
    nodelist = self.cfg.GetNodeList()
914
    if len(nodelist) != 1 or nodelist[0] != master:
915
      raise errors.OpPrereqError("There are still %d node(s) in"
916
                                 " this cluster." % (len(nodelist) - 1),
917
                                 errors.ECODE_INVAL)
918
    instancelist = self.cfg.GetInstanceList()
919
    if instancelist:
920
      raise errors.OpPrereqError("There are still %d instance(s) in"
921
                                 " this cluster." % len(instancelist),
922
                                 errors.ECODE_INVAL)
923

    
924
  def Exec(self, feedback_fn):
925
    """Destroys the cluster.
926

927
    """
928
    master = self.cfg.GetMasterNode()
929
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
930

    
931
    # Run post hooks on master node before it's removed
932
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
933
    try:
934
      hm.RunPhase(constants.HOOKS_PHASE_POST, [master])
935
    except:
936
      # pylint: disable-msg=W0702
937
      self.LogWarning("Errors occurred running hooks on %s" % master)
938

    
939
    result = self.rpc.call_node_stop_master(master, False)
940
    result.Raise("Could not disable the master role")
941

    
942
    if modify_ssh_setup:
943
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
944
      utils.CreateBackup(priv_key)
945
      utils.CreateBackup(pub_key)
946

    
947
    return master
948

    
949

    
950
def _VerifyCertificateInner(filename, expired, not_before, not_after, now,
951
                            warn_days=constants.SSL_CERT_EXPIRATION_WARN,
952
                            error_days=constants.SSL_CERT_EXPIRATION_ERROR):
953
  """Verifies certificate details for LUVerifyCluster.
954

955
  """
956
  if expired:
957
    msg = "Certificate %s is expired" % filename
958

    
959
    if not_before is not None and not_after is not None:
960
      msg += (" (valid from %s to %s)" %
961
              (_FormatTimestamp(not_before),
962
               _FormatTimestamp(not_after)))
963
    elif not_before is not None:
964
      msg += " (valid from %s)" % _FormatTimestamp(not_before)
965
    elif not_after is not None:
966
      msg += " (valid until %s)" % _FormatTimestamp(not_after)
967

    
968
    return (LUVerifyCluster.ETYPE_ERROR, msg)
969

    
970
  elif not_before is not None and not_before > now:
971
    return (LUVerifyCluster.ETYPE_WARNING,
972
            "Certificate %s not yet valid (valid from %s)" %
973
            (filename, _FormatTimestamp(not_before)))
974

    
975
  elif not_after is not None:
976
    remaining_days = int((not_after - now) / (24 * 3600))
977

    
978
    msg = ("Certificate %s expires in %d days" % (filename, remaining_days))
979

    
980
    if remaining_days <= error_days:
981
      return (LUVerifyCluster.ETYPE_ERROR, msg)
982

    
983
    if remaining_days <= warn_days:
984
      return (LUVerifyCluster.ETYPE_WARNING, msg)
985

    
986
  return (None, None)
987

    
988

    
989
def _VerifyCertificate(filename):
990
  """Verifies a certificate for LUVerifyCluster.
991

992
  @type filename: string
993
  @param filename: Path to PEM file
994

995
  """
996
  try:
997
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
998
                                           utils.ReadFile(filename))
999
  except Exception, err: # pylint: disable-msg=W0703
1000
    return (LUVerifyCluster.ETYPE_ERROR,
1001
            "Failed to load X509 certificate %s: %s" % (filename, err))
1002

    
1003
  # Depending on the pyOpenSSL version, this can just return (None, None)
1004
  (not_before, not_after) = utils.GetX509CertValidity(cert)
1005

    
1006
  return _VerifyCertificateInner(filename, cert.has_expired(),
1007
                                 not_before, not_after, time.time())
1008

    
1009

    
1010
class LUVerifyCluster(LogicalUnit):
1011
  """Verifies the cluster status.
1012

1013
  """
1014
  HPATH = "cluster-verify"
1015
  HTYPE = constants.HTYPE_CLUSTER
1016
  _OP_REQP = ["skip_checks", "verbose", "error_codes", "debug_simulate_errors"]
1017
  REQ_BGL = False
1018

    
1019
  TCLUSTER = "cluster"
1020
  TNODE = "node"
1021
  TINSTANCE = "instance"
1022

    
1023
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1024
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1025
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1026
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1027
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1028
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1029
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1030
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1031
  ENODEDRBD = (TNODE, "ENODEDRBD")
1032
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1033
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1034
  ENODEHV = (TNODE, "ENODEHV")
1035
  ENODELVM = (TNODE, "ENODELVM")
1036
  ENODEN1 = (TNODE, "ENODEN1")
1037
  ENODENET = (TNODE, "ENODENET")
1038
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1039
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1040
  ENODERPC = (TNODE, "ENODERPC")
1041
  ENODESSH = (TNODE, "ENODESSH")
1042
  ENODEVERSION = (TNODE, "ENODEVERSION")
1043
  ENODESETUP = (TNODE, "ENODESETUP")
1044
  ENODETIME = (TNODE, "ENODETIME")
1045

    
1046
  ETYPE_FIELD = "code"
1047
  ETYPE_ERROR = "ERROR"
1048
  ETYPE_WARNING = "WARNING"
1049

    
1050
  def ExpandNames(self):
1051
    self.needed_locks = {
1052
      locking.LEVEL_NODE: locking.ALL_SET,
1053
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1054
    }
1055
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1056

    
1057
  def _Error(self, ecode, item, msg, *args, **kwargs):
1058
    """Format an error message.
1059

1060
    Based on the opcode's error_codes parameter, either format a
1061
    parseable error code, or a simpler error string.
1062

1063
    This must be called only from Exec and functions called from Exec.
1064

1065
    """
1066
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1067
    itype, etxt = ecode
1068
    # first complete the msg
1069
    if args:
1070
      msg = msg % args
1071
    # then format the whole message
1072
    if self.op.error_codes:
1073
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1074
    else:
1075
      if item:
1076
        item = " " + item
1077
      else:
1078
        item = ""
1079
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1080
    # and finally report it via the feedback_fn
1081
    self._feedback_fn("  - %s" % msg)
1082

    
1083
  def _ErrorIf(self, cond, *args, **kwargs):
1084
    """Log an error message if the passed condition is True.
1085

1086
    """
1087
    cond = bool(cond) or self.op.debug_simulate_errors
1088
    if cond:
1089
      self._Error(*args, **kwargs)
1090
    # do not mark the operation as failed for WARN cases only
1091
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1092
      self.bad = self.bad or cond
1093

    
1094
  def _VerifyNode(self, nodeinfo, file_list, local_cksum,
1095
                  node_result, master_files, drbd_map, vg_name):
1096
    """Run multiple tests against a node.
1097

1098
    Test list:
1099

1100
      - compares ganeti version
1101
      - checks vg existence and size > 20G
1102
      - checks config file checksum
1103
      - checks ssh to other nodes
1104

1105
    @type nodeinfo: L{objects.Node}
1106
    @param nodeinfo: the node to check
1107
    @param file_list: required list of files
1108
    @param local_cksum: dictionary of local files and their checksums
1109
    @param node_result: the results from the node
1110
    @param master_files: list of files that only masters should have
1111
    @param drbd_map: the useddrbd minors for this node, in
1112
        form of minor: (instance, must_exist) which correspond to instances
1113
        and their running status
1114
    @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
1115

1116
    """
1117
    node = nodeinfo.name
1118
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1119

    
1120
    # main result, node_result should be a non-empty dict
1121
    test = not node_result or not isinstance(node_result, dict)
1122
    _ErrorIf(test, self.ENODERPC, node,
1123
                  "unable to verify node: no data returned")
1124
    if test:
1125
      return
1126

    
1127
    # compares ganeti version
1128
    local_version = constants.PROTOCOL_VERSION
1129
    remote_version = node_result.get('version', None)
1130
    test = not (remote_version and
1131
                isinstance(remote_version, (list, tuple)) and
1132
                len(remote_version) == 2)
1133
    _ErrorIf(test, self.ENODERPC, node,
1134
             "connection to node returned invalid data")
1135
    if test:
1136
      return
1137

    
1138
    test = local_version != remote_version[0]
1139
    _ErrorIf(test, self.ENODEVERSION, node,
1140
             "incompatible protocol versions: master %s,"
1141
             " node %s", local_version, remote_version[0])
1142
    if test:
1143
      return
1144

    
1145
    # node seems compatible, we can actually try to look into its results
1146

    
1147
    # full package version
1148
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1149
                  self.ENODEVERSION, node,
1150
                  "software version mismatch: master %s, node %s",
1151
                  constants.RELEASE_VERSION, remote_version[1],
1152
                  code=self.ETYPE_WARNING)
1153

    
1154
    # checks vg existence and size > 20G
1155
    if vg_name is not None:
1156
      vglist = node_result.get(constants.NV_VGLIST, None)
1157
      test = not vglist
1158
      _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1159
      if not test:
1160
        vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1161
                                              constants.MIN_VG_SIZE)
1162
        _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1163

    
1164
    # checks config file checksum
1165

    
1166
    remote_cksum = node_result.get(constants.NV_FILELIST, None)
1167
    test = not isinstance(remote_cksum, dict)
1168
    _ErrorIf(test, self.ENODEFILECHECK, node,
1169
             "node hasn't returned file checksum data")
1170
    if not test:
1171
      for file_name in file_list:
1172
        node_is_mc = nodeinfo.master_candidate
1173
        must_have = (file_name not in master_files) or node_is_mc
1174
        # missing
1175
        test1 = file_name not in remote_cksum
1176
        # invalid checksum
1177
        test2 = not test1 and remote_cksum[file_name] != local_cksum[file_name]
1178
        # existing and good
1179
        test3 = not test1 and remote_cksum[file_name] == local_cksum[file_name]
1180
        _ErrorIf(test1 and must_have, self.ENODEFILECHECK, node,
1181
                 "file '%s' missing", file_name)
1182
        _ErrorIf(test2 and must_have, self.ENODEFILECHECK, node,
1183
                 "file '%s' has wrong checksum", file_name)
1184
        # not candidate and this is not a must-have file
1185
        _ErrorIf(test2 and not must_have, self.ENODEFILECHECK, node,
1186
                 "file '%s' should not exist on non master"
1187
                 " candidates (and the file is outdated)", file_name)
1188
        # all good, except non-master/non-must have combination
1189
        _ErrorIf(test3 and not must_have, self.ENODEFILECHECK, node,
1190
                 "file '%s' should not exist"
1191
                 " on non master candidates", file_name)
1192

    
1193
    # checks ssh to any
1194

    
1195
    test = constants.NV_NODELIST not in node_result
1196
    _ErrorIf(test, self.ENODESSH, node,
1197
             "node hasn't returned node ssh connectivity data")
1198
    if not test:
1199
      if node_result[constants.NV_NODELIST]:
1200
        for a_node, a_msg in node_result[constants.NV_NODELIST].items():
1201
          _ErrorIf(True, self.ENODESSH, node,
1202
                   "ssh communication with node '%s': %s", a_node, a_msg)
1203

    
1204
    test = constants.NV_NODENETTEST not in node_result
1205
    _ErrorIf(test, self.ENODENET, node,
1206
             "node hasn't returned node tcp connectivity data")
1207
    if not test:
1208
      if node_result[constants.NV_NODENETTEST]:
1209
        nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
1210
        for anode in nlist:
1211
          _ErrorIf(True, self.ENODENET, node,
1212
                   "tcp communication with node '%s': %s",
1213
                   anode, node_result[constants.NV_NODENETTEST][anode])
1214

    
1215
    hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
1216
    if isinstance(hyp_result, dict):
1217
      for hv_name, hv_result in hyp_result.iteritems():
1218
        test = hv_result is not None
1219
        _ErrorIf(test, self.ENODEHV, node,
1220
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1221

    
1222
    # check used drbd list
1223
    if vg_name is not None:
1224
      used_minors = node_result.get(constants.NV_DRBDLIST, [])
1225
      test = not isinstance(used_minors, (tuple, list))
1226
      _ErrorIf(test, self.ENODEDRBD, node,
1227
               "cannot parse drbd status file: %s", str(used_minors))
1228
      if not test:
1229
        for minor, (iname, must_exist) in drbd_map.items():
1230
          test = minor not in used_minors and must_exist
1231
          _ErrorIf(test, self.ENODEDRBD, node,
1232
                   "drbd minor %d of instance %s is not active",
1233
                   minor, iname)
1234
        for minor in used_minors:
1235
          test = minor not in drbd_map
1236
          _ErrorIf(test, self.ENODEDRBD, node,
1237
                   "unallocated drbd minor %d is in use", minor)
1238
    test = node_result.get(constants.NV_NODESETUP,
1239
                           ["Missing NODESETUP results"])
1240
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1241
             "; ".join(test))
1242

    
1243
    # check pv names
1244
    if vg_name is not None:
1245
      pvlist = node_result.get(constants.NV_PVLIST, None)
1246
      test = pvlist is None
1247
      _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1248
      if not test:
1249
        # check that ':' is not present in PV names, since it's a
1250
        # special character for lvcreate (denotes the range of PEs to
1251
        # use on the PV)
1252
        for _, pvname, owner_vg in pvlist:
1253
          test = ":" in pvname
1254
          _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1255
                   " '%s' of VG '%s'", pvname, owner_vg)
1256

    
1257
  def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
1258
                      node_instance, n_offline):
1259
    """Verify an instance.
1260

1261
    This function checks to see if the required block devices are
1262
    available on the instance's node.
1263

1264
    """
1265
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1266
    node_current = instanceconfig.primary_node
1267

    
1268
    node_vol_should = {}
1269
    instanceconfig.MapLVsByNode(node_vol_should)
1270

    
1271
    for node in node_vol_should:
1272
      if node in n_offline:
1273
        # ignore missing volumes on offline nodes
1274
        continue
1275
      for volume in node_vol_should[node]:
1276
        test = node not in node_vol_is or volume not in node_vol_is[node]
1277
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1278
                 "volume %s missing on node %s", volume, node)
1279

    
1280
    if instanceconfig.admin_up:
1281
      test = ((node_current not in node_instance or
1282
               not instance in node_instance[node_current]) and
1283
              node_current not in n_offline)
1284
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1285
               "instance not running on its primary node %s",
1286
               node_current)
1287

    
1288
    for node in node_instance:
1289
      if (not node == node_current):
1290
        test = instance in node_instance[node]
1291
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1292
                 "instance should not run on node %s", node)
1293

    
1294
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is):
1295
    """Verify if there are any unknown volumes in the cluster.
1296

1297
    The .os, .swap and backup volumes are ignored. All other volumes are
1298
    reported as unknown.
1299

1300
    """
1301
    for node in node_vol_is:
1302
      for volume in node_vol_is[node]:
1303
        test = (node not in node_vol_should or
1304
                volume not in node_vol_should[node])
1305
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1306
                      "volume %s is unknown", volume)
1307

    
1308
  def _VerifyOrphanInstances(self, instancelist, node_instance):
1309
    """Verify the list of running instances.
1310

1311
    This checks what instances are running but unknown to the cluster.
1312

1313
    """
1314
    for node in node_instance:
1315
      for o_inst in node_instance[node]:
1316
        test = o_inst not in instancelist
1317
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1318
                      "instance %s on node %s should not exist", o_inst, node)
1319

    
1320
  def _VerifyNPlusOneMemory(self, node_info, instance_cfg):
1321
    """Verify N+1 Memory Resilience.
1322

1323
    Check that if one single node dies we can still start all the instances it
1324
    was primary for.
1325

1326
    """
1327
    for node, nodeinfo in node_info.iteritems():
1328
      # This code checks that every node which is now listed as secondary has
1329
      # enough memory to host all instances it is supposed to should a single
1330
      # other node in the cluster fail.
1331
      # FIXME: not ready for failover to an arbitrary node
1332
      # FIXME: does not support file-backed instances
1333
      # WARNING: we currently take into account down instances as well as up
1334
      # ones, considering that even if they're down someone might want to start
1335
      # them even in the event of a node failure.
1336
      for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
1337
        needed_mem = 0
1338
        for instance in instances:
1339
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1340
          if bep[constants.BE_AUTO_BALANCE]:
1341
            needed_mem += bep[constants.BE_MEMORY]
1342
        test = nodeinfo['mfree'] < needed_mem
1343
        self._ErrorIf(test, self.ENODEN1, node,
1344
                      "not enough memory on to accommodate"
1345
                      " failovers should peer node %s fail", prinode)
1346

    
1347
  def CheckPrereq(self):
1348
    """Check prerequisites.
1349

1350
    Transform the list of checks we're going to skip into a set and check that
1351
    all its members are valid.
1352

1353
    """
1354
    self.skip_set = frozenset(self.op.skip_checks)
1355
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1356
      raise errors.OpPrereqError("Invalid checks to be skipped specified",
1357
                                 errors.ECODE_INVAL)
1358

    
1359
  def BuildHooksEnv(self):
1360
    """Build hooks env.
1361

1362
    Cluster-Verify hooks just ran in the post phase and their failure makes
1363
    the output be logged in the verify output and the verification to fail.
1364

1365
    """
1366
    all_nodes = self.cfg.GetNodeList()
1367
    env = {
1368
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1369
      }
1370
    for node in self.cfg.GetAllNodesInfo().values():
1371
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1372

    
1373
    return env, [], all_nodes
1374

    
1375
  def Exec(self, feedback_fn):
1376
    """Verify integrity of cluster, performing various test on nodes.
1377

1378
    """
1379
    self.bad = False
1380
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1381
    verbose = self.op.verbose
1382
    self._feedback_fn = feedback_fn
1383
    feedback_fn("* Verifying global settings")
1384
    for msg in self.cfg.VerifyConfig():
1385
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
1386

    
1387
    # Check the cluster certificates
1388
    for cert_filename in constants.ALL_CERT_FILES:
1389
      (errcode, msg) = _VerifyCertificate(cert_filename)
1390
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
1391

    
1392
    vg_name = self.cfg.GetVGName()
1393
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1394
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1395
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1396
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1397
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1398
                        for iname in instancelist)
1399
    i_non_redundant = [] # Non redundant instances
1400
    i_non_a_balanced = [] # Non auto-balanced instances
1401
    n_offline = [] # List of offline nodes
1402
    n_drained = [] # List of nodes being drained
1403
    node_volume = {}
1404
    node_instance = {}
1405
    node_info = {}
1406
    instance_cfg = {}
1407

    
1408
    # FIXME: verify OS list
1409
    # do local checksums
1410
    master_files = [constants.CLUSTER_CONF_FILE]
1411

    
1412
    file_names = ssconf.SimpleStore().GetFileList()
1413
    file_names.extend(constants.ALL_CERT_FILES)
1414
    file_names.extend(master_files)
1415

    
1416
    local_checksums = utils.FingerprintFiles(file_names)
1417

    
1418
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1419
    node_verify_param = {
1420
      constants.NV_FILELIST: file_names,
1421
      constants.NV_NODELIST: [node.name for node in nodeinfo
1422
                              if not node.offline],
1423
      constants.NV_HYPERVISOR: hypervisors,
1424
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1425
                                  node.secondary_ip) for node in nodeinfo
1426
                                 if not node.offline],
1427
      constants.NV_INSTANCELIST: hypervisors,
1428
      constants.NV_VERSION: None,
1429
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1430
      constants.NV_NODESETUP: None,
1431
      constants.NV_TIME: None,
1432
      }
1433

    
1434
    if vg_name is not None:
1435
      node_verify_param[constants.NV_VGLIST] = None
1436
      node_verify_param[constants.NV_LVLIST] = vg_name
1437
      node_verify_param[constants.NV_PVLIST] = [vg_name]
1438
      node_verify_param[constants.NV_DRBDLIST] = None
1439

    
1440
    # Due to the way our RPC system works, exact response times cannot be
1441
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
1442
    # time before and after executing the request, we can at least have a time
1443
    # window.
1444
    nvinfo_starttime = time.time()
1445
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1446
                                           self.cfg.GetClusterName())
1447
    nvinfo_endtime = time.time()
1448

    
1449
    cluster = self.cfg.GetClusterInfo()
1450
    master_node = self.cfg.GetMasterNode()
1451
    all_drbd_map = self.cfg.ComputeDRBDMap()
1452

    
1453
    feedback_fn("* Verifying node status")
1454
    for node_i in nodeinfo:
1455
      node = node_i.name
1456

    
1457
      if node_i.offline:
1458
        if verbose:
1459
          feedback_fn("* Skipping offline node %s" % (node,))
1460
        n_offline.append(node)
1461
        continue
1462

    
1463
      if node == master_node:
1464
        ntype = "master"
1465
      elif node_i.master_candidate:
1466
        ntype = "master candidate"
1467
      elif node_i.drained:
1468
        ntype = "drained"
1469
        n_drained.append(node)
1470
      else:
1471
        ntype = "regular"
1472
      if verbose:
1473
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1474

    
1475
      msg = all_nvinfo[node].fail_msg
1476
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
1477
      if msg:
1478
        continue
1479

    
1480
      nresult = all_nvinfo[node].payload
1481
      node_drbd = {}
1482
      for minor, instance in all_drbd_map[node].items():
1483
        test = instance not in instanceinfo
1484
        _ErrorIf(test, self.ECLUSTERCFG, None,
1485
                 "ghost instance '%s' in temporary DRBD map", instance)
1486
          # ghost instance should not be running, but otherwise we
1487
          # don't give double warnings (both ghost instance and
1488
          # unallocated minor in use)
1489
        if test:
1490
          node_drbd[minor] = (instance, False)
1491
        else:
1492
          instance = instanceinfo[instance]
1493
          node_drbd[minor] = (instance.name, instance.admin_up)
1494

    
1495
      self._VerifyNode(node_i, file_names, local_checksums,
1496
                       nresult, master_files, node_drbd, vg_name)
1497

    
1498
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1499
      if vg_name is None:
1500
        node_volume[node] = {}
1501
      elif isinstance(lvdata, basestring):
1502
        _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
1503
                 utils.SafeEncode(lvdata))
1504
        node_volume[node] = {}
1505
      elif not isinstance(lvdata, dict):
1506
        _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
1507
        continue
1508
      else:
1509
        node_volume[node] = lvdata
1510

    
1511
      # node_instance
1512
      idata = nresult.get(constants.NV_INSTANCELIST, None)
1513
      test = not isinstance(idata, list)
1514
      _ErrorIf(test, self.ENODEHV, node,
1515
               "rpc call to node failed (instancelist): %s",
1516
               utils.SafeEncode(str(idata)))
1517
      if test:
1518
        continue
1519

    
1520
      node_instance[node] = idata
1521

    
1522
      # node_info
1523
      nodeinfo = nresult.get(constants.NV_HVINFO, None)
1524
      test = not isinstance(nodeinfo, dict)
1525
      _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
1526
      if test:
1527
        continue
1528

    
1529
      # Node time
1530
      ntime = nresult.get(constants.NV_TIME, None)
1531
      try:
1532
        ntime_merged = utils.MergeTime(ntime)
1533
      except (ValueError, TypeError):
1534
        _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1535

    
1536
      if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1537
        ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1538
      elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1539
        ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1540
      else:
1541
        ntime_diff = None
1542

    
1543
      _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1544
               "Node time diverges by at least %s from master node time",
1545
               ntime_diff)
1546

    
1547
      if ntime_diff is not None:
1548
        continue
1549

    
1550
      try:
1551
        node_info[node] = {
1552
          "mfree": int(nodeinfo['memory_free']),
1553
          "pinst": [],
1554
          "sinst": [],
1555
          # dictionary holding all instances this node is secondary for,
1556
          # grouped by their primary node. Each key is a cluster node, and each
1557
          # value is a list of instances which have the key as primary and the
1558
          # current node as secondary.  this is handy to calculate N+1 memory
1559
          # availability if you can only failover from a primary to its
1560
          # secondary.
1561
          "sinst-by-pnode": {},
1562
        }
1563
        # FIXME: devise a free space model for file based instances as well
1564
        if vg_name is not None:
1565
          test = (constants.NV_VGLIST not in nresult or
1566
                  vg_name not in nresult[constants.NV_VGLIST])
1567
          _ErrorIf(test, self.ENODELVM, node,
1568
                   "node didn't return data for the volume group '%s'"
1569
                   " - it is either missing or broken", vg_name)
1570
          if test:
1571
            continue
1572
          node_info[node]["dfree"] = int(nresult[constants.NV_VGLIST][vg_name])
1573
      except (ValueError, KeyError):
1574
        _ErrorIf(True, self.ENODERPC, node,
1575
                 "node returned invalid nodeinfo, check lvm/hypervisor")
1576
        continue
1577

    
1578
    node_vol_should = {}
1579

    
1580
    feedback_fn("* Verifying instance status")
1581
    for instance in instancelist:
1582
      if verbose:
1583
        feedback_fn("* Verifying instance %s" % instance)
1584
      inst_config = instanceinfo[instance]
1585
      self._VerifyInstance(instance, inst_config, node_volume,
1586
                           node_instance, n_offline)
1587
      inst_nodes_offline = []
1588

    
1589
      inst_config.MapLVsByNode(node_vol_should)
1590

    
1591
      instance_cfg[instance] = inst_config
1592

    
1593
      pnode = inst_config.primary_node
1594
      _ErrorIf(pnode not in node_info and pnode not in n_offline,
1595
               self.ENODERPC, pnode, "instance %s, connection to"
1596
               " primary node failed", instance)
1597
      if pnode in node_info:
1598
        node_info[pnode]['pinst'].append(instance)
1599

    
1600
      if pnode in n_offline:
1601
        inst_nodes_offline.append(pnode)
1602

    
1603
      # If the instance is non-redundant we cannot survive losing its primary
1604
      # node, so we are not N+1 compliant. On the other hand we have no disk
1605
      # templates with more than one secondary so that situation is not well
1606
      # supported either.
1607
      # FIXME: does not support file-backed instances
1608
      if len(inst_config.secondary_nodes) == 0:
1609
        i_non_redundant.append(instance)
1610
      _ErrorIf(len(inst_config.secondary_nodes) > 1,
1611
               self.EINSTANCELAYOUT, instance,
1612
               "instance has multiple secondary nodes", code="WARNING")
1613

    
1614
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1615
        i_non_a_balanced.append(instance)
1616

    
1617
      for snode in inst_config.secondary_nodes:
1618
        _ErrorIf(snode not in node_info and snode not in n_offline,
1619
                 self.ENODERPC, snode,
1620
                 "instance %s, connection to secondary node"
1621
                 " failed", instance)
1622

    
1623
        if snode in node_info:
1624
          node_info[snode]['sinst'].append(instance)
1625
          if pnode not in node_info[snode]['sinst-by-pnode']:
1626
            node_info[snode]['sinst-by-pnode'][pnode] = []
1627
          node_info[snode]['sinst-by-pnode'][pnode].append(instance)
1628

    
1629
        if snode in n_offline:
1630
          inst_nodes_offline.append(snode)
1631

    
1632
      # warn that the instance lives on offline nodes
1633
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
1634
               "instance lives on offline node(s) %s",
1635
               utils.CommaJoin(inst_nodes_offline))
1636

    
1637
    feedback_fn("* Verifying orphan volumes")
1638
    self._VerifyOrphanVolumes(node_vol_should, node_volume)
1639

    
1640
    feedback_fn("* Verifying remaining instances")
1641
    self._VerifyOrphanInstances(instancelist, node_instance)
1642

    
1643
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1644
      feedback_fn("* Verifying N+1 Memory redundancy")
1645
      self._VerifyNPlusOneMemory(node_info, instance_cfg)
1646

    
1647
    feedback_fn("* Other Notes")
1648
    if i_non_redundant:
1649
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1650
                  % len(i_non_redundant))
1651

    
1652
    if i_non_a_balanced:
1653
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1654
                  % len(i_non_a_balanced))
1655

    
1656
    if n_offline:
1657
      feedback_fn("  - NOTICE: %d offline node(s) found." % len(n_offline))
1658

    
1659
    if n_drained:
1660
      feedback_fn("  - NOTICE: %d drained node(s) found." % len(n_drained))
1661

    
1662
    return not self.bad
1663

    
1664
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1665
    """Analyze the post-hooks' result
1666

1667
    This method analyses the hook result, handles it, and sends some
1668
    nicely-formatted feedback back to the user.
1669

1670
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1671
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1672
    @param hooks_results: the results of the multi-node hooks rpc call
1673
    @param feedback_fn: function used send feedback back to the caller
1674
    @param lu_result: previous Exec result
1675
    @return: the new Exec result, based on the previous result
1676
        and hook results
1677

1678
    """
1679
    # We only really run POST phase hooks, and are only interested in
1680
    # their results
1681
    if phase == constants.HOOKS_PHASE_POST:
1682
      # Used to change hooks' output to proper indentation
1683
      indent_re = re.compile('^', re.M)
1684
      feedback_fn("* Hooks Results")
1685
      assert hooks_results, "invalid result from hooks"
1686

    
1687
      for node_name in hooks_results:
1688
        res = hooks_results[node_name]
1689
        msg = res.fail_msg
1690
        test = msg and not res.offline
1691
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
1692
                      "Communication failure in hooks execution: %s", msg)
1693
        if res.offline or msg:
1694
          # No need to investigate payload if node is offline or gave an error.
1695
          # override manually lu_result here as _ErrorIf only
1696
          # overrides self.bad
1697
          lu_result = 1
1698
          continue
1699
        for script, hkr, output in res.payload:
1700
          test = hkr == constants.HKR_FAIL
1701
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
1702
                        "Script %s failed, output:", script)
1703
          if test:
1704
            output = indent_re.sub('      ', output)
1705
            feedback_fn("%s" % output)
1706
            lu_result = 0
1707

    
1708
      return lu_result
1709

    
1710

    
1711
class LUVerifyDisks(NoHooksLU):
1712
  """Verifies the cluster disks status.
1713

1714
  """
1715
  _OP_REQP = []
1716
  REQ_BGL = False
1717

    
1718
  def ExpandNames(self):
1719
    self.needed_locks = {
1720
      locking.LEVEL_NODE: locking.ALL_SET,
1721
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1722
    }
1723
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1724

    
1725
  def CheckPrereq(self):
1726
    """Check prerequisites.
1727

1728
    This has no prerequisites.
1729

1730
    """
1731
    pass
1732

    
1733
  def Exec(self, feedback_fn):
1734
    """Verify integrity of cluster disks.
1735

1736
    @rtype: tuple of three items
1737
    @return: a tuple of (dict of node-to-node_error, list of instances
1738
        which need activate-disks, dict of instance: (node, volume) for
1739
        missing volumes
1740

1741
    """
1742
    result = res_nodes, res_instances, res_missing = {}, [], {}
1743

    
1744
    vg_name = self.cfg.GetVGName()
1745
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1746
    instances = [self.cfg.GetInstanceInfo(name)
1747
                 for name in self.cfg.GetInstanceList()]
1748

    
1749
    nv_dict = {}
1750
    for inst in instances:
1751
      inst_lvs = {}
1752
      if (not inst.admin_up or
1753
          inst.disk_template not in constants.DTS_NET_MIRROR):
1754
        continue
1755
      inst.MapLVsByNode(inst_lvs)
1756
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1757
      for node, vol_list in inst_lvs.iteritems():
1758
        for vol in vol_list:
1759
          nv_dict[(node, vol)] = inst
1760

    
1761
    if not nv_dict:
1762
      return result
1763

    
1764
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1765

    
1766
    for node in nodes:
1767
      # node_volume
1768
      node_res = node_lvs[node]
1769
      if node_res.offline:
1770
        continue
1771
      msg = node_res.fail_msg
1772
      if msg:
1773
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1774
        res_nodes[node] = msg
1775
        continue
1776

    
1777
      lvs = node_res.payload
1778
      for lv_name, (_, _, lv_online) in lvs.items():
1779
        inst = nv_dict.pop((node, lv_name), None)
1780
        if (not lv_online and inst is not None
1781
            and inst.name not in res_instances):
1782
          res_instances.append(inst.name)
1783

    
1784
    # any leftover items in nv_dict are missing LVs, let's arrange the
1785
    # data better
1786
    for key, inst in nv_dict.iteritems():
1787
      if inst.name not in res_missing:
1788
        res_missing[inst.name] = []
1789
      res_missing[inst.name].append(key)
1790

    
1791
    return result
1792

    
1793

    
1794
class LURepairDiskSizes(NoHooksLU):
1795
  """Verifies the cluster disks sizes.
1796

1797
  """
1798
  _OP_REQP = ["instances"]
1799
  REQ_BGL = False
1800

    
1801
  def ExpandNames(self):
1802
    if not isinstance(self.op.instances, list):
1803
      raise errors.OpPrereqError("Invalid argument type 'instances'",
1804
                                 errors.ECODE_INVAL)
1805

    
1806
    if self.op.instances:
1807
      self.wanted_names = []
1808
      for name in self.op.instances:
1809
        full_name = _ExpandInstanceName(self.cfg, name)
1810
        self.wanted_names.append(full_name)
1811
      self.needed_locks = {
1812
        locking.LEVEL_NODE: [],
1813
        locking.LEVEL_INSTANCE: self.wanted_names,
1814
        }
1815
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1816
    else:
1817
      self.wanted_names = None
1818
      self.needed_locks = {
1819
        locking.LEVEL_NODE: locking.ALL_SET,
1820
        locking.LEVEL_INSTANCE: locking.ALL_SET,
1821
        }
1822
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1823

    
1824
  def DeclareLocks(self, level):
1825
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
1826
      self._LockInstancesNodes(primary_only=True)
1827

    
1828
  def CheckPrereq(self):
1829
    """Check prerequisites.
1830

1831
    This only checks the optional instance list against the existing names.
1832

1833
    """
1834
    if self.wanted_names is None:
1835
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
1836

    
1837
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
1838
                             in self.wanted_names]
1839

    
1840
  def _EnsureChildSizes(self, disk):
1841
    """Ensure children of the disk have the needed disk size.
1842

1843
    This is valid mainly for DRBD8 and fixes an issue where the
1844
    children have smaller disk size.
1845

1846
    @param disk: an L{ganeti.objects.Disk} object
1847

1848
    """
1849
    if disk.dev_type == constants.LD_DRBD8:
1850
      assert disk.children, "Empty children for DRBD8?"
1851
      fchild = disk.children[0]
1852
      mismatch = fchild.size < disk.size
1853
      if mismatch:
1854
        self.LogInfo("Child disk has size %d, parent %d, fixing",
1855
                     fchild.size, disk.size)
1856
        fchild.size = disk.size
1857

    
1858
      # and we recurse on this child only, not on the metadev
1859
      return self._EnsureChildSizes(fchild) or mismatch
1860
    else:
1861
      return False
1862

    
1863
  def Exec(self, feedback_fn):
1864
    """Verify the size of cluster disks.
1865

1866
    """
1867
    # TODO: check child disks too
1868
    # TODO: check differences in size between primary/secondary nodes
1869
    per_node_disks = {}
1870
    for instance in self.wanted_instances:
1871
      pnode = instance.primary_node
1872
      if pnode not in per_node_disks:
1873
        per_node_disks[pnode] = []
1874
      for idx, disk in enumerate(instance.disks):
1875
        per_node_disks[pnode].append((instance, idx, disk))
1876

    
1877
    changed = []
1878
    for node, dskl in per_node_disks.items():
1879
      newl = [v[2].Copy() for v in dskl]
1880
      for dsk in newl:
1881
        self.cfg.SetDiskID(dsk, node)
1882
      result = self.rpc.call_blockdev_getsizes(node, newl)
1883
      if result.fail_msg:
1884
        self.LogWarning("Failure in blockdev_getsizes call to node"
1885
                        " %s, ignoring", node)
1886
        continue
1887
      if len(result.data) != len(dskl):
1888
        self.LogWarning("Invalid result from node %s, ignoring node results",
1889
                        node)
1890
        continue
1891
      for ((instance, idx, disk), size) in zip(dskl, result.data):
1892
        if size is None:
1893
          self.LogWarning("Disk %d of instance %s did not return size"
1894
                          " information, ignoring", idx, instance.name)
1895
          continue
1896
        if not isinstance(size, (int, long)):
1897
          self.LogWarning("Disk %d of instance %s did not return valid"
1898
                          " size information, ignoring", idx, instance.name)
1899
          continue
1900
        size = size >> 20
1901
        if size != disk.size:
1902
          self.LogInfo("Disk %d of instance %s has mismatched size,"
1903
                       " correcting: recorded %d, actual %d", idx,
1904
                       instance.name, disk.size, size)
1905
          disk.size = size
1906
          self.cfg.Update(instance, feedback_fn)
1907
          changed.append((instance.name, idx, size))
1908
        if self._EnsureChildSizes(disk):
1909
          self.cfg.Update(instance, feedback_fn)
1910
          changed.append((instance.name, idx, disk.size))
1911
    return changed
1912

    
1913

    
1914
class LURenameCluster(LogicalUnit):
1915
  """Rename the cluster.
1916

1917
  """
1918
  HPATH = "cluster-rename"
1919
  HTYPE = constants.HTYPE_CLUSTER
1920
  _OP_REQP = ["name"]
1921

    
1922
  def BuildHooksEnv(self):
1923
    """Build hooks env.
1924

1925
    """
1926
    env = {
1927
      "OP_TARGET": self.cfg.GetClusterName(),
1928
      "NEW_NAME": self.op.name,
1929
      }
1930
    mn = self.cfg.GetMasterNode()
1931
    all_nodes = self.cfg.GetNodeList()
1932
    return env, [mn], all_nodes
1933

    
1934
  def CheckPrereq(self):
1935
    """Verify that the passed name is a valid one.
1936

1937
    """
1938
    hostname = utils.GetHostInfo(self.op.name)
1939

    
1940
    new_name = hostname.name
1941
    self.ip = new_ip = hostname.ip
1942
    old_name = self.cfg.GetClusterName()
1943
    old_ip = self.cfg.GetMasterIP()
1944
    if new_name == old_name and new_ip == old_ip:
1945
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
1946
                                 " cluster has changed",
1947
                                 errors.ECODE_INVAL)
1948
    if new_ip != old_ip:
1949
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1950
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
1951
                                   " reachable on the network. Aborting." %
1952
                                   new_ip, errors.ECODE_NOTUNIQUE)
1953

    
1954
    self.op.name = new_name
1955

    
1956
  def Exec(self, feedback_fn):
1957
    """Rename the cluster.
1958

1959
    """
1960
    clustername = self.op.name
1961
    ip = self.ip
1962

    
1963
    # shutdown the master IP
1964
    master = self.cfg.GetMasterNode()
1965
    result = self.rpc.call_node_stop_master(master, False)
1966
    result.Raise("Could not disable the master role")
1967

    
1968
    try:
1969
      cluster = self.cfg.GetClusterInfo()
1970
      cluster.cluster_name = clustername
1971
      cluster.master_ip = ip
1972
      self.cfg.Update(cluster, feedback_fn)
1973

    
1974
      # update the known hosts file
1975
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
1976
      node_list = self.cfg.GetNodeList()
1977
      try:
1978
        node_list.remove(master)
1979
      except ValueError:
1980
        pass
1981
      result = self.rpc.call_upload_file(node_list,
1982
                                         constants.SSH_KNOWN_HOSTS_FILE)
1983
      for to_node, to_result in result.iteritems():
1984
        msg = to_result.fail_msg
1985
        if msg:
1986
          msg = ("Copy of file %s to node %s failed: %s" %
1987
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
1988
          self.proc.LogWarning(msg)
1989

    
1990
    finally:
1991
      result = self.rpc.call_node_start_master(master, False, False)
1992
      msg = result.fail_msg
1993
      if msg:
1994
        self.LogWarning("Could not re-enable the master role on"
1995
                        " the master, please restart manually: %s", msg)
1996

    
1997

    
1998
def _RecursiveCheckIfLVMBased(disk):
1999
  """Check if the given disk or its children are lvm-based.
2000

2001
  @type disk: L{objects.Disk}
2002
  @param disk: the disk to check
2003
  @rtype: boolean
2004
  @return: boolean indicating whether a LD_LV dev_type was found or not
2005

2006
  """
2007
  if disk.children:
2008
    for chdisk in disk.children:
2009
      if _RecursiveCheckIfLVMBased(chdisk):
2010
        return True
2011
  return disk.dev_type == constants.LD_LV
2012

    
2013

    
2014
class LUSetClusterParams(LogicalUnit):
2015
  """Change the parameters of the cluster.
2016

2017
  """
2018
  HPATH = "cluster-modify"
2019
  HTYPE = constants.HTYPE_CLUSTER
2020
  _OP_REQP = []
2021
  REQ_BGL = False
2022

    
2023
  def CheckArguments(self):
2024
    """Check parameters
2025

2026
    """
2027
    if not hasattr(self.op, "candidate_pool_size"):
2028
      self.op.candidate_pool_size = None
2029
    if self.op.candidate_pool_size is not None:
2030
      try:
2031
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
2032
      except (ValueError, TypeError), err:
2033
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
2034
                                   str(err), errors.ECODE_INVAL)
2035
      if self.op.candidate_pool_size < 1:
2036
        raise errors.OpPrereqError("At least one master candidate needed",
2037
                                   errors.ECODE_INVAL)
2038

    
2039
  def ExpandNames(self):
2040
    # FIXME: in the future maybe other cluster params won't require checking on
2041
    # all nodes to be modified.
2042
    self.needed_locks = {
2043
      locking.LEVEL_NODE: locking.ALL_SET,
2044
    }
2045
    self.share_locks[locking.LEVEL_NODE] = 1
2046

    
2047
  def BuildHooksEnv(self):
2048
    """Build hooks env.
2049

2050
    """
2051
    env = {
2052
      "OP_TARGET": self.cfg.GetClusterName(),
2053
      "NEW_VG_NAME": self.op.vg_name,
2054
      }
2055
    mn = self.cfg.GetMasterNode()
2056
    return env, [mn], [mn]
2057

    
2058
  def CheckPrereq(self):
2059
    """Check prerequisites.
2060

2061
    This checks whether the given params don't conflict and
2062
    if the given volume group is valid.
2063

2064
    """
2065
    if self.op.vg_name is not None and not self.op.vg_name:
2066
      instances = self.cfg.GetAllInstancesInfo().values()
2067
      for inst in instances:
2068
        for disk in inst.disks:
2069
          if _RecursiveCheckIfLVMBased(disk):
2070
            raise errors.OpPrereqError("Cannot disable lvm storage while"
2071
                                       " lvm-based instances exist",
2072
                                       errors.ECODE_INVAL)
2073

    
2074
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2075

    
2076
    # if vg_name not None, checks given volume group on all nodes
2077
    if self.op.vg_name:
2078
      vglist = self.rpc.call_vg_list(node_list)
2079
      for node in node_list:
2080
        msg = vglist[node].fail_msg
2081
        if msg:
2082
          # ignoring down node
2083
          self.LogWarning("Error while gathering data on node %s"
2084
                          " (ignoring node): %s", node, msg)
2085
          continue
2086
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2087
                                              self.op.vg_name,
2088
                                              constants.MIN_VG_SIZE)
2089
        if vgstatus:
2090
          raise errors.OpPrereqError("Error on node '%s': %s" %
2091
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2092

    
2093
    self.cluster = cluster = self.cfg.GetClusterInfo()
2094
    # validate params changes
2095
    if self.op.beparams:
2096
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2097
      self.new_beparams = objects.FillDict(
2098
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
2099

    
2100
    if self.op.nicparams:
2101
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2102
      self.new_nicparams = objects.FillDict(
2103
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
2104
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2105
      nic_errors = []
2106

    
2107
      # check all instances for consistency
2108
      for instance in self.cfg.GetAllInstancesInfo().values():
2109
        for nic_idx, nic in enumerate(instance.nics):
2110
          params_copy = copy.deepcopy(nic.nicparams)
2111
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2112

    
2113
          # check parameter syntax
2114
          try:
2115
            objects.NIC.CheckParameterSyntax(params_filled)
2116
          except errors.ConfigurationError, err:
2117
            nic_errors.append("Instance %s, nic/%d: %s" %
2118
                              (instance.name, nic_idx, err))
2119

    
2120
          # if we're moving instances to routed, check that they have an ip
2121
          target_mode = params_filled[constants.NIC_MODE]
2122
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2123
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2124
                              (instance.name, nic_idx))
2125
      if nic_errors:
2126
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2127
                                   "\n".join(nic_errors))
2128

    
2129
    # hypervisor list/parameters
2130
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
2131
    if self.op.hvparams:
2132
      if not isinstance(self.op.hvparams, dict):
2133
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input",
2134
                                   errors.ECODE_INVAL)
2135
      for hv_name, hv_dict in self.op.hvparams.items():
2136
        if hv_name not in self.new_hvparams:
2137
          self.new_hvparams[hv_name] = hv_dict
2138
        else:
2139
          self.new_hvparams[hv_name].update(hv_dict)
2140

    
2141
    # os hypervisor parameters
2142
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2143
    if self.op.os_hvp:
2144
      if not isinstance(self.op.os_hvp, dict):
2145
        raise errors.OpPrereqError("Invalid 'os_hvp' parameter on input",
2146
                                   errors.ECODE_INVAL)
2147
      for os_name, hvs in self.op.os_hvp.items():
2148
        if not isinstance(hvs, dict):
2149
          raise errors.OpPrereqError(("Invalid 'os_hvp' parameter on"
2150
                                      " input"), errors.ECODE_INVAL)
2151
        if os_name not in self.new_os_hvp:
2152
          self.new_os_hvp[os_name] = hvs
2153
        else:
2154
          for hv_name, hv_dict in hvs.items():
2155
            if hv_name not in self.new_os_hvp[os_name]:
2156
              self.new_os_hvp[os_name][hv_name] = hv_dict
2157
            else:
2158
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
2159

    
2160
    if self.op.enabled_hypervisors is not None:
2161
      self.hv_list = self.op.enabled_hypervisors
2162
      if not self.hv_list:
2163
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
2164
                                   " least one member",
2165
                                   errors.ECODE_INVAL)
2166
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
2167
      if invalid_hvs:
2168
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
2169
                                   " entries: %s" %
2170
                                   utils.CommaJoin(invalid_hvs),
2171
                                   errors.ECODE_INVAL)
2172
    else:
2173
      self.hv_list = cluster.enabled_hypervisors
2174

    
2175
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
2176
      # either the enabled list has changed, or the parameters have, validate
2177
      for hv_name, hv_params in self.new_hvparams.items():
2178
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
2179
            (self.op.enabled_hypervisors and
2180
             hv_name in self.op.enabled_hypervisors)):
2181
          # either this is a new hypervisor, or its parameters have changed
2182
          hv_class = hypervisor.GetHypervisor(hv_name)
2183
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2184
          hv_class.CheckParameterSyntax(hv_params)
2185
          _CheckHVParams(self, node_list, hv_name, hv_params)
2186

    
2187
    if self.op.os_hvp:
2188
      # no need to check any newly-enabled hypervisors, since the
2189
      # defaults have already been checked in the above code-block
2190
      for os_name, os_hvp in self.new_os_hvp.items():
2191
        for hv_name, hv_params in os_hvp.items():
2192
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2193
          # we need to fill in the new os_hvp on top of the actual hv_p
2194
          cluster_defaults = self.new_hvparams.get(hv_name, {})
2195
          new_osp = objects.FillDict(cluster_defaults, hv_params)
2196
          hv_class = hypervisor.GetHypervisor(hv_name)
2197
          hv_class.CheckParameterSyntax(new_osp)
2198
          _CheckHVParams(self, node_list, hv_name, new_osp)
2199

    
2200

    
2201
  def Exec(self, feedback_fn):
2202
    """Change the parameters of the cluster.
2203

2204
    """
2205
    if self.op.vg_name is not None:
2206
      new_volume = self.op.vg_name
2207
      if not new_volume:
2208
        new_volume = None
2209
      if new_volume != self.cfg.GetVGName():
2210
        self.cfg.SetVGName(new_volume)
2211
      else:
2212
        feedback_fn("Cluster LVM configuration already in desired"
2213
                    " state, not changing")
2214
    if self.op.hvparams:
2215
      self.cluster.hvparams = self.new_hvparams
2216
    if self.op.os_hvp:
2217
      self.cluster.os_hvp = self.new_os_hvp
2218
    if self.op.enabled_hypervisors is not None:
2219
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
2220
    if self.op.beparams:
2221
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
2222
    if self.op.nicparams:
2223
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
2224

    
2225
    if self.op.candidate_pool_size is not None:
2226
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
2227
      # we need to update the pool size here, otherwise the save will fail
2228
      _AdjustCandidatePool(self, [])
2229

    
2230
    self.cfg.Update(self.cluster, feedback_fn)
2231

    
2232

    
2233
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
2234
  """Distribute additional files which are part of the cluster configuration.
2235

2236
  ConfigWriter takes care of distributing the config and ssconf files, but
2237
  there are more files which should be distributed to all nodes. This function
2238
  makes sure those are copied.
2239

2240
  @param lu: calling logical unit
2241
  @param additional_nodes: list of nodes not in the config to distribute to
2242

2243
  """
2244
  # 1. Gather target nodes
2245
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
2246
  dist_nodes = lu.cfg.GetOnlineNodeList()
2247
  if additional_nodes is not None:
2248
    dist_nodes.extend(additional_nodes)
2249
  if myself.name in dist_nodes:
2250
    dist_nodes.remove(myself.name)
2251

    
2252
  # 2. Gather files to distribute
2253
  dist_files = set([constants.ETC_HOSTS,
2254
                    constants.SSH_KNOWN_HOSTS_FILE,
2255
                    constants.RAPI_CERT_FILE,
2256
                    constants.RAPI_USERS_FILE,
2257
                    constants.HMAC_CLUSTER_KEY,
2258
                   ])
2259

    
2260
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
2261
  for hv_name in enabled_hypervisors:
2262
    hv_class = hypervisor.GetHypervisor(hv_name)
2263
    dist_files.update(hv_class.GetAncillaryFiles())
2264

    
2265
  # 3. Perform the files upload
2266
  for fname in dist_files:
2267
    if os.path.exists(fname):
2268
      result = lu.rpc.call_upload_file(dist_nodes, fname)
2269
      for to_node, to_result in result.items():
2270
        msg = to_result.fail_msg
2271
        if msg:
2272
          msg = ("Copy of file %s to node %s failed: %s" %
2273
                 (fname, to_node, msg))
2274
          lu.proc.LogWarning(msg)
2275

    
2276

    
2277
class LURedistributeConfig(NoHooksLU):
2278
  """Force the redistribution of cluster configuration.
2279

2280
  This is a very simple LU.
2281

2282
  """
2283
  _OP_REQP = []
2284
  REQ_BGL = False
2285

    
2286
  def ExpandNames(self):
2287
    self.needed_locks = {
2288
      locking.LEVEL_NODE: locking.ALL_SET,
2289
    }
2290
    self.share_locks[locking.LEVEL_NODE] = 1
2291

    
2292
  def CheckPrereq(self):
2293
    """Check prerequisites.
2294

2295
    """
2296

    
2297
  def Exec(self, feedback_fn):
2298
    """Redistribute the configuration.
2299

2300
    """
2301
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
2302
    _RedistributeAncillaryFiles(self)
2303

    
2304

    
2305
def _WaitForSync(lu, instance, oneshot=False):
2306
  """Sleep and poll for an instance's disk to sync.
2307

2308
  """
2309
  if not instance.disks:
2310
    return True
2311

    
2312
  if not oneshot:
2313
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
2314

    
2315
  node = instance.primary_node
2316

    
2317
  for dev in instance.disks:
2318
    lu.cfg.SetDiskID(dev, node)
2319

    
2320
  # TODO: Convert to utils.Retry
2321

    
2322
  retries = 0
2323
  degr_retries = 10 # in seconds, as we sleep 1 second each time
2324
  while True:
2325
    max_time = 0
2326
    done = True
2327
    cumul_degraded = False
2328
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
2329
    msg = rstats.fail_msg
2330
    if msg:
2331
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
2332
      retries += 1
2333
      if retries >= 10:
2334
        raise errors.RemoteError("Can't contact node %s for mirror data,"
2335
                                 " aborting." % node)
2336
      time.sleep(6)
2337
      continue
2338
    rstats = rstats.payload
2339
    retries = 0
2340
    for i, mstat in enumerate(rstats):
2341
      if mstat is None:
2342
        lu.LogWarning("Can't compute data for node %s/%s",
2343
                           node, instance.disks[i].iv_name)
2344
        continue
2345

    
2346
      cumul_degraded = (cumul_degraded or
2347
                        (mstat.is_degraded and mstat.sync_percent is None))
2348
      if mstat.sync_percent is not None:
2349
        done = False
2350
        if mstat.estimated_time is not None:
2351
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
2352
          max_time = mstat.estimated_time
2353
        else:
2354
          rem_time = "no time estimate"
2355
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
2356
                        (instance.disks[i].iv_name, mstat.sync_percent,
2357
                         rem_time))
2358

    
2359
    # if we're done but degraded, let's do a few small retries, to
2360
    # make sure we see a stable and not transient situation; therefore
2361
    # we force restart of the loop
2362
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
2363
      logging.info("Degraded disks found, %d retries left", degr_retries)
2364
      degr_retries -= 1
2365
      time.sleep(1)
2366
      continue
2367

    
2368
    if done or oneshot:
2369
      break
2370

    
2371
    time.sleep(min(60, max_time))
2372

    
2373
  if done:
2374
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
2375
  return not cumul_degraded
2376

    
2377

    
2378
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
2379
  """Check that mirrors are not degraded.
2380

2381
  The ldisk parameter, if True, will change the test from the
2382
  is_degraded attribute (which represents overall non-ok status for
2383
  the device(s)) to the ldisk (representing the local storage status).
2384

2385
  """
2386
  lu.cfg.SetDiskID(dev, node)
2387

    
2388
  result = True
2389

    
2390
  if on_primary or dev.AssembleOnSecondary():
2391
    rstats = lu.rpc.call_blockdev_find(node, dev)
2392
    msg = rstats.fail_msg
2393
    if msg:
2394
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2395
      result = False
2396
    elif not rstats.payload:
2397
      lu.LogWarning("Can't find disk on node %s", node)
2398
      result = False
2399
    else:
2400
      if ldisk:
2401
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2402
      else:
2403
        result = result and not rstats.payload.is_degraded
2404

    
2405
  if dev.children:
2406
    for child in dev.children:
2407
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2408

    
2409
  return result
2410

    
2411

    
2412
class LUDiagnoseOS(NoHooksLU):
2413
  """Logical unit for OS diagnose/query.
2414

2415
  """
2416
  _OP_REQP = ["output_fields", "names"]
2417
  REQ_BGL = False
2418
  _FIELDS_STATIC = utils.FieldSet()
2419
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status", "variants")
2420
  # Fields that need calculation of global os validity
2421
  _FIELDS_NEEDVALID = frozenset(["valid", "variants"])
2422

    
2423
  def ExpandNames(self):
2424
    if self.op.names:
2425
      raise errors.OpPrereqError("Selective OS query not supported",
2426
                                 errors.ECODE_INVAL)
2427

    
2428
    _CheckOutputFields(static=self._FIELDS_STATIC,
2429
                       dynamic=self._FIELDS_DYNAMIC,
2430
                       selected=self.op.output_fields)
2431

    
2432
    # Lock all nodes, in shared mode
2433
    # Temporary removal of locks, should be reverted later
2434
    # TODO: reintroduce locks when they are lighter-weight
2435
    self.needed_locks = {}
2436
    #self.share_locks[locking.LEVEL_NODE] = 1
2437
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2438

    
2439
  def CheckPrereq(self):
2440
    """Check prerequisites.
2441

2442
    """
2443

    
2444
  @staticmethod
2445
  def _DiagnoseByOS(rlist):
2446
    """Remaps a per-node return list into an a per-os per-node dictionary
2447

2448
    @param rlist: a map with node names as keys and OS objects as values
2449

2450
    @rtype: dict
2451
    @return: a dictionary with osnames as keys and as value another map, with
2452
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2453

2454
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2455
                                     (/srv/..., False, "invalid api")],
2456
                           "node2": [(/srv/..., True, "")]}
2457
          }
2458

2459
    """
2460
    all_os = {}
2461
    # we build here the list of nodes that didn't fail the RPC (at RPC
2462
    # level), so that nodes with a non-responding node daemon don't
2463
    # make all OSes invalid
2464
    good_nodes = [node_name for node_name in rlist
2465
                  if not rlist[node_name].fail_msg]
2466
    for node_name, nr in rlist.items():
2467
      if nr.fail_msg or not nr.payload:
2468
        continue
2469
      for name, path, status, diagnose, variants in nr.payload:
2470
        if name not in all_os:
2471
          # build a list of nodes for this os containing empty lists
2472
          # for each node in node_list
2473
          all_os[name] = {}
2474
          for nname in good_nodes:
2475
            all_os[name][nname] = []
2476
        all_os[name][node_name].append((path, status, diagnose, variants))
2477
    return all_os
2478

    
2479
  def Exec(self, feedback_fn):
2480
    """Compute the list of OSes.
2481

2482
    """
2483
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2484
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2485
    pol = self._DiagnoseByOS(node_data)
2486
    output = []
2487
    calc_valid = self._FIELDS_NEEDVALID.intersection(self.op.output_fields)
2488
    calc_variants = "variants" in self.op.output_fields
2489

    
2490
    for os_name, os_data in pol.items():
2491
      row = []
2492
      if calc_valid:
2493
        valid = True
2494
        variants = None
2495
        for osl in os_data.values():
2496
          valid = valid and osl and osl[0][1]
2497
          if not valid:
2498
            variants = None
2499
            break
2500
          if calc_variants:
2501
            node_variants = osl[0][3]
2502
            if variants is None:
2503
              variants = node_variants
2504
            else:
2505
              variants = [v for v in variants if v in node_variants]
2506

    
2507
      for field in self.op.output_fields:
2508
        if field == "name":
2509
          val = os_name
2510
        elif field == "valid":
2511
          val = valid
2512
        elif field == "node_status":
2513
          # this is just a copy of the dict
2514
          val = {}
2515
          for node_name, nos_list in os_data.items():
2516
            val[node_name] = nos_list
2517
        elif field == "variants":
2518
          val =  variants
2519
        else:
2520
          raise errors.ParameterError(field)
2521
        row.append(val)
2522
      output.append(row)
2523

    
2524
    return output
2525

    
2526

    
2527
class LURemoveNode(LogicalUnit):
2528
  """Logical unit for removing a node.
2529

2530
  """
2531
  HPATH = "node-remove"
2532
  HTYPE = constants.HTYPE_NODE
2533
  _OP_REQP = ["node_name"]
2534

    
2535
  def BuildHooksEnv(self):
2536
    """Build hooks env.
2537

2538
    This doesn't run on the target node in the pre phase as a failed
2539
    node would then be impossible to remove.
2540

2541
    """
2542
    env = {
2543
      "OP_TARGET": self.op.node_name,
2544
      "NODE_NAME": self.op.node_name,
2545
      }
2546
    all_nodes = self.cfg.GetNodeList()
2547
    try:
2548
      all_nodes.remove(self.op.node_name)
2549
    except ValueError:
2550
      logging.warning("Node %s which is about to be removed not found"
2551
                      " in the all nodes list", self.op.node_name)
2552
    return env, all_nodes, all_nodes
2553

    
2554
  def CheckPrereq(self):
2555
    """Check prerequisites.
2556

2557
    This checks:
2558
     - the node exists in the configuration
2559
     - it does not have primary or secondary instances
2560
     - it's not the master
2561

2562
    Any errors are signaled by raising errors.OpPrereqError.
2563

2564
    """
2565
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
2566
    node = self.cfg.GetNodeInfo(self.op.node_name)
2567
    assert node is not None
2568

    
2569
    instance_list = self.cfg.GetInstanceList()
2570

    
2571
    masternode = self.cfg.GetMasterNode()
2572
    if node.name == masternode:
2573
      raise errors.OpPrereqError("Node is the master node,"
2574
                                 " you need to failover first.",
2575
                                 errors.ECODE_INVAL)
2576

    
2577
    for instance_name in instance_list:
2578
      instance = self.cfg.GetInstanceInfo(instance_name)
2579
      if node.name in instance.all_nodes:
2580
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2581
                                   " please remove first." % instance_name,
2582
                                   errors.ECODE_INVAL)
2583
    self.op.node_name = node.name
2584
    self.node = node
2585

    
2586
  def Exec(self, feedback_fn):
2587
    """Removes the node from the cluster.
2588

2589
    """
2590
    node = self.node
2591
    logging.info("Stopping the node daemon and removing configs from node %s",
2592
                 node.name)
2593

    
2594
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
2595

    
2596
    # Promote nodes to master candidate as needed
2597
    _AdjustCandidatePool(self, exceptions=[node.name])
2598
    self.context.RemoveNode(node.name)
2599

    
2600
    # Run post hooks on the node before it's removed
2601
    hm = self.proc.hmclass(self.rpc.call_hooks_runner, self)
2602
    try:
2603
      hm.RunPhase(constants.HOOKS_PHASE_POST, [node.name])
2604
    except:
2605
      # pylint: disable-msg=W0702
2606
      self.LogWarning("Errors occurred running hooks on %s" % node.name)
2607

    
2608
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
2609
    msg = result.fail_msg
2610
    if msg:
2611
      self.LogWarning("Errors encountered on the remote node while leaving"
2612
                      " the cluster: %s", msg)
2613

    
2614

    
2615
class LUQueryNodes(NoHooksLU):
2616
  """Logical unit for querying nodes.
2617

2618
  """
2619
  # pylint: disable-msg=W0142
2620
  _OP_REQP = ["output_fields", "names", "use_locking"]
2621
  REQ_BGL = False
2622

    
2623
  _SIMPLE_FIELDS = ["name", "serial_no", "ctime", "mtime", "uuid",
2624
                    "master_candidate", "offline", "drained"]
2625

    
2626
  _FIELDS_DYNAMIC = utils.FieldSet(
2627
    "dtotal", "dfree",
2628
    "mtotal", "mnode", "mfree",
2629
    "bootid",
2630
    "ctotal", "cnodes", "csockets",
2631
    )
2632

    
2633
  _FIELDS_STATIC = utils.FieldSet(*[
2634
    "pinst_cnt", "sinst_cnt",
2635
    "pinst_list", "sinst_list",
2636
    "pip", "sip", "tags",
2637
    "master",
2638
    "role"] + _SIMPLE_FIELDS
2639
    )
2640

    
2641
  def ExpandNames(self):
2642
    _CheckOutputFields(static=self._FIELDS_STATIC,
2643
                       dynamic=self._FIELDS_DYNAMIC,
2644
                       selected=self.op.output_fields)
2645

    
2646
    self.needed_locks = {}
2647
    self.share_locks[locking.LEVEL_NODE] = 1
2648

    
2649
    if self.op.names:
2650
      self.wanted = _GetWantedNodes(self, self.op.names)
2651
    else:
2652
      self.wanted = locking.ALL_SET
2653

    
2654
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2655
    self.do_locking = self.do_node_query and self.op.use_locking
2656
    if self.do_locking:
2657
      # if we don't request only static fields, we need to lock the nodes
2658
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2659

    
2660
  def CheckPrereq(self):
2661
    """Check prerequisites.
2662

2663
    """
2664
    # The validation of the node list is done in the _GetWantedNodes,
2665
    # if non empty, and if empty, there's no validation to do
2666
    pass
2667

    
2668
  def Exec(self, feedback_fn):
2669
    """Computes the list of nodes and their attributes.
2670

2671
    """
2672
    all_info = self.cfg.GetAllNodesInfo()
2673
    if self.do_locking:
2674
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2675
    elif self.wanted != locking.ALL_SET:
2676
      nodenames = self.wanted
2677
      missing = set(nodenames).difference(all_info.keys())
2678
      if missing:
2679
        raise errors.OpExecError(
2680
          "Some nodes were removed before retrieving their data: %s" % missing)
2681
    else:
2682
      nodenames = all_info.keys()
2683

    
2684
    nodenames = utils.NiceSort(nodenames)
2685
    nodelist = [all_info[name] for name in nodenames]
2686

    
2687
    # begin data gathering
2688

    
2689
    if self.do_node_query:
2690
      live_data = {}
2691
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2692
                                          self.cfg.GetHypervisorType())
2693
      for name in nodenames:
2694
        nodeinfo = node_data[name]
2695
        if not nodeinfo.fail_msg and nodeinfo.payload:
2696
          nodeinfo = nodeinfo.payload
2697
          fn = utils.TryConvert
2698
          live_data[name] = {
2699
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2700
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2701
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2702
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2703
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2704
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2705
            "bootid": nodeinfo.get('bootid', None),
2706
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2707
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2708
            }
2709
        else:
2710
          live_data[name] = {}
2711
    else:
2712
      live_data = dict.fromkeys(nodenames, {})
2713

    
2714
    node_to_primary = dict([(name, set()) for name in nodenames])
2715
    node_to_secondary = dict([(name, set()) for name in nodenames])
2716

    
2717
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2718
                             "sinst_cnt", "sinst_list"))
2719
    if inst_fields & frozenset(self.op.output_fields):
2720
      inst_data = self.cfg.GetAllInstancesInfo()
2721

    
2722
      for inst in inst_data.values():
2723
        if inst.primary_node in node_to_primary:
2724
          node_to_primary[inst.primary_node].add(inst.name)
2725
        for secnode in inst.secondary_nodes:
2726
          if secnode in node_to_secondary:
2727
            node_to_secondary[secnode].add(inst.name)
2728

    
2729
    master_node = self.cfg.GetMasterNode()
2730

    
2731
    # end data gathering
2732

    
2733
    output = []
2734
    for node in nodelist:
2735
      node_output = []
2736
      for field in self.op.output_fields:
2737
        if field in self._SIMPLE_FIELDS:
2738
          val = getattr(node, field)
2739
        elif field == "pinst_list":
2740
          val = list(node_to_primary[node.name])
2741
        elif field == "sinst_list":
2742
          val = list(node_to_secondary[node.name])
2743
        elif field == "pinst_cnt":
2744
          val = len(node_to_primary[node.name])
2745
        elif field == "sinst_cnt":
2746
          val = len(node_to_secondary[node.name])
2747
        elif field == "pip":
2748
          val = node.primary_ip
2749
        elif field == "sip":
2750
          val = node.secondary_ip
2751
        elif field == "tags":
2752
          val = list(node.GetTags())
2753
        elif field == "master":
2754
          val = node.name == master_node
2755
        elif self._FIELDS_DYNAMIC.Matches(field):
2756
          val = live_data[node.name].get(field, None)
2757
        elif field == "role":
2758
          if node.name == master_node:
2759
            val = "M"
2760
          elif node.master_candidate:
2761
            val = "C"
2762
          elif node.drained:
2763
            val = "D"
2764
          elif node.offline:
2765
            val = "O"
2766
          else:
2767
            val = "R"
2768
        else:
2769
          raise errors.ParameterError(field)
2770
        node_output.append(val)
2771
      output.append(node_output)
2772

    
2773
    return output
2774

    
2775

    
2776
class LUQueryNodeVolumes(NoHooksLU):
2777
  """Logical unit for getting volumes on node(s).
2778

2779
  """
2780
  _OP_REQP = ["nodes", "output_fields"]
2781
  REQ_BGL = False
2782
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2783
  _FIELDS_STATIC = utils.FieldSet("node")
2784

    
2785
  def ExpandNames(self):
2786
    _CheckOutputFields(static=self._FIELDS_STATIC,
2787
                       dynamic=self._FIELDS_DYNAMIC,
2788
                       selected=self.op.output_fields)
2789

    
2790
    self.needed_locks = {}
2791
    self.share_locks[locking.LEVEL_NODE] = 1
2792
    if not self.op.nodes:
2793
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2794
    else:
2795
      self.needed_locks[locking.LEVEL_NODE] = \
2796
        _GetWantedNodes(self, self.op.nodes)
2797

    
2798
  def CheckPrereq(self):
2799
    """Check prerequisites.
2800

2801
    This checks that the fields required are valid output fields.
2802

2803
    """
2804
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2805

    
2806
  def Exec(self, feedback_fn):
2807
    """Computes the list of nodes and their attributes.
2808

2809
    """
2810
    nodenames = self.nodes
2811
    volumes = self.rpc.call_node_volumes(nodenames)
2812

    
2813
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
2814
             in self.cfg.GetInstanceList()]
2815

    
2816
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
2817

    
2818
    output = []
2819
    for node in nodenames:
2820
      nresult = volumes[node]
2821
      if nresult.offline:
2822
        continue
2823
      msg = nresult.fail_msg
2824
      if msg:
2825
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
2826
        continue
2827

    
2828
      node_vols = nresult.payload[:]
2829
      node_vols.sort(key=lambda vol: vol['dev'])
2830

    
2831
      for vol in node_vols:
2832
        node_output = []
2833
        for field in self.op.output_fields:
2834
          if field == "node":
2835
            val = node
2836
          elif field == "phys":
2837
            val = vol['dev']
2838
          elif field == "vg":
2839
            val = vol['vg']
2840
          elif field == "name":
2841
            val = vol['name']
2842
          elif field == "size":
2843
            val = int(float(vol['size']))
2844
          elif field == "instance":
2845
            for inst in ilist:
2846
              if node not in lv_by_node[inst]:
2847
                continue
2848
              if vol['name'] in lv_by_node[inst][node]:
2849
                val = inst.name
2850
                break
2851
            else:
2852
              val = '-'
2853
          else:
2854
            raise errors.ParameterError(field)
2855
          node_output.append(str(val))
2856

    
2857
        output.append(node_output)
2858

    
2859
    return output
2860

    
2861

    
2862
class LUQueryNodeStorage(NoHooksLU):
2863
  """Logical unit for getting information on storage units on node(s).
2864

2865
  """
2866
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
2867
  REQ_BGL = False
2868
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
2869

    
2870
  def ExpandNames(self):
2871
    storage_type = self.op.storage_type
2872

    
2873
    if storage_type not in constants.VALID_STORAGE_TYPES:
2874
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2875
                                 errors.ECODE_INVAL)
2876

    
2877
    _CheckOutputFields(static=self._FIELDS_STATIC,
2878
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
2879
                       selected=self.op.output_fields)
2880

    
2881
    self.needed_locks = {}
2882
    self.share_locks[locking.LEVEL_NODE] = 1
2883

    
2884
    if self.op.nodes:
2885
      self.needed_locks[locking.LEVEL_NODE] = \
2886
        _GetWantedNodes(self, self.op.nodes)
2887
    else:
2888
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2889

    
2890
  def CheckPrereq(self):
2891
    """Check prerequisites.
2892

2893
    This checks that the fields required are valid output fields.
2894

2895
    """
2896
    self.op.name = getattr(self.op, "name", None)
2897

    
2898
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2899

    
2900
  def Exec(self, feedback_fn):
2901
    """Computes the list of nodes and their attributes.
2902

2903
    """
2904
    # Always get name to sort by
2905
    if constants.SF_NAME in self.op.output_fields:
2906
      fields = self.op.output_fields[:]
2907
    else:
2908
      fields = [constants.SF_NAME] + self.op.output_fields
2909

    
2910
    # Never ask for node or type as it's only known to the LU
2911
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
2912
      while extra in fields:
2913
        fields.remove(extra)
2914

    
2915
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
2916
    name_idx = field_idx[constants.SF_NAME]
2917

    
2918
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2919
    data = self.rpc.call_storage_list(self.nodes,
2920
                                      self.op.storage_type, st_args,
2921
                                      self.op.name, fields)
2922

    
2923
    result = []
2924

    
2925
    for node in utils.NiceSort(self.nodes):
2926
      nresult = data[node]
2927
      if nresult.offline:
2928
        continue
2929

    
2930
      msg = nresult.fail_msg
2931
      if msg:
2932
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
2933
        continue
2934

    
2935
      rows = dict([(row[name_idx], row) for row in nresult.payload])
2936

    
2937
      for name in utils.NiceSort(rows.keys()):
2938
        row = rows[name]
2939

    
2940
        out = []
2941

    
2942
        for field in self.op.output_fields:
2943
          if field == constants.SF_NODE:
2944
            val = node
2945
          elif field == constants.SF_TYPE:
2946
            val = self.op.storage_type
2947
          elif field in field_idx:
2948
            val = row[field_idx[field]]
2949
          else:
2950
            raise errors.ParameterError(field)
2951

    
2952
          out.append(val)
2953

    
2954
        result.append(out)
2955

    
2956
    return result
2957

    
2958

    
2959
class LUModifyNodeStorage(NoHooksLU):
2960
  """Logical unit for modifying a storage volume on a node.
2961

2962
  """
2963
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
2964
  REQ_BGL = False
2965

    
2966
  def CheckArguments(self):
2967
    self.opnode_name = _ExpandNodeName(self.cfg, self.op.node_name)
2968

    
2969
    storage_type = self.op.storage_type
2970
    if storage_type not in constants.VALID_STORAGE_TYPES:
2971
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
2972
                                 errors.ECODE_INVAL)
2973

    
2974
  def ExpandNames(self):
2975
    self.needed_locks = {
2976
      locking.LEVEL_NODE: self.op.node_name,
2977
      }
2978

    
2979
  def CheckPrereq(self):
2980
    """Check prerequisites.
2981

2982
    """
2983
    storage_type = self.op.storage_type
2984

    
2985
    try:
2986
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
2987
    except KeyError:
2988
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
2989
                                 " modified" % storage_type,
2990
                                 errors.ECODE_INVAL)
2991

    
2992
    diff = set(self.op.changes.keys()) - modifiable
2993
    if diff:
2994
      raise errors.OpPrereqError("The following fields can not be modified for"
2995
                                 " storage units of type '%s': %r" %
2996
                                 (storage_type, list(diff)),
2997
                                 errors.ECODE_INVAL)
2998

    
2999
  def Exec(self, feedback_fn):
3000
    """Computes the list of nodes and their attributes.
3001

3002
    """
3003
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
3004
    result = self.rpc.call_storage_modify(self.op.node_name,
3005
                                          self.op.storage_type, st_args,
3006
                                          self.op.name, self.op.changes)
3007
    result.Raise("Failed to modify storage unit '%s' on %s" %
3008
                 (self.op.name, self.op.node_name))
3009

    
3010

    
3011
class LUAddNode(LogicalUnit):
3012
  """Logical unit for adding node to the cluster.
3013

3014
  """
3015
  HPATH = "node-add"
3016
  HTYPE = constants.HTYPE_NODE
3017
  _OP_REQP = ["node_name"]
3018

    
3019
  def CheckArguments(self):
3020
    # validate/normalize the node name
3021
    self.op.node_name = utils.HostInfo.NormalizeName(self.op.node_name)
3022

    
3023
  def BuildHooksEnv(self):
3024
    """Build hooks env.
3025

3026
    This will run on all nodes before, and on all nodes + the new node after.
3027

3028
    """
3029
    env = {
3030
      "OP_TARGET": self.op.node_name,
3031
      "NODE_NAME": self.op.node_name,
3032
      "NODE_PIP": self.op.primary_ip,
3033
      "NODE_SIP": self.op.secondary_ip,
3034
      }
3035
    nodes_0 = self.cfg.GetNodeList()
3036
    nodes_1 = nodes_0 + [self.op.node_name, ]
3037
    return env, nodes_0, nodes_1
3038

    
3039
  def CheckPrereq(self):
3040
    """Check prerequisites.
3041

3042
    This checks:
3043
     - the new node is not already in the config
3044
     - it is resolvable
3045
     - its parameters (single/dual homed) matches the cluster
3046

3047
    Any errors are signaled by raising errors.OpPrereqError.
3048

3049
    """
3050
    node_name = self.op.node_name
3051
    cfg = self.cfg
3052

    
3053
    dns_data = utils.GetHostInfo(node_name)
3054

    
3055
    node = dns_data.name
3056
    primary_ip = self.op.primary_ip = dns_data.ip
3057
    secondary_ip = getattr(self.op, "secondary_ip", None)
3058
    if secondary_ip is None:
3059
      secondary_ip = primary_ip
3060
    if not utils.IsValidIP(secondary_ip):
3061
      raise errors.OpPrereqError("Invalid secondary IP given",
3062
                                 errors.ECODE_INVAL)
3063
    self.op.secondary_ip = secondary_ip
3064

    
3065
    node_list = cfg.GetNodeList()
3066
    if not self.op.readd and node in node_list:
3067
      raise errors.OpPrereqError("Node %s is already in the configuration" %
3068
                                 node, errors.ECODE_EXISTS)
3069
    elif self.op.readd and node not in node_list:
3070
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
3071
                                 errors.ECODE_NOENT)
3072

    
3073
    for existing_node_name in node_list:
3074
      existing_node = cfg.GetNodeInfo(existing_node_name)
3075

    
3076
      if self.op.readd and node == existing_node_name:
3077
        if (existing_node.primary_ip != primary_ip or
3078
            existing_node.secondary_ip != secondary_ip):
3079
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
3080
                                     " address configuration as before",
3081
                                     errors.ECODE_INVAL)
3082
        continue
3083

    
3084
      if (existing_node.primary_ip == primary_ip or
3085
          existing_node.secondary_ip == primary_ip or
3086
          existing_node.primary_ip == secondary_ip or
3087
          existing_node.secondary_ip == secondary_ip):
3088
        raise errors.OpPrereqError("New node ip address(es) conflict with"
3089
                                   " existing node %s" % existing_node.name,
3090
                                   errors.ECODE_NOTUNIQUE)
3091

    
3092
    # check that the type of the node (single versus dual homed) is the
3093
    # same as for the master
3094
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
3095
    master_singlehomed = myself.secondary_ip == myself.primary_ip
3096
    newbie_singlehomed = secondary_ip == primary_ip
3097
    if master_singlehomed != newbie_singlehomed:
3098
      if master_singlehomed:
3099
        raise errors.OpPrereqError("The master has no private ip but the"
3100
                                   " new node has one",
3101
                                   errors.ECODE_INVAL)
3102
      else:
3103
        raise errors.OpPrereqError("The master has a private ip but the"
3104
                                   " new node doesn't have one",
3105
                                   errors.ECODE_INVAL)
3106

    
3107
    # checks reachability
3108
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
3109
      raise errors.OpPrereqError("Node not reachable by ping",
3110
                                 errors.ECODE_ENVIRON)
3111

    
3112
    if not newbie_singlehomed:
3113
      # check reachability from my secondary ip to newbie's secondary ip
3114
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
3115
                           source=myself.secondary_ip):
3116
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
3117
                                   " based ping to noded port",
3118
                                   errors.ECODE_ENVIRON)
3119

    
3120
    if self.op.readd:
3121
      exceptions = [node]
3122
    else:
3123
      exceptions = []
3124

    
3125
    self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
3126

    
3127
    if self.op.readd:
3128
      self.new_node = self.cfg.GetNodeInfo(node)
3129
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
3130
    else:
3131
      self.new_node = objects.Node(name=node,
3132
                                   primary_ip=primary_ip,
3133
                                   secondary_ip=secondary_ip,
3134
                                   master_candidate=self.master_candidate,
3135
                                   offline=False, drained=False)
3136

    
3137
  def Exec(self, feedback_fn):
3138
    """Adds the new node to the cluster.
3139

3140
    """
3141
    new_node = self.new_node
3142
    node = new_node.name
3143

    
3144
    # for re-adds, reset the offline/drained/master-candidate flags;
3145
    # we need to reset here, otherwise offline would prevent RPC calls
3146
    # later in the procedure; this also means that if the re-add
3147
    # fails, we are left with a non-offlined, broken node
3148
    if self.op.readd:
3149
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
3150
      self.LogInfo("Readding a node, the offline/drained flags were reset")
3151
      # if we demote the node, we do cleanup later in the procedure
3152
      new_node.master_candidate = self.master_candidate
3153

    
3154
    # notify the user about any possible mc promotion
3155
    if new_node.master_candidate:
3156
      self.LogInfo("Node will be a master candidate")
3157

    
3158
    # check connectivity
3159
    result = self.rpc.call_version([node])[node]
3160
    result.Raise("Can't get version information from node %s" % node)
3161
    if constants.PROTOCOL_VERSION == result.payload:
3162
      logging.info("Communication to node %s fine, sw version %s match",
3163
                   node, result.payload)
3164
    else:
3165
      raise errors.OpExecError("Version mismatch master version %s,"
3166
                               " node version %s" %
3167
                               (constants.PROTOCOL_VERSION, result.payload))
3168

    
3169
    # setup ssh on node
3170
    if self.cfg.GetClusterInfo().modify_ssh_setup:
3171
      logging.info("Copy ssh key to node %s", node)
3172
      priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
3173
      keyarray = []
3174
      keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
3175
                  constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
3176
                  priv_key, pub_key]
3177

    
3178
      for i in keyfiles:
3179
        keyarray.append(utils.ReadFile(i))
3180

    
3181
      result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
3182
                                      keyarray[2], keyarray[3], keyarray[4],
3183
                                      keyarray[5])
3184
      result.Raise("Cannot transfer ssh keys to the new node")
3185

    
3186
    # Add node to our /etc/hosts, and add key to known_hosts
3187
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3188
      utils.AddHostToEtcHosts(new_node.name)
3189

    
3190
    if new_node.secondary_ip != new_node.primary_ip:
3191
      result = self.rpc.call_node_has_ip_address(new_node.name,
3192
                                                 new_node.secondary_ip)
3193
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
3194
                   prereq=True, ecode=errors.ECODE_ENVIRON)
3195
      if not result.payload:
3196
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
3197
                                 " you gave (%s). Please fix and re-run this"
3198
                                 " command." % new_node.secondary_ip)
3199

    
3200
    node_verify_list = [self.cfg.GetMasterNode()]
3201
    node_verify_param = {
3202
      constants.NV_NODELIST: [node],
3203
      # TODO: do a node-net-test as well?
3204
    }
3205

    
3206
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
3207
                                       self.cfg.GetClusterName())
3208
    for verifier in node_verify_list:
3209
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
3210
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
3211
      if nl_payload:
3212
        for failed in nl_payload:
3213
          feedback_fn("ssh/hostname verification failed"
3214
                      " (checking from %s): %s" %
3215
                      (verifier, nl_payload[failed]))
3216
        raise errors.OpExecError("ssh/hostname verification failed.")
3217

    
3218
    if self.op.readd:
3219
      _RedistributeAncillaryFiles(self)
3220
      self.context.ReaddNode(new_node)
3221
      # make sure we redistribute the config
3222
      self.cfg.Update(new_node, feedback_fn)
3223
      # and make sure the new node will not have old files around
3224
      if not new_node.master_candidate:
3225
        result = self.rpc.call_node_demote_from_mc(new_node.name)
3226
        msg = result.fail_msg
3227
        if msg:
3228
          self.LogWarning("Node failed to demote itself from master"
3229
                          " candidate status: %s" % msg)
3230
    else:
3231
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
3232
      self.context.AddNode(new_node, self.proc.GetECId())
3233

    
3234

    
3235
class LUSetNodeParams(LogicalUnit):
3236
  """Modifies the parameters of a node.
3237

3238
  """
3239
  HPATH = "node-modify"
3240
  HTYPE = constants.HTYPE_NODE
3241
  _OP_REQP = ["node_name"]
3242
  REQ_BGL = False
3243

    
3244
  def CheckArguments(self):
3245
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3246
    _CheckBooleanOpField(self.op, 'master_candidate')
3247
    _CheckBooleanOpField(self.op, 'offline')
3248
    _CheckBooleanOpField(self.op, 'drained')
3249
    _CheckBooleanOpField(self.op, 'auto_promote')
3250
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
3251
    if all_mods.count(None) == 3:
3252
      raise errors.OpPrereqError("Please pass at least one modification",
3253
                                 errors.ECODE_INVAL)
3254
    if all_mods.count(True) > 1:
3255
      raise errors.OpPrereqError("Can't set the node into more than one"
3256
                                 " state at the same time",
3257
                                 errors.ECODE_INVAL)
3258

    
3259
    # Boolean value that tells us whether we're offlining or draining the node
3260
    self.offline_or_drain = (self.op.offline == True or
3261
                             self.op.drained == True)
3262
    self.deoffline_or_drain = (self.op.offline == False or
3263
                               self.op.drained == False)
3264
    self.might_demote = (self.op.master_candidate == False or
3265
                         self.offline_or_drain)
3266

    
3267
    self.lock_all = self.op.auto_promote and self.might_demote
3268

    
3269

    
3270
  def ExpandNames(self):
3271
    if self.lock_all:
3272
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
3273
    else:
3274
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
3275

    
3276
  def BuildHooksEnv(self):
3277
    """Build hooks env.
3278

3279
    This runs on the master node.
3280

3281
    """
3282
    env = {
3283
      "OP_TARGET": self.op.node_name,
3284
      "MASTER_CANDIDATE": str(self.op.master_candidate),
3285
      "OFFLINE": str(self.op.offline),
3286
      "DRAINED": str(self.op.drained),
3287
      }
3288
    nl = [self.cfg.GetMasterNode(),
3289
          self.op.node_name]
3290
    return env, nl, nl
3291

    
3292
  def CheckPrereq(self):
3293
    """Check prerequisites.
3294

3295
    This only checks the instance list against the existing names.
3296

3297
    """
3298
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
3299

    
3300
    if (self.op.master_candidate is not None or
3301
        self.op.drained is not None or
3302
        self.op.offline is not None):
3303
      # we can't change the master's node flags
3304
      if self.op.node_name == self.cfg.GetMasterNode():
3305
        raise errors.OpPrereqError("The master role can be changed"
3306
                                   " only via masterfailover",
3307
                                   errors.ECODE_INVAL)
3308

    
3309

    
3310
    if node.master_candidate and self.might_demote and not self.lock_all:
3311
      assert not self.op.auto_promote, "auto-promote set but lock_all not"
3312
      # check if after removing the current node, we're missing master
3313
      # candidates
3314
      (mc_remaining, mc_should, _) = \
3315
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
3316
      if mc_remaining != mc_should:
3317
        raise errors.OpPrereqError("Not enough master candidates, please"
3318
                                   " pass auto_promote to allow promotion",
3319
                                   errors.ECODE_INVAL)
3320

    
3321
    if (self.op.master_candidate == True and
3322
        ((node.offline and not self.op.offline == False) or
3323
         (node.drained and not self.op.drained == False))):
3324
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
3325
                                 " to master_candidate" % node.name,
3326
                                 errors.ECODE_INVAL)
3327

    
3328
    # If we're being deofflined/drained, we'll MC ourself if needed
3329
    if (self.deoffline_or_drain and not self.offline_or_drain and not
3330
        self.op.master_candidate == True and not node.master_candidate):
3331
      self.op.master_candidate = _DecideSelfPromotion(self)
3332
      if self.op.master_candidate:
3333
        self.LogInfo("Autopromoting node to master candidate")
3334

    
3335
    return
3336

    
3337
  def Exec(self, feedback_fn):
3338
    """Modifies a node.
3339

3340
    """
3341
    node = self.node
3342

    
3343
    result = []
3344
    changed_mc = False
3345

    
3346
    if self.op.offline is not None:
3347
      node.offline = self.op.offline
3348
      result.append(("offline", str(self.op.offline)))
3349
      if self.op.offline == True:
3350
        if node.master_candidate:
3351
          node.master_candidate = False
3352
          changed_mc = True
3353
          result.append(("master_candidate", "auto-demotion due to offline"))
3354
        if node.drained:
3355
          node.drained = False
3356
          result.append(("drained", "clear drained status due to offline"))
3357

    
3358
    if self.op.master_candidate is not None:
3359
      node.master_candidate = self.op.master_candidate
3360
      changed_mc = True
3361
      result.append(("master_candidate", str(self.op.master_candidate)))
3362
      if self.op.master_candidate == False:
3363
        rrc = self.rpc.call_node_demote_from_mc(node.name)
3364
        msg = rrc.fail_msg
3365
        if msg:
3366
          self.LogWarning("Node failed to demote itself: %s" % msg)
3367

    
3368
    if self.op.drained is not None:
3369
      node.drained = self.op.drained
3370
      result.append(("drained", str(self.op.drained)))
3371
      if self.op.drained == True:
3372
        if node.master_candidate:
3373
          node.master_candidate = False
3374
          changed_mc = True
3375
          result.append(("master_candidate", "auto-demotion due to drain"))
3376
          rrc = self.rpc.call_node_demote_from_mc(node.name)
3377
          msg = rrc.fail_msg
3378
          if msg:
3379
            self.LogWarning("Node failed to demote itself: %s" % msg)
3380
        if node.offline:
3381
          node.offline = False
3382
          result.append(("offline", "clear offline status due to drain"))
3383

    
3384
    # we locked all nodes, we adjust the CP before updating this node
3385
    if self.lock_all:
3386
      _AdjustCandidatePool(self, [node.name])
3387

    
3388
    # this will trigger configuration file update, if needed
3389
    self.cfg.Update(node, feedback_fn)
3390

    
3391
    # this will trigger job queue propagation or cleanup
3392
    if changed_mc:
3393
      self.context.ReaddNode(node)
3394

    
3395
    return result
3396

    
3397

    
3398
class LUPowercycleNode(NoHooksLU):
3399
  """Powercycles a node.
3400

3401
  """
3402
  _OP_REQP = ["node_name", "force"]
3403
  REQ_BGL = False
3404

    
3405
  def CheckArguments(self):
3406
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3407
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
3408
      raise errors.OpPrereqError("The node is the master and the force"
3409
                                 " parameter was not set",
3410
                                 errors.ECODE_INVAL)
3411

    
3412
  def ExpandNames(self):
3413
    """Locking for PowercycleNode.
3414

3415
    This is a last-resort option and shouldn't block on other
3416
    jobs. Therefore, we grab no locks.
3417

3418
    """
3419
    self.needed_locks = {}
3420

    
3421
  def CheckPrereq(self):
3422
    """Check prerequisites.
3423

3424
    This LU has no prereqs.
3425

3426
    """
3427
    pass
3428

    
3429
  def Exec(self, feedback_fn):
3430
    """Reboots a node.
3431

3432
    """
3433
    result = self.rpc.call_node_powercycle(self.op.node_name,
3434
                                           self.cfg.GetHypervisorType())
3435
    result.Raise("Failed to schedule the reboot")
3436
    return result.payload
3437

    
3438

    
3439
class LUQueryClusterInfo(NoHooksLU):
3440
  """Query cluster configuration.
3441

3442
  """
3443
  _OP_REQP = []
3444
  REQ_BGL = False
3445

    
3446
  def ExpandNames(self):
3447
    self.needed_locks = {}
3448

    
3449
  def CheckPrereq(self):
3450
    """No prerequsites needed for this LU.
3451

3452
    """
3453
    pass
3454

    
3455
  def Exec(self, feedback_fn):
3456
    """Return cluster config.
3457

3458
    """
3459
    cluster = self.cfg.GetClusterInfo()
3460
    os_hvp = {}
3461

    
3462
    # Filter just for enabled hypervisors
3463
    for os_name, hv_dict in cluster.os_hvp.items():
3464
      os_hvp[os_name] = {}
3465
      for hv_name, hv_params in hv_dict.items():
3466
        if hv_name in cluster.enabled_hypervisors:
3467
          os_hvp[os_name][hv_name] = hv_params
3468

    
3469
    result = {
3470
      "software_version": constants.RELEASE_VERSION,
3471
      "protocol_version": constants.PROTOCOL_VERSION,
3472
      "config_version": constants.CONFIG_VERSION,
3473
      "os_api_version": max(constants.OS_API_VERSIONS),
3474
      "export_version": constants.EXPORT_VERSION,
3475
      "architecture": (platform.architecture()[0], platform.machine()),
3476
      "name": cluster.cluster_name,
3477
      "master": cluster.master_node,
3478
      "default_hypervisor": cluster.enabled_hypervisors[0],
3479
      "enabled_hypervisors": cluster.enabled_hypervisors,
3480
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3481
                        for hypervisor_name in cluster.enabled_hypervisors]),
3482
      "os_hvp": os_hvp,
3483
      "beparams": cluster.beparams,
3484
      "nicparams": cluster.nicparams,
3485
      "candidate_pool_size": cluster.candidate_pool_size,
3486
      "master_netdev": cluster.master_netdev,
3487
      "volume_group_name": cluster.volume_group_name,
3488
      "file_storage_dir": cluster.file_storage_dir,
3489
      "ctime": cluster.ctime,
3490
      "mtime": cluster.mtime,
3491
      "uuid": cluster.uuid,
3492
      "tags": list(cluster.GetTags()),
3493
      }
3494

    
3495
    return result
3496

    
3497

    
3498
class LUQueryConfigValues(NoHooksLU):
3499
  """Return configuration values.
3500

3501
  """
3502
  _OP_REQP = []
3503
  REQ_BGL = False
3504
  _FIELDS_DYNAMIC = utils.FieldSet()
3505
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
3506
                                  "watcher_pause")
3507

    
3508
  def ExpandNames(self):
3509
    self.needed_locks = {}
3510

    
3511
    _CheckOutputFields(static=self._FIELDS_STATIC,
3512
                       dynamic=self._FIELDS_DYNAMIC,
3513
                       selected=self.op.output_fields)
3514

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

3518
    """
3519
    pass
3520

    
3521
  def Exec(self, feedback_fn):
3522
    """Dump a representation of the cluster config to the standard output.
3523

3524
    """
3525
    values = []
3526
    for field in self.op.output_fields:
3527
      if field == "cluster_name":
3528
        entry = self.cfg.GetClusterName()
3529
      elif field == "master_node":
3530
        entry = self.cfg.GetMasterNode()
3531
      elif field == "drain_flag":
3532
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3533
      elif field == "watcher_pause":
3534
        entry = utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
3535
      else:
3536
        raise errors.ParameterError(field)
3537
      values.append(entry)
3538
    return values
3539

    
3540

    
3541
class LUActivateInstanceDisks(NoHooksLU):
3542
  """Bring up an instance's disks.
3543

3544
  """
3545
  _OP_REQP = ["instance_name"]
3546
  REQ_BGL = False
3547

    
3548
  def ExpandNames(self):
3549
    self._ExpandAndLockInstance()
3550
    self.needed_locks[locking.LEVEL_NODE] = []
3551
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3552

    
3553
  def DeclareLocks(self, level):
3554
    if level == locking.LEVEL_NODE:
3555
      self._LockInstancesNodes()
3556

    
3557
  def CheckPrereq(self):
3558
    """Check prerequisites.
3559

3560
    This checks that the instance is in the cluster.
3561

3562
    """
3563
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3564
    assert self.instance is not None, \
3565
      "Cannot retrieve locked instance %s" % self.op.instance_name
3566
    _CheckNodeOnline(self, self.instance.primary_node)
3567
    if not hasattr(self.op, "ignore_size"):
3568
      self.op.ignore_size = False
3569

    
3570
  def Exec(self, feedback_fn):
3571
    """Activate the disks.
3572

3573
    """
3574
    disks_ok, disks_info = \
3575
              _AssembleInstanceDisks(self, self.instance,
3576
                                     ignore_size=self.op.ignore_size)
3577
    if not disks_ok:
3578
      raise errors.OpExecError("Cannot activate block devices")
3579

    
3580
    return disks_info
3581

    
3582

    
3583
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3584
                           ignore_size=False):
3585
  """Prepare the block devices for an instance.
3586

3587
  This sets up the block devices on all nodes.
3588

3589
  @type lu: L{LogicalUnit}
3590
  @param lu: the logical unit on whose behalf we execute
3591
  @type instance: L{objects.Instance}
3592
  @param instance: the instance for whose disks we assemble
3593
  @type ignore_secondaries: boolean
3594
  @param ignore_secondaries: if true, errors on secondary nodes
3595
      won't result in an error return from the function
3596
  @type ignore_size: boolean
3597
  @param ignore_size: if true, the current known size of the disk
3598
      will not be used during the disk activation, useful for cases
3599
      when the size is wrong
3600
  @return: False if the operation failed, otherwise a list of
3601
      (host, instance_visible_name, node_visible_name)
3602
      with the mapping from node devices to instance devices
3603

3604
  """
3605
  device_info = []
3606
  disks_ok = True
3607
  iname = instance.name
3608
  # With the two passes mechanism we try to reduce the window of
3609
  # opportunity for the race condition of switching DRBD to primary
3610
  # before handshaking occured, but we do not eliminate it
3611

    
3612
  # The proper fix would be to wait (with some limits) until the
3613
  # connection has been made and drbd transitions from WFConnection
3614
  # into any other network-connected state (Connected, SyncTarget,
3615
  # SyncSource, etc.)
3616

    
3617
  # 1st pass, assemble on all nodes in secondary mode
3618
  for inst_disk in instance.disks:
3619
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3620
      if ignore_size:
3621
        node_disk = node_disk.Copy()
3622
        node_disk.UnsetSize()
3623
      lu.cfg.SetDiskID(node_disk, node)
3624
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3625
      msg = result.fail_msg
3626
      if msg:
3627
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3628
                           " (is_primary=False, pass=1): %s",
3629
                           inst_disk.iv_name, node, msg)
3630
        if not ignore_secondaries:
3631
          disks_ok = False
3632

    
3633
  # FIXME: race condition on drbd migration to primary
3634

    
3635
  # 2nd pass, do only the primary node
3636
  for inst_disk in instance.disks:
3637
    dev_path = None
3638

    
3639
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3640
      if node != instance.primary_node:
3641
        continue
3642
      if ignore_size:
3643
        node_disk = node_disk.Copy()
3644
        node_disk.UnsetSize()
3645
      lu.cfg.SetDiskID(node_disk, node)
3646
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3647
      msg = result.fail_msg
3648
      if msg:
3649
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3650
                           " (is_primary=True, pass=2): %s",
3651
                           inst_disk.iv_name, node, msg)
3652
        disks_ok = False
3653
      else:
3654
        dev_path = result.payload
3655

    
3656
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
3657

    
3658
  # leave the disks configured for the primary node
3659
  # this is a workaround that would be fixed better by
3660
  # improving the logical/physical id handling
3661
  for disk in instance.disks:
3662
    lu.cfg.SetDiskID(disk, instance.primary_node)
3663

    
3664
  return disks_ok, device_info
3665

    
3666

    
3667
def _StartInstanceDisks(lu, instance, force):
3668
  """Start the disks of an instance.
3669

3670
  """
3671
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3672
                                           ignore_secondaries=force)
3673
  if not disks_ok:
3674
    _ShutdownInstanceDisks(lu, instance)
3675
    if force is not None and not force:
3676
      lu.proc.LogWarning("", hint="If the message above refers to a"
3677
                         " secondary node,"
3678
                         " you can retry the operation using '--force'.")
3679
    raise errors.OpExecError("Disk consistency error")
3680

    
3681

    
3682
class LUDeactivateInstanceDisks(NoHooksLU):
3683
  """Shutdown an instance's disks.
3684

3685
  """
3686
  _OP_REQP = ["instance_name"]
3687
  REQ_BGL = False
3688

    
3689
  def ExpandNames(self):
3690
    self._ExpandAndLockInstance()
3691
    self.needed_locks[locking.LEVEL_NODE] = []
3692
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3693

    
3694
  def DeclareLocks(self, level):
3695
    if level == locking.LEVEL_NODE:
3696
      self._LockInstancesNodes()
3697

    
3698
  def CheckPrereq(self):
3699
    """Check prerequisites.
3700

3701
    This checks that the instance is in the cluster.
3702

3703
    """
3704
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3705
    assert self.instance is not None, \
3706
      "Cannot retrieve locked instance %s" % self.op.instance_name
3707

    
3708
  def Exec(self, feedback_fn):
3709
    """Deactivate the disks
3710

3711
    """
3712
    instance = self.instance
3713
    _SafeShutdownInstanceDisks(self, instance)
3714

    
3715

    
3716
def _SafeShutdownInstanceDisks(lu, instance):
3717
  """Shutdown block devices of an instance.
3718

3719
  This function checks if an instance is running, before calling
3720
  _ShutdownInstanceDisks.
3721

3722
  """
3723
  pnode = instance.primary_node
3724
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
3725
  ins_l.Raise("Can't contact node %s" % pnode)
3726

    
3727
  if instance.name in ins_l.payload:
3728
    raise errors.OpExecError("Instance is running, can't shutdown"
3729
                             " block devices.")
3730

    
3731
  _ShutdownInstanceDisks(lu, instance)
3732

    
3733

    
3734
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3735
  """Shutdown block devices of an instance.
3736

3737
  This does the shutdown on all nodes of the instance.
3738

3739
  If the ignore_primary is false, errors on the primary node are
3740
  ignored.
3741

3742
  """
3743
  all_result = True
3744
  for disk in instance.disks:
3745
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
3746
      lu.cfg.SetDiskID(top_disk, node)
3747
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
3748
      msg = result.fail_msg
3749
      if msg:
3750
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
3751
                      disk.iv_name, node, msg)
3752
        if not ignore_primary or node != instance.primary_node:
3753
          all_result = False
3754
  return all_result
3755

    
3756

    
3757
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
3758
  """Checks if a node has enough free memory.
3759

3760
  This function check if a given node has the needed amount of free
3761
  memory. In case the node has less memory or we cannot get the
3762
  information from the node, this function raise an OpPrereqError
3763
  exception.
3764

3765
  @type lu: C{LogicalUnit}
3766
  @param lu: a logical unit from which we get configuration data
3767
  @type node: C{str}
3768
  @param node: the node to check
3769
  @type reason: C{str}
3770
  @param reason: string to use in the error message
3771
  @type requested: C{int}
3772
  @param requested: the amount of memory in MiB to check for
3773
  @type hypervisor_name: C{str}
3774
  @param hypervisor_name: the hypervisor to ask for memory stats
3775
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
3776
      we cannot check the node
3777

3778
  """
3779
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
3780
  nodeinfo[node].Raise("Can't get data from node %s" % node,
3781
                       prereq=True, ecode=errors.ECODE_ENVIRON)
3782
  free_mem = nodeinfo[node].payload.get('memory_free', None)
3783
  if not isinstance(free_mem, int):
3784
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
3785
                               " was '%s'" % (node, free_mem),
3786
                               errors.ECODE_ENVIRON)
3787
  if requested > free_mem:
3788
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
3789
                               " needed %s MiB, available %s MiB" %
3790
                               (node, reason, requested, free_mem),
3791
                               errors.ECODE_NORES)
3792

    
3793

    
3794
class LUStartupInstance(LogicalUnit):
3795
  """Starts an instance.
3796

3797
  """
3798
  HPATH = "instance-start"
3799
  HTYPE = constants.HTYPE_INSTANCE
3800
  _OP_REQP = ["instance_name", "force"]
3801
  REQ_BGL = False
3802

    
3803
  def ExpandNames(self):
3804
    self._ExpandAndLockInstance()
3805

    
3806
  def BuildHooksEnv(self):
3807
    """Build hooks env.
3808

3809
    This runs on master, primary and secondary nodes of the instance.
3810

3811
    """
3812
    env = {
3813
      "FORCE": self.op.force,
3814
      }
3815
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3816
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3817
    return env, nl, nl
3818

    
3819
  def CheckPrereq(self):
3820
    """Check prerequisites.
3821

3822
    This checks that the instance is in the cluster.
3823

3824
    """
3825
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3826
    assert self.instance is not None, \
3827
      "Cannot retrieve locked instance %s" % self.op.instance_name
3828

    
3829
    # extra beparams
3830
    self.beparams = getattr(self.op, "beparams", {})
3831
    if self.beparams:
3832
      if not isinstance(self.beparams, dict):
3833
        raise errors.OpPrereqError("Invalid beparams passed: %s, expected"
3834
                                   " dict" % (type(self.beparams), ),
3835
                                   errors.ECODE_INVAL)
3836
      # fill the beparams dict
3837
      utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
3838
      self.op.beparams = self.beparams
3839

    
3840
    # extra hvparams
3841
    self.hvparams = getattr(self.op, "hvparams", {})
3842
    if self.hvparams:
3843
      if not isinstance(self.hvparams, dict):
3844
        raise errors.OpPrereqError("Invalid hvparams passed: %s, expected"
3845
                                   " dict" % (type(self.hvparams), ),
3846
                                   errors.ECODE_INVAL)
3847

    
3848
      # check hypervisor parameter syntax (locally)
3849
      cluster = self.cfg.GetClusterInfo()
3850
      utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
3851
      filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor],
3852
                                    instance.hvparams)
3853
      filled_hvp.update(self.hvparams)
3854
      hv_type = hypervisor.GetHypervisor(instance.hypervisor)
3855
      hv_type.CheckParameterSyntax(filled_hvp)
3856
      _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
3857
      self.op.hvparams = self.hvparams
3858

    
3859
    _CheckNodeOnline(self, instance.primary_node)
3860

    
3861
    bep = self.cfg.GetClusterInfo().FillBE(instance)
3862
    # check bridges existence
3863
    _CheckInstanceBridgesExist(self, instance)
3864

    
3865
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3866
                                              instance.name,
3867
                                              instance.hypervisor)
3868
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3869
                      prereq=True, ecode=errors.ECODE_ENVIRON)
3870
    if not remote_info.payload: # not running already
3871
      _CheckNodeFreeMemory(self, instance.primary_node,
3872
                           "starting instance %s" % instance.name,
3873
                           bep[constants.BE_MEMORY], instance.hypervisor)
3874

    
3875
  def Exec(self, feedback_fn):
3876
    """Start the instance.
3877

3878
    """
3879
    instance = self.instance
3880
    force = self.op.force
3881

    
3882
    self.cfg.MarkInstanceUp(instance.name)
3883

    
3884
    node_current = instance.primary_node
3885

    
3886
    _StartInstanceDisks(self, instance, force)
3887

    
3888
    result = self.rpc.call_instance_start(node_current, instance,
3889
                                          self.hvparams, self.beparams)
3890
    msg = result.fail_msg
3891
    if msg:
3892
      _ShutdownInstanceDisks(self, instance)
3893
      raise errors.OpExecError("Could not start instance: %s" % msg)
3894

    
3895

    
3896
class LURebootInstance(LogicalUnit):
3897
  """Reboot an instance.
3898

3899
  """
3900
  HPATH = "instance-reboot"
3901
  HTYPE = constants.HTYPE_INSTANCE
3902
  _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
3903
  REQ_BGL = False
3904

    
3905
  def CheckArguments(self):
3906
    """Check the arguments.
3907

3908
    """
3909
    self.shutdown_timeout = getattr(self.op, "shutdown_timeout",
3910
                                    constants.DEFAULT_SHUTDOWN_TIMEOUT)
3911

    
3912
  def ExpandNames(self):
3913
    if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
3914
                                   constants.INSTANCE_REBOOT_HARD,
3915
                                   constants.INSTANCE_REBOOT_FULL]:
3916
      raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
3917
                                  (constants.INSTANCE_REBOOT_SOFT,
3918
                                   constants.INSTANCE_REBOOT_HARD,
3919
                                   constants.INSTANCE_REBOOT_FULL))
3920
    self._ExpandAndLockInstance()
3921

    
3922
  def BuildHooksEnv(self):
3923
    """Build hooks env.
3924

3925
    This runs on master, primary and secondary nodes of the instance.
3926

3927
    """
3928
    env = {
3929
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
3930
      "REBOOT_TYPE": self.op.reboot_type,
3931
      "SHUTDOWN_TIMEOUT": self.shutdown_timeout,
3932
      }
3933
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3934
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3935
    return env, nl, nl
3936

    
3937
  def CheckPrereq(self):
3938
    """Check prerequisites.
3939

3940
    This checks that the instance is in the cluster.
3941

3942
    """
3943
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3944
    assert self.instance is not None, \
3945
      "Cannot retrieve locked instance %s" % self.op.instance_name
3946

    
3947
    _CheckNodeOnline(self, instance.primary_node)
3948

    
3949
    # check bridges existence
3950
    _CheckInstanceBridgesExist(self, instance)
3951

    
3952
  def Exec(self, feedback_fn):
3953
    """Reboot the instance.
3954

3955
    """
3956
    instance = self.instance
3957
    ignore_secondaries = self.op.ignore_secondaries
3958
    reboot_type = self.op.reboot_type
3959

    
3960
    node_current = instance.primary_node
3961

    
3962
    if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
3963
                       constants.INSTANCE_REBOOT_HARD]:
3964
      for disk in instance.disks:
3965
        self.cfg.SetDiskID(disk, node_current)
3966
      result = self.rpc.call_instance_reboot(node_current, instance,
3967
                                             reboot_type,
3968
                                             self.shutdown_timeout)
3969
      result.Raise("Could not reboot instance")
3970
    else:
3971
      result = self.rpc.call_instance_shutdown(node_current, instance,
3972
                                               self.shutdown_timeout)
3973
      result.Raise("Could not shutdown instance for full reboot")
3974
      _ShutdownInstanceDisks(self, instance)
3975
      _StartInstanceDisks(self, instance, ignore_secondaries)
3976
      result = self.rpc.call_instance_start(node_current, instance, None, None)
3977
      msg = result.fail_msg
3978
      if msg:
3979
        _ShutdownInstanceDisks(self, instance)
3980
        raise errors.OpExecError("Could not start instance for"
3981
                                 " full reboot: %s" % msg)
3982

    
3983
    self.cfg.MarkInstanceUp(instance.name)
3984

    
3985

    
3986
class LUShutdownInstance(LogicalUnit):
3987
  """Shutdown an instance.
3988

3989
  """
3990
  HPATH = "instance-stop"
3991
  HTYPE = constants.HTYPE_INSTANCE
3992
  _OP_REQP = ["instance_name"]
3993
  REQ_BGL = False
3994

    
3995
  def CheckArguments(self):
3996
    """Check the arguments.
3997

3998
    """
3999
    self.timeout = getattr(self.op, "timeout",
4000
                           constants.DEFAULT_SHUTDOWN_TIMEOUT)
4001

    
4002
  def ExpandNames(self):
4003
    self._ExpandAndLockInstance()
4004

    
4005
  def BuildHooksEnv(self):
4006
    """Build hooks env.
4007

4008
    This runs on master, primary and secondary nodes of the instance.
4009

4010
    """
4011
    env = _BuildInstanceHookEnvByObject(self, self.instance)
4012
    env["TIMEOUT"] = self.timeout
4013
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
4014
    return env, nl, nl
4015

    
4016
  def CheckPrereq(self):
4017
    """Check prerequisites.
4018

4019
    This checks that the instance is in the cluster.
4020

4021
    """
4022
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
4023
    assert self.instance is not None, \
4024
      "Cannot retrieve locked instance %s" % self.op.instance_name
4025
    _CheckNodeOnline(self, self.instance.primary_node)
4026

    
4027
  def Exec(self, feedback_fn):
4028
    """Shutdown the instance.
4029

4030
    """
4031
    instance = self.instance
4032
    node_current = instance.primary_node
4033
    timeout = self.timeout
4034
    self.cfg.MarkInstanceDown(instance.name)