From a985b4178d349acd92d52588c8b2fea06d1d56d3 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 24 Feb 2009 15:25:01 +0000
Subject: [PATCH] Simplify a little the hypervisor routines
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Instead of β€œinstance.hvparams”, we use a shorter β€œhvp” name to make readability
better.

Reviewed-by: imsnah
---
 lib/hypervisor/hv_kvm.py | 36 +++++++++++++++++++-----------------
 lib/hypervisor/hv_xen.py | 23 ++++++++++++-----------
 2 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index 92bad4172..9e47ebc00 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -235,14 +235,15 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     if not instance.hvparams[constants.HV_ACPI]:
       kvm_cmd.extend(['-no-acpi'])
 
-    boot_disk = (instance.hvparams[constants.HV_BOOT_ORDER] == "disk")
-    boot_cdrom = (instance.hvparams[constants.HV_BOOT_ORDER] == "cdrom")
-    boot_network = (instance.hvparams[constants.HV_BOOT_ORDER] == "network")
+    hvp = instance.hvparams
+    boot_disk = hvp[constants.HV_BOOT_ORDER] == "disk"
+    boot_cdrom = hvp[constants.HV_BOOT_ORDER] == "cdrom"
+    boot_network = hvp[constants.HV_BOOT_ORDER] == "network"
 
     if boot_network:
       kvm_cmd.extend(['-boot', 'n'])
 
-    disk_type = instance.hvparams[constants.HV_DISK_TYPE]
+    disk_type = hvp[constants.HV_DISK_TYPE]
     if disk_type == constants.HT_DISK_PARAVIRTUAL:
       if_val = ',if=virtio'
     else:
@@ -263,7 +264,7 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       drive_val = 'file=%s,format=raw%s%s' % (dev_path, if_val, boot_val)
       kvm_cmd.extend(['-drive', drive_val])
 
-    iso_image = instance.hvparams[constants.HV_CDROM_IMAGE_PATH]
+    iso_image = hvp[constants.HV_CDROM_IMAGE_PATH]
     if iso_image:
       options = ',format=raw,media=cdrom'
       if boot_cdrom:
@@ -274,10 +275,10 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       drive_val = 'file=%s%s' % (iso_image, options)
       kvm_cmd.extend(['-drive', drive_val])
 
-    kernel_path = instance.hvparams[constants.HV_KERNEL_PATH]
+    kernel_path = hvp[constants.HV_KERNEL_PATH]
     if kernel_path:
       kvm_cmd.extend(['-kernel', kernel_path])
-      initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
+      initrd_path = hvp[constants.HV_INITRD_PATH]
       if initrd_path:
         kvm_cmd.extend(['-initrd', initrd_path])
       root_append = 'root=%s ro' % instance.hvparams[constants.HV_ROOT_PATH]
@@ -286,13 +287,13 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       else:
         kvm_cmd.extend(['-append', root_append])
 
-    mouse_type = instance.hvparams[constants.HV_USB_MOUSE]
+    mouse_type = hvp[constants.HV_USB_MOUSE]
     if mouse_type:
       kvm_cmd.extend(['-usb'])
       kvm_cmd.extend(['-usbdevice', mouse_type])
 
     # FIXME: handle vnc password
-    vnc_bind_address = instance.hvparams[constants.HV_VNC_BIND_ADDRESS]
+    vnc_bind_address = hvp[constants.HV_VNC_BIND_ADDRESS]
     if vnc_bind_address:
       if utils.IsValidIP(vnc_bind_address):
         if instance.network_port > constants.VNC_BASE_PORT:
@@ -311,14 +312,14 @@ class KVMHypervisor(hv_base.BaseHypervisor):
         # Only allow tls and other option when not binding to a file, for now.
         # kvm/qemu gets confused otherwise about the filename to use.
         vnc_append = ''
-        if instance.hvparams[constants.HV_VNC_TLS]:
+        if hvp[constants.HV_VNC_TLS]:
           vnc_append = '%s,tls' % vnc_append
-          if instance.hvparams[constants.HV_VNC_X509_VERIFY]:
+          if hvp[constants.HV_VNC_X509_VERIFY]:
             vnc_append = '%s,x509verify=%s' % (vnc_append,
-              instance.hvparams[constants.HV_VNC_X509])
-          elif instance.hvparams[constants.HV_VNC_X509]:
+                                               hvp[constants.HV_VNC_X509])
+          elif hvp[constants.HV_VNC_X509]:
             vnc_append = '%s,x509=%s' % (vnc_append,
-              instance.hvparams[constants.HV_VNC_X509])
+                                         hvp[constants.HV_VNC_X509])
         vnc_arg = '%s%s' % (vnc_arg, vnc_append)
 
       else:
@@ -331,8 +332,9 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     monitor_dev = 'unix:%s,server,nowait' % \
       self._InstanceMonitor(instance.name)
     kvm_cmd.extend(['-monitor', monitor_dev])
