Example #1
0
def find_device(ec2, module, device_id, isinstance=True):
    """ Attempt to find the EC2 instance and return it """

    if isinstance:
        try:
            reservations = ec2.get_all_reservations(instance_ids=[device_id])
        except boto.exception.EC2ResponseError as e:
            module.fail_json(msg=str(e))

        if len(reservations) == 1:
            instances = reservations[0].instances
            if len(instances) == 1:
                return instances[0]
    else:
        try:
            interfaces = ec2.get_all_network_interfaces(network_interface_ids=[device_id])
        except boto.exception.EC2ResponseError as e:
            module.fail_json(msg=str(e))

        if len(interfaces) == 1:
            return interfaces[0]

    raise EIPException("could not find instance" + device_id)
Example #2
0
def find_device(ec2, device_id, isinstance=True):
    """ Attempt to find the EC2 instance and return it """

    if isinstance:
        try:
            reservations = ec2.get_all_reservations(instance_ids=[device_id])
        except boto.exception.EC2ResponseError, e:
            module.fail_json(msg=str(e))

        if len(reservations) == 1:
            instances = reservations[0].instances
            if len(instances) == 1:
                return instances[0]
    else:
        try:
            interfaces = ec2.get_all_network_interfaces(network_interface_ids=[device_id])
        except boto.exception.EC2ResponseError, e:
            module.fail_json(msg=str(e))

        if len(interfaces) == 1:
            return interfaces[0]

    raise EIPException("could not find instance" + device_id)


def ensure_present(ec2, domain, address, device_id, reuse_existing_ip_allowed, check_mode, isinstance=True):
    changed = False

    # Return the EIP object since we've been given a public IP
    if not address:
        if check_mode:
Example #3
0
def find_device(ec2, device_id, isinstance=True):
    """ Attempt to find the EC2 instance and return it """

    if isinstance:
        try:
            reservations = ec2.get_all_reservations(instance_ids=[device_id])
        except boto.exception.EC2ResponseError, e:
            module.fail_json(msg=str(e))

        if len(reservations) == 1:
            instances = reservations[0].instances
            if len(instances) == 1:
                return instances[0]
    else:
        try:
            interfaces = ec2.get_all_network_interfaces(
                network_interface_ids=[device_id])
        except boto.exception.EC2ResponseError, e:
            module.fail_json(msg=str(e))

        if len(interfaces) == 1:
            return interfaces[0]

    raise EIPException("could not find instance" + device_id)


def ensure_present(ec2,
                   domain,
                   address,
                   device_id,
                   reuse_existing_ip_allowed,
                   check_mode,
Example #4
0
def find_eni(ec2, eni_id, module):
    try:
        enis = ec2.get_all_network_interfaces(network_interface_ids=[eni_id])
    except boto.exception.EC2ResponseError, e:
        module.fail_json(msg=str(e))
def assign_new_private_ip(ec2, module, eni_id, private_address_count):
    # retrieve Elastic Network Interface
    try:
        enis = ec2.get_all_network_interfaces(network_interface_ids=[eni_id])
    except boto.exception.EC2ResponseError, e:
        module.fail_json(msg=str(e))
    HAS_BOTO = False


def assign_new_private_ip(ec2, module, eni_id, private_address_count):
    # retrieve Elastic Network Interface
    try:
        enis = ec2.get_all_network_interfaces(network_interface_ids=[eni_id])
    except boto.exception.EC2ResponseError, e:
        module.fail_json(msg=str(e))

    if not enis:
        module.fail_json(msg="Couldn't find Elastic Network Interface {}".format(eni_id))

    try:
        if ec2.assign_private_ip_addresses(network_interface_id=eni_id, secondary_private_ip_address_count=private_address_count):
            enis_after = ec2.get_all_network_interfaces(network_interface_ids=[eni_id])
            eni = enis_after[0]
            new_ips = eni.private_ip_addresses[-private_address_count:]
            new_ips_info = [get_private_address_info(addr) for addr in new_ips]
            module.exit_json(changed=True, count=private_address_count, private_ip_addresses=new_ips_info)
        else:
            module.exit_json(changed=False)
    except boto.exception.EC2ResponseError, e:
        module.fail_json(msg=str(e))


def get_private_address_info(private_ip_address):
    private_address_info = {
        "private_ip_address": private_ip_address.private_ip_address,
        "primary": private_ip_address.primary if hasattr(private_ip_address, 'primary') else False
    }
elb = boto.ec2.elb.connect_to_region(args.region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
load_balancers = elb.get_all_load_balancers()
for load_balancer in load_balancers:
    if load_balancer.source_security_group.name not in groups_in_use:
        groups_in_use.append(load_balancer.source_security_group.name)

rds = boto.rds.connect_to_region(args.region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
dbs = rds.get_all_dbinstances()
for db in dbs:
    if len(db.vpc_security_groups) > 0:
        sg_name = lookup_by_id(db.vpc_security_groups[0].vpc_group)
        if sg_name not in groups_in_use:
            groups_in_use.append(sg_name)

enis = ec2.get_all_network_interfaces()
for eni in enis:
    for eni_grp in eni.groups:
        if eni_grp.name not in groups_in_use:
            groups_in_use.append(eni_grp.name)

delete_candidates = []
for group in allgroups:
    if group not in groups_in_use and not group.startswith('AWS-OpsWorks-'):
        delete_candidates.append(group)

if args.delete:
    print "We will now delete security groups identified to not be in use."
    for group in delete_candidates:
        ec2.delete_security_group(group)
else:
Example #8
0
def get_all_network_interfaces(ec2, filters):
    return ec2.get_all_network_interfaces(filters=filters)
elb = boto.ec2.elb.connect_to_region(args.region)
load_balancers = elb.get_all_load_balancers()
for load_balancer in load_balancers:
    if load_balancer.source_security_group.name not in groups_in_use:
        groups_in_use.append(load_balancer.source_security_group.name)

rds = boto.rds.connect_to_region(args.region)
dbs = rds.get_all_dbinstances()
for db in dbs:
    if len(db.vpc_security_groups) > 0:
        sg_name = lookup_by_id(db.vpc_security_groups[0].vpc_group)
        if sg_name not in groups_in_use:
            groups_in_use.append(sg_name)

enis = ec2.get_all_network_interfaces()
for eni in enis:
    for eni_grp in eni.groups:
      if eni_grp.name not in groups_in_use:
        groups_in_use.append(eni_grp.name)

delete_candidates = []
for group in allgroups:
    if group not in groups_in_use and not group.startswith('AWS-OpsWorks-'):
        delete_candidates.append(group)

if args.delete:
    print "We will now delete security groups identified to not be in use."
    for group in delete_candidates:
        ec2.delete_security_group(group)
else: