some fabric trap and tricks

Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Here’s some trap and tricks I met and got from my usual use.

trap 1: operation error  cause execution abort
fabric operations like run or put, would cause task execute to fail when error occured.
trick:
run fab with -w option, or turn on warn_only temporarily with code like this:

with settings(warn_only = True):
    run('some commmand')

trap 2. low level network error cause execution abort
Low level network error like name resolve error, connection timeout, connection refuesed, would normally cause execution abort.
trick:
catch NetworkError exception like this:

from fabric.exceptions import NetworkError
#...
@task
def some_task():
    try:
        whatever()
    except NetworkError as e:
        print e

trap 3. too many connection/thread cause execution abort
Fabric will keep a connection cache for performance, but when you are running task on thousands of hosts, Fabric may run out of resource(especially reach thread limit) with error message like thread.error: can’t start new thread
trick:
turn on eagerly_disconnect setting to close connection resource as soon as possible

env.eagerly_disconnect = True

This entry was posted in Programming, Python, System Administration and tagged , . Bookmark the permalink.

Leave a Reply