关于如何清理(例如,删除临时文件等),是否有公认的智慧?在fabric任务中?如果我像往常一样使用atexit模块,那么就会遇到困难,因为我不能使用@roles装饰器来修饰传递给atexit.register()的函数。或者我可以吗?其他fabric用户是如何处理这一问题的?
发布于 2013-07-19 01:40:38
我也有同样的问题。Next Code并不理想,但我目前有一个类似这样的实现。
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.')结果:
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.https://stackoverflow.com/questions/17641195
复制相似问题