【问题标题】:Fabric - ThreadingGroup exception stops remaining requests?Fabric - ThreadingGroup 异常停止剩余请求?
【发布时间】:2018-12-13 14:12:13
【问题描述】:

我是 Fabric 新手,想对一些远程 SSH 服务器并行执行一系列命令。

看来我应该使用 ThreadingGroup 来做到这一点,我可以做到,而且似乎可以工作。

我唯一真正的问题是我想了解如何处理错误情况,以及如何将服务器列表作为字符串传递,我可以从文件或命令行获取。

我该怎么做?

【问题讨论】:

    标签: python python-2.7 fabric


    【解决方案1】:

    我找到了an example in a github issue,并将其扩展为适用于我的用例...希望对您有所帮助!

    test.py

    # requires fabric 2.x - run 'pip install fabric' to install it
    import logging, socket, paramiko.ssh_exception
    from fabric import Connection, Config, SerialGroup, ThreadingGroup, exceptions, runners
    from fabric.exceptions import GroupException
    
    # Note: You need to supply your own valid servers here to ssh to of course!
    def main():
        testHosts("All should succeed", "validServer1,validServer2,validServer3")
        testHosts("Some should fail", "validServer1,validServer2,BADSERVER1,validServer3,BADSERVER2")
    
    def testHosts(message, hostsAsString):
        print("")
        print(message)
    
        # Get list of hosts from somewhere, and convert them to connections
        hosts = hostsAsString.split(",")
        servers = [Connection(host=host) for host in hosts]
            
        # Create a thread group to run requests in parallel
        g = ThreadingGroup.from_connections(servers)
        try:
            command = "df -h / | tail -n1 | awk '{print $5}'"
            results = g.run(command, hide=True)
            for r in results:
                connection = results[r]
                print("{}".format(r.host) )
                print("  SUCCESS, " + connection.stdout.strip())
        except GroupException as e:
            # If an exception occurred, at least one request failed. 
            # Iterate through results here
            for c, r in e.result.items():
                print("{}".format(c.host) )
                if isinstance(r,runners.Result) :
                    print("  SUCCESS, " + r.stdout.strip())
                elif isinstance(r,socket.gaierror) :
                    print("  FAILED,  Network error")
                elif isinstance(r,paramiko.ssh_exception.AuthenticationException) :
                    print("  FAILED,  Auth failed")
                else:
                    print("  FAILED,  Something other reason")
    
    main()
    

    产生以下输出

    $ python test.py
    
    All should succeed
    validServer1
      SUCCESS, 59%
    validServer2
      SUCCESS, 54%
    validServer3
      SUCCESS, 53%
    
    Some should fail
    validServer1
      SUCCESS, 59%
    validServer2
      SUCCESS, 54%
    validServer3
      SUCCESS, 53%
    BADSERVER1
      FAILED,  Network error
    BADSERVER2
      FAILED,  Network error
    

    【讨论】:

    • 如果故障设备列为第一台主机,此脚本将无法正常工作!仍然不确定为什么。因此,如果: 1.1.1.1 已启动且 2.2.2.2 已关闭,则“1.1.1.1,2.2.2.2”将起作用。 “2.2.2.2,1.1.1.1”会起作用,但也会出现屏幕上的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多