tags:

views:

39

answers:

1

Hi

This is a very Fabric specific question, but more experienced python hackers might be able to answer this, even if they don't know Fabric.

I am trying to specify different behaviour in a command depending on which role it is running for, i.e.:

def restart():
    if (SERVERTYPE == "APACHE"):
        sudo("apache2ctl graceful",pty=True)
    elif (SERVERTYPE == "APE"):
        sudo("supervisorctl reload",pty=True)

I was hacking this with functions like this one:

def apache():
    global SERVERTYPE
    SERVERTYPE = "APACHE"
    env.hosts = ['xxx.xxx.com']

But that is obviously not very elegant and I just discovered roles, so my question is:

How do I figure out which role a current instance belongs to?

env.roledefs = {
    'apache': ['xxx.xxx.com'],
    'APE': ['yyy.xxx.com'],
}

Thanks!

A: 

For everyone else ever with this question, here is my solution:

The key was finding env.host_string.

This is how I restart different types of servers with one command:

env.roledefs = {
    'apache': ['xxx.xxx.com'],
    'APE': ['yyy.xxx.com']
}

def apache():
    env.roles = ['apache']

...

def restart():
    if env.host_string in env.roledefs['apache']:
        sudo("apache2ctl graceful", pty=True)
    elif env.host_string in env.roledefs['APE']:
        sudo ("supervisorctl reload", pty=True)

enjoy!

rdrey