-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added hook support and installation instructions
- Loading branch information
1 parent
ea4ee56
commit 3fcf929
Showing
12 changed files
with
392 additions
and
47 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
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 |
---|---|---|
@@ -1,14 +1,36 @@ | ||
# vim: set ft=hcl : | ||
service "my-service" { | ||
# Use a semaphore to update one machine at a time | ||
sequentialUpdates = true | ||
checkInterval = "1s" | ||
|
||
preCommand = "echo hello world" | ||
postCommand = "echo hello world" | ||
|
||
# Check for updates every 10s | ||
checkInterval = "10s" | ||
|
||
# Run this command before update starts | ||
before "script" { | ||
command = "initctl my-service stop" | ||
} | ||
|
||
# Artifact defines what repository to use (rackspace) and where | ||
# your artifact live on that repository | ||
artifact "rackspace" { | ||
bucket = "my-service" | ||
path = "final/blue/my-service.tar.gz" | ||
destination = "./usr/bin" | ||
bucket = "my-container" | ||
path = "my-service.tar.gz" | ||
destination = "./test/dest" | ||
} | ||
|
||
# After successful update send an event to graphite | ||
# this allows you to show deploy annotations in tools like grafana | ||
after "graphite-event" { | ||
host = "http://my-graphite-server" | ||
tags = "my-service deployment" | ||
what = "deployed to {{.Hostname}}" | ||
data = "{{.Hash}}" | ||
} | ||
|
||
# Run this command after the update finishes | ||
after "script" { | ||
command = "initctl my-service start" | ||
} | ||
} | ||
|
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,39 @@ | ||
package hook | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ChrisMcKenzie/dropship/service" | ||
"github.com/hashicorp/consul/api" | ||
) | ||
|
||
type ConsulEventHook struct{} | ||
|
||
func (h ConsulEventHook) Execute(config map[string]interface{}, service service.Config) error { | ||
client, err := api.NewClient(api.DefaultConfig()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
payload := map[string]string{ | ||
"hash": service.Hash, | ||
} | ||
|
||
plBytes, err := json.Marshal(payload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id, meta, err := client.Event().Fire(&api.UserEvent{ | ||
Name: config["name"].(string), | ||
Payload: plBytes, | ||
ServiceFilter: config["service"].(string), | ||
TagFilter: config["tag"].(string), | ||
NodeFilter: config["node"].(string), | ||
}, nil) | ||
|
||
fmt.Println(id, meta, err) | ||
|
||
return err | ||
} |
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,18 @@ | ||
package hook | ||
|
||
import "testing" | ||
|
||
func TestConsulEventHook(t *testing.T) { | ||
hook := ConsulEventHook{} | ||
|
||
err := hook.Execute(map[string]string{ | ||
"name": "graphite", | ||
"tag": "blue", | ||
"service": "data-service-api-v4", | ||
"node": "api2.data-service-v4.iad", | ||
}) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
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,73 @@ | ||
package hook | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/ChrisMcKenzie/dropship/service" | ||
) | ||
|
||
type GraphiteEventHook struct{} | ||
|
||
func (h GraphiteEventHook) Execute(config map[string]interface{}, service service.Config) error { | ||
host := config["host"].(string) | ||
delete(config, "host") | ||
|
||
config["when"] = time.Now().Unix() | ||
|
||
what, err := parseTemplate(config["what"].(string), service) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
config["what"] = what | ||
|
||
data, err := parseTemplate(config["what"].(string), service) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
config["data"] = data | ||
|
||
config["tags"] = config["tags"].(string) + service.Name | ||
|
||
body, err := json.Marshal(config) | ||
if err != nil { | ||
return fmt.Errorf("Graphite Hook: %s", err) | ||
} | ||
|
||
resp, err := http.Post(host+"/events/", "application/json", bytes.NewReader(body)) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Graphite Hook: %s", err) | ||
} | ||
|
||
if resp.StatusCode >= 400 { | ||
return fmt.Errorf("Graphite Hook: unable to post to events. responded with %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseTemplate(temp string, service service.Config) (string, error) { | ||
tmpl, err := template.New("data").Parse(temp) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
hostname, err := os.Hostname() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
data := TemplateData{service, hostname} | ||
|
||
var buf bytes.Buffer | ||
err = tmpl.Execute(&buf, data) | ||
return buf.String(), err | ||
} |
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,18 @@ | ||
package hook | ||
|
||
import "testing" | ||
|
||
func TestGraphiteEventHook(t *testing.T) { | ||
var hook GraphiteEventHook | ||
|
||
err := hook.Execute(map[string]string{ | ||
"host": "http://graphite2.analytics.iad", | ||
"what": "deployed by dropship", | ||
"tags": "data-service deployment", | ||
"data": "dropship is awesome!", | ||
}) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
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 hook | ||
|
||
import "github.com/ChrisMcKenzie/dropship/service" | ||
|
||
type TemplateData struct { | ||
service.Config | ||
Hostname string | ||
} | ||
|
||
type Hook interface { | ||
Execute(config map[string]interface{}, service service.Config) error | ||
} | ||
|
||
func GetHookByName(name string) Hook { | ||
switch name { | ||
case "script": | ||
return ScriptHook{} | ||
case "consul-event": | ||
return ConsulEventHook{} | ||
case "graphite-event": | ||
return GraphiteEventHook{} | ||
} | ||
|
||
return nil | ||
} |
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,21 @@ | ||
package hook | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/ChrisMcKenzie/dropship/service" | ||
) | ||
|
||
type ScriptHook struct{} | ||
|
||
func (h ScriptHook) Execute(config map[string]interface{}, service service.Config) error { | ||
_, err := executeCommand(config["command"].(string)) | ||
return err | ||
} | ||
|
||
func executeCommand(c string) (string, error) { | ||
cmd := strings.Fields(c) | ||
out, err := exec.Command(cmd[0], cmd[1:]...).Output() | ||
return string(out), err | ||
} |
Oops, something went wrong.