结构-在所有任务完成之前和之后在本地运行命令 [英] Fabric - Run a command locally before and after all tasks complete
问题描述
我正在尝试在结构脚本中宣布部署开始和结束.这样的感觉应该很容易,但是对于我自己的一生,我不知道该怎么做.
I'm attempting to announce deployment start and end in my fabric script. Feels like this should be easy, but for the life of me I can't figure out how to do it.
env.hosts = ['www1', 'www2', 'www3', 'www4']
def announce_start():
# code to connect to irc server and announce deployment begins
pass
def announce_finish():
# code to connect to irc server and announce deployment finishes
pass
def deploy():
# actual deployment code here
pass
这是我尝试过的:
如果我使部署任务包含"announce_start"和"announce_finish".它将尝试在每台服务器上运行所有这些任务.
If I make my deploy task contain 'announce_start' and 'announce_finish'. It will attempt to run all those tasks on each server.
def deploy():
announce_start()
# actual deployment code here
announce_finish()
如果我用@hosts('localhost')装饰announce_start()和announce_end(),它会在localhost上运行它,但是仍然运行四次.每个主机一个.
If I decorate announce_start() and announce_end() with @hosts('localhost'), it runs it on localhost, but still four times. One for each host.
当我键入此命令时,我终于通过在announce_start/end上使用装饰器@hosts('localhost')和fab命令使其工作:
As I was typing this, I finally got it to work by using the decorator @hosts('localhost') on announce_start/end and the fab command:
fab announce_start deploy announce_end
但是,这似乎有些古怪.我希望所有这些都包装在一个deploy命令中.有办法吗?
But this seems a bit hacky. I'd like it all wrapped in a single deploy command. Is there a way to do this?
推荐答案
例如,您可以使用fabric.api.execute
You can use fabric.api.execute, e.g.
def announce_start():
# code to connect to irc server and announce deployment begins
pass
def announce_finish():
# code to connect to irc server and announce deployment finishes
pass
@hosts(...)
def deploy_machine1():
pass
@hosts(...)
def deploy_machine2():
pass
def deploy():
announce_start()
execute(deploy_machine1)
execute(deploy_machine2)
announce_finish()
然后仅调用fab deploy
and then just invoke fab deploy
这篇关于结构-在所有任务完成之前和之后在本地运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!