Skip to content

Commit

Permalink
Change infrastructure code for frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
pratishshr committed Oct 21, 2019
1 parent e6ca4c6 commit 9bf916c
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 285 deletions.
2 changes: 2 additions & 0 deletions cli/internals/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/leapfrogtechnology/shift/core/platforms/aws"
"github.com/leapfrogtechnology/shift/core/services/storage"
"github.com/leapfrogtechnology/shift/core/structs"
"github.com/leapfrogtechnology/shift/infrastructure/internals/initialize"
)

type projectDetails struct {
Expand Down Expand Up @@ -200,6 +201,7 @@ func Run() {
storage.Save(projectRequest)

// 2. Run infrastructre code and save to JSON again with updated information.
initialize.Run(projectRequest, deploymentDetails.Environment)

// 3. Deploy to created infrastructure.
}
2 changes: 1 addition & 1 deletion core/platforms/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"
"strings"

"github.com/leapfrogtechnology/shift/utils/system/exit"
"github.com/leapfrogtechnology/shift/core/utils/system/exit"
)

func handle(err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import (
func Error(err interface{}) {
fmt.Println("Error")
fmt.Println(err)

os.Exit(1)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ go 1.12

require (
github.com/AlecAivazis/survey/v2 v2.0.4
github.com/briandowns/spinner v1.6.1
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4
github.com/go-resty/resty/v2 v2.0.0
github.com/leapfrogtechnology/shift/deployment v0.0.0-20191002072505-b6271e5a3950
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 h1:GY1+t5Dr9OKADM64SYnQjw/w99HMYvQ0A8/JoUkxVmc=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA=
github.com/gabriel-vasile/mimetype v0.3.18/go.mod h1:kMJbg3SlWZCsj4R73F1WDzbT9AyGCOVmUtIxxwO5pmI=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-resty/resty/v2 v2.0.0 h1:9Nq/U+V4xsoDnDa/iTrABDWUCuk3Ne92XFHPe6dKWUc=
github.com/go-resty/resty/v2 v2.0.0/go.mod h1:dZGr0i9PLlaaTD4H/hoZIDjQ+r6xq8mgbRzHZf7f2J8=
github.com/h2non/filetype v1.0.10/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 h1:rhqTjzJlm7EbkELJDKMTU7udov+Se0xZkWmugr6zGok=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
github.com/juju/loggo v0.0.0-20180524022052-584905176618/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leapfrogtechnology/shift/deployment v0.0.0-20191002072505-b6271e5a3950 h1:RYjt5BiZYvgONzLF4CQqLLXwicMC33rTbT8YM164gJw=
github.com/leapfrogtechnology/shift/deployment v0.0.0-20191002072505-b6271e5a3950/go.mod h1:FQKoH5MsRprr0o/ANivppPjVXkRD3JqUacK9BNHuGYM=
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b h1:PMbSa9CgaiQR9NLlUTwKi+7aeLl3GG5JX5ERJxfQ3IE=
Expand All @@ -29,6 +39,7 @@ github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -52,5 +63,7 @@ golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20181221001348-537d06c36207/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
25 changes: 0 additions & 25 deletions infrastructure/.gitignore

This file was deleted.

118 changes: 0 additions & 118 deletions infrastructure/infrastructure/infrastrucutre.go

This file was deleted.

18 changes: 0 additions & 18 deletions infrastructure/internals/initialize.go

This file was deleted.

145 changes: 145 additions & 0 deletions infrastructure/internals/initialize/initialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package initialize

import (
"encoding/json"
"errors"
"fmt"

"strings"

"path/filepath"

"github.com/leapfrogtechnology/shift/core/utils/logger"
"github.com/leapfrogtechnology/shift/infrastructure/utils"

"github.com/leapfrogtechnology/shift/core/structs"
"github.com/leapfrogtechnology/shift/infrastructure/internals/terraform"
"github.com/leapfrogtechnology/shift/infrastructure/internals/terraform/templates/providers/aws/template"
// backendHaArchitecture "github.com/leapfrogtechnology/shift/infrastructure/internals/terraform/templates/providers/aws/backend-ha-architecture"
)

type terraformOutput struct {
Sensitive bool `json:"sensitive"`
Type string `json:"type"`
Value string `json:"value"`
}

type frontendTerraformOutput struct {
BucketName terraformOutput `json:"bucketName"`
FrontendWebURL terraformOutput `json:"appUrl"`
}

// // CreateBackend creates infrastructure for the backend.
// func CreateBackend(project structs.Project) (string, error) {
// workspaceRoot := "/tmp"
// var clientArgs utils.Client
// var backendTerraformOutput utils.BackendTerraformOutput
// logger.LogInfo("Gathering Info")

// err := json.Unmarshal(ClientArgs, &clientArgs)
// if err != nil {
// logger.LogError(err, "Error Parsing Body")
// return "", err
// }
// workspaceDir := filepath.Join(workspaceRoot, clientArgs.Project, clientArgs.Deployment.Name, clientArgs.Deployment.Type)
// logger.LogInfo("Generating Template")
// err = utils.GenerateFrontendTemplateFile(backendHaArchitecture.InfrastructureTemplate, clientArgs, workspaceDir)
// containerTemplateFile := workspaceDir + "/sample.json.tpl"
// _ = ioutil.WriteFile(containerTemplateFile, []byte(backendHaArchitecture.ContainerTemplate), 0600)
// if err != nil {
// logger.LogError(err, "Cannot Generate Template")
// return "", err
// }
// logger.LogInfo("Running Infrastructure Changes")
// terraformOutput, err := terraform.RunInfrastructureChanges(workspaceDir)
// if err != nil {
// logger.LogError(err, "Cannot Run Changes")
// return "", err
// }
// _ = json.Unmarshal([]byte(terraformOutput), &backendTerraformOutput)
// result := utils.BackendResult{
// Project: clientArgs.Project,
// Deployment: clientArgs.Deployment,
// Data: backendTerraformOutput,
// }
// out, err := json.Marshal(result)
// if err != nil {
// logger.LogError(err, "Error Marshalling output")
// return "", err
// }
// logger.LogOutput(string(out))

// return string(out), err
// }

// Run intitializes the infrastructure in the specified cloud provider.
func Run(project structs.Project, environment string) string {
if !utils.CommandExists("terraform") {
logger.FailOnError(errors.New("terraform does not exist"), "Please install terraform on your device")
}

infrastructureInfo := Initialize(project, environment)

logger.LogOutput(infrastructureInfo)

return infrastructureInfo
}

// Initialize creates infrastructure for frontend and backend according the given input.
func Initialize(project structs.Project, environment string) string {
if strings.EqualFold(project.Type, "frontend") {
out := CreateFrontend(project, environment)

return out
}
// else if strings.EqualFold(project.Type, "backend") {
// // out, err := CreateBackend(project)

// return out
// }

logger.FailOnError(errors.New("Unknown Deployment Type"), project.Type)

return ""
}

// CreateFrontend creates infrastructure for frontend.
func CreateFrontend(project structs.Project, environment string) string {
workspaceRoot := "/tmp"

terraformOutput := frontendTerraformOutput{}

workspaceDir := filepath.Join(workspaceRoot, project.Name, project.Type, environment)

logger.LogInfo("Generating Template")
template.GenerateFrontendTemplate(project, workspaceDir, environment)

logger.LogInfo("Running Infrastructure Changes")
workspaceName := project.Name + "-" + project.Type + "-" + environment
output, err := terraform.RunInfrastructureChanges(workspaceDir, workspaceName)

logger.FailOnError(err, "Cannot run changes")

_ = json.Unmarshal([]byte(output), &terraformOutput)

fmt.Println(terraformOutput)

// result := utils.FrontendResult{
// Project: clientArgs.Project,
// Deployment: clientArgs.Deployment,
// Data: frontendTerraformOutput,
// }

// out, err := json.Marshal(result)

// if err != nil {
// logger.LogError(err, "Error Marshalling output")
// return "", err
// }

// logger.LogOutput(string(out))

// return string(out), err

return ""
}
Loading

0 comments on commit 9bf916c

Please sign in to comment.