2016-04-14 18:26:00 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
This script generated test_cases for test_distribution_version.py.
|
|
|
|
|
2018-12-11 02:04:14 +01:00
|
|
|
To do so it outputs the relevant files from /etc/*release, the output of distro.linux_distribution()
|
|
|
|
and the current ansible_facts regarding the distribution version.
|
2016-04-14 18:26:00 +02:00
|
|
|
|
|
|
|
This assumes a working ansible version in the path.
|
|
|
|
"""
|
|
|
|
|
2020-06-23 04:05:30 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2018-12-11 02:04:14 +01:00
|
|
|
|
2020-07-21 19:47:11 +02:00
|
|
|
import json
|
2016-04-14 18:26:00 +02:00
|
|
|
import os.path
|
2020-07-21 19:47:11 +02:00
|
|
|
import platform
|
2016-04-14 18:26:00 +02:00
|
|
|
import subprocess
|
2016-09-27 00:35:36 +02:00
|
|
|
import sys
|
2016-04-14 18:26:00 +02:00
|
|
|
|
2018-12-11 02:04:14 +01:00
|
|
|
from ansible.module_utils import distro
|
2019-06-29 01:21:35 +02:00
|
|
|
from ansible.module_utils._text import to_text
|
2018-12-11 02:04:14 +01:00
|
|
|
|
|
|
|
|
2016-04-14 18:26:00 +02:00
|
|
|
filelist = [
|
2017-01-29 08:28:53 +01:00
|
|
|
'/etc/oracle-release',
|
|
|
|
'/etc/slackware-version',
|
|
|
|
'/etc/redhat-release',
|
|
|
|
'/etc/vmware-release',
|
|
|
|
'/etc/openwrt_release',
|
|
|
|
'/etc/system-release',
|
|
|
|
'/etc/alpine-release',
|
|
|
|
'/etc/release',
|
|
|
|
'/etc/arch-release',
|
|
|
|
'/etc/os-release',
|
|
|
|
'/etc/SuSE-release',
|
|
|
|
'/etc/gentoo-release',
|
|
|
|
'/etc/os-release',
|
|
|
|
'/etc/lsb-release',
|
|
|
|
'/etc/altlinux-release',
|
|
|
|
'/etc/os-release',
|
|
|
|
'/etc/coreos/update.conf',
|
2020-06-02 15:11:53 +02:00
|
|
|
'/etc/flatcar/update.conf',
|
2018-01-20 21:05:53 +01:00
|
|
|
'/usr/lib/os-release',
|
2016-04-14 18:26:00 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
fcont = {}
|
|
|
|
|
|
|
|
for f in filelist:
|
|
|
|
if os.path.exists(f):
|
|
|
|
s = os.path.getsize(f)
|
|
|
|
if s > 0 and s < 10000:
|
|
|
|
with open(f) as fh:
|
|
|
|
fcont[f] = fh.read()
|
|
|
|
|
2018-12-11 02:04:14 +01:00
|
|
|
dist = distro.linux_distribution(full_distribution_name=False)
|
2016-04-14 18:26:00 +02:00
|
|
|
|
2016-09-17 23:27:35 +02:00
|
|
|
facts = ['distribution', 'distribution_version', 'distribution_release', 'distribution_major_version', 'os_family']
|
2016-09-27 00:35:36 +02:00
|
|
|
|
|
|
|
try:
|
2019-06-29 01:21:35 +02:00
|
|
|
b_ansible_out = subprocess.check_output(
|
2016-09-27 00:35:36 +02:00
|
|
|
['ansible', 'localhost', '-m', 'setup'])
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print("ERROR: ansible run failed, output was: \n")
|
|
|
|
print(e.output)
|
|
|
|
sys.exit(e.returncode)
|
|
|
|
|
2019-06-29 01:21:35 +02:00
|
|
|
ansible_out = to_text(b_ansible_out)
|
2016-04-14 18:26:00 +02:00
|
|
|
parsed = json.loads(ansible_out[ansible_out.index('{'):])
|
|
|
|
ansible_facts = {}
|
|
|
|
for fact in facts:
|
2016-04-29 22:18:50 +02:00
|
|
|
try:
|
2017-05-16 19:52:07 +02:00
|
|
|
ansible_facts[fact] = parsed['ansible_facts']['ansible_' + fact]
|
2018-09-08 02:59:46 +02:00
|
|
|
except Exception:
|
2016-04-29 22:18:50 +02:00
|
|
|
ansible_facts[fact] = "N/A"
|
2016-04-14 18:26:00 +02:00
|
|
|
|
2016-04-14 20:53:36 +02:00
|
|
|
nicename = ansible_facts['distribution'] + ' ' + ansible_facts['distribution_version']
|
|
|
|
|
2016-04-14 18:26:00 +02:00
|
|
|
output = {
|
2016-04-14 20:53:36 +02:00
|
|
|
'name': nicename,
|
2019-06-29 01:21:35 +02:00
|
|
|
'distro': {
|
|
|
|
'codename': distro.codename(),
|
|
|
|
'id': distro.id(),
|
|
|
|
'name': distro.name(),
|
|
|
|
'version': distro.version(),
|
|
|
|
'version_best': distro.version(best=True),
|
2020-02-11 22:46:48 +01:00
|
|
|
'lsb_release_info': distro.lsb_release_info(),
|
|
|
|
'os_release_info': distro.os_release_info(),
|
2019-06-29 01:21:35 +02:00
|
|
|
},
|
2016-04-14 18:26:00 +02:00
|
|
|
'input': fcont,
|
|
|
|
'platform.dist': dist,
|
|
|
|
'result': ansible_facts,
|
|
|
|
}
|
|
|
|
|
2020-07-21 19:47:11 +02:00
|
|
|
system = platform.system()
|
|
|
|
if system != 'Linux':
|
|
|
|
output['platform.system'] = system
|
|
|
|
|
|
|
|
release = platform.release()
|
|
|
|
if release:
|
|
|
|
output['platform.release'] = release
|
|
|
|
|
2016-04-14 20:53:36 +02:00
|
|
|
print(json.dumps(output, indent=4))
|