Skip to content

Commit 4b17653

Browse files
committed
[run] Add exclusive environment and exclusive secrets flags
1 parent 94519d5 commit 4b17653

File tree

2 files changed

+158
-5
lines changed

2 files changed

+158
-5
lines changed

ecs_deploy/cli.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ def scale(cluster, service, desired_count, access_key_id, secret_access_key, reg
308308
@click.option('--secret-access-key', help='AWS secret access key')
309309
@click.option('--profile', help='AWS configuration profile name')
310310
@click.option('--diff/--no-diff', default=True, help='Print what values were changed in the task definition')
311-
def run(cluster, task, count, command, env, secret, launchtype, subnet, securitygroup, public_ip, region, access_key_id, secret_access_key, profile, diff):
311+
@click.option('--exclusive-env', is_flag=True, default=False, help='Set the given environment variables exclusively and remove all other pre-existing env variables from all containers')
312+
@click.option('--exclusive-secrets', is_flag=True, default=False, help='Set the given secrets exclusively and remove all other pre-existing secrets from all containers')
313+
def run(cluster, task, count, command, env, secret, launchtype, subnet, securitygroup, public_ip, region, access_key_id, secret_access_key, profile, diff, exclusive_env, exclusive_secrets):
312314
"""
313315
Run a one-off task.
314316
@@ -323,8 +325,8 @@ def run(cluster, task, count, command, env, secret, launchtype, subnet, security
323325

324326
td = action.get_task_definition(task)
325327
td.set_commands(**{key: value for (key, value) in command})
326-
td.set_environment(env)
327-
td.set_secrets(secret)
328+
td.set_environment(env, exclusive_env)
329+
td.set_secrets(secret, exclusive_secrets)
328330

329331
if diff:
330332
print_diff(td, 'Using task definition: %s' % task)

tests/test_cli.py

Lines changed: 153 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,12 @@ def test_run_task_with_command(get_client, runner):
643643

644644

645645
@patch('ecs_deploy.cli.get_client')
646-
def test_run_task_with_environment_var(get_client, runner):
646+
def test_run_one_new_environment_variable(get_client, runner):
647647
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
648648
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'application', 'foo', 'bar'))
649649

650-
assert not result.exception
651650
assert result.exit_code == 0
651+
assert not result.exception
652652

653653
assert u"Using task definition: test-task" in result.output
654654
assert u'Changed environment "foo" of container "application" to: "bar"' in result.output
@@ -657,6 +657,157 @@ def test_run_task_with_environment_var(get_client, runner):
657657
assert u"- arn:lorem:ipsum" in result.output
658658

659659

660+
@patch('ecs_deploy.cli.get_client')
661+
def test_run_change_environment_variable_empty_string(get_client, runner):
662+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
663+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'application', 'foo', ''))
664+
665+
assert result.exit_code == 0
666+
assert not result.exception
667+
668+
assert u"Using task definition: test-task" in result.output
669+
assert u'Changed environment "foo" of container "application" to: ""' in result.output
670+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
671+
assert u"- arn:foo:bar" in result.output
672+
assert u"- arn:lorem:ipsum" in result.output
673+
674+
675+
@patch('ecs_deploy.cli.get_client')
676+
def test_run_new_empty_environment_variable(get_client, runner):
677+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
678+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'application', 'new', ''))
679+
680+
assert result.exit_code == 0
681+
assert not result.exception
682+
683+
assert u"Using task definition: test-task" in result.output
684+
assert u'Changed environment "new" of container "application" to: ""' in result.output
685+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
686+
assert u"- arn:foo:bar" in result.output
687+
assert u"- arn:lorem:ipsum" in result.output
688+
689+
690+
@patch('ecs_deploy.cli.get_client')
691+
def test_run_empty_environment_variable_again(get_client, runner):
692+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
693+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'empty', ''))
694+
695+
assert result.exit_code == 0
696+
assert not result.exception
697+
698+
assert u"Using task definition: test-task" not in result.output
699+
assert u'Changed environment' not in result.output
700+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
701+
assert u"- arn:foo:bar" in result.output
702+
assert u"- arn:lorem:ipsum" in result.output
703+
704+
705+
@patch('ecs_deploy.cli.get_client')
706+
def test_run_previously_empty_environment_variable_with_value(get_client, runner):
707+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
708+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'empty', 'not-empty'))
709+
710+
assert result.exit_code == 0
711+
assert not result.exception
712+
713+
assert u"Using task definition: test-task" in result.output
714+
assert u'Changed environment "empty" of container "webserver" to: "not-empty"' in result.output
715+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
716+
assert u"- arn:foo:bar" in result.output
717+
assert u"- arn:lorem:ipsum" in result.output
718+
719+
720+
@patch('ecs_deploy.cli.get_client')
721+
def test_run_exclusive_environment(get_client, runner):
722+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
723+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'new-env', 'new-value', '--exclusive-env'))
724+
725+
assert result.exit_code == 0
726+
assert not result.exception
727+
728+
assert u"Using task definition: test-task" in result.output
729+
assert u'Changed environment "new-env" of container "webserver" to: "new-value"' in result.output
730+
731+
assert u'Removed environment "foo" of container "webserver"' in result.output
732+
assert u'Removed environment "lorem" of container "webserver"' in result.output
733+
734+
assert u'Removed secret' not in result.output
735+
736+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
737+
assert u"- arn:foo:bar" in result.output
738+
assert u"- arn:lorem:ipsum" in result.output
739+
740+
741+
@patch('ecs_deploy.cli.get_client')
742+
def test_run_exclusive_secret(get_client, runner):
743+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
744+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-s', 'webserver', 'new-secret', 'new-place', '--exclusive-secrets'))
745+
746+
assert result.exit_code == 0
747+
assert not result.exception
748+
749+
assert u"Using task definition: test-task" in result.output
750+
assert u'Changed secret "new-secret" of container "webserver" to: "new-place"' in result.output
751+
752+
assert u'Removed secret "baz" of container "webserver"' in result.output
753+
assert u'Removed secret "dolor" of container "webserver"' in result.output
754+
755+
assert u'Removed environment' not in result.output
756+
757+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
758+
assert u"- arn:foo:bar" in result.output
759+
assert u"- arn:lorem:ipsum" in result.output
760+
761+
762+
@patch('ecs_deploy.cli.get_client')
763+
def test_run_one_new_secret_variable(get_client, runner):
764+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
765+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2',
766+
'-s', 'application', 'baz', 'qux',
767+
'-s', 'webserver', 'baz', 'quux'))
768+
769+
assert result.exit_code == 0
770+
assert not result.exception
771+
772+
assert u"Using task definition: test-task" in result.output
773+
assert u'Changed secret "baz" of container "application" to: "qux"' in result.output
774+
assert u'Changed secret "baz" of container "webserver" to: "quux"' in result.output
775+
assert u'Changed secret "dolor" of container "webserver" to: "sit"' not in result.output
776+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
777+
assert u"- arn:foo:bar" in result.output
778+
assert u"- arn:lorem:ipsum" in result.output
779+
780+
781+
@patch('ecs_deploy.cli.get_client')
782+
def test_run_without_changing_environment_value(get_client, runner):
783+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
784+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'foo', 'bar'))
785+
786+
assert result.exit_code == 0
787+
assert not result.exception
788+
789+
assert u"Using task definition: test-task" not in result.output
790+
assert u'Changed environment' not in result.output
791+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
792+
assert u"- arn:foo:bar" in result.output
793+
assert u"- arn:lorem:ipsum" in result.output
794+
795+
796+
@patch('ecs_deploy.cli.get_client')
797+
def test_run_without_changing_secrets_value(get_client, runner):
798+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
799+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-s', 'webserver', 'baz', 'qux'))
800+
801+
assert result.exit_code == 0
802+
assert not result.exception
803+
804+
assert u"Using task definition: test-task" not in result.output
805+
assert u'Changed secrets' not in result.output
806+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
807+
assert u"- arn:foo:bar" in result.output
808+
assert u"- arn:lorem:ipsum" in result.output
809+
810+
660811
@patch('ecs_deploy.cli.get_client')
661812
def test_run_task_without_diff(get_client, runner):
662813
get_client.return_value = EcsTestClient('acces_key', 'secret_key')

0 commit comments

Comments
 (0)