This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
384 lines (329 loc) · 9.27 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/dchest/uniuri"
lssqs "github.com/flowerinthenight/longsub/awssqs"
lspubsub "github.com/flowerinthenight/longsub/gcppubsub"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
)
var (
rootcmd = &cobra.Command{
Use: "oops",
Short: "k8s-native testing tool",
Long: "Kubernetes-native testing tool.",
RunE: runE,
}
project string
pubsub string
region string
key string
secret string
rolearn string
snssqs string
files []string
dir string
tags []string
repslack string
reppubsub string
verbose bool
)
type cmd struct {
// Valid values: start | process
// start = initiate distribution of files in --dir to SNS
// process = normal processing (one yaml at a time)
Code string `json:"code"`
// To identify a batch. Sent by the initiator together with
// the 'process' code.
ID string `json:"id"`
// The file to process. Sent together with the 'process' code.
Scenario string `json:"scenario"`
}
func runE(cmd *cobra.Command, args []string) error {
return doScenario(&doScenarioInput{
ScenarioFiles: combineFilesAndDir(),
ReportSlack: repslack,
ReportPubsub: reppubsub,
Verbose: verbose,
})
}
func combineFilesAndDir() []string {
tmp := make(map[string]struct{})
for _, v := range files {
f, _ := filepath.Abs(v)
tmp[f] = struct{}{}
}
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
f, _ := filepath.Abs(path)
log.Printf("input: %v", f)
if strings.HasSuffix(f, ".yaml") {
tmp[f] = struct{}{}
}
return nil
})
var final []string
for k := range tmp {
_, err := os.Stat(k)
if os.IsNotExist(err) {
log.Printf("File does not exist: %v", k)
} else {
final = append(final, k)
}
}
if len(final) == 0 {
log.Fatal("No files found. Please recheck directory.")
}
return final
}
func distributePubsub(app *appctx) {
id := fmt.Sprintf("%s", uuid.NewV4())
final := combineFilesAndDir()
for _, f := range final {
nc := cmd{
Code: "process",
ID: id,
Scenario: f,
}
err := app.pub.Publish(uniuri.NewLen(10), nc)
if err != nil {
log.Printf("publish failed: %v ", err)
continue
}
}
}
func distributeSQS(app *appctx) {
sess, _ := session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(key, secret, ""),
})
var svc *sns.SNS
if rolearn != "" {
cnf := &aws.Config{Credentials: stscreds.NewCredentials(sess, rolearn)}
svc = sns.New(sess, cnf)
} else {
svc = sns.New(sess)
}
id := fmt.Sprintf("%s", uuid.NewV4())
final := combineFilesAndDir()
for _, f := range final {
nc := cmd{
Code: "process",
ID: id,
Scenario: f,
}
b, _ := json.Marshal(nc)
key := uniuri.NewLen(10)
m := &sns.PublishInput{
TopicArn: app.topicArn,
Subject: &key,
Message: aws.String(string(b)),
}
_, err := svc.Publish(m)
if err != nil {
log.Printf("Publish failed: %v", err)
continue
}
}
}
type appctx struct {
pub *lspubsub.PubsubPublisher // starter publisher topic
rpub *lspubsub.PubsubPublisher // topic to publish reports
mtx *sync.Mutex
topicArn *string
}
// Our message processing callback.
func process(ctx interface{}, data []byte) error {
app := ctx.(*appctx)
app.mtx.Lock()
defer app.mtx.Unlock()
var c cmd
err := json.Unmarshal(data, &c)
if err != nil {
log.Printf("Unmarshal failed: %v", err)
return err
}
switch {
case c.Code == "start":
var dist string
switch {
case pubsub != "":
distributePubsub(app)
dist = fmt.Sprintf("pubsub=%v", pubsub)
case snssqs != "":
distributeSQS(app)
dist = snssqs
dist = fmt.Sprintf("sns/sqs=%v", snssqs)
}
host, _ := os.Hostname()
// Send to slack, if any.
if repslack != "" {
payload := SlackMessage{
Attachments: []SlackAttachment{
{
Color: "good",
Title: "start tests",
Text: fmt.Sprintf("from %v through %v", host, dist),
Footer: "oops",
Timestamp: time.Now().Unix(),
},
},
}
err = payload.Notify(repslack)
if err != nil {
log.Printf("Notify (slack) failed: %v", err)
}
}
case c.Code == "process":
log.Printf("process: %+v", c)
doScenario(&doScenarioInput{
app: app,
ScenarioFiles: []string{c.Scenario},
ReportSlack: repslack,
ReportPubsub: reppubsub,
Verbose: verbose,
})
}
return nil
}
func run(ctx context.Context, done chan error) {
var err error
if snssqs != "" && pubsub != "" {
log.Fatal("cannot set both --sns-sqs and --pubsub")
}
log.Printf("rootdir: %v", dir)
log.Printf("report-slack: %v", repslack)
if pubsub != "" {
log.Printf("project: %v", project)
log.Printf("svcacct: %v", os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"))
}
if snssqs != "" {
log.Printf("region: %v", region)
log.Printf("key: %v", key)
log.Printf("rolearn: %v", rolearn)
}
app := &appctx{mtx: &sync.Mutex{}}
ctx0, cancelCtx0 := context.WithCancel(ctx)
defer cancelCtx0()
done0 := make(chan error, 1)
switch {
case pubsub != "":
// Setup reports publisher topic, if provided.
if reppubsub != "" {
app.rpub, err = lspubsub.NewPubsubPublisher(project, reppubsub)
if err != nil {
log.Fatalf("create publisher %v failed: %v", reppubsub, err)
}
}
// Make sure topic/subscription is created. Only used for creating subscription if needed.
_, t, err := lspubsub.GetPublisher(project, pubsub)
if err != nil {
log.Fatalf("publisher get/create for %v failed: %v", pubsub, err)
}
app.pub, err = lspubsub.NewPubsubPublisher(project, pubsub)
if err != nil {
log.Fatalf("create publisher %v failed: %v", pubsub, err)
}
if app.pub == nil {
log.Fatalf("fatal error, publisher nil")
}
_, err = lspubsub.GetSubscription(project, pubsub, t, time.Second*60)
if err != nil {
log.Fatalf("subscription get/create for %v failed: %v", pubsub, err)
}
go func() {
// Messages should be payer level. We will subdivide linked accts to separate messages for
// linked-acct-level processing.
ls := lspubsub.NewLengthySubscriber(app, project, pubsub, process)
err = ls.Start(ctx0, done0)
if err != nil {
log.Fatalf("listener for export csv failed: %v", err)
}
}()
case snssqs != "":
lsh := lssqs.NewHelper(region, key, secret, rolearn)
t, err := lsh.SetupSnsSqsSubscription(snssqs, snssqs)
if err != nil {
log.Fatal(err)
}
app.topicArn = t
log.Printf("%v subscribed to %v", snssqs, snssqs)
go func() {
ls := lssqs.NewLengthySubscriber(app, snssqs, process,
lssqs.WithRegion(region),
lssqs.WithAccessKeyId(key),
lssqs.WithSecretAccessKey(secret),
lssqs.WithRoleArn(rolearn),
)
err := ls.Start(ctx0, done0)
if err != nil {
log.Fatalf("start long processing for %v failed: %v", snssqs, err)
}
}()
}
<-ctx.Done()
done <- <-done0
}
func runCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Run as service",
Long: "Run oops as a long-running service.",
RunE: func(cmd *cobra.Command, args []string) error {
defer func(begin time.Time) {
log.Printf("stop oops after %v", time.Since(begin))
}(time.Now())
log.Printf("start oops on %v", time.Now())
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error)
go run(ctx, done)
go func() {
sigch := make(chan os.Signal)
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
log.Println(<-sigch)
cancel()
}()
return <-done
},
}
cmd.Flags().StringVar(&snssqs, "snssqs", snssqs, "name of the SNS topic and SQS queue")
cmd.Flags().StringVar(&pubsub, "pubsub", pubsub, "name of the GCP pubsub and subscription")
return cmd
}
func init() {
rootcmd.PersistentFlags().StringVar(&project, "project-id", os.Getenv("GCP_PROJECT_ID"), "GCP project id")
rootcmd.PersistentFlags().StringVar(®ion, "region", os.Getenv("AWS_REGION"), "AWS region")
rootcmd.PersistentFlags().StringVar(&key, "aws-key", os.Getenv("AWS_ACCESS_KEY_ID"), "AWS access key")
rootcmd.PersistentFlags().StringVar(&secret, "aws-secret", os.Getenv("AWS_SECRET_ACCESS_KEY"), "AWS secret key")
rootcmd.PersistentFlags().StringVar(&rolearn, "aws-rolearn", os.Getenv("ROLE_ARN"), "AWS role ARN to assume")
rootcmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", verbose, "verbose mode")
rootcmd.PersistentFlags().StringVarP(&dir, "dir", "d", dir, "root directory for scenario file[s]")
rootcmd.PersistentFlags().StringVar(&repslack, "report-slack", repslack, "slack url for notification")
rootcmd.PersistentFlags().StringVar(&reppubsub, "report-pubsub", reppubsub, "pubsub topic for notification")
rootcmd.PersistentFlags().StringSliceVarP(&files, "scenarios", "s", files, "scenario file[s] to run, comma-separated, or multiple -s")
rootcmd.PersistentFlags().StringSliceVarP(&tags, "tags", "t", tags, "key=value labels in scenario files that are allowed to run, empty means all")
rootcmd.AddCommand(runCmd())
}
func main() {
log.SetFlags(0)
log.SetPrefix("[oops] ")
log.SetOutput(os.Stdout)
rootcmd.Execute()
}