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());
}
}
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);
}
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");
}
}
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());
}
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);
}
}
}
Aggregations