Skip to content

Commit 2da3e25

Browse files
committed
Add tag to prevent instance from being stopped
1 parent 99b4ec6 commit 2da3e25

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.amazonaws.services.ec2.model.Reservation;
3737
import com.amazonaws.services.ec2.model.StartInstancesRequest;
3838
import com.amazonaws.services.ec2.model.StopInstancesRequest;
39+
import com.amazonaws.services.ec2.model.Tag;
3940
import com.google.common.annotations.VisibleForTesting;
4041
import hudson.Extension;
4142
import hudson.Util;
@@ -63,7 +64,6 @@
6364
import org.kohsuke.stapler.DataBoundConstructor;
6465
import org.kohsuke.stapler.DataBoundSetter;
6566
import org.kohsuke.stapler.QueryParameter;
66-
import org.kohsuke.stapler.StaplerResponse;
6767
import org.kohsuke.stapler.interceptor.RequirePOST;
6868

6969
/**
@@ -95,7 +95,9 @@ public class AmazonEC2Cloud extends EC2Cloud {
9595

9696
private String instanceTagForJenkins;
9797

98-
private String nodeTagForEc2;
98+
private String nodeLabelForEc2;
99+
100+
private String preventStopAwsTag;
99101

100102
private String maxIdleMinutes;
101103

@@ -186,23 +188,32 @@ public void setAltEC2Endpoint(String altEC2Endpoint) {
186188
this.altEC2Endpoint = altEC2Endpoint;
187189
}
188190

189-
public String getNodeTagForEc2() {
190-
return nodeTagForEc2;
191+
public String getNodeLabelForEc2() {
192+
return nodeLabelForEc2;
193+
}
194+
195+
@DataBoundSetter
196+
public void setNodeLabelForEc2(String nodeLabelForEc2 ) {
197+
this.nodeLabelForEc2 = nodeLabelForEc2;
198+
}
199+
200+
public String getPreventStopAwsTag() {
201+
return preventStopAwsTag;
191202
}
192203

193204
@DataBoundSetter
194-
public void setNodeTagForEc2(String nodeTagForEc2) {
195-
this.nodeTagForEc2 = nodeTagForEc2;
205+
public void setPreventStopAwsTag( String preventStopAwsTag ) {
206+
this.preventStopAwsTag = preventStopAwsTag;
196207
}
197208

198209
public boolean isEc2Node(Node node) {
199210
//If no label is specified then we check all nodes
200-
if (nodeTagForEc2 == null || nodeTagForEc2.trim().length() == 0) {
211+
if ( nodeLabelForEc2 == null || nodeLabelForEc2.trim().length() == 0) {
201212
return true;
202213
}
203214

204215
for (LabelAtom label : node.getAssignedLabels()) {
205-
if (label.getExpression().equalsIgnoreCase(nodeTagForEc2)) {
216+
if (label.getExpression().equalsIgnoreCase( nodeLabelForEc2 )) {
206217
return true;
207218
}
208219
}
@@ -281,13 +292,17 @@ public void stopNode(Node node) {
281292

282293
final String instanceId = nodeInstance.getInstanceId();
283294

284-
try {
285-
StopInstancesRequest request = new StopInstancesRequest();
286-
request.setInstanceIds(Collections.singletonList(instanceId));
287-
connect().stopInstances(request);
288-
LOGGER.log(Level.INFO, "Stopped instance: {0}", instanceId);
289-
} catch (Exception e) {
290-
LOGGER.log(Level.INFO, "Unable to stop instance: " + instanceId, e);
295+
if (stopAllowed( nodeInstance )) {
296+
try {
297+
StopInstancesRequest request = new StopInstancesRequest();
298+
request.setInstanceIds( Collections.singletonList( instanceId ) );
299+
connect().stopInstances( request );
300+
LOGGER.log( Level.INFO, "Stopped instance: {0}", instanceId );
301+
} catch ( Exception e ) {
302+
LOGGER.log( Level.INFO, "Unable to stop instance: " + instanceId, e );
303+
}
304+
} else {
305+
LOGGER.log( Level.FINEST, "Not allowed to stop node: {0}", instanceId);
291306
}
292307
}
293308

@@ -339,6 +354,18 @@ private Instance getInstance(List<Filter> filters, InstanceStateName desiredStat
339354
return null;
340355
}
341356

357+
private boolean stopAllowed(Instance instance) {
358+
List<Tag> tags = instance.getTags();
359+
if (tags != null) {
360+
for ( Tag tag : tags) {
361+
if (tag.getKey().trim().equals( preventStopAwsTag )) {
362+
return false;
363+
}
364+
}
365+
}
366+
return true;
367+
}
368+
342369
@Extension
343370
public static class DescriptorImpl extends EC2Cloud.DescriptorImpl {
344371

src/main/resources/hudson/plugins/ec2/AmazonEC2Cloud/config-entries.jelly

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ THE SOFTWARE.
5555
<f:entry title="${%Session Name}" field="roleSessionName">
5656
<f:textbox />
5757
</f:entry>
58-
<f:entry title="${%Stop/Start Jenkins node role}" field="nodeTagForEc2">
58+
<f:entry title="${%Stop/Start Jenkins node label}" field="nodeLabelForEc2">
5959
<f:textbox default="ec2" />
6060
</f:entry>
6161
<f:entry title="${%Stop/Start EC2 Instance Tag}" field="instanceTagForJenkins">
6262
<f:textbox />
6363
</f:entry>
64+
<f:entry title="${%Prevent Stop EC2 Instance Tag}" field="preventStopAwsTag">
65+
<f:textbox default="prevent_stop"/>
66+
</f:entry>
6467
<f:entry title="${%Max minutes instance idle}" field="maxIdleMinutes">
6568
<f:textbox default="15" />
6669
</f:entry>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Marker tag used on EC2 instances that should not be stopped by this plugin.
2+
Tag value is not checked as the presence of the tag is enough to prevent the plugin from stopping the instance.

0 commit comments

Comments
 (0)