Skip to content

Commit

Permalink
Merge pull request #58 from ministryofjustice/bulk-apply
Browse files Browse the repository at this point in the history
Added Bulk apply capability
  • Loading branch information
Alejandro Garrido Mota authored Nov 9, 2020
2 parents 76f4158 + 4ccb200 commit 2bf4aa4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
20 changes: 14 additions & 6 deletions pkg/commands/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
},
}
Expand All @@ -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()

Expand Down Expand Up @@ -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")
Expand Down
62 changes: 59 additions & 3 deletions pkg/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Commander struct {
Workspace string
VarFile string
DisplayTfOutput bool
BulkTfPlanPaths string
BulkTfPaths string
}

// Terraform creates terraform command to be executed
Expand Down Expand Up @@ -196,7 +196,7 @@ func (s *Commander) Apply() error {
}

if s.DisplayTfOutput {
fmt.Println(output.Stdout)
output.redacted()
}

if output.ExitCode == 0 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 2bf4aa4

Please sign in to comment.