Search in sources :

Example 41 with Instance

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

the class InstanceJanitorCrawler method getInstanceResources.

private List<Resource> getInstanceResources(String... instanceIds) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    Map<String, AutoScalingInstanceDetails> idToASGInstance = new HashMap<String, AutoScalingInstanceDetails>();
    for (AutoScalingInstanceDetails instanceDetails : awsClient.describeAutoScalingInstances(instanceIds)) {
        idToASGInstance.put(instanceDetails.getInstanceId(), instanceDetails);
    }
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        Resource instanceResource = new AWSResource().withId(instance.getInstanceId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.INSTANCE).withLaunchTime(instance.getLaunchTime());
        for (Tag tag : instance.getTags()) {
            instanceResource.setTag(tag.getKey(), tag.getValue());
        }
        String description = String.format("type=%s; host=%s", instance.getInstanceType(), instance.getPublicDnsName() == null ? "" : instance.getPublicDnsName());
        instanceResource.setDescription(description);
        instanceResource.setOwnerEmail(getOwnerEmailForResource(instanceResource));
        String asgName = getAsgName(instanceResource, idToASGInstance);
        if (asgName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_ASG_NAME, asgName);
            LOGGER.info(String.format("instance %s has a ASG tag name %s.", instanceResource.getId(), asgName));
        }
        String opsworksStackName = getOpsWorksStackName(instanceResource);
        if (opsworksStackName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_OPSWORKS_STACK_NAME, opsworksStackName);
            LOGGER.info(String.format("instance %s is part of an OpsWorks stack named %s.", instanceResource.getId(), opsworksStackName));
        }
        if (instance.getState() != null) {
            ((AWSResource) instanceResource).setAWSResourceState(instance.getState().getName());
        }
        resources.add(instanceResource);
    }
    return resources;
}
Also used : HashMap(java.util.HashMap) Instance(com.amazonaws.services.ec2.model.Instance) AWSResource(com.netflix.simianarmy.aws.AWSResource) 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) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 42 with Instance

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

the class InstanceInVPC method checkInstancesInVPC.

private Set<String> checkInstancesInVPC(String region, Collection<String> instances) {
    Set<String> failedInstances = Sets.newHashSet();
    for (String instanceId : instances) {
        for (Instance awsInstance : getAWSInstances(region, instanceId)) {
            if (awsInstance.getVpcId() == null) {
                LOGGER.info(String.format("Instance %s is not in a virtual private cloud", instanceId));
                failedInstances.add(instanceId);
            }
        }
    }
    return failedInstances;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance)

Example 43 with Instance

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

the class InstanceTooOld method getInstanceLaunchTimes.

/**
     * Gets the launch time (in milliseconds) for a list of instance ids of the same region. The default
     * implementation is using an AWS client. The method can be overridden in subclasses to get the instance
     * launch times differently.
     * @param region
     *      the region of the instances
     * @param instanceIds
     *      the instance ids, all instances should be in the same region.
     * @return
     *      the map from instance id to the launch time in milliseconds
     */
protected Map<String, Long> getInstanceLaunchTimes(String region, String... instanceIds) {
    Map<String, Long> result = Maps.newHashMap();
    if (instanceIds == null || instanceIds.length == 0) {
        return result;
    }
    AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        if (instance.getLaunchTime() != null) {
            result.put(instance.getInstanceId(), instance.getLaunchTime().getTime());
        } else {
            LOGGER.warn(String.format("No launch time found for instance %s", instance.getInstanceId()));
        }
    }
    return result;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AWSClient(com.netflix.simianarmy.client.aws.AWSClient)

Example 44 with Instance

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

the class TestInstanceInVPC method setUp.

@BeforeMethod
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    List<Instance> instanceList = Lists.newArrayList();
    Instance instance = new Instance().withInstanceId(VPC_INSTANCE_ID).withVpcId("12345");
    instanceList.add(instance);
    doReturn(instanceList).when(instanceInVPC).getAWSInstances(REGION, VPC_INSTANCE_ID);
    List<Instance> instanceList2 = Lists.newArrayList();
    Instance instance2 = new Instance().withInstanceId(INSTANCE_ID);
    instanceList2.add(instance2);
    doReturn(instanceList2).when(instanceInVPC).getAWSInstances(REGION, INSTANCE_ID);
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 45 with Instance

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

the class TestInstanceJanitorCrawler method testInstancesNotExistingInASG.

@Test
public void testInstancesNotExistingInASG() {
    List<AutoScalingInstanceDetails> instanceDetailsList = Collections.emptyList();
    List<Instance> instanceList = createInstanceList();
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    List<Resource> resources = crawler.resources();
    Assert.assertEquals(resources.size(), instanceList.size());
}
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)

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