Search in sources :

Example 46 with Instance

use of com.amazonaws.services.ec2.model.Instance in project SimianArmy by Netflix.

the class TestInstanceJanitorCrawler method testInstancesWithIds.

@Test
public void testInstancesWithIds() {
    List<AutoScalingInstanceDetails> instanceDetailsList = createInstanceDetailsList();
    List<Instance> instanceList = createInstanceList();
    String[] ids = { "i-12345678901234560", "i-12345678901234561" };
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList, ids);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    List<Resource> resources = crawler.resources(ids);
    verifyInstanceList(resources, instanceDetailsList);
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Test(org.testng.annotations.Test)

Example 47 with Instance

use of com.amazonaws.services.ec2.model.Instance in project SimianArmy by Netflix.

the class AWSClient method listAttachedVolumes.

@Override
public List<String> listAttachedVolumes(String instanceId, boolean includeRoot) {
    Validate.notEmpty(instanceId);
    LOGGER.info(String.format("Listing volumes attached to instance %s in region %s.", instanceId, region));
    try {
        List<String> volumeIds = new ArrayList<String>();
        for (Instance instance : describeInstances(instanceId)) {
            String rootDeviceName = instance.getRootDeviceName();
            for (InstanceBlockDeviceMapping ibdm : instance.getBlockDeviceMappings()) {
                EbsInstanceBlockDevice ebs = ibdm.getEbs();
                if (ebs == null) {
                    continue;
                }
                String volumeId = ebs.getVolumeId();
                if (Strings.isNullOrEmpty(volumeId)) {
                    continue;
                }
                if (!includeRoot && rootDeviceName != null && rootDeviceName.equals(ibdm.getDeviceName())) {
                    continue;
                }
                volumeIds.add(volumeId);
            }
        }
        return volumeIds;
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            throw new NotFoundException("AWS instance " + instanceId + " not found", e);
        }
        throw e;
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AmazonServiceException(com.amazonaws.AmazonServiceException) NotFoundException(com.netflix.simianarmy.NotFoundException)

Example 48 with Instance

use of com.amazonaws.services.ec2.model.Instance in project eureka by Netflix.

the class EIPManager method unbindEIP.

/**
     * Unbind the EIP that this instance is associated with.
     */
public void unbindEIP() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myPublicIP = null;
    if (myInfo != null && myInfo.getDataCenterInfo().getName() == Name.Amazon) {
        myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4);
        if (myPublicIP == null) {
            logger.info("Instance is not associated with an EIP. Will not try to unbind");
            return;
        }
        try {
            AmazonEC2 ec2Service = getEC2Service();
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(myPublicIP);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                DisassociateAddressRequest dissociateRequest = new DisassociateAddressRequest();
                String domain = eipAddress.getDomain();
                if ("vpc".equals(domain)) {
                    dissociateRequest.setAssociationId(eipAddress.getAssociationId());
                } else {
                    dissociateRequest.setPublicIp(eipAddress.getPublicIp());
                }
                ec2Service.disassociateAddress(dissociateRequest);
                logger.info("Dissociated the EIP {} from this instance", myPublicIP);
            }
        } catch (Throwable e) {
            throw new RuntimeException("Cannot dissociate address from this instance", e);
        }
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 49 with Instance

use of com.amazonaws.services.ec2.model.Instance in project eureka by Netflix.

the class EIPManager method bindEIP.

/**
     * Checks if an EIP is bound and optionally binds the EIP.
     *
     * The list of EIPs are arranged with the EIPs allocated in the zone first
     * followed by other EIPs.
     *
     * If an EIP is already bound to this instance this method simply returns. Otherwise, this method tries to find
     * an unused EIP based on information from AWS. If it cannot find any unused EIP this method, it will be retried
     * for a specified interval.
     *
     * One of the following scenarios can happen here :
     *
     *  1) If the instance is already bound to an EIP as deemed by AWS, no action is taken.
     *  2) If an EIP is already bound to another instance as deemed by AWS, that EIP is skipped.
     *  3) If an EIP is not already bound to an instance and if this instance is not bound to an EIP, then
     *     the EIP is bound to this instance.
     */
public void bindEIP() {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone);
    Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone);
    AmazonEC2 ec2Service = getEC2Service();
    boolean isMyinstanceAssociatedWithEIP = false;
    Address selectedEIP = null;
    for (String eipEntry : candidateEIPs) {
        try {
            String associatedInstanceId;
            // Check with AWS, if this EIP is already been used by another instance
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(eipEntry);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                associatedInstanceId = eipAddress.getInstanceId();
                // already marked.
                if (((associatedInstanceId == null) || (associatedInstanceId.isEmpty()))) {
                    if (selectedEIP == null) {
                        selectedEIP = eipAddress;
                    }
                } else if (isMyinstanceAssociatedWithEIP = (associatedInstanceId.equals(myInstanceId))) {
                    // This EIP is associated with an instance, check if this is the same as the current instance.
                    // If it is the same, stop searching for an EIP as this instance is already associated with an
                    // EIP
                    selectedEIP = eipAddress;
                    break;
                } else {
                    // The EIP is used by some other instance, hence skip it
                    logger.warn("The selected EIP {} is associated with another instance {} according to AWS," + " hence skipping this", eipEntry, associatedInstanceId);
                }
            }
        } catch (Throwable t) {
            logger.error("Failed to bind elastic IP: {} to {}", eipEntry, myInstanceId, t);
        }
    }
    if (null != selectedEIP) {
        String publicIp = selectedEIP.getPublicIp();
        // Only bind if the EIP is not already associated
        if (!isMyinstanceAssociatedWithEIP) {
            AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest().withInstanceId(myInstanceId);
            String domain = selectedEIP.getDomain();
            if ("vpc".equals(domain)) {
                associateAddressRequest.setAllocationId(selectedEIP.getAllocationId());
            } else {
                associateAddressRequest.setPublicIp(publicIp);
            }
            ec2Service.associateAddress(associateAddressRequest);
            logger.info("\n\n\nAssociated {} running in zone: {} to elastic IP: {}", myInstanceId, myZone, publicIp);
        }
        logger.info("My instance {} seems to be already associated with the EIP {}", myInstanceId, publicIp);
    } else {
        logger.info("No EIP is free to be associated with this instance. Candidate EIPs are: {}", candidateEIPs);
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 50 with Instance

use of com.amazonaws.services.ec2.model.Instance in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method unbind.

/**
     * Unbind the IP that this instance is associated with.
     */
public void unbind() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    AmazonEC2 ec2 = getEC2Service();
    List<InstanceNetworkInterface> result = instanceData(myInstanceId, ec2).getNetworkInterfaces();
    List<String> ips = getCandidateIps();
    for (InstanceNetworkInterface networkInterface : result) {
        if (ips.contains(networkInterface.getPrivateIpAddress())) {
            String attachmentId = networkInterface.getAttachment().getAttachmentId();
            ec2.detachNetworkInterface(new DetachNetworkInterfaceRequest().withAttachmentId(attachmentId));
            break;
        }
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)33 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)20 Reservation (com.amazonaws.services.ec2.model.Reservation)16 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)11 Test (org.junit.Test)10 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)7 Filter (com.amazonaws.services.ec2.model.Filter)7 Tag (com.amazonaws.services.ec2.model.Tag)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)7 ArrayList (java.util.ArrayList)7 Exchange (org.apache.camel.Exchange)7 Processor (org.apache.camel.Processor)7 AmazonServiceException (com.amazonaws.AmazonServiceException)6 LinkedList (java.util.LinkedList)6 AWSCredentials (com.amazonaws.auth.AWSCredentials)5 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)5 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)5