【问题标题】:Fabric - Run a command locally before and after all tasks completeFabric - 在所有任务完成之前和之后在本地运行命令
【发布时间】:2013-11-07 04:52:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: python fabric


    【解决方案1】:

    你可以使用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() 很有帮助,谢谢!最终使部署只运行非本地主机,然后在所有主机上运行另一个silent_deploy。看起来仍然很时髦,但它确实有效。
    猜你喜欢
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多