com.spotify.docker.client.messages.ImageInfo Java Examples

The following examples show how to use com.spotify.docker.client.messages.ImageInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskConfig.java    From helios with Apache License 2.0 6 votes vote down vote up
/**
 * Create docker container configuration for a job.
 *
 * @param imageInfo The ImageInfo object.
 *
 * @return The ContainerConfig object.
 */
public ContainerConfig containerConfig(final ImageInfo imageInfo,
                                       final Optional<String> dockerVersion) {
  final ContainerConfig.Builder builder = ContainerConfig.builder();

  builder.image(job.getImage());
  builder.cmd(job.getCommand());
  builder.hostname(job.getHostname());
  builder.env(containerEnvStrings());
  builder.exposedPorts(containerExposedPorts());
  builder.volumes(volumes().keySet());
  builder.labels(job.getLabels());

  for (final ContainerDecorator decorator : containerDecorators) {
    decorator.decorateContainerConfig(job, imageInfo, dockerVersion, builder);
  }

  return builder.build();
}
 
Example #2
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesNotPullPresentProbeImage() throws Exception {
  when(this.dockerClient.inspectImage(HeliosSoloDeployment.PROBE_IMAGE))
      .thenReturn(mock(ImageInfo.class));

  buildHeliosSoloDeployment();

  verify(this.dockerClient, never()).pull(HeliosSoloDeployment.PROBE_IMAGE);
}
 
Example #3
Source File: SupervisorTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySupervisorRestartsExitedContainer() throws Exception {
  final String containerId1 = "deadbeef1";
  final String containerId2 = "deadbeef2";

  final ContainerCreation createResponse1 = ContainerCreation.builder().id(containerId1).build();
  final ContainerCreation createResponse2 = ContainerCreation.builder().id(containerId2).build();

  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(createResponse1);

  final ImageInfo imageInfo = mock(ImageInfo.class);
  when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);

  when(docker.inspectContainer(eq(containerId1))).thenReturn(runningResponse);

  final SettableFuture<ContainerExit> waitFuture1 = SettableFuture.create();
  final SettableFuture<ContainerExit> waitFuture2 = SettableFuture.create();
  when(docker.waitContainer(containerId1)).thenAnswer(futureAnswer(waitFuture1));
  when(docker.waitContainer(containerId2)).thenAnswer(futureAnswer(waitFuture2));

  // Start the job
  sut.setGoal(START);
  verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
  verify(docker, timeout(30000)).startContainer(eq(containerId1));
  verify(docker, timeout(30000)).waitContainer(containerId1);

  // Indicate that the container exited
  when(docker.inspectContainer(eq(containerId1))).thenReturn(stoppedResponse);
  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(createResponse2);
  when(docker.inspectContainer(eq(containerId2))).thenReturn(runningResponse);
  waitFuture1.set(ContainerExit.create(1L));

  // Verify that the container was restarted
  verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
  verify(docker, timeout(30000)).startContainer(eq(containerId2));
  verify(docker, timeout(30000)).waitContainer(containerId2);
}
 
Example #4
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Optional<ImageInfo> pullAndInspect() {
  try {
    dockerClient.pull(image);

    return inspectImage();
  }
  catch (DockerException | InterruptedException e) {
    log.error("Failed to pull docker image '{}'", image, e);
  }

  return empty();
}
 
Example #5
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Optional<ImageInfo> inspectImage() {
  try {
    return ofNullable(dockerClient.inspectImage(image));
  }
  catch (DockerException | InterruptedException e) {
    log.warn("Unable to inspect image '{}'", image);
    log.debug("Exception is : ", e);
  }

  return empty();
}
 
Example #6
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public ImageInfo inspectImage(final String image) throws DockerException, InterruptedException {
  try {
    final WebTarget resource = resource().path("images").path(image).path("json");
    return request(GET, ImageInfo.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ImageNotFoundException(image, e);
      default:
        throw e;
    }
  }
}
 
Example #7
Source File: SyslogRedirectingContainerDecorator.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
                                    ContainerConfig.Builder containerConfigBuilder) {
  if (!useSyslogRedirector(dockerVersion)) {
    return;
  }

  final ContainerConfig imageConfig = imageInfo.config();

  // Inject syslog-redirector in the entrypoint to capture std out/err
  final String syslogRedirectorPath = Optional.fromNullable(job.getEnv().get("SYSLOG_REDIRECTOR"))
      .or("/helios/syslog-redirector");

  final List<String> entrypoint = Lists.newArrayList(syslogRedirectorPath,
      "-h", syslogHostPort,
      "-n", job.getId().toString(),
      "--");
  if (imageConfig.entrypoint() != null) {
    entrypoint.addAll(imageConfig.entrypoint());
  }
  containerConfigBuilder.entrypoint(entrypoint);

  final ContainerConfig containerConfig = containerConfigBuilder.build();

  // If there's no explicit container cmd specified, copy over the one from the image.
  // Only setting the entrypoint causes dockerd to not use the image cmd.
  if ((containerConfig.cmd() == null || containerConfig.cmd().isEmpty())
      && imageConfig.cmd() != null) {
    containerConfigBuilder.cmd(imageConfig.cmd());
  }

  final ImmutableSet.Builder<String> volumesBuilder = ImmutableSet.builder();
  if (containerConfig.volumes() != null) {
    volumesBuilder.addAll(containerConfig.volumes());
  }
  volumesBuilder.add("/helios");
  containerConfigBuilder.volumes(volumesBuilder.build());
}
 
Example #8
Source File: TaskRunner.java    From helios with Apache License 2.0 5 votes vote down vote up
private String startContainer(final String image, final Optional<String> dockerVersion)
    throws InterruptedException, DockerException {

  // Get container image info
  final ImageInfo imageInfo = docker.inspectImage(image);
  if (imageInfo == null) {
    throw new HeliosRuntimeException("docker inspect image returned null on image " + image);
  }

  // Create container
  final HostConfig hostConfig = config.hostConfig(dockerVersion);
  final ContainerConfig containerConfig = config.containerConfig(imageInfo, dockerVersion)
      .toBuilder()
      .hostConfig(hostConfig)
      .build();
  listener.creating();
  final ContainerCreation container = docker.createContainer(containerConfig, containerName);
  log.info("created container: {}: {}, {}", config, container, containerConfig);
  listener.created(container.id());

  // Start container
  log.info("starting container: {}: {} {}", config, container.id(), hostConfig);
  listener.starting();
  docker.startContainer(container.id());
  log.info("started container: {}: {}", config, container.id());
  listener.started();

  return container.id();
}
 
Example #9
Source File: TaskConfigTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testContainerConfig() throws Exception {
  final TaskConfig taskConfig = TaskConfig.builder()
      .namespace("test")
      .host(HOST)
      .job(JOB)
      .build();

  final ImageInfo imageInfo = mock(ImageInfo.class);
  final ContainerConfig containerConfig = taskConfig.containerConfig(
      imageInfo, Optional.absent());
  assertThat(ImmutableMap.copyOf(containerConfig.labels()), equalTo(LABELS));
}
 
Example #10
Source File: BindVolumeContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
                                    ContainerConfig.Builder containerConfig) {
}
 
Example #11
Source File: SupervisorTest.java    From helios with Apache License 2.0 4 votes vote down vote up
@Test
public void verifySupervisorStopsDockerContainerWithConfiguredKillTime() throws Exception {
  final String containerId = "deadbeef";

  final Job longKillTimeJob = Job.newBuilder()
      .setName(NAME)
      .setCommand(COMMAND)
      .setImage(IMAGE)
      .setVersion(VERSION)
      .setSecondsToWaitBeforeKill(30)
      .build();

  mockTaskStatus(longKillTimeJob.getId());

  final Supervisor longKillTimeSupervisor = createSupervisor(longKillTimeJob);

  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(ContainerCreation.builder().id(containerId).build());

  final ImageInfo imageInfo = mock(ImageInfo.class);
  when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);

  // Have waitContainer wait forever.
  final SettableFuture<ContainerExit> waitFuture = SettableFuture.create();
  when(docker.waitContainer(containerId)).thenAnswer(futureAnswer(waitFuture));

  // Start the job (so that a runner exists)
  longKillTimeSupervisor.setGoal(START);
  when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);

  // This is already verified above, but it works as a hack to wait for the model/docker state
  // to converge in such a way that a setGoal(STOP) will work. :|
  verify(docker, timeout(30000)).waitContainer(containerId);

  // Stop the job
  longKillTimeSupervisor.setGoal(STOP);
  verify(docker, timeout(30000)).stopContainer(
      eq(containerId), eq(longKillTimeJob.getSecondsToWaitBeforeKill()));

  // Change docker container state to stopped now that it was killed
  when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);
}
 
Example #12
Source File: SupervisorTest.java    From helios with Apache License 2.0 4 votes vote down vote up
@Test
public void verifySupervisorStartsAndStopsDockerContainer() throws Exception {
  final String containerId = "deadbeef";

  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(ContainerCreation.builder().id(containerId).build());

  final ImageInfo imageInfo = mock(ImageInfo.class);
  when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);

  // Have waitContainer wait forever.
  final SettableFuture<ContainerExit> waitFuture = SettableFuture.create();
  when(docker.waitContainer(containerId)).thenAnswer(futureAnswer(waitFuture));

  // Start the job
  sut.setGoal(START);

  // Verify that the container is created
  verify(docker, timeout(30000)).createContainer(containerConfigCaptor.capture(),
      containerNameCaptor.capture());
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(CREATING)
          .setContainerId(null)
          .setEnv(ENV)
          .build())
  );
  final ContainerConfig containerConfig = containerConfigCaptor.getValue();
  assertEquals(IMAGE, containerConfig.image());
  assertEquals(EXPECTED_CONTAINER_ENV, ImmutableSet.copyOf(containerConfig.env()));
  final String containerName = containerNameCaptor.getValue();

  assertEquals(JOB.getId().toShortString(), shortJobIdFromContainerName(containerName));

  // Verify that the container is started
  verify(docker, timeout(30000)).startContainer(eq(containerId));
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(STARTING)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );
  when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);

  verify(docker, timeout(30000)).waitContainer(containerId);
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(RUNNING)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );

  // Stop the job
  sut.setGoal(STOP);
  verify(docker, timeout(30000)).stopContainer(
      eq(containerId), eq(Supervisor.DEFAULT_SECONDS_TO_WAIT_BEFORE_KILL));

  // Change docker container state to stopped now that it was killed
  when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);

  // Verify that the pulling state is signalled
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(PULLING_IMAGE)
          .setContainerId(null)
          .setEnv(ENV)
          .build())
  );

  // Verify that the STOPPING and STOPPED states are signalled
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(STOP)
          .setState(STOPPING)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );

  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(STOP)
          .setState(STOPPED)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );
}
 
Example #13
Source File: TaskRunnerTest.java    From helios with Apache License 2.0 4 votes vote down vote up
@Test
public void testContainerNotRunningVariation() throws Throwable {
  final TaskRunner.NopListener mockListener = mock(TaskRunner.NopListener.class);
  final ImageInfo mockImageInfo = mock(ImageInfo.class);
  final ContainerCreation mockCreation = mock(ContainerCreation.class);
  final HealthChecker mockHealthChecker = mock(HealthChecker.class);

  final ContainerState stoppedState = mock(ContainerState.class);
  when(stoppedState.running()).thenReturn(false);
  when(stoppedState.error()).thenReturn("container is a potato");
  final ContainerInfo stopped = mock(ContainerInfo.class);
  when(stopped.state()).thenReturn(stoppedState);

  when(mockCreation.id()).thenReturn("potato");
  when(mockDocker.inspectContainer(anyString())).thenReturn(stopped);
  when(mockDocker.inspectImage(IMAGE)).thenReturn(mockImageInfo);
  when(mockDocker.createContainer(any(ContainerConfig.class), anyString()))
      .thenReturn(mockCreation);
  when(mockHealthChecker.check(anyString())).thenReturn(false);

  final TaskRunner tr = TaskRunner.builder()
      .delayMillis(0)
      .config(TaskConfig.builder()
          .namespace("test")
          .host(HOST)
          .job(JOB)
          .containerDecorators(ImmutableList.of(containerDecorator))
          .build())
      .docker(mockDocker)
      .listener(mockListener)
      .healthChecker(mockHealthChecker)
      .build();

  tr.run();

  try {
    tr.resultFuture().get();
    fail("this should throw");
  } catch (Exception t) {
    assertTrue(t instanceof ExecutionException);
    assertEquals(RuntimeException.class, t.getCause().getClass());
    verify(mockListener).failed(t.getCause(), "container is a potato");
  }
}
 
