Revision 1c5945b6 scripts/gnt-instance

b/scripts/gnt-instance
31 31
from cStringIO import StringIO
32 32

  
33 33
from ganeti.cli import *
34
from ganeti import cli
35 34
from ganeti import opcodes
36 35
from ganeti import constants
37 36
from ganeti import utils
......
175 174
      raise errors.OpPrereqError("Instance '%s' does not exist" % orig_name)
176 175

  
177 176

  
177
def GenericManyOps(operation, fn):
178
  """Generic multi-instance operations.
179

  
180
  The will return a wrapper that processes the options and arguments
181
  given, and uses the passed function to build the opcode needed for
182
  the specific operation. Thus all the generic loop/confirmation code
183
  is abstracted into this function.
184

  
185
  """
186
  def realfn(opts, args):
187
    if opts.multi_mode is None:
188
      opts.multi_mode = _SHUTDOWN_INSTANCES
189
    cl = GetClient()
190
    inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
191
    if not inames:
192
      raise errors.OpPrereqError("Selection filter does not match"
193
                                 " any instances")
194
    multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
195
    if not (opts.force_multi or not multi_on
196
            or _ConfirmOperation(inames, operation)):
197
      return 1
198
    jex = JobExecutor(verbose=multi_on, cl=cl)
199
    for name in inames:
200
      op = fn(name, opts)
201
      jex.QueueJob(name, op)
202
    jex.WaitOrShow(not opts.submit_only)
203
    return 0
204
  return realfn
205

  
206

  
178 207
def ListInstances(opts, args):
179 208
  """List instances and their properties.
180 209

  
......
729 758
  return 0
730 759

  
731 760

  
732
def StartupInstance(opts, args):
761
def _StartupInstance(name, opts):
733 762
  """Startup instances.
734 763

  
735
  Depending on the options given, this will start one or more
736
  instances.
764
  This returns the opcode to start an instance, and its decorator will
765
  wrap this into a loop starting all desired instances.
737 766

  
767
  @param name: the name of the instance to act on
738 768
  @param opts: the command line options selected by the user
739
  @type args: list
740
  @param args: the instance or node names based on which we
741
      create the final selection (in conjunction with the
742
      opts argument)
743
  @rtype: int
744
  @return: the desired exit code
769
  @return: the opcode needed for the operation
745 770

  
746 771
  """
747
  cl = GetClient()
748
  if opts.multi_mode is None:
749
    opts.multi_mode = _SHUTDOWN_INSTANCES
750
  inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
751
  if not inames:
752
    raise errors.OpPrereqError("Selection filter does not match any instances")
753
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
754
  if not (opts.force_multi or not multi_on
755
          or _ConfirmOperation(inames, "startup")):
756
    return 1
757
  jex = cli.JobExecutor(verbose=multi_on, cl=cl)
758
  for name in inames:
759
    op = opcodes.OpStartupInstance(instance_name=name,
760
                                   force=opts.force)
761
    # do not add these parameters to the opcode unless they're defined
762
    if opts.hvparams:
763
      op.hvparams = opts.hvparams
764
    if opts.beparams:
765
      op.beparams = opts.beparams
766
    jex.QueueJob(name, op)
767
  jex.WaitOrShow(not opts.submit_only)
768
  return 0
772
  op = opcodes.OpStartupInstance(instance_name=name,
773
                                 force=opts.force)
774
  # do not add these parameters to the opcode unless they're defined
775
  if opts.hvparams:
776
    op.hvparams = opts.hvparams
777
  if opts.beparams:
778
    op.beparams = opts.beparams
779
  return op
769 780

  
770 781

  
771
def RebootInstance(opts, args):
782
def _RebootInstance(name, opts):
772 783
  """Reboot instance(s).
773 784

  
774
  Depending on the parameters given, this will reboot one or more
775
  instances.
785
  This returns the opcode to reboot an instance, and its decorator
786
  will wrap this into a loop rebooting all desired instances.
776 787

  
788
  @param name: the name of the instance to act on
777 789
  @param opts: the command line options selected by the user
778
  @type args: list
779
  @param args: the instance or node names based on which we
