From bd2a5569a3f70a8f72aa14d6e1b0411940bc7fb3 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 3 Aug 2011 11:46:10 +0200
Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=9Cgnt-instance=20change-group?=
 =?UTF-8?q?=E2=80=9D=C2=A0command?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/client/gnt_instance.py | 37 +++++++++++++++++++++++++++++++++++++
 man/gnt-instance.rst       | 17 +++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/lib/client/gnt_instance.py b/lib/client/gnt_instance.py
index 58e242e6d..f3cb84517 100644
--- a/lib/client/gnt_instance.py
+++ b/lib/client/gnt_instance.py
@@ -1313,6 +1313,39 @@ def SetInstanceParams(opts, args):
   return 0
 
 
+def ChangeGroup(opts, args):
+  """Moves an instance to another group.
+
+  """
+  (instance_name, ) = args
+
+  cl = GetClient()
+
+  op = opcodes.OpInstanceChangeGroup(instance_name=instance_name,
+                                     iallocator=opts.iallocator,
+                                     target_groups=opts.to,
+                                     early_release=opts.early_release)
+  result = SubmitOpCode(op, cl=cl, opts=opts)
+
+  # Keep track of submitted jobs
+  jex = JobExecutor(cl=cl, opts=opts)
+
+  for (status, job_id) in result[constants.JOB_IDS_KEY]:
+    jex.AddJobId(None, status, job_id)
+
+  results = jex.GetResults()
+  bad_cnt = len([row for row in results if not row[0]])
+  if bad_cnt == 0:
+    ToStdout("Instance '%s' changed group successfully.", instance_name)
+    rcode = constants.EXIT_SUCCESS
+  else:
+    ToStdout("There were %s errors while changing group of instance '%s'.",
+             bad_cnt, instance_name)
+    rcode = constants.EXIT_FAILURE
+
+  return rcode
+
+
 # multi-instance selection options
 m_force_multi = cli_option("--force-multiple", dest="force_multi",
                            help="Do not ask for confirmation when more than"
@@ -1490,6 +1523,10 @@ commands = {
      ArgUnknown(min=1, max=1)],
     [SUBMIT_OPT, NWSYNC_OPT, DRY_RUN_OPT, PRIORITY_OPT],
     "<instance> <disk> <size>", "Grow an instance's disk"),
+  "change-group": (
+    ChangeGroup, ARGS_ONE_INSTANCE,
+    [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT],
+    "[-I <iallocator>] [--to <group>]", "Change group of instance"),
   "list-tags": (
     ListTags, ARGS_ONE_INSTANCE, [PRIORITY_OPT],
     "<instance_name>", "List the tags of the given instance"),
diff --git a/man/gnt-instance.rst b/man/gnt-instance.rst
index 7193ef96c..a2c2c8153 100644
--- a/man/gnt-instance.rst
+++ b/man/gnt-instance.rst
@@ -1386,6 +1386,23 @@ Example::
     # gnt-instance move -n node3.example.com instance1.example.com
 
 
+CHANGE-GROUP
+~~~~~~~~~~~~
+
+**change-group** [--iallocator *NAME*] [--to *GROUP*...] {*instance*}
+
+This command moves an instance to another node group. The move is
+calculated by an iallocator, either given on the command line or as a
+cluster default.
+
+If no specific destination groups are specified using ``--to``, all
+groups except the one containing the instance are considered.
+
+Example::
+
+    # gnt-instance change-group -I hail --to rack2 inst1.example.com
+
+
 TAGS
 ~~~~
 
-- 
GitLab