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?
This is lame but it works as of Fabric 1.1.2
here's my test code:
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.
Try this one. Obviously you want to replace local with
run
orsudo
. The key is the empty@hosts
decorator fordeploy
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: