mirror of https://github.com/docker/docs.git
Merge pull request #2335 from dnephin/volumes_refactoring
Some small refactoring around volumes
This commit is contained in:
commit
774d852f93
|
@ -252,7 +252,7 @@ class ServiceLoader(object):
|
||||||
if not self.already_seen:
|
if not self.already_seen:
|
||||||
validate_against_service_schema(service_dict, self.service_name)
|
validate_against_service_schema(service_dict, self.service_name)
|
||||||
|
|
||||||
return process_container_options(service_dict, working_dir=self.working_dir)
|
return process_container_options(self.working_dir, service_dict)
|
||||||
|
|
||||||
def validate_and_construct_extends(self):
|
def validate_and_construct_extends(self):
|
||||||
extends = self.service_dict['extends']
|
extends = self.service_dict['extends']
|
||||||
|
@ -321,7 +321,7 @@ def resolve_environment(working_dir, service_dict):
|
||||||
|
|
||||||
env = {}
|
env = {}
|
||||||
if 'env_file' in service_dict:
|
if 'env_file' in service_dict:
|
||||||
for env_file in get_env_files(service_dict, working_dir=working_dir):
|
for env_file in get_env_files(working_dir, service_dict):
|
||||||
env.update(env_vars_from_file(env_file))
|
env.update(env_vars_from_file(env_file))
|
||||||
|
|
||||||
env.update(parse_environment(service_dict.get('environment')))
|
env.update(parse_environment(service_dict.get('environment')))
|
||||||
|
@ -345,14 +345,14 @@ def validate_extended_service_dict(service_dict, filename, service):
|
||||||
"%s services with 'net: container' cannot be extended" % error_prefix)
|
"%s services with 'net: container' cannot be extended" % error_prefix)
|
||||||
|
|
||||||
|
|
||||||
def process_container_options(service_dict, working_dir=None):
|
def process_container_options(working_dir, service_dict):
|
||||||
service_dict = service_dict.copy()
|
service_dict = dict(service_dict)
|
||||||
|
|
||||||
if 'volumes' in service_dict and service_dict.get('volume_driver') is None:
|
if 'volumes' in service_dict and service_dict.get('volume_driver') is None:
|
||||||
service_dict['volumes'] = resolve_volume_paths(service_dict, working_dir=working_dir)
|
service_dict['volumes'] = resolve_volume_paths(working_dir, service_dict)
|
||||||
|
|
||||||
if 'build' in service_dict:
|
if 'build' in service_dict:
|
||||||
service_dict['build'] = resolve_build_path(service_dict['build'], working_dir=working_dir)
|
service_dict['build'] = expand_path(working_dir, service_dict['build'])
|
||||||
|
|
||||||
if 'labels' in service_dict:
|
if 'labels' in service_dict:
|
||||||
service_dict['labels'] = parse_labels(service_dict['labels'])
|
service_dict['labels'] = parse_labels(service_dict['labels'])
|
||||||
|
@ -428,7 +428,7 @@ def merge_environment(base, override):
|
||||||
return env
|
return env
|
||||||
|
|
||||||
|
|
||||||
def get_env_files(options, working_dir=None):
|
def get_env_files(working_dir, options):
|
||||||
if 'env_file' not in options:
|
if 'env_file' not in options:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
@ -488,17 +488,14 @@ def env_vars_from_file(filename):
|
||||||
return env
|
return env
|
||||||
|
|
||||||
|
|
||||||
def resolve_volume_paths(service_dict, working_dir=None):
|
def resolve_volume_paths(working_dir, service_dict):
|
||||||
if working_dir is None:
|
|
||||||
raise Exception("No working_dir passed to resolve_volume_paths()")
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
resolve_volume_path(v, working_dir, service_dict['name'])
|
resolve_volume_path(working_dir, volume, service_dict['name'])
|
||||||
for v in service_dict['volumes']
|
for volume in service_dict['volumes']
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def resolve_volume_path(volume, working_dir, service_name):
|
def resolve_volume_path(working_dir, volume, service_name):
|
||||||
container_path, host_path = split_path_mapping(volume)
|
container_path, host_path = split_path_mapping(volume)
|
||||||
|
|
||||||
if host_path is not None:
|
if host_path is not None:
|
||||||
|
@ -510,12 +507,6 @@ def resolve_volume_path(volume, working_dir, service_name):
|
||||||
return container_path
|
return container_path
|
||||||
|
|
||||||
|
|
||||||
def resolve_build_path(build_path, working_dir=None):
|
|
||||||
if working_dir is None:
|
|
||||||
raise Exception("No working_dir passed to resolve_build_path")
|
|
||||||
return expand_path(working_dir, build_path)
|
|
||||||
|
|
||||||
|
|
||||||
def validate_paths(service_dict):
|
def validate_paths(service_dict):
|
||||||
if 'build' in service_dict:
|
if 'build' in service_dict:
|
||||||
build_path = service_dict['build']
|
build_path = service_dict['build']
|
||||||
|
@ -582,7 +573,7 @@ def parse_labels(labels):
|
||||||
return dict(split_label(e) for e in labels)
|
return dict(split_label(e) for e in labels)
|
||||||
|
|
||||||
if isinstance(labels, dict):
|
if isinstance(labels, dict):
|
||||||
return labels
|
return dict(labels)
|
||||||
|
|
||||||
|
|
||||||
def split_label(label):
|
def split_label(label):
|
||||||
|
|
|
@ -913,14 +913,15 @@ def merge_volume_bindings(volumes_option, previous_container):
|
||||||
"""Return a list of volume bindings for a container. Container data volumes
|
"""Return a list of volume bindings for a container. Container data volumes
|
||||||
are replaced by those from the previous container.
|
are replaced by those from the previous container.
|
||||||
"""
|
"""
|
||||||
|
volumes = [parse_volume_spec(volume) for volume in volumes_option or []]
|
||||||
volume_bindings = dict(
|
volume_bindings = dict(
|
||||||
build_volume_binding(parse_volume_spec(volume))
|
build_volume_binding(volume)
|
||||||
for volume in volumes_option or []
|
for volume in volumes
|
||||||
if ':' in volume)
|
if volume.external)
|
||||||
|
|
||||||
if previous_container:
|
if previous_container:
|
||||||
volume_bindings.update(
|
volume_bindings.update(
|
||||||
get_container_data_volumes(previous_container, volumes_option))
|
get_container_data_volumes(previous_container, volumes))
|
||||||
|
|
||||||
return list(volume_bindings.values())
|
return list(volume_bindings.values())
|
||||||
|
|
||||||
|
@ -931,12 +932,14 @@ def get_container_data_volumes(container, volumes_option):
|
||||||
"""
|
"""
|
||||||
volumes = []
|
volumes = []
|
||||||
|
|
||||||
volumes_option = volumes_option or []
|
|
||||||
container_volumes = container.get('Volumes') or {}
|
container_volumes = container.get('Volumes') or {}
|
||||||
image_volumes = container.image_config['ContainerConfig'].get('Volumes') or {}
|
image_volumes = [
|
||||||
|
parse_volume_spec(volume)
|
||||||
|
for volume in
|
||||||
|
container.image_config['ContainerConfig'].get('Volumes') or {}
|
||||||
|
]
|
||||||
|
|
||||||
for volume in set(volumes_option + list(image_volumes)):
|
for volume in set(volumes_option + image_volumes):
|
||||||
volume = parse_volume_spec(volume)
|
|
||||||
# No need to preserve host volumes
|
# No need to preserve host volumes
|
||||||
if volume.external:
|
if volume.external:
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -577,7 +577,7 @@ class VolumeConfigTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_volume_path_with_non_ascii_directory(self):
|
def test_volume_path_with_non_ascii_directory(self):
|
||||||
volume = u'/Füü/data:/data'
|
volume = u'/Füü/data:/data'
|
||||||
container_path = config.resolve_volume_path(volume, ".", "test")
|
container_path = config.resolve_volume_path(".", volume, "test")
|
||||||
self.assertEqual(container_path, volume)
|
self.assertEqual(container_path, volume)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -593,11 +593,11 @@ class ServiceVolumesTest(unittest.TestCase):
|
||||||
self.assertEqual(binding, ('/inside', '/outside:/inside:rw'))
|
self.assertEqual(binding, ('/inside', '/outside:/inside:rw'))
|
||||||
|
|
||||||
def test_get_container_data_volumes(self):
|
def test_get_container_data_volumes(self):
|
||||||
options = [
|
options = [parse_volume_spec(v) for v in [
|
||||||
'/host/volume:/host/volume:ro',
|
'/host/volume:/host/volume:ro',
|
||||||
'/new/volume',
|
'/new/volume',
|
||||||
'/existing/volume',
|
'/existing/volume',
|
||||||
]
|
]]
|
||||||
|
|
||||||
self.mock_client.inspect_image.return_value = {
|
self.mock_client.inspect_image.return_value = {
|
||||||
'ContainerConfig': {
|
'ContainerConfig': {
|
||||||
|
|
Loading…
Reference in New Issue