Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ bd315bfa

History | View | Annotate | Download (276.4 kB)

1
#
2
#
3

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

    
21

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

    
24
# pylint: disable-msg=W0613,W0201
25

    
26
import os
27
import os.path
28
import time
29
import re
30
import platform
31
import logging
32
import copy
33

    
34
from ganeti import ssh
35
from ganeti import utils
36
from ganeti import errors
37
from ganeti import hypervisor
38
from ganeti import locking
39
from ganeti import constants
40
from ganeti import objects
41
from ganeti import serializer
42
from ganeti import ssconf
43

    
44

    
45
class LogicalUnit(object):
46
  """Logical Unit base class.
47

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

57
  Note that all commands require root permissions.
58

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

62
  """
63
  HPATH = None
64
  HTYPE = None
65
  _OP_REQP = []
66
  REQ_BGL = True
67

    
68
  def __init__(self, processor, op, context, rpc):
69
    """Constructor for LogicalUnit.
70

71
    This needs to be overridden in derived classes in order to check op
72
    validity.
73

74
    """
75
    self.proc = processor
76
    self.op = op
77
    self.cfg = context.cfg
78
    self.context = context
79
    self.rpc = rpc
80
    # Dicts used to declare locking needs to mcpu
81
    self.needed_locks = None
82
    self.acquired_locks = {}
83
    self.share_locks = dict.fromkeys(locking.LEVELS, 0)
84
    self.add_locks = {}
85
    self.remove_locks = {}
86
    # Used to force good behavior when calling helper functions
87
    self.recalculate_locks = {}
88
    self.__ssh = None
89
    # logging
90
    self.LogWarning = processor.LogWarning
91
    self.LogInfo = processor.LogInfo
92
    self.LogStep = processor.LogStep
93
    # support for dry-run
94
    self.dry_run_result = None
95

    
96
    # Tasklets
97
    self.tasklets = None
98

    
99
    for attr_name in self._OP_REQP:
100
      attr_val = getattr(op, attr_name, None)
101
      if attr_val is None:
102
        raise errors.OpPrereqError("Required parameter '%s' missing" %
103
                                   attr_name)
104

    
105
    self.CheckArguments()
106

    
107
  def __GetSSH(self):
108
    """Returns the SshRunner object
109

110
    """
111
    if not self.__ssh:
112
      self.__ssh = ssh.SshRunner(self.cfg.GetClusterName())
113
    return self.__ssh
114

    
115
  ssh = property(fget=__GetSSH)
116

    
117
  def CheckArguments(self):
118
    """Check syntactic validity for the opcode arguments.
119

120
    This method is for doing a simple syntactic check and ensure
121
    validity of opcode parameters, without any cluster-related
122
    checks. While the same can be accomplished in ExpandNames and/or
123
    CheckPrereq, doing these separate is better because:
124

125
      - ExpandNames is left as as purely a lock-related function
126
      - CheckPrereq is run after we have acquired locks (and possible
127
        waited for them)
128

129
    The function is allowed to change the self.op attribute so that
130
    later methods can no longer worry about missing parameters.
131

132
    """
133
    pass
134

    
135
  def ExpandNames(self):
136
    """Expand names for this LU.
137

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

143
    LUs which implement this method must also populate the self.needed_locks
144
    member, as a dict with lock levels as keys, and a list of needed lock names
145
    as values. Rules:
146

147
      - use an empty dict if you don't need any lock
148
      - if you don't need any lock at a particular level omit that level
149
      - don't put anything for the BGL level
150
      - if you want all locks at a level use locking.ALL_SET as a value
151

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

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

160
    Examples::
161

162
      # Acquire all nodes and one instance
163
      self.needed_locks = {
164
        locking.LEVEL_NODE: locking.ALL_SET,
165
        locking.LEVEL_INSTANCE: ['instance1.example.tld'],
166
      }
167
      # Acquire just two nodes
168
      self.needed_locks = {
169
        locking.LEVEL_NODE: ['node1.example.tld', 'node2.example.tld'],
170
      }
171
      # Acquire no locks
172
      self.needed_locks = {} # No, you can't leave it to the default value None
173

174
    """
175
    # The implementation of this method is mandatory only if the new LU is
176
    # concurrent, so that old LUs don't need to be changed all at the same
177
    # time.
178
    if self.REQ_BGL:
179
      self.needed_locks = {} # Exclusive LUs don't need locks.
180
    else:
181
      raise NotImplementedError
182

    
183
  def DeclareLocks(self, level):
184
    """Declare LU locking needs for a level
185

186
    While most LUs can just declare their locking needs at ExpandNames time,
187
    sometimes there's the need to calculate some locks after having acquired
188
    the ones before. This function is called just before acquiring locks at a
189
    particular level, but after acquiring the ones at lower levels, and permits
190
    such calculations. It can be used to modify self.needed_locks, and by
191
    default it does nothing.
192

193
    This function is only called if you have something already set in
194
    self.needed_locks for the level.
195

196
    @param level: Locking level which is going to be locked
197
    @type level: member of ganeti.locking.LEVELS
198

199
    """
200

    
201
  def CheckPrereq(self):
202
    """Check prerequisites for this LU.
203

204
    This method should check that the prerequisites for the execution
205
    of this LU are fulfilled. It can do internode communication, but
206
    it should be idempotent - no cluster or system changes are
207
    allowed.
208

209
    The method should raise errors.OpPrereqError in case something is
210
    not fulfilled. Its return value is ignored.
211

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

215
    """
216
    if self.tasklets is not None:
217
      for (idx, tl) in enumerate(self.tasklets):
218
        logging.debug("Checking prerequisites for tasklet %s/%s",
219
                      idx + 1, len(self.tasklets))
220
        tl.CheckPrereq()
221
    else:
222
      raise NotImplementedError
223

    
224
  def Exec(self, feedback_fn):
225
    """Execute the LU.
226

227
    This method should implement the actual work. It should raise
228
    errors.OpExecError for failures that are somewhat dealt with in
229
    code, or expected.
230

231
    """
232
    if self.tasklets is not None:
233
      for (idx, tl) in enumerate(self.tasklets):
234
        logging.debug("Executing tasklet %s/%s", idx + 1, len(self.tasklets))
235
        tl.Exec(feedback_fn)
236
    else:
237
      raise NotImplementedError
238

    
239
  def BuildHooksEnv(self):
240
    """Build hooks environment for this LU.
241

242
    This method should return a three-node tuple consisting of: a dict
243
    containing the environment that will be used for running the
244
    specific hook for this LU, a list of node names on which the hook
245
    should run before the execution, and a list of node names on which
246
    the hook should run after the execution.
247

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

253
    No nodes should be returned as an empty list (and not None).
254

255
    Note that if the HPATH for a LU class is None, this function will
256
    not be called.
257

258
    """
259
    raise NotImplementedError
260

    
261
  def HooksCallBack(self, phase, hook_results, feedback_fn, lu_result):
262
    """Notify the LU about the results of its hooks.
263

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

270
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
271
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
272
    @param hook_results: the results of the multi-node hooks rpc call
273
    @param feedback_fn: function used send feedback back to the caller
274
    @param lu_result: the previous Exec result this LU had, or None
275
        in the PRE phase
276
    @return: the new Exec result, based on the previous result
277
        and hook results
278

279
    """
280
    return lu_result
281

    
282
  def _ExpandAndLockInstance(self):
283
    """Helper function to expand and lock an instance.
284

285
    Many LUs that work on an instance take its name in self.op.instance_name
286
    and need to expand it and then declare the expanded name for locking. This
287
    function does it, and then updates self.op.instance_name to the expanded
288
    name. It also initializes needed_locks as a dict, if this hasn't been done
289
    before.
290

291
    """
292
    if self.needed_locks is None:
293
      self.needed_locks = {}
294
    else:
295
      assert locking.LEVEL_INSTANCE not in self.needed_locks, \
296
        "_ExpandAndLockInstance called with instance-level locks set"
297
    expanded_name = self.cfg.ExpandInstanceName(self.op.instance_name)
298
    if expanded_name is None:
299
      raise errors.OpPrereqError("Instance '%s' not known" %
300
                                  self.op.instance_name)
301
    self.needed_locks[locking.LEVEL_INSTANCE] = expanded_name
302
    self.op.instance_name = expanded_name
303

    
304
  def _LockInstancesNodes(self, primary_only=False):
305
    """Helper function to declare instances' nodes for locking.
306

307
    This function should be called after locking one or more instances to lock
308
    their nodes. Its effect is populating self.needed_locks[locking.LEVEL_NODE]
309
    with all primary or secondary nodes for instances already locked and
310
    present in self.needed_locks[locking.LEVEL_INSTANCE].
311

312
    It should be called from DeclareLocks, and for safety only works if
313
    self.recalculate_locks[locking.LEVEL_NODE] is set.
314

315
    In the future it may grow parameters to just lock some instance's nodes, or
316
    to just lock primaries or secondary nodes, if needed.
317

318
    If should be called in DeclareLocks in a way similar to::
319

320
      if level == locking.LEVEL_NODE:
321
        self._LockInstancesNodes()
322

323
    @type primary_only: boolean
324
    @param primary_only: only lock primary nodes of locked instances
325

326
    """
327
    assert locking.LEVEL_NODE in self.recalculate_locks, \
328
      "_LockInstancesNodes helper function called with no nodes to recalculate"
329

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

    
332
    # For now we'll replace self.needed_locks[locking.LEVEL_NODE], but in the
333
    # future we might want to have different behaviors depending on the value
334
    # of self.recalculate_locks[locking.LEVEL_NODE]
335
    wanted_nodes = []
336
    for instance_name in self.acquired_locks[locking.LEVEL_INSTANCE]:
337
      instance = self.context.cfg.GetInstanceInfo(instance_name)
338
      wanted_nodes.append(instance.primary_node)
339
      if not primary_only:
340
        wanted_nodes.extend(instance.secondary_nodes)
341

    
342
    if self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_REPLACE:
343
      self.needed_locks[locking.LEVEL_NODE] = wanted_nodes
344
    elif self.recalculate_locks[locking.LEVEL_NODE] == constants.LOCKS_APPEND:
345
      self.needed_locks[locking.LEVEL_NODE].extend(wanted_nodes)
346

    
347
    del self.recalculate_locks[locking.LEVEL_NODE]
348

    
349

    
350
class NoHooksLU(LogicalUnit):
351
  """Simple LU which runs no hooks.
352

353
  This LU is intended as a parent for other LogicalUnits which will
354
  run no hooks, in order to reduce duplicate code.
355

356
  """
357
  HPATH = None
358
  HTYPE = None
359

    
360

    
361
class Tasklet:
362
  """Tasklet base class.
363

364
  Tasklets are subcomponents for LUs. LUs can consist entirely of tasklets or
365
  they can mix legacy code with tasklets. Locking needs to be done in the LU,
366
  tasklets know nothing about locks.
367

368
  Subclasses must follow these rules:
369
    - Implement CheckPrereq
370
    - Implement Exec
371

372
  """
373
  def __init__(self, lu):
374
    self.lu = lu
375

    
376
    # Shortcuts
377
    self.cfg = lu.cfg
378
    self.rpc = lu.rpc
379

    
380
  def CheckPrereq(self):
381
    """Check prerequisites for this tasklets.
382

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

387
    The method should raise errors.OpPrereqError in case something is not
388
    fulfilled. Its return value is ignored.
389

390
    This method should also update all parameters to their canonical form if it
391
    hasn't been done before.
392

393
    """
394
    raise NotImplementedError
395

    
396
  def Exec(self, feedback_fn):
397
    """Execute the tasklet.
398

399
    This method should implement the actual work. It should raise
400
    errors.OpExecError for failures that are somewhat dealt with in code, or
401
    expected.
402

403
    """
404
    raise NotImplementedError
405

    
406

    
407
def _GetWantedNodes(lu, nodes):
408
  """Returns list of checked and expanded node names.
409

410
  @type lu: L{LogicalUnit}
411
  @param lu: the logical unit on whose behalf we execute
412
  @type nodes: list
413
  @param nodes: list of node names or None for all nodes
414
  @rtype: list
415
  @return: the list of nodes, sorted
416
  @raise errors.OpProgrammerError: if the nodes parameter is wrong type
417

418
  """
419
  if not isinstance(nodes, list):
420
    raise errors.OpPrereqError("Invalid argument type 'nodes'")
421

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

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

    
433
  return utils.NiceSort(wanted)
434

    
435

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

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

448
  """
449
  if not isinstance(instances, list):
450
    raise errors.OpPrereqError("Invalid argument type 'instances'")
451

    
452
  if instances:
453
    wanted = []
454

    
455
    for name in instances:
456
      instance = lu.cfg.ExpandInstanceName(name)
457
      if instance is None:
458
        raise errors.OpPrereqError("No such instance name '%s'" % name)
459
      wanted.append(instance)
460

    
461
  else:
462
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
463
  return wanted
464

    
465

    
466
def _CheckOutputFields(static, dynamic, selected):
467
  """Checks whether all selected fields are valid.
468

469
  @type static: L{utils.FieldSet}
470
  @param static: static fields set
471
  @type dynamic: L{utils.FieldSet}
472
  @param dynamic: dynamic fields set
473

474
  """
475
  f = utils.FieldSet()
476
  f.Extend(static)
477
  f.Extend(dynamic)
478

    
479
  delta = f.NonMatching(selected)
480
  if delta:
481
    raise errors.OpPrereqError("Unknown output fields selected: %s"
482
                               % ",".join(delta))
483

    
484

    
485
def _CheckBooleanOpField(op, name):
486
  """Validates boolean opcode parameters.
487

488
  This will ensure that an opcode parameter is either a boolean value,
489
  or None (but that it always exists).
490

491
  """
492
  val = getattr(op, name, None)
493
  if not (val is None or isinstance(val, bool)):
494
    raise errors.OpPrereqError("Invalid boolean parameter '%s' (%s)" %
495
                               (name, str(val)))
496
  setattr(op, name, val)
497

    
498

    
499
def _CheckNodeOnline(lu, node):
500
  """Ensure that a given node is online.
501

502
  @param lu: the LU on behalf of which we make the check
503
  @param node: the node to check
504
  @raise errors.OpPrereqError: if the node is offline
505

506
  """
507
  if lu.cfg.GetNodeInfo(node).offline:
508
    raise errors.OpPrereqError("Can't use offline node %s" % node)
509

    
510

    
511
def _CheckNodeNotDrained(lu, node):
512
  """Ensure that a given node is not drained.
513

514
  @param lu: the LU on behalf of which we make the check
515
  @param node: the node to check
516
  @raise errors.OpPrereqError: if the node is drained
517

518
  """
519
  if lu.cfg.GetNodeInfo(node).drained:
520
    raise errors.OpPrereqError("Can't use drained node %s" % node)
521

    
522

    
523
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
524
                          memory, vcpus, nics, disk_template, disks,
525
                          bep, hvp, hypervisor_name):
526
  """Builds instance related env variables for hooks
527

528
  This builds the hook environment from individual variables.
529

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

560
  """
561
  if status:
562
    str_status = "up"
563
  else:
564
    str_status = "down"
565
  env = {
566
    "OP_TARGET": name,
567
    "INSTANCE_NAME": name,
568
    "INSTANCE_PRIMARY": primary_node,
569
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
570
    "INSTANCE_OS_TYPE": os_type,
571
    "INSTANCE_STATUS": str_status,
572
    "INSTANCE_MEMORY": memory,
573
    "INSTANCE_VCPUS": vcpus,
574
    "INSTANCE_DISK_TEMPLATE": disk_template,
575
    "INSTANCE_HYPERVISOR": hypervisor_name,
576
  }
577

    
578
  if nics:
579
    nic_count = len(nics)
580
    for idx, (ip, mac, mode, link) in enumerate(nics):
581
      if ip is None:
582
        ip = ""
583
      env["INSTANCE_NIC%d_IP" % idx] = ip
584
      env["INSTANCE_NIC%d_MAC" % idx] = mac
585
      env["INSTANCE_NIC%d_MODE" % idx] = mode
586
      env["INSTANCE_NIC%d_LINK" % idx] = link
587
      if mode == constants.NIC_MODE_BRIDGED:
588
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
589
  else:
590
    nic_count = 0
591

    
592
  env["INSTANCE_NIC_COUNT"] = nic_count
593

    
594
  if disks:
595
    disk_count = len(disks)
596
    for idx, (size, mode) in enumerate(disks):
597
      env["INSTANCE_DISK%d_SIZE" % idx] = size
598
      env["INSTANCE_DISK%d_MODE" % idx] = mode
599
  else:
600
    disk_count = 0
601

    
602
  env["INSTANCE_DISK_COUNT"] = disk_count
603

    
604
  for source, kind in [(bep, "BE"), (hvp, "HV")]:
605
    for key, value in source.items():
606
      env["INSTANCE_%s_%s" % (kind, key)] = value
607

    
608
  return env
609

    
610

    
611
def _NICListToTuple(lu, nics):
612
  """Build a list of nic information tuples.
613

614
  This list is suitable to be passed to _BuildInstanceHookEnv or as a return
615
  value in LUQueryInstanceData.
616

617
  @type lu:  L{LogicalUnit}
618
  @param lu: the logical unit on whose behalf we execute
619
  @type nics: list of L{objects.NIC}
620
  @param nics: list of nics to convert to hooks tuples
621

622
  """
623
  hooks_nics = []
624
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT]
625
  for nic in nics:
626
    ip = nic.ip
627
    mac = nic.mac
628
    filled_params = objects.FillDict(c_nicparams, nic.nicparams)
629
    mode = filled_params[constants.NIC_MODE]
630
    link = filled_params[constants.NIC_LINK]
631
    hooks_nics.append((ip, mac, mode, link))
632
  return hooks_nics
633

    
634

    
635
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
636
  """Builds instance related env variables for hooks from an object.
637

638
  @type lu: L{LogicalUnit}
639
  @param lu: the logical unit on whose behalf we execute
640
  @type instance: L{objects.Instance}
641
  @param instance: the instance for which we should build the
642
      environment
643
  @type override: dict
644
  @param override: dictionary with key/values that will override
645
      our values
646
  @rtype: dict
647
  @return: the hook environment dictionary
648

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

    
672

    
673
def _AdjustCandidatePool(lu):
674
  """Adjust the candidate pool after node operations.
675

676
  """
677
  mod_list = lu.cfg.MaintainCandidatePool()
678
  if mod_list:
679
    lu.LogInfo("Promoted nodes to master candidate role: %s",
680
               ", ".join(node.name for node in mod_list))
681
    for name in mod_list:
682
      lu.context.ReaddNode(name)
683
  mc_now, mc_max = lu.cfg.GetMasterCandidateStats()
684
  if mc_now > mc_max:
685
    lu.LogInfo("Note: more nodes are candidates (%d) than desired (%d)" %
686
               (mc_now, mc_max))
687

    
688

    
689
def _CheckNicsBridgesExist(lu, target_nics, target_node,
690
                               profile=constants.PP_DEFAULT):
691
  """Check that the brigdes needed by a list of nics exist.
692

693
  """
694
  c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile]
695
  paramslist = [objects.FillDict(c_nicparams, nic.nicparams)
696
                for nic in target_nics]
697
  brlist = [params[constants.NIC_LINK] for params in paramslist
698
            if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED]
699
  if brlist:
700
    result = lu.rpc.call_bridges_exist(target_node, brlist)
701
    result.Raise("Error checking bridges on destination node '%s'" %
702
                 target_node, prereq=True)
703

    
704

    
705
def _CheckInstanceBridgesExist(lu, instance, node=None):
706
  """Check that the brigdes needed by an instance exist.
707

708
  """
709
  if node is None:
710
    node = instance.primary_node
711
  _CheckNicsBridgesExist(lu, instance.nics, node)
712

    
713

    
714
def _GetNodePrimaryInstances(cfg, node_name):
715
  """Returns primary instances on a node.
716

717
  """
718
  instances = []
719

    
720
  for (_, inst) in cfg.GetAllInstancesInfo().iteritems():
721
    if node_name == inst.primary_node:
722
      instances.append(inst)
723

    
724
  return instances
725

    
726

    
727
def _GetNodeSecondaryInstances(cfg, node_name):
728
  """Returns secondary instances on a node.
729

730
  """
731
  instances = []
732

    
733
  for (_, inst) in cfg.GetAllInstancesInfo().iteritems():
734
    if node_name in inst.secondary_nodes:
735
      instances.append(inst)
736

    
737
  return instances
738

    
739

    
740
def _GetStorageTypeArgs(cfg, storage_type):
741
  """Returns the arguments for a storage type.
742

743
  """
744
  # Special case for file storage
745
  if storage_type == constants.ST_FILE:
746
    # storage.FileStorage wants a list of storage directories
747
    return [[cfg.GetFileStorageDir()]]
748

    
749
  return []
750

    
751

    
752
class LUPostInitCluster(LogicalUnit):
753
  """Logical unit for running hooks after cluster initialization.
754

755
  """
756
  HPATH = "cluster-init"
757
  HTYPE = constants.HTYPE_CLUSTER
758
  _OP_REQP = []
759

    
760
  def BuildHooksEnv(self):
761
    """Build hooks env.
762

763
    """
764
    env = {"OP_TARGET": self.cfg.GetClusterName()}
765
    mn = self.cfg.GetMasterNode()
766
    return env, [], [mn]
767

    
768
  def CheckPrereq(self):
769
    """No prerequisites to check.
770

771
    """
772
    return True
773

    
774
  def Exec(self, feedback_fn):
775
    """Nothing to do.
776

777
    """
778
    return True
779

    
780

    
781
class LUDestroyCluster(NoHooksLU):
782
  """Logical unit for destroying the cluster.
783

784
  """
785
  _OP_REQP = []
786

    
787
  def CheckPrereq(self):
788
    """Check prerequisites.
789

790
    This checks whether the cluster is empty.
791

792
    Any errors are signaled by raising errors.OpPrereqError.
793

794
    """
795
    master = self.cfg.GetMasterNode()
796

    
797
    nodelist = self.cfg.GetNodeList()
798
    if len(nodelist) != 1 or nodelist[0] != master:
799
      raise errors.OpPrereqError("There are still %d node(s) in"
800
                                 " this cluster." % (len(nodelist) - 1))
801
    instancelist = self.cfg.GetInstanceList()
802
    if instancelist:
803
      raise errors.OpPrereqError("There are still %d instance(s) in"
804
                                 " this cluster." % len(instancelist))
805

    
806
  def Exec(self, feedback_fn):
807
    """Destroys the cluster.
808

809
    """
810
    master = self.cfg.GetMasterNode()
811
    result = self.rpc.call_node_stop_master(master, False)
812
    result.Raise("Could not disable the master role")
813
    priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
814
    utils.CreateBackup(priv_key)
815
    utils.CreateBackup(pub_key)
816
    return master
817

    
818

    
819
class LUVerifyCluster(LogicalUnit):
820
  """Verifies the cluster status.
821

822
  """
823
  HPATH = "cluster-verify"
824
  HTYPE = constants.HTYPE_CLUSTER
825
  _OP_REQP = ["skip_checks"]
826
  REQ_BGL = False
827

    
828
  def ExpandNames(self):
829
    self.needed_locks = {
830
      locking.LEVEL_NODE: locking.ALL_SET,
831
      locking.LEVEL_INSTANCE: locking.ALL_SET,
832
    }
833
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
834

    
835
  def _VerifyNode(self, nodeinfo, file_list, local_cksum,
836
                  node_result, feedback_fn, master_files,
837
                  drbd_map, vg_name):
838
    """Run multiple tests against a node.
839

840
    Test list:
841

842
      - compares ganeti version
843
      - checks vg existence and size > 20G
844
      - checks config file checksum
845
      - checks ssh to other nodes
846

847
    @type nodeinfo: L{objects.Node}
848
    @param nodeinfo: the node to check
849
    @param file_list: required list of files
850
    @param local_cksum: dictionary of local files and their checksums
851
    @param node_result: the results from the node
852
    @param feedback_fn: function used to accumulate results
853
    @param master_files: list of files that only masters should have
854
    @param drbd_map: the useddrbd minors for this node, in
855
        form of minor: (instance, must_exist) which correspond to instances
856
        and their running status
857
    @param vg_name: Ganeti Volume Group (result of self.cfg.GetVGName())
858

859
    """
860
    node = nodeinfo.name
861

    
862
    # main result, node_result should be a non-empty dict
863
    if not node_result or not isinstance(node_result, dict):
864
      feedback_fn("  - ERROR: unable to verify node %s." % (node,))
865
      return True
866

    
867
    # compares ganeti version
868
    local_version = constants.PROTOCOL_VERSION
869
    remote_version = node_result.get('version', None)
870
    if not (remote_version and isinstance(remote_version, (list, tuple)) and
871
            len(remote_version) == 2):
872
      feedback_fn("  - ERROR: connection to %s failed" % (node))
873
      return True
874

    
875
    if local_version != remote_version[0]:
876
      feedback_fn("  - ERROR: incompatible protocol versions: master %s,"
877
                  " node %s %s" % (local_version, node, remote_version[0]))
878
      return True
879

    
880
    # node seems compatible, we can actually try to look into its results
881

    
882
    bad = False
883

    
884
    # full package version
885
    if constants.RELEASE_VERSION != remote_version[1]:
886
      feedback_fn("  - WARNING: software version mismatch: master %s,"
887
                  " node %s %s" %
888
                  (constants.RELEASE_VERSION, node, remote_version[1]))
889

    
890
    # checks vg existence and size > 20G
891
    if vg_name is not None:
892
      vglist = node_result.get(constants.NV_VGLIST, None)
893
      if not vglist:
894
        feedback_fn("  - ERROR: unable to check volume groups on node %s." %
895
                        (node,))
896
        bad = True
897
      else:
898
        vgstatus = utils.CheckVolumeGroupSize(vglist, vg_name,
899
                                              constants.MIN_VG_SIZE)
900
        if vgstatus:
901
          feedback_fn("  - ERROR: %s on node %s" % (vgstatus, node))
902
          bad = True
903

    
904
    # checks config file checksum
905

    
906
    remote_cksum = node_result.get(constants.NV_FILELIST, None)
907
    if not isinstance(remote_cksum, dict):
908
      bad = True
909
      feedback_fn("  - ERROR: node hasn't returned file checksum data")
910
    else:
911
      for file_name in file_list:
912
        node_is_mc = nodeinfo.master_candidate
913
        must_have_file = file_name not in master_files
914
        if file_name not in remote_cksum:
915
          if node_is_mc or must_have_file:
916
            bad = True
917
            feedback_fn("  - ERROR: file '%s' missing" % file_name)
918
        elif remote_cksum[file_name] != local_cksum[file_name]:
919
          if node_is_mc or must_have_file:
920
            bad = True
921
            feedback_fn("  - ERROR: file '%s' has wrong checksum" % file_name)
922
          else:
923
            # not candidate and this is not a must-have file
924
            bad = True
925
            feedback_fn("  - ERROR: file '%s' should not exist on non master"
926
                        " candidates (and the file is outdated)" % file_name)
927
        else:
928
          # all good, except non-master/non-must have combination
929
          if not node_is_mc and not must_have_file:
930
            feedback_fn("  - ERROR: file '%s' should not exist on non master"
931
                        " candidates" % file_name)
932

    
933
    # checks ssh to any
934

    
935
    if constants.NV_NODELIST not in node_result:
936
      bad = True
937
      feedback_fn("  - ERROR: node hasn't returned node ssh connectivity data")
938
    else:
939
      if node_result[constants.NV_NODELIST]:
940
        bad = True
941
        for node in node_result[constants.NV_NODELIST]:
942
          feedback_fn("  - ERROR: ssh communication with node '%s': %s" %
943
                          (node, node_result[constants.NV_NODELIST][node]))
944

    
945
    if constants.NV_NODENETTEST not in node_result:
946
      bad = True
947
      feedback_fn("  - ERROR: node hasn't returned node tcp connectivity data")
948
    else:
949
      if node_result[constants.NV_NODENETTEST]:
950
        bad = True
951
        nlist = utils.NiceSort(node_result[constants.NV_NODENETTEST].keys())
952
        for node in nlist:
953
          feedback_fn("  - ERROR: tcp communication with node '%s': %s" %
954
                          (node, node_result[constants.NV_NODENETTEST][node]))
955

    
956
    hyp_result = node_result.get(constants.NV_HYPERVISOR, None)
957
    if isinstance(hyp_result, dict):
958
      for hv_name, hv_result in hyp_result.iteritems():
959
        if hv_result is not None:
960
          feedback_fn("  - ERROR: hypervisor %s verify failure: '%s'" %
961
                      (hv_name, hv_result))
962

    
963
    # check used drbd list
964
    if vg_name is not None:
965
      used_minors = node_result.get(constants.NV_DRBDLIST, [])
966
      if not isinstance(used_minors, (tuple, list)):
967
        feedback_fn("  - ERROR: cannot parse drbd status file: %s" %
968
                    str(used_minors))
969
      else:
970
        for minor, (iname, must_exist) in drbd_map.items():
971
          if minor not in used_minors and must_exist:
972
            feedback_fn("  - ERROR: drbd minor %d of instance %s is"
973
                        " not active" % (minor, iname))
974
            bad = True
975
        for minor in used_minors:
976
          if minor not in drbd_map:
977
            feedback_fn("  - ERROR: unallocated drbd minor %d is in use" %
978
                        minor)
979
            bad = True
980

    
981
    return bad
982

    
983
  def _VerifyInstance(self, instance, instanceconfig, node_vol_is,
984
                      node_instance, feedback_fn, n_offline):
985
    """Verify an instance.
986

987
    This function checks to see if the required block devices are
988
    available on the instance's node.
989

990
    """
991
    bad = False
992

    
993
    node_current = instanceconfig.primary_node
994

    
995
    node_vol_should = {}
996
    instanceconfig.MapLVsByNode(node_vol_should)
997

    
998
    for node in node_vol_should:
999
      if node in n_offline:
1000
        # ignore missing volumes on offline nodes
1001
        continue
1002
      for volume in node_vol_should[node]:
1003
        if node not in node_vol_is or volume not in node_vol_is[node]:
1004
          feedback_fn("  - ERROR: volume %s missing on node %s" %
1005
                          (volume, node))
1006
          bad = True
1007

    
1008
    if instanceconfig.admin_up:
1009
      if ((node_current not in node_instance or
1010
          not instance in node_instance[node_current]) and
1011
          node_current not in n_offline):
1012
        feedback_fn("  - ERROR: instance %s not running on node %s" %
1013
                        (instance, node_current))
1014
        bad = True
1015

    
1016
    for node in node_instance:
1017
      if (not node == node_current):
1018
        if instance in node_instance[node]:
1019
          feedback_fn("  - ERROR: instance %s should not run on node %s" %
1020
                          (instance, node))
1021
          bad = True
1022

    
1023
    return bad
1024

    
1025
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is, feedback_fn):
1026
    """Verify if there are any unknown volumes in the cluster.
1027

1028
    The .os, .swap and backup volumes are ignored. All other volumes are
1029
    reported as unknown.
1030

1031
    """
1032
    bad = False
1033

    
1034
    for node in node_vol_is:
1035
      for volume in node_vol_is[node]:
1036
        if node not in node_vol_should or volume not in node_vol_should[node]:
1037
          feedback_fn("  - ERROR: volume %s on node %s should not exist" %
1038
                      (volume, node))
1039
          bad = True
1040
    return bad
1041

    
1042
  def _VerifyOrphanInstances(self, instancelist, node_instance, feedback_fn):
1043
    """Verify the list of running instances.
1044

1045
    This checks what instances are running but unknown to the cluster.
1046

1047
    """
1048
    bad = False
1049
    for node in node_instance:
1050
      for runninginstance in node_instance[node]:
1051
        if runninginstance not in instancelist:
1052
          feedback_fn("  - ERROR: instance %s on node %s should not exist" %
1053
                          (runninginstance, node))
1054
          bad = True
1055
    return bad
1056

    
1057
  def _VerifyNPlusOneMemory(self, node_info, instance_cfg, feedback_fn):
1058
    """Verify N+1 Memory Resilience.
1059

1060
    Check that if one single node dies we can still start all the instances it
1061
    was primary for.
1062

1063
    """
1064
    bad = False
1065

    
1066
    for node, nodeinfo in node_info.iteritems():
1067
      # This code checks that every node which is now listed as secondary has
1068
      # enough memory to host all instances it is supposed to should a single
1069
      # other node in the cluster fail.
1070
      # FIXME: not ready for failover to an arbitrary node
1071
      # FIXME: does not support file-backed instances
1072
      # WARNING: we currently take into account down instances as well as up
1073
      # ones, considering that even if they're down someone might want to start
1074
      # them even in the event of a node failure.
1075
      for prinode, instances in nodeinfo['sinst-by-pnode'].iteritems():
1076
        needed_mem = 0
1077
        for instance in instances:
1078
          bep = self.cfg.GetClusterInfo().FillBE(instance_cfg[instance])
1079
          if bep[constants.BE_AUTO_BALANCE]:
1080
            needed_mem += bep[constants.BE_MEMORY]
1081
        if nodeinfo['mfree'] < needed_mem:
1082
          feedback_fn("  - ERROR: not enough memory on node %s to accommodate"
1083
                      " failovers should node %s fail" % (node, prinode))
1084
          bad = True
1085
    return bad
1086

    
1087
  def CheckPrereq(self):
1088
    """Check prerequisites.
1089

1090
    Transform the list of checks we're going to skip into a set and check that
1091
    all its members are valid.
1092

1093
    """
1094
    self.skip_set = frozenset(self.op.skip_checks)
1095
    if not constants.VERIFY_OPTIONAL_CHECKS.issuperset(self.skip_set):
1096
      raise errors.OpPrereqError("Invalid checks to be skipped specified")
1097

    
1098
  def BuildHooksEnv(self):
1099
    """Build hooks env.
1100

1101
    Cluster-Verify hooks just ran in the post phase and their failure makes
1102
    the output be logged in the verify output and the verification to fail.
1103

1104
    """
1105
    all_nodes = self.cfg.GetNodeList()
1106
    env = {
1107
      "CLUSTER_TAGS": " ".join(self.cfg.GetClusterInfo().GetTags())
1108
      }
1109
    for node in self.cfg.GetAllNodesInfo().values():
1110
      env["NODE_TAGS_%s" % node.name] = " ".join(node.GetTags())
1111

    
1112
    return env, [], all_nodes
1113

    
1114
  def Exec(self, feedback_fn):
1115
    """Verify integrity of cluster, performing various test on nodes.
1116

1117
    """
1118
    bad = False
1119
    feedback_fn("* Verifying global settings")
1120
    for msg in self.cfg.VerifyConfig():
1121
      feedback_fn("  - ERROR: %s" % msg)
1122

    
1123
    vg_name = self.cfg.GetVGName()
1124
    hypervisors = self.cfg.GetClusterInfo().enabled_hypervisors
1125
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
1126
    nodeinfo = [self.cfg.GetNodeInfo(nname) for nname in nodelist]
1127
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
1128
    instanceinfo = dict((iname, self.cfg.GetInstanceInfo(iname))
1129
                        for iname in instancelist)
1130
    i_non_redundant = [] # Non redundant instances
1131
    i_non_a_balanced = [] # Non auto-balanced instances
1132
    n_offline = [] # List of offline nodes
1133
    n_drained = [] # List of nodes being drained
1134
    node_volume = {}
1135
    node_instance = {}
1136
    node_info = {}
1137
    instance_cfg = {}
1138

    
1139
    # FIXME: verify OS list
1140
    # do local checksums
1141
    master_files = [constants.CLUSTER_CONF_FILE]
1142

    
1143
    file_names = ssconf.SimpleStore().GetFileList()
1144
    file_names.append(constants.SSL_CERT_FILE)
1145
    file_names.append(constants.RAPI_CERT_FILE)
1146
    file_names.extend(master_files)
1147

    
1148
    local_checksums = utils.FingerprintFiles(file_names)
1149

    
1150
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
1151
    node_verify_param = {
1152
      constants.NV_FILELIST: file_names,
1153
      constants.NV_NODELIST: [node.name for node in nodeinfo
1154
                              if not node.offline],
1155
      constants.NV_HYPERVISOR: hypervisors,
1156
      constants.NV_NODENETTEST: [(node.name, node.primary_ip,
1157
                                  node.secondary_ip) for node in nodeinfo
1158
                                 if not node.offline],
1159
      constants.NV_INSTANCELIST: hypervisors,
1160
      constants.NV_VERSION: None,
1161
      constants.NV_HVINFO: self.cfg.GetHypervisorType(),
1162
      }
1163
    if vg_name is not None:
1164
      node_verify_param[constants.NV_VGLIST] = None
1165
      node_verify_param[constants.NV_LVLIST] = vg_name
1166
      node_verify_param[constants.NV_DRBDLIST] = None
1167
    all_nvinfo = self.rpc.call_node_verify(nodelist, node_verify_param,
1168
                                           self.cfg.GetClusterName())
1169

    
1170
    cluster = self.cfg.GetClusterInfo()
1171
    master_node = self.cfg.GetMasterNode()
1172
    all_drbd_map = self.cfg.ComputeDRBDMap()
1173

    
1174
    for node_i in nodeinfo:
1175
      node = node_i.name
1176

    
1177
      if node_i.offline:
1178
        feedback_fn("* Skipping offline node %s" % (node,))
1179
        n_offline.append(node)
1180
        continue
1181

    
1182
      if node == master_node:
1183
        ntype = "master"
1184
      elif node_i.master_candidate:
1185
        ntype = "master candidate"
1186
      elif node_i.drained:
1187
        ntype = "drained"
1188
        n_drained.append(node)
1189
      else:
1190
        ntype = "regular"
1191
      feedback_fn("* Verifying node %s (%s)" % (node, ntype))
1192

    
1193
      msg = all_nvinfo[node].fail_msg
1194
      if msg:
1195
        feedback_fn("  - ERROR: while contacting node %s: %s" % (node, msg))
1196
        bad = True
1197
        continue
1198

    
1199
      nresult = all_nvinfo[node].payload
1200
      node_drbd = {}
1201
      for minor, instance in all_drbd_map[node].items():
1202
        if instance not in instanceinfo:
1203
          feedback_fn("  - ERROR: ghost instance '%s' in temporary DRBD map" %
1204
                      instance)
1205
          # ghost instance should not be running, but otherwise we
1206
          # don't give double warnings (both ghost instance and
1207
          # unallocated minor in use)
1208
          node_drbd[minor] = (instance, False)
1209
        else:
1210
          instance = instanceinfo[instance]
1211
          node_drbd[minor] = (instance.name, instance.admin_up)
1212
      result = self._VerifyNode(node_i, file_names, local_checksums,
1213
                                nresult, feedback_fn, master_files,
1214
                                node_drbd, vg_name)
1215
      bad = bad or result
1216

    
1217
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
1218
      if vg_name is None:
1219
        node_volume[node] = {}
1220
      elif isinstance(lvdata, basestring):
1221
        feedback_fn("  - ERROR: LVM problem on node %s: %s" %
1222
                    (node, utils.SafeEncode(lvdata)))
1223
        bad = True
1224
        node_volume[node] = {}
1225
      elif not isinstance(lvdata, dict):
1226
        feedback_fn("  - ERROR: connection to %s failed (lvlist)" % (node,))
1227
        bad = True
1228
        continue
1229
      else:
1230
        node_volume[node] = lvdata
1231

    
1232
      # node_instance
1233
      idata = nresult.get(constants.NV_INSTANCELIST, None)
1234
      if not isinstance(idata, list):
1235
        feedback_fn("  - ERROR: connection to %s failed (instancelist)" %
1236
                    (node,))
1237
        bad = True
1238
        continue
1239

    
1240
      node_instance[node] = idata
1241

    
1242
      # node_info
1243
      nodeinfo = nresult.get(constants.NV_HVINFO, None)
1244
      if not isinstance(nodeinfo, dict):
1245
        feedback_fn("  - ERROR: connection to %s failed (hvinfo)" % (node,))
1246
        bad = True
1247
        continue
1248

    
1249
      try:
1250
        node_info[node] = {
1251
          "mfree": int(nodeinfo['memory_free']),
1252
          "pinst": [],
1253
          "sinst": [],
1254
          # dictionary holding all instances this node is secondary for,
1255
          # grouped by their primary node. Each key is a cluster node, and each
1256
          # value is a list of instances which have the key as primary and the
1257
          # current node as secondary.  this is handy to calculate N+1 memory
1258
          # availability if you can only failover from a primary to its
1259
          # secondary.
1260
          "sinst-by-pnode": {},
1261
        }
1262
        # FIXME: devise a free space model for file based instances as well
1263
        if vg_name is not None:
1264
          if (constants.NV_VGLIST not in nresult or
1265
              vg_name not in nresult[constants.NV_VGLIST]):
1266
            feedback_fn("  - ERROR: node %s didn't return data for the"
1267
                        " volume group '%s' - it is either missing or broken" %
1268
                        (node, vg_name))
1269
            bad = True
1270
            continue
1271
          node_info[node]["dfree"] = int(nresult[constants.NV_VGLIST][vg_name])
1272
      except (ValueError, KeyError):
1273
        feedback_fn("  - ERROR: invalid nodeinfo value returned"
1274
                    " from node %s" % (node,))
1275
        bad = True
1276
        continue
1277

    
1278
    node_vol_should = {}
1279

    
1280
    for instance in instancelist:
1281
      feedback_fn("* Verifying instance %s" % instance)
1282
      inst_config = instanceinfo[instance]
1283
      result =  self._VerifyInstance(instance, inst_config, node_volume,
1284
                                     node_instance, feedback_fn, n_offline)
1285
      bad = bad or result
1286
      inst_nodes_offline = []
1287

    
1288
      inst_config.MapLVsByNode(node_vol_should)
1289

    
1290
      instance_cfg[instance] = inst_config
1291

    
1292
      pnode = inst_config.primary_node
1293
      if pnode in node_info:
1294
        node_info[pnode]['pinst'].append(instance)
1295
      elif pnode not in n_offline:
1296
        feedback_fn("  - ERROR: instance %s, connection to primary node"
1297
                    " %s failed" % (instance, pnode))
1298
        bad = True
1299

    
1300
      if pnode in n_offline:
1301
        inst_nodes_offline.append(pnode)
1302

    
1303
      # If the instance is non-redundant we cannot survive losing its primary
1304
      # node, so we are not N+1 compliant. On the other hand we have no disk
1305
      # templates with more than one secondary so that situation is not well
1306
      # supported either.
1307
      # FIXME: does not support file-backed instances
1308
      if len(inst_config.secondary_nodes) == 0:
1309
        i_non_redundant.append(instance)
1310
      elif len(inst_config.secondary_nodes) > 1:
1311
        feedback_fn("  - WARNING: multiple secondaries for instance %s"
1312
                    % instance)
1313

    
1314
      if not cluster.FillBE(inst_config)[constants.BE_AUTO_BALANCE]:
1315
        i_non_a_balanced.append(instance)
1316

    
1317
      for snode in inst_config.secondary_nodes:
1318
        if snode in node_info:
1319
          node_info[snode]['sinst'].append(instance)
1320
          if pnode not in node_info[snode]['sinst-by-pnode']:
1321
            node_info[snode]['sinst-by-pnode'][pnode] = []
1322
          node_info[snode]['sinst-by-pnode'][pnode].append(instance)
1323
        elif snode not in n_offline:
1324
          feedback_fn("  - ERROR: instance %s, connection to secondary node"
1325
                      " %s failed" % (instance, snode))
1326
          bad = True
1327
        if snode in n_offline:
1328
          inst_nodes_offline.append(snode)
1329

    
1330
      if inst_nodes_offline:
1331
        # warn that the instance lives on offline nodes, and set bad=True
1332
        feedback_fn("  - ERROR: instance lives on offline node(s) %s" %
1333
                    ", ".join(inst_nodes_offline))
1334
        bad = True
1335

    
1336
    feedback_fn("* Verifying orphan volumes")
1337
    result = self._VerifyOrphanVolumes(node_vol_should, node_volume,
1338
                                       feedback_fn)
1339
    bad = bad or result
1340

    
1341
    feedback_fn("* Verifying remaining instances")
1342
    result = self._VerifyOrphanInstances(instancelist, node_instance,
1343
                                         feedback_fn)
1344
    bad = bad or result
1345

    
1346
    if constants.VERIFY_NPLUSONE_MEM not in self.skip_set:
1347
      feedback_fn("* Verifying N+1 Memory redundancy")
1348
      result = self._VerifyNPlusOneMemory(node_info, instance_cfg, feedback_fn)
1349
      bad = bad or result
1350

    
1351
    feedback_fn("* Other Notes")
1352
    if i_non_redundant:
1353
      feedback_fn("  - NOTICE: %d non-redundant instance(s) found."
1354
                  % len(i_non_redundant))
1355

    
1356
    if i_non_a_balanced:
1357
      feedback_fn("  - NOTICE: %d non-auto-balanced instance(s) found."
1358
                  % len(i_non_a_balanced))
1359

    
1360
    if n_offline:
1361
      feedback_fn("  - NOTICE: %d offline node(s) found." % len(n_offline))
1362

    
1363
    if n_drained:
1364
      feedback_fn("  - NOTICE: %d drained node(s) found." % len(n_drained))
1365

    
1366
    return not bad
1367

    
1368
  def HooksCallBack(self, phase, hooks_results, feedback_fn, lu_result):
1369
    """Analyze the post-hooks' result
1370

1371
    This method analyses the hook result, handles it, and sends some
1372
    nicely-formatted feedback back to the user.
1373

1374
    @param phase: one of L{constants.HOOKS_PHASE_POST} or
1375
        L{constants.HOOKS_PHASE_PRE}; it denotes the hooks phase
1376
    @param hooks_results: the results of the multi-node hooks rpc call
1377
    @param feedback_fn: function used send feedback back to the caller
1378
    @param lu_result: previous Exec result
1379
    @return: the new Exec result, based on the previous result
1380
        and hook results
1381

1382
    """
1383
    # We only really run POST phase hooks, and are only interested in
1384
    # their results
1385
    if phase == constants.HOOKS_PHASE_POST:
1386
      # Used to change hooks' output to proper indentation
1387
      indent_re = re.compile('^', re.M)
1388
      feedback_fn("* Hooks Results")
1389
      if not hooks_results:
1390
        feedback_fn("  - ERROR: general communication failure")
1391
        lu_result = 1
1392
      else:
1393
        for node_name in hooks_results:
1394
          show_node_header = True
1395
          res = hooks_results[node_name]
1396
          msg = res.fail_msg
1397
          if msg:
1398
            if res.offline:
1399
              # no need to warn or set fail return value
1400
              continue
1401
            feedback_fn("    Communication failure in hooks execution: %s" %
1402
                        msg)
1403
            lu_result = 1
1404
            continue
1405
          for script, hkr, output in res.payload:
1406
            if hkr == constants.HKR_FAIL:
1407
              # The node header is only shown once, if there are
1408
              # failing hooks on that node
1409
              if show_node_header:
1410
                feedback_fn("  Node %s:" % node_name)
1411
                show_node_header = False
1412
              feedback_fn("    ERROR: Script %s failed, output:" % script)
1413
              output = indent_re.sub('      ', output)
1414
              feedback_fn("%s" % output)
1415
              lu_result = 1
1416

    
1417
      return lu_result
1418

    
1419

    
1420
class LUVerifyDisks(NoHooksLU):
1421
  """Verifies the cluster disks status.
1422

1423
  """
1424
  _OP_REQP = []
1425
  REQ_BGL = False
1426

    
1427
  def ExpandNames(self):
1428
    self.needed_locks = {
1429
      locking.LEVEL_NODE: locking.ALL_SET,
1430
      locking.LEVEL_INSTANCE: locking.ALL_SET,
1431
    }
1432
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
1433

    
1434
  def CheckPrereq(self):
1435
    """Check prerequisites.
1436

1437
    This has no prerequisites.
1438

1439
    """
1440
    pass
1441

    
1442
  def Exec(self, feedback_fn):
1443
    """Verify integrity of cluster disks.
1444

1445
    @rtype: tuple of three items
1446
    @return: a tuple of (dict of node-to-node_error, list of instances
1447
        which need activate-disks, dict of instance: (node, volume) for
1448
        missing volumes
1449

1450
    """
1451
    result = res_nodes, res_instances, res_missing = {}, [], {}
1452

    
1453
    vg_name = self.cfg.GetVGName()
1454
    nodes = utils.NiceSort(self.cfg.GetNodeList())
1455
    instances = [self.cfg.GetInstanceInfo(name)
1456
                 for name in self.cfg.GetInstanceList()]
1457

    
1458
    nv_dict = {}
1459
    for inst in instances:
1460
      inst_lvs = {}
1461
      if (not inst.admin_up or
1462
          inst.disk_template not in constants.DTS_NET_MIRROR):
1463
        continue
1464
      inst.MapLVsByNode(inst_lvs)
1465
      # transform { iname: {node: [vol,],},} to {(node, vol): iname}
1466
      for node, vol_list in inst_lvs.iteritems():
1467
        for vol in vol_list:
1468
          nv_dict[(node, vol)] = inst
1469

    
1470
    if not nv_dict:
1471
      return result
1472

    
1473
    node_lvs = self.rpc.call_lv_list(nodes, vg_name)
1474

    
1475
    for node in nodes:
1476
      # node_volume
1477
      node_res = node_lvs[node]
1478
      if node_res.offline:
1479
        continue
1480
      msg = node_res.fail_msg
1481
      if msg:
1482
        logging.warning("Error enumerating LVs on node %s: %s", node, msg)
1483
        res_nodes[node] = msg
1484
        continue
1485

    
1486
      lvs = node_res.payload
1487
      for lv_name, (_, lv_inactive, lv_online) in lvs.items():
1488
        inst = nv_dict.pop((node, lv_name), None)
1489
        if (not lv_online and inst is not None
1490
            and inst.name not in res_instances):
1491
          res_instances.append(inst.name)
1492

    
1493
    # any leftover items in nv_dict are missing LVs, let's arrange the
1494
    # data better
1495
    for key, inst in nv_dict.iteritems():
1496
      if inst.name not in res_missing:
1497
        res_missing[inst.name] = []
1498
      res_missing[inst.name].append(key)
1499

    
1500
    return result
1501

    
1502

    
1503
class LURepairDiskSizes(NoHooksLU):
1504
  """Verifies the cluster disks sizes.
1505

1506
  """
1507
  _OP_REQP = ["instances"]
1508
  REQ_BGL = False
1509

    
1510
  def ExpandNames(self):
1511

    
1512
    if not isinstance(self.op.instances, list):
1513
      raise errors.OpPrereqError("Invalid argument type 'instances'")
1514

    
1515
    if self.op.instances:
1516
      self.wanted_names = []
1517
      for name in self.op.instances:
1518
        full_name = self.cfg.ExpandInstanceName(name)
1519
        if full_name is None:
1520
          raise errors.OpPrereqError("Instance '%s' not known" % name)
1521
        self.wanted_names.append(full_name)
1522
      self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names
1523
      self.needed_locks = {
1524
        locking.LEVEL_NODE: [],
1525
        locking.LEVEL_INSTANCE: self.wanted_names,
1526
        }
1527
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1528
    else:
1529
      self.wanted_names = None
1530
      self.needed_locks = {
1531
        locking.LEVEL_NODE: locking.ALL_SET,
1532
        locking.LEVEL_INSTANCE: locking.ALL_SET,
1533
        }
1534
    self.share_locks = dict(((i, 1) for i in locking.LEVELS))
1535

    
1536
  def DeclareLocks(self, level):
1537
    if level == locking.LEVEL_NODE and self.wanted_names is not None:
1538
      self._LockInstancesNodes(primary_only=True)
1539

    
1540
  def CheckPrereq(self):
1541
    """Check prerequisites.
1542

1543
    This only checks the optional instance list against the existing names.
1544

1545
    """
1546
    if self.wanted_names is None:
1547
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
1548

    
1549
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
1550
                             in self.wanted_names]
1551

    
1552
  def Exec(self, feedback_fn):
1553
    """Verify the size of cluster disks.
1554

1555
    """
1556
    # TODO: check child disks too
1557
    # TODO: check differences in size between primary/secondary nodes
1558
    per_node_disks = {}
1559
    for instance in self.wanted_instances:
1560
      pnode = instance.primary_node
1561
      if pnode not in per_node_disks:
1562
        per_node_disks[pnode] = []
1563
      for idx, disk in enumerate(instance.disks):
1564
        per_node_disks[pnode].append((instance, idx, disk))
1565

    
1566
    changed = []
1567
    for node, dskl in per_node_disks.items():
1568
      result = self.rpc.call_blockdev_getsizes(node, [v[2] for v in dskl])
1569
      if result.failed:
1570
        self.LogWarning("Failure in blockdev_getsizes call to node"
1571
                        " %s, ignoring", node)
1572
        continue
1573
      if len(result.data) != len(dskl):
1574
        self.LogWarning("Invalid result from node %s, ignoring node results",
1575
                        node)
1576
        continue
1577
      for ((instance, idx, disk), size) in zip(dskl, result.data):
1578
        if size is None:
1579
          self.LogWarning("Disk %d of instance %s did not return size"
1580
                          " information, ignoring", idx, instance.name)
1581
          continue
1582
        if not isinstance(size, (int, long)):
1583
          self.LogWarning("Disk %d of instance %s did not return valid"
1584
                          " size information, ignoring", idx, instance.name)
1585
          continue
1586
        size = size >> 20
1587
        if size != disk.size:
1588
          self.LogInfo("Disk %d of instance %s has mismatched size,"
1589
                       " correcting: recorded %d, actual %d", idx,
1590
                       instance.name, disk.size, size)
1591
          disk.size = size
1592
          self.cfg.Update(instance)
1593
          changed.append((instance.name, idx, size))
1594
    return changed
1595

    
1596

    
1597
class LURenameCluster(LogicalUnit):
1598
  """Rename the cluster.
1599

1600
  """
1601
  HPATH = "cluster-rename"
1602
  HTYPE = constants.HTYPE_CLUSTER
1603
  _OP_REQP = ["name"]
1604

    
1605
  def BuildHooksEnv(self):
1606
    """Build hooks env.
1607

1608
    """
1609
    env = {
1610
      "OP_TARGET": self.cfg.GetClusterName(),
1611
      "NEW_NAME": self.op.name,
1612
      }
1613
    mn = self.cfg.GetMasterNode()
1614
    return env, [mn], [mn]
1615

    
1616
  def CheckPrereq(self):
1617
    """Verify that the passed name is a valid one.
1618

1619
    """
1620
    hostname = utils.HostInfo(self.op.name)
1621

    
1622
    new_name = hostname.name
1623
    self.ip = new_ip = hostname.ip
1624
    old_name = self.cfg.GetClusterName()
1625
    old_ip = self.cfg.GetMasterIP()
1626
    if new_name == old_name and new_ip == old_ip:
1627
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
1628
                                 " cluster has changed")
1629
    if new_ip != old_ip:
1630
      if utils.TcpPing(new_ip, constants.DEFAULT_NODED_PORT):
1631
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
1632
                                   " reachable on the network. Aborting." %
1633
                                   new_ip)
1634

    
1635
    self.op.name = new_name
1636

    
1637
  def Exec(self, feedback_fn):
1638
    """Rename the cluster.
1639

1640
    """
1641
    clustername = self.op.name
1642
    ip = self.ip
1643

    
1644
    # shutdown the master IP
1645
    master = self.cfg.GetMasterNode()
1646
    result = self.rpc.call_node_stop_master(master, False)
1647
    result.Raise("Could not disable the master role")
1648

    
1649
    try:
1650
      cluster = self.cfg.GetClusterInfo()
1651
      cluster.cluster_name = clustername
1652
      cluster.master_ip = ip
1653
      self.cfg.Update(cluster)
1654

    
1655
      # update the known hosts file
1656
      ssh.WriteKnownHostsFile(self.cfg, constants.SSH_KNOWN_HOSTS_FILE)
1657
      node_list = self.cfg.GetNodeList()
1658
      try:
1659
        node_list.remove(master)
1660
      except ValueError:
1661
        pass
1662
      result = self.rpc.call_upload_file(node_list,
1663
                                         constants.SSH_KNOWN_HOSTS_FILE)
1664
      for to_node, to_result in result.iteritems():
1665
        msg = to_result.fail_msg
1666
        if msg:
1667
          msg = ("Copy of file %s to node %s failed: %s" %
1668
                 (constants.SSH_KNOWN_HOSTS_FILE, to_node, msg))
1669
          self.proc.LogWarning(msg)
1670

    
1671
    finally:
1672
      result = self.rpc.call_node_start_master(master, False, False)
1673
      msg = result.fail_msg
1674
      if msg:
1675
        self.LogWarning("Could not re-enable the master role on"
1676
                        " the master, please restart manually: %s", msg)
1677

    
1678

    
1679
def _RecursiveCheckIfLVMBased(disk):
1680
  """Check if the given disk or its children are lvm-based.
1681

1682
  @type disk: L{objects.Disk}
1683
  @param disk: the disk to check
1684
  @rtype: boolean
1685
  @return: boolean indicating whether a LD_LV dev_type was found or not
1686

1687
  """
1688
  if disk.children:
1689
    for chdisk in disk.children:
1690
      if _RecursiveCheckIfLVMBased(chdisk):
1691
        return True
1692
  return disk.dev_type == constants.LD_LV
1693

    
1694

    
1695
class LUSetClusterParams(LogicalUnit):
1696
  """Change the parameters of the cluster.
1697

1698
  """
1699
  HPATH = "cluster-modify"
1700
  HTYPE = constants.HTYPE_CLUSTER
1701
  _OP_REQP = []
1702
  REQ_BGL = False
1703

    
1704
  def CheckArguments(self):
1705
    """Check parameters
1706

1707
    """
1708
    if not hasattr(self.op, "candidate_pool_size"):
1709
      self.op.candidate_pool_size = None
1710
    if self.op.candidate_pool_size is not None:
1711
      try:
1712
        self.op.candidate_pool_size = int(self.op.candidate_pool_size)
1713
      except (ValueError, TypeError), err:
1714
        raise errors.OpPrereqError("Invalid candidate_pool_size value: %s" %
1715
                                   str(err))
1716
      if self.op.candidate_pool_size < 1:
1717
        raise errors.OpPrereqError("At least one master candidate needed")
1718

    
1719
  def ExpandNames(self):
1720
    # FIXME: in the future maybe other cluster params won't require checking on
1721
    # all nodes to be modified.
1722
    self.needed_locks = {
1723
      locking.LEVEL_NODE: locking.ALL_SET,
1724
    }
1725
    self.share_locks[locking.LEVEL_NODE] = 1
1726

    
1727
  def BuildHooksEnv(self):
1728
    """Build hooks env.
1729

1730
    """
1731
    env = {
1732
      "OP_TARGET": self.cfg.GetClusterName(),
1733
      "NEW_VG_NAME": self.op.vg_name,
1734
      }
1735
    mn = self.cfg.GetMasterNode()
1736
    return env, [mn], [mn]
1737

    
1738
  def CheckPrereq(self):
1739
    """Check prerequisites.
1740

1741
    This checks whether the given params don't conflict and
1742
    if the given volume group is valid.
1743

1744
    """
1745
    if self.op.vg_name is not None and not self.op.vg_name:
1746
      instances = self.cfg.GetAllInstancesInfo().values()
1747
      for inst in instances:
1748
        for disk in inst.disks:
1749
          if _RecursiveCheckIfLVMBased(disk):
1750
            raise errors.OpPrereqError("Cannot disable lvm storage while"
1751
                                       " lvm-based instances exist")
1752

    
1753
    node_list = self.acquired_locks[locking.LEVEL_NODE]
1754

    
1755
    # if vg_name not None, checks given volume group on all nodes
1756
    if self.op.vg_name:
1757
      vglist = self.rpc.call_vg_list(node_list)
1758
      for node in node_list:
1759
        msg = vglist[node].fail_msg
1760
        if msg:
1761
          # ignoring down node
1762
          self.LogWarning("Error while gathering data on node %s"
1763
                          " (ignoring node): %s", node, msg)
1764
          continue
1765
        vgstatus = utils.CheckVolumeGroupSize(vglist[node].payload,
1766
                                              self.op.vg_name,
1767
                                              constants.MIN_VG_SIZE)
1768
        if vgstatus:
1769
          raise errors.OpPrereqError("Error on node '%s': %s" %
1770
                                     (node, vgstatus))
1771

    
1772
    self.cluster = cluster = self.cfg.GetClusterInfo()
1773
    # validate params changes
1774
    if self.op.beparams:
1775
      utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
1776
      self.new_beparams = objects.FillDict(
1777
        cluster.beparams[constants.PP_DEFAULT], self.op.beparams)
1778

    
1779
    if self.op.nicparams:
1780
      utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES)
1781
      self.new_nicparams = objects.FillDict(
1782
        cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams)
1783
      objects.NIC.CheckParameterSyntax(self.new_nicparams)
1784

    
1785
    # hypervisor list/parameters
1786
    self.new_hvparams = objects.FillDict(cluster.hvparams, {})
1787
    if self.op.hvparams:
1788
      if not isinstance(self.op.hvparams, dict):
1789
        raise errors.OpPrereqError("Invalid 'hvparams' parameter on input")
1790
      for hv_name, hv_dict in self.op.hvparams.items():
1791
        if hv_name not in self.new_hvparams:
1792
          self.new_hvparams[hv_name] = hv_dict
1793
        else:
1794
          self.new_hvparams[hv_name].update(hv_dict)
1795

    
1796
    if self.op.enabled_hypervisors is not None:
1797
      self.hv_list = self.op.enabled_hypervisors
1798
      if not self.hv_list:
1799
        raise errors.OpPrereqError("Enabled hypervisors list must contain at"
1800
                                   " least one member")
1801
      invalid_hvs = set(self.hv_list) - constants.HYPER_TYPES
1802
      if invalid_hvs:
1803
        raise errors.OpPrereqError("Enabled hypervisors contains invalid"
1804
                                   " entries: %s" % invalid_hvs)
1805
    else:
1806
      self.hv_list = cluster.enabled_hypervisors
1807

    
1808
    if self.op.hvparams or self.op.enabled_hypervisors is not None:
1809
      # either the enabled list has changed, or the parameters have, validate
1810
      for hv_name, hv_params in self.new_hvparams.items():
1811
        if ((self.op.hvparams and hv_name in self.op.hvparams) or
1812
            (self.op.enabled_hypervisors and
1813
             hv_name in self.op.enabled_hypervisors)):
1814
          # either this is a new hypervisor, or its parameters have changed
1815
          hv_class = hypervisor.GetHypervisor(hv_name)
1816
          utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
1817
          hv_class.CheckParameterSyntax(hv_params)
1818
          _CheckHVParams(self, node_list, hv_name, hv_params)
1819

    
1820
  def Exec(self, feedback_fn):
1821
    """Change the parameters of the cluster.
1822

1823
    """
1824
    if self.op.vg_name is not None:
1825
      new_volume = self.op.vg_name
1826
      if not new_volume:
1827
        new_volume = None
1828
      if new_volume != self.cfg.GetVGName():
1829
        self.cfg.SetVGName(new_volume)
1830
      else:
1831
        feedback_fn("Cluster LVM configuration already in desired"
1832
                    " state, not changing")
1833
    if self.op.hvparams:
1834
      self.cluster.hvparams = self.new_hvparams
1835
    if self.op.enabled_hypervisors is not None:
1836
      self.cluster.enabled_hypervisors = self.op.enabled_hypervisors
1837
    if self.op.beparams:
1838
      self.cluster.beparams[constants.PP_DEFAULT] = self.new_beparams
1839
    if self.op.nicparams:
1840
      self.cluster.nicparams[constants.PP_DEFAULT] = self.new_nicparams
1841

    
1842
    if self.op.candidate_pool_size is not None:
1843
      self.cluster.candidate_pool_size = self.op.candidate_pool_size
1844
      # we need to update the pool size here, otherwise the save will fail
1845
      _AdjustCandidatePool(self)
1846

    
1847
    self.cfg.Update(self.cluster)
1848

    
1849

    
1850
def _RedistributeAncillaryFiles(lu, additional_nodes=None):
1851
  """Distribute additional files which are part of the cluster configuration.
1852

1853
  ConfigWriter takes care of distributing the config and ssconf files, but
1854
  there are more files which should be distributed to all nodes. This function
1855
  makes sure those are copied.
1856

1857
  @param lu: calling logical unit
1858
  @param additional_nodes: list of nodes not in the config to distribute to
1859

1860
  """
1861
  # 1. Gather target nodes
1862
  myself = lu.cfg.GetNodeInfo(lu.cfg.GetMasterNode())
1863
  dist_nodes = lu.cfg.GetNodeList()
1864
  if additional_nodes is not None:
1865
    dist_nodes.extend(additional_nodes)
1866
  if myself.name in dist_nodes:
1867
    dist_nodes.remove(myself.name)
1868
  # 2. Gather files to distribute
1869
  dist_files = set([constants.ETC_HOSTS,
1870
                    constants.SSH_KNOWN_HOSTS_FILE,
1871
                    constants.RAPI_CERT_FILE,
1872
                    constants.RAPI_USERS_FILE,
1873
                    constants.HMAC_CLUSTER_KEY,
1874
                   ])
1875

    
1876
  enabled_hypervisors = lu.cfg.GetClusterInfo().enabled_hypervisors
1877
  for hv_name in enabled_hypervisors:
1878
    hv_class = hypervisor.GetHypervisor(hv_name)
1879
    dist_files.update(hv_class.GetAncillaryFiles())
1880

    
1881
  # 3. Perform the files upload
1882
  for fname in dist_files:
1883
    if os.path.exists(fname):
1884
      result = lu.rpc.call_upload_file(dist_nodes, fname)
1885
      for to_node, to_result in result.items():
1886
        msg = to_result.fail_msg
1887
        if msg:
1888
          msg = ("Copy of file %s to node %s failed: %s" %
1889
                 (fname, to_node, msg))
1890
          lu.proc.LogWarning(msg)
1891

    
1892

    
1893
class LURedistributeConfig(NoHooksLU):
1894
  """Force the redistribution of cluster configuration.
1895

1896
  This is a very simple LU.
1897

1898
  """
1899
  _OP_REQP = []
1900
  REQ_BGL = False
1901

    
1902
  def ExpandNames(self):
1903
    self.needed_locks = {
1904
      locking.LEVEL_NODE: locking.ALL_SET,
1905
    }
1906
    self.share_locks[locking.LEVEL_NODE] = 1
1907

    
1908
  def CheckPrereq(self):
1909
    """Check prerequisites.
1910

1911
    """
1912

    
1913
  def Exec(self, feedback_fn):
1914
    """Redistribute the configuration.
1915

1916
    """
1917
    self.cfg.Update(self.cfg.GetClusterInfo())
1918
    _RedistributeAncillaryFiles(self)
1919

    
1920

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

1924
  """
1925
  if not instance.disks:
1926
    return True
1927

    
1928
  if not oneshot:
1929
    lu.proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
1930

    
1931
  node = instance.primary_node
1932

    
1933
  for dev in instance.disks:
1934
    lu.cfg.SetDiskID(dev, node)
1935

    
1936
  retries = 0
1937
  degr_retries = 10 # in seconds, as we sleep 1 second each time
1938
  while True:
1939
    max_time = 0
1940
    done = True
1941
    cumul_degraded = False
1942
    rstats = lu.rpc.call_blockdev_getmirrorstatus(node, instance.disks)
1943
    msg = rstats.fail_msg
1944
    if msg:
1945
      lu.LogWarning("Can't get any data from node %s: %s", node, msg)
1946
      retries += 1
1947
      if retries >= 10:
1948
        raise errors.RemoteError("Can't contact node %s for mirror data,"
1949
                                 " aborting." % node)
1950
      time.sleep(6)
1951
      continue
1952
    rstats = rstats.payload
1953
    retries = 0
1954
    for i, mstat in enumerate(rstats):
1955
      if mstat is None:
1956
        lu.LogWarning("Can't compute data for node %s/%s",
1957
                           node, instance.disks[i].iv_name)
1958
        continue
1959

    
1960
      cumul_degraded = (cumul_degraded or
1961
                        (mstat.is_degraded and mstat.sync_percent is None))
1962
      if mstat.sync_percent is not None:
1963
        done = False
1964
        if mstat.estimated_time is not None:
1965
          rem_time = "%d estimated seconds remaining" % mstat.estimated_time
1966
          max_time = mstat.estimated_time
1967
        else:
1968
          rem_time = "no time estimate"
1969
        lu.proc.LogInfo("- device %s: %5.2f%% done, %s" %
1970
                        (instance.disks[i].iv_name, mstat.sync_percent, rem_time))
1971

    
1972
    # if we're done but degraded, let's do a few small retries, to
1973
    # make sure we see a stable and not transient situation; therefore
1974
    # we force restart of the loop
1975
    if (done or oneshot) and cumul_degraded and degr_retries > 0:
1976
      logging.info("Degraded disks found, %d retries left", degr_retries)
1977
      degr_retries -= 1
1978
      time.sleep(1)
1979
      continue
1980

    
1981
    if done or oneshot:
1982
      break
1983

    
1984
    time.sleep(min(60, max_time))
1985

    
1986
  if done:
1987
    lu.proc.LogInfo("Instance %s's disks are in sync." % instance.name)
1988
  return not cumul_degraded
1989

    
1990

    
1991
def _CheckDiskConsistency(lu, dev, node, on_primary, ldisk=False):
1992
  """Check that mirrors are not degraded.
1993

1994
  The ldisk parameter, if True, will change the test from the
1995
  is_degraded attribute (which represents overall non-ok status for
1996
  the device(s)) to the ldisk (representing the local storage status).
1997

1998
  """
1999
  lu.cfg.SetDiskID(dev, node)
2000

    
2001
  result = True
2002

    
2003
  if on_primary or dev.AssembleOnSecondary():
2004
    rstats = lu.rpc.call_blockdev_find(node, dev)
2005
    msg = rstats.fail_msg
2006
    if msg:
2007
      lu.LogWarning("Can't find disk on node %s: %s", node, msg)
2008
      result = False
2009
    elif not rstats.payload:
2010
      lu.LogWarning("Can't find disk on node %s", node)
2011
      result = False
2012
    else:
2013
      if ldisk:
2014
        result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
2015
      else:
2016
        result = result and not rstats.payload.is_degraded
2017

    
2018
  if dev.children:
2019
    for child in dev.children:
2020
      result = result and _CheckDiskConsistency(lu, child, node, on_primary)
2021

    
2022
  return result
2023

    
2024

    
2025
class LUDiagnoseOS(NoHooksLU):
2026
  """Logical unit for OS diagnose/query.
2027

2028
  """
2029
  _OP_REQP = ["output_fields", "names"]
2030
  REQ_BGL = False
2031
  _FIELDS_STATIC = utils.FieldSet()
2032
  _FIELDS_DYNAMIC = utils.FieldSet("name", "valid", "node_status")
2033

    
2034
  def ExpandNames(self):
2035
    if self.op.names:
2036
      raise errors.OpPrereqError("Selective OS query not supported")
2037

    
2038
    _CheckOutputFields(static=self._FIELDS_STATIC,
2039
                       dynamic=self._FIELDS_DYNAMIC,
2040
                       selected=self.op.output_fields)
2041

    
2042
    # Lock all nodes, in shared mode
2043
    # Temporary removal of locks, should be reverted later
2044
    # TODO: reintroduce locks when they are lighter-weight
2045
    self.needed_locks = {}
2046
    #self.share_locks[locking.LEVEL_NODE] = 1
2047
    #self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2048

    
2049
  def CheckPrereq(self):
2050
    """Check prerequisites.
2051

2052
    """
2053

    
2054
  @staticmethod
2055
  def _DiagnoseByOS(node_list, rlist):
2056
    """Remaps a per-node return list into an a per-os per-node dictionary
2057

2058
    @param node_list: a list with the names of all nodes
2059
    @param rlist: a map with node names as keys and OS objects as values
2060

2061
    @rtype: dict
2062
    @return: a dictionary with osnames as keys and as value another map, with
2063
        nodes as keys and tuples of (path, status, diagnose) as values, eg::
2064

2065
          {"debian-etch": {"node1": [(/usr/lib/..., True, ""),
2066
                                     (/srv/..., False, "invalid api")],
2067
                           "node2": [(/srv/..., True, "")]}
2068
          }
2069

2070
    """
2071
    all_os = {}
2072
    # we build here the list of nodes that didn't fail the RPC (at RPC
2073
    # level), so that nodes with a non-responding node daemon don't
2074
    # make all OSes invalid
2075
    good_nodes = [node_name for node_name in rlist
2076
                  if not rlist[node_name].fail_msg]
2077
    for node_name, nr in rlist.items():
2078
      if nr.fail_msg or not nr.payload:
2079
        continue
2080
      for name, path, status, diagnose in nr.payload:
2081
        if name not in all_os:
2082
          # build a list of nodes for this os containing empty lists
2083
          # for each node in node_list
2084
          all_os[name] = {}
2085
          for nname in good_nodes:
2086
            all_os[name][nname] = []
2087
        all_os[name][node_name].append((path, status, diagnose))
2088
    return all_os
2089

    
2090
  def Exec(self, feedback_fn):
2091
    """Compute the list of OSes.
2092

2093
    """
2094
    valid_nodes = [node for node in self.cfg.GetOnlineNodeList()]
2095
    node_data = self.rpc.call_os_diagnose(valid_nodes)
2096
    pol = self._DiagnoseByOS(valid_nodes, node_data)
2097
    output = []
2098
    for os_name, os_data in pol.items():
2099
      row = []
2100
      for field in self.op.output_fields:
2101
        if field == "name":
2102
          val = os_name
2103
        elif field == "valid":
2104
          val = utils.all([osl and osl[0][1] for osl in os_data.values()])
2105
        elif field == "node_status":
2106
          # this is just a copy of the dict
2107
          val = {}
2108
          for node_name, nos_list in os_data.items():
2109
            val[node_name] = nos_list
2110
        else:
2111
          raise errors.ParameterError(field)
2112
        row.append(val)
2113
      output.append(row)
2114

    
2115
    return output
2116

    
2117

    
2118
class LURemoveNode(LogicalUnit):
2119
  """Logical unit for removing a node.
2120

2121
  """
2122
  HPATH = "node-remove"
2123
  HTYPE = constants.HTYPE_NODE
2124
  _OP_REQP = ["node_name"]
2125

    
2126
  def BuildHooksEnv(self):
2127
    """Build hooks env.
2128

2129
    This doesn't run on the target node in the pre phase as a failed
2130
    node would then be impossible to remove.
2131

2132
    """
2133
    env = {
2134
      "OP_TARGET": self.op.node_name,
2135
      "NODE_NAME": self.op.node_name,
2136
      }
2137
    all_nodes = self.cfg.GetNodeList()
2138
    all_nodes.remove(self.op.node_name)
2139
    return env, all_nodes, all_nodes
2140

    
2141
  def CheckPrereq(self):
2142
    """Check prerequisites.
2143

2144
    This checks:
2145
     - the node exists in the configuration
2146
     - it does not have primary or secondary instances
2147
     - it's not the master
2148

2149
    Any errors are signaled by raising errors.OpPrereqError.
2150

2151
    """
2152
    node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
2153
    if node is None:
2154
      raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name)
2155

    
2156
    instance_list = self.cfg.GetInstanceList()
2157

    
2158
    masternode = self.cfg.GetMasterNode()
2159
    if node.name == masternode:
2160
      raise errors.OpPrereqError("Node is the master node,"
2161
                                 " you need to failover first.")
2162

    
2163
    for instance_name in instance_list:
2164
      instance = self.cfg.GetInstanceInfo(instance_name)
2165
      if node.name in instance.all_nodes:
2166
        raise errors.OpPrereqError("Instance %s is still running on the node,"
2167
                                   " please remove first." % instance_name)
2168
    self.op.node_name = node.name
2169
    self.node = node
2170

    
2171
  def Exec(self, feedback_fn):
2172
    """Removes the node from the cluster.
2173

2174
    """
2175
    node = self.node
2176
    logging.info("Stopping the node daemon and removing configs from node %s",
2177
                 node.name)
2178

    
2179
    self.context.RemoveNode(node.name)
2180

    
2181
    result = self.rpc.call_node_leave_cluster(node.name)
2182
    msg = result.fail_msg
2183
    if msg:
2184
      self.LogWarning("Errors encountered on the remote node while leaving"
2185
                      " the cluster: %s", msg)
2186

    
2187
    # Promote nodes to master candidate as needed
2188
    _AdjustCandidatePool(self)
2189

    
2190

    
2191
class LUQueryNodes(NoHooksLU):
2192
  """Logical unit for querying nodes.
2193

