-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcd.go
121 lines (104 loc) · 2.82 KB
/
etcd.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
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright © 2015 Jason Smith <[email protected]>.
//
// Use of this source code is governed by the LGPL-3
// license that can be found in the LICENSE file.
package etcd
import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/coreos/etcd/client"
"github.com/duckbunny/herald"
"github.com/duckbunny/service"
"golang.org/x/net/context"
)
var (
etcdMachines string
uname string
upass string
// Where the ServiceKVPath resides
KVServicesPath string = "services"
// Title for specifying herald in flags
Title string = "etcd"
)
func init() {
flag.StringVar(&etcdMachines, "etcd-machines", os.Getenv("ETCD_MACHINES"), "The etcd machines.")
flag.StringVar(&uname, "etcd-username", os.Getenv("ETCD_USER"), "The etcd username if secured.")
flag.StringVar(&upass, "etcd-pass", os.Getenv("ETCD_PASS"), "The etcd password if secured.")
}
// Etcd structure stores the etcd clients KeysAPI
type Etcd struct {
KeysAPI client.KeysAPI
}
// New returns a new Etcd struct
func New() *Etcd {
return new(Etcd)
}
// Declare the service per the Declare interface in Herald.
func (e *Etcd) Declare(s *service.Service) error {
js, err := json.Marshal(s)
if err != nil {
return err
}
key := FormattedKey(s)
_, err = e.KeysAPI.Set(context.Background(), key, string(js), nil)
if err != nil {
return ProcessEtcdErrors(err)
}
return nil
}
// Get retrieves a service per the Declare interface in Herald
func (e *Etcd) GetService(s *service.Service) error {
key := FormattedKey(s)
resp, err := e.KeysAPI.Get(context.Background(), key, nil)
if err != nil {
return ProcessEtcdErrors(err)
}
return json.Unmarshal([]byte(resp.Node.Value), s)
}
func ProcessEtcdErrors(err error) error {
if err == context.Canceled {
return errors.New("Context cancelled by another routine")
} else if err == context.DeadlineExceeded {
return errors.New("Context deadline exceeded")
//Need clarification on this one
//} else if cerr, ok := err.(*client.ClusterError); ok {
// process (cerr.Errors)
} else {
return errors.New("Bad cluster endpoints, which are not etcd servers")
}
}
func (e *Etcd) Init() error {
config := client.Config{
Endpoints: Machines(),
}
if uname != "" {
config.Username = uname
if upass != "" {
config.Password = upass
} else {
return errors.New("Etcd username provided but no password")
}
}
cl, err := client.New(config)
if err != nil {
return err
}
e.KeysAPI = client.NewKeysAPI(cl)
return nil
}
func Machines() []string {
return strings.Split(etcdMachines, "|")
}
// FormattedKey returns correctly formatted key of the service
func FormattedKey(s *service.Service) string {
return fmt.Sprintf("/%v/%v/%v/%v/definition", KVServicesPath, s.Domain, s.Title, s.Version)
}
// Register this herald with consul
func Register() {
c := New()
herald.AddDeclaration(Title, c)
}