-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
71 lines (59 loc) · 1.28 KB
/
node.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
package main
import (
"encoding/json"
"net"
"path/filepath"
"time"
log "github.com/Sirupsen/logrus"
"github.com/coreos/go-etcd/etcd"
"github.com/spf13/cobra"
)
type nodeAnnouncement struct {
IP net.IP
Path string
etcd *etcd.Client
Data string
}
func (a *nodeAnnouncement) announce() {
_, err := a.etcd.Set(a.Path, a.Data, uint64(nodeTTL))
if err != nil {
log.WithFields(log.Fields{
"path": a.Path,
"data": a.Data,
"error": err,
}).Error("failed to set path data")
}
}
func runNode(cmd *cobra.Command, args []string) {
name, err := getNodeName()
if err != nil {
log.WithField("error", err).Fatal("failed to get node name")
}
ip, err := getNodeIP()
if err != nil {
log.WithFields(log.Fields{
"name": name,
"error": err,
}).Fatal("failed to get node ip")
}
n := &node{IP: ip}
data, err := json.Marshal(n)
if err != nil {
log.WithFields(log.Fields{
"name": name,
"node": n,
"error": err,
}).Fatal("failed to json marshal node")
}
a := &nodeAnnouncement{
etcd: etcd.NewClient(([]string{etcdAddress})),
Path: filepath.Join("/", etcdPrefix, "nodes", name),
IP: ip,
Data: string(data),
}
handleRemoveOnExit(a.etcd, a.Path)
a.announce()
for _ = range time.Tick(time.Duration(nodeInterval) * time.Second) {
a.announce()
}
}