Skip to content

Commit

Permalink
Merge pull request #1007 from rohanKanojia/issue986
Browse files Browse the repository at this point in the history
Fix #986 : Create volumes with proper configuration during "docker:start"
  • Loading branch information
rhuss authored Sep 25, 2018
2 parents 4532b2f + 1e5b6ab commit 4481c2f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 15 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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
2 changes: 2 additions & 0 deletions src/main/asciidoc/inc/start/_volumes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ A container can bind (or "mount") volumes from various source when starting up:

In this example the container creates a new volume named `/logs` on the container and mounts `/opt/host_export` from the host as `/opt/container_import` on the container. In addition all exported volumes from the container which has been created from the image `jolokia/docker-demo` are mounted directly into the container (with the same directory names under which the exporting container exposes these directories). This image must be also configured for this plugin. Instead of the full image name, an alias name can be used, too.

If a volume name instead of a path is referenced to in `<bind>` and a <<docker:volume-create,volume configuration>> exists with this name, then this this volume is created upfront with the provided options instead of using default options.

You can use Maven variables in the path specifications. This should even work for boot2docker and docker-machine:

.Example with absolute paths
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/fabric8/maven/docker/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.config.NetworkConfig;
import io.fabric8.maven.docker.config.RunImageConfiguration;
import io.fabric8.maven.docker.config.RunVolumeConfiguration;
import io.fabric8.maven.docker.config.VolumeConfiguration;
import io.fabric8.maven.docker.log.LogDispatcher;
import io.fabric8.maven.docker.model.Container;
import io.fabric8.maven.docker.service.ImagePullManager;
Expand Down Expand Up @@ -343,6 +345,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
67 changes: 52 additions & 15 deletions src/main/java/io/fabric8/maven/docker/service/RunService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
* limitations under the License.
*/

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import io.fabric8.maven.docker.access.ContainerCreateConfig;
import io.fabric8.maven.docker.access.ContainerHostConfig;
import io.fabric8.maven.docker.access.ContainerNetworkingConfig;
Expand All @@ -31,33 +45,20 @@
import io.fabric8.maven.docker.config.RestartPolicy;
import io.fabric8.maven.docker.config.RunImageConfiguration;
import io.fabric8.maven.docker.config.RunVolumeConfiguration;
import io.fabric8.maven.docker.config.VolumeConfiguration;
import io.fabric8.maven.docker.log.LogOutputSpecFactory;
import io.fabric8.maven.docker.model.Container;
import io.fabric8.maven.docker.model.ContainerDetails;
import io.fabric8.maven.docker.model.ExecDetails;
import io.fabric8.maven.docker.model.Network;
import io.fabric8.maven.docker.util.ContainerNamingUtil;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.GavLabel;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.StartOrderResolver;
import io.fabric8.maven.docker.wait.WaitTimeoutException;
import io.fabric8.maven.docker.wait.WaitUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import static io.fabric8.maven.docker.util.VolumeBindingUtil.resolveRelativeVolumeBindings;


Expand Down Expand Up @@ -537,4 +538,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;
}
}
25 changes: 25 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 @@ -2,7 +2,10 @@

import com.google.gson.JsonObject;

import io.fabric8.maven.docker.config.VolumeConfiguration;
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.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -48,6 +51,12 @@ public class RunServiceTest {

private ContainerCreateConfig containerConfig;

@Mocked
private MavenProject project;

@Mocked
private MavenSession session;

@Mocked
private DockerAccess docker;

Expand Down Expand Up @@ -112,6 +121,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 @@ -261,6 +275,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 4481c2f

Please sign in to comment.