Search in sources :

Example 1 with ExecCreation

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

the class ImageQuery method exec.

private String exec(String[] cmd) {
    LogStream stream = null;
    try {
        ExecCreation exeCr = client.execCreate(id, cmd, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.detach(), // needed to avoid connection reset on unix socket
        ExecCreateParam.attachStdin());
        stream = client.execStart(exeCr.id());
        StringBuffer res = new StringBuffer();
        while (stream.hasNext()) {
            ByteBuffer b = stream.next().content();
            byte[] buffer = new byte[b.remaining()];
            b.get(buffer);
            res.append(new String(buffer));
        }
        return res.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stream.close();
    }
    return null;
}
Also used : ExecCreation(com.spotify.docker.client.messages.ExecCreation) LogStream(com.spotify.docker.client.LogStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) DockerException(org.eclipse.linuxtools.docker.core.DockerException)

Example 2 with ExecCreation

use of com.spotify.docker.client.messages.ExecCreation 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 3 with ExecCreation

use of com.spotify.docker.client.messages.ExecCreation in project docker-client by spotify.

the class DefaultDockerClientTest method testExec.

@Test
public void testExec() throws Exception {
    requireDockerApiVersionAtLeast("1.15", "Exec");
    // CircleCI uses lxc, doesn't support exec - https://circleci.com/docs/docker/#docker-exec
    assumeFalse(CIRCLECI);
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    final ExecCreation execCreation = sut.execCreate(containerId, new String[] { "ls", "-la" }, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr());
    final String execId = execCreation.id();
    log.info("execId = {}", execId);
    try (final LogStream stream = sut.execStart(execId)) {
        final String output = stream.readFully();
        log.info("Result:\n{}", output);
        assertThat(output, containsString("total"));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ExecCreation(com.spotify.docker.client.messages.ExecCreation) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 4 with ExecCreation

use of com.spotify.docker.client.messages.ExecCreation in project zalenium by zalando.

the class DockerContainerClient method executeCommand.

public void executeCommand(String containerId, String[] command, boolean waitForExecution) {
    final ExecCreation execCreation;
    try {
        execCreation = dockerClient.execCreate(containerId, command, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin());
        final LogStream output = dockerClient.execStart(execCreation.id());
        logger.info(String.format("%s %s", nodeId, Arrays.toString(command)));
        if (waitForExecution) {
            try {
                String commandOutput = output.readFully();
                logger.debug(String.format("%s %s", nodeId, commandOutput));
            } catch (Exception e) {
                logger.debug(nodeId + " Error while executing the output.readFully()", e);
                ga.trackException(e);
            }
        }
    } catch (DockerException | InterruptedException e) {
        logger.debug(nodeId + " Error while executing the command", e);
        ga.trackException(e);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ExecCreation(com.spotify.docker.client.messages.ExecCreation) LogStream(com.spotify.docker.client.LogStream) DockerException(com.spotify.docker.client.exceptions.DockerException)

Example 5 with ExecCreation

use of com.spotify.docker.client.messages.ExecCreation in project zeppelin by apache.

the class DockerInterpreterProcess method execInContainer.

private void execInContainer(String containerId, String execCommand, boolean logout) throws DockerException, InterruptedException {
    LOGGER.info("exec container commmand: " + execCommand);
    final String[] command = { "sh", "-c", execCommand };
    final ExecCreation execCreation = docker.execCreate(containerId, command, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr());
    LogStream logStream = docker.execStart(execCreation.id());
    while (logStream.hasNext() && logout) {
        final String log = UTF_8.decode(logStream.next().content()).toString();
        LOGGER.info(log);
    }
}
Also used : ExecCreation(com.spotify.docker.client.messages.ExecCreation) LogStream(com.spotify.docker.client.LogStream)

Aggregations

ExecCreation (com.spotify.docker.client.messages.ExecCreation)12 LogStream (com.spotify.docker.client.LogStream)7 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)4 Long.toHexString (java.lang.Long.toHexString)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)4 DockerClient (com.spotify.docker.client.DockerClient)3 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)3 DockerException (com.spotify.docker.client.exceptions.DockerException)3 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)3 ExecCreateParam (com.spotify.docker.client.DockerClient.ExecCreateParam)2 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 ExecState (com.spotify.docker.client.messages.ExecState)2 ProcessConfig (com.spotify.docker.client.messages.ProcessConfig)2 ByteBuffer (java.nio.ByteBuffer)2 DockerException (org.eclipse.linuxtools.docker.core.DockerException)2