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

feat: add "pipeline delete" command #652

Merged
merged 9 commits into from
Feb 17, 2020
Merged

Conversation

SoManyHs
Copy link
Contributor

Closes #630

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

@SoManyHs SoManyHs requested a review from a team as a code owner February 12, 2020 09:48
@SoManyHs SoManyHs requested review from clareliguori and removed request for clareliguori February 12, 2020 09:48
Copy link
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.

Looks really good, thank you!

Comment on lines +134 to +142
if err := o.deleteSecret(); err != nil {
return err
}

if err := o.deleteStack(); err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we ignore the errors if the secret and stack are already deleted?

Maybe we can write something like log.Successf("Secret %s is already removed.", o.PipelineSecret)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, is this an error we handle like this elsewhere? I was mostly following suit of what app_delete.go does for the stack deletion. On the underlying cloudformation.DeleteStack method we already wrap some errors. Wondering if you think we should add some sugar on top of that?

For the deletion of secret, the errors returned by secretsmanager.DeleteSecret don't seem to return like, an idempotency error. Unless I'm misunderstanding what you mean when you say these things are already deleted?

Copy link
Contributor

Choose a reason for hiding this comment

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

app_delete calls delete-stack API which does not return an error if stack does not exist. However describe-stacks will return ValidationError if stack does not exist. Maybe we can use delete-stack in pipeline delete as well so that we don't need to validate if pipeline already exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay so, with the latest changes (see: 3b9bf4c) here's what's happening with the following workflow:

  1. Runpipeline init - creates new secret and writes pipeline.yml
  2. Runpipeline update - deploys pipeline via CFN
  3. From the AWS Console, I schedule the deletion of the secret I just made through pipeline init. This schedules the secret for deletion in N days.
  4. run pipeline delete :
~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline delete


? Are you sure you want to delete pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend? Yes


? Are you sure you want to delete the source secret github-token-backend-aws-reinvent-trivia-2019-backend associated with pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend? Yes
✔ Deleted secret github-token-backend-aws-reinvent-trivia-2019-backend.
✔ Deleted pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend.

~/aws-reinvent-2019-trivia-backend(master)$ aws secretsmanager list-secrets --region us-east-2
{
    "SecretList": [
    ]
}

