Exemple #1
0
def test_create_ssh_command(
        mocker, ec2, inst_name, use_inst_id, username, keyfile, port, ssh_options, use_private_ip,
        use_gateway, gateway_username, profile_name, expected):
    """create_ssh_command test"""
    from jungle.ec2 import create_ssh_command
    mocker.patch('boto3.Session', new=lambda profile_name: boto3)
    mocker.patch('click.prompt', new=lambda msg, type, default: 0)
    ssh_server_instance_id = ec2[
        'ssh_target_server'].id if use_inst_id else None
    gateway_server_instance_id = ec2[
        'gateway_target_server'].id if use_gateway else None
    ssh_command = create_ssh_command(
        create_session(profile_name),
        ssh_server_instance_id, inst_name, username, keyfile, port, ssh_options, use_private_ip,
        gateway_server_instance_id, gateway_username)
    if use_gateway:
        expected_output = expected.format(
            ec2['gateway_target_server'].public_ip_address,
            ec2['ssh_target_server'].private_ip_address)
    elif use_private_ip:
        expected_output = expected.format(
            ec2['ssh_target_server'].private_ip_address)
    else:
        expected_output = expected.format(
            ec2['ssh_target_server'].public_ip_address)
    assert ssh_command == expected_output
Exemple #2
0
def ssh(instance_id, instance_name, username, key_file, port, ssh_options,
        private_ip, gateway_instance_id, gateway_username, dry_run,
        profile_name):
    """SSH to EC2 instance"""
    session = create_session(profile_name)

    if instance_id is None and instance_name is None:
        click.echo(
            "One of --instance-id/-i or --instance-name/-n"
            " has to be specified.",
            err=True)
        sys.exit(1)
    elif instance_id is not None and instance_name is not None:
        click.echo(
            "Both --instance-id/-i and --instance-name/-n "
            "can't to be specified at the same time.",
            err=True)
        sys.exit(1)
    cmd = create_ssh_command(session, instance_id, instance_name, username,
                             key_file, port, ssh_options, private_ip,
                             gateway_instance_id, gateway_username)
    if not dry_run:
        subprocess.call(cmd, shell=True)
    else:
        click.echo(cmd)
Exemple #3
0
def test_create_ssh_command(mocker, ec2, inst_name, use_inst_id, username,
                            keyfile, port, ssh_options, use_private_ip,
                            use_gateway, gateway_username, profile_name,
                            expected):
    """create_ssh_command test"""
    from jungle.ec2 import create_ssh_command
    mocker.patch('boto3.Session', new=lambda profile_name: boto3)
    mocker.patch('click.prompt', new=lambda msg, type, default: 0)
    ssh_server_instance_id = ec2[
        'ssh_target_server'].id if use_inst_id else None
    gateway_server_instance_id = ec2[
        'gateway_target_server'].id if use_gateway else None
    ssh_command = create_ssh_command(create_session(profile_name),
                                     ssh_server_instance_id, inst_name,
                                     username, keyfile, port, ssh_options,
                                     use_private_ip,
                                     gateway_server_instance_id,
                                     gateway_username)
    if use_gateway:
        expected_output = expected.format(
            ec2['gateway_target_server'].public_ip_address,
            ec2['ssh_target_server'].private_ip_address)
    elif use_private_ip:
        expected_output = expected.format(
            ec2['ssh_target_server'].private_ip_address)
    else:
        expected_output = expected.format(
            ec2['ssh_target_server'].public_ip_address)
    assert ssh_command == expected_output
Exemple #4
0
def ls(list_formatted, profile_name):
    """List RDS instances"""
    session = create_session(profile_name)
    rds = session.client('rds')
    instances = rds.describe_db_instances()
    out = format_output(instances['DBInstances'], list_formatted)
    click.echo('\n'.join(out))
Exemple #5
0
def ls(ctx, list_formatted):
    """List RDS instances"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    rds = session.client('rds')
    instances = rds.describe_db_instances()
    out = format_output(instances['DBInstances'], list_formatted)
    click.echo('\n'.join(out))
Exemple #6
0
def ls(ctx, list_formatted):
    """List RDS instances"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    rds = session.client('rds')
    instances = rds.describe_db_instances()
    out = format_output(instances['DBInstances'], list_formatted)
    click.echo('\n'.join(out))
Exemple #7
0
def ls(name, profile_name):
    """List EMR instances"""
    session = create_session(profile_name)
    client = session.client('emr')
    results = client.list_clusters(
        ClusterStates=['RUNNING', 'STARTING', 'BOOTSTRAPPING', 'WAITING']
    )
    for cluster in results['Clusters']:
        click.echo("{0}\t{1}\t{2}".format(cluster['Id'], cluster['Name'], cluster['Status']['State']))
Exemple #8
0
def ls(ctx, name):
    """List EMR instances"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    client = session.client('emr')
    results = client.list_clusters(
        ClusterStates=['RUNNING', 'STARTING', 'BOOTSTRAPPING', 'WAITING']
    )
    for cluster in results['Clusters']:
        click.echo("{0}\t{1}\t{2}".format(cluster['Id'], cluster['Name'], cluster['Status']['State']))
Exemple #9
0
def ssh(cluster_id, key_file, profile_name):
    """SSH login to EMR master node"""
    session = create_session(profile_name)
    client = session.client('emr')
    result = client.describe_cluster(ClusterId=cluster_id)
    target_dns = result['Cluster']['MasterPublicDnsName']
    ssh_options = '-o StrictHostKeyChecking=no -o ServerAliveInterval=10'
    cmd = 'ssh {ssh_options}  -i {key_file} hadoop@{target_dns}'.format(
        ssh_options=ssh_options, key_file=key_file, target_dns=target_dns)
    subprocess.call(cmd, shell=True)
Exemple #10
0
def up(ctx, instance_id):
    """Start EC2 instance"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])
    ec2 = session.resource('ec2')
    try:
        instance = ec2.Instance(instance_id)
        instance.start()
    except botocore.exceptions.ClientError as e:
        click.echo("Invalid instance ID {0} ({1})".format(instance_id, e),
                   err=True)
        sys.exit(2)
