这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Mar 4, 2021. It is now read-only.

OpsWorks-aware Janitor monkey #185

Merged
merged 1 commit into from
Aug 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class InstanceJanitorCrawler extends AbstractAWSJanitorCrawler {
/** The name representing the additional field name of ASG's name. */
public static final String INSTANCE_FIELD_ASG_NAME = "ASG_NAME";

/** The name representing the additional field name of the OpsWork stack name. */
public static final String INSTANCE_FIELD_OPSWORKS_STACK_NAME = "OPSWORKS_STACK_NAME";

/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(InstanceJanitorCrawler.class);

Expand Down Expand Up @@ -101,6 +104,11 @@ private List<Resource> getInstanceResources(String... instanceIds) {
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());
}
Expand All @@ -122,4 +130,8 @@ private String getAsgName(Resource instanceResource, Map<String, AutoScalingInst
}
return asgName;
}

private String getOpsWorksStackName(Resource instanceResource) {
return instanceResource.getTag("opsworks:stack");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class OrphanedInstanceRule implements Rule {
private static final Logger LOGGER = LoggerFactory.getLogger(OrphanedInstanceRule.class);

private static final String TERMINATION_REASON = "No ASG is associated with this instance";
private static final String ASG_OR_OPSWORKS_TERMINATION_REASON = "No ASG or OpsWorks stack is associated with this instance";

private final MonkeyCalendar calendar;

Expand All @@ -50,6 +51,8 @@ public class OrphanedInstanceRule implements Rule {
private final int retentionDaysWithOwner;

private final int retentionDaysWithoutOwner;

private final boolean respectOpsWorksParentage;

/**
* Constructor for OrphanedInstanceRule.
Expand All @@ -64,9 +67,11 @@ public class OrphanedInstanceRule implements Rule {
* @param retentionDaysWithoutOwner
* The number of days that the orphaned instance is retained before being terminated
* when the instance has no owner specified
* @param respectOpsWorksParentage
* If true, don't consider members of an OpsWorks stack as orphans
*/
public OrphanedInstanceRule(MonkeyCalendar calendar,
int instanceAgeThreshold, int retentionDaysWithOwner, int retentionDaysWithoutOwner) {
int instanceAgeThreshold, int retentionDaysWithOwner, int retentionDaysWithoutOwner, boolean respectOpsWorksParentage) {
Validate.notNull(calendar);
Validate.isTrue(instanceAgeThreshold >= 0);
Validate.isTrue(retentionDaysWithOwner >= 0);
Expand All @@ -75,6 +80,12 @@ public OrphanedInstanceRule(MonkeyCalendar calendar,
this.instanceAgeThreshold = instanceAgeThreshold;
this.retentionDaysWithOwner = retentionDaysWithOwner;
this.retentionDaysWithoutOwner = retentionDaysWithoutOwner;
this.respectOpsWorksParentage = respectOpsWorksParentage;
}

public OrphanedInstanceRule(MonkeyCalendar calendar,
int instanceAgeThreshold, int retentionDaysWithOwner, int retentionDaysWithoutOwner) {
this(calendar, instanceAgeThreshold, retentionDaysWithOwner, retentionDaysWithoutOwner, false);
}

@Override
Expand All @@ -92,7 +103,9 @@ public boolean isValid(Resource resource) {
}
AWSResource instanceResource = (AWSResource) resource;
String asgName = instanceResource.getAdditionalField(InstanceJanitorCrawler.INSTANCE_FIELD_ASG_NAME);
if (StringUtils.isEmpty(asgName)) {
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;
Expand All @@ -113,7 +126,7 @@ public boolean isValid(Resource resource) {
}
Date terminationTime = calendar.getBusinessDay(new Date(now.getMillis()), retentionDays);
resource.setExpectedTerminationTime(terminationTime);
resource.setTerminationReason(TERMINATION_REASON);
resource.setTerminationReason((respectOpsWorksParentage) ? ASG_OR_OPSWORKS_TERMINATION_REASON : TERMINATION_REASON);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ private InstanceJanitor getInstanceJanitor() {
"simianarmy.janitor.rule.orphanedInstanceRule.retentionDaysWithOwner", 3),
(int) configuration().getNumOrElse(
"simianarmy.janitor.rule.orphanedInstanceRule.retentionDaysWithoutOwner",
8)));
8),
configuration().getBoolOrElse(
"simianarmy.janitor.rule.orphanedInstanceRule.opsworks.parentage",
false)));
}
JanitorCrawler instanceCrawler;
if (configuration().getBoolOrElse("simianarmy.janitor.edda.enabled", false)) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/janitor.properties
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ simianarmy.janitor.rule.orphanedInstanceRule.retentionDaysWithOwner = 3
# The number of business days the instance is kept after a notification is sent for the termination
# when the instance has no owner.
simianarmy.janitor.rule.orphanedInstanceRule.retentionDaysWithoutOwner = 8
# If true, don't consider members of an OpsWorks stack as orphans
simianarmy.janitor.rule.orphanedInstanceRule.opsworks.parentage = false

# The following properties are used by the Janitor rule for cleaning up volumes that have been
# detached from instances for certain days.
Expand Down