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
Prev Previous commit
Next Next commit
refactor into methods
  • Loading branch information
AbhineetJain committed Jun 22, 2022
commit 544243fda4b9ec8620823212cda137555b15797b
13 changes: 1 addition & 12 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,7 @@ func (e executor) checkMinVersion(ctx context.Context) error {
}

func (e executor) version(ctx context.Context) (*version.Version, error) {
// #nosec
cmd := exec.CommandContext(ctx, e.binaryPath, "version", "-json")
out, err := cmd.Output()
if err != nil {
return nil, err
}
vj := tfjson.VersionOutput{}
err = json.Unmarshal(out, &vj)
if err != nil {
return nil, err
}
return version.NewVersion(vj.Version)
return versionFromBinaryPath(ctx, e.binaryPath)
}

func versionFromBinaryPath(ctx context.Context, binaryPath string) (*version.Version, error) {
Expand Down
53 changes: 28 additions & 25 deletions provisioner/terraform/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,38 @@ 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!

binaryPath, err := safeexec.LookPath("terraform")
if err != nil {
return "", false
}
// 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 "", false
}
// Checking the installed version of Terraform.
version, err := versionFromBinaryPath(ctx, absoluteBinary)
if err != nil {
return "", false
} else if version.LessThan(minTerraformVersion) || version.GreaterThanOrEqual(maxTerraformVersion) {
return "", false
}
return absoluteBinary, true
}

// Serve starts a dRPC server on the provided transport speaking Terraform provisioner.
func Serve(ctx context.Context, options *ServeOptions) error {
if options.BinaryPath == "" {
binaryPath, err := safeexec.LookPath("terraform")
var downloadTerraform bool
if err != nil {
downloadTerraform = true
absoluteBinary, ok := getAbsoluteBinaryPath(ctx)

if ok {
options.BinaryPath = absoluteBinary
} else {
// 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.
version, err := versionFromBinaryPath(ctx, absoluteBinary)
if err != nil {
downloadTerraform = true
} else if version.LessThan(minTerraformVersion) || version.GreaterThanOrEqual(maxTerraformVersion) {
downloadTerraform = true
}
if !downloadTerraform {
options.BinaryPath = absoluteBinary
}
}
if downloadTerraform {
installer := &releases.ExactVersion{
InstallDir: options.CachePath,
Product: product.Terraform,
Expand Down