Skip to content

Commit

Permalink
Return task ARN from the handle command
Browse files Browse the repository at this point in the history
When running ecsmanage in a programmatic setting, it is useful
to get the ARN of the task which it started, in addition to the
URL being printed to the console. This doesn't break existing
workflows, but adds the possibility of new ones.
  • Loading branch information
rajadain committed Jan 5, 2024
1 parent a075466 commit 201c7fd
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions ecsmanage/management/commands/ecsmanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def add_arguments(self, parser):
def handle(self, *args, **options):
"""
Run the given command on the latest app CLI task definition and print
out a URL to view the status.
out a URL to view the status. Returns the task ARN of the started task.
"""
self.env = options["env"]
cmd = options["cmd"]
Expand All @@ -45,7 +45,15 @@ def handle(self, *args, **options):
security_group_id = self.get_security_group(config["SECURITY_GROUP_TAGS"])
subnet_id = self.get_subnet(config["SUBNET_TAGS"])

task_id = self.run_task(config, task_def_arn, security_group_id, subnet_id, cmd)
task_arn = self.run_task(config, task_def_arn, security_group_id, subnet_id, cmd)

# Task ARNs have at least two formats:
#
# - Old: arn:aws:ecs:region:aws_account_id:task/task-id
# - New: arn:aws:ecs:region:aws_account_id:task/cluster-name/task-id
#
# See: https://docs.aws.amazon.com/AmazonECS/latest/userguide/ecs-account-settings.html#ecs-resource-ids # NOQA
task_id = task_arn.split("/")[-1]

cluster_name = config["CLUSTER_NAME"]

Expand All @@ -58,6 +66,8 @@ def handle(self, *args, **options):
self.style.SUCCESS(f"Task started! View here:\n{url}")
) # NOQA

return task_arn

def parse_config(self):
"""
Parse configuration settings for the app, checking to make sure that
Expand Down Expand Up @@ -165,7 +175,7 @@ def get_subnet(self, subnet_tags):
def run_task(self, config, task_def_arn, security_group_id, subnet_id, cmd):
"""
Run a task for a given task definition ARN using the given security
group and subnets, and return the task ID.
group and subnets, and returns the task ARN of the started task.
"""
task_def = self.ecs_client.describe_task_definition(
taskDefinition=task_def_arn
Expand Down Expand Up @@ -200,11 +210,4 @@ def run_task(self, config, task_def_arn, security_group_id, subnet_id, cmd):

task = self.parse_response(self.ecs_client.run_task(**kwargs), "tasks", 0)

# Task ARNs have at least two formats:
#
# - Old: arn:aws:ecs:region:aws_account_id:task/task-id
# - New: arn:aws:ecs:region:aws_account_id:task/cluster-name/task-id
#
# See: https://docs.aws.amazon.com/AmazonECS/latest/userguide/ecs-account-settings.html#ecs-resource-ids # NOQA
task_id = task["taskArn"].split("/")[-1]
return task_id
return task["taskArn"]

0 comments on commit 201c7fd

Please sign in to comment.