2012-01-06 17 views
0

ファブリックを使用して、ファイルシステムに1台のマシンを使用し、データベースサーバーに他のマシンを使用するWebサイトでいくつかの操作を行う必要があります。私は2つのホストを処理する必要があります。どうやってやるの?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関数を各サーバーごとに2つの関数に分割できます。

そしてfabric.decorators.hosts(* host_listを)デコレータで正しいサーバにこれらの機能を制限たとえば

、以下では、コマンドラインで上書きを禁止する、ことを保証する、my_funcは上で実行されますHOST1、HOST2およびHOST3、およびhost1とHOST3上の特定のユーザーと:

@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

"fab get_install"を実行しているfile_server()とdatabase_server()にこれを行う方法がありますか?私はあなたが言ったようにしていますが、私はすぐにそれを行う必要があります。可能です? –

+0

ただ1つのコマンドを入力するだけですか?私はそうは思わない。全体のポイントは、環境から独立したアクションを持つことで、環境を交換することができるようにすることです。しかし、私はファブリックをよく知っていないので、おそらくそこにあります。 – Tom

関連する問題