Skip to content

Commit

Permalink
Merge pull request #1 from leapfrogtechnology/trigger
Browse files Browse the repository at this point in the history
Trigger Deployment
  • Loading branch information
pratishshr committed Sep 10, 2019
2 parents 93aa90f + d413f7a commit 0f8c99a
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 93 deletions.
14 changes: 13 additions & 1 deletion cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package cmd

import (
"fmt"
"github.com/urfave/cli"
"os"

"github.com/urfave/cli"
)

// Info defines the basic information required for the CLI.
Expand Down Expand Up @@ -36,6 +37,17 @@ func Initialize(info *Info) error {
Action: func(ctx *cli.Context) error {
Setup()

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

Deploy(project, deployment)

return nil
},
},
Expand Down
10 changes: 10 additions & 0 deletions cli/cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cmd

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

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

import (
"encoding/json"

"github.com/leapfrogtechnology/shift/cli/services/mq/trigger"
)

// TriggerRequest defined the structure for Trigger
type TriggerRequest struct {
Project string `json:"project"`
Deployment string `json:"deployment"`
}

// Run triggers the deployment.
func Run(project string, deployment string) {
triggerRequest := TriggerRequest{
Project: project,
Deployment: deployment,
}

triggerRequestJSON, _ := json.Marshal(triggerRequest)

trigger.Publish(triggerRequestJSON)
}
4 changes: 2 additions & 2 deletions cli/internals/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/leapfrogtechnology/shift/cli/services/mq"
"github.com/leapfrogtechnology/shift/cli/services/mq/infrastructure"
"github.com/leapfrogtechnology/shift/cli/utils/github"
"github.com/leapfrogtechnology/shift/cli/utils/spinner"
)
Expand Down Expand Up @@ -256,5 +256,5 @@ func Run() {

fmt.Println(string(projectRequestJSON))

mq.Publish(projectRequestJSON)
infrastructure.Publish(projectRequestJSON)
}
71 changes: 71 additions & 0 deletions cli/services/mq/infrastructure/infrastructure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package infrastructure

import (
"fmt"

"github.com/leapfrogtechnology/shift/cli/services/mq"
"github.com/streadway/amqp"
)

func failOnError(err error, msg string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err))
}
}

func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) {
conn := mq.GetConnection()

ch, err := conn.Channel()

failOnError(err, "Failed to open a channel")

q, err := ch.QueueDeclare("Infrastructure",
false, // durable bool
false, // autoDelete bool
false, // exclusive bool
false, // noWait bool
nil, // args amqp.Table
)

failOnError(err, "Failed to declare a queue")

return conn, ch, &q
}

// Publish message to queue
func Publish(message []byte) {
_, ch, q := getQueue()

defer ch.Close()

msg := amqp.Publishing{
ContentType: "application/json",
Body: message,
}

ch.Publish("", q.Name, false, false, msg)
}

// Consume listens to message from queue
func Consume(deploy func([]byte)) {
_, ch, q := getQueue()

defer ch.Close()

msgs, err := ch.Consume(
q.Name, // queue string
"", // consumer string
true, // autoAck bool
false, // exclusive bool
false, // noLocal bool
false, // noWait bool
nil, //args amqp.Table
)

failOnError(err, "Failed to register a consumer")

for msg := range msgs {
deploy(msg.Body)
}
}
34 changes: 3 additions & 31 deletions cli/services/mq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,11 @@ func failOnError(err error, msg string) {
}
}

func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) {
// GetConnection returns the connection to rabbitmq
func GetConnection() *amqp.Connection {
conn, err := amqp.Dial("amqp://shift:shiftdeveloper@dev-shiftmq.lftechnology.com:5672")

failOnError(err, "Failed to connect to RabbitMQ")

ch, err := conn.Channel()

failOnError(err, "Failed to open a channel")

q, err := ch.QueueDeclare("Infrastructure",
false, //durable bool
false, //autoDelete bool
false, //exclusive bool
false, //noWait bool
nil, // args amqp.Table
)

failOnError(err, "Faied to declare a queue")

return conn, ch, &q
}

// Publish message to queue
func Publish(message []byte) {
conn, ch, q := getQueue()

defer conn.Close()
defer ch.Close()

msg := amqp.Publishing{
ContentType: "application/json",
Body: message,
}

ch.Publish("", q.Name, false, false, msg)
return conn
}
71 changes: 71 additions & 0 deletions cli/services/mq/trigger/trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package trigger

import (
"fmt"

"github.com/leapfrogtechnology/shift/cli/services/mq"
"github.com/streadway/amqp"
)

func failOnError(err error, msg string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err))
}
}

