Just an update on this for anyone who may have had the same question -

Looking through the Fabric code the ssh/paramiko portions use the network.py 
file which automatically calls the abort() function for failures setting up the 
ssh tunnel (ie: socket timeout, lookup failures, etc).

The fabric run/local calls however call the _handle_failure method which 
observes the env.warn_only setting and calls either the warn() or abort() 
methods appropriately.

Since these settings don't apply to the ssh tunnel paramiko setup obviously 
warn_only wouldn't have any affect if the ssh connection failed and abort() is 
called directly.

However the header block on the abort() method was nice enough to contain the 
comment below which made it obvious how to have fabric not fatally quit if the 
ssh connection fails:
def abort(msg):
    """
    Abort execution, print ``msg`` to stderr and exit with error status (1.)

    This function currently makes use of `sys.exit`_, which raises
    `SystemExit`_. Therefore, it's possible to detect and recover from inner
    calls to `abort` by using ``except SystemExit`` or similar.

    .. _sys.exit: http://docs.python.org/library/sys.html#sys.exit
    .. _SystemExit: 
http://docs.python.org/library/exceptions.html#exceptions.SystemExit
    """

So something like the following would do the trick:

def verify_host():
    """
    HELPER: Host SSH check to make sure it responds
    """
    logger.info("INFO: Adding %s to run queue" % env['host_string'])
    try:
        myhost = run("hostname").stdout
        logger.info("INFO: Added %s to run queue" % myhost)
    except SystemExit, e:
        logger.error("ERROR: Couldn't setup initial connection to %s" %
        env['host_string'])

-Chris H

From: [email protected] 
[mailto:[email protected]] On Behalf Of 
Chris Henry
Sent: Friday, July 29, 2011 5:50 PM
To: [email protected]
Subject: [Fab-user] warn_only with fatal host errors

So far with all of my fabric code I've been able to wrap any everything I 
didn't want to end my script via:
with settings(warn_only=True):
                do pythonic something here

however this seems to still fail and not purely warn if it is unable to connect 
to a host.  Ie: No route to host, invalid DNS name, etc.

Is there any way to keep fabric from exiting out on this and letting me 
continue?  Not of the other settings in env seem to be related to exit behavior

Examples:

Invalid hostname:

In [12]: env['host_string']='someinvalidhost.mydomain.blah'
In [13]: with settings(warn_only=True):
    run("ls")
   ....:
   ....:
[someinvalidhost.mydomain.blah] run: ls

Fatal error: Name lookup failed for someinvalidhost.mydomain.blah

Aborting.
---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)


No Route to host:

In [18]: env['host_string']='10.1.1.1'
In [19]: with settings(warn_only=True):
    run("help")
   ....:
   ....:
[10.21.1.200] run: help

Fatal error: Low level socket error connecting to host 10.21.1.200: No route to 
host

Aborting.
---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)

-Chris H

_______________________________________________
Fab-user mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/fab-user

Reply via email to