From e5b7c4ca2e7f0591eda069e7bfa93255c964fced Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 13 Jul 2009 11:11:41 +0200
Subject: [PATCH] RAPI: implement instance reinstall
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch adds instance reinstall to RAPI, with two optional parameters:
  - β€˜os', in order to change the OS on reinstall
  - β€˜nostartup’, in order to leave the instance down after reinstall

The call will first shutdown the instance, the reinstall it, and unless
β€˜nostartup’ has been passed and is equal to 1, it will be started
automatically.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/rapi/baserlib.py  | 12 ++++++++++++
 lib/rapi/connector.py |  2 ++
 lib/rapi/rlib2.py     | 30 ++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/lib/rapi/baserlib.py b/lib/rapi/baserlib.py
index 2bca63ba0..7a025274c 100644
--- a/lib/rapi/baserlib.py
+++ b/lib/rapi/baserlib.py
@@ -252,6 +252,18 @@ class R_Generic(object):
                                 " '%s' parameter" % (name,))
     return val
 
+  def _checkStringVariable(self, name, default=None):
+    """Return the parsed value of an int argument.
+
+    """
+    val = self.queryargs.get(name, default)
+    if isinstance(val, list):
+      if val:
+        val = val[0]
+      else:
+        val = default
+    return val
+
   def getBodyParameter(self, name, *args):
     """Check and return the value for a given parameter.
 
diff --git a/lib/rapi/connector.py b/lib/rapi/connector.py
index fcc4f2d4e..c6270abed 100644
--- a/lib/rapi/connector.py
+++ b/lib/rapi/connector.py
@@ -163,6 +163,8 @@ CONNECTOR.update({
   re.compile(r'^/2/instances/([\w\._-]+)/tags$'): rlib2.R_2_instances_name_tags,
   re.compile(r'^/2/instances/([\w\._-]+)/reboot$'):
       rlib2.R_2_instances_name_reboot,
+  re.compile(r'^/2/instances/([\w\._-]+)/reinstall$'):
+      rlib2.R_2_instances_name_reinstall,
   re.compile(r'^/2/instances/([\w\._-]+)/shutdown$'):
       rlib2.R_2_instances_name_shutdown,
   re.compile(r'^/2/instances/([\w\._-]+)/startup$'):
diff --git a/lib/rapi/rlib2.py b/lib/rapi/rlib2.py
index 738f86f02..45f649bea 100644
--- a/lib/rapi/rlib2.py
+++ b/lib/rapi/rlib2.py
@@ -493,6 +493,36 @@ class R_2_instances_name_shutdown(baserlib.R_Generic):
     return baserlib.SubmitJob([op])
 
 
+class R_2_instances_name_reinstall(baserlib.R_Generic):
+  """/2/instances/[instance_name]/reinstall resource.
+
+  Implements an instance reinstall.
+
+  """
+
+  DOC_URI = "/2/instances/[instance_name]/reinstall"
+
+  def POST(self):
+    """Reinstall an instance.
+
+    The URI takes os=name and nostartup=[0|1] optional
+    parameters. By default, the instance will be started
+    automatically.
+
+    """
+    instance_name = self.items[0]
+    ostype = self._checkStringVariable('os')
+    nostartup = self._checkIntVariable('nostartup')
+    ops = [
+      opcodes.OpShutdownInstance(instance_name=instance_name),
+      opcodes.OpReinstallInstance(instance_name=instance_name, os_type=ostype),
+      ]
+    if not nostartup:
+      ops.append(opcodes.OpStartupInstance(instance_name=instance_name,
+                                           force=False))
+    return baserlib.SubmitJob(ops)
+
+
 class _R_Tags(baserlib.R_Generic):
   """ Quasiclass for tagging resources
 
-- 
GitLab