From 8374750398ce57569d84ec3a11fd03a4b3b3ba74 Mon Sep 17 00:00:00 2001 From: Joseph Hall Date: Thu, 12 Jun 2014 11:38:42 -0600 Subject: [PATCH 1/2] Check for iptables before trying to run iptables --- salt/modules/iptables.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/salt/modules/iptables.py b/salt/modules/iptables.py index 22629578ee..90f29be1be 100644 --- a/salt/modules/iptables.py +++ b/salt/modules/iptables.py @@ -21,13 +21,14 @@ def __virtual__(): ''' Only load the module if iptables is installed ''' + if not salt.utils.which('iptables'): + return False + global HAS_CHECK if '--check' in salt_cmd.run('iptables --help', output_loglevel='quiet'): HAS_CHECK = True - if salt.utils.which('iptables'): - return True - return False + return True def _iptables_cmd(family='ipv4'): From 4ba24c7a16a739ab3696481060892875be6be220 Mon Sep 17 00:00:00 2001 From: Joseph Hall Date: Thu, 12 Jun 2014 12:14:51 -0600 Subject: [PATCH 2/2] Move HAS_CHECK out of `__virtual__()` --- salt/modules/iptables.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/salt/modules/iptables.py b/salt/modules/iptables.py index 90f29be1be..0d30fc4691 100644 --- a/salt/modules/iptables.py +++ b/salt/modules/iptables.py @@ -14,7 +14,6 @@ import salt.utils from salt.state import STATE_INTERNAL_KEYWORDS as _STATE_INTERNAL_KEYWORDS from salt.exceptions import SaltException import salt.modules.cmdmod as salt_cmd -HAS_CHECK = False def __virtual__(): @@ -24,10 +23,6 @@ def __virtual__(): if not salt.utils.which('iptables'): return False - global HAS_CHECK - if '--check' in salt_cmd.run('iptables --help', output_loglevel='quiet'): - HAS_CHECK = True - return True @@ -485,6 +480,9 @@ def check(table='filter', chain=None, rule=None, family='ipv4'): if not rule: return 'Error: Rule needs to be specified' + if '--check' in salt_cmd.run('iptables --help', output_loglevel='quiet'): + HAS_CHECK = True + if HAS_CHECK is False: cmd = '{0}-save' . format(_iptables_cmd(family)) out = __salt__['cmd.run'](cmd).find('-A {1} {2}'.format(