Revision 83f54caa lib/cmdlib/instance_operation.py

b/lib/cmdlib/instance_operation.py
1 1
#
2 2
#
3 3

  
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
33 33
from ganeti import hypervisor
34 34
from ganeti import locking
35 35
from ganeti import objects
36
from ganeti import ssh
36 37
from ganeti import utils
37 38
from ganeti.cmdlib.base import LogicalUnit, NoHooksLU
38 39
from ganeti.cmdlib.common import INSTANCE_ONLINE, INSTANCE_DOWN, \
39 40
  CheckHVParams, CheckInstanceState, CheckNodeOnline, GetUpdatedParams, \
40
  CheckOSParams, ShareAll
41
  CheckOSParams, CheckOSImage, ShareAll
41 42
from ganeti.cmdlib.instance_storage import StartInstanceDisks, \
42
  ShutdownInstanceDisks
43
  ShutdownInstanceDisks, ImageDisks
43 44
from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \
44 45
  CheckInstanceBridgesExist, CheckNodeFreeMemory, CheckNodeHasOS
45 46
from ganeti.hypervisor import hv_base
......
256 257
  HTYPE = constants.HTYPE_INSTANCE
257 258
  REQ_BGL = False
258 259

  
260
  def CheckArguments(self):
261
    CheckOSImage(self.op)
262

  
259 263
  def ExpandNames(self):
260 264
    self._ExpandAndLockInstance()
261 265

  
......
321 325
    params_secret = self.op.osparams_secret
322 326

  
323 327
    cluster = self.cfg.GetClusterInfo()
324
    self.os_inst = cluster.SimpleFillOS(
328
    self.osparams = cluster.SimpleFillOS(
325 329
      instance_os,
326 330
      params_public,
327 331
      os_params_private=params_private,
328 332
      os_params_secret=params_secret
329 333
    )
330 334

  
331
    CheckOSParams(self, True, node_uuids, instance_os, self.os_inst)
335
    CheckOSParams(self, True, node_uuids, instance_os, self.osparams)
336

  
337
  def _ReinstallOSScripts(self, instance, osparams, debug_level):
338
    """Reinstall OS scripts on an instance.
339

  
340
    @type instance: L{objects.Instance}
341
    @param instance: instance of which the OS scripts should run
342

  
343
    @type osparams: L{dict}
344
    @param osparams: OS parameters
345

  
346
    @type debug_level: non-negative int
347
    @param debug_level: debug level
348

  
349
    @rtype: NoneType
350
    @return: None
351
    @raise errors.OpExecError: in case of failure
352

  
353
    """
354
    self.LogInfo("Running instance OS create scripts...")
355
    result = self.rpc.call_instance_os_add(instance.primary_node,
356
                                           (instance, osparams),
357
                                           True,
358
                                           debug_level)
359
    result.Raise("Could not install OS for instance '%s' on node '%s'" %
360
                 (instance.name, self.cfg.GetNodeName(instance.primary_node)))
361

  
362
  def _ReinstallOSImage(self, instance, os_image):
363
    """Reinstall OS image on an instance.
364

  
365
    @type instance: L{objects.Instance}
366
    @param instance: instance on which the OS image should be installed
367

  
368
    @type os_image: string
369
    @param os_image: OS image URL or absolute file path
370

  
371
    @rtype: NoneType
372
    @return: None
373
    @raise errors.OpExecError: in case of failure
374

  
375
    """
376
    master = self.cfg.GetMasterNode()
377
    pnode = self.cfg.GetNodeInfo(instance.primary_node)
378

  
379
    if not utils.IsUrl(os_image) and master != pnode.uuid:
380
      ssh_port = pnode.ndparams.get(constants.ND_SSH_PORT)
381
      srun = ssh.SshRunner(self.cfg.GetClusterName())
382
      srun.CopyFileToNode(pnode.name, ssh_port, os_image)
383

  
384
    ImageDisks(self, instance, os_image)
332 385

  
333 386
  def Exec(self, feedback_fn):
334 387
    """Reinstall the instance.
335 388

  
336 389
    """
337
    if self.op.os_type is not None:
338
      feedback_fn("Changing OS to '%s'..." % self.op.os_type)
339
      self.instance.os = self.op.os_type
340
      # Write to configuration
390
    os_image = objects.GetOSImage(self.op.osparams)
391

  
392
    if os_image is not None:
393
      feedback_fn("Using OS image '%s', not changing instance"
394
                  " configuration" % os_image)
395
    else:
396
      os_image = objects.GetOSImage(self.instance.osparams)
397

  
398
    os_type = self.op.os_type
399

  
400
    if os_type is not None:
401
      feedback_fn("Changing OS scripts to '%s'..." % os_type)
402
      self.instance.os = os_type
341 403
      self.cfg.Update(self.instance, feedback_fn)
404
    else:
405
      os_type = self.instance.os
342 406

  
343
    StartInstanceDisks(self, self.instance, None)
344
    try:
345
      feedback_fn("Running the instance OS create scripts...")
346
      # FIXME: pass debug option from opcode to backend
347
      result = self.rpc.call_instance_os_add(self.instance.primary_node,
348
                                             (self.instance, self.os_inst),
349
                                             True, self.op.debug_level)
350
      result.Raise("Could not install OS for instance %s on node %s" %
351
                   (self.instance.name,
352
                    self.cfg.GetNodeName(self.instance.primary_node)))
353
    finally:
354
      ShutdownInstanceDisks(self, self.instance)
407
    if not os_image and not os_type:
408
      self.LogInfo("No OS scripts or OS image specified or found in the"
409
                   " instance's configuration, nothing to install")
410
    else:
411
      StartInstanceDisks(self, self.instance, None)
412
      try:
413
        if os_image:
414
          self._ReinstallOSImage(self.instance, os_image)
415

  
416
        if os_type:
417
          self._ReinstallOSScripts(self.instance, self.osparams,
418
                                   self.op.debug_level)
419
      finally:
420
        ShutdownInstanceDisks(self, self.instance)
355 421

  
356 422

  
357 423
class LUInstanceReboot(LogicalUnit):

Also available in: Unified diff