简体   繁体   English

如何在不使用fabfile的情况下使用Fabric在远程主机上并行发出命令?

[英]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.

相关问题 无需使用fabfile即可在结构中进行远程ssh - Remote ssh in fabric without using fabfile Fabric 使用线程在不同主机上并行运行命令 - Fabric running commands on different hosts in parallel using threads 如何使用 Fabric 和 fabfile 解密受密码保护的 SSH 密钥? - How to decrypt passphrase-protected SSH key using Fabric and fabfile? 如何确定fabfile中的结构角色以将特定文件发送到主机 - How to determine fabric role in fabfile to send specific files to hosts 如何使用结构在具有不同参数的多个主机上并行运行同一任务? - How to run the same task in parallel on multiple hosts with different parameters using fabric? 如何使用结构在具有不同参数的多个主机上并行运行同一任务? - How to run same task in parallel on multiple hosts with different parameters using fabric? 使用Fabric的Python代码不使用远程主机 - Python code using Fabric does not use remote hosts 如何在 Python Fabric `fabfile.py` 的函数中正确设置 `env.hosts`? - How can I properly set the `env.hosts` in a function in my Python Fabric `fabfile.py`? 对 fabfile 使用 sphinx autodoc - Using sphinx autodoc for a fabfile 在Python中使用Fabric,尝试在远程bash服务器上运行tsch命令 - Using Fabric in Python, trying to run tsch commands on remote bash server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM