有没有办法在fabric文件中进行滚动部署? [英] Is there a way to conduct rolling deployment in fabric files?

查看:93
本文介绍了有没有办法在fabric文件中进行滚动部署?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 从fabric.api import env,运行

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

完成。
断开与host1 ...完成。
断开与host2的连接...完成。
断开与host3的连接...完成。

然而,我非常想要执行整个 fab print_user 顺序,间隔10秒间隔,以确保先前的主机完成其操作,然后下一个主机将踢的动作关闭:

  [host1] out:implicit_user 
< 10秒这里...>
[explicit_user @ host2] out:explicit_user
< 10秒这里...>
[host3] out:implicit_user
< 10秒这里...>

完成。
断开与host1 ...完成。
断开与host2的连接...完成。
断开与host3的连接...完成。

有办法吗?我应该如何调整我的fabfile来实现?

解决方案

您的文件已经执行顺序,除非您通过命令行指定并行。要明确地说明这个顺序执行,请使用 @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():
如果random.randint(0,3)== 0:
运行(/ bin / false)
else:
run(/ bin / true)

如果你真的想要这个10秒延迟我猜你可以让一个睡眠10或只是在你的任务结束睡觉的装饰。


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?

解决方案

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.

这篇关于有没有办法在fabric文件中进行滚动部署?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