terramate-io / terramate

Terramate CLI is an open-source Infrastructure as Code (IaC) Orchestration and Code Generation tool for Terraform, OpenTofu and Terragrunt.
https://terramate.io
Mozilla Public License 2.0
3.12k stars 86 forks source link

[FEATURE] Add OpenTofu support to Terramate Cloud synchronization #1647

Closed athak closed 2 months ago

athak commented 2 months ago

Describe the bug The Terramate Cloud sync preview in the CLI is always using terraform to show the plan files. When using OpenTofu and there is a version discrepancy between the binaries in the path (e.g. OpenTofu 1.6.x and Terraform 1.5.x) or when there is no Terraform installed, it results in error.

To Reproduce Run:

terramate run --continue-on-error \
                    --cloud-sync-preview \
                    --cloud-sync-terraform-plan-file=out.tfplan \
                    --debug-preview-url preview_url.txt \
                    -- \
                    tofu plan -out out.tfplan -lock=false

without a terraform binary in the path or with a terraform binary that it is a different version.

Expected behavior It should use OpenTofu to show the plan files and not result in error.

Log Output With different versions installed:

2024-04-16T20:07:49-03:00 ERR command stderr action=runTerraformShow command="terraform show -no-color drift.tfplan" planfile=drift.tfplan stack=/x/y/z stderr="\nError: Failed to read the given file as a state or plan file\n\nState read error: Error reading drift.tfplan as a statefile: 2 problems:\n\n- Unsupported state file format: The state file could not be parsed as JSON:\nsyntax error at byte offset 1.\n- Unsupported state file format: The state file does not have a \"version\"\nattribute, which is required to identify the format version.\n\nPlan read error: plan file was created by Terraform 1.6.2, but this is 1.5.7;\nplan files cannot be transferred between different Terraform versions\n"

Without Terraform installed:

024-04-17T18:39:37Z ERR command stderr action=runTerraformShow command="terraform show -no-color out.tfplan" planfile=out.tfplan stack=/x/y/z stderr=
2024-04-17T18:39:37Z WRN failed to synchronize the ASCII plan output error="failed to gather details from plan file: executing: terraform show -no-color out.tfplan" action=getTerraformChangeset planfile=out.tfplan stack=/x/y/z

Environment (please complete the following information):

soerenmartius commented 2 months ago

turns out that we always call terraform under the hood when syncing plans to Terramate Cloud. We'll provide a fix soon that will allow you to switch to opentofu using the --opentofu flag to switch to tofu similar to what we've done for terragrunt

athak commented 2 months ago

@soerenmartius sounds great, thanks! Any chance that Terramate can autodetect what binary to use based on the command used for run? You could always override with a flag but from UX point of view it would better IMHO.

soerenmartius commented 2 months ago

@soerenmartius sounds great, thanks! Any chance that Terramate can autodetect what binary to use based on the command used for run? You could always override with a flag but from UX point of view it would better IMHO.

good idea, let me discuss this with the team and see what we can come up with

i4ki commented 2 months ago

Hey @athak

We released Terramate v0.8.0 last week with support for --tofu-plan-file.

Now your command should be:

terramate run --continue-on-error \
                    --sync-preview \
                    --tofu-plan-file=out.tfplan \
                    --debug-preview-url preview_url.txt \
                    -- \
                    tofu plan -out out.tfplan -lock=false

Let us know if it works for you.