1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Clean up device fact gathering

Remove lots of re use that really shouldn't have been re in the first
place. Initialize pcidata even if lspci is unavailable, and check for
its usability before trying to use it.

Fixes #2060.
This commit is contained in:
Daniel Hokka Zakrisson 2013-02-12 18:45:48 +01:00
parent 0212fed922
commit 85fb8494c5

View file

@ -380,6 +380,8 @@ class LinuxHardware(Hardware):
lspci = module.get_bin_path('lspci') lspci = module.get_bin_path('lspci')
if lspci: if lspci:
rc, pcidata, err = module.run_command(lspci) rc, pcidata, err = module.run_command(lspci)
else:
pcidata = None
try: try:
block_devs = os.listdir("/sys/block") block_devs = os.listdir("/sys/block")
@ -397,19 +399,18 @@ class LinuxHardware(Hardware):
sysfs_no_links = 1 sysfs_no_links = 1
else: else:
continue continue
if re.search("virtual", path): if "virtual" in path:
continue continue
sysdir = os.path.join("/sys/block", path) sysdir = os.path.join("/sys/block", path)
if sysfs_no_links == 1: if sysfs_no_links == 1:
for folder in os.listdir(sysdir): for folder in os.listdir(sysdir):
if re.search("device", folder): if "device" in folder:
virtual = 0 virtual = 0
break break
if virtual: if virtual:
continue continue
d = {} d = {}
m = re.match(".*/(.+)$", sysdir) diskname = os.path.basename(sysdir)
diskname = m.group(1)
for key in ['vendor', 'model']: for key in ['vendor', 'model']:
d[key] = get_file_content(sysdir + "/device/" + key) d[key] = get_file_content(sysdir + "/device/" + key)
@ -448,8 +449,8 @@ class LinuxHardware(Hardware):
d['size'] = module.pretty_bytes(float(d['sectors']) * float(d['sectorsize'])) d['size'] = module.pretty_bytes(float(d['sectors']) * float(d['sectorsize']))
d['host'] = "" d['host'] = ""
m = re.match(".+/\d+:(\w+:\w+\.\w)/host\d+/\s*", sysdir) m = re.match(".+/[a-f0-9]+:([a-f0-9]+:[a-f0-9]+\.[a-f0-9]+)/host\d+/", sysdir)
if m: if m and pcidata:
pciid = m.group(1) pciid = m.group(1)
did = re.escape(pciid) did = re.escape(pciid)
m = re.search("^" + did + "\s(.*)$", pcidata, re.MULTILINE) m = re.search("^" + did + "\s(.*)$", pcidata, re.MULTILINE)
@ -457,8 +458,9 @@ class LinuxHardware(Hardware):
d['holders'] = [] d['holders'] = []
for folder in os.listdir(sysdir + "/holders"): for folder in os.listdir(sysdir + "/holders"):
if re.search("^dm-.*", folder): if not folder.startswith("dm-"):
name = get_file_content(sysdir + "/holders/" + folder + "/dm/name") continue
name = get_file_content(sysdir + "/holders/" + folder + "/dm/name")
if name: if name:
d['holders'].append(name) d['holders'].append(name)
else: else: