-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
167 lines (135 loc) · 3.75 KB
/
config.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
package ecstaskmonitoring
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
"go.uber.org/zap"
)
const (
// AppName ... This tool name.
AppName = "ecs-task-monitoring"
// ColorRED ... Red color code
ColorRED = "#F08080"
)
var (
log Logger
// MonitorInterval ... Monitoring interval
MonitorInterval time.Duration
// DefaultParallelTaskCount ... Default number of tasks that can move in parallel
DefaultParallelTaskCount int
// CurrentTaskThresholdFailureCount ... How many times the current task has counted a failure
CurrentTaskThresholdFailureCount = 0
// ParallelNotifyTime ... Store notification time
ParallelNotifyTime time.Time
// ParallelNotifyInterval ... To parallel notify interval. minutes
ParallelNotifyInterval int
// Parallels ... Count tasks that run in parallel
Parallels []*Parallel
)
// Logger ... Store logging
type Logger struct {
sugar *zap.SugaredLogger
}
// Config ... Store from xxxx.toml
type Config struct {
Clusters []*Cluster `toml:"cluster"`
}
// Cluster ... Store ecs cluster data
type Cluster struct {
Name string `toml:"name"`
FailureCount int `toml:"failure_count"`
TaskThreshold int `toml:"task_threshold"`
AwsProfile string `toml:"aws_profile"`
AwsRegion string `toml:"aws_region"`
Tasks []*Task `toml:"task"`
IncomingWebhook string `toml:"incoming_webhook"`
Client *Client
Slack
}
// Task ... Store ecs task data
type Task struct {
Name string `toml:"name"`
Times int `toml:"times"`
EcsDescribeTasks []*ecs.Task
Slack
}
// Parallel ... increase Parallel times
type Parallel struct {
Name string
Times int
}
// ParallelNotify ... for slack message
type ParallelNotify struct {
Message string
ClusterName string
AwsProfile string
AwsRegion string
}
// Client ... Store ECS client with a session
type Client struct {
ecs ecsiface.ECSAPI
}
// SlackMessage ... Store slack message data
type SlackMessage struct {
Attachments []*Attachment `json:"attachments"`
}
// Attachment ... Slack Attachment Data
type Attachment struct {
Color string `json:"color,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
Footer string `json:"footer,omitempty"`
}
// LoadToml ... Read the toml file in the directory
func LoadToml(dir string) (*Config, error) {
// Add if not ending in /
if string(dir[len(dir)-1]) != "/" {
dir = dir + "/"
}
// load config. Combine and read the configuration files under the directory
files, _ := ioutil.ReadDir(dir)
openFiles := make([]io.Reader, len(files)*2)
// Add line breaks when joining files
for i := 0; i < len(files); i++ {
num := int(2 * float64(i))
if i == 0 {
num = 0
}
openFiles[num], _ = os.Open(fmt.Sprintf("%s%s", dir, files[i].Name()))
openFiles[num+1] = strings.NewReader("\n")
}
reader := io.MultiReader(openFiles...)
var config Config
if _, err := toml.DecodeReader(reader, &config); err != nil {
return nil, err
}
return &config, nil
}
// Validation ... Validation that the value set in toml is correct
func (c *Config) Validation() error {
invalidMessage := "invalid parameter"
for _, v := range c.Clusters {
if v.FailureCount == 0 {
return fmt.Errorf("%s: failure_count is 0", invalidMessage)
}
if v.TaskThreshold == 0 {
return fmt.Errorf("%s: task_threshold is 0", invalidMessage)
}
if v.Name == "" {
return fmt.Errorf("%s: name is empty", invalidMessage)
}
if v.AwsRegion == "" {
return fmt.Errorf("%s: aws_region is empty", invalidMessage)
}
if v.IncomingWebhook == "" {
return fmt.Errorf("%s: incoming_webhook is empty", invalidMessage)
}
}
return nil
}