如何在Fabric文件中设置目标主机 [英] How to set target hosts in Fabric file
问题描述
我想使用Fabric将我的Web应用程序代码部署到开发,登台和生产服务器.我的fabfile:
I want to use Fabric to deploy my web app code to development, staging and production servers. My fabfile:
def deploy_2_dev():
deploy('dev')
def deploy_2_staging():
deploy('staging')
def deploy_2_prod():
deploy('prod')
def deploy(server):
print 'env.hosts:', env.hosts
env.hosts = [server]
print 'env.hosts:', env.hosts
示例输出:
host:folder user$ fab deploy_2_dev
env.hosts: []
env.hosts: ['dev']
No hosts found. Please specify (single) host string for connection:
When I create a set_hosts()
task as shown in the Fabric docs, env.hosts is set properly. However, this is not a viable option, neither is a decorator. Passing hosts on the command line would ultimately result in some kind of shell script that calls the fabfile, I would prefer having one single tool do the job properly.
在Fabric文档中说"env.hosts仅仅是一个Python列表对象".根据我的观察,这是完全不正确的.
It says in the Fabric docs that 'env.hosts is simply a Python list object'. From my observations, this is simply not true.
任何人都可以解释这里发生了什么吗?如何设置要部署到的主机?
Can anyone explain what is going on here ? How can I set the host to deploy to ?
推荐答案
我通过为每个环境声明一个实际函数来做到这一点.例如:
I do this by declaring an actual function for each environment. For example:
def test():
env.user = 'testuser'
env.hosts = ['test.server.com']
def prod():
env.user = 'produser'
env.hosts = ['prod.server.com']
def deploy():
...
使用以上功能,我将键入以下内容以部署到我的测试环境:
Using the above functions, I would type the following to deploy to my test environment:
fab test deploy
...以及以下内容部署到生产环境:
...and the following to deploy to production:
fab prod deploy
这样做的好处是test
和prod
函数可以在 any fab函数之前使用,而不仅仅是部署.这是非常有用的.
The nice thing about doing it this way is that the test
and prod
functions can be used before any fab function, not just deploy. It is incredibly useful.
这篇关于如何在Fabric文件中设置目标主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!