use of com.github.dockerjava.api.command.ExecCreateCmd in project camel by apache.
the class DockerProducer method executeExecCreateRequest.
/*********************
* Exec Requests
********************/
/**
* Produces a exec create request
*
* @param client
* @param message
* @return
*/
private ExecCreateCmd executeExecCreateRequest(DockerClient client, Message message) {
LOGGER.debug("Executing Docker Exec Create Request");
String containerId = DockerHelper.getProperty(DockerConstants.DOCKER_CONTAINER_ID, configuration, message, String.class);
ObjectHelper.notNull(containerId, "Container ID must be specified");
ExecCreateCmd execCreateCmd = client.execCreateCmd(containerId);
Boolean attachStdIn = DockerHelper.getProperty(DockerConstants.DOCKER_ATTACH_STD_IN, configuration, message, Boolean.class);
Boolean attachStdErr = DockerHelper.getProperty(DockerConstants.DOCKER_ATTACH_STD_ERR, configuration, message, Boolean.class);
if (attachStdErr != null) {
execCreateCmd.withAttachStderr(attachStdErr);
}
if (attachStdIn != null) {
execCreateCmd.withAttachStdin(attachStdIn);
}
Boolean attachStdOut = DockerHelper.getProperty(DockerConstants.DOCKER_ATTACH_STD_OUT, configuration, message, Boolean.class);
if (attachStdOut != null) {
execCreateCmd.withAttachStdout(attachStdOut);
}
String[] cmd = DockerHelper.parseDelimitedStringHeader(DockerConstants.DOCKER_CMD, message);
if (cmd != null) {
execCreateCmd.withCmd(cmd);
}
Boolean tty = DockerHelper.getProperty(DockerConstants.DOCKER_TTY, configuration, message, Boolean.class);
if (tty != null) {
execCreateCmd.withTty(tty);
}
return execCreateCmd;
}
use of com.github.dockerjava.api.command.ExecCreateCmd 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));
}
use of com.github.dockerjava.api.command.ExecCreateCmd in project hub-docker-inspector by blackducksoftware.
the class DockerClientManager method execCommandInContainer.
private void execCommandInContainer(final DockerClient dockerClient, final String imageId, final String containerId, final List<String> cmd) throws InterruptedException {
logger.info(String.format("Running %s in container %s from image %s", cmd.get(0), containerId, imageId));
final ExecCreateCmd execCreateCmd = dockerClient.execCreateCmd(containerId).withAttachStdout(true).withAttachStderr(true);
// final String[] cmdArr = (String[]) cmd.toArray();
final String[] cmdArr = new String[cmd.size()];
for (int i = 0; i < cmd.size(); i++) {
logger.debug(String.format("cmdArr[%d]=%s", i, cmd.get(i)));
cmdArr[i] = cmd.get(i);
}
execCreateCmd.withCmd(cmdArr);
final ExecCreateCmdResponse execCreateCmdResponse = execCreateCmd.exec();
logger.info("Invoking container appropriate for this target image");
dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(new ExecStartResultCallback(System.out, System.err)).awaitCompletion();
logger.info("The container execution has completed");
}
Aggregations