Skip to content

Commit

Permalink
Merge branch 'master' into confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
mawinter69 committed Jul 10, 2023
2 parents 44dfeef + fbf1e27 commit 733cb48
Show file tree
Hide file tree
Showing 49 changed files with 1,042 additions and 818 deletions.
2 changes: 1 addition & 1 deletion ath.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -o xtrace
cd "$(dirname "$0")"

# https://github.com/jenkinsci/acceptance-test-harness/releases
export ATH_VERSION=5631.v2dcb_f66e58f7
export ATH_VERSION=5658.v5b_fe603c573d

if [[ $# -eq 0 ]]; then
export JDK=17
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/hudson/util/HudsonIsRestarting.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,29 @@
* @author Kohsuke Kawaguchi
*/
public class HudsonIsRestarting {
private boolean safeRestart;

/**
* @since TODO
*/
public HudsonIsRestarting(boolean safeRestart) {
this.safeRestart = safeRestart;
}

@Deprecated
public HudsonIsRestarting() {
this.safeRestart = false;
}

public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, InterruptedException {
rsp.setStatus(SC_SERVICE_UNAVAILABLE);
req.getView(this, "index.jelly").forward(req, rsp);
}

/**
* @since TODO
*/
public boolean isSafeRestart() {
return safeRestart;
}
}
59 changes: 59 additions & 0 deletions core/src/main/java/jenkins/cli/SafeRestartCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* The MIT License
*
* Copyright (c) 2023, Jan Meiswinkel
*
* 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 jenkins.cli;

import hudson.Extension;
import hudson.cli.CLICommand;
import hudson.cli.Messages;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.args4j.Option;

/**
* Safe Restart Jenkins - do not accept any new jobs and try to pause existing.
*
* @since TODO
*/
@Extension
@Restricted(NoExternalUse.class)
public class SafeRestartCommand extends CLICommand {
private static final Logger LOGGER = Logger.getLogger(SafeRestartCommand.class.getName());

@Option(name = "-message", usage = "Message for safe restart that will be visible to users")
public String message = null;

@Override
public String getShortDescription() {
return Messages.SafeRestartCommand_ShortDescription();
}

@Override
protected int run() throws Exception {
Jenkins.get().doSafeRestart(null, message);
return 0;
}
}
115 changes: 94 additions & 21 deletions core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve

@CheckForNull
private transient volatile QuietDownInfo quietDownInfo;

private transient volatile boolean terminating;
@GuardedBy("Jenkins.class")
private transient boolean cleanUpStarted;
Expand Down Expand Up @@ -2943,6 +2944,20 @@ public boolean isQuietingDown() {
return quietDownInfo != null;
}

/**
* Returns if the quietingDown is a safe restart.
* @since TODO
*/
@Restricted(NoExternalUse.class)
@NonNull
public boolean isPreparingSafeRestart() {
QuietDownInfo quietDownInfo = this.quietDownInfo;
if (quietDownInfo != null) {
return quietDownInfo.isSafeRestart();
}
return false;
}

/**
* Returns quiet down reason if it was indicated.
* @return
Expand All @@ -2953,7 +2968,7 @@ public boolean isQuietingDown() {
@CheckForNull
public String getQuietDownReason() {
final QuietDownInfo info = quietDownInfo;
return info != null ? info.reason : null;
return info != null ? info.message : null;
}

/**
Expand Down Expand Up @@ -4093,7 +4108,7 @@ public synchronized HttpRedirect doQuietDown() {
*
* @param block Block until the system really quiets down and no builds are running
* @param timeout If non-zero, only block up to the specified number of milliseconds
* @deprecated since 2.267; use {@link #doQuietDown(boolean, int, String)} instead.
* @deprecated since 2.267; use {@link #doQuietDown(boolean, int, String, boolean)} instead.
*/
@Deprecated
public synchronized HttpRedirect doQuietDown(boolean block, int timeout) {
Expand All @@ -4109,16 +4124,34 @@ public synchronized HttpRedirect doQuietDown(boolean block, int timeout) {
*
* @param block Block until the system really quiets down and no builds are running
* @param timeout If non-zero, only block up to the specified number of milliseconds
* @param reason Quiet reason that will be visible to user
* @since 2.267
* @param message Quiet reason that will be visible to user
* @deprecated use {@link #doQuietDown(boolean, int, String, boolean)} instead.
*/
@Deprecated(since = "TODO")
public HttpRedirect doQuietDown(boolean block,
int timeout,
@CheckForNull String message) throws InterruptedException, IOException {

return doQuietDown(block, timeout, message, false);
}

/**
* Quiet down Jenkins - preparation for a restart
*
* @param block Block until the system really quiets down and no builds are running
* @param timeout If non-zero, only block up to the specified number of milliseconds
* @param message Quiet reason that will be visible to user
* @param safeRestart If the quietDown is for a safeRestart
* @since TODO
*/
@RequirePOST
public HttpRedirect doQuietDown(@QueryParameter boolean block,
@QueryParameter int timeout,
@QueryParameter @CheckForNull String reason) throws InterruptedException, IOException {
@QueryParameter @CheckForNull String message,
@QueryParameter boolean safeRestart) throws InterruptedException, IOException {
synchronized (this) {
checkPermission(MANAGE);
quietDownInfo = new QuietDownInfo(reason);
quietDownInfo = new QuietDownInfo(message, safeRestart);
}
if (block) {
long waitUntil = timeout;
Expand Down Expand Up @@ -4513,20 +4546,35 @@ public void doRestart(StaplerRequest req, StaplerResponse rsp) throws IOExceptio
}

/**
* Queues up a restart of Jenkins for when there are no builds running, if we can.
* Queues up a safe restart of Jenkins.
* Builds that cannot continue while the controller is not running have to finish or pause before it can proceed.
* No new builds will be started. No new jobs are accepted.
*
* This first replaces "app" to {@link HudsonIsRestarting}
* @deprecated use {@link #doSafeRestart(StaplerRequest, String)} instead.
*
* @since 1.332
*/
@CLIMethod(name = "safe-restart")
@Deprecated(since = "TODO")
public HttpResponse doSafeRestart(StaplerRequest req) throws IOException, ServletException, RestartNotSupportedException {
return doSafeRestart(req, null);
}

/**
* Queues up a safe restart of Jenkins. Jobs have to finish or pause before it can proceed. No new jobs are accepted.
*
* @since TODO
*/
public HttpResponse doSafeRestart(StaplerRequest req, @QueryParameter("message") String message) throws IOException, ServletException, RestartNotSupportedException {
checkPermission(MANAGE);
if (req != null && req.getMethod().equals("GET"))
if (req != null && req.getMethod().equals("GET")) {
return HttpResponses.forwardToView(this, "_safeRestart.jelly");
}

if (req != null && req.getParameter("cancel") != null) {
return doCancelQuietDown();
}

if (req == null || req.getMethod().equals("POST")) {
safeRestart();
safeRestart(message);
}

return HttpResponses.redirectToDot();
Expand Down Expand Up @@ -4572,11 +4620,22 @@ public void run() {
/**
* Queues up a restart to be performed once there are no builds currently running.
* @since 1.332
* @deprecated use {@link #safeRestart(String)} instead.
*/
@Deprecated(since = "TODO")
public void safeRestart() throws RestartNotSupportedException {
safeRestart(null);
}

/**
* Queues up a restart to be performed once there are no builds currently running.
* @param message the message to show to users in the shutdown banner.
* @since TODO
*/
public void safeRestart(String message) throws RestartNotSupportedException {
final Lifecycle lifecycle = restartableLifecycle();
// Quiet down so that we won't launch new builds.
quietDownInfo = new QuietDownInfo();
quietDownInfo = new QuietDownInfo(message, true);

new Thread("safe-restart thread") {
final String exitUser = getAuthentication2().getName();
Expand All @@ -4585,11 +4644,10 @@ public void run() {
try (ACLContext ctx = ACL.as2(ACL.SYSTEM2)) {

// Wait 'til we have no active executors.
doQuietDown(true, 0, null);

doQuietDown(true, 0, message, true);
// Make sure isQuietingDown is still true.
if (isQuietingDown()) {
servletContext.setAttribute("app", new HudsonIsRestarting());
servletContext.setAttribute("app", new HudsonIsRestarting(true));
// give some time for the browser to load the "reloading" page
lifecycle.onStatusUpdate("Restart in 10 seconds");
Thread.sleep(TimeUnit.SECONDS.toMillis(10));
Expand Down Expand Up @@ -5761,16 +5819,31 @@ private static void _setJenkinsJVM(boolean jenkinsJVM) {
}

private static final class QuietDownInfo {

@CheckForNull
final String reason;
final String message;

private boolean safeRestart;

QuietDownInfo() {
this(null);
this(null, false);
}

QuietDownInfo(final String message) {
this(message, false);
}

QuietDownInfo(final String message, final boolean safeRestart) {
this.message = message;
this.safeRestart = safeRestart;
}


boolean isSafeRestart() {
return safeRestart;
}

QuietDownInfo(final String reason) {
this.reason = reason;
void setSafeRestart(boolean safeRestart) {
this.safeRestart = safeRestart;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -75,6 +76,11 @@ public class OperatingSystemEndOfLifeAdminMonitor extends AdministrativeMonitor
private String endOfLifeDate = "2099-12-31";
private String documentationUrl = "https://www.jenkins.io/redirect/operating-system-end-of-life";

/* Remember the last dataFile to avoid reading it again */
private File lastDataFile = null;
/* Remember the lines of the last dataFile */
private List<String> lastLines = null;

public OperatingSystemEndOfLifeAdminMonitor(String id) throws IOException {
super(id);
fillOperatingSystemList();
Expand Down Expand Up @@ -153,6 +159,10 @@ void readOperatingSystemList(String initialOperatingSystemJson) throws IOExcepti
}
}
}
if (lastLines != null) {
// Discard the cached contents of the last read file
lastLines.clear();
}
}

@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN",
Expand All @@ -177,13 +187,17 @@ String readOperatingSystemName(File dataFile, @NonNull String patternStr) {
Pattern pattern = Pattern.compile("^PRETTY_NAME=[\"](" + patternStr + ".*)[\"]");
String name = "";
try {
List<String> lines = Files.readAllLines(dataFile.toPath());
List<String> lines = dataFile.equals(lastDataFile) ? lastLines : Files.readAllLines(dataFile.toPath());
for (String line : lines) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
name = matcher.group(1);
}
}
if (!dataFile.equals(lastDataFile)) {
lastDataFile = dataFile;
lastLines = new ArrayList<>(lines);
}
} catch (IOException ioe) {
LOGGER.log(Level.SEVERE, "File read exception", ioe);
}
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/resources/hudson/PluginManager/_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,26 @@ window.addEventListener("load", function () {
);
});
});

// Enable/disable the 'Update' button depending on if any updates are checked
const anyCheckboxesSelected = () => {
return (
document.querySelectorAll("input[type='checkbox']:checked:not(:disabled)")
.length > 0
);
};
const updateButton = document.querySelector("#button-update");
const checkboxes = document.querySelectorAll(
"input[type='checkbox'], [data-select], .jenkins-table__checkbox"
);
checkboxes.forEach((checkbox) => {
checkbox.addEventListener("click", () => {
setTimeout(() => {
updateButton.disabled = !anyCheckboxesSelected();
});
});
});

// Show update center error if element exists
const updateCenterError = document.querySelector("#update-center-error");
if (updateCenterError) {
Expand Down
Loading

0 comments on commit 733cb48

Please sign in to comment.