From 11dc66f3bec6a08c34929017b604c0430cdc9fbc Mon Sep 17 00:00:00 2001
From: Bernardo Dal Seno <bdalseno@google.com>
Date: Mon, 5 Dec 2011 20:10:42 +0100
Subject: [PATCH] Add new back-end parameter "always_failover"

Instances that have this parameter is set to True are never migrated, but
instead they can only fail over.  There are some cases where freezing the
kernel may cause problems, and hence this behavior is preferable.

Signed-off-by: Bernardo Dal Seno <bdalseno@google.com>
Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/client/gnt_instance.py |  3 +++
 lib/cmdlib.py              | 12 ++++++++++++
 lib/constants.py           |  3 +++
 man/gnt-backup.rst         |  5 +++++
 man/gnt-cluster.rst        |  4 ++++
 man/gnt-instance.rst       |  7 ++++++-
 6 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/lib/client/gnt_instance.py b/lib/client/gnt_instance.py
index 55f30728b..d0a91ea79 100644
--- a/lib/client/gnt_instance.py
+++ b/lib/client/gnt_instance.py
@@ -1228,6 +1228,9 @@ def ShowInstanceConfig(opts, args):
     buf.write("    - memory: %sMiB\n" %
               compat.TryToRoman(instance["be_actual"][constants.BE_MAXMEM],
                                 convert=opts.roman_integers))
+    buf.write("    - %s: %s\n" %
+              (constants.BE_ALWAYS_FAILOVER,
+               instance["be_actual"][constants.BE_ALWAYS_FAILOVER]))
     buf.write("    - NICs:\n")
     for idx, (ip, mac, mode, link) in enumerate(instance["nics"]):
       buf.write("      - nic/%d: MAC: %s, IP: %s, mode: %s, link: %s\n" %
diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 8541e938e..7f728d670 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -7547,6 +7547,18 @@ class TLMigrateInstance(Tasklet):
       self.lu.LogInfo("Not checking memory on the secondary node as"
                       " instance will not be started")
 
+    # check if failover must be forced instead of migration
+    if (not self.cleanup and not self.failover and
+        i_be[constants.BE_ALWAYS_FAILOVER]):
+      if self.fallback:
+        self.lu.LogInfo("Instance configured to always failover; fallback"
+                        " to failover")
+        self.failover = True
+      else:
+        raise errors.OpPrereqError("This instance has been configured to"
+                                   " always failover, please allow failover",
+                                   errors.ECODE_STATE)
+
     # check bridge existance
     _CheckInstanceBridgesExist(self.lu, instance, node=target_node)
 
diff --git a/lib/constants.py b/lib/constants.py
index 574bf9113..f0bba6a66 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -904,12 +904,14 @@ BE_MAXMEM = "maxmem"
 BE_MINMEM = "minmem"
 BE_VCPUS = "vcpus"
 BE_AUTO_BALANCE = "auto_balance"
+BE_ALWAYS_FAILOVER = "always_failover"
 
 BES_PARAMETER_TYPES = {
   BE_MAXMEM: VTYPE_SIZE,
   BE_MINMEM: VTYPE_SIZE,
   BE_VCPUS: VTYPE_INT,
   BE_AUTO_BALANCE: VTYPE_BOOL,
+  BE_ALWAYS_FAILOVER: VTYPE_BOOL,
   }
 
 BES_PARAMETER_COMPAT = {
@@ -1715,6 +1717,7 @@ BEC_DEFAULTS = {
   BE_MAXMEM: 128,
   BE_VCPUS: 1,
   BE_AUTO_BALANCE: True,
+  BE_ALWAYS_FAILOVER: False,
   }
 
 NDC_DEFAULTS = {
diff --git a/man/gnt-backup.rst b/man/gnt-backup.rst
index 0e0077a46..ff01d6343 100644
--- a/man/gnt-backup.rst
+++ b/man/gnt-backup.rst
@@ -152,6 +152,11 @@ auto_balance
     whether the instance is considered in the N+1 cluster checks
     (enough redundancy in the cluster to survive a node failure)
 
+always\_failover
+    ``True`` or ``False``, whether the instance must be failed over
+    (shut down and rebooted) always or it may be migrated (briefly
+    suspended)
+
 
 The ``-t`` options specifies the disk layout type for the instance.
 If not passed, the configuration of the original instance is used.
diff --git a/man/gnt-cluster.rst b/man/gnt-cluster.rst
index 3c46a2956..187b69da4 100644
--- a/man/gnt-cluster.rst
+++ b/man/gnt-cluster.rst
@@ -337,6 +337,10 @@ auto\_balance
     Value of the auto\_balance flag for instances to use by default,
     will be set to true if not specified.
 
+always\_failover
+    Default value for the ``always\_failover`` flag for instances; if
+    not set, ``False`` is used.
+
 
 The ``-N (--nic-parameters)`` option allows you to set the default nic
 parameters for the cluster. The parameter format is a comma-separated
diff --git a/man/gnt-instance.rst b/man/gnt-instance.rst
index dcee0d448..2fab5d441 100644
--- a/man/gnt-instance.rst
+++ b/man/gnt-instance.rst
@@ -144,6 +144,11 @@ auto\_balance
     whether the instance is considered in the N+1 cluster checks
     (enough redundancy in the cluster to survive a node failure)
 
+always\_failover
+    ``True`` or ``False``, whether the instance must be failed over
+    (shut down and rebooted) always or it may be migrated (briefly
+    suspended)
+
 Note that before 2.6 Ganeti had a ``memory`` parameter, which was the
 only value of memory an instance could have. With the
 ``maxmem``/``minmem`` change Ganeti guarantees that at least the minimum
@@ -1444,7 +1449,7 @@ ignored.
 The option ``-f`` will skip the prompting for confirmation.
 
 If ``--allow-failover`` is specified it tries to fallback to failover if
-it already can determine that a migration won't work (i.e. if the
+it already can determine that a migration won't work (e.g. if the
 instance is shut down). Please note that the fallback will not happen
 during execution. If a migration fails during execution it still fails.
 
-- 
GitLab