-
Notifications
You must be signed in to change notification settings - Fork 446
/
consul.go
217 lines (179 loc) · 4.35 KB
/
consul.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package services
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"io/ioutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/solo-io/go-utils/log"
)
const consulDockerImage = "consul:1.5.2"
type ConsulFactory struct {
consulPath string
tmpdir string
}
type serviceDef struct {
Service *consulService `json:"service"`
}
type consulService struct {
ID string `json:"id"`
Name string `json:"name"`
Port uint32 `json:"port"`
Tags []string `json:"tags"`
Address string `json:"address"`
}
func NewConsulFactory() (*ConsulFactory, error) {
consulPath := os.Getenv("CONSUL_BINARY")
if consulPath != "" {
return &ConsulFactory{
consulPath: consulPath,
}, nil
}
consulPath, err := exec.LookPath("consul")
if err == nil {
log.Printf("Using consul from PATH: %s", consulPath)
return &ConsulFactory{
consulPath: consulPath,
}, nil
}
// try to grab one form docker...
tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "consul")
if err != nil {
return nil, err
}
bash := fmt.Sprintf(`
set -ex
CID=$(docker run -d %s /bin/sh -c exit)
# just print the image sha for repoducibility
echo "Using Consul Image:"
docker inspect %s -f "{{.RepoDigests}}"
docker cp $CID:/bin/consul .
docker rm -f $CID
`, consulDockerImage, consulDockerImage)
scriptFile := filepath.Join(tmpdir, "get_consul.sh")
err = ioutil.WriteFile(scriptFile, []byte(bash), 0755)
if err != nil {
return nil, err
}
cmd := exec.Command("bash", scriptFile)
cmd.Dir = tmpdir
cmd.Stdout = GinkgoWriter
cmd.Stderr = GinkgoWriter
if err := cmd.Run(); err != nil {
return nil, err
}
return &ConsulFactory{
consulPath: filepath.Join(tmpdir, "consul"),
tmpdir: tmpdir,
}, nil
}
func (ef *ConsulFactory) Clean() error {
if ef == nil {
return nil
}
if ef.tmpdir != "" {
_ = os.RemoveAll(ef.tmpdir)
}
return nil
}
func (ef *ConsulFactory) NewConsulInstance() (*ConsulInstance, error) {
// try to grab one form docker...
tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "consul")
if err != nil {
return nil, err
}
cfgDir := filepath.Join(tmpdir, "config")
err = os.Mkdir(cfgDir, 0755)
if err != nil {
return nil, err
}
cmd := exec.Command(ef.consulPath, "agent", "-dev", "--client=0.0.0.0", "-config-dir", cfgDir,
"-node", "consul-dev")
cmd.Dir = ef.tmpdir
cmd.Stdout = GinkgoWriter
cmd.Stderr = GinkgoWriter
return &ConsulInstance{
consulPath: ef.consulPath,
tmpdir: tmpdir,
cfgDir: cfgDir,
cmd: cmd,
registeredServices: map[string]*serviceDef{},
}, nil
}
type ConsulInstance struct {
consulPath string
tmpdir string
cfgDir string
cmd *exec.Cmd
session *gexec.Session
registeredServices map[string]*serviceDef
}
func (i *ConsulInstance) AddConfig(svcId, content string) error {
fileName := filepath.Join(i.cfgDir, svcId+".json")
return ioutil.WriteFile(fileName, []byte(content), 0644)
}
func (i *ConsulInstance) AddConfigFromStruct(svcId string, cfg interface{}) error {
content, err := json.Marshal(cfg)
if err != nil {
return err
}
return i.AddConfig(svcId, string(content))
}
func (i *ConsulInstance) ReloadConfig() error {
err := i.cmd.Process.Signal(syscall.SIGHUP)
if err != nil {
return err
}
return nil
}
func (i *ConsulInstance) Silence() {
i.cmd.Stdout = nil
i.cmd.Stderr = nil
}
func (i *ConsulInstance) Run() error {
var err error
i.session, err = gexec.Start(i.cmd, GinkgoWriter, GinkgoWriter)
if err != nil {
return err
}
EventuallyWithOffset(2, i.session.Out, "5s").Should(gbytes.Say("New leader elected"))
return nil
}
func (i *ConsulInstance) Binary() string {
return i.consulPath
}
func (i *ConsulInstance) Clean() error {
if i.session != nil {
i.session.Kill()
}
if i.cmd != nil && i.cmd.Process != nil {
i.cmd.Process.Kill()
}
if i.tmpdir != "" {
return os.RemoveAll(i.tmpdir)
}
return nil
}
func (i *ConsulInstance) RegisterService(svcName, svcId, address string, tags []string, port uint32) error {
svcDef := &serviceDef{
Service: &consulService{
ID: svcId,
Name: svcName,
Address: address,
Tags: tags,
Port: port,
},
}
i.registeredServices[svcId] = svcDef
err := i.AddConfigFromStruct(svcId, svcDef)
if err != nil {
return err
}
return i.ReloadConfig()
}