Skip to content
Permalink
Browse files

JENKINS-32690 Make manually provision really work (change to EnumSets)

  • Loading branch information...
Francis Upton IV
Francis Upton IV committed May 8, 2016
1 parent 5949378 commit a5ab74708841a8828ece8136d70c82f9911b83d3
@@ -65,6 +65,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -495,12 +496,12 @@ private synchronized EC2AbstractSlave provisionSlaveIfPossible(SlaveTemplate tem
}

try {
int options = 0;
EnumSet<SlaveTemplate.ProvisionOptions> provisionOptions = EnumSet.noneOf(SlaveTemplate.ProvisionOptions.class);
if (forceCreateNew)
options = SlaveTemplate.PROVISION_FORCE_CREATE_NEW;
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.FORCE_CREATE);
else if (possibleSlavesCount > 0)
options = SlaveTemplate.PROVISION_ALLOW_CREATE_NEW;
return template.provision(StreamTaskListener.fromStdout(), options);
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.ALLOW_CREATE);
return template.provision(StreamTaskListener.fromStdout(), provisionOptions);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Exception during provisioning", e);
return null;
@@ -366,22 +366,20 @@ public String getIamInstanceProfile() {
return iamInstanceProfile;
}

public static final int PROVISION_ALLOW_CREATE_NEW = 0x01;
public static final int PROVISION_FORCE_CREATE_NEW = 0x02;

enum ProvisionOptions { ALLOW_CREATE, FORCE_CREATE }

/**
* Provisions a new EC2 slave or starts a previously stopped on-demand instance.
*
* @return always non-null. This needs to be then added to {@link Hudson#addNode(Node)}.
*/
public EC2AbstractSlave provision(TaskListener listener, int options) throws AmazonClientException, IOException {
public EC2AbstractSlave provision(TaskListener listener, EnumSet<ProvisionOptions> provisionOptions) throws AmazonClientException, IOException {
if (this.spotConfig != null) {
if (options != 0)
if (provisionOptions.contains(ProvisionOptions.ALLOW_CREATE) || provisionOptions.contains(ProvisionOptions.FORCE_CREATE))
return provisionSpot(listener);
return null;
}
return provisionOndemand(listener, options);
return provisionOndemand(listener, provisionOptions);
}

private boolean checkInstance(PrintStream logger, Instance existingInstance, EC2AbstractSlave[] returnNode) {
@@ -442,7 +440,7 @@ private void logProvisionInfo(PrintStream logger, String message) {
/**
* Provisions an On-demand EC2 slave by launching a new instance or starting a previously-stopped instance.
*/
private EC2AbstractSlave provisionOndemand(TaskListener listener, int options) throws AmazonClientException, IOException {
private EC2AbstractSlave provisionOndemand(TaskListener listener, EnumSet<ProvisionOptions> provisionOptions) throws AmazonClientException, IOException {
PrintStream logger = listener.getLogger();
AmazonEC2 ec2 = getParent().connect();

@@ -557,7 +555,7 @@ private EC2AbstractSlave provisionOndemand(TaskListener listener, int options) t
DescribeInstancesResult diResult = ec2.describeInstances(diRequest);
EC2AbstractSlave[] ec2Node = new EC2AbstractSlave[1];
Instance existingInstance = null;
if ((options & PROVISION_FORCE_CREATE_NEW) == 0) {
if (!provisionOptions.contains(ProvisionOptions.FORCE_CREATE)) {
reservationLoop:
for (Reservation reservation : diResult.getReservations()) {
for (Instance instance : reservation.getInstances()) {
@@ -571,7 +569,8 @@ private EC2AbstractSlave provisionOndemand(TaskListener listener, int options) t
}

if (existingInstance == null) {
if ((options & (PROVISION_ALLOW_CREATE_NEW | PROVISION_FORCE_CREATE_NEW)) == 0) {
if (!provisionOptions.contains(ProvisionOptions.FORCE_CREATE) &&
!provisionOptions.contains(ProvisionOptions.ALLOW_CREATE)) {
logProvision(logger, "No existing instance found - but cannot create new instance");
return null;
}

0 comments on commit a5ab747

Please sign in to comment.
You can’t perform that action at this time.