/
add.go
138 lines (123 loc) · 3.38 KB
/
add.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
package cmd
import (
"errors"
"time"
"github.com/d-kuro/escale/pkg/aws"
"github.com/d-kuro/escale/pkg/aws/autoscaling"
"github.com/d-kuro/escale/pkg/elasticsearch"
"github.com/d-kuro/escale/pkg/log"
"github.com/cenkalti/backoff"
"github.com/spf13/cobra"
)
type AddOption struct {
host string
port int
autoScalingGroup string
desired int
maxRetry uint64
delay time.Duration
region string
profile string
configFile string
}
func NewAddCommand(o *AddOption) *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Add Elasticsearch nodes",
RunE: func(cmd *cobra.Command, args []string) error {
return runAddCommand(o)
},
}
fset := cmd.Flags()
fset.StringVar(&o.host, "host", "", "Elasticsearch host")
fset.IntVar(&o.port, "port", 9200, "Elasticsearch port")
fset.StringVarP(&o.autoScalingGroup, "auto-scaling-group", "g", "", "Auto Scaling Group name")
fset.IntVar(&o.desired, "desired", 0, "Desired capacity")
fset.Uint64Var(&o.maxRetry, "max-retry", 100, "Max retry count")
fset.DurationVar(&o.delay, "delay", 5*time.Second, "Delay between retries")
fset.StringVar(&o.region, "region", "", "AWS region")
fset.StringVar(&o.profile, "profile", "", "AWS profile name")
fset.StringVarP(&o.configFile, "config-file", "f", ".escale.yaml", "Configuration file to read in")
return cmd
}
func runAddCommand(o *AddOption) error {
o, err := o.readConfig()
if err != nil {
return err
}
if err := o.validate(); err != nil {
return err
}
esClient := elasticsearch.NewClient(o.host, o.port)
log.Logger.Printf("Disable allocation for the cluster")
if _, err := esClient.SetAllocation(false); err != nil {
return err
}
sess, err := aws.NewSession(o.region, o.profile)
if err != nil {
return err
}
client := autoscaling.NewClient(sess)
if err := client.IncreaseInstances(o.autoScalingGroup, int64(o.desired)); err != nil {
return err
}
log.Logger.Printf("Waiting for nodes join to Elasticsearch cluster...")
backOff := backoff.WithMaxRetries(backoff.NewConstantBackOff(o.delay), o.maxRetry)
if err := backoff.Retry(getElasticsearchNodes(esClient, o), backOff); err != nil {
return err
}
log.Logger.Printf("Enable allocation for the cluster")
if _, err := esClient.SetAllocation(true); err != nil {
return err
}
log.Logger.Printf("Finished")
return nil
}
func (o AddOption) readConfig() (*AddOption, error) {
if o.configFile == "" {
return &o, nil
}
config, err := GetConfig(o.configFile)
if err != nil {
switch err.(type) {
case *ErrFileNotExist:
return &o, nil
default:
return nil, err
}
}
if o.host == "" {
o.host = config.Host
}
if o.port == 9200 {
o.port = config.Port
}
if o.autoScalingGroup == "" {
o.autoScalingGroup = config.AutoScalingGroup
}
return &o, nil
}
func (o AddOption) validate() error {
if o.host == "" {
return errors.New("host is required")
}
if o.autoScalingGroup == "" {
return errors.New("auto-scaling-group is required")
}
if o.desired <= 0 {
return errors.New("desired is required (desired > 0)")
}
return nil
}
func getElasticsearchNodes(client *elasticsearch.Client, o *AddOption) func() error {
return func() error {
nodes, err := client.ListNodes()
if err != nil {
return err
}
if len(nodes) == o.desired {
return nil
}
return errors.New("timed out: added nodes do not join to Elasticsearch cluster")
}
}