Skip to content

Commit

Permalink
Support a default docker-compose.override.yml for overrides
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <[email protected]>
  • Loading branch information
dnephin committed Sep 15, 2015
1 parent e3ba7e3 commit 6b65431
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
16 changes: 11 additions & 5 deletions compose/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
'fig.yaml',
]

DEFAULT_OVERRIDE_FILENAME = 'docker-compose.override.yml'

PATH_START_CHARS = [
'/',
Expand All @@ -101,16 +102,16 @@ def find(base_dir, filenames):
if filenames:
filenames = [os.path.join(base_dir, f) for f in filenames]
else:
filenames = get_default_config_path(base_dir)
filenames = get_default_config_files(base_dir)
return ConfigDetails(
os.path.dirname(filenames[0]),
[ConfigFile(f, load_yaml(f)) for f in filenames])


def get_default_config_path(base_dir):
def get_default_config_files(base_dir):
(candidates, path) = find_candidates_in_parent_dirs(SUPPORTED_FILENAMES, base_dir)

if len(candidates) == 0:
if not candidates:
raise ComposeFileNotFound(SUPPORTED_FILENAMES)

winner = candidates[0]
Expand All @@ -128,7 +129,12 @@ def get_default_config_path(base_dir):
log.warn("%s is deprecated and will not be supported in future. "
"Please rename your config file to docker-compose.yml\n" % winner)

return os.path.join(path, winner)
return [os.path.join(path, winner)] + get_default_override_file(path)


def get_default_override_file(path):
override_filename = os.path.join(path, DEFAULT_OVERRIDE_FILENAME)
return [override_filename] if os.path.exists(override_filename) else []


def find_candidates_in_parent_dirs(filenames, path):
Expand All @@ -142,7 +148,7 @@ def find_candidates_in_parent_dirs(filenames, path):
candidates = [filename for filename in filenames
if os.path.exists(os.path.join(path, filename))]

if len(candidates) == 0:
if not candidates:
parent_dir = os.path.join(path, '..')
if os.path.abspath(parent_dir) != os.path.abspath(path):
return find_candidates_in_parent_dirs(filenames, parent_dir)
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/override-files/docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

web:
command: "top"

db:
command: "top"
10 changes: 10 additions & 0 deletions tests/fixtures/override-files/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

web:
image: busybox:latest
command: "sleep 200"
links:
- db

db:
image: busybox:latest
command: "sleep 200"
9 changes: 9 additions & 0 deletions tests/fixtures/override-files/extra.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

web:
links:
- db
- other

other:
image: busybox:latest
command: "top"
39 changes: 38 additions & 1 deletion tests/integration/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,6 @@ def get_port(number, mock_stdout):
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

self.command.base_dir = 'tests/fixtures/ports-composefile-scale'
self.command.dispatch(['scale', 'simple=2'], None)
containers = sorted(
Expand Down Expand Up @@ -593,6 +592,44 @@ def test_home_and_env_var_in_volume_path(self):
self.assertTrue(components[-2:] == ['home-dir', 'my-volume'],
msg="Last two components differ: %s, %s" % (actual_host_path, expected_host_path))

def test_up_with_default_override_file(self):
self.command.base_dir = 'tests/fixtures/override-files'
self.command.dispatch(['up', '-d'], None)

containers = self.project.containers()
self.assertEqual(len(containers), 2)

web, db = containers
self.assertEqual(web.human_readable_command, 'top')
self.assertEqual(db.human_readable_command, 'top')

def test_up_with_multiple_files(self):
self.command.base_dir = 'tests/fixtures/override-files'
config_paths = [
'docker-compose.yml',
'docker-compose.override.yml',
'extra.yml',

]
self._project = get_project(self.command.base_dir, config_paths)
self.command.dispatch(
[
'-f', config_paths[0],
'-f', config_paths[1],
'-f', config_paths[2],
'up', '-d',
],
None)

containers = self.project.containers()
self.assertEqual(len(containers), 3)

web, other, db = containers
self.assertEqual(web.human_readable_command, 'top')
self.assertTrue({'db', 'other'} <= set(web.links()))
self.assertEqual(db.human_readable_command, 'top')
self.assertEqual(other.human_readable_command, 'top')

def test_up_with_extends(self):
self.command.base_dir = 'tests/fixtures/extends'
self.command.dispatch(['up', '-d'], None)
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/config/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ def make_files(dirname, filenames):
base_dir = tempfile.mkdtemp(dir=project_dir)
else:
base_dir = project_dir
return os.path.basename(config.get_config_path(base_dir))
filename, = config.get_default_config_files(base_dir)
return os.path.basename(filename)
finally:
shutil.rmtree(project_dir)

0 comments on commit 6b65431

Please sign in to comment.