Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return task ARN from the handle command #34

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 return 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"]
Loading