better fix for arch version detection (#15705)

* better fix for arch version detection

fixes  #15696

* be extra safe about tracebacks in facts.py

* add comments to explain the setup
* make allowempty more conservative, ignore file content
* wrap function call in try/except
  * should never happen, but if it happens the bug should be distribtion=N/A and not a traceback
This commit is contained in:
Robin Roth 2016-05-04 21:32:08 +02:00 committed by Toshio Kuratomi
parent 4f7a0925fd
commit 56ba10365c

View file

@ -604,6 +604,10 @@ class Distribution(object):
This is unit tested. Please extend the tests to cover all distributions if you have them available. This is unit tested. Please extend the tests to cover all distributions if you have them available.
""" """
# every distribution name mentioned here, must have one of
# - allowempty == True
# - be listed in SEARCH_STRING
# - have a function get_distribution_DISTNAME implemented
OSDIST_LIST = ( OSDIST_LIST = (
{'path': '/etc/oracle-release', 'name': 'OracleLinux'}, {'path': '/etc/oracle-release', 'name': 'OracleLinux'},
{'path': '/etc/slackware-version', 'name': 'Slackware'}, {'path': '/etc/slackware-version', 'name': 'Slackware'},
@ -687,12 +691,12 @@ class Distribution(object):
if not os.path.exists(path): if not os.path.exists(path):
continue continue
# if allowempty is set, we only check for file existance but not content
if 'allowempty' in ddict and ddict['allowempty']:
self.facts['distribution'] = name
break
if os.path.getsize(path) == 0: if os.path.getsize(path) == 0:
if 'allowempty' in ddict and ddict['allowempty']: continue
self.facts['distribution'] = name
break
else:
continue
data = get_file_content(path) data = get_file_content(path)
if name in self.SEARCH_STRING: if name in self.SEARCH_STRING:
@ -707,13 +711,19 @@ class Distribution(object):
break break
else: else:
# call a dedicated function for parsing the file content # call a dedicated function for parsing the file content
distfunc = getattr(self, 'get_distribution_' + name) try:
parsed = distfunc(name, data, path) distfunc = getattr(self, 'get_distribution_' + name)
if parsed is None or parsed: parsed = distfunc(name, data, path)
# distfunc return False if parsing failed if parsed is None or parsed:
# break only if parsing was succesful # distfunc return False if parsing failed
# otherwise continue with other distributions # break only if parsing was succesful
break # otherwise continue with other distributions
break
except AttributeError:
# this should never happen, but if it does fail quitely and not with a traceback
pass
# to debug multiple matching release files, one can use: # to debug multiple matching release files, one can use:
# self.facts['distribution_debug'].append({path + ' ' + name: # self.facts['distribution_debug'].append({path + ' ' + name:
@ -780,10 +790,6 @@ class Distribution(object):
if release: if release:
self.facts['distribution_release'] = release.groups()[0] self.facts['distribution_release'] = release.groups()[0]
def get_distribution_Archlinux(self, name, data, path):
self.facts['distribution'] = 'Archlinux'
self.facts['distribution_version'] = data
def get_distribution_Alpine(self, name, data, path): def get_distribution_Alpine(self, name, data, path):
self.facts['distribution'] = 'Alpine' self.facts['distribution'] = 'Alpine'
self.facts['distribution_version'] = data self.facts['distribution_version'] = data