Skip to content

Commit

Permalink
set local terraform binary path
Browse files Browse the repository at this point in the history
  • Loading branch information
mdimiceli committed Aug 23, 2024
1 parent f0efeb5 commit c9224c6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 55 deletions.
8 changes: 4 additions & 4 deletions application/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package application
import "github.com/cloudfoundry/bosh-bootloader/storage"

type GlobalConfiguration struct {
StateDir string
Debug bool
Name string
UseTfLocalBinary bool
StateDir string
Debug bool
Name string
TerraformBinary bool
}

type StringSlice []string
Expand Down
4 changes: 2 additions & 2 deletions bbl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ func main() {
// Terraform
terraformOutputBuffer := bytes.NewBuffer([]byte{})
dotTerraformDir := filepath.Join(appConfig.Global.StateDir, "terraform", ".terraform")
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary)
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary)
var (
terraformCLI terraform.CLI
out io.Writer
)
if appConfig.Global.Debug {
errBuffer := io.MultiWriter(os.Stderr, terraformOutputBuffer)
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary)
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary)
out = os.Stdout
} else {
terraformCLI = bufferingCLI
Expand Down
6 changes: 3 additions & 3 deletions commands/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Usage:
Global Options:
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
--debug [-d] Prints debugging output env:"BBL_DEBUG"
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
--debug [-d] Prints debugging output env:"BBL_DEBUG"
--version [-v] Prints version
--no-confirm [-n] No confirm
--use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY"
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
%s
`
CommandUsage = `
Expand Down
12 changes: 6 additions & 6 deletions commands/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Usage:
Global Options:
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
--debug [-d] Prints debugging output env:"BBL_DEBUG"
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
--debug [-d] Prints debugging output env:"BBL_DEBUG"
--version [-v] Prints version
--no-confirm [-n] No confirm
--use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY"
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
Basic Commands: A good place to start
up Deploys BOSH director on an IAAS, creates CF/Concourse load balancers. Updates existing director.
Expand Down Expand Up @@ -86,11 +86,11 @@ Troubleshooting Commands:
Global Options:
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
--debug [-d] Prints debugging output env:"BBL_DEBUG"
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
--debug [-d] Prints debugging output env:"BBL_DEBUG"
--version [-v] Prints version
--no-confirm [-n] No confirm
--use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY"
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
[my-command command options]
some message
Expand Down
18 changes: 9 additions & 9 deletions config/global_flags.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package config

type GlobalFlags struct {
Help bool `short:"h" long:"help"`
Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"`
Version bool `short:"v" long:"version"`
NoConfirm bool `short:"n" long:"no-confirm"`
StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"`
StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"`
EnvID string ` long:"name"`
IAAS string ` long:"iaas" env:"BBL_IAAS"`
UseTfLocalBinary bool `short:"u" long:"use-tf-local-binary" env:"BBL_USE_TF_LOCAL_BINARY"`
Help bool `short:"h" long:"help"`
Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"`
Version bool `short:"v" long:"version"`
NoConfirm bool `short:"n" long:"no-confirm"`
StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"`
StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"`
EnvID string ` long:"name"`
IAAS string ` long:"iaas" env:"BBL_IAAS"`
TerraformBinary string ` long:"terraform-binary" env:"BBL_TERRAFORM_BINARY"`

AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID"`
AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY"`
Expand Down
31 changes: 14 additions & 17 deletions terraform/binary_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
Expand All @@ -28,36 +27,34 @@ type tfBinaryPathFs interface {
}

type Binary struct {
FS tfBinaryPathFs
EmbedData embed.FS
Path string
UseLocalBinary bool
FS tfBinaryPathFs
EmbedData embed.FS
Path string
TerraformBinary string
}

//go:embed binary_dist
var content embed.FS

func NewBinary(tfUseLocalBinary bool) *Binary {
func NewBinary(terraformBinary string) *Binary {
fs := afero.Afero{Fs: afero.NewOsFs()}
return &Binary{
FS: fs,
Path: "binary_dist",
EmbedData: content,
UseLocalBinary: tfUseLocalBinary,
FS: fs,
Path: "binary_dist",
EmbedData: content,
TerraformBinary: terraformBinary,
}
}

func (binary *Binary) BinaryPath() (string, error) {
// if user has a terraform use it
if binary.UseLocalBinary {
userTerraform, err := exec.LookPath(tfBinDataAssetName)
if err == nil && userTerraform != "" {
return userTerraform, nil
}
// if user sets a terraform binary use it
exists, err := binary.FS.Exists(binary.TerraformBinary)
if err == nil && exists {
return binary.TerraformBinary, nil
}

destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName)
exists, err := binary.FS.Exists(destinationPath)
exists, err = binary.FS.Exists(destinationPath)
if err != nil {
return "", err
}
Expand Down
8 changes: 4 additions & 4 deletions terraform/binary_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ var _ = Describe("BinaryPath", func() {
fileSystem.ExistsCall.Returns.Bool = false

binary = &terraform.Binary{
Path: "testassets/success",
EmbedData: content,
FS: fileSystem,
UseLocalBinary: false,
Path: "testassets/success",
EmbedData: content,
FS: fileSystem,
TerraformBinary: "",
}
})

Expand Down
20 changes: 10 additions & 10 deletions terraform/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
)

type CLI struct {
errorBuffer io.Writer
outputBuffer io.Writer
tfDataDir string
tfUseLocalBinary bool
errorBuffer io.Writer
outputBuffer io.Writer
tfDataDir string
terraformBinary string
}

func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, tfUseLocalBinary bool) CLI {
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, terraformBinary string) CLI {
return CLI{
errorBuffer: errorBuffer,
outputBuffer: outputBuffer,
tfDataDir: tfDataDir,
tfUseLocalBinary: tfUseLocalBinary,
errorBuffer: errorBuffer,
outputBuffer: outputBuffer,
tfDataDir: tfDataDir,
terraformBinary: terraformBinary,
}
}

Expand All @@ -28,7 +28,7 @@ func (c CLI) Run(stdout io.Writer, workingDirectory string, args []string) error
}

func (c CLI) RunWithEnv(stdout io.Writer, workingDirectory string, args []string, extraEnvVars []string) error {
path, err := NewBinary(c.tfUseLocalBinary).BinaryPath()
path, err := NewBinary(c.terraformBinary).BinaryPath()
if err != nil {
return err
}
Expand Down

0 comments on commit c9224c6

Please sign in to comment.