Skip to content

Commit

Permalink
Fix fabric8io#986 : Create volumes with proper configuration during "…
Browse files Browse the repository at this point in the history
…docker:start"
  • Loading branch information
rohanKanojia committed May 16, 2018
1 parent b57bc9e commit a770153
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* **0.25.2** (2018-04-14)
- Fix for docker login issue with index.docker.io using a credential helper ([#946](https://github.com/fabric8io/docker-maven-plugin/issues/946))
- Fix for creating volumes with proper configuration during "docker:start" ([#986](https://github.com/fabric8io/docker-maven-plugin/issues/986))

* **0.25.1** (2018-04-12)
- Fix regression which broke labels and env with space ([#988](https://github.com/fabric8io/docker-maven-plugin/issues/988))
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/fabric8/maven/docker/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ private Queue<ImageConfiguration> prepareStart(ServiceHub hub, QueryService quer
queryService.hasImage(imageConfig.getName()));

NetworkConfig config = runConfig.getNetworkingConfig();
RunVolumeConfiguration runVolumeConfig = runConfig.getVolumeConfiguration();
if(!runVolumeConfig.getBind().isEmpty()) {
List<VolumeConfiguration> volumes = getVolumes();
runService.createVolumesAsPerVolumeBinds(hub, runVolumeConfig.getBind(), volumes);
}
if (autoCreateCustomNetworks && config.isCustomNetwork()) {
runService.createCustomNetworkIfNotExistant(config.getCustomNetwork());
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/fabric8/maven/docker/service/RunService.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,40 @@ public Void call() throws Exception {

return waited;
}

/**
* Creates a Volume if a volume is referred to during startup in bind mount mapping and
* a VolumeConfiguration exists
*
* @param hub Service hub
* @param volumeBinds volume binds present in ImageConfiguration
* @param volumesConfigs VolumeConfigs present
* @return List of volumes created
* @throws DockerAccessException
*/
public List<String> createVolumesAsPerVolumeBinds(ServiceHub hub, List<String> volumeBinds, List<VolumeConfiguration> volumesConfigs)
throws DockerAccessException {
Map<String, Integer> indexMap = new HashMap();
List<String> volumesCreated = new ArrayList<>();

for(Integer index = 0; index < volumesConfigs.size(); index++) {
indexMap.put(volumesConfigs.get(index).getName(), index);
}

for(String volumeBind : volumeBinds) {
if(volumeBind.contains(":")) {
volumeBind = volumeBind.substring(0, volumeBind.indexOf(":"));
}
Integer volumeConfigIndex = indexMap.get(volumeBind);
if(volumeConfigIndex != null && volumeConfigIndex >= 0 && volumeConfigIndex < volumesConfigs.size()) {
VolumeConfiguration aVolumeConfig = volumesConfigs.get(volumeConfigIndex);
hub.getVolumeService().createVolume(aVolumeConfig);
volumesCreated.add(aVolumeConfig.getName());
} else {
log.warn("No volumeBind found with name : " + volumeBind + " " + indexMap.toString());
}
}

return volumesCreated;
}
}
24 changes: 24 additions & 0 deletions src/test/java/io/fabric8/maven/docker/service/RunServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import io.fabric8.maven.docker.log.LogOutputSpecFactory;
import mockit.*;
import org.apache.commons.io.IOUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.junit.Before;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand All @@ -27,6 +29,12 @@ public class RunServiceTest {

private ContainerCreateConfig containerConfig;

@Mocked
private MavenProject project;

@Mocked
private MavenSession session;

@Mocked
private DockerAccess docker;

Expand Down Expand Up @@ -91,6 +99,11 @@ public void testCreateContainerAllConfig() throws Exception {
private String container = "testContainer";
private int SHUTDOWN_WAIT = 500;
private int KILL_AFTER = 1000;
private VolumeConfiguration volumeConfiguration = new VolumeConfiguration.Builder()
.name("sqlserver-backup-dev")
.driver("rexray")
.opts(Collections.singletonMap("size", "50"))
.build();

@Test
public void shutdownWithoutKeepingContainers() throws Exception {
Expand Down Expand Up @@ -211,6 +224,17 @@ public void testWithException() throws Exception {
runService.stopContainer(container, createImageConfig(SHUTDOWN_WAIT, 0), false, false);
}

@Test
public void testVolumesDuringStart() throws DockerAccessException {
ServiceHub hub = new ServiceHubFactory().createServiceHub(project, session, docker, log, new LogOutputSpecFactory(true, true, null));
List<String> volumeBinds = Collections.singletonList("sqlserver-backup-dev:/var/opt/mssql/data");
List<VolumeConfiguration> volumeConfigurations = Collections.singletonList(volumeConfiguration);

List<String> createdVolumes = runService.createVolumesAsPerVolumeBinds(hub, volumeBinds, volumeConfigurations);

assertEquals(createdVolumes.get(0), volumeConfigurations.get(0).getName());
assertTrue(createdVolumes.contains(volumeConfigurations.get(0).getName()));
}

private ImageConfiguration createImageConfig(int wait, int kill) {
return new ImageConfiguration.Builder()
Expand Down

0 comments on commit a770153

Please sign in to comment.