Search in sources :

Example 11 with Instance

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

the class VolumeTaggingMonkey method tagVolumesWithLatestAttachment.

private void tagVolumesWithLatestAttachment(AWSClient awsClient) {
    List<Volume> volumes = awsClient.describeVolumes();
    LOGGER.info(String.format("Trying to tag %d volumes for Janitor Monkey meta data.", volumes.size()));
    Date now = calendar.now().getTime();
    for (Volume volume : volumes) {
        String owner = null, instanceId = null;
        Date lastDetachTime = null;
        List<VolumeAttachment> attachments = volume.getAttachments();
        List<Tag> tags = volume.getTags();
        // by Janitor monkey.
        if ("donotmark".equals(getTagValue(JanitorMonkey.JANITOR_TAG, tags))) {
            LOGGER.info(String.format("The volume %s is tagged as not handled by Janitor", volume.getVolumeId()));
            continue;
        }
        Map<String, String> janitorMetadata = parseJanitorTag(tags);
        // finding the instance attached most recently.
        VolumeAttachment latest = null;
        for (VolumeAttachment attachment : attachments) {
            if (latest == null || latest.getAttachTime().before(attachment.getAttachTime())) {
                latest = attachment;
            }
        }
        if (latest != null) {
            instanceId = latest.getInstanceId();
            owner = getOwnerEmail(instanceId, janitorMetadata, tags, awsClient);
        }
        if (latest == null || "detached".equals(latest.getState())) {
            if (janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY) == null) {
                // There is no attached instance and the last detached time is not set.
                // Use the current time as the last detached time.
                LOGGER.info(String.format("Setting the last detached time to %s for volume %s", now, volume.getVolumeId()));
                lastDetachTime = now;
            } else {
                LOGGER.debug(String.format("The volume %s was already marked as detached at time %s", volume.getVolumeId(), janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY)));
            }
        } else {
            // The volume is currently attached to an instance
            lastDetachTime = null;
        }
        String existingOwner = janitorMetadata.get(BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY);
        if (owner == null && existingOwner != null) {
            // Save the current owner in the tag when we are not able to find a owner.
            owner = existingOwner;
        }
        if (needsUpdate(janitorMetadata, owner, instanceId, lastDetachTime)) {
            Event evt = updateJanitorMetaTag(volume, instanceId, owner, lastDetachTime, awsClient);
            if (evt != null) {
                context().recorder().recordEvent(evt);
            }
        }
    }
}
Also used : VolumeAttachment(com.amazonaws.services.ec2.model.VolumeAttachment) Volume(com.amazonaws.services.ec2.model.Volume) Event(com.netflix.simianarmy.MonkeyRecorder.Event) Tag(com.amazonaws.services.ec2.model.Tag) Date(java.util.Date)

Example 12 with Instance

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

the class AWSClient method getVpcId.

/**
     * Gets the VPC id for the given instance.
     *
     * @param instanceId
     *            instance we're checking
     * @return vpc id, or null if not a vpc instance
     */
String getVpcId(String instanceId) {
    Instance awsInstance = describeInstance(instanceId);
    String vpcId = awsInstance.getVpcId();
    if (Strings.isNullOrEmpty(vpcId)) {
        return null;
    }
    return vpcId;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance)

Example 13 with Instance

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

the class AWSClient method describeInstances.

/**
     * Describe a set of specific instances.
     *
     * @param instanceIds the instance ids
     * @return the instances
     */
public List<Instance> describeInstances(String... instanceIds) {
    if (instanceIds == null || instanceIds.length == 0) {
        LOGGER.info(String.format("Getting all EC2 instances in region %s.", region));
    } else {
        LOGGER.info(String.format("Getting EC2 instances for %d ids in region %s.", instanceIds.length, region));
    }
    List<Instance> instances = new LinkedList<Instance>();
    AmazonEC2 ec2Client = ec2Client();
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    if (instanceIds != null) {
        request.withInstanceIds(Arrays.asList(instanceIds));
    }
    DescribeInstancesResult result = ec2Client.describeInstances(request);
    for (Reservation reservation : result.getReservations()) {
        instances.addAll(reservation.getInstances());
    }
    LOGGER.info(String.format("Got %d EC2 instances in region %s.", instances.size(), region));
    return instances;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 14 with Instance

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

the class TestInstanceJanitorCrawler method testInstancesWithNullIds.

@Test
public void testInstancesWithNullIds() {
    List<AutoScalingInstanceDetails> instanceDetailsList = createInstanceDetailsList();
    List<Instance> instanceList = createInstanceList();
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    List<Resource> resources = crawler.resources();
    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 15 with Instance

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

the class TestInstanceJanitorCrawler method testInstancesWithResourceType.

@Test
public void testInstancesWithResourceType() {
    List<AutoScalingInstanceDetails> instanceDetailsList = createInstanceDetailsList();
    List<Instance> instanceList = createInstanceList();
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    for (AWSResourceType resourceType : AWSResourceType.values()) {
        List<Resource> resources = crawler.resources(resourceType);
        if (resourceType == AWSResourceType.INSTANCE) {
            verifyInstanceList(resources, instanceDetailsList);
        } else {
            Assert.assertTrue(resources.isEmpty());
        }
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) 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)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)7 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)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 Exchange (org.apache.camel.Exchange)7 Processor (org.apache.camel.Processor)7 AmazonServiceException (com.amazonaws.AmazonServiceException)6 ArrayList (java.util.ArrayList)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