780
      create the final selection (in conjunction with the
781
      opts argument)
782
  @rtype: int
783
  @return: the desired exit code
790
  @return: the opcode needed for the operation
784 791

  
785 792
  """
786
  cl = GetClient()
787
  if opts.multi_mode is None:
788
    opts.multi_mode = _SHUTDOWN_INSTANCES
789
  inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
790
  if not inames:
791
    raise errors.OpPrereqError("Selection filter does not match any instances")
792
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
793
  if not (opts.force_multi or not multi_on
794
          or _ConfirmOperation(inames, "reboot")):
795
    return 1
796
  jex = JobExecutor(verbose=multi_on, cl=cl)
797
  for name in inames:
798
    op = opcodes.OpRebootInstance(instance_name=name,
793
  return opcodes.OpRebootInstance(instance_name=name,
799 794
                                  reboot_type=opts.reboot_type,
800 795
                                  ignore_secondaries=opts.ignore_secondaries)
801
    jex.QueueJob(name, op)
802
  jex.WaitOrShow(not opts.submit_only)
803
  return 0
804 796

  
805 797

  
806
def ShutdownInstance(opts, args):
798
def _ShutdownInstance(name, opts):
807 799
  """Shutdown an instance.
808 800

  
801
  This returns the opcode to shutdown an instance, and its decorator
802
  will wrap this into a loop shutting down all desired instances.
803

  
804
  @param name: the name of the instance to act on
809 805
  @param opts: the command line options selected by the user
810
  @type args: list
811
  @param args: the instance or node names based on which we
812
      create the final selection (in conjunction with the
813
      opts argument)
814
  @rtype: int
815
  @return: the desired exit code
806
  @return: the opcode needed for the operation
816 807

  
817 808
  """
818
  cl = GetClient()
819
  if opts.multi_mode is None:
820
    opts.multi_mode = _SHUTDOWN_INSTANCES
821
  inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
822
  if not inames:
823
    raise errors.OpPrereqError("Selection filter does not match any instances")
824
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
825
  if not (opts.force_multi or not multi_on
826
          or _ConfirmOperation(inames, "shutdown")):
827
    return 1
828

  
829
  jex = cli.JobExecutor(verbose=multi_on, cl=cl)
830
  for name in inames:
831
    op = opcodes.OpShutdownInstance(instance_name=name)
832
    jex.QueueJob(name, op)
833
  jex.WaitOrShow(not opts.submit_only)
834
  return 0
809
  return opcodes.OpShutdownInstance(instance_name=name)
835 810

  
836 811

  
837 812
def ReplaceDisks(opts, args):
......
1445 1420
    [BACKEND_OPT, DISK_OPT, FORCE_OPT, HVOPTS_OPT, NET_OPT, SUBMIT_OPT],
1446 1421
    "<instance>", "Alters the parameters of an instance"),
1447 1422
  'shutdown': (
1448
    ShutdownInstance, [ArgInstance()],
1423
    GenericManyOps("shutdown", _ShutdownInstance), [ArgInstance()],
1449 1424
    [m_node_opt, m_pri_node_opt, m_sec_node_opt, m_clust_opt,
1450 1425
     m_inst_opt, m_force_multi, SUBMIT_OPT],
1451 1426
    "<instance>", "Stops an instance"),
1452 1427
  'startup': (
1453
    StartupInstance, [ArgInstance()],
1428
    GenericManyOps("startup", _StartupInstance), [ArgInstance()],
1454 1429
    [FORCE_OPT, m_force_multi, m_node_opt, m_pri_node_opt,
1455 1430
     m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT, HVOPTS_OPT,
1456 1431
     BACKEND_OPT],
1457 1432
    "<instance>", "Starts an instance"),
1458 1433
  'reboot': (
1459
    RebootInstance, [ArgInstance()],
1434
    GenericManyOps("reboot", _RebootInstance), [ArgInstance()],
1460 1435
    [m_force_multi, REBOOT_TYPE_OPT, IGNORE_SECONDARIES_OPT, m_node_opt,
1461 1436
     m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT],
1462 1437
    "<instance>", "Reboots an instance"),

Also available in: Unified diff