Skip to content

Commit

Permalink
Merge pull request #299 from kubernetes-simulator/reduce-init
Browse files Browse the repository at this point in the history
Reduce the number of times we Terraform init
  • Loading branch information
pi-unnerup authored Sep 22, 2020
2 parents 7ace01d + e4dcc83 commit 0037ea0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
9 changes: 5 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cmd
import (
"bufio"
"fmt"
"os"
"strings"

"github.com/kubernetes-simulator/simulator/pkg/simulator"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"strings"
)

func saveBucketConfig(logger *logrus.Logger, bucket string) {
Expand Down Expand Up @@ -40,7 +41,7 @@ func newInitCommand() *cobra.Command {
fmt.Print("Please choose a globally unique name for an S3 bucket to store the terraform state: ")
bucket, err := reader.ReadString('\n')
if err != nil {
return errors.Wrap(err, "Error reading bucket nbame from stdin")
return errors.Wrap(err, "Error reading bucket name from stdin")
}

bucket = strings.TrimSpace(bucket)
Expand All @@ -67,7 +68,7 @@ func newInitCommand() *cobra.Command {
logger.WithFields(logrus.Fields{
"BucketName": bucket,
}).Warn("Simulator is already configured to use an S3 bucket")
logger.Warn("Please remove the state-bucket from simulator.yaml to create another")
logger.Warn("Please remove the state-bucket from simulator.yaml then exit and re-enter the container to create another")
return nil
},
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/simulator/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simulator

import (
"fmt"
"os"
"strings"

"github.com/kubernetes-simulator/simulator/pkg/childminder"
Expand Down Expand Up @@ -105,10 +106,12 @@ func (s *Simulator) InitIfNeeded() error {
return errors.Wrap(err, "Error writing tfvars")
}

s.Logger.Info("Running terraform init")
_, err = s.Terraform("init")
if err != nil {
return errors.Wrap(err, "Error initialising terraform")
if _, err := os.Stat("/app/terraform/deployments/AWS/.terraform"); os.IsNotExist(err) {
s.Logger.Info("Running terraform init")
_, err = s.Terraform("init")
if err != nil {
return errors.Wrap(err, "Error initialising terraform")
}
}

return nil
Expand Down

0 comments on commit 0037ea0

Please sign in to comment.