Skip to content
Permalink
Browse files
JENKINS-15972 No display available in post build actions
  • Loading branch information
Zoran Regvart committed Dec 9, 2012
1 parent 7beee89 commit 2029cb27527a4534e55e9256e1bc45f4c58afff5
@@ -38,6 +38,8 @@
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.listeners.RunListener;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.tools.ToolInstallation;
@@ -55,6 +57,25 @@

public class XvfbBuildWrapper extends BuildWrapper {

@SuppressWarnings("rawtypes")
@Extension
public static final RunListener<Run> xvfbShutdownListener = new RunListener<Run>() {
@Override
public void onCompleted(Run r, TaskListener listener) {
XvfbEnvironment xvfbEnvironment = r.getAction(XvfbEnvironment.class);

if (xvfbEnvironment != null && xvfbEnvironment.isShutdownWithBuild()) {
try {
shutdownAndCleanup(xvfbEnvironment, listener);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};

@Extension
public static class XvfbBuildWrapperDescriptor extends BuildWrapperDescriptor {

@@ -149,9 +170,12 @@ private FormValidation validateOptionalPositiveInteger(final String value) {
/** Additional options to be passed to Xvfb */
private final String additionalOptions;

/** Should the Xvfb display be around for post build actions, i.e. should it terminate with the whole build */
private boolean shutdownWithBuild = false;

@DataBoundConstructor
public XvfbBuildWrapper(final String installationName, final Integer displayName, final String screen, final Boolean debug, final int timeout, final int displayNameOffset,
final String additionalOptions) {
final String additionalOptions, Boolean shutdownWithBuild) {
this.installationName = installationName;
this.displayName = displayName;

@@ -171,6 +195,8 @@ public XvfbBuildWrapper(final String installationName, final Integer displayName
this.displayNameOffset = displayNameOffset;
}
this.additionalOptions = additionalOptions;

this.shutdownWithBuild = shutdownWithBuild;
}

public String getAdditionalOptions() {
@@ -222,6 +248,10 @@ public boolean isDebug() {
return debug;
}

public boolean isShutdownWithBuild() {
return shutdownWithBuild;
}

private XvfbEnvironment launchXvfb(@SuppressWarnings("rawtypes") final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {
int displayNameUsed;

@@ -278,7 +308,9 @@ private XvfbEnvironment launchXvfb(@SuppressWarnings("rawtypes") final AbstractB

Thread.sleep(timeout * MILLIS_IN_SECOND);

return new XvfbEnvironment(frameBufferDir, displayNameUsed, process);
final XvfbEnvironment xvfbEnvironment = new XvfbEnvironment(frameBufferDir, displayNameUsed, process, shutdownWithBuild);

return xvfbEnvironment;
}

@Override
@@ -292,6 +324,15 @@ public void makeBuildVariables(@SuppressWarnings("rawtypes") final AbstractBuild
}
}

private static void shutdownAndCleanup(XvfbEnvironment environment, TaskListener listener) throws IOException, InterruptedException {
final FilePath frameBufferDir = environment.getFrameBufferDir();
final Proc process = environment.getProcess();

listener.getLogger().println(Messages.XvfbBuildWrapper_Stopping());
process.kill();
frameBufferDir.deleteRecursive();
}

@Override
public Environment setUp(@SuppressWarnings("rawtypes") final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {
if (!launcher.isUnix()) {
@@ -308,10 +349,6 @@ public Environment setUp(@SuppressWarnings("rawtypes") final AbstractBuild build

final int displayNameUsed = xvfbEnvironment.getDisplayNameUsed();

final FilePath frameBufferDir = xvfbEnvironment.getFrameBufferDir();

final Proc process = xvfbEnvironment.getProcess();

return new Environment() {
@Override
public void buildEnvVars(final Map<String, String> env) {
@@ -320,9 +357,9 @@ public void buildEnvVars(final Map<String, String> env) {

@Override
public boolean tearDown(@SuppressWarnings("rawtypes") final AbstractBuild build, final BuildListener listener) throws IOException, InterruptedException {
listener.getLogger().println(Messages.XvfbBuildWrapper_Stopping());
process.kill();
frameBufferDir.deleteRecursive();
if (!shutdownWithBuild) {
shutdownAndCleanup(xvfbEnvironment, listener);
}

return true;
}
@@ -14,10 +14,14 @@ public class XvfbEnvironment extends InvisibleAction {
/** Handle to the Xvfb process. */
private final transient Proc process;

public XvfbEnvironment(final FilePath frameBufferDir, final int displayNameUsed, final Proc process) {
/** The shutdownWithBuild indicator from the job configuration. */
private final transient boolean shutdownWithBuild;

public XvfbEnvironment(final FilePath frameBufferDir, final int displayNameUsed, final Proc process, boolean shutdownWithBuild) {
this.frameBufferDir = frameBufferDir;
this.displayNameUsed = displayNameUsed;
this.process = process;
this.shutdownWithBuild = shutdownWithBuild;
}

public int getDisplayNameUsed() {
@@ -31,4 +35,8 @@ public FilePath getFrameBufferDir() {
public Proc getProcess() {
return process;
}

public boolean isShutdownWithBuild() {
return shutdownWithBuild;
}
}
@@ -64,6 +64,12 @@ SUCH DAMAGE.
<f:entry title="${%Log Xvfb output}" field="debug">
<f:checkbox value="${instance.debug}" />
</f:entry>

<f:entry
title="${%Shoutdown Xvfb with whole job, not just with the main build action}"
field="shutdownWithBuild">
<f:checkbox value="${instance.shutdownWithBuild}" />
</f:entry>
</f:advanced>

</j:jelly>
@@ -0,0 +1,26 @@
<!--
Copyright (c) 2012 Zoran Regvart All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
-->
<div>Should the display be kept until the whole job ends
(including the post build steps).</div>

0 comments on commit 2029cb2

Please sign in to comment.