summaryrefslogtreecommitdiff
path: root/ambari-common/src/main/python/ambari_commons/os_check.py
diff options
context:
space:
mode:
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.py73
1 files changed, 55 insertions, 18 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 c5457bb2a5..b430c869d0 100644
--- a/ambari-common/src/main/python/ambari_commons/os_check.py
+++ b/ambari-common/src/main/python/ambari_commons/os_check.py
@@ -56,6 +56,8 @@ RESOURCES_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "resou
# family JSON data
OSFAMILY_JSON_RESOURCE = "os_family.json"
+JSON_OS_MAPPING = "mapping"
+JSON_OS_ALIASES = "aliases"
JSON_OS_TYPE = "distro"
JSON_OS_VERSION = "versions"
JSON_EXTENDS = "extends"
@@ -76,6 +78,8 @@ VER_NT_SERVER = 3
_IS_ORACLE_LINUX = os.path.exists('/etc/oracle-release')
_IS_REDHAT_LINUX = os.path.exists('/etc/redhat-release')
+SYSTEM_RELEASE_FILE = "/etc/system-release"
+
def _is_oracle_linux():
return _IS_ORACLE_LINUX
@@ -84,16 +88,16 @@ def _is_redhat_linux():
def advanced_check(distribution):
distribution = list(distribution)
- if os.path.exists("/etc/issue"):
- with open("/etc/issue", "rb") as fp:
+ if os.path.exists(SYSTEM_RELEASE_FILE):
+ with open(SYSTEM_RELEASE_FILE, "rb") as fp:
issue_content = fp.read()
if "Amazon" in issue_content:
distribution[0] = "amazon"
- search_groups = re.search('(\d+)\.(\d+)', issue_content)
+ search_groups = re.search('(\d+\.\d+)', issue_content)
if search_groups:
- distribution[1] = search_groups.group(1) # if version is 2015.09 only get 2015.
+ distribution[1] = search_groups.group(1)
return tuple(distribution)
@@ -114,16 +118,24 @@ class OS_CONST_TYPE(type):
f = open(os.path.join(RESOURCES_DIR, OSFAMILY_JSON_RESOURCE))
json_data = eval(f.read())
f.close()
- for family in json_data:
+
+ if JSON_OS_MAPPING not in json_data:
+ raise Exception("Invalid {0}".format(OSFAMILY_JSON_RESOURCE))
+
+ json_mapping_data = json_data[JSON_OS_MAPPING]
+
+ for family in json_mapping_data:
cls.FAMILY_COLLECTION += [family]
- cls.OS_COLLECTION += json_data[family][JSON_OS_TYPE]
+ cls.OS_COLLECTION += json_mapping_data[family][JSON_OS_TYPE]
cls.OS_FAMILY_COLLECTION += [{
'name': family,
- 'os_list': json_data[family][JSON_OS_TYPE]
+ 'os_list': json_mapping_data[family][JSON_OS_TYPE]
}]
- if JSON_EXTENDS in json_data[family]:
- cls.OS_FAMILY_COLLECTION[-1][JSON_EXTENDS] = json_data[family][JSON_EXTENDS]
+ if JSON_EXTENDS in json_mapping_data[family]:
+ cls.OS_FAMILY_COLLECTION[-1][JSON_EXTENDS] = json_mapping_data[family][JSON_EXTENDS]
+
+ cls.OS_TYPE_ALIASES = json_data[JSON_OS_ALIASES] if JSON_OS_ALIASES in json_data else {}
except:
raise Exception("Couldn't load '%s' file" % OSFAMILY_JSON_RESOURCE)
@@ -194,7 +206,24 @@ class OSCheck:
distribution = ("Darwin", "TestOnly", "1.1.1", "1.1.1", "1.1")
return distribution
-
+
+ @staticmethod
+ def get_alias(os_type, os_version):
+ version_parts = os_version.split('.')
+ full_os_and_major_version = os_type + version_parts[0]
+
+ if full_os_and_major_version in OSConst.OS_TYPE_ALIASES:
+ alias = OSConst.OS_TYPE_ALIASES[full_os_and_major_version]
+ re_groups = re.search('(\D+)(\d+)$', alias).groups()
+ os_type = re_groups[0]
+ os_major_version = re_groups[1]
+
+ version_parts[0] = os_major_version
+ os_version = '.'.join(version_parts)
+
+ return os_type, os_version
+
+
@staticmethod
def get_os_type():
"""
@@ -205,6 +234,10 @@ class OSCheck:
In case cannot detect - exit.
"""
+ return OSCheck.get_alias(OSCheck._get_os_type(), OSCheck._get_os_version())[0]
+
+ @staticmethod
+ def _get_os_type():
# Read content from /etc/*-release file
# Full release name
dist = OSCheck.os_distribution()
@@ -212,18 +245,18 @@ class OSCheck:
# special cases
if _is_oracle_linux():
- return 'oraclelinux'
+ operatingSystem = 'oraclelinux'
elif operatingSystem.startswith('suse linux enterprise server'):
- return 'sles'
+ operatingSystem = 'sles'
elif operatingSystem.startswith('red hat enterprise linux'):
- return 'redhat'
+ operatingSystem = 'redhat'
elif operatingSystem.startswith('darwin'):
- return 'mac'
+ operatingSystem = 'mac'
- if operatingSystem != '':
- return operatingSystem
- else:
+ if operatingSystem == '':
raise Exception("Cannot detect os type. Exiting...")
+
+ return operatingSystem
@staticmethod
def get_os_family():
@@ -257,11 +290,15 @@ class OSCheck:
In case cannot detect raises exception.
"""
+ return OSCheck.get_alias(OSCheck._get_os_type(), OSCheck._get_os_version())[1]
+
+ @staticmethod
+ def _get_os_version():
# Read content from /etc/*-release file
# Full release name
dist = OSCheck.os_distribution()
dist = dist[1]
-
+
if dist:
return dist
else: