Python Fabric 任务可以调用其他任务并尊重其主机列表吗?

发布于 2024-10-24 08:47:11 字数 620 浏览 2 评论 0原文

我有一个如下所示的 fabfile:

@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()

我想做

fab deploy

并让它相当于

fab host1_deploy host2_deploy

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

有没有办法在这里更新部署()任务,以便它可以做我想要的事情,同时保留子任务,以便它们可以单独运行?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℉服软 2024-10-31 08:47:11

从 Fabric 1.3 开始,execute 助手现在可以用来执行此操作。该文档位于此处:智能执行执行任务

这是他们使用的示例:

from fabric.api import run, roles, execute

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

然后从另一个任务deploy运行migrateweb

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

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

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, execute

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.

淡紫姑娘! 2024-10-31 08:47:11

这是蹩脚的,但它从 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()

这是我的测试代码:

@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')

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')
尹雨沫 2024-10-31 08:47:11

可能有更好的方法来处理它,但是您可以将两个主机传递给deploy(),然后在host1_deploy()和host2_deploy()中检查env.host:

def host1_deploy():
    if env.host in ['host1']:
         run(whatever1)

def host2_deploy():
    if env.host in ['host2']:
         run(whatever2)

@hosts('host1','host2')
def deploy():
    host1_deploy()
    host2_deploy()

There's probably a better way to handle it, but you could pass both hosts to deploy(), and then in host1_deploy() and host2_deploy() check env.host:

def host1_deploy():
    if env.host in ['host1']:
         run(whatever1)

def host2_deploy():
    if env.host in ['host2']:
         run(whatever2)

@hosts('host1','host2')
def deploy():
    host1_deploy()
    host2_deploy()
凉月流沐 2024-10-31 08:47:11

试试这个。显然您想用 runsudo 替换 local。关键是用于 deploy 的空 @hosts 装饰器

from fabric.api import local
from fabric.decorators import hosts

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

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

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

Try this one. Obviously you want to replace local with run or sudo. The key is the empty @hosts decorator for deploy

from fabric.api import local
from fabric.decorators import hosts

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

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

@hosts('')
def deploy():
    """"Deploy to both hosts, each using its own logic"""
    host1_deploy()
    host2_deploy()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文