gpt4 book ai didi

python - Fabric - 在所有任务完成之前和之后在本地运行命令

转载 作者:太空狗 更新时间:2023-10-30 03:03:14 25 4
gpt4 key购买 nike

我试图在我的结构脚本中宣布部署开始和结束。感觉这应该很容易,但对于我的生活,我不知道该怎么做。

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”。它将尝试在每台服务器上运行所有这些任务。

def deploy(): 
announce_start()
# actual deployment code here
announce_finish()

如果我用@hosts('localhost') 修饰 announce_start() 和 announce_end(),它会在本地主机上运行它,但仍然运行四次。每个主机一个。

当我输入这个时,我终于通过在 announce_start/end 上使用装饰器 @hosts('localhost') 和 fab 命令让它工作:

fab announce_start deploy announce_end

但这似乎有点老套。我希望所有这些都包含在一个部署命令中。有办法做到这一点吗?

最佳答案

您可以使用 fabric.api.execute,例如

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

关于python - Fabric - 在所有任务完成之前和之后在本地运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19828248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com