-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreaper.go
144 lines (111 loc) · 2.92 KB
/
reaper.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
"github.com/golang/glog"
"github.com/smartystreets/go-aws-auth"
)
var (
logLevel = os.Getenv("LOG_LEVEL")
indexPrefix = os.Getenv("INDEX_PREFIX")
maxCountStr = os.Getenv("MAX_COUNT")
esURL = os.Getenv("ES_URL")
maxCount = 30
indexReaper awsESIndexReaper
)
type awsESQueryer interface {
esRequest(string, string) ([]byte, error)
}
type awsESIndexReaper struct {
indexList indexes
}
type indexSettings struct {
Settings struct {
Index struct {
CreationDate string `json:"creation_date"`
ProvidedName string `json:"provided_name"`
} `json:"index"`
} `json:"settings"`
}
type indexes map[string]indexSettings
func (es awsESIndexReaper) esRequest(url, action string) ([]byte, error) {
client := new(http.Client)
req, err := http.NewRequest(action, url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
awsauth.Sign(req)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func makeESRequest(es awsESQueryer, url, action string) ([]byte, error) {
return es.esRequest(url, action)
}
func (es awsESIndexReaper) sortIndexesByAge() ([]indexSettings, error) {
indexesSlice := []indexSettings{}
for _, v := range es.indexList {
indexesSlice = append(indexesSlice, v)
}
sort.Slice(indexesSlice, func(i, j int) bool {
return indexesSlice[i].Settings.Index.CreationDate < indexesSlice[j].Settings.Index.CreationDate
})
return indexesSlice, nil
}
func main() {
flag.Parse()
flag.Lookup("logtostderr").Value.Set("true")
if logLevel != "" {
flag.Lookup("v").Value.Set(logLevel)
} else {
flag.Lookup("v").Value.Set("2")
}
if esURL == "" {
glog.Fatal("Set ES_URL to the elasticsearch URL")
}
if indexPrefix == "" {
glog.Fatal("Set INDEX_PREFIX to the name of the indexes to match")
}
if maxCountStr != "" {
t, _ := strconv.Atoi(maxCountStr)
maxCount = t
}
glog.Infof("Index reaper started. Will keep %d indexes matching ^%s on ES domain %s", maxCount, indexPrefix, esURL)
body, err := makeESRequest(indexReaper, fmt.Sprintf("%s/%s*/_settings?pretty", esURL, indexPrefix), "GET")
if err != nil {
glog.Fatal(err)
}
err = json.Unmarshal(body, &indexReaper.indexList)
if err != nil {
glog.Fatal(err)
}
sortedIndexes, err := indexReaper.sortIndexesByAge()
if err != nil {
glog.Fatal(err)
}
if len(sortedIndexes) > maxCount {
for _, e := range sortedIndexes[:len(sortedIndexes)-maxCount] {
iName := e.Settings.Index.ProvidedName
glog.Infof("Deleting index %s", iName)
body, err := indexReaper.esRequest(fmt.Sprintf(esURL+"/"+iName), "DELETE")
if err != nil {
glog.Errorf("An error occurred deleting index %s: %s", iName, err)
}
glog.V(4).Infof("Delete response is %s", body)
}
}
}