2012-01-06 105 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

相關問題