mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Adjustments to behavior of Facts and subclass behavior
The Facts class and subclasses no longer take a dict argument for instantiation. populate() now returns self.facts. Other changes: - Facts.__init__() takes over most of the work from populate() so that subclasses can benefit from its knowledge. - Drop setting unknown facts to 'NA' in __init__() in various subclasses. - Check for presence of facts in get_virtual_facts() - Update ansible_facts() to use facts.update(<classname>().populate())
This commit is contained in:
parent
ae1b2394ac
commit
97c64f3632
1 changed files with 42 additions and 48 deletions
|
@ -59,15 +59,16 @@ class Facts(object):
|
||||||
'/etc/vmware-release': 'VMwareESX' }
|
'/etc/vmware-release': 'VMwareESX' }
|
||||||
SELINUX_MODE_DICT = { 1: 'enforcing', 0: 'permissive', -1: 'disabled' }
|
SELINUX_MODE_DICT = { 1: 'enforcing', 0: 'permissive', -1: 'disabled' }
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
self.facts = facts
|
self.facts = {}
|
||||||
|
|
||||||
def populate(self):
|
|
||||||
self.get_platform_facts()
|
self.get_platform_facts()
|
||||||
self.get_distribution_facts()
|
self.get_distribution_facts()
|
||||||
self.get_public_ssh_host_keys()
|
self.get_public_ssh_host_keys()
|
||||||
self.get_selinux_facts()
|
self.get_selinux_facts()
|
||||||
|
|
||||||
|
def populate(self):
|
||||||
|
return self.facts
|
||||||
|
|
||||||
# Platform
|
# Platform
|
||||||
# patform.system() can be Linux, Darwin, Java, or Windows
|
# patform.system() can be Linux, Darwin, Java, or Windows
|
||||||
def get_platform_facts(self):
|
def get_platform_facts(self):
|
||||||
|
@ -156,25 +157,18 @@ class Hardware(Facts):
|
||||||
"""
|
"""
|
||||||
platform = 'Generic'
|
platform = 'Generic'
|
||||||
|
|
||||||
def __new__(cls, facts, *arguments, **keyword):
|
def __new__(cls, *arguments, **keyword):
|
||||||
subclass = cls
|
subclass = cls
|
||||||
for sc in Hardware.__subclasses__():
|
for sc in Hardware.__subclasses__():
|
||||||
if sc.platform == platform.system():
|
if sc.platform == platform.system():
|
||||||
subclass = sc
|
subclass = sc
|
||||||
return super(cls, subclass).__new__(subclass, *arguments, **keyword)
|
return super(cls, subclass).__new__(subclass, *arguments, **keyword)
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Facts.__init__(self, facts)
|
Facts.__init__(self)
|
||||||
self.facts['memfree_mb'] = 'NA'
|
|
||||||
self.facts['memtotal_mb'] = 'NA'
|
|
||||||
self.facts['swapfree_mb'] = 'NA'
|
|
||||||
self.facts['swaptotal_mb'] = 'NA'
|
|
||||||
self.facts['processor'] = ['NA']
|
|
||||||
self.facts['processor_cores'] = 'NA'
|
|
||||||
self.facts['processor_count'] = 'NA'
|
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
pass
|
return self.facts
|
||||||
|
|
||||||
class LinuxHardware(Hardware):
|
class LinuxHardware(Hardware):
|
||||||
"""
|
"""
|
||||||
|
@ -210,13 +204,14 @@ class LinuxHardware(Hardware):
|
||||||
"Rack Mount Chassis", "Sealed-case PC", "Multi-system",
|
"Rack Mount Chassis", "Sealed-case PC", "Multi-system",
|
||||||
"CompactPCI", "AdvancedTCA" ]
|
"CompactPCI", "AdvancedTCA" ]
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Hardware.__init__(self, facts)
|
Hardware.__init__(self)
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
self.get_cpu_facts()
|
self.get_cpu_facts()
|
||||||
self.get_memory_facts()
|
self.get_memory_facts()
|
||||||
self.get_dmi_facts()
|
self.get_dmi_facts()
|
||||||
|
return self.facts
|
||||||
|
|
||||||
def get_memory_facts(self):
|
def get_memory_facts(self):
|
||||||
if not os.access("/proc/meminfo", os.R_OK):
|
if not os.access("/proc/meminfo", os.R_OK):
|
||||||
|
@ -274,12 +269,13 @@ class SunOSHardware(Hardware):
|
||||||
"""
|
"""
|
||||||
platform = 'SunOS'
|
platform = 'SunOS'
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Hardware.__init__(self, facts)
|
Hardware.__init__(self)
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
self.get_cpu_facts()
|
self.get_cpu_facts()
|
||||||
self.get_memory_facts()
|
self.get_memory_facts()
|
||||||
|
return self.facts
|
||||||
|
|
||||||
def get_cpu_facts(self):
|
def get_cpu_facts(self):
|
||||||
cmd = subprocess.Popen("/usr/sbin/psrinfo -v", shell=True,
|
cmd = subprocess.Popen("/usr/sbin/psrinfo -v", shell=True,
|
||||||
|
@ -327,12 +323,13 @@ class FreeBSDHardware(Hardware):
|
||||||
platform = 'FreeBSD'
|
platform = 'FreeBSD'
|
||||||
DMESG_BOOT = '/var/run/dmesg.boot'
|
DMESG_BOOT = '/var/run/dmesg.boot'
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Hardware.__init__(self, facts)
|
Hardware.__init__(self)
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
self.get_cpu_facts()
|
self.get_cpu_facts()
|
||||||
self.get_memory_facts()
|
self.get_memory_facts()
|
||||||
|
return self.facts
|
||||||
|
|
||||||
def get_cpu_facts(self):
|
def get_cpu_facts(self):
|
||||||
self.facts['processor'] = []
|
self.facts['processor'] = []
|
||||||
|
@ -387,19 +384,18 @@ class Network(Facts):
|
||||||
"""
|
"""
|
||||||
platform = 'Generic'
|
platform = 'Generic'
|
||||||
|
|
||||||
def __new__(cls, facts, *arguments, **keyword):
|
def __new__(cls, *arguments, **keyword):
|
||||||
subclass = cls
|
subclass = cls
|
||||||
for sc in Network.__subclasses__():
|
for sc in Network.__subclasses__():
|
||||||
if sc.platform == platform.system():
|
if sc.platform == platform.system():
|
||||||
subclass = sc
|
subclass = sc
|
||||||
return super(cls, subclass).__new__(subclass, *arguments, **keyword)
|
return super(cls, subclass).__new__(subclass, *arguments, **keyword)
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Facts.__init__(self, facts)
|
Facts.__init__(self)
|
||||||
self.facts['interfaces'] = ['NA']
|
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
pass
|
return self.facts
|
||||||
|
|
||||||
class LinuxNetwork(Network):
|
class LinuxNetwork(Network):
|
||||||
"""
|
"""
|
||||||
|
@ -411,11 +407,12 @@ class LinuxNetwork(Network):
|
||||||
SIOCGIFCONF = 0x8912
|
SIOCGIFCONF = 0x8912
|
||||||
SIOCGIFHWADDR = 0x8927
|
SIOCGIFHWADDR = 0x8927
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Network.__init__(self, facts)
|
Network.__init__(self)
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
self.get_network_facts()
|
self.get_network_facts()
|
||||||
|
return self.facts
|
||||||
|
|
||||||
# get list of interfaces that are up
|
# get list of interfaces that are up
|
||||||
def get_interfaces(self):
|
def get_interfaces(self):
|
||||||
|
@ -501,20 +498,18 @@ class Virtual(Facts):
|
||||||
All subclasses MUST define platform.
|
All subclasses MUST define platform.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __new__(cls, facts, *arguments, **keyword):
|
def __new__(cls, *arguments, **keyword):
|
||||||
subclass = cls
|
subclass = cls
|
||||||
for sc in Virtual.__subclasses__():
|
for sc in Virtual.__subclasses__():
|
||||||
if sc.platform == platform.system():
|
if sc.platform == platform.system():
|
||||||
subclass = sc
|
subclass = sc
|
||||||
return super(cls, subclass).__new__(subclass, *arguments, **keyword)
|
return super(cls, subclass).__new__(subclass, *arguments, **keyword)
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Facts.__init__(self, facts)
|
Facts.__init__(self)
|
||||||
self.facts['virtualization_type'] = 'NA'
|
|
||||||
self.facts['virtualization_role'] = 'NA'
|
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
pass
|
return self.facts
|
||||||
|
|
||||||
class LinuxVirtual(Virtual):
|
class LinuxVirtual(Virtual):
|
||||||
"""
|
"""
|
||||||
|
@ -524,11 +519,14 @@ class LinuxVirtual(Virtual):
|
||||||
"""
|
"""
|
||||||
platform = 'Linux'
|
platform = 'Linux'
|
||||||
|
|
||||||
def __init__(self, facts):
|
def __init__(self):
|
||||||
Virtual.__init__(self, facts)
|
Virtual.__init__(self)
|
||||||
self.facts = facts
|
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
|
self.get_virtual_facts()
|
||||||
|
return self.facts
|
||||||
|
|
||||||
|
def get_virtual_facts(self):
|
||||||
if os.path.exists("/proc/xen"):
|
if os.path.exists("/proc/xen"):
|
||||||
self.facts['virtualization_type'] = 'xen'
|
self.facts['virtualization_type'] = 'xen'
|
||||||
self.facts['virtualization_role'] = 'guest'
|
self.facts['virtualization_role'] = 'guest'
|
||||||
|
@ -548,10 +546,10 @@ class LinuxVirtual(Virtual):
|
||||||
elif 'vboxguest' in modules:
|
elif 'vboxguest' in modules:
|
||||||
self.facts['virtualization_type'] = 'virtualbox'
|
self.facts['virtualization_type'] = 'virtualbox'
|
||||||
self.facts['virtualization_role'] = 'guest'
|
self.facts['virtualization_role'] = 'guest'
|
||||||
if 'QEMU' in self.facts['processor'][0]:
|
if 'processor' in self.facts and 'QEMU' in self.facts['processor'][0]:
|
||||||
self.facts['virtualization_type'] = 'kvm'
|
self.facts['virtualization_type'] = 'kvm'
|
||||||
self.facts['virtualization_role'] = 'guest'
|
self.facts['virtualization_role'] = 'guest'
|
||||||
if self.facts['distribution'] == 'VMwareESX':
|
if 'distribution' in self.facts and self.facts['distribution'] == 'VMwareESX':
|
||||||
self.facts['virtualization_type'] = 'VMware'
|
self.facts['virtualization_type'] = 'VMware'
|
||||||
self.facts['virtualization_role'] = 'host'
|
self.facts['virtualization_role'] = 'host'
|
||||||
# You can spawn a dmidecode process and parse that or infer from devices
|
# You can spawn a dmidecode process and parse that or infer from devices
|
||||||
|
@ -566,15 +564,11 @@ class LinuxVirtual(Virtual):
|
||||||
|
|
||||||
def ansible_facts():
|
def ansible_facts():
|
||||||
facts = {}
|
facts = {}
|
||||||
f = Facts(facts)
|
facts.update(Facts().populate())
|
||||||
f.populate()
|
facts.update(Hardware().populate())
|
||||||
f = Hardware(f.facts)
|
facts.update(Network().populate())
|
||||||
f.populate()
|
facts.update(Virtual().populate())
|
||||||
f = Network(f.facts)
|
return facts
|
||||||
f.populate()
|
|
||||||
f = Virtual(f.facts)
|
|
||||||
f.populate()
|
|
||||||
return f.facts
|
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue