首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Fabric(Python)中定义多个服务器环境?

如何在Fabric(Python)中定义多个服务器环境?
EN

Stack Overflow用户
提问于 2012-01-06 23:04:05
回答 2查看 362关注 0票数 0

我需要使用Fabric在一个网站中执行一些操作,该网站使用一台机器作为文件系统,使用另一台机器作为数据库服务器。我需要处理两个主机。我该怎么做呢?

我有一些代码,但是我不能让环境定义工作。

其思想是连接到远程文件系统服务器并获取文件,然后连接到远程数据库服务器并获取数据库模式。

我现在拥有的代码是这样的:

代码语言:javascript
复制
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm

'''
Here I define where is my "aid"s file structure
'''
local_root = '/home/andre/test' # This is the root folder for the audits 
code_location = '/remote_code' # This is the root folder dor the customer code inside each audit


#
# ENVIRONMENTS CONFIGURATIONS
#
'''
Here I configure where is the remote file server
'''
def file_server():
    env.user = 'andre'
    env.hosts = ['localhost']

'''
Here I configure where is the database server
'''
def database_server():
    env.user = 'andre'
    env.hosts = ['192.168.5.1']  


#
# START SCRIPT
#
def get_install(remote_location, aid):
    ### I will get the files
    '''
    Here I need to load the file_server() definitions
    '''    
    working_folder = local_root + '/%s' % aid # I will define the working folder 
    local('mkdir ' + working_folder) # I will create the working folder for this audit
    local('mkdir ' + working_folder + code_location) # I will create the folder to receive the code
    get(remote_location, working_folder + code_location) # I will download the code to my machine
    ### I will get the database
    '''
    Here I need to load the database_server() definitions
    ''' 
    local('dir') # Just to test

如何在get_install()中定义环境file_server()和database_server()?

诚挚的问候,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-25 22:30:49

我不太明白你到底想做什么,但也许你可以把你的get_install函数分成两个函数,每个函数对应一个服务器。

然后使用fabric.decorators.hosts(*host_list)装饰器将这些功能限制在正确的服务器上:

例如,以下代码将确保,除非在命令行上进行覆盖,否则my_func将在host1、host2和host3上运行,并且特定用户将在host1和host3上运行:

代码语言:javascript
复制
@hosts('user1@host1', 'host2', 'user2@host3')
def my_func():
    pass

(有关详细信息,请参阅http://readthedocs.org/docs/fabric/en/1.1.0/api/core/decorators.html#fabric.decorators.hosts)

然后,您可以通过定义get_install方法来一次性调用这两个函数:

代码语言:javascript
复制
def get_install():
    func1()
    func2()
票数 1
EN

Stack Overflow用户

发布于 2012-01-06 23:35:03

您应该能够使用fab database_server get_install做到这一点。基本上,工厂环境应该做你想做的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8759828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档