2012-01-06 62 views
0

我需要使用Fabric在网站中执行一些操作,这些操作使用一台计算机作为文件系统,其他计算机使用数据库服务器。我需要处理两台主机。我怎样才能做到这一点?如何在Fabric(Python)中定义多个服务器环境?

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

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

,我对现在的代码是这样的:

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()?

最好的问候,

回答

1

我不明白你正在尝试做什么,但也许你可以把你的get_install函数分成两个函数,每个函数对应每个服务器。

然后限制到正确的服务器的那些功能与fabric.decorators.hosts(* host_list)装饰:

例如,下面将确保,除非在命令行上的覆盖,my_func,并将将被运行在主机1,主机2和主机,并与主机1和主机3特定用户:

@hosts('[email protected]', 'host2', '[email protected]') 
def my_func(): 
    pass 

(欲了解更多信息请参阅http://readthedocs.org/docs/fabric/en/1.1.0/api/core/decorators.html#fabric.decorators.hosts

而且你可以比通过定义get_install方法调用一气呵成那些2个功能:

def get_install(): 
    func1() 
    func2() 
0

你应该能够fab database_server get_install做到这一点。基本上,fab [环境] [命令]应该做你想做的事情。

+0

有一种方法可以为file_server()和database_server()运行“fab get_install”?我像你说的那样做,但我需要立即做。有可能的? – 2012-01-06 15:49:15

+0

你的意思是只需输入一个命令?我不这么认为。整个观点是要有独立于环境的行动,然后能够将环境交换出去。但我不太了解Fabric,所以也许有。 – Tom 2012-01-06 17:23:58

相关问题