2194
  """
2195
  _OP_REQP = ["output_fields", "names", "use_locking"]
2196
  REQ_BGL = False
2197
  _FIELDS_DYNAMIC = utils.FieldSet(
2198
    "dtotal", "dfree",
2199
    "mtotal", "mnode", "mfree",
2200
    "bootid",
2201
    "ctotal", "cnodes", "csockets",
2202
    )
2203

    
2204
  _FIELDS_STATIC = utils.FieldSet(
2205
    "name", "pinst_cnt", "sinst_cnt",
2206
    "pinst_list", "sinst_list",
2207
    "pip", "sip", "tags",
2208
    "serial_no",
2209
    "master_candidate",
2210
    "master",
2211
    "offline",
2212
    "drained",
2213
    "role",
2214
    )
2215

    
2216
  def ExpandNames(self):
2217
    _CheckOutputFields(static=self._FIELDS_STATIC,
2218
                       dynamic=self._FIELDS_DYNAMIC,
2219
                       selected=self.op.output_fields)
2220

    
2221
    self.needed_locks = {}
2222
    self.share_locks[locking.LEVEL_NODE] = 1
2223

    
2224
    if self.op.names:
2225
      self.wanted = _GetWantedNodes(self, self.op.names)
2226
    else:
2227
      self.wanted = locking.ALL_SET
2228

    
2229
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2230
    self.do_locking = self.do_node_query and self.op.use_locking
2231
    if self.do_locking:
2232
      # if we don't request only static fields, we need to lock the nodes
2233
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
2234

    
2235

    
2236
  def CheckPrereq(self):
2237
    """Check prerequisites.
2238

2239
    """
2240
    # The validation of the node list is done in the _GetWantedNodes,
2241
    # if non empty, and if empty, there's no validation to do
2242
    pass
2243

    
2244
  def Exec(self, feedback_fn):
2245
    """Computes the list of nodes and their attributes.
2246

2247
    """
2248
    all_info = self.cfg.GetAllNodesInfo()
2249
    if self.do_locking:
2250
      nodenames = self.acquired_locks[locking.LEVEL_NODE]
2251
    elif self.wanted != locking.ALL_SET:
2252
      nodenames = self.wanted
2253
      missing = set(nodenames).difference(all_info.keys())
2254
      if missing:
2255
        raise errors.OpExecError(
2256
          "Some nodes were removed before retrieving their data: %s" % missing)
2257
    else:
2258
      nodenames = all_info.keys()
2259

    
2260
    nodenames = utils.NiceSort(nodenames)
2261
    nodelist = [all_info[name] for name in nodenames]
2262

    
2263
    # begin data gathering
2264

    
2265
    if self.do_node_query:
2266
      live_data = {}
2267
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
2268
                                          self.cfg.GetHypervisorType())
2269
      for name in nodenames:
2270
        nodeinfo = node_data[name]
2271
        if not nodeinfo.fail_msg and nodeinfo.payload:
2272
          nodeinfo = nodeinfo.payload
2273
          fn = utils.TryConvert
2274
          live_data[name] = {
2275
            "mtotal": fn(int, nodeinfo.get('memory_total', None)),
2276
            "mnode": fn(int, nodeinfo.get('memory_dom0', None)),
2277
            "mfree": fn(int, nodeinfo.get('memory_free', None)),
2278
            "dtotal": fn(int, nodeinfo.get('vg_size', None)),
2279
            "dfree": fn(int, nodeinfo.get('vg_free', None)),
2280
            "ctotal": fn(int, nodeinfo.get('cpu_total', None)),
2281
            "bootid": nodeinfo.get('bootid', None),
2282
            "cnodes": fn(int, nodeinfo.get('cpu_nodes', None)),
2283
            "csockets": fn(int, nodeinfo.get('cpu_sockets', None)),
2284
            }
2285
        else:
2286
          live_data[name] = {}
2287
    else:
2288
      live_data = dict.fromkeys(nodenames, {})
2289

    
2290
    node_to_primary = dict([(name, set()) for name in nodenames])
2291
    node_to_secondary = dict([(name, set()) for name in nodenames])
2292

    
2293
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
2294
                             "sinst_cnt", "sinst_list"))
2295
    if inst_fields & frozenset(self.op.output_fields):
2296
      instancelist = self.cfg.GetInstanceList()
2297

    
2298
      for instance_name in instancelist:
2299
        inst = self.cfg.GetInstanceInfo(instance_name)
2300
        if inst.primary_node in node_to_primary:
2301
          node_to_primary[inst.primary_node].add(inst.name)
2302
        for secnode in inst.secondary_nodes:
2303
          if secnode in node_to_secondary:
2304
            node_to_secondary[secnode].add(inst.name)
2305

    
2306
    master_node = self.cfg.GetMasterNode()
2307

    
2308
    # end data gathering
2309

    
2310
    output = []
2311
    for node in nodelist:
2312
      node_output = []
2313
      for field in self.op.output_fields:
2314
        if field == "name":
2315
          val = node.name
2316
        elif field == "pinst_list":
2317
          val = list(node_to_primary[node.name])
2318
        elif field == "sinst_list":
2319
          val = list(node_to_secondary[node.name])
2320
        elif field == "pinst_cnt":
2321
          val = len(node_to_primary[node.name])
2322
        elif field == "sinst_cnt":
2323
          val = len(node_to_secondary[node.name])
2324
        elif field == "pip":
2325
          val = node.primary_ip
2326
        elif field == "sip":
2327
          val = node.secondary_ip
2328
        elif field == "tags":
2329
          val = list(node.GetTags())
2330
        elif field == "serial_no":
2331
          val = node.serial_no
2332
        elif field == "master_candidate":
2333
          val = node.master_candidate
2334
        elif field == "master":
2335
          val = node.name == master_node
2336
        elif field == "offline":
2337
          val = node.offline
2338
        elif field == "drained":
2339
          val = node.drained
2340
        elif self._FIELDS_DYNAMIC.Matches(field):
2341
          val = live_data[node.name].get(field, None)
2342
        elif field == "role":
2343
          if node.name == master_node:
2344
            val = "M"
2345
          elif node.master_candidate:
2346
            val = "C"
2347
          elif node.drained:
2348
            val = "D"
2349
          elif node.offline:
2350
            val = "O"
2351
          else:
2352
            val = "R"
2353
        else:
2354
          raise errors.ParameterError(field)
2355
        node_output.append(val)
2356
      output.append(node_output)
2357

    
2358
    return output
2359

    
2360

    
2361
class LUQueryNodeVolumes(NoHooksLU):
2362
  """Logical unit for getting volumes on node(s).
2363

2364
  """
2365
  _OP_REQP = ["nodes", "output_fields"]
2366
  REQ_BGL = False
2367
  _FIELDS_DYNAMIC = utils.FieldSet("phys", "vg", "name", "size", "instance")
2368
  _FIELDS_STATIC = utils.FieldSet("node")
2369

    
2370
  def ExpandNames(self):
2371
    _CheckOutputFields(static=self._FIELDS_STATIC,
2372
                       dynamic=self._FIELDS_DYNAMIC,
2373
                       selected=self.op.output_fields)
2374

    
2375
    self.needed_locks = {}
2376
    self.share_locks[locking.LEVEL_NODE] = 1
2377
    if not self.op.nodes:
2378
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2379
    else:
2380
      self.needed_locks[locking.LEVEL_NODE] = \
2381
        _GetWantedNodes(self, self.op.nodes)
2382

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

2386
    This checks that the fields required are valid output fields.
2387

2388
    """
2389
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2390

    
2391
  def Exec(self, feedback_fn):
2392
    """Computes the list of nodes and their attributes.
2393

2394
    """
2395
    nodenames = self.nodes
2396
    volumes = self.rpc.call_node_volumes(nodenames)
2397

    
2398
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
2399
             in self.cfg.GetInstanceList()]
2400

    
2401
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
2402

    
2403
    output = []
2404
    for node in nodenames:
2405
      nresult = volumes[node]
2406
      if nresult.offline:
2407
        continue
2408
      msg = nresult.fail_msg
2409
      if msg:
2410
        self.LogWarning("Can't compute volume data on node %s: %s", node, msg)
2411
        continue
2412

    
2413
      node_vols = nresult.payload[:]
2414
      node_vols.sort(key=lambda vol: vol['dev'])
2415

    
2416
      for vol in node_vols:
2417
        node_output = []
2418
        for field in self.op.output_fields:
2419
          if field == "node":
2420
            val = node
2421
          elif field == "phys":
2422
            val = vol['dev']
2423
          elif field == "vg":
2424
            val = vol['vg']
2425
          elif field == "name":
2426
            val = vol['name']
2427
          elif field == "size":
2428
            val = int(float(vol['size']))
2429
          elif field == "instance":
2430
            for inst in ilist:
2431
              if node not in lv_by_node[inst]:
2432
                continue
2433
              if vol['name'] in lv_by_node[inst][node]:
2434
                val = inst.name
2435
                break
2436
            else:
2437
              val = '-'
2438
          else:
2439
            raise errors.ParameterError(field)
2440
          node_output.append(str(val))
2441

    
2442
        output.append(node_output)
2443

    
2444
    return output
2445

    
2446

    
2447
class LUQueryNodeStorage(NoHooksLU):
2448
  """Logical unit for getting information on storage units on node(s).
2449

2450
  """
2451
  _OP_REQP = ["nodes", "storage_type", "output_fields"]
2452
  REQ_BGL = False
2453
  _FIELDS_STATIC = utils.FieldSet("node")
2454

    
2455
  def ExpandNames(self):
2456
    storage_type = self.op.storage_type
2457

    
2458
    if storage_type not in constants.VALID_STORAGE_FIELDS:
2459
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type)
2460

    
2461
    dynamic_fields = constants.VALID_STORAGE_FIELDS[storage_type]
2462

    
2463
    _CheckOutputFields(static=self._FIELDS_STATIC,
2464
                       dynamic=utils.FieldSet(*dynamic_fields),
2465
                       selected=self.op.output_fields)
2466

    
2467
    self.needed_locks = {}
2468
    self.share_locks[locking.LEVEL_NODE] = 1
2469

    
2470
    if self.op.nodes:
2471
      self.needed_locks[locking.LEVEL_NODE] = \
2472
        _GetWantedNodes(self, self.op.nodes)
2473
    else:
2474
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2475

    
2476
  def CheckPrereq(self):
2477
    """Check prerequisites.
2478

2479
    This checks that the fields required are valid output fields.
2480

2481
    """
2482
    self.op.name = getattr(self.op, "name", None)
2483

    
2484
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
2485

    
2486
  def Exec(self, feedback_fn):
2487
    """Computes the list of nodes and their attributes.
2488

2489
    """
2490
    # Always get name to sort by
2491
    if constants.SF_NAME in self.op.output_fields:
2492
      fields = self.op.output_fields[:]
2493
    else:
2494
      fields = [constants.SF_NAME] + self.op.output_fields
2495

    
2496
    # Never ask for node as it's only known to the LU
2497
    while "node" in fields:
2498
      fields.remove("node")
2499

    
2500
    field_idx = dict([(name, idx) for (idx, name) in enumerate(fields)])
2501
    name_idx = field_idx[constants.SF_NAME]
2502

    
2503
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2504
    data = self.rpc.call_storage_list(self.nodes,
2505
                                      self.op.storage_type, st_args,
2506
                                      self.op.name, fields)
2507

    
2508
    result = []
2509

    
2510
    for node in utils.NiceSort(self.nodes):
2511
      nresult = data[node]
2512
      if nresult.offline:
2513
        continue
2514

    
2515
      msg = nresult.fail_msg
2516
      if msg:
2517
        self.LogWarning("Can't get storage data from node %s: %s", node, msg)
2518
        continue
2519

    
2520
      rows = dict([(row[name_idx], row) for row in nresult.payload])
2521

    
2522
      for name in utils.NiceSort(rows.keys()):
2523
        row = rows[name]
2524

    
2525
        out = []
2526

    
2527
        for field in self.op.output_fields:
2528
          if field == "node":
2529
            val = node
2530
          elif field in field_idx:
2531
            val = row[field_idx[field]]
2532
          else:
2533
            raise errors.ParameterError(field)
2534

    
2535
          out.append(val)
2536

    
2537
        result.append(out)
2538

    
2539
    return result
2540

    
2541

    
2542
class LUModifyNodeStorage(NoHooksLU):
2543
  """Logical unit for modifying a storage volume on a node.
2544

2545
  """
2546
  _OP_REQP = ["node_name", "storage_type", "name", "changes"]
2547
  REQ_BGL = False
2548

    
2549
  def CheckArguments(self):
2550
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2551
    if node_name is None:
2552
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name)
2553

    
2554
    self.op.node_name = node_name
2555

    
2556
    storage_type = self.op.storage_type
2557
    if storage_type not in constants.VALID_STORAGE_FIELDS:
2558
      raise errors.OpPrereqError("Unknown storage type: %s" % storage_type)
2559

    
2560
  def ExpandNames(self):
2561
    self.needed_locks = {
2562
      locking.LEVEL_NODE: self.op.node_name,
2563
      }
2564

    
2565
  def CheckPrereq(self):
2566
    """Check prerequisites.
2567

2568
    """
2569
    storage_type = self.op.storage_type
2570

    
2571
    try:
2572
      modifiable = constants.MODIFIABLE_STORAGE_FIELDS[storage_type]
2573
    except KeyError:
2574
      raise errors.OpPrereqError("Storage units of type '%s' can not be"
2575
                                 " modified" % storage_type)
2576

    
2577
    diff = set(self.op.changes.keys()) - modifiable
2578
    if diff:
2579
      raise errors.OpPrereqError("The following fields can not be modified for"
2580
                                 " storage units of type '%s': %r" %
2581
                                 (storage_type, list(diff)))
2582

    
2583
  def Exec(self, feedback_fn):
2584
    """Computes the list of nodes and their attributes.
2585

2586
    """
2587
    st_args = _GetStorageTypeArgs(self.cfg, self.op.storage_type)
2588
    result = self.rpc.call_storage_modify(self.op.node_name,
2589
                                          self.op.storage_type, st_args,
2590
                                          self.op.name, self.op.changes)
2591
    result.Raise("Failed to modify storage unit '%s' on %s" %
2592
                 (self.op.name, self.op.node_name))
2593

    
2594

    
2595
class LUAddNode(LogicalUnit):
2596
  """Logical unit for adding node to the cluster.
2597

2598
  """
2599
  HPATH = "node-add"
2600
  HTYPE = constants.HTYPE_NODE
2601
  _OP_REQP = ["node_name"]
2602

    
2603
  def BuildHooksEnv(self):
2604
    """Build hooks env.
2605

2606
    This will run on all nodes before, and on all nodes + the new node after.
2607

2608
    """
2609
    env = {
2610
      "OP_TARGET": self.op.node_name,
2611
      "NODE_NAME": self.op.node_name,
2612
      "NODE_PIP": self.op.primary_ip,
2613
      "NODE_SIP": self.op.secondary_ip,
2614
      }
2615
    nodes_0 = self.cfg.GetNodeList()
2616
    nodes_1 = nodes_0 + [self.op.node_name, ]
2617
    return env, nodes_0, nodes_1
2618

    
2619
  def CheckPrereq(self):
2620
    """Check prerequisites.
2621

2622
    This checks:
2623
     - the new node is not already in the config
2624
     - it is resolvable
2625
     - its parameters (single/dual homed) matches the cluster
2626

2627
    Any errors are signaled by raising errors.OpPrereqError.
2628

2629
    """
2630
    node_name = self.op.node_name
2631
    cfg = self.cfg
2632

    
2633
    dns_data = utils.HostInfo(node_name)
2634

    
2635
    node = dns_data.name
2636
    primary_ip = self.op.primary_ip = dns_data.ip
2637
    secondary_ip = getattr(self.op, "secondary_ip", None)
2638
    if secondary_ip is None:
2639
      secondary_ip = primary_ip
2640
    if not utils.IsValidIP(secondary_ip):
2641
      raise errors.OpPrereqError("Invalid secondary IP given")
2642
    self.op.secondary_ip = secondary_ip
2643

    
2644
    node_list = cfg.GetNodeList()
2645
    if not self.op.readd and node in node_list:
2646
      raise errors.OpPrereqError("Node %s is already in the configuration" %
2647
                                 node)
2648
    elif self.op.readd and node not in node_list:
2649
      raise errors.OpPrereqError("Node %s is not in the configuration" % node)
2650

    
2651
    for existing_node_name in node_list:
2652
      existing_node = cfg.GetNodeInfo(existing_node_name)
2653

    
2654
      if self.op.readd and node == existing_node_name:
2655
        if (existing_node.primary_ip != primary_ip or
2656
            existing_node.secondary_ip != secondary_ip):
2657
          raise errors.OpPrereqError("Readded node doesn't have the same IP"
2658
                                     " address configuration as before")
2659
        continue
2660

    
2661
      if (existing_node.primary_ip == primary_ip or
2662
          existing_node.secondary_ip == primary_ip or
2663
          existing_node.primary_ip == secondary_ip or
2664
          existing_node.secondary_ip == secondary_ip):
2665
        raise errors.OpPrereqError("New node ip address(es) conflict with"
2666
                                   " existing node %s" % existing_node.name)
2667

    
2668
    # check that the type of the node (single versus dual homed) is the
2669
    # same as for the master
2670
    myself = cfg.GetNodeInfo(self.cfg.GetMasterNode())
2671
    master_singlehomed = myself.secondary_ip == myself.primary_ip
2672
    newbie_singlehomed = secondary_ip == primary_ip
2673
    if master_singlehomed != newbie_singlehomed:
2674
      if master_singlehomed:
2675
        raise errors.OpPrereqError("The master has no private ip but the"
2676
                                   " new node has one")
2677
      else:
2678
        raise errors.OpPrereqError("The master has a private ip but the"
2679
                                   " new node doesn't have one")
2680

    
2681
    # checks reachability
2682
    if not utils.TcpPing(primary_ip, constants.DEFAULT_NODED_PORT):
2683
      raise errors.OpPrereqError("Node not reachable by ping")
2684

    
2685
    if not newbie_singlehomed:
2686
      # check reachability from my secondary ip to newbie's secondary ip
2687
      if not utils.TcpPing(secondary_ip, constants.DEFAULT_NODED_PORT,
2688
                           source=myself.secondary_ip):
2689
        raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
2690
                                   " based ping to noded port")
2691

    
2692
    cp_size = self.cfg.GetClusterInfo().candidate_pool_size
2693
    if self.op.readd:
2694
      exceptions = [node]
2695
    else:
2696
      exceptions = []
2697
    mc_now, mc_max = self.cfg.GetMasterCandidateStats(exceptions)
2698
    # the new node will increase mc_max with one, so:
2699
    mc_max = min(mc_max + 1, cp_size)
2700
    self.master_candidate = mc_now < mc_max
2701

    
2702
    if self.op.readd:
2703
      self.new_node = self.cfg.GetNodeInfo(node)
2704
      assert self.new_node is not None, "Can't retrieve locked node %s" % node
2705
    else:
2706
      self.new_node = objects.Node(name=node,
2707
                                   primary_ip=primary_ip,
2708
                                   secondary_ip=secondary_ip,
2709
                                   master_candidate=self.master_candidate,
2710
                                   offline=False, drained=False)
2711

    
2712
  def Exec(self, feedback_fn):
2713
    """Adds the new node to the cluster.
2714

2715
    """
2716
    new_node = self.new_node
2717
    node = new_node.name
2718

    
2719
    # for re-adds, reset the offline/drained/master-candidate flags;
2720
    # we need to reset here, otherwise offline would prevent RPC calls
2721
    # later in the procedure; this also means that if the re-add
2722
    # fails, we are left with a non-offlined, broken node
2723
    if self.op.readd:
2724
      new_node.drained = new_node.offline = False
2725
      self.LogInfo("Readding a node, the offline/drained flags were reset")
2726
      # if we demote the node, we do cleanup later in the procedure
2727
      new_node.master_candidate = self.master_candidate
2728

    
2729
    # notify the user about any possible mc promotion
2730
    if new_node.master_candidate:
2731
      self.LogInfo("Node will be a master candidate")
2732

    
2733
    # check connectivity
2734
    result = self.rpc.call_version([node])[node]
2735
    result.Raise("Can't get version information from node %s" % node)
2736
    if constants.PROTOCOL_VERSION == result.payload:
2737
      logging.info("Communication to node %s fine, sw version %s match",
2738
                   node, result.payload)
2739
    else:
2740
      raise errors.OpExecError("Version mismatch master version %s,"
2741
                               " node version %s" %
2742
                               (constants.PROTOCOL_VERSION, result.payload))
2743

    
2744
    # setup ssh on node
2745
    logging.info("Copy ssh key to node %s", node)
2746
    priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
2747
    keyarray = []
2748
    keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
2749
                constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
2750
                priv_key, pub_key]
2751

    
2752
    for i in keyfiles:
2753
      f = open(i, 'r')
2754
      try:
2755
        keyarray.append(f.read())
2756
      finally:
2757
        f.close()
2758

    
2759
    result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
2760
                                    keyarray[2],
2761
                                    keyarray[3], keyarray[4], keyarray[5])
2762
    result.Raise("Cannot transfer ssh keys to the new node")
2763

    
2764
    # Add node to our /etc/hosts, and add key to known_hosts
2765
    if self.cfg.GetClusterInfo().modify_etc_hosts:
2766
      utils.AddHostToEtcHosts(new_node.name)
2767

    
2768
    if new_node.secondary_ip != new_node.primary_ip:
2769
      result = self.rpc.call_node_has_ip_address(new_node.name,
2770
                                                 new_node.secondary_ip)
2771
      result.Raise("Failure checking secondary ip on node %s" % new_node.name,
2772
                   prereq=True)
2773
      if not result.payload:
2774
        raise errors.OpExecError("Node claims it doesn't have the secondary ip"
2775
                                 " you gave (%s). Please fix and re-run this"
2776
                                 " command." % new_node.secondary_ip)
2777

    
2778
    node_verify_list = [self.cfg.GetMasterNode()]
2779
    node_verify_param = {
2780
      'nodelist': [node],
2781
      # TODO: do a node-net-test as well?
2782
    }
2783

    
2784
    result = self.rpc.call_node_verify(node_verify_list, node_verify_param,
2785
                                       self.cfg.GetClusterName())
2786
    for verifier in node_verify_list:
2787
      result[verifier].Raise("Cannot communicate with node %s" % verifier)
2788
      nl_payload = result[verifier].payload['nodelist']
2789
      if nl_payload:
2790
        for failed in nl_payload:
2791
          feedback_fn("ssh/hostname verification failed %s -> %s" %
2792
                      (verifier, nl_payload[failed]))
2793
        raise errors.OpExecError("ssh/hostname verification failed.")
2794

    
2795
    if self.op.readd:
2796
      _RedistributeAncillaryFiles(self)
2797
      self.context.ReaddNode(new_node)
2798
      # make sure we redistribute the config
2799
      self.cfg.Update(new_node)
2800
      # and make sure the new node will not have old files around
2801
      if not new_node.master_candidate:
2802
        result = self.rpc.call_node_demote_from_mc(new_node.name)
2803
        msg = result.RemoteFailMsg()
2804
        if msg:
2805
          self.LogWarning("Node failed to demote itself from master"
2806
                          " candidate status: %s" % msg)
2807
    else:
2808
      _RedistributeAncillaryFiles(self, additional_nodes=[node])
2809
      self.context.AddNode(new_node)
2810

    
2811

    
2812
class LUSetNodeParams(LogicalUnit):
2813
  """Modifies the parameters of a node.
2814

2815
  """
2816
  HPATH = "node-modify"
2817
  HTYPE = constants.HTYPE_NODE
2818
  _OP_REQP = ["node_name"]
2819
  REQ_BGL = False
2820

    
2821
  def CheckArguments(self):
2822
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2823
    if node_name is None:
2824
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name)
2825
    self.op.node_name = node_name
2826
    _CheckBooleanOpField(self.op, 'master_candidate')
2827
    _CheckBooleanOpField(self.op, 'offline')
2828
    _CheckBooleanOpField(self.op, 'drained')
2829
    all_mods = [self.op.offline, self.op.master_candidate, self.op.drained]
2830
    if all_mods.count(None) == 3:
2831
      raise errors.OpPrereqError("Please pass at least one modification")
2832
    if all_mods.count(True) > 1:
2833
      raise errors.OpPrereqError("Can't set the node into more than one"
2834
                                 " state at the same time")
2835

    
2836
  def ExpandNames(self):
2837
    self.needed_locks = {locking.LEVEL_NODE: self.op.node_name}
2838

    
2839
  def BuildHooksEnv(self):
2840
    """Build hooks env.
2841

2842
    This runs on the master node.
2843

2844
    """
2845
    env = {
2846
      "OP_TARGET": self.op.node_name,
2847
      "MASTER_CANDIDATE": str(self.op.master_candidate),
2848
      "OFFLINE": str(self.op.offline),
2849
      "DRAINED": str(self.op.drained),
2850
      }
2851
    nl = [self.cfg.GetMasterNode(),
2852
          self.op.node_name]
2853
    return env, nl, nl
2854

    
2855
  def CheckPrereq(self):
2856
    """Check prerequisites.
2857

2858
    This only checks the instance list against the existing names.
2859

2860
    """
2861
    node = self.node = self.cfg.GetNodeInfo(self.op.node_name)
2862

    
2863
    if ((self.op.master_candidate == False or self.op.offline == True or
2864
         self.op.drained == True) and node.master_candidate):
2865
      # we will demote the node from master_candidate
2866
      if self.op.node_name == self.cfg.GetMasterNode():
2867
        raise errors.OpPrereqError("The master node has to be a"
2868
                                   " master candidate, online and not drained")
2869
      cp_size = self.cfg.GetClusterInfo().candidate_pool_size
2870
      num_candidates, _ = self.cfg.GetMasterCandidateStats()
2871
      if num_candidates <= cp_size:
2872
        msg = ("Not enough master candidates (desired"
2873
               " %d, new value will be %d)" % (cp_size, num_candidates-1))
2874
        if self.op.force:
2875
          self.LogWarning(msg)
2876
        else:
2877
          raise errors.OpPrereqError(msg)
2878

    
2879
    if (self.op.master_candidate == True and
2880
        ((node.offline and not self.op.offline == False) or
2881
         (node.drained and not self.op.drained == False))):
2882
      raise errors.OpPrereqError("Node '%s' is offline or drained, can't set"
2883
                                 " to master_candidate" % node.name)
2884

    
2885
    return
2886

    
2887
  def Exec(self, feedback_fn):
2888
    """Modifies a node.
2889

2890
    """
2891
    node = self.node
2892

    
2893
    result = []
2894
    changed_mc = False
2895

    
2896
    if self.op.offline is not None:
2897
      node.offline = self.op.offline
2898
      result.append(("offline", str(self.op.offline)))
2899
      if self.op.offline == True:
2900
        if node.master_candidate:
2901
          node.master_candidate = False
2902
          changed_mc = True
2903
          result.append(("master_candidate", "auto-demotion due to offline"))
2904
        if node.drained:
2905
          node.drained = False
2906
          result.append(("drained", "clear drained status due to offline"))
2907

    
2908
    if self.op.master_candidate is not None:
2909
      node.master_candidate = self.op.master_candidate
2910
      changed_mc = True
2911
      result.append(("master_candidate", str(self.op.master_candidate)))
2912
      if self.op.master_candidate == False:
2913
        rrc = self.rpc.call_node_demote_from_mc(node.name)
2914
        msg = rrc.fail_msg
2915
        if msg:
2916
          self.LogWarning("Node failed to demote itself: %s" % msg)
2917

    
2918
    if self.op.drained is not None:
2919
      node.drained = self.op.drained
2920
      result.append(("drained", str(self.op.drained)))
2921
      if self.op.drained == True:
2922
        if node.master_candidate:
2923
          node.master_candidate = False
2924
          changed_mc = True
2925
          result.append(("master_candidate", "auto-demotion due to drain"))
2926
          rrc = self.rpc.call_node_demote_from_mc(node.name)
2927
          msg = rrc.RemoteFailMsg()
2928
          if msg:
2929
            self.LogWarning("Node failed to demote itself: %s" % msg)
2930
        if node.offline:
2931
          node.offline = False
2932
          result.append(("offline", "clear offline status due to drain"))
2933

    
2934
    # this will trigger configuration file update, if needed
2935
    self.cfg.Update(node)
2936
    # this will trigger job queue propagation or cleanup
2937
    if changed_mc:
2938
      self.context.ReaddNode(node)
2939

    
2940
    return result
2941

    
2942

    
2943
class LUPowercycleNode(NoHooksLU):
2944
  """Powercycles a node.
2945

2946
  """
2947
  _OP_REQP = ["node_name", "force"]
2948
  REQ_BGL = False
2949

    
2950
  def CheckArguments(self):
2951
    node_name = self.cfg.ExpandNodeName(self.op.node_name)
2952
    if node_name is None:
2953
      raise errors.OpPrereqError("Invalid node name '%s'" % self.op.node_name)
2954
    self.op.node_name = node_name
2955
    if node_name == self.cfg.GetMasterNode() and not self.op.force:
2956
      raise errors.OpPrereqError("The node is the master and the force"
2957
                                 " parameter was not set")
2958

    
2959
  def ExpandNames(self):
2960
    """Locking for PowercycleNode.
2961

2962
    This is a last-resort option and shouldn't block on other
2963
    jobs. Therefore, we grab no locks.
2964

2965
    """
2966
    self.needed_locks = {}
2967

    
2968
  def CheckPrereq(self):
2969
    """Check prerequisites.
2970

2971
    This LU has no prereqs.
2972

2973
    """
2974
    pass
2975

    
2976
  def Exec(self, feedback_fn):
2977
    """Reboots a node.
2978

2979
    """
2980
    result = self.rpc.call_node_powercycle(self.op.node_name,
2981
                                           self.cfg.GetHypervisorType())
2982
    result.Raise("Failed to schedule the reboot")
2983
    return result.payload
2984

    
2985

    
2986
class LUQueryClusterInfo(NoHooksLU):
2987
  """Query cluster configuration.
2988

2989
  """
2990
  _OP_REQP = []
2991
  REQ_BGL = False
2992

    
2993
  def ExpandNames(self):
2994
    self.needed_locks = {}
2995

    
2996
  def CheckPrereq(self):
2997
    """No prerequsites needed for this LU.
2998

2999
    """
3000
    pass
3001

    
3002
  def Exec(self, feedback_fn):
3003
    """Return cluster config.
3004

3005
    """
3006
    cluster = self.cfg.GetClusterInfo()
3007
    result = {
3008
      "software_version": constants.RELEASE_VERSION,
3009
      "protocol_version": constants.PROTOCOL_VERSION,
3010
      "config_version": constants.CONFIG_VERSION,
3011
      "os_api_version": max(constants.OS_API_VERSIONS),
3012
      "export_version": constants.EXPORT_VERSION,
3013
      "architecture": (platform.architecture()[0], platform.machine()),
3014
      "name": cluster.cluster_name,
3015
      "master": cluster.master_node,
3016
      "default_hypervisor": cluster.enabled_hypervisors[0],
3017
      "enabled_hypervisors": cluster.enabled_hypervisors,
3018
      "hvparams": dict([(hypervisor_name, cluster.hvparams[hypervisor_name])
3019
                        for hypervisor_name in cluster.enabled_hypervisors]),
3020
      "beparams": cluster.beparams,
3021
      "nicparams": cluster.nicparams,
3022
      "candidate_pool_size": cluster.candidate_pool_size,
3023
      "master_netdev": cluster.master_netdev,
3024
      "volume_group_name": cluster.volume_group_name,
3025
      "file_storage_dir": cluster.file_storage_dir,
3026
      }
3027

    
3028
    return result
3029

    
3030

    
3031
class LUQueryConfigValues(NoHooksLU):
3032
  """Return configuration values.
3033

3034
  """
3035
  _OP_REQP = []
3036
  REQ_BGL = False
3037
  _FIELDS_DYNAMIC = utils.FieldSet()
3038
  _FIELDS_STATIC = utils.FieldSet("cluster_name", "master_node", "drain_flag")
3039

    
3040
  def ExpandNames(self):
3041
    self.needed_locks = {}
3042

    
3043
    _CheckOutputFields(static=self._FIELDS_STATIC,
3044
                       dynamic=self._FIELDS_DYNAMIC,
3045
                       selected=self.op.output_fields)
3046

    
3047
  def CheckPrereq(self):
3048
    """No prerequisites.
3049

3050
    """
3051
    pass
3052

    
3053
  def Exec(self, feedback_fn):
3054
    """Dump a representation of the cluster config to the standard output.
3055

3056
    """
3057
    values = []
3058
    for field in self.op.output_fields:
3059
      if field == "cluster_name":
3060
        entry = self.cfg.GetClusterName()
3061
      elif field == "master_node":
3062
        entry = self.cfg.GetMasterNode()
3063
      elif field == "drain_flag":
3064
        entry = os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
3065
      else:
3066
        raise errors.ParameterError(field)
3067
      values.append(entry)
3068
    return values
3069

    
3070

    
3071
class LUActivateInstanceDisks(NoHooksLU):
3072
  """Bring up an instance's disks.
3073

3074
  """
3075
  _OP_REQP = ["instance_name"]
3076
  REQ_BGL = False
3077

    
3078
  def ExpandNames(self):
3079
    self._ExpandAndLockInstance()
3080
    self.needed_locks[locking.LEVEL_NODE] = []
3081
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3082

    
3083
  def DeclareLocks(self, level):
3084
    if level == locking.LEVEL_NODE:
3085
      self._LockInstancesNodes()
3086

    
3087
  def CheckPrereq(self):
3088
    """Check prerequisites.
3089

3090
    This checks that the instance is in the cluster.
3091

3092
    """
3093
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3094
    assert self.instance is not None, \
3095
      "Cannot retrieve locked instance %s" % self.op.instance_name
3096
    _CheckNodeOnline(self, self.instance.primary_node)
3097
    if not hasattr(self.op, "ignore_size"):
3098
      self.op.ignore_size = False
3099

    
3100
  def Exec(self, feedback_fn):
3101
    """Activate the disks.
3102

3103
    """
3104
    disks_ok, disks_info = \
3105
              _AssembleInstanceDisks(self, self.instance,
3106
                                     ignore_size=self.op.ignore_size)
3107
    if not disks_ok:
3108
      raise errors.OpExecError("Cannot activate block devices")
3109

    
3110
    return disks_info
3111

    
3112

    
3113
def _AssembleInstanceDisks(lu, instance, ignore_secondaries=False,
3114
                           ignore_size=False):
3115
  """Prepare the block devices for an instance.
3116

3117
  This sets up the block devices on all nodes.
3118

3119
  @type lu: L{LogicalUnit}
3120
  @param lu: the logical unit on whose behalf we execute
3121
  @type instance: L{objects.Instance}
3122
  @param instance: the instance for whose disks we assemble
3123
  @type ignore_secondaries: boolean
3124
  @param ignore_secondaries: if true, errors on secondary nodes
3125
      won't result in an error return from the function
3126
  @type ignore_size: boolean
3127
  @param ignore_size: if true, the current known size of the disk
3128
      will not be used during the disk activation, useful for cases
3129
      when the size is wrong
3130
  @return: False if the operation failed, otherwise a list of
3131
      (host, instance_visible_name, node_visible_name)
3132
      with the mapping from node devices to instance devices
3133

3134
  """
3135
  device_info = []
3136
  disks_ok = True
3137
  iname = instance.name
3138
  # With the two passes mechanism we try to reduce the window of
3139
  # opportunity for the race condition of switching DRBD to primary
3140
  # before handshaking occured, but we do not eliminate it
3141

    
3142
  # The proper fix would be to wait (with some limits) until the
3143
  # connection has been made and drbd transitions from WFConnection
3144
  # into any other network-connected state (Connected, SyncTarget,
3145
  # SyncSource, etc.)
3146

    
3147
  # 1st pass, assemble on all nodes in secondary mode
3148
  for inst_disk in instance.disks:
3149
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3150
      if ignore_size:
3151
        node_disk = node_disk.Copy()
3152
        node_disk.UnsetSize()
3153
      lu.cfg.SetDiskID(node_disk, node)
3154
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, False)
3155
      msg = result.fail_msg
3156
      if msg:
3157
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3158
                           " (is_primary=False, pass=1): %s",
3159
                           inst_disk.iv_name, node, msg)
3160
        if not ignore_secondaries:
3161
          disks_ok = False
3162

    
3163
  # FIXME: race condition on drbd migration to primary
3164

    
3165
  # 2nd pass, do only the primary node
3166
  for inst_disk in instance.disks:
3167
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
3168
      if node != instance.primary_node:
3169
        continue
3170
      if ignore_size:
3171
        node_disk = node_disk.Copy()
3172
        node_disk.UnsetSize()
3173
      lu.cfg.SetDiskID(node_disk, node)
3174
      result = lu.rpc.call_blockdev_assemble(node, node_disk, iname, True)
3175
      msg = result.fail_msg
3176
      if msg:
3177
        lu.proc.LogWarning("Could not prepare block device %s on node %s"
3178
                           " (is_primary=True, pass=2): %s",
3179
                           inst_disk.iv_name, node, msg)
3180
        disks_ok = False
3181
    device_info.append((instance.primary_node, inst_disk.iv_name,
3182
                        result.payload))
3183

    
3184
  # leave the disks configured for the primary node
3185
  # this is a workaround that would be fixed better by
3186
  # improving the logical/physical id handling
3187
  for disk in instance.disks:
3188
    lu.cfg.SetDiskID(disk, instance.primary_node)
3189

    
3190
  return disks_ok, device_info
3191

    
3192

    
3193
def _StartInstanceDisks(lu, instance, force):
3194
  """Start the disks of an instance.
3195

3196
  """
3197
  disks_ok, _ = _AssembleInstanceDisks(lu, instance,
3198
                                           ignore_secondaries=force)
3199
  if not disks_ok:
3200
    _ShutdownInstanceDisks(lu, instance)
3201
    if force is not None and not force:
3202
      lu.proc.LogWarning("", hint="If the message above refers to a"
3203
                         " secondary node,"
3204
                         " you can retry the operation using '--force'.")
3205
    raise errors.OpExecError("Disk consistency error")
3206

    
3207

    
3208
class LUDeactivateInstanceDisks(NoHooksLU):
3209
  """Shutdown an instance's disks.
3210

3211
  """
3212
  _OP_REQP = ["instance_name"]
3213
  REQ_BGL = False
3214

    
3215
  def ExpandNames(self):
3216
    self._ExpandAndLockInstance()
3217
    self.needed_locks[locking.LEVEL_NODE] = []
3218
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3219

    
3220
  def DeclareLocks(self, level):
3221
    if level == locking.LEVEL_NODE:
3222
      self._LockInstancesNodes()
3223

    
3224
  def CheckPrereq(self):
