Python 结构-在所有任务完成之前和之后在本地运行命令

Python 结构-在所有任务完成之前和之后在本地运行命令,python,fabric,Python,Fabric,我正在尝试在结构脚本中宣布部署开始和结束。感觉这应该很容易,但就我的一生而言,我不知道该怎么做 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 s

我正在尝试在结构脚本中宣布部署开始和结束。感觉这应该很容易,但就我的一生而言,我不知道该怎么做

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
以下是我尝试过的:

如果我使部署任务包含“宣布开始”和“宣布完成”。它将尝试在每台服务器上运行所有这些任务

def deploy(): 
    announce_start()
    # actual deployment code here
    announce_finish()
如果我用@hosts('localhost')修饰annound_start()和annound_end(),它会在localhost上运行它,但仍然会运行四次。每台主机一台

当我键入此命令时,我最终通过在announce\u start/end上使用decorator@hosts('localhost')和fab命令使其工作:

fab announce_start deploy announce_end

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

您可以使用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

execute()就可以了,谢谢!最终使deploy只运行非本地主机,然后是另一个在所有主机上运行的静默部署。看起来仍然很时髦,但它很管用。