use of com.spotify.docker.client.messages.ExecCreation in project linuxtools by eclipse.
the class ImageQuery method exec.
private String exec(String[] cmd) {
LogStream stream = null;
try {
ExecCreation exeCr = client.execCreate(id, cmd, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.detach(), // needed to avoid connection reset on unix socket
ExecCreateParam.attachStdin());
stream = client.execStart(exeCr.id());
StringBuffer res = new StringBuffer();
while (stream.hasNext()) {
ByteBuffer b = stream.next().content();
byte[] buffer = new byte[b.remaining()];
b.get(buffer);
res.append(new String(buffer));
}
return res.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
stream.close();
}
return null;
}
use of com.spotify.docker.client.messages.ExecCreation in project linuxtools by eclipse.
the class DockerConnection method readContainerDirectory.
@SuppressWarnings("unused")
public List<ContainerFileProxy> readContainerDirectory(final String id, final String path) throws DockerException {
List<ContainerFileProxy> childList = new ArrayList<>();
try {
DockerClient copyClient = getClientCopy();
final ExecCreation execCreation = copyClient.execCreate(id, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
new String[] { "/bin/sh", "-c", "ls -l -F -L -Q " + path }, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr());
final String execId = execCreation.id();
final LogStream pty_stream = copyClient.execStart(execId);
try {
while (pty_stream.hasNext()) {
ByteBuffer b = pty_stream.next().content();
byte[] buffer = new byte[b.remaining()];
b.get(buffer);
String s = new String(buffer);
// $NON-NLS-1$
String[] lines = s.split("\\r?\\n");
for (String line : lines) {
if (// $NON-NLS-1$
line.trim().startsWith("total"))
// ignore the total line
continue;
// $NON-NLS-1$
String[] token = line.split("\\s+");
// $NON-NLS-1$
boolean isDirectory = token[0].startsWith("d");
// $NON-NLS-1$
boolean isLink = token[0].startsWith("l");
if (token.length > 8) {
// last token depends on whether we have a link or not
String link = null;
if (isLink) {
String linkname = token[token.length - 1];
if (linkname.endsWith("/")) {
// $NON-NLS-1$
linkname = linkname.substring(0, linkname.length() - 1);
isDirectory = true;
}
IPath linkPath = new Path(path);
linkPath = linkPath.append(linkname);
link = linkPath.toString();
String name = token[token.length - 3];
childList.add(new ContainerFileProxy(path, name, isDirectory, isLink, link));
} else {
String name = token[token.length - 1];
// remove quotes and any indicator char
name = name.substring(1, name.length() - (name.endsWith("\"") ? 1 : 2));
childList.add(new ContainerFileProxy(path, name, isDirectory));
}
}
}
}
} finally {
if (pty_stream != null)
pty_stream.close();
if (copyClient != null)
copyClient.close();
}
} catch (Exception e) {
// e.printStackTrace();
}
return childList;
}
use of com.spotify.docker.client.messages.ExecCreation in project docker-client by spotify.
the class DefaultDockerClientTest method testExec.
@Test
public void testExec() throws Exception {
requireDockerApiVersionAtLeast("1.15", "Exec");
// 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 ExecCreation execCreation = sut.execCreate(containerId, new String[] { "ls", "-la" }, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr());
final String execId = execCreation.id();
log.info("execId = {}", execId);
try (final LogStream stream = sut.execStart(execId)) {
final String output = stream.readFully();
log.info("Result:\n{}", output);
assertThat(output, containsString("total"));
}
}
use of com.spotify.docker.client.messages.ExecCreation in project zalenium by zalando.
the class DockerContainerClient method executeCommand.
public void executeCommand(String containerId, String[] command, boolean waitForExecution) {
final ExecCreation execCreation;
try {
execCreation = dockerClient.execCreate(containerId, command, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin());
final LogStream output = dockerClient.execStart(execCreation.id());
logger.info(String.format("%s %s", nodeId, Arrays.toString(command)));
if (waitForExecution) {
try {
String commandOutput = output.readFully();
logger.debug(String.format("%s %s", nodeId, commandOutput));
} catch (Exception e) {
logger.debug(nodeId + " Error while executing the output.readFully()", e);
ga.trackException(e);
}
}
} catch (DockerException | InterruptedException e) {
logger.debug(nodeId + " Error while executing the command", e);
ga.trackException(e);
}
}
use of com.spotify.docker.client.messages.ExecCreation in project zeppelin by apache.
the class DockerInterpreterProcess method execInContainer.
private void execInContainer(String containerId, String execCommand, boolean logout) throws DockerException, InterruptedException {
LOGGER.info("exec container commmand: " + execCommand);
final String[] command = { "sh", "-c", execCommand };
final ExecCreation execCreation = docker.execCreate(containerId, command, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr());
LogStream logStream = docker.execStart(execCreation.id());
while (logStream.hasNext() && logout) {
final String log = UTF_8.decode(logStream.next().content()).toString();
LOGGER.info(log);
}
}
Aggregations