-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheskeeper.go
141 lines (117 loc) · 2.48 KB
/
eskeeper.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
package eskeeper
import (
"context"
"fmt"
"io"
)
// Eskeeper manages indices & aliases.
type Eskeeper struct {
client *esclient
// options
user string
pass string
verbose bool
skipPreCheck bool
}
// NewOption is optional func for eskeeper.New
type NewOption func(*Eskeeper)
// UserName is optional func for Elasticsearch user name.
func UserName(name string) NewOption {
return func(e *Eskeeper) {
e.user = name
}
}
// Pass is optional func for Elasticsearch password.
func Pass(pass string) NewOption {
return func(e *Eskeeper) {
e.pass = pass
}
}
// Verbose is optional func for verbose option.
func Verbose(v bool) NewOption {
return func(e *Eskeeper) {
e.verbose = v
}
}
// SkipPreCheck is optional func for skipping pre-check-stage..
func SkipPreCheck(v bool) NewOption {
return func(e *Eskeeper) {
e.skipPreCheck = v
}
}
// New inits Eskeeper.
func New(urls []string, opts ...NewOption) (*Eskeeper, error) {
eskeeper := &Eskeeper{}
for _, opt := range opts {
opt(eskeeper)
}
es, err := newEsClient(urls, eskeeper.user, eskeeper.pass)
if err != nil {
return nil, err
}
es.verbose = eskeeper.verbose
eskeeper.client = es
return eskeeper, nil
}
// Sync synchronizes config & Elasticsearch State.
func (e *Eskeeper) Sync(ctx context.Context, reader io.Reader) error {
e.log("loading config ...")
conf, err := yaml2Conf(reader)
if err != nil {
return err
}
e.log("\n=== validation stage ===")
err = e.validateConfigFormat(conf)
if err != nil {
return err
}
if !e.skipPreCheck {
e.log("\n=== pre-check stage ===")
err = e.client.preCheck(ctx, conf)
if err != nil {
return err
}
}
e.log("\n=== sync stage ===")
err = e.client.syncIndices(ctx, conf)
if err != nil {
return err
}
err = e.client.syncAliases(ctx, conf)
if err != nil {
return err
}
err = e.client.syncCloseStatus(ctx, conf)
if err != nil {
return err
}
e.log("\n=== post-check stage ===")
err = e.client.postCheck(ctx, conf)
if err != nil {
return err
}
e.log("\nsucceeded")
return nil
}
// Validate validates cofig.
func (e *Eskeeper) Validate(ctx context.Context, reader io.Reader) error {
conf, err := yaml2Conf(reader)
if err != nil {
return err
}
err = e.validateConfigFormat(conf)
if err != nil {
return err
}
return nil
}
func (e *Eskeeper) log(msg string) {
if e.verbose {
fmt.Printf("\x1b[34m%s\x1b[0m\n", msg)
}
}
func (e *Eskeeper) logf(format string, a ...interface{}) {
if e.verbose {
fmt.Printf(format, a...)
}
}