-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsesame-seed.go
218 lines (203 loc) · 5.77 KB
/
sesame-seed.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
217
218
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sts"
)
var version string
var cfg config
type config struct {
verbosity bool
}
func handleErr(err error) {
if cfg.verbosity {
fmt.Println(err)
}
}
func parseCWDimensions(cwDimensions string) (dims []*cloudwatch.Dimension) {
// example input string is `Host=myhost,MetricSource=sesame-seed`
sdims := strings.Split(cwDimensions, ",")
for _, sdim := range sdims {
name := strings.Split(sdim, "=")[0]
value := strings.Split(sdim, "=")[1]
dim := cloudwatch.Dimension{
Name: &name,
Value: &value,
}
dims = append(dims, &dim)
}
return dims
}
func functionCWPutMetric(region, cwNamespace, cwMetricName, cwDimensions, cwAssumeRoleArn string, cwValue float64) (err error) {
// first handle the assume role business
if cfg.verbosity {
fmt.Println("Inside functionCWPutMetric")
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region)},
)
svcProfile := sts.New(sess)
roleSessionName := "sesame-seed"
var duration int64
duration = 3200
params := &sts.AssumeRoleInput{
RoleArn: &cwAssumeRoleArn,
RoleSessionName: &roleSessionName,
DurationSeconds: &duration,
}
// now try the assume-role with the loaded creds
resp, err := svcProfile.AssumeRole(params)
if err != nil {
return err
}
if cfg.verbosity {
fmt.Printf("Successfully assumed role %s", cwAssumeRoleArn)
}
statCreds := credentials.NewStaticCredentials(
*resp.Credentials.AccessKeyId,
*resp.Credentials.SecretAccessKey,
*resp.Credentials.SessionToken)
sess = session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Credentials: statCreds,
Region: ®ion,
},
}))
if err != nil {
handleErr(err)
return err
}
c := cloudwatch.New(sess)
dims := parseCWDimensions(cwDimensions)
metricDatum := cloudwatch.MetricDatum{
MetricName: &cwMetricName,
Dimensions: dims,
Value: &cwValue,
}
var metricDatums []*cloudwatch.MetricDatum
metricDatums = append(metricDatums, &metricDatum)
cwInput := cloudwatch.PutMetricDataInput{
Namespace: &cwNamespace,
MetricData: metricDatums,
}
if cfg.verbosity {
fmt.Println("Attempting to put metric...")
}
_, err = c.PutMetricData(&cwInput)
if err != nil {
handleErr(err)
return err
}
fmt.Printf("Successfully put %d metric(s) of value %f\n", len(metricDatums), cwValue)
return err
}
func functionS3Download(region, s3bucket, s3key, s3dest string) (err error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region)},
)
if err != nil {
handleErr(err)
return err
}
s := s3.New(sess)
goInput := s3.GetObjectInput{
Bucket: &s3bucket,
Key: &s3key,
}
result, err := s.GetObject(&goInput)
if err != nil {
handleErr(err)
return err
}
body, err := ioutil.ReadAll(result.Body)
if err != nil {
handleErr(err)
return err
}
f, err := os.Create(s3dest)
if err != nil {
handleErr(err)
return err
}
w := bufio.NewWriter(f)
written, err := w.Write(body)
if err != nil {
handleErr(err)
return err
}
w.Flush()
fmt.Printf("Wrote %d bytes to disk to filename '%s'.\n", written, s3dest)
return err
}
func main() {
var function, region string
var s3bucket, s3key, s3dest string
var cwNamespace, cwMetricName, cwDimensions, cwAssumeRoleArn string
var cwValue float64
var versionFlag, verbose bool
flag.StringVar(®ion, "region", "us-east-1", "Region to use for all actions. Set as 'instance' to pull the instance's region from metadata.")
flag.StringVar(&function, "function", "s3download", "Which function to perform ('s3download' or 'cwputmetric'")
// process s3 vars
flag.StringVar(&s3bucket, "s3bucket", "my-bucket", "Name of bucket to get object from")
flag.StringVar(&s3key, "s3key", "/path/to/my/object", "Full key path to object")
flag.StringVar(&s3dest, "s3dest", "/path/on/disk", "path to destination on disk")
// now process cloudwatch vars
flag.StringVar(&cwNamespace, "cwnamespace", "my-namespace", "cloudwatch metric namespace")
flag.Float64Var(&cwValue, "cwvalue", 42, "cloudwatch metric value must be convertable to float64, i.e, no strings")
flag.StringVar(&cwMetricName, "cwmetricname", "my-metric", "cloudwatch metric name")
flag.StringVar(&cwDimensions, "cwdimensions", "Host=myhost,MetricSource=sesame-seed", "cloudwatch metric dimensions. Key value pairs separated by comma. ")
flag.StringVar(&cwAssumeRoleArn, "cwassumerolearn", "arn:aws:iam::123456789012:role/devopsdept/metrics-putter", "role to assume before attemping putmetric")
flag.BoolVar(&verbose, "verbose", false, "Verbose output")
flag.BoolVar(&versionFlag, "version", false, "Prints version and exits")
flag.Parse()
if versionFlag {
fmt.Printf("sesame-seed %s\n", version)
os.Exit(0)
}
cfg.verbosity = verbose
if region == "instance" {
svc := ec2metadata.New(session.New(), aws.NewConfig())
id, err := svc.GetInstanceIdentityDocument()
if err != nil {
handleErr(err)
panic(err)
}
region = id.Region
}
var err error
if cfg.verbosity {
fmt.Printf("Using region: '%s'\n", region)
}
switch function {
case "s3download":
if cfg.verbosity {
fmt.Println("Attempting to run functionS3Download...")
}
err = functionS3Download(region, s3bucket, s3key, s3dest)
if err != nil {
handleErr(err)
os.Exit(1)
}
os.Exit(0)
case "cwputmetric":
if cfg.verbosity {
fmt.Println("Attempting to run functionCWPutMetric...")
}
err = functionCWPutMetric(region, cwNamespace, cwMetricName, cwDimensions, cwAssumeRoleArn, cwValue)
if err != nil {
handleErr(err)
os.Exit(1)
}
os.Exit(0)
}
}