Python Fabric 任务可以调用其他任务并尊重其主机列表吗?
我有一个如下所示的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 Fabric 1.3 开始,
execute
助手现在可以用来执行此操作。该文档位于此处:智能执行执行任务。这是他们使用的示例:
然后从另一个任务
deploy
运行migrate
和web
:这将尊重角色和主机列表这些任务有。
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:
And then to run both
migrate
andweb
from another taskdeploy
:And this will respect the roles and hosts lists that those tasks have.
这是蹩脚的,但它从 Fabric 1.1.2 开始工作,
这是我的测试代码:
This is lame but it works as of Fabric 1.1.2
here's my test code:
可能有更好的方法来处理它,但是您可以将两个主机传递给deploy(),然后在host1_deploy()和host2_deploy()中检查env.host:
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:
试试这个。显然您想用
run
或sudo
替换 local。关键是用于deploy
的空@hosts
装饰器Try this one. Obviously you want to replace local with
run
orsudo
. The key is the empty@hosts
decorator fordeploy