Skip to content

feat: implement --generate-cmd flag for task run#2201

Merged
mergify[bot] merged 22 commits intoaws:mainlinefrom
Lou1415926:generate/task-run
May 5, 2021
Merged

feat: implement --generate-cmd flag for task run#2201
mergify[bot] merged 22 commits intoaws:mainlinefrom
Lou1415926:generate/task-run

Conversation

@Lou1415926
Copy link
Copy Markdown
Contributor

This PR implements --generate-cmd for task run given either a non-Copilot ECS service or a Copilot service.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@Lou1415926 Lou1415926 requested a review from a team as a code owner April 21, 2021 22:00
@Lou1415926 Lou1415926 requested a review from bvtujo April 21, 2021 22:00
@Lou1415926
Copy link
Copy Markdown
Contributor Author

Lou1415926 commented Apr 21, 2021

Conducted the following manual testings.

For Copilot service

  1. Deployed a Copilot service with an Aurora add-on (which requires correctly configured secret and security group for access)
  2. Generate a command using the Copilot service. --generate-cmd <app>/<env>/<svc>
  3. Run the generated command using the Copilot service

For non-Copilot ECS Service

Using cluster name and service name

  1. Started an ECS service in default cluster with entrypoint and command. Didn't specify an execution role or task role.
  2. Generate a command using the ECS service. --generate-cmd default/<service-name>
    The
  3. Run the generated command using the ECS service.
    The task's task definition has the correct entrypoint/command override, image, subnet, security group, etc. It runs in default cluster. The execution role is the default execution role and no task role is assumed.

Using ARN

  1. Generate a command using the same ECS service as above. --generate-cmd default/<service-name>
    The
    The command generated is the same as the command generated using cluster/service name.

Comment thread internal/pkg/cli/flag.go Outdated
Comment thread internal/pkg/cli/flag.go Outdated
Comment thread internal/pkg/cli/task_run.go
Comment thread internal/pkg/cli/task_run_test.go Outdated
Copy link
Copy Markdown
Contributor

@huanjani huanjani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

Copy link
Copy Markdown
Contributor

@bvtujo bvtujo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! Just a few questions.

Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
if err != nil {
return nil, fmt.Errorf("extract service name from arn %s", svcARN)
}
return generator.ECSServiceCommandGenerator{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we not supporting jobs with this feature? I apologize I didn't get in on the earlier review of this. Or was the decision made to delegate that to the hypothetical job run subcommand?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make a draft PR for job for this feature! The code is working but is a little messy and I haven't got the chance to really clean it up, so it's not in this PR yet.

Copy link
Copy Markdown
Contributor

@efekarakus efekarakus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good! let me know what's your thought on the comment below :)

Comment thread internal/pkg/cli/task_run.go Outdated
Comment on lines +56 to +58
type cmdGenerator interface {
Generate() (*generator.GenerateCommandOpts, error)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting 💭 this is the first time that we have a package that populates values for a command.

Did we go with this route to keep the cli pkg small? I think that makes sense to me, but I wonder if instead of a new pkg we could have moved most of the functionality into the internal/pkg/ecs pkg and removed the generator pkg altogether.
This functionality seems like should belong in thecli pkg since it can only be used by task_run so there is no other possible consumer of the generator pkg:

func (o GenerateCommandOpts) String() string {
output := []string{"copilot task run"}

Hope that makes sense! Instead, we can maybe move this functionality to the ecs pkg:

func (g ECSServiceCommandGenerator) Generate() (*GenerateCommandOpts, error) {
to something like:

func RunTaskRequestFromECSService(client ecsDescriber, cluster, service string) (*RunTaskRequest, error)
func RunTaskRequestFromService(client ecsDescriber, app, env, svc string) (*RunTaskRequest, error)

@efekarakus efekarakus added the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Apr 29, 2021
@bvtujo bvtujo removed the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Apr 30, 2021
Copy link
Copy Markdown
Contributor

@iamhopaul123 iamhopaul123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work! LGTM mostly but just some nits.

Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
return nil
}

func (o *runTaskOpts) runTaskCommand() (*ecs.RunTaskRequest, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe i was missing anything but i don't see the logic is well tested for this func...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed!

Comment thread internal/pkg/ecs/run_task_request.go Outdated
Comment thread internal/pkg/ecs/run_task_request.go Outdated
}

if len(taskDef.ContainerDefinitions) > 1 {
return nil, fmt.Errorf("found more than one container in task definition: %s", taskDefNameOrARN)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for a better user experience, it would be nice if we can log here about the reason why they cannot use a taskdef with more than one container. But we don't log elsewhere than the cli pkg. What do you think of it @efekarakus ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can throw a typed error here and catch that in the cli package and log. So something like:

var errMultiContainers  *ecs.ErrMultipleContainersInTaskDef
if errors.As(err, &errMultiContainers) {
  log.Errorf("`copilot task run` does not support running more than one container")
}
return err

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly what I've been thinking!

Copy link
Copy Markdown
Contributor

@efekarakus efekarakus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay looks great, just one more minor suggestion and then we can :shipit:

Comment thread internal/pkg/cli/flag.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/ecs/run_task_request.go
Comment thread internal/pkg/ecs/run_task_request.go Outdated
}

if len(taskDef.ContainerDefinitions) > 1 {
return nil, fmt.Errorf("found more than one container in task definition: %s", taskDefNameOrARN)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can throw a typed error here and catch that in the cli package and log. So something like:

var errMultiContainers  *ecs.ErrMultipleContainersInTaskDef
if errors.As(err, &errMultiContainers) {
  log.Errorf("`copilot task run` does not support running more than one container")
}
return err

Comment thread internal/pkg/ecs/run_task_request.go Outdated
}

// String stringifies a RunTaskRequest.
func (r RunTaskRequest) String() string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should implement the fmt.Stringer interface if it can't be used across packages. For example, if we do fmt.Sprintf("%s", RunTaskRequest{...}) it's going to write "copilot task run ..." which is surprising.

Instead, we can create our own string method: CLIString() string and a type CLIStringer interface so that we can implement this interface across many data types

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed!

@Lou1415926 Lou1415926 added the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label May 5, 2021
Copy link
Copy Markdown
Contributor

@iamhopaul123 iamhopaul123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests! LGTM 🚢

@Lou1415926 Lou1415926 removed the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label May 5, 2021
@mergify mergify Bot merged commit ff64cf1 into aws:mainline May 5, 2021
mergify Bot pushed a commit that referenced this pull request May 10, 2021
…oad (#2246)

This PR implement the function to generate a run task request from a deployed job, and the functions to support it.

This PR is rebased from #2201 (which is not merged yet). The real changes to this PR are at 
- `ecs/run_task_request.go` (where the information needed to generated a `task run` command is grabbed from a Copilot job)
- `ecs/run_task_request_test.go`
- `aws/stepfunctions/*` (where the client to make API calls to stepfunctions is implemented)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
patnolanireland pushed a commit to patnolanireland/copilot-cli that referenced this pull request May 12, 2021
…oad (aws#2246)

This PR implement the function to generate a run task request from a deployed job, and the functions to support it.

This PR is rebased from aws#2201 (which is not merged yet). The real changes to this PR are at 
- `ecs/run_task_request.go` (where the information needed to generated a `task run` command is grabbed from a Copilot job)
- `ecs/run_task_request_test.go`
- `aws/stepfunctions/*` (where the client to make API calls to stepfunctions is implemented)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
thrau pushed a commit to localstack/copilot-cli-local that referenced this pull request Dec 9, 2022
This PR implements `--generate-cmd` for `task run` given either a non-Copilot ECS service or a Copilot service.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
thrau pushed a commit to localstack/copilot-cli-local that referenced this pull request Dec 9, 2022
…oad (aws#2246)

This PR implement the function to generate a run task request from a deployed job, and the functions to support it.

This PR is rebased from aws#2201 (which is not merged yet). The real changes to this PR are at 
- `ecs/run_task_request.go` (where the information needed to generated a `task run` command is grabbed from a Copilot job)
- `ecs/run_task_request_test.go`
- `aws/stepfunctions/*` (where the client to make API calls to stepfunctions is implemented)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants