Paul Walsh
2013-12-19 06:44:15 UTC
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.
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.