Skip to content

Commit

Permalink
Merge pull request #474 from hazelops/IZE-516-ize-up-infra-explain-ba…
Browse files Browse the repository at this point in the history
…sh-alternative-shown

IZE-516 ize up infra explain bash alternative shown
  • Loading branch information
psihachina authored Sep 30, 2022
2 parents ef786cb + 04a26c8 commit 96cc544
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
69 changes: 69 additions & 0 deletions internal/commands/up_infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type UpInfraOptions struct {
AwsRegion string
Version string
UI terminal.UI
Explain bool
}

var upInfraLongDesc = templates.LongDesc(`
Expand Down Expand Up @@ -80,6 +81,7 @@ func NewCmdUpInfra(project *config.Project) *cobra.Command {
}

cmd.Flags().BoolVar(&o.SkipGen, "skip-gen", false, "skip generating terraform files")
cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")
cmd.Flags().StringVar(&o.Version, "infra.terraform.version", "", "set terraform version")
cmd.Flags().StringVar(&o.AwsRegion, "infra.terraform.aws-region", "", "set aws region")
cmd.Flags().StringVar(&o.AwsProfile, "infra.terraform.aws-profile", "", "set aws profile")
Expand Down Expand Up @@ -113,10 +115,16 @@ func (o *UpInfraOptions) Complete() error {
o.Config.Terraform["infra"].AwsRegion = o.Config.AwsRegion
}


if len(o.Config.Terraform["infra"].StateBucketRegion) == 0 {
o.Config.Terraform["infra"].StateBucketRegion = o.Config.Terraform["infra"].AwsRegion
}

if len(o.Version) != 0 {
o.Config.Terraform["infra"].Version = o.Version
}


if len(o.Config.Terraform["infra"].Version) == 0 {
o.Config.Terraform["infra"].Version = o.Config.TerraformVersion
}
Expand All @@ -140,6 +148,67 @@ func (o *UpInfraOptions) Validate() error {
}

func (o *UpInfraOptions) Run() error {
if o.Explain {
tmpl := `# Change to the dir
cd {{.EnvDir}}
# Generate Backend.tf
cat << EOF > backend.tf
provider "aws" {
profile = var.aws_profile
region = var.aws_region
default_tags {
tags = {
env = "{{.Env}}"
namespace = "{{.Namespace}}"
terraform = "true"
}
}
}
terraform {
backend "s3" {
bucket = "{{.Namespace}}-tf-state"
key = "{{.Env}}/terraform.tfstate"
region = "{{.Terraform.infra.StateBucketRegion}}"
profile = "{{.AwsProfile}}"
dynamodb_table = "tf-state-lock"
}
}
EOF
# Generate variables.tfvars
cat << EOF > variables.tfvars
env = "{{.Env}}"
aws_profile = "{{.AwsProfile}}"
aws_region = "{{.AwsRegion}}"
ec2_key_pair_name = "{{.Env}}-{{.Namespace}}"
docker_image_tag = "{{.Tag}}"
ssh_public_key = ""
docker_registry = "{{.DockerRegistry}}"
namespace = "{{.Namespace}}"
root_domain_name = "{{.Terraform.infra.RootDomainName}}"
EOF
# Ensure Terraform is v {{.TerraformVersion}}
terraform --version
# Terraform Plan
terraform plan
# Terraform Apply
terraform apply
`
err := o.Config.Generate(tmpl)
if err != nil {
return err
}

return nil
}

ui := o.UI

if _, ok := o.Config.Terraform["infra"]; ok {
Expand Down
16 changes: 16 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"reflect"
"strings"
"text/template"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
Expand Down Expand Up @@ -562,3 +563,18 @@ func GetApps(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirecti

return apps, cobra.ShellCompDirectiveNoFileComp
}

func (p *Project) Generate(tmpl string) error {
t := template.New("template")
t, err := t.Parse(tmpl)
if err != nil {
return err
}

err = t.Execute(os.Stdout, p)
if err != nil {
return err
}

return nil
}

0 comments on commit 96cc544

Please sign in to comment.