Search in sources :

Example 6 with StreamTaskListener

use of hudson.util.StreamTaskListener in project hudson-2.x by hudson.

the class InstallToolCommand method install.

/**
     * Performs an installation.
     */
private int install(ToolInstallation t, BuildIDs id, AbstractProject p) throws IOException, InterruptedException {
    Run b = p.getBuildByNumber(Integer.parseInt(id.number));
    if (b == null)
        throw new AbortException("No such build: " + id.number);
    Executor exec = b.getExecutor();
    if (exec == null)
        throw new AbortException(b.getFullDisplayName() + " is not building");
    Node node = exec.getOwner().getNode();
    if (t instanceof NodeSpecific) {
        NodeSpecific n = (NodeSpecific) t;
        t = (ToolInstallation) n.forNode(node, new StreamTaskListener(stderr));
    }
    if (t instanceof EnvironmentSpecific) {
        EnvironmentSpecific e = (EnvironmentSpecific) t;
        t = (ToolInstallation) e.forEnvironment(EnvVars.getRemote(channel));
    }
    stdout.println(t.getHome());
    return 0;
}
Also used : Executor(hudson.model.Executor) EnvironmentSpecific(hudson.model.EnvironmentSpecific) StreamTaskListener(hudson.util.StreamTaskListener) Node(hudson.model.Node) NodeSpecific(hudson.slaves.NodeSpecific) Run(hudson.model.Run) AbortException(hudson.AbortException)

Example 7 with StreamTaskListener

use of hudson.util.StreamTaskListener in project hudson-2.x by hudson.

the class WindowsInstallerLink method doRestart.

