Skip to content

Commit

Permalink
Merge pull request #941 from gdborton/install-terraform-if-binary-not…
Browse files Browse the repository at this point in the history
…-found

Install a fresh terraform binary if we're unable to locate a matching version in PATH.
  • Loading branch information
cjgajard authored Dec 19, 2024
2 parents 5155b00 + 594fe8c commit 41f44c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Requirements

- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
- [Terraform](https://www.terraform.io/downloads.html) >= 1.1.0
- [Go](https://golang.org/doc/install) 1.20 (to build the provider plugin)

## Building the Provider
Expand Down
25 changes: 23 additions & 2 deletions pagerduty/terraform_state_snapshot_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"context"
"fmt"
"log"
"os"
"strings"
"sync"
Expand All @@ -11,6 +12,7 @@ import (
install "github.com/hashicorp/hc-install"
"github.com/hashicorp/hc-install/fs"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hc-install/src"
"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
Expand Down Expand Up @@ -50,19 +52,38 @@ func getTFStateSnapshot() (*tfStateSnapshot, error) {
installer := install.NewInstaller()
defer installer.Remove(ctx)

tfVersionConstrain := ">= 1.0.6"
tfVersionConstrain := ">= 1.1.0"
log.Printf("[pagerduty] Ensuring terraform %q is installed...", tfVersionConstrain)
execPath, err := installer.Ensure(ctx, []src.Source{
&fs.Version{
Product: product.Terraform,
Constraints: version.MustConstraints(version.NewConstraint(tfVersionConstrain)),
},
})

if err != nil {
isTFVersionUnavailableError := strings.Contains(err.Error(), "terraform: executable file not found in $PATH")
if !isTFVersionUnavailableError {
return nil, err
}
return nil, fmt.Errorf("terraform binary version %q not found in $PATH", tfVersionConstrain)
installVersionString := "1.9.8" // latest at the time of writing
installTargetVersion := version.Must(version.NewVersion(installVersionString))
log.Printf("[pagerduty] Unable to locate terraform binary matching %q in $PATH, installing %q", tfVersionConstrain, installVersionString)
execPathInstalled, installError := installer.Ensure(context.Background(), []src.Source{
&fs.ExactVersion{
Product: product.Terraform,
Version: installTargetVersion,
},
&releases.ExactVersion{
Product: product.Terraform,
Version: installTargetVersion,
},
})
if installError != nil {
return nil, fmt.Errorf("[pagerduty] Failed to install terraform %q", installVersionString)
}
log.Printf("[pagerduty] Successfully installed to %q", execPathInstalled)
execPath = execPathInstalled
}

workingDir, err := os.Getwd()
Expand Down

0 comments on commit 41f44c6

Please sign in to comment.