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: state rm #2880

Merged
merged 15 commits into from
Jan 19, 2023
Merged

feat: state rm #2880

merged 15 commits into from
Jan 19, 2023

Conversation

krrrr38
Copy link
Contributor

@krrrr38 krrrr38 commented Dec 26, 2022

what

  • comment_parser can parse subcommand
    • command handles command args, so add arg count limitation by command
  • add atlantis state rm ADDRESS... command
  • add state_rm stage which do init and state_rm steps
    • state_rm step do terraform state rm and discard a planfile.
  • not implemented in this PR
    • state_requirements like apply_requirements

why

terraform state rm is required manually. If Atlantis provides this feature, we do it on GitHub PR and so on.

references

@krrrr38 krrrr38 marked this pull request as ready for review December 26, 2022 06:30
@krrrr38 krrrr38 requested a review from a team as a code owner December 26, 2022 06:30
```
### Explanation
Runs `terraform state rm` that matches the directory/project/workspace.
This command discards the terraform plan result. After an import and before an apply, another `atlantis plan` must be run again.
Copy link
Member

@nitrocode nitrocode Dec 26, 2022

Choose a reason for hiding this comment

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

Should there be another flag like atlantis terraform state rm ADDRESS --auto-plan-on-success ?

We spoke about it in another comment for atlantis terraform import.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it might be helpful. In this case, we can do atlantis plan manually, so this is not a required parameter.

Copy link
Member

@nitrocode nitrocode left a comment

Choose a reason for hiding this comment

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

Thanks again for this contribution. Looks very promising!

runatlantis.io/docs/using-atlantis.md Show resolved Hide resolved
@nitrocode nitrocode added this to the 0.22.1 milestone Dec 27, 2022
@krrrr38 krrrr38 force-pushed the feat-state-rm branch 5 times, most recently from da8a8b1 to 410920e Compare December 27, 2022 20:58
@jamengual jamengual added the feature New functionality/enhancement label Dec 29, 2022
@nitrocode nitrocode modified the milestones: 0.22.2, 0.22.3 Jan 6, 2023
@krrrr38
Copy link
Contributor Author

krrrr38 commented Jan 7, 2023

@krrrr38 krrrr38 mentioned this pull request Jan 7, 2023
1 task
stateRmCmd = append(stateRmCmd, ctx.EscapedCommentArgs...)
out, err := p.TerraformExecutor.RunCommandWithVersion(ctx, filepath.Clean(path), stateRmCmd, envs, tfVersion, ctx.Workspace)

// If the state rm was successful and a plan file exists, delete the plan.
Copy link
Contributor

Choose a reason for hiding this comment

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

We should document this in the .md docs

Copy link
Contributor Author

@krrrr38 krrrr38 Jan 13, 2023

Choose a reason for hiding this comment

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

docs is written in https://deploy-preview-2880--runatlantis.netlify.app/docs/using-atlantis.html#atlantis-state-rm, like This command discards the terraform plan result. After run state rm and before an apply, another atlantis plan must be run again.

In addition, I added vcs comment about discarding like notify import/state rm discard plan file


revert to 🔁 use repeat instead warning for re-plan

@@ -217,7 +225,7 @@ func (p *DefaultProjectCommandBuilder) BuildAutoplanCommands(ctx *command.Contex
// See ProjectCommandBuilder.BuildPlanCommands.
func (p *DefaultProjectCommandBuilder) BuildPlanCommands(ctx *command.Context, cmd *CommentCommand) ([]command.ProjectContext, error) {
if !cmd.IsForSpecificProject() {
return p.buildAllCommandsByCfg(ctx, cmd.CommandName(), cmd.Flags, cmd.Verbose)
return p.buildAllCommandsByCfg(ctx, cmd.CommandName(), cmd.SubName, cmd.Flags, cmd.Verbose)
Copy link
Member

@nitrocode nitrocode Jan 10, 2023

Choose a reason for hiding this comment

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

This probably came up before, but why not just pass the whole cmd instead of select SubName, Flags, and Verbose like we do with buildProjectCommand and buildProjectApplyCommand ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BuildAutoplanCommands doesn't have cmd *CommentCommand. We can do like following, but I think this should not be introduced. Because cmd := CommentCommand{Name: command.Plan} might be cause nil pointer panic or invalid implementations by contributers who want to change buildAllCommandsByCfg and don't know passed lacked CommentCommand.

>  gd
diff --git a/server/events/project_command_builder.go b/server/events/project_command_builder.go
index d1068da2..08928d88 100644
--- a/server/events/project_command_builder.go
+++ b/server/events/project_command_builder.go
@@ -207,7 +207,8 @@ type DefaultProjectCommandBuilder struct {

 // See ProjectCommandBuilder.BuildAutoplanCommands.
 func (p *DefaultProjectCommandBuilder) BuildAutoplanCommands(ctx *command.Context) ([]command.ProjectContext, error) {
-       projCtxs, err := p.buildAllCommandsByCfg(ctx, command.Plan, "", nil, false)
+       cmd := CommentCommand{Name: command.Plan}
+       projCtxs, err := p.buildAllCommandsByCfg(ctx, cmd)
        if err != nil {
                return nil, err
        }
@@ -225,7 +226,7 @@ func (p *DefaultProjectCommandBuilder) BuildAutoplanCommands(ctx *command.Contex
 // See ProjectCommandBuilder.BuildPlanCommands.
 func (p *DefaultProjectCommandBuilder) BuildPlanCommands(ctx *command.Context, cmd *CommentCommand) ([]command.ProjectContext, error) {
        if !cmd.IsForSpecificProject() {
-               return p.buildAllCommandsByCfg(ctx, cmd.CommandName(), cmd.SubName, cmd.Flags, cmd.Verbose)
+               return p.buildAllCommandsByCfg(ctx, cmd)
        }

@nitrocode nitrocode modified the milestones: 0.22.3, 0.23.0 Jan 10, 2023
@@ -12,5 +12,5 @@ The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
```

