diff --git a/pkg/commands/terraform.go b/pkg/commands/terraform.go index 3c82da21..6ccfe52a 100644 --- a/pkg/commands/terraform.go +++ b/pkg/commands/terraform.go @@ -43,11 +43,19 @@ func addTerraformCmd(topLevel *cobra.Command) { Run: func(cmd *cobra.Command, args []string) { contextLogger := log.WithFields(log.Fields{"subcommand": "apply"}) - contextLogger.Info("Executing terraform apply") - err := options.Apply() + if options.BulkTfPaths == "" { + contextLogger.Info("Executing terraform apply") + err := options.Apply() + if err != nil { + contextLogger.Fatal("Error executing terraform apply - check the outputs") + } + } else { + err := options.BulkApply() + + if err != nil { + contextLogger.Fatal(err) + } - if err != nil { - contextLogger.Fatal("Error executing terraform apply - check the outputs") } }, } @@ -59,7 +67,7 @@ func addTerraformCmd(topLevel *cobra.Command) { Run: func(cmd *cobra.Command, args []string) { contextLogger := log.WithFields(log.Fields{"subcommand": "plan"}) - if options.BulkTfPlanPaths == "" { + if options.BulkTfPaths == "" { contextLogger.Info("Executing terraform plan") err := options.Plan() @@ -96,7 +104,7 @@ func addCommonFlags(cmd *cobra.Command, o *terraform.Commander) { cmd.PersistentFlags().StringVarP(&o.Workspace, "workspace", "w", "default", "Default workspace where terraform is going to be executed") cmd.PersistentFlags().BoolVarP(&o.DisplayTfOutput, "display-tf-output", "d", true, "Display or not terraform plan output") cmd.PersistentFlags().StringVarP(&o.VarFile, "var-file", "v", "", "tfvar to be used by terraform") - cmd.PersistentFlags().StringVar(&o.BulkTfPlanPaths, "dirs-file", "", "Required for bulk-plans, file path which holds directories where terraform plan is going to be executed") + cmd.PersistentFlags().StringVar(&o.BulkTfPaths, "dirs-file", "", "Required for bulk-plans, file path which holds directories where terraform plan is going to be executed") cmd.MarkPersistentFlagRequired("aws-access-key-id") cmd.MarkPersistentFlagRequired("aws-secret-access-key") diff --git a/pkg/terraform/terraform.go b/pkg/terraform/terraform.go index 1ab58bdf..09d79575 100644 --- a/pkg/terraform/terraform.go +++ b/pkg/terraform/terraform.go @@ -27,7 +27,7 @@ type Commander struct { Workspace string VarFile string DisplayTfOutput bool - BulkTfPlanPaths string + BulkTfPaths string } // Terraform creates terraform command to be executed @@ -196,7 +196,7 @@ func (s *Commander) Apply() error { } if s.DisplayTfOutput { - fmt.Println(output.Stdout) + output.redacted() } if output.ExitCode == 0 { @@ -273,7 +273,7 @@ func (c *Commander) workspaces() ([]string, error) { // BulkPlan executes plan against all directories that changed in the PR. func (c *Commander) BulkPlan() error { - dirs, err := targetDirs(c.BulkTfPlanPaths) + dirs, err := targetDirs(c.BulkTfPaths) if err != nil { return err } @@ -326,3 +326,59 @@ func (c *Commander) BulkPlan() error { return nil } + +// BulkApply executes teraform apply against all directories that changed in the PR. +func (c *Commander) BulkApply() error { + dirs, err := targetDirs(c.BulkTfPaths) + if err != nil { + return err + } + + for _, dir := range dirs { + fmt.Printf("\n") + fmt.Println("#########################################################################") + fmt.Printf("APPLY FOR DIRECTORY: %v\n", dir) + fmt.Println("#########################################################################") + fmt.Printf("\n") + c.cmdDir = dir + err := c.Init(false) + if err != nil { + return err + } + + ws, err := c.workspaces() + if err != nil { + return err + } + + if contains(ws, " live-1") { + log.WithFields(log.Fields{"dir": dir}).Info("Using live-1 context with: KUBE_CTX=live-1.cloud-platform.service.justice.gov.uk") + + c.cmdEnv = append(os.Environ(), "KUBE_CTX=live-1.cloud-platform.service.justice.gov.uk") + c.Workspace = "live-1" + + err := c.Apply() + if err != nil { + return err + } + } else if contains(ws, " manager") { + log.WithFields(log.Fields{"dir": dir}).Info("Using manager context with: KUBE_CTX=manager.cloud-platform.service.justice.gov.uk") + + c.cmdEnv = append(os.Environ(), "KUBE_CTX=manager.cloud-platform.service.justice.gov.uk") + c.Workspace = "manager" + + err := c.Apply() + if err != nil { + return err + } + } else { + log.WithFields(log.Fields{"dir": dir}).Info("No context, normal terraform plan") + err := c.Apply() + if err != nil { + return err + } + } + } + + return nil +}