diff options
Diffstat (limited to 'ambari-common/src/main/python/ambari_commons/os_check.py')
-rw-r--r-- | ambari-common/src/main/python/ambari_commons/os_check.py | 126 |
1 files changed, 111 insertions, 15 deletions
diff --git a/ambari-common/src/main/python/ambari_commons/os_check.py b/ambari-common/src/main/python/ambari_commons/os_check.py index 78cf7844bd..03e94c4f91 100644 --- a/ambari-common/src/main/python/ambari_commons/os_check.py +++ b/ambari-common/src/main/python/ambari_commons/os_check.py @@ -36,16 +36,40 @@ def linux_distribution(): PYTHON_VER = sys.version_info[0] * 10 + sys.version_info[1] if PYTHON_VER < 26: - linux_distribution = platform.dist() + (distname, version, id) = platform.dist() elif os.path.exists('/etc/redhat-release'): - linux_distribution = platform.dist() + (distname, version, id) = platform.dist() else: - linux_distribution = platform.linux_distribution() + (distname, version, id) = platform.linux_distribution() - return linux_distribution + return (platform.system(), os.name, distname, version, id) +def windows_distribution(): + from os_windows import get_windows_version + + # Only support Windows Server 64 bit + (win_release, win_version, win_csd, win_ptype) = platform.win32_ver() + + if win_version.startswith("6.2."): + # win32_ver() doesn't work correctly for Windows Server 2012 R2 and Windows 8.1 + (win_ver_major, win_ver_minor, win_ver_build) = get_windows_version() + if win_ver_major == 6 and win_ver_minor == 3: + win_release = "2012ServerR2" + win_version = "%d.%d.%d" % (win_ver_major, win_ver_minor, win_ver_build) + + #if win_version + return (platform.system(), os.name, "win" + win_release, win_version, win_ptype) class OS_CONST_TYPE(type): + # os platforms + LINUX_OS = 'linux' + WINDOWS_OS = 'windows' + + # os families + REDHAT_FAMILY = 'redhat' + DEBIAN_FAMILY = 'debian' + SUSE_FAMILY = 'suse' + WINSRV_FAMILY = 'winsrv' # Declare here os type mapping OS_FAMILY_COLLECTION = [] @@ -58,7 +82,8 @@ class OS_CONST_TYPE(type): Initialize internal data structures from file """ try: - f = open(os.path.join(RESOURCES_DIR, OSFAMILY_JSON_RESOURCE)) + fpath = os.path.join(RESOURCES_DIR, OSFAMILY_JSON_RESOURCE) + f = open(fpath) json_data = json.load(f) f.close() for family in json_data: @@ -69,7 +94,7 @@ class OS_CONST_TYPE(type): 'os_list': json_data[family][JSON_OS_TYPE] }] except: - raise Exception("Couldn't load '%s' file" % OSFAMILY_JSON_RESOURCE) + raise Exception("Couldn't load '%s' file" % fpath) def __init__(cls, name, bases, dct): cls.initialize_data() @@ -89,17 +114,45 @@ class OS_CONST_TYPE(type): return name[:-7] raise Exception("Unknown class property '%s'" % name) +def get_os_distribution(): + if platform.system() == 'Windows': + dist = windows_distribution() + else: + if platform.system() == 'Mac': + raise Exception("MacOS not supported. Exiting...") + else: + # Linux + # Read content from /etc/*-release file + # Full release name + dist = linux_distribution() + return dist class OSConst: __metaclass__ = OS_CONST_TYPE class OSCheck: + _dist = get_os_distribution() + + @staticmethod + def get_os_os(): + """ + Return values: + windows, linux + + In case cannot detect - exit. + """ + # Read content from /etc/*-release file + # Full release name + os_os = OSCheck._dist[0].lower() + + return os_os @staticmethod def get_os_type(): """ Return values: + win2008server, win2012server, redhat, fedora, centos, oraclelinux, ascendos, amazon, xenserver, oel, ovs, cloudlinux, slc, scientific, psbm, ubuntu, debian, sles, sled, opensuse, suse ... and others @@ -108,8 +161,7 @@ class OSCheck: """ # Read content from /etc/*-release file # Full release name - dist = linux_distribution() - operatingSystem = dist[0].lower() + operatingSystem = OSCheck._dist[2].lower() # special cases if os.path.exists('/etc/oracle-release'): @@ -147,10 +199,7 @@ class OSCheck: In case cannot detect raises exception. """ - # Read content from /etc/*-release file - # Full release name - dist = linux_distribution() - dist = dist[1] + dist = OSCheck._dist[3] if dist: return dist @@ -173,8 +222,7 @@ class OSCheck: In case cannot detect raises exception. """ - dist = linux_distribution() - dist = dist[2].lower() + dist = OSCheck._dist[4].lower() if dist: return dist @@ -226,6 +274,48 @@ class OSCheck: return False @staticmethod + def is_windows_family(): + """ + Return true if it is so or false if not + + This is safe check for windows family, doesn't generate exception + """ + try: + if OSCheck.get_os_family() == OSConst.WINSRV_FAMILY: + return True + except Exception: + pass + return False + + @staticmethod + def is_linux_os(): + """ + Return true if it is so or false if not + + This is safe check for linux os, doesn't generate exception + """ + try: + if OSCheck.get_os_os() == OSConst.LINUX_OS: + return True + except Exception: + pass + return False + + @staticmethod + def is_windows_os(): + """ + Return true if it is so or false if not + + This is safe check for windows os, doesn't generate exception + """ + try: + if OSCheck.get_os_os() == OSConst.WINDOWS_OS: + return True + except Exception: + pass + return False + + @staticmethod def is_redhat7(): """ Return true if it is so or false if not @@ -238,4 +328,10 @@ class OSCheck: return True except Exception: pass - return False
\ No newline at end of file + return False + +# OS info +OS_VERSION = OSCheck().get_os_major_version() +OS_TYPE = OSCheck.get_os_type() +OS_FAMILY = OSCheck.get_os_family() +OS_OS = OSCheck.get_os_os() |