Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IZE-516 ize up infra explain bash alternative shown #474

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be best to use backend config template as it is actually generated, and not recreate it manually. Can we do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

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
}