Skip to content

Commit

Permalink
Automate Java version recommendation administrative monitor (jenkinsc…
Browse files Browse the repository at this point in the history
…i#8526)

* Automate Java version recommendation administrative monitor

* Improve dates
  • Loading branch information
basil authored Sep 29, 2023
1 parent 823ab34 commit 02502e2
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@

package jenkins.monitor;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.AdministrativeMonitor;
import hudson.security.Permission;
import java.io.IOException;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
import java.util.NavigableMap;
import java.util.TreeMap;
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import jenkins.util.java.JavaUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
Expand All @@ -46,15 +54,68 @@
@Symbol("javaVersionRecommendation")
public class JavaVersionRecommendationAdminMonitor extends AdministrativeMonitor {

/**
* The list of supported Java long-term support (LTS) releases. The key is the {@link
* Runtime.Version#feature() feature-release counter}. The value is the date the Jenkins project
* drops support for that release, which must be before the date the Eclipse Temurin project
* drops support for that release. This list must remain synchronized with the one in {@code
* executable.Main}.
*
* <p>To add support for a Java version:
*
* <ul>
* <li>Update {@link #SUPPORTED_JAVA_VERSIONS}
* <li>Update {@code executable.Main#SUPPORTED_JAVA_VERSIONS}
* <li>Update the {@code Jenkinsfile} for core and core components
* <li>Update the {@code Jenkinsfile} for PCT
* <li>Update the {@code Jenkinsfile} for ATH
* <li>Update the archetype and the {@code Jenkinsfile} for critical plugins
* </ul>
*
* @see <a href="https://endoflife.date/eclipse-temurin">Eclipse Temurin End of Life</a>
*/
private static final NavigableMap<Integer, LocalDate> SUPPORTED_JAVA_VERSIONS;

static {
NavigableMap<Integer, LocalDate> supportedVersions = new TreeMap<>();
supportedVersions.put(11, LocalDate.of(2024, 9, 30)); // Temurin: 2024-10-31
supportedVersions.put(17, LocalDate.of(2026, 3, 31)); // Temurin: 2027-10-31
supportedVersions.put(21, LocalDate.of(2027, 9, 30)); // Temurin: 2029-09-30
SUPPORTED_JAVA_VERSIONS = Collections.unmodifiableNavigableMap(supportedVersions);
}

public JavaVersionRecommendationAdminMonitor() {
super(JavaVersionRecommendationAdminMonitor.class.getName() + "-3");
super(getId());
}

/**
* Compute the ID for the administrative monitor. The ID includes the Java version, EOL date,
* and severity so that changes to the EOL date for a given Java version will invalidate
* previous dismissals of the administrative monitor and so that users who decline to upgrade
* after the first warning get a second warning when they are closer to the deadline.
*
* @return The computed ID.
*/
private static String getId() {
StringBuilder id = new StringBuilder();
id.append(JavaVersionRecommendationAdminMonitor.class.getName());
LocalDate endOfLife = getEndOfLife();
if (endOfLife.isBefore(LocalDate.MAX)) {
id.append('-');
id.append(Runtime.version().feature());
id.append('-');
id.append(endOfLife);
id.append('-');
id.append(getSeverity());
}
return id.toString();
}

private static Boolean disabled = SystemProperties.getBoolean(JavaVersionRecommendationAdminMonitor.class.getName() + ".disabled", false);

@Override
public boolean isActivated() {
return !disabled && JavaUtils.isRunningWithJava8OrBelow();
return !disabled && getDeprecationPeriod().getYears() < 2;
}

@Override
Expand All @@ -78,8 +139,55 @@ public HttpResponse doAct(@QueryParameter String no) throws IOException {
disable(true);
return HttpResponses.forwardToPreviousPage();
} else {
return new HttpRedirect("https://www.jenkins.io/redirect/upgrading-jenkins-java-version-8-to-11");
return new HttpRedirect("https://jenkins.io/redirect/java-support/");
}
}

@NonNull
private static LocalDate getEndOfLife() {
LocalDate endOfLife = SUPPORTED_JAVA_VERSIONS.get(Runtime.version().feature());
return endOfLife != null ? endOfLife : LocalDate.MAX;
}

@NonNull
private static Period getDeprecationPeriod() {
return Period.between(LocalDate.now(), getEndOfLife());
}

@NonNull
private static Severity getSeverity() {
return getDeprecationPeriod().getYears() < 1 ? Severity.DANGER : Severity.WARNING;
}

/**
* @return The current feature-release counter.
* @see Runtime#version()
*/
@Restricted(DoNotUse.class)
public int getJavaVersion() {
return Runtime.version().feature();
}

/**
* @return The end of life date for the current Java version in the system default time zone.
*/
@Restricted(DoNotUse.class)
public Date getEndOfLifeAsDate() {
return Date.from(getEndOfLife().atStartOfDay(ZoneId.systemDefault()).toInstant());
}

/**
* @return The severity of the administrative monitor, used to set the background color of the alert.
*/
@Restricted(DoNotUse.class)
public String getSeverityAsString() {
return getSeverity().toString().toLowerCase(Locale.US);
}

private enum Severity {
SUCCESS,
INFO,
WARNING,
DANGER
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:st="jelly:stapler">
${%blurb}
${%blurb(it.javaVersion)}
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
blurb = Recommends Java 11 for running Jenkins if an older version is used.
blurb = Recommends Java {0,number} for running Jenkins if an older version is used.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

blurb=Recomenda o Java 11 para executar o Jenkins se uma versão anterior está em uso.
blurb=Recomenda o Java {0,number} para executar o Jenkins se uma versão anterior está em uso.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
blurb = Рекомендует запускать Jenkins на Java 11, если используется более старая версия.
blurb = Рекомендует запускать Jenkins на Java {0,number}, если используется более старая версия.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
blurb=若您使用的 Java 版本低於 11,推薦您改用 Java 11 來執行 Jenkins。
blurb=若您使用的 Java 版本低於 {0,number},推薦您改用 Java {0,number} 來執行 Jenkins。
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ THE SOFTWARE.

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:f="/lib/form">
<j:invokeStatic var="currentRuntimeJavaVersion" className="jenkins.util.java.JavaUtils" method="getCurrentRuntimeJavaVersion" />
<div class="alert alert-danger">
<div class="alert alert-${it.severityAsString}">
<form id="JavaVersionRecommendationAdminMonitor" method="post" action="${rootURL}/${it.url}/act" name="${it.id}">
<f:submit name="yes" value="${%More Info}"/>
<l:isAdmin>
<f:submit value="${%Ignore}" name="no"/>
</l:isAdmin>
</form>
<h2>${%Recommended_Java_Version_Heading(currentRuntimeJavaVersion)}</h2>
${%Recommended_Java_Version(currentRuntimeJavaVersion)}
<h2>${%Recommended_Java_Version_Heading(it.javaVersion)}</h2>
${%Recommended_Java_Version(it.javaVersion, it.endOfLifeAsDate)}
</div>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Recommended_Java_Version_Heading=Java {0} end of life in Jenkins
Recommended_Java_Version=<p class="jenkins-!-margin-bottom-0">You are running Jenkins on Java {0}, support for which will end on or after <strong>June 21, 2022</strong>. \
This is earlier than a previously announced date.</p> \
<p class="jenkins-!-margin-top-0">The Long Term Support (LTS) line of Jenkins will continue support till September 2022.</p> \
<p>Please refer to <a href="https://www.jenkins.io/redirect/upgrading-jenkins-java-version-8-to-11" target="_blank" rel="noopener noreferrer">the documentation</a> for details on upgrading to Java 11.</p>
Recommended_Java_Version_Heading=Java {0,number} end of life in Jenkins
Recommended_Java_Version=You are running Jenkins on Java {0,number}, support for which will end on or after {1,date}. \
Refer to <a href="https://jenkins.io/redirect/java-support/" target="_blank" rel="noopener noreferrer">the documentation</a> for more details.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

More\ Info=Mais informação
Dismiss=Dispensar
Recommended_Java_Version=<p class="jenkins-!-margin-bottom-0">Você está executando o Jenkins com o Java {0}, cujo \
suporte será encerrado em <strong>21 de junho de 2022</strong>. Isto é antes do que a data previamente anunciada.</p>\
<p class="jenkins-!-margin-top-0">A linha de suporte de longo período (LTS) do Jenkins continuará a suportar esta \
versão até setembro de 2022.</p><p>Por favor se refira a \
<a href="https://www.jenkins.io/redirect/upgrading-jenkins-java-version-8-to-11" target="_blank" rel="noopener noreferrer">\
a documentação</a> para maiores detalhes sobre atualizar para o Java versão 11.</p>
Recommended_Java_Version_Heading=Fim de suporte ao Java {0} pelo Jenkins
Recommended_Java_Version=Você está executando o Jenkins com o Java {0,number}, cujo suporte será encerrado em {1,date}. \
Se refira a <a href="https://jenkins.io/redirect/java-support/" target="_blank" rel="noopener noreferrer">a documentação</a> para maiores detalhes.
Recommended_Java_Version_Heading=Fim de suporte ao Java {0,number} pelo Jenkins
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
Recommended_Java_Version_Heading=Конец поддержки Java {0} в Jenkins
Recommended_Java_Version=<p class="jenkins-!-margin-bottom-0">Вы запустили Jenkins на Java {0}. Её поддержка будет \
остановлена <strong>21 июня 2022</strong>. \
Это раньше, чем дата, объявленная ранее.</p> \
<p class="jenkins-!-margin-top-0">Долгосрочно поддерживаемые (LTS) релизы Jenkins будут поддерживать её до сентября \
2022.</p> \
<p>Пожалуйста, смотрите детали об обновлении до Java 11 в \
<a href="https://www.jenkins.io/redirect/upgrading-jenkins-java-version-8-to-11" \
target="_blank" rel="noopener noreferrer">документации</a>.</p>
Recommended_Java_Version_Heading=Конец поддержки Java {0,number} в Jenkins
Recommended_Java_Version=Вы запустили Jenkins на Java {0,number}. Её поддержка будет остановлена {1,date}. \
Смотрите детали в <a href="https://jenkins.io/redirect/java-support/" target="_blank" rel="noopener noreferrer">документации</a>.
More\ Info=Подробнее
Dismiss=Убрать
4 changes: 4 additions & 0 deletions war/src/main/java/executable/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
*/
public class Main {

/**
* This list must remain synchronized with the one in {@code
* JavaVersionRecommendationAdminMonitor}.
*/
private static final NavigableSet<Integer> SUPPORTED_JAVA_VERSIONS =
new TreeSet<>(Arrays.asList(11, 17, 21));

Expand Down

0 comments on commit 02502e2

Please sign in to comment.