Search in sources :

Example 6 with ExecCreation

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

the class DockerConnection method execShell.

public void execShell(final String id) throws DockerException {
    try {
        final ExecCreation execCreation = client.execCreate(id, // $NON-NLS-1$
        new String[] { "/bin/sh" }, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.attachStdin(), ExecCreateParam.tty());
        final String execId = execCreation.id();
        final LogStream pty_stream = client.execStart(execId, DockerClient.ExecStartParameter.TTY);
        final IDockerContainerInfo info = getContainerInfo(id);
        // $NON-NLS-1$
        openTerminal(pty_stream, info.name() + " [shell]", null);
    } catch (Exception e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) ExecCreation(com.spotify.docker.client.messages.ExecCreation) 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 7 with ExecCreation

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

the class DefaultDockerClientTest method testExecNoTimeout.

@Test
public void testExecNoTimeout() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String volumeContainer = randomName();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "sleep 15").build();
    sut.createContainer(volumeConfig, volumeContainer);
    sut.startContainer(volumeContainer);
    final StringBuffer result = new StringBuffer();
    final String[] cmd = new String[] { "sh", "-c", "sleep 10 ;" + "echo Finished ;" };
    final ExecCreation execCreation = sut.execCreate(volumeContainer, cmd, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr());
    final LogStream stream = sut.execStart(execCreation.id());
    try {
        while (stream.hasNext()) {
            final String r = UTF_8.decode(stream.next().content()).toString();
            log.info(r);
            result.append(r);
        }
    } catch (Exception e) {
        log.info(e.getMessage());
    }
    verifyNoTimeoutContainer(volumeContainer, result);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) 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) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) ImagePushFailedException(com.spotify.docker.client.exceptions.ImagePushFailedException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) BadParamException(com.spotify.docker.client.exceptions.BadParamException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) UnsupportedApiVersionException(com.spotify.docker.client.exceptions.UnsupportedApiVersionException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 8 with ExecCreation

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

the class DefaultDockerClientTest method testExecInspect.

@Test
public void testExecInspect() throws Exception {
    requireDockerApiVersionAtLeast("1.16", "Exec Inspect");
    // 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 List<ExecCreateParam> createParams = newArrayList(ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.attachStdin(), ExecCreateParam.tty());
    // some functionality in this test depends on API 1.19 (exec user)
    final boolean execUserSupported = dockerApiVersionAtLeast("1.19");
    if (execUserSupported) {
        createParams.add(ExecCreateParam.user("1000"));
    }
    final ExecCreation execCreation = sut.execCreate(containerId, new String[] { "sh", "-c", "exit 2" }, createParams.toArray(new ExecCreateParam[createParams.size()]));
    final String execId = execCreation.id();
    log.info("execId = {}", execId);
    final ExecState notStarted = sut.execInspect(execId);
    assertThat(notStarted.id(), is(execId));
    assertThat(notStarted.running(), is(false));
    if (dockerApiVersionLessThan("1.22")) {
        assertThat(notStarted.exitCode(), is(0));
    } else {
        assertThat(notStarted.exitCode(), nullValue());
    }
    assertThat(notStarted.openStdin(), is(true));
    assertThat(notStarted.openStderr(), is(true));
    assertThat(notStarted.openStdout(), is(true));
    try (final LogStream stream = sut.execStart(execId)) {
        stream.readFully();
    }
    final ExecState started = sut.execInspect(execId);
    assertThat(started.id(), is(execId));
    assertThat(started.running(), is(false));
    assertThat(started.exitCode(), is(2));
    assertThat(started.openStdin(), is(true));
    assertThat(started.openStderr(), is(true));
    assertThat(started.openStdout(), is(true));
    final ProcessConfig processConfig = started.processConfig();
    assertThat(processConfig.privileged(), is(false));
    if (execUserSupported) {
        assertThat(processConfig.user(), is("1000"));
    }
    assertThat(processConfig.tty(), is(true));
    assertThat(processConfig.entrypoint(), is("sh"));
    assertThat(processConfig.arguments(), Matchers.<List<String>>is(ImmutableList.of("-c", "exit 2")));
    if (dockerApiVersionLessThan("1.22")) {
        final ContainerInfo containerInfo = started.container();
        assertThat(containerInfo.path(), is("sh"));
        assertThat(containerInfo.args(), Matchers.<List<String>>is(ImmutableList.of("-c", "while :; do sleep 1; done")));
        assertThat(containerInfo.config().image(), is(BUSYBOX_LATEST));
    } else {
        assertNotNull(started.containerId(), "containerId");
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ExecCreateParam(com.spotify.docker.client.DockerClient.ExecCreateParam) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ProcessConfig(com.spotify.docker.client.messages.ProcessConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ExecState(com.spotify.docker.client.messages.ExecState) Test(org.junit.Test)

Example 9 with ExecCreation

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

the class DefaultDockerClientTest method testExecInspectNoUser.

@Test
public void testExecInspectNoUser() throws Exception {
    requireDockerApiVersionAtLeast("1.16", "Exec Inspect");
    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 List<ExecCreateParam> createParams = newArrayList(ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.attachStdin(), ExecCreateParam.tty());
    final ExecCreation execCreation = sut.execCreate(containerId, new String[] { "sh", "-c", "exit 2" }, createParams.toArray(new ExecCreateParam[createParams.size()]));
    final String execId = execCreation.id();
    log.info("execId = {}", execId);
    try (final LogStream stream = sut.execStart(execId)) {
        stream.readFully();
    }
    final ExecState state = sut.execInspect(execId);
    assertThat(state.id(), is(execId));
    final ProcessConfig processConfig = state.processConfig();
    assertThat(processConfig.user(), isEmptyOrNullString());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ExecCreateParam(com.spotify.docker.client.DockerClient.ExecCreateParam) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ProcessConfig(com.spotify.docker.client.messages.ProcessConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ExecState(com.spotify.docker.client.messages.ExecState) Test(org.junit.Test)

Example 10 with ExecCreation

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

the class MongoTestRunner method tryInitializeDb.

/**
 * Helper method to run some initialization command on Mongo with some retry logic if the
 * command fails. Since it's not determinate how long starting the database will take, we need
 * this retry logic.
 *
 * @param initializationCommands The command to actually run
 * @param retriesRemaining How many more times to retry the command if it fails
 * @param pauseTimeMillis How long to pause between retries
 * @throws InterruptedException Thrown when the thread gets interrupted trying to sleep.
 */
private void tryInitializeDb(String[] initializationCommands, int retriesRemaining, long pauseTimeMillis) throws InterruptedException {
    Exception exception = null;
    String execOutput = "";
    try {
        final ExecCreation execCreation = dockerClient.execCreate(this.runningDockerContainerId, initializationCommands, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.detach(false));
        final LogStream output = dockerClient.execStart(execCreation.id());
        execOutput = output.readFully();
    } catch (Exception e) {
        exception = e;
    }
    // success
    if (exception != null || !execOutput.contains("Using a default configuration for the set")) {
        // This is the case that the command didn't work
        if (retriesRemaining == 0) {
            // We're out of retries, we should fail
            if (exception != null) {
                exception.printStackTrace();
            }
            fail("Failed to initialize MongoDB, no retries remaining. Output was: " + execOutput);
        } else {
            Thread.sleep(pauseTimeMillis);
            tryInitializeDb(initializationCommands, retriesRemaining - 1, pauseTimeMillis);
        }
    }
}
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