我需要使用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()中定义环境file_server()和database_server()?
诚挚的问候,
发布于 2012-01-25 22:30:49
我不太明白你到底想做什么,但也许你可以把你的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()发布于 2012-01-06 23:35:03
您应该能够使用fab database_server get_install做到这一点。基本上,工厂环境应该做你想做的事情。
https://stackoverflow.com/questions/8759828
复制相似问题