Port from plaform.dist to ansible.module_utils.distro.linux_distribution

ci_complete
This commit is contained in:
Toshio Kuratomi 2018-12-10 17:04:14 -08:00
commit 61b1daa65f
10 changed files with 248 additions and 114 deletions

View file

@ -1,7 +1,17 @@
# Copyright (c), Michael DeHaan <michael.dehaan@gmail.com>, 2012-2013
# Copyright (c), Toshio Kuratomi <tkuratomi@ansible.com> 2016
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import os
import platform
from ansible.module_utils import distro
# Backwards compat. New code should just use platform.system()
def get_platform():
'''
:rtype: NativeString
@ -16,39 +26,60 @@ def get_distribution():
:returns: Name of the distribution the module is running on
'''
distribution = None
additional_linux = ('alpine', 'arch', 'devuan')
supported_dists = platform._supported_dists + additional_linux
if platform.system() == 'Linux':
try:
distribution = platform.linux_distribution(supported_dists=supported_dists)[0].capitalize()
if not distribution and os.path.isfile('/etc/system-release'):
distribution = platform.linux_distribution(supported_dists=['system'])[0].capitalize()
if 'Amazon' in distribution:
distribution = 'Amazon'
else:
distribution = 'OtherLinux'
except Exception:
# FIXME: MethodMissing, I assume?
distribution = platform.dist()[0].capitalize()
distribution = distro.name().capitalize()
# FIXME: Would we need to normalize these if we used: id() instead of name()?
distribution_words = distribution.split()
if 'Amazon' in distribution_words:
distribution = 'Amazon'
elif not distribution:
distribution = 'OtherLinux'
return distribution
def get_distribution_version():
'''
:rtype: NativeString or None
:returns: A string representation of the version of the distribution
:returns: A string representation of the version of the distribution. None if this is not run
on a Linux machine
'''
distribution_version = None
version = None
if platform.system() == 'Linux':
try:
distribution_version = platform.linux_distribution()[1]
if not distribution_version and os.path.isfile('/etc/system-release'):
distribution_version = platform.linux_distribution(supported_dists=['system'])[1]
except Exception:
# FIXME: MethodMissing, I assume?
distribution_version = platform.dist()[1]
return distribution_version
version = distro.version()
return version
def get_distribution_codename():
'''
Return the code name for this Linux Distribution
:rtype: NativeString or None
:returns: A string representation of the distribution's codename or None if not a Linux distro
'''
codename = None
if platform.system() == 'Linux':
# Until this gets merged and we update our bundled copy of distro:
# https://github.com/nir0s/distro/pull/230
# Fixes Fedora 28+ not having a code name and Ubuntu Xenial Xerus needing to be "xenial"
os_release_info = distro.os_release_info()
codename = os_release_info.get('version_codename')
if codename is None:
codename = os_release_info.get('ubuntu_codename')
if codename is None and distro.id() == 'ubuntu':
lsb_release_info = distro.lsb_release_info()
codename = lsb_release_info.get('codename')
if codename is None:
codename = distro.codename()
if codename == u'':
codename = None
return codename
def get_all_subclasses(cls):

View file

@ -20,8 +20,9 @@ import os
import platform
import re
from ansible.module_utils.common.sys_info import get_distribution, get_distribution_version, \
get_distribution_codename
from ansible.module_utils.facts.utils import get_file_content
from ansible.module_utils.facts.collector import BaseFactCollector
@ -111,7 +112,7 @@ class DistributionFiles:
dist_file_dict = {}
if name in self.SEARCH_STRING:
# look for the distribution string in the data and replace according to RELEASE_NAME_MAP
# only the distribution name is set, the version is assumed to be correct from platform.dist()
# only the distribution name is set, the version is assumed to be correct from distro.linux_distribution()
if self.SEARCH_STRING[name] in dist_file_content:
# this sets distribution=RedHat if 'Red Hat' shows up in data
dist_file_dict['distribution'] = name
@ -152,12 +153,15 @@ class DistributionFiles:
def _guess_distribution(self):
# try to find out which linux distribution this is
dist = platform.dist()
dist = (get_distribution(), get_distribution_version(), get_distribution_codename())
distribution_guess = {}
distribution_guess['distribution'] = dist[0].capitalize() or 'NA'
distribution_guess['distribution'] = dist[0] or 'NA'
distribution_guess['distribution_version'] = dist[1] or 'NA'
distribution_guess['distribution_major_version'] = dist[1].split('.')[0] or 'NA'
distribution_guess['distribution_release'] = dist[2] or 'NA'
distribution_guess['distribution_major_version'] = \
distribution_guess['distribution_version'].split('.')[0] or 'NA'
# distribution_release can be the empty string
distribution_guess['distribution_release'] = 'NA' if dist[2] is None else dist[2]
return distribution_guess
def process_dist_files(self):
@ -362,8 +366,7 @@ class DistributionFiles:
def parse_distribution_file_Coreos(self, name, data, path, collected_facts):
coreos_facts = {}
# FIXME: pass in ro copy of facts for this kind of thing
dist = platform.dist()
distro = dist[0]
distro = get_distribution()
if distro.lower() == 'coreos':
if not data: