use of com.github.dockerjava.api.command.InspectExecResponse in project vespa by vespa-engine.
the class DockerImpl method executeInContainerAsUser.
/**
* Execute command in container as user, "user" can be "username", "username:group", "uid" or "uid:gid"
*/
private ProcessResult executeInContainerAsUser(ContainerName containerName, String user, Optional<Long> timeoutSeconds, String... command) {
try {
final ExecCreateCmdResponse response = dockerClient.execCreateCmd(containerName.asString()).withCmd(command).withAttachStdout(true).withAttachStderr(true).withUser(user).exec();
ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream errors = new ByteArrayOutputStream();
ExecStartCmd execStartCmd = dockerClient.execStartCmd(response.getId());
ExecStartResultCallback callback = execStartCmd.exec(new ExecStartResultCallback(output, errors));
if (timeoutSeconds.isPresent()) {
if (!callback.awaitCompletion(timeoutSeconds.get(), TimeUnit.SECONDS)) {
throw new DockerExecTimeoutException(String.format("Command '%s' did not finish within %s seconds.", command[0], timeoutSeconds));
}
} else {
// Wait for completion no timeout
callback.awaitCompletion();
}
final InspectExecResponse state = dockerClient.inspectExecCmd(execStartCmd.getExecId()).exec();
assert !state.isRunning();
Integer exitCode = state.getExitCode();
assert exitCode != null;
return new ProcessResult(exitCode, new String(output.toByteArray()), new String(errors.toByteArray()));
} catch (RuntimeException | InterruptedException e) {
numberOfDockerDaemonFails.add();
throw new DockerException("Container '" + containerName.asString() + "' failed to execute " + Arrays.toString(command), e);
}
}
use of com.github.dockerjava.api.command.InspectExecResponse in project bookkeeper by apache.
the class DockerUtils method runCommand.
public static void runCommand(DockerClient docker, String containerId, String... cmd) throws Exception {
CompletableFuture<Boolean> future = new CompletableFuture<>();
String execid = docker.execCreateCmd(containerId).withCmd(cmd).exec().getId();
String cmdString = Arrays.stream(cmd).collect(Collectors.joining(" "));
docker.execStartCmd(execid).withDetach(false).exec(new ResultCallback<Frame>() {
@Override
public void close() {
}
@Override
public void onStart(Closeable closeable) {
LOG.info("DOCKER.exec({}:{}): Executing...", containerId, cmdString);
}
@Override
public void onNext(Frame object) {
LOG.info("DOCKER.exec({}:{}): {}", containerId, cmdString, object);
}
@Override
public void onError(Throwable throwable) {
future.completeExceptionally(throwable);
}
@Override
public void onComplete() {
LOG.info("DOCKER.exec({}:{}): Done", containerId, cmdString);
future.complete(true);
}
});
future.get();
InspectExecResponse resp = docker.inspectExecCmd(execid).exec();
while (resp.isRunning()) {
Thread.sleep(200);
resp = docker.inspectExecCmd(execid).exec();
}
int retCode = resp.getExitCode();
if (retCode != 0) {
throw new Exception(String.format("cmd(%s) failed on %s with exitcode %d", cmdString, containerId, retCode));
}
}
use of com.github.dockerjava.api.command.InspectExecResponse in project incubator-pulsar by apache.
the class DockerUtils method runCommand.
public static String runCommand(DockerClient docker, String containerId, String... cmd) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
String execid = docker.execCreateCmd(containerId).withCmd(cmd).withAttachStderr(true).withAttachStdout(true).exec().getId();
String cmdString = Arrays.stream(cmd).collect(Collectors.joining(" "));
StringBuffer output = new StringBuffer();
docker.execStartCmd(execid).withDetach(false).exec(new ResultCallback<Frame>() {
@Override
public void close() {
}
@Override
public void onStart(Closeable closeable) {
LOG.info("DOCKER.exec({}:{}): Executing...", containerId, cmdString);
}
@Override
public void onNext(Frame object) {
LOG.info("DOCKER.exec({}:{}): {}", containerId, cmdString, object);
output.append(new String(object.getPayload()));
}
@Override
public void onError(Throwable throwable) {
future.completeExceptionally(throwable);
}
@Override
public void onComplete() {
LOG.info("DOCKER.exec({}:{}): Done", containerId, cmdString);
future.complete(true);
}
});
future.join();
InspectExecResponse resp = docker.inspectExecCmd(execid).exec();
while (resp.isRunning()) {
try {
Thread.sleep(200);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
resp = docker.inspectExecCmd(execid).exec();
}
int retCode = resp.getExitCode();
if (retCode != 0) {
throw new RuntimeException(String.format("cmd(%s) failed on %s with exitcode %d", cmdString, containerId, retCode));
} else {
LOG.info("DOCKER.exec({}:{}): completed with {}", containerId, cmdString, retCode);
}
return output.toString();
}
use of com.github.dockerjava.api.command.InspectExecResponse in project vespa by vespa-engine.
the class DockerImplTest method testExecuteCompletes.
@Test
public void testExecuteCompletes() throws Exception {
final String containerId = "container-id";
final String[] command = new String[] { "/bin/ls", "-l" };
final String execId = "exec-id";
final int exitCode = 3;
final DockerClient dockerClient = mock(DockerClient.class);
final ExecCreateCmdResponse response = mock(ExecCreateCmdResponse.class);
when(response.getId()).thenReturn(execId);
final ExecCreateCmd execCreateCmd = mock(ExecCreateCmd.class);
when(dockerClient.execCreateCmd(any(String.class))).thenReturn(execCreateCmd);
when(execCreateCmd.withCmd(Matchers.<String>anyVararg())).thenReturn(execCreateCmd);
when(execCreateCmd.withAttachStdout(any(Boolean.class))).thenReturn(execCreateCmd);
when(execCreateCmd.withAttachStderr(any(Boolean.class))).thenReturn(execCreateCmd);
when(execCreateCmd.withUser(any(String.class))).thenReturn(execCreateCmd);
when(execCreateCmd.exec()).thenReturn(response);
final ExecStartCmd execStartCmd = mock(ExecStartCmd.class);
when(dockerClient.execStartCmd(any(String.class))).thenReturn(execStartCmd);
when(execStartCmd.exec(any(ExecStartResultCallback.class))).thenReturn(mock(ExecStartResultCallback.class));
final InspectExecCmd inspectExecCmd = mock(InspectExecCmd.class);
final InspectExecResponse state = mock(InspectExecResponse.class);
when(dockerClient.inspectExecCmd(any(String.class))).thenReturn(inspectExecCmd);
when(inspectExecCmd.exec()).thenReturn(state);
when(state.isRunning()).thenReturn(false);
when(state.getExitCode()).thenReturn(exitCode);
final Docker docker = new DockerImpl(dockerClient);
final ProcessResult result = docker.executeInContainer(new ContainerName(containerId), command);
assertThat(result.getExitStatus(), is(exitCode));
}
Aggregations