Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #1 from batmat/JENKINS-41763
[FIX JENKINS-41763] Show a column with the Java version of an agent
- Loading branch information
Showing
15 changed files
with
675 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
= VersionColumn Plugin | ||
|
||
This plugin offers two _node monitors_: | ||
|
||
* Remoting Version | ||
* JVM Version | ||
== Remoting Version Node Monitor | ||
|
||
NOTE: Remoting is the library that handles the network connection between Master and agents. | ||
|
||
This monitor will disconnect an agent if it does not run the same version of the Remoting library than the one on the Master. | ||
|
||
== JVM Version Node Monitor | ||
|
||
This monitor offers 3 levels of monitoring: | ||
|
||
[cols="2", options="header,border"] | ||
|=== | ||
| Description | ||
| Examples | ||
|
||
a| (default) Agent must run a JVM whose version is greater or equal than the one the Master was compiled against (strongly recommended minimum). | ||
|
||
NOTE: Even outside the context of that plugin, respecting that requirement is **critical** to the health of your Jenkins cluster. | ||
a| | ||
* an agent running Java 6 or less will be disconnected from 2.32.3 Master | ||
* an agent running Java 6 will not be disconnected from a 1.609 Master | ||
* an agent running Java 7 will be disconnected from a 2.54 Master | ||
|
||
| Agent must run a JVM whose major.minor version is equal to the Master one (recommended to avoid issues, already seen in the field). | ||
a| | ||
* an agent running 1.7 or less will be disconnected from a Master running 1.8.112 | ||
* an agent running 1.8.66 will not be disconnected from a Master running 1.8.112 | ||
|
||
| Agent must run a JVM whose version is *exactly* equal to the Master one (paranoid option) | ||
a| | ||
* an agent running 1.8.66 will be disconnected from a Master running 1.8.112 | ||
|
||
|=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/hudson/plugin/versioncolumn/JVMConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hudson.plugin.versioncolumn; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
class JVMConstants { | ||
|
||
public static final int JAVA_5 = 49; | ||
public static final int JAVA_6 = 50; | ||
public static final int JAVA_7 = 51; | ||
public static final int JAVA_8 = 52; | ||
public static final int JAVA_9 = 53; | ||
|
||
static final Map<String, Integer> JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING = new LinkedHashMap<String, Integer>(); | ||
|
||
static { | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.1", 45); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.2", 46); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.3", 47); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.4", 48); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.5", JAVA_5); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.6", JAVA_6); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("6", JAVA_6); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.7", JAVA_7); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("7", JAVA_7); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.8", JAVA_8); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("8", JAVA_8); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("1.9", JAVA_9); | ||
JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.put("9", JAVA_9); | ||
} | ||
} |
172 changes: 172 additions & 0 deletions
172
src/main/java/hudson/plugin/versioncolumn/JVMVersionComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017-, Baptiste Mathus | ||
* | ||
* 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.plugin.versioncolumn; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import hudson.util.VersionNumber; | ||
import jenkins.model.Jenkins; | ||
import org.apache.commons.codec.binary.Hex; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.jar.JarFile; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.zip.ZipEntry; | ||
|
||
import static hudson.plugin.versioncolumn.JVMConstants.JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING; | ||
|
||
/** | ||
* Responsible for master and agent jvm versions comparisons, and notions of "compatibility". | ||
* <p>For instance, the default behaviour is to consider 1.8.0 compatible with 1.8.3-whatever and so on. | ||
* Only considering <em>major.minor</em> part, that is</p> | ||
*/ | ||
class JVMVersionComparator { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(JVMVersionComparator.class.getName()); | ||
|
||
private static final Pattern MAJOR_MINOR_PATTERN = Pattern.compile("(\\d+\\.\\d+).*"); | ||
private final MasterBytecodeMajorVersionNumberGetter masterBytecodeMajorVersionNumberGetter; | ||
private boolean compatible; | ||
|
||
JVMVersionComparator(String masterVersion, String agentVersion, ComparisonMode comparisonMode) { | ||
this(masterVersion, agentVersion, comparisonMode, new MasterBytecodeMajorVersionNumberGetter()); | ||
} | ||
|
||
@VisibleForTesting | ||
JVMVersionComparator(String masterVersion, String agentVersion, ComparisonMode comparisonMode, MasterBytecodeMajorVersionNumberGetter versionNumberGetter) { | ||
masterBytecodeMajorVersionNumberGetter = versionNumberGetter; | ||
if (ComparisonMode.RUNTIME_GREATER_OR_EQUAL_MASTER_BYTECODE == comparisonMode) { | ||
compatible = isAgentRuntimeCompatibleWithJenkinsBytecodeLevel(computeMajorMinor(agentVersion)); | ||
} else if (ComparisonMode.EXACT_MATCH == comparisonMode) { | ||
compatible = masterVersion.equals(agentVersion); | ||
} else if (ComparisonMode.MAJOR_MINOR_MATCH == comparisonMode) { | ||
compatible = computeMajorMinor(masterVersion).equals(computeMajorMinor(agentVersion)); | ||
} | ||
} | ||
|
||
@Nonnull | ||
@VisibleForTesting | ||
static String computeMajorMinor(String version) { | ||
final Matcher matcher = MAJOR_MINOR_PATTERN.matcher(version); | ||
if (!matcher.matches()) { | ||
throw new IllegalArgumentException(version + " is not a supported JVM version pattern"); | ||
} | ||
return matcher.group(1); | ||
} | ||
|
||
/** | ||
* Reading the Jenkins.class bytecode first, then fallback to Jenkins.getVersion() is something is wrong. | ||
* | ||
* @return the bytecode major version of the Jenkins class (example: 51 for Java 7, 52 for Java 8...). | ||
* @see <a href="https://en.wikipedia.org/wiki/Java_class_file#General_layout">the Java bytecode general layout</a>. | ||
*/ | ||
private int getMasterBytecodeMajorVersionNumber() { | ||
return masterBytecodeMajorVersionNumberGetter.get(); | ||
} | ||
|
||
private boolean isAgentRuntimeCompatibleWithJenkinsBytecodeLevel(String agentMajorMinorVersion) { | ||
Integer masterBytecodeLevel = getMasterBytecodeMajorVersionNumber(); | ||
Integer agentVMMaxBytecodeLevel = JDK_VERSION_NUMBER_TO_BYTECODE_LEVEL_MAPPING.get(agentMajorMinorVersion); | ||
|
||
return masterBytecodeLevel <= agentVMMaxBytecodeLevel; | ||
} | ||
|
||
public boolean isCompatible() { | ||
return compatible; | ||
} | ||
|
||
public boolean isNotCompatible() { | ||
return !isCompatible(); | ||
} | ||
|
||
public enum ComparisonMode { | ||
RUNTIME_GREATER_OR_EQUAL_MASTER_BYTECODE(Messages.JVMVersionMonitor_RUNTIME_GREATER_OR_EQUAL_MASTER_BYTECODE()), | ||
MAJOR_MINOR_MATCH(Messages.JVMVersionMonitor_MAJOR_MINOR_MATCH()), | ||
EXACT_MATCH(Messages.JVMVersionMonitor_EXACT_MATCH()); | ||
|
||
private String description; | ||
|
||
ComparisonMode(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Delegate exclusively dedicated to testability | ||
*/ | ||
@VisibleForTesting | ||
static class MasterBytecodeMajorVersionNumberGetter { | ||
|
||
public int get() { | ||
|
||
final URL location = Jenkins.class.getProtectionDomain().getCodeSource().getLocation(); | ||
try (JarFile jarFile = new JarFile(location.getFile())) { | ||
final ZipEntry jenkinsClassEntry = jarFile.getEntry("jenkins/model/Jenkins.class"); | ||
|
||
final InputStream inputStream = jarFile.getInputStream(jenkinsClassEntry); | ||
byte[] magicAndClassFileVersion = new byte[8]; | ||
|
||
int read = inputStream.read(magicAndClassFileVersion); | ||
final String hexaBytes = Hex.encodeHexString(magicAndClassFileVersion); | ||
LOGGER.log(Level.FINE, "Jenkins.class file 8 first bytes: {0}", hexaBytes); | ||
if (read != 8 || !hexaBytes.startsWith("cafebabe")) { | ||
throw new IllegalStateException("Jenkins.class content is abnormal: '" + hexaBytes + "'"); | ||
} | ||
int javaMajor = (magicAndClassFileVersion[6] << 8) & 0xff00 | | ||
magicAndClassFileVersion[7] & 0xff; | ||
LOGGER.log(Level.FINEST, "Bytecode major version {0}", javaMajor); | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Issue while reading Jenkins.class bytecode level", e); | ||
} | ||
|
||
LOGGER.log(Level.FINE, "Falling back to using Jenkins.getVersion to infer bytecode level"); | ||
VersionNumber jenkinsVersion = Jenkins.getVersion(); | ||
if (jenkinsVersion == null) { | ||
throw new IllegalStateException("Jenkins.getVersion() returned a null value, stopping."); | ||
} | ||
// So Jenkins started with Java 1.4 (or less?) reading the *old* changelog (like around ~1.100) | ||
// but well not sure I'll bother | ||
if (jenkinsVersion.isOlderThan(new VersionNumber("1.520"))) { | ||
return JVMConstants.JAVA_5; | ||
} else if (jenkinsVersion.isOlderThan(new VersionNumber("1.612"))) { | ||
return JVMConstants.JAVA_6; | ||
} else if (jenkinsVersion.isOlderThan(new VersionNumber("2.54"))) { | ||
return JVMConstants.JAVA_7; | ||
} else if (jenkinsVersion.isNewerThan(new VersionNumber("2.54"))) { | ||
return JVMConstants.JAVA_8; | ||
} | ||
|
||
throw new IllegalStateException("Jenkins Bytecode Level could not be inferred"); | ||
} | ||
} | ||
} |
Oops, something went wrong.