made missing cli tools non fatal (#37194)

* made missing cli tools non fatal

* remove bare exceptions

these shoudl not be there as they can prevent 'wanted' exits
pull/23971/head
Brian Coca 6 years ago committed by GitHub
parent 42912e1ac8
commit 0c2e7fd841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -66,12 +66,18 @@ class FreeBSDHardware(Hardware):
def get_cpu_facts(self): def get_cpu_facts(self):
cpu_facts = {} cpu_facts = {}
cpu_facts['processor'] = [] cpu_facts['processor'] = []
rc, out, err = self.module.run_command("/sbin/sysctl -n hw.ncpu") sysctl = self.module.get_bin_path('sysctl')
cpu_facts['processor_count'] = out.strip() if sysctl:
rc, out, err = self.module.run_command("%s -n hw.ncpu" % sysctl, check_rc=False)
cpu_facts['processor_count'] = out.strip()
dmesg_boot = get_file_content(FreeBSDHardware.DMESG_BOOT) dmesg_boot = get_file_content(FreeBSDHardware.DMESG_BOOT)
if not dmesg_boot: if not dmesg_boot:
rc, dmesg_boot, err = self.module.run_command("/sbin/dmesg") try:
rc, dmesg_boot, err = self.module.run_command(self.module.get_bin_path("dmesg"), check_rc=False)
except Exception:
dmesg_boot = ''
for line in dmesg_boot.splitlines(): for line in dmesg_boot.splitlines():
if 'CPU:' in line: if 'CPU:' in line:
cpu = re.sub(r'CPU:\s+', r"", line) cpu = re.sub(r'CPU:\s+', r"", line)
@ -84,29 +90,34 @@ class FreeBSDHardware(Hardware):
def get_memory_facts(self): def get_memory_facts(self):
memory_facts = {} memory_facts = {}
rc, out, err = self.module.run_command("/sbin/sysctl vm.stats") sysctl = self.module.get_bin_path('sysctl')
for line in out.splitlines(): if sysctl:
data = line.split() rc, out, err = self.module.run_command("%s vm.stats" % sysctl, check_rc=False)
if 'vm.stats.vm.v_page_size' in line: for line in out.splitlines():
pagesize = int(data[1]) data = line.split()
if 'vm.stats.vm.v_page_count' in line: if 'vm.stats.vm.v_page_size' in line:
pagecount = int(data[1]) pagesize = int(data[1])
if 'vm.stats.vm.v_free_count' in line: if 'vm.stats.vm.v_page_count' in line:
freecount = int(data[1]) pagecount = int(data[1])
memory_facts['memtotal_mb'] = pagesize * pagecount // 1024 // 1024 if 'vm.stats.vm.v_free_count' in line:
memory_facts['memfree_mb'] = pagesize * freecount // 1024 // 1024 freecount = int(data[1])
# Get swapinfo. swapinfo output looks like: memory_facts['memtotal_mb'] = pagesize * pagecount // 1024 // 1024
# Device 1M-blocks Used Avail Capacity memory_facts['memfree_mb'] = pagesize * freecount // 1024 // 1024
# /dev/ada0p3 314368 0 314368 0%
# swapinfo = self.module.get_bin_path('swapinfo')
rc, out, err = self.module.run_command("/usr/sbin/swapinfo -k") if swapinfo:
lines = out.splitlines() # Get swapinfo. swapinfo output looks like:
if len(lines[-1]) == 0: # Device 1M-blocks Used Avail Capacity
lines.pop() # /dev/ada0p3 314368 0 314368 0%
data = lines[-1].split() #
if data[0] != 'Device': rc, out, err = self.module.run_command("%s -k" % swapinfo)
memory_facts['swaptotal_mb'] = int(data[1]) // 1024 lines = out.splitlines()
memory_facts['swapfree_mb'] = int(data[3]) // 1024 if len(lines[-1]) == 0:
lines.pop()
data = lines[-1].split()
if data[0] != 'Device':
memory_facts['swaptotal_mb'] = int(data[1]) // 1024
memory_facts['swapfree_mb'] = int(data[3]) // 1024
return memory_facts return memory_facts

@ -20,7 +20,7 @@ def is_chroot():
# check if my file system is the root one # check if my file system is the root one
proc_root = os.stat('/proc/1/root/.') proc_root = os.stat('/proc/1/root/.')
is_chroot = my_root.st_ino != proc_root.st_ino or my_root.st_dev != proc_root.st_dev is_chroot = my_root.st_ino != proc_root.st_ino or my_root.st_dev != proc_root.st_dev
except: except Exception:
# I'm not root or no proc, fallback to checking it is inode #2 # I'm not root or no proc, fallback to checking it is inode #2
is_chroot = (my_root.st_ino != 2) is_chroot = (my_root.st_ino != 2)

@ -29,7 +29,7 @@ def get_file_content(path, default=None, strip=True):
data = default data = default
finally: finally:
datafile.close() datafile.close()
except: except Exception:
# ignore errors as some jails/containers might have readable permissions but not allow reads to proc # ignore errors as some jails/containers might have readable permissions but not allow reads to proc
# done in 2 blocks for 2.4 compat # done in 2 blocks for 2.4 compat
pass pass

Loading…
Cancel
Save