| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import os |
| 9 |
import platform |
| 10 |
import re |
| 11 |
import typing as t |
| 12 |
|
| 13 |
from ansible.module_utils.common.sys_info import get_distribution, get_distribution_version, \ |
| 14 |
get_distribution_codename |
| 15 |
from ansible.module_utils.facts.utils import get_file_content, get_file_lines |
| 16 |
from ansible.module_utils.facts.collector import BaseFactCollector |
| 17 |
|
| 18 |
|
| 19 |
def get_uname(module, flags=('-v')): |
| 20 |
if isinstance(flags, str): |
| 21 |
flags = flags.split() |
| 22 |
command = ['uname'] |
| 23 |
command.extend(flags) |
| 24 |
rc, out, err = module.run_command(command) |
| 25 |
if rc == 0: |
| 26 |
return out |
| 27 |
return None |
| 28 |
|
| 29 |
|
| 30 |
def _file_exists(path, allow_empty=False): |
| 31 |
# not finding the file, exit early |
| 32 |
if not os.path.isfile(path): |
| 33 |
return False |
| 34 |
|
| 35 |
# if just the path needs to exists (ie, it can be empty) we are done |
| 36 |
if allow_empty: |
| 37 |
return True |
| 38 |
|
| 39 |
# file exists but is empty and we dont allow_empty |
| 40 |
if os.path.getsize(path) == 0: |
| 41 |
return False |
| 42 |
|
| 43 |
# file exists with some content |
| 44 |
return True |
| 45 |
|
| 46 |
|
| 47 |
class DistributionFiles: |
| 48 |
"""has-a various distro file parsers (os-release, etc) and logic for finding the right one.""" |
| 49 |
# every distribution name mentioned here, must have one of |
| 50 |
# - allowempty == True |
| 51 |
# - be listed in SEARCH_STRING |
| 52 |
# - have a function get_distribution_DISTNAME implemented |
| 53 |
# keep names in sync with Conditionals page of docs |
| 54 |
OSDIST_LIST = ( |
| 55 |
{'path': '/etc/altlinux-release', 'name': 'Altlinux'}, |
| 56 |
{'path': '/etc/oracle-release', 'name': 'OracleLinux'}, |
| 57 |
{'path': '/etc/slackware-version', 'name': 'Slackware'}, |
| 58 |
{'path': '/etc/centos-release', 'name': 'CentOS'}, |
| 59 |
{'path': '/etc/redhat-release', 'name': 'RedHat'}, |
| 60 |
{'path': '/etc/vmware-release', 'name': 'VMwareESX', 'allowempty': True}, |
| 61 |
{'path': '/etc/openwrt_release', 'name': 'OpenWrt'}, |
| 62 |
{'path': '/etc/os-release', 'name': 'Amazon'}, |
| 63 |
{'path': '/etc/system-release', 'name': 'Amazon'}, |
| 64 |
{'path': '/etc/alpine-release', 'name': 'Alpine'}, |
| 65 |
{'path': '/etc/arch-release', 'name': 'Archlinux', 'allowempty': True}, |
| 66 |
{'path': '/etc/os-release', 'name': 'Archlinux'}, |
| 67 |
{'path': '/etc/os-release', 'name': 'SUSE'}, |
| 68 |
{'path': '/etc/SuSE-release', 'name': 'SUSE'}, |
| 69 |
{'path': '/etc/gentoo-release', 'name': 'Gentoo'}, |
| 70 |
{'path': '/etc/os-release', 'name': 'Debian'}, |
| 71 |
{'path': '/etc/lsb-release', 'name': 'Debian'}, |
| 72 |
{'path': '/etc/lsb-release', 'name': 'Mandriva'}, |
| 73 |
{'path': '/etc/sourcemage-release', 'name': 'SMGL'}, |
| 74 |
{'path': '/usr/lib/os-release', 'name': 'ClearLinux'}, |
| 75 |
{'path': '/etc/coreos/update.conf', 'name': 'Coreos'}, |
| 76 |
{'path': '/etc/os-release', 'name': 'Flatcar'}, |
| 77 |
{'path': '/etc/os-release', 'name': 'NA'}, |
| 78 |
) |
| 79 |
|
| 80 |
SEARCH_STRING = { |
| 81 |
'OracleLinux': 'Oracle Linux', |
| 82 |
'RedHat': 'Red Hat', |
| 83 |
'Altlinux': 'ALT', |
| 84 |
'SMGL': 'Source Mage GNU/Linux', |
| 85 |
} |
| 86 |
|
| 87 |
# We can't include this in SEARCH_STRING because a name match on its keys |
| 88 |
# causes a fallback to using the first whitespace separated item from the file content |
| 89 |
# as the name. For os-release, that is in form 'NAME=Arch' |
| 90 |
OS_RELEASE_ALIAS = { |
| 91 |
'Archlinux': 'Arch Linux' |
| 92 |
} |
| 93 |
|
| 94 |
STRIP_QUOTES = r'\'\"\\' |
| 95 |
|
| 96 |
def __init__(self, module): |
| 97 |
self.module = module |
| 98 |
|
| 99 |
def _get_file_content(self, path): |
| 100 |
return get_file_content(path) |
| 101 |
|
| 102 |
def _get_dist_file_content(self, path, allow_empty=False): |
| 103 |
# can't find that dist file, or it is incorrectly empty |
| 104 |
if not _file_exists(path, allow_empty=allow_empty): |
| 105 |
return False, None |
| 106 |
|
| 107 |
data = self._get_file_content(path) |
| 108 |
return True, data |
| 109 |
|
| 110 |
def _parse_dist_file(self, name, dist_file_content, path, collected_facts): |
| 111 |
dist_file_dict = {} |
| 112 |
dist_file_content = dist_file_content.strip(DistributionFiles.STRIP_QUOTES) |
| 113 |
if name in self.SEARCH_STRING: |
| 114 |
# look for the distribution string in the data and replace according to RELEASE_NAME_MAP |
| 115 |
# only the distribution name is set, the version is assumed to be correct from distro.linux_distribution() |
| 116 |
if self.SEARCH_STRING[name] in dist_file_content: |
| 117 |
# this sets distribution=RedHat if 'Red Hat' shows up in data |
| 118 |
dist_file_dict['distribution'] = name |
| 119 |
dist_file_dict['distribution_file_search_string'] = self.SEARCH_STRING[name] |
| 120 |
else: |
| 121 |
# this sets distribution to what's in the data, e.g. CentOS, Scientific, ... |
| 122 |
dist_file_dict['distribution'] = dist_file_content.split()[0] |
| 123 |
|
| 124 |
return True, dist_file_dict |
| 125 |
|
| 126 |
if name in self.OS_RELEASE_ALIAS: |
| 127 |
if self.OS_RELEASE_ALIAS[name] in dist_file_content: |
| 128 |
dist_file_dict['distribution'] = name |
| 129 |
return True, dist_file_dict |
| 130 |
return False, dist_file_dict |
| 131 |
|
| 132 |
# call a dedicated function for parsing the file content |
| 133 |
# TODO: replace with a map or a class |
| 134 |
try: |
| 135 |
# FIXME: most of these dont actually look at the dist file contents, but random other stuff |
| 136 |
distfunc_name = 'parse_distribution_file_' + name |
| 137 |
distfunc = getattr(self, distfunc_name) |
| 138 |
parsed, dist_file_dict = distfunc(name, dist_file_content, path, collected_facts) |
| 139 |
return parsed, dist_file_dict |
| 140 |
except AttributeError as exc: |
| 141 |
self.module.debug('exc: %s' % exc) |
| 142 |
# this should never happen, but if it does fail quietly and not with a traceback |
| 143 |
return False, dist_file_dict |
| 144 |
|
| 145 |
return True, dist_file_dict |
| 146 |
# to debug multiple matching release files, one can use: |
| 147 |
# self.facts['distribution_debug'].append({path + ' ' + name: |
| 148 |
# (parsed, |
| 149 |
# self.facts['distribution'], |
| 150 |
# self.facts['distribution_version'], |
| 151 |
# self.facts['distribution_release'], |
| 152 |
# )}) |
| 153 |
|
| 154 |
def _guess_distribution(self): |
| 155 |
# try to find out which linux distribution this is |
| 156 |
dist = (get_distribution(), get_distribution_version(), get_distribution_codename()) |
| 157 |
distribution_guess = { |
| 158 |
'distribution': dist[0] or 'NA', |
| 159 |
'distribution_version': dist[1] or 'NA', |
| 160 |
# distribution_release can be the empty string |
| 161 |
'distribution_release': 'NA' if dist[2] is None else dist[2] |
| 162 |
} |
| 163 |
|
| 164 |
distribution_guess['distribution_major_version'] = distribution_guess['distribution_version'].split('.')[0] or 'NA' |
| 165 |
return distribution_guess |
| 166 |
|
| 167 |
def process_dist_files(self): |
| 168 |
# Try to handle the exceptions now ... |
| 169 |
# self.facts['distribution_debug'] = [] |
| 170 |
dist_file_facts = {} |
| 171 |
|
| 172 |
dist_guess = self._guess_distribution() |
| 173 |
dist_file_facts.update(dist_guess) |
| 174 |
|
| 175 |
for ddict in self.OSDIST_LIST: |
| 176 |
name = ddict['name'] |
| 177 |
path = ddict['path'] |
| 178 |
allow_empty = ddict.get('allowempty', False) |
| 179 |
|
| 180 |
has_dist_file, dist_file_content = self._get_dist_file_content(path, allow_empty=allow_empty) |
| 181 |
|
| 182 |
# but we allow_empty. For example, ArchLinux with an empty /etc/arch-release and a |
| 183 |
# /etc/os-release with a different name |
| 184 |
if has_dist_file and allow_empty: |
| 185 |
dist_file_facts['distribution'] = name |
| 186 |
dist_file_facts['distribution_file_path'] = path |
| 187 |
dist_file_facts['distribution_file_variety'] = name |
| 188 |
break |
| 189 |
|
| 190 |
if not has_dist_file: |
| 191 |
# keep looking |
| 192 |
continue |
| 193 |
|
| 194 |
parsed_dist_file, parsed_dist_file_facts = self._parse_dist_file(name, dist_file_content, path, dist_file_facts) |
| 195 |
|
| 196 |
# finally found the right os dist file and were able to parse it |
| 197 |
if parsed_dist_file: |
| 198 |
dist_file_facts['distribution'] = name |
| 199 |
dist_file_facts['distribution_file_path'] = path |
| 200 |
# distribution and file_variety are the same here, but distribution |
| 201 |
# will be changed/mapped to a more specific name. |
| 202 |
# ie, dist=Fedora, file_variety=RedHat |
| 203 |
dist_file_facts['distribution_file_variety'] = name |
| 204 |
dist_file_facts['distribution_file_parsed'] = parsed_dist_file |
| 205 |
dist_file_facts.update(parsed_dist_file_facts) |
| 206 |
break |
| 207 |
|
| 208 |
return dist_file_facts |
| 209 |
|
| 210 |
# FIXME: split distro file parsing into its own module or class |
| 211 |
def parse_distribution_file_Slackware(self, name, data, path, collected_facts): |
| 212 |
slackware_facts = {} |
| 213 |
if 'Slackware' not in data: |
| 214 |
return False, slackware_facts # TODO: remove |
| 215 |
slackware_facts['distribution'] = name |
| 216 |
version = re.findall(r'\w+[.]\w+\+?', data) |
| 217 |
if version: |
| 218 |
slackware_facts['distribution_version'] = version[0] |
| 219 |
return True, slackware_facts |
| 220 |
|
| 221 |
def parse_distribution_file_Amazon(self, name, data, path, collected_facts): |
| 222 |
amazon_facts = {} |
| 223 |
if 'Amazon' not in data: |
| 224 |
return False, amazon_facts |
| 225 |
amazon_facts['distribution'] = 'Amazon' |
| 226 |
if path == '/etc/os-release': |
| 227 |
version = re.search(r"VERSION_ID=\"(.*)\"", data) |
| 228 |
if version: |
| 229 |
distribution_version = version.group(1) |
| 230 |
amazon_facts['distribution_version'] = distribution_version |
| 231 |
version_data = distribution_version.split(".") |
| 232 |
if len(version_data) > 1: |
| 233 |
major, minor = version_data |
| 234 |
else: |
| 235 |
major, minor = version_data[0], 'NA' |
| 236 |
|
| 237 |
amazon_facts['distribution_major_version'] = major |
| 238 |
amazon_facts['distribution_minor_version'] = minor |
| 239 |
else: |
| 240 |
version = [n for n in data.split() if n.isdigit()] |
| 241 |
version = version[0] if version else 'NA' |
| 242 |
amazon_facts['distribution_version'] = version |
| 243 |
|
| 244 |
return True, amazon_facts |
| 245 |
|
| 246 |
def parse_distribution_file_OpenWrt(self, name, data, path, collected_facts): |
| 247 |
openwrt_facts = {} |
| 248 |
if 'OpenWrt' not in data: |
| 249 |
return False, openwrt_facts # TODO: remove |
| 250 |
openwrt_facts['distribution'] = name |
| 251 |
version = re.search('DISTRIB_RELEASE="(.*)"', data) |
| 252 |
if version: |
| 253 |
openwrt_facts['distribution_version'] = version.groups()[0] |
| 254 |
release = re.search('DISTRIB_CODENAME="(.*)"', data) |
| 255 |
if release: |
| 256 |
openwrt_facts['distribution_release'] = release.groups()[0] |
| 257 |
return True, openwrt_facts |
| 258 |
|
| 259 |
def parse_distribution_file_Alpine(self, name, data, path, collected_facts): |
| 260 |
alpine_facts = {} |
| 261 |
alpine_facts['distribution'] = 'Alpine' |
| 262 |
alpine_facts['distribution_version'] = data |
| 263 |
return True, alpine_facts |
| 264 |
|
| 265 |
def parse_distribution_file_SUSE(self, name, data, path, collected_facts): |
| 266 |
suse_facts = {} |
| 267 |
if 'suse' not in data.lower(): |
| 268 |
return False, suse_facts # TODO: remove if tested without this |
| 269 |
if path == '/etc/os-release': |
| 270 |
for line in data.splitlines(): |
| 271 |
distribution = re.search("^NAME=(.*)", line) |
| 272 |
if distribution: |
| 273 |
suse_facts['distribution'] = distribution.group(1).strip('"') |
| 274 |
# example pattern are 13.04 13.0 13 |
| 275 |
distribution_version = re.search(r'^VERSION_ID="?([0-9]+\.?[0-9]*)"?', line) |
| 276 |
if distribution_version: |
| 277 |
suse_facts['distribution_version'] = distribution_version.group(1) |
| 278 |
suse_facts['distribution_major_version'] = distribution_version.group(1).split('.')[0] |
| 279 |
if 'open' in data.lower(): |
| 280 |
release = re.search(r'^VERSION_ID="?[0-9]+\.?([0-9]*)"?', line) |
| 281 |
if release: |
| 282 |
suse_facts['distribution_release'] = release.groups()[0] |
| 283 |
elif 'enterprise' in data.lower() and 'VERSION_ID' in line: |
| 284 |
# SLES doesn't got funny release names |
| 285 |
release = re.search(r'^VERSION_ID="?[0-9]+\.?([0-9]*)"?', line) |
| 286 |
if release.group(1): |
| 287 |
release = release.group(1) |
| 288 |
else: |
| 289 |
release = "0" # no minor number, so it is the first release |
| 290 |
suse_facts['distribution_release'] = release |
| 291 |
elif path == '/etc/SuSE-release': |
| 292 |
if 'open' in data.lower(): |
| 293 |
data = data.splitlines() |
| 294 |
distdata = get_file_content(path).splitlines()[0] |
| 295 |
suse_facts['distribution'] = distdata.split()[0] |
| 296 |
for line in data: |
| 297 |
release = re.search('CODENAME *= *([^\n]+)', line) |
| 298 |
if release: |
| 299 |
suse_facts['distribution_release'] = release.groups()[0].strip() |
| 300 |
elif 'enterprise' in data.lower(): |
| 301 |
lines = data.splitlines() |
| 302 |
distribution = lines[0].split()[0] |
| 303 |
if "Server" in data: |
| 304 |
suse_facts['distribution'] = "SLES" |
| 305 |
elif "Desktop" in data: |
| 306 |
suse_facts['distribution'] = "SLED" |
| 307 |
for line in lines: |
| 308 |
release = re.search('PATCHLEVEL = ([0-9]+)', line) # SLES doesn't got funny release names |
| 309 |
if release: |
| 310 |
suse_facts['distribution_release'] = release.group(1) |
| 311 |
suse_facts['distribution_version'] = collected_facts['distribution_version'] + '.' + release.group(1) |
| 312 |
|
| 313 |
# Check VARIANT_ID first for SLES4SAP or SL-Micro |
| 314 |
variant_id_match = re.search(r'^VARIANT_ID="?([^"\n]*)"?', data, re.MULTILINE) |
| 315 |
if variant_id_match: |
| 316 |
variant_id = variant_id_match.group(1) |
| 317 |
if variant_id in ('server-sap', 'sles-sap'): |
| 318 |
suse_facts['distribution'] = 'SLES_SAP' |
| 319 |
elif variant_id == 'transactional': |
| 320 |
suse_facts['distribution'] = 'SL-Micro' |
| 321 |
else: |
| 322 |
# Fallback for older SLES 15 using baseproduct symlink |
| 323 |
if os.path.islink('/etc/products.d/baseproduct'): |
| 324 |
resolved = os.path.realpath('/etc/products.d/baseproduct') |
| 325 |
if resolved.endswith('SLES_SAP.prod'): |
| 326 |
suse_facts['distribution'] = 'SLES_SAP' |
| 327 |
elif resolved.endswith('SL-Micro.prod'): |
| 328 |
suse_facts['distribution'] = 'SL-Micro' |
| 329 |
|
| 330 |
return True, suse_facts |
| 331 |
|
| 332 |
def parse_distribution_file_Debian(self, name, data, path, collected_facts): |
| 333 |
debian_facts = {} |
| 334 |
if any(distro in data for distro in ('Debian', 'Raspbian')): |
| 335 |
debian_facts['distribution'] = 'Debian' |
| 336 |
release = re.search(r"PRETTY_NAME=[^(]+ \(?([^)]+?)\)", data) |
| 337 |
if release: |
| 338 |
debian_facts['distribution_release'] = release.groups()[0] |
| 339 |
|
| 340 |
# Last resort: try to find release from tzdata as either lsb is missing or this is very old debian |
| 341 |
if collected_facts['distribution_release'] == 'NA' and 'Debian' in data: |
| 342 |
dpkg_cmd = self.module.get_bin_path('dpkg') |
| 343 |
if dpkg_cmd: |
| 344 |
cmd = "%s --status tzdata|grep Provides|cut -f2 -d'-'" % dpkg_cmd |
| 345 |
rc, out, err = self.module.run_command(cmd) |
| 346 |
if rc == 0: |
| 347 |
debian_facts['distribution_release'] = out.strip() |
| 348 |
debian_version_path = '/etc/debian_version' |
| 349 |
distdata = get_file_lines(debian_version_path) |
| 350 |
for line in distdata: |
| 351 |
m = re.search(r'(\d+)\.(\d+)', line.strip()) |
| 352 |
if m: |
| 353 |
debian_facts['distribution_minor_version'] = m.groups()[1] |
| 354 |
elif 'Ubuntu' in data: |
| 355 |
debian_facts['distribution'] = 'Ubuntu' |
| 356 |
# nothing else to do, Ubuntu gets correct info from python functions |
| 357 |
elif 'SteamOS' in data: |
| 358 |
debian_facts['distribution'] = 'SteamOS' |
| 359 |
# nothing else to do, SteamOS gets correct info from python functions |
| 360 |
elif path in ('/etc/lsb-release', '/etc/os-release') and ('Kali' in data or 'Parrot' in data): |
| 361 |
if 'Kali' in data: |
| 362 |
# Kali does not provide /etc/lsb-release anymore |
| 363 |
debian_facts['distribution'] = 'Kali' |
| 364 |
elif 'Parrot' in data: |
| 365 |
debian_facts['distribution'] = 'Parrot' |
| 366 |
release = re.search('DISTRIB_RELEASE=(.*)', data) |
| 367 |
if release: |
| 368 |
debian_facts['distribution_release'] = release.groups()[0] |
| 369 |
elif 'Devuan' in data: |
| 370 |
debian_facts['distribution'] = 'Devuan' |
| 371 |
release = re.search(r"PRETTY_NAME=\"?[^(\"]+ \(?([^) \"]+)\)?", data) |
| 372 |
if release: |
| 373 |
debian_facts['distribution_release'] = release.groups()[0] |
| 374 |
version = re.search(r"VERSION_ID=\"(.*)\"", data) |
| 375 |
if version: |
| 376 |
debian_facts['distribution_version'] = version.group(1) |
| 377 |
debian_facts['distribution_major_version'] = version.group(1) |
| 378 |
elif 'Cumulus' in data: |
| 379 |
debian_facts['distribution'] = 'Cumulus Linux' |
| 380 |
version = re.search(r"VERSION_ID=(.*)", data) |
| 381 |
if version: |
| 382 |
major, _minor, _dummy_ver = version.group(1).split(".") |
| 383 |
debian_facts['distribution_version'] = version.group(1) |
| 384 |
debian_facts['distribution_major_version'] = major |
| 385 |
|
| 386 |
release = re.search(r'VERSION="(.*)"', data) |
| 387 |
if release: |
| 388 |
debian_facts['distribution_release'] = release.groups()[0] |
| 389 |
elif "Mint" in data: |
| 390 |
debian_facts['distribution'] = 'Linux Mint' |
| 391 |
version = re.search(r"VERSION_ID=\"(.*)\"", data) |
| 392 |
if version: |
| 393 |
debian_facts['distribution_version'] = version.group(1) |
| 394 |
debian_facts['distribution_major_version'] = version.group(1).split('.')[0] |
| 395 |
elif 'UOS' in data or 'Uos' in data or 'uos' in data: |
| 396 |
debian_facts['distribution'] = 'Uos' |
| 397 |
release = re.search(r"VERSION_CODENAME=\"?([^\"]+)\"?", data) |
| 398 |
if release: |
| 399 |
debian_facts['distribution_release'] = release.groups()[0] |
| 400 |
version = re.search(r"VERSION_ID=\"(.*)\"", data) |
| 401 |
if version: |
| 402 |
debian_facts['distribution_version'] = version.group(1) |
| 403 |
debian_facts['distribution_major_version'] = version.group(1).split('.')[0] |
| 404 |
elif 'Deepin' in data or 'deepin' in data: |
| 405 |
debian_facts['distribution'] = 'Deepin' |
| 406 |
release = re.search(r"VERSION_CODENAME=\"?([^\"]+)\"?", data) |
| 407 |
if release: |
| 408 |
debian_facts['distribution_release'] = release.groups()[0] |
| 409 |
version = re.search(r"VERSION_ID=\"(.*)\"", data) |
| 410 |
if version: |
| 411 |
debian_facts['distribution_version'] = version.group(1) |
| 412 |
debian_facts['distribution_major_version'] = version.group(1).split('.')[0] |
| 413 |
elif 'LMDE' in data: |
| 414 |
debian_facts['distribution'] = 'Linux Mint Debian Edition' |
| 415 |
else: |
| 416 |
return False, debian_facts |
| 417 |
|
| 418 |
return True, debian_facts |
| 419 |
|
| 420 |
def parse_distribution_file_Mandriva(self, name, data, path, collected_facts): |
| 421 |
mandriva_facts = {} |
| 422 |
if 'Mandriva' in data: |
| 423 |
mandriva_facts['distribution'] = 'Mandriva' |
| 424 |
version = re.search('DISTRIB_RELEASE="(.*)"', data) |
| 425 |
if version: |
| 426 |
mandriva_facts['distribution_version'] = version.groups()[0] |
| 427 |
release = re.search('DISTRIB_CODENAME="(.*)"', data) |
| 428 |
if release: |
| 429 |
mandriva_facts['distribution_release'] = release.groups()[0] |
| 430 |
mandriva_facts['distribution'] = name |
| 431 |
else: |
| 432 |
return False, mandriva_facts |
| 433 |
|
| 434 |
return True, mandriva_facts |
| 435 |
|
| 436 |
def parse_distribution_file_NA(self, name, data, path, collected_facts): |
| 437 |
na_facts = {} |
| 438 |
for line in data.splitlines(): |
| 439 |
distribution = re.search("^NAME=(.*)", line) |
| 440 |
if distribution and name == 'NA': |
| 441 |
na_facts['distribution'] = distribution.group(1).strip(DistributionFiles.STRIP_QUOTES) |
| 442 |
version = re.search("^VERSION=(.*)", line) |
| 443 |
if version and collected_facts['distribution_version'] == 'NA': |
| 444 |
na_facts['distribution_version'] = version.group(1).strip(DistributionFiles.STRIP_QUOTES) |
| 445 |
return True, na_facts |
| 446 |
|
| 447 |
def parse_distribution_file_Coreos(self, name, data, path, collected_facts): |
| 448 |
coreos_facts = {} |
| 449 |
# FIXME: pass in ro copy of facts for this kind of thing |
| 450 |
distro = get_distribution() |
| 451 |
|
| 452 |
if distro.lower() == 'coreos': |
| 453 |
if not data: |
| 454 |
# include fix from #15230, #15228 |
| 455 |
# TODO: verify this is ok for above bugs |
| 456 |
return False, coreos_facts |
| 457 |
release = re.search("^GROUP=(.*)", data) |
| 458 |
if release: |
| 459 |
coreos_facts['distribution_release'] = release.group(1).strip('"') |
| 460 |
else: |
| 461 |
return False, coreos_facts # TODO: remove if tested without this |
| 462 |
|
| 463 |
return True, coreos_facts |
| 464 |
|
| 465 |
def parse_distribution_file_Flatcar(self, name, data, path, collected_facts): |
| 466 |
flatcar_facts = {} |
| 467 |
distro = get_distribution() |
| 468 |
|
| 469 |
if distro.lower() != 'flatcar': |
| 470 |
return False, flatcar_facts |
| 471 |
|
| 472 |
if not data: |
| 473 |
return False, flatcar_facts |
| 474 |
|
| 475 |
version = re.search("VERSION=(.*)", data) |
| 476 |
if version: |
| 477 |
flatcar_facts['distribution_major_version'] = version.group(1).strip('"').split('.')[0] |
| 478 |
flatcar_facts['distribution_version'] = version.group(1).strip('"') |
| 479 |
|
| 480 |
return True, flatcar_facts |
| 481 |
|
| 482 |
def parse_distribution_file_ClearLinux(self, name, data, path, collected_facts): |
| 483 |
clear_facts = {} |
| 484 |
if "clearlinux" not in name.lower(): |
| 485 |
return False, clear_facts |
| 486 |
|
| 487 |
pname = re.search('NAME="(.*)"', data) |
| 488 |
if pname: |
| 489 |
if 'Clear Linux' not in pname.groups()[0]: |
| 490 |
return False, clear_facts |
| 491 |
clear_facts['distribution'] = pname.groups()[0] |
| 492 |
else: |
| 493 |
return False, clear_facts |
| 494 |
version = re.search('VERSION_ID=(.*)', data) |
| 495 |
if version: |
| 496 |
clear_facts['distribution_major_version'] = version.groups()[0] |
| 497 |
clear_facts['distribution_version'] = version.groups()[0] |
| 498 |
release = re.search('ID=(.*)', data) |
| 499 |
if release: |
| 500 |
clear_facts['distribution_release'] = release.groups()[0] |
| 501 |
return True, clear_facts |
| 502 |
|
| 503 |
def parse_distribution_file_CentOS(self, name, data, path, collected_facts): |
| 504 |
centos_facts = {} |
| 505 |
|
| 506 |
if 'CentOS Stream' in data: |
| 507 |
centos_facts['distribution_release'] = 'Stream' |
| 508 |
return True, centos_facts |
| 509 |
|
| 510 |
if "TencentOS Server" in data: |
| 511 |
centos_facts['distribution'] = 'TencentOS' |
| 512 |
return True, centos_facts |
| 513 |
|
| 514 |
return False, centos_facts |
| 515 |
|
| 516 |
|
| 517 |
class Distribution(object): |
| 518 |
""" |
| 519 |
This subclass of Facts fills the distribution, distribution_version and distribution_release variables |
| 520 |
|
| 521 |
To do so it checks the existence and content of typical files in /etc containing distribution information |
| 522 |
|
| 523 |
This is unit tested. Please extend the tests to cover all distributions if you have them available. |
| 524 |
""" |
| 525 |
|
| 526 |
# keep keys in sync with Conditionals page of docs |
| 527 |
OS_FAMILY_MAP = {'RedHat': ['RedHat', 'RHEL', 'Fedora', 'CentOS', 'Scientific', 'SLC', |
| 528 |
'Ascendos', 'CloudLinux', 'PSBM', 'OracleLinux', 'OVS', |
| 529 |
'OEL', 'Amazon', 'Amzn', 'Virtuozzo', 'XenServer', 'Alibaba', |
| 530 |
'EulerOS', 'openEuler', 'AlmaLinux', 'Rocky', 'TencentOS', |
| 531 |
'EuroLinux', 'Kylin Linux Advanced Server', 'MIRACLE'], |
| 532 |
'Debian': ['Debian', 'Ubuntu', 'Raspbian', 'Neon', 'KDE neon', |
| 533 |
'Linux Mint', 'SteamOS', 'Devuan', 'Kali', 'Cumulus Linux', |
| 534 |
'Pop!_OS', 'Parrot', 'Pardus GNU/Linux', 'Uos', 'Deepin', 'OSMC', |
| 535 |
'Linux Mint Debian Edition', 'Univention Corporate Server'], |
| 536 |
'Suse': ['SuSE', 'SLES', 'SLED', 'openSUSE', 'openSUSE Tumbleweed', |
| 537 |
'SLES_SAP', 'SUSE_LINUX', 'openSUSE Leap', 'ALP-Dolomite', 'SL-Micro', |
| 538 |
'openSUSE MicroOS'], |
| 539 |
'Archlinux': ['Archlinux', 'Antergos', 'Manjaro'], |
| 540 |
'Mandrake': ['Mandrake', 'Mandriva'], |
| 541 |
'Solaris': ['Solaris', 'Nexenta', 'OmniOS', 'OpenIndiana', 'SmartOS'], |
| 542 |
'Slackware': ['Slackware'], |
| 543 |
'Altlinux': ['Altlinux'], |
| 544 |
'SMGL': ['SMGL'], |
| 545 |
'Gentoo': ['Gentoo', 'Funtoo'], |
| 546 |
'Alpine': ['Alpine'], |
| 547 |
'AIX': ['AIX'], |
| 548 |
'HP-UX': ['HPUX'], |
| 549 |
'Darwin': ['MacOSX'], |
| 550 |
'FreeBSD': ['FreeBSD', 'TrueOS'], |
| 551 |
'ClearLinux': ['Clear Linux OS', 'Clear Linux Mix'], |
| 552 |
'DragonFly': ['DragonflyBSD', 'DragonFlyBSD', 'Gentoo/DragonflyBSD', 'Gentoo/DragonFlyBSD'], |
| 553 |
'NetBSD': ['NetBSD'], } |
| 554 |
|
| 555 |
OS_FAMILY = {} |
| 556 |
for family, names in OS_FAMILY_MAP.items(): |
| 557 |
for name in names: |
| 558 |
OS_FAMILY[name] = family |
| 559 |
|
| 560 |
def __init__(self, module): |
| 561 |
self.module = module |
| 562 |
|
| 563 |
def get_distribution_facts(self): |
| 564 |
distribution_facts = {} |
| 565 |
|
| 566 |
# The platform module provides information about the running |
| 567 |
# system/distribution. Use this as a baseline and fix buggy systems |
| 568 |
# afterwards |
| 569 |
system = platform.system() |
| 570 |
distribution_facts['distribution'] = system |
| 571 |
distribution_facts['distribution_release'] = platform.release() |
| 572 |
distribution_facts['distribution_version'] = platform.version() |
| 573 |
|
| 574 |
systems_implemented = ('AIX', 'HP-UX', 'Darwin', 'FreeBSD', 'OpenBSD', 'SunOS', 'DragonFly', 'NetBSD') |
| 575 |
|
| 576 |
if system in systems_implemented: |
| 577 |
cleanedname = system.replace('-', '') |
| 578 |
distfunc = getattr(self, 'get_distribution_' + cleanedname) |
| 579 |
dist_func_facts = distfunc() |
| 580 |
distribution_facts.update(dist_func_facts) |
| 581 |
elif system == 'Linux': |
| 582 |
|
| 583 |
distribution_files = DistributionFiles(module=self.module) |
| 584 |
|
| 585 |
# linux_distribution_facts = LinuxDistribution(module).get_distribution_facts() |
| 586 |
dist_file_facts = distribution_files.process_dist_files() |
| 587 |
|
| 588 |
distribution_facts.update(dist_file_facts) |
| 589 |
|
| 590 |
distro = distribution_facts['distribution'] |
| 591 |
# look for an os family alias for the 'distribution', if there isn't one, use 'distribution' |
| 592 |
distribution_facts['os_family'] = self.OS_FAMILY.get(distro, None) or distro |
| 593 |
|
| 594 |
return distribution_facts |
| 595 |
|
| 596 |
def get_distribution_AIX(self): |
| 597 |
aix_facts = {} |
| 598 |
rc, out, err = self.module.run_command("/usr/bin/oslevel") |
| 599 |
data = out.split('.') |
| 600 |
aix_facts['distribution_major_version'] = data[0] |
| 601 |
if len(data) > 1: |
| 602 |
aix_facts['distribution_version'] = '%s.%s' % (data[0], data[1]) |
| 603 |
aix_facts['distribution_release'] = data[1] |
| 604 |
else: |
| 605 |
aix_facts['distribution_version'] = data[0] |
| 606 |
return aix_facts |
| 607 |
|
| 608 |
def get_distribution_HPUX(self): |
| 609 |
hpux_facts = {} |
| 610 |
rc, out, err = self.module.run_command(r"/usr/sbin/swlist |egrep 'HPUX.*OE.*[AB].[0-9]+\.[0-9]+'", use_unsafe_shell=True) |
| 611 |
data = re.search(r'HPUX.*OE.*([AB].[0-9]+\.[0-9]+)\.([0-9]+).*', out) |
| 612 |
if data: |
| 613 |
hpux_facts['distribution_version'] = data.groups()[0] |
| 614 |
hpux_facts['distribution_release'] = data.groups()[1] |
| 615 |
return hpux_facts |
| 616 |
|
| 617 |
def get_distribution_Darwin(self): |
| 618 |
darwin_facts = {} |
| 619 |
darwin_facts['distribution'] = 'MacOSX' |
| 620 |
rc, out, err = self.module.run_command("/usr/bin/sw_vers -productVersion") |
| 621 |
data = out.split()[-1] |
| 622 |
if data: |
| 623 |
darwin_facts['distribution_major_version'] = data.split('.')[0] |
| 624 |
darwin_facts['distribution_version'] = data |
| 625 |
return darwin_facts |
| 626 |
|
| 627 |
def get_distribution_FreeBSD(self): |
| 628 |
freebsd_facts = {} |
| 629 |
freebsd_facts['distribution_release'] = platform.release() |
| 630 |
data = re.search(r'(\d+)\.(\d+)-(RELEASE|STABLE|CURRENT|RC|PRERELEASE).*', freebsd_facts['distribution_release']) |
| 631 |
if 'trueos' in platform.version(): |
| 632 |
freebsd_facts['distribution'] = 'TrueOS' |
| 633 |
if data: |
| 634 |
freebsd_facts['distribution_major_version'] = data.group(1) |
| 635 |
freebsd_facts['distribution_version'] = '%s.%s' % (data.group(1), data.group(2)) |
| 636 |
return freebsd_facts |
| 637 |
|
| 638 |
def get_distribution_OpenBSD(self): |
| 639 |
openbsd_facts = {} |
| 640 |
openbsd_facts['distribution_version'] = platform.release() |
| 641 |
rc, out, err = self.module.run_command("/sbin/sysctl -n kern.version") |
| 642 |
match = re.match(r'OpenBSD\s[0-9]+.[0-9]+-(\S+)\s.*', out) |
| 643 |
if match: |
| 644 |
openbsd_facts['distribution_release'] = match.groups()[0] |
| 645 |
else: |
| 646 |
openbsd_facts['distribution_release'] = 'release' |
| 647 |
return openbsd_facts |
| 648 |
|
| 649 |
def get_distribution_DragonFly(self): |
| 650 |
dragonfly_facts = { |
| 651 |
'distribution_release': platform.release() |
| 652 |
} |
| 653 |
rc, out, dummy = self.module.run_command("/sbin/sysctl -n kern.version") |
| 654 |
match = re.search(r'v(\d+)\.(\d+)\.(\d+)-(RELEASE|STABLE|CURRENT).*', out) |
| 655 |
if match: |
| 656 |
dragonfly_facts['distribution_major_version'] = match.group(1) |
| 657 |
dragonfly_facts['distribution_version'] = '%s.%s.%s' % match.groups()[:3] |
| 658 |
return dragonfly_facts |
| 659 |
|
| 660 |
def get_distribution_NetBSD(self): |
| 661 |
netbsd_facts = {} |
| 662 |
platform_release = platform.release() |
| 663 |
netbsd_facts['distribution_release'] = platform_release |
| 664 |
rc, out, dummy = self.module.run_command("/sbin/sysctl -n kern.version") |
| 665 |
match = re.match(r'NetBSD\s(\d+)\.(\d+)\s\((GENERIC)\).*', out) |
| 666 |
if match: |
| 667 |
netbsd_facts['distribution_major_version'] = match.group(1) |
| 668 |
netbsd_facts['distribution_version'] = '%s.%s' % match.groups()[:2] |
| 669 |
else: |
| 670 |
netbsd_facts['distribution_major_version'] = platform_release.split('.')[0] |
| 671 |
netbsd_facts['distribution_version'] = platform_release |
| 672 |
return netbsd_facts |
| 673 |
|
| 674 |
def get_distribution_SMGL(self): |
| 675 |
smgl_facts = {} |
| 676 |
smgl_facts['distribution'] = 'Source Mage GNU/Linux' |
| 677 |
return smgl_facts |
| 678 |
|
| 679 |
def get_distribution_SunOS(self): |
| 680 |
sunos_facts = {} |
| 681 |
|
| 682 |
data = get_file_content('/etc/release').splitlines()[0] |
| 683 |
|
| 684 |
if 'Solaris' in data: |
| 685 |
# for solaris 10 uname_r will contain 5.10, for solaris 11 it will have 5.11 |
| 686 |
uname_r = get_uname(self.module, flags=['-r']) |
| 687 |
ora_prefix = '' |
| 688 |
if 'Oracle Solaris' in data: |
| 689 |
data = data.replace('Oracle ', '') |
| 690 |
ora_prefix = 'Oracle ' |
| 691 |
sunos_facts['distribution'] = data.split()[0] |
| 692 |
sunos_facts['distribution_version'] = data.split()[1] |
| 693 |
sunos_facts['distribution_release'] = ora_prefix + data |
| 694 |
sunos_facts['distribution_major_version'] = uname_r.split('.')[1].rstrip() |
| 695 |
return sunos_facts |
| 696 |
|
| 697 |
uname_v = get_uname(self.module, flags=['-v']) |
| 698 |
distribution_version = None |
| 699 |
|
| 700 |
if 'SmartOS' in data: |
| 701 |
sunos_facts['distribution'] = 'SmartOS' |
| 702 |
if _file_exists('/etc/product'): |
| 703 |
product_data = dict([l.split(': ', 1) for l in get_file_content('/etc/product').splitlines() if ': ' in l]) |
| 704 |
if 'Image' in product_data: |
| 705 |
distribution_version = product_data.get('Image').split()[-1] |
| 706 |
elif 'OpenIndiana' in data: |
| 707 |
sunos_facts['distribution'] = 'OpenIndiana' |
| 708 |
elif 'OmniOS' in data: |
| 709 |
sunos_facts['distribution'] = 'OmniOS' |
| 710 |
distribution_version = data.split()[-1] |
| 711 |
elif uname_v is not None and 'NexentaOS_' in uname_v: |
| 712 |
sunos_facts['distribution'] = 'Nexenta' |
| 713 |
distribution_version = data.split()[-1].lstrip('v') |
| 714 |
|
| 715 |
if sunos_facts.get('distribution', '') in ('SmartOS', 'OpenIndiana', 'OmniOS', 'Nexenta'): |
| 716 |
sunos_facts['distribution_release'] = data.strip() |
| 717 |
if distribution_version is not None: |
| 718 |
sunos_facts['distribution_version'] = distribution_version |
| 719 |
elif uname_v is not None: |
| 720 |
sunos_facts['distribution_version'] = uname_v.splitlines()[0].strip() |
| 721 |
return sunos_facts |
| 722 |
|
| 723 |
return sunos_facts |
| 724 |
|
| 725 |
|
| 726 |
class DistributionFactCollector(BaseFactCollector): |
| 727 |
name = 'distribution' |
| 728 |
_fact_ids = set(['distribution_version', |
| 729 |
'distribution_release', |
| 730 |
'distribution_major_version', |
| 731 |
'os_family']) # type: t.Set[str] |
| 732 |
|
| 733 |
def collect(self, module=None, collected_facts=None): |
| 734 |
collected_facts = collected_facts or {} |
| 735 |
facts_dict = {} |
| 736 |
if not module: |
| 737 |
return facts_dict |
| 738 |
|
| 739 |
distribution = Distribution(module=module) |
| 740 |
distro_facts = distribution.get_distribution_facts() |
| 741 |
|
| 742 |
return distro_facts |