Преглед на файлове

Support volumes in config with an unspecified host path

Aanand Prasad преди 11 години
родител
ревизия
3669236aa1
променени са 2 файла, в които са добавени 21 реда и са изтрити 3 реда
  1. 15 3
      fig/service.py
  2. 6 0
      tests/service_test.py

+ 15 - 3
fig/service.py

@@ -114,8 +114,9 @@ class Service(object):
 
         if options.get('volumes', None) is not None:
             for volume in options['volumes']:
-                external_dir, internal_dir = volume.split(':')
-                volume_bindings[os.path.abspath(external_dir)] = internal_dir
+                if ':' in volume:
+                    external_dir, internal_dir = volume.split(':')
+                    volume_bindings[os.path.abspath(external_dir)] = internal_dir
 
         container.start(
             links=self._get_links(),
@@ -162,7 +163,7 @@ class Service(object):
             container_options['ports'] = ports
 
         if 'volumes' in container_options:
-            container_options['volumes'] = dict((v.split(':')[1], {}) for v in container_options['volumes'])
+            container_options['volumes'] = dict((split_volume(v)[1], {}) for v in container_options['volumes'])
 
         if self.can_be_built():
             if len(self.client.images(name=self._build_tag_name())) == 0:
@@ -233,3 +234,14 @@ def get_container_name(container):
     for name in container['Names']:
         if len(name.split('/')) == 2:
             return name[1:]
+
+
+def split_volume(v):
+    """
+    If v is of the format EXTERNAL:INTERNAL, returns (EXTERNAL, INTERNAL).
+    If v is of the format INTERNAL, returns (None, INTERNAL).
+    """
+    if ':' in v:
+        return v.split(':', 1)
+    else:
+        return (None, v)

+ 6 - 0
tests/service_test.py

@@ -102,6 +102,12 @@ class ServiceTest(DockerClientTestCase):
         container = db.create_container(one_off=True)
         self.assertEqual(container.name, 'figtest_db_run_1')
 
+    def test_create_container_with_unspecified_volume(self):
+        service = self.create_service('db', volumes=['/var/db'])
+        container = service.create_container()
+        service.start_container(container)
+        self.assertIn('/var/db', container.inspect()['Volumes'])
+
     def test_recreate_containers(self):
         service = self.create_service('db', environment={'FOO': '1'})
         container = service.create_container()