Search in sources :

Example 6 with AWSResource

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

the class OrphanedInstanceRule method isValid.

@Override
public boolean isValid(Resource resource) {
    Validate.notNull(resource);
    if (!resource.getResourceType().name().equals("INSTANCE")) {
        // resource not for cleanup.
        return true;
    }
    String awsStatus = ((AWSResource) resource).getAWSResourceState();
    if (!"running".equals(awsStatus) || "pending".equals(awsStatus)) {
        return true;
    }
    AWSResource instanceResource = (AWSResource) resource;
    String asgName = instanceResource.getAdditionalField(InstanceJanitorCrawler.INSTANCE_FIELD_ASG_NAME);
    String opsworkStackName = instanceResource.getAdditionalField(InstanceJanitorCrawler.INSTANCE_FIELD_OPSWORKS_STACK_NAME);
    // If there is no ASG AND it isn't an OpsWorks stack (or OpsWorks isn't respected as a parent), we have an orphan
    if (StringUtils.isEmpty(asgName) && (!respectOpsWorksParentage || StringUtils.isEmpty(opsworkStackName))) {
        if (resource.getLaunchTime() == null) {
            LOGGER.error(String.format("The instance %s has no launch time.", resource.getId()));
            return true;
        } else {
            DateTime launchTime = new DateTime(resource.getLaunchTime().getTime());
            DateTime now = new DateTime(calendar.now().getTimeInMillis());
            if (now.isBefore(launchTime.plusDays(instanceAgeThreshold))) {
                LOGGER.info(String.format("The orphaned instance %s has not launched for more than %d days", resource.getId(), instanceAgeThreshold));
                return true;
            }
            LOGGER.info(String.format("The orphaned instance %s has launched for more than %d days", resource.getId(), instanceAgeThreshold));
            if (resource.getExpectedTerminationTime() == null) {
                int retentionDays = retentionDaysWithoutOwner;
                if (resource.getOwnerEmail() != null) {
                    retentionDays = retentionDaysWithOwner;
                }
                Date terminationTime = calendar.getBusinessDay(new Date(now.getMillis()), retentionDays);
                resource.setExpectedTerminationTime(terminationTime);
                resource.setTerminationReason((respectOpsWorksParentage) ? ASG_OR_OPSWORKS_TERMINATION_REASON : TERMINATION_REASON);
            }
            return false;
        }
    }
    return true;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 7 with AWSResource

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

the class OldUnusedLaunchConfigRule method isValid.

@Override
public boolean isValid(Resource resource) {
    Validate.notNull(resource);
    if (!"LAUNCH_CONFIG".equals(resource.getResourceType().name())) {
        return true;
    }
    AWSResource lcResource = (AWSResource) resource;
    String usedByASG = lcResource.getAdditionalField(LaunchConfigJanitorCrawler.LAUNCH_CONFIG_FIELD_USED_BY_ASG);
    if (StringUtils.isNotEmpty(usedByASG) && !Boolean.parseBoolean(usedByASG)) {
        if (resource.getLaunchTime() == null) {
            LOGGER.error(String.format("The launch config %s has no creation time.", resource.getId()));
            return true;
        } else {
            DateTime launchTime = new DateTime(resource.getLaunchTime().getTime());
            DateTime now = new DateTime(calendar.now().getTimeInMillis());
            if (now.isBefore(launchTime.plusDays(ageThreshold))) {
                LOGGER.info(String.format("The unused launch config %s has not been created for more than %d days", resource.getId(), ageThreshold));
                return true;
            }
            LOGGER.info(String.format("The unused launch config %s has been created for more than %d days", resource.getId(), ageThreshold));
            if (resource.getExpectedTerminationTime() == null) {
                Date terminationTime = calendar.getBusinessDay(new Date(now.getMillis()), retentionDays);
                resource.setExpectedTerminationTime(terminationTime);
                resource.setTerminationReason(TERMINATION_REASON);
            }
            return false;
        }
    }
    return true;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 8 with AWSResource

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

the class NoGeneratedAMIRule method isValid.

@Override
public boolean isValid(Resource resource) {
    Validate.notNull(resource);
    if (!resource.getResourceType().name().equals("EBS_SNAPSHOT")) {
        return true;
    }
    if (!"completed".equals(((AWSResource) resource).getAWSResourceState())) {
        return true;
    }
    String janitorTag = resource.getTag(JanitorMonkey.JANITOR_TAG);
    if (janitorTag != null) {
        if ("donotmark".equals(janitorTag)) {
            LOGGER.info(String.format("The snapshot %s is tagged as not handled by Janitor", resource.getId()));
            return true;
        }
        try {
            // Owners can tag the volume with a termination date in the "janitor" tag.
            Date userSpecifiedDate = new Date(TERMINATION_DATE_FORMATTER.parseDateTime(janitorTag).getMillis());
            resource.setExpectedTerminationTime(userSpecifiedDate);
            resource.setTerminationReason(String.format("User specified termination date %s", janitorTag));
            if (ownerEmailOverride != null) {
                resource.setOwnerEmail(ownerEmailOverride);
            }
            return false;
        } catch (Exception e) {
            LOGGER.error(String.format("The janitor tag is not a user specified date: %s", janitorTag));
        }
    }
    if (hasGeneratedImage(resource)) {
        return true;
    }
    if (resource.getLaunchTime() == null) {
        LOGGER.error(String.format("Snapshot %s does not have a creation time.", resource.getId()));
        return true;
    }
    DateTime launchTime = new DateTime(resource.getLaunchTime().getTime());
    DateTime now = new DateTime(calendar.now().getTimeInMillis());
    if (launchTime.plusDays(ageThreshold).isBefore(now)) {
        if (ownerEmailOverride != null) {
            resource.setOwnerEmail(ownerEmailOverride);
        }
        if (resource.getExpectedTerminationTime() == null) {
            Date terminationTime = calendar.getBusinessDay(new Date(now.getMillis()), retentionDays);
            resource.setExpectedTerminationTime(terminationTime);
            resource.setTerminationReason(TERMINATION_REASON);
            LOGGER.info(String.format("Snapshot %s is marked to be cleaned at %s as there is no AMI generated using it", resource.getId(), resource.getExpectedTerminationTime()));
        } 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 9 with AWSResource

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

the class DeleteOnTerminationRule method isValid.

@Override
public boolean isValid(Resource resource) {
    Validate.notNull(resource);
    if (!resource.getResourceType().name().equals("EBS_VOLUME")) {
        return true;
    }
    // The state of the volume being "available" means that it is not attached to any instance.
    if (!"available".equals(((AWSResource) resource).getAWSResourceState())) {
        return true;
    }
    String janitorTag = resource.getTag(JanitorMonkey.JANITOR_TAG);
    if (janitorTag != null) {
        if ("donotmark".equals(janitorTag)) {
            LOGGER.info(String.format("The volume %s is tagged as not handled by Janitor", resource.getId()));
            return true;
        }
        try {
            // Owners can tag the volume with a termination date in the "janitor" tag.
            Date userSpecifiedDate = new Date(TERMINATION_DATE_FORMATTER.parseDateTime(janitorTag).getMillis());
            resource.setExpectedTerminationTime(userSpecifiedDate);
            resource.setTerminationReason(String.format("User specified termination date %s", janitorTag));
            return false;
        } catch (Exception e) {
            LOGGER.error(String.format("The janitor tag is not a user specified date: %s", janitorTag));
        }
    }
    if ("true".equals(resource.getAdditionalField(EddaEBSVolumeJanitorCrawler.DELETE_ON_TERMINATION))) {
        if (resource.getExpectedTerminationTime() == null) {
            Date terminationTime = calendar.getBusinessDay(calendar.now().getTime(), retentionDays);
            resource.setExpectedTerminationTime(terminationTime);
            resource.setTerminationReason(TERMINATION_REASON);
            LOGGER.info(String.format("Volume %s is marked to be cleaned at %s as it is detached and DeleteOnTermination was set", resource.getId(), resource.getExpectedTerminationTime()));
        } 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)

Example 10 with AWSResource

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

the class OldDetachedVolumeRule method isValid.

@Override
public boolean isValid(Resource resource) {
    Validate.notNull(resource);
    if (!resource.getResourceType().name().equals("EBS_VOLUME")) {
        return true;
    }
    if (!"available".equals(((AWSResource) resource).getAWSResourceState())) {
        return true;
    }
    String janitorTag = resource.getTag(JanitorMonkey.JANITOR_TAG);
    if (janitorTag != null) {
        if ("donotmark".equals(janitorTag)) {
            LOGGER.info(String.format("The volume %s is tagged as not handled by Janitor", resource.getId()));
            return true;
        }
        try {
            // Owners can tag the volume with a termination date in the "janitor" tag.
            Date userSpecifiedDate = new Date(TERMINATION_DATE_FORMATTER.parseDateTime(janitorTag).getMillis());
            resource.setExpectedTerminationTime(userSpecifiedDate);
            resource.setTerminationReason(String.format("User specified termination date %s", janitorTag));
            return false;
        } catch (Exception e) {
            LOGGER.error(String.format("The janitor tag is not a user specified date: %s", janitorTag));
        }
    }
    String janitorMetaTag = resource.getTag(JanitorMonkey.JANITOR_META_TAG);
    if (janitorMetaTag == null) {
        LOGGER.info(String.format("Volume %s is not tagged with the Janitor meta information, ignore.", resource.getId()));
        return true;
    }
    Map<String, String> metadata = VolumeTaggingMonkey.parseJanitorMetaTag(janitorMetaTag);
    String detachTimeTag = metadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY);
    if (detachTimeTag == null) {
        return true;
    }
    DateTime detachTime = null;
    try {
        detachTime = AWSResource.DATE_FORMATTER.parseDateTime(detachTimeTag);
    } catch (Exception e) {
        LOGGER.error(String.format("Detach time in the JANITOR_META tag of %s is not in the valid format: %s", resource.getId(), detachTime));
        return true;
    }
    DateTime now = new DateTime(calendar.now().getTimeInMillis());
    if (detachTime != null && detachTime.plusDays(detachDaysThreshold).isBefore(now)) {
        if (resource.getExpectedTerminationTime() == null) {
            Date terminationTime = calendar.getBusinessDay(new Date(now.getMillis()), retentionDays);
            resource.setExpectedTerminationTime(terminationTime);
            resource.setTerminationReason(String.format("Volume not attached for %d days", detachDaysThreshold + retentionDays));
            LOGGER.info(String.format("Volume %s is marked to be cleaned at %s as it is detached for more than %d days", resource.getId(), resource.getExpectedTerminationTime(), detachDaysThreshold));
        } 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)

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