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

The following examples show how to use com.spotify.docker.client.messages.Volume. 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: DockerSecretValidatorTest.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldValidateValidVolumeMountConfiguration() throws Exception {
    final Version version = mock(Version.class);
    final HashMap<String, String> properties = new HashMap<>();
    final VolumeList volumeList = mock(VolumeList.class);
    properties.put("Image", "alpine");
    properties.put("Mounts", "src=Foo, target=Bar");

    when(version.apiVersion()).thenReturn("1.27");
    when(dockerClient.version()).thenReturn(version);
    when(dockerClient.listVolumes()).thenReturn(volumeList);
    when(volumeList.volumes()).thenReturn(new ImmutableList.Builder<Volume>().add(Volume.builder().name("Foo").build()).build());

    ValidationResult validationResult = new DockerMountsValidator(createAgentRequest, dockerClientFactory).validate(properties);

    assertFalse(validationResult.hasErrors());
}
 
Example #2
Source File: DockerSecretValidatorTest.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldValidateInvalidDockerSecretsConfiguration() throws Exception {
    final Version version = mock(Version.class);
    final HashMap<String, String> properties = new HashMap<>();
    final VolumeList volumeList = mock(VolumeList.class);
    properties.put("Image", "alpine");
    properties.put("Mounts", "src=Foo");

    when(version.apiVersion()).thenReturn("1.27");
    when(dockerClient.version()).thenReturn(version);
    when(dockerClient.listVolumes()).thenReturn(volumeList);
    when(volumeList.volumes()).thenReturn(new ImmutableList.Builder<Volume>().add(Volume.builder().name("Foo").build()).build());

    ValidationResult validationResult = new DockerMountsValidator(createAgentRequest, dockerClientFactory).validate(properties);

    assertTrue(validationResult.hasErrors());
    assertThat(validationResult.allErrors(), contains(new ValidationError("Mounts", "Invalid mount target specification `src=Foo`. `target` has to be specified.")));
}
 
Example #3
Source File: DockerMountsTest.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildMountFromDockerMount() {
    final DockerMounts dockerMounts = DockerMounts.fromString("source=namedVolume, target=/path/in/container\ntype=bind, src=/path/in/host, target=/path/in/container2, readonly");
    final Volume volume = mock(Volume.class);

    when(volume.name()).thenReturn("namedVolume");

    final List<Mount> mounts = dockerMounts.toMount();

    assertThat(mounts, hasSize(2));
    assertThat(mounts.get(0).type(), is("volume"));
    assertThat(mounts.get(0).source(), is("namedVolume"));
    assertThat(mounts.get(0).target(), is("/path/in/container"));
    assertThat(mounts.get(0).readOnly(), is(false));

    assertThat(mounts.get(1).type(), is("bind"));
    assertThat(mounts.get(1).source(), is("/path/in/host"));
    assertThat(mounts.get(1).target(), is("/path/in/container2"));
    assertThat(mounts.get(1).readOnly(), is(true));
}
 
Example #4
Source File: DockerServiceElasticAgentTest.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStartContainerWithMountedVolume() throws Exception {
    requireDockerApiVersionAtLeast("1.26", "Docker volume mount.");

    final String volumeName = UUID.randomUUID().toString();

    final Volume volume = docker.createVolume(Volume.builder()
            .name(volumeName)
            .driver("local")
            .labels(Collections.singletonMap("cd.go.contrib.elasticagents.dockerswarm.elasticagent.DockerPlugin", ""))
            .build()
    );

    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "alpine:latest");
    properties.put("Mounts", "source=" + volumeName + ", target=/path/in/container");

    DockerService service = DockerService.create(new CreateAgentRequest("key", properties, "prod", new JobIdentifier(100L), new HashMap<>()), createClusterProfiles(), docker);
    services.add(service.name());

    final Service inspectServiceInfo = docker.inspectService(service.name());
    final Mount mount = inspectServiceInfo.spec().taskTemplate().containerSpec().mounts().get(0);

    assertThat(mount.source(), is(volumeName));
    assertThat(mount.type(), is("volume"));
}
 
Example #5
Source File: SysVolumeServiceImpl.java    From paas with Apache License 2.0 5 votes vote down vote up
@Override
public SysVolume createVolumes(String name, VolumeTypeEnum enums) {
    Volume.Builder builder = Volume.builder();

    if(StringUtils.isNotBlank(name)) {
        builder.name(name);
    }
    builder.driver("local");

    try {
        Volume created;

        // 根据数据卷类型创建
        if(enums == VolumeTypeEnum.CONTAINER) {
            created = dockerClient.createVolume(builder.build());
        } else if(enums == VolumeTypeEnum.SERVICE) {
            created = dockerSwarmClient.createVolume(builder.build());
        } else {
            throw new CustomException(ResultEnum.OTHER_ERROR.getCode(), "数据卷类型不被接收");
        }

        // 保存数据库
        SysVolume sysVolume = volume2SysVolume(created);
        volumesMapper.insert(sysVolume);

        return sysVolume;
    } catch (Exception e) {
        log.error("生成数据卷失败,错误位置:{},错误栈:{}",
                "SysVolumeServiceImpl.createVolumes()", HttpClientUtils.getStackTraceAsString(e));
        return null;
    }
}
 
Example #6
Source File: SysVolumeServiceImpl.java    From paas with Apache License 2.0 5 votes vote down vote up
private SysVolume volume2SysVolume(Volume volume) {
    SysVolume sysVolume = new SysVolume();
    sysVolume.setName(volume.name());
    sysVolume.setSource(volume.mountpoint());

    return sysVolume;
}
 
Example #7
Source File: SysVolumeServiceImpl.java    From paas with Apache License 2.0 5 votes vote down vote up
private SysVolumeVO sysVolume2VO(SysVolume sysVolume) throws Exception {
    Volume volume;
    SysVolumeVO volumeVO = new SysVolumeVO();
    int type = sysVolume.getType();

    if(type == VolumeTypeEnum.CONTAINER.getCode()) {
        volume = dockerClient.inspectVolume(sysVolume.getName());

        UserContainer container = containerMapper.selectById(sysVolume.getObjId());
        if(container != null) {
            volumeVO.setObjName(container.getName());
        }
    } else if(type == VolumeTypeEnum.SERVICE.getCode()) {
        volume = dockerSwarmClient.inspectVolume(sysVolume.getName());

        UserService service = serviceMapper.selectById(sysVolume.getObjId());
        if(service != null) {
            volumeVO.setObjName(service.getName());
        }
    } else {
        throw new CustomException(ResultEnum.OTHER_ERROR.getCode(), "数据卷类型不被接受");
    }

    BeanUtils.copyProperties(sysVolume, volumeVO);

    volumeVO.setVolume(volume);
    volumeVO.setTypeName(VolumeTypeEnum.getMessage(type));

    return volumeVO;
}
 
Example #8
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public Volume createVolume(final Volume volume) throws DockerException, InterruptedException {
  final WebTarget resource = resource().path("volumes").path("create");

  return request(POST, Volume.class, resource,
                 resource.request(APPLICATION_JSON_TYPE),
                 Entity.json(volume));
}
 
Example #9
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public Volume inspectVolume(final String volumeName)
    throws DockerException, InterruptedException {
  final WebTarget resource = resource().path("volumes").path(volumeName);
  try {
    return request(GET, Volume.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new VolumeNotFoundException(volumeName, e);
      default:
        throw e;
    }
  }
}
 
Example #10
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testInspectVolume() throws Exception {
  final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

  server.enqueue(new MockResponse()
      .setResponseCode(200)
      .addHeader("Content-Type", "application/json")
      .setBody(
          fixture("fixtures/1.33/inspectVolume.json")
      )
  );

  final Volume volume = dockerClient.inspectVolume("my-volume");

  assertThat(volume.name(), is("tardis"));
  assertThat(volume.driver(), is("custom"));
  assertThat(volume.mountpoint(), is("/var/lib/docker/volumes/tardis"));
  assertThat(volume.status(), is(ImmutableMap.of("hello", "world")));
  assertThat(volume.labels(), is(ImmutableMap.of(
      "com.example.some-label", "some-value",
      "com.example.some-other-label", "some-other-value"
  )));
  assertThat(volume.scope(), is("local"));
  assertThat(volume.options(), is(ImmutableMap.of(
      "foo", "bar",
      "baz", "qux"
  )));
}
 
Example #11
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Override
public Volume createVolume() throws DockerException, InterruptedException {
  return createVolume(Volume.builder().build());
}
 
Example #12
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Override
public void removeVolume(final Volume volume)
    throws DockerException, InterruptedException {
  removeVolume(volume.name());
}
 
Example #13
Source File: DockerClient.java    From docker-client with Apache License 2.0 votes vote down vote up
Volume createVolume() throws DockerException, InterruptedException; 
Example #14
Source File: DockerClient.java    From docker-client with Apache License 2.0 votes vote down vote up
Volume createVolume(Volume volume) throws DockerException, InterruptedException; 
Example #15
Source File: DockerClient.java    From docker-client with Apache License 2.0 votes vote down vote up
Volume inspectVolume(String volumeName) throws DockerException, InterruptedException; 
Example #16
Source File: DockerClient.java    From docker-client with Apache License 2.0 votes vote down vote up
void removeVolume(Volume volume) throws DockerException, InterruptedException;