@Override protected void waitUntilReady() { WaitingConsumer waitingConsumer = new WaitingConsumer(); LogUtils.followOutput(DockerClientFactory.instance().client(), waitStrategyTarget.getContainerId(), waitingConsumer); Predicate<OutputFrame> waitPredicate = outputFrame -> // (?s) enables line terminator matching (equivalent to Pattern.DOTALL) outputFrame.getUtf8String().matches("(?s)" + regEx); try { waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'"); } }
@Override protected void waitUntilReady() { final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts(); if (externalLivenessCheckPorts.isEmpty()) { log.debug("Liveness check ports of {} is empty. Not waiting.", waitStrategyTarget.getContainerInfo().getName()); return; } @SuppressWarnings("unchecked") List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts(); final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts); Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, internalPorts); Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts); try { Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call())); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for container port to open (" + waitStrategyTarget.getContainerIpAddress() + " ports: " + externalLivenessCheckPorts + " should be listening)"); } }
/** * @return the ports on which to check if the container is ready */ default Set<Integer> getLivenessCheckPortNumbers() { final Set<Integer> result = getExposedPorts().stream() .map(this::getMappedPort).distinct().collect(Collectors.toSet()); result.addAll(getBoundPortNumbers()); return result; } }
@Override protected boolean isReady() { String containerId = waitStrategyTarget.getContainerId(); log.debug("Check Aerospike container {} status", containerId); InspectContainerResponse containerInfo = waitStrategyTarget.getContainerInfo(); if (containerInfo == null) { log.debug("Aerospike container[{}] doesn't contain info. Abnormal situation, should not happen.", containerId); return false; } int port = getMappedPort(containerInfo.getNetworkSettings(), properties.port); String host = DockerClientFactory.instance().dockerHostIpAddress(); //TODO: Remove dependency to client https://www.aerospike.com/docs/tools/asmonitor/common_tasks.html try (AerospikeClient client = new AerospikeClient(host, port)) { return client.isConnected(); } catch (AerospikeException.Connection e) { log.debug("Aerospike container: {} not yet started. {}", containerId, e.getMessage()); } return false; }
private void tryPort(Integer internalPort) { String[][] commands = { {"/bin/sh", "-c", format("cat /proc/net/tcp{,6} | awk '{print $2}' | grep -i :%x && echo %s", internalPort, SUCCESS_MARKER)}, {"/bin/sh", "-c", format("nc -vz -w 1 localhost %d && echo %s", internalPort, SUCCESS_MARKER)}, {"/bin/bash", "-c", format("</dev/tcp/localhost/%d && echo %s", internalPort, SUCCESS_MARKER)} }; for (String[] command : commands) { try { if (ExecInContainerPattern.execInContainer(waitStrategyTarget.getContainerInfo(), command).getStdout().contains(SUCCESS_MARKER)) { return; } } catch (Exception e) { throw new IllegalStateException(e); } } throw new IllegalStateException("Socket not listening yet: " + internalPort); } }
/** * Build the URI on which to check if the container is ready. * * @param livenessCheckPort the liveness port * @return the liveness URI */ private URI buildLivenessUri(int livenessCheckPort) { final String scheme = (tlsEnabled ? "https" : "http") + "://"; final String host = waitStrategyTarget.getContainerIpAddress(); final String portSuffix; if ((tlsEnabled && 443 == livenessCheckPort) || (!tlsEnabled && 80 == livenessCheckPort)) { portSuffix = ""; } else { portSuffix = ":" + String.valueOf(livenessCheckPort); } return URI.create(scheme + host + portSuffix + path); }
/** * @return the ports on which to check if the container is ready */ protected Set<Integer> getLivenessCheckPorts() { return waitStrategyTarget.getLivenessCheckPortNumbers(); }
private Set<Integer> getInternalPorts(Set<Integer> externalLivenessCheckPorts, List<Integer> exposedPorts) { return exposedPorts.stream() .filter(it -> externalLivenessCheckPorts.contains(waitStrategyTarget.getMappedPort(it))) .collect(Collectors.toSet()); } }
@Override protected void waitUntilReady() { final String containerName = waitStrategyTarget.getContainerInfo().getName();
@Before public void setUp() throws Exception { listeningSocket1 = new ServerSocket(0); listeningSocket2 = new ServerSocket(0); nonListeningSocket = new ServerSocket(0); nonListeningSocket.close(); mockContainer = mock(WaitStrategyTarget.class); when(mockContainer.getContainerIpAddress()).thenReturn("127.0.0.1"); }
/** * @return the ports on which to check if the container is ready * @deprecated use {@link #getLivenessCheckPortNumbers()} instead */ @NotNull @NonNull @Deprecated protected Set<Integer> getLivenessCheckPorts() { final Set<Integer> result = WaitStrategyTarget.super.getLivenessCheckPortNumbers(); // for backwards compatibility if (this.getLivenessCheckPort() != null) { result.add(this.getLivenessCheckPort()); } return result; }
private Set<Integer> getInternalPorts(Set<Integer> externalLivenessCheckPorts, List<Integer> exposedPorts) { return exposedPorts.stream().filter(it -> externalLivenessCheckPorts.contains(waitStrategyTarget.getMappedPort(it))).collect(Collectors.toSet()); } }
@Override protected void waitUntilReady() { long seconds = startupTimeout.getSeconds(); try { Unreliables.retryUntilTrue((int) seconds, TimeUnit.SECONDS, () -> getRateLimiter().getWhenReady(this::isReady)); } catch (TimeoutException e) { throw new ContainerLaunchException( format("[%s] notifies that container[%s] is not ready after [%d] seconds, container cannot be started.", getContainerType(), waitStrategyTarget.getContainerId(), seconds)); } }
@Override protected void waitUntilReady() { final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts(); if (externalLivenessCheckPorts.isEmpty()) { log.debug("Liveness check ports of {} is empty. Not waiting.", waitStrategyTarget.getContainerInfo().getName()); return; } @SuppressWarnings("unchecked") List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts(); final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts); Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, internalPorts); Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts); try { Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call())); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for container port to open (" + waitStrategyTarget.getContainerIpAddress() + " ports: " + externalLivenessCheckPorts + " should be listening)"); } }
private void tryPort(Integer internalPort) { String[][] commands = {{"/bin/sh", "-c", format("cat /proc/net/tcp{,6} | awk \'{print $2}\' | grep -i :%x && echo %s", internalPort, SUCCESS_MARKER)}, {"/bin/sh", "-c", format("nc -vz -w 1 localhost %d && echo %s", internalPort, SUCCESS_MARKER)}, {"/bin/bash", "-c", format("</dev/tcp/localhost/%d && echo %s", internalPort, SUCCESS_MARKER)}}; for (String[] command : commands) { try { if (ExecInContainerPattern.execInContainer(waitStrategyTarget.getContainerInfo(), command).getStdout().contains(SUCCESS_MARKER)) { return; } } catch (Exception e) { throw new IllegalStateException(e); } } throw new IllegalStateException("Socket not listening yet: " + internalPort); }
/** * @return the ports on which to check if the container is ready */ default Set<Integer> getLivenessCheckPortNumbers() { final Set<Integer> result = getExposedPorts().stream() .map(this::getMappedPort).distinct().collect(Collectors.toSet()); result.addAll(getBoundPortNumbers()); return result; } }
/** * Build the URI on which to check if the container is ready. * * @param livenessCheckPort the liveness port * @return the liveness URI */ private URI buildLivenessUri(int livenessCheckPort) { final String scheme = (tlsEnabled ? "https" : "http") + "://"; final String host = waitStrategyTarget.getContainerIpAddress(); final String portSuffix; if ((tlsEnabled && 443 == livenessCheckPort) || (!tlsEnabled && 80 == livenessCheckPort)) { portSuffix = ""; } else { portSuffix = ":" + String.valueOf(livenessCheckPort); } return URI.create(scheme + host + portSuffix + path); }
/** * @return the ports on which to check if the container is ready */ protected Set<Integer> getLivenessCheckPorts() { return waitStrategyTarget.getLivenessCheckPortNumbers(); }
protected boolean isReady() { String commandName = getContainerType(); String containerId = waitStrategyTarget.getContainerId(); log.debug("{} execution of command {} for container id: {} ", commandName, containerId); ExecCmdResult healthCheckCmdResult = ContainerUtils.execCmd(DockerClientFactory.instance().client(), containerId, getCheckCommand()); log.debug("{} executed with exitCode: {}, output: {}", commandName, healthCheckCmdResult.getExitCode(), healthCheckCmdResult.getOutput()); if (healthCheckCmdResult.getExitCode() != 0) { log.debug("{} executed with exitCode !=0, considering status as unknown", commandName); return false; } log.debug("{} command executed, considering container {} successfully started", commandName, containerId); return true; } }
@Override protected void waitUntilReady() { final String containerName = waitStrategyTarget.getContainerInfo().getName(); final Integer livenessCheckPort = livenessPort.map(waitStrategyTarget::getMappedPort).orElseGet(() -> { final Set<Integer> livenessCheckPorts = getLivenessCheckPorts();