8000 feat: Download default terraform version when minor version mismatches by AbhineetJain · Pull Request #1775 · coder/coder · GitHub
[go: up one dir, main page]

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
use the new exectuor version method
  • Loading branch information
AbhineetJain committed Jun 22, 2022
commit 0e1aa3f5720ff9b197531d82984ff00a264fffb2
15 changes: 15 additions & 0 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ func (e executor) version(ctx context.Context) (*version.Version, error) {
return version.NewVersion(vj.Version)
}

func versionFromBinaryPath(ctx context.Context, binaryPath string) (*version.Version, error) {
// #nosec
cmd := exec.CommandContext(ctx, 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)
}

func (e executor) init(ctx context.Context, logr logger) error {
outWriter, doneOut := logWriter(logr, proto.LogLevel_DEBUG)
errWriter, doneErr := logWriter(logr, proto.LogLevel_ERROR)
Expand Down
31 changes: 7 additions & 24 deletions provisioner/terraform/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package terraform

import (
"context"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/cli/safeexec"
"github.com/hashicorp/go-version"
Expand All @@ -19,8 +16,9 @@ import (

// This is the exact version of Terraform used internally
// when Terraform is missing on the system.
const terraformVersion = "1.1.9"
const versionDelimiter = "."
var terraformVersion = version.Must(version.NewVersion("1.1.9"))
var minTerraformVersion = version.Must(version.NewVersion("1.1.0"))
var maxTerraformVersion = version.Must(version.NewVersion("1.2.0"))

var (
// The minimum version of Terraform supported by the provisioner.
Expand Down Expand Up @@ -63,25 +61,10 @@ func Serve(ctx context.Context, options *ServeOptions) error {
return xerrors.Errorf("absolute: %w", err)
}
// Checking the installed version of Terraform.
output, err := exec.Command(absoluteBinary, "version").Output()
version, err := versionFromBinaryPath(ctx, absoluteBinary)
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
} else if version.LessThan(minTerraformVersion) || version.GreaterThanOrEqual(maxTerraformVersion) {
downloadTerraform = true
}
if !downloadTerraform {
Expand All @@ -92,7 +75,7 @@ func Serve(ctx context.Context, options *ServeOptions) error {
installer := &releases.ExactVersion{
InstallDir: options.CachePath,
Product: product.Terraform,
Version: version.Must(version.NewVersion(terraformVersion)),
Version: terraformVersion,
}

execPath, err := installer.Install(ctx)
Expand Down
0