-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
92 lines (78 loc) · 1.67 KB
/
util.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
package main
import (
"fmt"
"net"
"os"
"os/signal"
"strings"
"syscall"
log "github.com/Sirupsen/logrus"
etcdErr "github.com/coreos/etcd/error"
"github.com/coreos/go-etcd/etcd"
)
func isKeyNotFound(err error) bool {
e, ok := err.(*etcd.EtcdError)
return ok && e.ErrorCode == etcdErr.EcodeKeyNotFound
}
func getNodeIP() (net.IP, error) {
if nodeIP == "" {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, fmt.Errorf("failed to get interface addresses: %s", err)
}
for _, a := range addrs {
ip, _, err := net.ParseCIDR(a.String())
if err != nil {
// log error?
continue
}
if ip.To4() == nil {
continue
}
if ip.IsGlobalUnicast() {
nodeIP = ip.String()
break
}
}
}
if nodeIP == "" {
return nil, fmt.Errorf("failed to get address")
}
ip := net.ParseIP(nodeIP)
if ip == nil {
return nil, fmt.Errorf("failed to parse address: %s", nodeIP)
}
// XXX: we currently only correctly handle v4
if ip.To4() == nil {
return nil, fmt.Errorf("not an ipv4 address: %s", nodeIP)
}
return ip, nil
}
func getNodeName() (string, error) {
if nodeName == "" {
var err error
nodeName, err = os.Hostname()
if err != nil {
return "", fmt.Errorf("failed to get hostname: %s", err)
}
}
return strings.ToLower(nodeName), nil
}
func handleRemoveOnExit(e *etcd.Client, key string) {
if removeOnExit {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
for _ = range c {
_, err := e.Delete(key, false)
if err != nil {
log.WithFields(log.Fields{
"key": key,
"error": err,
}).Error("failed to delete key")
}
os.Exit(0)
}
}()
}
}