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

如何在Fabric(Python)中定义多个服务器环境?,python,fabric,Python,Fabric,我需要使用Fabric在一个网站中执行一些操作,该网站使用一台机器作为文件系统,另一台机器作为数据库服务器。我需要处理两个主机。我该怎么做 我有一些代码,但无法使环境定义正常工作 其思想是连接到远程文件系统服务器并获取文件,然后连接到远程数据库服务器并获取数据库模式 我现在的代码是这样的: from __future__ import with_statement from fabric.api import * from fabric.contrib.console import confir

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

我有一些代码,但无法使环境定义正常工作

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

我现在的代码是这样的:

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()中定义环境文件_server()和数据库_server()


致以最诚挚的问候,

您应该能够使用
fab数据库服务器get\u install执行此操作。基本上,fab[environment][command]应该做你想做的事情。

我不太明白你到底想做什么,但也许你可以将get\u install函数拆分为两个函数,每个函数对应于每台服务器

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

例如,除非在命令行上进行重写,否则以下操作将确保my_func将在host1、host2和host3上运行,并在host1和host3上与特定用户一起运行:

@hosts('user1@host1', 'host2', 'user2@host3')
def my_func():
    pass
(有关更多信息,请参阅)

通过将get_install方法定义为:

def get_install():
    func1()
    func2()

对于文件服务器()和运行“fab get\u install”的数据库服务器(),有一种方法可以做到这一点吗?我照你说的做,但我必须马上做。有可能吗?你是说只需键入一个命令?我不这么认为。关键是让动作独立于环境,然后能够交换环境。但我对面料不太了解,所以可能有。