Can a Python Fabric task invoke other tasks and respect their hosts lists?

后端 未结 4 1163
误落风尘
误落风尘 2020-12-30 20:52

I have a fabfile like the following:

@hosts(\'host1\')
def host1_deploy():
    \"\"\"Some logic that is specific to deploying to host1\"\"\"

@hosts(\'host2\         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 21:01

    This is lame but it works as of Fabric 1.1.2

    def host1_deploy():
        """Some logic that is specific to deploying to host1"""
        if env.host in ["host1"]:
            pass #this is only on host2
    
    def host2_deploy():
        """Some logic that is specific to deploying to host2"""
        if env.host in ["host2"]:
            pass #this is only on host2
    
    def deploy():
        """"Deploy to both hosts, each using its own logic"""
        host1_deploy()
        host2_deploy()
    

    here's my test code:

    @task
    @roles(["prod_web","prod_workers"])
    def test_multi():
        test_multi_a()
        test_multi_b()
    
    def test_multi_a():
        if env.host in env.roledefs["prod_web"]:
            run('uname -a')
    
    def test_multi_b():
        if env.host in env.roledefs["prod_workers"]:
            run('uname -a')
    

提交回复
热议问题