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

Add ability to force unlock TF state and fix digest value of lock file in DynamoDB #235

Merged
merged 13 commits into from
Sep 7, 2023

Conversation

nb1701
Copy link
Contributor

@nb1701 nb1701 commented Aug 22, 2023

Sometimes, if a failure occurs or you've Ctrl+C'ed out of the middle of an apply, the Terraform state may be left in a locked state. When this happens, you must run 'terraform force-unlock' with the lock ID to unlock it.

This change detects when an apply results in a lock error, and prompts the user to rerun the command with the --force-unlock flag. It does this by capturing the stderr stream of the 'terraform apply' command to inspect the message if an error occurs. An added bonus is that since we are using Popen to execute the command, we can handle things more gracefully if a user Ctrl+Cs out of the apply and allow Terraform to release the lock, reducing the amount of ways we can get into this failure state.

Secondly, if something goes wrong during deployment, especially when a user has force-unlocked due to a previous issue and then multiple apply actions are happening at once, the digest value for the Terraform lock file in S3 can be incorrect. This lets us set the digest value to the correct value, as given by the error message of a previous Terraform command, without having to go into the AWS console to set it manually.

Requires civiform/civiform-deploy#50 and addresses civiform/civiform#5381

This also adds a bin/fmt script to format files like the checks are expecting. It is not containerized and requires the binaries to be installed locally, so we probably want to containerize it later. But it works as a first pass.

Copy link
Contributor

Choose a reason for hiding this comment

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

praise: Thanks for adding this!!

cloud/shared/bin/lib/terraform.py Outdated Show resolved Hide resolved
cloud/shared/bin/lib/terraform.py Outdated Show resolved Hide resolved
bin/fmt Outdated Show resolved Hide resolved
bin/fmt Outdated Show resolved Hide resolved
cloud/aws/templates/aws_oidc/bin/aws_cli.py Outdated Show resolved Hide resolved
cloud/aws/templates/aws_oidc/bin/aws_cli.py Outdated Show resolved Hide resolved
cloud/aws/templates/aws_oidc/bin/aws_cli.py Outdated Show resolved Hide resolved
cloud/shared/bin/lib/terraform.py Outdated Show resolved Hide resolved
cloud/shared/bin/lib/terraform.py Outdated Show resolved Hide resolved
cloud/shared/bin/lib/terraform.py Outdated Show resolved Hide resolved
Sometimes, if a failure occurs or you've Ctrl+C'ed out of the middle of
an apply, the Terraform state may be left in a locked state. When this
happens, you must run 'terraform force-unlock' with the lock ID to
unlock it.

This change detects when an apply results in a lock error, and prompts
the user to rerun the command with the --force-unlock flag. It does this
by capturing the stderr stream of the 'terraform apply' command to
inspect the message if an error occurs. An added bonus is that since we
are using Popen to execute the command, we can handle things more
gracefully if a user Ctrl+Cs out of the apply and allow Terraform to
release the lock, reducing the amount of ways we can get into this
failure state.

In order to support the --force-unlock flag, there will be a change to
the civiform-deploy repo as well.
If something goes wrong during deployment, especially when a user has force-unlocked due to a previous issue and then multiple apply actions are happening at once, the digest value for the Terraform lock file in S3 can be incorrect. This lets us set the digest value to the correct value, as given by the error message of a previous Terraform command, without having to go into the AWS console to set it manually.
This currently relies on the binaries being installed locally. We may want to move to using the civiform formatter container instead, or something like it, in the future.
If we hit a TF lock state issue, or the S3 digest issue, this allows the
user to fix the problem during this particular invocation of the
command, so they do not need to rerun the command. If we are not running
in a TTY, we'll fall back to prompting the user to rerun the command
with the appropriate flag.
… fix a problem

This allows us not to have to change a CE's fork of civiform-deploy.
@nb1701
Copy link
Contributor Author

nb1701 commented Aug 30, 2023

Sorry for re-reviewers who want to see changes since the last review, things got screwy and I had to force push a full set of new commits :(

@nb1701 nb1701 requested review from bion and dkatzz August 30, 2023 00:25
cloud/shared/bin/lib/terraform.py Show resolved Hide resolved
cloud/shared/bin/lib/terraform.py Outdated Show resolved Hide resolved
@nb1701 nb1701 removed the request for review from bion September 7, 2023 19:01
@nb1701 nb1701 requested review from bion and removed request for bion September 7, 2023 19:01
@nb1701 nb1701 merged commit ef8a912 into main Sep 7, 2023
5 checks passed
@nb1701 nb1701 deleted the nb1701/tf_lock branch September 7, 2023 20:56
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.

3 participants