@@ -7,8 +7,37 @@ import (
77 "github.com/spf13/cobra"
88 "github.com/spf13/viper"
99 "go.uber.org/zap"
10+ "io/ioutil"
11+ "path/filepath"
12+ "fmt"
13+ "strings"
1014)
1115
16+ func writeS3VarsFile (logger * zap.SugaredLogger , tfDir , bucket string ) error {
17+ logger .Infof ("Writing s3 bucket %s to tfvars\n " , bucket )
18+ bucketvarspath := filepath .Join (tfDir , "terraform.tfvars" )
19+ input , err := ioutil .ReadFile (bucketvarspath )
20+ if err != nil {
21+ return errors .Wrapf (err , "Error reading bucket vars file %s" , bucketvarspath )
22+ }
23+
24+ lines := strings .Split (string (input ), "\n " )
25+ for i , line := range lines {
26+ if strings .Contains (line , "s3_bucket_name = " ) {
27+ lines [i ] = fmt .Sprintf ("s3_bucket_name = \" %s\" " , bucket )
28+ }
29+ }
30+ output := strings .Join (lines , "\n " )
31+
32+ err = ioutil .WriteFile (bucketvarspath , []byte (output ), 0644 )
33+ if err != nil {
34+ return errors .Wrapf (err , "Error writing providers file %s" , bucketvarspath )
35+ }
36+
37+ logger .Infof ("Wrote s3 bucket %s to tfvars\n " , bucket )
38+ return nil
39+ }
40+
1241func newCreateCommand (logger * zap.SugaredLogger ) * cobra.Command {
1342 cmd := & cobra.Command {
1443 Use : `create` ,
@@ -24,7 +53,17 @@ func newCreateCommand(logger *zap.SugaredLogger) *cobra.Command {
2453 scenariosDir := viper .GetString ("scenarios-dir" )
2554 attackTag := viper .GetString ("attack-container-tag" )
2655 tfDir := viper .GetString ("tf-dir" )
27- err := simulator .Create (logger , tfDir , bucket , attackTag )
56+
57+ //bucket var
58+ logger .Infof ("Creating variable %s for terraform s3 bucket\n " , bucket )
59+ err := writeS3VarsFile (logger , tfDir , bucket )
60+ if err != nil {
61+ return errors .Wrap (err , "Error saving bucket name" )
62+ }
63+ logger .Infof ("Created s3 bucket %s for terraform remote state\n " , bucket )
64+ //bucket var
65+
66+ err = simulator .Create (logger , tfDir , bucket , attackTag )
2867 if err != nil {
2968 logger .Errorw ("Error creating infrastructure" , zap .Error (err ))
3069 }
0 commit comments