Skip to content

Commit

Permalink
Fix traceback in disks grains when /sys/block not available
Browse files Browse the repository at this point in the history
This prevents a traceback when salt is run in a container where
/sys/block is not present.
  • Loading branch information
terminalmage committed Feb 1, 2018
1 parent 5c3ede4 commit dfa6911
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions salt/grains/disks.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,23 @@ def _linux_disks():
ret = {'disks': [], 'SSDs': []}

for entry in glob.glob('/sys/block/*/queue/rotational'):
with salt.utils.files.fopen(entry) as entry_fp:
device = entry.split('/')[3]
flag = entry_fp.read(1)
if flag == '0':
ret['SSDs'].append(device)
log.trace('Device %s reports itself as an SSD', device)
elif flag == '1':
ret['disks'].append(device)
log.trace('Device %s reports itself as an HDD', device)
else:
log.trace(
'Unable to identify device %s as an SSD or HDD. It does '
'not report 0 or 1', device
)
try:
with salt.utils.files.fopen(entry) as entry_fp:
device = entry.split('/')[3]
flag = entry_fp.read(1)
if flag == '0':
ret['SSDs'].append(device)
log.trace('Device %s reports itself as an SSD', device)
elif flag == '1':
ret['disks'].append(device)
log.trace('Device %s reports itself as an HDD', device)
else:
log.trace(
'Unable to identify device %s as an SSD or HDD. It does '
'not report 0 or 1', device
)
except IOError:
pass
return ret


Expand Down

0 comments on commit dfa6911

Please sign in to comment.