Skip to content

Commit

Permalink
Merge pull request #144 from kubernetes-simulator/providerstf
Browse files Browse the repository at this point in the history
Providerstf
  • Loading branch information
raoulmillais authored Dec 19, 2019
2 parents 57818af + b4282b6 commit 18cd8c3
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 95 deletions.
40 changes: 3 additions & 37 deletions cmd/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,8 @@ 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{
Expand All @@ -53,17 +25,11 @@ func newCreateCommand(logger *zap.SugaredLogger) *cobra.Command {
scenariosDir := viper.GetString("scenarios-dir")
attackTag := viper.GetString("attack-container-tag")
tfDir := viper.GetString("tf-dir")

//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)
err := simulator.Create(logger, tfDir, bucket, attackTag)
if err != nil {
logger.Errorw("Error creating infrastructure", zap.Error(err))
}
Expand Down
23 changes: 15 additions & 8 deletions pkg/simulator/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"github.com/controlplaneio/simulator-standalone/pkg/util"
"github.com/pkg/errors"
"go.uber.org/zap"
"fmt"
)

// PrepareTfArgs takes a string with the terraform command desired and returns
// a slice of strings containing the complete list of arguments including the
// command to use when exec'ing terraform
func PrepareTfArgs(cmd string) []string {
func PrepareTfArgs(cmd string, bucket string) []string {
arguments := []string{cmd}

if cmd == "output" {
Expand All @@ -20,6 +21,12 @@ func PrepareTfArgs(cmd string) []string {
if cmd == "init" || cmd == "plan" || cmd == "apply" || cmd == "destroy" {
arguments = append(arguments, "-input=false")
arguments = append(arguments, "--var-file=settings/bastion.tfvars")

}

if cmd == "init" {
providerBucketArg := fmt.Sprintf("-backend-config=bucket=%s", bucket)
arguments = append(arguments, providerBucketArg)
}

if cmd == "apply" || cmd == "destroy" {
Expand All @@ -30,8 +37,8 @@ func PrepareTfArgs(cmd string) []string {
}

// Terraform wraps running terraform as a child process
func Terraform(wd, cmd string) (*string, error) {
args := PrepareTfArgs(cmd)
func Terraform(wd, cmd string, bucket string) (*string, error) {
args := PrepareTfArgs(cmd, bucket)
env := []string{"TF_IS_IN_AUTOMATION=1", "TF_INPUT=0"}
if cmd == "output" {
// TODO: (rem) deal with non-empty stderr?
Expand Down Expand Up @@ -77,7 +84,7 @@ func InitIfNeeded(logger *zap.SugaredLogger, tfDir, bucket, attackTag string) er
}

logger.Info("Running terraform init")
_, err = Terraform(tfDir, "init")
_, err = Terraform(tfDir, "init", bucket)
if err != nil {
return errors.Wrap(err, "Error initialising terraform")
}
Expand All @@ -97,13 +104,13 @@ func Create(logger *zap.SugaredLogger, tfDir, bucket, attackTag string) error {
}

logger.Info("Running terraform plan")
_, err = Terraform(tfDir, "plan")
_, err = Terraform(tfDir, "plan", bucket)
if err != nil {
return err
}

logger.Info("Running terraform apply")
_, err = Terraform(tfDir, "apply")
_, err = Terraform(tfDir, "apply", bucket)
return err
}

Expand All @@ -116,7 +123,7 @@ func Status(logger *zap.SugaredLogger, tfDir, bucket, attackTag string) (*Terraf
}

logger.Info("Running terraform output")
out, err := Terraform(tfDir, "output")
out, err := Terraform(tfDir, "output", bucket)
if err != nil {
return nil, errors.Wrap(err, "Error getting terraform outputs")
}
Expand All @@ -140,6 +147,6 @@ func Destroy(logger *zap.SugaredLogger, tfDir, bucket, attackTag string) error {
}

logger.Info("Running terrraform destroy")
_, err = Terraform(tfDir, "destroy")
_, err = Terraform(tfDir, "destroy", bucket)
return err
}
16 changes: 8 additions & 8 deletions pkg/simulator/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import (
var noopLogger = zap.NewNop().Sugar()

var tfCommandArgumentsTests = []struct {
command string
prepArgs []string
arguments []string
}{
{"output", []string{"output", "-json"}},
{"init", []string{"init", "-input=false", "--var-file=settings/bastion.tfvars"}},
{"plan", []string{"plan", "-input=false", "--var-file=settings/bastion.tfvars"}},
{"apply", []string{"apply", "-input=false", "--var-file=settings/bastion.tfvars", "-auto-approve"}},
{"destroy", []string{"destroy", "-input=false", "--var-file=settings/bastion.tfvars", "-auto-approve"}},
{[]string{"output", "test-bucket"}, []string{"output", "-json"}},
{[]string{"init", "test-bucket"}, []string{"init", "-input=false", "--var-file=settings/bastion.tfvars", "-backend-config=bucket=test-bucket"}},
{[]string{"plan", "test-bucket"}, []string{"plan", "-input=false", "--var-file=settings/bastion.tfvars"}},
{[]string{"apply", "test-bucket"}, []string{"apply", "-input=false", "--var-file=settings/bastion.tfvars", "-auto-approve"}},
{[]string{"destroy", "test-bucket"}, []string{"destroy", "-input=false", "--var-file=settings/bastion.tfvars", "-auto-approve"}},
}

func Test_PrepareTfArgs(t *testing.T) {
for _, tt := range tfCommandArgumentsTests {
t.Run("Test arguments for "+tt.command, func(t *testing.T) {
assert.Equal(t, simulator.PrepareTfArgs(tt.command), tt.arguments)
t.Run("Test arguments for "+tt.prepArgs[0], func(t *testing.T) {
assert.Equal(t, simulator.PrepareTfArgs(tt.prepArgs[0], tt.prepArgs[1]), tt.arguments)
})
}
}
Expand Down
36 changes: 1 addition & 35 deletions pkg/simulator/terraform_vars.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package simulator

import (
"fmt"
"github.com/controlplaneio/simulator-standalone/pkg/util"
"github.com/pkg/errors"
"io/ioutil"
"path/filepath"
"strings"
)

// TfVars struct representing the input variables for terraform to create the
Expand All @@ -28,31 +23,8 @@ func NewTfVars(publicKey, accessCIDR, bucketName, attackTag string) TfVars {
}
}

func writeProvidersFile(tfDir, bucket string) error {
providerspath := filepath.Join(tfDir, "providers.tf")
input, err := ioutil.ReadFile(providerspath)
if err != nil {
return errors.Wrapf(err, "Error reading providers file %s", providerspath)
}

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

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

return nil
}

func (tfv *TfVars) String() string {
return "access_key = \"" + tfv.PublicKey + "\"\n" + "access_cidr = \"" + tfv.AccessCIDR + "\"\n" + "attack_container_tag = \"" + tfv.AttackTag + "\"\n"
return "access_key = \"" + tfv.PublicKey + "\"\n" + "access_cidr = \"" + tfv.AccessCIDR + "\"\n" + "attack_container_tag = \"" + tfv.AttackTag + "\"\n" + "state_bucket_name = \"" + tfv.BucketName + "\"\n"

}

Expand All @@ -61,11 +33,5 @@ func EnsureLatestTfVarsFile(tfDir, publicKey, accessCIDR, bucket, attackTag stri
filename := tfDir + "/settings/bastion.tfvars"
tfv := NewTfVars(publicKey, accessCIDR, bucket, attackTag)

err := writeProvidersFile(tfDir, bucket)
if err != nil {
return errors.Wrap(err, "Error saving bucket name")

}

return util.OverwriteFile(filename, tfv.String())
}
5 changes: 3 additions & 2 deletions pkg/simulator/terraform_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

func Test_TfVars_String(t *testing.T) {
t.Parallel()
tfv := simulator.NewTfVars("ssh-rsa", "10.0.0.1/16", "test", "latest")
tfv := simulator.NewTfVars("ssh-rsa", "10.0.0.1/16", "test-bucket", "latest")
expected := `access_key = "ssh-rsa"
access_cidr = "10.0.0.1/16"
attack_container_tag = "latest"
state_bucket_name = "test-bucket"
`
assert.Equal(t, tfv.String(), expected)
}
Expand All @@ -21,7 +22,7 @@ func Test_Ensure_TfVarsFile_with_settings(t *testing.T) {
tfDir := fixture("tf-dir-with-settings")
varsFile := tfDir + "/settings/bastion.tfVars"

err := simulator.EnsureLatestTfVarsFile(tfDir, "ssh-rsa", "10.0.0.1/16", "test", "latest")
err := simulator.EnsureLatestTfVarsFile(tfDir, "ssh-rsa", "10.0.0.1/16", "test-bucket", "latest")
assert.Nil(t, err, "Got an error")

assert.Equal(t, util.MustSlurp(varsFile), "test = true\n")
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 = "${merge(var.default_tags, map("Simulator Bucket", "${var.s3_bucket_name}"))}"
aws_tags = "${merge(var.default_tags, map("Simulator Bucket", "${var.state_bucket_name}"))}"
}

// Setup networking
Expand Down
2 changes: 0 additions & 2 deletions terraform/deployments/AWS/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ provider "aws" {}
terraform {
backend "s3" {
key = "simulator.tfstate"
// 'bucket='' must have this exact number of spaces for simulator to replace it properly
bucket = "###REPLACED-BY-SIMULATOR###"
// Optional, S3 Bucket Server Side Encryption
encrypt = false
}
Expand Down
1 change: 0 additions & 1 deletion terraform/deployments/AWS/terraform.tfvars

This file was deleted.

3 changes: 2 additions & 1 deletion terraform/deployments/AWS/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ variable "attack_container_tag" {
default = "latest"
}

variable "s3_bucket_name" {
variable "state_bucket_name" {
description = "name of the s3 state bucket"
default = "not-defined"
}


Expand Down

0 comments on commit 18cd8c3

Please sign in to comment.