mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
Port from plaform.dist to ansible.module_utils.distro.linux_distribution
ci_complete
This commit is contained in:
parent
be65d9cfe5
commit
61b1daa65f
10 changed files with 248 additions and 114 deletions
|
@ -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):
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -46,7 +46,6 @@ EXAMPLES = '''
|
|||
# }
|
||||
'''
|
||||
|
||||
import platform
|
||||
|
||||
HAVE_XENAPI = False
|
||||
try:
|
||||
|
@ -55,6 +54,7 @@ try:
|
|||
except ImportError:
|
||||
pass
|
||||
|
||||
from ansible.module_utils import distro
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
|
@ -70,8 +70,7 @@ class XenServerFacts:
|
|||
|
||||
@property
|
||||
def version(self):
|
||||
# Be aware! Deprecated in Python 2.6!
|
||||
result = platform.dist()[1]
|
||||
result = distro.linux_distribution()[1]
|
||||
return result
|
||||
|
||||
@property
|
||||
|
|
|
@ -379,7 +379,6 @@ import errno
|
|||
import grp
|
||||
import os
|
||||
import re
|
||||
import platform
|
||||
import pty
|
||||
import pwd
|
||||
import select
|
||||
|
@ -388,6 +387,7 @@ import socket
|
|||
import subprocess
|
||||
import time
|
||||
|
||||
from ansible.module_utils import distro
|
||||
from ansible.module_utils._text import to_native, to_bytes, to_text
|
||||
from ansible.module_utils.basic import load_platform_subclass, AnsibleModule
|
||||
|
||||
|
@ -569,7 +569,7 @@ class User(object):
|
|||
# errors from useradd trying to create a group when
|
||||
# USERGROUPS_ENAB is set in /etc/login.defs.
|
||||
if os.path.exists('/etc/redhat-release'):
|
||||
dist = platform.dist()
|
||||
dist = distro.linux_distribution(full_distribution_name=False)
|
||||
major_release = int(dist[1].split('.')[0])
|
||||
if major_release <= 5:
|
||||
cmd.append('-n')
|
||||
|
@ -578,7 +578,7 @@ class User(object):
|
|||
elif os.path.exists('/etc/SuSE-release'):
|
||||
# -N did not exist in useradd before SLE 11 and did not
|
||||
# automatically create a group
|
||||
dist = platform.dist()
|
||||
dist = distro.linux_distribution(full_distribution_name=False)
|
||||
major_release = int(dist[1].split('.')[0])
|
||||
if major_release >= 12:
|
||||
cmd.append('-N')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue