Discussion:
[Fab-user] Providing a fallback for roles at runtime
Paul Walsh
2013-12-19 06:44:15 UTC
Permalink
I have a setup like this:

env.roledefs = {'default': ['1.2.3.4'], 'db': ['5.6.7.8']},
env.roles: ['default', 'db']
# other custom keys on env

@roles('db')
@task
def drop():
utilities.alert(u'Dropping the database.')
run('dropdb {name}'.format(name=env.db_name))


This is fine: the task only executes for the 'db' role, as expected.

But, this is part of a larger set of tasks that I have. And, what I actually want to do is say:

"execute this task on db if db is in roles, otherwise, execute on default."

So, to achieve that, I tried:

import utilities

@roles(utilities.get_role('db'))
@task
def drop():
utilities.alert(u'Dropping the database.')
run('dropdb {name}'.format(name=env.db_name))


# utilities.py
def get_role(target_role):
if target_role in env.roles:
return target_role
else:
return 'default'


However, the task still attempts to run on roles['default']. I'm diving in but I'm also wondering if there is another way I could, in the fabfile itself, dynamically set the role for a task like this.
Michael Gliwinski
2013-12-19 11:33:25 UTC
Permalink
On Thursday 19 Dec 2013 08:44:15 Paul Walsh wrote:
...
Post by Paul Walsh
"execute this task on db if db is in roles, otherwise, execute on default."
import utilities
@roles(utilities.get_role('db'))
@task
utilities.alert(u'Dropping the database.')
run('dropdb {name}'.format(name=env.db_name))
# utilities.py
return target_role
return 'default'
However, the task still attempts to run on roles['default']. I'm diving in
but I'm also wondering if there is another way I could, in the fabfile
itself, dynamically set the role for a task like this.
I'm not sure how you set env.roles, but note that utilities.get_role will be
executed when your fabfile is loaded.

Roles can actually be callables, so you could do something like:

def db_or_default():
if ...: return ['5.6.7.8']
return env.roledefs['default']

env.roledefs = {'default': ['1.2.3.4'], 'db': db_or_default}


**********************************************************************************************
The information in this email is confidential and may be legally privileged. It is intended solely for the addressee and access to the email by anyone else is unauthorised.
If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful.
When addressed to our clients, any opinions or advice contained in this e-mail are subject to the terms and conditions expressed in the governing client engagement leter or contract.
If you have received this email in error please notify ***@henderson-group.com

John Henderson (Holdings) Ltd
Registered office: 9 Hightown Avenue, Mallusk, County Antrim, Northern Ireland, BT36 4RT.
Registered in Northern Ireland
Registration Number NI010588
Vat No.: 814 6399 12
*********************************************************************************
Loading...