Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This change ads sudo to group calls #2052

Closed
wants to merge 1 commit into from

Conversation

WinstonN
Copy link

This PR was created from the discussion here: #1912 (comment)

I created this PR in the hope that we could get some traction into bringing this feature into Fabric 2
Sudo calls in group still suffers from this issue: #2051

Code used for testing

def get_config():
    """
    Set correct config for connections
    """
    config = Config(
        runtime_ssh_path='/path/to/.ssh/config.d/unicorn',
        overrides={
            'sudo': {
                'user': 'www'
            }
        }
    )
    return config


def setup_host_group(context, environment, filter, tags='private_ip_address'):
    """
    Setup hosts group from EC2 instances using boto3
    """
    hosts = extract_instances_information(context, environment, filter, tags)
    config = get_config()
    connection = [Connection(host=host, config=config) for host in hosts]

    return connection


@task
def exec(context, environment, filter, command):
    """
    Execute command on systems
    :param environment: The environment to target (Staging, Development, Production)
    :param filter: The filter wildcard string to run the commands on some hosts
    :param command: The command to execute
    """
    # run way (without sudo in ThreadingGroup)
    # hosts = extract_instances_information(context, environment, filter, tags='private_ip_address')
    # config = get_config()

    # for host in hosts:
    #     c = Connection(host=host, config=config)
    #     c.sudo(command)

    connection = setup_host_group(context, environment, filter)
    group = ThreadingGroup.from_connections(connection)
    
    try:
        group.sudo(command)
    except exceptions.GroupException as e:
        pass

Output is:

❯❯❯ fab exec -e stg -f "Web" -c "whoami; id; pwd;"
---
Your current IP Address is:
ip.ad.dre.ss
---
Targeting environment [stg] with filter 'Web'
Information for attributes: ['private_ip_address']
Found 2 host(s)
[
    "10.0.0.1",
    "10.0.0.2"
]
www
uid=500(ec2-user) gid=500(ec2-user) groups=500(ec2-user),10(wheel)
/home/ec2-user
www
uid=500(ec2-user) gid=500(ec2-user) groups=500(ec2-user),10(wheel)
/home/ec2-user

@akiuni
Copy link

akiuni commented Jan 29, 2020

Hello, I'm not used to git developpemen (PR is a strange word for me), feel free to tell me if I can help

@bitprophet
Copy link
Member

bitprophet commented Dec 31, 2020

Went with a different approach on my end that's a bit less copy/paste, but thanks anyway! Crediting in changelog :) See #1999 for the core ticket on this as well.

@bitprophet bitprophet closed this Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants