Skip to content

feat: Download default terraform version when minor version mismatches #1775

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

Merged
merged 12 commits into from
Jun 22, 2022

Conversation

AbhineetJain
Copy link
Contributor

@AbhineetJain AbhineetJain commented May 26, 2022

This PR downloads the default Terraform version when the minor version of the already installed Terraform is different.

Subtasks

  • Identify the installed version by parsing terraform version.
  • Download the default version if there's a mismatch or we can't identify the version.
  • Add unit tests for the feature.

Fixes #1743

@AbhineetJain AbhineetJain requested a review from mafredri May 26, 2022 06:19
Comment on lines 56 to 72
// If the "coder" binary is in the same directory as
// the "terraform" binary, "terraform" is returned.
//
// We must resolve the absolute path for other processes
// to execute this properly!
absoluteBinary, err := filepath.Abs(binaryPath)
if err != nil {
return xerrors.Errorf("absolute: %w", err)
}
// Checking the installed version of Terraform.
output, err := exec.Command(absoluteBinary, "version").Output()
if err != nil {
return xerrors.Errorf("terraform version: %w", err)
}
// The output for `terraform version` is:
// Terraform v1.2.1
// on linux_amd64
versionRegex := regexp.MustCompile("Terraform v(.+)\n?.*")
match := versionRegex.FindStringSubmatch(string(output))
if match != nil {
// match[0] is the entire string.
// match[1] is the matched substring.
version := match[1]
terraformMinorVersion := strings.Join(strings.Split(terraformVersion, versionDelimiter)[:2], versionDelimiter)
if !strings.HasPrefix(version, terraformMinorVersion) {
downloadTerraform = true
}
} else {
// Download the required Terraform version when unable to determine the existing one.
downloadTerraform = true
}
if !downloadTerraform {
options.BinaryPath = absoluteBinary
}
Copy link
Member

Choose a reason for hiding this comment

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

The terraform-exec library already handles most of this for us!

https://github.com/coder/coder/blob/main/provisioner/terraform/provision.go#L63-L70

@ammario
Copy link
Member

ammario commented Jun 16, 2022

What is the status of this PR?

@AbhineetJain
Copy link
Contributor Author

What is the status of this PR?

@ammario Got stuck in limbo as I moved onto some frontend changes. I am back to addressing the comments on it this week; we should be able to get it in by EOW.

@AbhineetJain AbhineetJain force-pushed the abhineetjain/default-terraform-version branch from 07c6480 to 91fd183 Compare June 17, 2022 15:45
@AbhineetJain AbhineetJain marked this pull request as ready for review June 17, 2022 16:39
@AbhineetJain AbhineetJain force-pushed the abhineetjain/default-terraform-version branch from 91fd183 to ccddf23 Compare June 17, 2022 19:08
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Looks good, beautiful tests 😉!

Comment on lines 93 to 96

t.Cleanup(func() {
t.Setenv("PATH", pathVariable)
})
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
t.Cleanup(func() {
t.Setenv("PATH", pathVariable)
})

I don't think this is needed? t.Setenv already defines a cleanup function that restores the environment in the earlier step.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed it along with the top level t.Parallel since this test was conflicting with some other tests.

@AbhineetJain AbhineetJain force-pushed the abhineetjain/default-terraform-version branch 2 times, most recently from cad1205 to 7964593 Compare June 21, 2022 14:45
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

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

Nice!

@@ -41,33 +43,54 @@ type ServeOptions struct {
Logger slog.Logger
}

func getAbsoluteBinaryPath(ctx context.Context) (string, bool) {
Copy link
Member

Choose a reason for hiding this comment

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

Following Go idioms, this get prefix could be removed!

@AbhineetJain AbhineetJain force-pushed the abhineetjain/default-terraform-version branch 4 times, most recently from ed969f2 to 4580e8c Compare June 22, 2022 19:02
@AbhineetJain AbhineetJain force-pushed the abhineetjain/default-terraform-version branch from 4580e8c to 744052f Compare June 22, 2022 21:03
@AbhineetJain AbhineetJain force-pushed the abhineetjain/default-terraform-version branch from 744052f to 6b84a73 Compare June 22, 2022 22:09
@AbhineetJain AbhineetJain enabled auto-merge (squash) June 22, 2022 22:58
@AbhineetJain AbhineetJain merged commit c6b1daa into main Jun 22, 2022
@AbhineetJain AbhineetJain deleted the abhineetjain/default-terraform-version branch June 22, 2022 23:11
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.

Default to downloading Terraform when the system has different minor version
5 participants