我需要使用Fabric在网站中执行某些操作,该网站使用一台机器用于文件系统,而其他机器用于数据库服务器。我需要处理两个主机。我怎么能这样做?
我有一些代码,但我无法使环境定义起作用。
我们的想法是连接到远程Filesystem服务器并获取文件,然后连接到远程数据库服务器并获取数据库架构。
我现在的代码是这样的:
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()?
最诚挚的问候,
答案 0 :(得分:1)
我不确切地知道你要做什么,但也许你可以将你的get_install函数分成两个函数,每个函数用于每个服务器。
然后使用fabric.decorators.hosts(* host_list)装饰器将这些函数限制到正确的服务器:
例如,以下内容将确保在命令行上禁止覆盖,my_func将在host1,host2和host3上运行,并在host1和host3上与特定用户一起运行:
@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方法定义为:
来一次性调用这两个函数def get_install():
func1()
func2()
答案 1 :(得分:0)
您应该可以使用fab database_server get_install
执行此操作。基本上,fab [environment] [command]应该做你想做的事。