So as far as idempotency for deleting the secret, it seems to be okay for both deleting the secret and the stack (since the stack only refers to the name of the secret, and does not actually create it so there wouldn't be any stack drift).

Copy link
Contributor Author

@SoManyHs SoManyHs Feb 14, 2020

Choose a reason for hiding this comment

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

app_delete calls delete-stack API which does not return an error if stack does not exist. However describe-stacks will return ValidationError if stack does not exist. Maybe we can use delete-stack in pipeline delete as well so that we don't need to validate if pipeline already exists?

I'm not seeing this in app_delete or in env_delete -- am I missing something? @iamhopaul123

Copy link
Contributor

Choose a reason for hiding this comment

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

@efekarakus
Copy link
Contributor

oh can you update the git commit to follow: https://www.conventionalcommits.org/en/v1.0.0/

feat: add "pipeline delete" command

@clareliguori clareliguori changed the title Delete pipeline feat: add "pipeline delete" command Feb 12, 2020
Copy link
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.

LGTM! Except for the idempotency issue since we possibly need to delete both secret and CFN stack.


func (cf CloudFormation) DeletePipeline(stackName string) error {
// Check if the stack exists
out, err := cf.describeStack(&cloudformation.DescribeStacksInput{
Copy link
Contributor

Choose a reason for hiding this comment

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

Same idempotency caveat here. If CFN stack has been deleted.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think what @iamhopaul123 means is that we can actually remove the describeStack call and call directly cf.delete(stackName) which handles ignoring the stackDoesNotExist error

@SoManyHs
Copy link
Contributor Author

Oh. Would we want to delete the pipeline manifest if we delete the pipeline? It doesn't look like we delete the app manifest when we call app delete, but wondering if there's a reason to keep them around? @efekarakus

@efekarakus
Copy link
Contributor

Oh. Would we want to delete the pipeline manifest if we delete the pipeline? It doesn't look like we delete the app manifest when we call app delete, but wondering if there's a reason to keep them around? @efekarakus

We delete them in app delete:
https://github.com/aws/amazon-ecs-cli-v2/blob/b223142e566e3bd123b541ff8cc500d4b3a113fb/internal/pkg/cli/app_delete.go#L264-L271

@SoManyHs SoManyHs added the area/pipeline Issues about pipelines to release applications. label Feb 13, 2020
@SoManyHs SoManyHs force-pushed the pipeline-delete branch 2 times, most recently from 3194e26 to 705e5ca Compare February 14, 2020 01:29
@SoManyHs
Copy link
Contributor Author

SoManyHs commented Feb 14, 2020

Noting here for my own bookkeeping:
Ran pipeline init and pipeline update, then deleted the CFN stack for the pipeline via the AWS console. Running pipeline delete results in:

~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline delete


? Are you sure you want to delete pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend? Yes


? Are you sure you want to delete the source secret github-token-backend-aws-reinvent-trivia-2019-backend associated with pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend? Yes
✔ Deleted secret github-token-backend-aws-reinvent-trivia-2019-backend.
✘ Failed to delete pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend: failed to find a stack named backend-pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend.
Error: failed to find a stack named backend-pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend

We should try to handle this gracefully.

Also, might want to test this if the pipeline is deleted from the CodePipeline (rather than the CFN) console manually.

return []*secretsmanager.Tag{
{
Key: aws.String("ecs-project"),
Value: aws.String(timestamp),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be the project name instead?


func (cf CloudFormation) DeletePipeline(stackName string) error {
// Check if the stack exists
out, err := cf.describeStack(&cloudformation.DescribeStacksInput{
Copy link
Contributor

Choose a reason for hiding this comment

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

I think what @iamhopaul123 means is that we can actually remove the describeStack call and call directly cf.delete(stackName) which handles ignoring the stackDoesNotExist error

This was leftover from a refactor in which we moved much of the PreRunE
logic into the constructor for the opts. This approach allows for ease
and consistency of testing.

NOTE: See 16d3bb1 -- previously, calling the constructors outside of
RunE made it harder to catch errors returned by the constructor itself.
This allows the errors to be caught will still getting values from the
command's flags..
Neither field will be passed in as flags, so they can be moved out of
deletePipelineVars.
Since we always assume the pipeline for a workspace will be named
"pipeline.yml", we are not passing this field in as a flag.
Copy link
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.

LGTM! As long as the deletion can continue and return 0 even if the pipeline stack has already been removed manually before; and secret manager won't yell at me if the secret's not exist before pipeline delete.

@SoManyHs SoManyHs force-pushed the pipeline-delete branch 2 times, most recently from 5c01605 to 34dc68b Compare February 17, 2020 00:57
@SoManyHs
Copy link
Contributor Author

~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline init

? Would you like to add an environment to your pipeline? Yes

? Which environment would you like to add to your pipeline? test

? Which GitHub repository would you like to use for your application? [email protected]:SoManyHs/aws-reinvent-trivia-2019-backend

? Please enter your GitHub Personal Access Token for your repository: aws-reinvent-trivia-2019-backend [? for help] *****
✔ Secret already exists for aws-reinvent-trivia-2019-backend! Do nothing.
✔ Wrote the pipeline manifest for aws-reinvent-trivia-2019-backend at 'ecs-project/pipeline.yml'
✔ Wrote the buildspec for the pipeline's build stage at 'ecs-project/buildspec.yml'
The manifest contains configurations for your CodePipeline resources, such as your pipeline stages and build steps.
The buildspec contains the commands to build and push your container images to your ECR repositories.

Recommended follow-up actions:
- Update the build phase of your buildspec to unit test your applications before pushing the images.
- Update your pipeline manifest to add additional stages.
- Run `ecs-preview pipeline update` to deploy your pipeline for the repository.

~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline update
✔ Successfully added pipeline resources to your project: backend
✔ Successfully created a new pipeline: pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend

~/aws-reinvent-2019-trivia-backend(master)$ aws secretsmanager list-secrets
{
    "SecretList": [
        {
            "Name": "github-token-backend-aws-reinvent-trivia-2019-backend", 
            "Tags": [
                {
                    "Value": "Mon Feb 17 00:54:21 UTC 2020", 
                    "Key": "ecs-project"
                }
            ], 
            "LastChangedDate": 1581901027.553, 
            "SecretVersionsToStages": {
                "872299C7-E403-4C5C-83C2-C69CF769BE36": [
                    "AWSCURRENT"
                ]
            }, 
            "LastAccessedDate": 1581897600.0, 
            "ARN": "arn:aws:secretsmanager:us-east-2:xxxxxxxx:secret:github-token-backend-aws-reinvent-trivia-2019-backend-tcA2LL"
        }
    ]
}

~/aws-reinvent-2019-trivia-backend(master)$ ls ecs-project/
api-app.yml	backend-app.yml	buildspec.yml	pipeline.yml

~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline delete


? Are you sure you want to delete pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend? Yes


? Are you sure you want to delete the source secret github-token-backend-aws-reinvent-trivia-2019-backend associated with pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend? Yes
✔ Deleted secret github-token-backend-aws-reinvent-trivia-2019-backend.
✔ Deleted pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend.
✔ Deleted pipeline manifest from workspace.

@SoManyHs
Copy link
Contributor Author

When running pipeline delete after pipeline stack has been deleted manually:

~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline init

? Would you like to add an environment to your pipeline? Yes

? Which environment would you like to add to your pipeline? test

? Which GitHub repository would you like to use for your application? [email protected]:SoManyHs/aws-reinvent-trivia-2019-backend

? Please enter your GitHub Personal Access Token for your repository: aws-reinvent-trivia-2019-backend [? for help] ******
✔ Created the secret github-token-backend-aws-reinvent-trivia-2019-backend for pipeline source stage!
✔ Wrote the pipeline manifest for aws-reinvent-trivia-2019-backend at 'ecs-project/pipeline.yml'
✔ Wrote the buildspec for the pipeline's build stage at 'ecs-project/buildspec.yml'
The manifest contains configurations for your CodePipeline resources, such as your pipeline stages and build steps.
The buildspec contains the commands to build and push your container images to your ECR repositories.

Recommended follow-up actions:
- Update the build phase of your buildspec to unit test your applications before pushing the images.
- Update your pipeline manifest to add additional stages.
- Run `ecs-preview pipeline update` to deploy your pipeline for the repository.

~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline update
✔ Successfully added pipeline resources to your project: backend
✔ Successfully created a new pipeline: pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend

~/aws-reinvent-2019-trivia-backend(master)$ ecs-preview pipeline delete


? Are you sure you want to delete pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend? Yes


? Are you sure you want to delete the source secret github-token-backend-aws-reinvent-trivia-2019-backend associated with pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend? Yes
✔ Deleted secret github-token-backend-aws-reinvent-trivia-2019-backend.
✔ Deleted pipeline pipeline-backend-SoManyHs-aws-reinvent-trivia-2019-backend from project backend.
✔ Deleted pipeline manifest from workspace.

The message isn't quite right, but can fix in a future PR.

@SoManyHs SoManyHs merged commit 4612ad5 into aws:master Feb 17, 2020
@SoManyHs SoManyHs deleted the pipeline-delete branch February 17, 2020 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/pipeline Issues about pipelines to release applications.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pipeline delete experience
3 participants