From 4be4691d5be0f08780678b18f75ed55b03fdba57 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 25 Nov 2008 12:57:13 +0000
Subject: [PATCH] Slight change to the LU initialization code

This patch adds support for a separate LU.CheckArguments() method which
should do syntactic checks without holding locks and without polluting
the ExpandNames which is a lock-related function. See for example the
FIXME in LUSetInstanceParams.CheckPrereq.

The patch also removes the REQ_MASTER and IsCluster checks since with
the current architecture, inside LU code we are always in a valid
cluster and we are always running on the master node.

Reviewed-by: ultrotter
---
 lib/cmdlib.py | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index dff156513..2459dd198 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -54,7 +54,6 @@ class LogicalUnit(object):
     - implement BuildHooksEnv
     - redefine HPATH and HTYPE
     - optionally redefine their run requirements:
-        REQ_MASTER: the LU needs to run on the master node
         REQ_BGL: the LU needs to hold the Big Ganeti Lock exclusively
 
   Note that all commands require root permissions.
@@ -63,7 +62,6 @@ class LogicalUnit(object):
   HPATH = None
   HTYPE = None
   _OP_REQP = []
-  REQ_MASTER = True
   REQ_BGL = True
 
   def __init__(self, processor, op, context, rpc):
@@ -96,15 +94,7 @@ class LogicalUnit(object):
       if attr_val is None:
         raise errors.OpPrereqError("Required parameter '%s' missing" %
                                    attr_name)
-
-    if not self.cfg.IsCluster():
-      raise errors.OpPrereqError("Cluster not initialized yet,"
-                                 " use 'gnt-cluster init' first.")
-    if self.REQ_MASTER:
-      master = self.cfg.GetMasterNode()
-      if master != utils.HostInfo().name:
-        raise errors.OpPrereqError("Commands must be run on the master"
-                                   " node %s" % master)
+    self.CheckArguments()
 
   def __GetSSH(self):
     """Returns the SshRunner object
@@ -116,6 +106,24 @@ class LogicalUnit(object):
 
   ssh = property(fget=__GetSSH)
 
+  def CheckArguments(self):
+    """Check syntactic validity for the opcode arguments.
+
+    This method is for doing a simple syntactic check and ensure
+    validity of opcode parameters, without any cluster-related
+    checks. While the same can be accomplished in ExpandNames and/or
+    CheckPrereq, doing these separate is better because:
+
+      - ExpandNames is left as as purely a lock-related function
+      - CheckPrereq is run after we have aquired locks (and possible
+        waited for them)
+
+    The function is allowed to change the self.op attribute so that
+    later methods can no longer worry about missing parameters.
+
+    """
+    pass
+
   def ExpandNames(self):
     """Expand names for this LU.
 
@@ -1947,7 +1955,6 @@ class LUQueryClusterInfo(NoHooksLU):
 
   """
   _OP_REQP = []
-  REQ_MASTER = False
   REQ_BGL = False
 
   def ExpandNames(self):
-- 
GitLab