tile38/controller/hooks.go

325 lines
7.3 KiB
Go
Raw Normal View History

2016-03-19 17:16:19 +03:00
package controller
import (
"bytes"
"errors"
2016-03-20 04:31:59 +03:00
"fmt"
2016-03-19 17:16:19 +03:00
"io"
2016-03-20 04:31:59 +03:00
"net/http"
2016-03-20 18:24:20 +03:00
"net/url"
2016-03-19 17:16:19 +03:00
"sort"
2016-03-20 18:24:20 +03:00
"strconv"
2016-03-19 17:16:19 +03:00
"strings"
"time"
2016-03-20 18:24:20 +03:00
"github.com/garyburd/redigo/redis"
2016-03-19 17:16:19 +03:00
"github.com/tidwall/tile38/controller/log"
)
type EndpointProtocol string
const (
HTTP = EndpointProtocol("http")
Disque = EndpointProtocol("disque")
)
type Endpoint struct {
Protocol EndpointProtocol
Original string
2016-03-20 18:24:20 +03:00
Disque struct {
Host string
Port int
QueueName string
Options struct {
Replicate int
}
}
2016-03-19 17:16:19 +03:00
}
2016-03-20 18:24:20 +03:00
2016-03-19 17:16:19 +03:00
type Hook struct {
Key string
Name string
2016-03-20 18:24:20 +03:00
Endpoints []Endpoint
2016-03-19 17:16:19 +03:00
Command string
Fence *liveFenceSwitches
ScanWriter *scanWriter
}
func (c *Controller) DoHook(hook *Hook, details *commandDetailsT) error {
2016-03-20 04:31:59 +03:00
msgs := c.FenceMatch(hook.Name, hook.ScanWriter, hook.Fence, details, false)
2016-03-19 17:16:19 +03:00
for _, msg := range msgs {
2016-03-20 18:24:20 +03:00
for _, endpoint := range hook.Endpoints {
switch endpoint.Protocol {
case HTTP:
if err := c.sendHTTPMessage(endpoint, msg); err != nil {
return err
}
return nil //sent
case Disque:
if err := c.sendDisqueMessage(endpoint, msg); err != nil {
return err
}
return nil // sent
2016-03-20 04:31:59 +03:00
}
}
2016-03-19 17:16:19 +03:00
}
2016-03-20 18:24:20 +03:00
return errors.New("not sent")
2016-03-19 17:16:19 +03:00
}
type hooksByName []*Hook
func (a hooksByName) Len() int {
return len(a)
}
func (a hooksByName) Less(i, j int) bool {
return a[i].Name < a[j].Name
}
func (a hooksByName) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func parseEndpoint(s string) (Endpoint, error) {
var endpoint Endpoint
endpoint.Original = s
switch {
default:
return endpoint, errors.New("unknown scheme")
case strings.HasPrefix(s, "http:"):
endpoint.Protocol = HTTP
case strings.HasPrefix(s, "https:"):
endpoint.Protocol = HTTP
case strings.HasPrefix(s, "disque:"):
endpoint.Protocol = Disque
}
s = s[strings.Index(s, ":")+1:]
if !strings.HasPrefix(s, "//") {
return endpoint, errors.New("missing the two slashes")
}
2016-03-20 18:24:20 +03:00
sqp := strings.Split(s[2:], "?")
sp := strings.Split(sqp[0], "/")
s = sp[0]
2016-03-19 17:16:19 +03:00
if s == "" {
return endpoint, errors.New("missing host")
}
2016-03-20 18:24:20 +03:00
if endpoint.Protocol == Disque {
dp := strings.Split(s, ":")
switch len(dp) {
default:
return endpoint, errors.New("invalid disque url")
case 1:
endpoint.Disque.Host = dp[0]
endpoint.Disque.Port = 7711
case 2:
endpoint.Disque.Host = dp[0]
n, err := strconv.ParseUint(dp[1], 10, 16)
if err != nil {
return endpoint, errors.New("invalid disque url")
}
endpoint.Disque.Port = int(n)
}
if len(sp) > 1 {
var err error
endpoint.Disque.QueueName, err = url.QueryUnescape(sp[1])
if err != nil {
return endpoint, errors.New("invalid disque queue name")
}
}
if len(sqp) > 1 {
m, err := url.ParseQuery(sqp[1])
if err != nil {
return endpoint, errors.New("invalid disque url")
}
for key, val := range m {
if len(val) == 0 {
continue
}
switch key {
case "replicate":
n, err := strconv.ParseUint(val[0], 10, 8)
if err != nil {
return endpoint, errors.New("invalid disque replicate value")
}
endpoint.Disque.Options.Replicate = int(n)
}
}
}
if endpoint.Disque.QueueName == "" {
return endpoint, errors.New("missing disque queue name")
}
}
2016-03-19 17:16:19 +03:00
return endpoint, nil
}
2016-03-20 18:24:20 +03:00
func (c *Controller) cmdSetHook(line string) (err error) {
2016-03-19 17:16:19 +03:00
//start := time.Now()
2016-03-20 18:24:20 +03:00
var name, values, cmd string
2016-03-19 17:16:19 +03:00
if line, name = token(line); name == "" {
return errInvalidNumberOfArguments
}
2016-03-20 18:24:20 +03:00
if line, values = token(line); values == "" {
2016-03-19 17:16:19 +03:00
return errInvalidNumberOfArguments
}
2016-03-20 18:24:20 +03:00
var endpoints []Endpoint
for _, value := range strings.Split(values, ",") {
endpoint, err := parseEndpoint(value)
if err != nil {
log.Errorf("sethook: %v", err)
return errInvalidArgument(value)
}
endpoints = append(endpoints, endpoint)
2016-03-19 17:16:19 +03:00
}
command := line
if line, cmd = token(line); cmd == "" {
return errInvalidNumberOfArguments
}
cmdlc := strings.ToLower(cmd)
var types []string
switch cmdlc {
default:
return errInvalidArgument(cmd)
case "nearby":
types = nearbyTypes
case "within", "intersects":
types = withinOrIntersectsTypes
}
s, err := c.cmdSearchArgs(cmdlc, line, types)
if err != nil {
return err
}
if !s.fence {
return errors.New("missing FENCE argument")
}
s.cmd = cmdlc
hook := &Hook{
2016-03-20 18:24:20 +03:00
Key: s.key,
Name: name,
Endpoints: endpoints,
Fence: &s,
Command: command,
2016-03-19 17:16:19 +03:00
}
var wr bytes.Buffer
hook.ScanWriter, err = c.newScanWriter(&wr, s.key, s.output, s.precision, s.glob, s.limit, s.wheres, s.nofields)
if err != nil {
return err
}
// delete the previous hook
if h, ok := c.hooks[name]; ok {
if hm, ok := c.hookcols[h.Key]; ok {
delete(hm, h.Name)
}
delete(c.hooks, h.Name)
}
c.hooks[name] = hook
hm, ok := c.hookcols[hook.Key]
if !ok {
hm = make(map[string]*Hook)
c.hookcols[hook.Key] = hm
}
hm[name] = hook
return nil
}
func (c *Controller) cmdDelHook(line string) (err error) {
var name string
if line, name = token(line); name == "" {
return errInvalidNumberOfArguments
}
if line != "" {
return errInvalidNumberOfArguments
}
if h, ok := c.hooks[name]; ok {
if hm, ok := c.hookcols[h.Key]; ok {
delete(hm, h.Name)
}
delete(c.hooks, h.Name)
}
return
}
func (c *Controller) cmdHooks(line string, w io.Writer) (err error) {
start := time.Now()
var pattern string
if line, pattern = token(line); pattern == "" {
return errInvalidNumberOfArguments
}
if line != "" {
return errInvalidNumberOfArguments
}
var hooks []*Hook
for name, hook := range c.hooks {
if ok, err := globMatch(pattern, name); err == nil && ok {
hooks = append(hooks, hook)
} else if err != nil {
return errInvalidArgument(pattern)
}
}
sort.Sort(hooksByName(hooks))
buf := &bytes.Buffer{}
2016-03-20 18:24:20 +03:00
buf.WriteString(`{"ok":true,"hooks":[`)
2016-03-19 17:16:19 +03:00
for i, hook := range hooks {
if i > 0 {
2016-03-20 18:24:20 +03:00
buf.WriteByte(',')
}
2016-03-20 19:53:15 +03:00
buf.WriteString(`{`)
2016-03-20 18:24:20 +03:00
buf.WriteString(`"name":` + jsonString(hook.Name))
buf.WriteString(`,"key":` + jsonString(hook.Key))
buf.WriteString(`,"endpoints":[`)
for i, endpoint := range hook.Endpoints {
if i > 0 {
buf.WriteByte(',')
}
buf.WriteString(jsonString(endpoint.Original))
2016-03-19 17:16:19 +03:00
}
2016-03-20 18:24:20 +03:00
buf.WriteString(`],"command":` + jsonString(hook.Command))
buf.WriteString(`}`)
2016-03-19 17:16:19 +03:00
}
2016-03-20 18:24:20 +03:00
buf.WriteString(`],"elapsed":"` + time.Now().Sub(start).String() + "\"}")
2016-03-19 17:16:19 +03:00
w.Write(buf.Bytes())
return
}
2016-03-20 18:24:20 +03:00
func (c *Controller) sendHTTPMessage(endpoint Endpoint, msg []byte) error {
resp, err := http.Post(endpoint.Original, "application/json", bytes.NewBuffer(msg))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("enpoint returned status code %d", resp.StatusCode)
}
return nil
}
func (c *Controller) sendDisqueMessage(endpoint Endpoint, msg []byte) error {
addr := fmt.Sprintf("%s:%d", endpoint.Disque.Host, endpoint.Disque.Port)
conn, err := redis.DialTimeout("tcp", addr, time.Second/4, time.Second/4, time.Second/4)
if err != nil {
return err
}
defer conn.Close()
options := []interface{}{endpoint.Disque.QueueName, msg, 0}
replicate := endpoint.Disque.Options.Replicate
if replicate > 0 {
options = append(options, "REPLICATE")
options = append(options, endpoint.Disque.Options.Replicate)
}
id, err := redis.String(conn.Do("ADDJOB", options...))
if err != nil {
return err
}
p := strings.Split(id, "-")
if len(p) != 4 {
return errors.New("invalid disque reply")
}
return nil
}