Browse Source

Handle test mode when enabling ufw

tags/v0.2.0
Alexandre Anriot 6 years ago
parent
commit
c60bc71357
2 changed files with 12 additions and 7 deletions
  1. +8
    -3
      _modules/ufw.py
  2. +4
    -4
      _states/ufw.py

+ 8
- 3
_modules/ufw.py View File

return policy return policy


def set_enabled(enabled): def set_enabled(enabled):
cmd = 'ufw --force enable' if enabled else 'ufw disable'
if __opts__['test']:
cmd = "ufw --dry-run "
else:
cmd = "ufw "
cmd += '--force enable' if enabled else 'disable'
__salt__['cmd.run'](cmd) __salt__['cmd.run'](cmd)


def add_rule(rule): def add_rule(rule):
if __opts__['test']: if __opts__['test']:
cmd = "ufw --dry-run " + rule
cmd = "ufw --dry-run "
else: else:
cmd = "ufw " + rule
cmd = "ufw "
cmd += rule
out = __salt__['cmd.run'](cmd, python_shell=True) out = __salt__['cmd.run'](cmd, python_shell=True)
return out return out



+ 4
- 4
_states/ufw.py View File

if __salt__['ufw.is_enabled'](): if __salt__['ufw.is_enabled']():
return _unchanged(name, "UFW is already enabled") return _unchanged(name, "UFW is already enabled")


if __opts__['test']:
return _test(name, "UFW will be enabled")

try: try:
__salt__['ufw.set_enabled'](True) __salt__['ufw.set_enabled'](True)
except (CommandExecutionError, CommandNotFoundError) as e: except (CommandExecutionError, CommandNotFoundError) as e:
return _error(name, e.message) return _error(name, e.message)


return _changed(name, "UFW is enabled", enabled=True)
if __opts__['test']:
return _test(name, "UFW would have been enabled")
else:
return _changed(name, "UFW is enabled", enabled=True)




def default_incoming(name, default): def default_incoming(name, default):

Loading…
Cancel
Save