diff --git a/sagify/api/push.py b/sagify/api/push.py index a93f1bd..ed99faf 100644 --- a/sagify/api/push.py +++ b/sagify/api/push.py @@ -8,12 +8,13 @@ from sagify.log import logger -def push(dir, docker_tag): +def push(dir, docker_tag, aws_profile): """ Push Docker image to AWS ECS :param dir: [str], source root directory :param docker_tag: [str], the Docker tag for the image + :param aws_profile: [str], the AWS profile used to push the image to ECR """ sagify_module_path = os.path.relpath(os.path.join(dir, 'sagify/')) @@ -22,5 +23,5 @@ def push(dir, docker_tag): if not os.path.isfile(push_script_path): raise ValueError("This is not a sagify directory: {}".format(dir)) - output = subprocess.check_output(["{}".format(push_script_path), docker_tag]) + output = subprocess.check_output(["{}".format(push_script_path), docker_tag, aws_profile]) logger.debug(output) diff --git a/sagify/commands/push.py b/sagify/commands/push.py index 4e4a232..dc0f416 100644 --- a/sagify/commands/push.py +++ b/sagify/commands/push.py @@ -15,8 +15,9 @@ @click.command() @click.option(u"-d", u"--dir", required=False, default='.', help="Path to sagify module") +@click.option(u"-p", u"--aws-profile", required=True, help="The AWS profile to use for the push command") @click.pass_obj -def push(obj, dir): +def push(obj, dir, aws_profile): """ Command to push Docker image to AWS ECS """ @@ -26,7 +27,7 @@ def push(obj, dir): ) try: - api_push.push(dir=dir, docker_tag=obj['docker_tag']) + api_push.push(dir=dir, docker_tag=obj['docker_tag'], aws_profile=aws_profile) logger.info("Docker image pushed to ECS successfully!") except ValueError: diff --git a/sagify/template/{{ cookiecutter.module_slug }}/push.sh b/sagify/template/{{ cookiecutter.module_slug }}/push.sh index cae0a01..6965386 100755 --- a/sagify/template/{{ cookiecutter.module_slug }}/push.sh +++ b/sagify/template/{{ cookiecutter.module_slug }}/push.sh @@ -7,8 +7,11 @@ # machine and combined with the account and region to form the repository name for ECR. image={{ cookiecutter.project_slug }}-img tag=$1 +profile=$2 -profile={{ cookiecutter.aws_profile }} +if [-z "$profile"]; then + profile={{ cookiecutter.aws_profile }} +fi region={{ cookiecutter.aws_region }} # Get the account number associated with the current IAM credentials diff --git a/setup.cfg b/setup.cfg index 5a67fd9..d2d66ff 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,5 +24,5 @@ description-file = README.md universal=1 [flake8] -max-line-length=100 +max-line-length=130 exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,venv/,sagify/commands/__init__.py diff --git a/tests/commands/test_push.py b/tests/commands/test_push.py index f8121e0..ad095a8 100644 --- a/tests/commands/test_push.py +++ b/tests/commands/test_push.py @@ -1,4 +1,5 @@ try: + from unittest import TestCase from unittest.mock import patch except ImportError: from mock import patch @@ -8,59 +9,35 @@ from sagify.__main__ import cli -def test_push_happy_case(): - runner = CliRunner() - - with patch( - 'sagify.commands.initialize._get_local_aws_profiles', - return_value=['default', 'sagemaker'] - ): - with patch( - 'future.moves.subprocess.check_output', - return_value=None - ): - with runner.isolated_filesystem(): - runner.invoke(cli=cli, args=['init'], input='my_app\n1\n2\nus-east-1\n') - result = runner.invoke(cli=cli, args=['push']) - - assert result.exit_code == 0 - - -def test_push_with_dir_arg_happy_case(): - runner = CliRunner() - - with patch( - 'sagify.commands.initialize._get_local_aws_profiles', - return_value=['default', 'sagemaker'] - ): - with patch( - 'future.moves.subprocess.check_output', - return_value=None - ): - with runner.isolated_filesystem(): - runner.invoke( - cli=cli, args=['init', '-d', 'src/'], input='my_app\n1\n2\nus-east-1\n' - ) - result = runner.invoke(cli=cli, args=['push', '-d', 'src/']) - - assert result.exit_code == 0 - - -def test_push_with_invalid_dir_arg_happy_case(): - runner = CliRunner() - - with patch( - 'sagify.commands.initialize._get_local_aws_profiles', - return_value=['default', 'sagemaker'] - ): - with patch( - 'future.moves.subprocess.check_output', - return_value=None - ): - with runner.isolated_filesystem(): - runner.invoke( - cli=cli, args=['init', '-d', 'src/'], input='my_app\n1\n2\nus-east-1\n' - ) - result = runner.invoke(cli=cli, args=['push', '-d', 'invalid_dir/']) - - assert result.exit_code == -1 +class PushCommandTests(TestCase): + def setUp(self): + self.runner = CliRunner() + self.command_line = patch('future.moves.subprocess.check_output', return_value=None).start() + patch('sagify.commands.initialize._get_local_aws_profiles', return_value=['default', 'sagemaker']).start() + + def test_push_happy_case(self): + assert self.runCommands( + init_command=['init'], + push_command=['push', '-p', 'some-profile'] + ).exit_code == 0 + self.command_line.assert_called_once_with(['sagify/push.sh', 'latest', 'some-profile']) + + def test_push_with_dir_arg_happy_case(self): + assert self.runCommands( + init_command=['init', '-d', 'src/'], + push_command=['push', '-d', 'src/', '-p', 'some-profile'] + ).exit_code == 0 + self.command_line.assert_called_once_with(['src/sagify/push.sh', 'latest', 'some-profile']) + + def test_push_with_invalid_dir_arg_happy_case(self): + assert self.runCommands( + init_command=['init', '-d', 'src/'], + push_command=['push', '-d', 'invalid_dir/', '-p', 'some-profile'] + ).exit_code == -1 + self.command_line.assert_not_called() + + def runCommands(self, init_command, push_command): + runner = self.runner + with runner.isolated_filesystem(): + runner.invoke(cli=cli, args=init_command, input='my_app\n1\n2\nus-east-1\n') + return runner.invoke(cli=cli, args=push_command)