Exemple #11
0
def ls(ctx, name, list_formatted):
    """List EC2 instances"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])
    ec2 = session.resource('ec2')
    if name == '*':
        instances = ec2.instances.filter()
    else:
        condition = {'Name': 'tag:Name', 'Values': [name]}
        instances = ec2.instances.filter(Filters=[condition])
    out = format_output(instances, list_formatted)
    click.echo('\n'.join(out))
Exemple #12
0
def ssh(ctx, cluster_id, key_file):
    """SSH login to EMR master node"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    client = session.client('emr')
    result = client.describe_cluster(ClusterId=cluster_id)
    target_dns = result['Cluster']['MasterPublicDnsName']
    ssh_options = '-o StrictHostKeyChecking=no -o ServerAliveInterval=10'
    cmd = 'ssh {ssh_options}  -i {key_file} hadoop@{target_dns}'.format(
        ssh_options=ssh_options, key_file=key_file, target_dns=target_dns)
    subprocess.call(cmd, shell=True)
Exemple #13
0
def down(instance_id, profile_name):
    """Stop EC2 instance"""
    session = create_session(profile_name)
    ec2 = session.resource('ec2')
    try:
        instance = ec2.Instance(instance_id)
        instance.stop()
    except botocore.exceptions.ClientError as e:
        click.echo("Invalid instance ID {0} ({1})".format(instance_id, e),
                   err=True)
        sys.exit(2)
Exemple #14
0
def ls(name, list_formatted, profile_name):
    """List EC2 instances"""
    session = create_session(profile_name)
    ec2 = session.resource('ec2')
    if name == '*':
        instances = ec2.instances.filter()
    else:
        condition = {'Name': 'tag:Name', 'Values': [name]}
        instances = ec2.instances.filter(Filters=[condition])
    out = format_output(instances, list_formatted)
    click.echo('\n'.join(out))
Exemple #15
0
def ls(name, list_formatted, profile_name):
    """List AutoScaling groups"""
    session = create_session(profile_name)
    client = session.client('autoscaling')
    if name == "*":
        groups = client.describe_auto_scaling_groups()
    else:
        groups = client.describe_auto_scaling_groups(AutoScalingGroupNames=[
            name,
        ])
    out = format_output(groups, list_formatted)
    click.echo('\n'.join(out))
Exemple #16
0
def ls(ctx, name, list_formatted):
    """List AutoScaling groups"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    client = session.client('autoscaling')
    if name == "*":
        groups = client.describe_auto_scaling_groups()
    else:
        groups = client.describe_auto_scaling_groups(AutoScalingGroupNames=[
            name,
        ])
    out = format_output(groups, list_formatted)
    click.echo('\n'.join(out))
Exemple #17
0
def rm(cluster_id, profile_name):
    """Terminate a EMR cluster"""
    session = create_session(profile_name)
    client = session.client('emr')
    try:
        result = client.describe_cluster(ClusterId=cluster_id)
        target_dns = result['Cluster']['MasterPublicDnsName']
        flag = click.prompt(
            "Are you sure you want to terminate {0}: {1}? [y/Y]".format(
                cluster_id, target_dns), type=str, default='n')
        if flag.lower() == 'y':
            result = client.terminate_job_flows(JobFlowIds=[cluster_id])
    except ClientError as e:
        click.echo(e, err=True)
Exemple #18
0
def rm(ctx, cluster_id):
    """Terminate a EMR cluster"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    client = session.client('emr')
    try:
        result = client.describe_cluster(ClusterId=cluster_id)
        target_dns = result['Cluster']['MasterPublicDnsName']
        flag = click.prompt(
            "Are you sure you want to terminate {0}: {1}? [y/Y]".format(
                cluster_id, target_dns), type=str, default='n')
        if flag.lower() == 'y':
            result = client.terminate_job_flows(JobFlowIds=[cluster_id])
    except ClientError as e:
        click.echo(e, err=True)
Exemple #19
0
def ls(ctx, name, list_formatted):
    """List AutoScaling groups"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    client = session.client('autoscaling')
    if name == "*":
        groups = client.describe_auto_scaling_groups()
    else:
        groups = client.describe_auto_scaling_groups(
            AutoScalingGroupNames=[
                name,
            ]
        )
    out = format_output(groups, list_formatted)
    click.echo('\n'.join(out))
Exemple #20
0
def ls(ctx, name, list_instances):
    """List ELB instances"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])

    client = session.client('elb')
    inst = {'LoadBalancerDescriptions': []}
    if name == '*':
        inst = client.describe_load_balancers()
    else:
        try:
            inst = client.describe_load_balancers(LoadBalancerNames=[name])
        except ClientError as e:
            click.echo(e, err=True)

    for i in inst['LoadBalancerDescriptions']:
        click.echo(i['LoadBalancerName'])
        if list_instances:
            for ec2 in i['Instances']:
                health = client.describe_instance_health(
                    LoadBalancerName=name,
                    Instances=[ec2]
                )
                click.echo('{0}\t{1}'.format(ec2['InstanceId'], health['InstanceStates'][0]['State']))