Failure handling with fabric

I am trying to handle a failure on fabric, but the example I saw in the docs was too localized to my liking. I need to perform rollback actions if any of several actions fail. Then I tried to use contexts to handle it, for example:

@_contextmanager
def failwrapper():
    with settings(warn_only=True):
        result = yield
    if result.failed:
        rollback()
        abort("********* Failed to execute deploy! *********")

And then

@task
def deploy():
    with failwrapper():
        updateCode()
        migrateDb()
        restartServer()

Unfortunately, when one of these tasks fails, I don't get anything on result.

Is there any way to do this? Or is there another way to deal with such situations?

+5
source share
2 answers

According to my tests, you can do this:

@_contextmanager
def failwrapper():
    try:
        yield
    except SystemExit:
        rollback()
        abort("********* Failed to execute deploy! *********")

, warn_only, , -, , , abort().

SystemExit , warn_only . .

+6

(Ctrl-C) :

@_contextmanager
def failwrapper():
    try:
        yield
    except:
        rollback()
        raise
+1

All Articles