4

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

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(),它会在localhost 上运行它,但仍然是四次。每个主机一个。

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

fab announce_start deploy announce_end

但这似乎有点骇人听闻。我希望它全部包含在一个部署命令中。有没有办法做到这一点?

4

1 回答 1

4

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

于 2013-11-07T06:36:25.333 回答