diff --git a/pom.xml b/pom.xml index 2e860acb..825b2df4 100644 --- a/pom.xml +++ b/pom.xml @@ -25,11 +25,11 @@ - 2 + 3 999999-SNAPSHOT 2.479 ${jenkins.baseline}.1 - 2.0.0 + 3.0.0 diff --git a/src/main/java/hudson/plugins/sshslaves/DefaultJavaProvider.java b/src/main/java/hudson/plugins/sshslaves/DefaultJavaProvider.java deleted file mode 100644 index 991bee5c..00000000 --- a/src/main/java/hudson/plugins/sshslaves/DefaultJavaProvider.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2004-, all the contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package hudson.plugins.sshslaves; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import com.trilead.ssh2.Connection; -import hudson.EnvVars; -import hudson.Extension; -import hudson.model.Descriptor; -import hudson.model.JDK; -import hudson.model.Node; -import hudson.model.TaskListener; -import hudson.slaves.EnvironmentVariablesNodeProperty; -import hudson.slaves.NodeProperty; -import hudson.slaves.SlaveComputer; -import hudson.tools.ToolLocationNodeProperty; -import hudson.tools.ToolLocationNodeProperty.ToolLocation; -import jenkins.model.Jenkins; - -/** - * Class to try to guess where is java. - * This is the list of places where it will try to find java: - *

- * - */ -@Extension -@Deprecated -public class DefaultJavaProvider extends JavaProvider { - - public static final String JAVA_HOME = "JAVA_HOME"; - public static final String BIN_JAVA = "/bin/java"; - public static final String JDK_BIN_JAVA = "/jdk/bin/java"; - - @Override - public List getJavas(SlaveComputer computer, TaskListener listener, Connection connection) { - List javas = new ArrayList<>(); - - String workingDirectory = SSHLauncher.getWorkingDirectory(computer); - if (workingDirectory != null) { - javas.add(workingDirectory + JDK_BIN_JAVA); - } - - final Node node = computer.getNode(); - javas.addAll(lookForJavaHome(node)); - javas.addAll(lookForTools(node)); - javas.addAll(Arrays.asList("java", - "/usr/bin/java", - "/usr/java/default/bin/java", - "/usr/java/latest/bin/java", - "/usr/local/bin/java", - "/usr/local/java/bin/java")); - return javas; - } - - private List lookForJavaHome(Node node) { - List ret = new ArrayList<>(); - if(node != null && node.getNodeProperties() != null){ - for (NodeProperty property : node.getNodeProperties()){ - if(property instanceof EnvironmentVariablesNodeProperty){ - EnvVars env = ((EnvironmentVariablesNodeProperty) property).getEnvVars(); - if (env != null && env.containsKey(JAVA_HOME)) { - ret.add(quoteIfHasWhiteSpaces(env.get(JAVA_HOME) + BIN_JAVA)); - } - } - } - } - return ret; - } - - private List lookForTools(Node node) { - List ret = new ArrayList<>(); - Descriptor jdk = Jenkins.getInstance().getDescriptorByType(JDK.DescriptorImpl.class); - if(node != null && node.getNodeProperties() != null){ - for (NodeProperty property : node.getNodeProperties()){ - if (property instanceof ToolLocationNodeProperty) { - for (ToolLocation tool : ((ToolLocationNodeProperty) property).getLocations()) { - if (tool.getType() == jdk) { - ret.add(quoteIfHasWhiteSpaces(tool.getHome() + BIN_JAVA)); - } - } - } - } - } - return ret; - } - - private String quoteIfHasWhiteSpaces(String path){ - return path.contains(" ") ? "\"" + path + "\"" : path; - } -} diff --git a/src/main/java/hudson/plugins/sshslaves/JavaProvider.java b/src/main/java/hudson/plugins/sshslaves/JavaProvider.java deleted file mode 100644 index fa738765..00000000 --- a/src/main/java/hudson/plugins/sshslaves/JavaProvider.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2004-, all the contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package hudson.plugins.sshslaves; - -import com.trilead.ssh2.Connection; -import edu.umd.cs.findbugs.annotations.NonNull; -import hudson.ExtensionList; -import hudson.ExtensionPoint; -import hudson.slaves.SlaveComputer; -import hudson.model.TaskListener; -import hudson.util.VersionNumber; - -import java.util.List; -import java.util.Collections; - -/** - * Guess where Java is. - */ -public abstract class JavaProvider implements ExtensionPoint { - - private static final VersionNumber JAVA_LEVEL_8 = new VersionNumber("8"); - - /** - * @deprecated - * Override {@link #getJavas(SlaveComputer, TaskListener, Connection)} instead. - */ - public List getJavas(TaskListener listener, Connection connection) { - return Collections.emptyList(); - } - - /** - * Returns the list of possible places where java executable might exist. - * - * @return - * Can be empty but never null. Absolute path to the possible locations of Java. - */ - public List getJavas(SlaveComputer computer, TaskListener listener, Connection connection) { - return getJavas(listener,connection); - } - - /** - * All regsitered instances. - */ - public static ExtensionList all() { - return ExtensionList.lookup(JavaProvider.class); - } - - /** - * Gets minimal required Java version. - * - * @return Minimal Java version required on the controller and agent side. - * @since TODO - * - */ - @NonNull - public static VersionNumber getMinJavaLevel() { - return JAVA_LEVEL_8; - } -} diff --git a/src/main/java/hudson/plugins/sshslaves/JavaVersionChecker.java b/src/main/java/hudson/plugins/sshslaves/JavaVersionChecker.java deleted file mode 100644 index 04a1bbf6..00000000 --- a/src/main/java/hudson/plugins/sshslaves/JavaVersionChecker.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2004-, all the contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package hudson.plugins.sshslaves; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintStream; -import java.io.StringWriter; -import java.nio.charset.Charset; -import java.text.NumberFormat; -import java.text.ParseException; -import java.util.Locale; -import java.util.logging.Logger; -import com.trilead.ssh2.Connection; -import edu.umd.cs.findbugs.annotations.CheckForNull; -import edu.umd.cs.findbugs.annotations.NonNull; -import org.kohsuke.accmod.Restricted; -import org.kohsuke.accmod.restrictions.NoExternalUse; -import hudson.model.TaskListener; -import hudson.slaves.SlaveComputer; -import hudson.util.VersionNumber; -import static java.util.logging.Level.FINE; - -/** - * class to check if the version of java installed on the agent is a supported one. - */ -@Deprecated -public class JavaVersionChecker { - private static final Logger LOGGER = Logger.getLogger(JavaVersionChecker.class.getName()); - - private final SlaveComputer computer; - private final TaskListener listener; - private final String jvmOptions; - private final Connection connection; - - public JavaVersionChecker(SlaveComputer computer, TaskListener listener, String jvmOptions, Connection connection) { - this.computer = computer; - this.listener = listener; - this.jvmOptions = jvmOptions; - this.connection = connection; - } - - /** - * return javaPath if specified in the configuration. - * Finds local Java. - */ - protected String resolveJava() throws InterruptedException, IOException { - for (JavaProvider provider : JavaProvider.all()) { - for (String javaCommand : provider.getJavas(computer, listener, connection)) { - LOGGER.fine("Trying Java at " + javaCommand); - try { - return checkJavaVersion(listener, javaCommand); - } catch (IOException e) { - LOGGER.log(FINE, "Failed to check the Java version",e); - // try the next one - } - } - } - throw new IOException("Java not found on " + computer + ". Install Java 8 or Java 11 on the Agent."); - } - - @NonNull - private String checkJavaVersion(TaskListener listener, String javaCommand) throws IOException, InterruptedException { - listener.getLogger().println(Messages.SSHLauncher_CheckingDefaultJava(SSHLauncher.getTimestamp(),javaCommand)); - StringWriter output = new StringWriter(); // record output from Java - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - connection.exec(javaCommand + " "+ jvmOptions + " -version",out); - //TODO: Seems we need to retrieve the encoding from the connection destination - BufferedReader r = new BufferedReader(new InputStreamReader( - new ByteArrayInputStream(out.toByteArray()), Charset.defaultCharset())); - final String result = checkJavaVersion(listener.getLogger(), javaCommand, r, output); - - if(null == result) { - listener.getLogger().println(Messages.SSHLauncher_UnknownJavaVersion(javaCommand)); - listener.getLogger().println(output); - throw new IOException(Messages.SSHLauncher_UnknownJavaVersion(javaCommand)); - } else { - return result; - } - } - - /** - * Given the output of "java -version" in r, determine if this - * version of Java is supported. - * - * @param logger - * where to log the output - * @param javaCommand - * the command executed, used for logging - * @param r - * the output of "java -version" - * @param output - * copy the data from r into this output buffer - */ - @CheckForNull - @Restricted(NoExternalUse.class) - public String checkJavaVersion(final PrintStream logger, String javaCommand, - final BufferedReader r, final StringWriter output) throws IOException { - String line; - while (null != (line = r.readLine())) { - output.write(line); - output.write("\n"); - line = line.toLowerCase(Locale.ENGLISH); - if (line.startsWith("java version \"") || line.startsWith("openjdk version \"")) { - final String versionStr = line.substring(line.indexOf('\"') + 1, line.lastIndexOf('\"')); - logger.println(Messages.SSHLauncher_JavaVersionResult(SSHLauncher.getTimestamp(), javaCommand, versionStr)); - - // parse as a number and we should be OK as all we care about is up through the first dot. - final VersionNumber minJavaLevel = JavaProvider.getMinJavaLevel(); - try { - final Number version = NumberFormat.getNumberInstance(Locale.US).parse(versionStr); - //TODO: burn it with fire - if(version.doubleValue() < Double.parseDouble("1."+minJavaLevel)) { - throw new IOException(Messages.SSHLauncher_NoJavaFound2(line, minJavaLevel.toString())); - } - } catch(final ParseException e) { - throw new IOException(Messages.SSHLauncher_NoJavaFound2(line, minJavaLevel)); - } - return javaCommand; - } - } - return null; - } -} diff --git a/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java b/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java index cf3c3111..4d478784 100644 --- a/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java +++ b/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java @@ -448,15 +448,9 @@ public void launch(final SlaveComputer computer, final TaskListener listener) th return Boolean.FALSE; } - String java = null; + String java = "java"; if (StringUtils.isNotBlank(javaPath)) { - java = expandExpression(computer, javaPath); - } else { - checkJavaIsInPath(listener); - //FIXME deprecated on 2020-12-10, it will removed after 2021-09-01 - JavaVersionChecker javaVersionChecker = new JavaVersionChecker(computer, listener, getJvmOptions(), - connection); - java = javaVersionChecker.resolveJava(); + java = expandExpression(computer, javaPath); } copyAgentJar(listener, workingDirectory); diff --git a/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java b/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java index dfb09bba..b7ef1cbf 100644 --- a/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java +++ b/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java @@ -254,38 +254,6 @@ public void workDirTest() { Assert.assertEquals(launcher.getWorkDirParam(rootFS), WORK_DIR_PARAM + anotherWorkDir + JAR_CACHE_PARAM + anotherWorkDir + JAR_CACHE_DIR); } - @Issue("JENKINS-53245") - @Test - public void setJavaHome() throws Exception { - String javaHome = "/java_home"; - String javaHomeTool = "/java_home_tool"; - - SSHLauncher sshLauncher = new SSHLauncher("Hostname", 22, "credentialID"); - DumbSlave agent = new DumbSlave("agent" + j.jenkins.getNodes().size(), "/home/test/agent", sshLauncher); - j.jenkins.addNode(agent); - SlaveComputer computer = agent.getComputer(); - - List env = new ArrayList<>(); - EnvironmentVariablesNodeProperty.Entry entry = new EnvironmentVariablesNodeProperty.Entry(DefaultJavaProvider.JAVA_HOME, javaHome); - env.add(entry); - NodeProperty javaHomeProperty = new EnvironmentVariablesNodeProperty(env); - - JDK.DescriptorImpl jdkType = Jenkins.get().getDescriptorByType(JDK.DescriptorImpl.class); - ToolLocationNodeProperty tool = new ToolLocationNodeProperty(new ToolLocationNodeProperty.ToolLocation( - jdkType, "toolJdk", javaHomeTool)); - - List> properties = new ArrayList<>(); - properties.add(javaHomeProperty); - properties.add(tool); - computer.getNode().setNodeProperties(properties); - - JavaProvider provider = new DefaultJavaProvider(); - List javas = provider.getJavas(computer, null, null); - assertTrue(javas.contains(javaHome + DefaultJavaProvider.BIN_JAVA)); - assertTrue(javas.contains(javaHomeTool + DefaultJavaProvider.BIN_JAVA)); - assertTrue(javas.contains(SSHLauncher.getWorkingDirectory(computer) + DefaultJavaProvider.JDK_BIN_JAVA)); - } - @Test public void timeoutAndRetrySettings() { final SSHLauncher launcher = new SSHLauncher("Hostname", 22, "credentialID", "jvmOptions",