Fix breakage introduced by commit 8044bf655
[ganeti-local] / lib / hypervisor / hv_xen.py
index 6693c6b..64c231d 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007, 2008 Google Inc.
+# Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -23,9 +23,6 @@
 
 """
 
-import os
-import os.path
-import time
 import logging
 from cStringIO import StringIO
 
@@ -33,6 +30,7 @@ from ganeti import constants
 from ganeti import errors
 from ganeti import utils
 from ganeti.hypervisor import hv_base
+from ganeti import netutils
 
 
 class XenHypervisor(hv_base.BaseHypervisor):
@@ -42,6 +40,7 @@ class XenHypervisor(hv_base.BaseHypervisor):
   all the functionality that is identical for both.
 
   """
+  CAN_MIGRATE = True
   REBOOT_RETRY_COUNT = 60
   REBOOT_RETRY_INTERVAL = 10
 
@@ -85,7 +84,22 @@ class XenHypervisor(hv_base.BaseHypervisor):
     utils.RemoveFile("/etc/xen/%s" % instance_name)
 
   @staticmethod
-  def _GetXMList(include_node):
+  def _RunXmList(xmlist_errors):
+    """Helper function for L{_GetXMList} to run "xm list".
+
+    """
+    result = utils.RunCmd(["xm", "list"])
+    if result.failed:
+      logging.error("xm list failed (%s): %s", result.fail_reason,
+                    result.output)
+      xmlist_errors.append(result)
+      raise utils.RetryAgain()
+
+    # skip over the heading
+    return result.stdout.splitlines()[1:]
+
+  @classmethod
+  def _GetXMList(cls, include_node):
     """Return the list of running instances.
 
     If the include_node argument is True, then we return information
@@ -94,21 +108,20 @@ class XenHypervisor(hv_base.BaseHypervisor):
     @return: list of (name, id, memory, vcpus, state, time spent)
 
     """
-    for dummy in range(5):
-      result = utils.RunCmd(["xm", "list"])
-      if not result.failed:
-        break
-      logging.error("xm list failed (%s): %s", result.fail_reason,
-                    result.output)
-      time.sleep(1)
+    xmlist_errors = []
+    try:
+      lines = utils.Retry(cls._RunXmList, 1, 5, args=(xmlist_errors, ))
+    except utils.RetryTimeout:
+      if xmlist_errors:
+        xmlist_result = xmlist_errors.pop()
 
-    if result.failed:
-      raise errors.HypervisorError("xm list failed, retries"
-                                   " exceeded (%s): %s" %
-                                   (result.fail_reason, result.output))
+        errmsg = ("xm list failed, timeout exceeded (%s): %s" %
+                  (xmlist_result.fail_reason, xmlist_result.output))
+      else:
+        errmsg = "xm list failed"
+
+      raise errors.HypervisorError(errmsg)
 
-    # skip over the heading
-    lines = result.stdout.splitlines()[1:]
     result = []
     for line in lines:
       # The format of lines is:
@@ -123,7 +136,7 @@ class XenHypervisor(hv_base.BaseHypervisor):
         data[2] = int(data[2])
         data[3] = int(data[3])
         data[5] = float(data[5])
-      except ValueError, err:
+      except (TypeError, ValueError), err:
         raise errors.HypervisorError("Can't parse output of xm list,"
                                      " line: %s, error: %s" % (line, err))
 
@@ -178,46 +191,53 @@ class XenHypervisor(hv_base.BaseHypervisor):
                                    (instance.name, result.fail_reason,
                                     result.output))
 
-  def StopInstance(self, instance, force=False):
+  def StopInstance(self, instance, force=False, retry=False, name=None):
     """Stop an instance.
 
     """
-    self._RemoveConfigFile(instance.name)
+    if name is None:
+      name = instance.name
+    self._RemoveConfigFile(name)
     if force:
-      command = ["xm", "destroy", instance.name]
+      command = ["xm", "destroy", name]
     else:
-      command = ["xm", "shutdown", instance.name]
+      command = ["xm", "shutdown", name]
     result = utils.RunCmd(command)
 
     if result.failed:
       raise errors.HypervisorError("Failed to stop instance %s: %s, %s" %
-                                   (instance.name, result.fail_reason,
-                                    result.output))
+                                   (name, result.fail_reason, result.output))
 
   def RebootInstance(self, instance):
     """Reboot an instance.
 
     """
     ini_info = self.GetInstanceInfo(instance.name)
