Beispiel #1
0
def _find_address_by_device_id(ec2, device_id, isinstance=True):
    if isinstance:
        addresses = ec2.get_all_addresses(None, {"instance-id": device_id})
    else:
        addresses = ec2.get_all_addresses(None, {"network-interface-id": device_id})
    if addresses:
        return addresses[0]
Beispiel #2
0
def _find_address_by_device_id(ec2, device_id, isinstance=True):
    if isinstance:
        addresses = ec2.get_all_addresses(None, {'instance-id': device_id})
    else:
        addresses = ec2.get_all_addresses(None, {'network-interface-id': device_id})
    if addresses:
        return addresses[0]
def dis_eip_associate(public_ip):
    ec2 = boto.ec2.connect_to_region(region)
    address = ec2.get_all_addresses(addresses='5.2.29.7')
    association_id = address[0].association_id
    ec2.disassociate_address(public_ip=public_ip,
                             association_id=association_id,
                             dry_run=False)
def eip_allocid(ip,region=None):
  ec2 = GetRegion()
  regions = ec2.get_regions() 

  if not ip:
      raise errors.AnsibleError('An ip address is required.')

  m = re.search('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', ip)
  if not m:
      raise errors.AnsibleError('"%s" is not a valid ip address.' % ip)

  if region:
    region = region
  else:
    if 'AWS_REGION' in os.environ:
      region = os.environ['AWS_REGION']
    else:
      raise errors.AnsibleError('aws region not found in argument or AWS_REGION env var')

  if not region in regions:
    raise errors.AnsibleError('%s is not a valid aws region' % aws_region)

  ec2 = boto.ec2.connect_to_region(region)

  try:
    eip_allocid = ec2.get_all_addresses(filters={'public-ip': ip})[0].allocation_id
  except Exception, e:
    raise errors.AnsibleError('Couldn\'t retrieve eip allocation id. Error was %s' % str(e))
Beispiel #5
0
def eip_allocid(ip, region=None):
    ec2 = GetRegion()
    regions = ec2.get_regions()

    if not ip:
        raise errors.AnsibleError('An ip address is required.')

    m = re.search('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', ip)
    if not m:
        raise errors.AnsibleError('"%s" is not a valid ip address.' % ip)

    if region:
        region = region
    else:
        if 'AWS_REGION' in os.environ:
            region = os.environ['AWS_REGION']
        else:
            raise errors.AnsibleError(
                'aws region not found in argument or AWS_REGION env var')

    if not region in regions:
        raise errors.AnsibleError('%s is not a valid aws region' % aws_region)

    ec2 = boto.ec2.connect_to_region(region)

    try:
        eip_allocid = ec2.get_all_addresses(
            filters={'public-ip': ip})[0].allocation_id
    except Exception, e:
        raise errors.AnsibleError(
            'Couldn\'t retrieve eip allocation id. Error was %s' % str(e))
def get_allocation_id(ip):
    allocation_id = None
    resp = ec2.get_all_addresses()
    for addr in resp:
        if addr.public_ip == ip:
            allocation_id = addr.allocation_id
            break
    return allocation_id
Beispiel #7
0
def address_is_associated_with_device(ec2, address, device_id, isinstance=True):
    """ Check if the elastic IP is currently associated with the device """
    address = ec2.get_all_addresses(address.public_ip)
    if address:
        if isinstance:
            return address and address[0].instance_id == device_id
        else:
            return address and address[0].network_interface_id == device_id
    return False
Beispiel #8
0
def address_is_associated_with_device(ec2, address, device_id, isinstance=True):
    """ Check if the elastic IP is currently associated with the device """
    address = ec2.get_all_addresses(address.public_ip)
    if address:
        if isinstance:
            return address and address[0].instance_id == device_id
        else:
            return address and address[0].network_interface_id == device_id
    return False
def release_ip(public_ip):
    ec2 = boto.ec2.connect_to_region(region)
    address = ec2.get_all_addresses(addresses=[public_ip])
    association_id = address[0].association_id
    allocation_id = address[0].allocation_id
    if association_id == None:
        #ec2.release_address(public_ip=public_ip, allocation_id=allocation_id, dry_run=False)
        ec2.release_address(public_ip=None, allocation_id=allocation_id)
    else:
        print 'The %s ip is be used' % public_ip
def isassociated(domain):
    public_ip = gethostbyname(domain)
    instance_id = requests.get("http://169.254.169.254/latest/meta-data/instance-id").content
    try:
      addresses = ec2.get_all_addresses(addresses=[public_ip, ],
                                        filters={"instance-id": instance_id})
    except EC2ResponseError:
      return False
    else:
      return len(addresses) > 0
def release_ip(public_ip):
    ec2 = boto.ec2.connect_to_region(region)
    address = ec2.get_all_addresses(addresses=[public_ip])
    association_id = address[0].association_id
    allocation_id = address[0].allocation_id
    if association_id == None:
        # ec2.release_address(public_ip=public_ip, allocation_id=allocation_id, dry_run=False)
        ec2.release_address(public_ip=None, allocation_id=allocation_id)
    else:
        print "The %s ip is be used" % public_ip
