Revision 7c3d51d4 lib/backend.py

b/lib/backend.py
886 886
  """Compute and return the api version of a given OS.
887 887

  
888 888
  This function will try to read the api version of the os given by
889
  the 'name' parameter. By default, it wil use the constants.OS_DIR
890
  as top-level directory for OSes, but this can be overriden by the
891
  use of the os_dir parameter. Return value will be either an
892
  integer denoting the version or None in the case when this is not
893
  a valid OS name.
889
  the 'name' parameter and residing in the 'os_dir' directory.
890

  
891
  By default if os_dir is not given it will search for a matching name in all
892
  the constants.OS_SEARCH_PATH directories. 
893

  
894
  Return value will be either an integer denoting the version or None in the
895
  case when this is not a valid OS name.
894 896

  
895 897
  """
896 898
  if os_dir is None:
897
    os_dir = os.path.sep.join([constants.OS_DIR, name])
899
    for base_dir in constants.OS_SEARCH_PATH:
900
      t_os_dir = os.path.sep.join([base_dir, name])
901
      if os.path.isdir(t_os_dir):
902
        os_dir = t_os_dir
903
        break
904
  
905
  if os_dir is None:
906
    raise errors.InvalidOS(name, "OS dir not found in search path")
898 907

  
899 908
  api_file = os.path.sep.join([os_dir, "ganeti_api_version"])
900 909

  
......
927 936
  return api_version
928 937

  
929 938

  
930
def DiagnoseOS(top_dir=None):
939
def DiagnoseOS(top_dirs=None):
931 940
  """Compute the validity for all OSes.
932 941

  
933
  For each name in the give top_dir parameter (if not given, defaults
934
  to constants.OS_DIR), it will return an object. If this is a valid
942
  For each name in all the given top directories (if not given defaults i
943
  to constants.OS_SEARCH_PATH it will return an object. If this is a valid
935 944
  os, the object will be an instance of the object.OS class. If not,
936 945
  it will be an instance of errors.InvalidOS and this signifies that
937 946
  this name does not correspond to a valid OS.
......
940 949
    list of objects
941 950

  
942 951
  """
943
  if top_dir is None:
944
    top_dir = constants.OS_DIR
952
  if top_dirs is None:
953
    top_dirs = constants.OS_SEARCH_PATH
945 954

  
946
  try:
947
    f_names = os.listdir(top_dir)
948
  except EnvironmentError, err:
949
    logger.Error("Can't list the OS directory: %s" % str(err))
950
    return False
951 955
  result = []
952
  for name in f_names:
953
    try:
954
      os_inst = OSFromDisk(name, os.path.sep.join([top_dir, name]))
955
      result.append(os_inst)
956
    except errors.InvalidOS, err:
957
      result.append(err)
956
  for dir in top_dirs:
957
    if os.path.isdir(dir):
958
      try:
959
        f_names = os.listdir(dir)
960
      except EnvironmentError, err:
961
        logger.Error("Can't list the OS directory %s: %s" % (dir,str(err)))
962
        break
963
      for name in f_names:
964
        try:
965
          os_inst = OSFromDisk(name, os_dir=os.path.sep.join([dir, name]))
966
          result.append(os_inst)
967
        except errors.InvalidOS, err:
968
          result.append(err)
958 969

  
959 970
  return result
960 971

  
......
967 978
  `errors.InvalidOS` exception, detailing why this is not a valid
968 979
  OS.
969 980

  
981
  Args:
982
    os_dir: Directory containing the OS scripts. Defaults to a search
983
            in all the OS_SEARCH_PATH directories.
984

  
970 985
  """
986

  
987
  if os_dir is None:
988
    for base_dir in constants.OS_SEARCH_PATH:
989
      t_os_dir = os.path.sep.join([base_dir, name])
990
      if os.path.isdir(t_os_dir):
991
        os_dir = t_os_dir
992
        break
993

  
971 994
  if os_dir is None:
972
    os_dir = os.path.sep.join([constants.OS_DIR, name])
995
    raise errors.InvalidOS(name, "OS dir not found in search path")
973 996

  
974
  api_version = _OSOndiskVersion(name, os_dir)
997
  api_version = _OSOndiskVersion(name, os_dir=os_dir)
975 998

  
976 999
  if api_version != constants.OS_API_VERSION:
977 1000
    raise errors.InvalidOS(name, "API version mismatch (found %s want %s)"

Also available in: Unified diff