Skip to content

fabric.api.env.roles not updated when using @roles #938

@exhuma

Description

@exhuma

See also the following SO answer: http://stackoverflow.com/a/17724207/160665

When using the @roles decorator, fabric.api.env.roles does not reflect this choice. This could cause issues when running a nested task which uses env.roles. A practical example from my case (leaving out the non-essential stuff):

import fabric.api as fab

fab.env.roledefs.update({
    'prod': '1.2.3.4',
    'staging': '2.3.4.5'
})

@fab.task
@fab.roles('staging')
def stage():
    # ... snip ...
    fab.execute(deploy)
    # ... snip ...


@fab.task
@fab.roles('prod')
def prod():
    # ... snip ...
    fab.execute(deploy)
    # ... snip ...


@fab.task
def deploy():
    print fab.env.roles
    if 'prod' in fab.env.roles:
        apache_vhostname = 'migrate.tld.foo.bar'
    elif 'staging' in fab.env.roles:
        apache_vhostname = 'migrate-rc.tld.foo.bar'
    else:
        raise ValueError('Can only deploy on roles "prod" and "staging". '
                        'You told me to deploy on {0!r}'.format(fab.env.roles))
    # ... snip ...

As illustrative purpose, running this fabfile gives me:

/tmp› fab prod
[1] Executing task 'prod'
[]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 736, in main
    *args, **kwargs
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 314, in execute
    multiprocessing
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 211, in _execute
    return task.run(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 121, in run
    return self.wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/fabric/decorators.py", line 51, in inner_decorator
    return func(*args, **kwargs)
File "/tmp/fabfile.py", line 20, in prod
    fab.execute(deploy)
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 347, in execute
    results['<local-only>'] = task.run(*args, **new_kwargs)
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 121, in run
    return self.wrapped(*args, **kwargs)
File "/tmp/fabfile.py", line 33, in deploy
    'You told me to deploy on {0!r}'.format(fab.env.roles))
ValueError: Can only deploy on roles "prod" and "staging". You told me to deploy on []

But I expected:

/tmp› fab prod
[1] Executing task 'prod'
['prod']

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions