-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
dashboard.go
422 lines (389 loc) · 9.18 KB
/
dashboard.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"sync"
"time"
)
var (
Namespace = "dashboard"
ConsulAddr = "127.0.0.1:8500"
Version string
ExtAssetDir string
Nodes []Node
Services map[string][]string
mutex sync.RWMutex
)
type KVPair struct {
Key string
CreateIndex int64
ModifyIndex int64
LockIndex int64
Flags int64
Value []byte
}
type Status int64
const (
Success Status = iota
Warning
Danger
Info
)
func (s Status) MarshalText() ([]byte, error) {
if s <= Info {
return []byte(strings.ToLower(s.String())), nil
} else {
return []byte(strconv.FormatInt(int64(s), 10)), nil
}
}
type Item struct {
Category string `json:"category"`
Node string `json:"node"`
Address string `json:"address"`
Timestamp string `json:"timestamp"`
Status Status `json:"status"`
Key string `json:"key"`
Data string `json:"data"`
}
func (kv *KVPair) NewItem() Item {
item := Item{
Data: string(kv.Value),
Timestamp: time.Unix(kv.Flags/1000, 0).Format("2006-01-02 15:04:05 -0700"),
}
item.Status = Status(kv.Flags % 1000)
// kv.Key : {namespace}/{category}/{node}/{key}
path := strings.Split(kv.Key, "/")
item.Category = path[1]
if len(path) >= 3 {
item.Node = path[2]
}
if len(path) >= 4 {
item.Key = path[3]
}
return item
}
type Node struct {
Node string
Address string
}
func main() {
var (
port int
showVersion bool
trigger string
)
flag.StringVar(&Namespace, "namespace", Namespace, "Consul kv top level key name. (/v1/kv/{namespace}/...)")
flag.IntVar(&port, "port", 3000, "http listen port")
flag.StringVar(&ExtAssetDir, "asset", "", "Serve files located in /assets from local directory. If not specified, use built-in asset.")
flag.BoolVar(&showVersion, "v", false, "show vesion")
flag.BoolVar(&showVersion, "version", false, "show vesion")
flag.StringVar(&trigger, "trigger", "", "trigger command")
flag.Parse()
if showVersion {
fmt.Println("consul-kv-dashboard: version:", Version)
return
}
mux := http.NewServeMux()
mux.HandleFunc("/", makeGzipHandler(indexPage))
mux.HandleFunc("/api/", makeGzipHandler(kvApiProxy))
if ExtAssetDir != "" {
mux.Handle("/assets/",
http.StripPrefix("/assets/", http.FileServer(http.Dir(ExtAssetDir))))
} else {
mux.Handle("/assets/",
http.FileServer(NewAssetFileSystem("/assets/")))
}
http.Handle("/", mux)
log.Println("listen port:", port)
log.Println("asset directory:", ExtAssetDir)
log.Println("namespace:", Namespace)
if trigger != "" {
log.Println("trigger:", trigger)
go watchForTrigger(trigger)
}
go updateNodes()
go updateServices()
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}
func indexPage(w http.ResponseWriter, r *http.Request) {
var (
data []byte
err error
)
if ExtAssetDir == "" {
data, err = Asset("index.html")
} else {
var f *os.File
f, err = os.Open(ExtAssetDir + "/index.html")
data, err = ioutil.ReadAll(f)
}
if err != nil {
log.Println(err)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, string(data))
}
func kvApiProxy(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
path := strings.TrimPrefix(r.URL.Path, "/api/")
resp, _, err := callConsulAPI(
"/v1/kv/" + Namespace + "/" + path + "?" + r.URL.RawQuery,
)
if err != nil {
http.Error(w, fmt.Sprintf("%s", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
w.Header().Set("Content-Type", "application/json")
http.Error(w, "[]", resp.StatusCode)
return
}
if resp.StatusCode != http.StatusOK {
http.Error(w, "", resp.StatusCode)
io.Copy(w, resp.Body)
return
}
// copy response header to client
for name, value := range resp.Header {
if strings.HasPrefix(name, "X-") || name == "Content-Type" {
for _, v := range value {
w.Header().Set(name, v)
}
}
}
// keys or values
dec := json.NewDecoder(resp.Body)
enc := json.NewEncoder(w)
if _, t := r.Form["keys"]; t {
var keys []string
uniqKeyMap := make(map[string]bool)
dec.Decode(&keys)
for _, key := range keys {
path := strings.Split(key, "/")
if len(path) >= 2 {
uniqKeyMap[path[1]] = true
}
}
uniqKeys := make([]string, 0, len(uniqKeyMap))
for key, _ := range uniqKeyMap {
uniqKeys = append(uniqKeys, key)
}
sort.Strings(uniqKeys)
enc.Encode(uniqKeys)
} else {
var kvps []*KVPair
dec.Decode(&kvps)
items := make([]Item, 0, len(kvps))
for _, kv := range kvps {
item := kv.NewItem()
if itemInCatalog(&item) {
items = append(items, item)
}
}
enc.Encode(items)
}
}
func watchForTrigger(command string) {
var index int64
lastStatus := make(map[string]Status)
prevItem := make(map[Item]Status)
for {
resp, newIndex, err := callConsulAPI(
"/v1/kv/" + Namespace + "/?recurse&wait=55s&index=" + strconv.FormatInt(index, 10),
)
if err != nil {
log.Println("[error]", err)
time.Sleep(10 * time.Second)
continue
}
index = newIndex
var kvps []*KVPair
dec := json.NewDecoder(resp.Body)
dec.Decode(&kvps)
resp.Body.Close()
// find each current item of category
currentItem := make(map[string]Item)
for _, kv := range kvps {
item := kv.NewItem()
if !itemInCatalog(&item) {
continue
}
current := compactItem(item)
_, exist := prevItem[current]
if exist && prevItem[current] != item.Status {
currentItem[item.Category] = item
}
}
for _, kv := range kvps {
item := kv.NewItem()
if !itemInCatalog(&item) {
continue
}
if _, exist := currentItem[item.Category]; !exist {
currentItem[item.Category] = item
} else if currentItem[item.Category].Status < item.Status {
currentItem[item.Category] = item
}
}
// invoke trigger when a category status was changed
for category, item := range currentItem {
if _, exist := lastStatus[category]; !exist {
// at first initialize
lastStatus[category] = item.Status
log.Printf("[info] %s: status %s", category, item.Status)
} else if lastStatus[category] != item.Status {
// status changed. invoking trigger.
log.Printf("[info] %s: status %s -> %s", category, lastStatus[category], item.Status)
lastStatus[category] = item.Status
b, _ := json.Marshal(item)
err := invokePipe(command, bytes.NewReader(b))
if err != nil {
log.Println("[error]", err)
}
}
}
// update previous item status
for _, kv := range kvps {
item := kv.NewItem()
prev := compactItem(item)
prevItem[prev] = item.Status
}
time.Sleep(1 * time.Second)
}
}
// compactItem builds `Item` struct that has only `Category`, `Key`, and `Node` fields.
func compactItem(item Item) Item {
return Item{
Key: item.Key,
Category: item.Category,
Node: item.Node,
}
}
func invokePipe(command string, src io.Reader) error {
log.Println("[info] Invoking command:", command)
cmd := exec.Command("sh", "-c", command)
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
cmdCh := make(chan error)
// src => stdin
go func() {
_, err := io.Copy(stdin, src)
if err != nil {
cmdCh <- err
}
stdin.Close()
}()
// wait for command exit
go func() {
cmdCh <- cmd.Wait()
}()
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
cmdErr := <-cmdCh
return cmdErr
}
func updateNodes() {
var index int64
for {
resp, newIndex, err := callConsulAPI(
"/v1/catalog/nodes?index=" + strconv.FormatInt(index, 10) + "&wait=55s",
)
if err != nil {
log.Println("[error]", err)
time.Sleep(10 * time.Second)
continue
}
index = newIndex
dec := json.NewDecoder(resp.Body)
mutex.Lock()
dec.Decode(&Nodes)
log.Println("[info]", Nodes)
mutex.Unlock()
time.Sleep(1 * time.Second)
resp.Body.Close()
}
}
func updateServices() {
var index int64
for {
resp, newIndex, err := callConsulAPI(
"/v1/catalog/services?index=" + strconv.FormatInt(index, 10) + "&wait=55s",
)
if err != nil {
log.Println("[error]", err)
time.Sleep(10 * time.Second)
continue
}
index = newIndex
dec := json.NewDecoder(resp.Body)
mutex.Lock()
dec.Decode(&Services)
mutex.Unlock()
time.Sleep(1 * time.Second)
resp.Body.Close()
}
}
func itemInCatalog(item *Item) bool {
mutex.RLock()
defer mutex.RUnlock()
for _, node := range Nodes {
if item.Node == node.Node {
item.Address = node.Address
return true
}
}
for name, tags := range Services {
if item.Node == name {
item.Address = "service"
return true
}
for _, tag := range tags {
if item.Node == fmt.Sprintf("%s.%s", tag, name) {
item.Address = "service"
return true
}
}
}
return false
}
func callConsulAPI(path string) (*http.Response, int64, error) {
var index int64
_url := "http://" + ConsulAddr + path
log.Println("[info] get", _url)
resp, err := http.Get(_url)
if err != nil {
log.Println("[error]", err)
return nil, index, err
}
_indexes := resp.Header["X-Consul-Index"]
if len(_indexes) > 0 {
index, _ = strconv.ParseInt(_indexes[0], 10, 64)
}
return resp, index, nil
}