Skip to content

Commit

Permalink
Merge pull request #135 from kubernetes-simulator/s3vars
Browse files Browse the repository at this point in the history
S3vars
  • Loading branch information
abdullahgarcia authored Dec 10, 2019
2 parents 6ddeefb + 1219b9c commit d84311d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
41 changes: 40 additions & 1 deletion cmd/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,37 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"io/ioutil"
"path/filepath"
"fmt"
"strings"
)

func writeS3VarsFile(logger *zap.SugaredLogger, tfDir, bucket string) error {
logger.Infof("Writing s3 bucket %s to tfvars\n", bucket)
bucketvarspath := filepath.Join(tfDir, "terraform.tfvars")
input, err := ioutil.ReadFile(bucketvarspath)
if err != nil {
return errors.Wrapf(err, "Error reading bucket vars file %s", bucketvarspath)
}

lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.Contains(line, "s3_bucket_name = ") {
lines[i] = fmt.Sprintf("s3_bucket_name = \"%s\"", bucket)
}
}
output := strings.Join(lines, "\n")

err = ioutil.WriteFile(bucketvarspath, []byte(output), 0644)
if err != nil {
return errors.Wrapf(err, "Error writing providers file %s", bucketvarspath)
}

logger.Infof("Wrote s3 bucket %s to tfvars\n", bucket)
return nil
}

func newCreateCommand(logger *zap.SugaredLogger) *cobra.Command {
cmd := &cobra.Command{
Use: `create`,
Expand All @@ -24,7 +53,17 @@ func newCreateCommand(logger *zap.SugaredLogger) *cobra.Command {
scenariosDir := viper.GetString("scenarios-dir")
attackTag := viper.GetString("attack-container-tag")
tfDir := viper.GetString("tf-dir")
err := simulator.Create(logger, tfDir, bucket, attackTag)

//bucket var
logger.Infof("Creating variable %s for terraform s3 bucket\n", bucket)
err := writeS3VarsFile(logger, tfDir, bucket)
if err != nil {
return errors.Wrap(err, "Error saving bucket name")
}
logger.Infof("Created s3 bucket %s for terraform remote state\n", bucket)
//bucket var

err = simulator.Create(logger, tfDir, bucket, attackTag)
if err != nil {
logger.Errorw("Error creating infrastructure", zap.Error(err))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func newInitCommand() *cobra.Command {
Short: "Creates and configures a bucket for remote state",
RunE: func(cmd *cobra.Command, args []string) error {
bucket := viper.GetString("state-bucket")

logger, err := newLogger(viper.GetString("loglevel"), "console")
if err != nil {
logger.Fatalf("Can't re-initialize zap logger: %v", err)
Expand All @@ -43,6 +44,7 @@ func newInitCommand() *cobra.Command {

bucket = strings.TrimSpace(bucket)


logger.Infof("Creating s3 bucket %s for terraform remote state\n", bucket)
if err = simulator.CreateRemoteStateBucket(logger, bucket); err != nil {
if strings.HasPrefix(errors.Cause(err).Error(), "BucketAlreadyOwnedByYou") {
Expand All @@ -53,8 +55,6 @@ func newInitCommand() *cobra.Command {

return errors.Wrapf(err, "Error creating s3 bucket %s", bucket)
}

logger.Infof("Created s3 bucket %s for terraform remote state\n", bucket)
saveBucketConfig(logger, bucket)

return nil
Expand Down
2 changes: 1 addition & 1 deletion terraform/deployments/AWS/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

locals {
aws_tags = "${var.default_tags}"
aws_tags = "${merge(var.default_tags, map("Simulator Bucket", "${var.s3_bucket_name}"))}"
}

// Setup networking
Expand Down
1 change: 1 addition & 0 deletions terraform/deployments/AWS/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s3_bucket_name = "###replaced by infra.go###"
6 changes: 5 additions & 1 deletion terraform/deployments/AWS/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ variable "attack_container_tag" {
default = "latest"
}

variable "s3_bucket_name" {
description = "name of the s3 state bucket"
}


variable "default_tags" {
description = "Default tags for all resources"
type = "map"
default = {
Product = "simulator"
}
}

0 comments on commit d84311d

Please sign in to comment.