Skip to content

Commit 198c20d

Browse files
mdimicelijpalermo
authored andcommitted
set local terraform binary path
1 parent 93e9169 commit 198c20d

File tree

8 files changed

+52
-55
lines changed

8 files changed

+52
-55
lines changed

application/configuration.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package application
33
import "github.com/cloudfoundry/bosh-bootloader/storage"
44

55
type GlobalConfiguration struct {
6-
StateDir string
7-
Debug bool
8-
Name string
9-
UseTfLocalBinary bool
6+
StateDir string
7+
Debug bool
8+
Name string
9+
TerraformBinary bool
1010
}
1111

1212
type StringSlice []string

bbl/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ func main() {
9494
// Terraform
9595
terraformOutputBuffer := bytes.NewBuffer([]byte{})
9696
dotTerraformDir := filepath.Join(appConfig.Global.StateDir, "terraform", ".terraform")
97-
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary)
97+
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary)
9898
var (
9999
terraformCLI terraform.CLI
100100
out io.Writer
101101
)
102102
if appConfig.Global.Debug {
103103
errBuffer := io.MultiWriter(os.Stderr, terraformOutputBuffer)
104-
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary)
104+
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary)
105105
out = os.Stdout
106106
} else {
107107
terraformCLI = bufferingCLI

commands/usage.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ Usage:
1414
1515
Global Options:
1616
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
17-
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
18-
--debug [-d] Prints debugging output env:"BBL_DEBUG"
17+
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
18+
--debug [-d] Prints debugging output env:"BBL_DEBUG"
1919
--version [-v] Prints version
2020
--no-confirm [-n] No confirm
21-
--use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY"
21+
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
2222
%s
2323
`
2424
CommandUsage = `

commands/usage_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ Usage:
4040
4141
Global Options:
4242
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
43-
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
44-
--debug [-d] Prints debugging output env:"BBL_DEBUG"
43+
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
44+
--debug [-d] Prints debugging output env:"BBL_DEBUG"
4545
--version [-v] Prints version
4646
--no-confirm [-n] No confirm
47-
--use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY"
47+
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
4848
4949
Basic Commands: A good place to start
5050
up Deploys BOSH director on an IAAS, creates CF/Concourse load balancers. Updates existing director.
@@ -86,11 +86,11 @@ Troubleshooting Commands:
8686
8787
Global Options:
8888
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
89-
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
90-
--debug [-d] Prints debugging output env:"BBL_DEBUG"
89+
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
90+
--debug [-d] Prints debugging output env:"BBL_DEBUG"
9191
--version [-v] Prints version
9292
--no-confirm [-n] No confirm
93-
--use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY"
93+
--terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY"
9494
9595
[my-command command options]
9696
some message

config/global_flags.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package config
22

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

1414
AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID"`
1515
AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY"`

terraform/binary_path.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"os"
8-
"os/exec"
98
"strconv"
109
"strings"
1110
"time"
@@ -28,36 +27,34 @@ type tfBinaryPathFs interface {
2827
}
2928

3029
type Binary struct {
31-
FS tfBinaryPathFs
32-
EmbedData embed.FS
33-
Path string
34-
UseLocalBinary bool
30+
FS tfBinaryPathFs
31+
EmbedData embed.FS
32+
Path string
33+
TerraformBinary string
3534
}
3635

3736
//go:embed binary_dist
3837
var content embed.FS
3938

40-
func NewBinary(tfUseLocalBinary bool) *Binary {
39+
func NewBinary(terraformBinary string) *Binary {
4140
fs := afero.Afero{Fs: afero.NewOsFs()}
4241
return &Binary{
43-
FS: fs,
44-
Path: "binary_dist",
45-
EmbedData: content,
46-
UseLocalBinary: tfUseLocalBinary,
42+
FS: fs,
43+
Path: "binary_dist",
44+
EmbedData: content,
45+
TerraformBinary: terraformBinary,
4746
}
4847
}
4948

5049
func (binary *Binary) BinaryPath() (string, error) {
51-
// if user has a terraform use it
52-
if binary.UseLocalBinary {
53-
userTerraform, err := exec.LookPath(tfBinDataAssetName)
54-
if err == nil && userTerraform != "" {
55-
return userTerraform, nil
56-
}
50+
// if user sets a terraform binary use it
51+
exists, err := binary.FS.Exists(binary.TerraformBinary)
52+
if err == nil && exists {
53+
return binary.TerraformBinary, nil
5754
}
5855

5956
destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName)
60-
exists, err := binary.FS.Exists(destinationPath)
57+
exists, err = binary.FS.Exists(destinationPath)
6158
if err != nil {
6259
return "", err
6360
}

terraform/binary_path_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ var _ = Describe("BinaryPath", func() {
3939
fileSystem.ExistsCall.Returns.Bool = false
4040

4141
binary = &terraform.Binary{
42-
Path: "testassets/success",
43-
EmbedData: content,
44-
FS: fileSystem,
45-
UseLocalBinary: false,
42+
Path: "testassets/success",
43+
EmbedData: content,
44+
FS: fileSystem,
45+
TerraformBinary: "",
4646
}
4747
})
4848

terraform/cli.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import (
88
)
99

1010
type CLI struct {
11-
errorBuffer io.Writer
12-
outputBuffer io.Writer
13-
tfDataDir string
14-
tfUseLocalBinary bool
11+
errorBuffer io.Writer
12+
outputBuffer io.Writer
13+
tfDataDir string
14+
terraformBinary string
1515
}
1616

17-
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, tfUseLocalBinary bool) CLI {
17+
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, terraformBinary string) CLI {
1818
return CLI{
19-
errorBuffer: errorBuffer,
20-
outputBuffer: outputBuffer,
21-
tfDataDir: tfDataDir,
22-
tfUseLocalBinary: tfUseLocalBinary,
19+
errorBuffer: errorBuffer,
20+
outputBuffer: outputBuffer,
21+
tfDataDir: tfDataDir,
22+
terraformBinary: terraformBinary,
2323
}
2424
}
2525

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

3030
func (c CLI) RunWithEnv(stdout io.Writer, workingDirectory string, args []string, extraEnvVars []string) error {
31-
path, err := NewBinary(c.tfUseLocalBinary).BinaryPath()
31+
path, err := NewBinary(c.terraformBinary).BinaryPath()
3232
if err != nil {
3333
return err
3434
}

0 commit comments

Comments
 (0)