-    if instance.hvparams[constants.HV_SERIAL_CONSOLE]:
-      serial_dev = 'unix:%s,server,nowait' % self._InstanceSerial(instance.name)
+    if hvp[constants.HV_SERIAL_CONSOLE]:
+      serial_dev = ('unix:%s,server,nowait' %
+                    self._InstanceSerial(instance.name))
       kvm_cmd.extend(['-serial', serial_dev])
     else:
       kvm_cmd.extend(['-serial', 'none'])
@@ -340,7 +342,7 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     # Save the current instance nics, but defer their expansion as parameters,
     # as we'll need to generate executable temp files for them.
     kvm_nics = instance.nics
-    hvparams = instance.hvparams
+    hvparams = hvp
 
     return (kvm_cmd, kvm_nics, hvparams)
 
diff --git a/lib/hypervisor/hv_xen.py b/lib/hypervisor/hv_xen.py
index 8f865ea15..f4049003d 100644
--- a/lib/hypervisor/hv_xen.py
+++ b/lib/hypervisor/hv_xen.py
@@ -439,15 +439,16 @@ class XenPvmHypervisor(XenHypervisor):
     """Write the Xen config file for the instance.
 
     """
+    hvp = instance.hvparams
     config = StringIO()
     config.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
 
     # kernel handling
-    kpath = instance.hvparams[constants.HV_KERNEL_PATH]
+    kpath = hvp[constants.HV_KERNEL_PATH]
     config.write("kernel = '%s'\n" % kpath)
 
     # initrd handling
-    initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
+    initrd_path = hvp[constants.HV_INITRD_PATH]
     if initrd_path:
       config.write("ramdisk = '%s'\n" % initrd_path)
 
@@ -479,8 +480,7 @@ class XenPvmHypervisor(XenHypervisor):
     # just in case it exists
     utils.RemoveFile("/etc/xen/auto/%s" % instance.name)
     try:
-      utils.WriteFile("/etc/xen/%s" % instance.name,
-                      data=config.getvalue())
+      utils.WriteFile("/etc/xen/%s" % instance.name, data=config.getvalue())
     except EnvironmentError, err:
       raise errors.HypervisorError("Cannot write Xen instance confile"
                                    " file /etc/xen/%s: %s" %
@@ -567,6 +567,8 @@ class XenHvmHypervisor(XenHypervisor):
     """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")
     config.write("kernel = '/usr/lib/xen/boot/hvmloader'\n")
@@ -588,16 +590,15 @@ class XenHvmHypervisor(XenHypervisor):
       config.write("device_model = '/usr/lib64/xen/bin/qemu-dm'\n")
     else:
       config.write("device_model = '/usr/lib/xen/bin/qemu-dm'\n")
-    config.write("boot = '%s'\n" % instance.hvparams[constants.HV_BOOT_ORDER])
+    config.write("boot = '%s'\n" % hvp[constants.HV_BOOT_ORDER])
     config.write("sdl = 0\n")
     config.write("usb = 1\n")
     config.write("usbdevice = 'tablet'\n")
     config.write("vnc = 1\n")
-    if instance.hvparams[constants.HV_VNC_BIND_ADDRESS] is None:
+    if hvp[constants.HV_VNC_BIND_ADDRESS] is None:
       config.write("vnclisten = '%s'\n" % constants.VNC_DEFAULT_BIND_ADDRESS)
     else:
-      config.write("vnclisten = '%s'\n" %
-                   instance.hvparams["vnc_bind_address"])
+      config.write("vnclisten = '%s'\n" % hvp["vnc_bind_address"])
 
     if instance.network_port > constants.VNC_BASE_PORT:
       display = instance.network_port - constants.VNC_BASE_PORT
@@ -619,7 +620,7 @@ class XenHvmHypervisor(XenHypervisor):
     config.write("localtime = 1\n")
 
     vif_data = []
-    nic_type = instance.hvparams[constants.HV_NIC_TYPE]
+    nic_type = hvp[constants.HV_NIC_TYPE]
     if nic_type is None:
       # ensure old instances don't change
       nic_type_str = ", type=ioemu"
@@ -637,13 +638,13 @@ class XenHvmHypervisor(XenHypervisor):
     config.write("vif = [%s]\n" % ",".join(vif_data))
     disk_data = cls._GetConfigFileDiskData(instance.disk_template,
                                             block_devices)
-    disk_type = instance.hvparams[constants.HV_DISK_TYPE]
+    disk_type = hvp[constants.HV_DISK_TYPE]
     if disk_type in (None, constants.HT_DISK_IOEMU):
       replacement = ",ioemu:hd"
     else:
       replacement = ",hd"
     disk_data = [line.replace(",sd", replacement) for line in disk_data]
-    iso_path = instance.hvparams[constants.HV_CDROM_IMAGE_PATH]
+    iso_path = hvp[constants.HV_CDROM_IMAGE_PATH]
     if iso_path:
       iso = "'file:%s,hdc:cdrom,r'" % iso_path
       disk_data.append(iso)
-- 
GitLab