-    result = utils.RunCmd(["xm", "reboot", instance.name])
 
+    if ini_info is None:
+      raise errors.HypervisorError("Failed to reboot instance %s,"
+                                   " not running" % instance.name)
+
+    result = utils.RunCmd(["xm", "reboot", instance.name])
     if result.failed:
       raise errors.HypervisorError("Failed to reboot instance %s: %s, %s" %
                                    (instance.name, result.fail_reason,
                                     result.output))
-    done = False
-    retries = self.REBOOT_RETRY_COUNT
-    while retries > 0:
+
+    def _CheckInstance():
       new_info = self.GetInstanceInfo(instance.name)
-      # check if the domain ID has changed or the run time has
-      # decreased
-      if new_info[1] != ini_info[1] or new_info[5] < ini_info[5]:
-        done = True
-        break
-      time.sleep(self.REBOOT_RETRY_INTERVAL)
-      retries -= 1
 
-    if not done:
+      # check if the domain ID has changed or the run time has decreased
+      if (new_info is not None and
+          (new_info[1] != ini_info[1] or new_info[5] < ini_info[5])):
+        return
+
+      raise utils.RetryAgain()
+
+    try:
+      utils.Retry(_CheckInstance, self.REBOOT_RETRY_INTERVAL,
+                  self.REBOOT_RETRY_INTERVAL * self.REBOOT_RETRY_COUNT)
+    except utils.RetryTimeout:
       raise errors.HypervisorError("Failed to reboot instance %s: instance"
                                    " did not reboot in the expected interval" %
                                    (instance.name, ))
@@ -292,13 +312,12 @@ class XenHypervisor(hv_base.BaseHypervisor):
       return "'xm info' failed: %s, %s" % (result.fail_reason, result.output)
 
   @staticmethod
-  def _GetConfigFileDiskData(disk_template, block_devices):
+  def _GetConfigFileDiskData(block_devices):
     """Get disk directive for xen config file.
 
     This method builds the xen config disk directive according to the
     given disk_template and block_devices.
 
-    @param disk_template: string containing instance disk template
     @param block_devices: list of tuples (cfdev, rldev):
         - cfdev: dict containing ganeti config disk part
         - rldev: ganeti.bdev.BlockDev object
@@ -362,7 +381,7 @@ class XenHypervisor(hv_base.BaseHypervisor):
     We do nothing on a failure, as we did not change anything at accept time.
 
     @type instance: L{objects.Instance}
-    @param instance: instance whose migration is being aborted
+    @param instance: instance whose migration is being finalized
     @type info: string
     @param info: content of the xen config file on the source node
     @type success: boolean
@@ -378,27 +397,34 @@ class XenHypervisor(hv_base.BaseHypervisor):
     The migration will not be attempted if the instance is not
     currently running.
 
-    @type instance: string
-    @param instance: instance name
+    @type instance: L{objects.Instance}
+    @param instance: the instance to be migrated
     @type target: string
     @param target: ip address of the target node
     @type live: boolean
     @param live: perform a live migration
 
     """
-    if self.GetInstanceInfo(instance) is None:
+    if self.GetInstanceInfo(instance.name) is None:
       raise errors.HypervisorError("Instance not running, cannot migrate")
-    args = ["xm", "migrate"]
+
+    port = instance.hvparams[constants.HV_MIGRATION_PORT]
+
+    if not netutils.TcpPing(target, port, live_port_needed=True):
+      raise errors.HypervisorError("Remote host %s not listening on port"
+                                   " %s, cannot migrate" % (target, port))
+
+    args = ["xm", "migrate", "-p", "%d" % port]
     if live:
       args.append("-l")
-    args.extend([instance, target])
+    args.extend([instance.name, target])
     result = utils.RunCmd(args)
     if result.failed:
       raise errors.HypervisorError("Failed to migrate instance %s: %s" %
-                                   (instance, result.output))
+                                   (instance.name, result.output))
     # remove old xen file after migration succeeded
     try:
-      self._RemoveConfigFile(instance)
+      self._RemoveConfigFile(instance.name)
     except EnvironmentError:
       logging.exception("Failure while removing instance config file")
 
@@ -424,10 +450,15 @@ class XenPvmHypervisor(XenHypervisor):
   """Xen PVM hypervisor interface"""
 
   PARAMETERS = {
+    constants.HV_USE_BOOTLOADER: hv_base.NO_CHECK,
+    constants.HV_BOOTLOADER_PATH: hv_base.OPT_FILE_CHECK,
+    constants.HV_BOOTLOADER_ARGS: hv_base.NO_CHECK,
     constants.HV_KERNEL_PATH: hv_base.REQ_FILE_CHECK,
     constants.HV_INITRD_PATH: hv_base.OPT_FILE_CHECK,
     constants.HV_ROOT_PATH: hv_base.REQUIRED_CHECK,
     constants.HV_KERNEL_ARGS: hv_base.NO_CHECK,
+    constants.HV_MIGRATION_PORT: hv_base.NET_PORT_CHECK,
+    constants.HV_MIGRATION_MODE: hv_base.MIGRATION_MODE_CHECK,
     }
 
   @classmethod
@@ -439,14 +470,29 @@ class XenPvmHypervisor(XenHypervisor):
     config = StringIO()
     config.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
 
-    # kernel handling
-    kpath = hvp[constants.HV_KERNEL_PATH]
-    config.write("kernel = '%s'\n" % kpath)
+    # if bootloader is True, use bootloader instead of kernel and ramdisk
+    # parameters.
+    if hvp[constants.HV_USE_BOOTLOADER]:
+      # bootloader handling
+      bootloader_path = hvp[constants.HV_BOOTLOADER_PATH]
+      if bootloader_path:
+        config.write("bootloader = '%s'\n" % bootloader_path)
+      else:
+        raise errors.HypervisorError("Bootloader enabled, but missing"
+                                     " bootloader path")
 
-    # initrd handling
-    initrd_path = hvp[constants.HV_INITRD_PATH]
-    if initrd_path:
-      config.write("ramdisk = '%s'\n" % initrd_path)
+      bootloader_args = hvp[constants.HV_BOOTLOADER_ARGS]
+      if bootloader_args:
+        config.write("bootargs = '%s'\n" % bootloader_args)
+    else:
+      # kernel handling
+      kpath = hvp[constants.HV_KERNEL_PATH]
+      config.write("kernel = '%s'\n" % kpath)
+
+      # initrd handling
+      initrd_path = hvp[constants.HV_INITRD_PATH]
+      if initrd_path:
+        config.write("ramdisk = '%s'\n" % initrd_path)
 
     # rest of the settings
     config.write("memory = %d\n" % instance.beparams[constants.BE_MEMORY])
@@ -455,16 +501,18 @@ class XenPvmHypervisor(XenHypervisor):
 
     vif_data = []
     for nic in instance.nics:
-      nic_str = "mac=%s, bridge=%s" % (nic.mac, nic.bridge)
+      nic_str = "mac=%s" % (nic.mac)
       ip = getattr(nic, "ip", None)
       if ip is not None:
         nic_str += ", ip=%s" % ip
+      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
+        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
       vif_data.append("'%s'" % nic_str)
 
+    disk_data = cls._GetConfigFileDiskData(block_devices)
+
     config.write("vif = [%s]\n" % ",".join(vif_data))
-    config.write("disk = [%s]\n" % ",".join(
-                 cls._GetConfigFileDiskData(instance.disk_template,
-                                            block_devices)))
+    config.write("disk = [%s]\n" % ",".join(disk_data))
 
     config.write("root = '%s'\n" % hvp[constants.HV_ROOT_PATH])
     config.write("on_poweroff = 'destroy'\n")
@@ -486,26 +534,31 @@ class XenPvmHypervisor(XenHypervisor):
 class XenHvmHypervisor(XenHypervisor):
   """Xen HVM hypervisor interface"""
 
