Skip to content

Commit

Permalink
run terraform init when plugins are missing
Browse files Browse the repository at this point in the history
Many environments remove the `./terraform/.terraform` directory from the
bbl state directory to reduce the bbl-state size.

bbl replaces these plugins on subsequent "up" and "destroy" operations
as needed, but as of bbl v9, these plugins are also required for "bbl
outputs" and "bbl jumpbox-address" commands.

This change runs "terraform init --upgrade" when terraform outputs are
requested and the terraform/.terraform directory is missing.

This allow environments to omit the .terraform directory to still reduce
the bbl-state size and also supports consuming bbl-state across
different architectures (e.g. linux <=> darwin) without having to
manually run "terraform init".
  • Loading branch information
abg authored and rkoster committed May 4, 2023
1 parent f5384de commit ad241f7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,25 @@ func (e Executor) Output(outputName string) (string, error) {
return strings.TrimSuffix(buffer.String(), "\n"), nil
}

func (e Executor) terraformInitIfNeeded(terraformDir string) error {
_, err := e.fs.Stat(filepath.Join(terraformDir, ".terraform"))
if err == nil {
return nil
}

return e.Init()
}

func (e Executor) Outputs() (map[string]interface{}, error) {
terraformDir, err := e.stateStore.GetTerraformDir()
if err != nil {
return map[string]interface{}{}, err
}

if err = e.terraformInitIfNeeded(terraformDir); err != nil {
return nil, err
}

varsDir, err := e.stateStore.GetVarsDir()
if err != nil {
return map[string]interface{}{}, err
Expand Down
39 changes: 39 additions & 0 deletions terraform/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,45 @@ var _ = Describe("Executor", func() {
}))
})

When("the .terraform diretory is present", func() {
It("does not run an unnecessary terraform init", func() {
_, err := executor.Outputs()
Expect(err).NotTo(HaveOccurred())

Expect(cli.RunCall.CallCount).To(Equal(0))
})
})

When("the .terraform directory is missing", func() {
BeforeEach(func() {
fileIO.StatCall.Fake = func(s string) (os.FileInfo, error) {
if strings.HasSuffix(s, ".terraform") {
return nil, os.ErrNotExist
}
return nil, nil
}
})

It("runs terraform init ", func() {
_, err := executor.Outputs()
Expect(err).NotTo(HaveOccurred())

Expect(cli.RunCall.CallCount).To(Equal(1))
Expect(cli.RunCall.Receives.Args).To(Equal([]string{"init", "--upgrade"}))
})

When("running terraform init fails", func() {
It("returns an error", func() {
cli.RunCall.Returns.Errors = []error{
errors.New("kiwi"),
}

_, err := executor.Outputs()
Expect(err).To(MatchError("Run terraform init --upgrade: kiwi"))
})
})
})

Context("when an error occurs", func() {
Context("when it fails to get vars dir", func() {
BeforeEach(func() {
Expand Down

0 comments on commit ad241f7

Please sign in to comment.