use of com.spotify.docker.client.LogStream in project linuxtools by eclipse.
the class DockerConnection method attachCommand.
public void attachCommand(final String id, final InputStream in, @SuppressWarnings("unused") final DockerConsoleOutputStream out) throws DockerException {
final byte[] prevCmd = new byte[1024];
try {
final LogStream pty_stream = client.attachContainer(id, AttachParameter.STDIN, AttachParameter.STDOUT, AttachParameter.STDERR, AttachParameter.STREAM, AttachParameter.LOGS);
final IDockerContainerInfo info = getContainerInfo(id);
final boolean isTtyEnabled = info.config().tty();
final boolean isOpenStdin = info.config().openStdin();
if (isTtyEnabled) {
openTerminal(pty_stream, info.name(), out);
}
// Data from the given input stream
// Written to container's STDIN
Thread t_in = new Thread(() -> {
byte[] buff = new byte[1024];
int n;
try {
WritableByteChannel pty_out = HttpHijackWorkaround.getOutputStream(pty_stream, getUri());
while ((n = in.read(buff)) != -1 && getContainerInfo(id).state().running()) {
synchronized (prevCmd) {
pty_out.write(ByteBuffer.wrap(buff, 0, n));
for (int i = 0; i < prevCmd.length; i++) {
prevCmd[i] = buff[i];
}
}
buff = new byte[1024];
}
} catch (Exception e) {
}
});
if (!isTtyEnabled && isOpenStdin) {
t_in.start();
}
} catch (Exception e) {
throw new DockerException(e.getMessage(), e.getCause());
}
}
use of com.spotify.docker.client.LogStream 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.LogStream in project linuxtools by eclipse.
the class DockerConnection method attachLog.
public void attachLog(final Closeable token, final String id, final OutputStream out, final OutputStream err) throws DockerException, InterruptedException, IOException {
try {
LogStream stream = ((DockerClient) token).logs(id, LogsParam.follow(), LogsParam.stdout(), LogsParam.stderr());
stream.attach(out, err);
stream.close();
} catch (com.spotify.docker.client.exceptions.DockerException e) {
throw new DockerException(e.getMessage(), e.getCause());
}
}
use of com.spotify.docker.client.LogStream in project linuxtools by eclipse.
the class DockerConnection method attachLog.
@Override
public void attachLog(final String id, final OutputStream out, final OutputStream err) throws DockerException, InterruptedException, IOException {
DockerClient copyClient;
try {
copyClient = getClientCopy();
LogStream stream = copyClient.logs(id, LogsParam.follow(), LogsParam.stdout(), LogsParam.stderr());
stream.attach(out, err);
stream.close();
} catch (com.spotify.docker.client.exceptions.DockerException e) {
throw new DockerException(e.getMessage(), e.getCause());
}
}
use of com.spotify.docker.client.LogStream in project helios by spotify.
the class TerminationTest method testTermOnExit.
@Test
public void testTermOnExit() throws Exception {
startDefaultMaster();
final String host = testHost();
startDefaultAgent(host);
final HeliosClient client = defaultClient();
awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);
// Note: signal 15 is SIGTERM
final Job jobToInterrupt = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("/bin/sh", "-c", "trap handle 15; handle() { echo term; exit 0; }; " + "while true; do sleep 1; done")).build();
final JobId jobId = createJob(jobToInterrupt);
deployJob(jobId, host);
awaitTaskState(jobId, host, RUNNING);
client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER, Deployment.EMPTY_DEPLOYER_MASTER, Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host);
final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED);
final String log;
try (final DockerClient dockerClient = getNewDockerClient();
LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) {
log = logs.readFully();
}
// Message expected, because the SIGTERM handler in the script should have run
assertEquals("term\n", log);
}
Aggregations