1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Rework distribution fact checking to be a little less heinous and add

support for Mandriva.

Fixes #9282
This commit is contained in:
Toshio Kuratomi 2014-10-10 10:28:07 -04:00
parent d19fe8d95d
commit 2b9e235ffd

View file

@ -85,16 +85,18 @@ class Facts(object):
_I386RE = re.compile(r'i[3456]86') _I386RE = re.compile(r'i[3456]86')
# For the most part, we assume that platform.dist() will tell the truth. # For the most part, we assume that platform.dist() will tell the truth.
# This is the fallback to handle unknowns or exceptions # This is the fallback to handle unknowns or exceptions
OSDIST_DICT = { '/etc/redhat-release': 'RedHat', OSDIST_LIST = ( ('/etc/redhat-release', 'RedHat'),
'/etc/vmware-release': 'VMwareESX', ('/etc/vmware-release', 'VMwareESX'),
'/etc/openwrt_release': 'OpenWrt', ('/etc/openwrt_release', 'OpenWrt'),
'/etc/system-release': 'OtherLinux', ('/etc/system-release', 'OtherLinux'),
'/etc/alpine-release': 'Alpine', ('/etc/alpine-release', 'Alpine'),
'/etc/release': 'Solaris', ('/etc/release', 'Solaris'),
'/etc/arch-release': 'Archlinux', ('/etc/arch-release', 'Archlinux'),
'/etc/SuSE-release': 'SuSE', ('/etc/SuSE-release', 'SuSE'),
'/etc/gentoo-release': 'Gentoo', ('/etc/os-release', 'SuSE'),
'/etc/os-release': 'Debian' } ('/etc/gentoo-release', 'Gentoo'),
('/etc/os-release', 'Debian'),
('/etc/lsb-release', 'Mandriva') )
SELINUX_MODE_DICT = { 1: 'enforcing', 0: 'permissive', -1: 'disabled' } SELINUX_MODE_DICT = { 1: 'enforcing', 0: 'permissive', -1: 'disabled' }
# A list of dicts. If there is a platform with more than one # A list of dicts. If there is a platform with more than one
@ -230,6 +232,8 @@ class Facts(object):
FreeBSD = 'FreeBSD', HPUX = 'HP-UX' FreeBSD = 'FreeBSD', HPUX = 'HP-UX'
) )
# TODO: Rewrite this to use the function references in a dict pattern
# as it's much cleaner than this massive if-else
if self.facts['system'] == 'AIX': if self.facts['system'] == 'AIX':
self.facts['distribution'] = 'AIX' self.facts['distribution'] = 'AIX'
rc, out, err = module.run_command("/usr/bin/oslevel") rc, out, err = module.run_command("/usr/bin/oslevel")
@ -268,21 +272,25 @@ class Facts(object):
self.facts['distribution_major_version'] = dist[1].split('.')[0] or 'NA' self.facts['distribution_major_version'] = dist[1].split('.')[0] or 'NA'
self.facts['distribution_release'] = dist[2] or 'NA' self.facts['distribution_release'] = dist[2] or 'NA'
# Try to handle the exceptions now ... # Try to handle the exceptions now ...
for (path, name) in Facts.OSDIST_DICT.items(): for (path, name) in Facts.OSDIST_LIST:
if os.path.exists(path) and os.path.getsize(path) > 0: if os.path.exists(path) and os.path.getsize(path) > 0:
if self.facts['distribution'] == 'Fedora': if self.facts['distribution'] in ('Fedora', ):
pass # Once we determine the value is one of these distros
# we trust the values are always correct
break
elif name == 'RedHat': elif name == 'RedHat':
data = get_file_content(path) data = get_file_content(path)
if 'Red Hat' in data: if 'Red Hat' in data:
self.facts['distribution'] = name self.facts['distribution'] = name
else: else:
self.facts['distribution'] = data.split()[0] self.facts['distribution'] = data.split()[0]
break
elif name == 'OtherLinux': elif name == 'OtherLinux':
data = get_file_content(path) data = get_file_content(path)
if 'Amazon' in data: if 'Amazon' in data:
self.facts['distribution'] = 'Amazon' self.facts['distribution'] = 'Amazon'
self.facts['distribution_version'] = data.split()[-1] self.facts['distribution_version'] = data.split()[-1]
break
elif name == 'OpenWrt': elif name == 'OpenWrt':
data = get_file_content(path) data = get_file_content(path)
if 'OpenWrt' in data: if 'OpenWrt' in data:
@ -293,12 +301,15 @@ class Facts(object):
release = re.search('DISTRIB_CODENAME="(.*)"', data) release = re.search('DISTRIB_CODENAME="(.*)"', data)
if release: if release:
self.facts['distribution_release'] = release.groups()[0] self.facts['distribution_release'] = release.groups()[0]
break
elif name == 'Alpine': elif name == 'Alpine':
data = get_file_content(path) data = get_file_content(path)
self.facts['distribution'] = 'Alpine' self.facts['distribution'] = name
self.facts['distribution_version'] = data self.facts['distribution_version'] = data
break
elif name == 'Solaris': elif name == 'Solaris':
data = get_file_content(path).split('\n')[0] data = get_file_content(path).split('\n')[0]
if 'Solaris' in data:
ora_prefix = '' ora_prefix = ''
if 'Oracle Solaris' in data: if 'Oracle Solaris' in data:
data = data.replace('Oracle ','') data = data.replace('Oracle ','')
@ -306,16 +317,39 @@ class Facts(object):
self.facts['distribution'] = data.split()[0] self.facts['distribution'] = data.split()[0]
self.facts['distribution_version'] = data.split()[1] self.facts['distribution_version'] = data.split()[1]
self.facts['distribution_release'] = ora_prefix + data self.facts['distribution_release'] = ora_prefix + data
break
elif name == 'SuSE': elif name == 'SuSE':
data = get_file_content(path).splitlines() data = get_file_content(path)
for line in data: if 'suse' in data.lower():
if '=' in line: if path == '/etc/os-release':
self.facts['distribution_release'] = line.split('=')[1].strip() release = re.search("PRETTY_NAME=[^(]+ \(?([^)]+?)\)", data)
elif name == 'Debian':
data = get_file_content(path).split('\n')[0]
release = re.search("PRETTY_NAME.+ \(?([^ ]+?)\)?\"", data)
if release: if release:
self.facts['distribution_release'] = release.groups()[0] self.facts['distribution_release'] = release.groups()[0]
break
elif path == '/etc/SuSE-release':
data = data.splitlines()
release = re.search('CODENAME *= *([^\n]+)\n', data)
if release:
self.facts['distribution_release'] = release.groups()[0].strip()
break
elif name == 'Debian':
data = get_file_content(path)
if 'Debian' in data:
release = re.search("PRETTY_NAME=[^(]+ \(?([^)]+?)\)", data)
if release:
self.facts['distribution_release'] = release.groups()[0]
break
elif name == 'Mandriva':
data = get_file_content(path)
if 'Mandriva' in data:
version = re.search('DISTRIB_RELEASE="(.*)"', data)
if version:
self.facts['distribution_version'] = version.groups()[0]
release = re.search('DISTRIB_CODENAME="(.*)"', data)
if release:
self.facts['distribution_release'] = release.groups()[0]
self.facts['distribution'] = name
break
else: else:
self.facts['distribution'] = name self.facts['distribution'] = name