-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
216 lines (178 loc) · 4.82 KB
/
main.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
//go:generate file2go -in template.yml -pkg main
// Package main provides a command-line tool for setting up the CloudWatch Logs integration for Apex Logs.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"text/template"
flag "github.com/integrii/flaggy"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
// version string.
var version string
// Config struct.
type Config struct {
StackName string
ProjectID string
Region string
Endpoint string
AuthToken string
Include []string
Exclude []string
Template bool
Create bool
}
// TemplateConfig struct.
type TemplateConfig struct {
Groups []string
}
func main() {
c := Config{
StackName: "ApexLogs",
}
// flags
flag.SetVersion(version)
flag.String(&c.ProjectID, "", "project-id", "Apex Logs destination project ID")
flag.String(&c.AuthToken, "", "auth-token", "Apex Logs authentication token")
flag.String(&c.Endpoint, "", "endpoint", "Apex Logs integration endpoint URL")
flag.String(&c.Region, "", "region", "AWS region id")
flag.String(&c.StackName, "", "stack-name", "AWS CloudFormation stack name")
flag.StringSlice(&c.Exclude, "", "exclude", "AWS CloudWatch log group filters")
flag.StringSlice(&c.Include, "", "include", "AWS CloudWatch log group filters")
flag.Bool(&c.Create, "", "confirm", "Confirm creation of the stack")
flag.Bool(&c.Template, "", "template", "Output the template and exit")
flag.Parse()
// why you no allow required flags :D
if c.ProjectID == "" {
log.Fatalf("error: project-id required")
}
if c.Endpoint == "" {
log.Fatalf("error: endpoint required")
}
if c.AuthToken == "" {
log.Fatalf("error: auth-token required")
}
if c.Region == "" {
log.Fatalf("error: region required")
}
// fetch log groups
fmt.Printf("==> Finding log groups\n")
groups, err := getLogGroups(c)
if err != nil {
log.Fatalf("error fetching log groups: %s", err)
}
fmt.Printf("==> Found %d matching log groups:\n\n", len(groups))
for _, g := range groups {
fmt.Printf(" %s\n", g)
}
fmt.Printf("\n")
// render template
tmpl, err := renderTemplate(TemplateConfig{
Groups: groups,
})
if err != nil {
log.Fatalf("error rendering template: %s", err)
}
// output template
if c.Template {
fmt.Printf("%s\n", tmpl)
return
}
f, err := ioutil.TempFile(os.TempDir(), "apex-logs-template-*.yml")
if err != nil {
log.Fatalf("error creating tmpfile: %s", err)
}
_, err = f.WriteString(tmpl)
if err != nil {
log.Fatalf("error writing template file: %s", err)
}
err = f.Close()
if err != nil {
log.Fatalf("error writing template file: %s", err)
}
// create stack
if c.Create {
fmt.Printf("==> Creating CloudFormation stack\n")
err = createStack(f.Name(), c)
if err != nil {
log.Fatalf("error creating stack: %s", err)
}
return
}
fmt.Printf("==> Run command again with --confirm to create the stack\n")
}
// createStack creates a CloudFormation stack.
func createStack(tmplPath string, c Config) error {
cmd := exec.Command("aws", "cloudformation", "create-stack",
"--stack-name", c.StackName,
"--region", c.Region,
"--template-body", "file://"+tmplPath,
"--capabilities", "CAPABILITY_IAM",
"--parameters",
"ParameterKey=Endpoint,ParameterValue="+c.Endpoint,
"ParameterKey=AuthToken,ParameterValue="+c.AuthToken,
"ParameterKey=ProjectID,ParameterValue="+c.ProjectID,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// getLogGroups returns all log groups.
func getLogGroups(c Config) ([]string, error) {
conf := aws.NewConfig().WithRegion(c.Region)
logs := cloudwatchlogs.New(session.New(conf))
var groups []string
var cursor *string
for {
res, err := logs.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{
NextToken: cursor,
})
if err != nil {
return nil, fmt.Errorf("describing log groups: %w", err)
}
cursor = res.NextToken
for _, group := range res.LogGroups {
name := *group.LogGroupName
if filter(name, c.Include, c.Exclude) {
groups = append(groups, name)
}
}
if cursor == nil {
break
}
}
return groups, nil
}
// renderTemplate returns a rendered CloudFormation template.
func renderTemplate(c TemplateConfig) (string, error) {
t := template.Must(template.New("template.yml").Parse(string(TemplateYml)))
var buf bytes.Buffer
err := t.Execute(&buf, c)
if err != nil {
return "", err
}
return buf.String(), nil
}
// filter returns true if the string matches the filters.
func filter(s string, include, exclude []string) bool {
if include != nil {
return match(s, include) && !match(s, exclude)
}
return !match(s, exclude)
}
// match returns true if s matches any of patterns.
func match(s string, patterns []string) bool {
for _, p := range patterns {
if strings.Contains(s, p) {
return true
}
}
return false
}