commit 417eabe2cee66412e0bdb7b770665c852f345022
Author: Iustin Pop <iustin@google.com>
Date:   Fri Apr 9 13:23:39 2010 +0200

    CreateInstance: Move some code to CheckArguments
    
    ExpandNames holds too much non-locking code (first LU to be converted to
    ExpandNames, and we didn't have CheckArguments at that poin), and this
    patch moves the checks that are lock-independent to CheckArguments.
    
    Signed-off-by: Iustin Pop <iustin@google.com>
    Reviewed-by: Michael Hanselmann <hansmi@google.com>

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index cceb7d7..7eff09e 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -6015,7 +6015,7 @@ class LUCreateInstance(LogicalUnit):
       else:
         has_no_adopt = True
     if has_adopt and has_no_adopt:
-      raise errors.OpPrereqError("Either all disks have are adoped or none is",
+      raise errors.OpPrereqError("Either all disks are adopted or none is",
                                  errors.ECODE_INVAL)
     if has_adopt:
       if self.op.disk_template != constants.DT_PLAIN:
@@ -6031,6 +6031,55 @@ class LUCreateInstance(LogicalUnit):
 
     self.adopt_disks = has_adopt
 
+    # verify creation mode
+    if self.op.mode not in (constants.INSTANCE_CREATE,
+                            constants.INSTANCE_IMPORT):
+      raise errors.OpPrereqError("Invalid instance creation mode '%s'" %
+                                 self.op.mode, errors.ECODE_INVAL)
+
+    # disk template
+    _CheckDiskTemplate(self.op.disk_template)
+
+    # instance name verification
+    if self.op.name_check:
+      self.hostname1 = utils.GetHostInfo(self.op.instance_name)
+      self.op.instance_name = self.hostname1.name
+      # used in CheckPrereq for ip ping check
+      self.check_ip = self.hostname1.ip
+    else:
+      self.check_ip = None
+
+    # file storage checks
+    if (self.op.file_driver and
+        not self.op.file_driver in constants.FILE_DRIVER):
+      raise errors.OpPrereqError("Invalid file driver name '%s'" %
+                                 self.op.file_driver, errors.ECODE_INVAL)
+
+    if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir):
+      raise errors.OpPrereqError("File storage directory path not absolute",
+                                 errors.ECODE_INVAL)
+
+    ### Node/iallocator related checks
+    if [self.op.iallocator, self.op.pnode].count(None) != 1:
+      raise errors.OpPrereqError("One and only one of iallocator and primary"
+                                 " node must be given",
+                                 errors.ECODE_INVAL)
+
+    if self.op.mode == constants.INSTANCE_IMPORT:
+      # On import force_variant must be True, because if we forced it at
+      # initial install, our only chance when importing it back is that it
+      # works again!
+      self.op.force_variant = True
+
+      if self.op.no_install:
+        self.LogInfo("No-installation mode has no effect during import")
+
+    else: # INSTANCE_CREATE
+      if getattr(self.op, "os_type", None) is None:
+        raise errors.OpPrereqError("No guest OS specified",
+                                   errors.ECODE_INVAL)
+      self.op.force_variant = getattr(self.op, "force_variant", False)
+
   def ExpandNames(self):
     """ExpandNames for CreateInstance.
 
@@ -6041,15 +6090,6 @@ class LUCreateInstance(LogicalUnit):
 
     # cheap checks, mostly valid constants given
 
-    # verify creation mode
-    if self.op.mode not in (constants.INSTANCE_CREATE,
-                            constants.INSTANCE_IMPORT):
-      raise errors.OpPrereqError("Invalid instance creation mode '%s'" %
-                                 self.op.mode, errors.ECODE_INVAL)
-
-    # disk template and mirror node verification
-    _CheckDiskTemplate(self.op.disk_template)
-
     if self.op.hypervisor is None:
       self.op.hypervisor = self.cfg.GetHypervisorType()
 
@@ -6078,16 +6118,7 @@ class LUCreateInstance(LogicalUnit):
 
     #### instance parameters check
 
-    # instance name verification
-    if self.op.name_check:
-      hostname1 = utils.GetHostInfo(self.op.instance_name)
-      self.op.instance_name = instance_name = hostname1.name
-      # used in CheckPrereq for ip ping check
-      self.check_ip = hostname1.ip
-    else:
-      instance_name = self.op.instance_name
-      self.check_ip = None
-
+    instance_name = self.op.instance_name
     # this is just a preventive check, but someone might still add this
     # instance in the meantime, and creation will fail at lock-add time
     if instance_name in self.cfg.GetInstanceList():
@@ -6119,7 +6150,7 @@ class LUCreateInstance(LogicalUnit):
           raise errors.OpPrereqError("IP address set to auto but name checks"
                                      " have been skipped. Aborting.",
                                      errors.ECODE_INVAL)
-        nic_ip = hostname1.ip
+        nic_ip = self.hostname1.ip
       else:
         if not utils.IsValidIP(ip):
           raise errors.OpPrereqError("Given IP address '%s' doesn't look"
@@ -6187,22 +6218,6 @@ class LUCreateInstance(LogicalUnit):
         new_disk["adopt"] = disk["adopt"]
       self.disks.append(new_disk)
 
-    # file storage checks
-    if (self.op.file_driver and
-        not self.op.file_driver in constants.FILE_DRIVER):
-      raise errors.OpPrereqError("Invalid file driver name '%s'" %
-                                 self.op.file_driver, errors.ECODE_INVAL)
-
-    if self.op.file_storage_dir and os.path.isabs(self.op.file_storage_dir):
-      raise errors.OpPrereqError("File storage directory path not absolute",
-                                 errors.ECODE_INVAL)
-
-    ### Node/iallocator related checks
-    if [self.op.iallocator, self.op.pnode].count(None) != 1:
-      raise errors.OpPrereqError("One and only one of iallocator and primary"
-                                 " node must be given",
-                                 errors.ECODE_INVAL)
-
     if self.op.iallocator:
       self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
     else:
@@ -6236,20 +6251,6 @@ class LUCreateInstance(LogicalUnit):
           self.op.src_path = src_path = \
             utils.PathJoin(constants.EXPORT_DIR, src_path)
 
-      # On import force_variant must be True, because if we forced it at
-      # initial install, our only chance when importing it back is that it
-      # works again!
-      self.op.force_variant = True
-
-      if self.op.no_install:
-        self.LogInfo("No-installation mode has no effect during import")
-
-    else: # INSTANCE_CREATE
-      if getattr(self.op, "os_type", None) is None:
-        raise errors.OpPrereqError("No guest OS specified",
-                                   errors.ECODE_INVAL)
-      self.op.force_variant = getattr(self.op, "force_variant", False)
-
   def _RunAllocator(self):
     """Run the allocator based on input opcode.
 