-  ANCILLARY_FILES = XenHypervisor.ANCILLARY_FILES + \
-    [constants.VNC_PASSWORD_FILE]
+  ANCILLARY_FILES = XenHypervisor.ANCILLARY_FILES + [
+    constants.VNC_PASSWORD_FILE,
+    ]
 
   PARAMETERS = {
     constants.HV_ACPI: hv_base.NO_CHECK,
-    constants.HV_BOOT_ORDER: (True, ) + \
-    (lambda x: x and len(x.strip("acdn")) == 0,
-     "Invalid boot order specified, must be one or more of [acdn]",
-     None, None),
+    constants.HV_BOOT_ORDER: (True, ) +
+      (lambda x: x and len(x.strip("acdn")) == 0,
+       "Invalid boot order specified, must be one or more of [acdn]",
+       None, None),
     constants.HV_CDROM_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
-    constants.HV_DISK_TYPE: \
-    hv_base.ParamInSet(True, constants.HT_HVM_VALID_DISK_TYPES),
-    constants.HV_NIC_TYPE: \
-    hv_base.ParamInSet(True, constants.HT_HVM_VALID_NIC_TYPES),
+    constants.HV_DISK_TYPE:
+      hv_base.ParamInSet(True, constants.HT_HVM_VALID_DISK_TYPES),
+    constants.HV_NIC_TYPE:
+      hv_base.ParamInSet(True, constants.HT_HVM_VALID_NIC_TYPES),
     constants.HV_PAE: hv_base.NO_CHECK,
-    constants.HV_VNC_BIND_ADDRESS: \
-    (False, utils.IsValidIP,
-     "VNC bind address is not a valid IP address", None, None),
+    constants.HV_VNC_BIND_ADDRESS:
+      (False, netutils.IsValidIP4,
+       "VNC bind address is not a valid IP address", None, None),
     constants.HV_KERNEL_PATH: hv_base.REQ_FILE_CHECK,
     constants.HV_DEVICE_MODEL: hv_base.REQ_FILE_CHECK,
+    constants.HV_VNC_PASSWORD_FILE: hv_base.REQ_FILE_CHECK,
+    constants.HV_MIGRATION_PORT: hv_base.NET_PORT_CHECK,
+    constants.HV_MIGRATION_MODE: hv_base.MIGRATION_MODE_CHECK,
+    constants.HV_USE_LOCALTIME: hv_base.NO_CHECK,
     }
 
   @classmethod
@@ -554,16 +607,18 @@ class XenHvmHypervisor(XenHypervisor):
       config.write("# vncdisplay = 1\n")
       config.write("vncunused = 1\n")
 
+    vnc_pwd_file = hvp[constants.HV_VNC_PASSWORD_FILE]
     try:
-      password = utils.ReadFile(constants.VNC_PASSWORD_FILE)
+      password = utils.ReadFile(vnc_pwd_file)
     except EnvironmentError, err:
       raise errors.HypervisorError("Failed to open VNC password file %s: %s" %
-                                   (constants.VNC_PASSWORD_FILE, err))
+                                   (vnc_pwd_file, err))
 
     config.write("vncpasswd = '%s'\n" % password.rstrip())
 
     config.write("serial = 'pty'\n")
-    config.write("localtime = 1\n")
+    if hvp[constants.HV_USE_LOCALTIME]:
+      config.write("localtime = 1\n")
 
     vif_data = []
     nic_type = hvp[constants.HV_NIC_TYPE]
@@ -575,15 +630,16 @@ class XenHvmHypervisor(XenHypervisor):
     else:
       nic_type_str = ", model=%s, type=ioemu" % nic_type
     for nic in instance.nics:
-      nic_str = "mac=%s, bridge=%s%s" % (nic.mac, nic.bridge, nic_type_str)
+      nic_str = "mac=%s%s" % (nic.mac, nic_type_str)
       ip = getattr(nic, "ip", None)
       if ip is not None:
         nic_str += ", ip=%s" % ip
+      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
+        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
       vif_data.append("'%s'" % nic_str)
 
     config.write("vif = [%s]\n" % ",".join(vif_data))
-    disk_data = cls._GetConfigFileDiskData(instance.disk_template,
-                                            block_devices)
+    disk_data = cls._GetConfigFileDiskData(block_devices)
     disk_type = hvp[constants.HV_DISK_TYPE]
     if disk_type in (None, constants.HT_DISK_IOEMU):
       replacement = ",ioemu:hd"