-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
716 lines (575 loc) · 21.4 KB
/
handlers.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optrefresh"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
log "github.com/sirupsen/logrus"
)
const filenameTimeFormat = "20060102T150405Z"
func createEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
type request struct {
environment
}
return func(c *gin.Context) {
var req request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := req.Validate(); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.SetDefaults(cfg)
ctx := c.Request.Context()
envName := req.Name
s, err := auto.NewStackInlineSource(ctx, envName, _projectName, infra(req.environment), opts...)
if err != nil {
// if stack already exists, 409
if auto.IsCreateStack409Error(err) {
c.JSON(http.StatusConflict, gin.H{"error": fmt.Sprintf("environment %q already exists", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
go func() {
start := time.Now()
sendToSlackWebHook(
fmt.Sprintf("Creating environment %q with %v services on Domain %q...", envName, len(req.RsbServices.Services), req.AwsServices.Route53.Domain),
req.SlackWebHook,
)
var msg strings.Builder
ctx := context.Background()
inMemoryStore := bytes.NewBuffer([]byte{})
gzipWriter, _ := gzip.NewWriterLevel(inMemoryStore, gzip.BestCompression)
defer func() {
gzipWriter.Close()
if err := uploadLogs(ctx, inMemoryStore, envName, "create", cfg, msg.String(), req.SlackWebHook, start); err != nil {
log.Error(err)
}
}()
res, err := s.Up(ctx, optup.ProgressStreams(os.Stdout, gzipWriter))
if err != nil {
log.Errorf("create env %q: %v", envName, err)
msg.WriteString(fmt.Sprintf("Error occured while creating environment %q", envName))
return
}
success := fmt.Sprintf("Created environment %q with %v services on Domain %q in %s", envName, len(req.RsbServices.Services), req.AwsServices.Route53.Domain, time.Since(start))
log.Infof(success)
msg.WriteString(success)
result := res.Outputs["result"].Value.(map[string]interface{})
msg.WriteString("\n" + createOverview(result))
}()
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("environment %q is being created", envName),
})
}
}
func listEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
type request struct {
environment
}
return func(c *gin.Context) {
var req request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.SetDefaults(cfg)
ctx := c.Request.Context()
ws, err := auto.NewLocalWorkspace(ctx, opts...)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
_ = ws.SetConfig(ctx, "", "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = ws.SetConfig(ctx, "", "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = ws.SetConfig(ctx, "", "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
stacks, err := ws.ListStacks(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"result": stacks,
})
}
}
func getEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(environment{}), opts...)
if err != nil {
// if stack doesn't already exist, 404
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
outs, err := s.Outputs(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
info, err := s.Info(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"result": gin.H{
"outputs": outs["result"].Value,
"info": info,
},
})
}
}
func exportEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(environment{}), opts...)
if err != nil {
// if stack doesn't already exist, 404
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
dep, err := s.Export(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if dep.Version != 3 {
c.JSON(http.StatusInternalServerError, gin.H{"error": "expected deployment version 3"})
return
}
c.JSON(http.StatusOK, gin.H{
"result": dep.Deployment,
})
}
}
func historyEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(environment{}), opts...)
if err != nil {
// if stack doesn't already exist, 404
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
page, _ := strconv.Atoi(c.DefaultQuery("page", "0"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("pagesize", "0"))
history, err := s.History(ctx, pageSize, page)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"result": history,
})
}
}
func previewEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
type request struct {
environment
}
return func(c *gin.Context) {
var req request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := req.environment.Validate(); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.environment.SetDefaults(cfg)
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(req.environment), opts...)
if err != nil {
// if stack doesn't already exist, 404
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
outs, err := s.Outputs(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
result, ok := outs["result"].Value.(map[string]interface{})
if !ok {
log.Errorf("can't retrieve stored output for env %q", envName)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving stored output. Try updating the environment again to regenerate the output."})
return
}
slackWebHook, ok := result["slack_webhook"].(string)
if !ok {
log.Warnf("can't retrieve slack webhook for env %q", envName)
}
domain, ok := result["domain"].(string)
if !ok {
log.Warnf("can't retrieve domain for env %q", envName)
}
go func() {
start := time.Now()
sendToSlackWebHook(
fmt.Sprintf("Previewing environment %q on Domain %q...", envName, domain),
slackWebHook,
)
var msg strings.Builder
ctx := context.Background()
inMemoryStore := bytes.NewBuffer([]byte{})
gzipWriter, _ := gzip.NewWriterLevel(inMemoryStore, gzip.BestCompression)
defer func() {
gzipWriter.Close()
if err := uploadLogs(ctx, inMemoryStore, envName, "preview", cfg, msg.String(), req.SlackWebHook, start); err != nil {
log.Error(err)
}
}()
_, err = s.Preview(ctx, optpreview.ProgressStreams(os.Stdout, gzipWriter))
if err != nil {
log.Errorf("preview env %q: %v", envName, err)
msg.WriteString(fmt.Sprintf("Error occured while previewing environment %q", envName))
return
}
success := fmt.Sprintf("Previewed environment %q on Domain %q in %s", envName, domain, time.Since(start))
log.Infof(success)
msg.WriteString(success)
}()
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("environment %q is being previewed", envName),
})
}
}
func refreshEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(environment{}), opts...)
if err != nil {
// if stack doesn't already exist, 404
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
outs, err := s.Outputs(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
result, ok := outs["result"].Value.(map[string]interface{})
if !ok {
log.Errorf("can't retrieve stored output for env %q", envName)
c.JSON(http.StatusInternalServerError, gin.H{"result": "Error retrieving stored output. Try updating the environment again to regenerate the output."})
return
}
slackWebHook, ok := result["slack_webhook"].(string)
if !ok {
log.Warnf("can't retrieve slack webhook for env %q", envName)
}
domain, ok := result["domain"].(string)
if !ok {
log.Warnf("can't retrieve domain for env %q", envName)
}
go func() {
start := time.Now()
sendToSlackWebHook(
fmt.Sprintf("Refreshing environment %q on Domain %q...", envName, domain),
slackWebHook,
)
var msg strings.Builder
ctx := context.Background()
inMemoryStore := bytes.NewBuffer([]byte{})
gzipWriter, _ := gzip.NewWriterLevel(inMemoryStore, gzip.BestCompression)
defer func() {
gzipWriter.Close()
if err := uploadLogs(ctx, inMemoryStore, envName, "refresh", cfg, msg.String(), slackWebHook, start); err != nil {
log.Error(err)
}
}()
_, err = s.Refresh(ctx, optrefresh.ProgressStreams(os.Stdout, gzipWriter))
if err != nil {
log.Errorf("refresh env %q: %v", envName, err)
msg.WriteString(fmt.Sprintf("Error occured while refreshing environment %q", envName))
return
}
success := fmt.Sprintf("Refreshed environment %q on Domain %q in %s", envName, domain, time.Since(start))
log.Infof(success)
msg.WriteString(success)
}()
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("environment %q is being refreshed", envName),
})
}
}
func importEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
type request struct {
apitype.DeploymentV3
}
return func(c *gin.Context) {
var req request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(environment{}), opts...)
if err != nil {
// if stack doesn't already exist, 404
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
outs, err := s.Outputs(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
result, ok := outs["result"].Value.(map[string]interface{})
if !ok {
log.Errorf("can't retrieve stored output for env %q", envName)
c.JSON(http.StatusInternalServerError, gin.H{"result": "Error retrieving stored output. Try updating the environment again to regenerate the output."})
return
}
slackWebHook, ok := result["slack_webhook"].(string)
if !ok {
log.Warnf("can't retrieve slack webhook for env %q", envName)
}
domain, ok := result["domain"].(string)
if !ok {
log.Warnf("can't retrieve domain for env %q", envName)
}
go func() {
start := time.Now()
sendToSlackWebHook(
fmt.Sprintf("Importing environment %q on Domain %q...", envName, domain),
slackWebHook,
)
marshalledStack, err := json.Marshal(req)
if err != nil {
log.Errorf("marshal stack env %q: %v", envName, err)
sendToSlackWebHook(fmt.Sprintf("Error occured while marshalling stack environment %q", envName), slackWebHook)
return
}
deployment := apitype.UntypedDeployment{
Version: 3,
Deployment: marshalledStack,
}
if err := s.Import(context.Background(), deployment); err != nil {
log.Errorf("import env %q: %v", envName, err)
sendToSlackWebHook(fmt.Sprintf("Error occured while importing environment %q", envName), slackWebHook)
return
}
msg := fmt.Sprintf("Imported environment %q on Domain %q in %s", envName, domain, time.Since(start))
log.Infof(msg)
sendToSlackWebHook(msg, slackWebHook)
}()
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("environment %q is being imported", envName),
})
}
}
func updateEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
type request struct {
environment
}
return func(c *gin.Context) {
var req request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := req.environment.Validate(); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.environment.SetDefaults(cfg)
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(req.environment), opts...)
if err != nil {
// if stack doesn't already exist, 404
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
go func() {
start := time.Now()
sendToSlackWebHook(
fmt.Sprintf("Updating environment %q with %v services on Domain %q...", envName, len(req.RsbServices.Services), req.AwsServices.Route53.Domain),
req.SlackWebHook,
)
var msg strings.Builder
ctx := context.Background()
inMemoryStore := bytes.NewBuffer([]byte{})
gzipWriter, _ := gzip.NewWriterLevel(inMemoryStore, gzip.BestCompression)
defer func() {
gzipWriter.Close()
if err := uploadLogs(ctx, inMemoryStore, envName, "update", cfg, msg.String(), req.SlackWebHook, start); err != nil {
log.Error(err)
}
}()
_, err = s.Up(ctx, optup.ProgressStreams(os.Stdout, gzipWriter))
if err != nil {
if auto.IsConcurrentUpdateError(err) {
msg.WriteString(fmt.Sprintf("Environment %q already has update in progress", envName))
return
}
log.Errorf("update env %q: %v", envName, err)
msg.WriteString(fmt.Sprintf("\nError occured while updating environment %q", envName))
return
}
success := fmt.Sprintf("Updated environment %q with %v services on Domain %q in %s", envName, len(req.RsbServices.Services), req.AwsServices.Route53.Domain, time.Since(start))
log.Infof(success)
msg.WriteString(success)
}()
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("environment %q is being updated", envName),
})
}
}
func deleteEnvironment(cfg config, opts ...auto.LocalWorkspaceOption) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
envName := c.Param("name")
s, err := auto.SelectStackInlineSource(ctx, envName, _projectName, infra(environment{}), opts...)
if err != nil {
if auto.IsSelectStack404Error(err) {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("environment %q not found", envName)})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Used for s3 backend storage for stack
_ = s.SetConfig(ctx, "aws:accessKey", auto.ConfigValue{Value: cfg.AWSAccessKeyID, Secret: true})
_ = s.SetConfig(ctx, "aws:secretKey", auto.ConfigValue{Value: cfg.AWSSecretAccessKey, Secret: true})
_ = s.SetConfig(ctx, "aws:region", auto.ConfigValue{Value: cfg.AWSRegion})
outs, err := s.Outputs(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
result, ok := outs["result"].Value.(map[string]interface{})
if !ok {
log.Errorf("can't retrieve stored output for env %q", envName)
c.JSON(http.StatusInternalServerError, gin.H{"result": "Error retrieving stored output. Try updating the environment again to regenerate the output."})
return
}
slackWebHook, ok := result["slack_webhook"].(string)
if !ok {
log.Warnf("can't retrieve slack webhook for env %q", envName)
}
domain, ok := result["domain"].(string)
if !ok {
log.Warnf("can't retrieve domain for env %q", envName)
}
go func() {
start := time.Now()
sendToSlackWebHook(
fmt.Sprintf("Deleting environment %q on Domain %q...", envName, domain),
slackWebHook,
)
var msg strings.Builder
ctx := context.Background()
inMemoryStore := bytes.NewBuffer([]byte{})
gzipWriter, _ := gzip.NewWriterLevel(inMemoryStore, gzip.BestCompression)
defer func() {
gzipWriter.Close()
if err := uploadLogs(ctx, inMemoryStore, envName, "delete", cfg, msg.String(), slackWebHook, start); err != nil {
log.Error(err)
}
}()
if _, err := s.Destroy(ctx, optdestroy.ProgressStreams(os.Stdout, gzipWriter)); err != nil {
log.Errorf("destroy env %q: %v", envName, err)
msg.WriteString(fmt.Sprintf("Error occured while deleting environment %q", envName))
return
}
if err = s.Workspace().RemoveStack(ctx, envName); err != nil {
log.Errorf("remove stack env %q: %v", envName, err)
msg.WriteString(fmt.Sprintf("Error occured while removing stack for environment %q", envName))
return
}
success := fmt.Sprintf("Deleted environment %q on Domain %q in %s", envName, domain, time.Since(start))
log.Infof(success)
msg.WriteString(success)
}()
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("environment %q is being deleted", envName),
})
}
}