Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 8b0273a5

History | View | Annotate | Download (426.3 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 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,C0302
25

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

    
29
# C0302: since we have waaaay to many lines in this module
30

    
31
import os
32
import os.path
33
import time
34
import re
35
import platform
36
import logging
37
import copy
38
import OpenSSL
39
import socket
40
import tempfile
41
import shutil
42
import itertools
43

    
44
from ganeti import ssh
45
from ganeti import utils
46
from ganeti import errors
47
from ganeti import hypervisor
48
from ganeti import locking
49
from ganeti import constants
50
from ganeti import objects
51
from ganeti import serializer
52
from ganeti import ssconf
53
from ganeti import uidpool
54
from ganeti import compat
55
from ganeti import masterd
56
from ganeti import netutils
57
from ganeti import query
58
from ganeti import qlang
59
from ganeti import opcodes
60

    
61
import ganeti.masterd.instance # pylint: disable-msg=W0611
62

    
63

    
64
def _SupportsOob(cfg, node):
65
  """Tells if node supports OOB.
66

67
  @type cfg: L{config.ConfigWriter}
68
  @param cfg: The cluster configuration
69
  @type node: L{objects.Node}
70
  @param node: The node
71
  @return: The OOB script if supported or an empty string otherwise
72

73
  """
74
  return cfg.GetNdParams(node)[constants.ND_OOB_PROGRAM]
75

    
76

    
77
class ResultWithJobs:
78
  """Data container for LU results with jobs.
79

80
  Instances of this class returned from L{LogicalUnit.Exec} will be recognized
81
  by L{mcpu.Processor._ProcessResult}. The latter will then submit the jobs
82
  contained in the C{jobs} attribute and include the job IDs in the opcode
83
  result.
84

85
  """
86
  def __init__(self, jobs, **kwargs):
87
    """Initializes this class.
88

89
    Additional return values can be specified as keyword arguments.
90

91
    @type jobs: list of lists of L{opcode.OpCode}
92
    @param jobs: A list of lists of opcode objects
93

94
    """
95
    self.jobs = jobs
96
    self.other = kwargs
97

    
98

    
99
class LogicalUnit(object):
100
  """Logical Unit base class.
101

102
  Subclasses must follow these rules:
103
    - implement ExpandNames
104
    - implement CheckPrereq (except when tasklets are used)
105
    - implement Exec (except when tasklets are used)
106
    - implement BuildHooksEnv
107
    - implement BuildHooksNodes
108
    - redefine HPATH and HTYPE
109
    - optionally redefine their run requirements:
110
        REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
111

112
  Note that all commands require root permissions.
113

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

117
  """
118
  HPATH = None
119
  HTYPE = None
120
  REQ_BGL = True
121

    
122
  def __init__(self, processor, op, context, rpc):
123
    """Constructor for LogicalUnit.
124

125
    This needs to be overridden in derived classes in order to check op
126
    validity.
127

128
    """
129
    self.proc = processor
130
    self.op = op
131
    self.cfg = context.cfg
132
    self.context = context
133
    self.rpc = rpc
134
    # Dicts used to declare locking needs to mcpu
135
    self.needed_locks = None
136
    self.acquired_locks = {}
137
    self.share_locks = dict.fromkeys(locking.LEVELS, 0)
138
    self.add_locks = {}
139
    self.remove_locks = {}
140
    # Used to force good behavior when calling helper functions
141
    self.recalculate_locks = {}
142
    # logging
143
    self.Log = processor.Log # pylint: disable-msg=C0103
144
    self.LogWarning = processor.LogWarning # pylint: disable-msg=C0103
145
    self.LogInfo = processor.LogInfo # pylint: disable-msg=C0103
146
    self.LogStep = processor.LogStep # pylint: disable-msg=C0103
147
    # support for dry-run
148
    self.dry_run_result = None
149
    # support for generic debug attribute
150
    if (not hasattr(self.op, "debug_level") or
151
        not isinstance(self.op.debug_level, int)):
152
      self.op.debug_level = 0
153

    
154
    # Tasklets
155
    self.tasklets = None
156

    
157
    # Validate opcode parameters and set defaults
158
    self.op.Validate(True)
159

    
160
    self.CheckArguments()
161

    
162
  def CheckArguments(self):
163
    """Check syntactic validity for the opcode arguments.
164

165
    This method is for doing a simple syntactic check and ensure
166
    validity of opcode parameters, without any cluster-related
167
    checks. While the same can be accomplished in ExpandNames and/or
168
    CheckPrereq, doing these separate is better because:
169

170
      - ExpandNames is left as as purely a lock-related function
171
      - CheckPrereq is run after we have acquired locks (and possible
172
        waited for them)
173

174
    The function is allowed to change the self.op attribute so that
175
    later methods can no longer worry about missing parameters.
176

177
    """
178
    pass
179

    
180
  def ExpandNames(self):
181
    """Expand names for this LU.
182

183
    This method is called before starting to execute the opcode, and it should
184
    update all the parameters of the opcode to their canonical form (e.g. a
185
    short node name must be fully expanded after this method has successfully
186
    completed). This way locking, hooks, logging, etc. can work correctly.
187

188
    LUs which implement this method must also populate the self.needed_locks
189
    member, as a dict with lock levels as keys, and a list of needed lock names
190
    as values. Rules:
191

192
      - use an empty dict if you don't need any lock
193
      - if you don't need any lock at a particular level omit that level
194
      - don't put anything for the BGL level
195
      - if you want all locks at a level use locking.ALL_SET as a value
196

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

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

205
    Examples::
206

207
      # Acquire all nodes and one instance
208
      self.needed_locks = {
209
        locking.LEVEL_NODE: locking.ALL_SET,
210
        locking.LEVEL_INSTANCE: ['instance1.example.com'],
211
      }
212
      # Acquire just two nodes
213
      self.needed_locks = {
214
        locking.LEVEL_NODE: ['node1.example.com', 'node2.example.com'],
215
      }
216
      # Acquire no locks
217
      self.needed_locks = {} # No, you can't leave it to the default value None
218

219
    """
220
    # The implementation of this method is mandatory only if the new LU is
221
    # concurrent, so that old LUs don't need to be changed all at the same
222
    # time.
223
    if self.REQ_BGL:
224
      self.needed_locks = {} # Exclusive LUs don't need locks.
225
    else:
226
      raise NotImplementedError
227

    
228
  def DeclareLocks(self, level):
229
    """Declare LU locking needs for a level
230

231
    While most LUs can just declare their locking needs at ExpandNames time,
232
    sometimes there's the need to calculate some locks after having acquired
233
    the ones before. This function is called just before acquiring locks at a
234
    particular level, but after acquiring the ones at lower levels, and permits
235
    such calculations. It can be used to modify self.needed_locks, and by
236
    default it does nothing.
237

238
    This function is only called if you have something already set in
239
    self.needed_locks for the level.
240

241
    @param level: Locking level which is going to be locked
242
    @type level: member of ganeti.locking.LEVELS
243

244
    """
245

    
246
  def CheckPrereq(self):
247
    """Check prerequisites for this LU.
248

249
    This method should check that the prerequisites for the execution
250
    of this LU are fulfilled. It can do internode communication, but
251
    it should be idempotent - no cluster or system changes are
252
    allowed.
253

254
    The method should raise errors.OpPrereqError in case something is
255
    not fulfilled. Its return value is ignored.
256

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

260
    """
261
    if self.tasklets is not None:
262
      for (idx, tl) in enumerate(self.tasklets):
263
        logging.debug("Checking prerequisites for tasklet %s/%s",
264
                      idx + 1, len(self.tasklets))
265
        tl.CheckPrereq()
266
    else:
267
      pass
268

    
269
  def Exec(self, feedback_fn):
270
    """Execute the LU.
271

272
    This method should implement the actual work. It should raise
273
    errors.OpExecError for failures that are somewhat dealt with in
274
    code, or expected.
275

276
    """
277
    if self.tasklets is not None:
278
      for (idx, tl) in enumerate(self.tasklets):
279
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
280
        tl.Exec(feedback_fn)
281
    else:
282
      raise NotImplementedError
283

    
284
  def BuildHooksEnv(self):
285
    """Build hooks environment for this LU.
286

287
    @rtype: dict
288
    @return: Dictionary containing the environment that will be used for
289
      running the hooks for this LU. The keys of the dict must not be prefixed
290
      with "GANETI_"--that'll be added by the hooks runner. The hooks runner
291
      will extend the environment with additional variables. If no environment
292
      should be defined, an empty dictionary should be returned (not C{None}).
293
    @note: If the C{HPATH} attribute of the LU class is C{None}, this function
294
      will not be called.
295

296
    """
297
    raise NotImplementedError
298

    
299
  def BuildHooksNodes(self):
300
    """Build list of nodes to run LU's hooks.
301

302
    @rtype: tuple; (list, list)
303
    @return: Tuple containing a list of node names on which the hook
304
      should run before the execution and a list of node names on which the
305
      hook should run after the execution. No nodes should be returned as an
306
      empty list (and not None).
307
    @note: If the C{HPATH} attribute of the LU class is C{None}, this function
308
      will not be called.
309

310
    """
311
    raise NotImplementedError
312

    
313
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
314
    """Notify the LU about the results of its hooks.
315

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

322
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
323
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
324
    @param hook_results: the results of the multi-node hooks rpc call
325
    @param feedback_fn: function used send feedback back to the caller
326
    @param lu_result: the previous Exec result this LU had, or None
327
        in the PRE phase
328
    @return: the new Exec result, based on the previous result
329
        and hook results
330

331
    """
332
    # API must be kept, thus we ignore the unused argument and could
333
    # be a function warnings
334
    # pylint: disable-msg=W0613,R0201
335
    return lu_result
336

    
337
  def _ExpandAndLockInstance(self):
338
    """Helper function to expand and lock an instance.
339

340
    Many LUs that work on an instance take its name in self.op.instance_name
341
    and need to expand it and then declare the expanded name for locking. This
342
    function does it, and then updates self.op.instance_name to the expanded
343
    name. It also initializes needed_locks as a dict, if this hasn't been done
344
    before.
345

346
    """
347
    if self.needed_locks is None:
348
      self.needed_locks = {}
349
    else:
350
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
351
        "_ExpandAndLockInstance called with instance-level locks set"
352
    self.op.instance_name = _ExpandInstanceName(self.cfg,
353
                                                self.op.instance_name)
354
    self.needed_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
355

    
356
  def _LockInstancesNodes(self, primary_only=False):
357
    """Helper function to declare instances' nodes for locking.
358

359
    This function should be called after locking one or more instances to lock
360
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
361
    with all primary or secondary nodes for instances already locked and
362
    present in self.needed_locks[locking.LEVEL_INSTANCE].
363

364
    It should be called from DeclareLocks, and for safety only works if
365
    self.recalculate_locks[locking.LEVEL_NODE] is set.
366

367
    In the future it may grow parameters to just lock some instance's nodes, or
368
    to just lock primaries or secondary nodes, if needed.
369

370
    If should be called in DeclareLocks in a way similar to::
371

372
      if level == locking.LEVEL_NODE:
373
        self._LockInstancesNodes()
374

375
    @type primary_only: boolean
376
    @param primary_only: only lock primary nodes of locked instances
377

378
    """
379
    assert locking.LEVEL_NODE in self.recalculate_locks, \
380
      "_LockInstancesNodes helper function called with no nodes to recalculate"
381

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

    
384
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
385
    # future we might want to have different behaviors depending on the value
386
    # of self.recalculate_locks[locking.LEVEL_NODE]
387
    wanted_nodes = []
388
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
389
      instance = self.context.cfg.GetInstanceInfo(instance_name)
390
      wanted_nodes.append(instance.primary_node)
391
      if not primary_only:
392
        wanted_nodes.extend(instance.secondary_nodes)
393

    
394
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
395
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
396
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
397
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
398

    
399
    del self.recalculate_locks[locking.LEVEL_NODE]
400

    
401

    
402
class NoHooksLU(LogicalUnit): # pylint: disable-msg=W0223
403
  """Simple LU which runs no hooks.
404

405
  This LU is intended as a parent for other LogicalUnits which will
406
  run no hooks, in order to reduce duplicate code.
407

408
  """
409
  HPATH = None
410
  HTYPE = None
411

    
412
  def BuildHooksEnv(self):
413
    """Empty BuildHooksEnv for NoHooksLu.
414

415
    This just raises an error.
416

417
    """
418
    raise AssertionError("BuildHooksEnv called for NoHooksLUs")
419

    
420
  def BuildHooksNodes(self):
421
    """Empty BuildHooksNodes for NoHooksLU.
422

423
    """
424
    raise AssertionError("BuildHooksNodes called for NoHooksLU")
425

    
426

    
427
class Tasklet:
428
  """Tasklet base class.
429

430
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
431
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
432
  tasklets know nothing about locks.
433

434
  Subclasses must follow these rules:
435
    - Implement CheckPrereq
436
    - Implement Exec
437

438
  """
439
  def __init__(self, lu):
440
    self.lu = lu
441

    
442
    # Shortcuts
443
    self.cfg = lu.cfg
444
    self.rpc = lu.rpc
445

    
446
  def CheckPrereq(self):
447
    """Check prerequisites for this tasklets.
448

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

453
    The method should raise errors.OpPrereqError in case something is not
454
    fulfilled. Its return value is ignored.
455

456
    This method should also update all parameters to their canonical form if it
457
    hasn't been done before.
458

459
    """
460
    pass
461

    
462
  def Exec(self, feedback_fn):
463
    """Execute the tasklet.
464

465
    This method should implement the actual work. It should raise
466
    errors.OpExecError for failures that are somewhat dealt with in code, or
467
    expected.
468

469
    """
470
    raise NotImplementedError
471

    
472

    
473
class _QueryBase:
474
  """Base for query utility classes.
475

476
  """
477
  #: Attribute holding field definitions
478
  FIELDS = None
479

    
480
  def __init__(self, filter_, fields, use_locking):
481
    """Initializes this class.
482

483
    """
484
    self.use_locking = use_locking
485

    
486
    self.query = query.Query(self.FIELDS, fields, filter_=filter_,
487
                             namefield="name")
488
    self.requested_data = self.query.RequestedData()
489
    self.names = self.query.RequestedNames()
490

    
491
    # Sort only if no names were requested
492
    self.sort_by_name = not self.names
493

    
494
    self.do_locking = None
495
    self.wanted = None
496

    
497
  def _GetNames(self, lu, all_names, lock_level):
498
    """Helper function to determine names asked for in the query.
499

500
    """
501
    if self.do_locking:
502
      names = lu.acquired_locks[lock_level]
503
    else:
504
      names = all_names
505

    
506
    if self.wanted == locking.ALL_SET:
507
      assert not self.names
508
      # caller didn't specify names, so ordering is not important
509
      return utils.NiceSort(names)
510

    
511
    # caller specified names and we must keep the same order
512
    assert self.names
513
    assert not self.do_locking or lu.acquired_locks[lock_level]
514

    
515
    missing = set(self.wanted).difference(names)
516
    if missing:
517
      raise errors.OpExecError("Some items were removed before retrieving"
518
                               " their data: %s" % missing)
519

    
520
    # Return expanded names
521
    return self.wanted
522

    
523
  def ExpandNames(self, lu):
524
    """Expand names for this query.
525

526
    See L{LogicalUnit.ExpandNames}.
527

528
    """
529
    raise NotImplementedError()
530

    
531
  def DeclareLocks(self, lu, level):
532
    """Declare locks for this query.
533

534
    See L{LogicalUnit.DeclareLocks}.
535

536
    """
537
    raise NotImplementedError()
538

    
539
  def _GetQueryData(self, lu):
540
    """Collects all data for this query.
541

542
    @return: Query data object
543

544
    """
545
    raise NotImplementedError()
546

    
547
  def NewStyleQuery(self, lu):
548
    """Collect data and execute query.
549

550
    """
551
    return query.GetQueryResponse(self.query, self._GetQueryData(lu),
552
                                  sort_by_name=self.sort_by_name)
553

    
554
  def OldStyleQuery(self, lu):
555
    """Collect data and execute query.
556

557
    """
558
    return self.query.OldStyleQuery(self._GetQueryData(lu),
559
                                    sort_by_name=self.sort_by_name)
560

    
561

    
562
def _GetWantedNodes(lu, nodes):
563
  """Returns list of checked and expanded node names.
564

565
  @type lu: L{LogicalUnit}
566
  @param lu: the logical unit on whose behalf we execute
567
  @type nodes: list
568
  @param nodes: list of node names or None for all nodes
569
  @rtype: list
570
  @return: the list of nodes, sorted
571
  @raise errors.ProgrammerError: if the nodes parameter is wrong type
572

573
  """
574
  if nodes:
575
    return [_ExpandNodeName(lu.cfg, name) for name in nodes]
576

    
577
  return utils.NiceSort(lu.cfg.GetNodeList())
578

    
579

    
580
def _GetWantedInstances(lu, instances):
581
  """Returns list of checked and expanded instance names.
582

583
  @type lu: L{LogicalUnit}
584
  @param lu: the logical unit on whose behalf we execute
585
  @type instances: list
586
  @param instances: list of instance names or None for all instances
587
  @rtype: list
588
  @return: the list of instances, sorted
589
  @raise errors.OpPrereqError: if the instances parameter is wrong type
590
  @raise errors.OpPrereqError: if any of the passed instances is not found
591

592
  """
593
  if instances:
594
    wanted = [_ExpandInstanceName(lu.cfg, name) for name in instances]
595
  else:
596
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
597
  return wanted
598

    
599

    
600
def _GetUpdatedParams(old_params, update_dict,
601
                      use_default=True, use_none=False):
602
  """Return the new version of a parameter dictionary.
603

604
  @type old_params: dict
605
  @param old_params: old parameters
606
  @type update_dict: dict
607
  @param update_dict: dict containing new parameter values, or
608
      constants.VALUE_DEFAULT to reset the parameter to its default
609
      value
610
  @param use_default: boolean
611
  @type use_default: whether to recognise L{constants.VALUE_DEFAULT}
612
      values as 'to be deleted' values
613
  @param use_none: boolean
614
  @type use_none: whether to recognise C{None} values as 'to be
615
      deleted' values
616
  @rtype: dict
617
  @return: the new parameter dictionary
618

619
  """
620
  params_copy = copy.deepcopy(old_params)
621
  for key, val in update_dict.iteritems():
622
    if ((use_default and val == constants.VALUE_DEFAULT) or
623
        (use_none and val is None)):
624
      try:
625
        del params_copy[key]
626
      except KeyError:
627
        pass
628
    else:
629
      params_copy[key] = val
630
  return params_copy
631

    
632

    
633
def _ReleaseLocks(lu, level, names=None, keep=None):
634
  """Releases locks owned by an LU.
635

636
  @type lu: L{LogicalUnit}
637
  @param level: Lock level
638
  @type names: list or None
639
  @param names: Names of locks to release
640
  @type keep: list or None
641
  @param keep: Names of locks to retain
642

643
  """
644
  assert not (keep is not None and names is not None), \
645
         "Only one of the 'names' and the 'keep' parameters can be given"
646

    
647
  if names is not None:
648
    should_release = names.__contains__
649
  elif keep:
650
    should_release = lambda name: name not in keep
651
  else:
652
    should_release = None
653

    
654
  if should_release:
655
    retain = []
656
    release = []
657

    
658
    # Determine which locks to release
659
    for name in lu.acquired_locks[level]:
660
      if should_release(name):
661
        release.append(name)
662
      else:
663
        retain.append(name)
664

    
665
    assert len(lu.acquired_locks[level]) == (len(retain) + len(release))
666

    
667
    # Release just some locks
668
    lu.context.glm.release(level, names=release)
669
    lu.acquired_locks[level] = retain
670

    
671
    assert frozenset(lu.context.glm.list_owned(level)) == frozenset(retain)
672
  else:
673
    # Release everything
674
    lu.context.glm.release(level)
675
    del lu.acquired_locks[level]
676

    
677
    assert not lu.context.glm.list_owned(level), "No locks should be owned"
678

    
679

    
680
def _RunPostHook(lu, node_name):
681
  """Runs the post-hook for an opcode on a single node.
682

683
  """
684
  hm = lu.proc.hmclass(lu.rpc.call_hooks_runner, lu)
685
  try:
686
    hm.RunPhase(constants.HOOKS_PHASE_POST, nodes=[node_name])
687
  except:
688
    # pylint: disable-msg=W0702
689
    lu.LogWarning("Errors occurred running hooks on %s" % node_name)
690

    
691

    
692
def _CheckOutputFields(static, dynamic, selected):
693
  """Checks whether all selected fields are valid.
694

695
  @type static: L{utils.FieldSet}
696
  @param static: static fields set
697
  @type dynamic: L{utils.FieldSet}
698
  @param dynamic: dynamic fields set
699

700
  """
701
  f = utils.FieldSet()
702
  f.Extend(static)
703
  f.Extend(dynamic)
704

    
705
  delta = f.NonMatching(selected)
706
  if delta:
707
    raise errors.OpPrereqError("Unknown output fields selected: %s"
708
                               % ",".join(delta), errors.ECODE_INVAL)
709

    
710

    
711
def _CheckGlobalHvParams(params):
712
  """Validates that given hypervisor params are not global ones.
713

714
  This will ensure that instances don't get customised versions of
715
  global params.
716

717
  """
718
  used_globals = constants.HVC_GLOBALS.intersection(params)
719
  if used_globals:
720
    msg = ("The following hypervisor parameters are global and cannot"
721
           " be customized at instance level, please modify them at"
722
           " cluster level: %s" % utils.CommaJoin(used_globals))
723
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
724

    
725

    
726
def _CheckNodeOnline(lu, node, msg=None):
727
  """Ensure that a given node is online.
728

729
  @param lu: the LU on behalf of which we make the check
730
  @param node: the node to check
731
  @param msg: if passed, should be a message to replace the default one
732
  @raise errors.OpPrereqError: if the node is offline
733

734
  """
735
  if msg is None:
736
    msg = "Can't use offline node"
737
  if lu.cfg.GetNodeInfo(node).offline:
738
    raise errors.OpPrereqError("%s: %s" % (msg, node), errors.ECODE_STATE)
739

    
740

    
741
def _CheckNodeNotDrained(lu, node):
742
  """Ensure that a given node is not drained.
743

744
  @param lu: the LU on behalf of which we make the check
745
  @param node: the node to check
746
  @raise errors.OpPrereqError: if the node is drained
747

748
  """
749
  if lu.cfg.GetNodeInfo(node).drained:
750
    raise errors.OpPrereqError("Can't use drained node %s" % node,
751
                               errors.ECODE_STATE)
752

    
753

    
754
def _CheckNodeVmCapable(lu, node):
755
  """Ensure that a given node is vm capable.
756

757
  @param lu: the LU on behalf of which we make the check
758
  @param node: the node to check
759
  @raise errors.OpPrereqError: if the node is not vm capable
760

761
  """
762
  if not lu.cfg.GetNodeInfo(node).vm_capable:
763
    raise errors.OpPrereqError("Can't use non-vm_capable node %s" % node,
764
                               errors.ECODE_STATE)
765

    
766

    
767
def _CheckNodeHasOS(lu, node, os_name, force_variant):
768
  """Ensure that a node supports a given OS.
769

770
  @param lu: the LU on behalf of which we make the check
771
  @param node: the node to check
772
  @param os_name: the OS to query about
773
  @param force_variant: whether to ignore variant errors
774
  @raise errors.OpPrereqError: if the node is not supporting the OS
775

776
  """
777
  result = lu.rpc.call_os_get(node, os_name)
778
  result.Raise("OS '%s' not in supported OS list for node %s" %
779
               (os_name, node),
780
               prereq=True, ecode=errors.ECODE_INVAL)
781
  if not force_variant:
782
    _CheckOSVariant(result.payload, os_name)
783

    
784

    
785
def _CheckNodeHasSecondaryIP(lu, node, secondary_ip, prereq):
786
  """Ensure that a node has the given secondary ip.
787

788
  @type lu: L{LogicalUnit}
789
  @param lu: the LU on behalf of which we make the check
790
  @type node: string
791
  @param node: the node to check
792
  @type secondary_ip: string
793
  @param secondary_ip: the ip to check
794
  @type prereq: boolean
795
  @param prereq: whether to throw a prerequisite or an execute error
796
  @raise errors.OpPrereqError: if the node doesn't have the ip, and prereq=True
797
  @raise errors.OpExecError: if the node doesn't have the ip, and prereq=False
798

799
  """
800
  result = lu.rpc.call_node_has_ip_address(node, secondary_ip)
801
  result.Raise("Failure checking secondary ip on node %s" % node,
802
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
803
  if not result.payload:
804
    msg = ("Node claims it doesn't have the secondary ip you gave (%s),"
805
           " please fix and re-run this command" % secondary_ip)
806
    if prereq:
807
      raise errors.OpPrereqError(msg, errors.ECODE_ENVIRON)
808
    else:
809
      raise errors.OpExecError(msg)
810

    
811

    
812
def _GetClusterDomainSecret():
813
  """Reads the cluster domain secret.
814

815
  """
816
  return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
817
                               strict=True)
818

    
819

    
820
def _CheckInstanceDown(lu, instance, reason):
821
  """Ensure that an instance is not running."""
822
  if instance.admin_up:
823
    raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
824
                               (instance.name, reason), errors.ECODE_STATE)
