-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from leapfrogtechnology/trigger
Trigger Deployment
- Loading branch information
Showing
15 changed files
with
371 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.