3225
    """Check prerequisites.
3226

3227
    This checks that the instance is in the cluster.
3228

3229
    """
3230
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3231
    assert self.instance is not None, \
3232
      "Cannot retrieve locked instance %s" % self.op.instance_name
3233

    
3234
  def Exec(self, feedback_fn):
3235
    """Deactivate the disks
3236

3237
    """
3238
    instance = self.instance
3239
    _SafeShutdownInstanceDisks(self, instance)
3240

    
3241

    
3242
def _SafeShutdownInstanceDisks(lu, instance):
3243
  """Shutdown block devices of an instance.
3244

3245
  This function checks if an instance is running, before calling
3246
  _ShutdownInstanceDisks.
3247

3248
  """
3249
  pnode = instance.primary_node
3250
  ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
3251
  ins_l.Raise("Can't contact node %s" % pnode)
3252

    
3253
  if instance.name in ins_l.payload:
3254
    raise errors.OpExecError("Instance is running, can't shutdown"
3255
                             " block devices.")
3256

    
3257
  _ShutdownInstanceDisks(lu, instance)
3258

    
3259

    
3260
def _ShutdownInstanceDisks(lu, instance, ignore_primary=False):
3261
  """Shutdown block devices of an instance.
3262

3263
  This does the shutdown on all nodes of the instance.
3264

3265
  If the ignore_primary is false, errors on the primary node are
3266
  ignored.
3267

3268
  """
3269
  all_result = True
3270
  for disk in instance.disks:
3271
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
3272
      lu.cfg.SetDiskID(top_disk, node)
3273
      result = lu.rpc.call_blockdev_shutdown(node, top_disk)
3274
      msg = result.fail_msg
3275
      if msg:
3276
        lu.LogWarning("Could not shutdown block device %s on node %s: %s",
3277
                      disk.iv_name, node, msg)
3278
        if not ignore_primary or node != instance.primary_node:
3279
          all_result = False
3280
  return all_result
3281

    
3282

    
3283
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
3284
  """Checks if a node has enough free memory.
3285

3286
  This function check if a given node has the needed amount of free
3287
  memory. In case the node has less memory or we cannot get the
3288
  information from the node, this function raise an OpPrereqError
3289
  exception.
3290

3291
  @type lu: C{LogicalUnit}
3292
  @param lu: a logical unit from which we get configuration data
3293
  @type node: C{str}
3294
  @param node: the node to check
3295
  @type reason: C{str}
3296
  @param reason: string to use in the error message
3297
  @type requested: C{int}
3298
  @param requested: the amount of memory in MiB to check for
3299
  @type hypervisor_name: C{str}
3300
  @param hypervisor_name: the hypervisor to ask for memory stats
3301
  @raise errors.OpPrereqError: if the node doesn't have enough memory, or
3302
      we cannot check the node
3303

3304
  """
3305
  nodeinfo = lu.rpc.call_node_info([node], lu.cfg.GetVGName(), hypervisor_name)
3306
  nodeinfo[node].Raise("Can't get data from node %s" % node, prereq=True)
3307
  free_mem = nodeinfo[node].payload.get('memory_free', None)
3308
  if not isinstance(free_mem, int):
3309
    raise errors.OpPrereqError("Can't compute free memory on node %s, result"
3310
                               " was '%s'" % (node, free_mem))
3311
  if requested > free_mem:
3312
    raise errors.OpPrereqError("Not enough memory on node %s for %s:"
3313
                               " needed %s MiB, available %s MiB" %
3314
                               (node, reason, requested, free_mem))
3315

    
3316

    
3317
class LUStartupInstance(LogicalUnit):
3318
  """Starts an instance.
3319

3320
  """
3321
  HPATH = "instance-start"
3322
  HTYPE = constants.HTYPE_INSTANCE
3323
  _OP_REQP = ["instance_name", "force"]
3324
  REQ_BGL = False
3325

    
3326
  def ExpandNames(self):
3327
    self._ExpandAndLockInstance()
3328

    
3329
  def BuildHooksEnv(self):
3330
    """Build hooks env.
3331

3332
    This runs on master, primary and secondary nodes of the instance.
3333

3334
    """
3335
    env = {
3336
      "FORCE": self.op.force,
3337
      }
3338
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3339
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3340
    return env, nl, nl
3341

    
3342
  def CheckPrereq(self):
3343
    """Check prerequisites.
3344

3345
    This checks that the instance is in the cluster.
3346

3347
    """
3348
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3349
    assert self.instance is not None, \
3350
      "Cannot retrieve locked instance %s" % self.op.instance_name
3351

    
3352
    # extra beparams
3353
    self.beparams = getattr(self.op, "beparams", {})
3354
    if self.beparams:
3355
      if not isinstance(self.beparams, dict):
3356
        raise errors.OpPrereqError("Invalid beparams passed: %s, expected"
3357
                                   " dict" % (type(self.beparams), ))
3358
      # fill the beparams dict
3359
      utils.ForceDictType(self.beparams, constants.BES_PARAMETER_TYPES)
3360
      self.op.beparams = self.beparams
3361

    
3362
    # extra hvparams
3363
    self.hvparams = getattr(self.op, "hvparams", {})
3364
    if self.hvparams:
3365
      if not isinstance(self.hvparams, dict):
3366
        raise errors.OpPrereqError("Invalid hvparams passed: %s, expected"
3367
                                   " dict" % (type(self.hvparams), ))
3368

    
3369
      # check hypervisor parameter syntax (locally)
3370
      cluster = self.cfg.GetClusterInfo()
3371
      utils.ForceDictType(self.hvparams, constants.HVS_PARAMETER_TYPES)
3372
      filled_hvp = objects.FillDict(cluster.hvparams[instance.hypervisor],
3373
                                    instance.hvparams)
3374
      filled_hvp.update(self.hvparams)
3375
      hv_type = hypervisor.GetHypervisor(instance.hypervisor)
3376
      hv_type.CheckParameterSyntax(filled_hvp)
3377
      _CheckHVParams(self, instance.all_nodes, instance.hypervisor, filled_hvp)
3378
      self.op.hvparams = self.hvparams
3379

    
3380
    _CheckNodeOnline(self, instance.primary_node)
3381

    
3382
    bep = self.cfg.GetClusterInfo().FillBE(instance)
3383
    # check bridges existence
3384
    _CheckInstanceBridgesExist(self, instance)
3385

    
3386
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3387
                                              instance.name,
3388
                                              instance.hypervisor)
3389
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3390
                      prereq=True)
3391
    if not remote_info.payload: # not running already
3392
      _CheckNodeFreeMemory(self, instance.primary_node,
3393
                           "starting instance %s" % instance.name,
3394
                           bep[constants.BE_MEMORY], instance.hypervisor)
3395

    
3396
  def Exec(self, feedback_fn):
3397
    """Start the instance.
3398

3399
    """
3400
    instance = self.instance
3401
    force = self.op.force
3402

    
3403
    self.cfg.MarkInstanceUp(instance.name)
3404

    
3405
    node_current = instance.primary_node
3406

    
3407
    _StartInstanceDisks(self, instance, force)
3408

    
3409
    result = self.rpc.call_instance_start(node_current, instance,
3410
                                          self.hvparams, self.beparams)
3411
    msg = result.fail_msg
3412
    if msg:
3413
      _ShutdownInstanceDisks(self, instance)
3414
      raise errors.OpExecError("Could not start instance: %s" % msg)
3415

    
3416

    
3417
class LURebootInstance(LogicalUnit):
3418
  """Reboot an instance.
3419

3420
  """
3421
  HPATH = "instance-reboot"
3422
  HTYPE = constants.HTYPE_INSTANCE
3423
  _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
3424
  REQ_BGL = False
3425

    
3426
  def ExpandNames(self):
3427
    if self.op.reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
3428
                                   constants.INSTANCE_REBOOT_HARD,
3429
                                   constants.INSTANCE_REBOOT_FULL]:
3430
      raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
3431
                                  (constants.INSTANCE_REBOOT_SOFT,
3432
                                   constants.INSTANCE_REBOOT_HARD,
3433
                                   constants.INSTANCE_REBOOT_FULL))
3434
    self._ExpandAndLockInstance()
3435

    
3436
  def BuildHooksEnv(self):
3437
    """Build hooks env.
3438

3439
    This runs on master, primary and secondary nodes of the instance.
3440

3441
    """
3442
    env = {
3443
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
3444
      "REBOOT_TYPE": self.op.reboot_type,
3445
      }
3446
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
3447
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3448
    return env, nl, nl
3449

    
3450
  def CheckPrereq(self):
3451
    """Check prerequisites.
3452

3453
    This checks that the instance is in the cluster.
3454

3455
    """
3456
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3457
    assert self.instance is not None, \
3458
      "Cannot retrieve locked instance %s" % self.op.instance_name
3459

    
3460
    _CheckNodeOnline(self, instance.primary_node)
3461

    
3462
    # check bridges existence
3463
    _CheckInstanceBridgesExist(self, instance)
3464

    
3465
  def Exec(self, feedback_fn):
3466
    """Reboot the instance.
3467

3468
    """
3469
    instance = self.instance
3470
    ignore_secondaries = self.op.ignore_secondaries
3471
    reboot_type = self.op.reboot_type
3472

    
3473
    node_current = instance.primary_node
3474

    
3475
    if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
3476
                       constants.INSTANCE_REBOOT_HARD]:
3477
      for disk in instance.disks:
3478
        self.cfg.SetDiskID(disk, node_current)
3479
      result = self.rpc.call_instance_reboot(node_current, instance,
3480
                                             reboot_type)
3481
      result.Raise("Could not reboot instance")
3482
    else:
3483
      result = self.rpc.call_instance_shutdown(node_current, instance)
3484
      result.Raise("Could not shutdown instance for full reboot")
3485
      _ShutdownInstanceDisks(self, instance)
3486
      _StartInstanceDisks(self, instance, ignore_secondaries)
3487
      result = self.rpc.call_instance_start(node_current, instance, None, None)
3488
      msg = result.fail_msg
3489
      if msg:
3490
        _ShutdownInstanceDisks(self, instance)
3491
        raise errors.OpExecError("Could not start instance for"
3492
                                 " full reboot: %s" % msg)
3493

    
3494
    self.cfg.MarkInstanceUp(instance.name)
3495

    
3496

    
3497
class LUShutdownInstance(LogicalUnit):
3498
  """Shutdown an instance.
3499

3500
  """
3501
  HPATH = "instance-stop"
3502
  HTYPE = constants.HTYPE_INSTANCE
3503
  _OP_REQP = ["instance_name"]
3504
  REQ_BGL = False
3505

    
3506
  def ExpandNames(self):
3507
    self._ExpandAndLockInstance()
3508

    
3509
  def BuildHooksEnv(self):
3510
    """Build hooks env.
3511

3512
    This runs on master, primary and secondary nodes of the instance.
3513

3514
    """
3515
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3516
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3517
    return env, nl, nl
3518

    
3519
  def CheckPrereq(self):
3520
    """Check prerequisites.
3521

3522
    This checks that the instance is in the cluster.
3523

3524
    """
3525
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3526
    assert self.instance is not None, \
3527
      "Cannot retrieve locked instance %s" % self.op.instance_name
3528
    _CheckNodeOnline(self, self.instance.primary_node)
3529

    
3530
  def Exec(self, feedback_fn):
3531
    """Shutdown the instance.
3532

3533
    """
3534
    instance = self.instance
3535
    node_current = instance.primary_node
3536
    self.cfg.MarkInstanceDown(instance.name)
3537
    result = self.rpc.call_instance_shutdown(node_current, instance)
3538
    msg = result.fail_msg
3539
    if msg:
3540
      self.proc.LogWarning("Could not shutdown instance: %s" % msg)
3541

    
3542
    _ShutdownInstanceDisks(self, instance)
3543

    
3544

    
3545
class LUReinstallInstance(LogicalUnit):
3546
  """Reinstall an instance.
3547

3548
  """
3549
  HPATH = "instance-reinstall"
3550
  HTYPE = constants.HTYPE_INSTANCE
3551
  _OP_REQP = ["instance_name"]
3552
  REQ_BGL = False
3553

    
3554
  def ExpandNames(self):
3555
    self._ExpandAndLockInstance()
3556

    
3557
  def BuildHooksEnv(self):
3558
    """Build hooks env.
3559

3560
    This runs on master, primary and secondary nodes of the instance.
3561

3562
    """
3563
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3564
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3565
    return env, nl, nl
3566

    
3567
  def CheckPrereq(self):
3568
    """Check prerequisites.
3569

3570
    This checks that the instance is in the cluster and is not running.
3571

3572
    """
3573
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3574
    assert instance is not None, \
3575
      "Cannot retrieve locked instance %s" % self.op.instance_name
3576
    _CheckNodeOnline(self, instance.primary_node)
3577

    
3578
    if instance.disk_template == constants.DT_DISKLESS:
3579
      raise errors.OpPrereqError("Instance '%s' has no disks" %
3580
                                 self.op.instance_name)
3581
    if instance.admin_up:
3582
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3583
                                 self.op.instance_name)
3584
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3585
                                              instance.name,
3586
                                              instance.hypervisor)
3587
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3588
                      prereq=True)
3589
    if remote_info.payload:
3590
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3591
                                 (self.op.instance_name,
3592
                                  instance.primary_node))
3593

    
3594
    self.op.os_type = getattr(self.op, "os_type", None)
3595
    if self.op.os_type is not None:
3596
      # OS verification
3597
      pnode = self.cfg.GetNodeInfo(
3598
        self.cfg.ExpandNodeName(instance.primary_node))
3599
      if pnode is None:
3600
        raise errors.OpPrereqError("Primary node '%s' is unknown" %
3601
                                   self.op.pnode)
3602
      result = self.rpc.call_os_get(pnode.name, self.op.os_type)
3603
      result.Raise("OS '%s' not in supported OS list for primary node %s" %
3604
                   (self.op.os_type, pnode.name), prereq=True)
3605

    
3606
    self.instance = instance
3607

    
3608
  def Exec(self, feedback_fn):
3609
    """Reinstall the instance.
3610

3611
    """
3612
    inst = self.instance
3613

    
3614
    if self.op.os_type is not None:
3615
      feedback_fn("Changing OS to '%s'..." % self.op.os_type)
3616
      inst.os = self.op.os_type
3617
      self.cfg.Update(inst)
3618

    
3619
    _StartInstanceDisks(self, inst, None)
3620
    try:
3621
      feedback_fn("Running the instance OS create scripts...")
3622
      result = self.rpc.call_instance_os_add(inst.primary_node, inst, True)
3623
      result.Raise("Could not install OS for instance %s on node %s" %
3624
                   (inst.name, inst.primary_node))
3625
    finally:
3626
      _ShutdownInstanceDisks(self, inst)
3627

    
3628

    
3629
class LURecreateInstanceDisks(LogicalUnit):
3630
  """Recreate an instance's missing disks.
3631

3632
  """
3633
  HPATH = "instance-recreate-disks"
3634
  HTYPE = constants.HTYPE_INSTANCE
3635
  _OP_REQP = ["instance_name", "disks"]
3636
  REQ_BGL = False
3637

    
3638
  def CheckArguments(self):
3639
    """Check the arguments.
3640

3641
    """
3642
    if not isinstance(self.op.disks, list):
3643
      raise errors.OpPrereqError("Invalid disks parameter")
3644
    for item in self.op.disks:
3645
      if (not isinstance(item, int) or
3646
          item < 0):
3647
        raise errors.OpPrereqError("Invalid disk specification '%s'" %
3648
                                   str(item))
3649

    
3650
  def ExpandNames(self):
3651
    self._ExpandAndLockInstance()
3652

    
3653
  def BuildHooksEnv(self):
3654
    """Build hooks env.
3655

3656
    This runs on master, primary and secondary nodes of the instance.
3657

3658
    """
3659
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3660
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3661
    return env, nl, nl
3662

    
3663
  def CheckPrereq(self):
3664
    """Check prerequisites.
3665

3666
    This checks that the instance is in the cluster and is not running.
3667

3668
    """
3669
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3670
    assert instance is not None, \
3671
      "Cannot retrieve locked instance %s" % self.op.instance_name
3672
    _CheckNodeOnline(self, instance.primary_node)
3673

    
3674
    if instance.disk_template == constants.DT_DISKLESS:
3675
      raise errors.OpPrereqError("Instance '%s' has no disks" %
3676
                                 self.op.instance_name)
3677
    if instance.admin_up:
3678
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3679
                                 self.op.instance_name)
3680
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3681
                                              instance.name,
3682
                                              instance.hypervisor)
3683
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3684
                      prereq=True)
3685
    if remote_info.payload:
3686
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3687
                                 (self.op.instance_name,
3688
                                  instance.primary_node))
3689

    
3690
    if not self.op.disks:
3691
      self.op.disks = range(len(instance.disks))
3692
    else:
3693
      for idx in self.op.disks:
3694
        if idx >= len(instance.disks):
3695
          raise errors.OpPrereqError("Invalid disk index passed '%s'" % idx)
3696

    
3697
    self.instance = instance
3698

    
3699
  def Exec(self, feedback_fn):
3700
    """Recreate the disks.
3701

3702
    """
3703
    to_skip = []
3704
    for idx, disk in enumerate(self.instance.disks):
3705
      if idx not in self.op.disks: # disk idx has not been passed in
3706
        to_skip.append(idx)
3707
        continue
3708

    
3709
    _CreateDisks(self, self.instance, to_skip=to_skip)
3710

    
3711

    
3712
class LURenameInstance(LogicalUnit):
3713
  """Rename an instance.
3714

3715
  """
3716
  HPATH = "instance-rename"
3717
  HTYPE = constants.HTYPE_INSTANCE
3718
  _OP_REQP = ["instance_name", "new_name"]
3719

    
3720
  def BuildHooksEnv(self):
3721
    """Build hooks env.
3722

3723
    This runs on master, primary and secondary nodes of the instance.
3724

3725
    """
3726
    env = _BuildInstanceHookEnvByObject(self, self.instance)
3727
    env["INSTANCE_NEW_NAME"] = self.op.new_name
3728
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
3729
    return env, nl, nl
3730

    
3731
  def CheckPrereq(self):
3732
    """Check prerequisites.
3733

3734
    This checks that the instance is in the cluster and is not running.
3735

3736
    """
3737
    instance = self.cfg.GetInstanceInfo(
3738
      self.cfg.ExpandInstanceName(self.op.instance_name))
3739
    if instance is None:
3740
      raise errors.OpPrereqError("Instance '%s' not known" %
3741
                                 self.op.instance_name)
3742
    _CheckNodeOnline(self, instance.primary_node)
3743

    
3744
    if instance.admin_up:
3745
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
3746
                                 self.op.instance_name)
3747
    remote_info = self.rpc.call_instance_info(instance.primary_node,
3748
                                              instance.name,
3749
                                              instance.hypervisor)
3750
    remote_info.Raise("Error checking node %s" % instance.primary_node,
3751
                      prereq=True)
3752
    if remote_info.payload:
3753
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
3754
                                 (self.op.instance_name,
3755
                                  instance.primary_node))
3756
    self.instance = instance
3757

    
3758
    # new name verification
3759
    name_info = utils.HostInfo(self.op.new_name)
3760

    
3761
    self.op.new_name = new_name = name_info.name
3762
    instance_list = self.cfg.GetInstanceList()
3763
    if new_name in instance_list:
3764
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
3765
                                 new_name)
3766

    
3767
    if not getattr(self.op, "ignore_ip", False):
3768
      if utils.TcpPing(name_info.ip, constants.DEFAULT_NODED_PORT):
3769
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
3770
                                   (name_info.ip, new_name))
3771

    
3772

    
3773
  def Exec(self, feedback_fn):
3774
    """Reinstall the instance.
3775

3776
    """
3777
    inst = self.instance
3778
    old_name = inst.name
3779

    
3780
    if inst.disk_template == constants.DT_FILE:
3781
      old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
3782

    
3783
    self.cfg.RenameInstance(inst.name, self.op.new_name)
3784
    # Change the instance lock. This is definitely safe while we hold the BGL
3785
    self.context.glm.remove(locking.LEVEL_INSTANCE, old_name)
3786
    self.context.glm.add(locking.LEVEL_INSTANCE, self.op.new_name)
3787

    
3788
    # re-read the instance from the configuration after rename
3789
    inst = self.cfg.GetInstanceInfo(self.op.new_name)
3790

    
3791
    if inst.disk_template == constants.DT_FILE:
3792
      new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
3793
      result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
3794
                                                     old_file_storage_dir,
3795
                                                     new_file_storage_dir)
3796
      result.Raise("Could not rename on node %s directory '%s' to '%s'"
3797
                   " (but the instance has been renamed in Ganeti)" %
3798
                   (inst.primary_node, old_file_storage_dir,
3799
                    new_file_storage_dir))
3800

    
3801
    _StartInstanceDisks(self, inst, None)
3802
    try:
3803
      result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
3804
                                                 old_name)
3805
      msg = result.fail_msg
3806
      if msg:
3807
        msg = ("Could not run OS rename script for instance %s on node %s"
3808
               " (but the instance has been renamed in Ganeti): %s" %
3809
               (inst.name, inst.primary_node, msg))
3810
        self.proc.LogWarning(msg)
3811
    finally:
3812
      _ShutdownInstanceDisks(self, inst)
3813

    
3814

    
3815
class LURemoveInstance(LogicalUnit):
3816
  """Remove an instance.
3817

3818
  """
3819
  HPATH = "instance-remove"
3820
  HTYPE = constants.HTYPE_INSTANCE
3821
  _OP_REQP = ["instance_name", "ignore_failures"]
3822
  REQ_BGL = False
3823

    
3824
  def ExpandNames(self):
3825
    self._ExpandAndLockInstance()
3826
    self.needed_locks[locking.LEVEL_NODE] = []
3827
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3828

    
3829
  def DeclareLocks(self, level):
3830
    if level == locking.LEVEL_NODE:
3831
      self._LockInstancesNodes()
3832

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

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

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

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

3846
    This checks that the instance is in the cluster.
3847

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

    
3853
  def Exec(self, feedback_fn):
3854
    """Remove the instance.
3855

3856
    """
3857
    instance = self.instance
3858
    logging.info("Shutting down instance %s on node %s",
3859
                 instance.name, instance.primary_node)
3860

    
3861
    result = self.rpc.call_instance_shutdown(instance.primary_node, instance)
3862
    msg = result.fail_msg
3863
    if msg:
3864
      if self.op.ignore_failures:
3865
        feedback_fn("Warning: can't shutdown instance: %s" % msg)
3866
      else:
3867
        raise errors.OpExecError("Could not shutdown instance %s on"
3868
                                 " node %s: %s" %
3869
                                 (instance.name, instance.primary_node, msg))
3870

    
3871
    logging.info("Removing block devices for instance %s", instance.name)
3872

    
3873
    if not _RemoveDisks(self, instance):
3874
      if self.op.ignore_failures:
3875
        feedback_fn("Warning: can't remove instance's disks")
3876
      else:
3877
        raise errors.OpExecError("Can't remove instance's disks")
3878

    
3879
    logging.info("Removing instance %s out of cluster config", instance.name)
3880

    
3881
    self.cfg.RemoveInstance(instance.name)
3882
    self.remove_locks[locking.LEVEL_INSTANCE] = instance.name
3883

    
3884

    
3885
class LUQueryInstances(NoHooksLU):
3886
  """Logical unit for querying instances.
3887

3888
  """
3889
  _OP_REQP = ["output_fields", "names", "use_locking"]
3890
  REQ_BGL = False
3891
  _FIELDS_STATIC = utils.FieldSet(*["name", "os", "pnode", "snodes",
3892
                                    "admin_state",
3893
                                    "disk_template", "ip", "mac", "bridge",
3894
                                    "nic_mode", "nic_link",
3895
                                    "sda_size", "sdb_size", "vcpus", "tags",
3896
                                    "network_port", "beparams",
3897
                                    r"(disk)\.(size)/([0-9]+)",
3898
                                    r"(disk)\.(sizes)", "disk_usage",
3899
                                    r"(nic)\.(mac|ip|mode|link)/([0-9]+)",
3900
                                    r"(nic)\.(bridge)/([0-9]+)",
3901
                                    r"(nic)\.(macs|ips|modes|links|bridges)",
3902
                                    r"(disk|nic)\.(count)",
3903
                                    "serial_no", "hypervisor", "hvparams",] +
3904
                                  ["hv/%s" % name
3905
                                   for name in constants.HVS_PARAMETERS] +
3906
                                  ["be/%s" % name
3907
                                   for name in constants.BES_PARAMETERS])
3908
  _FIELDS_DYNAMIC = utils.FieldSet("oper_state", "oper_ram", "status")
3909

    
3910

    
3911
  def ExpandNames(self):
3912
    _CheckOutputFields(static=self._FIELDS_STATIC,
3913
                       dynamic=self._FIELDS_DYNAMIC,
3914
                       selected=self.op.output_fields)
3915

    
3916
    self.needed_locks = {}
3917
    self.share_locks[locking.LEVEL_INSTANCE] = 1
3918
    self.share_locks[locking.LEVEL_NODE] = 1
3919

    
3920
    if self.op.names:
3921
      self.wanted = _GetWantedInstances(self, self.op.names)
3922
    else:
3923
      self.wanted = locking.ALL_SET
3924

    
3925
    self.do_node_query = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
3926
    self.do_locking = self.do_node_query and self.op.use_locking
3927
    if self.do_locking:
3928
      self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
3929
      self.needed_locks[locking.LEVEL_NODE] = []
3930
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
3931

    
3932
  def DeclareLocks(self, level):
3933
    if level == locking.LEVEL_NODE and self.do_locking:
3934
      self._LockInstancesNodes()
3935

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

3939
    """
3940
    pass
3941

    
3942
  def Exec(self, feedback_fn):
3943
    """Computes the list of nodes and their attributes.
3944

3945
    """
3946
    all_info = self.cfg.GetAllInstancesInfo()
3947
    if self.wanted == locking.ALL_SET:
3948
      # caller didn't specify instance names, so ordering is not important
3949
      if self.do_locking:
3950
        instance_names = self.acquired_locks[locking.LEVEL_INSTANCE]
3951
      else:
3952
        instance_names = all_info.keys()
3953
      instance_names = utils.NiceSort(instance_names)
3954
    else:
3955
      # caller did specify names, so we must keep the ordering
3956
      if self.do_locking:
3957
        tgt_set = self.acquired_locks[locking.LEVEL_INSTANCE]
3958
      else:
3959
        tgt_set = all_info.keys()
3960
      missing = set(self.wanted).difference(tgt_set)
3961
      if missing:
3962
        raise errors.OpExecError("Some instances were removed before"
3963
                                 " retrieving their data: %s" % missing)
3964
      instance_names = self.wanted
3965

    
3966
    instance_list = [all_info[iname] for iname in instance_names]
3967

    
3968
    # begin data gathering
3969

    
3970
    nodes = frozenset([inst.primary_node for inst in instance_list])
3971
    hv_list = list(set([inst.hypervisor for inst in instance_list]))
3972

    
3973
    bad_nodes = []
3974
    off_nodes = []
3975
    if self.do_node_query:
3976
      live_data = {}
3977
      node_data = self.rpc.call_all_instances_info(nodes, hv_list)
3978
      for name in nodes:
3979
        result = node_data[name]
3980
        if result.offline:
3981
          # offline nodes will be in both lists
3982
          off_nodes.append(name)
3983
        if result.failed or result.fail_msg:
3984
          bad_nodes.append(name)
3985
        else:
3986
          if result.payload:
3987
            live_data.update(result.payload)
3988
          # else no instance is alive
3989
    else:
3990
      live_data = dict([(name, {}) for name in instance_names])
3991

    
3992
    # end data gathering
3993

    
3994
    HVPREFIX = "hv/"
3995
    BEPREFIX = "be/"
3996
    output = []
3997
    cluster = self.cfg.GetClusterInfo()
3998
    for instance in instance_list:
3999
      iout = []
4000
      i_hv = cluster.FillHV(instance)
4001
      i_be = cluster.FillBE(instance)
4002
      i_nicp = [objects.FillDict(cluster.nicparams[constants.PP_DEFAULT],
4003
                                 nic.nicparams) for nic in instance.nics]
4004
      for field in self.op.output_fields:
4005
        st_match = self._FIELDS_STATIC.Matches(field)
4006
        if field == "name":
4007
          val = instance.name
4008
        elif field == "os":
4009
          val = instance.os
4010
        elif field == "pnode":
4011
          val = instance.primary_node
4012
        elif field == "snodes":
4013
          val = list(instance.secondary_nodes)
4014
        elif field == "admin_state":
4015
          val = instance.admin_up
4016
        elif field == "oper_state":
4017
          if instance.primary_node in bad_nodes:
4018
            val = None
4019
          else:
4020
            val = bool(live_data.get(instance.name))
4021
        elif field == "status":
4022
          if instance.primary_node in off_nodes:
4023
            val = "ERROR_nodeoffline"
4024
          elif instance.primary_node in bad_nodes:
4025
            val = "ERROR_nodedown"
4026
          else:
4027
            running = bool(live_data.get(instance.name))
4028
            if running:
4029
              if instance.admin_up:
4030
                val = "running"
4031
              else:
4032
                val = "ERROR_up"
4033
            else:
4034
              if instance.admin_up:
4035
                val = "ERROR_down"
4036
              else:
4037
                val = "ADMIN_down"
4038
        elif field == "oper_ram":
4039
          if instance.primary_node in bad_nodes:
4040
            val = None
4041
          elif instance.name in live_data:
4042
            val = live_data[instance.name].get("memory", "?")
4043
          else:
4044
            val = "-"
4045
        elif field == "vcpus":
4046
          val = i_be[constants.BE_VCPUS]
4047
        elif field == "disk_template":
4048
          val = instance.disk_template
4049
        elif field == "ip":
4050
          if instance.nics:
4051
            val = instance.nics[0].ip
4052
          else:
4053
            val = None
4054
        elif field == "nic_mode":
4055
          if instance.nics:
4056
            val = i_nicp[0][constants.NIC_MODE]
4057
          else:
4058
            val = None
4059
        elif field == "nic_link":
4060
          if instance.nics:
4061
            val = i_nicp[0][constants.NIC_LINK]
4062
          else:
4063
            val = None
4064
        elif field == "bridge":
4065
          if (instance.nics and
4066
              i_nicp[0][constants.NIC_MODE] == constants.NIC_MODE_BRIDGED):
4067
            val = i_nicp[0][constants.NIC_LINK]
4068
          else:
4069
            val = None
4070
        elif field == "mac":
4071
          if instance.nics:
4072
            val = instance.nics[0].mac
4073
          else:
4074
            val = None
4075
        elif field == "sda_size" or field == "sdb_size":
4076
          idx = ord(field[2]) - ord('a')
4077
          try:
4078
            val = instance.FindDisk(idx).size
4079
          except errors.OpPrereqError:
4080
            val = None
4081
        elif field == "disk_usage": # total disk usage per node
4082
          disk_sizes = [{'size': disk.size} for disk in instance.disks]
4083
          val = _ComputeDiskSize(instance.disk_template, disk_sizes)
4084
        elif field == "tags":
4085
          val = list(instance.GetTags())
4086
        elif field == "serial_no":
4087
          val = instance.serial_no
4088
        elif field == "network_port":
4089
          val = instance.network_port
4090
        elif field == "hypervisor":
4091
          val = instance.hypervisor
4092
        elif field == "hvparams":
4093
          val = i_hv
4094
        elif (field.startswith(HVPREFIX) and
4095
              field[len(HVPREFIX):] in constants.HVS_PARAMETERS):
4096
          val = i_hv.get(field[len(HVPREFIX):], None)
4097
        elif field == "beparams":
4098
          val = i_be
4099
        elif (field.startswith(BEPREFIX) and
4100
              field[len(BEPREFIX):] in constants.BES_PARAMETERS):
4101
          val = i_be.get(field[len(BEPREFIX):], None)
4102
        elif st_match and st_match.groups():
4103
          # matches a variable list
4104
          st_groups = st_match.groups()
4105
          if st_groups and st_groups[0] == "disk":
4106
            if st_groups[1] == "count":
4107
              val = len(instance.disks)
4108
            elif st_groups[1] == "sizes":
4109
              val = [disk.size for disk in instance.disks]
4110
            elif st_groups[1] == "size":
4111
              try:
4112
                val = instance.FindDisk(st_groups[2]).size
4113
              except errors.OpPrereqError:
4114
                val = None
4115
            else:
4116
              assert False, "Unhandled disk parameter"
4117
          elif st_groups[0] == "nic":
4118
            if st_groups[1] == "count":
4119
              val = len(instance.nics)
4120
            elif st_groups[1] == "macs":
4121
              val = [nic.mac for nic in instance.nics]
4122
            elif st_groups[1] == "ips":
4123
              val = [nic.ip for nic in instance.nics]
4124
            elif st_groups[1] == "modes":
4125
              val = [nicp[constants.NIC_MODE] for nicp in i_nicp]
4126
            elif st_groups[1] == "links":
4127
              val = [nicp[constants.NIC_LINK] for nicp in i_nicp]
4128
            elif st_groups[1] == "bridges":
4129
              val = []
4130
              for nicp in i_nicp:
4131
                if nicp[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
4132
                  val.append(nicp[constants.NIC_LINK])
4133
                else:
4134
                  val.append(None)
4135
            else:
4136
              # index-based item
4137
              nic_idx = int(st_groups[2])
4138
              if nic_idx >= len(instance.nics):
4139
                val = None
4140
              else:
4141
                if st_groups[1] == "mac":
4142
                  val = instance.nics[nic_idx].mac
4143
                elif st_groups[1] == "ip":
4144
                  val = instance.nics[nic_idx].ip
4145
                elif st_groups[1] == "mode":
4146
                  val = i_nicp[nic_idx][constants.NIC_MODE]
4147
                elif st_groups[1] == "link":
4148
                  val = i_nicp[nic_idx][constants.NIC_LINK]
4149
                elif st_groups[1] == "bridge":
4150
                  nic_mode = i_nicp[nic_idx][constants.NIC_MODE]
4151
                  if nic_mode == constants.NIC_MODE_BRIDGED:
4152
                    val = i_nicp[nic_idx][constants.NIC_LINK]
4153
                  else:
4154
                    val = None
4155
                else:
4156
                  assert False, "Unhandled NIC parameter"
4157
          else:
4158
            assert False, ("Declared but unhandled variable parameter '%s'" %
4159
                           field)
4160
        else:
4161
          assert False, "Declared but unhandled parameter '%s'" % field
4162
        iout.append(val)
4163
      output.append(iout)
4164

    
4165
    return output
4166

    
4167

    
4168
class LUFailoverInstance(LogicalUnit):
4169
  """Failover an instance.
4170

4171
  """
4172
  HPATH = "instance-failover"
4173
  HTYPE = constants.HTYPE_INSTANCE
4174
  _OP_REQP = ["instance_name", "ignore_consistency"]
4175
  REQ_BGL = False
4176

    
4177
  def ExpandNames(self):
4178
    self._ExpandAndLockInstance()
4179
    self.needed_locks[locking.LEVEL_NODE] = []
4180
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
4181

    
4182
  def DeclareLocks(self, level):
4183
    if level == locking.LEVEL_NODE:
4184
      self._LockInstancesNodes()
4185

    
4186
  def BuildHooksEnv(self):
4187
    """Build hooks env.
4188

4189
    This runs on master, primary and secondary nodes of the instance.
4190

4191
    """
4192
    env = {
4193
      "IGNORE_CONSISTENCY": self.op.ignore_consistency,
4194
      }
4195
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
4196
    nl = [self.cfg.GetMasterNode()] + list(self.instance.secondary_nodes)
4197
    return env, nl, nl
4198

    
4199
  def CheckPrereq(self):
4200
    """Check prerequisites.
4201

4202
    This checks that the instance is in the cluster.
4203

4204
    """
4205
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
4206
    assert self.instance is not None, \
4207
      "Cannot retrieve locked instance %s" % self.op.instance_name
4208

    
4209
    bep = self.cfg.GetClusterInfo().FillBE(instance)
4210
    if instance.disk_template not in constants.DTS_NET_MIRROR:
4211
      raise errors.OpPrereqError("Instance's disk layout is not"
4212
                                 " network mirrored, cannot failover.")
4213

    
4214
    secondary_nodes = instance.secondary_nodes
4215
    if not secondary_nodes:
4216
      raise errors.ProgrammerError("no secondary node but using "
4217
                                   "a mirrored disk template")
4218

    
4219
    target_node = secondary_nodes[0]
4220
    _CheckNodeOnline(self, target_node)
4221
    _CheckNodeNotDrained(self, target_node)
4222
    if instance.admin_up:
4223
      # check memory requirements on the secondary node
4224
      _CheckNodeFreeMemory(self, target_node, "failing over instance %s" %
4225
                           instance.name, bep[constants.BE_MEMORY],
4226
                           instance.hypervisor)
4227
    else:
4228
      self.LogInfo("Not checking memory on the secondary node as"
4229
                   " instance will not be started")
4230

    
4231
    # check bridge existance
4232
    _CheckInstanceBridgesExist(self, instance, node=target_node)
4233

    
4234
  def Exec(self, feedback_fn):
4235
    """Failover an instance.
4236

4237
    The failover is done by shutting it down on its present node and
4238
    starting it on the secondary.
4239

