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;
}
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();
}
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());
}
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);
}
}
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);
}
}
Aggregations