Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hudson.plugins.xvnc;
  
  import hudson.Extension;
  import hudson.FilePath;
  import hudson.Launcher;
  import hudson.Proc;
  import hudson.Util;
 
 import java.io.File;
 import java.util.Map;
 
hudson.tasks.BuildWrapper that runs xvnc.

Author(s):
Kohsuke Kawaguchi
 
 public class Xvnc extends BuildWrapper {
    
Whether or not to take a screenshot upon completion of the build.
 
     public boolean takeScreenshot;

    
Manages display numbers in use.
 
     private DisplayAllocator allocator;
 
     private static final String FILENAME_SCREENSHOT = "screenshot.jpg";
 
     public Xvnc(boolean takeScreenshot) {
         this. = takeScreenshot;
     }
 
     @Override
     public Environment setUp(AbstractBuild buildfinal Launcher launcherBuildListener listener
             throws IOExceptionInterruptedException {
         final PrintStream logger = listener.getLogger();
         DescriptorImpl DESCRIPTOR = Hudson.getInstance().getDescriptorByType(DescriptorImpl.class);
 
         // skip xvnc execution
         if (build.getBuiltOn().getAssignedLabels().contains(Hudson.getInstance().getLabelAtom("noxvnc"))
                 || build.getBuiltOn().getNodeProperties().get(NodePropertyImpl.class) != null) {
             return new Environment(){};
         }
         
         if (DESCRIPTOR.skipOnWindows && !launcher.isUnix()) {
             return new Environment(){};
         }
         
         if (DESCRIPTOR.cleanUp) {
             maybeCleanUp(launcherlistener);
         }
 
         String cmd = Util.nullify(DESCRIPTOR.xvnc);
          = new DisplayAllocator(DESCRIPTOR.minDisplayNumberDESCRIPTOR.maxDisplayNumber);
         if (cmd == null) {
             cmd = "vncserver :$DISPLAY_NUMBER -localhost -nolisten tcp";
         }
 
         return doSetUp(buildlauncherloggercmd, 10);
     }
 
     private Environment doSetUp(AbstractBuild buildfinal Launcher launcherfinal PrintStream logger,
             String cmdint retriesthrows IOExceptionInterruptedException {
 
         final int displayNumber = .allocate();
         final String actualCmd = Util.replaceMacro(cmd, Collections.singletonMap("DISPLAY_NUMBER",String.valueOf(displayNumber)));
 
         logger.println(Messages.Xvnc_STARTING());
 
         String[] cmds = Util.tokenize(actualCmd);
         final FilePath xauthority = build.getWorkspace().createTempFile(".Xauthority-""");
         final Map<String,StringxauthorityEnv = Collections.singletonMap("XAUTHORITY"xauthority.getRemote());
         final Proc proc = launcher.launch().cmds(cmds).envs(xauthorityEnv).stdout(logger).pwd(build.getWorkspace()).start();
         final String vncserverCommand;
         if (cmds[0].endsWith("vncserver") && cmd.contains(":$DISPLAY_NUMBER")) {
             // Command just started the server; -kill will stop it.
             vncserverCommand = cmds[0];
             int exit = proc.join();
            if (exit != 0) {
                // XXX I18N
                String message = "Failed to run \'" + actualCmd + "\' (exit code " + exit + "), blacklisting display #" + displayNumber +
                        "; consider checking the \"Clean up before start\" option";
                // Do not release it; it may be "stuck" until cleaned up by an administrator.
                //allocator.free(displayNumber);
                .blacklist(displayNumber);
                if (retries > 0) {
                    return doSetUp(buildlauncherloggercmdretries - 1);
                } else {
                    throw new IOException(message);
                }
            }
        } else {
            vncserverCommand = null;
        }
        return new Environment() {
            @Override
            public void buildEnvVars(Map<StringStringenv) {
                env.put("DISPLAY",":"+displayNumber);
                env.putAll(xauthorityEnv);
            }
            @Override
            public boolean tearDown(AbstractBuild buildBuildListener listenerthrows IOExceptionInterruptedException {
                if () {
                    FilePath ws = build.getWorkspace();
                    File artifactsDir = build.getArtifactsDir();
                    artifactsDir.mkdirs();
                    logger.println(Messages.Xvnc_TAKING_SCREENSHOT());
                    launcher.launch().cmds("import""-window""root""-display"":" + displayNumber).
                            envs(xauthorityEnv).stdout(logger).pwd(ws).join();
                    ws.child().copyTo(new FilePath(artifactsDir).child());
                }
                logger.println(Messages.Xvnc_TERMINATING());
                if (vncserverCommand != null) {
                    // #173: stopping the wrapper script will accomplish nothing. It has already exited, in fact.
                    launcher.launch().cmds(vncserverCommand"-kill"":" + displayNumber).envs(xauthorityEnv).stdout(logger).join();
                } else {
                    // Assume it can be shut down by being killed.
                    proc.kill();
                }
                .free(displayNumber);
                xauthority.delete();
                return true;
            }
        };
    }

    
Whether maybeCleanUp(hudson.Launcher,hudson.model.BuildListener) has already been run on a given node.
    private static final Map<Node,BooleancleanedUpOn = new WeakHashMap<Node,Boolean>();
    
    // XXX I18N
    private static synchronized void maybeCleanUp(Launcher launcherBuildListener listenerthrows IOExceptionInterruptedException {
        Node node = Computer.currentComputer().getNode();
        if (.put(nodetrue) != null) {
            return;
        }
        if (!launcher.isUnix()) {
            listener.error("Clean up not currently implemented for non-Unix nodes; skipping");
            return;
        }
        PrintStream logger = listener.getLogger();
        // ignore any error return codes
        launcher.launch().stdout(logger).cmds("pkill""Xvnc").join();
        launcher.launch().stdout(logger).cmds("pkill""Xrealvnc").join();
        launcher.launch().stdout(logger).cmds("sh""-c""rm -f /tmp/.X*-lock /tmp/.X11-unix/X*").join();
    }
    
    @Extension
    public static final class DescriptorImpl extends BuildWrapperDescriptor {
        
        
xvnc command line. This can include macro. If null, the default will kick in.
        public String xvnc;
        /*
         * Base X display number. 
         */
        public int minDisplayNumber = 10;
        /*
         * Maximum X display number. 
         */
        public int maxDisplayNumber = 99;

        
If true, skip xvnc launch on all Windows slaves.
        public boolean skipOnWindows = true;
        
        
If true, try to clean up old processes and locks when first run.
        public boolean cleanUp = false;
        public DescriptorImpl() {
            super(Xvnc.class);
            load();
        }
        public String getDisplayName() {
            return Messages.description();
        }
        @Override
        public boolean configure(StaplerRequest reqJSONObject jsonthrows FormException {
            // XXX is this now the right style?
            req.bindJSON(this,json);
            save();
            return true;
        }
        public boolean isApplicable(AbstractProject<?, ?> item) {
            return true;
        }
        public String getCommandline() {
            return ;
        }
        public void setCommandline(String value) {
            this. = value;
        }
        public FormValidation doCheckCommandline(@QueryParameter String value) {
            if (Util.nullify(value) == null || value.contains("$DISPLAY_NUMBER")) {
                return FormValidation.ok();
            } else {
                return FormValidation.warningWithMarkup(Messages.Xvnc_SHOULD_INCLUDE_DISPLAY_NUMBER());
            }
        }
    }
New to GrepCode? Check out our FAQ X