Skip to content

Commit dcc3fa7

Browse files
committed
[run] Override image, tag or role
1 parent 4b17653 commit dcc3fa7

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

ecs_deploy/cli.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,12 @@ def scale(cluster, service, desired_count, access_key_id, secret_access_key, reg
296296
@click.argument('cluster')
297297
@click.argument('task')
298298
@click.argument('count', required=False, default=1)
299+
@click.option('-t', '--tag', help='Changes the tag for ALL container images')
300+
@click.option('-i', '--image', type=(str, str), multiple=True, help='Overwrites the image for a container: <container> <image>')
299301
@click.option('-c', '--command', type=(str, str), multiple=True, help='Overwrites the command in a container: <container> <command>')
300302
@click.option('-e', '--env', type=(str, str, str), multiple=True, help='Adds or changes an environment variable: <container> <name> <value>')
301303
@click.option('-s', '--secret', type=(str, str, str), multiple=True, help='Adds or changes a secret environment variable from the AWS Parameter Store (Not available for Fargate): <container> <name> <parameter name>')
304+
@click.option('-r', '--role', type=str, help='Sets the task\'s role ARN: <task role ARN>')
302305
@click.option('--launchtype', type=click.Choice([LAUNCH_TYPE_EC2, LAUNCH_TYPE_FARGATE]), default=LAUNCH_TYPE_EC2, help='ECS Launch type (default: EC2)')
303306
@click.option('--subnet', type=str, multiple=True, help='A subnet ID to launch the task within. Required for launch type FARGATE (multiple values possible)')
304307
@click.option('--securitygroup', type=str, multiple=True, help='A security group ID to launch the task within. Required for launch type FARGATE (multiple values possible)')
@@ -310,7 +313,8 @@ def scale(cluster, service, desired_count, access_key_id, secret_access_key, reg
310313
@click.option('--diff/--no-diff', default=True, help='Print what values were changed in the task definition')
311314
@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')
312315
@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):
316+
@click.option('--deregister/--no-deregister', default=True, help='Deregister or keep the old task definition (default: --deregister)')
317+
def run(cluster, task, count, tag, image, command, env, secret, role, launchtype, subnet, securitygroup, public_ip, region, access_key_id, secret_access_key, profile, diff, exclusive_env, exclusive_secrets, deregister):
314318
"""
315319
Run a one-off task.
316320
@@ -319,18 +323,24 @@ def run(cluster, task, count, command, env, secret, launchtype, subnet, security
319323
TASK is the name of your task definition (e.g. 'my-task') within ECS.
320324
COUNT is the number of tasks your service should run.
321325
"""
326+
should_create_task_definition = image or tag or role
322327
try:
323328
client = get_client(access_key_id, secret_access_key, region, profile)
324329
action = RunAction(client, cluster)
325330

326-
td = action.get_task_definition(task)
331+
td = td_old = action.get_task_definition(task)
332+
td.set_images(tag, **{key: value for (key, value) in image})
327333
td.set_commands(**{key: value for (key, value) in command})
328334
td.set_environment(env, exclusive_env)
329335
td.set_secrets(secret, exclusive_secrets)
336+
td.set_role_arn(role)
330337

331338
if diff:
332339
print_diff(td, 'Using task definition: %s' % task)
333340

341+
if should_create_task_definition:
342+
td = create_task_definition(action, td)
343+
334344
action.run(td, count, 'ECS Deploy', launchtype, subnet, securitygroup, public_ip)
335345

336346
click.secho(
@@ -345,6 +355,9 @@ def run(cluster, task, count, command, env, secret, launchtype, subnet, security
345355
click.secho('- %s' % started_task['taskArn'], fg='green')
346356
click.secho(' ')
347357

358+
if should_create_task_definition and deregister:
359+
deregister_task_definition(action, td_old)
360+
348361
except EcsError as e:
349362
click.secho('%s\n' % str(e), fg='red', err=True)
350363
exit(1)

tests/test_cli.py

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,20 +623,98 @@ def test_run_task(get_client, runner):
623623
assert result.exit_code == 0
624624

625625
assert u"Successfully started 2 instances of task: test-task:2" in result.output
626+
assert u'Successfully deregistered revision: 2' not in result.output
626627
assert u"- arn:foo:bar" in result.output
627628
assert u"- arn:lorem:ipsum" in result.output
628629

629630

630631
@patch('ecs_deploy.cli.get_client')
631-
def test_run_task_with_command(get_client, runner):
632+
def test_run_with_role_arn_deregister_old_task_definiion(get_client, runner):
632633
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
633-
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-c', 'webserver', 'date'))
634+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task:1', '2', '-r', 'arn:new:role'))
635+
assert result.exit_code == 0
636+
assert not result.exception
637+
assert u"Using task definition: test-task" in result.output
638+
assert u'Changed role_arn to: "arn:new:role" (was: "arn:test:role:1")' in result.output
639+
assert u"Creating new task definition revision" in result.output
640+
assert u'Successfully created revision: 2' in result.output
641+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
642+
assert u'Successfully deregistered revision: 1' in result.output
643+
assert u"- arn:foo:bar" in result.output
644+
assert u"- arn:lorem:ipsum" in result.output
645+
634646

647+
@patch('ecs_deploy.cli.get_client')
648+
def test_run_with_role_arn_keep_old_task_definiion(get_client, runner):
649+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
650+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task:1', '2', '-r', 'arn:new:role', '--no-deregister'))
651+
assert result.exit_code == 0
635652
assert not result.exception
653+
assert u"Using task definition: test-task" in result.output
654+
assert u'Changed role_arn to: "arn:new:role" (was: "arn:test:role:1")' in result.output
655+
assert u"Creating new task definition revision" in result.output
656+
assert u'Successfully created revision: 2' in result.output
657+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
658+
assert u'Successfully deregistered revision: 1' not in result.output
659+
assert u"- arn:foo:bar" in result.output
660+
assert u"- arn:lorem:ipsum" in result.output
661+
662+
663+
@patch('ecs_deploy.cli.get_client')
664+
def test_run_new_tag(get_client, runner):
665+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
666+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-t', 'latest'))
636667
assert result.exit_code == 0
668+
assert not result.exception
669+
assert u"Using task definition: test-task" in result.output
670+
assert u"Creating new task definition revision" in result.output
671+
assert u'Changed image of container "webserver" to: "webserver:latest" (was: "webserver:123")' in result.output
672+
assert u'Changed image of container "application" to: "application:latest" (was: "application:123")' in result.output
673+
assert u'Successfully created revision: 2' in result.output
674+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
675+
assert u"- arn:foo:bar" in result.output
676+
assert u"- arn:lorem:ipsum" in result.output
677+
637678

679+
@patch('ecs_deploy.cli.get_client')
680+
def test_run_one_new_image(get_client, runner):
681+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
682+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-i', 'application', 'application:latest'))
683+
assert result.exit_code == 0
684+
assert not result.exception
685+
assert u"Using task definition: test-task" in result.output
686+
assert u"Creating new task definition revision" in result.output
687+
assert u'Changed image of container "application" to: "application:latest" (was: "application:123")' in result.output
688+
assert u'Successfully created revision: 2' in result.output
689+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
690+
assert u"- arn:foo:bar" in result.output
691+
assert u"- arn:lorem:ipsum" in result.output
692+
693+
694+
@patch('ecs_deploy.cli.get_client')
695+
def test_run_two_new_images(get_client, runner):
696+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
697+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-i', 'application', 'application:latest',
698+
'-i', 'webserver', 'webserver:latest'))
699+
assert result.exit_code == 0
700+
assert not result.exception
701+
assert u"Using task definition: test-task" in result.output
702+
assert u"Creating new task definition revision" in result.output
703+
assert u'Changed image of container "webserver" to: "webserver:latest" (was: "webserver:123")' in result.output
704+
assert u'Changed image of container "application" to: "application:latest" (was: "application:123")' in result.output
705+
assert u"Successfully started 2 instances of task: test-task:2" in result.output
706+
assert u"- arn:foo:bar" in result.output
707+
assert u"- arn:lorem:ipsum" in result.output
708+
709+
710+
@patch('ecs_deploy.cli.get_client')
711+
def test_run_one_new_command(get_client, runner):
712+
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
713+
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-c', 'application', 'date'))
714+
assert result.exit_code == 0
715+
assert not result.exception
638716
assert u"Using task definition: test-task" in result.output
639-
assert u'Changed command of container "webserver" to: "date" (was: "run")' in result.output
717+
assert u'Changed command of container "application" to: "date" (was: "run")' in result.output
640718
assert u"Successfully started 2 instances of task: test-task:2" in result.output
641719
assert u"- arn:foo:bar" in result.output
642720
assert u"- arn:lorem:ipsum" in result.output

0 commit comments

Comments
 (0)