Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-24696] Cannot assume that Jenkins.getInstance() != nul…
…l when we are running a boot failure script.

(cherry picked from commit 42de07b)
  • Loading branch information
jglick authored and olivergondza committed Feb 3, 2016
1 parent 501d7fb commit ef8ea8c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/init/impl/GroovyInitScript.java
Expand Up @@ -38,6 +38,6 @@
public class GroovyInitScript {
@Initializer(after=JOB_LOADED)
public static void init(Jenkins j) {
new GroovyHookScript("init").run();
new GroovyHookScript("init", j.servletContext, j.getRootDir(), j.getPluginManager().uberClassLoader).run();
}
}
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/util/BootFailure.java
Expand Up @@ -40,7 +40,10 @@ public void publish(ServletContext context, @CheckForNull File home) {
LOGGER.log(Level.SEVERE, "Failed to initialize Jenkins",this);

WebApp.get(context).setApp(this);
new GroovyHookScript("boot-failure")
if (home == null) {
return;
}
new GroovyHookScript("boot-failure", context, home, BootFailure.class.getClassLoader())
.bind("exception",this)
.bind("home",home)
.bind("servletContext", context)
Expand Down
28 changes: 16 additions & 12 deletions core/src/main/java/jenkins/util/groovy/GroovyHookScript.java
Expand Up @@ -3,18 +3,17 @@
import groovy.lang.Binding;
import groovy.lang.GroovyCodeSource;
import groovy.lang.GroovyShell;
import jenkins.model.Jenkins;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;

import static java.util.logging.Level.WARNING;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.servlet.ServletContext;

/**
* A collection of Groovy scripts that are executed as various hooks.
Expand All @@ -40,9 +39,15 @@
public class GroovyHookScript {
private final String hook;
private final Binding bindings = new Binding();
private final ServletContext servletContext;
private final File home;
private final ClassLoader loader;

public GroovyHookScript(String hook) {
public GroovyHookScript(String hook, @Nonnull ServletContext servletContext, @Nonnull File home, @Nonnull ClassLoader loader) {
this.hook = hook;
this.servletContext = servletContext;
this.home = home;
this.loader = loader;
}

public GroovyHookScript bind(String name, Object o) {
Expand All @@ -55,34 +60,33 @@ public Binding getBindings() {
}

public void run() {
Jenkins j = Jenkins.getInstance();
final String hookGroovy = hook+".groovy";
final String hookGroovyD = hook+".groovy.d";

try {
URL bundled = j.servletContext.getResource("/WEB-INF/"+ hookGroovy);
URL bundled = servletContext.getResource("/WEB-INF/"+ hookGroovy);
execute(bundled);
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to execute /WEB-INF/"+hookGroovy,e);
}

Set<String> resources = j.servletContext.getResourcePaths("/WEB-INF/"+ hookGroovyD +"/");
Set<String> resources = servletContext.getResourcePaths("/WEB-INF/"+ hookGroovyD +"/");
if (resources!=null) {
// sort to execute them in a deterministic order
for (String res : new TreeSet<String>(resources)) {
try {
URL bundled = j.servletContext.getResource(res);
URL bundled = servletContext.getResource(res);
execute(bundled);
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to execute " + res, e);
}
}
}

File script = new File(j.getRootDir(), hookGroovy);
File script = new File(home, hookGroovy);
execute(script);

File scriptD = new File(j.getRootDir(), hookGroovyD);
File scriptD = new File(home, hookGroovyD);
if (scriptD.isDirectory()) {
File[] scripts = scriptD.listFiles(new FileFilter() {
public boolean accept(File f) {
Expand Down Expand Up @@ -129,7 +133,7 @@ protected void execute(GroovyCodeSource s) {
* Can be used to customize the environment in which the script runs.
*/
protected GroovyShell createShell() {
return new GroovyShell(Jenkins.getInstance().getPluginManager().uberClassLoader, bindings);
return new GroovyShell(loader, bindings);
}

private static final Logger LOGGER = Logger.getLogger(GroovyHookScript.class.getName());
Expand Down
29 changes: 28 additions & 1 deletion test/src/test/java/hudson/util/BootFailureTest.java
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
Expand All @@ -19,6 +20,7 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.HudsonHomeLoader;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestEnvironment;
import org.jvnet.hudson.test.TestExtension;
Expand All @@ -34,6 +36,7 @@ public class BootFailureTest {
public TemporaryFolder tmpDir = new TemporaryFolder();

static boolean makeBootFail = true;
static WebAppMain wa;

static class CustomRule extends JenkinsRule {
@Override
Expand All @@ -46,7 +49,7 @@ public void before() throws Throwable {
@Override
public Hudson newHudson() throws Exception {
ServletContext ws = createWebServer();
WebAppMain wa = new WebAppMain() {
wa = new WebAppMain() {
@Override
public WebAppMain.FileAndDescription getHomeDir(ServletContextEvent event) {
try {
Expand Down Expand Up @@ -130,6 +133,30 @@ private static int bootFailures(File home) throws IOException {
return FileUtils.readLines(BootFailure.getBootFailureFile(home)).size();
}

@Issue("JENKINS-24696")
@Test
public void interruptedStartup() throws Exception {
final File home = tmpDir.newFolder();
j.with(new HudsonHomeLoader() {
@Override
public File allocate() throws Exception {
return home;
}
});
File d = new File(home, "boot-failure.groovy.d");
d.mkdirs();
FileUtils.write(new File(d, "1.groovy"), "hudson.util.BootFailureTest.runRecord << '1'");
j.newHudson();
assertEquals(Collections.singletonList("1"), runRecord);
}
@TestExtension("interruptedStartup")
public static class PauseBoot extends ItemListener {
@Override
public void onLoaded() {
wa.contextDestroyed(null);
}
}

// to be set by the script
public static Exception problem;
public static List<String> runRecord = new ArrayList<String>();
Expand Down

0 comments on commit ef8ea8c

Please sign in to comment.