|
36 | 36 | import com.amazonaws.services.ec2.model.Reservation;
|
37 | 37 | import com.amazonaws.services.ec2.model.StartInstancesRequest;
|
38 | 38 | import com.amazonaws.services.ec2.model.StopInstancesRequest;
|
| 39 | +import com.amazonaws.services.ec2.model.Tag; |
39 | 40 | import com.google.common.annotations.VisibleForTesting;
|
40 | 41 | import hudson.Extension;
|
41 | 42 | import hudson.Util;
|
|
63 | 64 | import org.kohsuke.stapler.DataBoundConstructor;
|
64 | 65 | import org.kohsuke.stapler.DataBoundSetter;
|
65 | 66 | import org.kohsuke.stapler.QueryParameter;
|
66 |
| -import org.kohsuke.stapler.StaplerResponse; |
67 | 67 | import org.kohsuke.stapler.interceptor.RequirePOST;
|
68 | 68 |
|
69 | 69 | /**
|
@@ -95,7 +95,9 @@ public class AmazonEC2Cloud extends EC2Cloud {
|
95 | 95 |
|
96 | 96 | private String instanceTagForJenkins;
|
97 | 97 |
|
98 |
| - private String nodeTagForEc2; |
| 98 | + private String nodeLabelForEc2; |
| 99 | + |
| 100 | + private String preventStopAwsTag; |
99 | 101 |
|
100 | 102 | private String maxIdleMinutes;
|
101 | 103 |
|
@@ -186,23 +188,32 @@ public void setAltEC2Endpoint(String altEC2Endpoint) {
|
186 | 188 | this.altEC2Endpoint = altEC2Endpoint;
|
187 | 189 | }
|
188 | 190 |
|
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; |
191 | 202 | }
|
192 | 203 |
|
193 | 204 | @DataBoundSetter
|
194 |
| - public void setNodeTagForEc2(String nodeTagForEc2) { |
195 |
| - this.nodeTagForEc2 = nodeTagForEc2; |
| 205 | + public void setPreventStopAwsTag( String preventStopAwsTag ) { |
| 206 | + this.preventStopAwsTag = preventStopAwsTag; |
196 | 207 | }
|
197 | 208 |
|
198 | 209 | public boolean isEc2Node(Node node) {
|
199 | 210 | //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) { |
201 | 212 | return true;
|
202 | 213 | }
|
203 | 214 |
|
204 | 215 | for (LabelAtom label : node.getAssignedLabels()) {
|
205 |
| - if (label.getExpression().equalsIgnoreCase(nodeTagForEc2)) { |
| 216 | + if (label.getExpression().equalsIgnoreCase( nodeLabelForEc2 )) { |
206 | 217 | return true;
|
207 | 218 | }
|
208 | 219 | }
|
@@ -281,13 +292,17 @@ public void stopNode(Node node) {
|
281 | 292 |
|
282 | 293 | final String instanceId = nodeInstance.getInstanceId();
|
283 | 294 |
|
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); |
291 | 306 | }
|
292 | 307 | }
|
293 | 308 |
|
@@ -339,6 +354,18 @@ private Instance getInstance(List<Filter> filters, InstanceStateName desiredStat
|
339 | 354 | return null;
|
340 | 355 | }
|
341 | 356 |
|
| 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 | + |
342 | 369 | @Extension
|
343 | 370 | public static class DescriptorImpl extends EC2Cloud.DescriptorImpl {
|
344 | 371 |
|
|
0 commit comments