* :repeat: To **plan** this project again, comment:
* :repeat: plan file was discarded, to **plan** this project again, comment:
Copy link
Member

Choose a reason for hiding this comment

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

This seems like more of a summary statement. Generally we put summary statements on its own line between the triple backticks and the * :repeat:

Copy link
Contributor Author

@krrrr38 krrrr38 Jan 14, 2023

Choose a reason for hiding this comment

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

like this? perl -pi -e 's!* 🔁 plan file was discarded. to!🚮 A plan file was dis…

your Terraform state and will henceforth be managed by Terraform.
```

:put_litter_in_its_place: A plan file was discarded. Re-plan would be required before applying.

* :repeat: To **plan** this project again, comment:
  * `atlantis plan`

@nitrocode
Copy link
Member

@krrrr38 is this ready to be merged?

@krrrr38
Copy link
Contributor Author

krrrr38 commented Jan 19, 2023

yes

@nitrocode nitrocode merged commit cb0aadf into runatlantis:main Jan 19, 2023
@nitrocode
Copy link
Member

Thanks @krrrr38 ! Can't wait to try out this feature.

@gmaghera
Copy link
Contributor

gmaghera commented Mar 2, 2023

Can the remove behavior be overridden via custom workflows?

Those of us who use Terragrunt would love to get this feature too.

@krrrr38
Copy link
Contributor Author

krrrr38 commented Mar 2, 2023

@gmaghera you can customize state rm workflow by overriing state_rm using run step`
https://www.runatlantis.io/docs/custom-workflows.html#workflow-1

In run step, you can use COMMENT_ARGS to pass args. But it has a problem which is separated by comma and so on. So now you need to do workaround like this.
Improvement is suggested in opened issue #3094.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality/enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support atlantis state rm subcommand
4 participants