Do not install init script in PREFIX/bin.
[ganeti-local] / lib / backend.py
index fdf72dd..068f964 100644 (file)
@@ -170,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
 
 
@@ -881,6 +887,27 @@ 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):
   """Compute and return the api version of a given OS.
@@ -898,11 +925,11 @@ def _OSOndiskVersion(name, os_dir):
   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:
@@ -912,14 +939,15 @@ def _OSOndiskVersion(name, os_dir):
     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
 
@@ -944,13 +972,13 @@ def DiagnoseOS(top_dirs=None):
   for dir in top_dirs:
     if os.path.isdir(dir):
       try:
-        f_names = os.listdir(dir)
+        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, os_dir=os.path.sep.join([dir, name]))
+          os_inst = OSFromDisk(name, base_dir=dir)
           result.append(os_inst)
         except errors.InvalidOS, err:
           result.append(err)
@@ -958,7 +986,7 @@ def DiagnoseOS(top_dirs=None):
   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
@@ -972,20 +1000,18 @@ def OSFromDisk(name, os_dir=None):
 
   """
 
-  if os_dir is None:
-    for base_dir in constants.OS_SEARCH_PATH:
-      t_os_dir = os.path.sep.join([base_dir, name])
-      if os.path.isdir(t_os_dir):
-        os_dir = t_os_dir
-        break
+  if base_dir is None:
+    base_dir = _OSSearch(name)
 
-  if os_dir is None:
-    raise errors.InvalidOS(name, "OS dir not found in search path")
+  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
@@ -997,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,
@@ -1260,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 []
 
@@ -1370,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