@Override protected void waitUntilReady() { try { Unreliables.retryUntilTrue((int) startupTimeout.toMillis(), TimeUnit.MILLISECONDS, () -> { CountDownLatch latch = new CountDownLatch(1); FrameConsumerResultCallback callback = new FrameConsumerResultCallback() { @Override public void onNext(Frame frame) { if (frame != null && new String(frame.getPayload()).contains("Connected")) { latch.countDown(); } } }; try ( Closeable __ = dockerClient.logContainerCmd(containerId) .withFollowStream(true) .withSince(0) .withStdErr(true) .exec(callback) ) { return latch.await(1, TimeUnit.SECONDS); } }); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for log output", e); } } });
/** * {@inheritDoc} */ public void followOutput(DockerClient dockerClient, String containerId, Consumer<OutputFrame> consumer, OutputFrame.OutputType... types) { final LogContainerCmd cmd = dockerClient.logContainerCmd(containerId) .withFollowStream(true) .withSince(0); final FrameConsumerResultCallback callback = new FrameConsumerResultCallback(); for (OutputFrame.OutputType type : types) { callback.addConsumer(type, consumer); if (type == STDOUT) cmd.withStdOut(true); if (type == STDERR) cmd.withStdErr(true); } cmd.exec(callback); }
resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger())); resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger())); dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback);
docker .logContainerCmd(container.getId()) .withStdErr(true) .withStdOut(true) .withFollowStream(true)
public String containerLog(String containerId) { this.readWriteLock.readLock().lock(); try { return dockerClient.logContainerCmd(containerId) .withStdErr(true) .withStdOut(true) .exec(new LogContainerTestCallback()).awaitCompletion().toString(); } catch (InterruptedException e) { log.log(Level.SEVERE, e.getMessage()); return ""; } finally { this.readWriteLock.readLock().unlock(); } }
public String containerLog(String containerId) { this.readWriteLock.readLock().lock(); try { return dockerClient.logContainerCmd(containerId) .withStdErr(true) .withStdOut(true) .exec(new LogContainerTestCallback()).awaitCompletion().toString(); } catch (InterruptedException e) { log.log(Level.SEVERE, e.getMessage()); return ""; } finally { this.readWriteLock.readLock().unlock(); } }
Tail(DockerClient docker, Container container, Logger logger) { Preconditions.checkNotNull(container,"Container must not be null"); this.logger = logger; this.logContainerCmd = docker.logContainerCmd(container.getId()) .withStdErr(true) .withStdOut(true) .withTailAll(); }
private void startContainer(Map<String, String> env, Map<Integer, Integer> ports, Map<String, String> cmd, Consumer<String> log) { final CreateContainerResponse container = client.createContainerCmd(image) .withCmd(cmd.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(toList())) .withLabels(map(label, "true")) .withEnv(env.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(toList())) .withPortBindings(ports.entrySet().stream().map(e -> new PortBinding(Ports.Binding.bindPort(e.getKey()), ExposedPort.tcp(e.getValue()))).collect(toList())) .exec(); client.startContainerCmd(container.getId()).exec(); client.logContainerCmd(container.getId()).withFollowStream(true).withStdOut(true).withStdErr(true).exec( new LogContainerResultCallback() { public void onNext(Frame frame) { final String msg = new String(frame.getPayload()); log.accept(msg.endsWith("\n") ? msg.substring(0, msg.length() - 1) : msg); } }); } }
@Override public boolean await() { final DockerClient client = dockerClientExecutor.getDockerClient(); final CountDownLatch containerUp = new CountDownLatch(1); try (final LogContainerResultCallback callback = new LogContainerResultCallback(containerUp, this.occurrences)) { client.logContainerCmd(cube.getId()) .withStdErr(true) .withStdOut(true) .withFollowStream(true) .exec(callback); return containerUp.await(this.timeout, TimeUnit.SECONDS); } catch (IOException | InterruptedException e) { logger.log(Level.SEVERE, String.format("Log Await Strategy failed with %s", e.getMessage())); return false; } }
@Override public boolean await() { final DockerClient client = dockerClientExecutor.getDockerClient(); final CountDownLatch containerUp = new CountDownLatch(1); try (final LogContainerResultCallback callback = new LogContainerResultCallback(containerUp, this.occurrences)) { client.logContainerCmd(cube.getId()) .withStdErr(true) .withStdOut(true) .withFollowStream(true) .exec(callback); return containerUp.await(this.timeout, TimeUnit.SECONDS); } catch (IOException | InterruptedException e) { logger.log(Level.SEVERE, String.format("Log Await Strategy failed with %s", e.getMessage())); return false; } }
@Override protected void waitUntilReady() { try { Unreliables.retryUntilTrue((int) startupTimeout.toMillis(), TimeUnit.MILLISECONDS, () -> { CountDownLatch latch = new CountDownLatch(1); FrameConsumerResultCallback callback = new FrameConsumerResultCallback() { @Override public void onNext(Frame frame) { if (frame != null && new String(frame.getPayload()).contains("Connected")) { latch.countDown(); } } }; try (Closeable __ = dockerClient.logContainerCmd(containerId).withFollowStream(true).withSince(0).withStdErr(true).exec(callback)) { return latch.await(1, TimeUnit.SECONDS); } }); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for log output", e); } } });
public void downloadLog(String containerName, Path file) throws IOException { LogContainerRetrieverCallback loggingCallback = new LogContainerRetrieverCallback(file); getClient().logContainerCmd(containerName).withStdErr(true).withStdOut(true) .exec(loggingCallback); try { loggingCallback.awaitCompletion(); } catch (InterruptedException e) { log.warn("Interrupted while downloading logs for container {}", containerName); } }
public String readLogsFully(Integer waitTimeSeconds) throws InterruptedException { final LogContainerCmd logContainerCmd = client.logContainerCmd(containerInfo.getId()) .withStdOut(true) .withFollowStream(true) .withStdErr(true); final StringBuilder stringBuilder = new StringBuilder(); final LogContainerResultCallback resultCallback = new LogContainerResultCallback() { @Override public void onNext(final Frame item) { stringBuilder.append(item.toString() + System.lineSeparator()); } }; logContainerCmd.exec(resultCallback); resultCallback.awaitCompletion(waitTimeSeconds, TimeUnit.SECONDS); return stringBuilder.toString(); }
public void downloadLog(String containerName, Path file) throws IOException { LogContainerRetrieverCallback loggingCallback = new LogContainerRetrieverCallback(file); getClient().logContainerCmd(containerName).withStdErr(true).withStdOut(true) .exec(loggingCallback); try { loggingCallback.awaitCompletion(); } catch (InterruptedException e) { log.warn("Interrupted while downloading logs for container {}", containerName); } }
public static void showLog(DockerClient dockerClient, String containerId, boolean follow, int numberOfLines, LogContainerResultCallback logCallback) { dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).withFollowStream(follow).withTail(numberOfLines).exec(logCallback); }
public void updateStartedForTest() { containersStartedByExtensionPerTest = getContainers().stream() .filter(c -> !containersStartedByExtension.contains(c)) .collect(Collectors.toList()); containersInspectPerTest = containersStartedByExtensionPerTest.stream() .map(c -> dockerClient.inspectContainerCmd(c.getId()).exec()) .collect(Collectors.toList()); logsPerTest = containersStartedByExtensionPerTest.stream() .map(c -> dockerClient.logContainerCmd(c.getId()) .withStdOut(true) .withStdErr(true) .exec(new LogCallback())) .collect(Collectors.toList()); }
/** * Synchronized method for returning logs of docker container * * @param containerId - ID of the container ot lookup logs * @return list of strings, where every string is log line */ public static List<String> getDockerLogs(String containerId) { final List<String> logs = new ArrayList<>(); LogContainerCmd logContainerCmd = DockerClientFactory.build().logContainerCmd(containerId); logContainerCmd.withStdOut(true).withStdErr(true); try { logContainerCmd.exec(new LogContainerResultCallback() { @Override public void onNext(Frame item) { logs.add(item.toString()); } }).awaitCompletion(); } catch (InterruptedException e) { throw new MinimesosException("Failed to retrieve logs of container " + containerId, e); } return logs; }
@Override public Stream<String> logs(String containerId) { return dockerClient.logContainerCmd(containerId).withFollowStream(true) .withStdOut(true) .withStdErr(true) .exec(new StreamLog()) .stream(); }
public void updateStarted() { containersStartedByExtension = getContainers(); containersInspect = containersStartedByExtension.stream() .map(c -> dockerClient.inspectContainerCmd(c.getId()).exec()) .collect(Collectors.toList()); logs = containersStartedByExtension.stream() .map(c -> dockerClient.logContainerCmd(c.getId()) .withStdOut(true) .withStdErr(true) .exec(new LogCallback())) .collect(Collectors.toList()); }
public static void followOutput(DockerClient dockerClient, String containerId, Consumer<OutputFrame> consumer) { followOutput(dockerClient, containerId, consumer, STDOUT, STDERR); }