-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdistro.py
More file actions
444 lines (348 loc) · 12.6 KB
/
distro.py
File metadata and controls
444 lines (348 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""Provides information about the Linux distribution it runs on, such as a reliable
machine-readable distro ID and "os_family" (known from Ansible).
Source Code is taken, converted, shortened and modified from:
* lib/ansible/module_utils/facts/system/distribution.py
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025061001'
import os
import platform
import re
from . import shell
# --- Static mappings ---
OSDIST_LIST = (
{'path': '/etc/alpine-release', 'name': 'Alpine'},
{'path': '/etc/arch-release', 'name': 'Archlinux', 'allowempty': True},
{'path': '/etc/centos-release', 'name': 'CentOS'},
{'path': '/etc/redhat-release', 'name': 'RedHat'},
{'path': '/etc/oracle-release', 'name': 'OracleLinux'},
{'path': '/etc/gentoo-release', 'name': 'Gentoo'},
{'path': '/etc/os-release', 'name': 'Debian'},
{'path': '/etc/os-release', 'name': 'Ubuntu'},
{'path': '/etc/os-release', 'name': 'Amazon'},
{'path': '/usr/lib/os-release', 'name': 'ClearLinux'},
{'path': '/etc/lsb-release', 'name': 'Debian'}, # fallback
)
SEARCH_STRING = {
'OracleLinux': 'Oracle Linux',
'RedHat': 'Red Hat',
'Altlinux': 'ALT',
'SMGL': 'Source Mage GNU/Linux',
}
OS_FAMILY_MAP = {
'RedHat': [
'RedHat',
'RHEL',
'CentOS',
'Scientific',
'OracleLinux',
'Fedora',
'AlmaLinux',
'Rocky',
],
'Debian': [
'Debian',
'Debian GNU/Linux',
'Ubuntu',
'Raspbian',
'Pop!_OS',
'Kali',
'Parrot',
'Devuan',
'Devuan GNU/Linux',
'Deepin',
'Mint',
],
'Suse': ['SUSE', 'openSUSE', 'SLES', 'SLED'],
'Archlinux': ['Archlinux', 'Manjaro', 'Antergos'],
'Gentoo': ['Gentoo', 'Funtoo'],
'Alpine': ['Alpine'],
'ClearLinux': ['ClearLinux'],
}
STRIP_QUOTES = r'\'\"\\'
def _file_exists(path, allow_empty=False):
"""
Check if a file exists and optionally allow empty files.
This function verifies the existence of a file at the given path. If `allow_empty` is `False`,
it additionally checks that the file is not empty.
### Parameters
- **path** (`str`):
Path to the file to check.
- **allow_empty** (`bool`, optional):
Whether to allow empty files as valid. Defaults to `False`.
### Returns
- **bool**:
`True` if the file exists (and is non-empty unless `allow_empty=True`), otherwise `False`.
### Notes
- Useful to validate configuration or system files before parsing.
### Example
>>> _file_exists('/etc/os-release')
True
"""
if not os.path.isfile(path):
return False
if allow_empty:
return True
return os.path.getsize(path) > 0
def _get_file_content(path):
"""
Read the content of a text file.
Opens and reads the entire content of a UTF-8 encoded text file at the specified path.
### Parameters
- **path** (`str`):
Path to the file to read.
### Returns
- **str**:
The complete contents of the file as a string.
### Notes
- Raises an exception if the file cannot be opened.
### Example
>>> _get_file_content('/etc/os-release')
'NAME="Ubuntu"\nVERSION="22.04.2 LTS (Jammy Jellyfish)"\n...'
"""
with open(path, 'r', encoding='utf-8') as f:
return f.read()
def _guess_linux_distribution():
"""
Provide basic distribution facts based on platform module.
Returns baseline information using the `platform` module, including distribution name,
kernel version, and major version.
### Parameters
- *None*
### Returns
- **dict**:
A dictionary containing `distribution`, `distribution_version`, `distribution_release`, and `distribution_major_version`.
### Notes
- Serves as a fallback if no release file parsing succeeds.
### Example
>>> _guess_linux_distribution()
{'distribution': 'Linux', 'distribution_version': '#1 SMP...', 'distribution_release': '5.15.0-86-generic', 'distribution_major_version': '5'}
"""
system = platform.system()
return {
'distribution': system if system else 'NA',
'distribution_version': platform.version() if system else 'NA',
'distribution_release': platform.release() if system else 'NA',
'distribution_major_version': platform.version().split('.')[0]
if '.' in platform.version()
else 'NA',
}
def _parse_distribution_file(name, data):
"""
Parse distribution information from release file content.
This function interprets known distro-specific patterns from given release file content
and extracts the distribution name if recognized.
### Parameters
- **name** (`str`):
The expected distribution name or file variety.
- **data** (`str`):
The contents of the release file.
### Returns
- **tuple** (`bool`, `dict`):
- First element: `True` if parsing succeeded, `False` otherwise.
- Second element: A dictionary of parsed distribution facts.
### Notes
- Handles special parsing for Archlinux, Debian, Ubuntu, Amazon, ClearLinux, Alpine.
### Example
>>> _parse_distribution_file('RedHat', 'Red Hat Enterprise Linux release 8.5...')
(True, {'distribution': 'RedHat'})
"""
dist_facts = {}
data = data.strip(STRIP_QUOTES)
if name in SEARCH_STRING:
if SEARCH_STRING[name] in data:
dist_facts['distribution'] = name
else:
dist_facts['distribution'] = data.split()[0]
return True, dist_facts
if name == 'Archlinux':
if 'Arch Linux' in data:
dist_facts['distribution'] = 'Archlinux'
return True, dist_facts
return False, {}
if name in ('Debian', 'Ubuntu', 'Amazon', 'ClearLinux'):
if 'PRETTY_NAME' in data or 'NAME=' in data:
m = re.search(r'^NAME="?([^"\n]*)"?', data, re.M)
if m:
dist_facts['distribution'] = m.group(1)
return True, dist_facts
if name == 'Alpine':
if 'Alpine' in data:
dist_facts['distribution'] = 'Alpine'
return True, dist_facts
return False, {}
return True, dist_facts
def _parse_os_release(path):
"""
Parse `/etc/os-release` for version information.
Reads the standard `os-release` file and extracts the version ID and major version.
### Parameters
- **path** (`str`):
Path to the `os-release` file.
### Returns
- **dict**:
Parsed facts: `distribution_version` and `distribution_major_version` if found.
### Notes
- Silent if file does not exist or parsing fails.
### Example
>>> _parse_os_release('/etc/os-release')
{'distribution_version': '22.04', 'distribution_major_version': '22'}
"""
facts = {}
if not _file_exists(path):
return facts
try:
data = _get_file_content(path)
except Exception:
return facts
m_version = re.search(r'^VERSION_ID="?([^"\n]*)"?', data, re.M)
if m_version:
facts['distribution_version'] = m_version.group(1)
facts['distribution_major_version'] = m_version.group(1).split('.')[0]
return facts
def _parse_lsb_release(path):
"""
Parse `/etc/lsb-release` for version information.
Reads the `lsb-release` file and extracts the release and major version if available.
### Parameters
- **path** (`str`):
Path to the `lsb-release` file.
### Returns
- **dict**:
Parsed facts: `distribution_version` and `distribution_major_version` if found.
### Notes
- Useful fallback for Debian-based systems when `os-release` is incomplete.
### Example
>>> _parse_lsb_release('/etc/lsb-release')
{'distribution_version': '20.04', 'distribution_major_version': '20'}
"""
facts = {}
if not _file_exists(path):
return facts
try:
data = _get_file_content(path)
except Exception:
return facts
m_version = re.search(r'^DISTRIB_RELEASE="?([^"\n]*)"?', data, re.M)
if m_version:
facts['distribution_version'] = m_version.group(1)
facts['distribution_major_version'] = m_version.group(1).split('.')[0]
return facts
def _process_dist_files():
"""
Process distribution-specific files to determine system identity.
Sequentially checks known distribution marker files and parses them to
extract distribution facts. Improves the guessed facts with better data
from `/etc/os-release` and `/etc/lsb-release` if available.
### Parameters
- *None*
### Returns
- **dict**:
Collected facts including distribution, version, major version, and parsing flags.
### Notes
- Stops at the first successful parsing.
### Example
>>> _process_dist_files()
{'distribution': 'Ubuntu', 'distribution_version': '22.04', 'distribution_major_version': '22', ...}
"""
facts = _guess_linux_distribution()
for entry in OSDIST_LIST:
name = entry['name']
path = entry['path']
allow_empty = entry.get('allowempty', False)
if not _file_exists(path, allow_empty=allow_empty):
continue
try:
data = _get_file_content(path)
except Exception:
continue
if allow_empty:
facts.update(
{
'distribution': name,
'distribution_file_path': path,
'distribution_file_variety': name,
}
)
break
parsed, parsed_facts = _parse_distribution_file(name, data)
if parsed:
facts.update(
{
'distribution': name,
'distribution_file_path': path,
'distribution_file_variety': name,
'distribution_file_parsed': True,
}
)
facts.update(parsed_facts)
break
# Improve facts from os-release or lsb-release
if '/etc/os-release' in [e['path'] for e in OSDIST_LIST]:
facts.update(_parse_os_release('/etc/os-release'))
if '/etc/lsb-release' in [e['path'] for e in OSDIST_LIST]:
facts.update(_parse_lsb_release('/etc/lsb-release'))
return facts
def _map_os_family(distribution):
"""
Map a detected distribution to its OS family.
Returns a broader OS family (like `RedHat`, `Debian`, etc.) based on the distribution name.
### Parameters
- **distribution** (`str`):
The detected distribution name.
### Returns
- **str**:
The mapped OS family name, or the original distribution if no mapping exists.
### Notes
- Helps categorize distributions consistently.
### Example
>>> _map_os_family('Fedora')
'RedHat'
"""
for family, members in OS_FAMILY_MAP.items():
if distribution in members:
return family
return distribution
def get_distribution_facts():
"""
Detect the Linux distribution and return normalized facts.
Collects detailed information about the Linux distribution based on release files,
and assigns a standardized OS family name.
### Parameters
- *None*
### Returns
- **dict**:
Dictionary of collected distribution facts:
- `distribution`
- `distribution_version`
- `distribution_release`
- `distribution_major_version`
- `distribution_file_path`
- `distribution_file_variety`
- `distribution_file_parsed`
- `os_family`
### Example
>>> get_distribution_facts()
{'distribution': 'Fedora', 'distribution_version': '41', 'distribution_release': '6.13.10-200.fc41.x86_64', 'distribution_major_version': '41', 'distribution_file_path': '/etc/redhat-release', 'distribution_file_variety': 'RedHat', 'distribution_file_parsed': True, 'os_family': 'RedHat'}
"""
facts = _process_dist_files()
distro = facts.get('distribution_file_variety', 'distribution')
facts['os_family'] = _map_os_family(distro) # returns 'RedHat', for example
# hardcoded command, no user input; shell=True needed to source the file
cmd = '. /etc/os-release && echo "$NAME $VERSION"'
success, result = shell.shell_exec(cmd, shell=True) # nosec B604
if not success:
return facts
stdout, _, _ = result
facts['os_info'] = (
stdout.strip()
) # returns 'Fedora Linux 41 (Workstation Edition)', for example
return facts