2016-04-14 16:26:00 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
This script generated test_cases for test_distribution_version.py.
|
|
|
|
|
2016-04-14 17:44:06 +00:00
|
|
|
To do so it outputs the relevant files from /etc/*release, the output of platform.dist() and the current ansible_facts regarding the distribution version.
|
2016-04-14 16:26:00 +00:00
|
|
|
|
|
|
|
This assumes a working ansible version in the path.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import platform
|
|
|
|
import os.path
|
|
|
|
import subprocess
|
|
|
|
import json
|
2016-09-26 22:35:36 +00:00
|
|
|
import sys
|
2016-04-14 16:26:00 +00:00
|
|
|
|
|
|
|
filelist = [
|
2017-01-29 07:28:53 +00: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',
|
2018-01-20 20:05:53 +00:00
|
|
|
'/usr/lib/os-release',
|
2016-04-14 16:26:00 +00: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()
|
|
|
|
|
|
|
|
dist = platform.dist()
|
|
|
|
|
|
|
|
|
2016-09-17 21:27:35 +00:00
|
|
|
facts = ['distribution', 'distribution_version', 'distribution_release', 'distribution_major_version', 'os_family']
|
2016-09-26 22:35:36 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
ansible_out = subprocess.check_output(
|
|
|
|
['ansible', 'localhost', '-m', 'setup'])
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print("ERROR: ansible run failed, output was: \n")
|
|
|
|
print(e.output)
|
|
|
|
sys.exit(e.returncode)
|
|
|
|
|
2016-04-14 16:26:00 +00:00
|
|
|
parsed = json.loads(ansible_out[ansible_out.index('{'):])
|
|
|
|
ansible_facts = {}
|
|
|
|
for fact in facts:
|
2016-04-29 20:18:50 +00:00
|
|
|
try:
|
2017-05-16 17:52:07 +00:00
|
|
|
ansible_facts[fact] = parsed['ansible_facts']['ansible_' + fact]
|
2016-04-29 20:18:50 +00:00
|
|
|
except:
|
|
|
|
ansible_facts[fact] = "N/A"
|
2016-04-14 16:26:00 +00:00
|
|
|
|
2016-04-14 18:53:36 +00:00
|
|
|
nicename = ansible_facts['distribution'] + ' ' + ansible_facts['distribution_version']
|
|
|
|
|
2016-04-14 16:26:00 +00:00
|
|
|
output = {
|
2016-04-14 18:53:36 +00:00
|
|
|
'name': nicename,
|
2016-04-14 16:26:00 +00:00
|
|
|
'input': fcont,
|
|
|
|
'platform.dist': dist,
|
|
|
|
'result': ansible_facts,
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:53:36 +00:00
|
|
|
print(json.dumps(output, indent=4))
|