4240
    """
4241
    instance = self.instance
4242

    
4243
    source_node = instance.primary_node
4244
    target_node = instance.secondary_nodes[0]
4245

    
4246
    feedback_fn("* checking disk consistency between source and target")
4247
    for dev in instance.disks:
4248
      # for drbd, these are drbd over lvm
4249
      if not _CheckDiskConsistency(self, dev, target_node, False):
4250
        if instance.admin_up and not self.op.ignore_consistency:
4251
          raise errors.OpExecError("Disk %s is degraded on target node,"
4252
                                   " aborting failover." % dev.iv_name)
4253

    
4254
    feedback_fn("* shutting down instance on source node")
4255
    logging.info("Shutting down instance %s on node %s",
4256
                 instance.name, source_node)
4257

    
4258
    result = self.rpc.call_instance_shutdown(source_node, instance)
4259
    msg = result.fail_msg
4260
    if msg:
4261
      if self.op.ignore_consistency:
4262
        self.proc.LogWarning("Could not shutdown instance %s on node %s."
4263
                             " Proceeding anyway. Please make sure node"
4264
                             " %s is down. Error details: %s",
4265
                             instance.name, source_node, source_node, msg)
4266
      else:
4267
        raise errors.OpExecError("Could not shutdown instance %s on"
4268
                                 " node %s: %s" %
4269
                                 (instance.name, source_node, msg))
4270

    
4271
    feedback_fn("* deactivating the instance's disks on source node")
4272
    if not _ShutdownInstanceDisks(self, instance, ignore_primary=True):
4273
      raise errors.OpExecError("Can't shut down the instance's disks.")
4274

    
4275
    instance.primary_node = target_node
4276
    # distribute new instance config to the other nodes
4277
    self.cfg.Update(instance)
4278

    
4279
    # Only start the instance if it's marked as up
4280
    if instance.admin_up:
4281
      feedback_fn("* activating the instance's disks on target node")
4282
      logging.info("Starting instance %s on node %s",
4283
                   instance.name, target_node)
4284

    
4285
      disks_ok, _ = _AssembleInstanceDisks(self, instance,
4286
                                               ignore_secondaries=True)
4287
      if not disks_ok:
4288
        _ShutdownInstanceDisks(self, instance)
4289
        raise errors.OpExecError("Can't activate the instance's disks")
4290

    
4291
      feedback_fn("* starting the instance on the target node")
4292
      result = self.rpc.call_instance_start(target_node, instance, None, None)
4293
      msg = result.fail_msg
4294
      if msg:
4295
        _ShutdownInstanceDisks(self, instance)
4296
        raise errors.OpExecError("Could not start instance %s on node %s: %s" %
4297
                                 (instance.name, target_node, msg))
4298

    
4299

    
4300
class LUMigrateInstance(LogicalUnit):
4301
  """Migrate an instance.
4302

4303
  This is migration without shutting down, compared to the failover,
4304
  which is done with shutdown.
4305

4306
  """
4307
  HPATH = "instance-migrate"
4308
  HTYPE = constants.HTYPE_INSTANCE
4309
  _OP_REQP = ["instance_name", "live", "cleanup"]
4310

    
4311
  REQ_BGL = False
4312

    
4313
  def ExpandNames(self):
4314
    self._ExpandAndLockInstance()
4315

    
4316
    self.needed_locks[locking.LEVEL_NODE] = []
4317
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
4318

    
4319
    self._migrater = TLMigrateInstance(self, self.op.instance_name,
4320
                                       self.op.live, self.op.cleanup)
4321
    self.tasklets = [self._migrater]
4322

    
4323
  def DeclareLocks(self, level):
4324
    if level == locking.LEVEL_NODE:
4325
      self._LockInstancesNodes()
4326

    
4327
  def BuildHooksEnv(self):
4328
    """Build hooks env.
4329

4330
    This runs on master, primary and secondary nodes of the instance.
4331

4332
    """
4333
    instance = self._migrater.instance
4334
    env = _BuildInstanceHookEnvByObject(self, instance)
4335
    env["MIGRATE_LIVE"] = self.op.live
4336
    env["MIGRATE_CLEANUP"] = self.op.cleanup
4337
    nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes)
4338
    return env, nl, nl
4339

    
4340

    
4341
class LUMigrateNode(LogicalUnit):
4342
  """Migrate all instances from a node.
4343

4344
  """
4345
  HPATH = "node-migrate"
4346
  HTYPE = constants.HTYPE_NODE
4347
  _OP_REQP = ["node_name", "live"]
4348
  REQ_BGL = False
4349

    
4350
  def ExpandNames(self):
4351
    self.op.node_name = self.cfg.ExpandNodeName(self.op.node_name)
4352
    if self.op.node_name is None:
4353
      raise errors.OpPrereqError("Node '%s' not known" % self.op.node_name)
4354

    
4355
    self.needed_locks = {
4356
      locking.LEVEL_NODE: [self.op.node_name],
4357
      }
4358

    
4359
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
4360

    
4361
    # Create tasklets for migrating instances for all instances on this node
4362
    names = []
4363
    tasklets = []
4364

    
4365
    for inst in _GetNodePrimaryInstances(self.cfg, self.op.node_name):
4366
      logging.debug("Migrating instance %s", inst.name)
4367
      names.append(inst.name)
4368

    
4369
      tasklets.append(TLMigrateInstance(self, inst.name, self.op.live, False))
4370

    
4371
    self.tasklets = tasklets
4372

    
4373
    # Declare instance locks
4374
    self.needed_locks[locking.LEVEL_INSTANCE] = names
4375

    
4376
  def DeclareLocks(self, level):
4377
    if level == locking.LEVEL_NODE:
4378
      self._LockInstancesNodes()
4379

    
4380
  def BuildHooksEnv(self):
4381
    """Build hooks env.
4382

4383
    This runs on the master, the primary and all the secondaries.
4384

4385
    """
4386
    env = {
4387
      "NODE_NAME": self.op.node_name,
4388
      }
4389

    
4390
    nl = [self.cfg.GetMasterNode()]
4391

    
4392
    return (env, nl, nl)
4393

    
4394

    
4395
class TLMigrateInstance(Tasklet):
4396
  def __init__(self, lu, instance_name, live, cleanup):
4397
    """Initializes this class.
4398

4399
    """
4400
    Tasklet.__init__(self, lu)
4401

    
4402
    # Parameters
4403
    self.instance_name = instance_name
4404
    self.live = live
4405
    self.cleanup = cleanup
4406

    
4407
  def CheckPrereq(self):
4408
    """Check prerequisites.
4409

4410
    This checks that the instance is in the cluster.
4411

4412
    """
4413
    instance = self.cfg.GetInstanceInfo(
4414
      self.cfg.ExpandInstanceName(self.instance_name))
4415
    if instance is None:
4416
      raise errors.OpPrereqError("Instance '%s' not known" %
4417
                                 self.instance_name)
4418

    
4419
    if instance.disk_template != constants.DT_DRBD8:
4420
      raise errors.OpPrereqError("Instance's disk layout is not"
4421
                                 " drbd8, cannot migrate.")
4422

    
4423
    secondary_nodes = instance.secondary_nodes
4424
    if not secondary_nodes:
4425
      raise errors.ConfigurationError("No secondary node but using"
4426
                                      " drbd8 disk template")
4427

    
4428
    i_be = self.cfg.GetClusterInfo().FillBE(instance)
4429

    
4430
    target_node = secondary_nodes[0]
4431
    # check memory requirements on the secondary node
4432
    _CheckNodeFreeMemory(self, target_node, "migrating instance %s" %
4433
                         instance.name, i_be[constants.BE_MEMORY],
4434
                         instance.hypervisor)
4435

    
4436
    # check bridge existance
4437
    _CheckInstanceBridgesExist(self, instance, node=target_node)
4438

    
4439
    if not self.cleanup:
4440
      _CheckNodeNotDrained(self, target_node)
4441
      result = self.rpc.call_instance_migratable(instance.primary_node,
4442
                                                 instance)
4443
      result.Raise("Can't migrate, please use failover", prereq=True)
4444

    
4445
    self.instance = instance
4446

    
4447
  def _WaitUntilSync(self):
4448
    """Poll with custom rpc for disk sync.
4449

4450
    This uses our own step-based rpc call.
4451

4452
    """
4453
    self.feedback_fn("* wait until resync is done")
4454
    all_done = False
4455
    while not all_done:
4456
      all_done = True
4457
      result = self.rpc.call_drbd_wait_sync(self.all_nodes,
4458
                                            self.nodes_ip,
4459
                                            self.instance.disks)
4460
      min_percent = 100
4461
      for node, nres in result.items():
4462
        nres.Raise("Cannot resync disks on node %s" % node)
4463
        node_done, node_percent = nres.payload
4464
        all_done = all_done and node_done
4465
        if node_percent is not None:
4466
          min_percent = min(min_percent, node_percent)
4467
      if not all_done:
4468
        if min_percent < 100:
4469
          self.feedback_fn("   - progress: %.1f%%" % min_percent)
4470
        time.sleep(2)
4471

    
4472
  def _EnsureSecondary(self, node):
4473
    """Demote a node to secondary.
4474

4475
    """
4476
    self.feedback_fn("* switching node %s to secondary mode" % node)
4477

    
4478
    for dev in self.instance.disks:
4479
      self.cfg.SetDiskID(dev, node)
4480

    
4481
    result = self.rpc.call_blockdev_close(node, self.instance.name,
4482
                                          self.instance.disks)
4483
    result.Raise("Cannot change disk to secondary on node %s" % node)
4484

    
4485
  def _GoStandalone(self):
4486
    """Disconnect from the network.
4487

4488
    """
4489
    self.feedback_fn("* changing into standalone mode")
4490
    result = self.rpc.call_drbd_disconnect_net(self.all_nodes, self.nodes_ip,
4491
                                               self.instance.disks)
4492
    for node, nres in result.items():
4493
      nres.Raise("Cannot disconnect disks node %s" % node)
4494

    
4495
  def _GoReconnect(self, multimaster):
4496
    """Reconnect to the network.
4497

4498
    """
4499
    if multimaster:
4500
      msg = "dual-master"
4501
    else:
4502
      msg = "single-master"
4503
    self.feedback_fn("* changing disks into %s mode" % msg)
4504
    result = self.rpc.call_drbd_attach_net(self.all_nodes, self.nodes_ip,
4505
                                           self.instance.disks,
4506
                                           self.instance.name, multimaster)
4507
    for node, nres in result.items():
4508
      nres.Raise("Cannot change disks config on node %s" % node)
4509

    
4510
  def _ExecCleanup(self):
4511
    """Try to cleanup after a failed migration.
4512

4513
    The cleanup is done by:
4514
      - check that the instance is running only on one node
4515
        (and update the config if needed)
4516
      - change disks on its secondary node to secondary
4517
      - wait until disks are fully synchronized
4518
      - disconnect from the network
4519
      - change disks into single-master mode
4520
      - wait again until disks are fully synchronized
4521

4522
    """
4523
    instance = self.instance
4524
    target_node = self.target_node
4525
    source_node = self.source_node
4526

    
4527
    # check running on only one node
4528
    self.feedback_fn("* checking where the instance actually runs"
4529
                     " (if this hangs, the hypervisor might be in"
4530
                     " a bad state)")
4531
    ins_l = self.rpc.call_instance_list(self.all_nodes, [instance.hypervisor])
4532
    for node, result in ins_l.items():
4533
      result.Raise("Can't contact node %s" % node)
4534

    
4535
    runningon_source = instance.name in ins_l[source_node].payload
4536
    runningon_target = instance.name in ins_l[target_node].payload
4537

    
4538
    if runningon_source and runningon_target:
4539
      raise errors.OpExecError("Instance seems to be running on two nodes,"
4540
                               " or the hypervisor is confused. You will have"
4541
                               " to ensure manually that it runs only on one"
4542
                               " and restart this operation.")
4543

    
4544
    if not (runningon_source or runningon_target):
4545
      raise errors.OpExecError("Instance does not seem to be running at all."
4546
                               " In this case, it's safer to repair by"
4547
                               " running 'gnt-instance stop' to ensure disk"
4548
                               " shutdown, and then restarting it.")
4549

    
4550
    if runningon_target:
4551
      # the migration has actually succeeded, we need to update the config
4552
      self.feedback_fn("* instance running on secondary node (%s),"
4553
                       " updating config" % target_node)
4554
      instance.primary_node = target_node
4555
      self.cfg.Update(instance)
4556
      demoted_node = source_node
4557
    else:
4558
      self.feedback_fn("* instance confirmed to be running on its"
4559
                       " primary node (%s)" % source_node)
4560
      demoted_node = target_node
4561

    
4562
    self._EnsureSecondary(demoted_node)
4563
    try:
4564
      self._WaitUntilSync()
4565
    except errors.OpExecError:
4566
      # we ignore here errors, since if the device is standalone, it
4567
      # won't be able to sync
4568
      pass
4569
    self._GoStandalone()
4570
    self._GoReconnect(False)
4571
    self._WaitUntilSync()
4572

    
4573
    self.feedback_fn("* done")
4574

    
4575
  def _RevertDiskStatus(self):
4576
    """Try to revert the disk status after a failed migration.
4577

4578
    """
4579
    target_node = self.target_node
4580
    try:
4581
      self._EnsureSecondary(target_node)
4582
      self._GoStandalone()
4583
      self._GoReconnect(False)
4584
      self._WaitUntilSync()
4585
    except errors.OpExecError, err:
4586
      self.lu.LogWarning("Migration failed and I can't reconnect the"
4587
                         " drives: error '%s'\n"
4588
                         "Please look and recover the instance status" %
4589
                         str(err))
4590

    
4591
  def _AbortMigration(self):
4592
    """Call the hypervisor code to abort a started migration.
4593

4594
    """
4595
    instance = self.instance
4596
    target_node = self.target_node
4597
    migration_info = self.migration_info
4598

    
4599
    abort_result = self.rpc.call_finalize_migration(target_node,
4600
                                                    instance,
4601
                                                    migration_info,
4602
                                                    False)
4603
    abort_msg = abort_result.fail_msg
4604
    if abort_msg:
4605
      logging.error("Aborting migration failed on target node %s: %s" %
4606
                    (target_node, abort_msg))
4607
      # Don't raise an exception here, as we stil have to try to revert the
4608
      # disk status, even if this step failed.
4609

    
4610
  def _ExecMigration(self):
4611
    """Migrate an instance.
4612

4613
    The migrate is done by:
4614
      - change the disks into dual-master mode
4615
      - wait until disks are fully synchronized again
4616
      - migrate the instance
4617
      - change disks on the new secondary node (the old primary) to secondary
4618
      - wait until disks are fully synchronized
4619
      - change disks into single-master mode
4620

4621
    """
4622
    instance = self.instance
4623
    target_node = self.target_node
4624
    source_node = self.source_node
4625

    
4626
    self.feedback_fn("* checking disk consistency between source and target")
4627
    for dev in instance.disks:
4628
      if not _CheckDiskConsistency(self, dev, target_node, False):
4629
        raise errors.OpExecError("Disk %s is degraded or not fully"
4630
                                 " synchronized on target node,"
4631
                                 " aborting migrate." % dev.iv_name)
4632

    
4633
    # First get the migration information from the remote node
4634
    result = self.rpc.call_migration_info(source_node, instance)
4635
    msg = result.fail_msg
4636
    if msg:
4637
      log_err = ("Failed fetching source migration information from %s: %s" %
4638
                 (source_node, msg))
4639
      logging.error(log_err)
4640
      raise errors.OpExecError(log_err)
4641

    
4642
    self.migration_info = migration_info = result.payload
4643

    
4644
    # Then switch the disks to master/master mode
4645
    self._EnsureSecondary(target_node)
4646
    self._GoStandalone()
4647
    self._GoReconnect(True)
4648
    self._WaitUntilSync()
4649

    
4650
    self.feedback_fn("* preparing %s to accept the instance" % target_node)
4651
    result = self.rpc.call_accept_instance(target_node,
4652
                                           instance,
4653
                                           migration_info,
4654
                                           self.nodes_ip[target_node])
4655

    
4656
    msg = result.fail_msg
4657
    if msg:
4658
      logging.error("Instance pre-migration failed, trying to revert"
4659
                    " disk status: %s", msg)
4660
      self._AbortMigration()
4661
      self._RevertDiskStatus()
4662
      raise errors.OpExecError("Could not pre-migrate instance %s: %s" %
4663
                               (instance.name, msg))
4664

    
4665
    self.feedback_fn("* migrating instance to %s" % target_node)
4666
    time.sleep(10)
4667
    result = self.rpc.call_instance_migrate(source_node, instance,
4668
                                            self.nodes_ip[target_node],
4669
                                            self.live)
4670
    msg = result.fail_msg
4671
    if msg:
4672
      logging.error("Instance migration failed, trying to revert"
4673
                    " disk status: %s", msg)
4674
      self._AbortMigration()
4675
      self._RevertDiskStatus()
4676
      raise errors.OpExecError("Could not migrate instance %s: %s" %
4677
                               (instance.name, msg))
4678
    time.sleep(10)
4679

    
4680
    instance.primary_node = target_node
4681
    # distribute new instance config to the other nodes
4682
    self.cfg.Update(instance)
4683

    
4684
    result = self.rpc.call_finalize_migration(target_node,
4685
                                              instance,
4686
                                              migration_info,
4687
                                              True)
4688
    msg = result.fail_msg
4689
    if msg:
4690
      logging.error("Instance migration succeeded, but finalization failed:"
4691
                    " %s" % msg)
4692
      raise errors.OpExecError("Could not finalize instance migration: %s" %
4693
                               msg)
4694

    
4695
    self._EnsureSecondary(source_node)
4696
    self._WaitUntilSync()
4697
    self._GoStandalone()
4698
    self._GoReconnect(False)
4699
    self._WaitUntilSync()
4700

    
4701
    self.feedback_fn("* done")
4702

    
4703
  def Exec(self, feedback_fn):
4704
    """Perform the migration.
4705

4706
    """
4707
    feedback_fn("Migrating instance %s" % self.instance.name)
4708

    
4709
    self.feedback_fn = feedback_fn
4710

    
4711
    self.source_node = self.instance.primary_node
4712
    self.target_node = self.instance.secondary_nodes[0]
4713
    self.all_nodes = [self.source_node, self.target_node]
4714
    self.nodes_ip = {
4715
      self.source_node: self.cfg.GetNodeInfo(self.source_node).secondary_ip,
4716
      self.target_node: self.cfg.GetNodeInfo(self.target_node).secondary_ip,
4717
      }
4718

    
4719
    if self.cleanup:
4720
      return self._ExecCleanup()
4721
    else:
4722
      return self._ExecMigration()
4723

    
4724

    
4725
def _CreateBlockDev(lu, node, instance, device, force_create,
4726
                    info, force_open):
4727
  """Create a tree of block devices on a given node.
4728

4729
  If this device type has to be created on secondaries, create it and
4730
  all its children.
4731

4732
  If not, just recurse to children keeping the same 'force' value.
4733

4734
  @param lu: the lu on whose behalf we execute
4735
  @param node: the node on which to create the device
4736
  @type instance: L{objects.Instance}
4737
  @param instance: the instance which owns the device
4738
  @type device: L{objects.Disk}
4739
  @param device: the device to create
4740
  @type force_create: boolean
4741
  @param force_create: whether to force creation of this device; this
4742
      will be change to True whenever we find a device which has
4743
      CreateOnSecondary() attribute
4744
  @param info: the extra 'metadata' we should attach to the device
4745
      (this will be represented as a LVM tag)
4746
  @type force_open: boolean
4747
  @param force_open: this parameter will be passes to the
4748
      L{backend.BlockdevCreate} function where it specifies
4749
      whether we run on primary or not, and it affects both
4750
      the child assembly and the device own Open() execution
4751

4752
  """
4753
  if device.CreateOnSecondary():
4754
    force_create = True
4755

    
4756
  if device.children:
4757
    for child in device.children:
4758
      _CreateBlockDev(lu, node, instance, child, force_create,
4759
                      info, force_open)
4760

    
4761
  if not force_create:
4762
    return
4763

    
4764
  _CreateSingleBlockDev(lu, node, instance, device, info, force_open)
4765

    
4766

    
4767
def _CreateSingleBlockDev(lu, node, instance, device, info, force_open):
4768
  """Create a single block device on a given node.
4769

4770
  This will not recurse over children of the device, so they must be
4771
  created in advance.
4772

4773
  @param lu: the lu on whose behalf we execute
4774
  @param node: the node on which to create the device
4775
  @type instance: L{objects.Instance}
4776
  @param instance: the instance which owns the device
4777
  @type device: L{objects.Disk}
4778
  @param device: the device to create
4779
  @param info: the extra 'metadata' we should attach to the device
4780
      (this will be represented as a LVM tag)
4781
  @type force_open: boolean
4782
  @param force_open: this parameter will be passes to the
4783
      L{backend.BlockdevCreate} function where it specifies
4784
      whether we run on primary or not, and it affects both
4785
      the child assembly and the device own Open() execution
4786

4787
  """
4788
  lu.cfg.SetDiskID(device, node)
4789
  result = lu.rpc.call_blockdev_create(node, device, device.size,
4790
                                       instance.name, force_open, info)
4791
  result.Raise("Can't create block device %s on"
4792
               " node %s for instance %s" % (device, node, instance.name))
4793
  if device.physical_id is None:
4794
    device.physical_id = result.payload
4795

    
4796

    
4797
def _GenerateUniqueNames(lu, exts):
4798
  """Generate a suitable LV name.
4799

4800
  This will generate a logical volume name for the given instance.
4801

4802
  """
4803
  results = []
4804
  for val in exts:
4805
    new_id = lu.cfg.GenerateUniqueID()
4806
    results.append("%s%s" % (new_id, val))
4807
  return results
4808

    
4809

    
4810
def _GenerateDRBD8Branch(lu, primary, secondary, size, names, iv_name,
4811
                         p_minor, s_minor):
4812
  """Generate a drbd8 device complete with its children.
4813

4814
  """
4815
  port = lu.cfg.AllocatePort()
4816
  vgname = lu.cfg.GetVGName()
4817
  shared_secret = lu.cfg.GenerateDRBDSecret()
4818
  dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
4819
                          logical_id=(vgname, names[0]))
4820
  dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
4821
                          logical_id=(vgname, names[1]))
4822
  drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size,
4823
                          logical_id=(primary, secondary, port,
4824
                                      p_minor, s_minor,
4825
                                      shared_secret),
4826
                          children=[dev_data, dev_meta],
4827
                          iv_name=iv_name)
4828
  return drbd_dev
4829

    
4830

    
4831
def _GenerateDiskTemplate(lu, template_name,
4832
                          instance_name, primary_node,
4833
                          secondary_nodes, disk_info,
4834
                          file_storage_dir, file_driver,
4835
                          base_index):
4836
  """Generate the entire disk layout for a given template type.
4837

4838
  """
4839
  #TODO: compute space requirements
4840

    
4841
  vgname = lu.cfg.GetVGName()
4842
  disk_count = len(disk_info)
4843
  disks = []
4844
  if template_name == constants.DT_DISKLESS:
4845
    pass
4846
  elif template_name == constants.DT_PLAIN:
4847
    if len(secondary_nodes) != 0:
4848
      raise errors.ProgrammerError("Wrong template configuration")
4849

    
4850
    names = _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
4851
                                      for i in range(disk_count)])
4852
    for idx, disk in enumerate(disk_info):
4853
      disk_index = idx + base_index
4854
      disk_dev = objects.Disk(dev_type=constants.LD_LV, size=disk["size"],
4855
                              logical_id=(vgname, names[idx]),
4856
                              iv_name="disk/%d" % disk_index,
4857
                              mode=disk["mode"])
4858
      disks.append(disk_dev)
4859
  elif template_name == constants.DT_DRBD8:
4860
    if len(secondary_nodes) != 1:
4861
      raise errors.ProgrammerError("Wrong template configuration")
4862
    remote_node = secondary_nodes[0]
4863
    minors = lu.cfg.AllocateDRBDMinor(
4864
      [primary_node, remote_node] * len(disk_info), instance_name)
4865

    
4866
    names = []
4867
    for lv_prefix in _GenerateUniqueNames(lu, [".disk%d" % (base_index + i)
4868
                                               for i in range(disk_count)]):
4869
      names.append(lv_prefix + "_data")
4870
      names.append(lv_prefix + "_meta")
4871
    for idx, disk in enumerate(disk_info):
4872
      disk_index = idx + base_index
4873
      disk_dev = _GenerateDRBD8Branch(lu, primary_node, remote_node,
4874
                                      disk["size"], names[idx*2:idx*2+2],
4875
                                      "disk/%d" % disk_index,
4876
                                      minors[idx*2], minors[idx*2+1])
4877
      disk_dev.mode = disk["mode"]
4878
      disks.append(disk_dev)
4879
  elif template_name == constants.DT_FILE:
4880
    if len(secondary_nodes) != 0:
4881
      raise errors.ProgrammerError("Wrong template configuration")
4882

    
4883
    for idx, disk in enumerate(disk_info):
4884
      disk_index = idx + base_index
4885
      disk_dev = objects.Disk(dev_type=constants.LD_FILE, size=disk["size"],
4886
                              iv_name="disk/%d" % disk_index,
4887
                              logical_id=(file_driver,
4888
                                          "%s/disk%d" % (file_storage_dir,
4889
                                                         disk_index)),
4890
                              mode=disk["mode"])
4891
      disks.append(disk_dev)
4892
  else:
4893
    raise errors.ProgrammerError("Invalid disk template '%s'" % template_name)
4894
  return disks
4895

    
4896

    
4897
def _GetInstanceInfoText(instance):
4898
  """Compute that text that should be added to the disk's metadata.
4899

4900
  """
4901
  return "originstname+%s" % instance.name
4902

    
4903

    
4904
def _CreateDisks(lu, instance, to_skip=None):
4905
  """Create all disks for an instance.
4906

4907
  This abstracts away some work from AddInstance.
4908

4909
  @type lu: L{LogicalUnit}
4910
  @param lu: the logical unit on whose behalf we execute
4911
  @type instance: L{objects.Instance}
4912
  @param instance: the instance whose disks we should create
4913
  @type to_skip: list
4914
  @param to_skip: list of indices to skip
4915
  @rtype: boolean
4916
  @return: the success of the creation
4917

4918
  """
4919
  info = _GetInstanceInfoText(instance)
4920
  pnode = instance.primary_node
4921

    
4922
  if instance.disk_template == constants.DT_FILE:
4923
    file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
4924
    result = lu.rpc.call_file_storage_dir_create(pnode, file_storage_dir)
4925

    
4926
    result.Raise("Failed to create directory '%s' on"
4927
                 " node %s: %s" % (file_storage_dir, pnode))
4928

    
4929
  # Note: this needs to be kept in sync with adding of disks in
4930
  # LUSetInstanceParams
4931
  for idx, device in enumerate(instance.disks):
4932
    if to_skip and idx in to_skip:
4933
      continue
4934
    logging.info("Creating volume %s for instance %s",
4935
                 device.iv_name, instance.name)
4936
    #HARDCODE
4937
    for node in instance.all_nodes:
4938
      f_create = node == pnode
4939
      _CreateBlockDev(lu, node, instance, device, f_create, info, f_create)
4940

    
4941

    
4942
def _RemoveDisks(lu, instance):
4943
  """Remove all disks for an instance.
4944

4945
  This abstracts away some work from `AddInstance()` and
4946
  `RemoveInstance()`. Note that in case some of the devices couldn't
4947
  be removed, the removal will continue with the other ones (compare
4948
  with `_CreateDisks()`).
4949

4950
  @type lu: L{LogicalUnit}
4951
  @param lu: the logical unit on whose behalf we execute
4952
  @type instance: L{objects.Instance}
4953
  @param instance: the instance whose disks we should remove
4954
  @rtype: boolean
4955
  @return: the success of the removal
4956

4957
  """
4958
  logging.info("Removing block devices for instance %s", instance.name)
4959

    
4960
  all_result = True
4961
  for device in instance.disks:
4962
    for node, disk in device.ComputeNodeTree(instance.primary_node):
4963
      lu.cfg.SetDiskID(disk, node)
4964
      msg = lu.rpc.call_blockdev_remove(node, disk).fail_msg
4965
      if msg:
4966
        lu.LogWarning("Could not remove block device %s on node %s,"
4967
                      " continuing anyway: %s", device.iv_name, node, msg)
4968
        all_result = False
4969

    
4970
  if instance.disk_template == constants.DT_FILE:
4971
    file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
4972
    result = lu.rpc.call_file_storage_dir_remove(instance.primary_node,
4973
                                                 file_storage_dir)
4974
    msg = result.fail_msg
4975
    if msg:
4976
      lu.LogWarning("Could not remove directory '%s' on node %s: %s",
4977
                    file_storage_dir, instance.primary_node, msg)
4978
      all_result = False
4979

    
4980
  return all_result
4981

    
4982

    
4983
def _ComputeDiskSize(disk_template, disks):
4984
  """Compute disk size requirements in the volume group
4985

4986
  """
4987
  # Required free disk space as a function of disk and swap space
4988
  req_size_dict = {
4989
    constants.DT_DISKLESS: None,
4990
    constants.DT_PLAIN: sum(d["size"] for d in disks),
4991
    # 128 MB are added for drbd metadata for each disk
4992
    constants.DT_DRBD8: sum(d["size"] + 128 for d in disks),
4993
    constants.DT_FILE: None,
4994
  }
4995

    
4996
  if disk_template not in req_size_dict:
4997
    raise errors.ProgrammerError("Disk template '%s' size requirement"
4998
                                 " is unknown" %  disk_template)
4999

    
5000
  return req_size_dict[disk_template]
5001

    
5002

    
5003
def _CheckHVParams(lu, nodenames, hvname, hvparams):
5004
  """Hypervisor parameter validation.
5005

5006
  This function abstract the hypervisor parameter validation to be
5007
  used in both instance create and instance modify.
5008

5009
  @type lu: L{LogicalUnit}
5010
  @param lu: the logical unit for which we check
5011
  @type nodenames: list
5012
  @param nodenames: the list of nodes on which we should check
5013
  @type hvname: string
5014
  @param hvname: the name of the hypervisor we should use
5015
  @type hvparams: dict
5016
  @param hvparams: the parameters which we need to check
5017
  @raise errors.OpPrereqError: if the parameters are not valid
5018

5019
  """
5020
  hvinfo = lu.rpc.call_hypervisor_validate_params(nodenames,
5021
                                                  hvname,
5022
                                                  hvparams)
5023
  for node in nodenames:
5024
    info = hvinfo[node]
5025
    if info.offline:
5026
      continue
5027
    info.Raise("Hypervisor parameter validation failed on node %s" % node)
5028

    
5029

    
5030
class LUCreateInstance(LogicalUnit):
5031
  """Create an instance.
5032

5033
  """
5034
  HPATH = "instance-add"
5035
  HTYPE = constants.HTYPE_INSTANCE
5036
  _OP_REQP = ["instance_name", "disks", "disk_template",
5037
              "mode", "start",
5038
              "wait_for_sync", "ip_check", "nics",
5039
              "hvparams", "beparams"]
5040
  REQ_BGL = False
5041

    
5042
  def _ExpandNode(self, node):
5043
    """Expands and checks one node name.
5044

5045
    """
5046
    node_full = self.cfg.ExpandNodeName(node)
5047
    if node_full is None:
5048
      raise errors.OpPrereqError("Unknown node %s" % node)
5049
    return node_full
5050

    
5051
  def ExpandNames(self):
5052
    """ExpandNames for CreateInstance.
5053

5054
    Figure out the right locks for instance creation.
5055

5056
    """
5057
    self.needed_locks = {}
5058

    
5059
    # set optional parameters to none if they don't exist
5060
    for attr in ["pnode", "snode", "iallocator", "hypervisor"]:
5061
      if not hasattr(self.op, attr):
5062
        setattr(self.op, attr, None)
5063

    
5064
    # cheap checks, mostly valid constants given
5065

    
5066
    # verify creation mode
5067
    if self.op.mode not in (constants.INSTANCE_CREATE,
5068
                            constants.INSTANCE_IMPORT):
5069
      raise errors.OpPrereqError("Invalid instance creation mode '%s'" %
5070
                                 self.op.mode)
5071

    
5072
    # disk template and mirror node verification
5073
    if self.op.disk_template not in constants.DISK_TEMPLATES:
5074
      raise errors.OpPrereqError("Invalid disk template name")
5075

    
5076
    if self.op.hypervisor is None:
5077
      self.op.hypervisor = self.cfg.GetHypervisorType()
5078

    
5079
    cluster = self.cfg.GetClusterInfo()
5080
    enabled_hvs = cluster.enabled_hypervisors
5081
    if self.op.hypervisor not in enabled_hvs:
5082
      raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
5083
                                 " cluster (%s)" % (self.op.hypervisor,
5084
                                  ",".join(enabled_hvs)))
5085

    
5086
    # check hypervisor parameter syntax (locally)
5087
    utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
5088
    filled_hvp = objects.FillDict(cluster.hvparams[self.op.hypervisor],
5089
                                  self.op.hvparams)
5090
    hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
5091
    hv_type.CheckParameterSyntax(filled_hvp)
5092
    self.hv_full = filled_hvp
5093

    
5094
    # fill and remember the beparams dict
5095
    utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
5096
    self.be_full = objects.FillDict(cluster.beparams[constants.PP_DEFAULT],
5097
                                    self.op.beparams)
5098

    
5099
    #### instance parameters check
5100

    
5101
    # instance name verification
5102
    hostname1 = utils.HostInfo(self.op.instance_name)
5103
    self.op.instance_name = instance_name = hostname1.name
5104

    
5105
    # this is just a preventive check, but someone might still add this
5106
    # instance in the meantime, and creation will fail at lock-add time
5107
    if instance_name in self.cfg.GetInstanceList():
5108
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
5109
                                 instance_name)
5110

    
5111
    self.add_locks[locking.LEVEL_INSTANCE] = instance_name
5112

    
5113
    # NIC buildup
5114
    self.nics = []
5115
    for idx, nic in enumerate(self.op.nics):
5116
      nic_mode_req = nic.get("mode", None)
5117
      nic_mode = nic_mode_req
5118
      if nic_mode is None:
5119
        nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE]
5120

    
5121
      # in routed mode, for the first nic, the default ip is 'auto'
5122
      if nic_mode == constants.NIC_MODE_ROUTED and idx == 0:
5123
        default_ip_mode = constants.VALUE_AUTO
5124
      else:
5125
        default_ip_mode = constants.VALUE_NONE
5126

    
5127
      # ip validity checks
5128
      ip = nic.get("ip", default_ip_mode)
5129
      if ip is None or ip.lower() == constants.VALUE_NONE:
5130
        nic_ip = None
5131
      elif ip.lower() == constants.VALUE_AUTO:
5132
        nic_ip = hostname1.ip
5133
      else:
5134
        if not utils.IsValidIP(ip):
5135
          raise errors.OpPrereqError("Given IP address '%s' doesn't look"
5136
                                     " like a valid IP" % ip)
5137
        nic_ip = ip
5138

    
5139
      # TODO: check the ip for uniqueness !!
5140
      if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip:
5141
        raise errors.OpPrereqError("Routed nic mode requires an ip address")
5142

    
5143
      # MAC address verification
5144
      mac = nic.get("mac", constants.VALUE_AUTO)
5145
      if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
5146
        if not utils.IsValidMac(mac.lower()):
5147
          raise errors.OpPrereqError("Invalid MAC address specified: %s" %
5148
                                     mac)
5149
      # bridge verification
5150
      bridge = nic.get("bridge", None)
5151
      link = nic.get("link", None)
5152
      if bridge and link:
5153
        raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'"
5154
                                   " at the same time")
5155
      elif bridge and nic_mode == constants.NIC_MODE_ROUTED:
5156
        raise errors.OpPrereqError("Cannot pass 'bridge' on a routed nic")
5157
      elif bridge:
5158
        link = bridge
5159

    
5160
      nicparams = {}
5161
      if nic_mode_req:
5162
        nicparams[constants.NIC_MODE] = nic_mode_req
5163
      if link:
5164
        nicparams[constants.NIC_LINK] = link
5165

    
5166
      check_params = objects.FillDict(cluster.nicparams[constants.PP_DEFAULT],
5167
                                      nicparams)
5168
      objects.NIC.CheckParameterSyntax(check_params)
5169
      self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
5170

    
5171
    # disk checks/pre-build
5172
    self.disks = []
5173
    for disk in self.op.disks:
5174
      mode = disk.get("mode", constants.DISK_RDWR)
5175
      if mode not in constants.DISK_ACCESS_SET:
5176
        raise errors.OpPrereqError("Invalid disk access mode '%s'" %
5177
                                   mode)
5178
      size = disk.get("size", None)
5179
      if size is None:
5180
        raise errors.OpPrereqError("Missing disk size")
5181
      try:
5182
        size = int(size)
5183
      except ValueError:
5184
        raise errors.OpPrereqError("Invalid disk size '%s'" % size)
5185
      self.disks.append({"size": size, "mode": mode})
5186

    
5187
    # used in CheckPrereq for ip ping check
5188
    self.check_ip = hostname1.ip
5189

    
5190
    # file storage checks
5191
    if (self.op.file_driver and
5192
        not self.op.file_driver in constants.FILE_DRIVER):
5193
      raise errors.OpPrereqError("Invalid file driver name '%s'" %
5194
                                 self.op.file_driver)
5195

    
5196
    if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir):
5197
      raise errors.OpPrereqError("File storage directory path not absolute")
5198

    
5199
    ### Node/iallocator related checks
5200
    if [self.op.iallocator, self.op.pnode].count(None) != 1:
5201
      raise errors.OpPrereqError("One and only one of iallocator and primary"
5202
                                 " node must be given")
5203

    
5204
    if self.op.iallocator:
5205
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
5206
    else:
5207
      self.op.pnode = self._ExpandNode(self.op.pnode)
5208
      nodelist = [self.op.pnode]
5209
      if self.op.snode is not None:
5210
        self.op.snode = self._ExpandNode(self.op.snode)
5211
        nodelist.append(self.op.snode)
5212
      self.needed_locks[locking.LEVEL_NODE] = nodelist
5213

    
5214
    # in case of import lock the source node too
5215
    if self.op.mode == constants.INSTANCE_IMPORT:
5216
      src_node = getattr(self.op, "src_node", None)
5217
      src_path = getattr(self.op, "src_path", None)
5218

    
5219
      if src_path is None:
5220
        self.op.src_path = src_path = self.op.instance_name
5221

    
5222
      if src_node is None:
5223
        self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
5224
        self.op.src_node = None
5225
        if os.path.isabs(src_path):
5226
          raise errors.OpPrereqError("Importing an instance from an absolute"
5227
                                     " path requires a source node option.")
5228
      else:
5229
        self.op.src_node = src_node = self._ExpandNode(src_node)
5230
        if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
5231
          self.needed_locks[locking.LEVEL_NODE].append(src_node)
5232
        if not os.path.isabs(src_path):
5233
          self.op.src_path = src_path = \
5234
            os.path.join(constants.EXPORT_DIR, src_path)
5235

    
5236
    else: # INSTANCE_CREATE
5237
      if getattr(self.op, "os_type", None) is None:
5238
        raise errors.OpPrereqError("No guest OS specified")
5239

    
5240
  def _RunAllocator(self):
5241
    """Run the allocator based on input opcode.
5242

