2

Is there a received wisdom on how to clean up (e.g. remove temp files etc.) in a fabric task? if I use the atexit module, as I would normally, then I have difficulty because I can't use the @roles decorator to decorate the function passed to atexit.register(). Or can I? How are other fabric users dealing with this?

4

1 回答 1

5

我也有同样的问题。Next Code 并不理想,但我目前有这样的实现。

工厂文件.py

from functools import wraps
from fabric.network import needs_host
from fabric.api import run, env

def runs_final(func):
    @wraps(func)
    def decorated(*args, **kwargs):
        if env.host_string == env.all_hosts[-1]:
            return func(*args, **kwargs)
        else:
            return None
    return decorated

@needs_host
def hello():
    run('hostname')
    atexit()

@runs_final
def atexit():
    print ('this is at exit command.')

结果:

fabric$ fab hello -H web01,web02
>[web01] Executing task 'hello'
>[web01] run: hostname
>[web01] out: web01
>[web01] out: 
>[web02] Executing task 'hello'
>[web02] run: hostname
>[web02] out: web02
>[web02] out: 
>
>this is at exit command.
>
>Done.
于 2013-07-18T17:40:38.260 回答