Skip to content

Commit

Permalink
add docker-cloud.yml to the top of the list of stack filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
tifayuki committed Feb 22, 2016
1 parent 0995708 commit cf96f24
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
35 changes: 19 additions & 16 deletions dockercloudcli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from dateutil import tz
from tabulate import tabulate

from exceptions import BadParameter, DockerNotFound, StreamOutputError
from exceptions import BadParameter, StreamOutputError
from interpolation import interpolate_environment_variables


Expand Down Expand Up @@ -303,27 +303,18 @@ def load_stack_file(name, stackfile, stack=None):
else:
name = stack.name

if not stackfile:
filematch = 0
if os.path.exists("tutum.yml"):
stackfile = "tutum.yml"
filematch += 1
if os.path.exists("docker-compose.yml"):
stackfile = "docker-compose.yml"
filematch += 1
if filematch == 0:
raise BadParameter("Cannot find stack file. Are you in the right directory?")
elif filematch > 1:
raise BadParameter("More than one stack file was found in the path. "
"Please specify which one you'd like to use with -f <filename>")
stackfile = get_stackfile_name(stackfile)

with open(stackfile, 'r') as f:
content = yaml.load(f.read())
interpolated_content = interpolate_environment_variables(content, 'service')
try:
interpolated_content = interpolate_environment_variables(content, 'service')
except:
raise BadParameter("Bad format of the stack file: %s" % stackfile)

services = []
if interpolated_content:
for k, v in interpolated_content.items():
print ()
v.update({"name": k})
services.append(v)

Expand All @@ -340,6 +331,18 @@ def load_stack_file(name, stackfile, stack=None):
return stack


def get_stackfile_name(stackfile):
if not stackfile:
stackfile_found = False
for stackfile in ["docker-cloud.yml", "tutum.yml", "docker-compose.yml"]:
if os.path.exists(stackfile):
stackfile_found = True
break
if not stackfile_found:
raise BadParameter("Cannot find stack file. Are you in the right directory?")
return stackfile


def inject_env_var(services):
for service in services:
try:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,19 @@ class ParseEnvironmentVariablesTestCase(unittest.TestCase):
def test_parse_envvars(self):
output = [{'key': 'MYSQL_PASS', 'value': 'mypass'}, {'key': 'MYSQL_USER', 'value': 'admin'}]
self.assertEqual(output, parse_envvars(['MYSQL_USER=admin', 'MYSQL_PASS=mypass'], []))


class GetStackfileName(unittest.TestCase):
def test_get_stackfile_name_not_empty_name(self):
self.assertEqual("abc", get_stackfile_name("abc"))

@mock.patch('dockercloudcli.utils.os.path.exists')
def test_get_stackfile_name_empty_name(self, mock_exist):
mock_exist.side_effect = [False, False, False]
self.assertRaises(BadParameter, get_stackfile_name, "")
mock_exist.side_effect = [True, True, True]
self.assertEqual("docker-cloud.yml", get_stackfile_name(""))
mock_exist.side_effect = [False, True, True]
self.assertEqual("tutum.yml", get_stackfile_name(""))
mock_exist.side_effect = [False, False, True]
self.assertEqual("docker-compose.yml", get_stackfile_name(""))

0 comments on commit cf96f24

Please sign in to comment.