fixes to facts timeout

also now handle timeout exception correctly everywhere
This commit is contained in:
Brian Coca 2016-12-17 00:03:04 -05:00 committed by Brian Coca
parent f533f6603c
commit b4bd6c80de

View file

@ -81,16 +81,16 @@ GATHER_TIMEOUT=None
class TimeoutError(Exception): class TimeoutError(Exception):
pass pass
def timeout(seconds=10, error_message="Timer expired"): def timeout(seconds=None, error_message="Timer expired"):
if seconds is None:
seconds = globals().get('GATHER_TIMEOUT') or 10
def decorator(func): def decorator(func):
def _handle_timeout(signum, frame): def _handle_timeout(signum, frame):
raise TimeoutError(error_message) raise TimeoutError(error_message)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
if 'GATHER_TIMEOUT' in globals():
if GATHER_TIMEOUT:
seconds = GATHER_TIMEOUT
signal.signal(signal.SIGALRM, _handle_timeout) signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(seconds) signal.alarm(seconds)
try: try:
@ -1327,7 +1327,7 @@ class LinuxHardware(Hardware):
mtab_entries.append(fields) mtab_entries.append(fields)
return mtab_entries return mtab_entries
@timeout(10) @timeout()
def get_mount_facts(self): def get_mount_facts(self):
self.facts['mounts'] = [] self.facts['mounts'] = []
@ -1584,7 +1584,7 @@ class SunOSHardware(Hardware):
self.facts['swap_allocated_mb'] = allocated // 1024 self.facts['swap_allocated_mb'] = allocated // 1024
self.facts['swap_reserved_mb'] = reserved // 1024 self.facts['swap_reserved_mb'] = reserved // 1024
@timeout(10) @timeout()
def get_mount_facts(self): def get_mount_facts(self):
self.facts['mounts'] = [] self.facts['mounts'] = []
# For a detailed format description see mnttab(4) # For a detailed format description see mnttab(4)
@ -1629,11 +1629,14 @@ class OpenBSDHardware(Hardware):
self.get_memory_facts() self.get_memory_facts()
self.get_processor_facts() self.get_processor_facts()
self.get_device_facts() self.get_device_facts()
self.get_mount_facts() try:
self.get_mount_facts()
except TimeoutError:
pass
self.get_dmi_facts() self.get_dmi_facts()
return self.facts return self.facts
@timeout(10) @timeout()
def get_mount_facts(self): def get_mount_facts(self):
self.facts['mounts'] = [] self.facts['mounts'] = []
fstab = get_file_content('/etc/fstab') fstab = get_file_content('/etc/fstab')
@ -1776,7 +1779,7 @@ class FreeBSDHardware(Hardware):
self.facts['swaptotal_mb'] = int(data[1]) // 1024 self.facts['swaptotal_mb'] = int(data[1]) // 1024
self.facts['swapfree_mb'] = int(data[3]) // 1024 self.facts['swapfree_mb'] = int(data[3]) // 1024
@timeout(10) @timeout()
def get_mount_facts(self): def get_mount_facts(self):
self.facts['mounts'] = [] self.facts['mounts'] = []
fstab = get_file_content('/etc/fstab') fstab = get_file_content('/etc/fstab')
@ -1907,7 +1910,7 @@ class NetBSDHardware(Hardware):
val = data[1].strip().split(' ')[0] val = data[1].strip().split(' ')[0]
self.facts["%s_mb" % key.lower()] = int(val) // 1024 self.facts["%s_mb" % key.lower()] = int(val) // 1024
@timeout(10) @timeout()
def get_mount_facts(self): def get_mount_facts(self):
self.facts['mounts'] = [] self.facts['mounts'] = []
fstab = get_file_content('/etc/fstab') fstab = get_file_content('/etc/fstab')
@ -2241,7 +2244,10 @@ class HurdHardware(LinuxHardware):
def populate(self): def populate(self):
self.get_uptime_facts() self.get_uptime_facts()
self.get_memory_facts() self.get_memory_facts()
self.get_mount_facts() try:
self.get_mount_facts()
except TimeoutError:
pass
return self.facts return self.facts
class Network(Facts): class Network(Facts):