Search in sources :

Example 11 with LogStream

use of com.spotify.docker.client.LogStream in project linuxtools by eclipse.

the class DockerConnection method attachCommand.

public void attachCommand(final String id, final InputStream in, @SuppressWarnings("unused") final DockerConsoleOutputStream out) throws DockerException {
    final byte[] prevCmd = new byte[1024];
    try {
        final LogStream pty_stream = client.attachContainer(id, AttachParameter.STDIN, AttachParameter.STDOUT, AttachParameter.STDERR, AttachParameter.STREAM, AttachParameter.LOGS);
        final IDockerContainerInfo info = getContainerInfo(id);
        final boolean isTtyEnabled = info.config().tty();
        final boolean isOpenStdin = info.config().openStdin();
        if (isTtyEnabled) {
            openTerminal(pty_stream, info.name(), out);
        }
        // Data from the given input stream
        // Written to container's STDIN
        Thread t_in = new Thread(() -> {
            byte[] buff = new byte[1024];
            int n;
            try {
                WritableByteChannel pty_out = HttpHijackWorkaround.getOutputStream(pty_stream, getUri());
                while ((n = in.read(buff)) != -1 && getContainerInfo(id).state().running()) {
                    synchronized (prevCmd) {
                        pty_out.write(ByteBuffer.wrap(buff, 0, n));
                        for (int i = 0; i < prevCmd.length; i++) {
                            prevCmd[i] = buff[i];
                        }
                    }
                    buff = new byte[1024];
                }
            } catch (Exception e) {
            }
        });
        if (!isTtyEnabled && isOpenStdin) {
            t_in.start();
        }
    } catch (Exception e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) WritableByteChannel(java.nio.channels.WritableByteChannel) LogStream(com.spotify.docker.client.LogStream) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) DockerPingConnectionException(org.eclipse.linuxtools.docker.core.DockerPingConnectionException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ProcessingException(javax.ws.rs.ProcessingException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StorageException(org.eclipse.equinox.security.storage.StorageException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerOpenConnectionException(org.eclipse.linuxtools.docker.core.DockerOpenConnectionException)

Example 12 with LogStream

use of com.spotify.docker.client.LogStream in project linuxtools by eclipse.

the class DockerConnection method readContainerDirectory.

@SuppressWarnings("unused")
public List<ContainerFileProxy> readContainerDirectory(final String id, final String path) throws DockerException {
    List<ContainerFileProxy> childList = new ArrayList<>();
    try {
        DockerClient copyClient = getClientCopy();
        final ExecCreation execCreation = copyClient.execCreate(id, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        new String[] { "/bin/sh", "-c", "ls -l -F -L -Q " + path }, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr());
        final String execId = execCreation.id();
        final LogStream pty_stream = copyClient.execStart(execId);
        try {
            while (pty_stream.hasNext()) {
                ByteBuffer b = pty_stream.next().content();
                byte[] buffer = new byte[b.remaining()];
                b.get(buffer);
                String s = new String(buffer);
                // $NON-NLS-1$
                String[] lines = s.split("\\r?\\n");
                for (String line : lines) {
                    if (// $NON-NLS-1$
                    line.trim().startsWith("total"))
                        // ignore the total line
                        continue;
                    // $NON-NLS-1$
                    String[] token = line.split("\\s+");
                    // $NON-NLS-1$
                    boolean isDirectory = token[0].startsWith("d");
                    // $NON-NLS-1$
                    boolean isLink = token[0].startsWith("l");
                    if (token.length > 8) {
                        // last token depends on whether we have a link or not
                        String link = null;
                        if (isLink) {
                            String linkname = token[token.length - 1];
                            if (linkname.endsWith("/")) {
                                // $NON-NLS-1$
                                linkname = linkname.substring(0, linkname.length() - 1);
                                isDirectory = true;
                            }
                            IPath linkPath = new Path(path);
                            linkPath = linkPath.append(linkname);
                            link = linkPath.toString();
                            String name = token[token.length - 3];
                            childList.add(new ContainerFileProxy(path, name, isDirectory, isLink, link));
                        } else {
                            String name = token[token.length - 1];
                            // remove quotes and any indicator char
                            name = name.substring(1, name.length() - (name.endsWith("\"") ? 1 : 2));
                            childList.add(new ContainerFileProxy(path, name, isDirectory));
                        }
                    }
                }
            }
        } finally {
            if (pty_stream != null)
                pty_stream.close();
            if (copyClient != null)
                copyClient.close();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    return childList;
}
Also used : Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) DockerClient(com.spotify.docker.client.DockerClient) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) LogStream(com.spotify.docker.client.LogStream) ByteBuffer(java.nio.ByteBuffer) DockerPingConnectionException(org.eclipse.linuxtools.docker.core.DockerPingConnectionException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ProcessingException(javax.ws.rs.ProcessingException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StorageException(org.eclipse.equinox.security.storage.StorageException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerOpenConnectionException(org.eclipse.linuxtools.docker.core.DockerOpenConnectionException) ExecCreation(com.spotify.docker.client.messages.ExecCreation)

Example 13 with LogStream

use of com.spotify.docker.client.LogStream in project linuxtools by eclipse.

the class DockerConnection method attachLog.

public void attachLog(final Closeable token, final String id, final OutputStream out, final OutputStream err) throws DockerException, InterruptedException, IOException {
    try {
        LogStream stream = ((DockerClient) token).logs(id, LogsParam.follow(), LogsParam.stdout(), LogsParam.stderr());
        stream.attach(out, err);
        stream.close();
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerClient(com.spotify.docker.client.DockerClient) LogStream(com.spotify.docker.client.LogStream)

Example 14 with LogStream

use of com.spotify.docker.client.LogStream in project linuxtools by eclipse.

the class DockerConnection method attachLog.

@Override
public void attachLog(final String id, final OutputStream out, final OutputStream err) throws DockerException, InterruptedException, IOException {
    DockerClient copyClient;
    try {
        copyClient = getClientCopy();
        LogStream stream = copyClient.logs(id, LogsParam.follow(), LogsParam.stdout(), LogsParam.stderr());
        stream.attach(out, err);
        stream.close();
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerClient(com.spotify.docker.client.DockerClient) LogStream(com.spotify.docker.client.LogStream)

Example 15 with LogStream

use of com.spotify.docker.client.LogStream in project helios by spotify.

the class TerminationTest method testTermOnExit.

@Test
public void testTermOnExit() throws Exception {
    startDefaultMaster();
    final String host = testHost();
    startDefaultAgent(host);
    final HeliosClient client = defaultClient();
    awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);
    // Note: signal 15 is SIGTERM
    final Job jobToInterrupt = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("/bin/sh", "-c", "trap handle 15; handle() { echo term; exit 0; }; " + "while true; do sleep 1; done")).build();
    final JobId jobId = createJob(jobToInterrupt);
    deployJob(jobId, host);
    awaitTaskState(jobId, host, RUNNING);
    client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER, Deployment.EMPTY_DEPLOYER_MASTER, Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host);
    final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED);
    final String log;
    try (final DockerClient dockerClient = getNewDockerClient();
        LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) {
        log = logs.readFully();
    }
    // Message expected, because the SIGTERM handler in the script should have run
    assertEquals("term\n", log);
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Deployment(com.spotify.helios.common.descriptors.Deployment) LogStream(com.spotify.docker.client.LogStream) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

LogStream (com.spotify.docker.client.LogStream)18 DockerClient (com.spotify.docker.client.DockerClient)15 JobId (com.spotify.helios.common.descriptors.JobId)10 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)10 Test (org.junit.Test)10 Matchers.containsString (org.hamcrest.Matchers.containsString)8 DockerException (org.eclipse.linuxtools.docker.core.DockerException)6 IOException (java.io.IOException)4 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)3 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)3 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)3 ExecCreation (com.spotify.docker.client.messages.ExecCreation)3 SocketException (java.net.SocketException)3 UnknownHostException (java.net.UnknownHostException)3 ProcessingException (javax.ws.rs.ProcessingException)3 StorageException (org.eclipse.equinox.security.storage.StorageException)3 DockerContainerNotFoundException (org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)3 DockerOpenConnectionException (org.eclipse.linuxtools.docker.core.DockerOpenConnectionException)3 DockerPingConnectionException (org.eclipse.linuxtools.docker.core.DockerPingConnectionException)3 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)2