-
Notifications
You must be signed in to change notification settings - Fork 1
/
announce.go
108 lines (90 loc) · 2.23 KB
/
announce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"encoding/json"
"os/exec"
"path"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/coreos/go-etcd/etcd"
"github.com/spf13/cobra"
)
type serviceAnnouncement struct {
Path string
etcd *etcd.Client
Data string
TTL uint64
Interval time.Duration
Check string
}
// XXX: when process exits should we remove the key from etcd? configurable via flag?
func runAnnounce(cmd *cobra.Command, args []string) {
if len(args) != 1 {
log.Fatal("need a service name")
}
if announceTTL != 0 && announceTTL < announceInterval {
log.WithFields(log.Fields{
"announceTTL": announceTTL,
"announceInterval": announceInterval,
}).Fatal("announce ttl must be greater than interval")
}
svc := strings.ToLower(args[0])
// need better validation of name
if len(svc) == 0 {
log.Fatal("empty service name")
}
name, err := getNodeName()
if err != nil {
log.Fatal(err)
}
r := &record{
Port: uint16(announcePort),
Weight: uint16(announceWeight),
Target: name,
Priority: uint16(announcePriority),
}
data, err := json.Marshal(r)
if err != nil {
log.WithFields(log.Fields{
"record": r,
"error": err,
}).Fatal("failed to marshal record json")
}
a := &serviceAnnouncement{
Check: announceCheck,
Data: string(data),
Interval: time.Duration(announceInterval) * time.Second,
Path: path.Join("/", etcdPrefix, "services", svc, name),
TTL: uint64(announceTTL),
etcd: etcd.NewClient(([]string{etcdAddress})),
}
handleRemoveOnExit(a.etcd, a.Path)
a.announce()
for _ = range time.Tick(a.Interval) {
a.announce()
}
}
func (a *serviceAnnouncement) announce() {
if a.Check != "" {
// should we wrap in a timeout?
c := exec.Command("/bin/sh", "-c", a.Check)
output, err := c.CombinedOutput()
if err != nil {
// should failure immediately remove the entry or should we let ttl timeout?
// do rise/fall style checks?
log.WithFields(log.Fields{
"check": a.Check,
"error": err,
"output": output,
}).Error("failed to run check")
return
}
}
_, err := a.etcd.Set(a.Path, a.Data, a.TTL)
if err != nil {
log.WithFields(log.Fields{
"path": a.Path,
"error": err,
}).Error("failed to set path data")
}
}