Python Fabric任务可以调用其他任务并尊重其主机列表吗? [英] Can a Python Fabric task invoke other tasks and respect their hosts lists?

查看:245
本文介绍了Python Fabric任务可以调用其他任务并尊重其主机列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个fabfile,如下所示:

  @hosts('host1')
def host1_deploy() :
一些特定于部署到host1的逻辑

@hosts('host2')
def host2_deploy():
一些特定于部署到host2的逻辑

def deploy():
部署到两个主机,每个使用自己的逻辑
host1_deploy()
host2_deploy()

我想做

  fab deploy 

并且等于

  fab host1_deploy host2_deploy 

换句话说,运行每个子任务,每个子任务使用它指定的主机列表,但这不起作用,而是()任务需要自己的主机列表,它将传播到其所有的子任务。



有没有办法在这里更新deploy()任务,这样它会做我想要的

解决方案

由于Fabric 1.3,执行 helper现在可以做到这一点。该文档可在此处获取:智能执行任务执行



以下是他们使用的示例:

  from fabric.api导入运行,角色

env.roledefs = {
'db':['db1','db2'],
'web':['web1','web2 ','web3'],
}

@roles('db')
def migrate():

pass

@roles('web')
def update():
#这里的代码更新。
pass

然后运行 migrate web 从另一个任务部署



<$
执行(更新)

这将尊重这些任务的角色和主机列表。


I have a fabfile like the following:

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

@hosts('host2')
def host2_deploy():
    """Some logic that is specific to deploying to host2"""

def deploy():
    """"Deploy to both hosts, each using its own logic"""
    host1_deploy()
    host2_deploy()

I would like to do

fab deploy

and have it be equivalent to

fab host1_deploy host2_deploy

In other words, run each of the subtasks and for each one use the list of hosts that it specifies. However, this does not work. Instead, the deploy() task wants its own list of hosts that it will propogate to all of its subtasks.

Is there a way to update the deploy() task here so it will do what I want while leaving the subtasks alone so they can be run individually?

解决方案

Since Fabric 1.3, the execute helper is now available to do just this. The documentation is available here: Intelligently executing tasks with execute.

Here is the example they use:

from fabric.api import run, roles

env.roledefs = {
    'db': ['db1', 'db2'],
    'web': ['web1', 'web2', 'web3'],
}

@roles('db')
def migrate():
    # Database stuff here.
    pass

@roles('web')
def update():
    # Code updates here.
   pass

And then to run both migrate and web from another task deploy:

def deploy():
    execute(migrate)
    execute(update)

And this will respect the roles and hosts lists that those tasks have.

这篇关于Python Fabric任务可以调用其他任务并尊重其主机列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