5243
    """
5244
    nics = [n.ToDict() for n in self.nics]
5245
    ial = IAllocator(self.cfg, self.rpc,
5246
                     mode=constants.IALLOCATOR_MODE_ALLOC,
5247
                     name=self.op.instance_name,
5248
                     disk_template=self.op.disk_template,
5249
                     tags=[],
5250
                     os=self.op.os_type,
5251
                     vcpus=self.be_full[constants.BE_VCPUS],
5252
                     mem_size=self.be_full[constants.BE_MEMORY],
5253
                     disks=self.disks,
5254
                     nics=nics,
5255
                     hypervisor=self.op.hypervisor,
5256
                     )
5257

    
5258
    ial.Run(self.op.iallocator)
5259

    
5260
    if not ial.success:
5261
      raise errors.OpPrereqError("Can't compute nodes using"
5262
                                 " iallocator '%s': %s" % (self.op.iallocator,
5263
                                                           ial.info))
5264
    if len(ial.nodes) != ial.required_nodes:
5265
      raise errors.OpPrereqError("iallocator '%s' returned invalid number"
5266
                                 " of nodes (%s), required %s" %
5267
                                 (self.op.iallocator, len(ial.nodes),
5268
                                  ial.required_nodes))
5269
    self.op.pnode = ial.nodes[0]
5270
    self.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
5271
                 self.op.instance_name, self.op.iallocator,
5272
                 ", ".join(ial.nodes))
5273
    if ial.required_nodes == 2:
5274
      self.op.snode = ial.nodes[1]
5275

    
5276
  def BuildHooksEnv(self):
5277
    """Build hooks env.
5278

5279
    This runs on master, primary and secondary nodes of the instance.
5280

5281
    """
5282
    env = {
5283
      "ADD_MODE": self.op.mode,
5284
      }
5285
    if self.op.mode == constants.INSTANCE_IMPORT:
5286
      env["SRC_NODE"] = self.op.src_node
5287
      env["SRC_PATH"] = self.op.src_path
5288
      env["SRC_IMAGES"] = self.src_images
5289

    
5290
    env.update(_BuildInstanceHookEnv(
5291
      name=self.op.instance_name,
5292
      primary_node=self.op.pnode,
5293
      secondary_nodes=self.secondaries,
5294
      status=self.op.start,
5295
      os_type=self.op.os_type,
5296
      memory=self.be_full[constants.BE_MEMORY],
5297
      vcpus=self.be_full[constants.BE_VCPUS],
5298
      nics=_NICListToTuple(self, self.nics),
5299
      disk_template=self.op.disk_template,
5300
      disks=[(d["size"], d["mode"]) for d in self.disks],
5301
      bep=self.be_full,
5302
      hvp=self.hv_full,
5303
      hypervisor_name=self.op.hypervisor,
5304
    ))
5305

    
5306
    nl = ([self.cfg.GetMasterNode(), self.op.pnode] +
5307
          self.secondaries)
5308
    return env, nl, nl
5309

    
5310

    
5311
  def CheckPrereq(self):
5312
    """Check prerequisites.
5313

5314
    """
5315
    if (not self.cfg.GetVGName() and
5316
        self.op.disk_template not in constants.DTS_NOT_LVM):
5317
      raise errors.OpPrereqError("Cluster does not support lvm-based"
5318
                                 " instances")
5319

    
5320
    if self.op.mode == constants.INSTANCE_IMPORT:
5321
      src_node = self.op.src_node
5322
      src_path = self.op.src_path
5323

    
5324
      if src_node is None:
5325
        locked_nodes = self.acquired_locks[locking.LEVEL_NODE]
5326
        exp_list = self.rpc.call_export_list(locked_nodes)
5327
        found = False
5328
        for node in exp_list:
5329
          if exp_list[node].fail_msg:
5330
            continue
5331
          if src_path in exp_list[node].payload:
5332
            found = True
5333
            self.op.src_node = src_node = node
5334
            self.op.src_path = src_path = os.path.join(constants.EXPORT_DIR,
5335
                                                       src_path)
5336
            break
5337
        if not found:
5338
          raise errors.OpPrereqError("No export found for relative path %s" %
5339
                                      src_path)
5340

    
5341
      _CheckNodeOnline(self, src_node)
5342
      result = self.rpc.call_export_info(src_node, src_path)
5343
      result.Raise("No export or invalid export found in dir %s" % src_path)
5344

    
5345
      export_info = objects.SerializableConfigParser.Loads(str(result.payload))
5346
      if not export_info.has_section(constants.INISECT_EXP):
5347
        raise errors.ProgrammerError("Corrupted export config")
5348

    
5349
      ei_version = export_info.get(constants.INISECT_EXP, 'version')
5350
      if (int(ei_version) != constants.EXPORT_VERSION):
5351
        raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
5352
                                   (ei_version, constants.EXPORT_VERSION))
5353

    
5354
      # Check that the new instance doesn't have less disks than the export
5355
      instance_disks = len(self.disks)
5356
      export_disks = export_info.getint(constants.INISECT_INS, 'disk_count')
5357
      if instance_disks < export_disks:
5358
        raise errors.OpPrereqError("Not enough disks to import."
5359
                                   " (instance: %d, export: %d)" %
5360
                                   (instance_disks, export_disks))
5361

    
5362
      self.op.os_type = export_info.get(constants.INISECT_EXP, 'os')
5363
      disk_images = []
5364
      for idx in range(export_disks):
5365
        option = 'disk%d_dump' % idx
5366
        if export_info.has_option(constants.INISECT_INS, option):
5367
          # FIXME: are the old os-es, disk sizes, etc. useful?
5368
          export_name = export_info.get(constants.INISECT_INS, option)
5369
          image = os.path.join(src_path, export_name)
5370
          disk_images.append(image)
5371
        else:
5372
          disk_images.append(False)
5373

    
5374
      self.src_images = disk_images
5375

    
5376
      old_name = export_info.get(constants.INISECT_INS, 'name')
5377
      # FIXME: int() here could throw a ValueError on broken exports
5378
      exp_nic_count = int(export_info.get(constants.INISECT_INS, 'nic_count'))
5379
      if self.op.instance_name == old_name:
5380
        for idx, nic in enumerate(self.nics):
5381
          if nic.mac == constants.VALUE_AUTO and exp_nic_count >= idx:
5382
            nic_mac_ini = 'nic%d_mac' % idx
5383
            nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini)
5384

    
5385
    # ENDIF: self.op.mode == constants.INSTANCE_IMPORT
5386
    # ip ping checks (we use the same ip that was resolved in ExpandNames)
5387
    if self.op.start and not self.op.ip_check:
5388
      raise errors.OpPrereqError("Cannot ignore IP address conflicts when"
5389
                                 " adding an instance in start mode")
5390

    
5391
    if self.op.ip_check:
5392
      if utils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT):
5393
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
5394
                                   (self.check_ip, self.op.instance_name))
5395

    
5396
    #### mac address generation
5397
    # By generating here the mac address both the allocator and the hooks get
5398
    # the real final mac address rather than the 'auto' or 'generate' value.
5399
    # There is a race condition between the generation and the instance object
5400
    # creation, which means that we know the mac is valid now, but we're not
5401
    # sure it will be when we actually add the instance. If things go bad
5402
    # adding the instance will abort because of a duplicate mac, and the
5403
    # creation job will fail.
5404
    for nic in self.nics:
5405
      if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
5406
        nic.mac = self.cfg.GenerateMAC()
5407

    
5408
    #### allocator run
5409

    
5410
    if self.op.iallocator is not None:
5411
      self._RunAllocator()
5412

    
5413
    #### node related checks
5414

    
5415
    # check primary node
5416
    self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode)
5417
    assert self.pnode is not None, \
5418
      "Cannot retrieve locked node %s" % self.op.pnode
5419
    if pnode.offline:
5420
      raise errors.OpPrereqError("Cannot use offline primary node '%s'" %
5421
                                 pnode.name)
5422
    if pnode.drained:
5423
      raise errors.OpPrereqError("Cannot use drained primary node '%s'" %
5424
                                 pnode.name)
5425

    
5426
    self.secondaries = []
5427

    
5428
    # mirror node verification
5429
    if self.op.disk_template in constants.DTS_NET_MIRROR:
5430
      if self.op.snode is None:
5431
        raise errors.OpPrereqError("The networked disk templates need"
5432
                                   " a mirror node")
5433
      if self.op.snode == pnode.name:
5434
        raise errors.OpPrereqError("The secondary node cannot be"
5435
                                   " the primary node.")
5436
      _CheckNodeOnline(self, self.op.snode)
5437
      _CheckNodeNotDrained(self, self.op.snode)
5438
      self.secondaries.append(self.op.snode)
5439

    
5440
    nodenames = [pnode.name] + self.secondaries
5441

    
5442
    req_size = _ComputeDiskSize(self.op.disk_template,
5443
                                self.disks)
5444

    
5445
    # Check lv size requirements
5446
    if req_size is not None:
5447
      nodeinfo = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
5448
                                         self.op.hypervisor)
5449
      for node in nodenames:
5450
        info = nodeinfo[node]
5451
        info.Raise("Cannot get current information from node %s" % node)
5452
        info = info.payload
5453
        vg_free = info.get('vg_free', None)
5454
        if not isinstance(vg_free, int):
5455
          raise errors.OpPrereqError("Can't compute free disk space on"
5456
                                     " node %s" % node)
5457
        if req_size > vg_free:
5458
          raise errors.OpPrereqError("Not enough disk space on target node %s."
5459
                                     " %d MB available, %d MB required" %
5460
                                     (node, vg_free, req_size))
5461

    
5462
    _CheckHVParams(self, nodenames, self.op.hypervisor, self.op.hvparams)
5463

    
5464
    # os verification
5465
    result = self.rpc.call_os_get(pnode.name, self.op.os_type)
5466
    result.Raise("OS '%s' not in supported os list for primary node %s" %
5467
                 (self.op.os_type, pnode.name), prereq=True)
5468

    
5469
    _CheckNicsBridgesExist(self, self.nics, self.pnode.name)
5470

    
5471
    # memory check on primary node
5472
    if self.op.start:
5473
      _CheckNodeFreeMemory(self, self.pnode.name,
5474
                           "creating instance %s" % self.op.instance_name,
5475
                           self.be_full[constants.BE_MEMORY],
5476
                           self.op.hypervisor)
5477

    
5478
    self.dry_run_result = list(nodenames)
5479

    
5480
  def Exec(self, feedback_fn):
5481
    """Create and add the instance to the cluster.
5482

5483
    """
5484
    instance = self.op.instance_name
5485
    pnode_name = self.pnode.name
5486

    
5487
    ht_kind = self.op.hypervisor
5488
    if ht_kind in constants.HTS_REQ_PORT:
5489
      network_port = self.cfg.AllocatePort()
5490
    else:
5491
      network_port = None
5492

    
5493
    ##if self.op.vnc_bind_address is None:
5494
    ##  self.op.vnc_bind_address = constants.VNC_DEFAULT_BIND_ADDRESS
5495

    
5496
    # this is needed because os.path.join does not accept None arguments
5497
    if self.op.file_storage_dir is None:
5498
      string_file_storage_dir = ""
5499
    else:
5500
      string_file_storage_dir = self.op.file_storage_dir
5501

    
5502
    # build the full file storage dir path
5503
    file_storage_dir = os.path.normpath(os.path.join(
5504
                                        self.cfg.GetFileStorageDir(),
5505
                                        string_file_storage_dir, instance))
5506

    
5507

    
5508
    disks = _GenerateDiskTemplate(self,
5509
                                  self.op.disk_template,
5510
                                  instance, pnode_name,
5511
                                  self.secondaries,
5512
                                  self.disks,
5513
                                  file_storage_dir,
5514
                                  self.op.file_driver,
5515
                                  0)
5516

    
5517
    iobj = objects.Instance(name=instance, os=self.op.os_type,
5518
                            primary_node=pnode_name,
5519
                            nics=self.nics, disks=disks,
5520
                            disk_template=self.op.disk_template,
5521
                            admin_up=False,
5522
                            network_port=network_port,
5523
                            beparams=self.op.beparams,
5524
                            hvparams=self.op.hvparams,
5525
                            hypervisor=self.op.hypervisor,
5526
                            )
5527

    
5528
    feedback_fn("* creating instance disks...")
5529
    try:
5530
      _CreateDisks(self, iobj)
5531
    except errors.OpExecError:
5532
      self.LogWarning("Device creation failed, reverting...")
5533
      try:
5534
        _RemoveDisks(self, iobj)
5535
      finally:
5536
        self.cfg.ReleaseDRBDMinors(instance)
5537
        raise
5538

    
5539
    feedback_fn("adding instance %s to cluster config" % instance)
5540

    
5541
    self.cfg.AddInstance(iobj)
5542
    # Declare that we don't want to remove the instance lock anymore, as we've
5543
    # added the instance to the config
5544
    del self.remove_locks[locking.LEVEL_INSTANCE]
5545
    # Unlock all the nodes
5546
    if self.op.mode == constants.INSTANCE_IMPORT:
5547
      nodes_keep = [self.op.src_node]
5548
      nodes_release = [node for node in self.acquired_locks[locking.LEVEL_NODE]
5549
                       if node != self.op.src_node]
5550
      self.context.glm.release(locking.LEVEL_NODE, nodes_release)
5551
      self.acquired_locks[locking.LEVEL_NODE] = nodes_keep
5552
    else:
5553
      self.context.glm.release(locking.LEVEL_NODE)
5554
      del self.acquired_locks[locking.LEVEL_NODE]
5555

    
5556
    if self.op.wait_for_sync:
5557
      disk_abort = not _WaitForSync(self, iobj)
5558
    elif iobj.disk_template in constants.DTS_NET_MIRROR:
5559
      # make sure the disks are not degraded (still sync-ing is ok)
5560
      time.sleep(15)
5561
      feedback_fn("* checking mirrors status")
5562
      disk_abort = not _WaitForSync(self, iobj, oneshot=True)
5563
    else:
5564
      disk_abort = False
5565

    
5566
    if disk_abort:
5567
      _RemoveDisks(self, iobj)
5568
      self.cfg.RemoveInstance(iobj.name)
5569
      # Make sure the instance lock gets removed
5570
      self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
5571
      raise errors.OpExecError("There are some degraded disks for"
5572
                               " this instance")
5573

    
5574
    feedback_fn("creating os for instance %s on node %s" %
5575
                (instance, pnode_name))
5576

    
5577
    if iobj.disk_template != constants.DT_DISKLESS:
5578
      if self.op.mode == constants.INSTANCE_CREATE:
5579
        feedback_fn("* running the instance OS create scripts...")
5580
        result = self.rpc.call_instance_os_add(pnode_name, iobj, False)
5581
        result.Raise("Could not add os for instance %s"
5582
                     " on node %s" % (instance, pnode_name))
5583

    
5584
      elif self.op.mode == constants.INSTANCE_IMPORT:
5585
        feedback_fn("* running the instance OS import scripts...")
5586
        src_node = self.op.src_node
5587
        src_images = self.src_images
5588
        cluster_name = self.cfg.GetClusterName()
5589
        import_result = self.rpc.call_instance_os_import(pnode_name, iobj,
5590
                                                         src_node, src_images,
5591
                                                         cluster_name)
5592
        msg = import_result.fail_msg
5593
        if msg:
5594
          self.LogWarning("Error while importing the disk images for instance"
5595
                          " %s on node %s: %s" % (instance, pnode_name, msg))
5596
      else:
5597
        # also checked in the prereq part
5598
        raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
5599
                                     % self.op.mode)
5600

    
5601
    if self.op.start:
5602
      iobj.admin_up = True
5603
      self.cfg.Update(iobj)
5604
      logging.info("Starting instance %s on node %s", instance, pnode_name)
5605
      feedback_fn("* starting instance...")
5606
      result = self.rpc.call_instance_start(pnode_name, iobj, None, None)
5607
      result.Raise("Could not start instance")
5608

    
5609
    return list(iobj.all_nodes)
5610

    
5611

    
5612
class LUConnectConsole(NoHooksLU):
5613
  """Connect to an instance's console.
5614

5615
  This is somewhat special in that it returns the command line that
5616
  you need to run on the master node in order to connect to the
5617
  console.
5618

5619
  """
5620
  _OP_REQP = ["instance_name"]
5621
  REQ_BGL = False
5622

    
5623
  def ExpandNames(self):
5624
    self._ExpandAndLockInstance()
5625

    
5626
  def CheckPrereq(self):
5627
    """Check prerequisites.
5628

5629
    This checks that the instance is in the cluster.
5630

5631
    """
5632
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
5633
    assert self.instance is not None, \
5634
      "Cannot retrieve locked instance %s" % self.op.instance_name
5635
    _CheckNodeOnline(self, self.instance.primary_node)
5636

    
5637
  def Exec(self, feedback_fn):
5638
    """Connect to the console of an instance
5639

5640
    """
5641
    instance = self.instance
5642
    node = instance.primary_node
5643

    
5644
    node_insts = self.rpc.call_instance_list([node],
5645
                                             [instance.hypervisor])[node]
5646
    node_insts.Raise("Can't get node information from %s" % node)
5647

    
5648
    if instance.name not in node_insts.payload:
5649
      raise errors.OpExecError("Instance %s is not running." % instance.name)
5650

    
5651
    logging.debug("Connecting to console of %s on %s", instance.name, node)
5652

    
5653
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
5654
    cluster = self.cfg.GetClusterInfo()
5655
    # beparams and hvparams are passed separately, to avoid editing the
5656
    # instance and then saving the defaults in the instance itself.
5657
    hvparams = cluster.FillHV(instance)
5658
    beparams = cluster.FillBE(instance)
5659
    console_cmd = hyper.GetShellCommandForConsole(instance, hvparams, beparams)
5660

    
5661
    # build ssh cmdline
5662
    return self.ssh.BuildCmd(node, "root", console_cmd, batch=True, tty=True)
5663

    
5664

    
5665
class LUReplaceDisks(LogicalUnit):
5666
  """Replace the disks of an instance.
5667

5668
  """
5669
  HPATH = "mirrors-replace"
5670
  HTYPE = constants.HTYPE_INSTANCE
5671
  _OP_REQP = ["instance_name", "mode", "disks"]
5672
  REQ_BGL = False
5673

    
5674
  def CheckArguments(self):
5675
    if not hasattr(self.op, "remote_node"):
5676
      self.op.remote_node = None
5677
    if not hasattr(self.op, "iallocator"):
5678
      self.op.iallocator = None
5679

    
5680
    TLReplaceDisks.CheckArguments(self.op.mode, self.op.remote_node,
5681
                                  self.op.iallocator)
5682

    
5683
  def ExpandNames(self):
5684
    self._ExpandAndLockInstance()
5685

    
5686
    if self.op.iallocator is not None:
5687
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
5688

    
5689
    elif self.op.remote_node is not None:
5690
      remote_node = self.cfg.ExpandNodeName(self.op.remote_node)
5691
      if remote_node is None:
5692
        raise errors.OpPrereqError("Node '%s' not known" %
5693
                                   self.op.remote_node)
5694

    
5695
      self.op.remote_node = remote_node
5696

    
5697
      # Warning: do not remove the locking of the new secondary here
5698
      # unless DRBD8.AddChildren is changed to work in parallel;
5699
      # currently it doesn't since parallel invocations of
5700
      # FindUnusedMinor will conflict
5701
      self.needed_locks[locking.LEVEL_NODE] = [remote_node]
5702
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
5703

    
5704
    else:
5705
      self.needed_locks[locking.LEVEL_NODE] = []
5706
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
5707

    
5708
    self.replacer = TLReplaceDisks(self, self.op.instance_name, self.op.mode,
5709
                                   self.op.iallocator, self.op.remote_node,
5710
                                   self.op.disks)
5711

    
5712
    self.tasklets = [self.replacer]
5713

    
5714
  def DeclareLocks(self, level):
5715
    # If we're not already locking all nodes in the set we have to declare the
5716
    # instance's primary/secondary nodes.
5717
    if (level == locking.LEVEL_NODE and
5718
        self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET):
5719
      self._LockInstancesNodes()
5720

    
5721
  def BuildHooksEnv(self):
5722
    """Build hooks env.
5723

5724
    This runs on the master, the primary and all the secondaries.
5725

5726
    """
5727
    instance = self.replacer.instance
5728
    env = {
5729
      "MODE": self.op.mode,
5730
      "NEW_SECONDARY": self.op.remote_node,
5731
      "OLD_SECONDARY": instance.secondary_nodes[0],
5732
      }
5733
    env.update(_BuildInstanceHookEnvByObject(self, instance))
5734
    nl = [
5735
      self.cfg.GetMasterNode(),
5736
      instance.primary_node,
5737
      ]
5738
    if self.op.remote_node is not None:
5739
      nl.append(self.op.remote_node)
5740
    return env, nl, nl
5741

    
5742

    
5743
class LUEvacuateNode(LogicalUnit):
5744
  """Relocate the secondary instances from a node.
5745

5746
  """
5747
  HPATH = "node-evacuate"
5748
  HTYPE = constants.HTYPE_NODE
5749
  _OP_REQP = ["node_name"]
5750
  REQ_BGL = False
5751

    
5752
  def CheckArguments(self):
5753
    if not hasattr(self.op, "remote_node"):
5754
      self.op.remote_node = None
5755
    if not hasattr(self.op, "iallocator"):
5756
      self.op.iallocator = None
5757

    
5758
    TLReplaceDisks.CheckArguments(constants.REPLACE_DISK_CHG,
5759
                                  self.op.remote_node,
5760
                                  self.op.iallocator)
5761

    
5762
  def ExpandNames(self):
5763
    self.op.node_name = self.cfg.ExpandNodeName(self.op.node_name)
5764
    if self.op.node_name is None:
5765
      raise errors.OpPrereqError("Node '%s' not known" % self.op.node_name)
5766

    
5767
    self.needed_locks = {}
5768

    
5769
    # Declare node locks
5770
    if self.op.iallocator is not None:
5771
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
5772

    
5773
    elif self.op.remote_node is not None:
5774
      remote_node = self.cfg.ExpandNodeName(self.op.remote_node)
5775
      if remote_node is None:
5776
        raise errors.OpPrereqError("Node '%s' not known" %
5777
                                   self.op.remote_node)
5778

    
5779
      self.op.remote_node = remote_node
5780

    
5781
      # Warning: do not remove the locking of the new secondary here
5782
      # unless DRBD8.AddChildren is changed to work in parallel;
5783
      # currently it doesn't since parallel invocations of
5784
      # FindUnusedMinor will conflict
5785
      self.needed_locks[locking.LEVEL_NODE] = [remote_node]
5786
      self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
5787

    
5788
    else:
5789
      raise errors.OpPrereqError("Invalid parameters")
5790

    
5791
    # Create tasklets for replacing disks for all secondary instances on this
5792
    # node
5793
    names = []
5794
    tasklets = []
5795

    
5796
    for inst in _GetNodeSecondaryInstances(self.cfg, self.op.node_name):
5797
      logging.debug("Replacing disks for instance %s", inst.name)
5798
      names.append(inst.name)
5799

    
5800
      replacer = TLReplaceDisks(self, inst.name, constants.REPLACE_DISK_CHG,
5801
                                self.op.iallocator, self.op.remote_node, [])
5802
      tasklets.append(replacer)
5803

    
5804
    self.tasklets = tasklets
5805
    self.instance_names = names
5806

    
5807
    # Declare instance locks
5808
    self.needed_locks[locking.LEVEL_INSTANCE] = self.instance_names
5809

    
5810
  def DeclareLocks(self, level):
5811
    # If we're not already locking all nodes in the set we have to declare the
5812
    # instance's primary/secondary nodes.
5813
    if (level == locking.LEVEL_NODE and
5814
        self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET):
5815
      self._LockInstancesNodes()
5816

    
5817
  def BuildHooksEnv(self):
5818
    """Build hooks env.
5819

5820
    This runs on the master, the primary and all the secondaries.
5821

5822
    """
5823
    env = {
5824
      "NODE_NAME": self.op.node_name,
5825
      }
5826

    
5827
    nl = [self.cfg.GetMasterNode()]
5828

    
5829
    if self.op.remote_node is not None:
5830
      env["NEW_SECONDARY"] = self.op.remote_node
5831
      nl.append(self.op.remote_node)
5832

    
5833
    return (env, nl, nl)
5834

    
5835

    
5836
class TLReplaceDisks(Tasklet):
5837
  """Replaces disks for an instance.
5838

5839
  Note: Locking is not within the scope of this class.
5840

5841
  """
5842
  def __init__(self, lu, instance_name, mode, iallocator_name, remote_node,
5843
               disks):
5844
    """Initializes this class.
5845

5846
    """
5847
    Tasklet.__init__(self, lu)
5848

    
5849
    # Parameters
5850
    self.instance_name = instance_name
5851
    self.mode = mode
5852
    self.iallocator_name = iallocator_name
5853
    self.remote_node = remote_node
5854
    self.disks = disks
5855

    
5856
    # Runtime data
5857
    self.instance = None
5858
    self.new_node = None
5859
    self.target_node = None
5860
    self.other_node = None
5861
    self.remote_node_info = None
5862
    self.node_secondary_ip = None
5863

    
5864
  @staticmethod
5865
  def CheckArguments(mode, remote_node, iallocator):
5866
    """Helper function for users of this class.
5867

5868
    """
5869
    # check for valid parameter combination
5870
    if mode == constants.REPLACE_DISK_CHG:
5871
      if remote_node is None and iallocator is None:
5872
        raise errors.OpPrereqError("When changing the secondary either an"
5873
                                   " iallocator script must be used or the"
5874
                                   " new node given")
5875

    
5876
      if remote_node is not None and iallocator is not None:
5877
        raise errors.OpPrereqError("Give either the iallocator or the new"
5878
                                   " secondary, not both")
5879

    
5880
    elif remote_node is not None or iallocator is not None:
5881
      # Not replacing the secondary
5882
      raise errors.OpPrereqError("The iallocator and new node options can"
5883
                                 " only be used when changing the"
5884
                                 " secondary node")
5885

    
5886
  @staticmethod
5887
  def _RunAllocator(lu, iallocator_name, instance_name, relocate_from):
5888
    """Compute a new secondary node using an IAllocator.
5889

5890
    """
5891
    ial = IAllocator(lu.cfg, lu.rpc,
5892
                     mode=constants.IALLOCATOR_MODE_RELOC,
5893
                     name=instance_name,
5894
                     relocate_from=relocate_from)
5895

    
5896
    ial.Run(iallocator_name)
5897

    
5898
    if not ial.success:
5899
      raise errors.OpPrereqError("Can't compute nodes using iallocator '%s':"
5900
                                 " %s" % (iallocator_name, ial.info))
5901

    
5902
    if len(ial.nodes) != ial.required_nodes:
5903
      raise errors.OpPrereqError("iallocator '%s' returned invalid number"
5904
                                 " of nodes (%s), required %s" %
5905
                                 (len(ial.nodes), ial.required_nodes))
5906

    
5907
    remote_node_name = ial.nodes[0]
5908

    
5909
    lu.LogInfo("Selected new secondary for instance '%s': %s",
5910
               instance_name, remote_node_name)
5911

    
5912
    return remote_node_name
5913

    
5914
  def _FindFaultyDisks(self, node_name):
5915
    faulty = []
5916

    
5917
    for dev in self.instance.disks:
5918
      self.cfg.SetDiskID(dev, node_name)
5919

    
5920
    result = self.rpc.call_blockdev_getmirrorstatus(node_name,
5921
                                                    self.instance.disks)
5922
    result.Raise("Failed to get disk status from node %s" % node_name,
5923
                 prereq=True)
5924

    
5925
    for idx, bdev_status in enumerate(result.payload):
5926
      if bdev_status and bdev_status.ldisk_status == constants.LDS_FAULTY:
5927
        faulty.append(idx)
5928

    
5929
    return faulty
5930

    
5931
  def CheckPrereq(self):
5932
    """Check prerequisites.
5933

5934
    This checks that the instance is in the cluster.
5935

5936
    """
5937
    self.instance = self.cfg.GetInstanceInfo(self.instance_name)
5938
    assert self.instance is not None, \
5939
      "Cannot retrieve locked instance %s" % self.instance_name
5940

    
5941
    if self.instance.disk_template != constants.DT_DRBD8:
5942
      raise errors.OpPrereqError("Can only run replace disks for DRBD8-based"
5943
                                 " instances")
5944

    
5945
    if len(self.instance.secondary_nodes) != 1:
5946
      raise errors.OpPrereqError("The instance has a strange layout,"
5947
                                 " expected one secondary but found %d" %
5948
                                 len(self.instance.secondary_nodes))
5949

    
5950
    secondary_node = self.instance.secondary_nodes[0]
5951

    
5952
    if self.iallocator_name is None:
5953
      remote_node = self.remote_node
5954
    else:
5955
      remote_node = self._RunAllocator(self.lu, self.iallocator_name,
5956
                                       self.instance.name, secondary_node)
5957

    
5958
    if remote_node is not None:
5959
      self.remote_node_info = self.cfg.GetNodeInfo(remote_node)
5960
      assert self.remote_node_info is not None, \
5961
        "Cannot retrieve locked node %s" % remote_node
5962
    else:
5963
      self.remote_node_info = None
5964

    
5965
    if remote_node == self.instance.primary_node:
5966
      raise errors.OpPrereqError("The specified node is the primary node of"
5967
                                 " the instance.")
5968

    
5969
    if remote_node == secondary_node:
5970
      raise errors.OpPrereqError("The specified node is already the"
5971
                                 " secondary node of the instance.")
5972

    
5973
    if self.disks and self.mode in (constants.REPLACE_DISK_AUTO,
5974
                                    constants.REPLACE_DISK_CHG):
5975
      raise errors.OpPrereqError("Cannot specify disks to be replaced")
5976

    
5977
    if self.mode == constants.REPLACE_DISK_AUTO:
5978
      faulty_primary = self._FindFaultyDisks(self.instance.primary_node)
5979
      faulty_secondary = self._FindFaultyDisks(secondary_node)
5980

    
5981
      if faulty_primary and faulty_secondary:
5982
        raise errors.OpPrereqError("Instance %s has faulty disks on more than"
5983
                                   " one node and can not be repaired"
5984
                                   " automatically" % self.instance_name)
5985

    
5986
      if faulty_primary:
5987
        self.disks = faulty_primary
5988
        self.target_node = self.instance.primary_node
5989
        self.other_node = secondary_node
5990
        check_nodes = [self.target_node, self.other_node]
5991
      elif faulty_secondary:
5992
        self.disks = faulty_secondary
5993
        self.target_node = secondary_node
5994
        self.other_node = self.instance.primary_node
5995
        check_nodes = [self.target_node, self.other_node]
5996
      else:
5997
        self.disks = []
5998
        check_nodes = []
5999

    
6000
    else:
6001
      # Non-automatic modes
6002
      if self.mode == constants.REPLACE_DISK_PRI:
6003
        self.target_node = self.instance.primary_node
6004
        self.other_node = secondary_node
6005
        check_nodes = [self.target_node, self.other_node]
6006

    
6007
      elif self.mode == constants.REPLACE_DISK_SEC:
6008
        self.target_node = secondary_node
6009
        self.other_node = self.instance.primary_node
6010
        check_nodes = [self.target_node, self.other_node]
6011

    
6012
      elif self.mode == constants.REPLACE_DISK_CHG:
6013
        self.new_node = remote_node
6014
        self.other_node = self.instance.primary_node
6015
        self.target_node = secondary_node
6016
        check_nodes = [self.new_node, self.other_node]
6017

    
6018
        _CheckNodeNotDrained(self.lu, remote_node)
6019

    
6020
      else:
6021
        raise errors.ProgrammerError("Unhandled disk replace mode (%s)" %
6022
                                     self.mode)
6023

    
6024
      # If not specified all disks should be replaced
6025
      if not self.disks:
6026
        self.disks = range(len(self.instance.disks))
6027

    
6028
    for node in check_nodes:
6029
      _CheckNodeOnline(self.lu, node)
6030

    
6031
    # Check whether disks are valid
6032
    for disk_idx in self.disks:
6033
      self.instance.FindDisk(disk_idx)
6034

    
6035
    # Get secondary node IP addresses
6036
    node_2nd_ip = {}
6037

    
6038
    for node_name in [self.target_node, self.other_node, self.new_node]:
6039
      if node_name is not None:
6040
        node_2nd_ip[node_name] = self.cfg.GetNodeInfo(node_name).secondary_ip
6041

    
6042
    self.node_secondary_ip = node_2nd_ip
6043

    
6044
  def Exec(self, feedback_fn):
6045
    """Execute disk replacement.
6046

6047
    This dispatches the disk replacement to the appropriate handler.
6048

6049
    """
6050
    if not self.disks:
6051
      feedback_fn("No disks need replacement")
6052
      return
6053

    
6054
    feedback_fn("Replacing disk(s) %s for %s" %
6055
                (", ".join([str(i) for i in self.disks]), self.instance.name))
6056

    
6057
    activate_disks = (not self.instance.admin_up)
6058

    
6059
    # Activate the instance disks if we're replacing them on a down instance
6060
    if activate_disks:
6061
      _StartInstanceDisks(self.lu, self.instance, True)
6062

    
6063
    try:
6064
      # Should we replace the secondary node?
6065
      if self.new_node is not None:
6066
        return self._ExecDrbd8Secondary()
6067
      else:
6068
        return self._ExecDrbd8DiskOnly()
6069

    
6070
    finally:
6071
      # Deactivate the instance disks if we're replacing them on a down instance
6072
      if activate_disks:
6073
        _SafeShutdownInstanceDisks(self.lu, self.instance)
6074

    
6075
  def _CheckVolumeGroup(self, nodes):
6076
    self.lu.LogInfo("Checking volume groups")
6077

    
6078
    vgname = self.cfg.GetVGName()
6079

    
6080
    # Make sure volume group exists on all involved nodes
6081
    results = self.rpc.call_vg_list(nodes)
6082
    if not results:
6083
      raise errors.OpExecError("Can't list volume groups on the nodes")
6084

    
6085
    for node in nodes:
6086
      res = results[node]
6087
      res.Raise("Error checking node %s" % node)
6088
      if vgname not in res.payload:
6089
        raise errors.OpExecError("Volume group '%s' not found on node %s" %
6090
                                 (vgname, node))
6091

    
6092
  def _CheckDisksExistence(self, nodes):
6093
    # Check disk existence
6094
    for idx, dev in enumerate(self.instance.disks):
6095
      if idx not in self.disks:
6096
        continue
6097

    
6098
      for node in nodes:
6099
        self.lu.LogInfo("Checking disk/%d on %s" % (idx, node))
6100
        self.cfg.SetDiskID(dev, node)
6101

    
6102
        result = self.rpc.call_blockdev_find(node, dev)
6103

    
6104
        msg = result.fail_msg
6105
        if msg or not result.payload:
6106
          if not msg:
6107
            msg = "disk not found"
6108
          raise errors.OpExecError("Can't find disk/%d on node %s: %s" %
6109
                                   (idx, node, msg))
6110

    
6111
  def _CheckDisksConsistency(self, node_name, on_primary, ldisk):
6112
    for idx, dev in enumerate(self.instance.disks):
6113
      if idx not in self.disks:
6114
        continue
6115

    
6116
      self.lu.LogInfo("Checking disk/%d consistency on node %s" %
6117
                      (idx, node_name))
6118

    
6119
      if not _CheckDiskConsistency(self.lu, dev, node_name, on_primary,
6120
                                   ldisk=ldisk):
6121
        raise errors.OpExecError("Node %s has degraded storage, unsafe to"
6122
                                 " replace disks for instance %s" %
6123
                                 (node_name, self.instance.name))
6124

    
6125
  def _CreateNewStorage(self, node_name):
6126
    vgname = self.cfg.GetVGName()
6127
    iv_names = {}
6128

    
6129
    for idx, dev in enumerate(self.instance.disks):
6130
      if idx not in self.disks:
6131
        continue
6132

    
6133
      self.lu.LogInfo("Adding storage on %s for disk/%d" % (node_name, idx))
6134

    
6135
      self.cfg.SetDiskID(dev, node_name)
6136

    
6137
      lv_names = [".disk%d_%s" % (idx, suffix) for suffix in ["data", "meta"]]
6138
      names = _GenerateUniqueNames(self.lu, lv_names)
6139

    
6140
      lv_data = objects.Disk(dev_type=constants.LD_LV, size=dev.size,
6141
                             logical_id=(vgname, names[0]))
6142
      lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
6143
                             logical_id=(vgname, names[1]))
6144

    
6145
      new_lvs = [lv_data, lv_meta]
6146
      old_lvs = dev.children
6147
      iv_names[dev.iv_name] = (dev, old_lvs, new_lvs)
6148

    
6149
      # we pass force_create=True to force the LVM creation
6150
      for new_lv in new_lvs:
6151
        _CreateBlockDev(self.lu, node_name, self.instance, new_lv, True,
6152
                        _GetInstanceInfoText(self.instance), False)
6153

    
6154
    return iv_names
6155

    
6156
  def _CheckDevices(self, node_name, iv_names):
6157
    for name, (dev, old_lvs, new_lvs) in iv_names.iteritems():
6158
      self.cfg.SetDiskID(dev, node_name)
6159

    
6160
      result = self.rpc.call_blockdev_find(node_name, dev)
6161

    
6162
      msg = result.fail_msg
6163
      if msg or not result.payload:
6164
        if not msg:
6165
          msg = "disk not found"
6166
        raise errors.OpExecError("Can't find DRBD device %s: %s" %
6167
                                 (name, msg))
6168

    
6169
      if result.payload.is_degraded:
6170
        raise errors.OpExecError("DRBD device %s is degraded!" % name)
6171

    
6172
  def _RemoveOldStorage(self, node_name, iv_names):
6173
    for name, (dev, old_lvs, _) in iv_names.iteritems():
6174
      self.lu.LogInfo("Remove logical volumes for %s" % name)
6175

    
6176
      for lv in old_lvs:
6177
        self.cfg.SetDiskID(lv, node_name)
6178

    
6179
        msg = self.rpc.call_blockdev_remove(node_name, lv).fail_msg
6180
        if msg:
6181
          self.lu.LogWarning("Can't remove old LV: %s" % msg,
6182
                             hint="remove unused LVs manually")
6183

    
6184
  def _ExecDrbd8DiskOnly(self):
6185
    """Replace a disk on the primary or secondary for DRBD 8.
6186

6187
    The algorithm for replace is quite complicated:
6188

6189
      1. for each disk to be replaced:
6190

6191
        1. create new LVs on the target node with unique names
6192
        1. detach old LVs from the drbd device
6193
        1. rename old LVs to name_replaced.<time_t>
6194
        1. rename new LVs to old LVs
6195
        1. attach the new LVs (with the old names now) to the drbd device
6196

6197
      1. wait for sync across all devices
6198

6199
      1. for each modified disk:
6200

6201
        1. remove old LVs (which have the name name_replaces.<time_t>)
6202

6203
    Failures are not very well handled.
6204

