Skip to content

Commit

Permalink
Merge pull request #766 from lemeurherve/JENKINS-69304-java-path
Browse files Browse the repository at this point in the history
[JENKINS-69304] Allow specifying a java path
  • Loading branch information
res0nance authored Aug 25, 2022
2 parents 1a236e4 + 1efef11 commit 33e4f99
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 38 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def slaveTemplateUsEast1Parameters = [
idleTerminationMinutes: '5',
initScript: '',
instanceCapStr: '2',
javaPath: 'java',
jvmopts: '',
labelString: 'aws.ec2.us.east.jenkins.worker',
launchTimeoutStr: '',
Expand Down Expand Up @@ -419,6 +420,7 @@ SlaveTemplate slaveTemplateUsEast1 = new SlaveTemplate(
slaveTemplateUsEast1Parameters.numExecutors,
slaveTemplateUsEast1Parameters.remoteAdmin,
slaveTemplateUsEast1Parameters.unixData,
slaveTemplateUsEast1Parameters.javaPath,
slaveTemplateUsEast1Parameters.jvmopts,
slaveTemplateUsEast1Parameters.stopOnTerminate,
slaveTemplateUsEast1Parameters.subnetId,
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public abstract class EC2AbstractSlave extends Slave {
public static final Boolean DEFAULT_METADATA_ENDPOINT_ENABLED = Boolean.TRUE;
public static final Boolean DEFAULT_METADATA_TOKENS_REQUIRED = Boolean.FALSE;
public static final Integer DEFAULT_METADATA_HOPS_LIMIT = 1;
public static final String DEFAULT_JAVA_PATH = "java";

private static final Logger LOGGER = Logger.getLogger(EC2AbstractSlave.class.getName());

Expand All @@ -93,6 +94,7 @@ public abstract class EC2AbstractSlave extends Slave {

public final String templateDescription;

public final String javaPath;
public final String jvmopts; // e.g. -Xmx1g
public final boolean stopOnTerminate;
public final String idleTerminationMinutes;
Expand Down Expand Up @@ -152,7 +154,7 @@ public abstract class EC2AbstractSlave extends Slave {

public static final String TEST_ZONE = "testZone";

public EC2AbstractSlave(String name, String instanceId, String templateDescription, String remoteFS, int numExecutors, Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy<EC2Computer> retentionStrategy, String initScript, String tmpDir, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String jvmopts, boolean stopOnTerminate, String idleTerminationMinutes, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses, Tenancy tenancy,
public EC2AbstractSlave(String name, String instanceId, String templateDescription, String remoteFS, int numExecutors, Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy<EC2Computer> retentionStrategy, String initScript, String tmpDir, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String javaPath, String jvmopts, boolean stopOnTerminate, String idleTerminationMinutes, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses, Tenancy tenancy,
Boolean metadataEndpointEnabled, Boolean metadataTokensRequired, Integer metadataHopsLimit)
throws FormException, IOException {
super(name, remoteFS, launcher);
Expand All @@ -167,6 +169,7 @@ public EC2AbstractSlave(String name, String instanceId, String templateDescripti
this.initScript = initScript;
this.tmpDir = tmpDir;
this.remoteAdmin = remoteAdmin;
this.javaPath = javaPath;
this.jvmopts = jvmopts;
this.stopOnTerminate = stopOnTerminate;
this.idleTerminationMinutes = idleTerminationMinutes;
Expand All @@ -187,7 +190,7 @@ public EC2AbstractSlave(String name, String instanceId, String templateDescripti
@Deprecated
public EC2AbstractSlave(String name, String instanceId, String templateDescription, String remoteFS, int numExecutors, Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy<EC2Computer> retentionStrategy, String initScript, String tmpDir, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String jvmopts, boolean stopOnTerminate, String idleTerminationMinutes, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses, Tenancy tenancy)
throws FormException, IOException {
this(name, instanceId, templateDescription, remoteFS, numExecutors, mode, labelString, launcher, retentionStrategy, initScript, tmpDir, nodeProperties, remoteAdmin, jvmopts, stopOnTerminate, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses, tenancy, DEFAULT_METADATA_ENDPOINT_ENABLED, DEFAULT_METADATA_TOKENS_REQUIRED, DEFAULT_METADATA_HOPS_LIMIT);
this(name, instanceId, templateDescription, remoteFS, numExecutors, mode, labelString, launcher, retentionStrategy, initScript, tmpDir, nodeProperties, remoteAdmin, DEFAULT_JAVA_PATH, jvmopts, stopOnTerminate, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses, tenancy, DEFAULT_METADATA_ENDPOINT_ENABLED, DEFAULT_METADATA_TOKENS_REQUIRED, DEFAULT_METADATA_HOPS_LIMIT);

}

Expand Down Expand Up @@ -557,6 +560,10 @@ String getSlaveCommandSuffix() {
return " " + commandSuffix;
}

String getJavaPath() {
return Util.fixNull(javaPath);
}

String getJvmopts() {
return Util.fixNull(jvmopts);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/ec2/EC2OndemandSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ public EC2OndemandSlave(String name, String instanceId, String templateDescripti
@Deprecated
public EC2OndemandSlave(String name, String instanceId, String templateDescription, String remoteFS, int numExecutors, String labelString, Mode mode, String initScript, String tmpDir, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String jvmopts, boolean stopOnTerminate, String idleTerminationMinutes, String publicDNS, String privateDNS, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses, Tenancy tenancy)
throws FormException, IOException {
this(name, instanceId, templateDescription, remoteFS, numExecutors, labelString, mode, initScript, tmpDir, nodeProperties, remoteAdmin, jvmopts, stopOnTerminate, idleTerminationMinutes, publicDNS, privateDNS, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses, tenancy, DEFAULT_METADATA_ENDPOINT_ENABLED, DEFAULT_METADATA_TOKENS_REQUIRED, DEFAULT_METADATA_HOPS_LIMIT);
this(name, instanceId, templateDescription, remoteFS, numExecutors, labelString, mode, initScript, tmpDir, nodeProperties, remoteAdmin, DEFAULT_JAVA_PATH, jvmopts, stopOnTerminate, idleTerminationMinutes, publicDNS, privateDNS, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses, tenancy, DEFAULT_METADATA_ENDPOINT_ENABLED, DEFAULT_METADATA_TOKENS_REQUIRED, DEFAULT_METADATA_HOPS_LIMIT);
}

@DataBoundConstructor
public EC2OndemandSlave(String name, String instanceId, String templateDescription, String remoteFS, int numExecutors, String labelString, Mode mode, String initScript, String tmpDir, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String jvmopts, boolean stopOnTerminate, String idleTerminationMinutes, String publicDNS, String privateDNS, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses, Tenancy tenancy,
public EC2OndemandSlave(String name, String instanceId, String templateDescription, String remoteFS, int numExecutors, String labelString, Mode mode, String initScript, String tmpDir, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String javaPath, String jvmopts, boolean stopOnTerminate, String idleTerminationMinutes, String publicDNS, String privateDNS, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses, Tenancy tenancy,
Boolean metadataEndpointEnabled, Boolean metadataTokensRequired, Integer metadataHopsLimit)
throws FormException, IOException {

super(name, instanceId, templateDescription, remoteFS, numExecutors, mode, labelString, (amiType.isWindows() ? new EC2WindowsLauncher() : (amiType.isMac() ? new EC2MacLauncher():
new EC2UnixLauncher())), new EC2RetentionStrategy(idleTerminationMinutes), initScript, tmpDir, nodeProperties, remoteAdmin, jvmopts, stopOnTerminate, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses, tenancy, metadataEndpointEnabled, metadataTokensRequired, metadataHopsLimit);
new EC2UnixLauncher())), new EC2RetentionStrategy(idleTerminationMinutes), initScript, tmpDir, nodeProperties, remoteAdmin, javaPath, jvmopts, stopOnTerminate, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses, tenancy, metadataEndpointEnabled, metadataTokensRequired, metadataHopsLimit);

this.publicDNS = publicDNS;
this.privateDNS = privateDNS;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/ec2/EC2SpotSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public EC2SpotSlave(String name, String spotInstanceRequestId, String templateDe
@Deprecated
public EC2SpotSlave(String name, String spotInstanceRequestId, String templateDescription, String remoteFS, int numExecutors, Mode mode, String initScript, String tmpDir, String labelString, String remoteAdmin, String jvmopts, String idleTerminationMinutes, List<EC2Tag> tags, String cloudName, boolean usePrivateDnsName, int launchTimeout, AMITypeData amiType)
throws FormException, IOException {
this(templateDescription + " (" + name + ")", spotInstanceRequestId, templateDescription, remoteFS, numExecutors, mode, initScript, tmpDir, labelString, Collections.emptyList(), remoteAdmin, jvmopts, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, ConnectionStrategy.backwardsCompatible(usePrivateDnsName, false, false), -1);
this(templateDescription + " (" + name + ")", spotInstanceRequestId, templateDescription, remoteFS, numExecutors, mode, initScript, tmpDir, labelString, Collections.emptyList(), remoteAdmin, DEFAULT_JAVA_PATH, jvmopts, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, ConnectionStrategy.backwardsCompatible(usePrivateDnsName, false, false), -1);
}

@DataBoundConstructor
public EC2SpotSlave(String name, String spotInstanceRequestId, String templateDescription, String remoteFS, int numExecutors, Mode mode, String initScript, String tmpDir, String labelString, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String jvmopts, String idleTerminationMinutes, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses)
public EC2SpotSlave(String name, String spotInstanceRequestId, String templateDescription, String remoteFS, int numExecutors, Mode mode, String initScript, String tmpDir, String labelString, List<? extends NodeProperty<?>> nodeProperties, String remoteAdmin, String javaPath, String jvmopts, String idleTerminationMinutes, List<EC2Tag> tags, String cloudName, int launchTimeout, AMITypeData amiType, ConnectionStrategy connectionStrategy, int maxTotalUses)
throws FormException, IOException {

super(name, "", templateDescription, remoteFS, numExecutors, mode, labelString, amiType.isWindows() ? new EC2WindowsLauncher() :
new EC2UnixLauncher(), new EC2RetentionStrategy(idleTerminationMinutes), initScript, tmpDir, nodeProperties, remoteAdmin, jvmopts, false, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses,null);
new EC2UnixLauncher(), new EC2RetentionStrategy(idleTerminationMinutes), initScript, tmpDir, nodeProperties, remoteAdmin, javaPath, jvmopts, false, idleTerminationMinutes, tags, cloudName, launchTimeout, amiType, connectionStrategy, maxTotalUses,null, DEFAULT_METADATA_ENDPOINT_ENABLED, DEFAULT_METADATA_TOKENS_REQUIRED, DEFAULT_METADATA_HOPS_LIMIT);

this.name = name;
this.spotInstanceRequestId = spotInstanceRequestId;
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_ENDPOINT_ENABLED;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_TOKENS_REQUIRED;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_HOPS_LIMIT;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_JAVA_PATH;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
Expand Down Expand Up @@ -177,6 +178,8 @@ public class SlaveTemplate implements Describable<SlaveTemplate> {

public final String remoteAdmin;

public final String javaPath;

public final String jvmopts;

public final String subnetId;
Expand Down Expand Up @@ -281,7 +284,7 @@ public class SlaveTemplate implements Describable<SlaveTemplate> {
@DataBoundConstructor
public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, String securityGroups, String remoteFS,
InstanceType type, boolean ebsOptimized, String labelString, Node.Mode mode, String description, String initScript,
String tmpDir, String userData, String numExecutors, String remoteAdmin, AMITypeData amiType, String jvmopts,
String tmpDir, String userData, String numExecutors, String remoteAdmin, AMITypeData amiType, String javaPath, String jvmopts,
boolean stopOnTerminate, String subnetId, List<EC2Tag> tags, String idleTerminationMinutes, int minimumNumberOfInstances,
int minimumNumberOfSpareInstances, String instanceCapStr, String iamInstanceProfile, boolean deleteRootOnTermination,
boolean useEphemeralDevices, String launchTimeoutStr, boolean associatePublicIp,
Expand Down Expand Up @@ -314,6 +317,13 @@ public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, Stri
this.userData = StringUtils.trimToEmpty(userData);
this.numExecutors = Util.fixNull(numExecutors).trim();
this.remoteAdmin = remoteAdmin;

if (StringUtils.isNotBlank(javaPath)) {
this.javaPath = javaPath;
} else {
this.javaPath = DEFAULT_JAVA_PATH;
}

this.jvmopts = jvmopts;
this.stopOnTerminate = stopOnTerminate;
this.subnetId = subnetId;
Expand Down Expand Up @@ -373,7 +383,7 @@ public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, Stri
List<? extends NodeProperty<?>> nodeProperties, HostKeyVerificationStrategyEnum hostKeyVerificationStrategy, Tenancy tenancy, EbsEncryptRootVolume ebsEncryptRootVolume) {
this(ami, zone, spotConfig, securityGroups, remoteFS,
type, ebsOptimized, labelString, mode, description, initScript,
tmpDir, userData, numExecutors, remoteAdmin, amiType, jvmopts,
tmpDir, userData, numExecutors, remoteAdmin, amiType, DEFAULT_JAVA_PATH, jvmopts,
stopOnTerminate, subnetId, tags, idleTerminationMinutes, minimumNumberOfInstances,
minimumNumberOfSpareInstances, instanceCapStr, iamInstanceProfile, deleteRootOnTermination,
useEphemeralDevices, launchTimeoutStr, associatePublicIp,
Expand Down Expand Up @@ -510,6 +520,7 @@ public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, Stri
monitoring, t2Unlimited, ConnectionStrategy.backwardsCompatible(usePrivateDnsName, connectUsingPublicIp, associatePublicIp), -1);
}

@Deprecated
public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, String securityGroups, String remoteFS,
InstanceType type, boolean ebsOptimized, String labelString, Node.Mode mode, String description, String initScript,
String tmpDir, String userData, String numExecutors, String remoteAdmin, AMITypeData amiType, String jvmopts,
Expand All @@ -524,6 +535,7 @@ public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, Stri
connectUsingPublicIp, false, false);
}

@Deprecated
public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, String securityGroups, String remoteFS,
InstanceType type, boolean ebsOptimized, String labelString, Node.Mode mode, String description, String initScript,
String tmpDir, String userData, String numExecutors, String remoteAdmin, AMITypeData amiType, String jvmopts,
Expand All @@ -537,6 +549,7 @@ public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, Stri
useDedicatedTenancy, launchTimeoutStr, associatePublicIp, customDeviceMapping, connectBySSHProcess, false);
}

@Deprecated
public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, String securityGroups, String remoteFS,
InstanceType type, boolean ebsOptimized, String labelString, Node.Mode mode, String description, String initScript,
String tmpDir, String userData, String numExecutors, String remoteAdmin, AMITypeData amiType, String jvmopts,
Expand Down Expand Up @@ -1547,6 +1560,7 @@ protected EC2OndemandSlave newOndemandSlave(Instance inst) throws FormException,
.withTmpDir(tmpDir)
.withNodeProperties(nodeProperties.toList())
.withRemoteAdmin(remoteAdmin)
.withJavaPath(javaPath)
.withJvmopts(jvmopts)
.withStopOnTerminate(stopOnTerminate)
.withIdleTerminationMinutes(idleTerminationMinutes)
Expand Down Expand Up @@ -1579,6 +1593,7 @@ protected EC2SpotSlave newSpotSlave(SpotInstanceRequest sir) throws FormExceptio
.withLabelString(labels)
.withNodeProperties(nodeProperties.toList())
.withRemoteAdmin(remoteAdmin)
.withJavaPath(javaPath)
.withJvmopts(jvmopts)
.withIdleTerminationMinutes(idleTerminationMinutes)
.withTags(EC2Tag.fromAmazonTags(sir.getTags()))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ protected void launchScript(EC2Computer computer, TaskListener listener) throws
}

// TODO: parse the version number. maven-enforcer-plugin might help
executeRemote(computer, conn, "java -fullversion", "curl -L -O https://corretto.aws/downloads/latest/amazon-corretto-8-x64-macos-jdk.pkg; sudo installer -pkg amazon-corretto-8-x64-macos-jdk.pkg -target /", logger, listener);
final String javaPath = node.javaPath;
executeRemote(computer, conn, javaPath + " -fullversion", "curl -L -O https://corretto.aws/downloads/latest/amazon-corretto-8-x64-macos-jdk.pkg; sudo installer -pkg amazon-corretto-8-x64-macos-jdk.pkg -target /", logger, listener);

// Always copy so we get the most recent remoting.jar
logInfo(computer, listener, "Copying remoting.jar to: " + tmpDir);
Expand All @@ -232,7 +233,7 @@ protected void launchScript(EC2Computer computer, TaskListener listener) throws
final String suffix = computer.getSlaveCommandSuffix();
final String remoteFS = node.getRemoteFS();
final String workDir = Util.fixEmptyAndTrim(remoteFS) != null ? remoteFS : tmpDir;
String launchString = prefix + " java " + (jvmopts != null ? jvmopts : "") + " -jar " + tmpDir + "/remoting.jar -workDir " + workDir + suffix;
String launchString = prefix + " " + javaPath + " " + (jvmopts != null ? jvmopts : "") + " -jar " + tmpDir + "/remoting.jar -workDir " + workDir + suffix;
// launchString = launchString.trim();

SlaveTemplate slaveTemplate = computer.getSlaveTemplate();
Expand Down
Loading

0 comments on commit 33e4f99

Please sign in to comment.