进行嵌套调用以“执行”使用 Python 的 Fabric 库

发布于 2024-12-28 01:05:58 字数 1150 浏览 1 评论 0原文

Python 的 Fabric 提供了使用 execute 函数调用 fab 实用程序之外的结构函数的能力。当在另一个使用execute调用的函数中调用execute函数时,就会出现上下文问题。当调用内部执行时,Fabric 会丢失外部执行的上下文,并且永远不会恢复它。例如:

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 完全丢失其上下文,并使用 host_string='lb1' 执行 deploy_code 函数中的所有后续命令> 而不是'web1'我怎样才能让它记住它?

我想出了这个技巧,但我觉得它可能会在未来的版本中崩溃:

 with settings(**env):
     execute(remove_webserver_from_load_balancer, sHost=env.host_string)

这有效地保存了所有状态并在调用后恢复它,但看起来像是一个意外使用该功能。有没有更好的方法来告诉 Fabric 它处于嵌套执行中并使用设置堆栈或等效方法来记住状态?

谢谢!

Python's Fabric provides the ability to invoke fabric functions outside of the fab utility using the execute function. A contextual problem arises when an execute function is invoked within another function that was called using execute. Fabric loses the context of the outer execute when the inner execute is invoked and never recovers it. For example:

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)

After the first call to execute, Fabric completely loses its context and executes all further commands within the deploy_code function with host_string='lb1' instead of 'web1'. How can I get it to remember it?

I came up with this hack, but I feel like it could break on future releases:

 with settings(**env):
     execute(remove_webserver_from_load_balancer, sHost=env.host_string)

This effectively saves all state and restores it after the call, but seems like an unintended use of the function. Is there a better way to tell Fabric that it's in a nested execute and to use a settings stack or an equivalent method to remember state?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2025-01-04 01:05:58

你没有正确使用布料。因为您只需调用 fab deploy_code ,而不是像 python 一样运行 fabfile 。我建议阅读本教程,以更好地了解如何构建 fabfile。

不管怎样,你可以看看这里 了解如何使用 execute()此处了解更多具体信息。

您有一个拼写错误,因为您从网络服务器角色中删除了“s”。这可能会导致您在第二个任务中需要它时没有好的主机字符串。

除此之外,您还可以在execute() 命令本身中设置角色和主机。

Your not using fabric right. As you'd just call fab deploy_code instead of running the fabfile like it's python. I'd suggest going through the tutorial for a better idea on how to structure your fabfile.

Anyhow though, you can look here for how to use execute(), and here for more of the specifics.

You have a typo in that you've dropped the 's' from the webservers role. Which might account for you not having a good host string when you want it on the second task.

But that aside, you can also set roles and hosts in the execute() command itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文