6205
    """
6206
    steps_total = 6
6207

    
6208
    # Step: check device activation
6209
    self.lu.LogStep(1, steps_total, "Check device existence")
6210
    self._CheckDisksExistence([self.other_node, self.target_node])
6211
    self._CheckVolumeGroup([self.target_node, self.other_node])
6212

    
6213
    # Step: check other node consistency
6214
    self.lu.LogStep(2, steps_total, "Check peer consistency")
6215
    self._CheckDisksConsistency(self.other_node,
6216
                                self.other_node == self.instance.primary_node,
6217
                                False)
6218

    
6219
    # Step: create new storage
6220
    self.lu.LogStep(3, steps_total, "Allocate new storage")
6221
    iv_names = self._CreateNewStorage(self.target_node)
6222

    
6223
    # Step: for each lv, detach+rename*2+attach
6224
    self.lu.LogStep(4, steps_total, "Changing drbd configuration")
6225
    for dev, old_lvs, new_lvs in iv_names.itervalues():
6226
      self.lu.LogInfo("Detaching %s drbd from local storage" % dev.iv_name)
6227

    
6228
      result = self.rpc.call_blockdev_removechildren(self.target_node, dev, old_lvs)
6229
      result.Raise("Can't detach drbd from local storage on node"
6230
                   " %s for device %s" % (self.target_node, dev.iv_name))
6231
      #dev.children = []
6232
      #cfg.Update(instance)
6233

    
6234
      # ok, we created the new LVs, so now we know we have the needed
6235
      # storage; as such, we proceed on the target node to rename
6236
      # old_lv to _old, and new_lv to old_lv; note that we rename LVs
6237
      # using the assumption that logical_id == physical_id (which in
6238
      # turn is the unique_id on that node)
6239

    
6240
      # FIXME(iustin): use a better name for the replaced LVs
6241
      temp_suffix = int(time.time())
6242
      ren_fn = lambda d, suff: (d.physical_id[0],
6243
                                d.physical_id[1] + "_replaced-%s" % suff)
6244

    
6245
      # Build the rename list based on what LVs exist on the node
6246
      rename_old_to_new = []
6247
      for to_ren in old_lvs:
6248
        result = self.rpc.call_blockdev_find(self.target_node, to_ren)
6249
        if not result.fail_msg and result.payload:
6250
          # device exists
6251
          rename_old_to_new.append((to_ren, ren_fn(to_ren, temp_suffix)))
6252

    
6253
      self.lu.LogInfo("Renaming the old LVs on the target node")
6254
      result = self.rpc.call_blockdev_rename(self.target_node, rename_old_to_new)
6255
      result.Raise("Can't rename old LVs on node %s" % self.target_node)
6256

    
6257
      # Now we rename the new LVs to the old LVs
6258
      self.lu.LogInfo("Renaming the new LVs on the target node")
6259
      rename_new_to_old = [(new, old.physical_id)
6260
                           for old, new in zip(old_lvs, new_lvs)]
6261
      result = self.rpc.call_blockdev_rename(self.target_node, rename_new_to_old)
6262
      result.Raise("Can't rename new LVs on node %s" % self.target_node)
6263

    
6264
      for old, new in zip(old_lvs, new_lvs):
6265
        new.logical_id = old.logical_id
6266
        self.cfg.SetDiskID(new, self.target_node)
6267

    
6268
      for disk in old_lvs:
6269
        disk.logical_id = ren_fn(disk, temp_suffix)
6270
        self.cfg.SetDiskID(disk, self.target_node)
6271

    
6272
      # Now that the new lvs have the old name, we can add them to the device
6273
      self.lu.LogInfo("Adding new mirror component on %s" % self.target_node)
6274
      result = self.rpc.call_blockdev_addchildren(self.target_node, dev, new_lvs)
6275
      msg = result.fail_msg
6276
      if msg:
6277
        for new_lv in new_lvs:
6278
          msg2 = self.rpc.call_blockdev_remove(self.target_node, new_lv).fail_msg
6279
          if msg2:
6280
            self.lu.LogWarning("Can't rollback device %s: %s", dev, msg2,
6281
                               hint=("cleanup manually the unused logical"
6282
                                     "volumes"))
6283
        raise errors.OpExecError("Can't add local storage to drbd: %s" % msg)
6284

    
6285
      dev.children = new_lvs
6286

    
6287
      self.cfg.Update(self.instance)
6288

    
6289
    # Wait for sync
6290
    # This can fail as the old devices are degraded and _WaitForSync
6291
    # does a combined result over all disks, so we don't check its return value
6292
    self.lu.LogStep(5, steps_total, "Sync devices")
6293
    _WaitForSync(self.lu, self.instance, unlock=True)
6294

    
6295
    # Check all devices manually
6296
    self._CheckDevices(self.instance.primary_node, iv_names)
6297

    
6298
    # Step: remove old storage
6299
    self.lu.LogStep(6, steps_total, "Removing old storage")
6300
    self._RemoveOldStorage(self.target_node, iv_names)
6301

    
6302
  def _ExecDrbd8Secondary(self):
6303
    """Replace the secondary node for DRBD 8.
6304

6305
    The algorithm for replace is quite complicated:
6306
      - for all disks of the instance:
6307
        - create new LVs on the new node with same names
6308
        - shutdown the drbd device on the old secondary
6309
        - disconnect the drbd network on the primary
6310
        - create the drbd device on the new secondary
6311
        - network attach the drbd on the primary, using an artifice:
6312
          the drbd code for Attach() will connect to the network if it
6313
          finds a device which is connected to the good local disks but
6314
          not network enabled
6315
      - wait for sync across all devices
6316
      - remove all disks from the old secondary
6317

6318
    Failures are not very well handled.
6319

6320
    """
6321
    steps_total = 6
6322

    
6323
    # Step: check device activation
6324
    self.lu.LogStep(1, steps_total, "Check device existence")
6325
    self._CheckDisksExistence([self.instance.primary_node])
6326
    self._CheckVolumeGroup([self.instance.primary_node])
6327

    
6328
    # Step: check other node consistency
6329
    self.lu.LogStep(2, steps_total, "Check peer consistency")
6330
    self._CheckDisksConsistency(self.instance.primary_node, True, True)
6331

    
6332
    # Step: create new storage
6333
    self.lu.LogStep(3, steps_total, "Allocate new storage")
6334
    for idx, dev in enumerate(self.instance.disks):
6335
      self.lu.LogInfo("Adding new local storage on %s for disk/%d" %
6336
                      (self.new_node, idx))
6337
      # we pass force_create=True to force LVM creation
6338
      for new_lv in dev.children:
6339
        _CreateBlockDev(self.lu, self.new_node, self.instance, new_lv, True,
6340
                        _GetInstanceInfoText(self.instance), False)
6341

    
6342
    # Step 4: dbrd minors and drbd setups changes
6343
    # after this, we must manually remove the drbd minors on both the
6344
    # error and the success paths
6345
    self.lu.LogStep(4, steps_total, "Changing drbd configuration")
6346
    minors = self.cfg.AllocateDRBDMinor([self.new_node for dev in self.instance.disks],
6347
                                        self.instance.name)
6348
    logging.debug("Allocated minors %r" % (minors,))
6349

    
6350
    iv_names = {}
6351
    for idx, (dev, new_minor) in enumerate(zip(self.instance.disks, minors)):
6352
      self.lu.LogInfo("activating a new drbd on %s for disk/%d" % (self.new_node, idx))
6353
      # create new devices on new_node; note that we create two IDs:
6354
      # one without port, so the drbd will be activated without
6355
      # networking information on the new node at this stage, and one
6356
      # with network, for the latter activation in step 4
6357
      (o_node1, o_node2, o_port, o_minor1, o_minor2, o_secret) = dev.logical_id
6358
      if self.instance.primary_node == o_node1:
6359
        p_minor = o_minor1
6360
      else:
6361
        p_minor = o_minor2
6362

    
6363
      new_alone_id = (self.instance.primary_node, self.new_node, None, p_minor, new_minor, o_secret)
6364
      new_net_id = (self.instance.primary_node, self.new_node, o_port, p_minor, new_minor, o_secret)
6365

    
6366
      iv_names[idx] = (dev, dev.children, new_net_id)
6367
      logging.debug("Allocated new_minor: %s, new_logical_id: %s", new_minor,
6368
                    new_net_id)
6369
      new_drbd = objects.Disk(dev_type=constants.LD_DRBD8,
6370
                              logical_id=new_alone_id,
6371
                              children=dev.children,
6372
                              size=dev.size)
6373
      try:
6374
        _CreateSingleBlockDev(self.lu, self.new_node, self.instance, new_drbd,
6375
                              _GetInstanceInfoText(self.instance), False)
6376
      except errors.GenericError:
6377
        self.cfg.ReleaseDRBDMinors(self.instance.name)
6378
        raise
6379

    
6380
    # We have new devices, shutdown the drbd on the old secondary
6381
    for idx, dev in enumerate(self.instance.disks):
6382
      self.lu.LogInfo("Shutting down drbd for disk/%d on old node" % idx)
6383
      self.cfg.SetDiskID(dev, self.target_node)
6384
      msg = self.rpc.call_blockdev_shutdown(self.target_node, dev).fail_msg
6385
      if msg:
6386
        self.lu.LogWarning("Failed to shutdown drbd for disk/%d on old"
6387
                           "node: %s" % (idx, msg),
6388
                           hint=("Please cleanup this device manually as"
6389
                                 " soon as possible"))
6390

    
6391
    self.lu.LogInfo("Detaching primary drbds from the network (=> standalone)")
6392
    result = self.rpc.call_drbd_disconnect_net([self.instance.primary_node], self.node_secondary_ip,
6393
                                               self.instance.disks)[self.instance.primary_node]
6394

    
6395
    msg = result.fail_msg
6396
    if msg:
6397
      # detaches didn't succeed (unlikely)
6398
      self.cfg.ReleaseDRBDMinors(self.instance.name)
6399
      raise errors.OpExecError("Can't detach the disks from the network on"
6400
                               " old node: %s" % (msg,))
6401

    
6402
    # if we managed to detach at least one, we update all the disks of
6403
    # the instance to point to the new secondary
6404
    self.lu.LogInfo("Updating instance configuration")
6405
    for dev, _, new_logical_id in iv_names.itervalues():
6406
      dev.logical_id = new_logical_id
6407
      self.cfg.SetDiskID(dev, self.instance.primary_node)
6408

    
6409
    self.cfg.Update(self.instance)
6410

    
6411
    # and now perform the drbd attach
6412
    self.lu.LogInfo("Attaching primary drbds to new secondary"
6413
                    " (standalone => connected)")
6414
    result = self.rpc.call_drbd_attach_net([self.instance.primary_node, self.new_node], self.node_secondary_ip,
6415
                                           self.instance.disks, self.instance.name,
6416
                                           False)
6417
    for to_node, to_result in result.items():
6418
      msg = to_result.fail_msg
6419
      if msg:
6420
        self.lu.LogWarning("Can't attach drbd disks on node %s: %s", to_node, msg,
6421
                           hint=("please do a gnt-instance info to see the"
6422
                                 " status of disks"))
6423

    
6424
    # Wait for sync
6425
    # This can fail as the old devices are degraded and _WaitForSync
6426
    # does a combined result over all disks, so we don't check its return value
6427
    self.lu.LogStep(5, steps_total, "Sync devices")
6428
    _WaitForSync(self.lu, self.instance, unlock=True)
6429

    
6430
    # Check all devices manually
6431
    self._CheckDevices(self.instance.primary_node, iv_names)
6432

    
6433
    # Step: remove old storage
6434
    self.lu.LogStep(6, steps_total, "Removing old storage")
6435
    self._RemoveOldStorage(self.target_node, iv_names)
6436

    
6437

    
6438
class LUGrowDisk(LogicalUnit):
6439
  """Grow a disk of an instance.
6440

6441
  """
6442
  HPATH = "disk-grow"
6443
  HTYPE = constants.HTYPE_INSTANCE
6444
  _OP_REQP = ["instance_name", "disk", "amount", "wait_for_sync"]
6445
  REQ_BGL = False
6446

    
6447
  def ExpandNames(self):
6448
    self._ExpandAndLockInstance()
6449
    self.needed_locks[locking.LEVEL_NODE] = []
6450
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6451

    
6452
  def DeclareLocks(self, level):
6453
    if level == locking.LEVEL_NODE:
6454
      self._LockInstancesNodes()
6455

    
6456
  def BuildHooksEnv(self):
6457
    """Build hooks env.
6458

6459
    This runs on the master, the primary and all the secondaries.
6460

6461
    """
6462
    env = {
6463
      "DISK": self.op.disk,
6464
      "AMOUNT": self.op.amount,
6465
      }
6466
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
6467
    nl = [
6468
      self.cfg.GetMasterNode(),
6469
      self.instance.primary_node,
6470
      ]
6471
    return env, nl, nl
6472

    
6473
  def CheckPrereq(self):
6474
    """Check prerequisites.
6475

6476
    This checks that the instance is in the cluster.
6477

6478
    """
6479
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6480
    assert instance is not None, \
6481
      "Cannot retrieve locked instance %s" % self.op.instance_name
6482
    nodenames = list(instance.all_nodes)
6483
    for node in nodenames:
6484
      _CheckNodeOnline(self, node)
6485

    
6486

    
6487
    self.instance = instance
6488

    
6489
    if instance.disk_template not in (constants.DT_PLAIN, constants.DT_DRBD8):
6490
      raise errors.OpPrereqError("Instance's disk layout does not support"
6491
                                 " growing.")
6492

    
6493
    self.disk = instance.FindDisk(self.op.disk)
6494

    
6495
    nodeinfo = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
6496
                                       instance.hypervisor)
6497
    for node in nodenames:
6498
      info = nodeinfo[node]
6499
      info.Raise("Cannot get current information from node %s" % node)
6500
      vg_free = info.payload.get('vg_free', None)
6501
      if not isinstance(vg_free, int):
6502
        raise errors.OpPrereqError("Can't compute free disk space on"
6503
                                   " node %s" % node)
6504
      if self.op.amount > vg_free:
6505
        raise errors.OpPrereqError("Not enough disk space on target node %s:"
6506
                                   " %d MiB available, %d MiB required" %
6507
                                   (node, vg_free, self.op.amount))
6508

    
6509
  def Exec(self, feedback_fn):
6510
    """Execute disk grow.
6511

6512
    """
6513
    instance = self.instance
6514
    disk = self.disk
6515
    for node in instance.all_nodes:
6516
      self.cfg.SetDiskID(disk, node)
6517
      result = self.rpc.call_blockdev_grow(node, disk, self.op.amount)
6518
      result.Raise("Grow request failed to node %s" % node)
6519
    disk.RecordGrow(self.op.amount)
6520
    self.cfg.Update(instance)
6521
    if self.op.wait_for_sync:
6522
      disk_abort = not _WaitForSync(self, instance)
6523
      if disk_abort:
6524
        self.proc.LogWarning("Warning: disk sync-ing has not returned a good"
6525
                             " status.\nPlease check the instance.")
6526

    
6527

    
6528
class LUQueryInstanceData(NoHooksLU):
6529
  """Query runtime instance data.
6530

6531
  """
6532
  _OP_REQP = ["instances", "static"]
6533
  REQ_BGL = False
6534

    
6535
  def ExpandNames(self):
6536
    self.needed_locks = {}
6537
    self.share_locks = dict.fromkeys(locking.LEVELS, 1)
6538

    
6539
    if not isinstance(self.op.instances, list):
6540
      raise errors.OpPrereqError("Invalid argument type 'instances'")
6541

    
6542
    if self.op.instances:
6543
      self.wanted_names = []
6544
      for name in self.op.instances:
6545
        full_name = self.cfg.ExpandInstanceName(name)
6546
        if full_name is None:
6547
          raise errors.OpPrereqError("Instance '%s' not known" % name)
6548
        self.wanted_names.append(full_name)
6549
      self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted_names
6550
    else:
6551
      self.wanted_names = None
6552
      self.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
6553

    
6554
    self.needed_locks[locking.LEVEL_NODE] = []
6555
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6556

    
6557
  def DeclareLocks(self, level):
6558
    if level == locking.LEVEL_NODE:
6559
      self._LockInstancesNodes()
6560

    
6561
  def CheckPrereq(self):
6562
    """Check prerequisites.
6563

6564
    This only checks the optional instance list against the existing names.
6565

6566
    """
6567
    if self.wanted_names is None:
6568
      self.wanted_names = self.acquired_locks[locking.LEVEL_INSTANCE]
6569

    
6570
    self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
6571
                             in self.wanted_names]
6572
    return
6573

    
6574
  def _ComputeBlockdevStatus(self, node, instance_name, dev):
6575
    """Returns the status of a block device
6576

6577
    """
6578
    if self.op.static:
6579
      return None
6580

    
6581
    self.cfg.SetDiskID(dev, node)
6582

    
6583
    result = self.rpc.call_blockdev_find(node, dev)
6584
    if result.offline:
6585
      return None
6586

    
6587
    result.Raise("Can't compute disk status for %s" % instance_name)
6588

    
6589
    status = result.payload
6590
    if status is None:
6591
      return None
6592

    
6593
    return (status.dev_path, status.major, status.minor,
6594
            status.sync_percent, status.estimated_time,
6595
            status.is_degraded, status.ldisk_status)
6596

    
6597
  def _ComputeDiskStatus(self, instance, snode, dev):
6598
    """Compute block device status.
6599

6600
    """
6601
    if dev.dev_type in constants.LDS_DRBD:
6602
      # we change the snode then (otherwise we use the one passed in)
6603
      if dev.logical_id[0] == instance.primary_node:
6604
        snode = dev.logical_id[1]
6605
      else:
6606
        snode = dev.logical_id[0]
6607

    
6608
    dev_pstatus = self._ComputeBlockdevStatus(instance.primary_node,
6609
                                              instance.name, dev)
6610
    dev_sstatus = self._ComputeBlockdevStatus(snode, instance.name, dev)
6611

    
6612
    if dev.children:
6613
      dev_children = [self._ComputeDiskStatus(instance, snode, child)
6614
                      for child in dev.children]
6615
    else:
6616
      dev_children = []
6617

    
6618
    data = {
6619
      "iv_name": dev.iv_name,
6620
      "dev_type": dev.dev_type,
6621
      "logical_id": dev.logical_id,
6622
      "physical_id": dev.physical_id,
6623
      "pstatus": dev_pstatus,
6624
      "sstatus": dev_sstatus,
6625
      "children": dev_children,
6626
      "mode": dev.mode,
6627
      "size": dev.size,
6628
      }
6629

    
6630
    return data
6631

    
6632
  def Exec(self, feedback_fn):
6633
    """Gather and return data"""
6634
    result = {}
6635

    
6636
    cluster = self.cfg.GetClusterInfo()
6637

    
6638
    for instance in self.wanted_instances:
6639
      if not self.op.static:
6640
        remote_info = self.rpc.call_instance_info(instance.primary_node,
6641
                                                  instance.name,
6642
                                                  instance.hypervisor)
6643
        remote_info.Raise("Error checking node %s" % instance.primary_node)
6644
        remote_info = remote_info.payload
6645
        if remote_info and "state" in remote_info:
6646
          remote_state = "up"
6647
        else:
6648
          remote_state = "down"
6649
      else:
6650
        remote_state = None
6651
      if instance.admin_up:
6652
        config_state = "up"
6653
      else:
6654
        config_state = "down"
6655

    
6656
      disks = [self._ComputeDiskStatus(instance, None, device)
6657
               for device in instance.disks]
6658

    
6659
      idict = {
6660
        "name": instance.name,
6661
        "config_state": config_state,
6662
        "run_state": remote_state,
6663
        "pnode": instance.primary_node,
6664
        "snodes": instance.secondary_nodes,
6665
        "os": instance.os,
6666
        # this happens to be the same format used for hooks
6667
        "nics": _NICListToTuple(self, instance.nics),
6668
        "disks": disks,
6669
        "hypervisor": instance.hypervisor,
6670
        "network_port": instance.network_port,
6671
        "hv_instance": instance.hvparams,
6672
        "hv_actual": cluster.FillHV(instance),
6673
        "be_instance": instance.beparams,
6674
        "be_actual": cluster.FillBE(instance),
6675
        }
6676

    
6677
      result[instance.name] = idict
6678

    
6679
    return result
6680

    
6681

    
6682
class LUSetInstanceParams(LogicalUnit):
6683
  """Modifies an instances's parameters.
6684

6685
  """
6686
  HPATH = "instance-modify"
6687
  HTYPE = constants.HTYPE_INSTANCE
6688
  _OP_REQP = ["instance_name"]
6689
  REQ_BGL = False
6690

    
6691
  def CheckArguments(self):
6692
    if not hasattr(self.op, 'nics'):
6693
      self.op.nics = []
6694
    if not hasattr(self.op, 'disks'):
6695
      self.op.disks = []
6696
    if not hasattr(self.op, 'beparams'):
6697
      self.op.beparams = {}
6698
    if not hasattr(self.op, 'hvparams'):
6699
      self.op.hvparams = {}
6700
    self.op.force = getattr(self.op, "force", False)
6701
    if not (self.op.nics or self.op.disks or
6702
            self.op.hvparams or self.op.beparams):
6703
      raise errors.OpPrereqError("No changes submitted")
6704

    
6705
    # Disk validation
6706
    disk_addremove = 0
6707
    for disk_op, disk_dict in self.op.disks:
6708
      if disk_op == constants.DDM_REMOVE:
6709
        disk_addremove += 1
6710
        continue
6711
      elif disk_op == constants.DDM_ADD:
6712
        disk_addremove += 1
6713
      else:
6714
        if not isinstance(disk_op, int):
6715
          raise errors.OpPrereqError("Invalid disk index")
6716
        if not isinstance(disk_dict, dict):
6717
          msg = "Invalid disk value: expected dict, got '%s'" % disk_dict
6718
          raise errors.OpPrereqError(msg)
6719

    
6720
      if disk_op == constants.DDM_ADD:
6721
        mode = disk_dict.setdefault('mode', constants.DISK_RDWR)
6722
        if mode not in constants.DISK_ACCESS_SET:
6723
          raise errors.OpPrereqError("Invalid disk access mode '%s'" % mode)
6724
        size = disk_dict.get('size', None)
6725
        if size is None:
6726
          raise errors.OpPrereqError("Required disk parameter size missing")
6727
        try:
6728
          size = int(size)
6729
        except ValueError, err:
6730
          raise errors.OpPrereqError("Invalid disk size parameter: %s" %
6731
                                     str(err))
6732
        disk_dict['size'] = size
6733
      else:
6734
        # modification of disk
6735
        if 'size' in disk_dict:
6736
          raise errors.OpPrereqError("Disk size change not possible, use"
6737
                                     " grow-disk")
6738

    
6739
    if disk_addremove > 1:
6740
      raise errors.OpPrereqError("Only one disk add or remove operation"
6741
                                 " supported at a time")
6742

    
6743
    # NIC validation
6744
    nic_addremove = 0
6745
    for nic_op, nic_dict in self.op.nics:
6746
      if nic_op == constants.DDM_REMOVE:
6747
        nic_addremove += 1
6748
        continue
6749
      elif nic_op == constants.DDM_ADD:
6750
        nic_addremove += 1
6751
      else:
6752
        if not isinstance(nic_op, int):
6753
          raise errors.OpPrereqError("Invalid nic index")
6754
        if not isinstance(nic_dict, dict):
6755
          msg = "Invalid nic value: expected dict, got '%s'" % nic_dict
6756
          raise errors.OpPrereqError(msg)
6757

    
6758
      # nic_dict should be a dict
6759
      nic_ip = nic_dict.get('ip', None)
6760
      if nic_ip is not None:
6761
        if nic_ip.lower() == constants.VALUE_NONE:
6762
          nic_dict['ip'] = None
6763
        else:
6764
          if not utils.IsValidIP(nic_ip):
6765
            raise errors.OpPrereqError("Invalid IP address '%s'" % nic_ip)
6766

    
6767
      nic_bridge = nic_dict.get('bridge', None)
6768
      nic_link = nic_dict.get('link', None)
6769
      if nic_bridge and nic_link:
6770
        raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'"
6771
                                   " at the same time")
6772
      elif nic_bridge and nic_bridge.lower() == constants.VALUE_NONE:
6773
        nic_dict['bridge'] = None
6774
      elif nic_link and nic_link.lower() == constants.VALUE_NONE:
6775
        nic_dict['link'] = None
6776

    
6777
      if nic_op == constants.DDM_ADD:
6778
        nic_mac = nic_dict.get('mac', None)
6779
        if nic_mac is None:
6780
          nic_dict['mac'] = constants.VALUE_AUTO
6781

    
6782
      if 'mac' in nic_dict:
6783
        nic_mac = nic_dict['mac']
6784
        if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
6785
          if not utils.IsValidMac(nic_mac):
6786
            raise errors.OpPrereqError("Invalid MAC address %s" % nic_mac)
6787
        if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO:
6788
          raise errors.OpPrereqError("'auto' is not a valid MAC address when"
6789
                                     " modifying an existing nic")
6790

    
6791
    if nic_addremove > 1:
6792
      raise errors.OpPrereqError("Only one NIC add or remove operation"
6793
                                 " supported at a time")
6794

    
6795
  def ExpandNames(self):
6796
    self._ExpandAndLockInstance()
6797
    self.needed_locks[locking.LEVEL_NODE] = []
6798
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
6799

    
6800
  def DeclareLocks(self, level):
6801
    if level == locking.LEVEL_NODE:
6802
      self._LockInstancesNodes()
6803

    
6804
  def BuildHooksEnv(self):
6805
    """Build hooks env.
6806

6807
    This runs on the master, primary and secondaries.
6808

6809
    """
6810
    args = dict()
6811
    if constants.BE_MEMORY in self.be_new:
6812
      args['memory'] = self.be_new[constants.BE_MEMORY]
6813
    if constants.BE_VCPUS in self.be_new:
6814
      args['vcpus'] = self.be_new[constants.BE_VCPUS]
6815
    # TODO: export disk changes. Note: _BuildInstanceHookEnv* don't export disk
6816
    # information at all.
6817
    if self.op.nics:
6818
      args['nics'] = []
6819
      nic_override = dict(self.op.nics)
6820
      c_nicparams = self.cluster.nicparams[constants.PP_DEFAULT]
6821
      for idx, nic in enumerate(self.instance.nics):
6822
        if idx in nic_override:
6823
          this_nic_override = nic_override[idx]
6824
        else:
6825
          this_nic_override = {}
6826
        if 'ip' in this_nic_override:
6827
          ip = this_nic_override['ip']
6828
        else:
6829
          ip = nic.ip
6830
        if 'mac' in this_nic_override:
6831
          mac = this_nic_override['mac']
6832
        else:
6833
          mac = nic.mac
6834
        if idx in self.nic_pnew:
6835
          nicparams = self.nic_pnew[idx]
6836
        else:
6837
          nicparams = objects.FillDict(c_nicparams, nic.nicparams)
6838
        mode = nicparams[constants.NIC_MODE]
6839
        link = nicparams[constants.NIC_LINK]
6840
        args['nics'].append((ip, mac, mode, link))
6841
      if constants.DDM_ADD in nic_override:
6842
        ip = nic_override[constants.DDM_ADD].get('ip', None)
6843
        mac = nic_override[constants.DDM_ADD]['mac']
6844
        nicparams = self.nic_pnew[constants.DDM_ADD]
6845
        mode = nicparams[constants.NIC_MODE]
6846
        link = nicparams[constants.NIC_LINK]
6847
        args['nics'].append((ip, mac, mode, link))
6848
      elif constants.DDM_REMOVE in nic_override:
6849
        del args['nics'][-1]
6850

    
6851
    env = _BuildInstanceHookEnvByObject(self, self.instance, override=args)
6852
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
6853
    return env, nl, nl
6854

    
6855
  def _GetUpdatedParams(self, old_params, update_dict,
6856
                        default_values, parameter_types):
6857
    """Return the new params dict for the given params.
6858

6859
    @type old_params: dict
6860
    @param old_params: old parameters
6861
    @type update_dict: dict
6862
    @param update_dict: dict containing new parameter values,
6863
                        or constants.VALUE_DEFAULT to reset the
6864
                        parameter to its default value
6865
    @type default_values: dict
6866
    @param default_values: default values for the filled parameters
6867
    @type parameter_types: dict
6868
    @param parameter_types: dict mapping target dict keys to types
6869
                            in constants.ENFORCEABLE_TYPES
6870
    @rtype: (dict, dict)
6871
    @return: (new_parameters, filled_parameters)
6872

6873
    """
6874
    params_copy = copy.deepcopy(old_params)
6875
    for key, val in update_dict.iteritems():
6876
      if val == constants.VALUE_DEFAULT:
6877
        try:
6878
          del params_copy[key]
6879
        except KeyError:
6880
          pass
6881
      else:
6882
        params_copy[key] = val
6883
    utils.ForceDictType(params_copy, parameter_types)
6884
    params_filled = objects.FillDict(default_values, params_copy)
6885
    return (params_copy, params_filled)
6886

    
6887
  def CheckPrereq(self):
6888
    """Check prerequisites.
6889

6890
    This only checks the instance list against the existing names.
6891

6892
    """
6893
    self.force = self.op.force
6894

    
6895
    # checking the new params on the primary/secondary nodes
6896

    
6897
    instance = self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
6898
    cluster = self.cluster = self.cfg.GetClusterInfo()
6899
    assert self.instance is not None, \
6900
      "Cannot retrieve locked instance %s" % self.op.instance_name
6901
    pnode = instance.primary_node
6902
    nodelist = list(instance.all_nodes)
6903

    
6904
    # hvparams processing
6905
    if self.op.hvparams:
6906
      i_hvdict, hv_new = self._GetUpdatedParams(
6907
                             instance.hvparams, self.op.hvparams,
6908
                             cluster.hvparams[instance.hypervisor],
6909
                             constants.HVS_PARAMETER_TYPES)
6910
      # local check
6911
      hypervisor.GetHypervisor(
6912
        instance.hypervisor).CheckParameterSyntax(hv_new)
6913
      _CheckHVParams(self, nodelist, instance.hypervisor, hv_new)
6914
      self.hv_new = hv_new # the new actual values
6915
      self.hv_inst = i_hvdict # the new dict (without defaults)
6916
    else:
6917
      self.hv_new = self.hv_inst = {}
6918

    
6919
    # beparams processing
6920
    if self.op.beparams:
6921
      i_bedict, be_new = self._GetUpdatedParams(
6922
                             instance.beparams, self.op.beparams,
6923
                             cluster.beparams[constants.PP_DEFAULT],
6924
                             constants.BES_PARAMETER_TYPES)
6925
      self.be_new = be_new # the new actual values
6926
      self.be_inst = i_bedict # the new dict (without defaults)
6927
    else:
6928
      self.be_new = self.be_inst = {}
6929

    
6930
    self.warn = []
6931

    
6932
    if constants.BE_MEMORY in self.op.beparams and not self.force:
6933
      mem_check_list = [pnode]
6934
      if be_new[constants.BE_AUTO_BALANCE]:
6935
        # either we changed auto_balance to yes or it was from before
6936
        mem_check_list.extend(instance.secondary_nodes)
6937
      instance_info = self.rpc.call_instance_info(pnode, instance.name,
6938
                                                  instance.hypervisor)
6939
      nodeinfo = self.rpc.call_node_info(mem_check_list, self.cfg.GetVGName(),
6940
                                         instance.hypervisor)
6941
      pninfo = nodeinfo[pnode]
6942
      msg = pninfo.fail_msg
6943
      if msg:
6944
        # Assume the primary node is unreachable and go ahead
6945
        self.warn.append("Can't get info from primary node %s: %s" %
6946
                         (pnode,  msg))
6947
      elif not isinstance(pninfo.payload.get('memory_free', None), int):
6948
        self.warn.append("Node data from primary node %s doesn't contain"
6949
                         " free memory information" % pnode)
6950
      elif instance_info.fail_msg:
6951
        self.warn.append("Can't get instance runtime information: %s" %
6952
                        instance_info.fail_msg)
6953
      else:
6954
        if instance_info.payload:
6955
          current_mem = int(instance_info.payload['memory'])
6956
        else:
6957
          # Assume instance not running
6958
          # (there is a slight race condition here, but it's not very probable,
6959
          # and we have no other way to check)
6960
          current_mem = 0
6961
        miss_mem = (be_new[constants.BE_MEMORY] - current_mem -
6962
                    pninfo.payload['memory_free'])
6963
        if miss_mem > 0:
6964
          raise errors.OpPrereqError("This change will prevent the instance"
6965
                                     " from starting, due to %d MB of memory"
6966
                                     " missing on its primary node" % miss_mem)
6967

    
6968
      if be_new[constants.BE_AUTO_BALANCE]:
6969
        for node, nres in nodeinfo.items():
6970
          if node not in instance.secondary_nodes:
6971
            continue
6972
          msg = nres.fail_msg
6973
          if msg:
6974
            self.warn.append("Can't get info from secondary node %s: %s" %
6975
                             (node, msg))
6976
          elif not isinstance(nres.payload.get('memory_free', None), int):
6977
            self.warn.append("Secondary node %s didn't return free"
6978
                             " memory information" % node)
6979
          elif be_new[constants.BE_MEMORY] > nres.payload['memory_free']:
6980
            self.warn.append("Not enough memory to failover instance to"
6981
                             " secondary node %s" % node)
6982

    
6983
    # NIC processing
6984
    self.nic_pnew = {}
6985
    self.nic_pinst = {}
6986
    for nic_op, nic_dict in self.op.nics:
6987
      if nic_op == constants.DDM_REMOVE:
6988
        if not instance.nics:
6989
          raise errors.OpPrereqError("Instance has no NICs, cannot remove")
6990
        continue
6991
      if nic_op != constants.DDM_ADD:
6992
        # an existing nic
6993
        if nic_op < 0 or nic_op >= len(instance.nics):
6994
          raise errors.OpPrereqError("Invalid NIC index %s, valid values"
6995
                                     " are 0 to %d" %
6996
                                     (nic_op, len(instance.nics)))
6997
        old_nic_params = instance.nics[nic_op].nicparams
6998
        old_nic_ip = instance.nics[nic_op].ip
6999
      else:
7000
        old_nic_params = {}
7001
        old_nic_ip = None
7002

    
7003
      update_params_dict = dict([(key, nic_dict[key])
7004
                                 for key in constants.NICS_PARAMETERS
7005
                                 if key in nic_dict])
7006

    
7007
      if 'bridge' in nic_dict:
7008
        update_params_dict[constants.NIC_LINK] = nic_dict['bridge']
7009

    
7010
      new_nic_params, new_filled_nic_params = \
7011
          self._GetUpdatedParams(old_nic_params, update_params_dict,
7012
                                 cluster.nicparams[constants.PP_DEFAULT],
7013
                                 constants.NICS_PARAMETER_TYPES)
7014
      objects.NIC.CheckParameterSyntax(new_filled_nic_params)
7015
      self.nic_pinst[nic_op] = new_nic_params
7016
      self.nic_pnew[nic_op] = new_filled_nic_params
7017
      new_nic_mode = new_filled_nic_params[constants.NIC_MODE]
7018

    
7019
      if new_nic_mode == constants.NIC_MODE_BRIDGED:
7020
        nic_bridge = new_filled_nic_params[constants.NIC_LINK]
7021
        msg = self.rpc.call_bridges_exist(pnode, [nic_bridge]).fail_msg
7022
        if msg:
7023
          msg = "Error checking bridges on node %s: %s" % (pnode, msg)
7024
          if self.force:
7025
            self.warn.append(msg)
7026
          else:
7027
            raise errors.OpPrereqError(msg)
7028
      if new_nic_mode == constants.NIC_MODE_ROUTED:
7029
        if 'ip' in nic_dict:
7030
          nic_ip = nic_dict['ip']
7031
        else:
7032
          nic_ip = old_nic_ip
7033
        if nic_ip is None:
7034
          raise errors.OpPrereqError('Cannot set the nic ip to None'
7035
                                     ' on a routed nic')
7036
      if 'mac' in nic_dict:
7037
        nic_mac = nic_dict['mac']
7038
        if nic_mac is None:
7039
          raise errors.OpPrereqError('Cannot set the nic mac to None')
7040
        elif nic_mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
7041
          # otherwise generate the mac
7042
          nic_dict['mac'] = self.cfg.GenerateMAC()
7043
        else:
7044
          # or validate/reserve the current one
7045
          if self.cfg.IsMacInUse(nic_mac):
7046
            raise errors.OpPrereqError("MAC address %s already in use"
7047
                                       " in cluster" % nic_mac)
7048

    
7049
    # DISK processing
7050
    if self.op.disks and instance.disk_template == constants.DT_DISKLESS:
7051
      raise errors.OpPrereqError("Disk operations not supported for"
7052
                                 " diskless instances")
7053
    for disk_op, disk_dict in self.op.disks:
7054
      if disk_op == constants.DDM_REMOVE:
7055
        if len(instance.disks) == 1:
7056
          raise errors.OpPrereqError("Cannot remove the last disk of"
7057
                                     " an instance")
7058
        ins_l = self.rpc.call_instance_list([pnode], [instance.hypervisor])
7059
        ins_l = ins_l[pnode]
7060
        msg = ins_l.fail_msg
7061
        if msg:
7062
          raise errors.OpPrereqError("Can't contact node %s: %s" %
7063
                                     (pnode, msg))
7064
        if instance.name in ins_l.payload:
7065
          raise errors.OpPrereqError("Instance is running, can't remove"
7066
                                     " disks.")
7067

    
7068
      if (disk_op == constants.DDM_ADD and
7069
          len(instance.nics) >= constants.MAX_DISKS):
7070
        raise errors.OpPrereqError("Instance has too many disks (%d), cannot"
7071
                                   " add more" % constants.MAX_DISKS)
7072
      if disk_op not in (constants.DDM_ADD, constants.DDM_REMOVE):
7073
        # an existing disk
7074
        if disk_op < 0 or disk_op >= len(instance.disks):
7075
          raise errors.OpPrereqError("Invalid disk index %s, valid values"
7076
                                     " are 0 to %d" %
7077
                                     (disk_op, len(instance.disks)))
7078

    
7079
    return
7080

    
7081
  def Exec(self, feedback_fn):
7082
    """Modifies an instance.
7083

7084
    All parameters take effect only at the next restart of the instance.
7085

7086
    """
7087
    # Process here the warnings from CheckPrereq, as we don't have a
7088
    # feedback_fn there.
7089
    for warn in self.warn:
7090
      feedback_fn("WARNING: %s" % warn)
7091

    
7092
    result = []
7093
    instance = self.instance
7094
    cluster = self.cluster
7095
    # disk changes
7096
    for disk_op, disk_dict in self.op.disks:
7097
      if disk_op == constants.DDM_REMOVE:
7098
        # remove the last disk
7099
        device = instance.disks.pop()
7100
        device_idx = len(instance.disks)
7101
        for node, disk in device.ComputeNodeTree(instance.primary_node):
7102
          self.cfg.SetDiskID(disk, node)
7103
          msg = self.rpc.call_blockdev_remove(node, disk).fail_msg
7104
          if msg:
7105
            self.LogWarning("Could not remove disk/%d on node %s: %s,"
7106
                            " continuing anyway", device_idx, node, msg)
7107
        result.append(("disk/%d" % device_idx, "remove"))
7108
      elif disk_op == constants.DDM_ADD:
7109
        # add a new disk
7110
        if instance.disk_template == constants.DT_FILE:
7111
          file_driver, file_path = instance.disks[0].logical_id
7112
          file_path = os.path.dirname(file_path)
7113
        else:
7114
          file_driver = file_path = None
7115
        disk_idx_base = len(instance.disks)
7116
        new_disk = _GenerateDiskTemplate(self,
7117
                                         instance.disk_template,
7118
                                         instance.name, instance.primary_node,
7119
                                         instance.secondary_nodes,
7120
                                         [disk_dict],
7121
                                         file_path,
7122
                                         file_driver,
7123
                                         disk_idx_base)[0]
7124
        instance.disks.append(new_disk)
7125
        info = _GetInstanceInfoText(instance)
7126

    
7127
        logging.info("Creating volume %s for instance %s",
7128
                     new_disk.iv_name, instance.name)
7129
        # Note: this needs to be kept in sync with _CreateDisks
7130
        #HARDCODE
7131
        for node in instance.all_nodes:
7132
          f_create = node == instance.primary_node
7133
          try:
7134
            _CreateBlockDev(self, node, instance, new_disk,
7135
                            f_create, info, f_create)
7136
          except errors.OpExecError, err:
7137
            self.LogWarning("Failed to create volume %s (%s) on"
7138
                            " node %s: %s",
7139
                            new_disk.iv_name, new_disk, node, err)
7140
        result.append(("disk/%d" % disk_idx_base, "add:size=%s,mode=%s" %
7141
                       (new_disk.size, new_disk.mode)))
7142
      else:
7143
        # change a given disk
7144
        instance.disks[disk_op].mode = disk_dict['mode']
7145
        result.append(("disk.mode/%d" % disk_op, disk_dict['mode']))
7146
    # NIC changes
7147
    for nic_op, nic_dict in self.op.nics:
7148
      if nic_op == constants.DDM_REMOVE:
7149
        # remove the last nic
7150
        del instance.nics[-1]
7151
        result.append(("nic.%d" % len(instance.nics), "remove"))
7152
      elif nic_op == constants.DDM_ADD:
7153
        # mac and bridge should be set, by now
7154
        mac = nic_dict['mac']
7155
        ip = nic_dict.get('ip', None)
7156
        nicparams = self.nic_pinst[constants.DDM_ADD]
7157
        new_nic = objects.NIC(mac=mac, ip=ip, nicparams=nicparams)
7158
        instance.nics.append(new_nic)
7159
        result.append(("nic.%d" % (len(instance.nics) - 1),
7160
                       "add:mac=%s,ip=%s,mode=%s,link=%s" %
7161
                       (new_nic.mac, new_nic.ip,
7162
                        self.nic_pnew[constants.DDM_ADD][constants.NIC_MODE],
7163
                        self.nic_pnew[constants.DDM_ADD][constants.NIC_LINK]
7164
                       )))
7165
      else:
7166
        for key in 'mac', 'ip':
7167
          if key in nic_dict:
7168
            setattr(instance.nics[nic_op], key, nic_dict[key])
7169
        if nic_op in self.nic_pnew:
7170
          instance.nics[nic_op].nicparams = self.nic_pnew[nic_op]
7171
        for key, val in nic_dict.iteritems():
7172
          result.append(("nic.%s/%d" % (key, nic_op), val))
7173

    
7174
    # hvparams changes
7175
    if self.op.hvparams:
7176
      instance.hvparams = self.hv_inst
7177
      for key, val in self.op.hvparams.iteritems():
7178
        result.append(("hv/%s" % key, val))
7179

    
7180
    # beparams changes
7181
    if self.op.beparams:
7182
      instance.beparams = self.be_inst
7183
      for key, val in self.op.beparams.iteritems():
7184
        result.append(("be/%s" % key, val))
7185

    
7186
    self.cfg.Update(instance)
7187

    
7188
    return result
7189

    
7190

    
7191
class LUQueryExports(NoHooksLU):
7192
  """Query the exports list
7193

7194
  """
7195
  _OP_REQP = ['nodes']
7196
  REQ_BGL = False
7197

    
7198
  def ExpandNames(self):
7199
    self.needed_locks = {}
7200
    self.share_locks[locking.LEVEL_NODE] = 1
7201
    if not self.op.nodes:
7202
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
7203
    else:
7204
      self.needed_locks[locking.LEVEL_NODE] = \
7205
        _GetWantedNodes(self, self.op.nodes)
7206

    
7207
  def CheckPrereq(self):
7208
    """Check prerequisites.
7209

7210
    """
7211
    self.nodes = self.acquired_locks[locking.LEVEL_NODE]
7212

    
7213
  def Exec(self, feedback_fn):
7214
    """Compute the list of all the exported system images.
7215

7216
    @rtype: dict
7217
    @return: a dictionary with the structure node->(export-list)
7218
        where export-list is a list of the instances exported on
7219
        that node.
7220

7221
    """
7222
    rpcresult = self.rpc.call_export_list(self.nodes)
7223
    result = {}
7224
    for node in rpcresult:
7225
      if rpcresult[node].fail_msg:
7226
        result[node] = False
7227
      else:
7228
        result[node] = rpcresult[node].payload
7229

    
7230
    return result
7231

    
7232

    
7233
class LUExportInstance(LogicalUnit):
7234
  """Export an instance to an image in the cluster.
7235

7236
  """
7237
  HPATH = "instance-export"
7238
  HTYPE = constants.HTYPE_INSTANCE
7239
  _OP_REQP = ["instance_name", "target_node", "shutdown"]
7240
  REQ_BGL = False
7241

    
7242
  def ExpandNames(self):
7243
    self._ExpandAndLockInstance()
7244
    # FIXME: lock only instance primary and destination node
7245
    #
7246
    # Sad but true, for now we have do lock all nodes, as we don't know where
7247
    # the previous export might be, and and in this LU we search for it and
7248
    # remove it from its current node. In the future we could fix this by:
7249
    #  - making a tasklet to search (share-lock all), then create the new one,
7250
    #    then one to remove, after
7251
    #  - removing the removal operation altogether
7252
    self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
7253

    
7254
  def DeclareLocks(self, level):
7255
    """Last minute lock declaration."""
7256
    # All nodes are locked anyway, so nothing to do here.
7257

    
7258
  def BuildHooksEnv(self):
7259
    """Build hooks env.
7260

7261
    This will run on the master, primary node and target node.
7262

7263
    """
7264
    env = {
7265
      "EXPORT_NODE": self.op.target_node,
7266
      "EXPORT_DO_SHUTDOWN": self.op.shutdown,
7267
      }
7268
    env.update(_BuildInstanceHookEnvByObject(self, self.instance))
7269
    nl = [self.cfg.GetMasterNode(), self.instance.primary_node,
7270
          self.op.target_node]
7271
    return env, nl, nl
7272

    
7273
  def CheckPrereq(self):
7274
    """Check prerequisites.
7275

7276
    This checks that the instance and node names are valid.
7277

7278
    """
7279
    instance_name = self.op.instance_name
7280
    self.instance = self.cfg.GetInstanceInfo(instance_name)
7281
    assert self.instance is not None, \
7282
          "Cannot retrieve locked instance %s" % self.op.instance_name
7283
    _CheckNodeOnline(self, self.instance.primary_node)
7284

    
7285
    self.dst_node = self.cfg.GetNodeInfo(
7286
      self.cfg.ExpandNodeName(self.op.target_node))
7287

    
7288
    if self.dst_node is None:
7289
      # This is wrong node name, not a non-locked node
7290
      raise errors.OpPrereqError("Wrong node name %s" % self.op.target_node)
7291
    _CheckNodeOnline(self, self.dst_node.name)
7292
    _CheckNodeNotDrained(self, self.dst_node.name)
7293

    
7294
    # instance disk type verification
7295
    for disk in self.instance.disks:
7296
      if disk.dev_type == constants.LD_FILE:
7297
        raise errors.OpPrereqError("Export not supported for instances with"
7298
                                   " file-based disks")
7299

    
7300
  def Exec(self, feedback_fn):
7301
    """Export an instance to an image in the cluster.
7302

7303
    """
7304
    instance = self.instance
7305
    dst_node = self.dst_node
7306
    src_node = instance.primary_node
7307
    if self.op.shutdown:
7308
      # shutdown the instance, but not the disks
7309
      result = self.rpc.call_instance_shutdown(src_node, instance)
7310
      result.Raise("Could not shutdown instance %s on"
7311
                   " node %s" % (instance.name, src_node))
7312

    
7313
    vgname = self.cfg.GetVGName()
7314

    
7315
    snap_disks = []
7316

    
7317
    # set the disks ID correctly since call_instance_start needs the
7318
    # correct drbd minor to create the symlinks
7319
    for disk in instance.disks:
7320
      self.cfg.SetDiskID(disk, src_node)
7321

    
7322
    # per-disk results
7323
    dresults = []
7324
    try:
7325
      for idx, disk in enumerate(instance.disks):
7326
        # result.payload will be a snapshot of an lvm leaf of the one we passed
7327
        result = self.rpc.call_blockdev_snapshot(src_node, disk)
7328
        msg = result.fail_msg
7329
        if msg:
7330
          self.LogWarning("Could not snapshot disk/%s on node %s: %s",
7331
                          idx, src_node, msg)
7332
          snap_disks.append(False)
7333
        else:
7334
          disk_id = (vgname, result.payload)
7335
          new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size,
7336
                                 logical_id=disk_id, physical_id=disk_id,
7337
                                 iv_name=disk.iv_name)
7338
          snap_disks.append(new_dev)
7339

    
7340
    finally:
7341
      if self.op.shutdown and instance.admin_up:
7342
        result = self.rpc.call_instance_start(src_node, instance, None, None)
7343
        msg = result.fail_msg
7344
        if msg:
7345
          _ShutdownInstanceDisks(self, instance)
7346
          raise errors.OpExecError("Could not start instance: %s" % msg)
7347

    
7348
    # TODO: check for size
7349

    
7350
    cluster_name = self.cfg.GetClusterName()
7351
    for idx, dev in enumerate(snap_disks):
7352
      if dev:
7353
        result = self.rpc.call_snapshot_export(src_node, dev, dst_node.name,
7354
                                               instance, cluster_name, idx)
7355
        msg = result.fail_msg
7356
        if msg:
7357
          self.LogWarning("Could not export disk/%s from node %s to"
7358
                          " node %s: %s", idx, src_node, dst_node.name, msg)
7359
          dresults.append(False)
7360
        else:
7361
          dresults.append(True)
7362
        msg = self.rpc.call_blockdev_remove(src_node, dev).fail_msg
7363
        if msg:
7364
          self.LogWarning("Could not remove snapshot for disk/%d from node"
7365
                          " %s: %s", idx, src_node, msg)
7366
      else:
7367
        dresults.append(False)
7368

    
7369
    result = self.rpc.call_finalize_export(dst_node.name, instance, snap_disks)
7370
    fin_resu = True
7371
    msg = result.fail_msg
7372
    if msg:
7373
      self.LogWarning("Could not finalize export for instance %s"
7374
                      " on node %s: %s", instance.name, dst_node.name, msg)
7375
      fin_resu = False
7376

    
7377
    nodelist = self.cfg.GetNodeList()
7378
    nodelist.remove(dst_node.name)
7379

    
7380
    # on one-node clusters nodelist will be empty after the removal
7381
    # if we proceed the backup would be removed because OpQueryExports
7382
    # substitutes an empty list with the full cluster node list.
7383
    iname = instance.name
7384
    if nodelist:
7385
      exportlist = self.rpc.call_export_list(nodelist)
7386
      for node in exportlist:
7387
        if exportlist[node].fail_msg:
7388
          continue
7389
        if iname in exportlist[node].payload:
7390
          msg = self.rpc.call_export_remove(node, iname).fail_msg
7391
          if msg:
7392
            self.LogWarning("Could not remove older export for instance %s"
7393
                            " on node %s: %s", iname, node, msg)
7394
    return fin_resu, dresults
7395

    
7396

    
7397
class LURemoveExport(NoHooksLU):
7398
  """Remove exports related to the named instance.
7399

7400
  """
7401
  _OP_REQP = ["instance_name"]
7402
  REQ_BGL = False
7403

    
7404
  def ExpandNames(self):
7405
    self.needed_locks = {}
7406
    # We need all nodes to be locked in order for RemoveExport to work, but we
7407
    # don't need to lock the instance itself, as nothing will happen to it (and
7408
    # we can remove exports also for a removed instance)
7409
    self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
7410

    
7411
  def CheckPrereq(self):
7412
    """Check prerequisites.
7413
    """
7414
    pass
7415

    
7416
  def Exec(self, feedback_fn):
7417
    """Remove any export.
7418

7419
    """
7420
    instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
7421
    # If the instance was not found we'll try with the name that was passed in.
7422
    # This will only work if it was an FQDN, though.
7423
    fqdn_warn = False
7424
    if not instance_name:
7425
      fqdn_warn = True
7426
      instance_name = self.op.instance_name
7427

    
7428
    locked_nodes = self.acquired_locks[locking.LEVEL_NODE]
7429
    exportlist = self.rpc.call_export_list(locked_nodes)
7430
    found = False
7431
    for node in exportlist:
7432
      msg = exportlist[node].fail_msg
7433
      if msg:
7434
        self.LogWarning("Failed to query node %s (continuing): %s", node, msg)
7435
        continue
7436
      if instance_name in exportlist[node].payload:
7437
        found = True
7438
        result = self.rpc.call_export_remove(node, instance_name)
7439
        msg = result.fail_msg
7440
        if msg:
7441
          logging.error("Could not remove export for instance %s"
7442
                        " on node %s: %s", instance_name, node, msg)
7443

    
7444
    if fqdn_warn and not found:
7445
      feedback_fn("Export not found. If trying to remove an export belonging"
7446
                  " to a deleted instance please use its Fully Qualified"
7447
                  " Domain Name.")
7448

    
7449

    
7450
class TagsLU(NoHooksLU):
7451
  """Generic tags LU.
7452

7453
  This is an abstract class which is the parent of all the other tags LUs.
7454

7455
  """
7456

    
7457
  def ExpandNames(self):
7458
    self.needed_locks = {}
7459
    if self.op.kind == constants.TAG_NODE:
7460
      name = self.cfg.ExpandNodeName(self.op.name)
7461
      if name is None:
7462
        raise errors.OpPrereqError("Invalid node name (%s)" %
7463
                                   (self.op.name,))
7464
      self.op.name = name
7465
      self.needed_locks[locking.LEVEL_NODE] = name
7466
    elif self.op.kind == constants.TAG_INSTANCE:
7467
      name = self.cfg.ExpandInstanceName(self.op.name)
7468
      if name is None:
7469
        raise errors.OpPrereqError("Invalid instance name (%s)" %
7470
                                   (self.op.name,))
7471
      self.op.name = name
7472
      self.needed_locks[locking.LEVEL_INSTANCE] = name
7473

    
7474
  def CheckPrereq(self):
7475
    """Check prerequisites.
7476

7477
    """
7478
    if self.op.kind == constants.TAG_CLUSTER:
7479
      self.target = self.cfg.GetClusterInfo()
7480
    elif self.op.kind == constants.TAG_NODE:
7481
      self.target = self.cfg.GetNodeInfo(self.op.name)
7482
    elif self.op.kind == constants.TAG_INSTANCE:
7483
      self.target = self.cfg.GetInstanceInfo(self.op.name)
7484
    else:
7485
      raise errors.OpPrereqError("Wrong tag type requested (%s)" %
7486
                                 str(self.op.kind))
7487

    
7488

    
7489
class LUGetTags(TagsLU):
7490
  """Returns the tags of a given object.
7491

7492
  """
7493
  _OP_REQP = ["kind", "name"]
7494
  REQ_BGL = False
7495

    
7496
  def Exec(self, feedback_fn):
7497
    """Returns the tag list.
7498

7499
    """
7500
    return list(self.target.GetTags())
7501

    
7502

    
7503
class LUSearchTags(NoHooksLU):
7504
  """Searches the tags for a given pattern.
7505

7506
  """
7507
  _OP_REQP = ["pattern"]
7508
  REQ_BGL = False
7509

    
7510
  def ExpandNames(self):
7511
    self.needed_locks = {}
7512

    
7513
  def CheckPrereq(self):
7514
    """Check prerequisites.
7515

7516
    This checks the pattern passed for validity by compiling it.
7517

7518
    """
7519
    try:
7520
      self.re = re.compile(self.op.pattern)
7521
    except re.error, err:
7522
      raise errors.OpPrereqError("Invalid search pattern '%s': %s" %
7523
                                 (self.op.pattern, err))
7524

    
7525
  def Exec(self, feedback_fn):
7526
    """Returns the tag list.
7527

7528
    """
7529
    cfg = self.cfg
7530
    tgts = [("/cluster", cfg.GetClusterInfo())]
7531
    ilist = cfg.GetAllInstancesInfo().values()
7532
    tgts.extend([("/instances/%s" % i.name, i) for i in ilist])
7533
    nlist = cfg.GetAllNodesInfo().values()
7534
    tgts.extend([("/nodes/%s" % n.name, n) for n in nlist])
7535
    results = []
7536
    for path, target in tgts:
7537
      for tag in target.GetTags():
7538
        if self.re.search(tag):
7539
          results.append((path, tag))
7540
    return results
7541

    
7542

    
7543
class LUAddTags(TagsLU):
7544
  """Sets a tag on a given object.
7545

7546
  """
7547
  _OP_REQP = ["kind", "name", "tags"]
7548
  REQ_BGL = False
7549

    
7550
  def CheckPrereq(self):
7551
    """Check prerequisites.
7552

7553
    This checks the type and length of the tag name and value.
7554

7555
    """
7556
    TagsLU.CheckPrereq(self)
7557
    for tag in self.op.tags:
7558
      objects.TaggableObject.ValidateTag(tag)
7559

    
7560
  def Exec(self, feedback_fn):
7561
    """Sets the tag.
7562

7563
    """
7564
    try:
7565
      for tag in self.op.tags:
7566
        self.target.AddTag(tag)
7567
    except errors.TagError, err:
7568
      raise errors.OpExecError("Error while setting tag: %s" % str(err))
7569
    try:
7570
      self.cfg.Update(self.target)
7571
    except errors.ConfigurationError:
7572
      raise errors.OpRetryError("There has been a modification to the"
7573
                                " config file and the operation has been"
7574
                                " aborted. Please retry.")
7575

    
7576

    
7577
class LUDelTags(TagsLU):
7578
  """Delete a list of tags from a given object.
7579

7580
  """
7581
  _OP_REQP = ["kind", "name", "tags"]
7582
  REQ_BGL = False
7583

    
7584
  def CheckPrereq(self):
7585
    """Check prerequisites.
7586

7587
    This checks that we have the given tag.
7588

7589
    """
7590
    TagsLU.CheckPrereq(self)
7591
    for tag in self.op.tags:
7592
      objects.TaggableObject.ValidateTag(tag)
7593
    del_tags = frozenset(self.op.tags)
7594
    cur_tags = self.target.GetTags()
7595
    if not del_tags <= cur_tags:
7596
      diff_tags = del_tags - cur_tags
7597
      diff_names = ["'%s'" % tag for tag in diff_tags]
7598
      diff_names.sort()
7599
      raise errors.OpPrereqError("Tag(s) %s not found" %
7600
                                 (",".join(diff_names)))
7601

    
7602
  def Exec(self, feedback_fn):
7603
    """Remove the tag from the object.
7604

7605
    """
7606
    for tag in self.op.tags:
7607
      self.target.RemoveTag(tag)
7608
    try:
7609
      self.cfg.Update(self.target)
7610
    except errors.ConfigurationError:
7611
      raise errors.OpRetryError("There has been a modification to the"
7612
                                " config file and the operation has been"
7613
                                " aborted. Please retry.")
7614

    
7615

    
7616
class LUTestDelay(NoHooksLU):
7617
  """Sleep for a specified amount of time.
7618

7619
  This LU sleeps on the master and/or nodes for a specified amount of
7620
  time.
7621

7622
  """
7623
  _OP_REQP = ["duration", "on_master", "on_nodes"]
7624
  REQ_BGL = False
7625

    
7626
  def ExpandNames(self):
7627
    """Expand names and set required locks.
7628

7629
    This expands the node list, if any.
7630

7631
    """
7632
    self.needed_locks = {}
7633
    if self.op.on_nodes:
7634
      # _GetWantedNodes can be used here, but is not always appropriate to use
7635
      # this way in ExpandNames. Check LogicalUnit.ExpandNames docstring for
7636
      # more information.
7637
      self.op.on_nodes = _GetWantedNodes(self, self.op.on_nodes)
7638
      self.needed_locks[locking.LEVEL_NODE] = self.op.on_nodes
7639

    
7640
  def CheckPrereq(self):
7641
    """Check prerequisites.
7642

7643
    """
7644

    
7645
  def Exec(self, feedback_fn):
7646
    """Do the actual sleep.
7647

7648
    """
7649
    if self.op.on_master:
7650
      if not utils.TestDelay(self.op.duration):
7651
        raise errors.OpExecError("Error during master delay test")
7652
    if self.op.on_nodes:
7653
      result = self.rpc.call_test_delay(self.op.on_nodes, self.op.duration)
7654
      for node, node_result in result.items():
7655
        node_result.Raise("Failure during rpc call to node %s" % node)
7656

    
7657

    
7658
class IAllocator(object):
7659
  """IAllocator framework.
7660

7661
  An IAllocator instance has three sets of attributes:
7662
    - cfg that is needed to query the cluster
7663
    - input data (all members of the _KEYS class attribute are required)
7664
    - four buffer attributes (in|out_data|text), that represent the
7665
      input (to the external script) in text and data structure format,
7666
      and the output from it, again in two formats
7667
    - the result variables from the script (success, info, nodes) for
7668
      easy usage
7669

7670
  """
7671
  _ALLO_KEYS = [
7672
    "mem_size", "disks", "disk_template",
7673
    "os", "tags", "nics", "vcpus", "hypervisor",
7674
    ]
7675
  _RELO_KEYS = [
7676
    "relocate_from",
7677
    ]
7678

    
7679
  def __init__(self, cfg, rpc, mode, name, **kwargs):
7680
    self.cfg = cfg
7681
    self.rpc = rpc
7682
    # init buffer variables
7683
    self.in_text = self.out_text = self.in_data = self.out_data = None
7684
    # init all input fields so that pylint is happy
7685
    self.mode = mode
7686
    self.name = name
7687
    self.mem_size = self.disks = self.disk_template = None
7688
    self.os = self.tags = self.nics = self.vcpus = None
7689
    self.hypervisor = None
7690
    self.relocate_from = None
7691
    # computed fields
7692
    self.required_nodes = None
7693
    # init result fields
7694
    self.success = self.info = self.nodes = None
7695
    if self.mode == constants.IALLOCATOR_MODE_ALLOC:
7696
      keyset = self._ALLO_KEYS
7697
    elif self.mode == constants.IALLOCATOR_MODE_RELOC:
7698
      keyset = self._RELO_KEYS
7699
    else:
7700
      raise errors.ProgrammerError("Unknown mode '%s' passed to the"
7701
                                   " IAllocator" % self.mode)
7702
    for key in kwargs:
7703
      if key not in keyset:
7704
        raise errors.ProgrammerError("Invalid input parameter '%s' to"
7705
                                     " IAllocator" % key)
7706
      setattr(self, key, kwargs[key])
7707
    for key in keyset:
7708
      if key not in kwargs:
7709
        raise errors.ProgrammerError("Missing input parameter '%s' to"
7710
                                     " IAllocator" % key)
7711
    self._BuildInputData()
7712

    
7713
  def _ComputeClusterData(self):
7714
    """Compute the generic allocator input data.
7715

7716
    This is the data that is independent of the actual operation.
7717

7718
    """
7719
    cfg = self.cfg
7720
    cluster_info = cfg.GetClusterInfo()
7721
    # cluster data
7722
    data = {
7723
      "version": constants.IALLOCATOR_VERSION,
7724
      "cluster_name": cfg.GetClusterName(),
7725
      "cluster_tags": list(cluster_info.GetTags()),
7726
      "enabled_hypervisors": list(cluster_info.enabled_hypervisors),
7727
      # we don't have job IDs
7728
      }
7729
    iinfo = cfg.GetAllInstancesInfo().values()
7730
    i_list = [(inst, cluster_info.FillBE(inst)) for inst in iinfo]
7731

    
7732
    # node data
7733
    node_results = {}
7734
    node_list = cfg.GetNodeList()
7735

    
7736
    if self.mode == constants.IALLOCATOR_MODE_ALLOC:
7737
      hypervisor_name = self.hypervisor
7738
    elif self.mode == constants.IALLOCATOR_MODE_RELOC:
7739
      hypervisor_name = cfg.GetInstanceInfo(self.name).hypervisor
7740

    
7741
    node_data = self.rpc.call_node_info(node_list, cfg.GetVGName(),
7742
                                        hypervisor_name)
7743
    node_iinfo = \
7744
      self.rpc.call_all_instances_info(node_list,
7745
                                       cluster_info.enabled_hypervisors)
7746
    for nname, nresult in node_data.items():
7747
      # first fill in static (config-based) values
7748
      ninfo = cfg.GetNodeInfo(nname)
7749
      pnr = {
7750
        "tags": list(ninfo.GetTags()),
7751
        "primary_ip": ninfo.primary_ip,
7752
        "secondary_ip": ninfo.secondary_ip,
7753
        "offline": ninfo.offline,
7754
        "drained": ninfo.drained,
7755
        "master_candidate": ninfo.master_candidate,
7756
        }
7757

    
7758
      if not (ninfo.offline or ninfo.drained):
7759
        nresult.Raise("Can't get data for node %s" % nname)
7760
        node_iinfo[nname].Raise("Can't get node instance info from node %s" %
7761
                                nname)
7762
        remote_info = nresult.payload
7763

    
7764
        for attr in ['memory_total', 'memory_free', 'memory_dom0',
7765
                     'vg_size', 'vg_free', 'cpu_total']:
7766
          if attr not in remote_info:
7767
            raise errors.OpExecError("Node '%s' didn't return attribute"
7768
                                     " '%s'" % (nname, attr))
7769
          if not isinstance(remote_info[attr], int):
7770
            raise errors.OpExecError("Node '%s' returned invalid value"
7771
                                     " for '%s': %s" %
7772
                                     (nname, attr, remote_info[attr]))
7773
        # compute memory used by primary instances
7774
        i_p_mem = i_p_up_mem = 0
7775
        for iinfo, beinfo in i_list:
7776
          if iinfo.primary_node == nname:
7777
            i_p_mem += beinfo[constants.BE_MEMORY]
7778
            if iinfo.name not in node_iinfo[nname].payload:
7779
              i_used_mem = 0
7780
            else:
7781
              i_used_mem = int(node_iinfo[nname].payload[iinfo.name]['memory'])
7782
            i_mem_diff = beinfo[constants.BE_MEMORY] - i_used_mem
7783
            remote_info['memory_free'] -= max(0, i_mem_diff)
7784

    
7785
            if iinfo.admin_up:
7786
              i_p_up_mem += beinfo[constants.BE_MEMORY]
7787

    
7788
        # compute memory used by instances
7789
        pnr_dyn = {
7790
          "total_memory": remote_info['memory_total'],
7791
          "reserved_memory": remote_info['memory_dom0'],
7792
          "free_memory": remote_info['memory_free'],
7793
          "total_disk": remote_info['vg_size'],
7794
          "free_disk": remote_info['vg_free'],
7795
          "total_cpus": remote_info['cpu_total'],
7796
          "i_pri_memory": i_p_mem,
7797
          "i_pri_up_memory": i_p_up_mem,
7798
          }
7799
        pnr.update(pnr_dyn)
7800

    
7801
      node_results[nname] = pnr
7802
    data["nodes"] = node_results
7803

    
7804
    # instance data
7805
    instance_data = {}
7806
    for iinfo, beinfo in i_list:
7807
      nic_data = []
7808
      for nic in iinfo.nics:
7809
        filled_params = objects.FillDict(
7810
            cluster_info.nicparams[constants.PP_DEFAULT],
7811
            nic.nicparams)
7812
        nic_dict = {"mac": nic.mac,
7813
                    "ip": nic.ip,
7814
                    "mode": filled_params[constants.NIC_MODE],
7815
                    "link": filled_params[constants.NIC_LINK],
7816
                   }
7817
        if filled_params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
7818
          nic_dict["bridge"] = filled_params[constants.NIC_LINK]
7819
        nic_data.append(nic_dict)
7820
      pir = {
7821
        "tags": list(iinfo.GetTags()),
7822
        "admin_up": iinfo.admin_up,
7823
        "vcpus": beinfo[constants.BE_VCPUS],
7824
        "memory": beinfo[constants.BE_MEMORY],
7825
        "os": iinfo.os,
7826
        "nodes": [iinfo.primary_node] + list(iinfo.secondary_nodes),
7827
        "nics": nic_data,
7828
        "disks": [{"size": dsk.size, "mode": dsk.mode} for dsk in iinfo.disks],
7829
        "disk_template": iinfo.disk_template,
7830
        "hypervisor": iinfo.hypervisor,
7831
        }
7832
      pir["disk_space_total"] = _ComputeDiskSize(iinfo.disk_template,
7833
                                                 pir["disks"])
7834
      instance_data[iinfo.name] = pir
7835

    
7836
    data["instances"] = instance_data
7837

    
7838
    self.in_data = data
7839

    
7840
  def _AddNewInstance(self):
7841
    """Add new instance data to allocator structure.
7842

7843
    This in combination with _AllocatorGetClusterData will create the
7844
    correct structure needed as input for the allocator.
7845

7846
    The checks for the completeness of the opcode must have already been
7847
    done.
7848

7849
    """
7850
    data = self.in_data
7851

    
7852
    disk_space = _ComputeDiskSize(self.disk_template, self.disks)
7853

    
7854
    if self.disk_template in constants.DTS_NET_MIRROR:
7855
      self.required_nodes = 2
7856
    else:
7857
      self.required_nodes = 1
7858
    request = {
7859
      "type": "allocate",
7860
      "name": self.name,
7861
      "disk_template": self.disk_template,
7862
      "tags": self.tags,
7863
      "os": self.os,
7864
      "vcpus": self.vcpus,
7865
      "memory": self.mem_size,
7866
      "disks": self.disks,
7867
      "disk_space_total": disk_space,
7868
      "nics": self.nics,
7869
      "required_nodes": self.required_nodes,
7870
      }
7871
    data["request"] = request
7872

    
7873
  def _AddRelocateInstance(self):
7874
    """Add relocate instance data to allocator structure.
7875

7876
    This in combination with _IAllocatorGetClusterData will create the
7877
    correct structure needed as input for the allocator.
7878

7879
    The checks for the completeness of the opcode must have already been
7880
    done.
7881

7882
    """
7883
    instance = self.cfg.GetInstanceInfo(self.name)
7884
    if instance is None:
7885
      raise errors.ProgrammerError("Unknown instance '%s' passed to"
7886
                                   " IAllocator" % self.name)
7887

    
7888
    if instance.disk_template not in constants.DTS_NET_MIRROR:
7889
      raise errors.OpPrereqError("Can't relocate non-mirrored instances")
7890

    
7891
    if len(instance.secondary_nodes) != 1:
7892
      raise errors.OpPrereqError("Instance has not exactly one secondary node")
7893

    
7894
    self.required_nodes = 1
7895
    disk_sizes = [{'size': disk.size} for disk in instance.disks]
7896
    disk_space = _ComputeDiskSize(instance.disk_template, disk_sizes)
7897

    
7898
    request = {
7899
      "type": "relocate",
7900
      "name": self.name,
7901
      "disk_space_total": disk_space,
7902
      "required_nodes": self.required_nodes,
7903
      "relocate_from": self.relocate_from,
7904
      }
7905
    self.in_data["request"] = request
7906

    
7907
  def _BuildInputData(self):
7908
    """Build input data structures.
7909

7910
    """
7911
    self._ComputeClusterData()
7912

    
7913
    if self.mode == constants.IALLOCATOR_MODE_ALLOC:
7914
      self._AddNewInstance()
7915
    else:
7916
      self._AddRelocateInstance()
7917

    
7918
    self.in_text = serializer.Dump(self.in_data)
7919

    
7920
  def Run(self, name, validate=True, call_fn=None):
7921
    """Run an instance allocator and return the results.
7922

7923
    """
7924
    if call_fn is None:
7925
      call_fn = self.rpc.call_iallocator_runner
7926

    
7927
    result = call_fn(self.cfg.GetMasterNode(), name, self.in_text)
7928
    result.Raise("Failure while running the iallocator script")
7929

    
7930
    self.out_text = result.payload
7931
    if validate:
7932
      self._ValidateResult()
7933

    
7934
  def _ValidateResult(self):
7935
    """Process the allocator results.
7936

7937
    This will process and if successful save the result in
7938
    self.out_data and the other parameters.
7939

7940
    """
7941
    try:
7942
      rdict = serializer.Load(self.out_text)
7943
    except Exception, err:
7944
      raise errors.OpExecError("Can't parse iallocator results: %s" % str(err))
7945

    
7946
    if not isinstance(rdict, dict):
7947
      raise errors.OpExecError("Can't parse iallocator results: not a dict")
7948

    
7949
    for key in "success", "info", "nodes":
7950
      if key not in rdict:
7951
        raise errors.OpExecError("Can't parse iallocator results:"
7952
                                 " missing key '%s'" % key)
7953
      setattr(self, key, rdict[key])
7954

    
7955
    if not isinstance(rdict["nodes"], list):
7956
      raise errors.OpExecError("Can't parse iallocator results: 'nodes' key"
7957
                               " is not a list")
7958
    self.out_data = rdict
7959

    
7960

    
7961
class LUTestAllocator(NoHooksLU):
7962
  """Run allocator tests.
7963

7964
  This LU runs the allocator tests
7965

7966
  """
7967
  _OP_REQP = ["direction", "mode", "name"]
7968

    
7969
  def CheckPrereq(self):
7970
    """Check prerequisites.
7971

7972
    This checks the opcode parameters depending on the director and mode test.
7973

7974
    """
7975
    if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
7976
      for attr in ["name", "mem_size", "disks", "disk_template",
7977
                   "os", "tags", "nics", "vcpus"]:
7978
        if not hasattr(self.op, attr):
7979
          raise errors.OpPrereqError("Missing attribute '%s' on opcode input" %
7980
                                     attr)
7981
      iname = self.cfg.ExpandInstanceName(self.op.name)
7982
      if iname is not None:
7983
        raise errors.OpPrereqError("Instance '%s' already in the cluster" %
7984
                                   iname)
7985
      if not isinstance(self.op.nics, list):
7986
        raise errors.OpPrereqError("Invalid parameter 'nics'")
7987
      for row in self.op.nics:
7988
        if (not isinstance(row, dict) or
7989
            "mac" not in row or
7990
            "ip" not in row or
7991
            "bridge" not in row):
7992
          raise errors.OpPrereqError("Invalid contents of the"
7993
                                     " 'nics' parameter")
7994
      if not isinstance(self.op.disks, list):
7995
        raise errors.OpPrereqError("Invalid parameter 'disks'")
7996
      for row in self.op.disks:
7997
        if (not isinstance(row, dict) or
7998
            "size" not in row or
7999
            not isinstance(row["size"], int) or
8000
            "mode" not in row or
8001
            row["mode"] not in ['r', 'w']):
8002
          raise errors.OpPrereqError("Invalid contents of the"
8003
                                     " 'disks' parameter")
8004
      if not hasattr(self.op, "hypervisor") or self.op.hypervisor is None:
8005
        self.op.hypervisor = self.cfg.GetHypervisorType()
8006
    elif self.op.mode == constants.IALLOCATOR_MODE_RELOC:
8007
      if not hasattr(self.op, "name"):
8008
        raise errors.OpPrereqError("Missing attribute 'name' on opcode input")
8009
      fname = self.cfg.ExpandInstanceName(self.op.name)
8010
      if fname is None:
8011
        raise errors.OpPrereqError("Instance '%s' not found for relocation" %
8012
                                   self.op.name)
8013
      self.op.name = fname
8014
      self.relocate_from = self.cfg.GetInstanceInfo(fname).secondary_nodes
8015
    else:
8016
      raise errors.OpPrereqError("Invalid test allocator mode '%s'" %
8017
                                 self.op.mode)
8018

    
8019
    if self.op.direction == constants.IALLOCATOR_DIR_OUT:
8020
      if not hasattr(self.op, "allocator") or self.op.allocator is None:
8021
        raise errors.OpPrereqError("Missing allocator name")
8022
    elif self.op.direction != constants.IALLOCATOR_DIR_IN:
8023
      raise errors.OpPrereqError("Wrong allocator test '%s'" %
8024
                                 self.op.direction)
8025

    
8026
  def Exec(self, feedback_fn):
8027
    """Run the allocator test.
8028

8029
    """
8030
    if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
8031
      ial = IAllocator(self.cfg, self.rpc,
8032
                       mode=self.op.mode,
8033
                       name=self.op.name,
8034
                       mem_size=self.op.mem_size,
8035
                       disks=self.op.disks,
8036
                       disk_template=self.op.disk_template,
8037
                       os=self.op.os,
8038
                       tags=self.op.tags,
8039
                       nics=self.op.nics,
8040
                       vcpus=self.op.vcpus,
8041
                       hypervisor=self.op.hypervisor,
8042
                       )
8043
    else:
8044
      ial = IAllocator(self.cfg, self.rpc,
8045
                       mode=self.op.mode,
8046
                       name=self.op.name,
8047
                       relocate_from=list(self.relocate_from),
8048
                       )
8049

    
8050
    if self.op.direction == constants.IALLOCATOR_DIR_IN:
8051
      result = ial.in_text
8052
    else:
8053
      ial.Run(self.op.allocator, validate=False)
8054
      result = ial.out_text
8055
    return result