List of usage examples for org.eclipse.jgit.util.io StreamCopyThread StreamCopyThread
public StreamCopyThread(InputStream i, OutputStream o)
From source file:com.google.gerrit.server.git.PushReplication.java
License:Apache License
private void replicateProject(final URIish replicateURI, final String head) { SshSessionFactory sshFactory = SshSessionFactory.getInstance(); RemoteSession sshSession;//ww w . j a va 2s . co m String projectPath = QuotedString.BOURNE.quote(replicateURI.getPath()); if (!usingSSH(replicateURI)) { log.warn("Cannot create new project on remote site since the connection " + "method is not SSH: " + replicateURI.toString()); return; } OutputStream errStream = createErrStream(); String cmd = "mkdir -p " + projectPath + "&& cd " + projectPath + "&& git init --bare" + "&& git symbolic-ref HEAD " + QuotedString.BOURNE.quote(head); try { sshSession = sshFactory.getSession(replicateURI, null, FS.DETECTED, 0); Process proc = sshSession.exec(cmd, 0); proc.getOutputStream().close(); StreamCopyThread out = new StreamCopyThread(proc.getInputStream(), errStream); StreamCopyThread err = new StreamCopyThread(proc.getErrorStream(), errStream); out.start(); err.start(); try { proc.waitFor(); out.halt(); err.halt(); } catch (InterruptedException interrupted) { // Don't wait, drop out immediately. } sshSession.disconnect(); } catch (IOException e) { log.error("Communication error when trying to replicate to: " + replicateURI.toString() + "\n" + "Error reported: " + e.getMessage() + "\n" + "Error in communication: " + errStream.toString()); } }
From source file:com.googlesource.gerrit.plugins.replication.ReplicationQueue.java
License:Apache License
private void executeRemoteSsh(URIish uri, String cmd, OutputStream errStream) throws IOException { RemoteSession ssh = connect(uri);/* w ww. j av a2 s . co m*/ Process proc = ssh.exec(cmd, 0); proc.getOutputStream().close(); StreamCopyThread out = new StreamCopyThread(proc.getInputStream(), errStream); StreamCopyThread err = new StreamCopyThread(proc.getErrorStream(), errStream); out.start(); err.start(); try { proc.waitFor(); out.halt(); err.halt(); } catch (InterruptedException interrupted) { // Don't wait, drop out immediately. } ssh.disconnect(); }
From source file:org.eclipse.egit.core.op.ConfigureGerritAfterCloneTask.java
License:Open Source License
private String runSshCommand(URIish sshUri, CredentialsProvider provider, FS fs, String command) throws IOException { RemoteSession session = null;//from www. java 2s . c om Process process = null; StreamCopyThread errorThread = null; try (MessageWriter stderr = new MessageWriter()) { session = SshSessionFactory.getInstance().getSession(sshUri, provider, fs, 1000 * timeout); process = session.exec(command, 0); errorThread = new StreamCopyThread(process.getErrorStream(), stderr.getRawStream()); errorThread.start(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream(), Constants.CHARSET))) { return reader.readLine(); } } finally { if (errorThread != null) { try { errorThread.halt(); } catch (InterruptedException e) { // Stop waiting and return anyway. } finally { errorThread = null; } } if (process != null) { process.destroy(); } if (session != null) { SshSessionFactory.getInstance().releaseSession(session); } } }
From source file:org.eclipse.ptp.rdt.sync.git.core.CommandRunner.java
License:Open Source License
/** * Execute command on a remote host and wait for the command to complete. * /*from w w w .j a v a 2 s .c o m*/ * @param conn * @param command * @return CommandResults (contains stdout, stderr, and exit code) * @throws IOException * in several cases if there is a problem communicating with the remote host. * @throws InterruptedException * if execution of remote command is interrupted. * @throws RemoteConnectionException * if connection closed and cannot be opened. * @throws RemoteSyncException * if other error */ public static CommandResults executeRemoteCommand(IRemoteConnection conn, List<String> commandList, String remoteDirectory, IProgressMonitor monitor) throws IOException, InterruptedException, RemoteConnectionException, RemoteSyncException { if (!conn.isOpen()) { conn.open(monitor); } final IRemoteProcessBuilder rpb = conn.getRemoteServices().getProcessBuilder(conn, commandList); final IRemoteFileManager rfm = conn.getRemoteServices().getFileManager(conn); rpb.directory(rfm.getResource(remoteDirectory)); // Run process and stream readers OutputStream output = new ByteArrayOutputStream(); OutputStream error = new ByteArrayOutputStream(); IRemoteProcess rp = rpb.start(); StreamCopyThread getOutput = new StreamCopyThread(rp.getInputStream(), output); StreamCopyThread getError = new StreamCopyThread(rp.getErrorStream(), error); getOutput.start(); getError.start(); //wait for EOF with the change for the ProcessMonitor to cancel for (;;) { getOutput.join(250); if (!getOutput.isAlive()) break; if (monitor != null && monitor.isCanceled()) { throw new RemoteSyncException( new Status(IStatus.CANCEL, Activator.PLUGIN_ID, Messages.CommandRunner_0)); } } //rp and getError should be finished as soon as getOutput is finished int exitCode = rp.waitFor(); getError.halt(); final CommandResults commandResults = new CommandResults(); commandResults.setExitCode(exitCode); commandResults.setStdout(output.toString()); commandResults.setStderr(error.toString()); return commandResults; }
From source file:org.libreoffice.ci.gerrit.buildbot.publisher.JenkinsLogPublisher.java
License:Mozilla Public License
@Override public String publishLog(BuildbotConfig config, String ticket, String boxId, TaskStatus status, InputStream in) {/*from w w w . j a v a 2 s.com*/ final String cmd = JENKINS_SSH_COMMAND + " --display " + ticket + "_" + boxId + " --dump-build-number " + " --job " + config.getExternalLogViewerJob() + " --result " + (status.isSuccess() ? "0" : "1") + " --log -"; OutputStream errStream = newErrorBufferStream(); OutputStream outStream = newErrorBufferStream(); URIish jenkins = null; try { jenkins = new URIish().setHost(config.getExternalLogViewerHost()); RemoteSession ssh = connect(jenkins); Process proc = ssh.exec(cmd, 0/*timeout*/); StreamCopyThread out = new StreamCopyThread(proc.getInputStream(), outStream); StreamCopyThread err = new StreamCopyThread(proc.getErrorStream(), errStream); StreamCopyThread inp = new StreamCopyThread(in, proc.getOutputStream()); out.start(); err.start(); inp.start(); try { out.flush(); err.flush(); inp.flush(); proc.waitFor(); proc.exitValue(); out.halt(); err.halt(); inp.halt(); } catch (InterruptedException interrupted) { log.error("process interupted: ", interrupted); } ssh.disconnect(); } catch (IOException e) { log.error(String.format( "Error pushing log to %s:\n" + " Exception: %s\n" + " Command: %s\n" + " Output: %s", jenkins, e, cmd, errStream), e); return null; } String result = String.format("%s/job/%s/%s", config.getExternalLogViewerUrl(), config.getExternalLogViewerJob(), outStream.toString().trim()); log.debug("log url: {}", result); return result; }
From source file:org.libreoffice.ci.gerrit.buildbot.publisher.JenkinsLogPublisher.java
License:Mozilla Public License
@Override public String testChannel(BuildbotConfig config) { final String cmd = JENKINS_TEST_CHANNEL; StringBuilder builder = new StringBuilder(cmd.length()).append(">ssh ") .append(config.getExternalLogViewerHost()).append(" ").append(cmd).append("\n"); OutputStream errStream = newErrorBufferStream(); OutputStream outStream = newErrorBufferStream(); URIish jenkins = null;// www . ja v a 2 s. c om try { jenkins = new URIish().setHost(config.getExternalLogViewerHost()); RemoteSession ssh = connect(jenkins); Process proc = ssh.exec(cmd, 0/*timeout*/); StreamCopyThread out = new StreamCopyThread(proc.getInputStream(), outStream); StreamCopyThread err = new StreamCopyThread(proc.getErrorStream(), errStream); out.start(); err.start(); try { out.flush(); err.flush(); proc.waitFor(); proc.exitValue(); out.halt(); err.halt(); } catch (InterruptedException interrupted) { log.error("process interupted: ", interrupted); } ssh.disconnect(); return builder.append("<").append(outStream.toString()).toString(); } catch (IOException e) { log.error(String.format( "Error testing log channel\n" + " Exception: %s\n" + " Command: %s\n" + " Output: %s", jenkins, e, cmd, errStream), e); return null; } }