Skip to content

Commit

Permalink
Add tag to prevent instance from being stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
troymohl committed Mar 16, 2020
1 parent 99b4ec6 commit 2da3e25
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
57 changes: 42 additions & 15 deletions src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StopInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;
import com.google.common.annotations.VisibleForTesting;
import hudson.Extension;
import hudson.Util;
Expand Down Expand Up @@ -63,7 +64,6 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;

/**
Expand Down Expand Up @@ -95,7 +95,9 @@ public class AmazonEC2Cloud extends EC2Cloud {

private String instanceTagForJenkins;

private String nodeTagForEc2;
private String nodeLabelForEc2;

private String preventStopAwsTag;

private String maxIdleMinutes;

Expand Down Expand Up @@ -186,23 +188,32 @@ public void setAltEC2Endpoint(String altEC2Endpoint) {
this.altEC2Endpoint = altEC2Endpoint;
}

public String getNodeTagForEc2() {
return nodeTagForEc2;
public String getNodeLabelForEc2() {
return nodeLabelForEc2;
}

@DataBoundSetter
public void setNodeLabelForEc2(String nodeLabelForEc2 ) {
this.nodeLabelForEc2 = nodeLabelForEc2;
}

public String getPreventStopAwsTag() {
return preventStopAwsTag;
}

@DataBoundSetter
public void setNodeTagForEc2(String nodeTagForEc2) {
this.nodeTagForEc2 = nodeTagForEc2;
public void setPreventStopAwsTag( String preventStopAwsTag ) {
this.preventStopAwsTag = preventStopAwsTag;
}

public boolean isEc2Node(Node node) {
//If no label is specified then we check all nodes
if (nodeTagForEc2 == null || nodeTagForEc2.trim().length() == 0) {
if ( nodeLabelForEc2 == null || nodeLabelForEc2.trim().length() == 0) {
return true;
}

for (LabelAtom label : node.getAssignedLabels()) {
if (label.getExpression().equalsIgnoreCase(nodeTagForEc2)) {
if (label.getExpression().equalsIgnoreCase( nodeLabelForEc2 )) {
return true;
}
}
Expand Down Expand Up @@ -281,13 +292,17 @@ public void stopNode(Node node) {

final String instanceId = nodeInstance.getInstanceId();

try {
StopInstancesRequest request = new StopInstancesRequest();
request.setInstanceIds(Collections.singletonList(instanceId));
connect().stopInstances(request);
LOGGER.log(Level.INFO, "Stopped instance: {0}", instanceId);
} catch (Exception e) {
LOGGER.log(Level.INFO, "Unable to stop instance: " + instanceId, e);
if (stopAllowed( nodeInstance )) {
try {
StopInstancesRequest request = new StopInstancesRequest();
request.setInstanceIds( Collections.singletonList( instanceId ) );
connect().stopInstances( request );
LOGGER.log( Level.INFO, "Stopped instance: {0}", instanceId );
} catch ( Exception e ) {
LOGGER.log( Level.INFO, "Unable to stop instance: " + instanceId, e );
}
} else {
LOGGER.log( Level.FINEST, "Not allowed to stop node: {0}", instanceId);
}
}

Expand Down Expand Up @@ -339,6 +354,18 @@ private Instance getInstance(List<Filter> filters, InstanceStateName desiredStat
return null;
}

private boolean stopAllowed(Instance instance) {
List<Tag> tags = instance.getTags();
if (tags != null) {
for ( Tag tag : tags) {
if (tag.getKey().trim().equals( preventStopAwsTag )) {
return false;
}
}
}
return true;
}

@Extension
public static class DescriptorImpl extends EC2Cloud.DescriptorImpl {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ THE SOFTWARE.
<f:entry title="${%Session Name}" field="roleSessionName">
<f:textbox />
</f:entry>
<f:entry title="${%Stop/Start Jenkins node role}" field="nodeTagForEc2">
<f:entry title="${%Stop/Start Jenkins node label}" field="nodeLabelForEc2">
<f:textbox default="ec2" />
</f:entry>
<f:entry title="${%Stop/Start EC2 Instance Tag}" field="instanceTagForJenkins">
<f:textbox />
</f:entry>
<f:entry title="${%Prevent Stop EC2 Instance Tag}" field="preventStopAwsTag">
<f:textbox default="prevent_stop"/>
</f:entry>
<f:entry title="${%Max minutes instance idle}" field="maxIdleMinutes">
<f:textbox default="15" />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Marker tag used on EC2 instances that should not be stopped by this plugin.
Tag value is not checked as the presence of the tag is enough to prevent the plugin from stopping the instance.

0 comments on commit 2da3e25

Please sign in to comment.