Skip to content

Commit

Permalink
WIP - Terrafrom Service RabbitMQ integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sparshadotel committed Aug 29, 2019
1 parent 74d2dfa commit f810f65
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 55 deletions.
16 changes: 4 additions & 12 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/leapfrogtechnology/shift/infrastructure"
"fmt"
"os"

"github.com/urfave/cli"
Expand All @@ -24,20 +24,12 @@ func Initialize(info *Info) error {

app.Commands = []cli.Command{
{
Name: "init",
Name: "infrastructure",
Description: "Initialize",
Aliases: nil,
Usage: "Initialize your Application",
Subcommands: []cli.Command{
{
Name: "frontend",
Aliases: nil,
Usage: "Initialize your frontend infrastructure",
Description: "Use this to initialize your frontend Infrastructure",
Action: func(c *cli.Context) {
infrastructure.InitializeFrontend()
},
},
Action: func(c *cli.Context) {
fmt.Println("Shift Shift shift")
},
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package infrastructure
package infrastrucuture

import (
"encoding/json"
"github.com/leapfrogtechnology/shift/infrastructure/templates/providers/aws/frontend-architecture"
"github.com/leapfrogtechnology/shift/infrastructure/utils"
"io/ioutil"
"os"
"path/filepath"
)

func InitializeFrontend() {
credentialsJsonFile, err := os.Open("config.json")
if err != nil {
panic(err)
}
defer credentialsJsonFile.Close()
byteValue, _ := ioutil.ReadAll(credentialsJsonFile)
func InitializeFrontend(infrastructureArgs string) error{

var frontendArgs utils.FrontendInfrastructureVariables
err = json.Unmarshal(byteValue, &frontendArgs)
err := json.Unmarshal([]byte(infrastructureArgs), &frontendArgs)
if err != nil {
panic(err)
utils.LogError(err, "Error Parsing Body")
return err
}
workspaceDir := filepath.Join("/tmp", frontendArgs.CLIENT_NAME)
utils.GenerateFrontendTemplateFile(frontend_architecture.InfrastructureTemplate, frontendArgs, workspaceDir)

err = utils.GenerateFrontendTemplateFile(frontend_architecture.InfrastructureTemplate, frontendArgs, workspaceDir)
if err != nil {
utils.LogError(err, "Cannot Generate Template")
return err
}
utils.RunInfrastructureChanges(workspaceDir)
return nil
}
7 changes: 7 additions & 0 deletions infrastructure/shift-infrastructure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/leapfrogtechnology/shift/infrastructure/infrastructure"

func main() {
infrastrucuture.InitializeFrontend()
}
13 changes: 13 additions & 0 deletions infrastructure/utils/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import "log"

func FailOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}

func LogError(err error, msg string) {
log.Printf("%s: %s", msg, err)
}
11 changes: 6 additions & 5 deletions infrastructure/utils/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ type FrontendInfrastructureVariables struct {
TERRAFORM_TOKEN string `json:"terraform_token"`
}

func GenerateFrontendTemplateFile(template string, s3Args FrontendInfrastructureVariables, terraformPath string) {
func GenerateFrontendTemplateFile(template string, s3Args FrontendInfrastructureVariables, terraformPath string) error{
tpl, err := pongo2.FromString(template)
if err != nil {
panic(err)
return err
}
out, err := tpl.Execute(pongo2.Context{"info": s3Args})
if err != nil {
panic(err)
return err
}
terraformFileName := terraformPath + "/infrastructure.tf"
err = os.MkdirAll(terraformPath, 0700)
if err != nil {
panic(err)
return err
}
err = ioutil.WriteFile(terraformFileName, []byte(out), 0600)
if err != nil {
panic(err)
return err
}
return nil
}
60 changes: 35 additions & 25 deletions infrastructure/utils/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@ package utils

import (
"bytes"
"fmt"
"github.com/briandowns/spinner"
"github.com/logrusorgru/aurora"
"os/exec"
"time"
)

func initTerraform(workspaceDir string) {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
func initTerraform(workspaceDir string) error {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
s.Prefix = " "
s.Suffix = " Initializing"
_ = s.Color("cyan", "bold")
s.Start()
cmd := exec.Command("terraform", "init")
cmd := exec.Command("terraform", "infrastructure")
cmd.Dir = workspaceDir
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ":" + stderr.String())
return
LogError(err, stderr.String())
return err
}
s.Stop()
return nil
}

func planTerraform(workspaceDir string) {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
func planTerraform(workspaceDir string) error {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
s.Prefix = " "
s.Suffix = " Planning"
_ = s.Color("cyan", "bold")
Expand All @@ -43,14 +42,15 @@ func planTerraform(workspaceDir string) {
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ":" + stderr.String())
return
LogError(err, stderr.String())
return err
}
s.Stop()
return nil
}

func applyTerraform(workspaceDir string) {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
func applyTerraform(workspaceDir string) error {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
s.Prefix = " "
s.Suffix = " Applying Changes"
_ = s.Color("cyan", "bold")
Expand All @@ -63,14 +63,15 @@ func applyTerraform(workspaceDir string) {
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ":" + stderr.String())
return
LogError(err, stderr.String())
return err
}
s.Stop()
return nil
}

func getTerraformOutput(workspaceDir string) {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
func getTerraformOutput(workspaceDir string) (string, error) {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
s.Prefix = " "
s.Suffix = " Generating Output"
_ = s.Color("cyan", "bold")
Expand All @@ -83,16 +84,25 @@ func getTerraformOutput(workspaceDir string) {
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ":" + stderr.String())
return
LogError(err, stderr.String())
return "", err
}
s.Stop()
fmt.Println(aurora.Cyan(stdout.String()))
return stdout.String(), nil
}

func RunInfrastructureChanges(workspaceDir string) {
initTerraform(workspaceDir)
planTerraform(workspaceDir)
applyTerraform(workspaceDir)
getTerraformOutput(workspaceDir)
func RunInfrastructureChanges(workspaceDir string) {
if initTerraform(workspaceDir) != nil {
return
}
if planTerraform(workspaceDir) !=nil {
return
}
if applyTerraform(workspaceDir) != nil {
return
}
if getTerraformOutput(workspaceDir) != nil {
return
}

}

0 comments on commit f810f65

Please sign in to comment.