Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ e730f372

History | View | Annotate | Download (425.9 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.glm = context.glm
133
    self.context = context
134
    self.rpc = rpc
135
    # Dicts used to declare locking needs to mcpu
136
    self.needed_locks = None
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.glm.list_owned(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.glm.list_owned(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.glm.is_owned(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.glm.list_owned(level):
660
      if should_release(name):
661
        release.append(name)
662
      else:
663
        retain.append(name)
664

    
665
    assert len(lu.glm.list_owned(level)) == (len(retain) + len(release))
666

    
667
    # Release just some locks
668
    lu.glm.release(level, names=release)
669

    
670
    assert frozenset(lu.glm.list_owned(level)) == frozenset(retain)
671
  else:
672
    # Release everything
673
    lu.glm.release(level)
674

    
675
    assert not lu.glm.is_owned(level), "No locks should be owned"
676

    
677

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

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

    
689

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

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

698
  """
699
  f = utils.FieldSet()
700
  f.Extend(static)
701
  f.Extend(dynamic)
702

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

    
708

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

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

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

    
723

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

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

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

    
738

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

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

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

    
751

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

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

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

    
764

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

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

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

    
782

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

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

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

    
809

    
810
def _GetClusterDomainSecret():
811
  """Reads the cluster domain secret.
812

813
  """
814
  return utils.ReadOneLineFile(constants.CLUSTER_DOMAIN_SECRET_FILE,
815
                               strict=True)
816

    
817

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

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

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

    
833

    
834
def _ExpandItemName(fn, name, kind):
835
  """Expand an item name.
836

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

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

    
850

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

    
855

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

    
860

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

866
  This builds the hook environment from individual variables.
867

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

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

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

    
930
  env["INSTANCE_NIC_COUNT"] = nic_count
931

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

    
940
  env["INSTANCE_DISK_COUNT"] = disk_count
941

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

    
946
  return env
947

    
948

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

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

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

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

    
972

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

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

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

    
1010

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

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

    
1026

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

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

    
1037

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

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

    
1051

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

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

    
1060

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

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

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

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

    
1080

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

    
1084

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

1088
  """
1089

    
1090
  return _GetNodeInstancesInner(cfg, lambda inst: node_name in inst.all_nodes)
1091

    
1092

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

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

    
1100

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

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

    
1108

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

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

    
1118
  return []
1119

    
1120

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

    
1124
  for dev in instance.disks:
1125
    cfg.SetDiskID(dev, node_name)
1126

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

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

    
1135
  return faulty
1136

    
1137

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

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

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

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

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

    
1169

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

1173
  """
1174
  HPATH = "cluster-init"
1175
  HTYPE = constants.HTYPE_CLUSTER
1176

    
1177
  def BuildHooksEnv(self):
1178
    """Build hooks env.
1179

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

    
1185
  def BuildHooksNodes(self):
1186
    """Build hooks nodes.
1187

1188
    """
1189
    return ([], [self.cfg.GetMasterNode()])
1190

    
1191
  def Exec(self, feedback_fn):
1192
    """Nothing to do.
1193

1194
    """
1195
    return True
1196

    
1197

    
1198
class LUClusterDestroy(LogicalUnit):
1199
  """Logical unit for destroying the cluster.
1200

1201
  """
1202
  HPATH = "cluster-destroy"
1203
  HTYPE = constants.HTYPE_CLUSTER
1204

    
1205
  def BuildHooksEnv(self):
1206
    """Build hooks env.
1207

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

    
1213
  def BuildHooksNodes(self):
1214
    """Build hooks nodes.
1215

1216
    """
1217
    return ([], [])
1218

    
1219
  def CheckPrereq(self):
1220
    """Check prerequisites.
1221

1222
    This checks whether the cluster is empty.
1223

1224
    Any errors are signaled by raising errors.OpPrereqError.
1225

1226
    """
1227
    master = self.cfg.GetMasterNode()
1228

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

    
1240
  def Exec(self, feedback_fn):
1241
    """Destroys the cluster.
1242

1243
    """
1244
    master = self.cfg.GetMasterNode()
1245

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

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

    
1252
    return master
1253

    
1254

    
1255
def _VerifyCertificate(filename):
1256
  """Verifies a certificate for LUClusterVerify.
1257

1258
  @type filename: string
1259
  @param filename: Path to PEM file
1260

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

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

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

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

    
1285
  raise errors.ProgrammerError("Unhandled certificate error code %r" % errcode)
1286

    
1287

    
1288
class LUClusterVerify(LogicalUnit):
1289
  """Verifies the cluster status.
1290

1291
  """
1292
  HPATH = "cluster-verify"
1293
  HTYPE = constants.HTYPE_CLUSTER
1294
  REQ_BGL = False
1295

    
1296
  TCLUSTER = "cluster"
1297
  TNODE = "node"
1298
  TINSTANCE = "instance"
1299

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

    
1328
  ETYPE_FIELD = "code"
1329
  ETYPE_ERROR = "ERROR"
1330
  ETYPE_WARNING = "WARNING"
1331

    
1332
  _HOOKS_INDENT_RE = re.compile("^", re.M)
1333

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

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

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

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

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

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

1397
    This must be called only from Exec and functions called from Exec.
1398

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

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

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

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

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

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

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

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

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

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

    
1471
    # node seems compatible, we can actually try to look into its results
1472

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

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

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

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

    
1499
    return True
1500

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

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

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

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

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

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

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

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

1541
    """
1542
    if vg_name is None:
1543
      return
1544

    
1545
    node = ninfo.name
1546
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
1547

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

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

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

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

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

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

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

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

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

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

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

    
1623
    node_vol_should = {}
1624
    instanceconfig.MapLVsByNode(node_vol_should)
1625

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

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

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

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

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

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

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

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

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

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

1691
    This checks what instances are running but unknown to the cluster.
1692

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

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

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

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

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

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

1745
    """
1746
    node_names = frozenset(node.name for node in nodeinfo)
1747

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

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

    
1762
    fileinfo = dict((filename, {}) for filename in file2nodefn.keys())
1763

    
1764
    for node in nodeinfo:
1765
      nresult = all_nvinfo[node.name]
1766

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

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

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

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

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

    
1792
      # Nodes missing file
1793
      missing_file = node_names - with_file
1794

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1904
    nimg.os_fail = test
1905

    
1906
    if test:
1907
      return
1908

    
1909
    os_dict = {}
1910

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

    
1914
      if name not in os_dict:
1915
        os_dict[name] = []
1916

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

    
1923
    nimg.oslist = os_dict
1924

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

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

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

    
1937
    assert not nimg.os_fail, "Entered _VerifyNodeOS with failed OS rpc?"
1938

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2099
    """
2100
    _ErrorIf = self._ErrorIf # pylint: disable-msg=C0103
2101

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

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

    
2116
      if not disks:
2117
        # No need to collect data
2118
        continue
2119

    
2120
      node_disks[nname] = disks
2121

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

    
2126
      for dev in devonly:
2127
        self.cfg.SetDiskID(dev, nname)
2128

    
2129
      node_disks_devonly[nname] = devonly
2130

    
2131
    assert len(node_disks) == len(node_disks_devonly)
2132

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

    
2137
    assert len(result) == len(node_disks)
2138

    
2139
    instdisk = {}
2140

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

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

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

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

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

    
2180
    return instdisk
2181

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

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

    
2196
  def BuildHooksEnv(self):
2197
    """Build hooks env.
2198

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

2202
    """
2203
    cfg = self.cfg
2204

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

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

    
2212
    return env
2213

    
2214
  def BuildHooksNodes(self):
2215
    """Build hooks nodes.
2216

2217
    """
2218
    return ([], self.cfg.GetNodeList())
2219

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

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

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

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

    
2255
    # FIXME: verify OS list
2256

    
2257
    # File verification
2258
    filemap = _ComputeAncillaryFiles(cluster, False)
2259

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

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

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

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

    
2312
    if drbd_helper:
2313
      node_verify_param[constants.NV_DRBDHELPER] = drbd_helper
2314

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

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

    
2328
    if oob_paths:
2329
      node_verify_param[constants.NV_OOB_PATHS] = oob_paths
2330

    
2331
    for instance in instancelist:
2332
      inst_config = instanceinfo[instance]
2333

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

    
2341
      inst_config.MapLVsByNode(node_vol_should)
2342

    
2343
      pnode = inst_config.primary_node
2344
      node_image[pnode].pinst.append(instance)
2345

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

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

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

    
2365
    all_drbd_map = self.cfg.ComputeDRBDMap()
2366

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

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

    
2373
    feedback_fn("* Verifying node status")
2374

    
2375
    refos_img = None
2376

    
2377
    for node_i in nodeinfo:
2378
      node = node_i.name
2379
      nimg = node_image[node]
2380

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

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

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

    
2405
      nresult = all_nvinfo[node].payload
2406

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2488
        if s_img.offline:
2489
          inst_nodes_offline.append(snode)
2490

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

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

    
2506
    feedback_fn("* Verifying orphan instances")
2507
    self._VerifyOrphanInstances(instancelist, node_image)
2508

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

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

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

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

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

    
2528
    return not self.bad
2529

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

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

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

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

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

    
2573
      return lu_result
2574

    
2575

    
2576
class LUClusterVerifyDisks(NoHooksLU):
2577
  """Verifies the cluster disks status.
2578

2579
  """
2580
  REQ_BGL = False
2581

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

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

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

2597
    """
2598
    result = res_nodes, res_instances, res_missing = {}, [], {}
2599

    
2600
    nodes = utils.NiceSort(self.cfg.GetVmCapableNodeList())
2601
    instances = self.cfg.GetAllInstancesInfo().values()
2602

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

    
2614
    if not nv_dict:
2615
      return result
2616

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

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

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

    
2641
    return result
2642

    
2643

    
2644
class LUClusterRepairDiskSizes(NoHooksLU):
2645
  """Verifies the cluster disks sizes.
2646

2647
  """
2648
  REQ_BGL = False
2649

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

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

    
2673
  def CheckPrereq(self):
2674
    """Check prerequisites.
2675

2676
    This only checks the optional instance list against the existing names.
2677

2678
    """
2679
    if self.wanted_names is None:
2680
      self.wanted_names = self.glm.list_owned(locking.LEVEL_INSTANCE)
2681

    
2682
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
2683
                             in self.wanted_names]
2684

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

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

2691
    @param disk: an L{ganeti.objects.Disk} object
2692

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

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

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

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

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

    
2760

    
2761
class LUClusterRename(LogicalUnit):
2762
  """Rename the cluster.
2763

2764
  """
2765
  HPATH = "cluster-rename"
2766
  HTYPE = constants.HTYPE_CLUSTER
2767

    
2768
  def BuildHooksEnv(self):
2769
    """Build hooks env.
2770

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

    
2777
  def BuildHooksNodes(self):
2778
    """Build hooks nodes.
2779

2780
    """
2781
    return ([self.cfg.GetMasterNode()], self.cfg.GetNodeList())
2782

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

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

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

    
2804
    self.op.name = new_name
2805

    
2806
  def Exec(self, feedback_fn):
2807
    """Rename the cluster.
2808

2809
    """
2810
    clustername = self.op.name
2811
    ip = self.ip
2812

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

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

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

    
2839
    return clustername
2840

    
2841

    
2842
class LUClusterSetParams(LogicalUnit):
2843
  """Change the parameters of the cluster.
2844

2845
  """
2846
  HPATH = "cluster-modify"
2847
  HTYPE = constants.HTYPE_CLUSTER
2848
  REQ_BGL = False
2849

    
2850
  def CheckArguments(self):
2851
    """Check parameters
2852

2853
    """
2854
    if self.op.uid_pool:
2855
      uidpool.CheckUidPool(self.op.uid_pool)
2856

    
2857
    if self.op.add_uids:
2858
      uidpool.CheckUidPool(self.op.add_uids)
2859

    
2860
    if self.op.remove_uids:
2861
      uidpool.CheckUidPool(self.op.remove_uids)
2862

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

    
2871
  def BuildHooksEnv(self):
2872
    """Build hooks env.
2873

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

    
2880
  def BuildHooksNodes(self):
2881
    """Build hooks nodes.
2882

2883
    """
2884
    mn = self.cfg.GetMasterNode()
2885
    return ([mn], [mn])
2886

    
2887
  def CheckPrereq(self):
2888
    """Check prerequisites.
2889

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

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

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

    
2905
    node_list = self.glm.list_owned(locking.LEVEL_NODE)
2906

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

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

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

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

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

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

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

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

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

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

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

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

    
3015
        self.new_osp[os_name] = _GetUpdatedParams(self.new_osp[os_name], osp,
3016
                                                  use_none=True)
3017

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

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

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

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

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

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

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

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

    
3119
    if self.op.maintain_node_health is not None:
3120
      self.cluster.maintain_node_health = self.op.maintain_node_health
3121

    
3122
    if self.op.prealloc_wipe_disks is not None:
3123
      self.cluster.prealloc_wipe_disks = self.op.prealloc_wipe_disks
3124

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

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

    
3131
    if self.op.uid_pool is not None:
3132
      self.cluster.uid_pool = self.op.uid_pool
3133

    
3134
    if self.op.default_iallocator is not None:
3135
      self.cluster.default_iallocator = self.op.default_iallocator
3136

    
3137
    if self.op.reserved_lvs is not None:
3138
      self.cluster.reserved_lvs = self.op.reserved_lvs
3139

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

    
3157
    if self.op.hidden_os:
3158
      helper_os("hidden_os", self.op.hidden_os, "hidden")
3159

    
3160
    if self.op.blacklisted_os:
3161
      helper_os("blacklisted_os", self.op.blacklisted_os, "blacklisted")
3162

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

    
3173
    self.cfg.Update(self.cluster, feedback_fn)
3174

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

    
3184

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

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

    
3198

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

3202
  @type redist: boolean
3203
  @param redist: Whether to include files which need to be redistributed
3204

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

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

    
3217
  if cluster.modify_etc_hosts:
3218
    files_all.add(constants.ETC_HOSTS)
3219

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

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

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

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

    
3240
  return (files_all, files_all_opt, files_mc, files_vm)
3241

    
3242

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

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

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

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

    
3260
  online_nodes = lu.cfg.GetOnlineNodeList()
3261
  vm_nodes = lu.cfg.GetVmCapableNodeList()
3262

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

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

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

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

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

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

    
3293

    
3294
class LUClusterRedistConf(NoHooksLU):
3295
  """Force the redistribution of cluster configuration.
3296

3297
  This is a very simple LU.
3298

3299
  """
3300
  REQ_BGL = False
3301

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

    
3308
  def Exec(self, feedback_fn):
3309
    """Redistribute the configuration.
3310

3311
    """
3312
    self.cfg.Update(self.cfg.GetClusterInfo(), feedback_fn)
3313
    _RedistributeAncillaryFiles(self)
3314

    
3315

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

3319
  """
3320
  if not instance.disks or disks is not None and not disks:
3321
    return True
3322

    
3323
  disks = _ExpandCheckDisks(instance, disks)
3324

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

    
3328
  node = instance.primary_node
3329

    
3330
  for dev in disks:
3331
    lu.cfg.SetDiskID(dev, node)
3332

    
3333
  # TODO: Convert to utils.Retry
3334

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

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

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

    
3381
    if done or oneshot:
3382
      break
3383

    
3384
    time.sleep(min(60, max_time))
3385

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

    
3390

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

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

3398
  """
3399
  lu.cfg.SetDiskID(dev, node)
3400

    
3401
  result = True
3402

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

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

    
3422
  return result
3423

    
3424

    
3425
class LUOobCommand(NoHooksLU):
3426
  """Logical unit for OOB handling.
3427

3428
  """
3429
  REG_BGL = False
3430
  _SKIP_MASTER = (constants.OOB_POWER_OFF, constants.OOB_POWER_CYCLE)
3431

    
3432
  def CheckPrereq(self):
3433
    """Check prerequisites.
3434

3435
    This checks:
3436
     - the node exists in the configuration
3437
     - OOB is supported
3438

3439
    Any errors are signaled by raising errors.OpPrereqError.
3440

3441
    """
3442
    self.nodes = []
3443
    self.master_node = self.cfg.GetMasterNode()
3444

    
3445
    assert self.op.power_delay >= 0.0
3446

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

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

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

    
3470
    if self.op.command in self._SKIP_MASTER:
3471
      assert self.master_node not in self.op.node_names
3472

    
3473
    for node_name in self.op.node_names:
3474
      node = self.cfg.GetNodeInfo(node_name)
3475

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

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

    
3488
  def ExpandNames(self):
3489
    """Gather locks we need.
3490

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

    
3499
    self.needed_locks = {
3500
      locking.LEVEL_NODE: lock_names,
3501
      }
3502

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

3506
    """
3507
    master_node = self.master_node
3508
    ret = []
3509

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

    
3514
      oob_program = _SupportsOob(self.cfg, node)
3515

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

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

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

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

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

    
3562
          node_entry.append((constants.RS_NORMAL, result.payload))
3563

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

    
3568
    return ret
3569

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

3573
    @param result: RPC result
3574
    @raises errors.OpExecError: If payload is not valid
3575

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

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

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

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

    
3606
class _OsQuery(_QueryBase):
3607
  FIELDS = query.OS_FIELDS
3608

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

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

    
3623
    self.do_locking = self.use_locking
3624

    
3625
  def DeclareLocks(self, lu, level):
3626
    pass
3627

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

3632
    @param rlist: a map with node names as keys and OS objects as values
3633

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

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

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

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

3671
    """
3672
    # Locking is not used
3673
    assert not (compat.any(lu.glm.is_owned(level)
3674
                           for level in locking.LEVELS
3675
                           if level != locking.LEVEL_CLUSTER) or
3676
                self.do_locking or self.use_locking)
3677

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

    
3684
    data = {}
3685

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

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

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

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

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

    
3716
      data[os_name] = info
3717

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

    
3722

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

3726
  """
3727
  REQ_BGL = False
3728

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

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

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

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

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

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

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

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

    
3766

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

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

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

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

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

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

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

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

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

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

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

    
3813
    instance_list = self.cfg.GetInstanceList()
3814

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

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

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

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

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

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

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

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

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

    
3861

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

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

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

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

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

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

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

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

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

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

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

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

    
3909
      inst_data = lu.cfg.GetAllInstancesInfo()
3910

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

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

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

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

    
3937

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

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

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

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

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

    
3955

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

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

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

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

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

3981
    """
3982
    nodenames = self.glm.list_owned(locking.LEVEL_NODE)
3983
    volumes = self.rpc.call_node_volumes(nodenames)
3984

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

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

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

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

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

    
4029
        output.append(node_output)
4030

    
4031
    return output
4032

    
4033

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

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

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

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

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

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

4059
    """
4060
    self.nodes = self.glm.list_owned(locking.LEVEL_NODE)
4061

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

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

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

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

    
4081
    result = []
4082

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

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

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

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

    
4098
        out = []
4099

    
4100
        for field in self.op.output_fields:
4101
          if field == constants.SF_NODE:
4102
            val = node
4103
          elif field == constants.SF_TYPE:
4104
            val = self.op.storage_type
4105
          elif field in field_idx:
4106
            val = row[field_idx[field]]
4107
          else:
4108
            raise errors.ParameterError(field)
4109

    
4110
          out.append(val)
4111

    
4112
        result.append(out)
4113

    
4114
    return result
4115

    
4116

    
4117
class _InstanceQuery(_QueryBase):
4118
  FIELDS = query.INSTANCE_FIELDS
4119

    
4120
  def ExpandNames(self, lu):
4121
    lu.needed_locks = {}
4122
    lu.share_locks[locking.LEVEL_INSTANCE] = 1
4123
    lu.share_locks[locking.LEVEL_NODE] = 1
4124

    
4125
    if self.names:
4126
      self.wanted = _GetWantedInstances(lu, self.names)
4127
    else:
4128
      self.wanted = locking.ALL_SET
4129

    
4130
    self.do_locking = (self.use_locking and
4131
                       query.IQ_LIVE in self.requested_data)
4132
    if self.do_locking:
4133
      lu.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
4134
      lu.needed_locks[locking.LEVEL_NODE] = []
4135
      lu.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
4136

    
4137
  def DeclareLocks(self, lu, level):
4138
    if level == locking.LEVEL_NODE and self.do_locking:
4139
      lu._LockInstancesNodes() # pylint: disable-msg=W0212
4140

    
4141
  def _GetQueryData(self, lu):
4142
    """Computes the list of instances and their attributes.
4143

4144
    """
4145
    cluster = lu.cfg.GetClusterInfo()
4146
    all_info = lu.cfg.GetAllInstancesInfo()
4147

    
4148
    instance_names = self._GetNames(lu, all_info.keys(), locking.LEVEL_INSTANCE)
4149

    
4150
    instance_list = [all_info[name] for name in instance_names]
4151
    nodes = frozenset(itertools.chain(*(inst.all_nodes
4152
                                        for inst in instance_list)))
4153
    hv_list = list(set([inst.hypervisor for inst in instance_list]))
4154
    bad_nodes = []
4155
    offline_nodes = []
4156
    wrongnode_inst = set()
4157

    
4158
    # Gather data as requested
4159
    if self.requested_data & set([query.IQ_LIVE, query.IQ_CONSOLE]):
4160
      live_data = {}
4161
      node_data = lu.rpc.call_all_instances_info(nodes, hv_list)
4162
      for name in nodes:
4163
        result = node_data[name]
4164
        if result.offline:
4165
          # offline nodes will be in both lists
4166
          assert result.fail_msg
4167
          offline_nodes.append(name)
4168
        if result.fail_msg:
4169
          bad_nodes.append(name)
4170
        elif result.payload:
4171
          for inst in result.payload:
4172
            if inst in all_info:
4173
              if all_info[inst].primary_node == name:
4174
                live_data.update(result.payload)
4175
              else:
4176
                wrongnode_inst.add(inst)
4177
            else:
4178
              # orphan instance; we don't list it here as we don't
4179
              # handle this case yet in the output of instance listing
4180
              logging.warning("Orphan instance '%s' found on node %s",
4181
                              inst, name)
4182
        # else no instance is alive
4183
    else:
4184
      live_data = {}
4185

    
4186
    if query.IQ_DISKUSAGE in self.requested_data:
4187
      disk_usage = dict((inst.name,
4188
                         _ComputeDiskSize(inst.disk_template,
4189
                                          [{constants.IDISK_SIZE: disk.size}
4190
                                           for disk in inst.disks]))
4191
                        for inst in instance_list)
4192
    else:
4193
      disk_usage = None
4194

    
4195
    if query.IQ_CONSOLE in self.requested_data:
4196
      consinfo = {}
4197
      for inst in instance_list:
4198
        if inst.name in live_data:
4199
          # Instance is running
4200
          consinfo[inst.name] = _GetInstanceConsole(cluster, inst)
4201
        else:
4202
          consinfo[inst.name] = None
4203
      assert set(consinfo.keys()) == set(instance_names)
4204
    else:
4205
      consinfo = None
4206

    
4207
    return query.InstanceQueryData(instance_list, lu.cfg.GetClusterInfo(),
4208
                                   disk_usage, offline_nodes, bad_nodes,
4209
                                   live_data, wrongnode_inst, consinfo)
4210

    
4211

    
4212
class LUQuery(NoHooksLU):
4213
  """Query for resources/items of a certain kind.
4214

4215
  """
4216
  # pylint: disable-msg=W0142
4217
  REQ_BGL = False
4218

    
4219
  def CheckArguments(self):
4220
    qcls = _GetQueryImplementation(self.op.what)
4221

    
4222
    self.impl = qcls(self.op.filter, self.op.fields, False)
4223

    
4224
  def ExpandNames(self):
4225
    self.impl.ExpandNames(self)
4226

    
4227
  def DeclareLocks(self, level):
4228
    self.impl.DeclareLocks(self, level)
4229

    
4230
  def Exec(self, feedback_fn):
4231
    return self.impl.NewStyleQuery(self)
4232

    
4233

    
4234
class LUQueryFields(NoHooksLU):
4235
  """Query for resources/items of a certain kind.
4236

4237
  """
4238
  # pylint: disable-msg=W0142
4239
  REQ_BGL = False
4240

    
4241
  def CheckArguments(self):
4242
    self.qcls = _GetQueryImplementation(self.op.what)
4243

    
4244
  def ExpandNames(self):
4245
    self.needed_locks = {}
4246

    
4247
  def Exec(self, feedback_fn):
4248
    return query.QueryFields(self.qcls.FIELDS, self.op.fields)
4249

    
4250

    
4251
class LUNodeModifyStorage(NoHooksLU):
4252
  """Logical unit for modifying a storage volume on a node.
4253

4254
  """
4255
  REQ_BGL = False
4256

    
4257
  def CheckArguments(self):
4258
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
4259

    
4260
    storage_type = self.op.storage_type
4261

    
4262
    try:
4263
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
4264
    except KeyError:
4265
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
4266
                                 " modified" % storage_type,
4267
                                 errors.ECODE_INVAL)
4268

    
4269
    diff = set(self.op.changes.keys()) - modifiable
4270
    if diff:
4271
      raise errors.OpPrereqError("The following fields can not be modified for"
4272
                                 " storage units of type '%s': %r" %
4273
                                 (storage_type, list(diff)),
4274
                                 errors.ECODE_INVAL)
4275

    
4276
  def ExpandNames(self):
4277
    self.needed_locks = {
4278
      locking.LEVEL_NODE: self.op.node_name,
4279
      }
4280

    
4281
  def Exec(self, feedback_fn):
4282
    """Computes the list of nodes and their attributes.
4283

4284
    """
4285
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
4286
    result = self.rpc.call_storage_modify(self.op.node_name,
4287
                                          self.op.storage_type, st_args,
4288
                                          self.op.name, self.op.changes)
4289
    result.Raise("Failed to modify storage unit '%s' on %s" %
4290
                 (self.op.name, self.op.node_name))
4291

    
4292

    
4293
class LUNodeAdd(LogicalUnit):
4294
  """Logical unit for adding node to the cluster.
4295

4296
  """
4297
  HPATH = "node-add"
4298
  HTYPE = constants.HTYPE_NODE
4299
  _NFLAGS = ["master_capable", "vm_capable"]
4300

    
4301
  def CheckArguments(self):
4302
    self.primary_ip_family = self.cfg.GetPrimaryIPFamily()
4303
    # validate/normalize the node name
4304
    self.hostname = netutils.GetHostname(name=self.op.node_name,
4305
                                         family=self.primary_ip_family)
4306
    self.op.node_name = self.hostname.name
4307

    
4308
    if self.op.readd and self.op.node_name == self.cfg.GetMasterNode():
4309
      raise errors.OpPrereqError("Cannot readd the master node",
4310
                                 errors.ECODE_STATE)
4311

    
4312
    if self.op.readd and self.op.group:
4313
      raise errors.OpPrereqError("Cannot pass a node group when a node is"
4314
                                 " being readded", errors.ECODE_INVAL)
4315

    
4316
  def BuildHooksEnv(self):
4317
    """Build hooks env.
4318

4319
    This will run on all nodes before, and on all nodes + the new node after.
4320

4321
    """
4322
    return {
4323
      "OP_TARGET": self.op.node_name,
4324
      "NODE_NAME": self.op.node_name,
4325
      "NODE_PIP": self.op.primary_ip,
4326
      "NODE_SIP": self.op.secondary_ip,
4327
      "MASTER_CAPABLE": str(self.op.master_capable),
4328
      "VM_CAPABLE": str(self.op.vm_capable),
4329
      }
4330

    
4331
  def BuildHooksNodes(self):
4332
    """Build hooks nodes.
4333

4334
    """
4335
    # Exclude added node
4336
    pre_nodes = list(set(self.cfg.GetNodeList()) - set([self.op.node_name]))
4337
    post_nodes = pre_nodes + [self.op.node_name, ]
4338

    
4339
    return (pre_nodes, post_nodes)
4340

    
4341
  def CheckPrereq(self):
4342
    """Check prerequisites.
4343

4344
    This checks:
4345
     - the new node is not already in the config
4346
     - it is resolvable
4347
     - its parameters (single/dual homed) matches the cluster
4348

4349
    Any errors are signaled by raising errors.OpPrereqError.
4350

4351
    """
4352
    cfg = self.cfg
4353
    hostname = self.hostname
4354
    node = hostname.name
4355
    primary_ip = self.op.primary_ip = hostname.ip
4356
    if self.op.secondary_ip is None:
4357
      if self.primary_ip_family == netutils.IP6Address.family:
4358
        raise errors.OpPrereqError("When using a IPv6 primary address, a valid"
4359
                                   " IPv4 address must be given as secondary",
4360
                                   errors.ECODE_INVAL)
4361
      self.op.secondary_ip = primary_ip
4362

    
4363
    secondary_ip = self.op.secondary_ip
4364
    if not netutils.IP4Address.IsValid(secondary_ip):
4365
      raise errors.OpPrereqError("Secondary IP (%s) needs to be a valid IPv4"
4366
                                 " address" % secondary_ip, errors.ECODE_INVAL)
4367

    
4368
    node_list = cfg.GetNodeList()
4369
    if not self.op.readd and node in node_list:
4370
      raise errors.OpPrereqError("Node %s is already in the configuration" %
4371
                                 node, errors.ECODE_EXISTS)
4372
    elif self.op.readd and node not in node_list:
4373
      raise errors.OpPrereqError("Node %s is not in the configuration" % node,
4374
                                 errors.ECODE_NOENT)
4375

    
4376
    self.changed_primary_ip = False
4377

    
4378
    for existing_node_name in node_list:
4379
      existing_node = cfg.GetNodeInfo(existing_node_name)
4380

    
4381
      if self.op.readd and node == existing_node_name:
4382
        if existing_node.secondary_ip != secondary_ip:
4383
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
4384
                                     " address configuration as before",
4385
                                     errors.ECODE_INVAL)
4386
        if existing_node.primary_ip != primary_ip:
4387
          self.changed_primary_ip = True
4388

    
4389
        continue
4390

    
4391
      if (existing_node.primary_ip == primary_ip or
4392
          existing_node.secondary_ip == primary_ip or
4393
          existing_node.primary_ip == secondary_ip or
4394
          existing_node.secondary_ip == secondary_ip):
4395
        raise errors.OpPrereqError("New node ip address(es) conflict with"
4396
                                   " existing node %s" % existing_node.name,
4397
                                   errors.ECODE_NOTUNIQUE)
4398

    
4399
    # After this 'if' block, None is no longer a valid value for the
4400
    # _capable op attributes
4401
    if self.op.readd:
4402
      old_node = self.cfg.GetNodeInfo(node)
4403
      assert old_node is not None, "Can't retrieve locked node %s" % node
4404
      for attr in self._NFLAGS:
4405
        if getattr(self.op, attr) is None:
4406
          setattr(self.op, attr, getattr(old_node, attr))
4407
    else:
4408
      for attr in self._NFLAGS:
4409
        if getattr(self.op, attr) is None:
4410
          setattr(self.op, attr, True)
4411

    
4412
    if self.op.readd and not self.op.vm_capable:
4413
      pri, sec = cfg.GetNodeInstances(node)
4414
      if pri or sec:
4415
        raise errors.OpPrereqError("Node %s being re-added with vm_capable"
4416
                                   " flag set to false, but it already holds"
4417
                                   " instances" % node,
4418
                                   errors.ECODE_STATE)
4419

    
4420
    # check that the type of the node (single versus dual homed) is the
4421
    # same as for the master
4422
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
4423
    master_singlehomed = myself.secondary_ip == myself.primary_ip
4424
    newbie_singlehomed = secondary_ip == primary_ip
4425
    if master_singlehomed != newbie_singlehomed:
4426
      if master_singlehomed:
4427
        raise errors.OpPrereqError("The master has no secondary ip but the"
4428
                                   " new node has one",
4429
                                   errors.ECODE_INVAL)
4430
      else:
4431
        raise errors.OpPrereqError("The master has a secondary ip but the"
4432
                                   " new node doesn't have one",
4433
                                   errors.ECODE_INVAL)
4434

    
4435
    # checks reachability
4436
    if not netutils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
4437
      raise errors.OpPrereqError("Node not reachable by ping",
4438
                                 errors.ECODE_ENVIRON)
4439

    
4440
    if not newbie_singlehomed:
4441
      # check reachability from my secondary ip to newbie's secondary ip
4442
      if not netutils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
4443
                           source=myself.secondary_ip):
4444
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
4445
                                   " based ping to node daemon port",
4446
                                   errors.ECODE_ENVIRON)
4447

    
4448
    if self.op.readd:
4449
      exceptions = [node]
4450
    else:
4451
      exceptions = []
4452

    
4453
    if self.op.master_capable:
4454
      self.master_candidate = _DecideSelfPromotion(self, exceptions=exceptions)
4455
    else:
4456
      self.master_candidate = False
4457

    
4458
    if self.op.readd:
4459
      self.new_node = old_node
4460
    else:
4461
      node_group = cfg.LookupNodeGroup(self.op.group)
4462
      self.new_node = objects.Node(name=node,
4463
                                   primary_ip=primary_ip,
4464
                                   secondary_ip=secondary_ip,
4465
                                   master_candidate=self.master_candidate,
4466
                                   offline=False, drained=False,
4467
                                   group=node_group)
4468

    
4469
    if self.op.ndparams:
4470
      utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
4471

    
4472
  def Exec(self, feedback_fn):
4473
    """Adds the new node to the cluster.
4474

4475
    """
4476
    new_node = self.new_node
4477
    node = new_node.name
4478

    
4479
    # We adding a new node so we assume it's powered
4480
    new_node.powered = True
4481

    
4482
    # for re-adds, reset the offline/drained/master-candidate flags;
4483
    # we need to reset here, otherwise offline would prevent RPC calls
4484
    # later in the procedure; this also means that if the re-add
4485
    # fails, we are left with a non-offlined, broken node
4486
    if self.op.readd:
4487
      new_node.drained = new_node.offline = False # pylint: disable-msg=W0201
4488
      self.LogInfo("Readding a node, the offline/drained flags were reset")
4489
      # if we demote the node, we do cleanup later in the procedure
4490
      new_node.master_candidate = self.master_candidate
4491
      if self.changed_primary_ip:
4492
        new_node.primary_ip = self.op.primary_ip
4493

    
4494
    # copy the master/vm_capable flags
4495
    for attr in self._NFLAGS:
4496
      setattr(new_node, attr, getattr(self.op, attr))
4497

    
4498
    # notify the user about any possible mc promotion
4499
    if new_node.master_candidate:
4500
      self.LogInfo("Node will be a master candidate")
4501

    
4502
    if self.op.ndparams:
4503
      new_node.ndparams = self.op.ndparams
4504
    else:
4505
      new_node.ndparams = {}
4506

    
4507
    # check connectivity
4508
    result = self.rpc.call_version([node])[node]
4509
    result.Raise("Can't get version information from node %s" % node)
4510
    if constants.PROTOCOL_VERSION == result.payload:
4511
      logging.info("Communication to node %s fine, sw version %s match",
4512
                   node, result.payload)
4513
    else:
4514
      raise errors.OpExecError("Version mismatch master version %s,"
4515
                               " node version %s" %
4516
                               (constants.PROTOCOL_VERSION, result.payload))
4517

    
4518
    # Add node to our /etc/hosts, and add key to known_hosts
4519
    if self.cfg.GetClusterInfo().modify_etc_hosts:
4520
      master_node = self.cfg.GetMasterNode()
4521
      result = self.rpc.call_etc_hosts_modify(master_node,
4522
                                              constants.ETC_HOSTS_ADD,
4523
                                              self.hostname.name,
4524
                                              self.hostname.ip)
4525
      result.Raise("Can't update hosts file with new host data")
4526

    
4527
    if new_node.secondary_ip != new_node.primary_ip:
4528
      _CheckNodeHasSecondaryIP(self, new_node.name, new_node.secondary_ip,
4529
                               False)
4530

    
4531
    node_verify_list = [self.cfg.GetMasterNode()]
4532
    node_verify_param = {
4533
      constants.NV_NODELIST: [node],
4534
      # TODO: do a node-net-test as well?
4535
    }
4536

    
4537
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
4538
                                       self.cfg.GetClusterName())
4539
    for verifier in node_verify_list:
4540
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
4541
      nl_payload = result[verifier].payload[constants.NV_NODELIST]
4542
      if nl_payload:
4543
        for failed in nl_payload:
4544
          feedback_fn("ssh/hostname verification failed"
4545
                      " (checking from %s): %s" %
4546
                      (verifier, nl_payload[failed]))
4547
        raise errors.OpExecError("ssh/hostname verification failed")
4548

    
4549
    if self.op.readd:
4550
      _RedistributeAncillaryFiles(self)
4551
      self.context.ReaddNode(new_node)
4552
      # make sure we redistribute the config
4553
      self.cfg.Update(new_node, feedback_fn)
4554
      # and make sure the new node will not have old files around
4555
      if not new_node.master_candidate:
4556
        result = self.rpc.call_node_demote_from_mc(new_node.name)
4557
        msg = result.fail_msg
4558
        if msg:
4559
          self.LogWarning("Node failed to demote itself from master"
4560
                          " candidate status: %s" % msg)
4561
    else:
4562
      _RedistributeAncillaryFiles(self, additional_nodes=[node],
4563
                                  additional_vm=self.op.vm_capable)
4564
      self.context.AddNode(new_node, self.proc.GetECId())
4565

    
4566

    
4567
class LUNodeSetParams(LogicalUnit):
4568
  """Modifies the parameters of a node.
4569

4570
  @cvar _F2R: a dictionary from tuples of flags (mc, drained, offline)
4571
      to the node role (as _ROLE_*)
4572
  @cvar _R2F: a dictionary from node role to tuples of flags
4573
  @cvar _FLAGS: a list of attribute names corresponding to the flags
4574

4575
  """
4576
  HPATH = "node-modify"
4577
  HTYPE = constants.HTYPE_NODE
4578
  REQ_BGL = False
4579
  (_ROLE_CANDIDATE, _ROLE_DRAINED, _ROLE_OFFLINE, _ROLE_REGULAR) = range(4)
4580
  _F2R = {
4581
    (True, False, False): _ROLE_CANDIDATE,
4582
    (False, True, False): _ROLE_DRAINED,
4583
    (False, False, True): _ROLE_OFFLINE,
4584
    (False, False, False): _ROLE_REGULAR,
4585
    }
4586
  _R2F = dict((v, k) for k, v in _F2R.items())
4587
  _FLAGS = ["master_candidate", "drained", "offline"]
4588

    
4589
  def CheckArguments(self):
4590
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
4591
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained,
4592
                self.op.master_capable, self.op.vm_capable,
4593
                self.op.secondary_ip, self.op.ndparams]
4594
    if all_mods.count(None) == len(all_mods):
4595
      raise errors.OpPrereqError("Please pass at least one modification",
4596
                                 errors.ECODE_INVAL)
4597
    if all_mods.count(True) > 1:
4598
      raise errors.OpPrereqError("Can't set the node into more than one"
4599
                                 " state at the same time",
4600
                                 errors.ECODE_INVAL)
4601

    
4602
    # Boolean value that tells us whether we might be demoting from MC
4603
    self.might_demote = (self.op.master_candidate == False or
4604
                         self.op.offline == True or
4605
                         self.op.drained == True or
4606
                         self.op.master_capable == False)
4607

    
4608
    if self.op.secondary_ip:
4609
      if not netutils.IP4Address.IsValid(self.op.secondary_ip):
4610
        raise errors.OpPrereqError("Secondary IP (%s) needs to be a valid IPv4"
4611
                                   " address" % self.op.secondary_ip,
4612
                                   errors.ECODE_INVAL)
4613

    
4614
    self.lock_all = self.op.auto_promote and self.might_demote
4615
    self.lock_instances = self.op.secondary_ip is not None
4616

    
4617
  def ExpandNames(self):
4618
    if self.lock_all:
4619
      self.needed_locks = {locking.LEVEL_NODE: locking.ALL_SET}
4620
    else:
4621
      self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
4622

    
4623
    if self.lock_instances:
4624
      self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
4625

    
4626
  def DeclareLocks(self, level):
4627
    # If we have locked all instances, before waiting to lock nodes, release
4628
    # all the ones living on nodes unrelated to the current operation.
4629
    if level == locking.LEVEL_NODE and self.lock_instances:
4630
      self.affected_instances = []
4631
      if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
4632
        instances_keep = []
4633

    
4634
        # Build list of instances to release
4635
        for instance_name in self.glm.list_owned(locking.LEVEL_INSTANCE):
4636
          instance = self.context.cfg.GetInstanceInfo(instance_name)
4637
          if (instance.disk_template in constants.DTS_INT_MIRROR and
4638
              self.op.node_name in instance.all_nodes):
4639
            instances_keep.append(instance_name)
4640
            self.affected_instances.append(instance)
4641

    
4642
        _ReleaseLocks(self, locking.LEVEL_INSTANCE, keep=instances_keep)
4643

    
4644
        assert (set(self.glm.list_owned(locking.LEVEL_INSTANCE)) ==
4645
                set(instances_keep))
4646

    
4647
  def BuildHooksEnv(self):
4648
    """Build hooks env.
4649

4650
    This runs on the master node.
4651

4652
    """
4653
    return {
4654
      "OP_TARGET": self.op.node_name,
4655
      "MASTER_CANDIDATE": str(self.op.master_candidate),
4656
      "OFFLINE": str(self.op.offline),
4657
      "DRAINED": str(self.op.drained),
4658
      "MASTER_CAPABLE": str(self.op.master_capable),
4659
      "VM_CAPABLE": str(self.op.vm_capable),
4660
      }
4661

    
4662
  def BuildHooksNodes(self):
4663
    """Build hooks nodes.
4664

4665
    """
4666
    nl = [self.cfg.GetMasterNode(), self.op.node_name]
4667
    return (nl, nl)
4668

    
4669
  def CheckPrereq(self):
4670
    """Check prerequisites.
4671

4672
    This only checks the instance list against the existing names.
4673

4674
    """
4675
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
4676

    
4677
    if (self.op.master_candidate is not None or
4678
        self.op.drained is not None or
4679
        self.op.offline is not None):
4680
      # we can't change the master's node flags
4681
      if self.op.node_name == self.cfg.GetMasterNode():
4682
        raise errors.OpPrereqError("The master role can be changed"
4683
                                   " only via master-failover",
4684
                                   errors.ECODE_INVAL)
4685

    
4686
    if self.op.master_candidate and not node.master_capable:
4687
      raise errors.OpPrereqError("Node %s is not master capable, cannot make"
4688
                                 " it a master candidate" % node.name,
4689
                                 errors.ECODE_STATE)
4690

    
4691
    if self.op.vm_capable == False:
4692
      (ipri, isec) = self.cfg.GetNodeInstances(self.op.node_name)
4693
      if ipri or isec:
4694
        raise errors.OpPrereqError("Node %s hosts instances, cannot unset"
4695
                                   " the vm_capable flag" % node.name,
4696
                                   errors.ECODE_STATE)
4697

    
4698
    if node.master_candidate and self.might_demote and not self.lock_all:
4699
      assert not self.op.auto_promote, "auto_promote set but lock_all not"
4700
      # check if after removing the current node, we're missing master
4701
      # candidates
4702
      (mc_remaining, mc_should, _) = \
4703
          self.cfg.GetMasterCandidateStats(exceptions=[node.name])
4704
      if mc_remaining < mc_should:
4705
        raise errors.OpPrereqError("Not enough master candidates, please"
4706
                                   " pass auto promote option to allow"
4707
                                   " promotion", errors.ECODE_STATE)
4708

    
4709
    self.old_flags = old_flags = (node.master_candidate,
4710
                                  node.drained, node.offline)
4711
    assert old_flags in self._F2R, "Un-handled old flags %s" % str(old_flags)
4712
    self.old_role = old_role = self._F2R[old_flags]
4713

    
4714
    # Check for ineffective changes
4715
    for attr in self._FLAGS:
4716
      if (getattr(self.op, attr) == False and getattr(node, attr) == False):
4717
        self.LogInfo("Ignoring request to unset flag %s, already unset", attr)
4718
        setattr(self.op, attr, None)
4719

    
4720
    # Past this point, any flag change to False means a transition
4721
    # away from the respective state, as only real changes are kept
4722

    
4723
    # TODO: We might query the real power state if it supports OOB
4724
    if _SupportsOob(self.cfg, node):
4725
      if self.op.offline is False and not (node.powered or
4726
                                           self.op.powered == True):
4727
        raise errors.OpPrereqError(("Node %s needs to be turned on before its"
4728
                                    " offline status can be reset") %
4729
                                   self.op.node_name)
4730
    elif self.op.powered is not None:
4731
      raise errors.OpPrereqError(("Unable to change powered state for node %s"
4732
                                  " as it does not support out-of-band"
4733
                                  " handling") % self.op.node_name)
4734

    
4735
    # If we're being deofflined/drained, we'll MC ourself if needed
4736
    if (self.op.drained == False or self.op.offline == False or
4737
        (self.op.master_capable and not node.master_capable)):
4738
      if _DecideSelfPromotion(self):
4739
        self.op.master_candidate = True
4740
        self.LogInfo("Auto-promoting node to master candidate")
4741

    
4742
    # If we're no longer master capable, we'll demote ourselves from MC
4743
    if self.op.master_capable == False and node.master_candidate:
4744
      self.LogInfo("Demoting from master candidate")
4745
      self.op.master_candidate = False
4746

    
4747
    # Compute new role
4748
    assert [getattr(self.op, attr) for attr in self._FLAGS].count(True) <= 1
4749
    if self.op.master_candidate:
4750
      new_role = self._ROLE_CANDIDATE
4751
    elif self.op.drained:
4752
      new_role = self._ROLE_DRAINED
4753
    elif self.op.offline:
4754
      new_role = self._ROLE_OFFLINE
4755
    elif False in [self.op.master_candidate, self.op.drained, self.op.offline]:
4756
      # False is still in new flags, which means we're un-setting (the
4757
      # only) True flag
4758
      new_role = self._ROLE_REGULAR
4759
    else: # no new flags, nothing, keep old role
4760
      new_role = old_role
4761

    
4762
    self.new_role = new_role
4763

    
4764
    if old_role == self._ROLE_OFFLINE and new_role != old_role:
4765
      # Trying to transition out of offline status
4766
      result = self.rpc.call_version([node.name])[node.name]
4767
      if result.fail_msg:
4768
        raise errors.OpPrereqError("Node %s is being de-offlined but fails"
4769
                                   " to report its version: %s" %
4770
                                   (node.name, result.fail_msg),
4771
                                   errors.ECODE_STATE)
4772
      else:
4773
        self.LogWarning("Transitioning node from offline to online state"
4774
                        " without using re-add. Please make sure the node"
4775
                        " is healthy!")
4776

    
4777
    if self.op.secondary_ip:
4778
      # Ok even without locking, because this can't be changed by any LU
4779
      master = self.cfg.GetNodeInfo(self.cfg.GetMasterNode())
4780
      master_singlehomed = master.secondary_ip == master.primary_ip
4781
      if master_singlehomed and self.op.secondary_ip:
4782
        raise errors.OpPrereqError("Cannot change the secondary ip on a single"
4783
                                   " homed cluster", errors.ECODE_INVAL)
4784

    
4785
      if node.offline:
4786
        if self.affected_instances:
4787
          raise errors.OpPrereqError("Cannot change secondary ip: offline"
4788
                                     " node has instances (%s) configured"
4789
                                     " to use it" % self.affected_instances)
4790
      else:
4791
        # On online nodes, check that no instances are running, and that
4792
        # the node has the new ip and we can reach it.
4793
        for instance in self.affected_instances:
4794
          _CheckInstanceDown(self, instance, "cannot change secondary ip")
4795

    
4796
        _CheckNodeHasSecondaryIP(self, node.name, self.op.secondary_ip, True)
4797
        if master.name != node.name:
4798
          # check reachability from master secondary ip to new secondary ip
4799
          if not netutils.TcpPing(self.op.secondary_ip,
4800
                                  constants.DEFAULT_NODED_PORT,
4801
                                  source=master.secondary_ip):
4802
            raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
4803
                                       " based ping to node daemon port",
4804
                                       errors.ECODE_ENVIRON)
4805

    
4806
    if self.op.ndparams:
4807
      new_ndparams = _GetUpdatedParams(self.node.ndparams, self.op.ndparams)
4808
      utils.ForceDictType(new_ndparams, constants.NDS_PARAMETER_TYPES)
4809
      self.new_ndparams = new_ndparams
4810

    
4811
  def Exec(self, feedback_fn):
4812
    """Modifies a node.
4813

4814
    """
4815
    node = self.node
4816
    old_role = self.old_role
4817
    new_role = self.new_role
4818

    
4819
    result = []
4820

    
4821
    if self.op.ndparams:
4822
      node.ndparams = self.new_ndparams
4823

    
4824
    if self.op.powered is not None:
4825
      node.powered = self.op.powered
4826

    
4827
    for attr in ["master_capable", "vm_capable"]:
4828
      val = getattr(self.op, attr)
4829
      if val is not None:
4830
        setattr(node, attr, val)
4831
        result.append((attr, str(val)))
4832

    
4833
    if new_role != old_role:
4834
      # Tell the node to demote itself, if no longer MC and not offline
4835
      if old_role == self._ROLE_CANDIDATE and new_role != self._ROLE_OFFLINE:
4836
        msg = self.rpc.call_node_demote_from_mc(node.name).fail_msg
4837
        if msg:
4838
          self.LogWarning("Node failed to demote itself: %s", msg)
4839

    
4840
      new_flags = self._R2F[new_role]
4841
      for of, nf, desc in zip(self.old_flags, new_flags, self._FLAGS):
4842
        if of != nf:
4843
          result.append((desc, str(nf)))
4844
      (node.master_candidate, node.drained, node.offline) = new_flags
4845

    
4846
      # we locked all nodes, we adjust the CP before updating this node
4847
      if self.lock_all:
4848
        _AdjustCandidatePool(self, [node.name])
4849

    
4850
    if self.op.secondary_ip:
4851
      node.secondary_ip = self.op.secondary_ip
4852
      result.append(("secondary_ip", self.op.secondary_ip))
4853

    
4854
    # this will trigger configuration file update, if needed
4855
    self.cfg.Update(node, feedback_fn)
4856

    
4857
    # this will trigger job queue propagation or cleanup if the mc
4858
    # flag changed
4859
    if [old_role, new_role].count(self._ROLE_CANDIDATE) == 1:
4860
      self.context.ReaddNode(node)
4861

    
4862
    return result
4863

    
4864

    
4865
class LUNodePowercycle(NoHooksLU):
4866
  """Powercycles a node.
4867

4868
  """
4869
  REQ_BGL = False
4870

    
4871
  def CheckArguments(self):
4872
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
4873
    if self.op.node_name == self.cfg.GetMasterNode() and not self.op.force:
4874
      raise errors.OpPrereqError("The node is the master and the force"
4875
                                 " parameter was not set",
4876
                                 errors.ECODE_INVAL)
4877

    
4878
  def ExpandNames(self):
4879
    """Locking for PowercycleNode.
4880

4881
    This is a last-resort option and shouldn't block on other
4882
    jobs. Therefore, we grab no locks.
4883

4884
    """
4885
    self.needed_locks = {}
4886

    
4887
  def Exec(self, feedback_fn):
4888
    """Reboots a node.
4889

4890
    """
4891
    result = self.rpc.call_node_powercycle(self.op.node_name,
4892
                                           self.cfg.GetHypervisorType())
4893
    result.Raise("Failed to schedule the reboot")
4894
    return result.payload
4895

    
4896

    
4897
class LUClusterQuery(NoHooksLU):
4898
  """Query cluster configuration.
4899

4900
  """
4901
  REQ_BGL = False
4902

    
4903
  def ExpandNames(self):
4904
    self.needed_locks = {}
4905

    
4906
  def Exec(self, feedback_fn):
4907
    """Return cluster config.
4908

4909
    """
4910
    cluster = self.cfg.GetClusterInfo()
4911
    os_hvp = {}
4912

    
4913
    # Filter just for enabled hypervisors
4914
    for os_name, hv_dict in cluster.os_hvp.items():
4915
      os_hvp[os_name] = {}
4916
      for hv_name, hv_params in hv_dict.items():
4917
        if hv_name in cluster.enabled_hypervisors:
4918
          os_hvp[os_name][hv_name] = hv_params
4919

    
4920
    # Convert ip_family to ip_version
4921
    primary_ip_version = constants.IP4_VERSION
4922
    if cluster.primary_ip_family == netutils.IP6Address.family:
4923
      primary_ip_version = constants.IP6_VERSION
4924

    
4925
    result = {
4926
      "software_version": constants.RELEASE_VERSION,
4927
      "protocol_version": constants.PROTOCOL_VERSION,
4928
      "config_version": constants.CONFIG_VERSION,
4929
      "os_api_version": max(constants.OS_API_VERSIONS),
4930
      "export_version": constants.EXPORT_VERSION,
4931
      "architecture": (platform.architecture()[0], platform.machine()),
4932
      "name": cluster.cluster_name,
4933
      "master": cluster.master_node,
4934
      "default_hypervisor": cluster.enabled_hypervisors[0],
4935
      "enabled_hypervisors": cluster.enabled_hypervisors,
4936
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
4937
                        for hypervisor_name in cluster.enabled_hypervisors]),
4938
      "os_hvp": os_hvp,
4939
      "beparams": cluster.beparams,
4940
      "osparams": cluster.osparams,
4941
      "nicparams": cluster.nicparams,
4942
      "ndparams": cluster.ndparams,
4943
      "candidate_pool_size": cluster.candidate_pool_size,
4944
      "master_netdev": cluster.master_netdev,
4945
      "volume_group_name": cluster.volume_group_name,
4946
      "drbd_usermode_helper": cluster.drbd_usermode_helper,
4947
      "file_storage_dir": cluster.file_storage_dir,
4948
      "shared_file_storage_dir": cluster.shared_file_storage_dir,
4949
      "maintain_node_health": cluster.maintain_node_health,
4950
      "ctime": cluster.ctime,
4951
      "mtime": cluster.mtime,
4952
      "uuid": cluster.uuid,
4953
      "tags": list(cluster.GetTags()),
4954
      "uid_pool": cluster.uid_pool,
4955
      "default_iallocator": cluster.default_iallocator,
4956
      "reserved_lvs": cluster.reserved_lvs,
4957
      "primary_ip_version": primary_ip_version,
4958
      "prealloc_wipe_disks": cluster.prealloc_wipe_disks,
4959
      "hidden_os": cluster.hidden_os,
4960
      "blacklisted_os": cluster.blacklisted_os,
4961
      }
4962

    
4963
    return result
4964

    
4965

    
4966
class LUClusterConfigQuery(NoHooksLU):
4967
  """Return configuration values.
4968

4969
  """
4970
  REQ_BGL = False
4971
  _FIELDS_DYNAMIC = utils.FieldSet()
4972
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag",
4973
                                  "watcher_pause", "volume_group_name")
4974

    
4975
  def CheckArguments(self):
4976
    _CheckOutputFields(static=self._FIELDS_STATIC,
4977
                       dynamic=self._FIELDS_DYNAMIC,
4978
                       selected=self.op.output_fields)
4979

    
4980
  def ExpandNames(self):
4981
    self.needed_locks = {}
4982

    
4983
  def Exec(self, feedback_fn):
4984
    """Dump a representation of the cluster config to the standard output.
4985

4986
    """
4987
    values = []
4988
    for field in self.op.output_fields:
4989
      if field == "cluster_name":
4990
        entry = self.cfg.GetClusterName()
4991
      elif field == "master_node":
4992
        entry = self.cfg.GetMasterNode()
4993
      elif field == "drain_flag":
4994
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
4995
      elif field == "watcher_pause":
4996
        entry = utils.ReadWatcherPauseFile(constants.WATCHER_PAUSEFILE)
4997
      elif field == "volume_group_name":
4998
        entry = self.cfg.GetVGName()
4999
      else:
5000
        raise errors.ParameterError(field)
5001
      values.append(entry)
5002
    return values
5003

    
5004

    
5005
class LUInstanceActivateDisks(NoHooksLU):
5006
  """Bring up an instance's disks.
5007

5008
  """
5009
  REQ_BGL = False
5010

    
5011
  def ExpandNames(self):
5012
    self._ExpandAndLockInstance()
5013
    self.needed_locks[locking.LEVEL_NODE] = []
5014
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5015

    
5016
  def DeclareLocks(self, level):
5017
    if level == locking.LEVEL_NODE:
5018
      self._LockInstancesNodes()
5019

    
5020
  def CheckPrereq(self):
5021
    """Check prerequisites.
5022

5023
    This checks that the instance is in the cluster.
5024

5025
    """
5026
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5027
    assert self.instance is not None, \
5028
      "Cannot retrieve locked instance %s" % self.op.instance_name
5029
    _CheckNodeOnline(self, self.instance.primary_node)
5030

    
5031
  def Exec(self, feedback_fn):
5032
    """Activate the disks.
5033

5034
    """
5035
    disks_ok, disks_info = \
5036
              _AssembleInstanceDisks(self, self.instance,
5037
                                     ignore_size=self.op.ignore_size)
5038
    if not disks_ok:
5039
      raise errors.OpExecError("Cannot activate block devices")
5040

    
5041
    return disks_info
5042

    
5043

    
5044
def _AssembleInstanceDisks(lu, instance, disks=None, ignore_secondaries=False,
5045
                           ignore_size=False):
5046
  """Prepare the block devices for an instance.
5047

5048
  This sets up the block devices on all nodes.
5049

5050
  @type lu: L{LogicalUnit}
5051
  @param lu: the logical unit on whose behalf we execute
5052
  @type instance: L{objects.Instance}
5053
  @param instance: the instance for whose disks we assemble
5054
  @type disks: list of L{objects.Disk} or None
5055
  @param disks: which disks to assemble (or all, if None)
5056
  @type ignore_secondaries: boolean
5057
  @param ignore_secondaries: if true, errors on secondary nodes
5058
      won't result in an error return from the function
5059
  @type ignore_size: boolean
5060
  @param ignore_size: if true, the current known size of the disk
5061
      will not be used during the disk activation, useful for cases
5062
      when the size is wrong
5063
  @return: False if the operation failed, otherwise a list of
5064
      (host, instance_visible_name, node_visible_name)
5065
      with the mapping from node devices to instance devices
5066

5067
  """
5068
  device_info = []
5069
  disks_ok = True
5070
  iname = instance.name
5071
  disks = _ExpandCheckDisks(instance, disks)
5072

    
5073
  # With the two passes mechanism we try to reduce the window of
5074
  # opportunity for the race condition of switching DRBD to primary
5075
  # before handshaking occured, but we do not eliminate it
5076

    
5077
  # The proper fix would be to wait (with some limits) until the
5078
  # connection has been made and drbd transitions from WFConnection
5079
  # into any other network-connected state (Connected, SyncTarget,
5080
  # SyncSource, etc.)
5081

    
5082
  # 1st pass, assemble on all nodes in secondary mode
5083
  for idx, inst_disk in enumerate(disks):
5084
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
5085
      if ignore_size:
5086
        node_disk = node_disk.Copy()
5087
        node_disk.UnsetSize()
5088
      lu.cfg.SetDiskID(node_disk, node)
5089
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False, idx)
5090
      msg = result.fail_msg
5091
      if msg:
5092
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
5093
                           " (is_primary=False, pass=1): %s",
5094
                           inst_disk.iv_name, node, msg)
5095
        if not ignore_secondaries:
5096
          disks_ok = False
5097

    
5098
  # FIXME: race condition on drbd migration to primary
5099

    
5100
  # 2nd pass, do only the primary node
5101
  for idx, inst_disk in enumerate(disks):
5102
    dev_path = None
5103

    
5104
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
5105
      if node != instance.primary_node:
5106
        continue
5107
      if ignore_size:
5108
        node_disk = node_disk.Copy()
5109
        node_disk.UnsetSize()
5110
      lu.cfg.SetDiskID(node_disk, node)
5111
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True, idx)
5112
      msg = result.fail_msg
5113
      if msg:
5114
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
5115
                           " (is_primary=True, pass=2): %s",
5116
                           inst_disk.iv_name, node, msg)
5117
        disks_ok = False
5118
      else:
5119
        dev_path = result.payload
5120

    
5121
    device_info.append((instance.primary_node, inst_disk.iv_name, dev_path))
5122

    
5123
  # leave the disks configured for the primary node
5124
  # this is a workaround that would be fixed better by
5125
  # improving the logical/physical id handling
5126
  for disk in disks:
5127
    lu.cfg.SetDiskID(disk, instance.primary_node)
5128

    
5129
  return disks_ok, device_info
5130

    
5131

    
5132
def _StartInstanceDisks(lu, instance, force):
5133
  """Start the disks of an instance.
5134

5135
  """
5136
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
5137
                                           ignore_secondaries=force)
5138
  if not disks_ok:
5139
    _ShutdownInstanceDisks(lu, instance)
5140
    if force is not None and not force:
5141
      lu.proc.LogWarning("", hint="If the message above refers to a"
5142
                         " secondary node,"
5143
                         " you can retry the operation using '--force'.")
5144
    raise errors.OpExecError("Disk consistency error")
5145

    
5146

    
5147
class LUInstanceDeactivateDisks(NoHooksLU):
5148
  """Shutdown an instance's disks.
5149

5150
  """
5151
  REQ_BGL = False
5152

    
5153
  def ExpandNames(self):
5154
    self._ExpandAndLockInstance()
5155
    self.needed_locks[locking.LEVEL_NODE] = []
5156
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5157

    
5158
  def DeclareLocks(self, level):
5159
    if level == locking.LEVEL_NODE:
5160
      self._LockInstancesNodes()
5161

    
5162
  def CheckPrereq(self):
5163
    """Check prerequisites.
5164

5165
    This checks that the instance is in the cluster.
5166

5167
    """
5168
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5169
    assert self.instance is not None, \
5170
      "Cannot retrieve locked instance %s" % self.op.instance_name
5171

    
5172
  def Exec(self, feedback_fn):
5173
    """Deactivate the disks
5174

5175
    """
5176
    instance = self.instance
5177
    if self.op.force:
5178
      _ShutdownInstanceDisks(self, instance)
5179
    else:
5180
      _SafeShutdownInstanceDisks(self, instance)
5181

    
5182

    
5183
def _SafeShutdownInstanceDisks(lu, instance, disks=None):
5184
  """Shutdown block devices of an instance.
5185

5186
  This function checks if an instance is running, before calling
5187
  _ShutdownInstanceDisks.
5188

5189
  """
5190
  _CheckInstanceDown(lu, instance, "cannot shutdown disks")
5191
  _ShutdownInstanceDisks(lu, instance, disks=disks)
5192

    
5193

    
5194
def _ExpandCheckDisks(instance, disks):
5195
  """Return the instance disks selected by the disks list
5196

5197
  @type disks: list of L{objects.Disk} or None
5198
  @param disks: selected disks
5199
  @rtype: list of L{objects.Disk}
5200
  @return: selected instance disks to act on
5201

5202
  """
5203
  if disks is None:
5204
    return instance.disks
5205
  else:
5206
    if not set(disks).issubset(instance.disks):
5207
      raise errors.ProgrammerError("Can only act on disks belonging to the"
5208
                                   " target instance")
5209
    return disks
5210

    
5211

    
5212
def _ShutdownInstanceDisks(lu, instance, disks=None, ignore_primary=False):
5213
  """Shutdown block devices of an instance.
5214

5215
  This does the shutdown on all nodes of the instance.
5216

5217
  If the ignore_primary is false, errors on the primary node are
5218
  ignored.
5219

5220
  """
5221
  all_result = True
5222
  disks = _ExpandCheckDisks(instance, disks)
5223

    
5224
  for disk in disks:
5225
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
5226
      lu.cfg.SetDiskID(top_disk, node)
5227
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
5228
      msg = result.fail_msg
5229
      if msg:
5230
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
5231
                      disk.iv_name, node, msg)
5232
        if ((node == instance.primary_node and not ignore_primary) or
5233
            (node != instance.primary_node and not result.offline)):
5234
          all_result = False
5235
  return all_result
5236

    
5237

    
5238
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
5239
  """Checks if a node has enough free memory.
5240

5241
  This function check if a given node has the needed amount of free
5242
  memory. In case the node has less memory or we cannot get the
5243
  information from the node, this function raise an OpPrereqError
5244
  exception.
5245

5246
  @type lu: C{LogicalUnit}
5247
  @param lu: a logical unit from which we get configuration data
5248
  @type node: C{str}
5249
  @param node: the node to check
5250
  @type reason: C{str}
5251
  @param reason: string to use in the error message
5252
  @type requested: C{int}
5253
  @param requested: the amount of memory in MiB to check for
5254
  @type hypervisor_name: C{str}
5255
  @param hypervisor_name: the hypervisor to ask for memory stats
5256
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
5257
      we cannot check the node
5258

5259
  """
5260
  nodeinfo = lu.rpc.call_node_info([node], None, hypervisor_name)
5261
  nodeinfo[node].Raise("Can't get data from node %s" % node,
5262
                       prereq=True, ecode=errors.ECODE_ENVIRON)
5263
  free_mem = nodeinfo[node].payload.get('memory_free', None)
5264
  if not isinstance(free_mem, int):
5265
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
5266
                               " was '%s'" % (node, free_mem),
5267
                               errors.ECODE_ENVIRON)
5268
  if requested > free_mem:
5269
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
5270
                               " needed %s MiB, available %s MiB" %
5271
                               (node, reason, requested, free_mem),
5272
                               errors.ECODE_NORES)
5273

    
5274

    
5275
def _CheckNodesFreeDiskPerVG(lu, nodenames, req_sizes):
5276
  """Checks if nodes have enough free disk space in the all VGs.
5277

5278
  This function check if all given nodes have the needed amount of
5279
  free disk. In case any node has less disk or we cannot get the
5280
  information from the node, this function raise an OpPrereqError
5281
  exception.
5282

5283
  @type lu: C{LogicalUnit}
5284
  @param lu: a logical unit from which we get configuration data
5285
  @type nodenames: C{list}
5286
  @param nodenames: the list of node names to check
5287
  @type req_sizes: C{dict}
5288
  @param req_sizes: the hash of vg and corresponding amount of disk in
5289
      MiB to check for
5290
  @raise errors.OpPrereqError: if the node doesn't have enough disk,
5291
      or we cannot check the node
5292

5293
  """
5294
  for vg, req_size in req_sizes.items():
5295
    _CheckNodesFreeDiskOnVG(lu, nodenames, vg, req_size)
5296

    
5297

    
5298
def _CheckNodesFreeDiskOnVG(lu, nodenames, vg, requested):
5299
  """Checks if nodes have enough free disk space in the specified VG.
5300

5301
  This function check if all given nodes have the needed amount of
5302
  free disk. In case any node has less disk or we cannot get the
5303
  information from the node, this function raise an OpPrereqError
5304
  exception.
5305

5306
  @type lu: C{LogicalUnit}
5307
  @param lu: a logical unit from which we get configuration data
5308
  @type nodenames: C{list}
5309
  @param nodenames: the list of node names to check
5310
  @type vg: C{str}
5311
  @param vg: the volume group to check
5312
  @type requested: C{int}
5313
  @param requested: the amount of disk in MiB to check for
5314
  @raise errors.OpPrereqError: if the node doesn't have enough disk,
5315
      or we cannot check the node
5316

5317
  """
5318
  nodeinfo = lu.rpc.call_node_info(nodenames, vg, None)
5319
  for node in nodenames:
5320
    info = nodeinfo[node]
5321
    info.Raise("Cannot get current information from node %s" % node,
5322
               prereq=True, ecode=errors.ECODE_ENVIRON)
5323
    vg_free = info.payload.get("vg_free", None)
5324
    if not isinstance(vg_free, int):
5325
      raise errors.OpPrereqError("Can't compute free disk space on node"
5326
                                 " %s for vg %s, result was '%s'" %
5327
                                 (node, vg, vg_free), errors.ECODE_ENVIRON)
5328
    if requested > vg_free:
5329
      raise errors.OpPrereqError("Not enough disk space on target node %s"
5330
                                 " vg %s: required %d MiB, available %d MiB" %
5331
                                 (node, vg, requested, vg_free),
5332
                                 errors.ECODE_NORES)
5333

    
5334

    
5335
class LUInstanceStartup(LogicalUnit):
5336
  """Starts an instance.
5337

5338
  """
5339
  HPATH = "instance-start"
5340
  HTYPE = constants.HTYPE_INSTANCE
5341
  REQ_BGL = False
5342

    
5343
  def CheckArguments(self):
5344
    # extra beparams
5345
    if self.op.beparams:
5346
      # fill the beparams dict
5347
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
5348

    
5349
  def ExpandNames(self):
5350
    self._ExpandAndLockInstance()
5351

    
5352
  def BuildHooksEnv(self):
5353
    """Build hooks env.
5354

5355
    This runs on master, primary and secondary nodes of the instance.
5356

5357
    """
5358
    env = {
5359
      "FORCE": self.op.force,
5360
      }
5361

    
5362
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
5363

    
5364
    return env
5365

    
5366
  def BuildHooksNodes(self):
5367
    """Build hooks nodes.
5368

5369
    """
5370
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5371
    return (nl, nl)
5372

    
5373
  def CheckPrereq(self):
5374
    """Check prerequisites.
5375

5376
    This checks that the instance is in the cluster.
5377

5378
    """
5379
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5380
    assert self.instance is not None, \
5381
      "Cannot retrieve locked instance %s" % self.op.instance_name
5382

    
5383
    # extra hvparams
5384
    if self.op.hvparams:
5385
      # check hypervisor parameter syntax (locally)
5386
      cluster = self.cfg.GetClusterInfo()
5387
      utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
5388
      filled_hvp = cluster.FillHV(instance)
5389
      filled_hvp.update(self.op.hvparams)
5390
      hv_type = hypervisor.GetHypervisor(instance.hypervisor)
5391
      hv_type.CheckParameterSyntax(filled_hvp)
5392
      _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
5393

    
5394
    self.primary_offline = self.cfg.GetNodeInfo(instance.primary_node).offline
5395

    
5396
    if self.primary_offline and self.op.ignore_offline_nodes:
5397
      self.proc.LogWarning("Ignoring offline primary node")
5398

    
5399
      if self.op.hvparams or self.op.beparams:
5400
        self.proc.LogWarning("Overridden parameters are ignored")
5401
    else:
5402
      _CheckNodeOnline(self, instance.primary_node)
5403

    
5404
      bep = self.cfg.GetClusterInfo().FillBE(instance)
5405

    
5406
      # check bridges existence
5407
      _CheckInstanceBridgesExist(self, instance)
5408

    
5409
      remote_info = self.rpc.call_instance_info(instance.primary_node,
5410
                                                instance.name,
5411
                                                instance.hypervisor)
5412
      remote_info.Raise("Error checking node %s" % instance.primary_node,
5413
                        prereq=True, ecode=errors.ECODE_ENVIRON)
5414
      if not remote_info.payload: # not running already
5415
        _CheckNodeFreeMemory(self, instance.primary_node,
5416
                             "starting instance %s" % instance.name,
5417
                             bep[constants.BE_MEMORY], instance.hypervisor)
5418

    
5419
  def Exec(self, feedback_fn):
5420
    """Start the instance.
5421

5422
    """
5423
    instance = self.instance
5424
    force = self.op.force
5425

    
5426
    self.cfg.MarkInstanceUp(instance.name)
5427

    
5428
    if self.primary_offline:
5429
      assert self.op.ignore_offline_nodes
5430
      self.proc.LogInfo("Primary node offline, marked instance as started")
5431
    else:
5432
      node_current = instance.primary_node
5433

    
5434
      _StartInstanceDisks(self, instance, force)
5435

    
5436
      result = self.rpc.call_instance_start(node_current, instance,
5437
                                            self.op.hvparams, self.op.beparams)
5438
      msg = result.fail_msg
5439
      if msg:
5440
        _ShutdownInstanceDisks(self, instance)
5441
        raise errors.OpExecError("Could not start instance: %s" % msg)
5442

    
5443

    
5444
class LUInstanceReboot(LogicalUnit):
5445
  """Reboot an instance.
5446

5447
  """
5448
  HPATH = "instance-reboot"
5449
  HTYPE = constants.HTYPE_INSTANCE
5450
  REQ_BGL = False
5451

    
5452
  def ExpandNames(self):
5453
    self._ExpandAndLockInstance()
5454

    
5455
  def BuildHooksEnv(self):
5456
    """Build hooks env.
5457

5458
    This runs on master, primary and secondary nodes of the instance.
5459

5460
    """
5461
    env = {
5462
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
5463
      "REBOOT_TYPE": self.op.reboot_type,
5464
      "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
5465
      }
5466

    
5467
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
5468

    
5469
    return env
5470

    
5471
  def BuildHooksNodes(self):
5472
    """Build hooks nodes.
5473

5474
    """
5475
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5476
    return (nl, nl)
5477

    
5478
  def CheckPrereq(self):
5479
    """Check prerequisites.
5480

5481
    This checks that the instance is in the cluster.
5482

5483
    """
5484
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5485
    assert self.instance is not None, \
5486
      "Cannot retrieve locked instance %s" % self.op.instance_name
5487

    
5488
    _CheckNodeOnline(self, instance.primary_node)
5489

    
5490
    # check bridges existence
5491
    _CheckInstanceBridgesExist(self, instance)
5492

    
5493
  def Exec(self, feedback_fn):
5494
    """Reboot the instance.
5495

5496
    """
5497
    instance = self.instance
5498
    ignore_secondaries = self.op.ignore_secondaries
5499
    reboot_type = self.op.reboot_type
5500

    
5501
    remote_info = self.rpc.call_instance_info(instance.primary_node,
5502
                                              instance.name,
5503
                                              instance.hypervisor)
5504
    remote_info.Raise("Error checking node %s" % instance.primary_node)
5505
    instance_running = bool(remote_info.payload)
5506

    
5507
    node_current = instance.primary_node
5508

    
5509
    if instance_running and reboot_type in [constants.INSTANCE_REBOOT_SOFT,
5510
                                            constants.INSTANCE_REBOOT_HARD]:
5511
      for disk in instance.disks:
5512
        self.cfg.SetDiskID(disk, node_current)
5513
      result = self.rpc.call_instance_reboot(node_current, instance,
5514
                                             reboot_type,
5515
                                             self.op.shutdown_timeout)
5516
      result.Raise("Could not reboot instance")
5517
    else:
5518
      if instance_running:
5519
        result = self.rpc.call_instance_shutdown(node_current, instance,
5520
                                                 self.op.shutdown_timeout)
5521
        result.Raise("Could not shutdown instance for full reboot")
5522
        _ShutdownInstanceDisks(self, instance)
5523
      else:
5524
        self.LogInfo("Instance %s was already stopped, starting now",
5525
                     instance.name)
5526
      _StartInstanceDisks(self, instance, ignore_secondaries)
5527
      result = self.rpc.call_instance_start(node_current, instance, None, None)
5528
      msg = result.fail_msg
5529
      if msg:
5530
        _ShutdownInstanceDisks(self, instance)
5531
        raise errors.OpExecError("Could not start instance for"
5532
                                 " full reboot: %s" % msg)
5533

    
5534
    self.cfg.MarkInstanceUp(instance.name)
5535

    
5536

    
5537
class LUInstanceShutdown(LogicalUnit):
5538
  """Shutdown an instance.
5539

5540
  """
5541
  HPATH = "instance-stop"
5542
  HTYPE = constants.HTYPE_INSTANCE
5543
  REQ_BGL = False
5544

    
5545
  def ExpandNames(self):
5546
    self._ExpandAndLockInstance()
5547

    
5548
  def BuildHooksEnv(self):
5549
    """Build hooks env.
5550

5551
    This runs on master, primary and secondary nodes of the instance.
5552

5553
    """
5554
    env = _BuildInstanceHookEnvByObject(self, self.instance)
5555
    env["TIMEOUT"] = self.op.timeout
5556
    return env
5557

    
5558
  def BuildHooksNodes(self):
5559
    """Build hooks nodes.
5560

5561
    """
5562
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5563
    return (nl, nl)
5564

    
5565
  def CheckPrereq(self):
5566
    """Check prerequisites.
5567

5568
    This checks that the instance is in the cluster.
5569

5570
    """
5571
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5572
    assert self.instance is not None, \
5573
      "Cannot retrieve locked instance %s" % self.op.instance_name
5574

    
5575
    self.primary_offline = \
5576
      self.cfg.GetNodeInfo(self.instance.primary_node).offline
5577

    
5578
    if self.primary_offline and self.op.ignore_offline_nodes:
5579
      self.proc.LogWarning("Ignoring offline primary node")
5580
    else:
5581
      _CheckNodeOnline(self, self.instance.primary_node)
5582

    
5583
  def Exec(self, feedback_fn):
5584
    """Shutdown the instance.
5585

5586
    """
5587
    instance = self.instance
5588
    node_current = instance.primary_node
5589
    timeout = self.op.timeout
5590

    
5591
    self.cfg.MarkInstanceDown(instance.name)
5592

    
5593
    if self.primary_offline:
5594
      assert self.op.ignore_offline_nodes
5595
      self.proc.LogInfo("Primary node offline, marked instance as stopped")
5596
    else:
5597
      result = self.rpc.call_instance_shutdown(node_current, instance, timeout)
5598
      msg = result.fail_msg
5599
      if msg:
5600
        self.proc.LogWarning("Could not shutdown instance: %s" % msg)
5601

    
5602
      _ShutdownInstanceDisks(self, instance)
5603

    
5604

    
5605
class LUInstanceReinstall(LogicalUnit):
5606
  """Reinstall an instance.
5607

5608
  """
5609
  HPATH = "instance-reinstall"
5610
  HTYPE = constants.HTYPE_INSTANCE
5611
  REQ_BGL = False
5612

    
5613
  def ExpandNames(self):
5614
    self._ExpandAndLockInstance()
5615

    
5616
  def BuildHooksEnv(self):
5617
    """Build hooks env.
5618

5619
    This runs on master, primary and secondary nodes of the instance.
5620

5621
    """
5622
    return _BuildInstanceHookEnvByObject(self, self.instance)
5623

    
5624
  def BuildHooksNodes(self):
5625
    """Build hooks nodes.
5626

5627
    """
5628
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5629
    return (nl, nl)
5630

    
5631
  def CheckPrereq(self):
5632
    """Check prerequisites.
5633

5634
    This checks that the instance is in the cluster and is not running.
5635

5636
    """
5637
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5638
    assert instance is not None, \
5639
      "Cannot retrieve locked instance %s" % self.op.instance_name
5640
    _CheckNodeOnline(self, instance.primary_node, "Instance primary node"
5641
                     " offline, cannot reinstall")
5642
    for node in instance.secondary_nodes:
5643
      _CheckNodeOnline(self, node, "Instance secondary node offline,"
5644
                       " cannot reinstall")
5645

    
5646
    if instance.disk_template == constants.DT_DISKLESS:
5647
      raise errors.OpPrereqError("Instance '%s' has no disks" %
5648
                                 self.op.instance_name,
5649
                                 errors.ECODE_INVAL)
5650
    _CheckInstanceDown(self, instance, "cannot reinstall")
5651

    
5652
    if self.op.os_type is not None:
5653
      # OS verification
5654
      pnode = _ExpandNodeName(self.cfg, instance.primary_node)
5655
      _CheckNodeHasOS(self, pnode, self.op.os_type, self.op.force_variant)
5656
      instance_os = self.op.os_type
5657
    else:
5658
      instance_os = instance.os
5659

    
5660
    nodelist = list(instance.all_nodes)
5661

    
5662
    if self.op.osparams:
5663
      i_osdict = _GetUpdatedParams(instance.osparams, self.op.osparams)
5664
      _CheckOSParams(self, True, nodelist, instance_os, i_osdict)
5665
      self.os_inst = i_osdict # the new dict (without defaults)
5666
    else:
5667
      self.os_inst = None
5668

    
5669
    self.instance = instance
5670

    
5671
  def Exec(self, feedback_fn):
5672
    """Reinstall the instance.
5673

5674
    """
5675
    inst = self.instance
5676

    
5677
    if self.op.os_type is not None:
5678
      feedback_fn("Changing OS to '%s'..." % self.op.os_type)
5679
      inst.os = self.op.os_type
5680
      # Write to configuration
5681
      self.cfg.Update(inst, feedback_fn)
5682

    
5683
    _StartInstanceDisks(self, inst, None)
5684
    try:
5685
      feedback_fn("Running the instance OS create scripts...")
5686
      # FIXME: pass debug option from opcode to backend
5687
      result = self.rpc.call_instance_os_add(inst.primary_node, inst, True,
5688
                                             self.op.debug_level,
5689
                                             osparams=self.os_inst)
5690
      result.Raise("Could not install OS for instance %s on node %s" %
5691
                   (inst.name, inst.primary_node))
5692
    finally:
5693
      _ShutdownInstanceDisks(self, inst)
5694

    
5695

    
5696
class LUInstanceRecreateDisks(LogicalUnit):
5697
  """Recreate an instance's missing disks.
5698

5699
  """
5700
  HPATH = "instance-recreate-disks"
5701
  HTYPE = constants.HTYPE_INSTANCE
5702
  REQ_BGL = False
5703

    
5704
  def ExpandNames(self):
5705
    self._ExpandAndLockInstance()
5706

    
5707
  def BuildHooksEnv(self):
5708
    """Build hooks env.
5709

5710
    This runs on master, primary and secondary nodes of the instance.
5711

5712
    """
5713
    return _BuildInstanceHookEnvByObject(self, self.instance)
5714

    
5715
  def BuildHooksNodes(self):
5716
    """Build hooks nodes.
5717

5718
    """
5719
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5720
    return (nl, nl)
5721

    
5722
  def CheckPrereq(self):
5723
    """Check prerequisites.
5724

5725
    This checks that the instance is in the cluster and is not running.
5726

5727
    """
5728
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5729
    assert instance is not None, \
5730
      "Cannot retrieve locked instance %s" % self.op.instance_name
5731
    _CheckNodeOnline(self, instance.primary_node)
5732

    
5733
    if instance.disk_template == constants.DT_DISKLESS:
5734
      raise errors.OpPrereqError("Instance '%s' has no disks" %
5735
                                 self.op.instance_name, errors.ECODE_INVAL)
5736
    _CheckInstanceDown(self, instance, "cannot recreate disks")
5737

    
5738
    if not self.op.disks:
5739
      self.op.disks = range(len(instance.disks))
5740
    else:
5741
      for idx in self.op.disks:
5742
        if idx >= len(instance.disks):
5743
          raise errors.OpPrereqError("Invalid disk index '%s'" % idx,
5744
                                     errors.ECODE_INVAL)
5745

    
5746
    self.instance = instance
5747

    
5748
  def Exec(self, feedback_fn):
5749
    """Recreate the disks.
5750

5751
    """
5752
    to_skip = []
5753
    for idx, _ in enumerate(self.instance.disks):
5754
      if idx not in self.op.disks: # disk idx has not been passed in
5755
        to_skip.append(idx)
5756
        continue
5757

    
5758
    _CreateDisks(self, self.instance, to_skip=to_skip)
5759

    
5760

    
5761
class LUInstanceRename(LogicalUnit):
5762
  """Rename an instance.
5763

5764
  """
5765
  HPATH = "instance-rename"
5766
  HTYPE = constants.HTYPE_INSTANCE
5767

    
5768
  def CheckArguments(self):
5769
    """Check arguments.
5770

5771
    """
5772
    if self.op.ip_check and not self.op.name_check:
5773
      # TODO: make the ip check more flexible and not depend on the name check
5774
      raise errors.OpPrereqError("IP address check requires a name check",
5775
                                 errors.ECODE_INVAL)
5776

    
5777
  def BuildHooksEnv(self):
5778
    """Build hooks env.
5779

5780
    This runs on master, primary and secondary nodes of the instance.
5781

5782
    """
5783
    env = _BuildInstanceHookEnvByObject(self, self.instance)
5784
    env["INSTANCE_NEW_NAME"] = self.op.new_name
5785
    return env
5786

    
5787
  def BuildHooksNodes(self):
5788
    """Build hooks nodes.
5789

5790
    """
5791
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
5792
    return (nl, nl)
5793

    
5794
  def CheckPrereq(self):
5795
    """Check prerequisites.
5796

5797
    This checks that the instance is in the cluster and is not running.
5798

5799
    """
5800
    self.op.instance_name = _ExpandInstanceName(self.cfg,
5801
                                                self.op.instance_name)
5802
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5803
    assert instance is not None
5804
    _CheckNodeOnline(self, instance.primary_node)
5805
    _CheckInstanceDown(self, instance, "cannot rename")
5806
    self.instance = instance
5807

    
5808
    new_name = self.op.new_name
5809
    if self.op.name_check:
5810
      hostname = netutils.GetHostname(name=new_name)
5811
      self.LogInfo("Resolved given name '%s' to '%s'", new_name,
5812
                   hostname.name)
5813
      if not utils.MatchNameComponent(self.op.new_name, [hostname.name]):
5814
        raise errors.OpPrereqError(("Resolved hostname '%s' does not look the"
5815
                                    " same as given hostname '%s'") %
5816
                                    (hostname.name, self.op.new_name),
5817
                                    errors.ECODE_INVAL)
5818
      new_name = self.op.new_name = hostname.name
5819
      if (self.op.ip_check and
5820
          netutils.TcpPing(hostname.ip, constants.DEFAULT_NODED_PORT)):
5821
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
5822
                                   (hostname.ip, new_name),
5823
                                   errors.ECODE_NOTUNIQUE)
5824

    
5825
    instance_list = self.cfg.GetInstanceList()
5826
    if new_name in instance_list and new_name != instance.name:
5827
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
5828
                                 new_name, errors.ECODE_EXISTS)
5829

    
5830
  def Exec(self, feedback_fn):
5831
    """Rename the instance.
5832

5833
    """
5834
    inst = self.instance
5835
    old_name = inst.name
5836

    
5837
    rename_file_storage = False
5838
    if (inst.disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE) and
5839
        self.op.new_name != inst.name):
5840
      old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
5841
      rename_file_storage = True
5842

    
5843
    self.cfg.RenameInstance(inst.name, self.op.new_name)
5844
    # Change the instance lock. This is definitely safe while we hold the BGL.
5845
    # Otherwise the new lock would have to be added in acquired mode.
5846
    assert self.REQ_BGL
5847
    self.glm.remove(locking.LEVEL_INSTANCE, old_name)
5848
    self.glm.add(locking.LEVEL_INSTANCE, self.op.new_name)
5849

    
5850
    # re-read the instance from the configuration after rename
5851
    inst = self.cfg.GetInstanceInfo(self.op.new_name)
5852

    
5853
    if rename_file_storage:
5854
      new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
5855
      result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
5856
                                                     old_file_storage_dir,
5857
                                                     new_file_storage_dir)
5858
      result.Raise("Could not rename on node %s directory '%s' to '%s'"
5859
                   " (but the instance has been renamed in Ganeti)" %
5860
                   (inst.primary_node, old_file_storage_dir,
5861
                    new_file_storage_dir))
5862

    
5863
    _StartInstanceDisks(self, inst, None)
5864
    try:
5865
      result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
5866
                                                 old_name, self.op.debug_level)
5867
      msg = result.fail_msg
5868
      if msg:
5869
        msg = ("Could not run OS rename script for instance %s on node %s"
5870
               " (but the instance has been renamed in Ganeti): %s" %
5871
               (inst.name, inst.primary_node, msg))
5872
        self.proc.LogWarning(msg)
5873
    finally:
5874
      _ShutdownInstanceDisks(self, inst)
5875

    
5876
    return inst.name
5877

    
5878

    
5879
class LUInstanceRemove(LogicalUnit):
5880
  """Remove an instance.
5881

5882
  """
5883
  HPATH = "instance-remove"
5884
  HTYPE = constants.HTYPE_INSTANCE
5885
  REQ_BGL = False
5886

    
5887
  def ExpandNames(self):
5888
    self._ExpandAndLockInstance()
5889
    self.needed_locks[locking.LEVEL_NODE] = []
5890
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5891

    
5892
  def DeclareLocks(self, level):
5893
    if level == locking.LEVEL_NODE:
5894
      self._LockInstancesNodes()
5895

    
5896
  def BuildHooksEnv(self):
5897
    """Build hooks env.
5898

5899
    This runs on master, primary and secondary nodes of the instance.
5900

5901
    """
5902
    env = _BuildInstanceHookEnvByObject(self, self.instance)
5903
    env["SHUTDOWN_TIMEOUT"] = self.op.shutdown_timeout
5904
    return env
5905

    
5906
  def BuildHooksNodes(self):
5907
    """Build hooks nodes.
5908

5909
    """
5910
    nl = [self.cfg.GetMasterNode()]
5911
    nl_post = list(self.instance.all_nodes) + nl
5912
    return (nl, nl_post)
5913

    
5914
  def CheckPrereq(self):
5915
    """Check prerequisites.
5916

5917
    This checks that the instance is in the cluster.
5918

5919
    """
5920
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5921
    assert self.instance is not None, \
5922
      "Cannot retrieve locked instance %s" % self.op.instance_name
5923

    
5924
  def Exec(self, feedback_fn):
5925
    """Remove the instance.
5926

5927
    """
5928
    instance = self.instance
5929
    logging.info("Shutting down instance %s on node %s",
5930
                 instance.name, instance.primary_node)
5931

    
5932
    result = self.rpc.call_instance_shutdown(instance.primary_node, instance,
5933
                                             self.op.shutdown_timeout)
5934
    msg = result.fail_msg
5935
    if msg:
5936
      if self.op.ignore_failures:
5937
        feedback_fn("Warning: can't shutdown instance: %s" % msg)
5938
      else:
5939
        raise errors.OpExecError("Could not shutdown instance %s on"
5940
                                 " node %s: %s" %
5941
                                 (instance.name, instance.primary_node, msg))
5942

    
5943
    _RemoveInstance(self, feedback_fn, instance, self.op.ignore_failures)
5944

    
5945

    
5946
def _RemoveInstance(lu, feedback_fn, instance, ignore_failures):
5947
  """Utility function to remove an instance.
5948

5949
  """
5950
  logging.info("Removing block devices for instance %s", instance.name)
5951

    
5952
  if not _RemoveDisks(lu, instance):
5953
    if not ignore_failures:
5954
      raise errors.OpExecError("Can't remove instance's disks")
5955
    feedback_fn("Warning: can't remove instance's disks")
5956

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

    
5959
  lu.cfg.RemoveInstance(instance.name)
5960

    
5961
  assert not lu.remove_locks.get(locking.LEVEL_INSTANCE), \
5962
    "Instance lock removal conflict"
5963

    
5964
  # Remove lock for the instance
5965
  lu.remove_locks[locking.LEVEL_INSTANCE] = instance.name
5966

    
5967

    
5968
class LUInstanceQuery(NoHooksLU):
5969
  """Logical unit for querying instances.
5970

5971
  """
5972
  # pylint: disable-msg=W0142
5973
  REQ_BGL = False
5974

    
5975
  def CheckArguments(self):
5976
    self.iq = _InstanceQuery(qlang.MakeSimpleFilter("name", self.op.names),
5977
                             self.op.output_fields, self.op.use_locking)
5978

    
5979
  def ExpandNames(self):
5980
    self.iq.ExpandNames(self)
5981

    
5982
  def DeclareLocks(self, level):
5983
    self.iq.DeclareLocks(self, level)
5984

    
5985
  def Exec(self, feedback_fn):
5986
    return self.iq.OldStyleQuery(self)
5987

    
5988

    
5989
class LUInstanceFailover(LogicalUnit):
5990
  """Failover an instance.
5991

5992
  """
5993
  HPATH = "instance-failover"
5994
  HTYPE = constants.HTYPE_INSTANCE
5995
  REQ_BGL = False
5996

    
5997
  def CheckArguments(self):
5998
    """Check the arguments.
5999

6000
    """
6001
    self.iallocator = getattr(self.op, "iallocator", None)
6002
    self.target_node = getattr(self.op, "target_node", None)
6003

    
6004
  def ExpandNames(self):
6005
    self._ExpandAndLockInstance()
6006

    
6007
    if self.op.target_node is not None:
6008
      self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node)
6009

    
6010
    self.needed_locks[locking.LEVEL_NODE] = []
6011
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6012

    
6013
    ignore_consistency = self.op.ignore_consistency
6014
    shutdown_timeout = self.op.shutdown_timeout
6015
    self._migrater = TLMigrateInstance(self, self.op.instance_name,
6016
                                       cleanup=False,
6017
                                       failover=True,
6018
                                       ignore_consistency=ignore_consistency,
6019
                                       shutdown_timeout=shutdown_timeout)
6020
    self.tasklets = [self._migrater]
6021

    
6022
  def DeclareLocks(self, level):
6023
    if level == locking.LEVEL_NODE:
6024
      instance = self.context.cfg.GetInstanceInfo(self.op.instance_name)
6025
      if instance.disk_template in constants.DTS_EXT_MIRROR:
6026
        if self.op.target_node is None:
6027
          self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
6028
        else:
6029
          self.needed_locks[locking.LEVEL_NODE] = [instance.primary_node,
6030
                                                   self.op.target_node]
6031
        del self.recalculate_locks[locking.LEVEL_NODE]
6032
      else:
6033
        self._LockInstancesNodes()
6034

    
6035
  def BuildHooksEnv(self):
6036
    """Build hooks env.
6037

6038
    This runs on master, primary and secondary nodes of the instance.
6039

6040
    """
6041
    instance = self._migrater.instance
6042
    source_node = instance.primary_node
6043
    target_node = self.op.target_node
6044
    env = {
6045
      "IGNORE_CONSISTENCY": self.op.ignore_consistency,
6046
      "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
6047
      "OLD_PRIMARY": source_node,
6048
      "NEW_PRIMARY": target_node,
6049
      }
6050

    
6051
    if instance.disk_template in constants.DTS_INT_MIRROR:
6052
      env["OLD_SECONDARY"] = instance.secondary_nodes[0]
6053
      env["NEW_SECONDARY"] = source_node
6054
    else:
6055
      env["OLD_SECONDARY"] = env["NEW_SECONDARY"] = ""
6056

    
6057
    env.update(_BuildInstanceHookEnvByObject(self, instance))
6058

    
6059
    return env
6060

    
6061
  def BuildHooksNodes(self):
6062
    """Build hooks nodes.
6063

6064
    """
6065
    instance = self._migrater.instance
6066
    nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes)
6067
    return (nl, nl + [instance.primary_node])
6068

    
6069

    
6070
class LUInstanceMigrate(LogicalUnit):
6071
  """Migrate an instance.
6072

6073
  This is migration without shutting down, compared to the failover,
6074
  which is done with shutdown.
6075

6076
  """
6077
  HPATH = "instance-migrate"
6078
  HTYPE = constants.HTYPE_INSTANCE
6079
  REQ_BGL = False
6080

    
6081
  def ExpandNames(self):
6082
    self._ExpandAndLockInstance()
6083

    
6084
    if self.op.target_node is not None:
6085
      self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node)
6086

    
6087
    self.needed_locks[locking.LEVEL_NODE] = []
6088
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6089

    
6090
    self._migrater = TLMigrateInstance(self, self.op.instance_name,
6091
                                       cleanup=self.op.cleanup,
6092
                                       failover=False,
6093
                                       fallback=self.op.allow_failover)
6094
    self.tasklets = [self._migrater]
6095

    
6096
  def DeclareLocks(self, level):
6097
    if level == locking.LEVEL_NODE:
6098
      instance = self.context.cfg.GetInstanceInfo(self.op.instance_name)
6099
      if instance.disk_template in constants.DTS_EXT_MIRROR:
6100
        if self.op.target_node is None:
6101
          self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
6102
        else:
6103
          self.needed_locks[locking.LEVEL_NODE] = [instance.primary_node,
6104
                                                   self.op.target_node]
6105
        del self.recalculate_locks[locking.LEVEL_NODE]
6106
      else:
6107
        self._LockInstancesNodes()
6108

    
6109
  def BuildHooksEnv(self):
6110
    """Build hooks env.
6111

6112
    This runs on master, primary and secondary nodes of the instance.
6113

6114
    """
6115
    instance = self._migrater.instance
6116
    source_node = instance.primary_node
6117
    target_node = self.op.target_node
6118
    env = _BuildInstanceHookEnvByObject(self, instance)
6119
    env.update({
6120
      "MIGRATE_LIVE": self._migrater.live,
6121
      "MIGRATE_CLEANUP": self.op.cleanup,
6122
      "OLD_PRIMARY": source_node,
6123
      "NEW_PRIMARY": target_node,
6124
      })
6125

    
6126
    if instance.disk_template in constants.DTS_INT_MIRROR:
6127
      env["OLD_SECONDARY"] = target_node
6128
      env["NEW_SECONDARY"] = source_node
6129
    else:
6130
      env["OLD_SECONDARY"] = env["NEW_SECONDARY"] = None
6131

    
6132
    return env
6133

    
6134
  def BuildHooksNodes(self):
6135
    """Build hooks nodes.
6136

6137
    """
6138
    instance = self._migrater.instance
6139
    nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes)
6140
    return (nl, nl + [instance.primary_node])
6141

    
6142

    
6143
class LUInstanceMove(LogicalUnit):
6144
  """Move an instance by data-copying.
6145

6146
  """
6147
  HPATH = "instance-move"
6148
  HTYPE = constants.HTYPE_INSTANCE
6149
  REQ_BGL = False
6150

    
6151
  def ExpandNames(self):
6152
    self._ExpandAndLockInstance()
6153
    target_node = _ExpandNodeName(self.cfg, self.op.target_node)
6154
    self.op.target_node = target_node
6155
    self.needed_locks[locking.LEVEL_NODE] = [target_node]
6156
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
6157

    
6158
  def DeclareLocks(self, level):
6159
    if level == locking.LEVEL_NODE:
6160
      self._LockInstancesNodes(primary_only=True)
6161

    
6162
  def BuildHooksEnv(self):
6163
    """Build hooks env.
6164

6165
    This runs on master, primary and secondary nodes of the instance.
6166

6167
    """
6168
    env = {
6169
      "TARGET_NODE": self.op.target_node,
6170
      "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
6171
      }
6172
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
6173
    return env
6174

    
6175
  def BuildHooksNodes(self):
6176
    """Build hooks nodes.
6177

6178
    """
6179
    nl = [
6180
      self.cfg.GetMasterNode(),
6181
      self.instance.primary_node,
6182
      self.op.target_node,
6183
      ]
6184
    return (nl, nl)
6185

    
6186
  def CheckPrereq(self):
6187
    """Check prerequisites.
6188

6189
    This checks that the instance is in the cluster.
6190

6191
    """
6192
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6193
    assert self.instance is not None, \
6194
      "Cannot retrieve locked instance %s" % self.op.instance_name
6195

    
6196
    node = self.cfg.GetNodeInfo(self.op.target_node)
6197
    assert node is not None, \
6198
      "Cannot retrieve locked node %s" % self.op.target_node
6199

    
6200
    self.target_node = target_node = node.name
6201

    
6202
    if target_node == instance.primary_node:
6203
      raise errors.OpPrereqError("Instance %s is already on the node %s" %
6204
                                 (instance.name, target_node),
6205
                                 errors.ECODE_STATE)
6206

    
6207
    bep = self.cfg.GetClusterInfo().FillBE(instance)
6208

    
6209
    for idx, dsk in enumerate(instance.disks):
6210
      if dsk.dev_type not in (constants.LD_LV, constants.LD_FILE):
6211
        raise errors.OpPrereqError("Instance disk %d has a complex layout,"
6212
                                   " cannot copy" % idx, errors.ECODE_STATE)
6213

    
6214
    _CheckNodeOnline(self, target_node)
6215
    _CheckNodeNotDrained(self, target_node)
6216
    _CheckNodeVmCapable(self, target_node)
6217

    
6218
    if instance.admin_up:
6219
      # check memory requirements on the secondary node
6220
      _CheckNodeFreeMemory(self, target_node, "failing over instance %s" %
6221
                           instance.name, bep[constants.BE_MEMORY],
6222
                           instance.hypervisor)
6223
    else:
6224
      self.LogInfo("Not checking memory on the secondary node as"
6225
                   " instance will not be started")
6226

    
6227
    # check bridge existance
6228
    _CheckInstanceBridgesExist(self, instance, node=target_node)
6229

    
6230
  def Exec(self, feedback_fn):
6231
    """Move an instance.
6232

6233
    The move is done by shutting it down on its present node, copying
6234
    the data over (slow) and starting it on the new node.
6235

6236
    """
6237
    instance = self.instance
6238

    
6239
    source_node = instance.primary_node
6240
    target_node = self.target_node
6241

    
6242
    self.LogInfo("Shutting down instance %s on source node %s",
6243
                 instance.name, source_node)
6244

    
6245
    result = self.rpc.call_instance_shutdown(source_node, instance,
6246
                                             self.op.shutdown_timeout)
6247
    msg = result.fail_msg
6248
    if msg:
6249
      if self.op.ignore_consistency:
6250
        self.proc.LogWarning("Could not shutdown instance %s on node %s."
6251
                             " Proceeding anyway. Please make sure node"
6252
                             " %s is down. Error details: %s",
6253
                             instance.name, source_node, source_node, msg)
6254
      else:
6255
        raise errors.OpExecError("Could not shutdown instance %s on"
6256
                                 " node %s: %s" %
6257
                                 (instance.name, source_node, msg))
6258

    
6259
    # create the target disks
6260
    try:
6261
      _CreateDisks(self, instance, target_node=target_node)
6262
    except errors.OpExecError:
6263
      self.LogWarning("Device creation failed, reverting...")
6264
      try:
6265
        _RemoveDisks(self, instance, target_node=target_node)
6266
      finally:
6267
        self.cfg.ReleaseDRBDMinors(instance.name)
6268
        raise
6269

    
6270
    cluster_name = self.cfg.GetClusterInfo().cluster_name
6271

    
6272
    errs = []
6273
    # activate, get path, copy the data over
6274
    for idx, disk in enumerate(instance.disks):
6275
      self.LogInfo("Copying data for disk %d", idx)
6276
      result = self.rpc.call_blockdev_assemble(target_node, disk,
6277
                                               instance.name, True, idx)
6278
      if result.fail_msg:
6279
        self.LogWarning("Can't assemble newly created disk %d: %s",
6280
                        idx, result.fail_msg)
6281
        errs.append(result.fail_msg)
6282
        break
6283
      dev_path = result.payload
6284
      result = self.rpc.call_blockdev_export(source_node, disk,
6285
                                             target_node, dev_path,
6286
                                             cluster_name)
6287
      if result.fail_msg:
6288
        self.LogWarning("Can't copy data over for disk %d: %s",
6289
                        idx, result.fail_msg)
6290
        errs.append(result.fail_msg)
6291
        break
6292

    
6293
    if errs:
6294
      self.LogWarning("Some disks failed to copy, aborting")
6295
      try:
6296
        _RemoveDisks(self, instance, target_node=target_node)
6297
      finally:
6298
        self.cfg.ReleaseDRBDMinors(instance.name)
6299
        raise errors.OpExecError("Errors during disk copy: %s" %
6300
                                 (",".join(errs),))
6301

    
6302
    instance.primary_node = target_node
6303
    self.cfg.Update(instance, feedback_fn)
6304

    
6305
    self.LogInfo("Removing the disks on the original node")
6306
    _RemoveDisks(self, instance, target_node=source_node)
6307

    
6308
    # Only start the instance if it's marked as up
6309
    if instance.admin_up:
6310
      self.LogInfo("Starting instance %s on node %s",
6311
                   instance.name, target_node)
6312

    
6313
      disks_ok, _ = _AssembleInstanceDisks(self, instance,
6314
                                           ignore_secondaries=True)
6315
      if not disks_ok:
6316
        _ShutdownInstanceDisks(self, instance)
6317
        raise errors.OpExecError("Can't activate the instance's disks")
6318

    
6319
      result = self.rpc.call_instance_start(target_node, instance, None, None)
6320
      msg = result.fail_msg
6321
      if msg:
6322
        _ShutdownInstanceDisks(self, instance)
6323
        raise errors.OpExecError("Could not start instance %s on node %s: %s" %
6324
                                 (instance.name, target_node, msg))
6325

    
6326

    
6327
class LUNodeMigrate(LogicalUnit):
6328
  """Migrate all instances from a node.
6329

6330
  """
6331
  HPATH = "node-migrate"
6332
  HTYPE = constants.HTYPE_NODE
6333
  REQ_BGL = False
6334

    
6335
  def CheckArguments(self):
6336
    _CheckIAllocatorOrNode(self, "iallocator", "remote_node")
6337

    
6338
  def ExpandNames(self):
6339
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
6340

    
6341
    self.needed_locks = {}
6342

    
6343
    # Create tasklets for migrating instances for all instances on this node
6344
    names = []
6345
    tasklets = []
6346

    
6347
    self.lock_all_nodes = False
6348

    
6349
    for inst in _GetNodePrimaryInstances(self.cfg, self.op.node_name):
6350
      logging.debug("Migrating instance %s", inst.name)
6351
      names.append(inst.name)
6352

    
6353
      tasklets.append(TLMigrateInstance(self, inst.name, cleanup=False))
6354

    
6355
      if inst.disk_template in constants.DTS_EXT_MIRROR:
6356
        # We need to lock all nodes, as the iallocator will choose the
6357
        # destination nodes afterwards
6358
        self.lock_all_nodes = True
6359

    
6360
    self.tasklets = tasklets
6361

    
6362
    # Declare node locks
6363
    if self.lock_all_nodes:
6364
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
6365
    else:
6366
      self.needed_locks[locking.LEVEL_NODE] = [self.op.node_name]
6367
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
6368

    
6369
    # Declare instance locks
6370
    self.needed_locks[locking.LEVEL_INSTANCE] = names
6371

    
6372
  def DeclareLocks(self, level):
6373
    if level == locking.LEVEL_NODE and not self.lock_all_nodes:
6374
      self._LockInstancesNodes()
6375

    
6376
  def BuildHooksEnv(self):
6377
    """Build hooks env.
6378

6379
    This runs on the master, the primary and all the secondaries.
6380

6381
    """
6382
    return {
6383
      "NODE_NAME": self.op.node_name,
6384
      }
6385

    
6386
  def BuildHooksNodes(self):
6387
    """Build hooks nodes.
6388

6389
    """
6390
    nl = [self.cfg.GetMasterNode()]
6391
    return (nl, nl)
6392

    
6393

    
6394
class TLMigrateInstance(Tasklet):
6395
  """Tasklet class for instance migration.
6396

6397
  @type live: boolean
6398
  @ivar live: whether the migration will be done live or non-live;
6399
      this variable is initalized only after CheckPrereq has run
6400
  @type cleanup: boolean
6401
  @ivar cleanup: Wheater we cleanup from a failed migration
6402
  @type iallocator: string
6403
  @ivar iallocator: The iallocator used to determine target_node
6404
  @type target_node: string
6405
  @ivar target_node: If given, the target_node to reallocate the instance to
6406
  @type failover: boolean
6407
  @ivar failover: Whether operation results in failover or migration
6408
  @type fallback: boolean
6409
  @ivar fallback: Whether fallback to failover is allowed if migration not
6410
                  possible
6411
  @type ignore_consistency: boolean
6412
  @ivar ignore_consistency: Wheter we should ignore consistency between source
6413
                            and target node
6414
  @type shutdown_timeout: int
6415
  @ivar shutdown_timeout: In case of failover timeout of the shutdown
6416

6417
  """
6418
  def __init__(self, lu, instance_name, cleanup=False,
6419
               failover=False, fallback=False,
6420
               ignore_consistency=False,
6421
               shutdown_timeout=constants.DEFAULT_SHUTDOWN_TIMEOUT):
6422
    """Initializes this class.
6423

6424
    """
6425
    Tasklet.__init__(self, lu)
6426

    
6427
    # Parameters
6428
    self.instance_name = instance_name
6429
    self.cleanup = cleanup
6430
    self.live = False # will be overridden later
6431
    self.failover = failover
6432
    self.fallback = fallback
6433
    self.ignore_consistency = ignore_consistency
6434
    self.shutdown_timeout = shutdown_timeout
6435

    
6436
  def CheckPrereq(self):
6437
    """Check prerequisites.
6438

6439
    This checks that the instance is in the cluster.
6440

6441
    """
6442
    instance_name = _ExpandInstanceName(self.lu.cfg, self.instance_name)
6443
    instance = self.cfg.GetInstanceInfo(instance_name)
6444
    assert instance is not None
6445
    self.instance = instance
6446

    
6447
    if (not self.cleanup and not instance.admin_up and not self.failover and
6448
        self.fallback):
6449
      self.lu.LogInfo("Instance is marked down, fallback allowed, switching"
6450
                      " to failover")
6451
      self.failover = True
6452

    
6453
    if instance.disk_template not in constants.DTS_MIRRORED:
6454
      if self.failover:
6455
        text = "failovers"
6456
      else:
6457
        text = "migrations"
6458
      raise errors.OpPrereqError("Instance's disk layout '%s' does not allow"
6459
                                 " %s" % (instance.disk_template, text),
6460
                                 errors.ECODE_STATE)
6461

    
6462
    if instance.disk_template in constants.DTS_EXT_MIRROR:
6463
      _CheckIAllocatorOrNode(self.lu, "iallocator", "target_node")
6464

    
6465
      if self.lu.op.iallocator:
6466
        self._RunAllocator()
6467
      else:
6468
        # We set set self.target_node as it is required by
6469
        # BuildHooksEnv
6470
        self.target_node = self.lu.op.target_node
6471

    
6472
      # self.target_node is already populated, either directly or by the
6473
      # iallocator run
6474
      target_node = self.target_node
6475

    
6476
      if len(self.lu.tasklets) == 1:
6477
        # It is safe to release locks only when we're the only tasklet
6478
        # in the LU
6479
        _ReleaseLocks(self.lu, locking.LEVEL_NODE,
6480
                      keep=[instance.primary_node, self.target_node])
6481

    
6482
    else:
6483
      secondary_nodes = instance.secondary_nodes
6484
      if not secondary_nodes:
6485
        raise errors.ConfigurationError("No secondary node but using"
6486
                                        " %s disk template" %
6487
                                        instance.disk_template)
6488
      target_node = secondary_nodes[0]
6489
      if self.lu.op.iallocator or (self.lu.op.target_node and
6490
                                   self.lu.op.target_node != target_node):
6491
        if self.failover:
6492
          text = "failed over"
6493
        else:
6494
          text = "migrated"
6495
        raise errors.OpPrereqError("Instances with disk template %s cannot"
6496
                                   " be %s to arbitrary nodes"
6497
                                   " (neither an iallocator nor a target"
6498
                                   " node can be passed)" %
6499
                                   (instance.disk_template, text),
6500
                                   errors.ECODE_INVAL)
6501

    
6502
    i_be = self.cfg.GetClusterInfo().FillBE(instance)
6503

    
6504
    # check memory requirements on the secondary node
6505
    if not self.failover or instance.admin_up:
6506
      _CheckNodeFreeMemory(self.lu, target_node, "migrating instance %s" %
6507
                           instance.name, i_be[constants.BE_MEMORY],
6508
                           instance.hypervisor)
6509
    else:
6510
      self.lu.LogInfo("Not checking memory on the secondary node as"
6511
                      " instance will not be started")
6512

    
6513
    # check bridge existance
6514
    _CheckInstanceBridgesExist(self.lu, instance, node=target_node)
6515

    
6516
    if not self.cleanup:
6517
      _CheckNodeNotDrained(self.lu, target_node)
6518
      if not self.failover:
6519
        result = self.rpc.call_instance_migratable(instance.primary_node,
6520
                                                   instance)
6521
        if result.fail_msg and self.fallback:
6522
          self.lu.LogInfo("Can't migrate, instance offline, fallback to"
6523
                          " failover")
6524
          self.failover = True
6525
        else:
6526
          result.Raise("Can't migrate, please use failover",
6527
                       prereq=True, ecode=errors.ECODE_STATE)
6528

    
6529
    assert not (self.failover and self.cleanup)
6530

    
6531
    if not self.failover:
6532
      if self.lu.op.live is not None and self.lu.op.mode is not None:
6533
        raise errors.OpPrereqError("Only one of the 'live' and 'mode'"
6534
                                   " parameters are accepted",
6535
                                   errors.ECODE_INVAL)
6536
      if self.lu.op.live is not None:
6537
        if self.lu.op.live:
6538
          self.lu.op.mode = constants.HT_MIGRATION_LIVE
6539
        else:
6540
          self.lu.op.mode = constants.HT_MIGRATION_NONLIVE
6541
        # reset the 'live' parameter to None so that repeated
6542
        # invocations of CheckPrereq do not raise an exception
6543
        self.lu.op.live = None
6544
      elif self.lu.op.mode is None:
6545
        # read the default value from the hypervisor
6546
        i_hv = self.cfg.GetClusterInfo().FillHV(self.instance,
6547
                                                skip_globals=False)
6548
        self.lu.op.mode = i_hv[constants.HV_MIGRATION_MODE]
6549

    
6550
      self.live = self.lu.op.mode == constants.HT_MIGRATION_LIVE
6551
    else:
6552
      # Failover is never live
6553
      self.live = False
6554

    
6555
  def _RunAllocator(self):
6556
    """Run the allocator based on input opcode.
6557

6558
    """
6559
    ial = IAllocator(self.cfg, self.rpc,
6560
                     mode=constants.IALLOCATOR_MODE_RELOC,
6561
                     name=self.instance_name,
6562
                     # TODO See why hail breaks with a single node below
6563
                     relocate_from=[self.instance.primary_node,
6564
                                    self.instance.primary_node],
6565
                     )
6566

    
6567
    ial.Run(self.lu.op.iallocator)
6568

    
6569
    if not ial.success:
6570
      raise errors.OpPrereqError("Can't compute nodes using"
6571
                                 " iallocator '%s': %s" %
6572
                                 (self.lu.op.iallocator, ial.info),
6573
                                 errors.ECODE_NORES)
6574
    if len(ial.result) != ial.required_nodes:
6575
      raise errors.OpPrereqError("iallocator '%s' returned invalid number"
6576
                                 " of nodes (%s), required %s" %
6577
                                 (self.lu.op.iallocator, len(ial.result),
6578
                                  ial.required_nodes), errors.ECODE_FAULT)
6579
    self.target_node = ial.result[0]
6580
    self.lu.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
6581
                 self.instance_name, self.lu.op.iallocator,
6582
                 utils.CommaJoin(ial.result))
6583

    
6584
  def _WaitUntilSync(self):
6585
    """Poll with custom rpc for disk sync.
6586

6587
    This uses our own step-based rpc call.
6588

6589
    """
6590
    self.feedback_fn("* wait until resync is done")
6591
    all_done = False
6592
    while not all_done:
6593
      all_done = True
6594
      result = self.rpc.call_drbd_wait_sync(self.all_nodes,
6595
                                            self.nodes_ip,
6596
                                            self.instance.disks)
6597
      min_percent = 100
6598
      for node, nres in result.items():
6599
        nres.Raise("Cannot resync disks on node %s" % node)
6600
        node_done, node_percent = nres.payload
6601
        all_done = all_done and node_done
6602
        if node_percent is not None:
6603
          min_percent = min(min_percent, node_percent)
6604
      if not all_done:
6605
        if min_percent < 100:
6606
          self.feedback_fn("   - progress: %.1f%%" % min_percent)
6607
        time.sleep(2)
6608

    
6609
  def _EnsureSecondary(self, node):
6610
    """Demote a node to secondary.
6611

6612
    """
6613
    self.feedback_fn("* switching node %s to secondary mode" % node)
6614

    
6615
    for dev in self.instance.disks:
6616
      self.cfg.SetDiskID(dev, node)
6617

    
6618
    result = self.rpc.call_blockdev_close(node, self.instance.name,
6619
                                          self.instance.disks)
6620
    result.Raise("Cannot change disk to secondary on node %s" % node)
6621

    
6622
  def _GoStandalone(self):
6623
    """Disconnect from the network.
6624

6625
    """
6626
    self.feedback_fn("* changing into standalone mode")
6627
    result = self.rpc.call_drbd_disconnect_net(self.all_nodes, self.nodes_ip,
6628
                                               self.instance.disks)
6629
    for node, nres in result.items():
6630
      nres.Raise("Cannot disconnect disks node %s" % node)
6631

    
6632
  def _GoReconnect(self, multimaster):
6633
    """Reconnect to the network.
6634

6635
    """
6636
    if multimaster:
6637
      msg = "dual-master"
6638
    else:
6639
      msg = "single-master"
6640
    self.feedback_fn("* changing disks into %s mode" % msg)
6641
    result = self.rpc.call_drbd_attach_net(self.all_nodes, self.nodes_ip,
6642
                                           self.instance.disks,
6643
                                           self.instance.name, multimaster)
6644
    for node, nres in result.items():
6645
      nres.Raise("Cannot change disks config on node %s" % node)
6646

    
6647
  def _ExecCleanup(self):
6648
    """Try to cleanup after a failed migration.
6649

6650
    The cleanup is done by:
6651
      - check that the instance is running only on one node
6652
        (and update the config if needed)
6653
      - change disks on its secondary node to secondary
6654
      - wait until disks are fully synchronized
6655
      - disconnect from the network
6656
      - change disks into single-master mode
6657
      - wait again until disks are fully synchronized
6658

6659
    """
6660
    instance = self.instance
6661
    target_node = self.target_node
6662
    source_node = self.source_node
6663

    
6664
    # check running on only one node
6665
    self.feedback_fn("* checking where the instance actually runs"
6666
                     " (if this hangs, the hypervisor might be in"
6667
                     " a bad state)")
6668
    ins_l = self.rpc.call_instance_list(self.all_nodes, [instance.hypervisor])
6669
    for node, result in ins_l.items():
6670
      result.Raise("Can't contact node %s" % node)
6671

    
6672
    runningon_source = instance.name in ins_l[source_node].payload
6673
    runningon_target = instance.name in ins_l[target_node].payload
6674

    
6675
    if runningon_source and runningon_target:
6676
      raise errors.OpExecError("Instance seems to be running on two nodes,"
6677
                               " or the hypervisor is confused; you will have"
6678
                               " to ensure manually that it runs only on one"
6679
                               " and restart this operation")
6680

    
6681
    if not (runningon_source or runningon_target):
6682
      raise errors.OpExecError("Instance does not seem to be running at all;"
6683
                               " in this case it's safer to repair by"
6684
                               " running 'gnt-instance stop' to ensure disk"
6685
                               " shutdown, and then restarting it")
6686

    
6687
    if runningon_target:
6688
      # the migration has actually succeeded, we need to update the config
6689
      self.feedback_fn("* instance running on secondary node (%s),"
6690
                       " updating config" % target_node)
6691
      instance.primary_node = target_node
6692
      self.cfg.Update(instance, self.feedback_fn)
6693
      demoted_node = source_node
6694
    else:
6695
      self.feedback_fn("* instance confirmed to be running on its"
6696
                       " primary node (%s)" % source_node)
6697
      demoted_node = target_node
6698

    
6699
    if instance.disk_template in constants.DTS_INT_MIRROR:
6700
      self._EnsureSecondary(demoted_node)
6701
      try:
6702
        self._WaitUntilSync()
6703
      except errors.OpExecError:
6704
        # we ignore here errors, since if the device is standalone, it
6705
        # won't be able to sync
6706
        pass
6707
      self._GoStandalone()
6708
      self._GoReconnect(False)
6709
      self._WaitUntilSync()
6710

    
6711
    self.feedback_fn("* done")
6712

    
6713
  def _RevertDiskStatus(self):
6714
    """Try to revert the disk status after a failed migration.
6715

6716
    """
6717
    target_node = self.target_node
6718
    if self.instance.disk_template in constants.DTS_EXT_MIRROR:
6719
      return
6720

    
6721
    try:
6722
      self._EnsureSecondary(target_node)
6723
      self._GoStandalone()
6724
      self._GoReconnect(False)
6725
      self._WaitUntilSync()
6726
    except errors.OpExecError, err:
6727
      self.lu.LogWarning("Migration failed and I can't reconnect the drives,"
6728
                         " please try to recover the instance manually;"
6729
                         " error '%s'" % str(err))
6730

    
6731
  def _AbortMigration(self):
6732
    """Call the hypervisor code to abort a started migration.
6733

6734
    """
6735
    instance = self.instance
6736
    target_node = self.target_node
6737
    migration_info = self.migration_info
6738

    
6739
    abort_result = self.rpc.call_finalize_migration(target_node,
6740
                                                    instance,
6741
                                                    migration_info,
6742
                                                    False)
6743
    abort_msg = abort_result.fail_msg
6744
    if abort_msg:
6745
      logging.error("Aborting migration failed on target node %s: %s",
6746
                    target_node, abort_msg)
6747
      # Don't raise an exception here, as we stil have to try to revert the
6748
      # disk status, even if this step failed.
6749

    
6750
  def _ExecMigration(self):
6751
    """Migrate an instance.
6752

6753
    The migrate is done by:
6754
      - change the disks into dual-master mode
6755
      - wait until disks are fully synchronized again
6756
      - migrate the instance
6757
      - change disks on the new secondary node (the old primary) to secondary
6758
      - wait until disks are fully synchronized
6759
      - change disks into single-master mode
6760

6761
    """
6762
    instance = self.instance
6763
    target_node = self.target_node
6764
    source_node = self.source_node
6765

    
6766
    self.feedback_fn("* checking disk consistency between source and target")
6767
    for dev in instance.disks:
6768
      if not _CheckDiskConsistency(self.lu, dev, target_node, False):
6769
        raise errors.OpExecError("Disk %s is degraded or not fully"
6770
                                 " synchronized on target node,"
6771
                                 " aborting migration" % dev.iv_name)
6772

    
6773
    # First get the migration information from the remote node
6774
    result = self.rpc.call_migration_info(source_node, instance)
6775
    msg = result.fail_msg
6776
    if msg:
6777
      log_err = ("Failed fetching source migration information from %s: %s" %
6778
                 (source_node, msg))
6779
      logging.error(log_err)
6780
      raise errors.OpExecError(log_err)
6781

    
6782
    self.migration_info = migration_info = result.payload
6783

    
6784
    if self.instance.disk_template not in constants.DTS_EXT_MIRROR:
6785
      # Then switch the disks to master/master mode
6786
      self._EnsureSecondary(target_node)
6787
      self._GoStandalone()
6788
      self._GoReconnect(True)
6789
      self._WaitUntilSync()
6790

    
6791
    self.feedback_fn("* preparing %s to accept the instance" % target_node)
6792
    result = self.rpc.call_accept_instance(target_node,
6793
                                           instance,
6794
                                           migration_info,
6795
                                           self.nodes_ip[target_node])
6796

    
6797
    msg = result.fail_msg
6798
    if msg:
6799
      logging.error("Instance pre-migration failed, trying to revert"
6800
                    " disk status: %s", msg)
6801
      self.feedback_fn("Pre-migration failed, aborting")
6802
      self._AbortMigration()
6803
      self._RevertDiskStatus()
6804
      raise errors.OpExecError("Could not pre-migrate instance %s: %s" %
6805
                               (instance.name, msg))
6806

    
6807
    self.feedback_fn("* migrating instance to %s" % target_node)
6808
    result = self.rpc.call_instance_migrate(source_node, instance,
6809
                                            self.nodes_ip[target_node],
6810
                                            self.live)
6811
    msg = result.fail_msg
6812
    if msg:
6813
      logging.error("Instance migration failed, trying to revert"
6814
                    " disk status: %s", msg)
6815
      self.feedback_fn("Migration failed, aborting")
6816
      self._AbortMigration()
6817
      self._RevertDiskStatus()
6818
      raise errors.OpExecError("Could not migrate instance %s: %s" %
6819
                               (instance.name, msg))
6820

    
6821
    instance.primary_node = target_node
6822
    # distribute new instance config to the other nodes
6823
    self.cfg.Update(instance, self.feedback_fn)
6824

    
6825
    result = self.rpc.call_finalize_migration(target_node,
6826
                                              instance,
6827
                                              migration_info,
6828
                                              True)
6829
    msg = result.fail_msg
6830
    if msg:
6831
      logging.error("Instance migration succeeded, but finalization failed:"
6832
                    " %s", msg)
6833
      raise errors.OpExecError("Could not finalize instance migration: %s" %
6834
                               msg)
6835

    
6836
    if self.instance.disk_template not in constants.DTS_EXT_MIRROR:
6837
      self._EnsureSecondary(source_node)
6838
      self._WaitUntilSync()
6839
      self._GoStandalone()
6840
      self._GoReconnect(False)
6841
      self._WaitUntilSync()
6842

    
6843
    self.feedback_fn("* done")
6844

    
6845
  def _ExecFailover(self):
6846
    """Failover an instance.
6847

6848
    The failover is done by shutting it down on its present node and
6849
    starting it on the secondary.
6850

6851
    """
6852
    instance = self.instance
6853
    primary_node = self.cfg.GetNodeInfo(instance.primary_node)
6854

    
6855
    source_node = instance.primary_node
6856
    target_node = self.target_node
6857

    
6858
    if instance.admin_up:
6859
      self.feedback_fn("* checking disk consistency between source and target")
6860
      for dev in instance.disks:
6861
        # for drbd, these are drbd over lvm
6862
        if not _CheckDiskConsistency(self, dev, target_node, False):
6863
          if not self.ignore_consistency:
6864
            raise errors.OpExecError("Disk %s is degraded on target node,"
6865
                                     " aborting failover" % dev.iv_name)
6866
    else:
6867
      self.feedback_fn("* not checking disk consistency as instance is not"
6868
                       " running")
6869

    
6870
    self.feedback_fn("* shutting down instance on source node")
6871
    logging.info("Shutting down instance %s on node %s",
6872
                 instance.name, source_node)
6873

    
6874
    result = self.rpc.call_instance_shutdown(source_node, instance,
6875
                                             self.shutdown_timeout)
6876
    msg = result.fail_msg
6877
    if msg:
6878
      if self.ignore_consistency or primary_node.offline:
6879
        self.lu.LogWarning("Could not shutdown instance %s on node %s,"
6880
                           " proceeding anyway; please make sure node"
6881
                           " %s is down; error details: %s",
6882
                           instance.name, source_node, source_node, msg)
6883
      else:
6884
        raise errors.OpExecError("Could not shutdown instance %s on"
6885
                                 " node %s: %s" %
6886
                                 (instance.name, source_node, msg))
6887

    
6888
    self.feedback_fn("* deactivating the instance's disks on source node")
6889
    if not _ShutdownInstanceDisks(self, instance, ignore_primary=True):
6890
      raise errors.OpExecError("Can't shut down the instance's disks.")
6891

    
6892
    instance.primary_node = target_node
6893
    # distribute new instance config to the other nodes
6894
    self.cfg.Update(instance, self.feedback_fn)
6895

    
6896
    # Only start the instance if it's marked as up
6897
    if instance.admin_up:
6898
      self.feedback_fn("* activating the instance's disks on target node")
6899
      logging.info("Starting instance %s on node %s",
6900
                   instance.name, target_node)
6901

    
6902
      disks_ok, _ = _AssembleInstanceDisks(self, instance,
6903
                                           ignore_secondaries=True)
6904
      if not disks_ok:
6905
        _ShutdownInstanceDisks(self, instance)
6906
        raise errors.OpExecError("Can't activate the instance's disks")
6907

    
6908
      self.feedback_fn("* starting the instance on the target node")
6909
      result = self.rpc.call_instance_start(target_node, instance, None, None)
6910
      msg = result.fail_msg
6911
      if msg:
6912
        _ShutdownInstanceDisks(self, instance)
6913
        raise errors.OpExecError("Could not start instance %s on node %s: %s" %
6914
                                 (instance.name, target_node, msg))
6915

    
6916
  def Exec(self, feedback_fn):
6917
    """Perform the migration.
6918

6919
    """
6920
    self.feedback_fn = feedback_fn
6921
    self.source_node = self.instance.primary_node
6922

    
6923
    # FIXME: if we implement migrate-to-any in DRBD, this needs fixing
6924
    if self.instance.disk_template in constants.DTS_INT_MIRROR:
6925
      self.target_node = self.instance.secondary_nodes[0]
6926
      # Otherwise self.target_node has been populated either
6927
      # directly, or through an iallocator.
6928

    
6929
    self.all_nodes = [self.source_node, self.target_node]
6930
    self.nodes_ip = {
6931
      self.source_node: self.cfg.GetNodeInfo(self.source_node).secondary_ip,
6932
      self.target_node: self.cfg.GetNodeInfo(self.target_node).secondary_ip,
6933
      }
6934

    
6935
    if self.failover:
6936
      feedback_fn("Failover instance %s" % self.instance.name)
6937
      self._ExecFailover()
6938
    else:
6939
      feedback_fn("Migrating instance %s" % self.instance.name)
6940

    
6941
      if self.cleanup:
6942
        return self._ExecCleanup()
6943
      else:
6944
        return self._ExecMigration()
6945

    
6946

    
6947
def _CreateBlockDev(lu, node, instance, device, force_create,
6948
                    info, force_open):
6949
  """Create a tree of block devices on a given node.
6950

6951
  If this device type has to be created on secondaries, create it and
6952
  all its children.
6953

6954
  If not, just recurse to children keeping the same 'force' value.
6955

6956
  @param lu: the lu on whose behalf we execute
6957
  @param node: the node on which to create the device
6958
  @type instance: L{objects.Instance}
6959
  @param instance: the instance which owns the device
6960
  @type device: L{objects.Disk}
6961
  @param device: the device to create
6962
  @type force_create: boolean
6963
  @param force_create: whether to force creation of this device; this
6964
      will be change to True whenever we find a device which has
6965
      CreateOnSecondary() attribute
6966
  @param info: the extra 'metadata' we should attach to the device
6967
      (this will be represented as a LVM tag)
6968
  @type force_open: boolean
6969
  @param force_open: this parameter will be passes to the
6970
      L{backend.BlockdevCreate} function where it specifies
6971
      whether we run on primary or not, and it affects both
6972
      the child assembly and the device own Open() execution
6973

6974
  """
6975
  if device.CreateOnSecondary():
6976
    force_create = True
6977

    
6978
  if device.children:
6979
    for child in device.children:
6980
      _CreateBlockDev(lu, node, instance, child, force_create,
6981
                      info, force_open)
6982

    
6983
  if not force_create:
6984
    return
6985

    
6986
  _CreateSingleBlockDev(lu, node, instance, device, info, force_open)
6987

    
6988

    
6989
def _CreateSingleBlockDev(lu, node, instance, device, info, force_open):
6990
  """Create a single block device on a given node.
6991

6992
  This will not recurse over children of the device, so they must be
6993
  created in advance.
6994

6995
  @param lu: the lu on whose behalf we execute
6996
  @param node: the node on which to create the device
6997
  @type instance: L{objects.Instance}
6998
  @param instance: the instance which owns the device
6999
  @type device: L{objects.Disk}
7000
  @param device: the device to create
7001
  @param info: the extra 'metadata' we should attach to the device
7002
      (this will be represented as a LVM tag)
7003
  @type force_open: boolean
7004
  @param force_open: this parameter will be passes to the
7005
      L{backend.BlockdevCreate} function where it specifies
7006
      whether we run on primary or not, and it affects both
7007
      the child assembly and the device own Open() execution
7008

7009
  """
7010
  lu.cfg.SetDiskID(device, node)
7011
  result = lu.rpc.call_blockdev_create(node, device, device.size,
7012
                                       instance.name, force_open, info)
7013
  result.Raise("Can't create block device %s on"
7014
               " node %s for instance %s" % (device, node, instance.name))
7015
  if device.physical_id is None:
7016
    device.physical_id = result.payload
7017

    
7018

    
7019
def _GenerateUniqueNames(lu, exts):
7020
  """Generate a suitable LV name.
7021

7022
  This will generate a logical volume name for the given instance.
7023

7024
  """
7025
  results = []
7026
  for val in exts:
7027
    new_id = lu.cfg.GenerateUniqueID(lu.proc.GetECId())
7028
    results.append("%s%s" % (new_id, val))
7029
  return results
7030

    
7031

    
7032
def _GenerateDRBD8Branch(lu, primary, secondary, size, vgnames, names,
7033
                         iv_name, p_minor, s_minor):
7034
  """Generate a drbd8 device complete with its children.
7035

7036
  """
7037
  assert len(vgnames) == len(names) == 2
7038
  port = lu.cfg.AllocatePort()
7039
  shared_secret = lu.cfg.GenerateDRBDSecret(lu.proc.GetECId())
7040
  dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
7041
                          logical_id=(vgnames[0], names[0]))
7042
  dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
7043
                          logical_id=(vgnames[1], names[1]))
7044
  drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size,
7045
                          logical_id=(primary, secondary, port,
7046
                                      p_minor, s_minor,
7047
                                      shared_secret),
7048
                          children=[dev_data, dev_meta],
7049
                          iv_name=iv_name)
7050
  return drbd_dev
7051

    
7052

    
7053
def _GenerateDiskTemplate(lu, template_name,
7054
                          instance_name, primary_node,
7055
                          secondary_nodes, disk_info,
7056
                          file_storage_dir, file_driver,
7057
                          base_index, feedback_fn):
7058
  """Generate the entire disk layout for a given template type.
7059

7060
  """
7061
  #TODO: compute space requirements
7062

    
7063
  vgname = lu.cfg.GetVGName()
7064
  disk_count = len(disk_info)
7065
  disks = []
7066
  if template_name == constants.DT_DISKLESS:
7067
    pass
7068
  elif template_name == constants.DT_PLAIN:
7069
    if len(secondary_nodes) != 0:
7070
      raise errors.ProgrammerError("Wrong template configuration")
7071

    
7072
    names = _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
7073
                                      for i in range(disk_count)])
7074
    for idx, disk in enumerate(disk_info):
7075
      disk_index = idx + base_index
7076
      vg = disk.get(constants.IDISK_VG, vgname)
7077
      feedback_fn("* disk %i, vg %s, name %s" % (idx, vg, names[idx]))
7078
      disk_dev = objects.Disk(dev_type=constants.LD_LV,
7079
                              size=disk[constants.IDISK_SIZE],
7080
                              logical_id=(vg, names[idx]),
7081
                              iv_name="disk/%d" % disk_index,
7082
                              mode=disk[constants.IDISK_MODE])
7083
      disks.append(disk_dev)
7084
  elif template_name == constants.DT_DRBD8:
7085
    if len(secondary_nodes) != 1:
7086
      raise errors.ProgrammerError("Wrong template configuration")
7087
    remote_node = secondary_nodes[0]
7088
    minors = lu.cfg.AllocateDRBDMinor(
7089
      [primary_node, remote_node] * len(disk_info), instance_name)
7090

    
7091
    names = []
7092
    for lv_prefix in _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
7093
                                               for i in range(disk_count)]):
7094
      names.append(lv_prefix + "_data")
7095
      names.append(lv_prefix + "_meta")
7096
    for idx, disk in enumerate(disk_info):
7097
      disk_index = idx + base_index
7098
      data_vg = disk.get(constants.IDISK_VG, vgname)
7099
      meta_vg = disk.get(constants.IDISK_METAVG, data_vg)
7100
      disk_dev = _GenerateDRBD8Branch(lu, primary_node, remote_node,
7101
                                      disk[constants.IDISK_SIZE],
7102
                                      [data_vg, meta_vg],
7103
                                      names[idx * 2:idx * 2 + 2],
7104
                                      "disk/%d" % disk_index,
7105
                                      minors[idx * 2], minors[idx * 2 + 1])
7106
      disk_dev.mode = disk[constants.IDISK_MODE]
7107
      disks.append(disk_dev)
7108
  elif template_name == constants.DT_FILE:
7109
    if len(secondary_nodes) != 0:
7110
      raise errors.ProgrammerError("Wrong template configuration")
7111

    
7112
    opcodes.RequireFileStorage()
7113

    
7114
    for idx, disk in enumerate(disk_info):
7115
      disk_index = idx + base_index
7116
      disk_dev = objects.Disk(dev_type=constants.LD_FILE,
7117
                              size=disk[constants.IDISK_SIZE],
7118
                              iv_name="disk/%d" % disk_index,
7119
                              logical_id=(file_driver,
7120
                                          "%s/disk%d" % (file_storage_dir,
7121
                                                         disk_index)),
7122
                              mode=disk[constants.IDISK_MODE])
7123
      disks.append(disk_dev)
7124
  elif template_name == constants.DT_SHARED_FILE:
7125
    if len(secondary_nodes) != 0:
7126
      raise errors.ProgrammerError("Wrong template configuration")
7127

    
7128
    opcodes.RequireSharedFileStorage()
7129

    
7130
    for idx, disk in enumerate(disk_info):
7131
      disk_index = idx + base_index
7132
      disk_dev = objects.Disk(dev_type=constants.LD_FILE,
7133
                              size=disk[constants.IDISK_SIZE],
7134
                              iv_name="disk/%d" % disk_index,
7135
                              logical_id=(file_driver,
7136
                                          "%s/disk%d" % (file_storage_dir,
7137
                                                         disk_index)),
7138
                              mode=disk[constants.IDISK_MODE])
7139
      disks.append(disk_dev)
7140
  elif template_name == constants.DT_BLOCK:
7141
    if len(secondary_nodes) != 0:
7142
      raise errors.ProgrammerError("Wrong template configuration")
7143

    
7144
    for idx, disk in enumerate(disk_info):
7145
      disk_index = idx + base_index
7146
      disk_dev = objects.Disk(dev_type=constants.LD_BLOCKDEV,
7147
                              size=disk[constants.IDISK_SIZE],
7148
                              logical_id=(constants.BLOCKDEV_DRIVER_MANUAL,
7149
                                          disk[constants.IDISK_ADOPT]),
7150
                              iv_name="disk/%d" % disk_index,
7151
                              mode=disk[constants.IDISK_MODE])
7152
      disks.append(disk_dev)
7153

    
7154
  else:
7155
    raise errors.ProgrammerError("Invalid disk template '%s'" % template_name)
7156
  return disks
7157

    
7158

    
7159
def _GetInstanceInfoText(instance):
7160
  """Compute that text that should be added to the disk's metadata.
7161

7162
  """
7163
  return "originstname+%s" % instance.name
7164

    
7165

    
7166
def _CalcEta(time_taken, written, total_size):
7167
  """Calculates the ETA based on size written and total size.
7168

7169
  @param time_taken: The time taken so far
7170
  @param written: amount written so far
7171
  @param total_size: The total size of data to be written
7172
  @return: The remaining time in seconds
7173

7174
  """
7175
  avg_time = time_taken / float(written)
7176
  return (total_size - written) * avg_time
7177

    
7178

    
7179
def _WipeDisks(lu, instance):
7180
  """Wipes instance disks.
7181

7182
  @type lu: L{LogicalUnit}
7183
  @param lu: the logical unit on whose behalf we execute
7184
  @type instance: L{objects.Instance}
7185
  @param instance: the instance whose disks we should create
7186
  @return: the success of the wipe
7187

7188
  """
7189
  node = instance.primary_node
7190

    
7191
  for device in instance.disks:
7192
    lu.cfg.SetDiskID(device, node)
7193

    
7194
  logging.info("Pause sync of instance %s disks", instance.name)
7195
  result = lu.rpc.call_blockdev_pause_resume_sync(node, instance.disks, True)
7196

    
7197
  for idx, success in enumerate(result.payload):
7198
    if not success:
7199
      logging.warn("pause-sync of instance %s for disks %d failed",
7200
                   instance.name, idx)
7201

    
7202
  try:
7203
    for idx, device in enumerate(instance.disks):
7204
      # The wipe size is MIN_WIPE_CHUNK_PERCENT % of the instance disk but
7205
      # MAX_WIPE_CHUNK at max
7206
      wipe_chunk_size = min(constants.MAX_WIPE_CHUNK, device.size / 100.0 *
7207
                            constants.MIN_WIPE_CHUNK_PERCENT)
7208
      # we _must_ make this an int, otherwise rounding errors will
7209
      # occur
7210
      wipe_chunk_size = int(wipe_chunk_size)
7211

    
7212
      lu.LogInfo("* Wiping disk %d", idx)
7213
      logging.info("Wiping disk %d for instance %s, node %s using"
7214
                   " chunk size %s", idx, instance.name, node, wipe_chunk_size)
7215

    
7216
      offset = 0
7217
      size = device.size
7218
      last_output = 0
7219
      start_time = time.time()
7220

    
7221
      while offset < size:
7222
        wipe_size = min(wipe_chunk_size, size - offset)
7223
        logging.debug("Wiping disk %d, offset %s, chunk %s",
7224
                      idx, offset, wipe_size)
7225
        result = lu.rpc.call_blockdev_wipe(node, device, offset, wipe_size)
7226
        result.Raise("Could not wipe disk %d at offset %d for size %d" %
7227
                     (idx, offset, wipe_size))
7228
        now = time.time()
7229
        offset += wipe_size
7230
        if now - last_output >= 60:
7231
          eta = _CalcEta(now - start_time, offset, size)
7232
          lu.LogInfo(" - done: %.1f%% ETA: %s" %
7233
                     (offset / float(size) * 100, utils.FormatSeconds(eta)))
7234
          last_output = now
7235
  finally:
7236
    logging.info("Resume sync of instance %s disks", instance.name)
7237

    
7238
    result = lu.rpc.call_blockdev_pause_resume_sync(node, instance.disks, False)
7239

    
7240
    for idx, success in enumerate(result.payload):
7241
      if not success:
7242
        lu.LogWarning("Resume sync of disk %d failed, please have a"
7243
                      " look at the status and troubleshoot the issue", idx)
7244
        logging.warn("resume-sync of instance %s for disks %d failed",
7245
                     instance.name, idx)
7246

    
7247

    
7248
def _CreateDisks(lu, instance, to_skip=None, target_node=None):
7249
  """Create all disks for an instance.
7250

7251
  This abstracts away some work from AddInstance.
7252

7253
  @type lu: L{LogicalUnit}
7254
  @param lu: the logical unit on whose behalf we execute
7255
  @type instance: L{objects.Instance}
7256
  @param instance: the instance whose disks we should create
7257
  @type to_skip: list
7258
  @param to_skip: list of indices to skip
7259
  @type target_node: string
7260
  @param target_node: if passed, overrides the target node for creation
7261
  @rtype: boolean
7262
  @return: the success of the creation
7263

7264
  """
7265
  info = _GetInstanceInfoText(instance)
7266
  if target_node is None:
7267
    pnode = instance.primary_node
7268
    all_nodes = instance.all_nodes
7269
  else:
7270
    pnode = target_node
7271
    all_nodes = [pnode]
7272

    
7273
  if instance.disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE):
7274
    file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
7275
    result = lu.rpc.call_file_storage_dir_create(pnode, file_storage_dir)
7276

    
7277
    result.Raise("Failed to create directory '%s' on"
7278
                 " node %s" % (file_storage_dir, pnode))
7279

    
7280
  # Note: this needs to be kept in sync with adding of disks in
7281
  # LUInstanceSetParams
7282
  for idx, device in enumerate(instance.disks):
7283
    if to_skip and idx in to_skip:
7284
      continue
7285
    logging.info("Creating volume %s for instance %s",
7286
                 device.iv_name, instance.name)
7287
    #HARDCODE
7288
    for node in all_nodes:
7289
      f_create = node == pnode
7290
      _CreateBlockDev(lu, node, instance, device, f_create, info, f_create)
7291

    
7292

    
7293
def _RemoveDisks(lu, instance, target_node=None):
7294
  """Remove all disks for an instance.
7295

7296
  This abstracts away some work from `AddInstance()` and
7297
  `RemoveInstance()`. Note that in case some of the devices couldn't
7298
  be removed, the removal will continue with the other ones (compare
7299
  with `_CreateDisks()`).
7300

7301
  @type lu: L{LogicalUnit}
7302
  @param lu: the logical unit on whose behalf we execute
7303
  @type instance: L{objects.Instance}
7304
  @param instance: the instance whose disks we should remove
7305
  @type target_node: string
7306
  @param target_node: used to override the node on which to remove the disks
7307
  @rtype: boolean
7308
  @return: the success of the removal
7309

7310
  """
7311
  logging.info("Removing block devices for instance %s", instance.name)
7312

    
7313
  all_result = True
7314
  for device in instance.disks:
7315
    if target_node:
7316
      edata = [(target_node, device)]
7317
    else:
7318
      edata = device.ComputeNodeTree(instance.primary_node)
7319
    for node, disk in edata:
7320
      lu.cfg.SetDiskID(disk, node)
7321
      msg = lu.rpc.call_blockdev_remove(node, disk).fail_msg
7322
      if msg:
7323
        lu.LogWarning("Could not remove block device %s on node %s,"
7324
                      " continuing anyway: %s", device.iv_name, node, msg)
7325
        all_result = False
7326

    
7327
  if instance.disk_template == constants.DT_FILE:
7328
    file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
7329
    if target_node:
7330
      tgt = target_node
7331
    else:
7332
      tgt = instance.primary_node
7333
    result = lu.rpc.call_file_storage_dir_remove(tgt, file_storage_dir)
7334
    if result.fail_msg:
7335
      lu.LogWarning("Could not remove directory '%s' on node %s: %s",
7336
                    file_storage_dir, instance.primary_node, result.fail_msg)
7337
      all_result = False
7338

    
7339
  return all_result
7340

    
7341

    
7342
def _ComputeDiskSizePerVG(disk_template, disks):
7343
  """Compute disk size requirements in the volume group
7344

7345
  """
7346
  def _compute(disks, payload):
7347
    """Universal algorithm.
7348

7349
    """
7350
    vgs = {}
7351
    for disk in disks:
7352
      vgs[disk[constants.IDISK_VG]] = \
7353
        vgs.get(constants.IDISK_VG, 0) + disk[constants.IDISK_SIZE] + payload
7354

    
7355
    return vgs
7356

    
7357
  # Required free disk space as a function of disk and swap space
7358
  req_size_dict = {
7359
    constants.DT_DISKLESS: {},
7360
    constants.DT_PLAIN: _compute(disks, 0),
7361
    # 128 MB are added for drbd metadata for each disk
7362
    constants.DT_DRBD8: _compute(disks, 128),
7363
    constants.DT_FILE: {},
7364
    constants.DT_SHARED_FILE: {},
7365
  }
7366

    
7367
  if disk_template not in req_size_dict:
7368
    raise errors.ProgrammerError("Disk template '%s' size requirement"
7369
                                 " is unknown" %  disk_template)
7370

    
7371
  return req_size_dict[disk_template]
7372

    
7373

    
7374
def _ComputeDiskSize(disk_template, disks):
7375
  """Compute disk size requirements in the volume group
7376

7377
  """
7378
  # Required free disk space as a function of disk and swap space
7379
  req_size_dict = {
7380
    constants.DT_DISKLESS: None,
7381
    constants.DT_PLAIN: sum(d[constants.IDISK_SIZE] for d in disks),
7382
    # 128 MB are added for drbd metadata for each disk
7383
    constants.DT_DRBD8: sum(d[constants.IDISK_SIZE] + 128 for d in disks),
7384
    constants.DT_FILE: None,
7385
    constants.DT_SHARED_FILE: 0,
7386
    constants.DT_BLOCK: 0,
7387
  }
7388

    
7389
  if disk_template not in req_size_dict:
7390
    raise errors.ProgrammerError("Disk template '%s' size requirement"
7391
                                 " is unknown" %  disk_template)
7392

    
7393
  return req_size_dict[disk_template]
7394

    
7395

    
7396
def _FilterVmNodes(lu, nodenames):
7397
  """Filters out non-vm_capable nodes from a list.
7398

7399
  @type lu: L{LogicalUnit}
7400
  @param lu: the logical unit for which we check
7401
  @type nodenames: list
7402
  @param nodenames: the list of nodes on which we should check
7403
  @rtype: list
7404
  @return: the list of vm-capable nodes
7405

7406
  """
7407
  vm_nodes = frozenset(lu.cfg.GetNonVmCapableNodeList())
7408
  return [name for name in nodenames if name not in vm_nodes]
7409

    
7410

    
7411
def _CheckHVParams(lu, nodenames, hvname, hvparams):
7412
  """Hypervisor parameter validation.
7413

7414
  This function abstract the hypervisor parameter validation to be
7415
  used in both instance create and instance modify.
7416

7417
  @type lu: L{LogicalUnit}
7418
  @param lu: the logical unit for which we check
7419
  @type nodenames: list
7420
  @param nodenames: the list of nodes on which we should check
7421
  @type hvname: string
7422
  @param hvname: the name of the hypervisor we should use
7423
  @type hvparams: dict
7424
  @param hvparams: the parameters which we need to check
7425
  @raise errors.OpPrereqError: if the parameters are not valid
7426

7427
  """
7428
  nodenames = _FilterVmNodes(lu, nodenames)
7429
  hvinfo = lu.rpc.call_hypervisor_validate_params(nodenames,
7430
                                                  hvname,
7431
                                                  hvparams)
7432
  for node in nodenames:
7433
    info = hvinfo[node]
7434
    if info.offline:
7435
      continue
7436
    info.Raise("Hypervisor parameter validation failed on node %s" % node)
7437

    
7438

    
7439
def _CheckOSParams(lu, required, nodenames, osname, osparams):
7440
  """OS parameters validation.
7441

7442
  @type lu: L{LogicalUnit}
7443
  @param lu: the logical unit for which we check
7444
  @type required: boolean
7445
  @param required: whether the validation should fail if the OS is not
7446
      found
7447
  @type nodenames: list
7448
  @param nodenames: the list of nodes on which we should check
7449
  @type osname: string
7450
  @param osname: the name of the hypervisor we should use
7451
  @type osparams: dict
7452
  @param osparams: the parameters which we need to check
7453
  @raise errors.OpPrereqError: if the parameters are not valid
7454

7455
  """
7456
  nodenames = _FilterVmNodes(lu, nodenames)
7457
  result = lu.rpc.call_os_validate(required, nodenames, osname,
7458
                                   [constants.OS_VALIDATE_PARAMETERS],
7459
                                   osparams)
7460
  for node, nres in result.items():
7461
    # we don't check for offline cases since this should be run only
7462
    # against the master node and/or an instance's nodes
7463
    nres.Raise("OS Parameters validation failed on node %s" % node)
7464
    if not nres.payload:
7465
      lu.LogInfo("OS %s not found on node %s, validation skipped",
7466
                 osname, node)
7467

    
7468

    
7469
class LUInstanceCreate(LogicalUnit):
7470
  """Create an instance.
7471

7472
  """
7473
  HPATH = "instance-add"
7474
  HTYPE = constants.HTYPE_INSTANCE
7475
  REQ_BGL = False
7476

    
7477
  def CheckArguments(self):
7478
    """Check arguments.
7479

7480
    """
7481
    # do not require name_check to ease forward/backward compatibility
7482
    # for tools
7483
    if self.op.no_install and self.op.start:
7484
      self.LogInfo("No-installation mode selected, disabling startup")
7485
      self.op.start = False
7486
    # validate/normalize the instance name
7487
    self.op.instance_name = \
7488
      netutils.Hostname.GetNormalizedName(self.op.instance_name)
7489

    
7490
    if self.op.ip_check and not self.op.name_check:
7491
      # TODO: make the ip check more flexible and not depend on the name check
7492
      raise errors.OpPrereqError("Cannot do IP address check without a name"
7493
                                 " check", errors.ECODE_INVAL)
7494

    
7495
    # check nics' parameter names
7496
    for nic in self.op.nics:
7497
      utils.ForceDictType(nic, constants.INIC_PARAMS_TYPES)
7498

    
7499
    # check disks. parameter names and consistent adopt/no-adopt strategy
7500
    has_adopt = has_no_adopt = False
7501
    for disk in self.op.disks:
7502
      utils.ForceDictType(disk, constants.IDISK_PARAMS_TYPES)
7503
      if constants.IDISK_ADOPT in disk:
7504
        has_adopt = True
7505
      else:
7506
        has_no_adopt = True
7507
    if has_adopt and has_no_adopt:
7508
      raise errors.OpPrereqError("Either all disks are adopted or none is",
7509
                                 errors.ECODE_INVAL)
7510
    if has_adopt:
7511
      if self.op.disk_template not in constants.DTS_MAY_ADOPT:
7512
        raise errors.OpPrereqError("Disk adoption is not supported for the"
7513
                                   " '%s' disk template" %
7514
                                   self.op.disk_template,
7515
                                   errors.ECODE_INVAL)
7516
      if self.op.iallocator is not None:
7517
        raise errors.OpPrereqError("Disk adoption not allowed with an"
7518
                                   " iallocator script", errors.ECODE_INVAL)
7519
      if self.op.mode == constants.INSTANCE_IMPORT:
7520
        raise errors.OpPrereqError("Disk adoption not allowed for"
7521
                                   " instance import", errors.ECODE_INVAL)
7522
    else:
7523
      if self.op.disk_template in constants.DTS_MUST_ADOPT:
7524
        raise errors.OpPrereqError("Disk template %s requires disk adoption,"
7525
                                   " but no 'adopt' parameter given" %
7526
                                   self.op.disk_template,
7527
                                   errors.ECODE_INVAL)
7528

    
7529
    self.adopt_disks = has_adopt
7530

    
7531
    # instance name verification
7532
    if self.op.name_check:
7533
      self.hostname1 = netutils.GetHostname(name=self.op.instance_name)
7534
      self.op.instance_name = self.hostname1.name
7535
      # used in CheckPrereq for ip ping check
7536
      self.check_ip = self.hostname1.ip
7537
    else:
7538
      self.check_ip = None
7539

    
7540
    # file storage checks
7541
    if (self.op.file_driver and
7542
        not self.op.file_driver in constants.FILE_DRIVER):
7543
      raise errors.OpPrereqError("Invalid file driver name '%s'" %
7544
                                 self.op.file_driver, errors.ECODE_INVAL)
7545

    
7546
    if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir):
7547
      raise errors.OpPrereqError("File storage directory path not absolute",
7548
                                 errors.ECODE_INVAL)
7549

    
7550
    ### Node/iallocator related checks
7551
    _CheckIAllocatorOrNode(self, "iallocator", "pnode")
7552

    
7553
    if self.op.pnode is not None:
7554
      if self.op.disk_template in constants.DTS_INT_MIRROR:
7555
        if self.op.snode is None:
7556
          raise errors.OpPrereqError("The networked disk templates need"
7557
                                     " a mirror node", errors.ECODE_INVAL)
7558
      elif self.op.snode:
7559
        self.LogWarning("Secondary node will be ignored on non-mirrored disk"
7560
                        " template")
7561
        self.op.snode = None
7562

    
7563
    self._cds = _GetClusterDomainSecret()
7564

    
7565
    if self.op.mode == constants.INSTANCE_IMPORT:
7566
      # On import force_variant must be True, because if we forced it at
7567
      # initial install, our only chance when importing it back is that it
7568
      # works again!
7569
      self.op.force_variant = True
7570

    
7571
      if self.op.no_install:
7572
        self.LogInfo("No-installation mode has no effect during import")
7573

    
7574
    elif self.op.mode == constants.INSTANCE_CREATE:
7575
      if self.op.os_type is None:
7576
        raise errors.OpPrereqError("No guest OS specified",
7577
                                   errors.ECODE_INVAL)
7578
      if self.op.os_type in self.cfg.GetClusterInfo().blacklisted_os:
7579
        raise errors.OpPrereqError("Guest OS '%s' is not allowed for"
7580
                                   " installation" % self.op.os_type,
7581
                                   errors.ECODE_STATE)
7582
      if self.op.disk_template is None:
7583
        raise errors.OpPrereqError("No disk template specified",
7584
                                   errors.ECODE_INVAL)
7585

    
7586
    elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
7587
      # Check handshake to ensure both clusters have the same domain secret
7588
      src_handshake = self.op.source_handshake
7589
      if not src_handshake:
7590
        raise errors.OpPrereqError("Missing source handshake",
7591
                                   errors.ECODE_INVAL)
7592

    
7593
      errmsg = masterd.instance.CheckRemoteExportHandshake(self._cds,
7594
                                                           src_handshake)
7595
      if errmsg:
7596
        raise errors.OpPrereqError("Invalid handshake: %s" % errmsg,
7597
                                   errors.ECODE_INVAL)
7598

    
7599
      # Load and check source CA
7600
      self.source_x509_ca_pem = self.op.source_x509_ca
7601
      if not self.source_x509_ca_pem:
7602
        raise errors.OpPrereqError("Missing source X509 CA",
7603
                                   errors.ECODE_INVAL)
7604

    
7605
      try:
7606
        (cert, _) = utils.LoadSignedX509Certificate(self.source_x509_ca_pem,
7607
                                                    self._cds)
7608
      except OpenSSL.crypto.Error, err:
7609
        raise errors.OpPrereqError("Unable to load source X509 CA (%s)" %
7610
                                   (err, ), errors.ECODE_INVAL)
7611

    
7612
      (errcode, msg) = utils.VerifyX509Certificate(cert, None, None)
7613
      if errcode is not None:
7614
        raise errors.OpPrereqError("Invalid source X509 CA (%s)" % (msg, ),
7615
                                   errors.ECODE_INVAL)
7616

    
7617
      self.source_x509_ca = cert
7618

    
7619
      src_instance_name = self.op.source_instance_name
7620
      if not src_instance_name:
7621
        raise errors.OpPrereqError("Missing source instance name",
7622
                                   errors.ECODE_INVAL)
7623

    
7624
      self.source_instance_name = \
7625
          netutils.GetHostname(name=src_instance_name).name
7626

    
7627
    else:
7628
      raise errors.OpPrereqError("Invalid instance creation mode %r" %
7629
                                 self.op.mode, errors.ECODE_INVAL)
7630

    
7631
  def ExpandNames(self):
7632
    """ExpandNames for CreateInstance.
7633

7634
    Figure out the right locks for instance creation.
7635

7636
    """
7637
    self.needed_locks = {}
7638

    
7639
    instance_name = self.op.instance_name
7640
    # this is just a preventive check, but someone might still add this
7641
    # instance in the meantime, and creation will fail at lock-add time
7642
    if instance_name in self.cfg.GetInstanceList():
7643
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
7644
                                 instance_name, errors.ECODE_EXISTS)
7645

    
7646
    self.add_locks[locking.LEVEL_INSTANCE] = instance_name
7647

    
7648
    if self.op.iallocator:
7649
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
7650
    else:
7651
      self.op.pnode = _ExpandNodeName(self.cfg, self.op.pnode)
7652
      nodelist = [self.op.pnode]
7653
      if self.op.snode is not None:
7654
        self.op.snode = _ExpandNodeName(self.cfg, self.op.snode)
7655
        nodelist.append(self.op.snode)
7656
      self.needed_locks[locking.LEVEL_NODE] = nodelist
7657

    
7658
    # in case of import lock the source node too
7659
    if self.op.mode == constants.INSTANCE_IMPORT:
7660
      src_node = self.op.src_node
7661
      src_path = self.op.src_path
7662

    
7663
      if src_path is None:
7664
        self.op.src_path = src_path = self.op.instance_name
7665

    
7666
      if src_node is None:
7667
        self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
7668
        self.op.src_node = None
7669
        if os.path.isabs(src_path):
7670
          raise errors.OpPrereqError("Importing an instance from an absolute"
7671
                                     " path requires a source node option",
7672
                                     errors.ECODE_INVAL)
7673
      else:
7674
        self.op.src_node = src_node = _ExpandNodeName(self.cfg, src_node)
7675
        if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
7676
          self.needed_locks[locking.LEVEL_NODE].append(src_node)
7677
        if not os.path.isabs(src_path):
7678
          self.op.src_path = src_path = \
7679
            utils.PathJoin(constants.EXPORT_DIR, src_path)
7680

    
7681
  def _RunAllocator(self):
7682
    """Run the allocator based on input opcode.
7683

7684
    """
7685
    nics = [n.ToDict() for n in self.nics]
7686
    ial = IAllocator(self.cfg, self.rpc,
7687
                     mode=constants.IALLOCATOR_MODE_ALLOC,
7688
                     name=self.op.instance_name,
7689
                     disk_template=self.op.disk_template,
7690
                     tags=[],
7691
                     os=self.op.os_type,
7692
                     vcpus=self.be_full[constants.BE_VCPUS],
7693
                     mem_size=self.be_full[constants.BE_MEMORY],
7694
                     disks=self.disks,
7695
                     nics=nics,
7696
                     hypervisor=self.op.hypervisor,
7697
                     )
7698

    
7699
    ial.Run(self.op.iallocator)
7700

    
7701
    if not ial.success:
7702
      raise errors.OpPrereqError("Can't compute nodes using"
7703
                                 " iallocator '%s': %s" %
7704
                                 (self.op.iallocator, ial.info),
7705
                                 errors.ECODE_NORES)
7706
    if len(ial.result) != ial.required_nodes:
7707
      raise errors.OpPrereqError("iallocator '%s' returned invalid number"
7708
                                 " of nodes (%s), required %s" %
7709
                                 (self.op.iallocator, len(ial.result),
7710
                                  ial.required_nodes), errors.ECODE_FAULT)
7711
    self.op.pnode = ial.result[0]
7712
    self.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
7713
                 self.op.instance_name, self.op.iallocator,
7714
                 utils.CommaJoin(ial.result))
7715
    if ial.required_nodes == 2:
7716
      self.op.snode = ial.result[1]
7717

    
7718
  def BuildHooksEnv(self):
7719
    """Build hooks env.
7720

7721
    This runs on master, primary and secondary nodes of the instance.
7722

7723
    """
7724
    env = {
7725
      "ADD_MODE": self.op.mode,
7726
      }
7727
    if self.op.mode == constants.INSTANCE_IMPORT:
7728
      env["SRC_NODE"] = self.op.src_node
7729
      env["SRC_PATH"] = self.op.src_path
7730
      env["SRC_IMAGES"] = self.src_images
7731

    
7732
    env.update(_BuildInstanceHookEnv(
7733
      name=self.op.instance_name,
7734
      primary_node=self.op.pnode,
7735
      secondary_nodes=self.secondaries,
7736
      status=self.op.start,
7737
      os_type=self.op.os_type,
7738
      memory=self.be_full[constants.BE_MEMORY],
7739
      vcpus=self.be_full[constants.BE_VCPUS],
7740
      nics=_NICListToTuple(self, self.nics),
7741
      disk_template=self.op.disk_template,
7742
      disks=[(d[constants.IDISK_SIZE], d[constants.IDISK_MODE])
7743
             for d in self.disks],
7744
      bep=self.be_full,
7745
      hvp=self.hv_full,
7746
      hypervisor_name=self.op.hypervisor,
7747
    ))
7748

    
7749
    return env
7750

    
7751
  def BuildHooksNodes(self):
7752
    """Build hooks nodes.
7753

7754
    """
7755
    nl = [self.cfg.GetMasterNode(), self.op.pnode] + self.secondaries
7756
    return nl, nl
7757

    
7758
  def _ReadExportInfo(self):
7759
    """Reads the export information from disk.
7760

7761
    It will override the opcode source node and path with the actual
7762
    information, if these two were not specified before.
7763

7764
    @return: the export information
7765

7766
    """
7767
    assert self.op.mode == constants.INSTANCE_IMPORT
7768

    
7769
    src_node = self.op.src_node
7770
    src_path = self.op.src_path
7771

    
7772
    if src_node is None:
7773
      locked_nodes = self.glm.list_owned(locking.LEVEL_NODE)
7774
      exp_list = self.rpc.call_export_list(locked_nodes)
7775
      found = False
7776
      for node in exp_list:
7777
        if exp_list[node].fail_msg:
7778
          continue
7779
        if src_path in exp_list[node].payload:
7780
          found = True
7781
          self.op.src_node = src_node = node
7782
          self.op.src_path = src_path = utils.PathJoin(constants.EXPORT_DIR,
7783
                                                       src_path)
7784
          break
7785
      if not found:
7786
        raise errors.OpPrereqError("No export found for relative path %s" %
7787
                                    src_path, errors.ECODE_INVAL)
7788

    
7789
    _CheckNodeOnline(self, src_node)
7790
    result = self.rpc.call_export_info(src_node, src_path)
7791
    result.Raise("No export or invalid export found in dir %s" % src_path)
7792

    
7793
    export_info = objects.SerializableConfigParser.Loads(str(result.payload))
7794
    if not export_info.has_section(constants.INISECT_EXP):
7795
      raise errors.ProgrammerError("Corrupted export config",
7796
                                   errors.ECODE_ENVIRON)
7797

    
7798
    ei_version = export_info.get(constants.INISECT_EXP, "version")
7799
    if (int(ei_version) != constants.EXPORT_VERSION):
7800
      raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
7801
                                 (ei_version, constants.EXPORT_VERSION),
7802
                                 errors.ECODE_ENVIRON)
7803
    return export_info
7804

    
7805
  def _ReadExportParams(self, einfo):
7806
    """Use export parameters as defaults.
7807

7808
    In case the opcode doesn't specify (as in override) some instance
7809
    parameters, then try to use them from the export information, if
7810
    that declares them.
7811

7812
    """
7813
    self.op.os_type = einfo.get(constants.INISECT_EXP, "os")
7814

    
7815
    if self.op.disk_template is None:
7816
      if einfo.has_option(constants.INISECT_INS, "disk_template"):
7817
        self.op.disk_template = einfo.get(constants.INISECT_INS,
7818
                                          "disk_template")
7819
      else:
7820
        raise errors.OpPrereqError("No disk template specified and the export"
7821
                                   " is missing the disk_template information",
7822
                                   errors.ECODE_INVAL)
7823

    
7824
    if not self.op.disks:
7825
      if einfo.has_option(constants.INISECT_INS, "disk_count"):
7826
        disks = []
7827
        # TODO: import the disk iv_name too
7828
        for idx in range(einfo.getint(constants.INISECT_INS, "disk_count")):
7829
          disk_sz = einfo.getint(constants.INISECT_INS, "disk%d_size" % idx)
7830
          disks.append({constants.IDISK_SIZE: disk_sz})
7831
        self.op.disks = disks
7832
      else:
7833
        raise errors.OpPrereqError("No disk info specified and the export"
7834
                                   " is missing the disk information",
7835
                                   errors.ECODE_INVAL)
7836

    
7837
    if (not self.op.nics and
7838
        einfo.has_option(constants.INISECT_INS, "nic_count")):
7839
      nics = []
7840
      for idx in range(einfo.getint(constants.INISECT_INS, "nic_count")):
7841
        ndict = {}
7842
        for name in list(constants.NICS_PARAMETERS) + ["ip", "mac"]:
7843
          v = einfo.get(constants.INISECT_INS, "nic%d_%s" % (idx, name))
7844
          ndict[name] = v
7845
        nics.append(ndict)
7846
      self.op.nics = nics
7847

    
7848
    if (self.op.hypervisor is None and
7849
        einfo.has_option(constants.INISECT_INS, "hypervisor")):
7850
      self.op.hypervisor = einfo.get(constants.INISECT_INS, "hypervisor")
7851
    if einfo.has_section(constants.INISECT_HYP):
7852
      # use the export parameters but do not override the ones
7853
      # specified by the user
7854
      for name, value in einfo.items(constants.INISECT_HYP):
7855
        if name not in self.op.hvparams:
7856
          self.op.hvparams[name] = value
7857

    
7858
    if einfo.has_section(constants.INISECT_BEP):
7859
      # use the parameters, without overriding
7860
      for name, value in einfo.items(constants.INISECT_BEP):
7861
        if name not in self.op.beparams:
7862
          self.op.beparams[name] = value
7863
    else:
7864
      # try to read the parameters old style, from the main section
7865
      for name in constants.BES_PARAMETERS:
7866
        if (name not in self.op.beparams and
7867
            einfo.has_option(constants.INISECT_INS, name)):
7868
          self.op.beparams[name] = einfo.get(constants.INISECT_INS, name)
7869

    
7870
    if einfo.has_section(constants.INISECT_OSP):
7871
      # use the parameters, without overriding
7872
      for name, value in einfo.items(constants.INISECT_OSP):
7873
        if name not in self.op.osparams:
7874
          self.op.osparams[name] = value
7875

    
7876
  def _RevertToDefaults(self, cluster):
7877
    """Revert the instance parameters to the default values.
7878

7879
    """
7880
    # hvparams
7881
    hv_defs = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type, {})
7882
    for name in self.op.hvparams.keys():
7883
      if name in hv_defs and hv_defs[name] == self.op.hvparams[name]:
7884
        del self.op.hvparams[name]
7885
    # beparams
7886
    be_defs = cluster.SimpleFillBE({})
7887
    for name in self.op.beparams.keys():
7888
      if name in be_defs and be_defs[name] == self.op.beparams[name]:
7889
        del self.op.beparams[name]
7890
    # nic params
7891
    nic_defs = cluster.SimpleFillNIC({})
7892
    for nic in self.op.nics:
7893
      for name in constants.NICS_PARAMETERS:
7894
        if name in nic and name in nic_defs and nic[name] == nic_defs[name]:
7895
          del nic[name]
7896
    # osparams
7897
    os_defs = cluster.SimpleFillOS(self.op.os_type, {})
7898
    for name in self.op.osparams.keys():
7899
      if name in os_defs and os_defs[name] == self.op.osparams[name]:
7900
        del self.op.osparams[name]
7901

    
7902
  def CheckPrereq(self):
7903
    """Check prerequisites.
7904

7905
    """
7906
    if self.op.mode == constants.INSTANCE_IMPORT:
7907
      export_info = self._ReadExportInfo()
7908
      self._ReadExportParams(export_info)
7909

    
7910
    if (not self.cfg.GetVGName() and
7911
        self.op.disk_template not in constants.DTS_NOT_LVM):
7912
      raise errors.OpPrereqError("Cluster does not support lvm-based"
7913
                                 " instances", errors.ECODE_STATE)
7914

    
7915
    if self.op.hypervisor is None:
7916
      self.op.hypervisor = self.cfg.GetHypervisorType()
7917

    
7918
    cluster = self.cfg.GetClusterInfo()
7919
    enabled_hvs = cluster.enabled_hypervisors
7920
    if self.op.hypervisor not in enabled_hvs:
7921
      raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
7922
                                 " cluster (%s)" % (self.op.hypervisor,
7923
                                  ",".join(enabled_hvs)),
7924
                                 errors.ECODE_STATE)
7925

    
7926
    # check hypervisor parameter syntax (locally)
7927
    utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
7928
    filled_hvp = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type,
7929
                                      self.op.hvparams)
7930
    hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
7931
    hv_type.CheckParameterSyntax(filled_hvp)
7932
    self.hv_full = filled_hvp
7933
    # check that we don't specify global parameters on an instance
7934
    _CheckGlobalHvParams(self.op.hvparams)
7935

    
7936
    # fill and remember the beparams dict
7937
    utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
7938
    self.be_full = cluster.SimpleFillBE(self.op.beparams)
7939

    
7940
    # build os parameters
7941
    self.os_full = cluster.SimpleFillOS(self.op.os_type, self.op.osparams)
7942

    
7943
    # now that hvp/bep are in final format, let's reset to defaults,
7944
    # if told to do so
7945
    if self.op.identify_defaults:
7946
      self._RevertToDefaults(cluster)
7947

    
7948
    # NIC buildup
7949
    self.nics = []
7950
    for idx, nic in enumerate(self.op.nics):
7951
      nic_mode_req = nic.get(constants.INIC_MODE, None)
7952
      nic_mode = nic_mode_req
7953
      if nic_mode is None:
7954
        nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE]
7955

    
7956
      # in routed mode, for the first nic, the default ip is 'auto'
7957
      if nic_mode == constants.NIC_MODE_ROUTED and idx == 0:
7958
        default_ip_mode = constants.VALUE_AUTO
7959
      else:
7960
        default_ip_mode = constants.VALUE_NONE
7961

    
7962
      # ip validity checks
7963
      ip = nic.get(constants.INIC_IP, default_ip_mode)
7964
      if ip is None or ip.lower() == constants.VALUE_NONE:
7965
        nic_ip = None
7966
      elif ip.lower() == constants.VALUE_AUTO:
7967
        if not self.op.name_check:
7968
          raise errors.OpPrereqError("IP address set to auto but name checks"
7969
                                     " have been skipped",
7970
                                     errors.ECODE_INVAL)
7971
        nic_ip = self.hostname1.ip
7972
      else:
7973
        if not netutils.IPAddress.IsValid(ip):
7974
          raise errors.OpPrereqError("Invalid IP address '%s'" % ip,
7975
                                     errors.ECODE_INVAL)
7976
        nic_ip = ip
7977

    
7978
      # TODO: check the ip address for uniqueness
7979
      if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip:
7980
        raise errors.OpPrereqError("Routed nic mode requires an ip address",
7981
                                   errors.ECODE_INVAL)
7982

    
7983
      # MAC address verification
7984
      mac = nic.get(constants.INIC_MAC, constants.VALUE_AUTO)
7985
      if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
7986
        mac = utils.NormalizeAndValidateMac(mac)
7987

    
7988
        try:
7989
          self.cfg.ReserveMAC(mac, self.proc.GetECId())
7990
        except errors.ReservationError:
7991
          raise errors.OpPrereqError("MAC address %s already in use"
7992
                                     " in cluster" % mac,
7993
                                     errors.ECODE_NOTUNIQUE)
7994

    
7995
      #  Build nic parameters
7996
      link = nic.get(constants.INIC_LINK, None)
7997
      nicparams = {}
7998
      if nic_mode_req:
7999
        nicparams[constants.NIC_MODE] = nic_mode_req
8000
      if link:
8001
        nicparams[constants.NIC_LINK] = link
8002

    
8003
      check_params = cluster.SimpleFillNIC(nicparams)
8004
      objects.NIC.CheckParameterSyntax(check_params)
8005
      self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
8006

    
8007
    # disk checks/pre-build
8008
    default_vg = self.cfg.GetVGName()
8009
    self.disks = []
8010
    for disk in self.op.disks:
8011
      mode = disk.get(constants.IDISK_MODE, constants.DISK_RDWR)
8012
      if mode not in constants.DISK_ACCESS_SET:
8013
        raise errors.OpPrereqError("Invalid disk access mode '%s'" %
8014
                                   mode, errors.ECODE_INVAL)
8015
      size = disk.get(constants.IDISK_SIZE, None)
8016
      if size is None:
8017
        raise errors.OpPrereqError("Missing disk size", errors.ECODE_INVAL)
8018
      try:
8019
        size = int(size)
8020
      except (TypeError, ValueError):
8021
        raise errors.OpPrereqError("Invalid disk size '%s'" % size,
8022
                                   errors.ECODE_INVAL)
8023

    
8024
      data_vg = disk.get(constants.IDISK_VG, default_vg)
8025
      new_disk = {
8026
        constants.IDISK_SIZE: size,
8027
        constants.IDISK_MODE: mode,
8028
        constants.IDISK_VG: data_vg,
8029
        constants.IDISK_METAVG: disk.get(constants.IDISK_METAVG, data_vg),
8030
        }
8031
      if constants.IDISK_ADOPT in disk:
8032
        new_disk[constants.IDISK_ADOPT] = disk[constants.IDISK_ADOPT]
8033
      self.disks.append(new_disk)
8034

    
8035
    if self.op.mode == constants.INSTANCE_IMPORT:
8036

    
8037
      # Check that the new instance doesn't have less disks than the export
8038
      instance_disks = len(self.disks)
8039
      export_disks = export_info.getint(constants.INISECT_INS, 'disk_count')
8040
      if instance_disks < export_disks:
8041
        raise errors.OpPrereqError("Not enough disks to import."
8042
                                   " (instance: %d, export: %d)" %
8043
                                   (instance_disks, export_disks),
8044
                                   errors.ECODE_INVAL)
8045

    
8046
      disk_images = []
8047
      for idx in range(export_disks):
8048
        option = 'disk%d_dump' % idx
8049
        if export_info.has_option(constants.INISECT_INS, option):
8050
          # FIXME: are the old os-es, disk sizes, etc. useful?
8051
          export_name = export_info.get(constants.INISECT_INS, option)
8052
          image = utils.PathJoin(self.op.src_path, export_name)
8053
          disk_images.append(image)
8054
        else:
8055
          disk_images.append(False)
8056

    
8057
      self.src_images = disk_images
8058

    
8059
      old_name = export_info.get(constants.INISECT_INS, 'name')
8060
      try:
8061
        exp_nic_count = export_info.getint(constants.INISECT_INS, 'nic_count')
8062
      except (TypeError, ValueError), err:
8063
        raise errors.OpPrereqError("Invalid export file, nic_count is not"
8064
                                   " an integer: %s" % str(err),
8065
                                   errors.ECODE_STATE)
8066
      if self.op.instance_name == old_name:
8067
        for idx, nic in enumerate(self.nics):
8068
          if nic.mac == constants.VALUE_AUTO and exp_nic_count >= idx:
8069
            nic_mac_ini = 'nic%d_mac' % idx
8070
            nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini)
8071

    
8072
    # ENDIF: self.op.mode == constants.INSTANCE_IMPORT
8073

    
8074
    # ip ping checks (we use the same ip that was resolved in ExpandNames)
8075
    if self.op.ip_check:
8076
      if netutils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT):
8077
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
8078
                                   (self.check_ip, self.op.instance_name),
8079
                                   errors.ECODE_NOTUNIQUE)
8080

    
8081
    #### mac address generation
8082
    # By generating here the mac address both the allocator and the hooks get
8083
    # the real final mac address rather than the 'auto' or 'generate' value.
8084
    # There is a race condition between the generation and the instance object
8085
    # creation, which means that we know the mac is valid now, but we're not
8086
    # sure it will be when we actually add the instance. If things go bad
8087
    # adding the instance will abort because of a duplicate mac, and the
8088
    # creation job will fail.
8089
    for nic in self.nics:
8090
      if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
8091
        nic.mac = self.cfg.GenerateMAC(self.proc.GetECId())
8092

    
8093
    #### allocator run
8094

    
8095
    if self.op.iallocator is not None:
8096
      self._RunAllocator()
8097

    
8098
    #### node related checks
8099

    
8100
    # check primary node
8101
    self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode)
8102
    assert self.pnode is not None, \
8103
      "Cannot retrieve locked node %s" % self.op.pnode
8104
    if pnode.offline:
8105
      raise errors.OpPrereqError("Cannot use offline primary node '%s'" %
8106
                                 pnode.name, errors.ECODE_STATE)
8107
    if pnode.drained:
8108
      raise errors.OpPrereqError("Cannot use drained primary node '%s'" %
8109
                                 pnode.name, errors.ECODE_STATE)
8110
    if not pnode.vm_capable:
8111
      raise errors.OpPrereqError("Cannot use non-vm_capable primary node"
8112
                                 " '%s'" % pnode.name, errors.ECODE_STATE)
8113

    
8114
    self.secondaries = []
8115

    
8116
    # mirror node verification
8117
    if self.op.disk_template in constants.DTS_INT_MIRROR:
8118
      if self.op.snode == pnode.name:
8119
        raise errors.OpPrereqError("The secondary node cannot be the"
8120
                                   " primary node", errors.ECODE_INVAL)
8121
      _CheckNodeOnline(self, self.op.snode)
8122
      _CheckNodeNotDrained(self, self.op.snode)
8123
      _CheckNodeVmCapable(self, self.op.snode)
8124
      self.secondaries.append(self.op.snode)
8125

    
8126
    nodenames = [pnode.name] + self.secondaries
8127

    
8128
    if not self.adopt_disks:
8129
      # Check lv size requirements, if not adopting
8130
      req_sizes = _ComputeDiskSizePerVG(self.op.disk_template, self.disks)
8131
      _CheckNodesFreeDiskPerVG(self, nodenames, req_sizes)
8132

    
8133
    elif self.op.disk_template == constants.DT_PLAIN: # Check the adoption data
8134
      all_lvs = set(["%s/%s" % (disk[constants.IDISK_VG],
8135
                                disk[constants.IDISK_ADOPT])
8136
                     for disk in self.disks])
8137
      if len(all_lvs) != len(self.disks):
8138
        raise errors.OpPrereqError("Duplicate volume names given for adoption",
8139
                                   errors.ECODE_INVAL)
8140
      for lv_name in all_lvs:
8141
        try:
8142
          # FIXME: lv_name here is "vg/lv" need to ensure that other calls
8143
          # to ReserveLV uses the same syntax
8144
          self.cfg.ReserveLV(lv_name, self.proc.GetECId())
8145
        except errors.ReservationError:
8146
          raise errors.OpPrereqError("LV named %s used by another instance" %
8147
                                     lv_name, errors.ECODE_NOTUNIQUE)
8148

    
8149
      vg_names = self.rpc.call_vg_list([pnode.name])[pnode.name]
8150
      vg_names.Raise("Cannot get VG information from node %s" % pnode.name)
8151

    
8152
      node_lvs = self.rpc.call_lv_list([pnode.name],
8153
                                       vg_names.payload.keys())[pnode.name]
8154
      node_lvs.Raise("Cannot get LV information from node %s" % pnode.name)
8155
      node_lvs = node_lvs.payload
8156

    
8157
      delta = all_lvs.difference(node_lvs.keys())
8158
      if delta:
8159
        raise errors.OpPrereqError("Missing logical volume(s): %s" %
8160
                                   utils.CommaJoin(delta),
8161
                                   errors.ECODE_INVAL)
8162
      online_lvs = [lv for lv in all_lvs if node_lvs[lv][2]]
8163
      if online_lvs:
8164
        raise errors.OpPrereqError("Online logical volumes found, cannot"
8165
                                   " adopt: %s" % utils.CommaJoin(online_lvs),
8166
                                   errors.ECODE_STATE)
8167
      # update the size of disk based on what is found
8168
      for dsk in self.disks:
8169
        dsk[constants.IDISK_SIZE] = \
8170
          int(float(node_lvs["%s/%s" % (dsk[constants.IDISK_VG],
8171
                                        dsk[constants.IDISK_ADOPT])][0]))
8172

    
8173
    elif self.op.disk_template == constants.DT_BLOCK:
8174
      # Normalize and de-duplicate device paths
8175
      all_disks = set([os.path.abspath(disk[constants.IDISK_ADOPT])
8176
                       for disk in self.disks])
8177
      if len(all_disks) != len(self.disks):
8178
        raise errors.OpPrereqError("Duplicate disk names given for adoption",
8179
                                   errors.ECODE_INVAL)
8180
      baddisks = [d for d in all_disks
8181
                  if not d.startswith(constants.ADOPTABLE_BLOCKDEV_ROOT)]
8182
      if baddisks:
8183
        raise errors.OpPrereqError("Device node(s) %s lie outside %s and"
8184
                                   " cannot be adopted" %
8185
                                   (", ".join(baddisks),
8186
                                    constants.ADOPTABLE_BLOCKDEV_ROOT),
8187
                                   errors.ECODE_INVAL)
8188

    
8189
      node_disks = self.rpc.call_bdev_sizes([pnode.name],
8190
                                            list(all_disks))[pnode.name]
8191
      node_disks.Raise("Cannot get block device information from node %s" %
8192
                       pnode.name)
8193
      node_disks = node_disks.payload
8194
      delta = all_disks.difference(node_disks.keys())
8195
      if delta:
8196
        raise errors.OpPrereqError("Missing block device(s): %s" %
8197
                                   utils.CommaJoin(delta),
8198
                                   errors.ECODE_INVAL)
8199
      for dsk in self.disks:
8200
        dsk[constants.IDISK_SIZE] = \
8201
          int(float(node_disks[dsk[constants.IDISK_ADOPT]]))
8202

    
8203
    _CheckHVParams(self, nodenames, self.op.hypervisor, self.op.hvparams)
8204

    
8205
    _CheckNodeHasOS(self, pnode.name, self.op.os_type, self.op.force_variant)
8206
    # check OS parameters (remotely)
8207
    _CheckOSParams(self, True, nodenames, self.op.os_type, self.os_full)
8208

    
8209
    _CheckNicsBridgesExist(self, self.nics, self.pnode.name)
8210

    
8211
    # memory check on primary node
8212
    if self.op.start:
8213
      _CheckNodeFreeMemory(self, self.pnode.name,
8214
                           "creating instance %s" % self.op.instance_name,
8215
                           self.be_full[constants.BE_MEMORY],
8216
                           self.op.hypervisor)
8217

    
8218
    self.dry_run_result = list(nodenames)
8219

    
8220
  def Exec(self, feedback_fn):
8221
    """Create and add the instance to the cluster.
8222

8223
    """
8224
    instance = self.op.instance_name
8225
    pnode_name = self.pnode.name
8226

    
8227
    ht_kind = self.op.hypervisor
8228
    if ht_kind in constants.HTS_REQ_PORT:
8229
      network_port = self.cfg.AllocatePort()
8230
    else:
8231
      network_port = None
8232

    
8233
    if constants.ENABLE_FILE_STORAGE or constants.ENABLE_SHARED_FILE_STORAGE:
8234
      # this is needed because os.path.join does not accept None arguments
8235
      if self.op.file_storage_dir is None:
8236
        string_file_storage_dir = ""
8237
      else:
8238
        string_file_storage_dir = self.op.file_storage_dir
8239

    
8240
      # build the full file storage dir path
8241
      if self.op.disk_template == constants.DT_SHARED_FILE:
8242
        get_fsd_fn = self.cfg.GetSharedFileStorageDir
8243
      else:
8244
        get_fsd_fn = self.cfg.GetFileStorageDir
8245

    
8246
      file_storage_dir = utils.PathJoin(get_fsd_fn(),
8247
                                        string_file_storage_dir, instance)
8248
    else:
8249
      file_storage_dir = ""
8250

    
8251
    disks = _GenerateDiskTemplate(self,
8252
                                  self.op.disk_template,
8253
                                  instance, pnode_name,
8254
                                  self.secondaries,
8255
                                  self.disks,
8256
                                  file_storage_dir,
8257
                                  self.op.file_driver,
8258
                                  0,
8259
                                  feedback_fn)
8260

    
8261
    iobj = objects.Instance(name=instance, os=self.op.os_type,
8262
                            primary_node=pnode_name,
8263
                            nics=self.nics, disks=disks,
8264
                            disk_template=self.op.disk_template,
8265
                            admin_up=False,
8266
                            network_port=network_port,
8267
                            beparams=self.op.beparams,
8268
                            hvparams=self.op.hvparams,
8269
                            hypervisor=self.op.hypervisor,
8270
                            osparams=self.op.osparams,
8271
                            )
8272

    
8273
    if self.adopt_disks:
8274
      if self.op.disk_template == constants.DT_PLAIN:
8275
        # rename LVs to the newly-generated names; we need to construct
8276
        # 'fake' LV disks with the old data, plus the new unique_id
8277
        tmp_disks = [objects.Disk.FromDict(v.ToDict()) for v in disks]
8278
        rename_to = []
8279
        for t_dsk, a_dsk in zip (tmp_disks, self.disks):
8280
          rename_to.append(t_dsk.logical_id)
8281
          t_dsk.logical_id = (t_dsk.logical_id[0], a_dsk[constants.IDISK_ADOPT])
8282
          self.cfg.SetDiskID(t_dsk, pnode_name)
8283
        result = self.rpc.call_blockdev_rename(pnode_name,
8284
                                               zip(tmp_disks, rename_to))
8285
        result.Raise("Failed to rename adoped LVs")
8286
    else:
8287
      feedback_fn("* creating instance disks...")
8288
      try:
8289
        _CreateDisks(self, iobj)
8290
      except errors.OpExecError:
8291
        self.LogWarning("Device creation failed, reverting...")
8292
        try:
8293
          _RemoveDisks(self, iobj)
8294
        finally:
8295
          self.cfg.ReleaseDRBDMinors(instance)
8296
          raise
8297

    
8298
    feedback_fn("adding instance %s to cluster config" % instance)
8299

    
8300
    self.cfg.AddInstance(iobj, self.proc.GetECId())
8301

    
8302
    # Declare that we don't want to remove the instance lock anymore, as we've
8303
    # added the instance to the config
8304
    del self.remove_locks[locking.LEVEL_INSTANCE]
8305

    
8306
    if self.op.mode == constants.INSTANCE_IMPORT:
8307
      # Release unused nodes
8308
      _ReleaseLocks(self, locking.LEVEL_NODE, keep=[self.op.src_node])
8309
    else:
8310
      # Release all nodes
8311
      _ReleaseLocks(self, locking.LEVEL_NODE)
8312

    
8313
    disk_abort = False
8314
    if not self.adopt_disks and self.cfg.GetClusterInfo().prealloc_wipe_disks:
8315
      feedback_fn("* wiping instance disks...")
8316
      try:
8317
        _WipeDisks(self, iobj)
8318
      except errors.OpExecError, err:
8319
        logging.exception("Wiping disks failed")
8320
        self.LogWarning("Wiping instance disks failed (%s)", err)
8321
        disk_abort = True
8322

    
8323
    if disk_abort:
8324
      # Something is already wrong with the disks, don't do anything else
8325
      pass
8326
    elif self.op.wait_for_sync:
8327
      disk_abort = not _WaitForSync(self, iobj)
8328
    elif iobj.disk_template in constants.DTS_INT_MIRROR:
8329
      # make sure the disks are not degraded (still sync-ing is ok)
8330
      time.sleep(15)
8331
      feedback_fn("* checking mirrors status")
8332
      disk_abort = not _WaitForSync(self, iobj, oneshot=True)
8333
    else:
8334
      disk_abort = False
8335

    
8336
    if disk_abort:
8337
      _RemoveDisks(self, iobj)
8338
      self.cfg.RemoveInstance(iobj.name)
8339
      # Make sure the instance lock gets removed
8340
      self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
8341
      raise errors.OpExecError("There are some degraded disks for"
8342
                               " this instance")
8343

    
8344
    if iobj.disk_template != constants.DT_DISKLESS and not self.adopt_disks:
8345
      if self.op.mode == constants.INSTANCE_CREATE:
8346
        if not self.op.no_install:
8347
          feedback_fn("* running the instance OS create scripts...")
8348
          # FIXME: pass debug option from opcode to backend
8349
          result = self.rpc.call_instance_os_add(pnode_name, iobj, False,
8350
                                                 self.op.debug_level)
8351
          result.Raise("Could not add os for instance %s"
8352
                       " on node %s" % (instance, pnode_name))
8353

    
8354
      elif self.op.mode == constants.INSTANCE_IMPORT:
8355
        feedback_fn("* running the instance OS import scripts...")
8356

    
8357
        transfers = []
8358

    
8359
        for idx, image in enumerate(self.src_images):
8360
          if not image:
8361
            continue
8362

    
8363
          # FIXME: pass debug option from opcode to backend
8364
          dt = masterd.instance.DiskTransfer("disk/%s" % idx,
8365
                                             constants.IEIO_FILE, (image, ),
8366
                                             constants.IEIO_SCRIPT,
8367
                                             (iobj.disks[idx], idx),
8368
                                             None)
8369
          transfers.append(dt)
8370

    
8371
        import_result = \
8372
          masterd.instance.TransferInstanceData(self, feedback_fn,
8373
                                                self.op.src_node, pnode_name,
8374
                                                self.pnode.secondary_ip,
8375
                                                iobj, transfers)
8376
        if not compat.all(import_result):
8377
          self.LogWarning("Some disks for instance %s on node %s were not"
8378
                          " imported successfully" % (instance, pnode_name))
8379

    
8380
      elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
8381
        feedback_fn("* preparing remote import...")
8382
        # The source cluster will stop the instance before attempting to make a
8383
        # connection. In some cases stopping an instance can take a long time,
8384
        # hence the shutdown timeout is added to the connection timeout.
8385
        connect_timeout = (constants.RIE_CONNECT_TIMEOUT +
8386
                           self.op.source_shutdown_timeout)
8387
        timeouts = masterd.instance.ImportExportTimeouts(connect_timeout)
8388

    
8389
        assert iobj.primary_node == self.pnode.name
8390
        disk_results = \
8391
          masterd.instance.RemoteImport(self, feedback_fn, iobj, self.pnode,
8392
                                        self.source_x509_ca,
8393
                                        self._cds, timeouts)
8394
        if not compat.all(disk_results):
8395
          # TODO: Should the instance still be started, even if some disks
8396
          # failed to import (valid for local imports, too)?
8397
          self.LogWarning("Some disks for instance %s on node %s were not"
8398
                          " imported successfully" % (instance, pnode_name))
8399

    
8400
        # Run rename script on newly imported instance
8401
        assert iobj.name == instance
8402
        feedback_fn("Running rename script for %s" % instance)
8403
        result = self.rpc.call_instance_run_rename(pnode_name, iobj,
8404
                                                   self.source_instance_name,
8405
                                                   self.op.debug_level)
8406
        if result.fail_msg:
8407
          self.LogWarning("Failed to run rename script for %s on node"
8408
                          " %s: %s" % (instance, pnode_name, result.fail_msg))
8409

    
8410
      else:
8411
        # also checked in the prereq part
8412
        raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
8413
                                     % self.op.mode)
8414

    
8415
    if self.op.start:
8416
      iobj.admin_up = True
8417
      self.cfg.Update(iobj, feedback_fn)
8418
      logging.info("Starting instance %s on node %s", instance, pnode_name)
8419
      feedback_fn("* starting instance...")
8420
      result = self.rpc.call_instance_start(pnode_name, iobj, None, None)
8421
      result.Raise("Could not start instance")
8422

    
8423
    return list(iobj.all_nodes)
8424

    
8425

    
8426
class LUInstanceConsole(NoHooksLU):
8427
  """Connect to an instance's console.
8428

8429
  This is somewhat special in that it returns the command line that
8430
  you need to run on the master node in order to connect to the
8431
  console.
8432

8433
  """
8434
  REQ_BGL = False
8435

    
8436
  def ExpandNames(self):
8437
    self._ExpandAndLockInstance()
8438

    
8439
  def CheckPrereq(self):
8440
    """Check prerequisites.
8441

8442
    This checks that the instance is in the cluster.
8443

8444
    """
8445
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
8446
    assert self.instance is not None, \
8447
      "Cannot retrieve locked instance %s" % self.op.instance_name
8448
    _CheckNodeOnline(self, self.instance.primary_node)
8449

    
8450
  def Exec(self, feedback_fn):
8451
    """Connect to the console of an instance
8452

8453
    """
8454
    instance = self.instance
8455
    node = instance.primary_node
8456

    
8457
    node_insts = self.rpc.call_instance_list([node],
8458
                                             [instance.hypervisor])[node]
8459
    node_insts.Raise("Can't get node information from %s" % node)
8460

    
8461
    if instance.name not in node_insts.payload:
8462
      if instance.admin_up:
8463
        state = constants.INSTST_ERRORDOWN
8464
      else:
8465
        state = constants.INSTST_ADMINDOWN
8466
      raise errors.OpExecError("Instance %s is not running (state %s)" %
8467
                               (instance.name, state))
8468

    
8469
    logging.debug("Connecting to console of %s on %s", instance.name, node)
8470

    
8471
    return _GetInstanceConsole(self.cfg.GetClusterInfo(), instance)
8472

    
8473

    
8474
def _GetInstanceConsole(cluster, instance):
8475
  """Returns console information for an instance.
8476

8477
  @type cluster: L{objects.Cluster}
8478
  @type instance: L{objects.Instance}
8479
  @rtype: dict
8480

8481
  """
8482
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
8483
  # beparams and hvparams are passed separately, to avoid editing the
8484
  # instance and then saving the defaults in the instance itself.
8485
  hvparams = cluster.FillHV(instance)
8486
  beparams = cluster.FillBE(instance)
8487
  console = hyper.GetInstanceConsole(instance, hvparams, beparams)
8488

    
8489
  assert console.instance == instance.name
8490
  assert console.Validate()
8491

    
8492
  return console.ToDict()
8493

    
8494

    
8495
class LUInstanceReplaceDisks(LogicalUnit):
8496
  """Replace the disks of an instance.
8497

8498
  """
8499
  HPATH = "mirrors-replace"
8500
  HTYPE = constants.HTYPE_INSTANCE
8501
  REQ_BGL = False
8502

    
8503
  def CheckArguments(self):
8504
    TLReplaceDisks.CheckArguments(self.op.mode, self.op.remote_node,
8505
                                  self.op.iallocator)
8506

    
8507
  def ExpandNames(self):
8508
    self._ExpandAndLockInstance()
8509

    
8510
    assert locking.LEVEL_NODE not in self.needed_locks
8511
    assert locking.LEVEL_NODEGROUP not in self.needed_locks
8512

    
8513
    assert self.op.iallocator is None or self.op.remote_node is None, \
8514
      "Conflicting options"
8515

    
8516
    if self.op.remote_node is not None:
8517
      self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
8518

    
8519
      # Warning: do not remove the locking of the new secondary here
8520
      # unless DRBD8.AddChildren is changed to work in parallel;
8521
      # currently it doesn't since parallel invocations of
8522
      # FindUnusedMinor will conflict
8523
      self.needed_locks[locking.LEVEL_NODE] = [self.op.remote_node]
8524
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
8525
    else:
8526
      self.needed_locks[locking.LEVEL_NODE] = []
8527
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
8528

    
8529
      if self.op.iallocator is not None:
8530
        # iallocator will select a new node in the same group
8531
        self.needed_locks[locking.LEVEL_NODEGROUP] = []
8532

    
8533
    self.replacer = TLReplaceDisks(self, self.op.instance_name, self.op.mode,
8534
                                   self.op.iallocator, self.op.remote_node,
8535
                                   self.op.disks, False, self.op.early_release)
8536

    
8537
    self.tasklets = [self.replacer]
8538

    
8539
  def DeclareLocks(self, level):
8540
    if level == locking.LEVEL_NODEGROUP:
8541
      assert self.op.remote_node is None
8542
      assert self.op.iallocator is not None
8543
      assert not self.needed_locks[locking.LEVEL_NODEGROUP]
8544

    
8545
      self.share_locks[locking.LEVEL_NODEGROUP] = 1
8546
      self.needed_locks[locking.LEVEL_NODEGROUP] = \
8547
        self.cfg.GetInstanceNodeGroups(self.op.instance_name)
8548

    
8549
    elif level == locking.LEVEL_NODE:
8550
      if self.op.iallocator is not None:
8551
        assert self.op.remote_node is None
8552
        assert not self.needed_locks[locking.LEVEL_NODE]
8553

    
8554
        # Lock member nodes of all locked groups
8555
        self.needed_locks[locking.LEVEL_NODE] = [node_name
8556
          for group_uuid in self.glm.list_owned(locking.LEVEL_NODEGROUP)
8557
          for node_name in self.cfg.GetNodeGroup(group_uuid).members]
8558
      else:
8559
        self._LockInstancesNodes()
8560

    
8561
  def BuildHooksEnv(self):
8562
    """Build hooks env.
8563

8564
    This runs on the master, the primary and all the secondaries.
8565

8566
    """
8567
    instance = self.replacer.instance
8568
    env = {
8569
      "MODE": self.op.mode,
8570
      "NEW_SECONDARY": self.op.remote_node,
8571
      "OLD_SECONDARY": instance.secondary_nodes[0],
8572
      }
8573
    env.update(_BuildInstanceHookEnvByObject(self, instance))
8574
    return env
8575

    
8576
  def BuildHooksNodes(self):
8577
    """Build hooks nodes.
8578

8579
    """
8580
    instance = self.replacer.instance
8581
    nl = [
8582
      self.cfg.GetMasterNode(),
8583
      instance.primary_node,
8584
      ]
8585
    if self.op.remote_node is not None:
8586
      nl.append(self.op.remote_node)
8587
    return nl, nl
8588

    
8589
  def CheckPrereq(self):
8590
    """Check prerequisites.
8591

8592
    """
8593
    assert (self.glm.is_owned(locking.LEVEL_NODEGROUP) or
8594
            self.op.iallocator is None)
8595

    
8596
    owned_groups = self.glm.list_owned(locking.LEVEL_NODEGROUP)
8597
    if owned_groups:
8598
      groups = self.cfg.GetInstanceNodeGroups(self.op.instance_name)
8599
      if owned_groups != groups:
8600
        raise errors.OpExecError("Node groups used by instance '%s' changed"
8601
                                 " since lock was acquired, current list is %r,"
8602
                                 " used to be '%s'" %
8603
                                 (self.op.instance_name,
8604
                                  utils.CommaJoin(groups),
8605
                                  utils.CommaJoin(owned_groups)))
8606

    
8607
    return LogicalUnit.CheckPrereq(self)
8608

    
8609

    
8610
class TLReplaceDisks(Tasklet):
8611
  """Replaces disks for an instance.
8612

8613
  Note: Locking is not within the scope of this class.
8614

8615
  """
8616
  def __init__(self, lu, instance_name, mode, iallocator_name, remote_node,
8617
               disks, delay_iallocator, early_release):
8618
    """Initializes this class.
8619

8620
    """
8621
    Tasklet.__init__(self, lu)
8622

    
8623
    # Parameters
8624
    self.instance_name = instance_name
8625
    self.mode = mode
8626
    self.iallocator_name = iallocator_name
8627
    self.remote_node = remote_node
8628
    self.disks = disks
8629
    self.delay_iallocator = delay_iallocator
8630
    self.early_release = early_release
8631

    
8632
    # Runtime data
8633
    self.instance = None
8634
    self.new_node = None
8635
    self.target_node = None
8636
    self.other_node = None
8637
    self.remote_node_info = None
8638
    self.node_secondary_ip = None
8639

    
8640
  @staticmethod
8641
  def CheckArguments(mode, remote_node, iallocator):
8642
    """Helper function for users of this class.
8643

8644
    """
8645
    # check for valid parameter combination
8646
    if mode == constants.REPLACE_DISK_CHG:
8647
      if remote_node is None and iallocator is None:
8648
        raise errors.OpPrereqError("When changing the secondary either an"
8649
                                   " iallocator script must be used or the"
8650
                                   " new node given", errors.ECODE_INVAL)
8651

    
8652
      if remote_node is not None and iallocator is not None:
8653
        raise errors.OpPrereqError("Give either the iallocator or the new"
8654
                                   " secondary, not both", errors.ECODE_INVAL)
8655

    
8656
    elif remote_node is not None or iallocator is not None:
8657
      # Not replacing the secondary
8658
      raise errors.OpPrereqError("The iallocator and new node options can"
8659
                                 " only be used when changing the"
8660
                                 " secondary node", errors.ECODE_INVAL)
8661

    
8662
  @staticmethod
8663
  def _RunAllocator(lu, iallocator_name, instance_name, relocate_from):
8664
    """Compute a new secondary node using an IAllocator.
8665

8666
    """
8667
    ial = IAllocator(lu.cfg, lu.rpc,
8668
                     mode=constants.IALLOCATOR_MODE_RELOC,
8669
                     name=instance_name,
8670
                     relocate_from=relocate_from)
8671

    
8672
    ial.Run(iallocator_name)
8673

    
8674
    if not ial.success:
8675
      raise errors.OpPrereqError("Can't compute nodes using iallocator '%s':"
8676
                                 " %s" % (iallocator_name, ial.info),
8677
                                 errors.ECODE_NORES)
8678

    
8679
    if len(ial.result) != ial.required_nodes:
8680
      raise errors.OpPrereqError("iallocator '%s' returned invalid number"
8681
                                 " of nodes (%s), required %s" %
8682
                                 (iallocator_name,
8683
                                  len(ial.result), ial.required_nodes),
8684
                                 errors.ECODE_FAULT)
8685

    
8686
    remote_node_name = ial.result[0]
8687

    
8688
    lu.LogInfo("Selected new secondary for instance '%s': %s",
8689
               instance_name, remote_node_name)
8690

    
8691
    return remote_node_name
8692

    
8693
  def _FindFaultyDisks(self, node_name):
8694
    return _FindFaultyInstanceDisks(self.cfg, self.rpc, self.instance,
8695
                                    node_name, True)
8696

    
8697
  def _CheckDisksActivated(self, instance):
8698
    """Checks if the instance disks are activated.
8699

8700
    @param instance: The instance to check disks
8701
    @return: True if they are activated, False otherwise
8702

8703
    """
8704
    nodes = instance.all_nodes
8705

    
8706
    for idx, dev in enumerate(instance.disks):
8707
      for node in nodes:
8708
        self.lu.LogInfo("Checking disk/%d on %s", idx, node)
8709
        self.cfg.SetDiskID(dev, node)
8710

    
8711
        result = self.rpc.call_blockdev_find(node, dev)
8712

    
8713
        if result.offline:
8714
          continue
8715
        elif result.fail_msg or not result.payload:
8716
          return False
8717

    
8718
    return True
8719

    
8720
  def CheckPrereq(self):
8721
    """Check prerequisites.
8722

8723
    This checks that the instance is in the cluster.
8724

8725
    """
8726
    self.instance = instance = self.cfg.GetInstanceInfo(self.instance_name)
8727
    assert instance is not None, \
8728
      "Cannot retrieve locked instance %s" % self.instance_name
8729

    
8730
    if instance.disk_template != constants.DT_DRBD8:
8731
      raise errors.OpPrereqError("Can only run replace disks for DRBD8-based"
8732
                                 " instances", errors.ECODE_INVAL)
8733

    
8734
    if len(instance.secondary_nodes) != 1:
8735
      raise errors.OpPrereqError("The instance has a strange layout,"
8736
                                 " expected one secondary but found %d" %
8737
                                 len(instance.secondary_nodes),
8738
                                 errors.ECODE_FAULT)
8739

    
8740
    if not self.delay_iallocator:
8741
      self._CheckPrereq2()
8742

    
8743
  def _CheckPrereq2(self):
8744
    """Check prerequisites, second part.
8745

8746
    This function should always be part of CheckPrereq. It was separated and is
8747
    now called from Exec because during node evacuation iallocator was only
8748
    called with an unmodified cluster model, not taking planned changes into
8749
    account.
8750

8751
    """
8752
    instance = self.instance
8753
    secondary_node = instance.secondary_nodes[0]
8754

    
8755
    if self.iallocator_name is None:
8756
      remote_node = self.remote_node
8757
    else:
8758
      remote_node = self._RunAllocator(self.lu, self.iallocator_name,
8759
                                       instance.name, instance.secondary_nodes)
8760

    
8761
    if remote_node is None:
8762
      self.remote_node_info = None
8763
    else:
8764
      assert remote_node in self.lu.glm.list_owned(locking.LEVEL_NODE), \
8765
             "Remote node '%s' is not locked" % remote_node
8766

    
8767
      self.remote_node_info = self.cfg.GetNodeInfo(remote_node)
8768
      assert self.remote_node_info is not None, \
8769
        "Cannot retrieve locked node %s" % remote_node
8770

    
8771
    if remote_node == self.instance.primary_node:
8772
      raise errors.OpPrereqError("The specified node is the primary node of"
8773
                                 " the instance", errors.ECODE_INVAL)
8774

    
8775
    if remote_node == secondary_node:
8776
      raise errors.OpPrereqError("The specified node is already the"
8777
                                 " secondary node of the instance",
8778
                                 errors.ECODE_INVAL)
8779

    
8780
    if self.disks and self.mode in (constants.REPLACE_DISK_AUTO,
8781
                                    constants.REPLACE_DISK_CHG):
8782
      raise errors.OpPrereqError("Cannot specify disks to be replaced",
8783
                                 errors.ECODE_INVAL)
8784

    
8785
    if self.mode == constants.REPLACE_DISK_AUTO:
8786
      if not self._CheckDisksActivated(instance):
8787
        raise errors.OpPrereqError("Please run activate-disks on instance %s"
8788
                                   " first" % self.instance_name,
8789
                                   errors.ECODE_STATE)
8790
      faulty_primary = self._FindFaultyDisks(instance.primary_node)
8791
      faulty_secondary = self._FindFaultyDisks(secondary_node)
8792

    
8793
      if faulty_primary and faulty_secondary:
8794
        raise errors.OpPrereqError("Instance %s has faulty disks on more than"
8795
                                   " one node and can not be repaired"
8796
                                   " automatically" % self.instance_name,
8797
                                   errors.ECODE_STATE)
8798

    
8799
      if faulty_primary:
8800
        self.disks = faulty_primary
8801
        self.target_node = instance.primary_node
8802
        self.other_node = secondary_node
8803
        check_nodes = [self.target_node, self.other_node]
8804
      elif faulty_secondary:
8805
        self.disks = faulty_secondary
8806
        self.target_node = secondary_node
8807
        self.other_node = instance.primary_node
8808
        check_nodes = [self.target_node, self.other_node]
8809
      else:
8810
        self.disks = []
8811
        check_nodes = []
8812

    
8813
    else:
8814
      # Non-automatic modes
8815
      if self.mode == constants.REPLACE_DISK_PRI:
8816
        self.target_node = instance.primary_node
8817
        self.other_node = secondary_node
8818
        check_nodes = [self.target_node, self.other_node]
8819

    
8820
      elif self.mode == constants.REPLACE_DISK_SEC:
8821
        self.target_node = secondary_node
8822
        self.other_node = instance.primary_node
8823
        check_nodes = [self.target_node, self.other_node]
8824

    
8825
      elif self.mode == constants.REPLACE_DISK_CHG:
8826
        self.new_node = remote_node
8827
        self.other_node = instance.primary_node
8828
        self.target_node = secondary_node
8829
        check_nodes = [self.new_node, self.other_node]
8830

    
8831
        _CheckNodeNotDrained(self.lu, remote_node)
8832
        _CheckNodeVmCapable(self.lu, remote_node)
8833

    
8834
        old_node_info = self.cfg.GetNodeInfo(secondary_node)
8835
        assert old_node_info is not None
8836
        if old_node_info.offline and not self.early_release:
8837
          # doesn't make sense to delay the release
8838
          self.early_release = True
8839
          self.lu.LogInfo("Old secondary %s is offline, automatically enabling"
8840
                          " early-release mode", secondary_node)
8841

    
8842
      else:
8843
        raise errors.ProgrammerError("Unhandled disk replace mode (%s)" %
8844
                                     self.mode)
8845

    
8846
      # If not specified all disks should be replaced
8847
      if not self.disks:
8848
        self.disks = range(len(self.instance.disks))
8849

    
8850
    for node in check_nodes:
8851
      _CheckNodeOnline(self.lu, node)
8852

    
8853
    touched_nodes = frozenset(node_name for node_name in [self.new_node,
8854
                                                          self.other_node,
8855
                                                          self.target_node]
8856
                              if node_name is not None)
8857

    
8858
    # Release unneeded node locks
8859
    _ReleaseLocks(self.lu, locking.LEVEL_NODE, keep=touched_nodes)
8860

    
8861
    # Release any owned node group
8862
    if self.lu.glm.is_owned(locking.LEVEL_NODEGROUP):
8863
      _ReleaseLocks(self.lu, locking.LEVEL_NODEGROUP)
8864

    
8865
    # Check whether disks are valid
8866
    for disk_idx in self.disks:
8867
      instance.FindDisk(disk_idx)
8868

    
8869
    # Get secondary node IP addresses
8870
    self.node_secondary_ip = \
8871
      dict((node_name, self.cfg.GetNodeInfo(node_name).secondary_ip)
8872
           for node_name in touched_nodes)
8873

    
8874
  def Exec(self, feedback_fn):
8875
    """Execute disk replacement.
8876

8877
    This dispatches the disk replacement to the appropriate handler.
8878

8879
    """
8880
    if self.delay_iallocator:
8881
      self._CheckPrereq2()
8882

    
8883
    if __debug__:
8884
      # Verify owned locks before starting operation
8885
      owned_locks = self.lu.glm.list_owned(locking.LEVEL_NODE)
8886
      assert set(owned_locks) == set(self.node_secondary_ip), \
8887
          ("Incorrect node locks, owning %s, expected %s" %
8888
           (owned_locks, self.node_secondary_ip.keys()))
8889

    
8890
      owned_locks = self.lu.glm.list_owned(locking.LEVEL_INSTANCE)
8891
      assert list(owned_locks) == [self.instance_name], \
8892
          "Instance '%s' not locked" % self.instance_name
8893

    
8894
      assert not self.lu.glm.is_owned(locking.LEVEL_NODEGROUP), \
8895
          "Should not own any node group lock at this point"
8896

    
8897
    if not self.disks:
8898
      feedback_fn("No disks need replacement")
8899
      return
8900

    
8901
    feedback_fn("Replacing disk(s) %s for %s" %
8902
                (utils.CommaJoin(self.disks), self.instance.name))
8903

    
8904
    activate_disks = (not self.instance.admin_up)
8905

    
8906
    # Activate the instance disks if we're replacing them on a down instance
8907
    if activate_disks:
8908
      _StartInstanceDisks(self.lu, self.instance, True)
8909

    
8910
    try:
8911
      # Should we replace the secondary node?
8912
      if self.new_node is not None:
8913
        fn = self._ExecDrbd8Secondary
8914
      else:
8915
        fn = self._ExecDrbd8DiskOnly
8916

    
8917
      result = fn(feedback_fn)
8918
    finally:
8919
      # Deactivate the instance disks if we're replacing them on a
8920
      # down instance
8921
      if activate_disks:
8922
        _SafeShutdownInstanceDisks(self.lu, self.instance)
8923

    
8924
    if __debug__:
8925
      # Verify owned locks
8926
      owned_locks = self.lu.glm.list_owned(locking.LEVEL_NODE)
8927
      nodes = frozenset(self.node_secondary_ip)
8928
      assert ((self.early_release and not owned_locks) or
8929
              (not self.early_release and not (set(owned_locks) - nodes))), \
8930
        ("Not owning the correct locks, early_release=%s, owned=%r,"
8931
         " nodes=%r" % (self.early_release, owned_locks, nodes))
8932

    
8933
    return result
8934

    
8935
  def _CheckVolumeGroup(self, nodes):
8936
    self.lu.LogInfo("Checking volume groups")
8937

    
8938
    vgname = self.cfg.GetVGName()
8939

    
8940
    # Make sure volume group exists on all involved nodes
8941
    results = self.rpc.call_vg_list(nodes)
8942
    if not results:
8943
      raise errors.OpExecError("Can't list volume groups on the nodes")
8944

    
8945
    for node in nodes:
8946
      res = results[node]
8947
      res.Raise("Error checking node %s" % node)
8948
      if vgname not in res.payload:
8949
        raise errors.OpExecError("Volume group '%s' not found on node %s" %
8950
                                 (vgname, node))
8951

    
8952
  def _CheckDisksExistence(self, nodes):
8953
    # Check disk existence
8954
    for idx, dev in enumerate(self.instance.disks):
8955
      if idx not in self.disks:
8956
        continue
8957

    
8958
      for node in nodes:
8959
        self.lu.LogInfo("Checking disk/%d on %s" % (idx, node))
8960
        self.cfg.SetDiskID(dev, node)
8961

    
8962
        result = self.rpc.call_blockdev_find(node, dev)
8963

    
8964
        msg = result.fail_msg
8965
        if msg or not result.payload:
8966
          if not msg:
8967
            msg = "disk not found"
8968
          raise errors.OpExecError("Can't find disk/%d on node %s: %s" %
8969
                                   (idx, node, msg))
8970

    
8971
  def _CheckDisksConsistency(self, node_name, on_primary, ldisk):
8972
    for idx, dev in enumerate(self.instance.disks):
8973
      if idx not in self.disks:
8974
        continue
8975

    
8976
      self.lu.LogInfo("Checking disk/%d consistency on node %s" %
8977
                      (idx, node_name))
8978

    
8979
      if not _CheckDiskConsistency(self.lu, dev, node_name, on_primary,
8980
                                   ldisk=ldisk):
8981
        raise errors.OpExecError("Node %s has degraded storage, unsafe to"
8982
                                 " replace disks for instance %s" %
8983
                                 (node_name, self.instance.name))
8984

    
8985
  def _CreateNewStorage(self, node_name):
8986
    iv_names = {}
8987

    
8988
    for idx, dev in enumerate(self.instance.disks):
8989
      if idx not in self.disks:
8990
        continue
8991

    
8992
      self.lu.LogInfo("Adding storage on %s for disk/%d" % (node_name, idx))
8993

    
8994
      self.cfg.SetDiskID(dev, node_name)
8995

    
8996
      lv_names = [".disk%d_%s" % (idx, suffix) for suffix in ["data", "meta"]]
8997
      names = _GenerateUniqueNames(self.lu, lv_names)
8998

    
8999
      vg_data = dev.children[0].logical_id[0]
9000
      lv_data = objects.Disk(dev_type=constants.LD_LV, size=dev.size,
9001
                             logical_id=(vg_data, names[0]))
9002
      vg_meta = dev.children[1].logical_id[0]
9003
      lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
9004
                             logical_id=(vg_meta, names[1]))
9005

    
9006
      new_lvs = [lv_data, lv_meta]
9007
      old_lvs = dev.children
9008
      iv_names[dev.iv_name] = (dev, old_lvs, new_lvs)
9009

    
9010
      # we pass force_create=True to force the LVM creation
9011
      for new_lv in new_lvs:
9012
        _CreateBlockDev(self.lu, node_name, self.instance, new_lv, True,
9013
                        _GetInstanceInfoText(self.instance), False)
9014

    
9015
    return iv_names
9016

    
9017
  def _CheckDevices(self, node_name, iv_names):
9018
    for name, (dev, _, _) in iv_names.iteritems():
9019
      self.cfg.SetDiskID(dev, node_name)
9020

    
9021
      result = self.rpc.call_blockdev_find(node_name, dev)
9022

    
9023
      msg = result.fail_msg
9024
      if msg or not result.payload:
9025
        if not msg:
9026
          msg = "disk not found"
9027
        raise errors.OpExecError("Can't find DRBD device %s: %s" %
9028
                                 (name, msg))
9029

    
9030
      if result.payload.is_degraded:
9031
        raise errors.OpExecError("DRBD device %s is degraded!" % name)
9032

    
9033
  def _RemoveOldStorage(self, node_name, iv_names):
9034
    for name, (_, old_lvs, _) in iv_names.iteritems():
9035
      self.lu.LogInfo("Remove logical volumes for %s" % name)
9036

    
9037
      for lv in old_lvs:
9038
        self.cfg.SetDiskID(lv, node_name)
9039

    
9040
        msg = self.rpc.call_blockdev_remove(node_name, lv).fail_msg
9041
        if msg:
9042
          self.lu.LogWarning("Can't remove old LV: %s" % msg,
9043
                             hint="remove unused LVs manually")
9044

    
9045
  def _ExecDrbd8DiskOnly(self, feedback_fn):
9046
    """Replace a disk on the primary or secondary for DRBD 8.
9047

9048
    The algorithm for replace is quite complicated:
9049

9050
      1. for each disk to be replaced:
9051

9052
        1. create new LVs on the target node with unique names
9053
        1. detach old LVs from the drbd device
9054
        1. rename old LVs to name_replaced.<time_t>
9055
        1. rename new LVs to old LVs
9056
        1. attach the new LVs (with the old names now) to the drbd device
9057

9058
      1. wait for sync across all devices
9059

9060
      1. for each modified disk:
9061

9062
        1. remove old LVs (which have the name name_replaces.<time_t>)
9063

9064
    Failures are not very well handled.
9065

9066
    """
9067
    steps_total = 6
9068

    
9069
    # Step: check device activation
9070
    self.lu.LogStep(1, steps_total, "Check device existence")
9071
    self._CheckDisksExistence([self.other_node, self.target_node])
9072
    self._CheckVolumeGroup([self.target_node, self.other_node])
9073

    
9074
    # Step: check other node consistency
9075
    self.lu.LogStep(2, steps_total, "Check peer consistency")
9076
    self._CheckDisksConsistency(self.other_node,
9077
                                self.other_node == self.instance.primary_node,
9078
                                False)
9079

    
9080
    # Step: create new storage
9081
    self.lu.LogStep(3, steps_total, "Allocate new storage")
9082
    iv_names = self._CreateNewStorage(self.target_node)
9083

    
9084
    # Step: for each lv, detach+rename*2+attach
9085
    self.lu.LogStep(4, steps_total, "Changing drbd configuration")
9086
    for dev, old_lvs, new_lvs in iv_names.itervalues():
9087
      self.lu.LogInfo("Detaching %s drbd from local storage" % dev.iv_name)
9088

    
9089
      result = self.rpc.call_blockdev_removechildren(self.target_node, dev,
9090
                                                     old_lvs)
9091
      result.Raise("Can't detach drbd from local storage on node"
9092
                   " %s for device %s" % (self.target_node, dev.iv_name))
9093
      #dev.children = []
9094
      #cfg.Update(instance)
9095

    
9096
      # ok, we created the new LVs, so now we know we have the needed
9097
      # storage; as such, we proceed on the target node to rename
9098
      # old_lv to _old, and new_lv to old_lv; note that we rename LVs
9099
      # using the assumption that logical_id == physical_id (which in
9100
      # turn is the unique_id on that node)
9101

    
9102
      # FIXME(iustin): use a better name for the replaced LVs
9103
      temp_suffix = int(time.time())
9104
      ren_fn = lambda d, suff: (d.physical_id[0],
9105
                                d.physical_id[1] + "_replaced-%s" % suff)
9106

    
9107
      # Build the rename list based on what LVs exist on the node
9108
      rename_old_to_new = []
9109
      for to_ren in old_lvs:
9110
        result = self.rpc.call_blockdev_find(self.target_node, to_ren)
9111
        if not result.fail_msg and result.payload:
9112
          # device exists
9113
          rename_old_to_new.append((to_ren, ren_fn(to_ren, temp_suffix)))
9114

    
9115
      self.lu.LogInfo("Renaming the old LVs on the target node")
9116
      result = self.rpc.call_blockdev_rename(self.target_node,
9117
                                             rename_old_to_new)
9118
      result.Raise("Can't rename old LVs on node %s" % self.target_node)
9119

    
9120
      # Now we rename the new LVs to the old LVs
9121
      self.lu.LogInfo("Renaming the new LVs on the target node")
9122
      rename_new_to_old = [(new, old.physical_id)
9123
                           for old, new in zip(old_lvs, new_lvs)]
9124
      result = self.rpc.call_blockdev_rename(self.target_node,
9125
                                             rename_new_to_old)
9126
      result.Raise("Can't rename new LVs on node %s" % self.target_node)
9127

    
9128
      for old, new in zip(old_lvs, new_lvs):
9129
        new.logical_id = old.logical_id
9130
        self.cfg.SetDiskID(new, self.target_node)
9131

    
9132
      for disk in old_lvs:
9133
        disk.logical_id = ren_fn(disk, temp_suffix)
9134
        self.cfg.SetDiskID(disk, self.target_node)
9135

    
9136
      # Now that the new lvs have the old name, we can add them to the device
9137
      self.lu.LogInfo("Adding new mirror component on %s" % self.target_node)
9138
      result = self.rpc.call_blockdev_addchildren(self.target_node, dev,
9139
                                                  new_lvs)
9140
      msg = result.fail_msg
9141
      if msg:
9142
        for new_lv in new_lvs:
9143
          msg2 = self.rpc.call_blockdev_remove(self.target_node,
9144
                                               new_lv).fail_msg
9145
          if msg2:
9146
            self.lu.LogWarning("Can't rollback device %s: %s", dev, msg2,
9147
                               hint=("cleanup manually the unused logical"
9148
                                     "volumes"))
9149
        raise errors.OpExecError("Can't add local storage to drbd: %s" % msg)
9150

    
9151
      dev.children = new_lvs
9152

    
9153
      self.cfg.Update(self.instance, feedback_fn)
9154

    
9155
    cstep = 5
9156
    if self.early_release:
9157
      self.lu.LogStep(cstep, steps_total, "Removing old storage")
9158
      cstep += 1
9159
      self._RemoveOldStorage(self.target_node, iv_names)
9160
      # WARNING: we release both node locks here, do not do other RPCs
9161
      # than WaitForSync to the primary node
9162
      _ReleaseLocks(self.lu, locking.LEVEL_NODE,
9163
                    names=[self.target_node, self.other_node])
9164

    
9165
    # Wait for sync
9166
    # This can fail as the old devices are degraded and _WaitForSync
9167
    # does a combined result over all disks, so we don't check its return value
9168
    self.lu.LogStep(cstep, steps_total, "Sync devices")
9169
    cstep += 1
9170
    _WaitForSync(self.lu, self.instance)
9171

    
9172
    # Check all devices manually
9173
    self._CheckDevices(self.instance.primary_node, iv_names)
9174

    
9175
    # Step: remove old storage
9176
    if not self.early_release:
9177
      self.lu.LogStep(cstep, steps_total, "Removing old storage")
9178
      cstep += 1
9179
      self._RemoveOldStorage(self.target_node, iv_names)
9180

    
9181
  def _ExecDrbd8Secondary(self, feedback_fn):
9182
    """Replace the secondary node for DRBD 8.
9183

9184
    The algorithm for replace is quite complicated:
9185
      - for all disks of the instance:
9186
        - create new LVs on the new node with same names
9187
        - shutdown the drbd device on the old secondary
9188
        - disconnect the drbd network on the primary
9189
        - create the drbd device on the new secondary
9190
        - network attach the drbd on the primary, using an artifice:
9191
          the drbd code for Attach() will connect to the network if it
9192
          finds a device which is connected to the good local disks but
9193
          not network enabled
9194
      - wait for sync across all devices
9195
      - remove all disks from the old secondary
9196

9197
    Failures are not very well handled.
9198

9199
    """
9200
    steps_total = 6
9201

    
9202
    # Step: check device activation
9203
    self.lu.LogStep(1, steps_total, "Check device existence")
9204
    self._CheckDisksExistence([self.instance.primary_node])
9205
    self._CheckVolumeGroup([self.instance.primary_node])
9206

    
9207
    # Step: check other node consistency
9208
    self.lu.LogStep(2, steps_total, "Check peer consistency")
9209
    self._CheckDisksConsistency(self.instance.primary_node, True, True)
9210

    
9211
    # Step: create new storage
9212
    self.lu.LogStep(3, steps_total, "Allocate new storage")
9213
    for idx, dev in enumerate(self.instance.disks):
9214
      self.lu.LogInfo("Adding new local storage on %s for disk/%d" %
9215
                      (self.new_node, idx))
9216
      # we pass force_create=True to force LVM creation
9217
      for new_lv in dev.children:
9218
        _CreateBlockDev(self.lu, self.new_node, self.instance, new_lv, True,
9219
                        _GetInstanceInfoText(self.instance), False)
9220

    
9221
    # Step 4: dbrd minors and drbd setups changes
9222
    # after this, we must manually remove the drbd minors on both the
9223
    # error and the success paths
9224
    self.lu.LogStep(4, steps_total, "Changing drbd configuration")
9225
    minors = self.cfg.AllocateDRBDMinor([self.new_node
9226
                                         for dev in self.instance.disks],
9227
                                        self.instance.name)
9228
    logging.debug("Allocated minors %r", minors)
9229

    
9230
    iv_names = {}
9231
    for idx, (dev, new_minor) in enumerate(zip(self.instance.disks, minors)):
9232
      self.lu.LogInfo("activating a new drbd on %s for disk/%d" %
9233
                      (self.new_node, idx))
9234
      # create new devices on new_node; note that we create two IDs:
9235
      # one without port, so the drbd will be activated without
9236
      # networking information on the new node at this stage, and one
9237
      # with network, for the latter activation in step 4
9238
      (o_node1, o_node2, o_port, o_minor1, o_minor2, o_secret) = dev.logical_id
9239
      if self.instance.primary_node == o_node1:
9240
        p_minor = o_minor1
9241
      else:
9242
        assert self.instance.primary_node == o_node2, "Three-node instance?"
9243
        p_minor = o_minor2
9244

    
9245
      new_alone_id = (self.instance.primary_node, self.new_node, None,
9246
                      p_minor, new_minor, o_secret)
9247
      new_net_id = (self.instance.primary_node, self.new_node, o_port,
9248
                    p_minor, new_minor, o_secret)
9249

    
9250
      iv_names[idx] = (dev, dev.children, new_net_id)
9251
      logging.debug("Allocated new_minor: %s, new_logical_id: %s", new_minor,
9252
                    new_net_id)
9253
      new_drbd = objects.Disk(dev_type=constants.LD_DRBD8,
9254
                              logical_id=new_alone_id,
9255
                              children=dev.children,
9256
                              size=dev.size)
9257
      try:
9258
        _CreateSingleBlockDev(self.lu, self.new_node, self.instance, new_drbd,
9259
                              _GetInstanceInfoText(self.instance), False)
9260
      except errors.GenericError:
9261
        self.cfg.ReleaseDRBDMinors(self.instance.name)
9262
        raise
9263

    
9264
    # We have new devices, shutdown the drbd on the old secondary
9265
    for idx, dev in enumerate(self.instance.disks):
9266
      self.lu.LogInfo("Shutting down drbd for disk/%d on old node" % idx)
9267
      self.cfg.SetDiskID(dev, self.target_node)
9268
      msg = self.rpc.call_blockdev_shutdown(self.target_node, dev).fail_msg
9269
      if msg:
9270
        self.lu.LogWarning("Failed to shutdown drbd for disk/%d on old"
9271
                           "node: %s" % (idx, msg),
9272
                           hint=("Please cleanup this device manually as"
9273
                                 " soon as possible"))
9274

    
9275
    self.lu.LogInfo("Detaching primary drbds from the network (=> standalone)")
9276
    result = self.rpc.call_drbd_disconnect_net([self.instance.primary_node],
9277
                                               self.node_secondary_ip,
9278
                                               self.instance.disks)\
9279
                                              [self.instance.primary_node]
9280

    
9281
    msg = result.fail_msg
9282
    if msg:
9283
      # detaches didn't succeed (unlikely)
9284
      self.cfg.ReleaseDRBDMinors(self.instance.name)
9285
      raise errors.OpExecError("Can't detach the disks from the network on"
9286
                               " old node: %s" % (msg,))
9287

    
9288
    # if we managed to detach at least one, we update all the disks of
9289
    # the instance to point to the new secondary
9290
    self.lu.LogInfo("Updating instance configuration")
9291
    for dev, _, new_logical_id in iv_names.itervalues():
9292
      dev.logical_id = new_logical_id
9293
      self.cfg.SetDiskID(dev, self.instance.primary_node)
9294

    
9295
    self.cfg.Update(self.instance, feedback_fn)
9296

    
9297
    # and now perform the drbd attach
9298
    self.lu.LogInfo("Attaching primary drbds to new secondary"
9299
                    " (standalone => connected)")
9300
    result = self.rpc.call_drbd_attach_net([self.instance.primary_node,
9301
                                            self.new_node],
9302
                                           self.node_secondary_ip,
9303
                                           self.instance.disks,
9304
                                           self.instance.name,
9305
                                           False)
9306
    for to_node, to_result in result.items():
9307
      msg = to_result.fail_msg
9308
      if msg:
9309
        self.lu.LogWarning("Can't attach drbd disks on node %s: %s",
9310
                           to_node, msg,
9311
                           hint=("please do a gnt-instance info to see the"
9312
                                 " status of disks"))
9313
    cstep = 5
9314
    if self.early_release:
9315
      self.lu.LogStep(cstep, steps_total, "Removing old storage")
9316
      cstep += 1
9317
      self._RemoveOldStorage(self.target_node, iv_names)
9318
      # WARNING: we release all node locks here, do not do other RPCs
9319
      # than WaitForSync to the primary node
9320
      _ReleaseLocks(self.lu, locking.LEVEL_NODE,
9321
                    names=[self.instance.primary_node,
9322
                           self.target_node,
9323
                           self.new_node])
9324

    
9325
    # Wait for sync
9326
    # This can fail as the old devices are degraded and _WaitForSync
9327
    # does a combined result over all disks, so we don't check its return value
9328
    self.lu.LogStep(cstep, steps_total, "Sync devices")
9329
    cstep += 1
9330
    _WaitForSync(self.lu, self.instance)
9331

    
9332
    # Check all devices manually
9333
    self._CheckDevices(self.instance.primary_node, iv_names)
9334

    
9335
    # Step: remove old storage
9336
    if not self.early_release:
9337
      self.lu.LogStep(cstep, steps_total, "Removing old storage")
9338
      self._RemoveOldStorage(self.target_node, iv_names)
9339

    
9340

    
9341
class LURepairNodeStorage(NoHooksLU):
9342
  """Repairs the volume group on a node.
9343

9344
  """
9345
  REQ_BGL = False
9346

    
9347
  def CheckArguments(self):
9348
    self.op.node_name = _ExpandNodeName(self.cfg, self.op.node_name)
9349

    
9350
    storage_type = self.op.storage_type
9351

    
9352
    if (constants.SO_FIX_CONSISTENCY not in
9353
        constants.VALID_STORAGE_OPERATIONS.get(storage_type, [])):
9354
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
9355
                                 " repaired" % storage_type,
9356
                                 errors.ECODE_INVAL)
9357

    
9358
  def ExpandNames(self):
9359
    self.needed_locks = {
9360
      locking.LEVEL_NODE: [self.op.node_name],
9361
      }
9362

    
9363
  def _CheckFaultyDisks(self, instance, node_name):
9364
    """Ensure faulty disks abort the opcode or at least warn."""
9365
    try:
9366
      if _FindFaultyInstanceDisks(self.cfg, self.rpc, instance,
9367
                                  node_name, True):
9368
        raise errors.OpPrereqError("Instance '%s' has faulty disks on"
9369
                                   " node '%s'" % (instance.name, node_name),
9370
                                   errors.ECODE_STATE)
9371
    except errors.OpPrereqError, err:
9372
      if self.op.ignore_consistency:
9373
        self.proc.LogWarning(str(err.args[0]))
9374
      else:
9375
        raise
9376

    
9377
  def CheckPrereq(self):
9378
    """Check prerequisites.
9379

9380
    """
9381
    # Check whether any instance on this node has faulty disks
9382
    for inst in _GetNodeInstances(self.cfg, self.op.node_name):
9383
      if not inst.admin_up:
9384
        continue
9385
      check_nodes = set(inst.all_nodes)
9386
      check_nodes.discard(self.op.node_name)
9387
      for inst_node_name in check_nodes:
9388
        self._CheckFaultyDisks(inst, inst_node_name)
9389

    
9390
  def Exec(self, feedback_fn):
9391
    feedback_fn("Repairing storage unit '%s' on %s ..." %
9392
                (self.op.name, self.op.node_name))
9393

    
9394
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
9395
    result = self.rpc.call_storage_execute(self.op.node_name,
9396
                                           self.op.storage_type, st_args,
9397
                                           self.op.name,
9398
                                           constants.SO_FIX_CONSISTENCY)
9399
    result.Raise("Failed to repair storage unit '%s' on %s" %
9400
                 (self.op.name, self.op.node_name))
9401

    
9402

    
9403
class LUNodeEvacStrategy(NoHooksLU):
9404
  """Computes the node evacuation strategy.
9405

9406
  """
9407
  REQ_BGL = False
9408

    
9409
  def CheckArguments(self):
9410
    _CheckIAllocatorOrNode(self, "iallocator", "remote_node")
9411

    
9412
  def ExpandNames(self):
9413
    self.op.nodes = _GetWantedNodes(self, self.op.nodes)
9414
    self.needed_locks = locks = {}
9415
    if self.op.remote_node is None:
9416
      locks[locking.LEVEL_NODE] = locking.ALL_SET
9417
    else:
9418
      self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
9419
      locks[locking.LEVEL_NODE] = self.op.nodes + [self.op.remote_node]
9420

    
9421
  def Exec(self, feedback_fn):
9422
    if self.op.remote_node is not None:
9423
      instances = []
9424
      for node in self.op.nodes:
9425
        instances.extend(_GetNodeSecondaryInstances(self.cfg, node))
9426
      result = []
9427
      for i in instances:
9428
        if i.primary_node == self.op.remote_node:
9429
          raise errors.OpPrereqError("Node %s is the primary node of"
9430
                                     " instance %s, cannot use it as"
9431
                                     " secondary" %
9432
                                     (self.op.remote_node, i.name),
9433
                                     errors.ECODE_INVAL)
9434
        result.append([i.name, self.op.remote_node])
9435
    else:
9436
      ial = IAllocator(self.cfg, self.rpc,
9437
                       mode=constants.IALLOCATOR_MODE_MEVAC,
9438
                       evac_nodes=self.op.nodes)
9439
      ial.Run(self.op.iallocator, validate=True)
9440
      if not ial.success:
9441
        raise errors.OpExecError("No valid evacuation solution: %s" % ial.info,
9442
                                 errors.ECODE_NORES)
9443
      result = ial.result
9444
    return result
9445

    
9446

    
9447
class LUInstanceGrowDisk(LogicalUnit):
9448
  """Grow a disk of an instance.
9449

9450
  """
9451
  HPATH = "disk-grow"
9452
  HTYPE = constants.HTYPE_INSTANCE
9453
  REQ_BGL = False
9454

    
9455
  def ExpandNames(self):
9456
    self._ExpandAndLockInstance()
9457
    self.needed_locks[locking.LEVEL_NODE] = []
9458
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
9459

    
9460
  def DeclareLocks(self, level):
9461
    if level == locking.LEVEL_NODE:
9462
      self._LockInstancesNodes()
9463

    
9464
  def BuildHooksEnv(self):
9465
    """Build hooks env.
9466

9467
    This runs on the master, the primary and all the secondaries.
9468

9469
    """
9470
    env = {
9471
      "DISK": self.op.disk,
9472
      "AMOUNT": self.op.amount,
9473
      }
9474
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
9475
    return env
9476

    
9477
  def BuildHooksNodes(self):
9478
    """Build hooks nodes.
9479

9480
    """
9481
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
9482
    return (nl, nl)
9483

    
9484
  def CheckPrereq(self):
9485
    """Check prerequisites.
9486

9487
    This checks that the instance is in the cluster.
9488

9489
    """
9490
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
9491
    assert instance is not None, \
9492
      "Cannot retrieve locked instance %s" % self.op.instance_name
9493
    nodenames = list(instance.all_nodes)
9494
    for node in nodenames:
9495
      _CheckNodeOnline(self, node)
9496

    
9497
    self.instance = instance
9498

    
9499
    if instance.disk_template not in constants.DTS_GROWABLE:
9500
      raise errors.OpPrereqError("Instance's disk layout does not support"
9501
                                 " growing", errors.ECODE_INVAL)
9502

    
9503
    self.disk = instance.FindDisk(self.op.disk)
9504

    
9505
    if instance.disk_template not in (constants.DT_FILE,
9506
                                      constants.DT_SHARED_FILE):
9507
      # TODO: check the free disk space for file, when that feature will be
9508
      # supported
9509
      _CheckNodesFreeDiskPerVG(self, nodenames,
9510
                               self.disk.ComputeGrowth(self.op.amount))
9511

    
9512
  def Exec(self, feedback_fn):
9513
    """Execute disk grow.
9514

9515
    """
9516
    instance = self.instance
9517
    disk = self.disk
9518

    
9519
    disks_ok, _ = _AssembleInstanceDisks(self, self.instance, disks=[disk])
9520
    if not disks_ok:
9521
      raise errors.OpExecError("Cannot activate block device to grow")
9522

    
9523
    for node in instance.all_nodes:
9524
      self.cfg.SetDiskID(disk, node)
9525
      result = self.rpc.call_blockdev_grow(node, disk, self.op.amount)
9526
      result.Raise("Grow request failed to node %s" % node)
9527

    
9528
      # TODO: Rewrite code to work properly
9529
      # DRBD goes into sync mode for a short amount of time after executing the
9530
      # "resize" command. DRBD 8.x below version 8.0.13 contains a bug whereby
9531
      # calling "resize" in sync mode fails. Sleeping for a short amount of
9532
      # time is a work-around.
9533
      time.sleep(5)
9534

    
9535
    disk.RecordGrow(self.op.amount)
9536
    self.cfg.Update(instance, feedback_fn)
9537
    if self.op.wait_for_sync:
9538
      disk_abort = not _WaitForSync(self, instance, disks=[disk])
9539
      if disk_abort:
9540
        self.proc.LogWarning("Disk sync-ing has not returned a good"
9541
                             " status; please check the instance")
9542
      if not instance.admin_up:
9543
        _SafeShutdownInstanceDisks(self, instance, disks=[disk])
9544
    elif not instance.admin_up:
9545
      self.proc.LogWarning("Not shutting down the disk even if the instance is"
9546
                           " not supposed to be running because no wait for"
9547
                           " sync mode was requested")
9548

    
9549

    
9550
class LUInstanceQueryData(NoHooksLU):
9551
  """Query runtime instance data.
9552

9553
  """
9554
  REQ_BGL = False
9555

    
9556
  def ExpandNames(self):
9557
    self.needed_locks = {}
9558

    
9559
    # Use locking if requested or when non-static information is wanted
9560
    if not (self.op.static or self.op.use_locking):
9561
      self.LogWarning("Non-static data requested, locks need to be acquired")
9562
      self.op.use_locking = True
9563

    
9564
    if self.op.instances or not self.op.use_locking:
9565
      # Expand instance names right here
9566
      self.wanted_names = _GetWantedInstances(self, self.op.instances)
9567
    else:
9568
      # Will use acquired locks
9569
      self.wanted_names = None
9570

    
9571
    if self.op.use_locking:
9572
      self.share_locks = dict.fromkeys(locking.LEVELS, 1)
9573

    
9574
      if self.wanted_names is None:
9575
        self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
9576
      else:
9577
        self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names
9578

    
9579
      self.needed_locks[locking.LEVEL_NODE] = []
9580
      self.share_locks = dict.fromkeys(locking.LEVELS, 1)
9581
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
9582

    
9583
  def DeclareLocks(self, level):
9584
    if self.op.use_locking and level == locking.LEVEL_NODE:
9585
      self._LockInstancesNodes()
9586

    
9587
  def CheckPrereq(self):
9588
    """Check prerequisites.
9589

9590
    This only checks the optional instance list against the existing names.
9591

9592
    """
9593
    if self.wanted_names is None:
9594
      assert self.op.use_locking, "Locking was not used"
9595
      self.wanted_names = self.glm.list_owned(locking.LEVEL_INSTANCE)
9596

    
9597
    self.wanted_instances = [self.cfg.GetInstanceInfo(name)
9598
                             for name in self.wanted_names]
9599

    
9600
  def _ComputeBlockdevStatus(self, node, instance_name, dev):
9601
    """Returns the status of a block device
9602

9603
    """
9604
    if self.op.static or not node:
9605
      return None
9606

    
9607
    self.cfg.SetDiskID(dev, node)
9608

    
9609
    result = self.rpc.call_blockdev_find(node, dev)
9610
    if result.offline:
9611
      return None
9612

    
9613
    result.Raise("Can't compute disk status for %s" % instance_name)
9614

    
9615
    status = result.payload
9616
    if status is None:
9617
      return None
9618

    
9619
    return (status.dev_path, status.major, status.minor,
9620
            status.sync_percent, status.estimated_time,
9621
            status.is_degraded, status.ldisk_status)
9622

    
9623
  def _ComputeDiskStatus(self, instance, snode, dev):
9624
    """Compute block device status.
9625

9626
    """
9627
    if dev.dev_type in constants.LDS_DRBD:
9628
      # we change the snode then (otherwise we use the one passed in)
9629
      if dev.logical_id[0] == instance.primary_node:
9630
        snode = dev.logical_id[1]
9631
      else:
9632
        snode = dev.logical_id[0]
9633

    
9634
    dev_pstatus = self._ComputeBlockdevStatus(instance.primary_node,
9635
                                              instance.name, dev)
9636
    dev_sstatus = self._ComputeBlockdevStatus(snode, instance.name, dev)
9637

    
9638
    if dev.children:
9639
      dev_children = [self._ComputeDiskStatus(instance, snode, child)
9640
                      for child in dev.children]
9641
    else:
9642
      dev_children = []
9643

    
9644
    return {
9645
      "iv_name": dev.iv_name,
9646
      "dev_type": dev.dev_type,
9647
      "logical_id": dev.logical_id,
9648
      "physical_id": dev.physical_id,
9649
      "pstatus": dev_pstatus,
9650
      "sstatus": dev_sstatus,
9651
      "children": dev_children,
9652
      "mode": dev.mode,
9653
      "size": dev.size,
9654
      }
9655

    
9656
  def Exec(self, feedback_fn):
9657
    """Gather and return data"""
9658
    result = {}
9659

    
9660
    cluster = self.cfg.GetClusterInfo()
9661

    
9662
    for instance in self.wanted_instances:
9663
      if not self.op.static:
9664
        remote_info = self.rpc.call_instance_info(instance.primary_node,
9665
                                                  instance.name,
9666
                                                  instance.hypervisor)
9667
        remote_info.Raise("Error checking node %s" % instance.primary_node)
9668
        remote_info = remote_info.payload
9669
        if remote_info and "state" in remote_info:
9670
          remote_state = "up"
9671
        else:
9672
          remote_state = "down"
9673
      else:
9674
        remote_state = None
9675
      if instance.admin_up:
9676
        config_state = "up"
9677
      else:
9678
        config_state = "down"
9679

    
9680
      disks = [self._ComputeDiskStatus(instance, None, device)
9681
               for device in instance.disks]
9682

    
9683
      result[instance.name] = {
9684
        "name": instance.name,
9685
        "config_state": config_state,
9686
        "run_state": remote_state,
9687
        "pnode": instance.primary_node,
9688
        "snodes": instance.secondary_nodes,
9689
        "os": instance.os,
9690
        # this happens to be the same format used for hooks
9691
        "nics": _NICListToTuple(self, instance.nics),
9692
        "disk_template": instance.disk_template,
9693
        "disks": disks,
9694
        "hypervisor": instance.hypervisor,
9695
        "network_port": instance.network_port,
9696
        "hv_instance": instance.hvparams,
9697
        "hv_actual": cluster.FillHV(instance, skip_globals=True),
9698
        "be_instance": instance.beparams,
9699
        "be_actual": cluster.FillBE(instance),
9700
        "os_instance": instance.osparams,
9701
        "os_actual": cluster.SimpleFillOS(instance.os, instance.osparams),
9702
        "serial_no": instance.serial_no,
9703
        "mtime": instance.mtime,
9704
        "ctime": instance.ctime,
9705
        "uuid": instance.uuid,
9706
        }
9707

    
9708
    return result
9709

    
9710

    
9711
class LUInstanceSetParams(LogicalUnit):
9712
  """Modifies an instances's parameters.
9713

9714
  """
9715
  HPATH = "instance-modify"
9716
  HTYPE = constants.HTYPE_INSTANCE
9717
  REQ_BGL = False
9718

    
9719
  def CheckArguments(self):
9720
    if not (self.op.nics or self.op.disks or self.op.disk_template or
9721
            self.op.hvparams or self.op.beparams or self.op.os_name):
9722
      raise errors.OpPrereqError("No changes submitted", errors.ECODE_INVAL)
9723

    
9724
    if self.op.hvparams:
9725
      _CheckGlobalHvParams(self.op.hvparams)
9726

    
9727
    # Disk validation
9728
    disk_addremove = 0
9729
    for disk_op, disk_dict in self.op.disks:
9730
      utils.ForceDictType(disk_dict, constants.IDISK_PARAMS_TYPES)
9731
      if disk_op == constants.DDM_REMOVE:
9732
        disk_addremove += 1
9733
        continue
9734
      elif disk_op == constants.DDM_ADD:
9735
        disk_addremove += 1
9736
      else:
9737
        if not isinstance(disk_op, int):
9738
          raise errors.OpPrereqError("Invalid disk index", errors.ECODE_INVAL)
9739
        if not isinstance(disk_dict, dict):
9740
          msg = "Invalid disk value: expected dict, got '%s'" % disk_dict
9741
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
9742

    
9743
      if disk_op == constants.DDM_ADD:
9744
        mode = disk_dict.setdefault(constants.IDISK_MODE, constants.DISK_RDWR)
9745
        if mode not in constants.DISK_ACCESS_SET:
9746
          raise errors.OpPrereqError("Invalid disk access mode '%s'" % mode,
9747
                                     errors.ECODE_INVAL)
9748
        size = disk_dict.get(constants.IDISK_SIZE, None)
9749
        if size is None:
9750
          raise errors.OpPrereqError("Required disk parameter size missing",
9751
                                     errors.ECODE_INVAL)
9752
        try:
9753
          size = int(size)
9754
        except (TypeError, ValueError), err:
9755
          raise errors.OpPrereqError("Invalid disk size parameter: %s" %
9756
                                     str(err), errors.ECODE_INVAL)
9757
        disk_dict[constants.IDISK_SIZE] = size
9758
      else:
9759
        # modification of disk
9760
        if constants.IDISK_SIZE in disk_dict:
9761
          raise errors.OpPrereqError("Disk size change not possible, use"
9762
                                     " grow-disk", errors.ECODE_INVAL)
9763

    
9764
    if disk_addremove > 1:
9765
      raise errors.OpPrereqError("Only one disk add or remove operation"
9766
                                 " supported at a time", errors.ECODE_INVAL)
9767

    
9768
    if self.op.disks and self.op.disk_template is not None:
9769
      raise errors.OpPrereqError("Disk template conversion and other disk"
9770
                                 " changes not supported at the same time",
9771
                                 errors.ECODE_INVAL)
9772

    
9773
    if (self.op.disk_template and
9774
        self.op.disk_template in constants.DTS_INT_MIRROR and
9775
        self.op.remote_node is None):
9776
      raise errors.OpPrereqError("Changing the disk template to a mirrored"
9777
                                 " one requires specifying a secondary node",
9778
                                 errors.ECODE_INVAL)
9779

    
9780
    # NIC validation
9781
    nic_addremove = 0
9782
    for nic_op, nic_dict in self.op.nics:
9783
      utils.ForceDictType(nic_dict, constants.INIC_PARAMS_TYPES)
9784
      if nic_op == constants.DDM_REMOVE:
9785
        nic_addremove += 1
9786
        continue
9787
      elif nic_op == constants.DDM_ADD:
9788
        nic_addremove += 1
9789
      else:
9790
        if not isinstance(nic_op, int):
9791
          raise errors.OpPrereqError("Invalid nic index", errors.ECODE_INVAL)
9792
        if not isinstance(nic_dict, dict):
9793
          msg = "Invalid nic value: expected dict, got '%s'" % nic_dict
9794
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
9795

    
9796
      # nic_dict should be a dict
9797
      nic_ip = nic_dict.get(constants.INIC_IP, None)
9798
      if nic_ip is not None:
9799
        if nic_ip.lower() == constants.VALUE_NONE:
9800
          nic_dict[constants.INIC_IP] = None
9801
        else:
9802
          if not netutils.IPAddress.IsValid(nic_ip):
9803
            raise errors.OpPrereqError("Invalid IP address '%s'" % nic_ip,
9804
                                       errors.ECODE_INVAL)
9805

    
9806
      nic_bridge = nic_dict.get('bridge', None)
9807
      nic_link = nic_dict.get(constants.INIC_LINK, None)
9808
      if nic_bridge and nic_link:
9809
        raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'"
9810
                                   " at the same time", errors.ECODE_INVAL)
9811
      elif nic_bridge and nic_bridge.lower() == constants.VALUE_NONE:
9812
        nic_dict['bridge'] = None
9813
      elif nic_link and nic_link.lower() == constants.VALUE_NONE:
9814
        nic_dict[constants.INIC_LINK] = None
9815

    
9816
      if nic_op == constants.DDM_ADD:
9817
        nic_mac = nic_dict.get(constants.INIC_MAC, None)
9818
        if nic_mac is None:
9819
          nic_dict[constants.INIC_MAC] = constants.VALUE_AUTO
9820

    
9821
      if constants.INIC_MAC in nic_dict:
9822
        nic_mac = nic_dict[constants.INIC_MAC]
9823
        if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
9824
          nic_mac = utils.NormalizeAndValidateMac(nic_mac)
9825

    
9826
        if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO:
9827
          raise errors.OpPrereqError("'auto' is not a valid MAC address when"
9828
                                     " modifying an existing nic",
9829
                                     errors.ECODE_INVAL)
9830

    
9831
    if nic_addremove > 1:
9832
      raise errors.OpPrereqError("Only one NIC add or remove operation"
9833
                                 " supported at a time", errors.ECODE_INVAL)
9834

    
9835
  def ExpandNames(self):
9836
    self._ExpandAndLockInstance()
9837
    self.needed_locks[locking.LEVEL_NODE] = []
9838
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
9839

    
9840
  def DeclareLocks(self, level):
9841
    if level == locking.LEVEL_NODE:
9842
      self._LockInstancesNodes()
9843
      if self.op.disk_template and self.op.remote_node:
9844
        self.op.remote_node = _ExpandNodeName(self.cfg, self.op.remote_node)
9845
        self.needed_locks[locking.LEVEL_NODE].append(self.op.remote_node)
9846

    
9847
  def BuildHooksEnv(self):
9848
    """Build hooks env.
9849

9850
    This runs on the master, primary and secondaries.
9851

9852
    """
9853
    args = dict()
9854
    if constants.BE_MEMORY in self.be_new:
9855
      args['memory'] = self.be_new[constants.BE_MEMORY]
9856
    if constants.BE_VCPUS in self.be_new:
9857
      args['vcpus'] = self.be_new[constants.BE_VCPUS]
9858
    # TODO: export disk changes. Note: _BuildInstanceHookEnv* don't export disk
9859
    # information at all.
9860
    if self.op.nics:
9861
      args['nics'] = []
9862
      nic_override = dict(self.op.nics)
9863
      for idx, nic in enumerate(self.instance.nics):
9864
        if idx in nic_override:
9865
          this_nic_override = nic_override[idx]
9866
        else:
9867
          this_nic_override = {}
9868
        if constants.INIC_IP in this_nic_override:
9869
          ip = this_nic_override[constants.INIC_IP]
9870
        else:
9871
          ip = nic.ip
9872
        if constants.INIC_MAC in this_nic_override:
9873
          mac = this_nic_override[constants.INIC_MAC]
9874
        else:
9875
          mac = nic.mac
9876
        if idx in self.nic_pnew:
9877
          nicparams = self.nic_pnew[idx]
9878
        else:
9879
          nicparams = self.cluster.SimpleFillNIC(nic.nicparams)
9880
        mode = nicparams[constants.NIC_MODE]
9881
        link = nicparams[constants.NIC_LINK]
9882
        args['nics'].append((ip, mac, mode, link))
9883
      if constants.DDM_ADD in nic_override:
9884
        ip = nic_override[constants.DDM_ADD].get(constants.INIC_IP, None)
9885
        mac = nic_override[constants.DDM_ADD][constants.INIC_MAC]
9886
        nicparams = self.nic_pnew[constants.DDM_ADD]
9887
        mode = nicparams[constants.NIC_MODE]
9888
        link = nicparams[constants.NIC_LINK]
9889
        args['nics'].append((ip, mac, mode, link))
9890
      elif constants.DDM_REMOVE in nic_override:
9891
        del args['nics'][-1]
9892

    
9893
    env = _BuildInstanceHookEnvByObject(self, self.instance, override=args)
9894
    if self.op.disk_template:
9895
      env["NEW_DISK_TEMPLATE"] = self.op.disk_template
9896

    
9897
    return env
9898

    
9899
  def BuildHooksNodes(self):
9900
    """Build hooks nodes.
9901

9902
    """
9903
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
9904
    return (nl, nl)
9905

    
9906
  def CheckPrereq(self):
9907
    """Check prerequisites.
9908

9909
    This only checks the instance list against the existing names.
9910

9911
    """
9912
    # checking the new params on the primary/secondary nodes
9913

    
9914
    instance = self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
9915
    cluster = self.cluster = self.cfg.GetClusterInfo()
9916
    assert self.instance is not None, \
9917
      "Cannot retrieve locked instance %s" % self.op.instance_name
9918
    pnode = instance.primary_node
9919
    nodelist = list(instance.all_nodes)
9920

    
9921
    # OS change
9922
    if self.op.os_name and not self.op.force:
9923
      _CheckNodeHasOS(self, instance.primary_node, self.op.os_name,
9924
                      self.op.force_variant)
9925
      instance_os = self.op.os_name
9926
    else:
9927
      instance_os = instance.os
9928

    
9929
    if self.op.disk_template:
9930
      if instance.disk_template == self.op.disk_template:
9931
        raise errors.OpPrereqError("Instance already has disk template %s" %
9932
                                   instance.disk_template, errors.ECODE_INVAL)
9933

    
9934
      if (instance.disk_template,
9935
          self.op.disk_template) not in self._DISK_CONVERSIONS:
9936
        raise errors.OpPrereqError("Unsupported disk template conversion from"
9937
                                   " %s to %s" % (instance.disk_template,
9938
                                                  self.op.disk_template),
9939
                                   errors.ECODE_INVAL)
9940
      _CheckInstanceDown(self, instance, "cannot change disk template")
9941
      if self.op.disk_template in constants.DTS_INT_MIRROR:
9942
        if self.op.remote_node == pnode:
9943
          raise errors.OpPrereqError("Given new secondary node %s is the same"
9944
                                     " as the primary node of the instance" %
9945
                                     self.op.remote_node, errors.ECODE_STATE)
9946
        _CheckNodeOnline(self, self.op.remote_node)
9947
        _CheckNodeNotDrained(self, self.op.remote_node)
9948
        # FIXME: here we assume that the old instance type is DT_PLAIN
9949
        assert instance.disk_template == constants.DT_PLAIN
9950
        disks = [{constants.IDISK_SIZE: d.size,
9951
                  constants.IDISK_VG: d.logical_id[0]}
9952
                 for d in instance.disks]
9953
        required = _ComputeDiskSizePerVG(self.op.disk_template, disks)
9954
        _CheckNodesFreeDiskPerVG(self, [self.op.remote_node], required)
9955

    
9956
    # hvparams processing
9957
    if self.op.hvparams:
9958
      hv_type = instance.hypervisor
9959
      i_hvdict = _GetUpdatedParams(instance.hvparams, self.op.hvparams)
9960
      utils.ForceDictType(i_hvdict, constants.HVS_PARAMETER_TYPES)
9961
      hv_new = cluster.SimpleFillHV(hv_type, instance.os, i_hvdict)
9962

    
9963
      # local check
9964
      hypervisor.GetHypervisor(hv_type).CheckParameterSyntax(hv_new)
9965
      _CheckHVParams(self, nodelist, instance.hypervisor, hv_new)
9966
      self.hv_new = hv_new # the new actual values
9967
      self.hv_inst = i_hvdict # the new dict (without defaults)
9968
    else:
9969
      self.hv_new = self.hv_inst = {}
9970

    
9971
    # beparams processing
9972
    if self.op.beparams:
9973
      i_bedict = _GetUpdatedParams(instance.beparams, self.op.beparams,
9974
                                   use_none=True)
9975
      utils.ForceDictType(i_bedict, constants.BES_PARAMETER_TYPES)
9976
      be_new = cluster.SimpleFillBE(i_bedict)
9977
      self.be_new = be_new # the new actual values
9978
      self.be_inst = i_bedict # the new dict (without defaults)
9979
    else:
9980
      self.be_new = self.be_inst = {}
9981

    
9982
    # osparams processing
9983
    if self.op.osparams:
9984
      i_osdict = _GetUpdatedParams(instance.osparams, self.op.osparams)
9985
      _CheckOSParams(self, True, nodelist, instance_os, i_osdict)
9986
      self.os_inst = i_osdict # the new dict (without defaults)
9987
    else:
9988
      self.os_inst = {}
9989

    
9990
    self.warn = []
9991

    
9992
    if constants.BE_MEMORY in self.op.beparams and not self.op.force:
9993
      mem_check_list = [pnode]
9994
      if be_new[constants.BE_AUTO_BALANCE]:
9995
        # either we changed auto_balance to yes or it was from before
9996
        mem_check_list.extend(instance.secondary_nodes)
9997
      instance_info = self.rpc.call_instance_info(pnode, instance.name,
9998
                                                  instance.hypervisor)
9999
      nodeinfo = self.rpc.call_node_info(mem_check_list, None,
10000
                                         instance.hypervisor)
10001
      pninfo = nodeinfo[pnode]
10002
      msg = pninfo.fail_msg
10003
      if msg:
10004
        # Assume the primary node is unreachable and go ahead
10005
        self.warn.append("Can't get info from primary node %s: %s" %
10006
                         (pnode,  msg))
10007
      elif not isinstance(pninfo.payload.get('memory_free', None), int):
10008
        self.warn.append("Node data from primary node %s doesn't contain"
10009
                         " free memory information" % pnode)
10010
      elif instance_info.fail_msg:
10011
        self.warn.append("Can't get instance runtime information: %s" %
10012
                        instance_info.fail_msg)
10013
      else:
10014
        if instance_info.payload:
10015
          current_mem = int(instance_info.payload['memory'])
10016
        else:
10017
          # Assume instance not running
10018
          # (there is a slight race condition here, but it's not very probable,
10019
          # and we have no other way to check)
10020
          current_mem = 0
10021
        miss_mem = (be_new[constants.BE_MEMORY] - current_mem -
10022
                    pninfo.payload['memory_free'])
10023
        if miss_mem > 0:
10024
          raise errors.OpPrereqError("This change will prevent the instance"
10025
                                     " from starting, due to %d MB of memory"
10026
                                     " missing on its primary node" % miss_mem,
10027
                                     errors.ECODE_NORES)
10028

    
10029
      if be_new[constants.BE_AUTO_BALANCE]:
10030
        for node, nres in nodeinfo.items():
10031
          if node not in instance.secondary_nodes:
10032
            continue
10033
          msg = nres.fail_msg
10034
          if msg:
10035
            self.warn.append("Can't get info from secondary node %s: %s" %
10036
                             (node, msg))
10037
          elif not isinstance(nres.payload.get('memory_free', None), int):
10038
            self.warn.append("Secondary node %s didn't return free"
10039
                             " memory information" % node)
10040
          elif be_new[constants.BE_MEMORY] > nres.payload['memory_free']:
10041
            self.warn.append("Not enough memory to failover instance to"
10042
                             " secondary node %s" % node)
10043

    
10044
    # NIC processing
10045
    self.nic_pnew = {}
10046
    self.nic_pinst = {}
10047
    for nic_op, nic_dict in self.op.nics:
10048
      if nic_op == constants.DDM_REMOVE:
10049
        if not instance.nics:
10050
          raise errors.OpPrereqError("Instance has no NICs, cannot remove",
10051
                                     errors.ECODE_INVAL)
10052
        continue
10053
      if nic_op != constants.DDM_ADD:
10054
        # an existing nic
10055
        if not instance.nics:
10056
          raise errors.OpPrereqError("Invalid NIC index %s, instance has"
10057
                                     " no NICs" % nic_op,
10058
                                     errors.ECODE_INVAL)
10059
        if nic_op < 0 or nic_op >= len(instance.nics):
10060
          raise errors.OpPrereqError("Invalid NIC index %s, valid values"
10061
                                     " are 0 to %d" %
10062
                                     (nic_op, len(instance.nics) - 1),
10063
                                     errors.ECODE_INVAL)
10064
        old_nic_params = instance.nics[nic_op].nicparams
10065
        old_nic_ip = instance.nics[nic_op].ip
10066
      else:
10067
        old_nic_params = {}
10068
        old_nic_ip = None
10069

    
10070
      update_params_dict = dict([(key, nic_dict[key])
10071
                                 for key in constants.NICS_PARAMETERS
10072
                                 if key in nic_dict])
10073

    
10074
      if 'bridge' in nic_dict:
10075
        update_params_dict[constants.NIC_LINK] = nic_dict['bridge']
10076

    
10077
      new_nic_params = _GetUpdatedParams(old_nic_params,
10078
                                         update_params_dict)
10079
      utils.ForceDictType(new_nic_params, constants.NICS_PARAMETER_TYPES)
10080
      new_filled_nic_params = cluster.SimpleFillNIC(new_nic_params)
10081
      objects.NIC.CheckParameterSyntax(new_filled_nic_params)
10082
      self.nic_pinst[nic_op] = new_nic_params
10083
      self.nic_pnew[nic_op] = new_filled_nic_params
10084
      new_nic_mode = new_filled_nic_params[constants.NIC_MODE]
10085

    
10086
      if new_nic_mode == constants.NIC_MODE_BRIDGED:
10087
        nic_bridge = new_filled_nic_params[constants.NIC_LINK]
10088
        msg = self.rpc.call_bridges_exist(pnode, [nic_bridge]).fail_msg
10089
        if msg:
10090
          msg = "Error checking bridges on node %s: %s" % (pnode, msg)
10091
          if self.op.force:
10092
            self.warn.append(msg)
10093
          else:
10094
            raise errors.OpPrereqError(msg, errors.ECODE_ENVIRON)
10095
      if new_nic_mode == constants.NIC_MODE_ROUTED:
10096
        if constants.INIC_IP in nic_dict:
10097
          nic_ip = nic_dict[constants.INIC_IP]
10098
        else:
10099
          nic_ip = old_nic_ip
10100
        if nic_ip is None:
10101
          raise errors.OpPrereqError('Cannot set the nic ip to None'
10102
                                     ' on a routed nic', errors.ECODE_INVAL)
10103
      if constants.INIC_MAC in nic_dict:
10104
        nic_mac = nic_dict[constants.INIC_MAC]
10105
        if nic_mac is None:
10106
          raise errors.OpPrereqError('Cannot set the nic mac to None',
10107
                                     errors.ECODE_INVAL)
10108
        elif nic_mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
10109
          # otherwise generate the mac
10110
          nic_dict[constants.INIC_MAC] = \
10111
            self.cfg.GenerateMAC(self.proc.GetECId())
10112
        else:
10113
          # or validate/reserve the current one
10114
          try:
10115
            self.cfg.ReserveMAC(nic_mac, self.proc.GetECId())
10116
          except errors.ReservationError:
10117
            raise errors.OpPrereqError("MAC address %s already in use"
10118
                                       " in cluster" % nic_mac,
10119
                                       errors.ECODE_NOTUNIQUE)
10120

    
10121
    # DISK processing
10122
    if self.op.disks and instance.disk_template == constants.DT_DISKLESS:
10123
      raise errors.OpPrereqError("Disk operations not supported for"
10124
                                 " diskless instances",
10125
                                 errors.ECODE_INVAL)
10126
    for disk_op, _ in self.op.disks:
10127
      if disk_op == constants.DDM_REMOVE:
10128
        if len(instance.disks) == 1:
10129
          raise errors.OpPrereqError("Cannot remove the last disk of"
10130
                                     " an instance", errors.ECODE_INVAL)
10131
        _CheckInstanceDown(self, instance, "cannot remove disks")
10132

    
10133
      if (disk_op == constants.DDM_ADD and
10134
          len(instance.disks) >= constants.MAX_DISKS):
10135
        raise errors.OpPrereqError("Instance has too many disks (%d), cannot"
10136
                                   " add more" % constants.MAX_DISKS,
10137
                                   errors.ECODE_STATE)
10138
      if disk_op not in (constants.DDM_ADD, constants.DDM_REMOVE):
10139
        # an existing disk
10140
        if disk_op < 0 or disk_op >= len(instance.disks):
10141
          raise errors.OpPrereqError("Invalid disk index %s, valid values"
10142
                                     " are 0 to %d" %
10143
                                     (disk_op, len(instance.disks)),
10144
                                     errors.ECODE_INVAL)
10145

    
10146
    return
10147

    
10148
  def _ConvertPlainToDrbd(self, feedback_fn):
10149
    """Converts an instance from plain to drbd.
10150

10151
    """
10152
    feedback_fn("Converting template to drbd")
10153
    instance = self.instance
10154
    pnode = instance.primary_node
10155
    snode = self.op.remote_node
10156

    
10157
    # create a fake disk info for _GenerateDiskTemplate
10158
    disk_info = [{constants.IDISK_SIZE: d.size, constants.IDISK_MODE: d.mode,
10159
                  constants.IDISK_VG: d.logical_id[0]}
10160
                 for d in instance.disks]
10161
    new_disks = _GenerateDiskTemplate(self, self.op.disk_template,
10162
                                      instance.name, pnode, [snode],
10163
                                      disk_info, None, None, 0, feedback_fn)
10164
    info = _GetInstanceInfoText(instance)
10165
    feedback_fn("Creating aditional volumes...")
10166
    # first, create the missing data and meta devices
10167
    for disk in new_disks:
10168
      # unfortunately this is... not too nice
10169
      _CreateSingleBlockDev(self, pnode, instance, disk.children[1],
10170
                            info, True)
10171
      for child in disk.children:
10172
        _CreateSingleBlockDev(self, snode, instance, child, info, True)
10173
    # at this stage, all new LVs have been created, we can rename the
10174
    # old ones
10175
    feedback_fn("Renaming original volumes...")
10176
    rename_list = [(o, n.children[0].logical_id)
10177
                   for (o, n) in zip(instance.disks, new_disks)]
10178
    result = self.rpc.call_blockdev_rename(pnode, rename_list)
10179
    result.Raise("Failed to rename original LVs")
10180

    
10181
    feedback_fn("Initializing DRBD devices...")
10182
    # all child devices are in place, we can now create the DRBD devices
10183
    for disk in new_disks:
10184
      for node in [pnode, snode]:
10185
        f_create = node == pnode
10186
        _CreateSingleBlockDev(self, node, instance, disk, info, f_create)
10187

    
10188
    # at this point, the instance has been modified
10189
    instance.disk_template = constants.DT_DRBD8
10190
    instance.disks = new_disks
10191
    self.cfg.Update(instance, feedback_fn)
10192

    
10193
    # disks are created, waiting for sync
10194
    disk_abort = not _WaitForSync(self, instance)
10195
    if disk_abort:
10196
      raise errors.OpExecError("There are some degraded disks for"
10197
                               " this instance, please cleanup manually")
10198

    
10199
  def _ConvertDrbdToPlain(self, feedback_fn):
10200
    """Converts an instance from drbd to plain.
10201

10202
    """
10203
    instance = self.instance
10204
    assert len(instance.secondary_nodes) == 1
10205
    pnode = instance.primary_node
10206
    snode = instance.secondary_nodes[0]
10207
    feedback_fn("Converting template to plain")
10208

    
10209
    old_disks = instance.disks
10210
    new_disks = [d.children[0] for d in old_disks]
10211

    
10212
    # copy over size and mode
10213
    for parent, child in zip(old_disks, new_disks):
10214
      child.size = parent.size
10215
      child.mode = parent.mode
10216

    
10217
    # update instance structure
10218
    instance.disks = new_disks
10219
    instance.disk_template = constants.DT_PLAIN
10220
    self.cfg.Update(instance, feedback_fn)
10221

    
10222
    feedback_fn("Removing volumes on the secondary node...")
10223
    for disk in old_disks:
10224
      self.cfg.SetDiskID(disk, snode)
10225
      msg = self.rpc.call_blockdev_remove(snode, disk).fail_msg
10226
      if msg:
10227
        self.LogWarning("Could not remove block device %s on node %s,"
10228
                        " continuing anyway: %s", disk.iv_name, snode, msg)
10229

    
10230
    feedback_fn("Removing unneeded volumes on the primary node...")
10231
    for idx, disk in enumerate(old_disks):
10232
      meta = disk.children[1]
10233
      self.cfg.SetDiskID(meta, pnode)
10234
      msg = self.rpc.call_blockdev_remove(pnode, meta).fail_msg
10235
      if msg:
10236
        self.LogWarning("Could not remove metadata for disk %d on node %s,"
10237
                        " continuing anyway: %s", idx, pnode, msg)
10238

    
10239
  def Exec(self, feedback_fn):
10240
    """Modifies an instance.
10241

10242
    All parameters take effect only at the next restart of the instance.
10243

10244
    """
10245
    # Process here the warnings from CheckPrereq, as we don't have a
10246
    # feedback_fn there.
10247
    for warn in self.warn:
10248
      feedback_fn("WARNING: %s" % warn)
10249

    
10250
    result = []
10251
    instance = self.instance
10252
    # disk changes
10253
    for disk_op, disk_dict in self.op.disks:
10254
      if disk_op == constants.DDM_REMOVE:
10255
        # remove the last disk
10256
        device = instance.disks.pop()
10257
        device_idx = len(instance.disks)
10258
        for node, disk in device.ComputeNodeTree(instance.primary_node):
10259
          self.cfg.SetDiskID(disk, node)
10260
          msg = self.rpc.call_blockdev_remove(node, disk).fail_msg
10261
          if msg:
10262
            self.LogWarning("Could not remove disk/%d on node %s: %s,"
10263
                            " continuing anyway", device_idx, node, msg)
10264
        result.append(("disk/%d" % device_idx, "remove"))
10265
      elif disk_op == constants.DDM_ADD:
10266
        # add a new disk
10267
        if instance.disk_template in (constants.DT_FILE,
10268
                                        constants.DT_SHARED_FILE):
10269
          file_driver, file_path = instance.disks[0].logical_id
10270
          file_path = os.path.dirname(file_path)
10271
        else:
10272
          file_driver = file_path = None
10273
        disk_idx_base = len(instance.disks)
10274
        new_disk = _GenerateDiskTemplate(self,
10275
                                         instance.disk_template,
10276
                                         instance.name, instance.primary_node,
10277
                                         instance.secondary_nodes,
10278
                                         [disk_dict],
10279
                                         file_path,
10280
                                         file_driver,
10281
                                         disk_idx_base, feedback_fn)[0]
10282
        instance.disks.append(new_disk)
10283
        info = _GetInstanceInfoText(instance)
10284

    
10285
        logging.info("Creating volume %s for instance %s",
10286
                     new_disk.iv_name, instance.name)
10287
        # Note: this needs to be kept in sync with _CreateDisks
10288
        #HARDCODE
10289
        for node in instance.all_nodes:
10290
          f_create = node == instance.primary_node
10291
          try:
10292
            _CreateBlockDev(self, node, instance, new_disk,
10293
                            f_create, info, f_create)
10294
          except errors.OpExecError, err:
10295
            self.LogWarning("Failed to create volume %s (%s) on"
10296
                            " node %s: %s",
10297
                            new_disk.iv_name, new_disk, node, err)
10298
        result.append(("disk/%d" % disk_idx_base, "add:size=%s,mode=%s" %
10299
                       (new_disk.size, new_disk.mode)))
10300
      else:
10301
        # change a given disk
10302
        instance.disks[disk_op].mode = disk_dict[constants.IDISK_MODE]
10303
        result.append(("disk.mode/%d" % disk_op,
10304
                       disk_dict[constants.IDISK_MODE]))
10305

    
10306
    if self.op.disk_template:
10307
      r_shut = _ShutdownInstanceDisks(self, instance)
10308
      if not r_shut:
10309
        raise errors.OpExecError("Cannot shutdown instance disks, unable to"
10310
                                 " proceed with disk template conversion")
10311
      mode = (instance.disk_template, self.op.disk_template)
10312
      try:
10313
        self._DISK_CONVERSIONS[mode](self, feedback_fn)
10314
      except:
10315
        self.cfg.ReleaseDRBDMinors(instance.name)
10316
        raise
10317
      result.append(("disk_template", self.op.disk_template))
10318

    
10319
    # NIC changes
10320
    for nic_op, nic_dict in self.op.nics:
10321
      if nic_op == constants.DDM_REMOVE:
10322
        # remove the last nic
10323
        del instance.nics[-1]
10324
        result.append(("nic.%d" % len(instance.nics), "remove"))
10325
      elif nic_op == constants.DDM_ADD:
10326
        # mac and bridge should be set, by now
10327
        mac = nic_dict[constants.INIC_MAC]
10328
        ip = nic_dict.get(constants.INIC_IP, None)
10329
        nicparams = self.nic_pinst[constants.DDM_ADD]
10330
        new_nic = objects.NIC(mac=mac, ip=ip, nicparams=nicparams)
10331
        instance.nics.append(new_nic)
10332
        result.append(("nic.%d" % (len(instance.nics) - 1),
10333
                       "add:mac=%s,ip=%s,mode=%s,link=%s" %
10334
                       (new_nic.mac, new_nic.ip,
10335
                        self.nic_pnew[constants.DDM_ADD][constants.NIC_MODE],
10336
                        self.nic_pnew[constants.DDM_ADD][constants.NIC_LINK]
10337
                       )))
10338
      else:
10339
        for key in (constants.INIC_MAC, constants.INIC_IP):
10340
          if key in nic_dict:
10341
            setattr(instance.nics[nic_op], key, nic_dict[key])
10342
        if nic_op in self.nic_pinst:
10343
          instance.nics[nic_op].nicparams = self.nic_pinst[nic_op]
10344
        for key, val in nic_dict.iteritems():
10345
          result.append(("nic.%s/%d" % (key, nic_op), val))
10346

    
10347
    # hvparams changes
10348
    if self.op.hvparams:
10349
      instance.hvparams = self.hv_inst
10350
      for key, val in self.op.hvparams.iteritems():
10351
        result.append(("hv/%s" % key, val))
10352

    
10353
    # beparams changes
10354
    if self.op.beparams:
10355
      instance.beparams = self.be_inst
10356
      for key, val in self.op.beparams.iteritems():
10357
        result.append(("be/%s" % key, val))
10358

    
10359
    # OS change
10360
    if self.op.os_name:
10361
      instance.os = self.op.os_name
10362

    
10363
    # osparams changes
10364
    if self.op.osparams:
10365
      instance.osparams = self.os_inst
10366
      for key, val in self.op.osparams.iteritems():
10367
        result.append(("os/%s" % key, val))
10368

    
10369
    self.cfg.Update(instance, feedback_fn)
10370

    
10371
    return result
10372

    
10373
  _DISK_CONVERSIONS = {
10374
    (constants.DT_PLAIN, constants.DT_DRBD8): _ConvertPlainToDrbd,
10375
    (constants.DT_DRBD8, constants.DT_PLAIN): _ConvertDrbdToPlain,
10376
    }
10377

    
10378

    
10379
class LUBackupQuery(NoHooksLU):
10380
  """Query the exports list
10381

10382
  """
10383
  REQ_BGL = False
10384

    
10385
  def ExpandNames(self):
10386
    self.needed_locks = {}
10387
    self.share_locks[locking.LEVEL_NODE] = 1
10388
    if not self.op.nodes:
10389
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
10390
    else:
10391
      self.needed_locks[locking.LEVEL_NODE] = \
10392
        _GetWantedNodes(self, self.op.nodes)
10393

    
10394
  def Exec(self, feedback_fn):
10395
    """Compute the list of all the exported system images.
10396

10397
    @rtype: dict
10398
    @return: a dictionary with the structure node->(export-list)
10399
        where export-list is a list of the instances exported on
10400
        that node.
10401

10402
    """
10403
    self.nodes = self.glm.list_owned(locking.LEVEL_NODE)
10404
    rpcresult = self.rpc.call_export_list(self.nodes)
10405
    result = {}
10406
    for node in rpcresult:
10407
      if rpcresult[node].fail_msg:
10408
        result[node] = False
10409
      else:
10410
        result[node] = rpcresult[node].payload
10411

    
10412
    return result
10413

    
10414

    
10415
class LUBackupPrepare(NoHooksLU):
10416
  """Prepares an instance for an export and returns useful information.
10417

10418
  """
10419
  REQ_BGL = False
10420

    
10421
  def ExpandNames(self):
10422
    self._ExpandAndLockInstance()
10423

    
10424
  def CheckPrereq(self):
10425
    """Check prerequisites.
10426

10427
    """
10428
    instance_name = self.op.instance_name
10429

    
10430
    self.instance = self.cfg.GetInstanceInfo(instance_name)
10431
    assert self.instance is not None, \
10432
          "Cannot retrieve locked instance %s" % self.op.instance_name
10433
    _CheckNodeOnline(self, self.instance.primary_node)
10434

    
10435
    self._cds = _GetClusterDomainSecret()
10436

    
10437
  def Exec(self, feedback_fn):
10438
    """Prepares an instance for an export.
10439

10440
    """
10441
    instance = self.instance
10442

    
10443
    if self.op.mode == constants.EXPORT_MODE_REMOTE:
10444
      salt = utils.GenerateSecret(8)
10445

    
10446
      feedback_fn("Generating X509 certificate on %s" % instance.primary_node)
10447
      result = self.rpc.call_x509_cert_create(instance.primary_node,
10448
                                              constants.RIE_CERT_VALIDITY)
10449
      result.Raise("Can't create X509 key and certificate on %s" % result.node)
10450

    
10451
      (name, cert_pem) = result.payload
10452

    
10453
      cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
10454
                                             cert_pem)
10455

    
10456
      return {
10457
        "handshake": masterd.instance.ComputeRemoteExportHandshake(self._cds),
10458
        "x509_key_name": (name, utils.Sha1Hmac(self._cds, name, salt=salt),
10459
                          salt),
10460
        "x509_ca": utils.SignX509Certificate(cert, self._cds, salt),
10461
        }
10462

    
10463
    return None
10464

    
10465

    
10466
class LUBackupExport(LogicalUnit):
10467
  """Export an instance to an image in the cluster.
10468

10469
  """
10470
  HPATH = "instance-export"
10471
  HTYPE = constants.HTYPE_INSTANCE
10472
  REQ_BGL = False
10473

    
10474
  def CheckArguments(self):
10475
    """Check the arguments.
10476

10477
    """
10478
    self.x509_key_name = self.op.x509_key_name
10479
    self.dest_x509_ca_pem = self.op.destination_x509_ca
10480

    
10481
    if self.op.mode == constants.EXPORT_MODE_REMOTE:
10482
      if not self.x509_key_name:
10483
        raise errors.OpPrereqError("Missing X509 key name for encryption",
10484
                                   errors.ECODE_INVAL)
10485

    
10486
      if not self.dest_x509_ca_pem:
10487
        raise errors.OpPrereqError("Missing destination X509 CA",
10488
                                   errors.ECODE_INVAL)
10489

    
10490
  def ExpandNames(self):
10491
    self._ExpandAndLockInstance()
10492

    
10493
    # Lock all nodes for local exports
10494
    if self.op.mode == constants.EXPORT_MODE_LOCAL:
10495
      # FIXME: lock only instance primary and destination node
10496
      #
10497
      # Sad but true, for now we have do lock all nodes, as we don't know where
10498
      # the previous export might be, and in this LU we search for it and
10499
      # remove it from its current node. In the future we could fix this by:
10500
      #  - making a tasklet to search (share-lock all), then create the
10501
      #    new one, then one to remove, after
10502
      #  - removing the removal operation altogether
10503
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
10504

    
10505
  def DeclareLocks(self, level):
10506
    """Last minute lock declaration."""
10507
    # All nodes are locked anyway, so nothing to do here.
10508

    
10509
  def BuildHooksEnv(self):
10510
    """Build hooks env.
10511

10512
    This will run on the master, primary node and target node.
10513

10514
    """
10515
    env = {
10516
      "EXPORT_MODE": self.op.mode,
10517
      "EXPORT_NODE": self.op.target_node,
10518
      "EXPORT_DO_SHUTDOWN": self.op.shutdown,
10519
      "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
10520
      # TODO: Generic function for boolean env variables
10521
      "REMOVE_INSTANCE": str(bool(self.op.remove_instance)),
10522
      }
10523

    
10524
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
10525

    
10526
    return env
10527

    
10528
  def BuildHooksNodes(self):
10529
    """Build hooks nodes.
10530

10531
    """
10532
    nl = [self.cfg.GetMasterNode(), self.instance.primary_node]
10533

    
10534
    if self.op.mode == constants.EXPORT_MODE_LOCAL:
10535
      nl.append(self.op.target_node)
10536

    
10537
    return (nl, nl)
10538

    
10539
  def CheckPrereq(self):
10540
    """Check prerequisites.
10541

10542
    This checks that the instance and node names are valid.
10543

10544
    """
10545
    instance_name = self.op.instance_name
10546

    
10547
    self.instance = self.cfg.GetInstanceInfo(instance_name)
10548
    assert self.instance is not None, \
10549
          "Cannot retrieve locked instance %s" % self.op.instance_name
10550
    _CheckNodeOnline(self, self.instance.primary_node)
10551

    
10552
    if (self.op.remove_instance and self.instance.admin_up and
10553
        not self.op.shutdown):
10554
      raise errors.OpPrereqError("Can not remove instance without shutting it"
10555
                                 " down before")
10556

    
10557
    if self.op.mode == constants.EXPORT_MODE_LOCAL:
10558
      self.op.target_node = _ExpandNodeName(self.cfg, self.op.target_node)
10559
      self.dst_node = self.cfg.GetNodeInfo(self.op.target_node)
10560
      assert self.dst_node is not None
10561

    
10562
      _CheckNodeOnline(self, self.dst_node.name)
10563
      _CheckNodeNotDrained(self, self.dst_node.name)
10564

    
10565
      self._cds = None
10566
      self.dest_disk_info = None
10567
      self.dest_x509_ca = None
10568

    
10569
    elif self.op.mode == constants.EXPORT_MODE_REMOTE:
10570
      self.dst_node = None
10571

    
10572
      if len(self.op.target_node) != len(self.instance.disks):
10573
        raise errors.OpPrereqError(("Received destination information for %s"
10574
                                    " disks, but instance %s has %s disks") %
10575
                                   (len(self.op.target_node), instance_name,
10576
                                    len(self.instance.disks)),
10577
                                   errors.ECODE_INVAL)
10578

    
10579
      cds = _GetClusterDomainSecret()
10580

    
10581
      # Check X509 key name
10582
      try:
10583
        (key_name, hmac_digest, hmac_salt) = self.x509_key_name
10584
      except (TypeError, ValueError), err:
10585
        raise errors.OpPrereqError("Invalid data for X509 key name: %s" % err)
10586

    
10587
      if not utils.VerifySha1Hmac(cds, key_name, hmac_digest, salt=hmac_salt):
10588
        raise errors.OpPrereqError("HMAC for X509 key name is wrong",
10589
                                   errors.ECODE_INVAL)
10590

    
10591
      # Load and verify CA
10592
      try:
10593
        (cert, _) = utils.LoadSignedX509Certificate(self.dest_x509_ca_pem, cds)
10594
      except OpenSSL.crypto.Error, err:
10595
        raise errors.OpPrereqError("Unable to load destination X509 CA (%s)" %
10596
                                   (err, ), errors.ECODE_INVAL)
10597

    
10598
      (errcode, msg) = utils.VerifyX509Certificate(cert, None, None)
10599
      if errcode is not None:
10600
        raise errors.OpPrereqError("Invalid destination X509 CA (%s)" %
10601
                                   (msg, ), errors.ECODE_INVAL)
10602

    
10603
      self.dest_x509_ca = cert
10604

    
10605
      # Verify target information
10606
      disk_info = []
10607
      for idx, disk_data in enumerate(self.op.target_node):
10608
        try:
10609
          (host, port, magic) = \
10610
            masterd.instance.CheckRemoteExportDiskInfo(cds, idx, disk_data)
10611
        except errors.GenericError, err:
10612
          raise errors.OpPrereqError("Target info for disk %s: %s" %
10613
                                     (idx, err), errors.ECODE_INVAL)
10614

    
10615
        disk_info.append((host, port, magic))
10616

    
10617
      assert len(disk_info) == len(self.op.target_node)
10618
      self.dest_disk_info = disk_info
10619

    
10620
    else:
10621
      raise errors.ProgrammerError("Unhandled export mode %r" %
10622
                                   self.op.mode)
10623

    
10624
    # instance disk type verification
10625
    # TODO: Implement export support for file-based disks
10626
    for disk in self.instance.disks:
10627
      if disk.dev_type == constants.LD_FILE:
10628
        raise errors.OpPrereqError("Export not supported for instances with"
10629
                                   " file-based disks", errors.ECODE_INVAL)
10630

    
10631
  def _CleanupExports(self, feedback_fn):
10632
    """Removes exports of current instance from all other nodes.
10633

10634
    If an instance in a cluster with nodes A..D was exported to node C, its
10635
    exports will be removed from the nodes A, B and D.
10636

10637
    """
10638
    assert self.op.mode != constants.EXPORT_MODE_REMOTE
10639

    
10640
    nodelist = self.cfg.GetNodeList()
10641
    nodelist.remove(self.dst_node.name)
10642

    
10643
    # on one-node clusters nodelist will be empty after the removal
10644
    # if we proceed the backup would be removed because OpBackupQuery
10645
    # substitutes an empty list with the full cluster node list.
10646
    iname = self.instance.name
10647
    if nodelist:
10648
      feedback_fn("Removing old exports for instance %s" % iname)
10649
      exportlist = self.rpc.call_export_list(nodelist)
10650
      for node in exportlist:
10651
        if exportlist[node].fail_msg:
10652
          continue
10653
        if iname in exportlist[node].payload:
10654
          msg = self.rpc.call_export_remove(node, iname).fail_msg
10655
          if msg:
10656
            self.LogWarning("Could not remove older export for instance %s"
10657
                            " on node %s: %s", iname, node, msg)
10658

    
10659
  def Exec(self, feedback_fn):
10660
    """Export an instance to an image in the cluster.
10661

10662
    """
10663
    assert self.op.mode in constants.EXPORT_MODES
10664

    
10665
    instance = self.instance
10666
    src_node = instance.primary_node
10667

    
10668
    if self.op.shutdown:
10669
      # shutdown the instance, but not the disks
10670
      feedback_fn("Shutting down instance %s" % instance.name)
10671
      result = self.rpc.call_instance_shutdown(src_node, instance,
10672
                                               self.op.shutdown_timeout)
10673
      # TODO: Maybe ignore failures if ignore_remove_failures is set
10674
      result.Raise("Could not shutdown instance %s on"
10675
                   " node %s" % (instance.name, src_node))
10676

    
10677
    # set the disks ID correctly since call_instance_start needs the
10678
    # correct drbd minor to create the symlinks
10679
    for disk in instance.disks:
10680
      self.cfg.SetDiskID(disk, src_node)
10681

    
10682
    activate_disks = (not instance.admin_up)
10683

    
10684
    if activate_disks:
10685
      # Activate the instance disks if we'exporting a stopped instance
10686
      feedback_fn("Activating disks for %s" % instance.name)
10687
      _StartInstanceDisks(self, instance, None)
10688

    
10689
    try:
10690
      helper = masterd.instance.ExportInstanceHelper(self, feedback_fn,
10691
                                                     instance)
10692

    
10693
      helper.CreateSnapshots()
10694
      try:
10695
        if (self.op.shutdown and instance.admin_up and
10696
            not self.op.remove_instance):
10697
          assert not activate_disks
10698
          feedback_fn("Starting instance %s" % instance.name)
10699
          result = self.rpc.call_instance_start(src_node, instance, None, None)
10700
          msg = result.fail_msg
10701
          if msg:
10702
            feedback_fn("Failed to start instance: %s" % msg)
10703
            _ShutdownInstanceDisks(self, instance)
10704
            raise errors.OpExecError("Could not start instance: %s" % msg)
10705

    
10706
        if self.op.mode == constants.EXPORT_MODE_LOCAL:
10707
          (fin_resu, dresults) = helper.LocalExport(self.dst_node)
10708
        elif self.op.mode == constants.EXPORT_MODE_REMOTE:
10709
          connect_timeout = constants.RIE_CONNECT_TIMEOUT
10710
          timeouts = masterd.instance.ImportExportTimeouts(connect_timeout)
10711

    
10712
          (key_name, _, _) = self.x509_key_name
10713

    
10714
          dest_ca_pem = \
10715
            OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM,
10716
                                            self.dest_x509_ca)
10717

    
10718
          (fin_resu, dresults) = helper.RemoteExport(self.dest_disk_info,
10719
                                                     key_name, dest_ca_pem,
10720
                                                     timeouts)
10721
      finally:
10722
        helper.Cleanup()
10723

    
10724
      # Check for backwards compatibility
10725
      assert len(dresults) == len(instance.disks)
10726
      assert compat.all(isinstance(i, bool) for i in dresults), \
10727
             "Not all results are boolean: %r" % dresults
10728

    
10729
    finally:
10730
      if activate_disks:
10731
        feedback_fn("Deactivating disks for %s" % instance.name)
10732
        _ShutdownInstanceDisks(self, instance)
10733

    
10734
    if not (compat.all(dresults) and fin_resu):
10735
      failures = []
10736
      if not fin_resu:
10737
        failures.append("export finalization")
10738
      if not compat.all(dresults):
10739
        fdsk = utils.CommaJoin(idx for (idx, dsk) in enumerate(dresults)
10740
                               if not dsk)
10741
        failures.append("disk export: disk(s) %s" % fdsk)
10742

    
10743
      raise errors.OpExecError("Export failed, errors in %s" %
10744
                               utils.CommaJoin(failures))
10745

    
10746
    # At this point, the export was successful, we can cleanup/finish
10747

    
10748
    # Remove instance if requested
10749
    if self.op.remove_instance:
10750
      feedback_fn("Removing instance %s" % instance.name)
10751
      _RemoveInstance(self, feedback_fn, instance,
10752
                      self.op.ignore_remove_failures)
10753

    
10754
    if self.op.mode == constants.EXPORT_MODE_LOCAL:
10755
      self._CleanupExports(feedback_fn)
10756

    
10757
    return fin_resu, dresults
10758

    
10759

    
10760
class LUBackupRemove(NoHooksLU):
10761
  """Remove exports related to the named instance.
10762

10763
  """
10764
  REQ_BGL = False
10765

    
10766
  def ExpandNames(self):
10767
    self.needed_locks = {}
10768
    # We need all nodes to be locked in order for RemoveExport to work, but we
10769
    # don't need to lock the instance itself, as nothing will happen to it (and
10770
    # we can remove exports also for a removed instance)
10771
    self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
10772

    
10773
  def Exec(self, feedback_fn):
10774
    """Remove any export.
10775

10776
    """
10777
    instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
10778
    # If the instance was not found we'll try with the name that was passed in.
10779
    # This will only work if it was an FQDN, though.
10780
    fqdn_warn = False
10781
    if not instance_name:
10782
      fqdn_warn = True
10783
      instance_name = self.op.instance_name
10784

    
10785
    locked_nodes = self.glm.list_owned(locking.LEVEL_NODE)
10786
    exportlist = self.rpc.call_export_list(locked_nodes)
10787
    found = False
10788
    for node in exportlist:
10789
      msg = exportlist[node].fail_msg
10790
      if msg:
10791
        self.LogWarning("Failed to query node %s (continuing): %s", node, msg)
10792
        continue
10793
      if instance_name in exportlist[node].payload:
10794
        found = True
10795
        result = self.rpc.call_export_remove(node, instance_name)
10796
        msg = result.fail_msg
10797
        if msg:
10798
          logging.error("Could not remove export for instance %s"
10799
                        " on node %s: %s", instance_name, node, msg)
10800

    
10801
    if fqdn_warn and not found:
10802
      feedback_fn("Export not found. If trying to remove an export belonging"
10803
                  " to a deleted instance please use its Fully Qualified"
10804
                  " Domain Name.")
10805

    
10806

    
10807
class LUGroupAdd(LogicalUnit):
10808
  """Logical unit for creating node groups.
10809

10810
  """
10811
  HPATH = "group-add"
10812
  HTYPE = constants.HTYPE_GROUP
10813
  REQ_BGL = False
10814

    
10815
  def ExpandNames(self):
10816
    # We need the new group's UUID here so that we can create and acquire the
10817
    # corresponding lock. Later, in Exec(), we'll indicate to cfg.AddNodeGroup
10818
    # that it should not check whether the UUID exists in the configuration.
10819
    self.group_uuid = self.cfg.GenerateUniqueID(self.proc.GetECId())
10820
    self.needed_locks = {}
10821
    self.add_locks[locking.LEVEL_NODEGROUP] = self.group_uuid
10822

    
10823
  def CheckPrereq(self):
10824
    """Check prerequisites.
10825

10826
    This checks that the given group name is not an existing node group
10827
    already.
10828

10829
    """
10830
    try:
10831
      existing_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
10832
    except errors.OpPrereqError:
10833
      pass
10834
    else:
10835
      raise errors.OpPrereqError("Desired group name '%s' already exists as a"
10836
                                 " node group (UUID: %s)" %
10837
                                 (self.op.group_name, existing_uuid),
10838
                                 errors.ECODE_EXISTS)
10839

    
10840
    if self.op.ndparams:
10841
      utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
10842

    
10843
  def BuildHooksEnv(self):
10844
    """Build hooks env.
10845

10846
    """
10847
    return {
10848
      "GROUP_NAME": self.op.group_name,
10849
      }
10850

    
10851
  def BuildHooksNodes(self):
10852
    """Build hooks nodes.
10853

10854
    """
10855
    mn = self.cfg.GetMasterNode()
10856
    return ([mn], [mn])
10857

    
10858
  def Exec(self, feedback_fn):
10859
    """Add the node group to the cluster.
10860

10861
    """
10862
    group_obj = objects.NodeGroup(name=self.op.group_name, members=[],
10863
                                  uuid=self.group_uuid,
10864
                                  alloc_policy=self.op.alloc_policy,
10865
                                  ndparams=self.op.ndparams)
10866

    
10867
    self.cfg.AddNodeGroup(group_obj, self.proc.GetECId(), check_uuid=False)
10868
    del self.remove_locks[locking.LEVEL_NODEGROUP]
10869

    
10870

    
10871
class LUGroupAssignNodes(NoHooksLU):
10872
  """Logical unit for assigning nodes to groups.
10873

10874
  """
10875
  REQ_BGL = False
10876

    
10877
  def ExpandNames(self):
10878
    # These raise errors.OpPrereqError on their own:
10879
    self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
10880
    self.op.nodes = _GetWantedNodes(self, self.op.nodes)
10881

    
10882
    # We want to lock all the affected nodes and groups. We have readily
10883
    # available the list of nodes, and the *destination* group. To gather the
10884
    # list of "source" groups, we need to fetch node information.
10885
    self.node_data = self.cfg.GetAllNodesInfo()
10886
    affected_groups = set(self.node_data[node].group for node in self.op.nodes)
10887
    affected_groups.add(self.group_uuid)
10888

    
10889
    self.needed_locks = {
10890
      locking.LEVEL_NODEGROUP: list(affected_groups),
10891
      locking.LEVEL_NODE: self.op.nodes,
10892
      }
10893

    
10894
  def CheckPrereq(self):
10895
    """Check prerequisites.
10896

10897
    """
10898
    self.group = self.cfg.GetNodeGroup(self.group_uuid)
10899
    instance_data = self.cfg.GetAllInstancesInfo()
10900

    
10901
    if self.group is None:
10902
      raise errors.OpExecError("Could not retrieve group '%s' (UUID: %s)" %
10903
                               (self.op.group_name, self.group_uuid))
10904

    
10905
    (new_splits, previous_splits) = \
10906
      self.CheckAssignmentForSplitInstances([(node, self.group_uuid)
10907
                                             for node in self.op.nodes],
10908
                                            self.node_data, instance_data)
10909

    
10910
    if new_splits:
10911
      fmt_new_splits = utils.CommaJoin(utils.NiceSort(new_splits))
10912

    
10913
      if not self.op.force:
10914
        raise errors.OpExecError("The following instances get split by this"
10915
                                 " change and --force was not given: %s" %
10916
                                 fmt_new_splits)
10917
      else:
10918
        self.LogWarning("This operation will split the following instances: %s",
10919
                        fmt_new_splits)
10920

    
10921
        if previous_splits:
10922
          self.LogWarning("In addition, these already-split instances continue"
10923
                          " to be split across groups: %s",
10924
                          utils.CommaJoin(utils.NiceSort(previous_splits)))
10925

    
10926
  def Exec(self, feedback_fn):
10927
    """Assign nodes to a new group.
10928

10929
    """
10930
    for node in self.op.nodes:
10931
      self.node_data[node].group = self.group_uuid
10932

    
10933
    self.cfg.Update(self.group, feedback_fn) # Saves all modified nodes.
10934

    
10935
  @staticmethod
10936
  def CheckAssignmentForSplitInstances(changes, node_data, instance_data):
10937
    """Check for split instances after a node assignment.
10938

10939
    This method considers a series of node assignments as an atomic operation,
10940
    and returns information about split instances after applying the set of
10941
    changes.
10942

10943
    In particular, it returns information about newly split instances, and
10944
    instances that were already split, and remain so after the change.
10945

10946
    Only instances whose disk template is listed in constants.DTS_INT_MIRROR are
10947
    considered.
10948

10949
    @type changes: list of (node_name, new_group_uuid) pairs.
10950
    @param changes: list of node assignments to consider.
10951
    @param node_data: a dict with data for all nodes
10952
    @param instance_data: a dict with all instances to consider
10953
    @rtype: a two-tuple
10954
    @return: a list of instances that were previously okay and result split as a
10955
      consequence of this change, and a list of instances that were previously
10956
      split and this change does not fix.
10957

10958
    """
10959
    changed_nodes = dict((node, group) for node, group in changes
10960
                         if node_data[node].group != group)
10961

    
10962
    all_split_instances = set()
10963
    previously_split_instances = set()
10964

    
10965
    def InstanceNodes(instance):
10966
      return [instance.primary_node] + list(instance.secondary_nodes)
10967

    
10968
    for inst in instance_data.values():
10969
      if inst.disk_template not in constants.DTS_INT_MIRROR:
10970
        continue
10971

    
10972
      instance_nodes = InstanceNodes(inst)
10973

    
10974
      if len(set(node_data[node].group for node in instance_nodes)) > 1:
10975
        previously_split_instances.add(inst.name)
10976

    
10977
      if len(set(changed_nodes.get(node, node_data[node].group)
10978
                 for node in instance_nodes)) > 1:
10979
        all_split_instances.add(inst.name)
10980

    
10981
    return (list(all_split_instances - previously_split_instances),
10982
            list(previously_split_instances & all_split_instances))
10983

    
10984

    
10985
class _GroupQuery(_QueryBase):
10986
  FIELDS = query.GROUP_FIELDS
10987

    
10988
  def ExpandNames(self, lu):
10989
    lu.needed_locks = {}
10990

    
10991
    self._all_groups = lu.cfg.GetAllNodeGroupsInfo()
10992
    name_to_uuid = dict((g.name, g.uuid) for g in self._all_groups.values())
10993

    
10994
    if not self.names:
10995
      self.wanted = [name_to_uuid[name]
10996
                     for name in utils.NiceSort(name_to_uuid.keys())]
10997
    else:
10998
      # Accept names to be either names or UUIDs.
10999
      missing = []
11000
      self.wanted = []
11001
      all_uuid = frozenset(self._all_groups.keys())
11002

    
11003
      for name in self.names:
11004
        if name in all_uuid:
11005
          self.wanted.append(name)
11006
        elif name in name_to_uuid:
11007
          self.wanted.append(name_to_uuid[name])
11008
        else:
11009
          missing.append(name)
11010

    
11011
      if missing:
11012
        raise errors.OpPrereqError("Some groups do not exist: %s" %
11013
                                   utils.CommaJoin(missing),
11014
                                   errors.ECODE_NOENT)
11015

    
11016
  def DeclareLocks(self, lu, level):
11017
    pass
11018

    
11019
  def _GetQueryData(self, lu):
11020
    """Computes the list of node groups and their attributes.
11021

11022
    """
11023
    do_nodes = query.GQ_NODE in self.requested_data
11024
    do_instances = query.GQ_INST in self.requested_data
11025

    
11026
    group_to_nodes = None
11027
    group_to_instances = None
11028

    
11029
    # For GQ_NODE, we need to map group->[nodes], and group->[instances] for
11030
    # GQ_INST. The former is attainable with just GetAllNodesInfo(), but for the
11031
    # latter GetAllInstancesInfo() is not enough, for we have to go through
11032
    # instance->node. Hence, we will need to process nodes even if we only need
11033
    # instance information.
11034
    if do_nodes or do_instances:
11035
      all_nodes = lu.cfg.GetAllNodesInfo()
11036
      group_to_nodes = dict((uuid, []) for uuid in self.wanted)
11037
      node_to_group = {}
11038

    
11039
      for node in all_nodes.values():
11040
        if node.group in group_to_nodes:
11041
          group_to_nodes[node.group].append(node.name)
11042
          node_to_group[node.name] = node.group
11043

    
11044
      if do_instances:
11045
        all_instances = lu.cfg.GetAllInstancesInfo()
11046
        group_to_instances = dict((uuid, []) for uuid in self.wanted)
11047

    
11048
        for instance in all_instances.values():
11049
          node = instance.primary_node
11050
          if node in node_to_group:
11051
            group_to_instances[node_to_group[node]].append(instance.name)
11052

    
11053
        if not do_nodes:
11054
          # Do not pass on node information if it was not requested.
11055
          group_to_nodes = None
11056

    
11057
    return query.GroupQueryData([self._all_groups[uuid]
11058
                                 for uuid in self.wanted],
11059
                                group_to_nodes, group_to_instances)
11060

    
11061

    
11062
class LUGroupQuery(NoHooksLU):
11063
  """Logical unit for querying node groups.
11064

11065
  """
11066
  REQ_BGL = False
11067

    
11068
  def CheckArguments(self):
11069
    self.gq = _GroupQuery(qlang.MakeSimpleFilter("name", self.op.names),
11070
                          self.op.output_fields, False)
11071

    
11072
  def ExpandNames(self):
11073
    self.gq.ExpandNames(self)
11074

    
11075
  def Exec(self, feedback_fn):
11076
    return self.gq.OldStyleQuery(self)
11077

    
11078

    
11079
class LUGroupSetParams(LogicalUnit):
11080
  """Modifies the parameters of a node group.
11081

11082
  """
11083
  HPATH = "group-modify"
11084
  HTYPE = constants.HTYPE_GROUP
11085
  REQ_BGL = False
11086

    
11087
  def CheckArguments(self):
11088
    all_changes = [
11089
      self.op.ndparams,
11090
      self.op.alloc_policy,
11091
      ]
11092

    
11093
    if all_changes.count(None) == len(all_changes):
11094
      raise errors.OpPrereqError("Please pass at least one modification",
11095
                                 errors.ECODE_INVAL)
11096

    
11097
  def ExpandNames(self):
11098
    # This raises errors.OpPrereqError on its own:
11099
    self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
11100

    
11101
    self.needed_locks = {
11102
      locking.LEVEL_NODEGROUP: [self.group_uuid],
11103
      }
11104

    
11105
  def CheckPrereq(self):
11106
    """Check prerequisites.
11107

11108
    """
11109
    self.group = self.cfg.GetNodeGroup(self.group_uuid)
11110

    
11111
    if self.group is None:
11112
      raise errors.OpExecError("Could not retrieve group '%s' (UUID: %s)" %
11113
                               (self.op.group_name, self.group_uuid))
11114

    
11115
    if self.op.ndparams:
11116
      new_ndparams = _GetUpdatedParams(self.group.ndparams, self.op.ndparams)
11117
      utils.ForceDictType(self.op.ndparams, constants.NDS_PARAMETER_TYPES)
11118
      self.new_ndparams = new_ndparams
11119

    
11120
  def BuildHooksEnv(self):
11121
    """Build hooks env.
11122

11123
    """
11124
    return {
11125
      "GROUP_NAME": self.op.group_name,
11126
      "NEW_ALLOC_POLICY": self.op.alloc_policy,
11127
      }
11128

    
11129
  def BuildHooksNodes(self):
11130
    """Build hooks nodes.
11131

11132
    """
11133
    mn = self.cfg.GetMasterNode()
11134
    return ([mn], [mn])
11135

    
11136
  def Exec(self, feedback_fn):
11137
    """Modifies the node group.
11138

11139
    """
11140
    result = []
11141

    
11142
    if self.op.ndparams:
11143
      self.group.ndparams = self.new_ndparams
11144
      result.append(("ndparams", str(self.group.ndparams)))
11145

    
11146
    if self.op.alloc_policy:
11147
      self.group.alloc_policy = self.op.alloc_policy
11148

    
11149
    self.cfg.Update(self.group, feedback_fn)
11150
    return result
11151

    
11152

    
11153

    
11154
class LUGroupRemove(LogicalUnit):
11155
  HPATH = "group-remove"
11156
  HTYPE = constants.HTYPE_GROUP
11157
  REQ_BGL = False
11158

    
11159
  def ExpandNames(self):
11160
    # This will raises errors.OpPrereqError on its own:
11161
    self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
11162
    self.needed_locks = {
11163
      locking.LEVEL_NODEGROUP: [self.group_uuid],
11164
      }
11165

    
11166
  def CheckPrereq(self):
11167
    """Check prerequisites.
11168

11169
    This checks that the given group name exists as a node group, that is
11170
    empty (i.e., contains no nodes), and that is not the last group of the
11171
    cluster.
11172

11173
    """
11174
    # Verify that the group is empty.
11175
    group_nodes = [node.name
11176
                   for node in self.cfg.GetAllNodesInfo().values()
11177
                   if node.group == self.group_uuid]
11178

    
11179
    if group_nodes:
11180
      raise errors.OpPrereqError("Group '%s' not empty, has the following"
11181
                                 " nodes: %s" %
11182
                                 (self.op.group_name,
11183
                                  utils.CommaJoin(utils.NiceSort(group_nodes))),
11184
                                 errors.ECODE_STATE)
11185

    
11186
    # Verify the cluster would not be left group-less.
11187
    if len(self.cfg.GetNodeGroupList()) == 1:
11188
      raise errors.OpPrereqError("Group '%s' is the only group,"
11189
                                 " cannot be removed" %
11190
                                 self.op.group_name,
11191
                                 errors.ECODE_STATE)
11192

    
11193
  def BuildHooksEnv(self):
11194
    """Build hooks env.
11195

11196
    """
11197
    return {
11198
      "GROUP_NAME": self.op.group_name,
11199
      }
11200

    
11201
  def BuildHooksNodes(self):
11202
    """Build hooks nodes.
11203

11204
    """
11205
    mn = self.cfg.GetMasterNode()
11206
    return ([mn], [mn])
11207

    
11208
  def Exec(self, feedback_fn):
11209
    """Remove the node group.
11210

11211
    """
11212
    try:
11213
      self.cfg.RemoveNodeGroup(self.group_uuid)
11214
    except errors.ConfigurationError:
11215
      raise errors.OpExecError("Group '%s' with UUID %s disappeared" %
11216
                               (self.op.group_name, self.group_uuid))
11217

    
11218
    self.remove_locks[locking.LEVEL_NODEGROUP] = self.group_uuid
11219

    
11220

    
11221
class LUGroupRename(LogicalUnit):
11222
  HPATH = "group-rename"
11223
  HTYPE = constants.HTYPE_GROUP
11224
  REQ_BGL = False
11225

    
11226
  def ExpandNames(self):
11227
    # This raises errors.OpPrereqError on its own:
11228
    self.group_uuid = self.cfg.LookupNodeGroup(self.op.group_name)
11229

    
11230
    self.needed_locks = {
11231
      locking.LEVEL_NODEGROUP: [self.group_uuid],
11232
      }
11233

    
11234
  def CheckPrereq(self):
11235
    """Check prerequisites.
11236

11237
    Ensures requested new name is not yet used.
11238

11239
    """
11240
    try:
11241
      new_name_uuid = self.cfg.LookupNodeGroup(self.op.new_name)
11242
    except errors.OpPrereqError:
11243
      pass
11244
    else:
11245
      raise errors.OpPrereqError("Desired new name '%s' clashes with existing"
11246
                                 " node group (UUID: %s)" %
11247
                                 (self.op.new_name, new_name_uuid),
11248
                                 errors.ECODE_EXISTS)
11249

    
11250
  def BuildHooksEnv(self):
11251
    """Build hooks env.
11252

11253
    """
11254
    return {
11255
      "OLD_NAME": self.op.group_name,
11256
      "NEW_NAME": self.op.new_name,
11257
      }
11258

    
11259
  def BuildHooksNodes(self):
11260
    """Build hooks nodes.
11261

11262
    """
11263
    mn = self.cfg.GetMasterNode()
11264

    
11265
    all_nodes = self.cfg.GetAllNodesInfo()
11266
    all_nodes.pop(mn, None)
11267

    
11268
    run_nodes = [mn]
11269
    run_nodes.extend(node.name for node in all_nodes.values()
11270
                     if node.group == self.group_uuid)
11271

    
11272
    return (run_nodes, run_nodes)
11273

    
11274
  def Exec(self, feedback_fn):
11275
    """Rename the node group.
11276

11277
    """
11278
    group = self.cfg.GetNodeGroup(self.group_uuid)
11279

    
11280
    if group is None:
11281
      raise errors.OpExecError("Could not retrieve group '%s' (UUID: %s)" %
11282
                               (self.op.group_name, self.group_uuid))
11283

    
11284
    group.name = self.op.new_name
11285
    self.cfg.Update(group, feedback_fn)
11286

    
11287
    return self.op.new_name
11288

    
11289

    
11290
class TagsLU(NoHooksLU): # pylint: disable-msg=W0223
11291
  """Generic tags LU.
11292

11293
  This is an abstract class which is the parent of all the other tags LUs.
11294

11295
  """
11296
  def ExpandNames(self):
11297
    self.group_uuid = None
11298
    self.needed_locks = {}
11299
    if self.op.kind == constants.TAG_NODE:
11300
      self.op.name = _ExpandNodeName(self.cfg, self.op.name)
11301
      self.needed_locks[locking.LEVEL_NODE] = self.op.name
11302
    elif self.op.kind == constants.TAG_INSTANCE:
11303
      self.op.name = _ExpandInstanceName(self.cfg, self.op.name)
11304
      self.needed_locks[locking.LEVEL_INSTANCE] = self.op.name
11305
    elif self.op.kind == constants.TAG_NODEGROUP:
11306
      self.group_uuid = self.cfg.LookupNodeGroup(self.op.name)
11307

    
11308
    # FIXME: Acquire BGL for cluster tag operations (as of this writing it's
11309
    # not possible to acquire the BGL based on opcode parameters)
11310

    
11311
  def CheckPrereq(self):
11312
    """Check prerequisites.
11313

11314
    """
11315
    if self.op.kind == constants.TAG_CLUSTER:
11316
      self.target = self.cfg.GetClusterInfo()
11317
    elif self.op.kind == constants.TAG_NODE:
11318
      self.target = self.cfg.GetNodeInfo(self.op.name)
11319
    elif self.op.kind == constants.TAG_INSTANCE:
11320
      self.target = self.cfg.GetInstanceInfo(self.op.name)
11321
    elif self.op.kind == constants.TAG_NODEGROUP:
11322
      self.target = self.cfg.GetNodeGroup(self.group_uuid)
11323
    else:
11324
      raise errors.OpPrereqError("Wrong tag type requested (%s)" %
11325
                                 str(self.op.kind), errors.ECODE_INVAL)
11326

    
11327

    
11328
class LUTagsGet(TagsLU):
11329
  """Returns the tags of a given object.
11330

11331
  """
11332
  REQ_BGL = False
11333

    
11334
  def ExpandNames(self):
11335
    TagsLU.ExpandNames(self)
11336

    
11337
    # Share locks as this is only a read operation
11338
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
11339

    
11340
  def Exec(self, feedback_fn):
11341
    """Returns the tag list.
11342

11343
    """
11344
    return list(self.target.GetTags())
11345

    
11346

    
11347
class LUTagsSearch(NoHooksLU):
11348
  """Searches the tags for a given pattern.
11349

11350
  """
11351
  REQ_BGL = False
11352

    
11353
  def ExpandNames(self):
11354
    self.needed_locks = {}
11355

    
11356
  def CheckPrereq(self):
11357
    """Check prerequisites.
11358

11359
    This checks the pattern passed for validity by compiling it.
11360

11361
    """
11362
    try:
11363
      self.re = re.compile(self.op.pattern)
11364
    except re.error, err:
11365
      raise errors.OpPrereqError("Invalid search pattern '%s': %s" %
11366
                                 (self.op.pattern, err), errors.ECODE_INVAL)
11367

    
11368
  def Exec(self, feedback_fn):
11369
    """Returns the tag list.
11370

11371
    """
11372
    cfg = self.cfg
11373
    tgts = [("/cluster", cfg.GetClusterInfo())]
11374
    ilist = cfg.GetAllInstancesInfo().values()
11375
    tgts.extend([("/instances/%s" % i.name, i) for i in ilist])
11376
    nlist = cfg.GetAllNodesInfo().values()
11377
    tgts.extend([("/nodes/%s" % n.name, n) for n in nlist])
11378
    tgts.extend(("/nodegroup/%s" % n.name, n)
11379
                for n in cfg.GetAllNodeGroupsInfo().values())
11380
    results = []
11381
    for path, target in tgts:
11382
      for tag in target.GetTags():
11383
        if self.re.search(tag):
11384
          results.append((path, tag))
11385
    return results
11386

    
11387

    
11388
class LUTagsSet(TagsLU):
11389
  """Sets a tag on a given object.
11390

11391
  """
11392
  REQ_BGL = False
11393

    
11394
  def CheckPrereq(self):
11395
    """Check prerequisites.
11396

11397
    This checks the type and length of the tag name and value.
11398

11399
    """
11400
    TagsLU.CheckPrereq(self)
11401
    for tag in self.op.tags:
11402
      objects.TaggableObject.ValidateTag(tag)
11403

    
11404
  def Exec(self, feedback_fn):
11405
    """Sets the tag.
11406

11407
    """
11408
    try:
11409
      for tag in self.op.tags:
11410
        self.target.AddTag(tag)
11411
    except errors.TagError, err:
11412
      raise errors.OpExecError("Error while setting tag: %s" % str(err))
11413
    self.cfg.Update(self.target, feedback_fn)
11414

    
11415

    
11416
class LUTagsDel(TagsLU):
11417
  """Delete a list of tags from a given object.
11418

11419
  """
11420
  REQ_BGL = False
11421

    
11422
  def CheckPrereq(self):
11423
    """Check prerequisites.
11424

11425
    This checks that we have the given tag.
11426

11427
    """
11428
    TagsLU.CheckPrereq(self)
11429
    for tag in self.op.tags:
11430
      objects.TaggableObject.ValidateTag(tag)
11431
    del_tags = frozenset(self.op.tags)
11432
    cur_tags = self.target.GetTags()
11433

    
11434
    diff_tags = del_tags - cur_tags
11435
    if diff_tags:
11436
      diff_names = ("'%s'" % i for i in sorted(diff_tags))
11437
      raise errors.OpPrereqError("Tag(s) %s not found" %
11438
                                 (utils.CommaJoin(diff_names), ),
11439
                                 errors.ECODE_NOENT)
11440

    
11441
  def Exec(self, feedback_fn):
11442
    """Remove the tag from the object.
11443

11444
    """
11445
    for tag in self.op.tags:
11446
      self.target.RemoveTag(tag)
11447
    self.cfg.Update(self.target, feedback_fn)
11448

    
11449

    
11450
class LUTestDelay(NoHooksLU):
11451
  """Sleep for a specified amount of time.
11452

11453
  This LU sleeps on the master and/or nodes for a specified amount of
11454
  time.
11455

11456
  """
11457
  REQ_BGL = False
11458

    
11459
  def ExpandNames(self):
11460
    """Expand names and set required locks.
11461

11462
    This expands the node list, if any.
11463

11464
    """
11465
    self.needed_locks = {}
11466
    if self.op.on_nodes:
11467
      # _GetWantedNodes can be used here, but is not always appropriate to use
11468
      # this way in ExpandNames. Check LogicalUnit.ExpandNames docstring for
11469
      # more information.
11470
      self.op.on_nodes = _GetWantedNodes(self, self.op.on_nodes)
11471
      self.needed_locks[locking.LEVEL_NODE] = self.op.on_nodes
11472

    
11473
  def _TestDelay(self):
11474
    """Do the actual sleep.
11475

11476
    """
11477
    if self.op.on_master:
11478
      if not utils.TestDelay(self.op.duration):
11479
        raise errors.OpExecError("Error during master delay test")
11480
    if self.op.on_nodes:
11481
      result = self.rpc.call_test_delay(self.op.on_nodes, self.op.duration)
11482
      for node, node_result in result.items():
11483
        node_result.Raise("Failure during rpc call to node %s" % node)
11484

    
11485
  def Exec(self, feedback_fn):
11486
    """Execute the test delay opcode, with the wanted repetitions.
11487

11488
    """
11489
    if self.op.repeat == 0:
11490
      self._TestDelay()
11491
    else:
11492
      top_value = self.op.repeat - 1
11493
      for i in range(self.op.repeat):
11494
        self.LogInfo("Test delay iteration %d/%d" % (i, top_value))
11495
        self._TestDelay()
11496

    
11497

    
11498
class LUTestJqueue(NoHooksLU):
11499
  """Utility LU to test some aspects of the job queue.
11500

11501
  """
11502
  REQ_BGL = False
11503

    
11504
  # Must be lower than default timeout for WaitForJobChange to see whether it
11505
  # notices changed jobs
11506
  _CLIENT_CONNECT_TIMEOUT = 20.0
11507
  _CLIENT_CONFIRM_TIMEOUT = 60.0
11508

    
11509
  @classmethod
11510
  def _NotifyUsingSocket(cls, cb, errcls):
11511
    """Opens a Unix socket and waits for another program to connect.
11512

11513
    @type cb: callable
11514
    @param cb: Callback to send socket name to client
11515
    @type errcls: class
11516
    @param errcls: Exception class to use for errors
11517

11518
    """
11519
    # Using a temporary directory as there's no easy way to create temporary
11520
    # sockets without writing a custom loop around tempfile.mktemp and
11521
    # socket.bind
11522
    tmpdir = tempfile.mkdtemp()
11523
    try:
11524
      tmpsock = utils.PathJoin(tmpdir, "sock")
11525

    
11526
      logging.debug("Creating temporary socket at %s", tmpsock)
11527
      sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
11528
      try:
11529
        sock.bind(tmpsock)
11530
        sock.listen(1)
11531

    
11532
        # Send details to client
11533
        cb(tmpsock)
11534

    
11535
        # Wait for client to connect before continuing
11536
        sock.settimeout(cls._CLIENT_CONNECT_TIMEOUT)
11537
        try:
11538
          (conn, _) = sock.accept()
11539
        except socket.error, err:
11540
          raise errcls("Client didn't connect in time (%s)" % err)
11541
      finally:
11542
        sock.close()
11543
    finally:
11544
      # Remove as soon as client is connected
11545
      shutil.rmtree(tmpdir)
11546

    
11547
    # Wait for client to close
11548
    try:
11549
      try:
11550
        # pylint: disable-msg=E1101
11551
        # Instance of '_socketobject' has no ... member
11552
        conn.settimeout(cls._CLIENT_CONFIRM_TIMEOUT)
11553
        conn.recv(1)
11554
      except socket.error, err:
11555
        raise errcls("Client failed to confirm notification (%s)" % err)
11556
    finally:
11557
      conn.close()
11558

    
11559
  def _SendNotification(self, test, arg, sockname):
11560
    """Sends a notification to the client.
11561

11562
    @type test: string
11563
    @param test: Test name
11564
    @param arg: Test argument (depends on test)
11565
    @type sockname: string
11566
    @param sockname: Socket path
11567

11568
    """
11569
    self.Log(constants.ELOG_JQUEUE_TEST, (sockname, test, arg))
11570

    
11571
  def _Notify(self, prereq, test, arg):
11572
    """Notifies the client of a test.
11573

11574
    @type prereq: bool
11575
    @param prereq: Whether this is a prereq-phase test
11576
    @type test: string
11577
    @param test: Test name
11578
    @param arg: Test argument (depends on test)
11579

11580
    """
11581
    if prereq:
11582
      errcls = errors.OpPrereqError
11583
    else:
11584
      errcls = errors.OpExecError
11585

    
11586
    return self._NotifyUsingSocket(compat.partial(self._SendNotification,
11587
                                                  test, arg),
11588
                                   errcls)
11589

    
11590
  def CheckArguments(self):
11591
    self.checkargs_calls = getattr(self, "checkargs_calls", 0) + 1
11592
    self.expandnames_calls = 0
11593

    
11594
  def ExpandNames(self):
11595
    checkargs_calls = getattr(self, "checkargs_calls", 0)
11596
    if checkargs_calls < 1:
11597
      raise errors.ProgrammerError("CheckArguments was not called")
11598

    
11599
    self.expandnames_calls += 1
11600

    
11601
    if self.op.notify_waitlock:
11602
      self._Notify(True, constants.JQT_EXPANDNAMES, None)
11603

    
11604
    self.LogInfo("Expanding names")
11605

    
11606
    # Get lock on master node (just to get a lock, not for a particular reason)
11607
    self.needed_locks = {
11608
      locking.LEVEL_NODE: self.cfg.GetMasterNode(),
11609
      }
11610

    
11611
  def Exec(self, feedback_fn):
11612
    if self.expandnames_calls < 1:
11613
      raise errors.ProgrammerError("ExpandNames was not called")
11614

    
11615
    if self.op.notify_exec:
11616
      self._Notify(False, constants.JQT_EXEC, None)
11617

    
11618
    self.LogInfo("Executing")
11619

    
11620
    if self.op.log_messages:
11621
      self._Notify(False, constants.JQT_STARTMSG, len(self.op.log_messages))
11622
      for idx, msg in enumerate(self.op.log_messages):
11623
        self.LogInfo("Sending log message %s", idx + 1)
11624
        feedback_fn(constants.JQT_MSGPREFIX + msg)
11625
        # Report how many test messages have been sent
11626
        self._Notify(False, constants.JQT_LOGMSG, idx + 1)
11627

    
11628
    if self.op.fail:
11629
      raise errors.OpExecError("Opcode failure was requested")
11630

    
11631
    return True
11632

    
11633

    
11634
class IAllocator(object):
11635
  """IAllocator framework.
11636

11637
  An IAllocator instance has three sets of attributes:
11638
    - cfg that is needed to query the cluster
11639
    - input data (all members of the _KEYS class attribute are required)
11640
    - four buffer attributes (in|out_data|text), that represent the
11641
      input (to the external script) in text and data structure format,
11642
      and the output from it, again in two formats
11643
    - the result variables from the script (success, info, nodes) for
11644
      easy usage
11645

11646
  """
11647
  # pylint: disable-msg=R0902
11648
  # lots of instance attributes
11649
  _ALLO_KEYS = [
11650
    "name", "mem_size", "disks", "disk_template",
11651
    "os", "tags", "nics", "vcpus", "hypervisor",
11652
    ]
11653
  _RELO_KEYS = [
11654
    "name", "relocate_from",
11655
    ]
11656
  _EVAC_KEYS = [
11657
    "evac_nodes",
11658
    ]
11659

    
11660
  def __init__(self, cfg, rpc, mode, **kwargs):
11661
    self.cfg = cfg
11662
    self.rpc = rpc
11663
    # init buffer variables
11664
    self.in_text = self.out_text = self.in_data = self.out_data = None
11665
    # init all input fields so that pylint is happy
11666
    self.mode = mode
11667
    self.mem_size = self.disks = self.disk_template = None
11668
    self.os = self.tags = self.nics = self.vcpus = None
11669
    self.hypervisor = None
11670
    self.relocate_from = None
11671
    self.name = None
11672
    self.evac_nodes = None
11673
    # computed fields
11674
    self.required_nodes = None
11675
    # init result fields
11676
    self.success = self.info = self.result = None
11677
    if self.mode == constants.IALLOCATOR_MODE_ALLOC:
11678
      keyset = self._ALLO_KEYS
11679
      fn = self._AddNewInstance
11680
    elif self.mode == constants.IALLOCATOR_MODE_RELOC:
11681
      keyset = self._RELO_KEYS
11682
      fn = self._AddRelocateInstance
11683
    elif self.mode == constants.IALLOCATOR_MODE_MEVAC:
11684
      keyset = self._EVAC_KEYS
11685
      fn = self._AddEvacuateNodes
11686
    else:
11687
      raise errors.ProgrammerError("Unknown mode '%s' passed to the"
11688
                                   " IAllocator" % self.mode)
11689
    for key in kwargs:
11690
      if key not in keyset:
11691
        raise errors.ProgrammerError("Invalid input parameter '%s' to"
11692
                                     " IAllocator" % key)
11693
      setattr(self, key, kwargs[key])
11694

    
11695
    for key in keyset:
11696
      if key not in kwargs:
11697
        raise errors.ProgrammerError("Missing input parameter '%s' to"
11698
                                     " IAllocator" % key)
11699
    self._BuildInputData(fn)
11700

    
11701
  def _ComputeClusterData(self):
11702
    """Compute the generic allocator input data.
11703

11704
    This is the data that is independent of the actual operation.
11705

11706
    """
11707
    cfg = self.cfg
11708
    cluster_info = cfg.GetClusterInfo()
11709
    # cluster data
11710
    data = {
11711
      "version": constants.IALLOCATOR_VERSION,
11712
      "cluster_name": cfg.GetClusterName(),
11713
      "cluster_tags": list(cluster_info.GetTags()),
11714
      "enabled_hypervisors": list(cluster_info.enabled_hypervisors),
11715
      # we don't have job IDs
11716
      }
11717
    ninfo = cfg.GetAllNodesInfo()
11718
    iinfo = cfg.GetAllInstancesInfo().values()
11719
    i_list = [(inst, cluster_info.FillBE(inst)) for inst in iinfo]
11720

    
11721
    # node data
11722
    node_list = [n.name for n in ninfo.values() if n.vm_capable]
11723

    
11724
    if self.mode == constants.IALLOCATOR_MODE_ALLOC:
11725
      hypervisor_name = self.hypervisor
11726
    elif self.mode == constants.IALLOCATOR_MODE_RELOC:
11727
      hypervisor_name = cfg.GetInstanceInfo(self.name).hypervisor
11728
    elif self.mode == constants.IALLOCATOR_MODE_MEVAC:
11729
      hypervisor_name = cluster_info.enabled_hypervisors[0]
11730

    
11731
    node_data = self.rpc.call_node_info(node_list, cfg.GetVGName(),
11732
                                        hypervisor_name)
11733
    node_iinfo = \
11734
      self.rpc.call_all_instances_info(node_list,
11735
                                       cluster_info.enabled_hypervisors)
11736

    
11737
    data["nodegroups"] = self._ComputeNodeGroupData(cfg)
11738

    
11739
    config_ndata = self._ComputeBasicNodeData(ninfo)
11740
    data["nodes"] = self._ComputeDynamicNodeData(ninfo, node_data, node_iinfo,
11741
                                                 i_list, config_ndata)
11742
    assert len(data["nodes"]) == len(ninfo), \
11743
        "Incomplete node data computed"
11744

    
11745
    data["instances"] = self._ComputeInstanceData(cluster_info, i_list)
11746

    
11747
    self.in_data = data
11748

    
11749
  @staticmethod
11750
  def _ComputeNodeGroupData(cfg):
11751
    """Compute node groups data.
11752

11753
    """
11754
    ng = {}
11755
    for guuid, gdata in cfg.GetAllNodeGroupsInfo().items():
11756
      ng[guuid] = {
11757
        "name": gdata.name,
11758
        "alloc_policy": gdata.alloc_policy,
11759
        }
11760
    return ng
11761

    
11762
  @staticmethod
11763
  def _ComputeBasicNodeData(node_cfg):
11764
    """Compute global node data.
11765

11766
    @rtype: dict
11767
    @returns: a dict of name: (node dict, node config)
11768

11769
    """
11770
    node_results = {}
11771
    for ninfo in node_cfg.values():
11772
      # fill in static (config-based) values
11773
      pnr = {
11774
        "tags": list(ninfo.GetTags()),
11775
        "primary_ip": ninfo.primary_ip,
11776
        "secondary_ip": ninfo.secondary_ip,
11777
        "offline": ninfo.offline,
11778
        "drained": ninfo.drained,
11779
        "master_candidate": ninfo.master_candidate,
11780
        "group": ninfo.group,
11781
        "master_capable": ninfo.master_capable,
11782
        "vm_capable": ninfo.vm_capable,
11783
        }
11784

    
11785
      node_results[ninfo.name] = pnr
11786

    
11787
    return node_results
11788

    
11789
  @staticmethod
11790
  def _ComputeDynamicNodeData(node_cfg, node_data, node_iinfo, i_list,
11791
                              node_results):
11792
    """Compute global node data.
11793

11794
    @param node_results: the basic node structures as filled from the config
11795

11796
    """
11797
    # make a copy of the current dict
11798
    node_results = dict(node_results)
11799
    for nname, nresult in node_data.items():
11800
      assert nname in node_results, "Missing basic data for node %s" % nname
11801
      ninfo = node_cfg[nname]
11802

    
11803
      if not (ninfo.offline or ninfo.drained):
11804
        nresult.Raise("Can't get data for node %s" % nname)
11805
        node_iinfo[nname].Raise("Can't get node instance info from node %s" %
11806
                                nname)
11807
        remote_info = nresult.payload
11808

    
11809
        for attr in ['memory_total', 'memory_free', 'memory_dom0',
11810
                     'vg_size', 'vg_free', 'cpu_total']:
11811
          if attr not in remote_info:
11812
            raise errors.OpExecError("Node '%s' didn't return attribute"
11813
                                     " '%s'" % (nname, attr))
11814
          if not isinstance(remote_info[attr], int):
11815
            raise errors.OpExecError("Node '%s' returned invalid value"
11816
                                     " for '%s': %s" %
11817
                                     (nname, attr, remote_info[attr]))
11818
        # compute memory used by primary instances
11819
        i_p_mem = i_p_up_mem = 0
11820
        for iinfo, beinfo in i_list:
11821
          if iinfo.primary_node == nname:
11822
            i_p_mem += beinfo[constants.BE_MEMORY]
11823
            if iinfo.name not in node_iinfo[nname].payload:
11824
              i_used_mem = 0
11825
            else:
11826
              i_used_mem = int(node_iinfo[nname].payload[iinfo.name]['memory'])
11827
            i_mem_diff = beinfo[constants.BE_MEMORY] - i_used_mem
11828
            remote_info['memory_free'] -= max(0, i_mem_diff)
11829

    
11830
            if iinfo.admin_up:
11831
              i_p_up_mem += beinfo[constants.BE_MEMORY]
11832

    
11833
        # compute memory used by instances
11834
        pnr_dyn = {
11835
          "total_memory": remote_info['memory_total'],
11836
          "reserved_memory": remote_info['memory_dom0'],
11837
          "free_memory": remote_info['memory_free'],
11838
          "total_disk": remote_info['vg_size'],
11839
          "free_disk": remote_info['vg_free'],
11840
          "total_cpus": remote_info['cpu_total'],
11841
          "i_pri_memory": i_p_mem,
11842
          "i_pri_up_memory": i_p_up_mem,
11843
          }
11844
        pnr_dyn.update(node_results[nname])
11845
        node_results[nname] = pnr_dyn
11846

    
11847
    return node_results
11848

    
11849
  @staticmethod
11850
  def _ComputeInstanceData(cluster_info, i_list):
11851
    """Compute global instance data.
11852

11853
    """
11854
    instance_data = {}
11855
    for iinfo, beinfo in i_list:
11856
      nic_data = []
11857
      for nic in iinfo.nics:
11858
        filled_params = cluster_info.SimpleFillNIC(nic.nicparams)
11859
        nic_dict = {"mac": nic.mac,
11860
                    "ip": nic.ip,
11861
                    "mode": filled_params[constants.NIC_MODE],
11862
                    "link": filled_params[constants.NIC_LINK],
11863
                   }
11864
        if filled_params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
11865
          nic_dict["bridge"] = filled_params[constants.NIC_LINK]
11866
        nic_data.append(nic_dict)
11867
      pir = {
11868
        "tags": list(iinfo.GetTags()),
11869
        "admin_up": iinfo.admin_up,
11870
        "vcpus": beinfo[constants.BE_VCPUS],
11871
        "memory": beinfo[constants.BE_MEMORY],
11872
        "os": iinfo.os,
11873
        "nodes": [iinfo.primary_node] + list(iinfo.secondary_nodes),
11874
        "nics": nic_data,
11875
        "disks": [{constants.IDISK_SIZE: dsk.size,
11876
                   constants.IDISK_MODE: dsk.mode}
11877
                  for dsk in iinfo.disks],
11878
        "disk_template": iinfo.disk_template,
11879
        "hypervisor": iinfo.hypervisor,
11880
        }
11881
      pir["disk_space_total"] = _ComputeDiskSize(iinfo.disk_template,
11882
                                                 pir["disks"])
11883
      instance_data[iinfo.name] = pir
11884

    
11885
    return instance_data
11886

    
11887
  def _AddNewInstance(self):
11888
    """Add new instance data to allocator structure.
11889

11890
    This in combination with _AllocatorGetClusterData will create the
11891
    correct structure needed as input for the allocator.
11892

11893
    The checks for the completeness of the opcode must have already been
11894
    done.
11895

11896
    """
11897
    disk_space = _ComputeDiskSize(self.disk_template, self.disks)
11898

    
11899
    if self.disk_template in constants.DTS_INT_MIRROR:
11900
      self.required_nodes = 2
11901
    else:
11902
      self.required_nodes = 1
11903
    request = {
11904
      "name": self.name,
11905
      "disk_template": self.disk_template,
11906
      "tags": self.tags,
11907
      "os": self.os,
11908
      "vcpus": self.vcpus,
11909
      "memory": self.mem_size,
11910
      "disks": self.disks,
11911
      "disk_space_total": disk_space,
11912
      "nics": self.nics,
11913
      "required_nodes": self.required_nodes,
11914
      }
11915
    return request
11916

    
11917
  def _AddRelocateInstance(self):
11918
    """Add relocate instance data to allocator structure.
11919

11920
    This in combination with _IAllocatorGetClusterData will create the
11921
    correct structure needed as input for the allocator.
11922

11923
    The checks for the completeness of the opcode must have already been
11924
    done.
11925

11926
    """
11927
    instance = self.cfg.GetInstanceInfo(self.name)
11928
    if instance is None:
11929
      raise errors.ProgrammerError("Unknown instance '%s' passed to"
11930
                                   " IAllocator" % self.name)
11931

    
11932
    if instance.disk_template not in constants.DTS_MIRRORED:
11933
      raise errors.OpPrereqError("Can't relocate non-mirrored instances",
11934
                                 errors.ECODE_INVAL)
11935

    
11936
    if instance.disk_template in constants.DTS_INT_MIRROR and \
11937
        len(instance.secondary_nodes) != 1:
11938
      raise errors.OpPrereqError("Instance has not exactly one secondary node",
11939
                                 errors.ECODE_STATE)
11940

    
11941
    self.required_nodes = 1
11942
    disk_sizes = [{constants.IDISK_SIZE: disk.size} for disk in instance.disks]
11943
    disk_space = _ComputeDiskSize(instance.disk_template, disk_sizes)
11944

    
11945
    request = {
11946
      "name": self.name,
11947
      "disk_space_total": disk_space,
11948
      "required_nodes": self.required_nodes,
11949
      "relocate_from": self.relocate_from,
11950
      }
11951
    return request
11952

    
11953
  def _AddEvacuateNodes(self):
11954
    """Add evacuate nodes data to allocator structure.
11955

11956
    """
11957
    request = {
11958
      "evac_nodes": self.evac_nodes
11959
      }
11960
    return request
11961

    
11962
  def _BuildInputData(self, fn):
11963
    """Build input data structures.
11964

11965
    """
11966
    self._ComputeClusterData()
11967

    
11968
    request = fn()
11969
    request["type"] = self.mode
11970
    self.in_data["request"] = request
11971

    
11972
    self.in_text = serializer.Dump(self.in_data)
11973

    
11974
  def Run(self, name, validate=True, call_fn=None):
11975
    """Run an instance allocator and return the results.
11976

11977
    """
11978
    if call_fn is None:
11979
      call_fn = self.rpc.call_iallocator_runner
11980

    
11981
    result = call_fn(self.cfg.GetMasterNode(), name, self.in_text)
11982
    result.Raise("Failure while running the iallocator script")
11983

    
11984
    self.out_text = result.payload
11985
    if validate:
11986
      self._ValidateResult()
11987

    
11988
  def _ValidateResult(self):
11989
    """Process the allocator results.
11990

11991
    This will process and if successful save the result in
11992
    self.out_data and the other parameters.
11993

11994
    """
11995
    try:
11996
      rdict = serializer.Load(self.out_text)
11997
    except Exception, err:
11998
      raise errors.OpExecError("Can't parse iallocator results: %s" % str(err))
11999

    
12000
    if not isinstance(rdict, dict):
12001
      raise errors.OpExecError("Can't parse iallocator results: not a dict")
12002

    
12003
    # TODO: remove backwards compatiblity in later versions
12004
    if "nodes" in rdict and "result" not in rdict:
12005
      rdict["result"] = rdict["nodes"]
12006
      del rdict["nodes"]
12007

    
12008
    for key in "success", "info", "result":
12009
      if key not in rdict:
12010
        raise errors.OpExecError("Can't parse iallocator results:"
12011
                                 " missing key '%s'" % key)
12012
      setattr(self, key, rdict[key])
12013

    
12014
    if not isinstance(rdict["result"], list):
12015
      raise errors.OpExecError("Can't parse iallocator results: 'result' key"
12016
                               " is not a list")
12017

    
12018
    if self.mode == constants.IALLOCATOR_MODE_RELOC:
12019
      assert self.relocate_from is not None
12020
      assert self.required_nodes == 1
12021

    
12022
      node2group = dict((name, ndata["group"])
12023
                        for (name, ndata) in self.in_data["nodes"].items())
12024

    
12025
      fn = compat.partial(self._NodesToGroups, node2group,
12026
                          self.in_data["nodegroups"])
12027

    
12028
      request_groups = fn(self.relocate_from)
12029
      result_groups = fn(rdict["result"])
12030

    
12031
      if result_groups != request_groups:
12032
        raise errors.OpExecError("Groups of nodes returned by iallocator (%s)"
12033
                                 " differ from original groups (%s)" %
12034
                                 (utils.CommaJoin(result_groups),
12035
                                  utils.CommaJoin(request_groups)))
12036

    
12037
    self.out_data = rdict
12038

    
12039
  @staticmethod
12040
  def _NodesToGroups(node2group, groups, nodes):
12041
    """Returns a list of unique group names for a list of nodes.
12042

12043
    @type node2group: dict
12044
    @param node2group: Map from node name to group UUID
12045
    @type groups: dict
12046
    @param groups: Group information
12047
    @type nodes: list
12048
    @param nodes: Node names
12049

12050
    """
12051
    result = set()
12052

    
12053
    for node in nodes:
12054
      try:
12055
        group_uuid = node2group[node]
12056
      except KeyError:
12057
        # Ignore unknown node
12058
        pass
12059
      else:
12060
        try:
12061
          group = groups[group_uuid]
12062
        except KeyError:
12063
          # Can't find group, let's use UUID
12064
          group_name = group_uuid
12065
        else:
12066
          group_name = group["name"]
12067

    
12068
        result.add(group_name)
12069

    
12070
    return sorted(result)
12071

    
12072

    
12073
class LUTestAllocator(NoHooksLU):
12074
  """Run allocator tests.
12075

12076
  This LU runs the allocator tests
12077

12078
  """
12079
  def CheckPrereq(self):
12080
    """Check prerequisites.
12081

12082
    This checks the opcode parameters depending on the director and mode test.
12083

12084
    """
12085
    if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
12086
      for attr in ["mem_size", "disks", "disk_template",
12087
                   "os", "tags", "nics", "vcpus"]:
12088
        if not hasattr(self.op, attr):
12089
          raise errors.OpPrereqError("Missing attribute '%s' on opcode input" %
12090
                                     attr, errors.ECODE_INVAL)
12091
      iname = self.cfg.ExpandInstanceName(self.op.name)
12092
      if iname is not None:
12093
        raise errors.OpPrereqError("Instance '%s' already in the cluster" %
12094
                                   iname, errors.ECODE_EXISTS)
12095
      if not isinstance(self.op.nics, list):
12096
        raise errors.OpPrereqError("Invalid parameter 'nics'",
12097
                                   errors.ECODE_INVAL)
12098
      if not isinstance(self.op.disks, list):
12099
        raise errors.OpPrereqError("Invalid parameter 'disks'",
12100
                                   errors.ECODE_INVAL)
12101
      for row in self.op.disks:
12102
        if (not isinstance(row, dict) or
12103
            "size" not in row or
12104
            not isinstance(row["size"], int) or
12105
            "mode" not in row or
12106
            row["mode"] not in ['r', 'w']):
12107
          raise errors.OpPrereqError("Invalid contents of the 'disks'"
12108
                                     " parameter", errors.ECODE_INVAL)
12109
      if self.op.hypervisor is None:
12110
        self.op.hypervisor = self.cfg.GetHypervisorType()
12111
    elif self.op.mode == constants.IALLOCATOR_MODE_RELOC:
12112
      fname = _ExpandInstanceName(self.cfg, self.op.name)
12113
      self.op.name = fname
12114
      self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes
12115
    elif self.op.mode == constants.IALLOCATOR_MODE_MEVAC:
12116
      if not hasattr(self.op, "evac_nodes"):
12117
        raise errors.OpPrereqError("Missing attribute 'evac_nodes' on"
12118
                                   " opcode input", errors.ECODE_INVAL)
12119
    else:
12120
      raise errors.OpPrereqError("Invalid test allocator mode '%s'" %
12121
                                 self.op.mode, errors.ECODE_INVAL)
12122

    
12123
    if self.op.direction == constants.IALLOCATOR_DIR_OUT:
12124
      if self.op.allocator is None:
12125
        raise errors.OpPrereqError("Missing allocator name",
12126
                                   errors.ECODE_INVAL)
12127
    elif self.op.direction != constants.IALLOCATOR_DIR_IN:
12128
      raise errors.OpPrereqError("Wrong allocator test '%s'" %
12129
                                 self.op.direction, errors.ECODE_INVAL)
12130

    
12131
  def Exec(self, feedback_fn):
12132
    """Run the allocator test.
12133

12134
    """
12135
    if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
12136
      ial = IAllocator(self.cfg, self.rpc,
12137
                       mode=self.op.mode,
12138
                       name=self.op.name,
12139
                       mem_size=self.op.mem_size,
12140
                       disks=self.op.disks,
12141
                       disk_template=self.op.disk_template,
12142
                       os=self.op.os,
12143
                       tags=self.op.tags,
12144
                       nics=self.op.nics,
12145
                       vcpus=self.op.vcpus,
12146
                       hypervisor=self.op.hypervisor,
12147
                       )
12148
    elif self.op.mode == constants.IALLOCATOR_MODE_RELOC:
12149
      ial = IAllocator(self.cfg, self.rpc,
12150
                       mode=self.op.mode,
12151
                       name=self.op.name,
12152
                       relocate_from=list(self.relocate_from),
12153
                       )
12154
    elif self.op.mode == constants.IALLOCATOR_MODE_MEVAC:
12155
      ial = IAllocator(self.cfg, self.rpc,
12156
                       mode=self.op.mode,
12157
                       evac_nodes=self.op.evac_nodes)
12158
    else:
12159
      raise errors.ProgrammerError("Uncatched mode %s in"
12160
                                   " LUTestAllocator.Exec", self.op.mode)
12161

    
12162
    if self.op.direction == constants.IALLOCATOR_DIR_IN:
12163
      result = ial.in_text
12164
    else:
12165
      ial.Run(self.op.allocator, validate=False)
12166
      result = ial.out_text
12167
    return result
12168

    
12169

    
12170
#: Query type implementations
12171
_QUERY_IMPL = {
12172
  constants.QR_INSTANCE: _InstanceQuery,
12173
  constants.QR_NODE: _NodeQuery,
12174
  constants.QR_GROUP: _GroupQuery,
12175
  constants.QR_OS: _OsQuery,
12176
  }
12177

    
12178
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
12179

    
12180

    
12181
def _GetQueryImplementation(name):
12182
  """Returns the implemtnation for a query type.
12183

12184
  @param name: Query type, must be one of L{constants.QR_VIA_OP}
12185

12186
  """
12187
  try:
12188
    return _QUERY_IMPL[name]
12189
  except KeyError:
12190
    raise errors.OpPrereqError("Unknown query resource '%s'" % name,
12191
                               errors.ECODE_INVAL)