| 1 |
# Copyright (c), Michael DeHaan <[email protected]>, 2012-2013 |
| 2 |
# Copyright (c), Toshio Kuratomi <[email protected]> 2016 |
| 3 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import platform |
| 8 |
|
| 9 |
from ansible.module_utils import distro |
| 10 |
from ansible.module_utils.common._utils import get_all_subclasses |
| 11 |
|
| 12 |
|
| 13 |
__all__ = ('get_distribution', 'get_distribution_version', 'get_platform_subclass') |
| 14 |
|
| 15 |
|
| 16 |
def get_distribution(): |
| 17 |
""" |
| 18 |
Return the name of the distribution the module is running on. |
| 19 |
|
| 20 |
:rtype: NativeString or None |
| 21 |
:returns: Name of the distribution the module is running on |
| 22 |
|
| 23 |
This function attempts to determine what distribution the code is running |
| 24 |
on and return a string representing that value. If the platform is Linux |
| 25 |
and the distribution cannot be determined, it returns ``OtherLinux``. |
| 26 |
""" |
| 27 |
distribution = distro.id().capitalize() |
| 28 |
|
| 29 |
if platform.system() == 'Linux': |
| 30 |
if distribution == 'Amzn': |
| 31 |
distribution = 'Amazon' |
| 32 |
elif distribution == 'Rhel': |
| 33 |
distribution = 'Redhat' |
| 34 |
elif not distribution: |
| 35 |
distribution = 'OtherLinux' |
| 36 |
|
| 37 |
return distribution |
| 38 |
|
| 39 |
|
| 40 |
def get_distribution_version(): |
| 41 |
""" |
| 42 |
Get the version of the distribution the code is running on |
| 43 |
|
| 44 |
:rtype: NativeString or None |
| 45 |
:returns: A string representation of the version of the distribution. If it |
| 46 |
cannot determine the version, it returns an empty string. If this is not run on |
| 47 |
a Linux machine it returns None. |
| 48 |
""" |
| 49 |
version = None |
| 50 |
|
| 51 |
needs_best_version = frozenset(( |
| 52 |
u'centos', |
| 53 |
u'debian', |
| 54 |
)) |
| 55 |
|
| 56 |
version = distro.version() |
| 57 |
distro_id = distro.id() |
| 58 |
|
| 59 |
if version is not None: |
| 60 |
if distro_id in needs_best_version: |
| 61 |
version_best = distro.version(best=True) |
| 62 |
|
| 63 |
# CentoOS maintainers believe only the major version is appropriate |
| 64 |
# but Ansible users desire minor version information, e.g., 7.5. |
| 65 |
# https://github.com/ansible/ansible/issues/50141#issuecomment-449452781 |
| 66 |
if distro_id == u'centos': |
| 67 |
version = u'.'.join(version_best.split(u'.')[:2]) |
| 68 |
|
| 69 |
# Debian does not include minor version in /etc/os-release. |
| 70 |
# Bug report filed upstream requesting this be added to /etc/os-release |
| 71 |
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931197 |
| 72 |
if distro_id == u'debian': |
| 73 |
version = version_best |
| 74 |
|
| 75 |
else: |
| 76 |
version = u'' |
| 77 |
|
| 78 |
return version |
| 79 |
|
| 80 |
|
| 81 |
def get_distribution_codename(): |
| 82 |
""" |
| 83 |
Return the code name for this Linux Distribution |
| 84 |
|
| 85 |
:rtype: NativeString or None |
| 86 |
:returns: A string representation of the distribution's codename or None if not a Linux distro |
| 87 |
""" |
| 88 |
codename = None |
| 89 |
if platform.system() == 'Linux': |
| 90 |
# Until this gets merged and we update our bundled copy of distro: |
| 91 |
# https://github.com/nir0s/distro/pull/230 |
| 92 |
# Fixes Fedora 28+ not having a code name and Ubuntu Xenial Xerus needing to be "xenial" |
| 93 |
os_release_info = distro.os_release_info() |
| 94 |
codename = os_release_info.get('version_codename') |
| 95 |
|
| 96 |
if codename is None: |
| 97 |
codename = os_release_info.get('ubuntu_codename') |
| 98 |
|
| 99 |
if codename is None and distro.id() == 'ubuntu': |
| 100 |
lsb_release_info = distro.lsb_release_info() |
| 101 |
codename = lsb_release_info.get('codename') |
| 102 |
|
| 103 |
if codename is None: |
| 104 |
codename = distro.codename() |
| 105 |
if codename == u'': |
| 106 |
codename = None |
| 107 |
|
| 108 |
return codename |
| 109 |
|
| 110 |
|
| 111 |
def get_distribution_cpe_name(): |
| 112 |
""" |
| 113 |
Return the CPE (Common Platform Enumeration) name for this OS distribution |
| 114 |
as published in the ``CPE_NAME`` field of the os-release file, or None if |
| 115 |
it is not available (the field is unset or there is no os-release file). |
| 116 |
|
| 117 |
This reads the os-release file directly (via the bundled ``distro`` library) |
| 118 |
so it works for any OS that ships one, including non-Linux systems such as |
| 119 |
FreeBSD and Solaris. |
| 120 |
""" |
| 121 |
return distro.os_release_info().get('cpe_name') or None |
| 122 |
|
| 123 |
|
| 124 |
def get_platform_subclass(cls): |
| 125 |
""" |
| 126 |
Finds a subclass implementing desired functionality on the platform the code is running on |
| 127 |
|
| 128 |
:arg cls: Class to find an appropriate subclass for |
| 129 |
:returns: A class that implements the functionality on this platform |
| 130 |
|
| 131 |
Some Ansible modules have different implementations depending on the platform they run on. This |
| 132 |
function is used to select between the various implementations and choose one. You can look at |
| 133 |
the implementation of the Ansible :ref:`User module<user_module>` module for an example of how to use this. |
| 134 |
|
| 135 |
This function replaces ``basic.load_platform_subclass()``. When you port code, you need to |
| 136 |
change the callers to be explicit about instantiating the class. For instance, code in the |
| 137 |
Ansible User module changed from:: |
| 138 |
|
| 139 |
.. code-block:: python |
| 140 |
|
| 141 |
# Old |
| 142 |
class User: |
| 143 |
def __new__(cls, args, kwargs): |
| 144 |
return load_platform_subclass(User, args, kwargs) |
| 145 |
|
| 146 |
# New |
| 147 |
class User: |
| 148 |
def __new__(cls, *args, **kwargs): |
| 149 |
new_cls = get_platform_subclass(User) |
| 150 |
return super(cls, new_cls).__new__(new_cls) |
| 151 |
""" |
| 152 |
this_platform = platform.system() |
| 153 |
distribution = get_distribution() |
| 154 |
|
| 155 |
subclass = None |
| 156 |
|
| 157 |
# get the most specific superclass for this platform |
| 158 |
if distribution is not None: |
| 159 |
for sc in get_all_subclasses(cls): |
| 160 |
if sc.distribution is not None and sc.distribution == distribution and sc.platform == this_platform: |
| 161 |
subclass = sc |
| 162 |
if subclass is None: |
| 163 |
for sc in get_all_subclasses(cls): |
| 164 |
if sc.platform == this_platform and sc.distribution is None: |
| 165 |
subclass = sc |
| 166 |
if subclass is None: |
| 167 |
subclass = cls |
| 168 |
|
| 169 |
return subclass |