Search in sources :

Example 66 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource 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 67 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class EddaASGJanitorCrawler method parseJsonElementToresource.

private Resource parseJsonElementToresource(String region, JsonNode jsonNode, Map<String, Long> lcNameToCreationTime) {
    Validate.notNull(jsonNode);
    String asgName = jsonNode.get("autoScalingGroupName").getTextValue();
    long createdTime = jsonNode.get("createdTime").getLongValue();
    Resource resource = new AWSResource().withId(asgName).withRegion(region).withResourceType(AWSResourceType.ASG).withLaunchTime(new Date(createdTime));
    JsonNode tags = jsonNode.get("tags");
    if (tags == null || !tags.isArray() || tags.size() == 0) {
        LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
    } else {
        for (Iterator<JsonNode> it = tags.getElements(); it.hasNext(); ) {
            JsonNode tag = it.next();
            String key = tag.get("key").getTextValue();
            String value = tag.get("value").getTextValue();
            resource.setTag(key, value);
        }
    }
    String owner = getOwnerEmailForResource(resource);
    if (owner != null) {
        resource.setOwnerEmail(owner);
    }
    JsonNode maxSize = jsonNode.get("maxSize");
    if (maxSize != null) {
        resource.setAdditionalField(ASG_FIELD_MAX_SIZE, String.valueOf(maxSize.getIntValue()));
    }
    // Adds instances and ELBs as additional fields.
    JsonNode instances = jsonNode.get("instances");
    resource.setDescription(String.format("%d instances", instances.size()));
    List<String> instanceIds = Lists.newArrayList();
    for (Iterator<JsonNode> it = instances.getElements(); it.hasNext(); ) {
        instanceIds.add(it.next().get("instanceId").getTextValue());
    }
    resource.setAdditionalField(ASG_FIELD_INSTANCES, StringUtils.join(instanceIds, ","));
    JsonNode elbs = jsonNode.get("loadBalancerNames");
    List<String> elbNames = Lists.newArrayList();
    for (Iterator<JsonNode> it = elbs.getElements(); it.hasNext(); ) {
        elbNames.add(it.next().getTextValue());
    }
    resource.setAdditionalField(ASG_FIELD_ELBS, StringUtils.join(elbNames, ","));
    JsonNode lc = jsonNode.get("launchConfigurationName");
    if (lc != null) {
        String lcName = lc.getTextValue();
        Long lcCreationTime = lcNameToCreationTime.get(lcName);
        if (lcName != null) {
            resource.setAdditionalField(ASG_FIELD_LC_NAME, lcName);
        }
        if (lcCreationTime != null) {
            resource.setAdditionalField(ASG_FIELD_LC_CREATION_TIME, String.valueOf(lcCreationTime));
        }
    }
    // sets the field for the time when the ASG's traffic is suspended from ELB
    JsonNode suspendedProcesses = jsonNode.get("suspendedProcesses");
    for (Iterator<JsonNode> it = suspendedProcesses.getElements(); it.hasNext(); ) {
        JsonNode sp = it.next();
        if ("AddToLoadBalancer".equals(sp.get("processName").getTextValue())) {
            String suspensionTime = getSuspensionTimeString(sp.get("suspensionReason").getTextValue());
            if (suspensionTime != null) {
                LOGGER.info(String.format("Suspension time of ASG %s is %s", asgName, suspensionTime));
                resource.setAdditionalField(ASG_FIELD_SUSPENSION_TIME, suspensionTime);
                break;
            }
        }
    }
    Long lastChangeTime = regionToAsgToLastChangeTime.get(region).get(asgName);
    if (lastChangeTime != null) {
        resource.setAdditionalField(ASG_FIELD_LAST_CHANGE_TIME, String.valueOf(lastChangeTime));
    }
    return resource;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

Example 68 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class EddaEBSSnapshotJanitorCrawler method parseJsonElementToSnapshotResource.

private Resource parseJsonElementToSnapshotResource(String region, JsonNode jsonNode) {
    Validate.notNull(jsonNode);
    long startTime = jsonNode.get("startTime").asLong();
    Resource resource = new AWSResource().withId(jsonNode.get("snapshotId").getTextValue()).withRegion(region).withResourceType(AWSResourceType.EBS_SNAPSHOT).withLaunchTime(new Date(startTime));
    JsonNode tags = jsonNode.get("tags");
    if (tags == null || !tags.isArray() || tags.size() == 0) {
        LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
    } else {
        for (Iterator<JsonNode> it = tags.getElements(); it.hasNext(); ) {
            JsonNode tag = it.next();
            String key = tag.get("key").getTextValue();
            String value = tag.get("value").getTextValue();
            resource.setTag(key, value);
        }
    }
    JsonNode description = jsonNode.get("description");
    if (description != null) {
        resource.setDescription(description.getTextValue());
    }
    ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").getTextValue());
    Collection<String> amis = snapshotToAMIs.get(resource.getId());
    if (amis != null) {
        resource.setAdditionalField(SNAPSHOT_FIELD_AMIS, StringUtils.join(amis, ","));
    }
    resource.setOwnerEmail(getOwnerEmailForResource(resource));
    return resource;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

Example 69 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class UnusedImageRule method isValid.

@Override
public boolean isValid(Resource resource) {
    Validate.notNull(resource);
    if (!"IMAGE".equals(resource.getResourceType().name())) {
        return true;
    }
    if (!"available".equals(((AWSResource) resource).getAWSResourceState())) {
        return true;
    }
    if ("true".equals(resource.getAdditionalField(EddaImageJanitorCrawler.AMI_FIELD_BASE_IMAGE))) {
        LOGGER.info(String.format("Image %s is a base image that is used to create other images", resource.getId()));
        return true;
    }
    long instanceRefTime = getRefTimeInMilis(resource, EddaImageJanitorCrawler.AMI_FIELD_LAST_INSTANCE_REF_TIME);
    long lcRefTime = getRefTimeInMilis(resource, EddaImageJanitorCrawler.AMI_FIELD_LAST_LC_REF_TIME);
    Date now = calendar.now().getTime();
    long windowStart = new DateTime(now.getTime()).minusDays(lastReferenceDaysThreshold).getMillis();
    if (instanceRefTime < windowStart && lcRefTime < windowStart) {
        if (resource.getExpectedTerminationTime() == null) {
            Date terminationTime = calendar.getBusinessDay(now, retentionDays);
            resource.setExpectedTerminationTime(terminationTime);
            resource.setTerminationReason(String.format("Image not referenced for %d days", lastReferenceDaysThreshold + retentionDays));
            LOGGER.info(String.format("Image %s in region %s is marked to be cleaned at %s as it is not referenced" + "for more than %d days", resource.getId(), resource.getRegion(), resource.getExpectedTerminationTime(), lastReferenceDaysThreshold));
        } else {
            LOGGER.info(String.format("Resource %s is already marked.", resource.getId()));
        }
        return false;
    }
    return true;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Date(java.util.Date) DateTime(org.joda.time.DateTime)

Example 70 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class EddaImageJanitorCrawler method parseJsonElementToresource.

private Resource parseJsonElementToresource(String region, JsonNode jsonNode) {
    Validate.notNull(jsonNode);
    String imageId = jsonNode.get("imageId").getTextValue();
    Resource resource = new AWSResource().withId(imageId).withRegion(region).withResourceType(AWSResourceType.IMAGE);
    Long creationTime = imageIdToCreationTime.get(imageId);
    if (creationTime != null) {
        resource.setLaunchTime(new Date(creationTime));
    }
    JsonNode tags = jsonNode.get("tags");
    if (tags == null || !tags.isArray() || tags.size() == 0) {
        LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
    } else {
        for (Iterator<JsonNode> it = tags.getElements(); it.hasNext(); ) {
            JsonNode tag = it.next();
            String key = tag.get("key").getTextValue();
            String value = tag.get("value").getTextValue();
            resource.setTag(key, value);
        }
    }
    JsonNode descNode = jsonNode.get("description");
    if (descNode != null && !descNode.isNull()) {
        String description = descNode.getTextValue();
        resource.setDescription(description);
        String ancestorImageId = getBaseAmiIdFromDescription(description);
        if (ancestorImageId != null && !ancestorImageIds.contains(ancestorImageId)) {
            LOGGER.info(String.format("Found base AMI id %s from description '%s'", ancestorImageId, description));
            ancestorImageIds.add(ancestorImageId);
        }
    }
    ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").getTextValue());
    String owner = getOwnerEmailForResource(resource);
    if (owner != null) {
        resource.setOwnerEmail(owner);
    }
    return resource;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

Aggregations

AWSResource (com.netflix.simianarmy.aws.AWSResource)109 Resource (com.netflix.simianarmy.Resource)102 Test (org.testng.annotations.Test)89 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)64 DateTime (org.joda.time.DateTime)63 Date (java.util.Date)41 MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 BeforeTest (org.testng.annotations.BeforeTest)9 JsonNode (org.codehaus.jackson.JsonNode)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)6 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)5 HashSet (java.util.HashSet)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 LinkedList (java.util.LinkedList)4 Tag (com.amazonaws.services.ec2.model.Tag)3 HashMap (java.util.HashMap)3 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 LaunchConfiguration (com.amazonaws.services.autoscaling.model.LaunchConfiguration)2 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)1 Instance (com.amazonaws.services.autoscaling.model.Instance)1