public void doRestart(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
    if (installationDir == null) {
        // if the user reloads the page after Hudson has restarted,
        // it comes back here. In such a case, don't let this restart Hudson.
        // so just send them back to the top page
        rsp.sendRedirect(req.getContextPath() + "/");
        return;
    }
    Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
    rsp.forward(this, "_restart", req);
    final File oldRoot = Hudson.getInstance().getRootDir();
    // initiate an orderly shutdown after we finished serving this request
    new Thread("terminator") {

        public void run() {
            try {
                Thread.sleep(1000);
                // let the service start after we close our sockets, to avoid conflicts
                Runtime.getRuntime().addShutdownHook(new Thread("service starter") {

                    public void run() {
                        try {
                            if (!oldRoot.equals(installationDir)) {
                                LOGGER.info("Moving data");
                                Move mv = new Move();
                                Project p = new Project();
                                p.addBuildListener(createLogger());
                                mv.setProject(p);
                                FileSet fs = new FileSet();
                                fs.setDir(oldRoot);
                                // we can't really move the exploded war. 
                                fs.setExcludes("war/**");
                                mv.addFileset(fs);
                                mv.setTodir(installationDir);
                                // plugins can also fail to move
                                mv.setFailOnError(false);
                                mv.execute();
                            }
                            LOGGER.info("Starting a Windows service");
                            StreamTaskListener task = StreamTaskListener.fromStdout();
                            int r = WindowsSlaveInstaller.runElevated(new File(installationDir, "hudson.exe"), "start", task, installationDir);
                            task.getLogger().println(r == 0 ? "Successfully started" : "start service failed. Exit code=" + r);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    private DefaultLogger createLogger() {
                        DefaultLogger logger = new DefaultLogger();
                        logger.setOutputPrintStream(System.out);
                        logger.setErrorPrintStream(System.err);
                        return logger;
                    }
                });
                System.exit(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
}
Also used : Project(org.apache.tools.ant.Project) FileSet(org.apache.tools.ant.types.FileSet) Move(org.apache.tools.ant.taskdefs.Move) StreamTaskListener(hudson.util.StreamTaskListener) IOException(java.io.IOException) File(java.io.File) DefaultLogger(org.apache.tools.ant.DefaultLogger)

Example 8 with StreamTaskListener

use of hudson.util.StreamTaskListener in project hudson-2.x by hudson.

the class WindowsServiceLifecycle method restart.

@Override
public void restart() throws IOException, InterruptedException {
    File me = getHudsonWar();
    File home = me.getParentFile();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamTaskListener task = new StreamTaskListener(baos);
    task.getLogger().println("Restarting a service");
    int r = new LocalLauncher(task).launch().cmds(new File(home, "hudson.exe"), "restart").stdout(task).pwd(home).join();
    if (r != 0)
        throw new IOException(baos.toString());
}
Also used : LocalLauncher(hudson.Launcher.LocalLauncher) StreamTaskListener(hudson.util.StreamTaskListener) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 9 with StreamTaskListener

use of hudson.util.StreamTaskListener in project hudson-2.x by hudson.

the class WindowsSlaveInstaller method actionPerformed.

/**
     * Called when the install menu is selected
     */
public void actionPerformed(ActionEvent e) {
    try {
        int r = JOptionPane.showConfirmDialog(dialog, Messages.WindowsSlaveInstaller_ConfirmInstallation(), Messages.WindowsInstallerLink_DisplayName(), OK_CANCEL_OPTION);
        if (r != JOptionPane.OK_OPTION)
            return;
        if (!DotNet.isInstalled(2, 0)) {
            JOptionPane.showMessageDialog(dialog, Messages.WindowsSlaveInstaller_DotNetRequired(), Messages.WindowsInstallerLink_DisplayName(), ERROR_MESSAGE);
            return;
        }
        final File dir = new File(rootDir);
        if (!dir.exists()) {
            JOptionPane.showMessageDialog(dialog, Messages.WindowsSlaveInstaller_RootFsDoesntExist(rootDir), Messages.WindowsInstallerLink_DisplayName(), ERROR_MESSAGE);
            return;
        }
        final File slaveExe = new File(dir, "hudson-slave.exe");
        FileUtils.copyURLToFile(getClass().getResource("/windows-service/hudson.exe"), slaveExe);
        // write out the descriptor
        URL jnlp = new URL(engine.getHudsonUrl(), "computer/" + Util.rawEncode(engine.slaveName) + "/slave-agent.jnlp");
        String xml = generateSlaveXml(generateServiceId(rootDir), System.getProperty("java.home") + "\\bin\\java.exe", "-jnlpUrl " + jnlp.toExternalForm());
        FileUtils.writeStringToFile(new File(dir, "hudson-slave.xml"), xml, "UTF-8");
        // copy slave.jar
        URL slaveJar = new URL(engine.getHudsonUrl(), "jnlpJars/remoting.jar");
        File dstSlaveJar = new File(dir, "slave.jar").getCanonicalFile();
        if (// perhaps slave.jar is already there?
        !dstSlaveJar.exists())
            FileUtils.copyURLToFile(slaveJar, dstSlaveJar);
        // install as a service
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        StreamTaskListener task = new StreamTaskListener(baos);
        r = runElevated(slaveExe, "install", task, dir);
        if (r != 0) {
            JOptionPane.showMessageDialog(dialog, baos.toString(), "Error", ERROR_MESSAGE);
            return;
        }
        r = JOptionPane.showConfirmDialog(dialog, Messages.WindowsSlaveInstaller_InstallationSuccessful(), Messages.WindowsInstallerLink_DisplayName(), OK_CANCEL_OPTION);
        if (r != JOptionPane.OK_OPTION)
            return;
        // let the service start after we close our connection, to avoid conflicts
        Runtime.getRuntime().addShutdownHook(new Thread("service starter") {

            public void run() {
                try {
                    StreamTaskListener task = StreamTaskListener.fromStdout();
                    int r = runElevated(slaveExe, "start", task, dir);
                    task.getLogger().println(r == 0 ? "Successfully started" : "start service failed. Exit code=" + r);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.exit(0);
    } catch (Exception t) {
        // this runs as a JNLP app, so if we let an exeption go, we'll never find out why it failed 
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        JOptionPane.showMessageDialog(dialog, sw.toString(), "Error", ERROR_MESSAGE);
    }
}
Also used : StringWriter(java.io.StringWriter) StreamTaskListener(hudson.util.StreamTaskListener) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 10 with StreamTaskListener

use of hudson.util.StreamTaskListener in project hudson-2.x by hudson.

the class UtilTest method testSymlink.

public void testSymlink() throws Exception {
    if (Functions.isWindows())
        return;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamTaskListener l = new StreamTaskListener(baos);
    File d = Util.createTempDir();
    try {
        new FilePath(new File(d, "a")).touch(0);
        Util.createSymlink(d, "a", "x", l);
        assertEquals("a", Util.resolveSymlink(new File(d, "x"), l));
        // test a long name
        StringBuilder buf = new StringBuilder(768);
        for (int i = 0; i < 768; i++) buf.append((char) ('0' + (i % 10)));
        Util.createSymlink(d, buf.toString(), "x", l);
        String log = baos.toString();
        if (log.length() > 0)
            System.err.println("log output: " + log);
        assertEquals(buf.toString(), Util.resolveSymlink(new File(d, "x"), l));
    } finally {
        Util.deleteRecursive(d);
    }
}
Also used : StreamTaskListener(hudson.util.StreamTaskListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Aggregations

StreamTaskListener (hudson.util.StreamTaskListener)11 IOException (java.io.IOException)8 File (java.io.File)5 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)5 ServletException (javax.servlet.ServletException)3 AbortException (hudson.AbortException)2 ZFSException (org.jvnet.solaris.libzfs.ZFSException)2 JavaVMArguments (com.sun.akuma.JavaVMArguments)1 Extension (hudson.Extension)1 FilePath (hudson.FilePath)1 Launcher (hudson.Launcher)1 LocalLauncher (hudson.Launcher.LocalLauncher)1 WindowsSlaveInstaller (hudson.lifecycle.WindowsSlaveInstaller)1 EnvironmentSpecific (hudson.model.EnvironmentSpecific)1 Executor (hudson.model.Executor)1 Hudson (hudson.model.Hudson)1 Node (hudson.model.Node)1 Run (hudson.model.Run)1 Channel (hudson.remoting.Channel)1 VirtualChannel (hudson.remoting.VirtualChannel)1