825

    
826
  pnode = instance.primary_node
827
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
828
  ins_l.Raise("Can't contact node %s for instance information" % pnode,
829
              prereq=True, ecode=errors.ECODE_ENVIRON)
830

    
831
  if instance.name in ins_l.payload:
832
    raise errors.OpPrereqError("Instance %s is running, %s" %
833
                               (instance.name, reason), errors.ECODE_STATE)
834

    
835

    
836
def _ExpandItemName(fn, name, kind):
837
  """Expand an item name.
838

839
  @param fn: the function to use for expansion
840
  @param name: requested item name
841
  @param kind: text description ('Node' or 'Instance')
842
  @return: the resolved (full) name
843
  @raise errors.OpPrereqError: if the item is not found
844

845
  """
846
  full_name = fn(name)
847
  if full_name is None:
848
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
849
                               errors.ECODE_NOENT)
850
  return full_name
851

    
852

    
853
def _ExpandNodeName(cfg, name):
854
  """Wrapper over L{_ExpandItemName} for nodes."""
855
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
856

    
857

    
858
def _ExpandInstanceName(cfg, name):
859
  """Wrapper over L{_ExpandItemName} for instance."""
860
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
861

    
862

    
863
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
864
                          memory, vcpus, nics, disk_template, disks,
865
                          bep, hvp, hypervisor_name):
866
  """Builds instance related env variables for hooks
867

868
  This builds the hook environment from individual variables.
869

870
  @type name: string
871
  @param name: the name of the instance
872
  @type primary_node: string
873
  @param primary_node: the name of the instance's primary node
874
  @type secondary_nodes: list
875
  @param secondary_nodes: list of secondary nodes as strings
876
  @type os_type: string
877
  @param os_type: the name of the instance's OS
878
  @type status: boolean
879
  @param status: the should_run status of the instance
880
  @type memory: string
881
  @param memory: the memory size of the instance
882
  @type vcpus: string
883
  @param vcpus: the count of VCPUs the instance has
884
  @type nics: list
885
  @param nics: list of tuples (ip, mac, mode, link) representing
886
      the NICs the instance has
887
  @type disk_template: string
888
  @param disk_template: the disk template of the instance
889
  @type disks: list
890
  @param disks: the list of (size, mode) pairs
891
  @type bep: dict
892
  @param bep: the backend parameters for the instance
893
  @type hvp: dict
894
  @param hvp: the hypervisor parameters for the instance
895
  @type hypervisor_name: string
896
  @param hypervisor_name: the hypervisor for the instance
897
  @rtype: dict
898
  @return: the hook environment for this instance
899

900
  """
901
  if status:
902
    str_status = "up"
903
  else:
904
    str_status = "down"
905
  env = {
906
    "OP_TARGET": name,
907
    "INSTANCE_NAME": name,
908
    "INSTANCE_PRIMARY": primary_node,
909
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
910
    "INSTANCE_OS_TYPE": os_type,
911
    "INSTANCE_STATUS": str_status,
912
    "INSTANCE_MEMORY": memory,
913
    "INSTANCE_VCPUS": vcpus,
914
    "INSTANCE_DISK_TEMPLATE": disk_template,
915
    "INSTANCE_HYPERVISOR": hypervisor_name,
916
  }
917

    
918
  if nics:
919
    nic_count = len(nics)
920
    for idx, (ip, mac, mode, link) in enumerate(nics):
921
      if ip is None:
922
        ip = ""
923
      env["INSTANCE_NIC%d_IP" % idx] = ip
924
      env["INSTANCE_NIC%d_MAC" % idx] = mac
925
      env["INSTANCE_NIC%d_MODE" % idx] = mode
926
      env["INSTANCE_NIC%d_LINK" % idx] = link
927
      if mode == constants.NIC_MODE_BRIDGED:
928
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
929
  else:
930
    nic_count = 0
931

    
932
  env["INSTANCE_NIC_COUNT"] = nic_count
933

    
934
  if disks:
935
    disk_count = len(disks)
936
    for idx, (size, mode) in enumerate(disks):
937
      env["INSTANCE_DISK%d_SIZE" % idx] = size
938
      env["INSTANCE_DISK%d_MODE" % idx] = mode
939
  else:
940
    disk_count = 0
941

    
942
  env["INSTANCE_DISK_COUNT"] = disk_count
943

    
944
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
945
    for key, value in source.items():
946
      env["INSTANCE_%s_%s" % (kind, key)] = value
947

    
948
  return env
949

    
950

    
951
def _NICListToTuple(lu, nics):
952
  """Build a list of nic information tuples.
953

954
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
955
  value in LUInstanceQueryData.
956

957
  @type lu:  L{LogicalUnit}
958
  @param lu: the logical unit on whose behalf we execute
959
  @type nics: list of L{objects.NIC}
960
  @param nics: list of nics to convert to hooks tuples
961

962
  """
963
  hooks_nics = []
964
  cluster = lu.cfg.GetClusterInfo()
965
  for nic in nics:
966
    ip = nic.ip
967
    mac = nic.mac
968
    filled_params = cluster.SimpleFillNIC(nic.nicparams)
969
    mode = filled_params[constants.NIC_MODE]
970
    link = filled_params[constants.NIC_LINK]
971
    hooks_nics.append((ip, mac, mode, link))
972
  return hooks_nics
973

    
974

    
975
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
976
  """Builds instance related env variables for hooks from an object.
977

978
  @type lu: L{LogicalUnit}
979
  @param lu: the logical unit on whose behalf we execute
980
  @type instance: L{objects.Instance}
981
  @param instance: the instance for which we should build the
982
      environment
983
  @type override: dict
984
  @param override: dictionary with key/values that will override
985
      our values
986
  @rtype: dict
987
  @return: the hook environment dictionary
988

989
  """
990
  cluster = lu.cfg.GetClusterInfo()
991
  bep = cluster.FillBE(instance)
992
  hvp = cluster.FillHV(instance)
993
  args = {
994
    'name': instance.name,
995
    'primary_node': instance.primary_node,
996
    'secondary_nodes': instance.secondary_nodes,
997
    'os_type': instance.os,
998
    'status': instance.admin_up,
999
    'memory': bep[constants.BE_MEMORY],
1000
    'vcpus': bep[constants.BE_VCPUS],
1001
    'nics': _NICListToTuple(lu, instance.nics),
1002
    'disk_template': instance.disk_template,
1003
    'disks': [(disk.size, disk.mode) for disk in instance.disks],
1004
    'bep': bep,
1005
    'hvp': hvp,
1006
    'hypervisor_name': instance.hypervisor,
1007
  }
1008
  if override:
1009
    args.update(override)
1010
  return _BuildInstanceHookEnv(**args) # pylint: disable-msg=W0142
1011

    
1012

    
1013
def _AdjustCandidatePool(lu, exceptions):
1014
  """Adjust the candidate pool after node operations.
1015

1016
  """
1017
  mod_list = lu.cfg.MaintainCandidatePool(exceptions)
1018
  if mod_list:
1019
    lu.LogInfo("Promoted nodes to master candidate role: %s",
1020
               utils.CommaJoin(node.name for node in mod_list))
1021
    for name in mod_list:
1022
      lu.context.ReaddNode(name)
1023
  mc_now, mc_max, _ = lu.cfg.GetMasterCandidateStats(exceptions)
1024
  if mc_now > mc_max:
1025
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
1026
               (mc_now, mc_max))
1027

    
1028

    
1029
def _DecideSelfPromotion(lu, exceptions=None):
1030
  """Decide whether I should promote myself as a master candidate.
1031

1032
  """
1033
  cp_size = lu.cfg.GetClusterInfo().candidate_pool_size
1034
  mc_now, mc_should, _ = lu.cfg.GetMasterCandidateStats(exceptions)
1035
  # the new node will increase mc_max with one, so:
1036
  mc_should = min(mc_should + 1, cp_size)
1037
  return mc_now < mc_should
1038

    
1039

    
1040
def _CheckNicsBridgesExist(lu, target_nics, target_node):
1041
  """Check that the brigdes needed by a list of nics exist.
1042

1043
  """
1044
  cluster = lu.cfg.GetClusterInfo()
1045
  paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics]
1046
  brlist = [params[constants.NIC_LINK] for params in paramslist
1047
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
1048
  if brlist:
1049
    result = lu.rpc.call_bridges_exist(target_node, brlist)
1050
    result.Raise("Error checking bridges on destination node '%s'" %
1051
                 target_node, prereq=True, ecode=errors.ECODE_ENVIRON)
1052

    
1053

    
1054
def _CheckInstanceBridgesExist(lu, instance, node=None):
1055
  """Check that the brigdes needed by an instance exist.
1056

1057
  """
1058
  if node is None:
1059
    node = instance.primary_node
1060
  _CheckNicsBridgesExist(lu, instance.nics, node)
1061

    
1062

    
1063
def _CheckOSVariant(os_obj, name):
1064
  """Check whether an OS name conforms to the os variants specification.
1065

1066
  @type os_obj: L{objects.OS}
1067
  @param os_obj: OS object to check
1068
  @type name: string
1069
  @param name: OS name passed by the user, to check for validity
1070

1071
  """
1072
  if not os_obj.supported_variants:
1073
    return
1074
  variant = objects.OS.GetVariant(name)
1075
  if not variant:
1076
    raise errors.OpPrereqError("OS name must include a variant",
1077
                               errors.ECODE_INVAL)
1078

    
1079
  if variant not in os_obj.supported_variants:
1080
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
1081

    
1082

    
1083
def _GetNodeInstancesInner(cfg, fn):
1084
  return [i for i in cfg.GetAllInstancesInfo().values() if fn(i)]
1085

    
1086

    
1087
def _GetNodeInstances(cfg, node_name):
1088
  """Returns a list of all primary and secondary instances on a node.
1089

1090
  """
1091

    
1092
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
1093

    
1094

    
1095
def _GetNodePrimaryInstances(cfg, node_name):
1096
  """Returns primary instances on a node.
1097

1098
  """
1099
  return _GetNodeInstancesInner(cfg,
1100
                                lambda inst: node_name == inst.primary_node)
1101

    
1102

    
1103
def _GetNodeSecondaryInstances(cfg, node_name):
1104
  """Returns secondary instances on a node.
1105

1106
  """
1107
  return _GetNodeInstancesInner(cfg,
1108
                                lambda inst: node_name in inst.secondary_nodes)
1109

    
1110

    
1111
def _GetStorageTypeArgs(cfg, storage_type):
1112
  """Returns the arguments for a storage type.
1113

1114
  """
1115
  # Special case for file storage
1116
  if storage_type == constants.ST_FILE:
1117
    # storage.FileStorage wants a list of storage directories
1118
    return [[cfg.GetFileStorageDir(), cfg.GetSharedFileStorageDir()]]
1119

    
1120
  return []
1121

    
1122

    
1123
def _FindFaultyInstanceDisks(cfg, rpc, instance, node_name, prereq):
1124
  faulty = []
1125

    
1126
  for dev in instance.disks:
1127
    cfg.SetDiskID(dev, node_name)
1128

    
1129
  result = rpc.call_blockdev_getmirrorstatus(node_name, instance.disks)
1130
  result.Raise("Failed to get disk status from node %s" % node_name,
1131
               prereq=prereq, ecode=errors.ECODE_ENVIRON)
1132

    
1133
  for idx, bdev_status in enumerate(result.payload):
1134
    if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
1135
      faulty.append(idx)
1136

    
1137
  return faulty
1138

    
1139

    
1140
def _CheckIAllocatorOrNode(lu, iallocator_slot, node_slot):
1141
  """Check the sanity of iallocator and node arguments and use the
1142
  cluster-wide iallocator if appropriate.
1143

1144
  Check that at most one of (iallocator, node) is specified. If none is
1145
  specified, then the LU's opcode's iallocator slot is filled with the
1146
  cluster-wide default iallocator.
1147

1148
  @type iallocator_slot: string
1149
  @param iallocator_slot: the name of the opcode iallocator slot
1150
  @type node_slot: string
1151
  @param node_slot: the name of the opcode target node slot
1152

1153
  """
1154
  node = getattr(lu.op, node_slot, None)
1155
  iallocator = getattr(lu.op, iallocator_slot, None)
1156

    
1157
  if node is not None and iallocator is not None:
1158
    raise errors.OpPrereqError("Do not specify both, iallocator and node.",
1159
                               errors.ECODE_INVAL)
1160
  elif node is None and iallocator is None:
1161
    default_iallocator = lu.cfg.GetDefaultIAllocator()
1162
    if default_iallocator:
1163
      setattr(lu.op, iallocator_slot, default_iallocator)
1164
    else:
1165
      raise errors.OpPrereqError("No iallocator or node given and no"
1166
                                 " cluster-wide default iallocator found."
1167
                                 " Please specify either an iallocator or a"
1168
                                 " node, or set a cluster-wide default"
1169
                                 " iallocator.")
1170

    
1171

    
1172
class LUClusterPostInit(LogicalUnit):
1173
  """Logical unit for running hooks after cluster initialization.
1174

1175
  """
1176
  HPATH = "cluster-init"
1177
  HTYPE = constants.HTYPE_CLUSTER
1178

    
1179
  def BuildHooksEnv(self):
1180
    """Build hooks env.
1181

1182
    """
1183
    return {
1184
      "OP_TARGET": self.cfg.GetClusterName(),
1185
      }
1186

    
1187
  def BuildHooksNodes(self):
1188
    """Build hooks nodes.
1189

1190
    """
1191
    return ([], [self.cfg.GetMasterNode()])
1192

    
1193
  def Exec(self, feedback_fn):
1194
    """Nothing to do.
1195

1196
    """
1197
    return True
1198

    
1199

    
1200
class LUClusterDestroy(LogicalUnit):
1201
  """Logical unit for destroying the cluster.
1202

1203
  """
1204
  HPATH = "cluster-destroy"
1205
  HTYPE = constants.HTYPE_CLUSTER
1206

    
1207
  def BuildHooksEnv(self):
1208
    """Build hooks env.
1209

1210
    """
1211
    return {
1212
      "OP_TARGET": self.cfg.GetClusterName(),
1213
      }
1214

    
1215
  def BuildHooksNodes(self):
1216
    """Build hooks nodes.
1217

1218
    """
1219
    return ([], [])
1220

    
1221
  def CheckPrereq(self):
1222
    """Check prerequisites.
1223

1224
    This checks whether the cluster is empty.
1225

1226
    Any errors are signaled by raising errors.OpPrereqError.
1227

1228
    """
1229
    master = self.cfg.GetMasterNode()
1230

    
1231
    nodelist = self.cfg.GetNodeList()
1232
    if len(nodelist) != 1 or nodelist[0] != master:
1233
      raise errors.OpPrereqError("There are still %d node(s) in"
1234
                                 " this cluster." % (len(nodelist) - 1),
1235
                                 errors.ECODE_INVAL)
1236
    instancelist = self.cfg.GetInstanceList()
1237
    if instancelist:
1238
      raise errors.OpPrereqError("There are still %d instance(s) in"
1239
                                 " this cluster." % len(instancelist),
1240
                                 errors.ECODE_INVAL)
1241

    
1242
  def Exec(self, feedback_fn):
1243
    """Destroys the cluster.
1244

1245
    """
1246
    master = self.cfg.GetMasterNode()
1247

    
1248
    # Run post hooks on master node before it's removed
1249
    _RunPostHook(self, master)
1250

    
1251
    result = self.rpc.call_node_stop_master(master, False)
1252
    result.Raise("Could not disable the master role")
1253

    
1254
    return master
1255

    
1256

    
1257
def _VerifyCertificate(filename):
1258
  """Verifies a certificate for LUClusterVerify.
1259

1260
  @type filename: string
1261
  @param filename: Path to PEM file
1262

1263
  """
1264
  try:
1265
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1266
                                           utils.ReadFile(filename))
1267
  except Exception, err: # pylint: disable-msg=W0703
1268
    return (LUClusterVerify.ETYPE_ERROR,
1269
            "Failed to load X509 certificate %s: %s" % (filename, err))
1270

    
1271
  (errcode, msg) = \
1272
    utils.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN,
1273
                                constants.SSL_CERT_EXPIRATION_ERROR)
1274

    
1275
  if msg:
1276
    fnamemsg = "While verifying %s: %s" % (filename, msg)
1277
  else:
1278
    fnamemsg = None
1279

    
1280
  if errcode is None:
1281
    return (None, fnamemsg)
1282
  elif errcode == utils.CERT_WARNING:
1283
    return (LUClusterVerify.ETYPE_WARNING, fnamemsg)
1284
  elif errcode == utils.CERT_ERROR:
1285
    return (LUClusterVerify.ETYPE_ERROR, fnamemsg)
1286

    
1287
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1288

    
1289

    
1290
class LUClusterVerify(LogicalUnit):
1291
  """Verifies the cluster status.
1292

1293
  """
1294
  HPATH = "cluster-verify"
1295
  HTYPE = constants.HTYPE_CLUSTER
1296
  REQ_BGL = False
1297

    
1298
  TCLUSTER = "cluster"
1299
  TNODE = "node"
1300
  TINSTANCE = "instance"
1301

    
1302
  ECLUSTERCFG = (TCLUSTER, "ECLUSTERCFG")
1303
  ECLUSTERCERT = (TCLUSTER, "ECLUSTERCERT")
1304
  ECLUSTERFILECHECK = (TCLUSTER, "ECLUSTERFILECHECK")
1305
  EINSTANCEBADNODE = (TINSTANCE, "EINSTANCEBADNODE")
1306
  EINSTANCEDOWN = (TINSTANCE, "EINSTANCEDOWN")
1307
  EINSTANCELAYOUT = (TINSTANCE, "EINSTANCELAYOUT")
1308
  EINSTANCEMISSINGDISK = (TINSTANCE, "EINSTANCEMISSINGDISK")
1309
  EINSTANCEFAULTYDISK = (TINSTANCE, "EINSTANCEFAULTYDISK")
1310
  EINSTANCEWRONGNODE = (TINSTANCE, "EINSTANCEWRONGNODE")
1311
  EINSTANCESPLITGROUPS = (TINSTANCE, "EINSTANCESPLITGROUPS")
1312
  ENODEDRBD = (TNODE, "ENODEDRBD")
1313
  ENODEDRBDHELPER = (TNODE, "ENODEDRBDHELPER")
1314
  ENODEFILECHECK = (TNODE, "ENODEFILECHECK")
1315
  ENODEHOOKS = (TNODE, "ENODEHOOKS")
1316
  ENODEHV = (TNODE, "ENODEHV")
1317
  ENODELVM = (TNODE, "ENODELVM")
1318
  ENODEN1 = (TNODE, "ENODEN1")
1319
  ENODENET = (TNODE, "ENODENET")
1320
  ENODEOS = (TNODE, "ENODEOS")
1321
  ENODEORPHANINSTANCE = (TNODE, "ENODEORPHANINSTANCE")
1322
  ENODEORPHANLV = (TNODE, "ENODEORPHANLV")
1323
  ENODERPC = (TNODE, "ENODERPC")
1324
  ENODESSH = (TNODE, "ENODESSH")
1325
  ENODEVERSION = (TNODE, "ENODEVERSION")
1326
  ENODESETUP = (TNODE, "ENODESETUP")
1327
  ENODETIME = (TNODE, "ENODETIME")
1328
  ENODEOOBPATH = (TNODE, "ENODEOOBPATH")
1329

    
1330
  ETYPE_FIELD = "code"
1331
  ETYPE_ERROR = "ERROR"
1332
  ETYPE_WARNING = "WARNING"
1333

    
1334
  _HOOKS_INDENT_RE = re.compile("^", re.M)
1335

    
1336
  class NodeImage(object):
1337
    """A class representing the logical and physical status of a node.
1338

1339
    @type name: string
1340
    @ivar name: the node name to which this object refers
1341
    @ivar volumes: a structure as returned from
1342
        L{ganeti.backend.GetVolumeList} (runtime)
1343
    @ivar instances: a list of running instances (runtime)
1344
    @ivar pinst: list of configured primary instances (config)
1345
    @ivar sinst: list of configured secondary instances (config)
1346
    @ivar sbp: dictionary of {primary-node: list of instances} for all
1347
        instances for which this node is secondary (config)
1348
    @ivar mfree: free memory, as reported by hypervisor (runtime)
1349
    @ivar dfree: free disk, as reported by the node (runtime)
1350
    @ivar offline: the offline status (config)
1351
    @type rpc_fail: boolean
1352
    @ivar rpc_fail: whether the RPC verify call was successfull (overall,
1353
        not whether the individual keys were correct) (runtime)
1354
    @type lvm_fail: boolean
1355
    @ivar lvm_fail: whether the RPC call didn't return valid LVM data
1356
    @type hyp_fail: boolean
1357
    @ivar hyp_fail: whether the RPC call didn't return the instance list
1358
    @type ghost: boolean
1359
    @ivar ghost: whether this is a known node or not (config)
1360
    @type os_fail: boolean
1361
    @ivar os_fail: whether the RPC call didn't return valid OS data
1362
    @type oslist: list
1363
    @ivar oslist: list of OSes as diagnosed by DiagnoseOS
1364
    @type vm_capable: boolean
1365
    @ivar vm_capable: whether the node can host instances
1366

1367
    """
1368
    def __init__(self, offline=False, name=None, vm_capable=True):
1369
      self.name = name
1370
      self.volumes = {}
1371
      self.instances = []
1372
      self.pinst = []
1373
      self.sinst = []
1374
      self.sbp = {}
1375
      self.mfree = 0
1376
      self.dfree = 0
1377
      self.offline = offline
1378
      self.vm_capable = vm_capable
1379
      self.rpc_fail = False
1380
      self.lvm_fail = False
1381
      self.hyp_fail = False
1382
      self.ghost = False
1383
      self.os_fail = False
1384
      self.oslist = {}
1385

    
1386
  def ExpandNames(self):
1387
    self.needed_locks = {
1388
      locking.LEVEL_NODE: locking.ALL_SET,
1389
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1390
    }
1391
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1392

    
1393
  def _Error(self, ecode, item, msg, *args, **kwargs):
1394
    """Format an error message.
1395

1396
    Based on the opcode's error_codes parameter, either format a
1397
    parseable error code, or a simpler error string.
1398

1399
    This must be called only from Exec and functions called from Exec.
1400

1401
    """
1402
    ltype = kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR)
1403
    itype, etxt = ecode
1404
    # first complete the msg
1405
    if args:
1406
      msg = msg % args
1407
    # then format the whole message
1408
    if self.op.error_codes:
1409
      msg = "%s:%s:%s:%s:%s" % (ltype, etxt, itype, item, msg)
1410
    else:
1411
      if item:
1412
        item = " " + item
1413
      else:
1414
        item = ""
1415
      msg = "%s: %s%s: %s" % (ltype, itype, item, msg)
1416
    # and finally report it via the feedback_fn
1417
    self._feedback_fn("  - %s" % msg)
1418

    
1419
  def _ErrorIf(self, cond, *args, **kwargs):
1420
    """Log an error message if the passed condition is True.
1421

1422
    """
1423
    cond = bool(cond) or self.op.debug_simulate_errors
1424
    if cond:
1425
      self._Error(*args, **kwargs)
1426
    # do not mark the operation as failed for WARN cases only
1427
    if kwargs.get(self.ETYPE_FIELD, self.ETYPE_ERROR) == self.ETYPE_ERROR:
1428
      self.bad = self.bad or cond
1429

    
1430
  def _VerifyNode(self, ninfo, nresult):
1431
    """Perform some basic validation on data returned from a node.
1432

1433
      - check the result data structure is well formed and has all the
1434
        mandatory fields
1435
      - check ganeti version
1436

1437
    @type ninfo: L{objects.Node}
1438
    @param ninfo: the node to check
1439
    @param nresult: the results from the node
1440
    @rtype: boolean
1441
    @return: whether overall this call was successful (and we can expect
1442
         reasonable values in the respose)
1443

1444
    """
1445
    node = ninfo.name
1446
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1447

    
1448
    # main result, nresult should be a non-empty dict
1449
    test = not nresult or not isinstance(nresult, dict)
1450
    _ErrorIf(test, self.ENODERPC, node,
1451
                  "unable to verify node: no data returned")
1452
    if test:
1453
      return False
1454

    
1455
    # compares ganeti version
1456
    local_version = constants.PROTOCOL_VERSION
1457
    remote_version = nresult.get("version", None)
1458
    test = not (remote_version and
1459
                isinstance(remote_version, (list, tuple)) and
1460
                len(remote_version) == 2)
1461
    _ErrorIf(test, self.ENODERPC, node,
1462
             "connection to node returned invalid data")
1463
    if test:
1464
      return False
1465

    
1466
    test = local_version != remote_version[0]
1467
    _ErrorIf(test, self.ENODEVERSION, node,
1468
             "incompatible protocol versions: master %s,"
1469
             " node %s", local_version, remote_version[0])
1470
    if test:
1471
      return False
1472

    
1473
    # node seems compatible, we can actually try to look into its results
1474

    
1475
    # full package version
1476
    self._ErrorIf(constants.RELEASE_VERSION != remote_version[1],
1477
                  self.ENODEVERSION, node,
1478
                  "software version mismatch: master %s, node %s",
1479
                  constants.RELEASE_VERSION, remote_version[1],
1480
                  code=self.ETYPE_WARNING)
1481

    
1482
    hyp_result = nresult.get(constants.NV_HYPERVISOR, None)
1483
    if ninfo.vm_capable and isinstance(hyp_result, dict):
1484
      for hv_name, hv_result in hyp_result.iteritems():
1485
        test = hv_result is not None
1486
        _ErrorIf(test, self.ENODEHV, node,
1487
                 "hypervisor %s verify failure: '%s'", hv_name, hv_result)
1488

    
1489
    hvp_result = nresult.get(constants.NV_HVPARAMS, None)
1490
    if ninfo.vm_capable and isinstance(hvp_result, list):
1491
      for item, hv_name, hv_result in hvp_result:
1492
        _ErrorIf(True, self.ENODEHV, node,
1493
                 "hypervisor %s parameter verify failure (source %s): %s",
1494
                 hv_name, item, hv_result)
1495

    
1496
    test = nresult.get(constants.NV_NODESETUP,
1497
                       ["Missing NODESETUP results"])
1498
    _ErrorIf(test, self.ENODESETUP, node, "node setup error: %s",
1499
             "; ".join(test))
1500

    
1501
    return True
1502

    
1503
  def _VerifyNodeTime(self, ninfo, nresult,
1504
                      nvinfo_starttime, nvinfo_endtime):
1505
    """Check the node time.
1506

1507
    @type ninfo: L{objects.Node}
1508
    @param ninfo: the node to check
1509
    @param nresult: the remote results for the node
1510
    @param nvinfo_starttime: the start time of the RPC call
1511
    @param nvinfo_endtime: the end time of the RPC call
1512

1513
    """
1514
    node = ninfo.name
1515
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1516

    
1517
    ntime = nresult.get(constants.NV_TIME, None)
1518
    try:
1519
      ntime_merged = utils.MergeTime(ntime)
1520
    except (ValueError, TypeError):
1521
      _ErrorIf(True, self.ENODETIME, node, "Node returned invalid time")
1522
      return
1523

    
1524
    if ntime_merged < (nvinfo_starttime - constants.NODE_MAX_CLOCK_SKEW):
1525
      ntime_diff = "%.01fs" % abs(nvinfo_starttime - ntime_merged)
1526
    elif ntime_merged > (nvinfo_endtime + constants.NODE_MAX_CLOCK_SKEW):
1527
      ntime_diff = "%.01fs" % abs(ntime_merged - nvinfo_endtime)
1528
    else:
1529
      ntime_diff = None
1530

    
1531
    _ErrorIf(ntime_diff is not None, self.ENODETIME, node,
1532
             "Node time diverges by at least %s from master node time",
1533
             ntime_diff)
1534

    
1535
  def _VerifyNodeLVM(self, ninfo, nresult, vg_name):
1536
    """Check the node time.
1537

1538
    @type ninfo: L{objects.Node}
1539
    @param ninfo: the node to check
1540
    @param nresult: the remote results for the node
1541
    @param vg_name: the configured VG name
1542

1543
    """
1544
    if vg_name is None:
1545
      return
1546

    
1547
    node = ninfo.name
1548
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1549

    
1550
    # checks vg existence and size > 20G
1551
    vglist = nresult.get(constants.NV_VGLIST, None)
1552
    test = not vglist
1553
    _ErrorIf(test, self.ENODELVM, node, "unable to check volume groups")
1554
    if not test:
1555
      vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
1556
                                            constants.MIN_VG_SIZE)
1557
      _ErrorIf(vgstatus, self.ENODELVM, node, vgstatus)
1558

    
1559
    # check pv names
1560
    pvlist = nresult.get(constants.NV_PVLIST, None)
1561
    test = pvlist is None
1562
    _ErrorIf(test, self.ENODELVM, node, "Can't get PV list from node")
1563
    if not test:
1564
      # check that ':' is not present in PV names, since it's a
1565
      # special character for lvcreate (denotes the range of PEs to
1566
      # use on the PV)
1567
      for _, pvname, owner_vg in pvlist:
1568
        test = ":" in pvname
1569
        _ErrorIf(test, self.ENODELVM, node, "Invalid character ':' in PV"
1570
                 " '%s' of VG '%s'", pvname, owner_vg)
1571

    
1572
  def _VerifyNodeNetwork(self, ninfo, nresult):
1573
    """Check the node time.
1574

1575
    @type ninfo: L{objects.Node}
1576
    @param ninfo: the node to check
1577
    @param nresult: the remote results for the node
1578

1579
    """
1580
    node = ninfo.name
1581
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1582

    
1583
    test = constants.NV_NODELIST not in nresult
1584
    _ErrorIf(test, self.ENODESSH, node,
1585
             "node hasn't returned node ssh connectivity data")
1586
    if not test:
1587
      if nresult[constants.NV_NODELIST]:
1588
        for a_node, a_msg in nresult[constants.NV_NODELIST].items():
1589
          _ErrorIf(True, self.ENODESSH, node,
1590
                   "ssh communication with node '%s': %s", a_node, a_msg)
1591

    
1592
    test = constants.NV_NODENETTEST not in nresult
1593
    _ErrorIf(test, self.ENODENET, node,
1594
             "node hasn't returned node tcp connectivity data")
1595
    if not test:
1596
      if nresult[constants.NV_NODENETTEST]:
1597
        nlist = utils.NiceSort(nresult[constants.NV_NODENETTEST].keys())
1598
        for anode in nlist:
1599
          _ErrorIf(True, self.ENODENET, node,
1600
                   "tcp communication with node '%s': %s",
1601
                   anode, nresult[constants.NV_NODENETTEST][anode])
1602

    
1603
    test = constants.NV_MASTERIP not in nresult
1604
    _ErrorIf(test, self.ENODENET, node,
1605
             "node hasn't returned node master IP reachability data")
1606
    if not test:
1607
      if not nresult[constants.NV_MASTERIP]:
1608
        if node == self.master_node:
1609
          msg = "the master node cannot reach the master IP (not configured?)"
1610
        else:
1611
          msg = "cannot reach the master IP"
1612
        _ErrorIf(True, self.ENODENET, node, msg)
1613

    
1614
  def _VerifyInstance(self, instance, instanceconfig, node_image,
1615
                      diskstatus):
1616
    """Verify an instance.
1617

1618
    This function checks to see if the required block devices are
1619
    available on the instance's node.
1620

1621
    """
1622
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1623
    node_current = instanceconfig.primary_node
1624

    
1625
    node_vol_should = {}
1626
    instanceconfig.MapLVsByNode(node_vol_should)
1627

    
1628
    for node in node_vol_should:
1629
      n_img = node_image[node]
1630
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1631
        # ignore missing volumes on offline or broken nodes
1632
        continue
1633
      for volume in node_vol_should[node]:
1634
        test = volume not in n_img.volumes
1635
        _ErrorIf(test, self.EINSTANCEMISSINGDISK, instance,
1636
                 "volume %s missing on node %s", volume, node)
1637

    
1638
    if instanceconfig.admin_up:
1639
      pri_img = node_image[node_current]
1640
      test = instance not in pri_img.instances and not pri_img.offline
1641
      _ErrorIf(test, self.EINSTANCEDOWN, instance,
1642
               "instance not running on its primary node %s",
1643
               node_current)
1644

    
1645
    for node, n_img in node_image.items():
1646
      if node != node_current:
1647
        test = instance in n_img.instances
1648
        _ErrorIf(test, self.EINSTANCEWRONGNODE, instance,
1649
                 "instance should not run on node %s", node)
1650

    
1651
    diskdata = [(nname, success, status, idx)
1652
                for (nname, disks) in diskstatus.items()
1653
                for idx, (success, status) in enumerate(disks)]
1654

    
1655
    for nname, success, bdev_status, idx in diskdata:
1656
      # the 'ghost node' construction in Exec() ensures that we have a
1657
      # node here
1658
      snode = node_image[nname]
1659
      bad_snode = snode.ghost or snode.offline
1660
      _ErrorIf(instanceconfig.admin_up and not success and not bad_snode,
1661
               self.EINSTANCEFAULTYDISK, instance,
1662
               "couldn't retrieve status for disk/%s on %s: %s",
1663
               idx, nname, bdev_status)
1664
      _ErrorIf((instanceconfig.admin_up and success and
1665
                bdev_status.ldisk_status == constants.LDS_FAULTY),
1666
               self.EINSTANCEFAULTYDISK, instance,
1667
               "disk/%s on %s is faulty", idx, nname)
1668

    
1669
  def _VerifyOrphanVolumes(self, node_vol_should, node_image, reserved):
1670
    """Verify if there are any unknown volumes in the cluster.
1671

1672
    The .os, .swap and backup volumes are ignored. All other volumes are
1673
    reported as unknown.
1674

1675
    @type reserved: L{ganeti.utils.FieldSet}
1676
    @param reserved: a FieldSet of reserved volume names
1677

1678
    """
1679
    for node, n_img in node_image.items():
1680
      if n_img.offline or n_img.rpc_fail or n_img.lvm_fail:
1681
        # skip non-healthy nodes
1682
        continue
1683
      for volume in n_img.volumes:
1684
        test = ((node not in node_vol_should or
1685
                volume not in node_vol_should[node]) and
1686
                not reserved.Matches(volume))
1687
        self._ErrorIf(test, self.ENODEORPHANLV, node,
1688
                      "volume %s is unknown", volume)
1689

    
1690
  def _VerifyOrphanInstances(self, instancelist, node_image):
1691
    """Verify the list of running instances.
1692

1693
    This checks what instances are running but unknown to the cluster.
1694

1695
    """
1696
    for node, n_img in node_image.items():
1697
      for o_inst in n_img.instances:
1698
        test = o_inst not in instancelist
1699
        self._ErrorIf(test, self.ENODEORPHANINSTANCE, node,
1700
                      "instance %s on node %s should not exist", o_inst, node)
1701

    
1702
  def _VerifyNPlusOneMemory(self, node_image, instance_cfg):
1703
    """Verify N+1 Memory Resilience.
1704

1705
    Check that if one single node dies we can still start all the
1706
    instances it was primary for.
1707

1708
    """
1709
    cluster_info = self.cfg.GetClusterInfo()
1710
    for node, n_img in node_image.items():
1711
      # This code checks that every node which is now listed as
1712
      # secondary has enough memory to host all instances it is
1713
      # supposed to should a single other node in the cluster fail.
1714
      # FIXME: not ready for failover to an arbitrary node
1715
      # FIXME: does not support file-backed instances
1716
      # WARNING: we currently take into account down instances as well
1717
      # as up ones, considering that even if they're down someone
1718
      # might want to start them even in the event of a node failure.
1719
      if n_img.offline:
1720
        # we're skipping offline nodes from the N+1 warning, since
1721
        # most likely we don't have good memory infromation from them;
1722
        # we already list instances living on such nodes, and that's
1723
        # enough warning
1724
        continue
1725
      for prinode, instances in n_img.sbp.items():
1726
        needed_mem = 0
1727
        for instance in instances:
1728
          bep = cluster_info.FillBE(instance_cfg[instance])
1729
          if bep[constants.BE_AUTO_BALANCE]:
1730
            needed_mem += bep[constants.BE_MEMORY]
1731
        test = n_img.mfree < needed_mem
1732
        self._ErrorIf(test, self.ENODEN1, node,
1733
                      "not enough memory to accomodate instance failovers"
1734
                      " should node %s fail (%dMiB needed, %dMiB available)",
1735
                      prinode, needed_mem, n_img.mfree)
1736

    
1737
  @classmethod
1738
  def _VerifyFiles(cls, errorif, nodeinfo, master_node, all_nvinfo,
1739
                   (files_all, files_all_opt, files_mc, files_vm)):
1740
    """Verifies file checksums collected from all nodes.
1741

1742
    @param errorif: Callback for reporting errors
1743
    @param nodeinfo: List of L{objects.Node} objects
1744
    @param master_node: Name of master node
1745
    @param all_nvinfo: RPC results
1746

1747
    """
1748
    node_names = frozenset(node.name for node in nodeinfo)
1749

    
1750
    assert master_node in node_names
1751
    assert (len(files_all | files_all_opt | files_mc | files_vm) ==
1752
            sum(map(len, [files_all, files_all_opt, files_mc, files_vm]))), \
1753
           "Found file listed in more than one file list"
1754

    
1755
    # Define functions determining which nodes to consider for a file
1756
    file2nodefn = dict([(filename, fn)
1757
      for (files, fn) in [(files_all, None),
1758
                          (files_all_opt, None),
1759
                          (files_mc, lambda node: (node.master_candidate or
1760
                                                   node.name == master_node)),
1761
                          (files_vm, lambda node: node.vm_capable)]
1762
      for filename in files])
1763

    
1764
    fileinfo = dict((filename, {}) for filename in file2nodefn.keys())
1765

    
1766
    for node in nodeinfo:
1767
      nresult = all_nvinfo[node.name]
1768

    
1769
      if nresult.fail_msg or not nresult.payload:
1770
        node_files = None
1771
      else:
1772
        node_files = nresult.payload.get(constants.NV_FILELIST, None)
1773

    
1774
      test = not (node_files and isinstance(node_files, dict))
1775
      errorif(test, cls.ENODEFILECHECK, node.name,
1776
              "Node did not return file checksum data")
1777
      if test:
1778
        continue
1779

    
1780
      for (filename, checksum) in node_files.items():
1781
        # Check if the file should be considered for a node
1782
        fn = file2nodefn[filename]
1783
        if fn is None or fn(node):
1784
          fileinfo[filename].setdefault(checksum, set()).add(node.name)
1785

    
1786
    for (filename, checksums) in fileinfo.items():
1787
      assert compat.all(len(i) > 10 for i in checksums), "Invalid checksum"
1788

    
1789
      # Nodes having the file
1790
      with_file = frozenset(node_name
1791
                            for nodes in fileinfo[filename].values()
1792
                            for node_name in nodes)
1793

    
1794
      # Nodes missing file
1795
      missing_file = node_names - with_file
1796

    
1797
      if filename in files_all_opt:
1798
        # All or no nodes
1799
        errorif(missing_file and missing_file != node_names,
1800
                cls.ECLUSTERFILECHECK, None,
1801
                "File %s is optional, but it must exist on all or no nodes (not"
1802
                " found on %s)",
1803
                filename, utils.CommaJoin(utils.NiceSort(missing_file)))
1804
      else:
1805
        errorif(missing_file, cls.ECLUSTERFILECHECK, None,
1806
                "File %s is missing from node(s) %s", filename,
1807
                utils.CommaJoin(utils.NiceSort(missing_file)))
1808

    
1809
      # See if there are multiple versions of the file
1810
      test = len(checksums) > 1
1811
      if test:
1812
        variants = ["variant %s on %s" %
1813
                    (idx + 1, utils.CommaJoin(utils.NiceSort(nodes)))
1814
                    for (idx, (checksum, nodes)) in
1815
                      enumerate(sorted(checksums.items()))]
1816
      else:
1817
        variants = []
1818

    
1819
      errorif(test, cls.ECLUSTERFILECHECK, None,
1820
              "File %s found with %s different checksums (%s)",
1821
              filename, len(checksums), "; ".join(variants))
1822

    
1823
  def _VerifyNodeDrbd(self, ninfo, nresult, instanceinfo, drbd_helper,
1824
                      drbd_map):
1825
    """Verifies and the node DRBD status.
1826

1827
    @type ninfo: L{objects.Node}
1828
    @param ninfo: the node to check
1829
    @param nresult: the remote results for the node
1830
    @param instanceinfo: the dict of instances
1831
    @param drbd_helper: the configured DRBD usermode helper
1832
    @param drbd_map: the DRBD map as returned by
1833
        L{ganeti.config.ConfigWriter.ComputeDRBDMap}
1834

1835
    """
1836
    node = ninfo.name
1837
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1838

    
1839
    if drbd_helper:
1840
      helper_result = nresult.get(constants.NV_DRBDHELPER, None)
1841
      test = (helper_result == None)
1842
      _ErrorIf(test, self.ENODEDRBDHELPER, node,
1843
               "no drbd usermode helper returned")
1844
      if helper_result:
1845
        status, payload = helper_result
1846
        test = not status
1847
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1848
                 "drbd usermode helper check unsuccessful: %s", payload)
1849
        test = status and (payload != drbd_helper)
1850
        _ErrorIf(test, self.ENODEDRBDHELPER, node,
1851
                 "wrong drbd usermode helper: %s", payload)
1852

    
1853
    # compute the DRBD minors
1854
    node_drbd = {}
1855
    for minor, instance in drbd_map[node].items():
1856
      test = instance not in instanceinfo
1857
      _ErrorIf(test, self.ECLUSTERCFG, None,
1858
               "ghost instance '%s' in temporary DRBD map", instance)
1859
        # ghost instance should not be running, but otherwise we
1860
        # don't give double warnings (both ghost instance and
1861
        # unallocated minor in use)
1862
      if test:
1863
        node_drbd[minor] = (instance, False)
1864
      else:
1865
        instance = instanceinfo[instance]
1866
        node_drbd[minor] = (instance.name, instance.admin_up)
1867

    
1868
    # and now check them
1869
    used_minors = nresult.get(constants.NV_DRBDLIST, [])
1870
    test = not isinstance(used_minors, (tuple, list))
1871
    _ErrorIf(test, self.ENODEDRBD, node,
1872
             "cannot parse drbd status file: %s", str(used_minors))
1873
    if test:
1874
      # we cannot check drbd status
1875
      return
1876

    
1877
    for minor, (iname, must_exist) in node_drbd.items():
1878
      test = minor not in used_minors and must_exist
1879
      _ErrorIf(test, self.ENODEDRBD, node,
1880
               "drbd minor %d of instance %s is not active", minor, iname)
1881
    for minor in used_minors:
1882
      test = minor not in node_drbd
1883
      _ErrorIf(test, self.ENODEDRBD, node,
1884
               "unallocated drbd minor %d is in use", minor)
1885

    
1886
  def _UpdateNodeOS(self, ninfo, nresult, nimg):
1887
    """Builds the node OS structures.
1888

1889
    @type ninfo: L{objects.Node}
1890
    @param ninfo: the node to check
1891
    @param nresult: the remote results for the node
1892
    @param nimg: the node image object
1893

1894
    """
1895
    node = ninfo.name
1896
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1897

    
1898
    remote_os = nresult.get(constants.NV_OSLIST, None)
1899
    test = (not isinstance(remote_os, list) or
1900
            not compat.all(isinstance(v, list) and len(v) == 7
1901
                           for v in remote_os))
1902

    
1903
    _ErrorIf(test, self.ENODEOS, node,
1904
             "node hasn't returned valid OS data")
1905

    
1906
    nimg.os_fail = test
1907

    
1908
    if test:
1909
      return
1910

    
1911
    os_dict = {}
1912

    
1913
    for (name, os_path, status, diagnose,
1914
         variants, parameters, api_ver) in nresult[constants.NV_OSLIST]:
1915

    
1916
      if name not in os_dict:
1917
        os_dict[name] = []
1918

    
1919
      # parameters is a list of lists instead of list of tuples due to
1920
      # JSON lacking a real tuple type, fix it:
1921
      parameters = [tuple(v) for v in parameters]
1922
      os_dict[name].append((os_path, status, diagnose,
1923
                            set(variants), set(parameters), set(api_ver)))
1924

    
1925
    nimg.oslist = os_dict
1926

    
1927
  def _VerifyNodeOS(self, ninfo, nimg, base):
1928
    """Verifies the node OS list.
1929

1930
    @type ninfo: L{objects.Node}
1931
    @param ninfo: the node to check
1932
    @param nimg: the node image object
1933
    @param base: the 'template' node we match against (e.g. from the master)
1934

1935
    """
1936
    node = ninfo.name
1937
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1938

    
1939
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1940

    
1941
    beautify_params = lambda l: ["%s: %s" % (k, v) for (k, v) in l]
1942
    for os_name, os_data in nimg.oslist.items():
1943
      assert os_data, "Empty OS status for OS %s?!" % os_name
1944
      f_path, f_status, f_diag, f_var, f_param, f_api = os_data[0]
1945
      _ErrorIf(not f_status, self.ENODEOS, node,
1946
               "Invalid OS %s (located at %s): %s", os_name, f_path, f_diag)
1947
      _ErrorIf(len(os_data) > 1, self.ENODEOS, node,
1948
               "OS '%s' has multiple entries (first one shadows the rest): %s",
1949
               os_name, utils.CommaJoin([v[0] for v in os_data]))
1950
      # this will catched in backend too
1951
      _ErrorIf(compat.any(v >= constants.OS_API_V15 for v in f_api)
1952
               and not f_var, self.ENODEOS, node,
1953
               "OS %s with API at least %d does not declare any variant",
1954
               os_name, constants.OS_API_V15)
1955
      # comparisons with the 'base' image
1956
      test = os_name not in base.oslist
1957
      _ErrorIf(test, self.ENODEOS, node,
1958
               "Extra OS %s not present on reference node (%s)",
1959
               os_name, base.name)
1960
      if test:
1961
        continue
1962
      assert base.oslist[os_name], "Base node has empty OS status?"
1963
      _, b_status, _, b_var, b_param, b_api = base.oslist[os_name][0]
1964
      if not b_status:
1965
        # base OS is invalid, skipping
1966
        continue
1967
      for kind, a, b in [("API version", f_api, b_api),
1968
                         ("variants list", f_var, b_var),
1969
                         ("parameters", beautify_params(f_param),
1970
                          beautify_params(b_param))]:
1971
        _ErrorIf(a != b, self.ENODEOS, node,
1972
                 "OS %s for %s differs from reference node %s: [%s] vs. [%s]",
1973
                 kind, os_name, base.name,
1974
                 utils.CommaJoin(sorted(a)), utils.CommaJoin(sorted(b)))
1975

    
1976
    # check any missing OSes
1977
    missing = set(base.oslist.keys()).difference(nimg.oslist.keys())
1978
    _ErrorIf(missing, self.ENODEOS, node,
1979
             "OSes present on reference node %s but missing on this node: %s",
1980
             base.name, utils.CommaJoin(missing))
1981

    
1982
  def _VerifyOob(self, ninfo, nresult):
1983
    """Verifies out of band functionality of a node.
1984

1985
    @type ninfo: L{objects.Node}
1986
    @param ninfo: the node to check
1987
    @param nresult: the remote results for the node
1988

1989
    """
1990
    node = ninfo.name
1991
    # We just have to verify the paths on master and/or master candidates
1992
    # as the oob helper is invoked on the master
1993
    if ((ninfo.master_candidate or ninfo.master_capable) and
1994
        constants.NV_OOB_PATHS in nresult):
1995
      for path_result in nresult[constants.NV_OOB_PATHS]:
1996
        self._ErrorIf(path_result, self.ENODEOOBPATH, node, path_result)
1997

    
1998
  def _UpdateNodeVolumes(self, ninfo, nresult, nimg, vg_name):
1999
    """Verifies and updates the node volume data.
2000

2001
    This function will update a L{NodeImage}'s internal structures
2002
    with data from the remote call.
2003

2004
    @type ninfo: L{objects.Node}
2005
    @param ninfo: the node to check
2006
    @param nresult: the remote results for the node
2007
    @param nimg: the node image object
2008
    @param vg_name: the configured VG name
2009

2010
    """
2011
    node = ninfo.name
2012
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2013

    
2014
    nimg.lvm_fail = True
2015
    lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
2016
    if vg_name is None:
2017
      pass
2018
    elif isinstance(lvdata, basestring):
2019
      _ErrorIf(True, self.ENODELVM, node, "LVM problem on node: %s",
2020
               utils.SafeEncode(lvdata))
2021
    elif not isinstance(lvdata, dict):
2022
      _ErrorIf(True, self.ENODELVM, node, "rpc call to node failed (lvlist)")
2023
    else:
2024
      nimg.volumes = lvdata
2025
      nimg.lvm_fail = False
2026

    
2027
  def _UpdateNodeInstances(self, ninfo, nresult, nimg):
2028
    """Verifies and updates the node instance list.
2029

2030
    If the listing was successful, then updates this node's instance
2031
    list. Otherwise, it marks the RPC call as failed for the instance
2032
    list key.
2033

2034
    @type ninfo: L{objects.Node}
2035
    @param ninfo: the node to check
2036
    @param nresult: the remote results for the node
2037
    @param nimg: the node image object
2038

2039
    """
2040
    idata = nresult.get(constants.NV_INSTANCELIST, None)
2041
    test = not isinstance(idata, list)
2042
    self._ErrorIf(test, self.ENODEHV, ninfo.name, "rpc call to node failed"
2043
                  " (instancelist): %s", utils.SafeEncode(str(idata)))
2044
    if test:
2045
      nimg.hyp_fail = True
2046
    else:
2047
      nimg.instances = idata
2048

    
2049
  def _UpdateNodeInfo(self, ninfo, nresult, nimg, vg_name):
2050
    """Verifies and computes a node information map
2051

2052
    @type ninfo: L{objects.Node}
2053
    @param ninfo: the node to check
2054
    @param nresult: the remote results for the node
2055
    @param nimg: the node image object
2056
    @param vg_name: the configured VG name
2057

2058
    """
2059
    node = ninfo.name
2060
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2061

    
2062
    # try to read free memory (from the hypervisor)
2063
    hv_info = nresult.get(constants.NV_HVINFO, None)
2064
    test = not isinstance(hv_info, dict) or "memory_free" not in hv_info
2065
    _ErrorIf(test, self.ENODEHV, node, "rpc call to node failed (hvinfo)")
2066
    if not test:
2067
      try:
2068
        nimg.mfree = int(hv_info["memory_free"])
2069
      except (ValueError, TypeError):
2070
        _ErrorIf(True, self.ENODERPC, node,
2071
                 "node returned invalid nodeinfo, check hypervisor")
2072

    
2073
    # FIXME: devise a free space model for file based instances as well
2074
    if vg_name is not None:
2075
      test = (constants.NV_VGLIST not in nresult or
2076
              vg_name not in nresult[constants.NV_VGLIST])
2077
      _ErrorIf(test, self.ENODELVM, node,
2078
               "node didn't return data for the volume group '%s'"
2079
               " - it is either missing or broken", vg_name)
2080
      if not test:
2081
        try:
2082
          nimg.dfree = int(nresult[constants.NV_VGLIST][vg_name])
2083
        except (ValueError, TypeError):
2084
          _ErrorIf(True, self.ENODERPC, node,
2085
                   "node returned invalid LVM info, check LVM status")
2086

    
2087
  def _CollectDiskInfo(self, nodelist, node_image, instanceinfo):
2088
    """Gets per-disk status information for all instances.
2089

2090
    @type nodelist: list of strings
2091
    @param nodelist: Node names
2092
    @type node_image: dict of (name, L{objects.Node})
2093
    @param node_image: Node objects
2094
    @type instanceinfo: dict of (name, L{objects.Instance})
2095
    @param instanceinfo: Instance objects
2096
    @rtype: {instance: {node: [(succes, payload)]}}
2097
    @return: a dictionary of per-instance dictionaries with nodes as
2098
        keys and disk information as values; the disk information is a
2099
        list of tuples (success, payload)
2100

2101
    """
2102
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2103

    
2104
    node_disks = {}
2105
    node_disks_devonly = {}
2106
    diskless_instances = set()
2107
    diskless = constants.DT_DISKLESS
2108

    
2109
    for nname in nodelist:
