-
Notifications
You must be signed in to change notification settings - Fork 887
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
Conversation
provisioner/terraform/serve.go
Outdated
// 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 | ||
} |
There was a problem hiding this comment.
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
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. |
07c6480
to
91fd183
Compare
91fd183
to
ccddf23
Compare
There was a problem hiding this 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 😉!
|
||
t.Cleanup(func() { | ||
t.Setenv("PATH", pathVariable) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.
cad1205
to
7964593
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
provisioner/terraform/serve.go
Outdated
@@ -41,33 +43,54 @@ type ServeOptions struct { | |||
Logger slog.Logger | |||
} | |||
|
|||
func getAbsoluteBinaryPath(ctx context.Context) (string, bool) { |
There was a problem hiding this comment.
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!
ed969f2
to
4580e8c
Compare
4580e8c
to
744052f
Compare
744052f
to
6b84a73
Compare
This PR downloads the default Terraform version when the minor version of the already installed Terraform is different.
Subtasks
terraform version
.Fixes #1743