Streamlining handling of spindles and default templates
[ganeti-local] / lib / backend.py
index 5051903..a16b22a 100644 (file)
 
 """
 
-# pylint: disable=E1103
+# pylint: disable=E1103,C0302
 
 # E1103: %s %r has no %r member (but some types could not be
 # inferred), because the _TryOSFromDisk returns either (True, os_obj)
 # or (False, "string") which confuses pylint
 
+# C0302: This module has become too big and should be split up
+
 
 import os
 import os.path
@@ -86,7 +88,7 @@ _IES_PID_FILE = "pid"
 _IES_CA_FILE = "ca"
 
 #: Valid LVS output line regex
-_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
+_LVSLINE_REGEX = re.compile(r"^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
 
 # Actions for the master setup script
 _MASTER_START = "start"
@@ -1134,6 +1136,18 @@ def VerifyNode(what, cluster_name, all_hvparams):
     result[constants.NV_ACCEPTED_STORAGE_PATHS] = \
         filestorage.ComputeWrongFileStoragePaths()
 
+  if what.get(constants.NV_FILE_STORAGE_PATH):
+    pathresult = filestorage.CheckFileStoragePath(
+        what[constants.NV_FILE_STORAGE_PATH])
+    if pathresult:
+      result[constants.NV_FILE_STORAGE_PATH] = pathresult
+
+  if what.get(constants.NV_SHARED_FILE_STORAGE_PATH):
+    pathresult = filestorage.CheckFileStoragePath(
+        what[constants.NV_SHARED_FILE_STORAGE_PATH])
+    if pathresult:
+      result[constants.NV_SHARED_FILE_STORAGE_PATH] = pathresult
+
   return result
 
 
@@ -1604,7 +1618,7 @@ def _GatherAndLinkBlockDevs(instance):
   devices must be already assembled.
 
   @type instance: L{objects.Instance}
-  @param instance: the instance whose disks we shoul assemble
+  @param instance: the instance whose disks we should assemble
   @rtype: list
   @return: list of (disk_object, device_path)
 
@@ -1622,7 +1636,7 @@ def _GatherAndLinkBlockDevs(instance):
       raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
                                     e.strerror)
 
-    block_devices.append((disk, link_name))
+    block_devices.append((disk, link_name, device))
 
   return block_devices
 
@@ -2425,13 +2439,13 @@ def BlockdevGetdimensions(disks):
   return result
 
 
-def BlockdevExport(disk, dest_node, dest_path, cluster_name):
+def BlockdevExport(disk, dest_node_ip, dest_path, cluster_name):
   """Export a block device to a remote node.
 
   @type disk: L{objects.Disk}
   @param disk: the description of the disk to export
-  @type dest_node: str
-  @param dest_node: the destination node to export to
+  @type dest_node_ip: str
+  @param dest_node_ip: the destination node IP to export to
   @type dest_path: str
   @param dest_path: the destination path on the target node
   @type cluster_name: str
@@ -2455,7 +2469,7 @@ def BlockdevExport(disk, dest_node, dest_path, cluster_name):
   destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
                                 " oflag=dsync", dest_path)
 
-  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
+  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node_ip,
                                                    constants.SSH_LOGIN_USER,
                                                    destcmd)
 
@@ -2833,11 +2847,11 @@ def OSEnvironment(instance, inst_os, debug=0):
     if constants.HV_DISK_TYPE in instance.hvparams:
       result["DISK_%d_FRONTEND_TYPE" % idx] = \
         instance.hvparams[constants.HV_DISK_TYPE]
-    if disk.dev_type in constants.LDS_BLOCK:
+    if disk.dev_type in constants.DTS_BLOCK:
       result["DISK_%d_BACKEND_TYPE" % idx] = "block"
-    elif disk.dev_type == constants.LD_FILE:
+    elif disk.dev_type in [constants.DT_FILE, constants.DT_SHARED_FILE]:
       result["DISK_%d_BACKEND_TYPE" % idx] = \
-        "file:%s" % disk.physical_id[0]
+        "file:%s" % disk.logical_id[0]
 
   # NICs
   for idx, nic in enumerate(instance.nics):
@@ -2957,12 +2971,12 @@ def BlockdevSnapshot(disk):
   @return: snapshot disk ID as (vg, lv)
 
   """
-  if disk.dev_type == constants.LD_DRBD8:
+  if disk.dev_type == constants.DT_DRBD8:
     if not disk.children:
       _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
             disk.unique_id)
     return BlockdevSnapshot(disk.children[0])
-  elif disk.dev_type == constants.LD_LV:
+  elif disk.dev_type == constants.DT_PLAIN:
     r_dev = _RecursiveFindBD(disk)
     if r_dev is not None:
       # FIXME: choose a saner value for the snapshot size
@@ -3062,7 +3076,7 @@ def FinalizeExport(instance, snap_disks):
       config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
                  ("%s" % disk.iv_name))
       config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
-                 ("%s" % disk.physical_id[1]))
+                 ("%s" % disk.logical_id[1]))
       config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
                  ("%d" % disk.size))
 
@@ -3145,11 +3159,9 @@ def BlockdevRename(devlist):
   """Rename a list of block devices.
 
   @type devlist: list of tuples
-  @param devlist: list of tuples of the form  (disk,
-      new_logical_id, new_physical_id); disk is an
-      L{objects.Disk} object describing the current disk,
-      and new logical_id/physical_id is the name we
-      rename it to
+  @param devlist: list of tuples of the form  (disk, new_unique_id); disk is
+      an L{objects.Disk} object describing the current disk, and new
+      unique_id is the name we rename it to
   @rtype: boolean
   @return: True if all renames succeeded, False otherwise
 
