Skip to content

Commit

Permalink
added hook support and installation instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMcKenzie committed Nov 10, 2015
1 parent ea4ee56 commit 3fcf929
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 47 deletions.
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,89 @@ and will download automatically
- Distributed sequential updates
- Multiple Artifact Repository Support

## Installation

To install on ubuntu do the following:

```
echo "deb http://dl.bintray.com/chrismckenzie/deb trusty main" >> /etc/apt/sources.list
sudo apt-get update
sudo apt-get install dropship
```

## Configuration

To setup dropship you will need to add/update the following files.

First you will need to tell dropship how to connect to your artifact repository
so you will need to uncomment out the desired repo and fill in its options.

_/etc/dropship.d/dropship.hcl_
```hcl
# vim: set ft=hcl :
# Location that service config will be read from
service_path = "/etc/dropship.d/services"
# Rackspace Repo Config
# =====================
rackspace {
user = "<your-rackspace-user>"
key = "<your-rackspace-key>"
region = "<rackspace-region>"
}
```

You will then have to create a file in the services directory of dropship. this
will tell dropship how to check and install you artifact. You can have multiple
`service` definitions in one file or multiple files.

_/etc/dropship.d/services/my-service.hcl_
```hcl
# vim: set ft=hcl :
service "my-service" {
# Use a semaphore to update one machine at a time
sequentialUpdates = true
# 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-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
#
# The graphite hook will automatically add this services name into the
# graphite tags. You also have access to all of the services meta data
# like Name, "current hash", hostname.
after "graphite-event" {
host = "http://<my-graphite-server>"
tags = "deployment"
what = "deployed to {{.Name}} on {{.Hostname}}"
data = "{{.Hash}}"
}
# Run this command after the update finishes
after "script" {
command = "initctl my-service start"
}
}
```

## Roadmap

- [X] Hooks
- [ ] Support for Amazon S3, and FTP
- [ ] Support for different file types deb, rpm, file _(currently only tar.gz)_
- [ ] Reporting system
- [ ] Hooks
- [ ] Redis, etcd for semaphore
29 changes: 4 additions & 25 deletions commands/agent.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package commands

import (
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"

"github.com/ChrisMcKenzie/dropship/service"
"github.com/hashicorp/hcl"
"github.com/ChrisMcKenzie/dropship/work"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -21,18 +19,17 @@ var agentCmd = &cobra.Command{

func agent(c *cobra.Command, args []string) {
InitializeConfig()

root := viper.GetString("servicePath")
services, err := loadServices(root)
services, err := service.LoadServices(root)
if err != nil {
log.Fatalln(err)
}

t := service.NewRunner(len(services))
t := work.NewRunner(len(services))
shutdownCh := make(chan struct{})

for _, s := range services {
_, err := service.NewDispatcher(s, t, shutdownCh)
_, err := work.NewDispatcher(s, t, shutdownCh)
if err != nil {
log.Fatal(err)
}
Expand All @@ -45,21 +42,3 @@ func agent(c *cobra.Command, args []string) {

t.Shutdown()
}

func loadServices(root string) (d []service.Config, err error) {
files, _ := filepath.Glob(root + "/*.hcl")
for _, file := range files {
var data []byte
data, err = ioutil.ReadFile(file)
if err != nil {
return
}

var deploy struct {
Services []service.Config `hcl:"service,expand"`
}
hcl.Decode(&deploy, string(data))
d = append(d, deploy.Services...)
}
return
}
36 changes: 29 additions & 7 deletions example.hcl
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"
}
}

39 changes: 39 additions & 0 deletions hook/consul-event.go
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
}
18 changes: 18 additions & 0 deletions hook/consul-event_test.go
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)
}
}
73 changes: 73 additions & 0 deletions hook/graphite-event.go
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
}
18 changes: 18 additions & 0 deletions hook/graphite-event_test.go
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)
}
}
25 changes: 25 additions & 0 deletions hook/hook.go
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
}
21 changes: 21 additions & 0 deletions hook/script.go
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
}
Loading

0 comments on commit 3fcf929

Please sign in to comment.