Search in sources :

Example 1 with ExecStartCmd

use of com.github.dockerjava.api.command.ExecStartCmd 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 ExecStartCmd

use of com.github.dockerjava.api.command.ExecStartCmd in project camel by apache.

the class AsyncDockerProducer method executeExecStartRequest.

/**
     * Produces a exec start request
     *
     * @param client
     * @param message
     * @return
     */
private ExecStartCmd executeExecStartRequest(DockerClient client, Message message) {
    LOGGER.debug("Executing Docker Exec Create Request");
    String execId = DockerHelper.getProperty(DockerConstants.DOCKER_EXEC_ID, configuration, message, String.class);
    ObjectHelper.notNull(execId, "Exec ID must be specified");
    ExecStartCmd execStartCmd = client.execStartCmd(execId);
    Boolean detach = DockerHelper.getProperty(DockerConstants.DOCKER_DETACH, configuration, message, Boolean.class);
    if (detach != null) {
        execStartCmd.withDetach(detach);
    }
    Boolean tty = DockerHelper.getProperty(DockerConstants.DOCKER_TTY, configuration, message, Boolean.class);
    if (tty != null) {
        execStartCmd.withTty(tty);
    }
    return execStartCmd;
}
Also used : ExecStartCmd(com.github.dockerjava.api.command.ExecStartCmd)

Example 3 with ExecStartCmd

use of com.github.dockerjava.api.command.ExecStartCmd 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

ExecStartCmd (com.github.dockerjava.api.command.ExecStartCmd)3 ExecCreateCmdResponse (com.github.dockerjava.api.command.ExecCreateCmdResponse)2 InspectExecResponse (com.github.dockerjava.api.command.InspectExecResponse)2 ExecStartResultCallback (com.github.dockerjava.core.command.ExecStartResultCallback)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 Test (org.junit.Test)1