Skip to content

Commit

Permalink
Added support from passing aws profile dynamically for the push command.
Browse files Browse the repository at this point in the history
  • Loading branch information
ilazakis committed Nov 7, 2018
1 parent 8de750f commit be1ab85
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 62 deletions.
5 changes: 3 additions & 2 deletions sagify/api/push.py
Expand Up @@ -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/'))

Expand All @@ -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)
5 changes: 3 additions & 2 deletions sagify/commands/push.py
Expand Up @@ -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
"""
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion sagify/template/{{ cookiecutter.module_slug }}/push.sh
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -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
89 changes: 33 additions & 56 deletions 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
Expand All @@ -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)

0 comments on commit be1ab85

Please sign in to comment.