Search in sources :

Example 1 with InspectExecResponse

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);
    }
}
Also used : ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExecCreateCmdResponse(com.github.dockerjava.api.command.ExecCreateCmdResponse) ExecStartCmd(com.github.dockerjava.api.command.ExecStartCmd) InspectExecResponse(com.github.dockerjava.api.command.InspectExecResponse)

Example 2 with InspectExecResponse

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));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Frame(com.github.dockerjava.api.model.Frame) Closeable(java.io.Closeable) InspectExecResponse(com.github.dockerjava.api.command.InspectExecResponse) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with InspectExecResponse

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();
}
Also used : Frame(com.github.dockerjava.api.model.Frame) Closeable(java.io.Closeable) CompletableFuture(java.util.concurrent.CompletableFuture) InspectExecResponse(com.github.dockerjava.api.command.InspectExecResponse)

Example 4 with InspectExecResponse

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));
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) InspectExecCmd(com.github.dockerjava.api.command.InspectExecCmd) ExecCreateCmdResponse(com.github.dockerjava.api.command.ExecCreateCmdResponse) ExecStartCmd(com.github.dockerjava.api.command.ExecStartCmd) InspectExecResponse(com.github.dockerjava.api.command.InspectExecResponse) ExecCreateCmd(com.github.dockerjava.api.command.ExecCreateCmd) Test(org.junit.Test)

Aggregations

InspectExecResponse (com.github.dockerjava.api.command.InspectExecResponse)4 ExecCreateCmdResponse (com.github.dockerjava.api.command.ExecCreateCmdResponse)2 ExecStartCmd (com.github.dockerjava.api.command.ExecStartCmd)2 Frame (com.github.dockerjava.api.model.Frame)2 ExecStartResultCallback (com.github.dockerjava.core.command.ExecStartResultCallback)2 Closeable (java.io.Closeable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 DockerClient (com.github.dockerjava.api.DockerClient)1 ExecCreateCmd (com.github.dockerjava.api.command.ExecCreateCmd)1 InspectExecCmd (com.github.dockerjava.api.command.InspectExecCmd)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1