Search in sources :

Example 1 with Move

use of org.apache.tools.ant.taskdefs.Move 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)

Aggregations

StreamTaskListener (hudson.util.StreamTaskListener)1 File (java.io.File)1 IOException (java.io.IOException)1 DefaultLogger (org.apache.tools.ant.DefaultLogger)1 Project (org.apache.tools.ant.Project)1 Move (org.apache.tools.ant.taskdefs.Move)1 FileSet (org.apache.tools.ant.types.FileSet)1