有没有办法在fabric文件中进行滚动部署?

发布于 2025-01-02 04:51:01 字数 938 浏览 1 评论 0原文

给出以下 fabfile:

from fabric.api import env, run

env.user = 'implicit_user'
env.hosts = ['host1', 'explicit_user@host2', 'host3']

def print_user():
    with hide('running'):
        run('echo "%(user)s"' % env)

当我们运行 fab print_user 时,我们得到:

[host1] out: implicit_user
[explicit_user@host2] out: explicit_user
[host3] out: implicit_user

Done.
Disconnecting from host1... done.
Disconnecting from host2... done.
Disconnecting from host3... done.

但是,我非常愿意按顺序执行整个 fab print_user ,中间间隔 10 秒确保前一个主机在下一个主机开始操作之前完成其操作:

[host1] out: implicit_user
<10 seconds here...>
[explicit_user@host2] out: explicit_user
<10 seconds here...>
[host3] out: implicit_user
<10 seconds here...>

Done.
Disconnecting from host1... done.
Disconnecting from host2... done.
Disconnecting from host3... done. 

有办法做到这一点吗?我应该如何调整我的 fabfile 来实现它?

Giving the following fabfile:

from fabric.api import env, run

env.user = 'implicit_user'
env.hosts = ['host1', 'explicit_user@host2', 'host3']

def print_user():
    with hide('running'):
        run('echo "%(user)s"' % env)

When we run fab print_user, we get:

[host1] out: implicit_user
[explicit_user@host2] out: explicit_user
[host3] out: implicit_user

Done.
Disconnecting from host1... done.
Disconnecting from host2... done.
Disconnecting from host3... done.

However, I would very much to conduct the entire fab print_user sequentially, with 10 second interval in between to make sure that the previous host is finished with its actions before the next host kicks the actions off:

[host1] out: implicit_user
<10 seconds here...>
[explicit_user@host2] out: explicit_user
<10 seconds here...>
[host3] out: implicit_user
<10 seconds here...>

Done.
Disconnecting from host1... done.
Disconnecting from host2... done.
Disconnecting from host3... done. 

Is there a way to do it? How should I tweak my fabfile to achieve it?

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

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

发布评论

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

评论(1

仙女山的月亮 2025-01-09 04:51:01

除非您通过命令行指定并行,否则您的文件已经按顺序执行。要明确此顺序执行,请使用 @serial装饰器

您希望通过延迟来处理失败吗? warn_only=False 将导致您的顺序任务之一失败而终止该任务(其他主机不会运行该任务)。这也可以在下面的示例中看到,其中一旦运行 false(它具有失败退出状态),其余主机就不会运行该任务。

from fabric.api import *
from fabric.decorators import hosts, parallel, serial
import random

@task
@serial
@with_settings(warn_only=False)
def maybe_fail():
    if random.randint(0,3) == 0:
        run("/bin/false") 
    else:
        run("/bin/true")

如果你真的想要这 10 秒的延迟,我想你可以制作一个休眠 10 秒的装饰器,或者只是在任务结束时休眠。

Your file already executes sequentially unless you are specifying parallel via the command line. To be explicit about this sequential execution use the @serial decorator .

Do you want this delay to deal with a failure? warn_only=False will cause a failure in one of your sequential tasks to terminate the task (other hosts will not run the task). This is also seen in the example below where as soon as false is run (it has failure exit status) the remaining host do not run the task.

from fabric.api import *
from fabric.decorators import hosts, parallel, serial
import random

@task
@serial
@with_settings(warn_only=False)
def maybe_fail():
    if random.randint(0,3) == 0:
        run("/bin/false") 
    else:
        run("/bin/true")

If you really want this 10 second delay I guess you could make a decorator that sleeps for 10 or just sleep at the end of your tasks.

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