如何在Fabric文件中设置目标主机 [英] How to set target hosts in Fabric file

查看:81
本文介绍了如何在Fabric文件中设置目标主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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:

当我创建set_hosts()任务时,如

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

这样做的好处是testprod函数可以在 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屋!

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