4
4
import com .amazon .jenkins .ec2fleet .aws .RegionHelper ;
5
5
import com .amazon .jenkins .ec2fleet .fleet .EC2Fleet ;
6
6
import com .amazon .jenkins .ec2fleet .fleet .EC2Fleets ;
7
- import com .amazon .jenkins .ec2fleet .utils .EC2FleetCloudAwareUtils ;
8
7
import com .amazonaws .services .ec2 .AmazonEC2 ;
9
8
import com .amazonaws .services .ec2 .model .Instance ;
10
9
import com .amazonaws .services .ec2 .model .InstanceStateName ;
13
12
import hudson .Extension ;
14
13
import hudson .model .Computer ;
15
14
import hudson .model .Descriptor ;
15
+ import hudson .model .Failure ;
16
16
import hudson .model .Label ;
17
17
import hudson .model .Node ;
18
18
import hudson .model .Queue ;
@@ -86,25 +86,6 @@ public class EC2FleetCloud extends AbstractEC2FleetCloud {
86
86
private static final Logger LOGGER = Logger .getLogger (EC2FleetCloud .class .getName ());
87
87
private static final ScheduledExecutorService EXECUTOR = Executors .newSingleThreadScheduledExecutor ();
88
88
89
- // Counter to keep track of planned nodes per EC2FleetCloud, used in node's display name
90
- private transient AtomicInteger plannedNodeCounter = new AtomicInteger (1 );
91
-
92
- /**
93
- * Provide unique identifier for this instance of {@link EC2FleetCloud}, <code>transient</code>
94
- * will not be stored. Not available for customer, instead use {@link EC2FleetCloud#name}
95
- * will be used only during Jenkins configuration update <code>config.jelly</code>,
96
- * when new instance of same cloud is created and we need to find old instance and
97
- * repoint resources like {@link Computer} {@link Node} etc.
98
- * <p>
99
- * It's lazy to support old versions which don't have this field at all.
100
- * <p>
101
- * However it's stable, as soon as it will be created and called first uuid will be same
102
- * for all future calls to the same instances of lazy uuid.
103
- *
104
- * @see EC2FleetCloudAware
105
- */
106
- private transient LazyUuid id ;
107
-
108
89
/**
109
90
* Replaced with {@link EC2FleetCloud#awsCredentialsId}
110
91
* <p>
@@ -178,9 +159,11 @@ public class EC2FleetCloud extends AbstractEC2FleetCloud {
178
159
179
160
private transient ArrayList <ScheduledFuture <?>> plannedNodeScheduledFutures ;
180
161
162
+ // Counter to keep track of planned nodes per EC2FleetCloud, used in node's display name
163
+ private transient AtomicInteger plannedNodeCounter = new AtomicInteger (1 );
164
+
181
165
@ DataBoundConstructor
182
166
public EC2FleetCloud (final String name ,
183
- final String oldId ,
184
167
final String awsCredentialsId ,
185
168
final @ Deprecated String credentialsId ,
186
169
final String region ,
@@ -238,8 +221,6 @@ public EC2FleetCloud(final String name,
238
221
if (fleet != null ) {
239
222
this .stats = EC2Fleets .get (fleet ).getState (
240
223
getAwsCredentialsId (), region , endpoint , getFleet ());
241
- // Reassign existing nodes/computer with new reference of cloud
242
- EC2FleetCloudAwareUtils .reassign (fleet , this );
243
224
}
244
225
}
245
226
@@ -259,16 +240,6 @@ public String getAwsCredentialsId() {
259
240
return StringUtils .isNotBlank (awsCredentialsId ) ? awsCredentialsId : credentialsId ;
260
241
}
261
242
262
- /**
263
- * Called old as will be used by new instance of cloud, for
264
- * which this id is old (not current)
265
- *
266
- * @return id of current cloud
267
- */
268
- public String getOldId () {
269
- return id .getValue ();
270
- }
271
-
272
243
public boolean isDisableTaskResubmit () {
273
244
return disableTaskResubmit ;
274
245
}
@@ -298,7 +269,6 @@ public String getEndpoint() {
298
269
return endpoint ;
299
270
}
300
271
301
- @ Override
302
272
public String getFleet () {
303
273
return fleet ;
304
274
}
@@ -419,6 +389,15 @@ private synchronized int getNextPlannedNodeCounter() {
419
389
420
390
@ Override
421
391
public synchronized Collection <NodeProvisioner .PlannedNode > provision (@ Nonnull final Cloud .CloudState cloudState , final int excessWorkload ) {
392
+ Jenkins jenkinsInstance = Jenkins .get ();
393
+ if (jenkinsInstance .isQuietingDown ()) {
394
+ LOGGER .log (Level .FINE , "Not provisioning nodes, Jenkins instance is quieting down" );
395
+ return Collections .emptyList ();
396
+ } else if (jenkinsInstance .isTerminating ()) {
397
+ LOGGER .log (Level .FINE , "Not provisioning nodes, Jenkins instance is terminating" );
398
+ return Collections .emptyList ();
399
+ }
400
+
422
401
fine ("excessWorkload %s" , excessWorkload );
423
402
424
403
if (stats == null ) {
@@ -469,6 +448,7 @@ public synchronized Collection<NodeProvisioner.PlannedNode> provision(@Nonnull f
469
448
// This protects us from leaving planned nodes stranded within Jenkins NodeProvisioner when the Fleet
470
449
// is updated or removed before it can scale. After scaling, EC2FleetOnlineChecker will cancel the future
471
450
// if something happens to the Fleet.
451
+ // TODO: refactor to consolidate logic with EC2FleetOnlineChecker
472
452
final ScheduledFuture <?> scheduledFuture = EXECUTOR .schedule (() -> {
473
453
if (completableFuture .isDone ()) {
474
454
return ;
@@ -644,7 +624,7 @@ public void run() {
644
624
645
625
final Set <String > jenkinsInstances = new HashSet <>();
646
626
for (final Node node : jenkins .getNodes ()) {
647
- if (node instanceof EC2FleetNode && ((EC2FleetNode ) node ).getCloud ().getFleet ().equals (fleet )) {
627
+ if (node instanceof EC2FleetNode && ((EC2FleetCloud )(( EC2FleetNode ) node ).getCloud () ).getFleet ().equals (fleet )) {
648
628
jenkinsInstances .add (node .getNodeName ());
649
629
}
650
630
}
@@ -776,7 +756,6 @@ public synchronized boolean scheduleToTerminate(final String instanceId, final b
776
756
@ Override
777
757
public boolean canProvision (final Cloud .CloudState cloudState ) {
778
758
final Label label = cloudState .getLabel ();
779
- fine ("CanProvision called on fleet: \" " + this .labelString + "\" wanting: \" " + (label == null ? "(unspecified)" : label .getName ()) + "\" ." );
780
759
if (fleet == null ) {
781
760
fine ("Fleet/ASG for cloud is null, returning false" );
782
761
return false ;
@@ -786,7 +765,7 @@ public boolean canProvision(final Cloud.CloudState cloudState) {
786
765
return false ;
787
766
}
788
767
if (label != null && !Label .parse (this .labelString ).containsAll (label .listAtoms ())) {
789
- fine ("Label '%s' not found within Fleet's labels '%s', returning false" , label , this .labelString );
768
+ finer ("Label '%s' not found within Fleet's labels '%s', returning false" , label , this .labelString );
790
769
return false ;
791
770
}
792
771
return true ;
@@ -798,8 +777,6 @@ private Object readResolve() {
798
777
}
799
778
800
779
private void init () {
801
- id = new LazyUuid ();
802
-
803
780
plannedNodesCache = new HashSet <>();
804
781
instanceIdsToTerminate = new HashMap <>();
805
782
plannedNodeScheduledFutures = new ArrayList <>();
@@ -864,7 +841,7 @@ private void addNewAgent(final AmazonEC2 ec2, final Instance instance, FleetStat
864
841
final Node .Mode nodeMode = restrictUsage ? Node .Mode .EXCLUSIVE : Node .Mode .NORMAL ;
865
842
final EC2FleetNode node = new EC2FleetNode (instanceId , "Fleet agent for " + instanceId ,
866
843
effectiveFsRoot , effectiveNumExecutors , nodeMode , labelString , new ArrayList <NodeProperty <?>>(),
867
- this , computerLauncher , maxTotalUses );
844
+ this . name , computerLauncher , maxTotalUses );
868
845
869
846
// Initialize our retention strategy
870
847
node .setRetentionStrategy (new EC2RetentionStrategy ());
@@ -905,7 +882,7 @@ private int getCurrentSpareInstanceCount(final FleetStateStats currentState, fin
905
882
}
906
883
907
884
// Do not count computer if it is not a part of the given fleet
908
- if (!Objects .equals (((EC2FleetNodeComputer ) computer ).getCloud ().getFleet (), currentState .getFleetId ())) {
885
+ if (!Objects .equals (((EC2FleetCloud )(( EC2FleetNodeComputer ) computer ).getCloud () ).getFleet (), currentState .getFleetId ())) {
909
886
continue ;
910
887
}
911
888
currentBusyInstances ++;
@@ -928,6 +905,10 @@ private void fine(final String msg, final Object... args) {
928
905
LOGGER .fine (getLogPrefix () + String .format (msg , args ));
929
906
}
930
907
908
+ private void finer (final String msg , final Object ... args ) {
909
+ LOGGER .finer (getLogPrefix () + String .format (msg , args ));
910
+ }
911
+
931
912
private void warning (final String msg , final Object ... args ) {
932
913
LOGGER .warning (getLogPrefix () + String .format (msg , args ));
933
914
}
@@ -936,6 +917,11 @@ private void warning(final Throwable t, final String msg, final Object... args)
936
917
LOGGER .log (Level .WARNING , getLogPrefix () + String .format (msg , args ), t );
937
918
}
938
919
920
+ @ Override
921
+ public DescriptorImpl getDescriptor () {
922
+ return (DescriptorImpl ) super .getDescriptor ();
923
+ }
924
+
939
925
@ Extension
940
926
@ SuppressWarnings ("unused" )
941
927
public static class DescriptorImpl extends Descriptor <Cloud > {
@@ -972,6 +958,15 @@ public FormValidation doCheckMaxTotalUses(@QueryParameter String value) {
972
958
return FormValidation .error ("Maximum Total Uses must be greater or equal to -1" );
973
959
}
974
960
961
+ public FormValidation doCheckCloudName (@ QueryParameter final String name ) {
962
+ try {
963
+ Jenkins .checkGoodName (name );
964
+ } catch (Failure e ) {
965
+ return FormValidation .error (e .getMessage ());
966
+ }
967
+ return FormValidation .ok ();
968
+ }
969
+
975
970
public ListBoxModel doFillFleetItems (@ QueryParameter final boolean showAllFleets ,
976
971
@ QueryParameter final String region ,
977
972
@ QueryParameter final String endpoint ,
@@ -985,7 +980,7 @@ public ListBoxModel doFillFleetItems(@QueryParameter final boolean showAllFleets
985
980
awsCredentialsId , region , endpoint , model , fleet , showAllFleets );
986
981
}
987
982
} catch (final Exception ex ) {
988
- LOGGER .log (Level .WARNING , String .format ("Cannot describe fleets in %s or by endpoint %s " , region , endpoint ), ex );
983
+ LOGGER .log (Level .WARNING , String .format ("Cannot describe fleets in '%s' or by endpoint '%s' " , region , endpoint ), ex );
989
984
return model ;
990
985
}
991
986
@@ -1026,7 +1021,5 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) th
1026
1021
save ();
1027
1022
return super .configure (req , formData );
1028
1023
}
1029
-
1030
1024
}
1031
-
1032
1025
}
0 commit comments