[英]How to issue commands on remote hosts in parallel using Fabric without using a fabfile?
I have a Python script that uses Fabric to launch tests on remote hosts, get the otuput file of the tests, and do some parsing. 我有一个Python脚本,该脚本使用Fabric在远程主机上启动测试,获取测试的otuput文件,并进行一些解析。 The Python script is not a fabfile.
Python脚本不是fabfile。
I would like to launch and run the tests in parallel. 我想并行启动和运行测试。 I've read about using the "@parallel" decorator but all the examples I've read has the script as a fabfile.
我已经读过有关使用“ @parallel”装饰器的信息,但是我阅读的所有示例都将脚本作为fabfile。
My code is something like this: 我的代码是这样的:
from fabric.api import *
# Copy the testfile on each of the hosts. This is sequential, it could be
# done in parallel but doing it in parallel is not that important
def copy_test(host_list, testfile_name):
for x in host_list:
env['host_string'] = x
target_testfile_name = "/tmp/" + testfile_name
put(testfile_name, target_testfile_name)
@parallel # Would this decorator work?
def run_test(host_list, testfile_name):
target_testfile_name = "/tmp/" + testfile_name
for x in host_list:
env['host_string'] = x
run(target_testfile_name)
if __name__ == '__main__':
HOSTS = ['10.10.10.10', '10.10.10.11', '10.10.10.12', '10.10.10.13']
testfile_name = "foo.py"
copy_test(HOSTS, testfile_name)
# I would like to launch run_test() in parallel
run_test(HOSTS, testfile_name)
This is a simplified version of the code. 这是代码的简化版本。 I have not included everything but I pass around configuration information of the hosts so that limits me in using this script as a fabfile where I issue something like:
我没有包含所有内容,但是我传递了主机的配置信息,因此限制了我将此脚本用作fabfile,在该脚本中,我发出类似以下内容:
"fab -H '10.10.10.10' copy_test"
“ fab -H '10 .10.10.10'copy_test”
"fab -H '10.10.10.10' run_test" -P
“ fab -H '10 .10.10.10'run_test” -P
I could execute run_test() using the threading.Threads library but I would rather do that as a last resort. 我可以使用threading.Threads库执行run_test(),但我宁愿做为最后的选择。
As you can see, I am not running this as a fabfile. 如您所见,我没有将它作为fabfile运行。
Is there a way I could execute run_test() using Fabric's parallel execution model without executing my script as a "fabfile"? 有没有一种方法可以使用Fabric的并行执行模型执行run_test()而不将我的脚本作为“ fabfile”执行?
I can't comment so i'll put an answer change your main to : 我无法发表评论,因此我将给出一个答案,将您的主要语言更改为:
if __name__ == '__main__':
HOSTS = ['10.10.10.10', '10.10.10.11', '10.10.10.12', '10.10.10.13']
testfile_name = "foo.py"
execute(copy_test,HOSTS, testfile_name)
# I would like to launch run_test() in parallel
execute(run_test,HOSTS, testfile_name)
if you call the fonction with execute()
and your fonction has the @parallel
it'll be launched in parallel 如果您使用
execute()
调用功能,并且您的功能具有@parallel
,则会并行启动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.