/** * @return is the container created? */ default boolean isCreated() { if (getContainerId() == null) { return false; } try { String status = getCurrentContainerInfo().getState().getStatus(); return "created".equalsIgnoreCase(status) || isRunning(); } catch (DockerException e) { return false; } }
public Optional<String> getContainerStatus(String containerId) { Optional<String> result = Optional.empty(); try { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(containerId).exec(); ContainerState status = inspectInfo.getState(); result = Optional.ofNullable(status.getStatus()); } catch (Exception exp) { logWarning(MODULE_NAME, exp.getMessage()); } return result; }
public List<Container> getRunningContainers() { return getContainers().stream() .filter(container -> { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(container.getId()).exec(); ContainerState containerState = inspectInfo.getState(); return containerState != null && containerState.getStatus() != null && MicroserviceState.fromText(containerState.getStatus()) == MicroserviceState.RUNNING; }) .collect(Collectors.toList()); }
return; if (!icr.getState().getStatus().equals("paused")) { return;
/** * gets {@link Container} status * * @param containerId - id of {@link Container} * @return {@link MicroserviceStatus} */ public MicroserviceStatus getMicroserviceStatus(String containerId) { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(containerId).exec(); ContainerState containerState = inspectInfo.getState(); MicroserviceStatus result = new MicroserviceStatus(); if (containerState != null) { if (containerState.getStartedAt() != null) { result.setStartTime(getStartedTime(containerState.getStartedAt())); } if (containerState.getStatus() != null) { MicroserviceState microserviceState = MicroserviceState.fromText(containerState.getStatus()); result.setStatus(MicroserviceState.RESTARTING.equals(microserviceState) && RestartStuckChecker.isStuck(containerId) ? MicroserviceState.STUCK_IN_RESTART : microserviceState); } result.setContainerId(containerId); result.setUsage(containerId); } return result; }
@Override protected ComputerLauncher createLauncher(DockerAPI api, String workdir, InspectContainerResponse inspect, TaskListener listener) throws IOException, InterruptedException { if ("exited".equals(inspect.getState().getStatus())) { // Something went wrong // FIXME report error "somewhere" visible to end user. LOGGER.error("Failed to launch docker SSH agent :" + inspect.getState().getExitCode()); throw new IOException("Failed to launch docker SSH agent. Container exited with status " + inspect.getState().getExitCode()); } LOGGER.debug("container created {}", inspect); final InetSocketAddress address = getBindingForPort(api, inspect, port); // Wait until sshd has started // TODO we could (also) have a more generic mechanism relying on healthcheck (inspect State.Health.Status) final PortUtils.ConnectionCheck connectionCheck = PortUtils.connectionCheck( address ) .withRetries( maxNumRetries ) .withEveryRetryWaitFor( retryWaitTime, TimeUnit.SECONDS ); if (!connectionCheck.execute() || !connectionCheck.useSSH().execute()) { throw new IOException("SSH service didn't start after " + retryWaitTime*maxNumRetries + "s."); } return sshKeyStrategy.getSSHLauncher(address, this); }
private Stream<Container> asContainer(String container) { return inspectContainerCmd(container) .map(response -> new Container( response.getConfig().getHostName(), new DockerImage(response.getConfig().getImage()), containerResourcesFromHostConfig(response.getHostConfig()), new ContainerName(decode(response.getName())), Container.State.valueOf(response.getState().getStatus().toUpperCase()), response.getState().getPid() )) .map(Stream::of) .orElse(Stream.empty()); }
/** * @return is the container created? */ default boolean isCreated() { if (getContainerId() == null) { return false; } try { String status = getCurrentContainerInfo().getState().getStatus(); return "created".equalsIgnoreCase(status) || isRunning(); } catch (DockerException e) { return false; } }
public ContainerAssert hasStatus(String status) { ContainerStateAssert stateAssert = new ContainerStateAssert(getContainerState()); stateAssert.isNotNull(); if (!Objects.areEqual(getContainerState().getStatus(), status)) { failWithMessage("Expected container's status to be %s but was %s", status, getContainerState().getStatus()); } return this; }
public final ComputerLauncher createLauncher(final DockerAPI api, @Nonnull final String containerId, String workdir, TaskListener listener) throws IOException, InterruptedException { final InspectContainerResponse inspect; try(final DockerClient client = api.getClient()) { inspect = client.inspectContainerCmd(containerId).exec(); } final ComputerLauncher launcher = createLauncher(api, workdir, inspect, listener); final Boolean running = inspect.getState().getRunning(); if (Boolean.FALSE.equals(running)) { listener.error("Container {} is not running. {}", containerId, inspect.getState().getStatus()); throw new IOException("Container is not running."); } return new DockerDelegatingComputerLauncher(launcher, api, containerId); }