Merge branch 'devel-2.5'
[ganeti-local] / lib / storage.py
index e589dd8..d382be5 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
 #
 #
 
-# Copyright (C) 2009 Google Inc.
+# Copyright (C) 2009, 2011 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
 #
 # 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
 
 """
 
 
 """
 
+# pylint: disable=W0232,R0201
+
+# W0232, since we use these as singletons rather than object holding
+# data
+
+# R0201, for the same reason
+
+# TODO: FileStorage initialised with paths whereas the others not
 
 import logging
 
 
 import logging
 
@@ -50,7 +58,7 @@ class _Base:
     """
     raise NotImplementedError()
 
     """
     raise NotImplementedError()
 
-  def Modify(self, name, changes):
+  def Modify(self, name, changes): # pylint: disable=W0613
     """Modifies an entity within the storage unit.
 
     @type name: string
     """Modifies an entity within the storage unit.
 
     @type name: string
@@ -76,7 +84,7 @@ class _Base:
     raise NotImplementedError()
 
 
     raise NotImplementedError()
 
 
-class FileStorage(_Base):
+class FileStorage(_Base): # pylint: disable=W0223
   """File storage unit.
 
   """
   """File storage unit.
 
   """
@@ -125,10 +133,10 @@ class FileStorage(_Base):
     else:
       dirsize = None
 
     else:
       dirsize = None
 
-    if constants.SF_FREE in fields:
-      fsfree = utils.GetFreeFilesystemSpace(path)
+    if constants.SF_FREE in fields or constants.SF_SIZE in fields:
+      fsstats = utils.GetFilesystemStats(path)
     else:
     else:
-      fsfree = None
+      fsstats = None
 
     # Make sure to update constants.VALID_STORAGE_FIELDS when changing fields.
     for field_name in fields:
 
     # Make sure to update constants.VALID_STORAGE_FIELDS when changing fields.
     for field_name in fields:
@@ -139,7 +147,13 @@ class FileStorage(_Base):
         values.append(dirsize)
 
       elif field_name == constants.SF_FREE:
         values.append(dirsize)
 
       elif field_name == constants.SF_FREE:
-        values.append(fsfree)
+        values.append(fsstats[1])
+
+      elif field_name == constants.SF_SIZE:
+        values.append(fsstats[0])
+
+      elif field_name == constants.SF_ALLOCATABLE:
+        values.append(True)
 
       else:
         raise errors.StorageError("Unknown field: %r" % field_name)
 
       else:
         raise errors.StorageError("Unknown field: %r" % field_name)
@@ -147,9 +161,14 @@ class FileStorage(_Base):
     return values
 
 
     return values
 
 
-class _LvmBase(_Base):
+class _LvmBase(_Base): # pylint: disable=W0223
   """Base class for LVM storage containers.
 
   """Base class for LVM storage containers.
 
+  @cvar LIST_FIELDS: list of tuples consisting of three elements: SF_*
+      constants, lvm command output fields (list), and conversion
+      function or static value (for static value, the lvm output field
+      can be an empty list)
+
   """
   LIST_SEP = "|"
   LIST_COMMAND = None
   """
   LIST_SEP = "|"
   LIST_COMMAND = None
@@ -189,7 +208,8 @@ class _LvmBase(_Base):
 
     """
     field_to_idx = dict([(field_name, idx)
 
     """
     field_to_idx = dict([(field_name, idx)
-                         for (idx, (field_name, _, _)) in enumerate(fields_def)])
+                         for (idx, (field_name, _, _)) in
+                         enumerate(fields_def)])
 
     lvm_fields = []
 
 
     lvm_fields = []
 
@@ -199,9 +219,9 @@ class _LvmBase(_Base):
       except IndexError:
         raise errors.StorageError("Unknown field: %r" % field_name)
 
       except IndexError:
         raise errors.StorageError("Unknown field: %r" % field_name)
 
-      (_, lvm_name, _) = fields_def[idx]
+      (_, lvm_names, _) = fields_def[idx]
 
 
-      lvm_fields.append(lvm_name)
+      lvm_fields.extend(lvm_names)
 
     return utils.UniqueSequence(lvm_fields)
 
 
     return utils.UniqueSequence(lvm_fields)
 
@@ -222,21 +242,34 @@ class _LvmBase(_Base):
     lvm_name_to_idx = dict([(lvm_name, idx)
                            for (idx, lvm_name) in enumerate(lvm_fields)])
     field_to_idx = dict([(field_name, idx)
     lvm_name_to_idx = dict([(lvm_name, idx)
                            for (idx, lvm_name) in enumerate(lvm_fields)])
     field_to_idx = dict([(field_name, idx)
-                         for (idx, (field_name, _, _)) in enumerate(fields_def)])
+                         for (idx, (field_name, _, _)) in
+                         enumerate(fields_def)])
 
     data = []
     for raw_data in cmd_result:
       row = []
 
       for field_name in wanted_field_names:
 
     data = []
     for raw_data in cmd_result:
       row = []
 
       for field_name in wanted_field_names:
-        (_, lvm_name, convert_fn) = fields_def[field_to_idx[field_name]]
-
-        value = raw_data[lvm_name_to_idx[lvm_name]]
-
-        if convert_fn:
-          value = convert_fn(value)
-
-        row.append(value)
+        (_, lvm_names, mapper) = fields_def[field_to_idx[field_name]]
+
+        values = [raw_data[lvm_name_to_idx[i]] for i in lvm_names]
+
+        if callable(mapper):
+          # we got a function, call it with all the declared fields
+          val = mapper(*values) # pylint: disable=W0142
+        elif len(values) == 1:
+          assert mapper is None, ("Invalid mapper value (neither callable"
+                                  " nor None) for one-element fields")
+          # we don't have a function, but we had a single field
+          # declared, pass it unchanged
+          val = values[0]
+        else:
+          # let's make sure there are no fields declared (cannot map >
+          # 1 field without a function)
+          assert not values, "LVM storage has multi-fields without a function"
+          val = mapper
+
+        row.append(val)
 
       data.append(row)
 
 
       data.append(row)
 
@@ -295,32 +328,39 @@ class _LvmBase(_Base):
       fields = line.strip().split(sep)
 
       if len(fields) != fieldcount:
       fields = line.strip().split(sep)
 
       if len(fields) != fieldcount:
+        logging.warning("Invalid line returned from lvm command: %s", line)
         continue
 
       yield fields
 
 
         continue
 
       yield fields
 
 
-class LvmPvStorage(_LvmBase):
-  """LVM Physical Volume storage unit.
+def _LvmPvGetAllocatable(attr):
+  """Determines whether LVM PV is allocatable.
+
+  @rtype: bool
 
   """
 
   """
-  def _GetAllocatable(attr):
-    if attr:
-      return (attr[0] == "a")
-    else:
-      logging.warning("Invalid PV attribute: %r", attr)
-      return False
+  if attr:
+    return (attr[0] == "a")
+  else:
+    logging.warning("Invalid PV attribute: %r", attr)
+    return False
 
 
+
+class LvmPvStorage(_LvmBase): # pylint: disable=W0223
+  """LVM Physical Volume storage unit.
+
+  """
   LIST_COMMAND = "pvs"
 
   # Make sure to update constants.VALID_STORAGE_FIELDS when changing field
   # definitions.
   LIST_FIELDS = [
   LIST_COMMAND = "pvs"
 
   # Make sure to update constants.VALID_STORAGE_FIELDS when changing field
   # definitions.
   LIST_FIELDS = [
-    (constants.SF_NAME, "pv_name", None),
-    (constants.SF_SIZE, "pv_size", _ParseSize),
-    (constants.SF_USED, "pv_used", _ParseSize),
-    (constants.SF_FREE, "pv_free", _ParseSize),
-    (constants.SF_ALLOCATABLE, "pv_attr", _GetAllocatable),
+    (constants.SF_NAME, ["pv_name"], None),
+    (constants.SF_SIZE, ["pv_size"], _ParseSize),
+    (constants.SF_USED, ["pv_used"], _ParseSize),
+    (constants.SF_FREE, ["pv_free"], _ParseSize),
+    (constants.SF_ALLOCATABLE, ["pv_attr"], _LvmPvGetAllocatable),
     ]
 
   def _SetAllocatable(self, name, allocatable):
     ]
 
   def _SetAllocatable(self, name, allocatable):
@@ -370,8 +410,12 @@ class LvmVgStorage(_LvmBase):
   # Make sure to update constants.VALID_STORAGE_FIELDS when changing field
   # definitions.
   LIST_FIELDS = [
   # Make sure to update constants.VALID_STORAGE_FIELDS when changing field
   # definitions.
   LIST_FIELDS = [
-    (constants.SF_NAME, "vg_name", None),
-    (constants.SF_SIZE, "vg_size", _ParseSize),
+    (constants.SF_NAME, ["vg_name"], None),
+    (constants.SF_SIZE, ["vg_size"], _ParseSize),
+    (constants.SF_FREE, ["vg_free"], _ParseSize),
+    (constants.SF_USED, ["vg_size", "vg_free"],
+     lambda x, y: _ParseSize(x) - _ParseSize(y)),
+    (constants.SF_ALLOCATABLE, [], True),
     ]
 
   def _RemoveMissing(self, name):
     ]
 
   def _RemoveMissing(self, name):