@@ -3620,8 +3632,6 @@ def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
 
     assert isinstance(disk_index, (int, long))
 
-    real_disk = _OpenRealBD(disk)
-
     inst_os = OSFromDisk(instance.os)
     env = OSEnvironment(instance, inst_os)
 
@@ -3631,6 +3641,7 @@ def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
       script = inst_os.import_script
 
     elif mode == constants.IEM_EXPORT:
+      real_disk = _OpenRealBD(disk)
       env["EXPORT_DEVICE"] = real_disk.dev_path
       env["EXPORT_INDEX"] = str(disk_index)
       script = inst_os.export_script
@@ -3858,35 +3869,31 @@ def CleanupImportExport(name):
   shutil.rmtree(status_dir, ignore_errors=True)
 
 
-def _SetPhysicalId(target_node_uuid, nodes_ip, disks):
-  """Sets the correct physical ID on all passed disks.
-
-  """
-  for cf in disks:
-    cf.SetPhysicalID(target_node_uuid, nodes_ip)
+def _FindDisks(disks):
+  """Finds attached L{BlockDev}s for the given disks.
 
+  @type disks: list of L{objects.Disk}
+  @param disks: the disk objects we need to find
 
-def _FindDisks(target_node_uuid, nodes_ip, disks):
-  """Sets the physical ID on disks and returns the block devices.
+  @return: list of L{BlockDev} objects or C{None} if a given disk
+           was not found or was no attached.
 
   """
-  _SetPhysicalId(target_node_uuid, nodes_ip, disks)
-
   bdevs = []
 
-  for cf in disks:
-    rd = _RecursiveFindBD(cf)
+  for disk in disks:
+    rd = _RecursiveFindBD(disk)
     if rd is None:
-      _Fail("Can't find device %s", cf)
+      _Fail("Can't find device %s", disk)
     bdevs.append(rd)
   return bdevs
 
 
-def DrbdDisconnectNet(target_node_uuid, nodes_ip, disks):
+def DrbdDisconnectNet(disks):
   """Disconnects the network on a list of drbd devices.
 
   """
-  bdevs = _FindDisks(target_node_uuid, nodes_ip, disks)
+  bdevs = _FindDisks(disks)
 
   # disconnect disks
   for rd in bdevs:
@@ -3897,12 +3904,11 @@ def DrbdDisconnectNet(target_node_uuid, nodes_ip, disks):
             err, exc=True)
 
 
-def DrbdAttachNet(target_node_uuid, nodes_ip, disks, instance_name,
-                  multimaster):
+def DrbdAttachNet(disks, instance_name, multimaster):
   """Attaches the network on a list of drbd devices.
 
   """
-  bdevs = _FindDisks(target_node_uuid, nodes_ip, disks)
+  bdevs = _FindDisks(disks)
 
   if multimaster:
     for idx, rd in enumerate(bdevs):
@@ -3960,7 +3966,7 @@ def DrbdAttachNet(target_node_uuid, nodes_ip, disks, instance_name,
         _Fail("Can't change to primary mode: %s", err)
 
 
-def DrbdWaitSync(target_node_uuid, nodes_ip, disks):
+def DrbdWaitSync(disks):
   """Wait until DRBDs have synchronized.
 
   """
@@ -3970,7 +3976,7 @@ def DrbdWaitSync(target_node_uuid, nodes_ip, disks):
       raise utils.RetryAgain()
     return stats
 
-  bdevs = _FindDisks(target_node_uuid, nodes_ip, disks)
+  bdevs = _FindDisks(disks)
 
   min_resync = 100
   alldone = True
@@ -3990,11 +3996,10 @@ def DrbdWaitSync(target_node_uuid, nodes_ip, disks):
   return (alldone, min_resync)
 
 
-def DrbdNeedsActivation(target_node_uuid, nodes_ip, disks):
+def DrbdNeedsActivation(disks):
   """Checks which of the passed disks needs activation and returns their UUIDs.
 
   """
-  _SetPhysicalId(target_node_uuid, nodes_ip, disks)
   faulty_disks = []
 
   for disk in disks:
@@ -4247,6 +4252,33 @@ def SetWatcherPause(until, _filename=pathutils.WATCHER_PAUSEFILE):
     utils.WriteFile(_filename, data="%d\n" % (until, ), mode=0644)
 
 
+def ConfigureOVS(ovs_name, ovs_link):
+  """Creates a OpenvSwitch on the node.
+
+  This function sets up a OpenvSwitch on the node with given name nad
+  connects it via a given eth device.
+
+  @type ovs_name: string
+  @param ovs_name: Name of the OpenvSwitch to create.
+  @type ovs_link: None or string
+  @param ovs_link: Ethernet device for outside connection (can be missing)
+
+  """
+  # Initialize the OpenvSwitch
+  result = utils.RunCmd(["ovs-vsctl", "add-br", ovs_name])
+  if result.failed:
+    _Fail("Failed to create openvswitch %s. Script return value: %s, output:"
+          " '%s'" % result.exit_code, result.output, log=True)
+
+  # And connect it to a physical interface, if given
+  if ovs_link:
+    result = utils.RunCmd(["ovs-vsctl", "add-port", ovs_name, ovs_link])
+    if result.failed:
+      _Fail("Failed to connect openvswitch to  interface %s. Script return"
+            " value: %s, output: '%s'" % ovs_link, result.exit_code,
+            result.output, log=True)
+
+
 class HooksRunner(object):
   """Hook runner.