Skip to content

Commit

Permalink
Merge pull request #22 from leapfrogtechnology/dev
Browse files Browse the repository at this point in the history
Initial release
  • Loading branch information
pratishshr committed Oct 25, 2019
2 parents d37d585 + 6bebf94 commit 31f4e3b
Show file tree
Hide file tree
Showing 50 changed files with 769 additions and 948 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

# Built Go Packages
shift
shift-infrastructure
deploy

# Configfiles
config.json
shift.json

# artifacts
artifact
36 changes: 23 additions & 13 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/urfave/cli"
Expand All @@ -23,15 +22,6 @@ func Initialize(info *Info) error {
app.Usage = info.Description

app.Commands = []cli.Command{
{
Name: "infrastructure",
Description: "Initialize",
Aliases: nil,
Usage: "Initialize your Application",
Action: func(c *cli.Context) {
fmt.Println("Shift Shift shift")
},
},
cli.Command{
Name: "setup",
Action: func(ctx *cli.Context) error {
Expand All @@ -43,14 +33,34 @@ func Initialize(info *Info) error {
cli.Command{
Name: "deploy",
Action: func(ctx *cli.Context) error {
project := ctx.Args().Get(0)
deployment := ctx.Args().Get(1)
environment := ctx.Args().Get(0)
Deploy(environment)

Deploy(project, deployment)
return nil
},
},
cli.Command{
Name: "destroy",
Action: func(ctx *cli.Context) error {
environment := ctx.Args().Get(0)
Destroy(environment)

return nil
},
},
cli.Command{
Name: "add",
Subcommands: []cli.Command{
{
Name: "env",
Action: func(ctx *cli.Context) error {
AddEnv()

return nil
},
},
},
},
}

return app.Run(os.Args)
Expand Down
10 changes: 6 additions & 4 deletions cli/cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

// "github.com/leapfrogtechnology/shift/cli/internal/deploy"
import (
"github.com/leapfrogtechnology/shift/cli/internals/deploy"
)

// Deploy triggers deployment for provided project.
func Deploy(project string, deployment string) {
// deploy.Run(project, deployment)
// Deploy triggers deployment for the given environment.
func Deploy(environment string) {
deploy.Run(environment)
}
8 changes: 8 additions & 0 deletions cli/cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cmd

import "github.com/leapfrogtechnology/shift/cli/internals/destroy"

// Destroy deletes existing infrastructure
func Destroy(environment string) {
destroy.Run(environment)
}
10 changes: 10 additions & 0 deletions cli/cmd/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cmd

import (
"github.com/leapfrogtechnology/shift/cli/internals/env"
)

// AddEnv adds a new environment to the project.
func AddEnv() {
env.Run()
}
2 changes: 1 addition & 1 deletion cli/cmd/setup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/leapfrogtechnology/shift/cli/internal/setup"
"github.com/leapfrogtechnology/shift/cli/internals/setup"
)

// Setup prompts user for required information for creating a project.
Expand Down
36 changes: 36 additions & 0 deletions cli/internals/deploy/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package deploy

import (
"errors"
"fmt"
"strings"

"github.com/leapfrogtechnology/shift/core/services/slack"
"github.com/leapfrogtechnology/shift/core/services/storage"
"github.com/leapfrogtechnology/shift/core/utils/system"
"github.com/leapfrogtechnology/shift/core/utils/system/exit"

"github.com/leapfrogtechnology/shift/deployment/internals/frontend"
)

// Run starts deployment for the given environment
func Run(environment string) {
project := storage.Read()

_, ok := project.Env[environment]

if !ok {
exit.Error(errors.New("Unknown deployment type "+"'"+environment+"'"), "Error")
}

slack.Notify(project.SlackURL,
fmt.Sprintf("*There is a new deployment in progress.* \n Project: `%s` \n Environment: `%s` \n Started by: `%s`",
project.Name, environment, system.CurrentUser()),
"#1CA7FB")

if strings.EqualFold(project.Type, "frontend") {
frontend.Deploy(project, environment)
}

slack.Notify(project.SlackURL, fmt.Sprintf("*%s* succesfully deployed to *%s*. 🎉 🎉 🎉", project.Name, environment), "#04EBB8")
}
55 changes: 55 additions & 0 deletions cli/internals/destroy/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 askConfirmation(environment, projectName string) string {
confirmation := ""
prompt := &survey.Input{
Message: "Are you sure you want to destroy " + environment + " environment from " + projectName + " ?(Y/N): ",
}
survey.AskOne(prompt, &confirmation)

return confirmation
}

// Run initializes destruction of infrastructure
func Run(environment string) {
project := storage.Read()
_, env := project.Env[environment]

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

confirmation := askConfirmation(environment, project.Name)

if strings.EqualFold(confirmation, "Y") || strings.EqualFold(confirmation, "yes") {

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

exists := file.Exists(terraformFile)

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

} else {
const message = "Operation aborted"
exit.Error(errors.New(message), "Cancelled")
}
}
41 changes: 41 additions & 0 deletions cli/internals/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package env

import (
"github.com/AlecAivazis/survey/v2"

"github.com/leapfrogtechnology/shift/cli/internals/deploy"

"github.com/leapfrogtechnology/shift/core/services/storage"
"github.com/leapfrogtechnology/shift/core/structs"
"github.com/leapfrogtechnology/shift/core/utils/logger"
"github.com/leapfrogtechnology/shift/infrastructure/internals/initialize"
)

func askEnvironmentName() string {
environment := ""
prompt := &survey.Input{
Message: "Environment Name (eg: dev): ",
}
survey.AskOne(prompt, &environment)

return environment
}

// Run initializes new environment.
func Run() {
project := storage.Read()
environment := askEnvironmentName()

infraInfo := initialize.Run(project, environment)

project.Env[environment] = structs.Env{
Bucket: infraInfo.Bucket,
}

storage.Save(project)

deploy.Run(environment)

logger.Info("Project Deployed At: " + infraInfo.URL)
logger.Info("⏳ Stay put. It might take some time for the changes to be reflected. ⏳")
}
Loading

0 comments on commit 31f4e3b

Please sign in to comment.