From b5a32d280cecff0245d2a988d43a0cda43ce03d0 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Tue, 15 May 2018 16:55:10 +0200 Subject: Bug 534683 - Docker Run Image ignores stdin in regular console input Change-Id: I464c99d3844d3834e5ade764a390bc4186e53d06 Signed-off-by: Mickael Istria Reviewed-on: https://git.eclipse.org/r/122684 Tested-by: CI Bot Reviewed-by: Jeff Johnston --- .../docker/ui/launch/ContainerCommandProcess.java | 49 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java index 8c96982510..6a395457f8 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java @@ -21,7 +21,9 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; @@ -51,6 +53,9 @@ public class ContainerCommandProcess extends Process { private String imageName; private PipedInputStream stdout; private PipedInputStream stderr; + private OutputStream stdin; + private PipedInputStream pipedStdinIn; + final private Set toClose = new HashSet<>(); private Map remoteVolumes; private boolean keepContainer; private Thread thread; @@ -72,6 +77,25 @@ public class ContainerCommandProcess extends Process { this.stdout = new PipedInputStream(); this.stderr = new PipedInputStream(); this.keepContainer = keepContainer; + final IDockerContainerInfo info = connection.getContainerInfo(containerId); + if (info.config().openStdin()) { + try { + PipedOutputStream pipedStdinOut = new PipedOutputStream(); + toClose.add(pipedStdinOut); + pipedStdinIn = new PipedInputStream(pipedStdinOut); + toClose.add(pipedStdinIn); + this.stdin = new OutputStream() { + @Override + public void write(int b) throws IOException { + pipedStdinOut.write(b); + } + }; + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + this.stdin = new ByteArrayOutputStream(); + } // Lambda Runnable Runnable logContainer = () -> { PipedOutputStream pipedOut = null; @@ -85,8 +109,23 @@ public class ContainerCommandProcess extends Process { pipedErr = pipedStderr; connection.startContainer(containerId, outputStream); threadStarted = true; - ((DockerConnection) connection).attachLog(token, containerId, - pipedStdout, pipedStderr); + if (info.config().openStdin()) { + IDockerContainerState state = connection.getContainerInfo(containerId).state(); + do { + if (!state.running() + && (state.finishDate() == null || state.finishDate().before(state.startDate()))) { + Thread.sleep(50); + } + state = info.state(); + } while (!state.running() + && (state.finishDate() == null || state.finishDate().before(state.startDate()))); + Thread.sleep(50); + state = connection.getContainerInfo(containerId).state(); + if (state.running()) { + ((DockerConnection) connection).attachCommand(containerId, pipedStdinIn, null); + } + } + ((DockerConnection) connection).attachLog(token, containerId, pipedStdout, pipedStderr); pipedStdout.flush(); pipedStderr.flush(); } catch (DockerException | InterruptedException | IOException e) { @@ -146,6 +185,10 @@ public class ContainerCommandProcess extends Process { } this.stdout.close(); this.stderr.close(); + this.stdin.close(); + for (Closeable close : toClose) { + close.close(); + } } catch (IOException e) { // ignore } @@ -242,7 +285,7 @@ public class ContainerCommandProcess extends Process { @Override public OutputStream getOutputStream() { - return new ByteArrayOutputStream(); + return this.stdin; } /** -- cgit v1.2.3