Skip to content
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

Use local terraform binary #603

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions application/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package application
import "github.com/cloudfoundry/bosh-bootloader/storage"

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

type StringSlice []string
Expand Down
5 changes: 4 additions & 1 deletion application/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func (l *Logger) Prompt(message string) bool {
l.newline = true

var proceed string
fmt.Fscanln(l.reader, &proceed)
_, err := fmt.Fscanln(l.reader, &proceed)
if err != nil {
return false
}

proceed = strings.ToLower(proceed)
if proceed == "yes" || proceed == "y" {
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)
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)
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary)
out = os.Stdout
} else {
terraformCLI = bufferingCLI
Expand Down
11 changes: 6 additions & 5 deletions commands/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ Usage:
bbl [GLOBAL OPTIONS] %s [OPTIONS]

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"
--version [-v] Prints version
--no-confirm [-n] No confirm
--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"
--version [-v] Prints version
--no-confirm [-n] No confirm
--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
22 changes: 12 additions & 10 deletions commands/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ Usage:
bbl [GLOBAL OPTIONS] COMMAND [OPTIONS]

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"
--version [-v] Prints version
--no-confirm [-n] No confirm
--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"
--version [-v] Prints version
--no-confirm [-n] No confirm
--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 @@ -84,11 +85,12 @@ Troubleshooting Commands:
bbl [GLOBAL OPTIONS] my-command [OPTIONS]

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"
--version [-v] Prints version
--no-confirm [-n] No confirm
--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"
--version [-v] Prints version
--no-confirm [-n] No confirm
--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
17 changes: 9 additions & 8 deletions config/global_flags.go
Original file line number Diff line number Diff line change
@@ -1,14 +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"`
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
24 changes: 17 additions & 7 deletions terraform/binary_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,34 @@ type tfBinaryPathFs interface {
}

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

//go:embed binary_dist
var content embed.FS

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

func (binary *Binary) BinaryPath() (string, error) {
// if user sets a terraform binary use it
if binary.TerraformBinary != "" {
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)
if err != nil {
Expand Down
34 changes: 31 additions & 3 deletions terraform/binary_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ var _ = Describe("BinaryPath", func() {
fileSystem.ExistsCall.Returns.Bool = false

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

Expand All @@ -59,6 +60,33 @@ var _ = Describe("BinaryPath", func() {
Expect(fileSystem.ChtimesCall.Receives.ModTime).To(Equal(modTime))
})

Context("when a custom terraform binary path is set", func() {
BeforeEach(func() {
binary.TerraformBinary = "/some/custom/path/bbl-terraform"
})

Context("and the file exists", func() {
BeforeEach(func() {
fileSystem.ExistsCall.Returns.Bool = true
})

It("doesn't rewrite the file", func() {
res, err := binary.BinaryPath()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal("/some/custom/path/bbl-terraform"))
Expect(fileSystem.WriteFileCall.CallCount).To(Equal(0))
})
})

Context("but the file does not exist", func() {
It("uses the embedded binary ", func() {
res, err := binary.BinaryPath()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal("/some/tmp/path/bbl-terraform"))
})
})
})

Context("when there is no my-terraform-binary in box", func() {
BeforeEach(func() {
binary.EmbedData = contentOnlyModTime
Expand Down
18 changes: 10 additions & 8 deletions terraform/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
)

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

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

Expand All @@ -26,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().BinaryPath()
path, err := NewBinary(c.terraformBinary).BinaryPath()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (e Executor) runTFCommandWithEnvs(args, envs []string) error {
if e.debug {
return err
}
return fmt.Errorf(redactedError)
return fmt.Errorf("%s", redactedError)
}

return nil
Expand Down Expand Up @@ -224,7 +224,7 @@ func (e Executor) Validate(credentials map[string]string) error {
if e.debug {
return err
}
return fmt.Errorf(redactedError)
return fmt.Errorf("%s", redactedError)
}

return nil
Expand Down
Loading