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?
I also have the same problem. Next Code is not ideal, but I have an implementation like this currently.
fabfile.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.')
Result:
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.