@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); }
.withStdErr(true) .withStdOut(true) .withFollowStream(true) .withTailAll() .exec(loggingCallback)
@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; } }
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 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 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(); }
private static void waitForContainer( final CreateContainerResponse containerResponse, final DockerClient client, final DockerClientConfig config ) throws InterruptedException { final CountDownLatch countDownLatch = new CountDownLatch(1); final LogContainerCmd logContainerCmd = client.logContainerCmd(containerResponse.getId()) .withStdOut(true) .withFollowStream(true) .withStdErr(true); logContainerCmd.exec(new LogContainerResultCallback() { @Override public void onNext(final Frame item) { if (LogMatcher.matches(item.toString(), config.getWaitForLogLine(), config.getMatchFormat())) { countDownLatch.countDown(); } } @Override public void onComplete() { countDownLatch.countDown(); } }); if (!countDownLatch.await(config.getMaxWaitLogSeconds(), TimeUnit.SECONDS)) { logger.warn("Didn't find log line in a timely fashion, continuing"); } }
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); }
@java.lang.SuppressWarnings("all") public static Optional<String> getDefaultGateway() { java.lang.Object value = DockerClientConfigUtils.defaultGateway.get(); if (value == null) { synchronized (DockerClientConfigUtils.defaultGateway) { value = DockerClientConfigUtils.defaultGateway.get(); if (value == null) { final Optional<String> actualValue = Optional.ofNullable(DockerClientFactory.instance().runInsideDocker(cmd -> cmd.withCmd("sh", "-c", "ip route|awk \'/default/ { print $3 }\'"), (client, id) -> { try { LogToStringContainerCallback loggingCallback = new LogToStringContainerCallback(); client.logContainerCmd(id).withStdOut(true).withFollowStream(true).exec(loggingCallback).awaitStarted(); loggingCallback.awaitCompletion(3, SECONDS); return loggingCallback.toString(); } catch (Exception e) { log.warn("Can\'t parse the default gateway IP", e); return null; } })).map(StringUtils::trimToEmpty).filter(StringUtils::isNotBlank); value = actualValue == null ? DockerClientConfigUtils.defaultGateway : actualValue; DockerClientConfigUtils.defaultGateway.set(value); } } } return (Optional<String>) (value == DockerClientConfigUtils.defaultGateway ? null : value); } }
public void copyLog(String containerId, boolean follow, boolean stdout, boolean stderr, boolean timestamps, int tail, OutputStream outputStream) throws IOException { this.readWriteLock.readLock().lock(); try { LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId).withStdErr(false).withStdOut(false); logContainerCmd.withFollowStream(follow); logContainerCmd.withStdOut(stdout); logContainerCmd.withStdErr(stderr); logContainerCmd.withTimestamps(timestamps); if (tail < 0) { logContainerCmd.withTailAll(); } else { logContainerCmd.withTail(tail); } OutputStreamLogsResultCallback outputStreamLogsResultCallback = new OutputStreamLogsResultCallback(outputStream); logContainerCmd.exec(outputStreamLogsResultCallback); try { outputStreamLogsResultCallback.awaitCompletion(); } catch (InterruptedException e) { throw new IOException(e); } } finally { this.readWriteLock.readLock().unlock(); } }
public void copyLog(String containerId, boolean follow, boolean stdout, boolean stderr, boolean timestamps, int tail, OutputStream outputStream) throws IOException { this.readWriteLock.readLock().lock(); try { LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId).withStdErr(false).withStdOut(false); logContainerCmd.withFollowStream(follow); logContainerCmd.withStdOut(stdout); logContainerCmd.withStdErr(stderr); logContainerCmd.withTimestamps(timestamps); if (tail < 0) { logContainerCmd.withTailAll(); } else { logContainerCmd.withTail(tail); } OutputStreamLogsResultCallback outputStreamLogsResultCallback = new OutputStreamLogsResultCallback(outputStream); logContainerCmd.exec(outputStreamLogsResultCallback); try { outputStreamLogsResultCallback.awaitCompletion(); } catch (InterruptedException e) { throw new IOException(e); } } finally { this.readWriteLock.readLock().unlock(); } }
@Override public Stream<String> logs(String containerId) { return dockerClient.logContainerCmd(containerId).withFollowStream(true) .withStdOut(true) .withStdErr(true) .exec(new StreamLog()) .stream(); }
.withStdOut(true) .withTailAll() .withFollowStream(true);
public static void followOutput(DockerClient dockerClient, String containerId, Consumer<OutputFrame> consumer) { followOutput(dockerClient, containerId, consumer, STDOUT, STDERR); }
}); dockerClient.startContainerCmd(exec.getId()).exec(); dockerClient.logContainerCmd(exec.getId()).withFollowStream(true).withStdErr(true).withStdOut(true).exec(callback); while (dockerClient.inspectContainerCmd(exec.getId()).exec().getState().getRunning()) { try {
logContainerCmd.withFollowStream(followStream);