【问题标题】:How can I tell if a python fabric connection is ready?如何判断 python 结构连接是否已准备好?
【发布时间】:2020-03-25 14:55:08
【问题描述】:

我有一个创建 AWS 实例并使用 fabric==2.5.0 配置它们的脚本。这是我在脚本中使用 fabric2 Connection 实例的问题:

def setup_site(self, ip, name):
    connection = Connection(ip, user='toolman')
    # If I put ipdb here, just continuing it will work.
    # If take the debug line out, it will fail
    # with "NoValidConnectionsError: [Errno None] Unable to connect to port 22 on ..."
    # import ipdb; ipdb.set_trace()
    connection.put(self.create_setup_file(name), 'setup.yaml')

我可以点击“c”继续调试器,每次都一切正常。如果我没有调试器线,connection.put 调用每次都会失败,并显示“NoValidConnectionsError: [Errno None] Unable to connect to port 22 on ...”。

将文件放入的 AWS 实例正在运行(instance.wait_until_running() 在调用 setup_site 的代码中返回)。

【问题讨论】:

  • 您可能需要调用connection.open() 来初始化连接 - docs.fabfile.org/en/2.5/api/…
  • 我会尝试,但我不需要在发生 ipdb 中断的情况下这样做

标签: python amazon-web-services amazon-ec2 fabric


【解决方案1】:

如果我让 python 睡 30 秒一切正常,那么这个问题可能是 AWS 实例中没有准备好的问题。

connection.open() 不应该是必需的,但有时可能是因为它需要时间。

我最终使用以下函数代替了 Connection 调用:

def ensure_connection(*connection_args, ensure_connection_timeout=30, **connection_kwargs):
    start = timer()
    while (timer() - start) < ensure_connection_timeout:
        try:
            connection = Connection(*connection_args, **connection_kwargs)
            connection.open()
            return connection
        except NoValidConnectionsError as e:
            connection_error = e
    # noinspection PyUnboundLocalVariable
    raise connection_error

【讨论】:

    猜你喜欢
    • 2010-10-14
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2019-08-20
    • 2013-12-31
    • 2011-10-23
    • 2012-05-01
    相关资源
    最近更新 更多