Do not install init script in PREFIX/bin.
[ganeti-local] / lib / backend.py
index 33daa23..068f964 100644 (file)
@@ -41,7 +41,6 @@ from ganeti import constants
 from ganeti import bdev
 from ganeti import objects
 from ganeti import ssconf
-from ganeti import _autoconf
 
 
 def StartMaster():
@@ -121,7 +120,7 @@ def AddNode(dsa, dsapub, rsa, rsapub, sshkey, sshpub):
   finally:
     f.close()
 
-  utils.RunCmd([_autoconf.INITD_SSH, "restart"])
+  utils.RunCmd([constants.SSH_INITD_SCRIPT, "restart"])
 
   return True
 
@@ -171,6 +170,12 @@ def GetNodeInfo(vgname):
   if hyp_info is not None:
     outputarray.update(hyp_info)
 
+  f = open("/proc/sys/kernel/random/boot_id", 'r')
+  try:
+    outputarray["bootid"] = f.read(128).rstrip("\n")
+  finally:
+    f.close()
+
   return outputarray
 
 
@@ -882,31 +887,49 @@ def _ErrnoOrStr(err):
     detail = str(err)
   return detail
 
+def _OSSearch(name, search_path=None):
+  """Search for OSes with the given name in the search_path.
+
+  Args:
+    name: The name of the OS to look for
+    search_path: List of dirs to search (defaults to constants.OS_SEARCH_PATH)
+
+  Returns:
+    The base_dir the OS resides in
+
+  """
+
+  if search_path is None:
+    search_path = constants.OS_SEARCH_PATH
+
+  for dir in search_path:
+    t_os_dir = os.path.sep.join([dir, name])
+    if os.path.isdir(t_os_dir):
+        return dir
+
+  return None
 
-def _OSOndiskVersion(name, os_dir=None):
+def _OSOndiskVersion(name, os_dir):
   """Compute and return the api version of a given OS.
 
   This function will try to read the api version of the os given by
-  the 'name' parameter. By default, it wil use the constants.OS_DIR
-  as top-level directory for OSes, but this can be overriden by the
-  use of the os_dir parameter. Return value will be either an
-  integer denoting the version or None in the case when this is not
-  a valid OS name.
+  the 'name' parameter and residing in the 'os_dir' directory.
+
+  Return value will be either an integer denoting the version or None in the
+  case when this is not a valid OS name.
 
   """
-  if os_dir is None:
-    os_dir = os.path.sep.join([constants.OS_DIR, name])
 
   api_file = os.path.sep.join([os_dir, "ganeti_api_version"])
 
   try:
     st = os.stat(api_file)
   except EnvironmentError, err:
-    raise errors.InvalidOS(name, "'ganeti_api_version' file not"
+    raise errors.InvalidOS(name, os_dir, "'ganeti_api_version' file not"
                            " found (%s)" % _ErrnoOrStr(err))
 
   if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
-    raise errors.InvalidOS(name, "'ganeti_api_version' file is not"
+    raise errors.InvalidOS(name, os_dir, "'ganeti_api_version' file is not"
                            " a regular file")
 
   try:
@@ -916,23 +939,24 @@ def _OSOndiskVersion(name, os_dir=None):
     finally:
       f.close()
   except EnvironmentError, err:
-    raise errors.InvalidOS(name, "error while reading the"
+    raise errors.InvalidOS(name, os_dir, "error while reading the"
                            " API version (%s)" % _ErrnoOrStr(err))
 
   api_version = api_version.strip()
   try:
     api_version = int(api_version)
   except (TypeError, ValueError), err:
-    raise errors.InvalidOS(name, "API version is not integer (%s)" % str(err))
+    raise errors.InvalidOS(name, os_dir,
+                           "API version is not integer (%s)" % str(err))
 
   return api_version
 
 
-def DiagnoseOS(top_dir=None):
+def DiagnoseOS(top_dirs=None):
   """Compute the validity for all OSes.
 
-  For each name in the give top_dir parameter (if not given, defaults
-  to constants.OS_DIR), it will return an object. If this is a valid
+  For each name in all the given top directories (if not given defaults i
+  to constants.OS_SEARCH_PATH it will return an object. If this is a valid
   os, the object will be an instance of the object.OS class. If not,
   it will be an instance of errors.InvalidOS and this signifies that
   this name does not correspond to a valid OS.
@@ -941,26 +965,28 @@ def DiagnoseOS(top_dir=None):
     list of objects
 
   """
-  if top_dir is None:
-    top_dir = constants.OS_DIR
+  if top_dirs is None:
+    top_dirs = constants.OS_SEARCH_PATH
 
-  try:
-    f_names = os.listdir(top_dir)
-  except EnvironmentError, err:
-    logger.Error("Can't list the OS directory: %s" % str(err))
-    return False
   result = []
-  for name in f_names:
-    try:
-      os_inst = OSFromDisk(name, os.path.sep.join([top_dir, name]))
-      result.append(os_inst)
-    except errors.InvalidOS, err:
-      result.append(err)
+  for dir in top_dirs:
+    if os.path.isdir(dir):
+      try:
+        f_names = utils.ListVisibleFiles(dir)
+      except EnvironmentError, err:
+        logger.Error("Can't list the OS directory %s: %s" % (dir,str(err)))
+        break
+      for name in f_names:
+        try:
+          os_inst = OSFromDisk(name, base_dir=dir)
+          result.append(os_inst)
+        except errors.InvalidOS, err:
+          result.append(err)
 
   return result
 
 
-def OSFromDisk(name, os_dir=None):
+def OSFromDisk(name, base_dir=None):
   """Create an OS instance from disk.
 
   This function will return an OS instance if the given name is a
@@ -968,14 +994,24 @@ def OSFromDisk(name, os_dir=None):
   `errors.InvalidOS` exception, detailing why this is not a valid
   OS.
 
+  Args:
+    os_dir: Directory containing the OS scripts. Defaults to a search
+            in all the OS_SEARCH_PATH directories.
+
   """
-  if os_dir is None:
-    os_dir = os.path.sep.join([constants.OS_DIR, name])
 
+  if base_dir is None:
+    base_dir = _OSSearch(name)
+
+  if base_dir is None:
+    raise errors.InvalidOS(name, None, "OS dir not found in search path")
+
+  os_dir = os.path.sep.join([base_dir, name])
   api_version = _OSOndiskVersion(name, os_dir)
 
   if api_version != constants.OS_API_VERSION:
-    raise errors.InvalidOS(name, "API version mismatch (found %s want %s)"
+    raise errors.InvalidOS(name, os_dir, "API version mismatch"
+                           " (found %s want %s)"
                            % (api_version, constants.OS_API_VERSION))
 
   # OS Scripts dictionary, we will populate it with the actual script names
@@ -987,14 +1023,16 @@ def OSFromDisk(name, os_dir=None):
     try:
       st = os.stat(os_scripts[script])
     except EnvironmentError, err:
-      raise errors.InvalidOS(name, "'%s' script missing (%s)" %
+      raise errors.InvalidOS(name, os_dir, "'%s' script missing (%s)" %
                              (script, _ErrnoOrStr(err)))
 
     if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
-      raise errors.InvalidOS(name, "'%s' script not executable" % script)
+      raise errors.InvalidOS(name, os_dir, "'%s' script not executable" %
+                             script)
 
     if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
-      raise errors.InvalidOS(name, "'%s' is not a regular file" % script)
+      raise errors.InvalidOS(name, os_dir, "'%s' is not a regular file" %
+                             script)
 
 
   return objects.OS(name=name, path=os_dir,
@@ -1250,7 +1288,7 @@ def ListExports():
 
   """
   if os.path.isdir(constants.EXPORT_DIR):
-    return os.listdir(constants.EXPORT_DIR)
+    return utils.ListVisibleFiles(constants.EXPORT_DIR)
   else:
     return []
 
@@ -1360,7 +1398,7 @@ class HooksRunner(object):
     subdir = "%s-%s.d" % (hpath, suffix)
     dir_name = "%s/%s" % (self._BASE_DIR, subdir)
     try:
-      dir_contents = os.listdir(dir_name)
+      dir_contents = utils.ListVisibleFiles(dir_name)
     except OSError, err:
       # must log
       return rr