def allocate_address(ec2, domain, reuse_existing_ip_allowed):
    """ Allocate a new elastic IP address (when needed) and return it """
    if reuse_existing_ip_allowed:
        domain_filter = {'domain': domain or 'standard'}
        all_addresses = ec2.get_all_addresses(filters=domain_filter)

        unassociated_addresses = [a for a in all_addresses if not a.device_id]
        if unassociated_addresses:
            return unassociated_addresses[0]

    return ec2.allocate_address(domain=domain)
Beispiel #13
0
def allocate_address(ec2, domain, reuse_existing_ip_allowed):
    """ Allocate a new elastic IP address (when needed) and return it """
    if reuse_existing_ip_allowed:
        domain_filter = {"domain": domain or "standard"}
        all_addresses = ec2.get_all_addresses(filters=domain_filter)

        unassociated_addresses = [a for a in all_addresses if not a.device_id]
        if unassociated_addresses:
            return unassociated_addresses[0]

    return ec2.allocate_address(domain=domain)
Beispiel #14
0
def ec2_release_eip(ec2):
	addresses = ec2.get_all_addresses()
	for a in addresses:
		try:
			if a.domain == 'standard': 
				print "---Releasing EIP: " + a.public_ip
				ec2.release_address(public_ip=a.public_ip)
			elif a.domain == 'vpc':
				print "---Releasing EIP: " + a.public_ip
				ec2.release_address(allocation_id=a.allocation_id) 
		except Exception, e:
			print(e)
def isassociated(domain):
    public_ip = gethostbyname(domain)
    instance_id = requests.get(
        "http://169.254.169.254/latest/meta-data/instance-id").content
    try:
        addresses = ec2.get_all_addresses(addresses=[
            public_ip,
        ],
                                          filters={"instance-id": instance_id})
    except EC2ResponseError:
        return False
    else:
        return len(addresses) > 0
Beispiel #16
0
def allocate_address(ec2, domain, reuse_existing_ip_allowed):
    """ Allocate a new elastic IP address (when needed) and return it """
    if reuse_existing_ip_allowed:
        domain_filter = {'domain': domain or 'standard'}
        all_addresses = ec2.get_all_addresses(filters=domain_filter)

        if domain == 'vpc':
            unassociated_addresses = [a for a in all_addresses
                                      if not a.association_id]
        else:
            unassociated_addresses = [a for a in all_addresses
                                      if not a.instance_id]
        if unassociated_addresses:
            return unassociated_addresses[0]

    return ec2.allocate_address(domain=domain)
Beispiel #17
0
def find_address(ec2, public_ip, module):
    """ Find an existing Elastic IP address """
    if wait_timeout != 0:
        timeout = time.time() + wait_timeout
        while timeout > time.time():
            try:
                addresses = ec2.get_all_addresses([public_ip])
                break
            except boto.exception.EC2ResponseError, e:
                if "Address '%s' not found." % public_ip in e.message:
                    pass
                else:
                    module.fail_json(msg=str(e.message))
            time.sleep(5)

        if timeout <= time.time():
            module.fail_json(msg="wait for EIPs timeout on %s" % time.asctime())
Beispiel #18
0
def allocate_address(ec2, domain, reuse_existing_ip_allowed):
    """ Allocate a new elastic IP address (when needed) and return it """
    if reuse_existing_ip_allowed:
        domain_filter = {'domain': domain or 'standard'}
        all_addresses = ec2.get_all_addresses(filters=domain_filter)

        if domain == 'vpc':
            unassociated_addresses = [
                a for a in all_addresses if not a.association_id
            ]
        else:
            unassociated_addresses = [
                a for a in all_addresses if not a.instance_id
            ]
        if unassociated_addresses:
            return unassociated_addresses[0], False

    return ec2.allocate_address(domain=domain), True
Beispiel #19
0
def find_address(ec2, public_ip, module):
    """ Find an existing Elastic IP address """
    if wait_timeout != 0:
        timeout = time.time() + wait_timeout
        while timeout > time.time():
            try:
                addresses = ec2.get_all_addresses([public_ip])
                break
            except boto.exception.EC2ResponseError, e:
                if "Address '%s' not found." % public_ip in e.message:
                    pass
                else:
                    module.fail_json(msg=str(e.message))
            time.sleep(5)

        if timeout <= time.time():
            module.fail_json(msg="wait for EIPs timeout on %s" %
                             time.asctime())
Beispiel #20
0
def allocate_address(ec2, domain, module, reuse_existing_ip_allowed):
    """ Allocate a new elastic IP address (when needed) and return it """
    # If we're in check mode, nothing else to do
    if module.check_mode:
        module.exit_json(change=True)

    if reuse_existing_ip_allowed:
      if domain:
        domain_filter = { 'domain' : domain }
      else:
        domain_filter = { 'domain' : 'standard' }
      all_addresses = ec2.get_all_addresses(filters=domain_filter)

      unassociated_addresses = filter(lambda a: not a.instance_id, all_addresses)
      if unassociated_addresses:
        address = unassociated_addresses[0];
      else:
        address = ec2.allocate_address(domain=domain)
    else:
      address = ec2.allocate_address(domain=domain)

    return address
Beispiel #21
0
def allocate_address(ec2, domain, module, reuse_existing_ip_allowed):
    """ Allocate a new elastic IP address (when needed) and return it """
    # If we're in check mode, nothing else to do
    if module.check_mode:
        module.exit_json(change=True)

    if reuse_existing_ip_allowed:
      if domain:
        domain_filter = { 'domain' : domain }
      else:
        domain_filter = { 'domain' : 'standard' }
      all_addresses = ec2.get_all_addresses(filters=domain_filter)

      unassociated_addresses = filter(lambda a: not a.instance_id, all_addresses)
      if unassociated_addresses:
        address = unassociated_addresses[0];
      else:
        address = ec2.allocate_address(domain=domain)
    else:
      address = ec2.allocate_address(domain=domain)

    return address
def eip_associate(instanceid):
    ec2 = boto.ec2.connect_to_region(region)
    address = ec2.get_all_addresses(addresses="5.2.29.7")
    ec2.associate_address(instanceid, address[0].public_ip)
                addresses = ec2.get_all_addresses([public_ip])
                break
            except boto.exception.EC2ResponseError, e:
                if "Address '%s' not found." % public_ip in e.message:
                    if not fail_on_not_found:
                        return None
                else:
                    module.fail_json(msg=str(e.message))
            time.sleep(5)

        if timeout <= time.time():
            module.fail_json(msg="wait for EIPs timeout on %s" %
                             time.asctime())
    else:
        try:
            addresses = ec2.get_all_addresses([public_ip])
        except boto.exception.EC2ResponseError, e:
            if "Address '%s' not found." % public_ip in e.message:
                if not fail_on_not_found:
                    return None
            module.fail_json(msg=str(e.message))

    return addresses[0]


def ip_is_associated_with_instance(ec2, public_ip, instance_id, module):
    """ Check if the elastic IP is currently associated with the instance """
    address = find_address(ec2, public_ip, module)
    if address:
        return address.instance_id == instance_id
    else:
def eip_associate(instanceid):
    ec2 = boto.ec2.connect_to_region(region)
    address = ec2.get_all_addresses(addresses='5.2.29.7')
    ec2.associate_address(instanceid, address[0].public_ip)
Beispiel #25
0
            try:
                addresses = ec2.get_all_addresses([public_ip])
                break
            except boto.exception.EC2ResponseError, e:
                if "Address '%s' not found." % public_ip in e.message :
                    if not fail_on_not_found:
                        return None
                else:
                    module.fail_json(msg=str(e.message))
            time.sleep(5)
        
        if timeout <= time.time():
            module.fail_json(msg = "wait for EIPs timeout on %s" % time.asctime())    
    else:
        try:
            addresses = ec2.get_all_addresses([public_ip])
        except boto.exception.EC2ResponseError, e:
            if "Address '%s' not found." % public_ip in e.message :
                if not fail_on_not_found:
                    return None
            module.fail_json(msg=str(e.message))

    return addresses[0]


def ip_is_associated_with_instance(ec2, public_ip, instance_id, module):
    """ Check if the elastic IP is currently associated with the instance """
    address = find_address(ec2, public_ip, module)
    if address:
        return address.instance_id == instance_id
    else:
Beispiel #26
0
def _find_address_by_ip(ec2, public_ip):
    try:
        return ec2.get_all_addresses([public_ip])[0]
    except boto.exception.EC2ResponseError as e:
        if "Address '{}' not found.".format(public_ip) not in e.message:
            raise
def _find_address_by_ip(ec2, public_ip):
    try:
        return ec2.get_all_addresses([public_ip])[0]
    except boto.exception.EC2ResponseError as e:
        if "Address '{}' not found.".format(public_ip) not in e.message:
            raise
def domain2localip(domain):
    public_ip = gethostbyname(domain)
    eip = ec2.get_all_addresses(addresses=[public_ip, ])[0]
    return eip.private_ip_address
def _find_address_by_instance_id(ec2, instance_id):
    addresses = ec2.get_all_addresses(None, {'instance-id': instance_id})
    if addresses:
        return addresses[0]
def dis_eip_associate(public_ip):
    ec2 = boto.ec2.connect_to_region(region)
    address = ec2.get_all_addresses(addresses="5.2.29.7")
    association_id = address[0].association_id
    ec2.disassociate_address(public_ip=public_ip, association_id=association_id, dry_run=False)
Beispiel #31
0
def _find_address_by_instance_id(ec2, instance_id):
    addresses = ec2.get_all_addresses(None, {'instance-id': instance_id})
    if addresses:
        return addresses[0]
def domain2localip(domain):
    public_ip = gethostbyname(domain)
    eip = ec2.get_all_addresses(addresses=[
        public_ip,
    ])[0]
    return eip.private_ip_address