2110
      node_instances = list(itertools.chain(node_image[nname].pinst,
2111
                                            node_image[nname].sinst))
2112
      diskless_instances.update(inst for inst in node_instances
2113
                                if instanceinfo[inst].disk_template == diskless)
2114
      disks = [(inst, disk)
2115
               for inst in node_instances
2116
               for disk in instanceinfo[inst].disks]
2117

    
2118
      if not disks:
2119
        # No need to collect data
2120
        continue
2121

    
2122
      node_disks[nname] = disks
2123

    
2124
      # Creating copies as SetDiskID below will modify the objects and that can
2125
      # lead to incorrect data returned from nodes
2126
      devonly = [dev.Copy() for (_, dev) in disks]
2127

    
2128
      for dev in devonly:
2129
        self.cfg.SetDiskID(dev, nname)
2130

    
2131
      node_disks_devonly[nname] = devonly
2132

    
2133
    assert len(node_disks) == len(node_disks_devonly)
2134

    
2135
    # Collect data from all nodes with disks
2136
    result = self.rpc.call_blockdev_getmirrorstatus_multi(node_disks.keys(),
2137
                                                          node_disks_devonly)
2138

    
2139
    assert len(result) == len(node_disks)
2140

    
2141
    instdisk = {}
2142

    
2143
    for (nname, nres) in result.items():
2144
      disks = node_disks[nname]
2145

    
2146
      if nres.offline:
2147
        # No data from this node
2148
        data = len(disks) * [(False, "node offline")]
2149
      else:
2150
        msg = nres.fail_msg
2151
        _ErrorIf(msg, self.ENODERPC, nname,
2152
                 "while getting disk information: %s", msg)
2153
        if msg:
2154
          # No data from this node
2155
          data = len(disks) * [(False, msg)]
2156
        else:
2157
          data = []
2158
          for idx, i in enumerate(nres.payload):
2159
            if isinstance(i, (tuple, list)) and len(i) == 2:
2160
              data.append(i)
2161
            else:
2162
              logging.warning("Invalid result from node %s, entry %d: %s",
2163
                              nname, idx, i)
2164
              data.append((False, "Invalid result from the remote node"))
2165

    
2166
      for ((inst, _), status) in zip(disks, data):
2167
        instdisk.setdefault(inst, {}).setdefault(nname, []).append(status)
2168

    
2169
    # Add empty entries for diskless instances.
2170
    for inst in diskless_instances:
2171
      assert inst not in instdisk
2172
      instdisk[inst] = {}
2173

    
2174
    assert compat.all(len(statuses) == len(instanceinfo[inst].disks) and
2175
                      len(nnames) <= len(instanceinfo[inst].all_nodes) and
2176
                      compat.all(isinstance(s, (tuple, list)) and
2177
                                 len(s) == 2 for s in statuses)
2178
                      for inst, nnames in instdisk.items()
2179
                      for nname, statuses in nnames.items())
2180
    assert set(instdisk) == set(instanceinfo), "instdisk consistency failure"
2181

    
2182
    return instdisk
2183

    
2184
  def _VerifyHVP(self, hvp_data):
2185
    """Verifies locally the syntax of the hypervisor parameters.
2186

2187
    """
2188
    for item, hv_name, hv_params in hvp_data:
2189
      msg = ("hypervisor %s parameters syntax check (source %s): %%s" %
2190
             (item, hv_name))
2191
      try:
2192
        hv_class = hypervisor.GetHypervisor(hv_name)
2193
        utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
2194
        hv_class.CheckParameterSyntax(hv_params)
2195
      except errors.GenericError, err:
2196
        self._ErrorIf(True, self.ECLUSTERCFG, None, msg % str(err))
2197

    
2198
  def BuildHooksEnv(self):
2199
    """Build hooks env.
2200

2201
    Cluster-Verify hooks just ran in the post phase and their failure makes
2202
    the output be logged in the verify output and the verification to fail.
2203

2204
    """
2205
    cfg = self.cfg
2206

    
2207
    env = {
2208
      "CLUSTER_TAGS": " ".join(cfg.GetClusterInfo().GetTags())
2209
      }
2210

    
2211
    env.update(("NODE_TAGS_%s" % node.name, " ".join(node.GetTags()))
2212
               for node in cfg.GetAllNodesInfo().values())
2213

    
2214
    return env
2215

    
2216
  def BuildHooksNodes(self):
2217
    """Build hooks nodes.
2218

2219
    """
2220
    return ([], self.cfg.GetNodeList())
2221

    
2222
  def Exec(self, feedback_fn):
2223
    """Verify integrity of cluster, performing various test on nodes.
2224

2225
    """
2226
    # This method has too many local variables. pylint: disable-msg=R0914
2227
    self.bad = False
2228
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2229
    verbose = self.op.verbose
2230
    self._feedback_fn = feedback_fn
2231
    feedback_fn("* Verifying global settings")
2232
    for msg in self.cfg.VerifyConfig():
2233
      _ErrorIf(True, self.ECLUSTERCFG, None, msg)
2234

    
2235
    # Check the cluster certificates
2236
    for cert_filename in constants.ALL_CERT_FILES:
2237
      (errcode, msg) = _VerifyCertificate(cert_filename)
2238
      _ErrorIf(errcode, self.ECLUSTERCERT, None, msg, code=errcode)
2239

    
2240
    vg_name = self.cfg.GetVGName()
2241
    drbd_helper = self.cfg.GetDRBDHelper()
2242
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
2243
    cluster = self.cfg.GetClusterInfo()
2244
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
2245
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
2246
    nodeinfo_byname = dict(zip(nodelist, nodeinfo))
2247
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
2248
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
2249
                        for iname in instancelist)
2250
    groupinfo = self.cfg.GetAllNodeGroupsInfo()
2251
    i_non_redundant = [] # Non redundant instances
2252
    i_non_a_balanced = [] # Non auto-balanced instances
2253
    n_offline = 0 # Count of offline nodes
2254
    n_drained = 0 # Count of nodes being drained
2255
    node_vol_should = {}
2256

    
2257
    # FIXME: verify OS list
2258

    
2259
    # File verification
2260
    filemap = _ComputeAncillaryFiles(cluster, False)
2261

    
2262
    # do local checksums
2263
    master_node = self.master_node = self.cfg.GetMasterNode()
2264
    master_ip = self.cfg.GetMasterIP()
2265

    
2266
    # Compute the set of hypervisor parameters
2267
    hvp_data = []
2268
    for hv_name in hypervisors:
2269
      hvp_data.append(("cluster", hv_name, cluster.GetHVDefaults(hv_name)))
2270
    for os_name, os_hvp in cluster.os_hvp.items():
2271
      for hv_name, hv_params in os_hvp.items():
2272
        if not hv_params:
2273
          continue
2274
        full_params = cluster.GetHVDefaults(hv_name, os_name=os_name)
2275
        hvp_data.append(("os %s" % os_name, hv_name, full_params))
2276
    # TODO: collapse identical parameter values in a single one
2277
    for instance in instanceinfo.values():
2278
      if not instance.hvparams:
2279
        continue
2280
      hvp_data.append(("instance %s" % instance.name, instance.hypervisor,
2281
                       cluster.FillHV(instance)))
2282
    # and verify them locally
2283
    self._VerifyHVP(hvp_data)
2284

    
2285
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
2286
    node_verify_param = {
2287
      constants.NV_FILELIST:
2288
        utils.UniqueSequence(filename
2289
                             for files in filemap
2290
                             for filename in files),
2291
      constants.NV_NODELIST: [node.name for node in nodeinfo
2292
                              if not node.offline],
2293
      constants.NV_HYPERVISOR: hypervisors,
2294
      constants.NV_HVPARAMS: hvp_data,
2295
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
2296
                                  node.secondary_ip) for node in nodeinfo
2297
                                 if not node.offline],
2298
      constants.NV_INSTANCELIST: hypervisors,
2299
      constants.NV_VERSION: None,
2300
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
2301
      constants.NV_NODESETUP: None,
2302
      constants.NV_TIME: None,
2303
      constants.NV_MASTERIP: (master_node, master_ip),
2304
      constants.NV_OSLIST: None,
2305
      constants.NV_VMNODES: self.cfg.GetNonVmCapableNodeList(),
2306
      }
2307

    
2308
    if vg_name is not None:
2309
      node_verify_param[constants.NV_VGLIST] = None
2310
      node_verify_param[constants.NV_LVLIST] = vg_name
2311
      node_verify_param[constants.NV_PVLIST] = [vg_name]
2312
      node_verify_param[constants.NV_DRBDLIST] = None
2313

    
2314
    if drbd_helper:
2315
      node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
2316

    
2317
    # Build our expected cluster state
2318
    node_image = dict((node.name, self.NodeImage(offline=node.offline,
2319
                                                 name=node.name,
2320
                                                 vm_capable=node.vm_capable))
2321
                      for node in nodeinfo)
2322

    
2323
    # Gather OOB paths
2324
    oob_paths = []
2325
    for node in nodeinfo:
2326
      path = _SupportsOob(self.cfg, node)
2327
      if path and path not in oob_paths:
2328
        oob_paths.append(path)
2329

    
2330
    if oob_paths:
2331
      node_verify_param[constants.NV_OOB_PATHS] = oob_paths
2332

    
2333
    for instance in instancelist:
2334
      inst_config = instanceinfo[instance]
2335

    
2336
      for nname in inst_config.all_nodes:
2337
        if nname not in node_image:
2338
          # ghost node
2339
          gnode = self.NodeImage(name=nname)
2340
          gnode.ghost = True
2341
          node_image[nname] = gnode
2342

    
2343
      inst_config.MapLVsByNode(node_vol_should)
2344

    
2345
      pnode = inst_config.primary_node
2346
      node_image[pnode].pinst.append(instance)
2347

    
2348
      for snode in inst_config.secondary_nodes:
2349
        nimg = node_image[snode]
2350
        nimg.sinst.append(instance)
2351
        if pnode not in nimg.sbp:
2352
          nimg.sbp[pnode] = []
2353
        nimg.sbp[pnode].append(instance)
2354

    
2355
    # At this point, we have the in-memory data structures complete,
2356
    # except for the runtime information, which we'll gather next
2357

    
2358
    # Due to the way our RPC system works, exact response times cannot be
2359
    # guaranteed (e.g. a broken node could run into a timeout). By keeping the
2360
    # time before and after executing the request, we can at least have a time
2361
    # window.
2362
    nvinfo_starttime = time.time()
2363
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
2364
                                           self.cfg.GetClusterName())
2365
    nvinfo_endtime = time.time()
2366

    
2367
    all_drbd_map = self.cfg.ComputeDRBDMap()
2368

    
2369
    feedback_fn("* Gathering disk information (%s nodes)" % len(nodelist))
2370
    instdisk = self._CollectDiskInfo(nodelist, node_image, instanceinfo)
2371

    
2372
    feedback_fn("* Verifying configuration file consistency")
2373
    self._VerifyFiles(_ErrorIf, nodeinfo, master_node, all_nvinfo, filemap)
2374

    
2375
    feedback_fn("* Verifying node status")
2376

    
2377
    refos_img = None
2378

    
2379
    for node_i in nodeinfo:
2380
      node = node_i.name
2381
      nimg = node_image[node]
2382

    
2383
      if node_i.offline:
2384
        if verbose:
2385
          feedback_fn("* Skipping offline node %s" % (node,))
2386
        n_offline += 1
2387
        continue
2388

    
2389
      if node == master_node:
2390
        ntype = "master"
2391
      elif node_i.master_candidate:
2392
        ntype = "master candidate"
2393
      elif node_i.drained:
2394
        ntype = "drained"
2395
        n_drained += 1
2396
      else:
2397
        ntype = "regular"
2398
      if verbose:
2399
        feedback_fn("* Verifying node %s (%s)" % (node, ntype))
2400

    
2401
      msg = all_nvinfo[node].fail_msg
2402
      _ErrorIf(msg, self.ENODERPC, node, "while contacting node: %s", msg)
2403
      if msg:
2404
        nimg.rpc_fail = True
2405
        continue
2406

    
2407
      nresult = all_nvinfo[node].payload
2408

    
2409
      nimg.call_ok = self._VerifyNode(node_i, nresult)
2410
      self._VerifyNodeTime(node_i, nresult, nvinfo_starttime, nvinfo_endtime)
2411
      self._VerifyNodeNetwork(node_i, nresult)
2412
      self._VerifyOob(node_i, nresult)
2413

    
2414
      if nimg.vm_capable:
2415
        self._VerifyNodeLVM(node_i, nresult, vg_name)
2416
        self._VerifyNodeDrbd(node_i, nresult, instanceinfo, drbd_helper,
2417
                             all_drbd_map)
2418

    
2419
        self._UpdateNodeVolumes(node_i, nresult, nimg, vg_name)
2420
        self._UpdateNodeInstances(node_i, nresult, nimg)
2421
        self._UpdateNodeInfo(node_i, nresult, nimg, vg_name)
2422
        self._UpdateNodeOS(node_i, nresult, nimg)
2423
        if not nimg.os_fail:
2424
          if refos_img is None:
2425
            refos_img = nimg
2426
          self._VerifyNodeOS(node_i, nimg, refos_img)
2427

    
2428
    feedback_fn("* Verifying instance status")
2429
    for instance in instancelist:
2430
      if verbose:
2431
        feedback_fn("* Verifying instance %s" % instance)
2432
      inst_config = instanceinfo[instance]
2433
      self._VerifyInstance(instance, inst_config, node_image,
2434
                           instdisk[instance])
2435
      inst_nodes_offline = []
2436

    
2437
      pnode = inst_config.primary_node
2438
      pnode_img = node_image[pnode]
2439
      _ErrorIf(pnode_img.rpc_fail and not pnode_img.offline,
2440
               self.ENODERPC, pnode, "instance %s, connection to"
2441
               " primary node failed", instance)
2442

    
2443
      _ErrorIf(inst_config.admin_up and pnode_img.offline,
2444
               self.EINSTANCEBADNODE, instance,
2445
               "instance is marked as running and lives on offline node %s",
2446
               inst_config.primary_node)
2447

    
2448
      # If the instance is non-redundant we cannot survive losing its primary
2449
      # node, so we are not N+1 compliant. On the other hand we have no disk
2450
      # templates with more than one secondary so that situation is not well
2451
      # supported either.
2452
      # FIXME: does not support file-backed instances
2453
      if not inst_config.secondary_nodes:
2454
        i_non_redundant.append(instance)
2455

    
2456
      _ErrorIf(len(inst_config.secondary_nodes) > 1, self.EINSTANCELAYOUT,
2457
               instance, "instance has multiple secondary nodes: %s",
2458
               utils.CommaJoin(inst_config.secondary_nodes),
2459
               code=self.ETYPE_WARNING)
2460

    
2461
      if inst_config.disk_template in constants.DTS_INT_MIRROR:
2462
        pnode = inst_config.primary_node
2463
        instance_nodes = utils.NiceSort(inst_config.all_nodes)
2464
        instance_groups = {}
2465

    
2466
        for node in instance_nodes:
2467
          instance_groups.setdefault(nodeinfo_byname[node].group,
2468
                                     []).append(node)
2469

    
2470
        pretty_list = [
2471
          "%s (group %s)" % (utils.CommaJoin(nodes), groupinfo[group].name)
2472
          # Sort so that we always list the primary node first.
2473
          for group, nodes in sorted(instance_groups.items(),
2474
                                     key=lambda (_, nodes): pnode in nodes,
2475
                                     reverse=True)]
2476

    
2477
        self._ErrorIf(len(instance_groups) > 1, self.EINSTANCESPLITGROUPS,
2478
                      instance, "instance has primary and secondary nodes in"
2479
                      " different groups: %s", utils.CommaJoin(pretty_list),
2480
                      code=self.ETYPE_WARNING)
2481

    
2482
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
2483
        i_non_a_balanced.append(instance)
2484

    
2485
      for snode in inst_config.secondary_nodes:
2486
        s_img = node_image[snode]
2487
        _ErrorIf(s_img.rpc_fail and not s_img.offline, self.ENODERPC, snode,
2488
                 "instance %s, connection to secondary node failed", instance)
2489

    
2490
        if s_img.offline:
2491
          inst_nodes_offline.append(snode)
2492

    
2493
      # warn that the instance lives on offline nodes
2494
      _ErrorIf(inst_nodes_offline, self.EINSTANCEBADNODE, instance,
2495
               "instance has offline secondary node(s) %s",
2496
               utils.CommaJoin(inst_nodes_offline))
2497
      # ... or ghost/non-vm_capable nodes
2498
      for node in inst_config.all_nodes:
2499
        _ErrorIf(node_image[node].ghost, self.EINSTANCEBADNODE, instance,
2500
                 "instance lives on ghost node %s", node)
2501
        _ErrorIf(not node_image[node].vm_capable, self.EINSTANCEBADNODE,
2502
                 instance, "instance lives on non-vm_capable node %s", node)
2503

    
2504
    feedback_fn("* Verifying orphan volumes")
2505
    reserved = utils.FieldSet(*cluster.reserved_lvs)
2506
    self._VerifyOrphanVolumes(node_vol_should, node_image, reserved)
2507

    
2508
    feedback_fn("* Verifying orphan instances")
2509
    self._VerifyOrphanInstances(instancelist, node_image)
2510

    
2511
    if constants.VERIFY_NPLUSONE_MEM not in self.op.skip_checks:
2512
      feedback_fn("* Verifying N+1 Memory redundancy")
2513
      self._VerifyNPlusOneMemory(node_image, instanceinfo)
2514

    
2515
    feedback_fn("* Other Notes")
2516
    if i_non_redundant:
2517
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
2518
                  % len(i_non_redundant))
2519

    
2520
    if i_non_a_balanced:
2521
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
2522
                  % len(i_non_a_balanced))
2523

    
2524
    if n_offline:
2525
      feedback_fn("  - NOTICE: %d offline node(s) found." % n_offline)
2526

    
2527
    if n_drained:
2528
      feedback_fn("  - NOTICE: %d drained node(s) found." % n_drained)
2529

    
2530
    return not self.bad
2531

    
2532
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
2533
    """Analyze the post-hooks' result
2534

2535
    This method analyses the hook result, handles it, and sends some
2536
    nicely-formatted feedback back to the user.
2537

2538
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
2539
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
2540
    @param hooks_results: the results of the multi-node hooks rpc call
2541
    @param feedback_fn: function used send feedback back to the caller
2542
    @param lu_result: previous Exec result
2543
    @return: the new Exec result, based on the previous result
2544
        and hook results
2545

2546
    """
2547
    # We only really run POST phase hooks, and are only interested in
2548
    # their results
2549
    if phase == constants.HOOKS_PHASE_POST:
2550
      # Used to change hooks' output to proper indentation
2551
      feedback_fn("* Hooks Results")
2552
      assert hooks_results, "invalid result from hooks"
2553

    
2554
      for node_name in hooks_results:
2555
        res = hooks_results[node_name]
2556
        msg = res.fail_msg
2557
        test = msg and not res.offline
2558
        self._ErrorIf(test, self.ENODEHOOKS, node_name,
2559
                      "Communication failure in hooks execution: %s", msg)
2560
        if res.offline or msg:
2561
          # No need to investigate payload if node is offline or gave an error.
2562
          # override manually lu_result here as _ErrorIf only
2563
          # overrides self.bad
2564
          lu_result = 1
2565
          continue
2566
        for script, hkr, output in res.payload:
2567
          test = hkr == constants.HKR_FAIL
2568
          self._ErrorIf(test, self.ENODEHOOKS, node_name,
2569
                        "Script %s failed, output:", script)
2570
          if test:
2571
            output = self._HOOKS_INDENT_RE.sub('      ', output)
2572
            feedback_fn("%s" % output)
2573
            lu_result = 0
2574

    
2575
      return lu_result
2576

    
2577

    
2578
class LUClusterVerifyDisks(NoHooksLU):
2579
  """Verifies the cluster disks status.
2580

2581
  """
2582
  REQ_BGL = False
2583

    
2584
  def ExpandNames(self):
2585
    self.needed_locks = {
2586
      locking.LEVEL_NODE: locking.ALL_SET,
2587
      locking.LEVEL_INSTANCE: locking.ALL_SET,
2588
    }
2589
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
2590

    
2591
  def Exec(self, feedback_fn):
2592
    """Verify integrity of cluster disks.
2593

2594
    @rtype: tuple of three items
2595
    @return: a tuple of (dict of node-to-node_error, list of instances
2596
        which need activate-disks, dict of instance: (node, volume) for
2597
        missing volumes
2598

2599
    """
2600
    result = res_nodes, res_instances, res_missing = {}, [], {}
2601

    
2602
    nodes = utils.NiceSort(self.cfg.GetVmCapableNodeList())
2603
    instances = self.cfg.GetAllInstancesInfo().values()
2604

    
2605
    nv_dict = {}
2606
    for inst in instances:
2607
      inst_lvs = {}
2608
      if not inst.admin_up:
2609
        continue
2610
      inst.MapLVsByNode(inst_lvs)
2611
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
2612
      for node, vol_list in inst_lvs.iteritems():
2613
        for vol in vol_list:
2614
          nv_dict[(node, vol)] = inst
2615

    
2616
    if not nv_dict:
2617
      return result
2618

    
2619
    node_lvs = self.rpc.call_lv_list(nodes, [])
2620
    for node, node_res in node_lvs.items():
2621
      if node_res.offline:
2622
        continue
2623
      msg = node_res.fail_msg
2624
      if msg:
2625
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
2626
        res_nodes[node] = msg
2627
        continue
2628

    
2629
      lvs = node_res.payload
2630
      for lv_name, (_, _, lv_online) in lvs.items():
2631
        inst = nv_dict.pop((node, lv_name), None)
2632
        if (not lv_online and inst is not None
2633
            and inst.name not in res_instances):
2634
          res_instances.append(inst.name)
2635

    
2636
    # any leftover items in nv_dict are missing LVs, let's arrange the
2637
    # data better
2638
    for key, inst in nv_dict.iteritems():
2639
      if inst.name not in res_missing:
2640
        res_missing[inst.name] = []
2641
      res_missing[inst.name].append(key)
2642

    
2643
    return result
2644

    
2645

    
2646
class LUClusterRepairDiskSizes(NoHooksLU):
2647
  """Verifies the cluster disks sizes.
2648

2649
  """
2650
  REQ_BGL = False
2651

    
2652
  def ExpandNames(self):
2653
    if self.op.instances:
2654
      self.wanted_names = []
2655
      for name in self.op.instances:
2656
        full_name = _ExpandInstanceName(self.cfg, name)
2657
        self.wanted_names.append(full_name)
2658
      self.needed_locks = {
2659
        locking.LEVEL_NODE: [],
2660
        locking.LEVEL_INSTANCE: self.wanted_names,
2661
        }
2662
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2663
    else:
2664
      self.wanted_names = None
2665
      self.needed_locks = {
2666
        locking.LEVEL_NODE: locking.ALL_SET,
2667
        locking.LEVEL_INSTANCE: locking.ALL_SET,
2668
        }
2669
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
2670

    
2671
  def DeclareLocks(self, level):
2672
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
2673
      self._LockInstancesNodes(primary_only=True)
2674

    
2675
  def CheckPrereq(self):
2676
    """Check prerequisites.
2677

2678
    This only checks the optional instance list against the existing names.
2679

2680
    """
2681
    if self.wanted_names is None:
2682
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
2683

    
2684
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2685
                             in self.wanted_names]
2686

    
2687
  def _EnsureChildSizes(self, disk):
2688
    """Ensure children of the disk have the needed disk size.
2689

2690
    This is valid mainly for DRBD8 and fixes an issue where the
2691
    children have smaller disk size.
2692

2693
    @param disk: an L{ganeti.objects.Disk} object
2694

2695
    """
2696
    if disk.dev_type == constants.LD_DRBD8:
2697
      assert disk.children, "Empty children for DRBD8?"
2698
      fchild = disk.children[0]
2699
      mismatch = fchild.size < disk.size
2700
      if mismatch:
2701
        self.LogInfo("Child disk has size %d, parent %d, fixing",
2702
                     fchild.size, disk.size)
2703
        fchild.size = disk.size
2704

    
2705
      # and we recurse on this child only, not on the metadev
2706
      return self._EnsureChildSizes(fchild) or mismatch
2707
    else:
2708
      return False
2709

    
2710
  def Exec(self, feedback_fn):
2711
    """Verify the size of cluster disks.
2712

2713
    """
2714
    # TODO: check child disks too
2715
    # TODO: check differences in size between primary/secondary nodes
2716
    per_node_disks = {}
2717
    for instance in self.wanted_instances:
2718
      pnode = instance.primary_node
2719
      if pnode not in per_node_disks:
2720
        per_node_disks[pnode] = []
2721
      for idx, disk in enumerate(instance.disks):
2722
        per_node_disks[pnode].append((instance, idx, disk))
2723

    
2724
    changed = []
2725
    for node, dskl in per_node_disks.items():
2726
      newl = [v[2].Copy() for v in dskl]
2727
      for dsk in newl:
2728
        self.cfg.SetDiskID(dsk, node)
2729
      result = self.rpc.call_blockdev_getsize(node, newl)
2730
      if result.fail_msg:
2731
        self.LogWarning("Failure in blockdev_getsize call to node"
2732
                        " %s, ignoring", node)
2733
        continue
2734
      if len(result.payload) != len(dskl):
2735
        logging.warning("Invalid result from node %s: len(dksl)=%d,"
2736
                        " result.payload=%s", node, len(dskl), result.payload)
2737
        self.LogWarning("Invalid result from node %s, ignoring node results",
2738
                        node)
2739
        continue
2740
      for ((instance, idx, disk), size) in zip(dskl, result.payload):
2741
        if size is None:
2742
          self.LogWarning("Disk %d of instance %s did not return size"
2743
                          " information, ignoring", idx, instance.name)
2744
          continue
2745
        if not isinstance(size, (int, long)):
2746
          self.LogWarning("Disk %d of instance %s did not return valid"
2747
                          " size information, ignoring", idx, instance.name)
2748
          continue
2749
        size = size >> 20
2750
        if size != disk.size:
2751
          self.LogInfo("Disk %d of instance %s has mismatched size,"
2752
                       " correcting: recorded %d, actual %d", idx,
2753
                       instance.name, disk.size, size)
2754
          disk.size = size
2755
          self.cfg.Update(instance, feedback_fn)
2756
          changed.append((instance.name, idx, size))
2757
        if self._EnsureChildSizes(disk):
2758
          self.cfg.Update(instance, feedback_fn)
2759
          changed.append((instance.name, idx, disk.size))
2760
    return changed
2761

    
2762

    
2763
class LUClusterRename(LogicalUnit):
2764
  """Rename the cluster.
2765

2766
  """
2767
  HPATH = "cluster-rename"
2768
  HTYPE = constants.HTYPE_CLUSTER
2769

    
2770
  def BuildHooksEnv(self):
2771
    """Build hooks env.
2772

2773
    """
2774
    return {
2775
      "OP_TARGET": self.cfg.GetClusterName(),
2776
      "NEW_NAME": self.op.name,
2777
      }
2778

    
2779
  def BuildHooksNodes(self):
2780
    """Build hooks nodes.
2781

2782
    """
2783
    return ([self.cfg.GetMasterNode()], self.cfg.GetNodeList())
2784

    
2785
  def CheckPrereq(self):
2786
    """Verify that the passed name is a valid one.
2787

2788
    """
2789
    hostname = netutils.GetHostname(name=self.op.name,
2790
                                    family=self.cfg.GetPrimaryIPFamily())
2791

    
2792
    new_name = hostname.name
2793
    self.ip = new_ip = hostname.ip
2794
    old_name = self.cfg.GetClusterName()
2795
    old_ip = self.cfg.GetMasterIP()
2796
    if new_name == old_name and new_ip == old_ip:
2797
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
2798
                                 " cluster has changed",
2799
                                 errors.ECODE_INVAL)
2800
    if new_ip != old_ip:
2801
      if netutils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
2802
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
2803
                                   " reachable on the network" %
2804
                                   new_ip, errors.ECODE_NOTUNIQUE)
2805

    
2806
    self.op.name = new_name
2807

    
2808
  def Exec(self, feedback_fn):
2809
    """Rename the cluster.
2810

2811
    """
2812
    clustername = self.op.name
2813
    ip = self.ip
2814

    
2815
    # shutdown the master IP
2816
    master = self.cfg.GetMasterNode()
2817
    result = self.rpc.call_node_stop_master(master, False)
2818
    result.Raise("Could not disable the master role")
2819

    
2820
    try:
2821
      cluster = self.cfg.GetClusterInfo()
2822
      cluster.cluster_name = clustername
2823
      cluster.master_ip = ip
2824
      self.cfg.Update(cluster, feedback_fn)
2825

    
2826
      # update the known hosts file
2827
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
2828
      node_list = self.cfg.GetOnlineNodeList()
2829
      try:
2830
        node_list.remove(master)
2831
      except ValueError:
2832
        pass
2833
      _UploadHelper(self, node_list, constants.SSH_KNOWN_HOSTS_FILE)
2834
    finally:
2835
      result = self.rpc.call_node_start_master(master, False, False)
2836
      msg = result.fail_msg
2837
      if msg:
2838
        self.LogWarning("Could not re-enable the master role on"
2839
                        " the master, please restart manually: %s", msg)
2840

    
2841
    return clustername
2842

    
2843

    
2844
class LUClusterSetParams(LogicalUnit):
2845
  """Change the parameters of the cluster.
2846

2847
  """
2848
  HPATH = "cluster-modify"
2849
  HTYPE = constants.HTYPE_CLUSTER
2850
  REQ_BGL = False
2851

    
2852
  def CheckArguments(self):
2853
    """Check parameters
2854

2855
    """
2856
    if self.op.uid_pool:
2857
      uidpool.CheckUidPool(self.op.uid_pool)
2858

    
2859
    if self.op.add_uids:
2860
      uidpool.CheckUidPool(self.op.add_uids)
2861

    
2862
    if self.op.remove_uids:
2863
      uidpool.CheckUidPool(self.op.remove_uids)
2864

    
2865
  def ExpandNames(self):
2866
    # FIXME: in the future maybe other cluster params won't require checking on
2867
    # all nodes to be modified.
2868
    self.needed_locks = {
2869
      locking.LEVEL_NODE: locking.ALL_SET,
2870
    }
2871
    self.share_locks[locking.LEVEL_NODE] = 1
2872

    
2873
  def BuildHooksEnv(self):
2874
    """Build hooks env.
2875

2876
    """
2877
    return {
2878
      "OP_TARGET": self.cfg.GetClusterName(),
2879
      "NEW_VG_NAME": self.op.vg_name,
2880
      }
2881

    
2882
  def BuildHooksNodes(self):
2883
    """Build hooks nodes.
2884

2885
    """
2886
    mn = self.cfg.GetMasterNode()
2887
    return ([mn], [mn])
2888

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

2892
    This checks whether the given params don't conflict and
2893
    if the given volume group is valid.
2894

2895
    """
2896
    if self.op.vg_name is not None and not self.op.vg_name:
2897
      if self.cfg.HasAnyDiskOfType(constants.LD_LV):
2898
        raise errors.OpPrereqError("Cannot disable lvm storage while lvm-based"
2899
                                   " instances exist", errors.ECODE_INVAL)
2900

    
2901
    if self.op.drbd_helper is not None and not self.op.drbd_helper:
2902
      if self.cfg.HasAnyDiskOfType(constants.LD_DRBD8):
2903
        raise errors.OpPrereqError("Cannot disable drbd helper while"
2904
                                   " drbd-based instances exist",
2905
                                   errors.ECODE_INVAL)
2906

    
2907
    node_list = self.acquired_locks[locking.LEVEL_NODE]
2908

    
2909
    # if vg_name not None, checks given volume group on all nodes
2910
    if self.op.vg_name:
2911
      vglist = self.rpc.call_vg_list(node_list)
2912
      for node in node_list:
2913
        msg = vglist[node].fail_msg
2914
        if msg:
2915
          # ignoring down node
2916
          self.LogWarning("Error while gathering data on node %s"
2917
                          " (ignoring node): %s", node, msg)
2918
          continue
2919
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
2920
                                              self.op.vg_name,
2921
                                              constants.MIN_VG_SIZE)
2922
        if vgstatus:
2923
          raise errors.OpPrereqError("Error on node '%s': %s" %
2924
                                     (node, vgstatus), errors.ECODE_ENVIRON)
2925

    
2926
    if self.op.drbd_helper:
2927
      # checks given drbd helper on all nodes
2928
      helpers = self.rpc.call_drbd_helper(node_list)
2929
      for node in node_list:
2930
        ninfo = self.cfg.GetNodeInfo(node)
2931
        if ninfo.offline:
2932
          self.LogInfo("Not checking drbd helper on offline node %s", node)
2933
          continue
2934
        msg = helpers[node].fail_msg
2935
        if msg:
2936
          raise errors.OpPrereqError("Error checking drbd helper on node"
2937
                                     " '%s': %s" % (node, msg),
2938
                                     errors.ECODE_ENVIRON)
2939
        node_helper = helpers[node].payload
2940
        if node_helper != self.op.drbd_helper:
2941
          raise errors.OpPrereqError("Error on node '%s': drbd helper is %s" %
2942
                                     (node, node_helper), errors.ECODE_ENVIRON)
2943

    
2944
    self.cluster = cluster = self.cfg.GetClusterInfo()
2945
    # validate params changes
2946
    if self.op.beparams:
2947
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
2948
      self.new_beparams = cluster.SimpleFillBE(self.op.beparams)
2949

    
2950
    if self.op.ndparams:
2951
      utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
2952
      self.new_ndparams = cluster.SimpleFillND(self.op.ndparams)
2953

    
2954
      # TODO: we need a more general way to handle resetting
2955
      # cluster-level parameters to default values
2956
      if self.new_ndparams["oob_program"] == "":
2957
        self.new_ndparams["oob_program"] = \
2958
            constants.NDC_DEFAULTS[constants.ND_OOB_PROGRAM]
2959

    
2960
    if self.op.nicparams:
2961
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
2962
      self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams)
2963
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
2964
      nic_errors = []
2965

    
2966
      # check all instances for consistency
2967
      for instance in self.cfg.GetAllInstancesInfo().values():
2968
        for nic_idx, nic in enumerate(instance.nics):
2969
          params_copy = copy.deepcopy(nic.nicparams)
2970
          params_filled = objects.FillDict(self.new_nicparams, params_copy)
2971

    
2972
          # check parameter syntax
2973
          try:
2974
            objects.NIC.CheckParameterSyntax(params_filled)
2975
          except errors.ConfigurationError, err:
2976
            nic_errors.append("Instance %s, nic/%d: %s" %
2977
                              (instance.name, nic_idx, err))
2978

    
2979
          # if we're moving instances to routed, check that they have an ip
2980
          target_mode = params_filled[constants.NIC_MODE]
2981
          if target_mode == constants.NIC_MODE_ROUTED and not nic.ip:
2982
            nic_errors.append("Instance %s, nic/%d: routed nick with no ip" %
2983
                              (instance.name, nic_idx))
2984
      if nic_errors:
2985
        raise errors.OpPrereqError("Cannot apply the change, errors:\n%s" %
2986
                                   "\n".join(nic_errors))
2987

    
2988
    # hypervisor list/parameters
2989
    self.new_hvparams = new_hvp = objects.FillDict(cluster.hvparams, {})
2990
    if self.op.hvparams:
2991
      for hv_name, hv_dict in self.op.hvparams.items():
2992
        if hv_name not in self.new_hvparams:
2993
          self.new_hvparams[hv_name] = hv_dict
2994
        else:
2995
          self.new_hvparams[hv_name].update(hv_dict)
2996

    
2997
    # os hypervisor parameters
2998
    self.new_os_hvp = objects.FillDict(cluster.os_hvp, {})
2999
    if self.op.os_hvp:
3000
      for os_name, hvs in self.op.os_hvp.items():
3001
        if os_name not in self.new_os_hvp:
3002
          self.new_os_hvp[os_name] = hvs
3003
        else:
3004
          for hv_name, hv_dict in hvs.items():
3005
            if hv_name not in self.new_os_hvp[os_name]:
3006
              self.new_os_hvp[os_name][hv_name] = hv_dict
3007
            else:
3008
              self.new_os_hvp[os_name][hv_name].update(hv_dict)
3009

    
3010
    # os parameters
3011
    self.new_osp = objects.FillDict(cluster.osparams, {})
3012
    if self.op.osparams:
3013
      for os_name, osp in self.op.osparams.items():
3014
        if os_name not in self.new_osp:
3015
          self.new_osp[os_name] = {}
3016

    
3017
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
3018
                                                  use_none=True)
3019

    
3020
        if not self.new_osp[os_name]:
3021
          # we removed all parameters
3022
          del self.new_osp[os_name]
3023
        else:
3024
          # check the parameter validity (remote check)
3025
          _CheckOSParams(self, False, [self.cfg.GetMasterNode()],
3026
                         os_name, self.new_osp[os_name])
3027

    
3028
    # changes to the hypervisor list
3029
    if self.op.enabled_hypervisors is not None:
3030
      self.hv_list = self.op.enabled_hypervisors
3031
      for hv in self.hv_list:
3032
        # if the hypervisor doesn't already exist in the cluster
3033
        # hvparams, we initialize it to empty, and then (in both
3034
        # cases) we make sure to fill the defaults, as we might not
3035
        # have a complete defaults list if the hypervisor wasn't
3036
        # enabled before
3037
        if hv not in new_hvp:
3038
          new_hvp[hv] = {}
3039
        new_hvp[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], new_hvp[hv])
3040
        utils.ForceDictType(new_hvp[hv], constants.HVS_PARAMETER_TYPES)
3041
    else:
3042
      self.hv_list = cluster.enabled_hypervisors
3043

    
3044
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
3045
      # either the enabled list has changed, or the parameters have, validate
3046
      for hv_name, hv_params in self.new_hvparams.items():
3047
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
3048
            (self.op.enabled_hypervisors and
3049
             hv_name in self.op.enabled_hypervisors)):
3050
          # either this is a new hypervisor, or its parameters have changed
3051
          hv_class = hypervisor.GetHypervisor(hv_name)
3052
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
3053
          hv_class.CheckParameterSyntax(hv_params)
3054
          _CheckHVParams(self, node_list, hv_name, hv_params)
3055

    
3056
    if self.op.os_hvp:
3057
      # no need to check any newly-enabled hypervisors, since the
3058
      # defaults have already been checked in the above code-block
3059
      for os_name, os_hvp in self.new_os_hvp.items():
3060
        for hv_name, hv_params in os_hvp.items():
3061
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
3062
          # we need to fill in the new os_hvp on top of the actual hv_p
3063
          cluster_defaults = self.new_hvparams.get(hv_name, {})
3064
          new_osp = objects.FillDict(cluster_defaults, hv_params)
3065
          hv_class = hypervisor.GetHypervisor(hv_name)
3066
          hv_class.CheckParameterSyntax(new_osp)
3067
          _CheckHVParams(self, node_list, hv_name, new_osp)
3068

    
3069
    if self.op.default_iallocator:
3070
      alloc_script = utils.FindFile(self.op.default_iallocator,
3071
                                    constants.IALLOCATOR_SEARCH_PATH,
3072
                                    os.path.isfile)
3073
      if alloc_script is None:
3074
        raise errors.OpPrereqError("Invalid default iallocator script '%s'"
3075
                                   " specified" % self.op.default_iallocator,
3076
                                   errors.ECODE_INVAL)
3077

    
3078
  def Exec(self, feedback_fn):
3079
    """Change the parameters of the cluster.
3080

3081
    """
3082
    if self.op.vg_name is not None:
3083
      new_volume = self.op.vg_name
3084
      if not new_volume:
3085
        new_volume = None
3086
      if new_volume != self.cfg.GetVGName():
3087
        self.cfg.SetVGName(new_volume)
3088
      else:
3089
        feedback_fn("Cluster LVM configuration already in desired"
3090
                    " state, not changing")
3091
    if self.op.drbd_helper is not None:
3092
      new_helper = self.op.drbd_helper
3093
      if not new_helper:
3094
        new_helper = None
3095
      if new_helper != self.cfg.GetDRBDHelper():
3096
        self.cfg.SetDRBDHelper(new_helper)
3097
      else:
3098
        feedback_fn("Cluster DRBD helper already in desired state,"
3099
                    " not changing")
3100
    if self.op.hvparams:
3101
      self.cluster.hvparams = self.new_hvparams
3102
    if self.op.os_hvp:
3103
      self.cluster.os_hvp = self.new_os_hvp
3104
    if self.op.enabled_hypervisors is not None:
3105
      self.cluster.hvparams = self.new_hvparams
3106
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
3107
    if self.op.beparams:
3108
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
3109
    if self.op.nicparams:
3110
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
3111
    if self.op.osparams:
3112
      self.cluster.osparams = self.new_osp
3113
    if self.op.ndparams:
3114
      self.cluster.ndparams = self.new_ndparams
3115

    
3116
    if self.op.candidate_pool_size is not None:
3117
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
3118
      # we need to update the pool size here, otherwise the save will fail
3119
      _AdjustCandidatePool(self, [])
3120

    
3121
    if self.op.maintain_node_health is not None:
3122
      self.cluster.maintain_node_health = self.op.maintain_node_health
3123

    
3124
    if self.op.prealloc_wipe_disks is not None:
3125
      self.cluster.prealloc_wipe_disks = self.op.prealloc_wipe_disks
3126

    
3127
    if self.op.add_uids is not None:
3128
      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
3129

    
3130
    if self.op.remove_uids is not None:
3131
      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
3132

    
3133
    if self.op.uid_pool is not None:
3134
      self.cluster.uid_pool = self.op.uid_pool
3135

    
3136
    if self.op.default_iallocator is not None:
3137
      self.cluster.default_iallocator = self.op.default_iallocator
3138

    
3139
    if self.op.reserved_lvs is not None:
3140
      self.cluster.reserved_lvs = self.op.reserved_lvs
3141

    
3142
    def helper_os(aname, mods, desc):
3143
      desc += " OS list"
3144
      lst = getattr(self.cluster, aname)
3145
      for key, val in mods:
3146
        if key == constants.DDM_ADD:
3147
          if val in lst:
3148
            feedback_fn("OS %s already in %s, ignoring" % (val, desc))
3149
          else:
3150
            lst.append(val)
3151
        elif key == constants.DDM_REMOVE:
3152
          if val in lst:
3153
            lst.remove(val)
3154
          else:
3155
            feedback_fn("OS %s not found in %s, ignoring" % (val, desc))
3156
        else:
3157
          raise errors.ProgrammerError("Invalid modification '%s'" % key)
3158

    
3159
    if self.op.hidden_os:
3160
      helper_os("hidden_os", self.op.hidden_os, "hidden")
3161

    
3162
    if self.op.blacklisted_os:
3163
      helper_os("blacklisted_os", self.op.blacklisted_os, "blacklisted")
3164

    
3165
    if self.op.master_netdev:
3166
      master = self.cfg.GetMasterNode()
3167
      feedback_fn("Shutting down master ip on the current netdev (%s)" %
3168
                  self.cluster.master_netdev)
3169
      result = self.rpc.call_node_stop_master(master, False)
3170
      result.Raise("Could not disable the master ip")
3171
      feedback_fn("Changing master_netdev from %s to %s" %
3172
                  (self.cluster.master_netdev, self.op.master_netdev))
3173
      self.cluster.master_netdev = self.op.master_netdev
3174

    
3175
    self.cfg.Update(self.cluster, feedback_fn)
3176

    
3177
    if self.op.master_netdev:
3178
      feedback_fn("Starting the master ip on the new master netdev (%s)" %
3179
                  self.op.master_netdev)
3180
      result = self.rpc.call_node_start_master(master, False, False)
3181
      if result.fail_msg:
3182
        self.LogWarning("Could not re-enable the master ip on"
3183
                        " the master, please restart manually: %s",
3184
                        result.fail_msg)
3185

    
3186

    
3187
def _UploadHelper(lu, nodes, fname):
3188
  """Helper for uploading a file and showing warnings.
3189

3190
  """
3191
  if os.path.exists(fname):
3192
    result = lu.rpc.call_upload_file(nodes, fname)
3193
    for to_node, to_result in result.items():
3194
      msg = to_result.fail_msg
3195
      if msg:
3196
        msg = ("Copy of file %s to node %s failed: %s" %
3197
               (fname, to_node, msg))
3198
        lu.proc.LogWarning(msg)
3199

    
3200

    
3201
def _ComputeAncillaryFiles(cluster, redist):
3202
  """Compute files external to Ganeti which need to be consistent.
3203

3204
  @type redist: boolean
3205
  @param redist: Whether to include files which need to be redistributed
3206

3207
  """
3208
  # Compute files for all nodes
3209
  files_all = set([
3210
    constants.SSH_KNOWN_HOSTS_FILE,
3211
    constants.CONFD_HMAC_KEY,
3212
    constants.CLUSTER_DOMAIN_SECRET_FILE,
3213
    ])
3214

    
3215
  if not redist:
3216
    files_all.update(constants.ALL_CERT_FILES)
3217
    files_all.update(ssconf.SimpleStore().GetFileList())
3218

    
3219
  if cluster.modify_etc_hosts:
3220
    files_all.add(constants.ETC_HOSTS)
3221

    
3222
  # Files which must either exist on all nodes or on none
3223
  files_all_opt = set([
3224
    constants.RAPI_USERS_FILE,
3225
    ])
3226

    
3227
  # Files which should only be on master candidates
3228
  files_mc = set()
3229
  if not redist:
3230
    files_mc.add(constants.CLUSTER_CONF_FILE)
3231

    
3232
  # Files which should only be on VM-capable nodes
3233
  files_vm = set(filename
3234
    for hv_name in cluster.enabled_hypervisors
3235
    for filename in hypervisor.GetHypervisor(hv_name).GetAncillaryFiles())
3236

    
3237
  # Filenames must be unique
3238
  assert (len(files_all | files_all_opt | files_mc | files_vm) ==
3239
          sum(map(len, [files_all, files_all_opt, files_mc, files_vm]))), \
3240
         "Found file listed in more than one file list"
3241

    
3242
  return (files_all, files_all_opt, files_mc, files_vm)
3243

    
3244

    
3245
def _RedistributeAncillaryFiles(lu, additional_nodes=None, additional_vm=True):
3246
  """Distribute additional files which are part of the cluster configuration.
3247

3248
  ConfigWriter takes care of distributing the config and ssconf files, but
3249
  there are more files which should be distributed to all nodes. This function
3250
  makes sure those are copied.
3251

3252
  @param lu: calling logical unit
3253
  @param additional_nodes: list of nodes not in the config to distribute to
3254
  @type additional_vm: boolean
3255
  @param additional_vm: whether the additional nodes are vm-capable or not
3256

3257
  """
3258
  # Gather target nodes
3259
  cluster = lu.cfg.GetClusterInfo()
3260
  master_info = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
3261

    
3262
  online_nodes = lu.cfg.GetOnlineNodeList()
3263
  vm_nodes = lu.cfg.GetVmCapableNodeList()
3264

    
3265
  if additional_nodes is not None:
3266
    online_nodes.extend(additional_nodes)
3267
    if additional_vm:
3268
      vm_nodes.extend(additional_nodes)
3269

    
3270
  # Never distribute to master node
3271
  for nodelist in [online_nodes, vm_nodes]:
3272
    if master_info.name in nodelist:
3273
      nodelist.remove(master_info.name)
3274

    
3275
  # Gather file lists
3276
  (files_all, files_all_opt, files_mc, files_vm) = \
3277
    _ComputeAncillaryFiles(cluster, True)
3278

    
3279
  # Never re-distribute configuration file from here
3280
  assert not (constants.CLUSTER_CONF_FILE in files_all or
3281
              constants.CLUSTER_CONF_FILE in files_vm)
3282
  assert not files_mc, "Master candidates not handled in this function"
3283

    
3284
  filemap = [
3285
    (online_nodes, files_all),
3286
    (online_nodes, files_all_opt),
3287
    (vm_nodes, files_vm),
3288
    ]
3289

    
3290
  # Upload the files
3291
  for (node_list, files) in filemap:
3292
    for fname in files:
3293
      _UploadHelper(lu, node_list, fname)
3294

    
3295

    
3296
class LUClusterRedistConf(NoHooksLU):
3297
  """Force the redistribution of cluster configuration.
3298

3299
  This is a very simple LU.
3300

3301
  """
3302
  REQ_BGL = False
3303

    
3304
  def ExpandNames(self):
3305
    self.needed_locks = {
3306
      locking.LEVEL_NODE: locking.ALL_SET,
3307
    }
3308
    self.share_locks[locking.LEVEL_NODE] = 1
3309

    
3310
  def Exec(self, feedback_fn):
3311
    """Redistribute the configuration.
3312

3313
    """
3314
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
3315
    _RedistributeAncillaryFiles(self)
3316

    
3317

    
3318
def _WaitForSync(lu, instance, disks=None, oneshot=False):
3319
  """Sleep and poll for an instance's disk to sync.
3320

3321
  """
3322
  if not instance.disks or disks is not None and not disks:
3323
    return True
3324

    
3325
  disks = _ExpandCheckDisks(instance, disks)
3326

    
3327
  if not oneshot:
3328
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
3329

    
3330
  node = instance.primary_node
3331

    
3332
  for dev in disks:
3333
    lu.cfg.SetDiskID(dev, node)
3334

    
3335
  # TODO: Convert to utils.Retry
3336

    
3337
  retries = 0
3338
  degr_retries = 10 # in seconds, as we sleep 1 second each time
3339
  while True:
3340
    max_time = 0
3341
    done = True
3342
    cumul_degraded = False
3343
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, disks)
3344
    msg = rstats.fail_msg
3345
    if msg:
3346
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
3347
      retries += 1
3348
      if retries >= 10:
3349
        raise errors.RemoteError("Can't contact node %s for mirror data,"
3350
                                 " aborting." % node)
3351
      time.sleep(6)
3352
      continue
3353
    rstats = rstats.payload
3354
    retries = 0
3355
    for i, mstat in enumerate(rstats):
3356
      if mstat is None:
3357
        lu.LogWarning("Can't compute data for node %s/%s",
3358
                           node, disks[i].iv_name)
3359
        continue
3360

    
3361
      cumul_degraded = (cumul_degraded or
3362
                        (mstat.is_degraded and mstat.sync_percent is None))
3363
      if mstat.sync_percent is not None:
3364
        done = False
3365
        if mstat.estimated_time is not None:
3366
          rem_time = ("%s remaining (estimated)" %
3367
                      utils.FormatSeconds(mstat.estimated_time))
3368
          max_time = mstat.estimated_time
3369
        else:
3370
          rem_time = "no time estimate"
3371
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
3372
                        (disks[i].iv_name, mstat.sync_percent, rem_time))
3373

    
3374
    # if we're done but degraded, let's do a few small retries, to
3375
    # make sure we see a stable and not transient situation; therefore
3376
    # we force restart of the loop
3377
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
3378
      logging.info("Degraded disks found, %d retries left", degr_retries)
3379
      degr_retries -= 1
3380
      time.sleep(1)
3381
      continue
3382

    
3383
    if done or oneshot:
3384
      break
3385

    
3386
    time.sleep(min(60, max_time))
3387

    
3388
  if done:
3389
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
3390
  return not cumul_degraded
3391

    
3392

    
3393
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
3394
  """Check that mirrors are not degraded.
3395

3396
  The ldisk parameter, if True, will change the test from the
3397
  is_degraded attribute (which represents overall non-ok status for
3398
  the device(s)) to the ldisk (representing the local storage status).
3399

3400
  """
3401
  lu.cfg.SetDiskID(dev, node)
3402

    
3403
  result = True
3404

    
3405
  if on_primary or dev.AssembleOnSecondary():
3406
    rstats = lu.rpc.call_blockdev_find(node, dev)
3407
    msg = rstats.fail_msg
3408
    if msg:
3409
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
3410
      result = False
3411
    elif not rstats.payload:
3412
      lu.LogWarning("Can't find disk on node %s", node)
3413
      result = False
3414
    else:
3415
      if ldisk:
3416
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
3417
      else:
3418
        result = result and not rstats.payload.is_degraded
3419

    
3420
  if dev.children:
3421
    for child in dev.children:
3422
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
3423

    
3424
  return result
3425

    
3426

    
3427
class LUOobCommand(NoHooksLU):
3428
  """Logical unit for OOB handling.
3429

3430
  """
3431
  REG_BGL = False
3432
  _SKIP_MASTER = (constants.OOB_POWER_OFF, constants.OOB_POWER_CYCLE)
3433

    
3434
  def CheckPrereq(self):
3435
    """Check prerequisites.
3436

3437
    This checks:
3438
     - the node exists in the configuration
3439
     - OOB is supported
3440

3441
    Any errors are signaled by raising errors.OpPrereqError.
3442

3443
    """
3444
    self.nodes = []
3445
    self.master_node = self.cfg.GetMasterNode()
3446

    
3447
    assert self.op.power_delay >= 0.0
3448

    
3449
    if self.op.node_names:
3450
      if (self.op.command in self._SKIP_MASTER and
3451
          self.master_node in self.op.node_names):
3452
        master_node_obj = self.cfg.GetNodeInfo(self.master_node)
3453
        master_oob_handler = _SupportsOob(self.cfg, master_node_obj)
3454

    
3455
        if master_oob_handler:
3456
          additional_text = ("run '%s %s %s' if you want to operate on the"
3457
                             " master regardless") % (master_oob_handler,
3458
                                                      self.op.command,
3459
                                                      self.master_node)
3460
        else:
3461
          additional_text = "it does not support out-of-band operations"
3462

    
3463
        raise errors.OpPrereqError(("Operating on the master node %s is not"
3464
                                    " allowed for %s; %s") %
3465
                                   (self.master_node, self.op.command,
3466
                                    additional_text), errors.ECODE_INVAL)
3467
    else:
3468
      self.op.node_names = self.cfg.GetNodeList()
3469
      if self.op.command in self._SKIP_MASTER:
3470
        self.op.node_names.remove(self.master_node)
3471

    
3472
    if self.op.command in self._SKIP_MASTER:
3473
      assert self.master_node not in self.op.node_names
3474

    
3475
    for node_name in self.op.node_names:
3476
      node = self.cfg.GetNodeInfo(node_name)
3477

    
3478
      if node is None:
3479
        raise errors.OpPrereqError("Node %s not found" % node_name,
3480
                                   errors.ECODE_NOENT)
3481
      else:
3482
        self.nodes.append(node)
3483

    
3484
      if (not self.op.ignore_status and
3485
          (self.op.command == constants.OOB_POWER_OFF and not node.offline)):
3486
        raise errors.OpPrereqError(("Cannot power off node %s because it is"
3487
                                    " not marked offline") % node_name,
3488
                                   errors.ECODE_STATE)
3489

    
3490
  def ExpandNames(self):
3491
    """Gather locks we need.
3492

3493
    """
3494
    if self.op.node_names:
3495
      self.op.node_names = [_ExpandNodeName(self.cfg, name)
3496
                            for name in self.op.node_names]
3497
      lock_names = self.op.node_names
3498
    else:
3499
      lock_names = locking.ALL_SET
3500

    
3501
    self.needed_locks = {
3502
      locking.LEVEL_NODE: lock_names,
3503
      }
3504

    
3505
  def Exec(self, feedback_fn):
3506
    """Execute OOB and return result if we expect any.
3507

3508
    """
3509
    master_node = self.master_node
3510
    ret = []
3511

    
3512
    for idx, node in enumerate(self.nodes):
3513
      node_entry = [(constants.RS_NORMAL, node.name)]
3514
      ret.append(node_entry)
3515

    
3516
      oob_program = _SupportsOob(self.cfg, node)
3517

    
3518
      if not oob_program:
3519
        node_entry.append((constants.RS_UNAVAIL, None))
3520
        continue
3521

    
3522
      logging.info("Executing out-of-band command '%s' using '%s' on %s",
3523
                   self.op.command, oob_program, node.name)
3524
      result = self.rpc.call_run_oob(master_node, oob_program,
3525
                                     self.op.command, node.name,
3526
                                     self.op.timeout)
3527

    
3528
      if result.fail_msg:
3529
        self.LogWarning("Out-of-band RPC failed on node '%s': %s",
3530
                        node.name, result.fail_msg)
3531
        node_entry.append((constants.RS_NODATA, None))
3532
      else:
3533
        try:
3534
          self._CheckPayload(result)
3535
        except errors.OpExecError, err:
3536
          self.LogWarning("Payload returned by node '%s' is not valid: %s",
3537
                          node.name, err)
3538
          node_entry.append((constants.RS_NODATA, None))
3539
        else:
3540
          if self.op.command == constants.OOB_HEALTH:
3541
            # For health we should log important events
3542
            for item, status in result.payload:
3543
              if status in [constants.OOB_STATUS_WARNING,
3544
                            constants.OOB_STATUS_CRITICAL]:
3545
                self.LogWarning("Item '%s' on node '%s' has status '%s'",
3546
                                item, node.name, status)
3547

    
3548
          if self.op.command == constants.OOB_POWER_ON:
3549
            node.powered = True
3550
          elif self.op.command == constants.OOB_POWER_OFF:
3551
            node.powered = False
3552
          elif self.op.command == constants.OOB_POWER_STATUS:
3553
            powered = result.payload[constants.OOB_POWER_STATUS_POWERED]
3554
            if powered != node.powered:
3555
              logging.warning(("Recorded power state (%s) of node '%s' does not"
3556
                               " match actual power state (%s)"), node.powered,
3557
                              node.name, powered)
3558

    
3559
          # For configuration changing commands we should update the node
3560
          if self.op.command in (constants.OOB_POWER_ON,
3561
                                 constants.OOB_POWER_OFF):
3562
            self.cfg.Update(node, feedback_fn)
3563

    
3564
          node_entry.append((constants.RS_NORMAL, result.payload))
3565

    
3566
          if (self.op.command == constants.OOB_POWER_ON and
3567
              idx < len(self.nodes) - 1):
3568
            time.sleep(self.op.power_delay)
3569

    
3570
    return ret
3571

    
3572
  def _CheckPayload(self, result):
3573
    """Checks if the payload is valid.
