Skip to content

Commit

Permalink
Condense df sanitization
Browse files Browse the repository at this point in the history
  • Loading branch information
techhat committed Oct 12, 2013
1 parent 8e5afe5 commit 6a9752c
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions salt/modules/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ def __virtual__():
return 'disk'


def _clean_flags(args, caller):
'''
Sanitize flags passed into df
'''
flags = ''
allowed = ('a', 'B', 'h', 'H', 'i', 'k', 'l', 'P', 't', 'T', 'x', 'v')
for flag in args:
if flag in allowed:
flags += flag
else:
raise CommandExecutionError(
'Invalid flag passed to {0}'.format(caller)
)
return flags


def usage(args=None):
'''
Return usage information for volumes mounted on this minion
Expand All @@ -33,20 +49,14 @@ def usage(args=None):
salt '*' disk.usage
'''
flags = ''
allowed = ('a', 'B', 'h', 'H', 'i', 'k', 'l', 'P', 't', 'T', 'x', 'v')
for flag in args:
if flag in allowed:
flags += flag
else:
raise CommandExecutionError('Invalid flag passed to disk.usage')
flags = _clean_flags(args, 'disk.usage')
if __grains__['kernel'] == 'Linux':
cmd = 'df -P'
elif __grains__['kernel'] == 'OpenBSD':
cmd = 'df -kP'
else:
cmd = 'df'
if args:
if flags:
cmd += ' -{0}'.format(flags)
ret = {}
out = __salt__['cmd.run'](cmd).splitlines()
Expand Down Expand Up @@ -95,17 +105,9 @@ def inodeusage(args=None):
salt '*' disk.inodeusage
'''
flags = ''
allowed = ('a', 'B', 'h', 'H', 'i', 'k', 'l', 'P', 't', 'T', 'x', 'v')
for flag in args:
if flag in allowed:
flags += flag
else:
raise CommandExecutionError(
'Invalid flag passed to disk.inodeusage'
)
flags = _clean_flags(args, 'disk.inodeusage')
cmd = 'df -i'
if args is not None:
if flags:
cmd += ' -{0}'.format(flags)
ret = {}
out = __salt__['cmd.run'](cmd).splitlines()
Expand Down

0 comments on commit 6a9752c

Please sign in to comment.