Skip to content Skip to sidebar Skip to footer

How To Combine Boto With Fabric

I need to mention. I use Windows. Now I know how to use boto. But I faced the problem that I can't run 'sudo' based on boto. status, stdout, stderr = ssh_client.run('sudo python k

Solution 1:

I disagree with two things you're doing. One: sudo python... No. Make that run as www-data or equivalent. Also, use supervisord and not what you're currently doing.

Shouldn't matter if you're on windows or not.. You're telling me this doesn't work for you?

fabfile.py:

import boto.ec2
from fabric.api import env, run, sudo, task

env.key_filename = "/PATH/TO/SSH/FILE.pem"
env.user = "ubuntu"@taskdefamazon(**kwargs):
    conn = boto.ec2.connect_to_region(
        'us-east-1',
        aws_access_key_id="*********",
        aws_secret_access_key="**************"
    )

    hosts = []
    for reservation in conn.get_all_instances():
        for instance in reservation.instances:
            # if filters were appliedif kwargs:
                skip_instance = Falsefor key, value in kwargs.items():
                    instance_value = getattr(instance, key)

                    # makes sure that `group` is handeledifisinstance(instance_value, list):
                        new_value = []
                        for item in instance_value:
                            ifisinstance(item, boto.ec2.group.Group):
                                new_value.append(item.name)
                            else:
                                new_value.append(item)
                        instance_value = new_value

                        if value notin instance_value:
                            skip_instance = Truebreakelse:
                        # every other single value gets handeled hereif instance_value != value:
                            skip_instance = Truebreakif skip_instance:
                    continueif instance.dns_name:
                hosts.append(instance.dns_name)
            elif instance.ip_address:
                hosts.append(instance.ip_address)

    env.hosts = hosts


@taskdefwhoami():
    run('whoami')
    sudo('whoami')

I added filters for you, just in case, you can run it as:

fab amazon whoami -- it will go through every single server in amazon and connect and run whoami commands.

fab amazon:ip_address=<IP OF AN INSTANCE YOU KNOW OF> whoami -- will only use the box whos ip matched on the filter. (It should work for every field in the instance in boto)

that one is just a gimmick, groups is the one "I" would use:

fab amazon:groups=<GROUP NAME FROM AMAZON> whoami -- will run whoami on all servers that matched said group name.

proof:

$ fab amazon:dns_name=******* whoami
[*******] Executing task 'whoami'
[*******] run: whoami
[*******] out: ubuntu
[*******] out: 

[*******] sudo: whoami
[*******] out: root
[*******] out: 


Done.
Disconnecting from *******... done.

and

$ fab amazon:groups=webservers whoami
[***1***] Executing task 'whoami'
[***1***] run: whoami
[***1***] out: ubuntu
[***1***] out: 

[***1***] sudo: whoami
[***1***] out: root
[***1***] out: 

... truncated...

[***4***] Executing task 'whoami'
[***4***] run: whoami
[***4***] out: ubuntu
[***4***] out: 

[***4***] sudo: whoami
[***4***] out: root
[***4***] out: 


Done.
Disconnecting from***1***... done.
Disconnecting from***2***... done.
Disconnecting from***3***... done.
Disconnecting from***4***... done.
Disconnecting from***5***... done.
Disconnecting from***6***... done.
Disconnecting from***7***... done.

Post a Comment for "How To Combine Boto With Fabric"