Skip to content

Commit

Permalink
asset/cluster: Invoke terraform in a temp dir.
Browse files Browse the repository at this point in the history
In order to be able to use openshift-install to launch multiple
clusters we need to create a working dir for each invocation.

Here we create a tmp dir that has the terraform.tfvars file and
invoke the cluster within the dir.

The dir will be removed after the invocation is done.
  • Loading branch information
Yifan Gu committed Sep 24, 2018
1 parent 00862d4 commit 7f3ae0a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/asset/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/openshift/installer/pkg/asset"
Expand Down Expand Up @@ -49,8 +50,14 @@ func (c *Cluster) Generate(parents map[asset.Asset]*asset.State) (*asset.State,
return nil, fmt.Errorf("failed to get terraform.tfvar state in the parent asset states")
}

// Copy the terraform.tfvars to the working dir for the terraform.
if err := ioutil.WriteFile(filepath.Join(dir, state.Contents[0].Name), state.Contents[0].Data, 0600); err != nil {
// Copy the terraform.tfvars to a temp directory where the terraform will be invoked within.
tmpDir, err := ioutil.TempDir(os.TempDir(), "openshift-install-")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)

if err := ioutil.WriteFile(filepath.Join(tmpDir, state.Contents[0].Name), state.Contents[0].Data, 0600); err != nil {
return nil, fmt.Errorf("failed to write terraform.tfvars file: %v", err)
}

Expand All @@ -65,11 +72,11 @@ func (c *Cluster) Generate(parents map[asset.Asset]*asset.State) (*asset.State,
}

// This runs the terraform in the terraform template directory.
if err := terraform.Init(dir, templateDir); err != nil {
if err := terraform.Init(tmpDir, templateDir); err != nil {
return nil, err
}

stateFile, err := terraform.Apply(dir, terraform.InfraStep, templateDir)
stateFile, err := terraform.Apply(tmpDir, terraform.InfraStep, templateDir)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7f3ae0a

Please sign in to comment.