fabric/fabric — Issue #449: Flag to `run` allowing expected command failures
Author: wolever
Created: 2011-10-11 17:46:57+00:00
State: open

It would be nice if a flag could be passed to `run` (and friends) which says “I expect this command to return non-zero, so don't issue a warning or error if it appars to fail”.

For a simple example: I'd like to use `ping` to check if a host is up, but since `ping` returns non-zero if the host isn't up, this is difficult to do from Fabric (as it will either result in an exit, or issue an unnecessary warning).

Would a patch adding an optional argument named something like `failure_ok` be considered?

I imagine it would be used like this:

def check_host():
out = local("ping -t1 -W1 1.2.3.4 > /dev/null", failure_ok=True)
if out.return_code == 0:
print "the host is up"
else:
print "the host isn't up"

Which would produce the output:

$ fab -f my_fabfile.py check_host
[localhost] local: ping -t1 -W1 1.2.3.4 > /dev/null
the host isn't up

Done.

Instead of the current, noisy, output:

$ fab -f my_fabfile.py check_host
[localhost] local: ping -t1 -W1 1.2.3.4 > /dev/null

Warning: local() encountered an error (return code 2) while executing 'ping -t1 -W1 1.2.3.4 > /dev/null'

the host isn't up

Done.