Example #14
Source File: SyslogRedirectingContainerDecoratorTest.java    From helios with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  imageInfo = mock(ImageInfo.class);
  when(imageInfo.config()).thenReturn(mock(ContainerConfig.class));
}
 
Example #15
Source File: ContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
ContainerConfig.Builder containerConfig);
 
Example #16
Source File: NoOpContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
                                    ContainerConfig.Builder containerConfig) {
  //noop
}
 
Example #17
Source File: RepositoryImageServiceImpl.java    From paas with Apache License 2.0 4 votes vote down vote up
/**
 * RepositoryImage --> SysImage
 * @author jitwxs
 * @since 2018/7/5 23:28
 */
private SysImage repositoryImage2SysImage(RepositoryImage repositoryImage) {
    SysImage sysImage = new SysImage();
    String fullName = repositoryImage.getFullName();

    // 1、设置公共信息
    sysImage.setFullName(fullName);
    sysImage.setName(repositoryImage.getName());
    sysImage.setTag(repositoryImage.getTag());
    sysImage.setRepo(repositoryImage.getRepo());

    // 2、设置Type
    // Hub上的镜像拉取下来类型是公共镜像
    sysImage.setType(ImageTypeEnum.LOCAL_PUBLIC_IMAGE.getCode());

    try {
        // 3、设置其他信息
        List<Image> images = dockerClient.listImages(DockerClient.ListImagesParam.byName(fullName));
        if(images.size()  != 0) {
            Image image = images.get(0);
            // 设置ImageId
            sysImage.setImageId(splitImageId(image.id()));
            // 设置大小
            sysImage.setSize(image.size());
            // 设置虚拟大小
            sysImage.setVirtualSize(image.virtualSize());
            // 设置Label
            sysImage.setLabels(JsonUtils.mapToJson(image.labels()));
            // 设置父节点
            sysImage.setParentId(image.parentId());
            sysImage.setCreateDate(new Date());
        }
        // 设置CMD
        ImageInfo info = dockerClient.inspectImage(fullName);
        sysImage.setCmd(JsonUtils.objectToJson(info.containerConfig().cmd()));
    } catch (Exception e) {
        log.error("读取镜像信息异错误,错误位置:{},错误栈:{}",
                "RepositoryImageServiceImpl.repositoryImage2SysImage()", HttpClientUtils.getStackTraceAsString(e));
    }

    return sysImage;
}
 
Example #18
Source File: AddExtraHostContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateContainerConfig(final Job job, final ImageInfo imageInfo,
                                    final Optional<String> dockerVersion,
                                    final ContainerConfig.Builder containerConfig) {
  //do nothing
}
 
Example #19
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private Optional<ImageInfo> pullIfNotExist() {
  return ofNullable(inspectImage().orElseGet(() -> {
    log.info("Image '{}' is not local, attempting to pull", image);
    return pull().orElse(null);
  }));
}
 
Example #20
Source File: DockerClient.java    From docker-client with Apache License 2.0 2 votes vote down vote up
/**
 * Inspect a docker container image.
 *
 * @param image The image to inspect.
 * @return Info about the image.
 * @throws ImageNotFoundException
 *                            if image was not found (404)
 * @throws DockerException      if a server error occurred (500)
 * @throws InterruptedException If the thread is interrupted
 */
ImageInfo inspectImage(String image) throws DockerException, InterruptedException;
 
Example #21
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Pull a docker container image.
 *
 * @return Optional of {@link ImageInfo}
 */
public Optional<ImageInfo> pull() {
  return pullAndInspect();
}