use of org.eclipse.che.plugin.docker.client.json.Volume in project che by eclipse.
the class MachineProviderImplTest method shouldAddBindMountAndRegularVolumesOnInstanceCreationFromRecipe.
@Test
public void shouldAddBindMountAndRegularVolumesOnInstanceCreationFromRecipe() throws Exception {
String[] bindMountVolumesFromMachine = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z" };
String[] volumesFromMachine = new String[] { "/projects", "/something", "/something/else" };
String[] expectedBindMountVolumes = new String[] { "/my/bind/mount1:/from/host1", "/my/bind/mount2:/from/host2:ro", "/my/bind/mount3:/from/host3:ro,Z" };
Map<String, Volume> expectedVolumes = Stream.of("/projects", "/something", "/something/else").collect(toMap(Function.identity(), v -> new Volume()));
provider = new MachineProviderBuilder().setDevMachineVolumes(emptySet()).setAllMachineVolumes(emptySet()).build();
CheServiceImpl service = createService();
service.setVolumes(Stream.concat(Stream.of(bindMountVolumesFromMachine), Stream.of(volumesFromMachine)).collect(Collectors.toList()));
createInstanceFromRecipe(service, true);
ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
verify(dockerConnector).createContainer(argumentCaptor.capture());
String[] actualBindMountVolumes = argumentCaptor.getValue().getContainerConfig().getHostConfig().getBinds();
Map<String, Volume> actualVolumes = argumentCaptor.getValue().getContainerConfig().getVolumes();
assertEquals(actualVolumes, expectedVolumes);
assertEqualsNoOrder(actualBindMountVolumes, expectedBindMountVolumes);
}
use of org.eclipse.che.plugin.docker.client.json.Volume in project che by eclipse.
the class MachineProviderImpl method createContainer.
private String createContainer(String workspaceId, String machineName, boolean isDev, String image, String networkName, CheServiceImpl service) throws IOException {
long machineMemorySwap = memorySwapMultiplier == -1 ? -1 : (long) (service.getMemLimit() * memorySwapMultiplier);
addSystemWideContainerSettings(workspaceId, isDev, service);
EndpointConfig endpointConfig = new EndpointConfig().withAliases(machineName).withLinks(toArrayIfNotNull(service.getLinks()));
NetworkingConfig networkingConfig = new NetworkingConfig().withEndpointsConfig(singletonMap(networkName, endpointConfig));
HostConfig hostConfig = new HostConfig();
hostConfig.withMemorySwap(machineMemorySwap).withMemory(service.getMemLimit()).withNetworkMode(networkName).withLinks(toArrayIfNotNull(service.getLinks())).withPortBindings(service.getPorts().stream().collect(toMap(Function.identity(), value -> new PortBinding[0]))).withVolumesFrom(toArrayIfNotNull(service.getVolumesFrom()));
ContainerConfig config = new ContainerConfig();
config.withImage(image).withExposedPorts(service.getExpose().stream().distinct().collect(toMap(Function.identity(), value -> emptyMap()))).withHostConfig(hostConfig).withCmd(toArrayIfNotNull(service.getCommand())).withEntrypoint(toArrayIfNotNull(service.getEntrypoint())).withLabels(service.getLabels()).withNetworkingConfig(networkingConfig).withEnv(service.getEnvironment().entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).toArray(String[]::new));
List<String> bindMountVolumes = new ArrayList<>();
Map<String, Volume> nonBindMountVolumes = new HashMap<>();
for (String volume : service.getVolumes()) {
// If volume contains colon then it is bind volume, otherwise - non bind-mount volume.
if (volume.contains(":")) {
bindMountVolumes.add(volume);
} else {
nonBindMountVolumes.put(volume, new Volume());
}
}
hostConfig.setBinds(bindMountVolumes.toArray(new String[bindMountVolumes.size()]));
config.setVolumes(nonBindMountVolumes);
addStaticDockerConfiguration(config);
return docker.createContainer(CreateContainerParams.create(config).withContainerName(service.getContainerName())).getId();
}
Aggregations