使用Python的Fabric库进行嵌套调用“execute”
Python的Fabric库可以让你在不使用fab
工具的情况下,调用Fabric的功能,这主要是通过execute
函数来实现的。不过,当你在一个函数里面调用另一个用execute
函数启动的函数时,就会出现一个问题。此时,Fabric会失去外层execute
的上下文,导致它无法记住之前的状态。例如:
env.roledefs = {
'webservers': ['web1','web2'],
'load_balancer': ['lb1']
}
@roles('webserver')
def deploy_code():
#ship over tar.gz of code to unpack.
...
execute(remove_webserver_from_load_balancer, sHost=env.host_string)
...
#shutdown webserver, unpack files, and restart web server
...
execute(add_webserver_to_load_balancer, sHost=env.host_string)
@roles('load_balancer')
def remove_webserver_from_load_balancer(sHost=None):
ssh("remove_host %s" % sHost)
execute(deploy_code)
在第一次调用execute
之后,Fabric完全失去了上下文,接下来的所有命令都会在deploy_code
函数中以host_string='lb1'
的方式执行,而不是'web1'
。 我该如何让它记住这个状态呢?
我想出了一个解决办法,但我觉得在未来的版本中可能会出问题:
with settings(**env):
execute(remove_webserver_from_load_balancer, sHost=env.host_string)
这个方法实际上是保存了所有的状态,并在调用后恢复,但看起来像是对这个函数的意外使用。有没有更好的方法可以告诉Fabric它在一个嵌套的execute中,并使用一个设置堆栈或类似的方法来记住状态呢?
谢谢!