func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) {
conn := mq.GetConnection()

ch, err := conn.Channel()

failOnError(err, "Failed to open a channel")

q, err := ch.QueueDeclare("Trigger",
false, // durable bool
false, // autoDelete bool
false, // exclusive bool
false, // noWait bool
nil, // args amqp.Table
)

failOnError(err, "Failed to declare a queue")

return conn, ch, &q
}

// Publish message to queue
func Publish(message []byte) {
_, ch, q := getQueue()

defer ch.Close()

msg := amqp.Publishing{
ContentType: "application/json",
Body: message,
}

ch.Publish("", q.Name, false, false, msg)
}

// Consume listens to message from queue
func Consume(deploy func([]byte)) {
_, ch, q := getQueue()

defer ch.Close()

msgs, err := ch.Consume(
q.Name, // queue string
"", // consumer string
true, // autoAck bool
false, // exclusive bool
false, // noLocal bool
false, // noWait bool
nil, //args amqp.Table
)

failOnError(err, "Failed to register a consumer")

for msg := range msgs {
deploy(msg.Body)
}
}
Binary file added deployment/deploy
Binary file not shown.
24 changes: 22 additions & 2 deletions deployment/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"encoding/json"
"fmt"

"github.com/leapfrogtechnology/shift/deployment/domain/project"
"github.com/leapfrogtechnology/shift/deployment/internals/frontend"
"github.com/leapfrogtechnology/shift/deployment/services/aws/s3"
"github.com/leapfrogtechnology/shift/deployment/services/mq"
"github.com/leapfrogtechnology/shift/deployment/services/mq/deployment"
"github.com/leapfrogtechnology/shift/deployment/services/mq/trigger"
"github.com/leapfrogtechnology/shift/deployment/services/storage"
)

Expand Down Expand Up @@ -37,6 +39,24 @@ func deploy(msg []byte) {
storage.Save(projectResponse)
}

func triggerDeploy(msg []byte) {
triggerRequest := project.TriggerRequest{}
json.Unmarshal(msg, &triggerRequest)

jsonData := storage.Read()

deploymentData := jsonData[triggerRequest.Project][triggerRequest.Deployment]

if _, ok := jsonData[triggerRequest.Project][triggerRequest.Deployment]; ok {
deploymentDataJSON, _ := json.Marshal(deploymentData)

deployment.Publish(deploymentDataJSON)
} else {
fmt.Println("Deployment " + triggerRequest.Deployment + " for Project " + triggerRequest.Project + " not found")
}
}

func main() {
mq.Consume(deploy)
go trigger.Consume(triggerDeploy)
deployment.Consume(deploy)
}
6 changes: 6 additions & 0 deletions deployment/domain/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ type Response struct {
Deployment deployment `json:"deployment"`
Data infrastructure `json:"data"`
}

// TriggerRequest defines the requst to trigger a deployment.
type TriggerRequest struct {
Project string `json:"project"`
Deployment string `json:"deployment"`
}
1 change: 1 addition & 0 deletions deployment/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
71 changes: 71 additions & 0 deletions deployment/services/mq/deployment/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package deployment

import (
"fmt"

"github.com/leapfrogtechnology/shift/deployment/services/mq"
"github.com/streadway/amqp"
)

func failOnError(err error, msg string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err))
}
}

func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) {
conn := mq.GetConnection()

ch, err := conn.Channel()

failOnError(err, "Failed to open a channel")

q, err := ch.QueueDeclare("Deployment",
false, // durable bool
false, // autoDelete bool
false, // exclusive bool
false, // noWait bool
nil, // args amqp.Table
)

failOnError(err, "Failed to declare a queue")

return conn, ch, &q
}

// Publish message to queue
func Publish(message []byte) {
_, ch, q := getQueue()

defer ch.Close()

msg := amqp.Publishing{
ContentType: "application/json",
Body: message,
}

ch.Publish("", q.Name, false, false, msg)
}

// Consume listens to message from queue
func Consume(deploy func([]byte)) {
_, ch, q := getQueue()

defer ch.Close()

msgs, err := ch.Consume(
q.Name, // queue string
"", // consumer string
true, // autoAck bool
false, // exclusive bool
false, // noLocal bool
false, // noWait bool
nil, //args amqp.Table
)

failOnError(err, "Failed to register a consumer")

for msg := range msgs {
deploy(msg.Body)
}
}
Loading

0 comments on commit 0f8c99a

Please sign in to comment.