3574

3575
    @param result: RPC result
3576
    @raises errors.OpExecError: If payload is not valid
3577

3578
    """
3579
    errs = []
3580
    if self.op.command == constants.OOB_HEALTH:
3581
      if not isinstance(result.payload, list):
3582
        errs.append("command 'health' is expected to return a list but got %s" %
3583
                    type(result.payload))
3584
      else:
3585
        for item, status in result.payload:
3586
          if status not in constants.OOB_STATUSES:
3587
            errs.append("health item '%s' has invalid status '%s'" %
3588
                        (item, status))
3589

    
3590
    if self.op.command == constants.OOB_POWER_STATUS:
3591
      if not isinstance(result.payload, dict):
3592
        errs.append("power-status is expected to return a dict but got %s" %
3593
                    type(result.payload))
3594

    
3595
    if self.op.command in [
3596
        constants.OOB_POWER_ON,
3597
        constants.OOB_POWER_OFF,
3598
        constants.OOB_POWER_CYCLE,
3599
        ]:
3600
      if result.payload is not None:
3601
        errs.append("%s is expected to not return payload but got '%s'" %
3602
                    (self.op.command, result.payload))
3603

    
3604
    if errs:
3605
      raise errors.OpExecError("Check of out-of-band payload failed due to %s" %
3606
                               utils.CommaJoin(errs))
3607

    
3608
class _OsQuery(_QueryBase):
3609
  FIELDS = query.OS_FIELDS
3610

    
3611
  def ExpandNames(self, lu):
3612
    # Lock all nodes in shared mode
3613
    # Temporary removal of locks, should be reverted later
3614
    # TODO: reintroduce locks when they are lighter-weight
3615
    lu.needed_locks = {}
3616
    #self.share_locks[locking.LEVEL_NODE] = 1
3617
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3618

    
3619
    # The following variables interact with _QueryBase._GetNames
3620
    if self.names:
3621
      self.wanted = self.names
3622
    else:
3623
      self.wanted = locking.ALL_SET
3624

    
3625
    self.do_locking = self.use_locking
3626

    
3627
  def DeclareLocks(self, lu, level):
3628
    pass
3629

    
3630
  @staticmethod
3631
  def _DiagnoseByOS(rlist):
3632
    """Remaps a per-node return list into an a per-os per-node dictionary
3633

3634
    @param rlist: a map with node names as keys and OS objects as values
3635

3636
    @rtype: dict
3637
    @return: a dictionary with osnames as keys and as value another
3638
        map, with nodes as keys and tuples of (path, status, diagnose,
3639
        variants, parameters, api_versions) as values, eg::
3640

3641
          {"debian-etch": {"node1": [(/usr/lib/..., True, "", [], []),
3642
                                     (/srv/..., False, "invalid api")],
3643
                           "node2": [(/srv/..., True, "", [], [])]}
3644
          }
3645

3646
    """
3647
    all_os = {}
3648
    # we build here the list of nodes that didn't fail the RPC (at RPC
3649
    # level), so that nodes with a non-responding node daemon don't
3650
    # make all OSes invalid
3651
    good_nodes = [node_name for node_name in rlist
3652
                  if not rlist[node_name].fail_msg]
3653
    for node_name, nr in rlist.items():
3654
      if nr.fail_msg or not nr.payload:
3655
        continue
3656
      for (name, path, status, diagnose, variants,
3657
           params, api_versions) in nr.payload:
3658
        if name not in all_os:
3659
          # build a list of nodes for this os containing empty lists
3660
          # for each node in node_list
3661
          all_os[name] = {}
3662
          for nname in good_nodes:
3663
            all_os[name][nname] = []
3664
        # convert params from [name, help] to (name, help)
3665
        params = [tuple(v) for v in params]
3666
        all_os[name][node_name].append((path, status, diagnose,
3667
                                        variants, params, api_versions))
3668
    return all_os
3669

    
3670
  def _GetQueryData(self, lu):
3671
    """Computes the list of nodes and their attributes.
3672

3673
    """
3674
    # Locking is not used
3675
    assert not (lu.acquired_locks or self.do_locking or self.use_locking)
3676

    
3677
    valid_nodes = [node.name
3678
                   for node in lu.cfg.GetAllNodesInfo().values()
3679
                   if not node.offline and node.vm_capable]
3680
    pol = self._DiagnoseByOS(lu.rpc.call_os_diagnose(valid_nodes))
3681
    cluster = lu.cfg.GetClusterInfo()
3682

    
3683
    data = {}
3684

    
3685
    for (os_name, os_data) in pol.items():
3686
      info = query.OsInfo(name=os_name, valid=True, node_status=os_data,
3687
                          hidden=(os_name in cluster.hidden_os),
3688
                          blacklisted=(os_name in cluster.blacklisted_os))
3689

    
3690
      variants = set()
3691
      parameters = set()
3692
      api_versions = set()
3693

    
3694
      for idx, osl in enumerate(os_data.values()):
3695
        info.valid = bool(info.valid and osl and osl[0][1])
3696
        if not info.valid:
3697
          break
3698

    
3699
        (node_variants, node_params, node_api) = osl[0][3:6]
3700
        if idx == 0:
3701
          # First entry
3702
          variants.update(node_variants)
3703
          parameters.update(node_params)
3704
          api_versions.update(node_api)
3705
        else:
3706
          # Filter out inconsistent values
3707
          variants.intersection_update(node_variants)
3708
          parameters.intersection_update(node_params)
3709
          api_versions.intersection_update(node_api)
3710

    
3711
      info.variants = list(variants)
3712
      info.parameters = list(parameters)
3713
      info.api_versions = list(api_versions)
3714

    
3715
      data[os_name] = info
3716

    
3717
    # Prepare data in requested order
3718
    return [data[name] for name in self._GetNames(lu, pol.keys(), None)
3719
            if name in data]
3720

    
3721

    
3722
class LUOsDiagnose(NoHooksLU):
3723
  """Logical unit for OS diagnose/query.
3724

3725
  """
3726
  REQ_BGL = False
3727

    
3728
  @staticmethod
3729
  def _BuildFilter(fields, names):
3730
    """Builds a filter for querying OSes.
3731

3732
    """
3733
    name_filter = qlang.MakeSimpleFilter("name", names)
3734

    
3735
    # Legacy behaviour: Hide hidden, blacklisted or invalid OSes if the
3736
    # respective field is not requested
3737
    status_filter = [[qlang.OP_NOT, [qlang.OP_TRUE, fname]]
3738
                     for fname in ["hidden", "blacklisted"]
3739
                     if fname not in fields]
3740
    if "valid" not in fields:
3741
      status_filter.append([qlang.OP_TRUE, "valid"])
3742

    
3743
    if status_filter:
3744
      status_filter.insert(0, qlang.OP_AND)
3745
    else:
3746
      status_filter = None
3747

    
3748
    if name_filter and status_filter:
3749
      return [qlang.OP_AND, name_filter, status_filter]
3750
    elif name_filter:
3751
      return name_filter
3752
    else:
3753
      return status_filter
3754

    
3755
  def CheckArguments(self):
3756
    self.oq = _OsQuery(self._BuildFilter(self.op.output_fields, self.op.names),
3757
                       self.op.output_fields, False)
3758

    
3759
  def ExpandNames(self):
3760
    self.oq.ExpandNames(self)
3761

    
3762
  def Exec(self, feedback_fn):
3763
    return self.oq.OldStyleQuery(self)
3764

    
3765

    
3766
class LUNodeRemove(LogicalUnit):
3767
  """Logical unit for removing a node.
3768

3769
  """
3770
  HPATH = "node-remove"
3771
  HTYPE = constants.HTYPE_NODE
3772

    
3773
  def BuildHooksEnv(self):
3774
    """Build hooks env.
3775

3776
    This doesn't run on the target node in the pre phase as a failed
3777
    node would then be impossible to remove.
3778

3779
    """
3780
    return {
3781
      "OP_TARGET": self.op.node_name,
3782
      "NODE_NAME": self.op.node_name,
3783
      }
3784

    
3785
  def BuildHooksNodes(self):
3786
    """Build hooks nodes.
3787

3788
    """
3789
    all_nodes = self.cfg.GetNodeList()
3790
    try:
3791
      all_nodes.remove(self.op.node_name)
3792
    except ValueError:
3793
      logging.warning("Node '%s', which is about to be removed, was not found"
3794
                      " in the list of all nodes", self.op.node_name)
3795
    return (all_nodes, all_nodes)
3796

    
3797
  def CheckPrereq(self):
3798
    """Check prerequisites.
3799

3800
    This checks:
3801
     - the node exists in the configuration
3802
     - it does not have primary or secondary instances
3803
     - it's not the master
3804

3805
    Any errors are signaled by raising errors.OpPrereqError.
3806

3807
    """
3808
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
3809
    node = self.cfg.GetNodeInfo(self.op.node_name)
3810
    assert node is not None
3811

    
3812
    instance_list = self.cfg.GetInstanceList()
3813

    
3814
    masternode = self.cfg.GetMasterNode()
3815
    if node.name == masternode:
3816
      raise errors.OpPrereqError("Node is the master node, failover to another"
3817
                                 " node is required", errors.ECODE_INVAL)
3818

    
3819
    for instance_name in instance_list:
3820
      instance = self.cfg.GetInstanceInfo(instance_name)
3821
      if node.name in instance.all_nodes:
3822
        raise errors.OpPrereqError("Instance %s is still running on the node,"
3823
                                   " please remove first" % instance_name,
3824
                                   errors.ECODE_INVAL)
3825
    self.op.node_name = node.name
3826
    self.node = node
3827

    
3828
  def Exec(self, feedback_fn):
3829
    """Removes the node from the cluster.
3830

3831
    """
3832
    node = self.node
3833
    logging.info("Stopping the node daemon and removing configs from node %s",
3834
                 node.name)
3835

    
3836
    modify_ssh_setup = self.cfg.GetClusterInfo().modify_ssh_setup
3837

    
3838
    # Promote nodes to master candidate as needed
3839
    _AdjustCandidatePool(self, exceptions=[node.name])
3840
    self.context.RemoveNode(node.name)
3841

    
3842
    # Run post hooks on the node before it's removed
3843
    _RunPostHook(self, node.name)
3844

    
3845
    result = self.rpc.call_node_leave_cluster(node.name, modify_ssh_setup)
3846
    msg = result.fail_msg
3847
    if msg:
3848
      self.LogWarning("Errors encountered on the remote node while leaving"
3849
                      " the cluster: %s", msg)
3850

    
3851
    # Remove node from our /etc/hosts
3852
    if self.cfg.GetClusterInfo().modify_etc_hosts:
3853
      master_node = self.cfg.GetMasterNode()
3854
      result = self.rpc.call_etc_hosts_modify(master_node,
3855
                                              constants.ETC_HOSTS_REMOVE,
3856
                                              node.name, None)
3857
      result.Raise("Can't update hosts file with new host data")
3858
      _RedistributeAncillaryFiles(self)
3859

    
3860

    
3861
class _NodeQuery(_QueryBase):
3862
  FIELDS = query.NODE_FIELDS
3863

    
3864
  def ExpandNames(self, lu):
3865
    lu.needed_locks = {}
3866
    lu.share_locks[locking.LEVEL_NODE] = 1
3867

    
3868
    if self.names:
3869
      self.wanted = _GetWantedNodes(lu, self.names)
3870
    else:
3871
      self.wanted = locking.ALL_SET
3872

    
3873
    self.do_locking = (self.use_locking and
3874
                       query.NQ_LIVE in self.requested_data)
3875

    
3876
    if self.do_locking:
3877
      # if we don't request only static fields, we need to lock the nodes
3878
      lu.needed_locks[locking.LEVEL_NODE] = self.wanted
3879

    
3880
  def DeclareLocks(self, lu, level):
3881
    pass
3882

    
3883
  def _GetQueryData(self, lu):
3884
    """Computes the list of nodes and their attributes.
3885

3886
    """
3887
    all_info = lu.cfg.GetAllNodesInfo()
3888

    
3889
    nodenames = self._GetNames(lu, all_info.keys(), locking.LEVEL_NODE)
3890

    
3891
    # Gather data as requested
3892
    if query.NQ_LIVE in self.requested_data:
3893
      # filter out non-vm_capable nodes
3894
      toquery_nodes = [name for name in nodenames if all_info[name].vm_capable]
3895

    
3896
      node_data = lu.rpc.call_node_info(toquery_nodes, lu.cfg.GetVGName(),
3897
                                        lu.cfg.GetHypervisorType())
3898
      live_data = dict((name, nresult.payload)
3899
                       for (name, nresult) in node_data.items()
3900
                       if not nresult.fail_msg and nresult.payload)
3901
    else:
3902
      live_data = None
3903

    
3904
    if query.NQ_INST in self.requested_data:
3905
      node_to_primary = dict([(name, set()) for name in nodenames])
3906
      node_to_secondary = dict([(name, set()) for name in nodenames])
3907

    
3908
      inst_data = lu.cfg.GetAllInstancesInfo()
3909

    
3910
      for inst in inst_data.values():
3911
        if inst.primary_node in node_to_primary:
3912
          node_to_primary[inst.primary_node].add(inst.name)
3913
        for secnode in inst.secondary_nodes:
3914
          if secnode in node_to_secondary:
3915
            node_to_secondary[secnode].add(inst.name)
3916
    else:
3917
      node_to_primary = None
3918
      node_to_secondary = None
3919

    
3920
    if query.NQ_OOB in self.requested_data:
3921
      oob_support = dict((name, bool(_SupportsOob(lu.cfg, node)))
3922
                         for name, node in all_info.iteritems())
3923
    else:
3924
      oob_support = None
3925

    
3926
    if query.NQ_GROUP in self.requested_data:
3927
      groups = lu.cfg.GetAllNodeGroupsInfo()
3928
    else:
3929
      groups = {}
3930

    
3931
    return query.NodeQueryData([all_info[name] for name in nodenames],
3932
                               live_data, lu.cfg.GetMasterNode(),
3933
                               node_to_primary, node_to_secondary, groups,
3934
                               oob_support, lu.cfg.GetClusterInfo())
3935

    
3936

    
3937
class LUNodeQuery(NoHooksLU):
3938
  """Logical unit for querying nodes.
3939

3940
  """
3941
  # pylint: disable-msg=W0142
3942
  REQ_BGL = False
3943

    
3944
  def CheckArguments(self):
3945
    self.nq = _NodeQuery(qlang.MakeSimpleFilter("name", self.op.names),
3946
                         self.op.output_fields, self.op.use_locking)
3947

    
3948
  def ExpandNames(self):
3949
    self.nq.ExpandNames(self)
3950

    
3951
  def Exec(self, feedback_fn):
3952
    return self.nq.OldStyleQuery(self)
3953

    
3954

    
3955
class LUNodeQueryvols(NoHooksLU):
3956
  """Logical unit for getting volumes on node(s).
3957

3958
  """
3959
  REQ_BGL = False
3960
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
3961
  _FIELDS_STATIC = utils.FieldSet("node")
3962

    
3963
  def CheckArguments(self):
3964
    _CheckOutputFields(static=self._FIELDS_STATIC,
3965
                       dynamic=self._FIELDS_DYNAMIC,
3966
                       selected=self.op.output_fields)
3967

    
3968
  def ExpandNames(self):
3969
    self.needed_locks = {}
3970
    self.share_locks[locking.LEVEL_NODE] = 1
3971
    if not self.op.nodes:
3972
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3973
    else:
3974
      self.needed_locks[locking.LEVEL_NODE] = \
3975
        _GetWantedNodes(self, self.op.nodes)
3976

    
3977
  def Exec(self, feedback_fn):
3978
    """Computes the list of nodes and their attributes.
3979

3980
    """
3981
    nodenames = self.acquired_locks[locking.LEVEL_NODE]
3982
    volumes = self.rpc.call_node_volumes(nodenames)
3983

    
3984
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
3985
             in self.cfg.GetInstanceList()]
3986

    
3987
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
3988

    
3989
    output = []
3990
    for node in nodenames:
3991
      nresult = volumes[node]
3992
      if nresult.offline:
3993
        continue
3994
      msg = nresult.fail_msg
3995
      if msg:
3996
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
3997
        continue
3998

    
3999
      node_vols = nresult.payload[:]
4000
      node_vols.sort(key=lambda vol: vol['dev'])
4001

    
4002
      for vol in node_vols:
4003
        node_output = []
4004
        for field in self.op.output_fields:
4005
          if field == "node":
4006
            val = node
4007
          elif field == "phys":
4008
            val = vol['dev']
4009
          elif field == "vg":
4010
            val = vol['vg']
4011
          elif field == "name":
4012
            val = vol['name']
4013
          elif field == "size":
4014
            val = int(float(vol['size']))
4015
          elif field == "instance":
4016
            for inst in ilist:
4017
              if node not in lv_by_node[inst]:
4018
                continue
4019
              if vol['name'] in lv_by_node[inst][node]:
4020
                val = inst.name
4021
                break
4022
            else:
4023
              val = '-'
4024
          else:
4025
            raise errors.ParameterError(field)
4026
          node_output.append(str(val))
4027

    
4028
        output.append(node_output)
4029

    
4030
    return output
4031

    
4032

    
4033
class LUNodeQueryStorage(NoHooksLU):
4034
  """Logical unit for getting information on storage units on node(s).
4035

4036
  """
4037
  _FIELDS_STATIC = utils.FieldSet(constants.SF_NODE)
4038
  REQ_BGL = False
4039

    
4040
  def CheckArguments(self):
4041
    _CheckOutputFields(static=self._FIELDS_STATIC,
4042
                       dynamic=utils.FieldSet(*constants.VALID_STORAGE_FIELDS),
4043
                       selected=self.op.output_fields)
4044

    
4045
  def ExpandNames(self):
4046
    self.needed_locks = {}
4047
    self.share_locks[locking.LEVEL_NODE] = 1
4048

    
4049
    if self.op.nodes:
4050
      self.needed_locks[locking.LEVEL_NODE] = \
4051
        _GetWantedNodes(self, self.op.nodes)
4052
    else:
4053
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
4054

    
4055
  def Exec(self, feedback_fn):
4056
    """Computes the list of nodes and their attributes.
4057

4058
    """
4059
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
4060

    
4061
    # Always get name to sort by
4062
    if constants.SF_NAME in self.op.output_fields:
4063
      fields = self.op.output_fields[:]
4064
    else:
4065
      fields = [constants.SF_NAME] + self.op.output_fields
4066

    
4067
    # Never ask for node or type as it's only known to the LU
4068
    for extra in [constants.SF_NODE, constants.SF_TYPE]:
4069
      while extra in fields:
4070
        fields.remove(extra)
4071

    
4072
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
4073
    name_idx = field_idx[constants.SF_NAME]
4074

    
4075
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
4076
    data = self.rpc.call_storage_list(self.nodes,
4077
                                      self.op.storage_type, st_args,
4078
                                      self.op.name, fields)
4079

    
4080
    result = []
4081

    
4082
    for node in utils.NiceSort(self.nodes):
4083
      nresult = data[node]
4084
      if nresult.offline:
4085
        continue
4086

    
4087
      msg = nresult.fail_msg
4088
      if msg:
4089
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
4090
        continue
4091

    
4092
      rows = dict([(row[name_idx], row) for row in nresult.payload])
4093

    
4094
      for name in utils.NiceSort(rows.keys()):
4095
        row = rows[name]
4096

    
4097
        out = []
4098

    
4099
        for field in self.op.output_fields:
4100
          if field == constants.SF_NODE:
4101
            val = node
4102