Is there a way to conduct rolling deployment in fa

2019-02-10 23:32发布

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?

1条回答
你好瞎i
2楼-- · 2019-02-11 00:04

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.

查看更多
登录 后发表回答