Skip to content

Commit

Permalink
Add conformation while destroying infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
razzkumar committed Oct 24, 2019
1 parent 4638a45 commit 2f300e2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 0 additions & 2 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func Initialize(info *Info) error {
Name: "deploy",
Action: func(ctx *cli.Context) error {
environment := ctx.Args().Get(0)

Deploy(environment)

return nil
Expand All @@ -44,7 +43,6 @@ func Initialize(info *Info) error {
Name: "destroy",
Action: func(ctx *cli.Context) error {
environment := ctx.Args().Get(0)

Destroy(environment)

return nil
Expand Down
36 changes: 27 additions & 9 deletions cli/internals/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,53 @@ package destroy
import (
"errors"
"path/filepath"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/leapfrogtechnology/shift/core/services/storage"
"github.com/leapfrogtechnology/shift/core/utils/file"
"github.com/leapfrogtechnology/shift/core/utils/system/exit"
"github.com/leapfrogtechnology/shift/infrastructure/internals/terraform"
)

func askConformation(environment, projectName string) string {
conformation := ""
prompt := &survey.Input{
Message: "Are you sure you want to destroy " + environment + " environment from " + projectName + " ?(Y/N): ",
}
survey.AskOne(prompt, &conformation)

return conformation
}

// Run initializes destruction of infrastructure
func Run(environment string) {

project := storage.Read()
_, env := project.Env[environment]

if !env {
exit.Error(errors.New("Unknown Environment type "+"'"+environment+"'"), "Error")
const message = "Unknown Environment type "
exit.Error(errors.New(message+"'"+environment+"'"), "Error")
}

workspaceRoot := "/tmp"
conformation := askConformation(environment, project.Name)

workspaceDir := filepath.Join(workspaceRoot, project.Name, project.Type, environment)
if strings.EqualFold(conformation, "Y") || strings.EqualFold(conformation, "yes") {

terraformFile := workspaceDir + "/infrastructure.tf"
workspaceRoot := "/tmp"
workspaceDir := filepath.Join(workspaceRoot, project.Name, project.Type, environment)
terraformFile := workspaceDir + "/infrastructure.tf"

isExist := file.Exists(terraformFile)
exists := file.Exists(terraformFile)

if isExist {
terraform.DestroyInfrastructure(workspaceDir)
if exists {
terraform.DestroyInfrastructure(workspaceDir)
} else {
terraform.MakeTempAndDestroy(project, environment, workspaceDir)
}

} else {
terraform.MakeTempAndDestroy(project, environment, workspaceDir)
const message = "Denied by user"
exit.Error(errors.New(message), "Cancelled")
}
}
22 changes: 11 additions & 11 deletions infrastructure/internals/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package terraform
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"

Expand All @@ -28,16 +27,16 @@ func initTerraform(workspaceDir string) error {

err := cmd.Run()
if err != nil {
fmt.Println(err)
logger.LogError(err, "")
spinner.Stop()
return err
}
spinner.Stop()

return nil
}

func applyTerraform(workspaceDir string) error {

logger.LogInfo(" Applying Changes")
spinner.Start(" ")

Expand All @@ -54,25 +53,28 @@ func applyTerraform(workspaceDir string) error {
return err
}
spinner.Stop()

return nil
}

func getTerraformOutput(workspaceDir string) (string, error) {

logger.LogInfo(" Generating Output")
spinner.Start(" ")

cmd := exec.Command("terraform", "output", "-json")
cmd.Dir = workspaceDir
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()

if err != nil {
logger.LogError(err, stderr.String())
spinner.Stop()
return "", err
}

spinner.Stop()

return stdout.String(), err
Expand All @@ -85,7 +87,6 @@ func RunInfrastructureChanges(workspaceDir string, workspaceName string) (string

// Set local execution instead of remote.
terraform.ActivateLocalRun(workspaceName)

if err != nil {
logger.LogError(err, "Couldnot initialize")

Expand All @@ -94,15 +95,13 @@ func RunInfrastructureChanges(workspaceDir string, workspaceName string) (string

logger.LogInfo("Applying")
err = applyTerraform(workspaceDir)

if err != nil {
logger.LogError(err, "Failed to apply changes")

return "", err
}

out, err := getTerraformOutput(workspaceDir)

if err != nil {
logger.LogError(err, "Failed to get terraform output")

Expand All @@ -122,28 +121,29 @@ func DestroyInfrastructure(workspaceDir string) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
message := " ERROR While Destroying Infrastructure"

const message = " ERROR While Destroying Infrastructure"
if err != nil {
logger.LogError(err, message)
spinner.Stop()
return errors.New(err.Error() + message)
}

spinner.Stop()
return nil

return nil
}

// MakeTempAndDestroy create infrastructure template and distroy the infrastructure
func MakeTempAndDestroy(project structs.Project, environment, workspaceDir string) error {

logger.LogInfo("Generating Templates....")

if project.Type == "Frontend" {
template.GenerateFrontendTemplate(project, workspaceDir, environment)
} else {
// TODO backend
}
initTerraform(workspaceDir)
err := DestroyInfrastructure(workspaceDir)

return err
}

0 comments on commit 2f300e2

Please sign in to comment.