Generate a shared HMAC key at cluster init time
[ganeti-local] / lib / hypervisor / hv_fake.py
index 70cef2b..38599d7 100644 (file)
@@ -25,7 +25,6 @@
 
 import os
 import os.path
-import re
 
 from ganeti import utils
 from ganeti import constants
@@ -56,21 +55,20 @@ class FakeHypervisor(hv_base.BaseHypervisor):
   def GetInstanceInfo(self, instance_name):
     """Get instance properties.
 
-    Args:
-      instance_name: the instance name
+    @param instance_name: the instance name
+
+    @return: tuple of (name, id, memory, vcpus, stat, times)
 
-    Returns:
-      (name, id, memory, vcpus, stat, times)
     """
     file_name = "%s/%s" % (self._ROOT_DIR, instance_name)
     if not os.path.exists(file_name):
       return None
     try:
-      fh = file(file_name, "r")
+      fh = open(file_name, "r")
       try:
         inst_id = fh.readline().strip()
-        memory = fh.readline().strip()
-        vcpus = fh.readline().strip()
+        memory = utils.TryConvert(int, fh.readline().strip())
+        vcpus = utils.TryConvert(fh.readline().strip())
         stat = "---b-"
         times = "0"
         return (instance_name, inst_id, memory, vcpus, stat, times)
@@ -83,21 +81,22 @@ class FakeHypervisor(hv_base.BaseHypervisor):
   def GetAllInstancesInfo(self):
     """Get properties of all instances.
 
-    Returns:
-      [(name, id, memory, vcpus, stat, times),...]
+    @return: list of tuples (name, id, memory, vcpus, stat, times)
+
     """
     data = []
     for file_name in os.listdir(self._ROOT_DIR):
       try:
-        fh = file(self._ROOT_DIR+"/"+file_name, "r")
+        fh = open(self._ROOT_DIR+"/"+file_name, "r")
         inst_id = "-1"
-        memory = "0"
+        memory = 0
+        vcpus = 1
         stat = "-----"
         times = "-1"
         try:
           inst_id = fh.readline().strip()
-          memory = fh.readline().strip()
-          vcpus = fh.readline().strip()
+          memory = utils.TryConvert(int, fh.readline().strip())
+          vcpus = utils.TryConvert(int, fh.readline().strip())
           stat = "---b-"
           times = "0"
         finally:
@@ -107,7 +106,7 @@ class FakeHypervisor(hv_base.BaseHypervisor):
         raise errors.HypervisorError("Failed to list instances: %s" % err)
     return data
 
-  def StartInstance(self, instance, force, extra_args):
+  def StartInstance(self, instance, block_devices):
     """Start an instance.
 
     For the fake hypervisor, it just creates a file in the base dir,
@@ -122,7 +121,9 @@ class FakeHypervisor(hv_base.BaseHypervisor):
     try:
       fh = file(file_name, "w")
       try:
-        fh.write("0\n%d\n%d\n" % (instance.memory, instance.vcpus))
+        fh.write("0\n%d\n%d\n" %
+                 (instance.beparams[constants.BE_MEMORY],
+                  instance.beparams[constants.BE_VCPUS]))
       finally:
         fh.close()
     except IOError, err:
@@ -153,58 +154,23 @@ class FakeHypervisor(hv_base.BaseHypervisor):
   def GetNodeInfo(self):
     """Return information about the node.
 
-    The return value is a dict, which has to have the following items:
-      (all values in MiB)
-      - memory_total: the total memory size on the node
-      - memory_free: the available memory on the node for instances
-      - memory_dom0: the memory used by the node itself, if available
+    This is just a wrapper over the base GetLinuxNodeInfo method.
 
-    """
-    # global ram usage from the xm info command
-    # memory                 : 3583
-    # free_memory            : 747
-    # note: in xen 3, memory has changed to total_memory
-    try:
-      fh = file("/proc/meminfo")
-      try:
-        data = fh.readlines()
-      finally:
-        fh.close()
-    except IOError, err:
-      raise errors.HypervisorError("Failed to list node info: %s" % err)
-
-    result = {}
-    sum_free = 0
-    for line in data:
-      splitfields = line.split(":", 1)
-
-      if len(splitfields) > 1:
-        key = splitfields[0].strip()
-        val = splitfields[1].strip()
-        if key == 'MemTotal':
-          result['memory_total'] = int(val.split()[0])/1024
-        elif key in ('MemFree', 'Buffers', 'Cached'):
-          sum_free += int(val.split()[0])/1024
-        elif key == 'Active':
-          result['memory_dom0'] = int(val.split()[0])/1024
-    result['memory_free'] = sum_free
-
-    cpu_total = 0
-    try:
-      fh = open("/proc/cpuinfo")
-      try:
-        cpu_total = len(re.findall("(?m)^processor\s*:\s*[0-9]+\s*$",
-                                   fh.read()))
-      finally:
-        fh.close()
-    except EnvironmentError, err:
-      raise HypervisorError("Failed to list node info: %s" % err)
-    result['cpu_total'] = cpu_total
+    @return: a dict with the following keys (values in MiB):
+          - memory_total: the total memory size on the node
+          - memory_free: the available memory on the node for instances
+          - memory_dom0: the memory used by the node itself, if available
 
+    """
+    result = self.GetLinuxNodeInfo()
+    # substract running instances
+    all_instances = self.GetAllInstancesInfo()
+    result['memory_free'] -= min(result['memory_free'],
+                                 sum([row[2] for row in all_instances]))
     return result
 
-  @staticmethod
-  def GetShellCommandForConsole(instance):
+  @classmethod
+  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
     """Return a command for connecting to the console of an instance.
 
     """
@@ -219,3 +185,10 @@ class FakeHypervisor(hv_base.BaseHypervisor):
     """
     if not os.path.exists(self._ROOT_DIR):
       return "The required directory '%s' does not exist." % self._ROOT_DIR
+
+  @classmethod
+  def PowercycleNode(cls):
+    """Fake hypervisor powercycle, just a wrapper over Linux powercycle.
+
+    """
+    cls.LinuxPowercycle()