commit c3d839f5df970eaba6c1d038078e3b56130d47a3
Author: Michael Hanselmann <hansmi@google.com>
Date:   Tue Jan 22 15:42:07 2013 +0100

    hv_xen: Simplify writing configuration
    
    Instead of calling _WriteConfigFileStatic from both derived classes,
    those now only return the content (minus the “do not edit” header). The
    configuration is then written by the base class, XenHypervisor.
    
    Signed-off-by: Michael Hanselmann <hansmi@google.com>
    Reviewed-by: Guido Trotter <ultrotter@google.com>

diff --git a/lib/hypervisor/hv_xen.py b/lib/hypervisor/hv_xen.py
index 62ecef3..7b07e98 100644
--- a/lib/hypervisor/hv_xen.py
+++ b/lib/hypervisor/hv_xen.py
@@ -340,13 +340,13 @@ class XenHypervisor(hv_base.BaseHypervisor):
     return utils.PathJoin(self._cfgdir, instance_name)
 
   @classmethod
-  def _WriteConfigFile(cls, instance, startup_memory, block_devices):
-    """Write the Xen config file for the instance.
+  def _GetConfig(cls, instance, startup_memory, block_devices):
+    """Build Xen configuration for an instance.
 
     """
     raise NotImplementedError
 
-  def _WriteConfigFileStatic(self, instance_name, data):
+  def _WriteConfigFile(self, instance_name, data):
     """Write the Xen config file for the instance.
 
     This version of the function just writes the config file from static data.
@@ -423,12 +423,28 @@ class XenHypervisor(hv_base.BaseHypervisor):
     xm_list = self._GetXmList(False)
     return xm_list
 
+  def _MakeConfigFile(self, instance, startup_memory, block_devices):
+    """Gather configuration details and write to disk.
+
+    See L{_GetConfig} for arguments.
+
+    """
+    buf = StringIO()
+    buf.write("# Automatically generated by Ganeti. Do not edit!\n")
+    buf.write("\n")
+    buf.write(self._GetConfig(instance, startup_memory, block_devices))
+    buf.write("\n")
+
+    self._WriteConfigFile(instance.name, buf.getvalue())
+
   def StartInstance(self, instance, block_devices, startup_paused):
     """Start an instance.
 
     """
     startup_memory = self._InstanceStartupMemory(instance)
-    self._WriteConfigFile(instance, startup_memory, block_devices)
+
+    self._MakeConfigFile(instance, startup_memory, block_devices)
+
     cmd = [constants.XEN_CMD, "create"]
     if startup_paused:
       cmd.extend(["-p"])
@@ -594,7 +610,7 @@ class XenHypervisor(hv_base.BaseHypervisor):
 
     """
     if success:
-      self._WriteConfigFileStatic(instance.name, info)
+      self._WriteConfigFile(instance.name, info)
 
   def MigrateInstance(self, instance, target, live):
     """Migrate an instance to a target node.
@@ -716,7 +732,7 @@ class XenPvmHypervisor(XenHypervisor):
       (False, lambda x: 0 < x < 65536, "invalid weight", None, None),
     }
 
-  def _WriteConfigFile(self, instance, startup_memory, block_devices):
+  def _GetConfig(self, instance, startup_memory, block_devices):
     """Write the Xen config file for the instance.
 
     """
@@ -789,9 +805,8 @@ class XenPvmHypervisor(XenHypervisor):
       config.write("on_reboot = 'destroy'\n")
     config.write("on_crash = 'restart'\n")
     config.write("extra = '%s'\n" % hvp[constants.HV_KERNEL_ARGS])
-    self._WriteConfigFileStatic(instance.name, config.getvalue())
 
-    return True
+    return config.getvalue()
 
 
 class XenHvmHypervisor(XenHypervisor):
@@ -837,14 +852,13 @@ class XenHvmHypervisor(XenHypervisor):
       (False, lambda x: 0 < x < 65535, "invalid weight", None, None),
     }
 
-  def _WriteConfigFile(self, instance, startup_memory, block_devices):
+  def _GetConfig(self, instance, startup_memory, block_devices):
     """Create a Xen 3.1 HVM config file.
 
     """
     hvp = instance.hvparams
 
     config = StringIO()
-    config.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
 
     # kernel handling
     kpath = hvp[constants.HV_KERNEL_PATH]
@@ -947,6 +961,5 @@ class XenHvmHypervisor(XenHypervisor):
     else:
       config.write("on_reboot = 'destroy'\n")
     config.write("on_crash = 'restart'\n")
-    self._WriteConfigFileStatic(instance.name, config.getvalue())
 
-    return True
+    return config.getvalue()
