/
app.go
719 lines (676 loc) · 20 KB
/
app.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
717
718
719
package app
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"github.com/apppackio/apppack/auth"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/codebuild"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/google/uuid"
"github.com/logrusorgru/aurora"
)
var maxLifetime = 12 * 60 * 60
var waitForConnect = 60
var ShellBackgroundCommand = []string{
"/bin/sh",
"-c",
strings.Join([]string{
// Get initial proc count
"EXPECTED_PROCS=\"$(ls -1 /proc | grep -c [0-9])\"",
"STOP=$(($(date +%s)+" + fmt.Sprintf("%d", maxLifetime) + "))",
// Give user time to connect
"sleep " + fmt.Sprintf("%d", waitForConnect),
// Loop until procs are less than or equal to initial count
// As long as a user has a shell open, this task will keep running
"while true",
"do PROCS=\"$(ls -1 /proc | grep -c [0-9])\"",
"test \"$PROCS\" -le \"$EXPECTED_PROCS\" && exit",
// Timeout if exceeds max lifetime
"test \"$STOP\" -lt \"$(date +%s)\" && exit 1",
"sleep 30",
"done",
}, "; "),
}
// App is a representation of a AppPack app
type App struct {
Name string
Session *session.Session
Settings *Settings
ECSConfig *ECSConfig
DeployStatus *DeployStatus
PendingDeployStatuses []*DeployStatus
}
type settingsItem struct {
PrimaryID string `json:"primary_id"`
SecondaryID string `json:"secondary_id"`
Settings Settings `json:"value"`
}
type Settings struct {
Cluster struct {
ARN string `json:"arn"`
Name string `json:"name"`
} `json:"cluster"`
Domains []string `json:"domains"`
Shell struct {
Command string `json:"command"`
TaskFamily string `json:"task_family"`
} `json:"shell"`
DBUtils struct {
ShellTaskFamily string `json:"shell_task_family"`
S3Bucket string `json:"s3_bucket"`
DumpLoadTaskFamily string `json:"dumpload_task_family"`
Engine string `json:"engine"`
} `json:"dbutils"`
CodebuildProject struct {
Name string `json:"name"`
} `json:"codebuild_project"`
LogGroup struct {
Name string `json:"name"`
} `json:"log_group"`
}
type deployStatusItem struct {
PrimaryID string `json:"primary_id"`
SecondaryID string `json:"secondary_id"`
DeployStatus DeployStatus `json:"value"`
}
type DeployStatus struct {
Phase string `json:"phase"`
Processes map[string]Process `json:"processes"`
BuildID string `json:"build_id"`
LastUpdate int64 `json:"last_update"`
Commit string `json:"commit"`
BuildNumber int `json:"build_number"`
Failed bool `json:"failed"`
}
type Process struct {
CPU string `json:"cpu"`
Memory string `json:"memory"`
MinProcesses int `json:"min_processes"`
MaxProcesses int `json:"max_processes"`
State string `json:"state"`
Command string `json:"command"`
}
type appItem struct {
PrimaryID string `json:"primary_id"`
SecondaryID string `json:"secondary_id"`
App App `json:"value"`
}
// locationName is the tag used by aws-sdk internally
// we can use it to load specific AWS Input types from our JSON
type ecsConfigItem struct {
PrimaryID string `locationName:"primary_id"`
SecondaryID string `locationName:"secondary_id"`
ECSConfig ECSConfig `locationName:"value"`
}
type ECSConfig struct {
RunTaskArgs ecs.RunTaskInput `locationName:"run_task_args"`
RunTaskArgsFargate ecs.RunTaskInput `locationName:"run_task_args_fargate"`
}
func ddbItem(sess *session.Session, primaryID string, secondaryID string) (*map[string]*dynamodb.AttributeValue, error) {
ddbSvc := dynamodb.New(sess)
result, err := ddbSvc.GetItem(&dynamodb.GetItemInput{
TableName: aws.String("apppack"),
Key: map[string]*dynamodb.AttributeValue{
"primary_id": {
S: aws.String(primaryID),
},
"secondary_id": {
S: aws.String(secondaryID),
},
},
})
if err != nil {
return nil, err
}
if result.Item == nil {
return nil, fmt.Errorf("Could not find DDB item %s %s", primaryID, secondaryID)
}
return &result.Item, nil
}
func (a *App) ddbItem(key string) (*map[string]*dynamodb.AttributeValue, error) {
return ddbItem(a.Session, fmt.Sprintf("APP#%s", a.Name), key)
}
// LoadECSConfig will set the app.ECSConfig value from DDB
func (a *App) LoadECSConfig() error {
if a.ECSConfig != nil {
return nil
}
Item, err := a.ddbItem("CONFIG#ecs")
if err != nil {
return err
}
i := ecsConfigItem{}
err = dynamodbattribute.NewDecoder(func(d *dynamodbattribute.Decoder) {
d.TagKey = "locationName"
}).Decode(&dynamodb.AttributeValue{M: *Item}, &i)
if err != nil {
return err
}
a.ECSConfig = &i.ECSConfig
return nil
}
// GetDeployStatus will get a DeployStatus value from DDB
func (a *App) GetDeployStatus(buildARN string) (*DeployStatus, error) {
key := "DEPLOYSTATUS"
if buildARN != "" {
key = strings.Join([]string{key, buildARN}, "#")
}
Item, err := a.ddbItem(key)
if err != nil {
return nil, err
}
i := deployStatusItem{}
err = dynamodbattribute.UnmarshalMap(*Item, &i)
if err != nil {
return nil, err
}
return &i.DeployStatus, nil
}
// LoadDeployStatus will set the app.DeployStatus value from DDB
func (a *App) LoadDeployStatus() error {
if a.DeployStatus != nil {
return nil
}
deployStatus, err := a.GetDeployStatus("")
if err != nil {
return err
}
a.DeployStatus = deployStatus
return nil
}
// LoadSettings will set the app.Settings value from DDB
func (a *App) LoadSettings() error {
if a.Settings != nil {
return nil
}
Item, err := a.ddbItem("settings")
if err != nil {
return err
}
i := settingsItem{}
err = dynamodbattribute.UnmarshalMap(*Item, &i)
if err != nil {
return err
}
a.Settings = &i.Settings
return nil
}
// StartTask start a new task on ECS
func (a *App) StartTask(taskFamily *string, command []string, fargate bool) (*ecs.RunTaskOutput, error) {
ecsSvc := ecs.New(a.Session)
err := a.LoadSettings()
if err != nil {
return nil, err
}
err = a.LoadECSConfig()
if err != nil {
return nil, err
}
taskDefn, err := ecsSvc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: taskFamily,
})
if err != nil {
return nil, err
}
var runTaskArgs ecs.RunTaskInput
if fargate {
runTaskArgs = a.ECSConfig.RunTaskArgsFargate
} else {
runTaskArgs = a.ECSConfig.RunTaskArgs
}
cmd := []*string{}
for i := range command {
cmd = append(cmd, &command[i])
}
email, err := auth.WhoAmI()
if err != nil {
return nil, err
}
startedBy := fmt.Sprintf("apppack-cli/shell/%s", *email)
runTaskArgs.TaskDefinition = taskDefn.TaskDefinition.TaskDefinitionArn
runTaskArgs.StartedBy = &startedBy
runTaskArgs.Overrides = &ecs.TaskOverride{
ContainerOverrides: []*ecs.ContainerOverride{
{
Name: taskDefn.TaskDefinition.ContainerDefinitions[0].Name,
Command: cmd,
},
},
}
return ecsSvc.RunTask(&runTaskArgs)
}
// WaitForTaskRunning waits for a task to be running or complete
func (a *App) WaitForTaskRunning(task *ecs.Task) error {
ecsSvc := ecs.New(a.Session)
return ecsSvc.WaitUntilTasksRunning(&ecs.DescribeTasksInput{
Cluster: task.ClusterArn,
Tasks: []*string{task.TaskArn},
})
}
// WaitForTaskStopped waits for a task to be running or complete
func (a *App) WaitForTaskStopped(task *ecs.Task) error {
ecsSvc := ecs.New(a.Session)
input := ecs.DescribeTasksInput{
Cluster: task.ClusterArn,
Tasks: []*string{task.TaskArn},
}
err := ecsSvc.WaitUntilTasksStopped(&input)
if err != nil {
return err
}
taskDesc, err := ecsSvc.DescribeTasks(&input)
if err != nil {
return err
}
task = taskDesc.Tasks[0]
if *task.StopCode != "EssentialContainerExited" {
return fmt.Errorf("task %s failed %s: %s", *task.TaskArn, *task.StopCode, *task.StoppedReason)
}
if *task.Containers[0].ExitCode > 0 {
return fmt.Errorf("task %s failed with exit code %d", *task.TaskArn, *task.Containers[0].ExitCode)
}
return nil
}
// ConnectToTask open a SSM Session to the Docker host and exec into container
func (a *App) ConnectToTask(task *ecs.Task, cmd *string) error {
binaryPath, err := exec.LookPath("session-manager-plugin")
if err != nil {
fmt.Println(aurora.Red("AWS Session Manager plugin was not found on the path. Install it locally to use this feature."))
fmt.Println(aurora.White("https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"))
os.Exit(1)
}
ecsSvc := ecs.New(a.Session)
resp, err := ecsSvc.DescribeContainerInstances(&ecs.DescribeContainerInstancesInput{
Cluster: task.ClusterArn,
ContainerInstances: []*string{task.ContainerInstanceArn},
})
ssmSvc := ssm.New(a.Session)
documentName := "AWS-StartInteractiveCommand"
region := a.Session.Config.Region
err = a.LoadSettings()
if err != nil {
return err
}
command := fmt.Sprintf("docker exec -it $(docker ps -q -f label=com.amazonaws.ecs.task-arn=%s) %s", *task.TaskArn, *cmd)
input := ssm.StartSessionInput{
DocumentName: &documentName,
Target: resp.ContainerInstances[0].Ec2InstanceId,
Parameters: map[string][]*string{"command": {&command}},
}
startSessionResp, err := ssmSvc.StartSession(&input)
if err != nil {
return err
}
arg1, err := json.Marshal(startSessionResp)
if err != nil {
return err
}
arg2, err := json.Marshal(input)
if err != nil {
return err
}
// session-manager-plugin isn't documented
// args were determined from here: https://github.com/aws/aws-cli/blob/84f751b71131489afcb5401d8297bb5b3faa29cb/awscli/customizations/sessionmanager.py#L83-L89
err = syscall.Exec(binaryPath, []string{"session-manager-plugin", string(arg1), *region, "StartSession", "", string(arg2), fmt.Sprintf("https://ssm.%s.amazonaws.com", *region)}, os.Environ())
if err != nil {
return err
}
return nil
}
// StartBuild starts a new CodeBuild run
func (a *App) StartBuild() (*codebuild.Build, error) {
codebuildSvc := codebuild.New(a.Session)
err := a.LoadSettings()
if err != nil {
return nil, err
}
build, err := codebuildSvc.StartBuild(&codebuild.StartBuildInput{
ProjectName: &a.Settings.CodebuildProject.Name,
})
return build.Build, err
}
// BuildStatus checks the status of a CodeBuild run
func (a *App) BuildStatus(build *codebuild.Build) (*DeployStatus, error) {
deployStatus, err := a.GetDeployStatus(*build.Arn)
if err != nil {
deployStatus, err = a.GetDeployStatus("")
if err != nil || deployStatus.BuildID != *build.Arn {
return nil, fmt.Errorf("no status found for build #%d", *build.BuildNumber)
}
}
if deployStatus.Failed {
return nil, fmt.Errorf("build #%d failed in phase %s", *build.BuildNumber, deployStatus.Phase)
}
return deployStatus, nil
}
// ListBuilds lists recent CodeBuild runs
func (a *App) ListBuilds() ([]*codebuild.Build, error) {
codebuildSvc := codebuild.New(a.Session)
err := a.LoadSettings()
if err != nil {
return nil, err
}
buildList, err := codebuildSvc.ListBuildsForProject(&codebuild.ListBuildsForProjectInput{
ProjectName: &a.Settings.CodebuildProject.Name,
})
if err != nil {
return nil, err
}
builds, err := codebuildSvc.BatchGetBuilds(&codebuild.BatchGetBuildsInput{
Ids: buildList.Ids,
})
if err != nil {
return nil, err
}
return builds.Builds, nil
}
// LastBuild retrieves the most recent build
func (a *App) LastBuild() (*codebuild.Build, error) {
codebuildSvc := codebuild.New(a.Session)
err := a.LoadSettings()
if err != nil {
return nil, err
}
buildList, err := codebuildSvc.ListBuildsForProject(&codebuild.ListBuildsForProjectInput{
ProjectName: &a.Settings.CodebuildProject.Name,
SortOrder: aws.String("DESCENDING"),
})
if err != nil {
return nil, err
}
if len(buildList.Ids) == 0 {
return nil, fmt.Errorf("no builds have started for %s", a.Name)
}
builds, err := codebuildSvc.BatchGetBuilds(&codebuild.BatchGetBuildsInput{
Ids: buildList.Ids[0:1],
})
if err != nil {
return nil, err
}
return builds.Builds[0], nil
}
// GetBuildArtifact retrieves an artifact stored in S3
func (a *App) GetBuildArtifact(build *codebuild.Build, name string) ([]byte, error) {
artifactArn := build.Artifacts.Location
if artifactArn == nil {
return []byte{}, nil
}
s3Path := strings.Join(strings.Split(*artifactArn, ":")[5:], ":")
pathParts := strings.Split(s3Path, "/")
s3Svc := s3.New(a.Session)
obj, err := s3Svc.GetObject(&s3.GetObjectInput{
Bucket: &pathParts[0],
Key: aws.String(strings.Join(append(pathParts[1:], name), "/")),
})
if err != nil {
return []byte{}, err
}
return ioutil.ReadAll(obj.Body)
}
// GetConfig returns a list of config parameters for the app
func (a *App) GetConfig() ([]*ssm.Parameter, error) {
ssmSvc := ssm.New(a.Session)
var parameters []*ssm.Parameter
input := ssm.GetParametersByPathInput{
Path: aws.String(fmt.Sprintf("/apppack/apps/%s/config/", a.Name)),
WithDecryption: aws.Bool(true),
}
err := ssmSvc.GetParametersByPathPages(&input, func(resp *ssm.GetParametersByPathOutput, lastPage bool) bool {
for _, parameter := range resp.Parameters {
if parameter == nil {
continue
}
parameters = append(parameters, parameter)
}
return !lastPage
})
if err != nil {
return nil, err
}
return parameters, nil
}
// SetConfig sets a config value for the app
func (a *App) SetConfig(key string, value string, overwrite bool) error {
ssmSvc := ssm.New(a.Session)
_, err := ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: aws.String(fmt.Sprintf("/apppack/apps/%s/config/%s", a.Name, key)),
Type: aws.String("SecureString"),
Overwrite: &overwrite,
Value: &value,
})
return err
}
// GetConsoleURL generate a URL which will sign the user in to the AWS console and redirect to the desinationURL
func (a *App) GetConsoleURL(destinationURL string) (*string, error) {
return auth.GetConsoleURL(a.Session, destinationURL)
}
// DescribeTasks generate a URL which will sign the user in to the AWS console and redirect to the desinationURL
func (a *App) DescribeTasks() ([]*ecs.Task, error) {
err := a.LoadSettings()
if err != nil {
return nil, err
}
err = a.LoadDeployStatus()
if err != nil {
return nil, err
}
ecsSvc := ecs.New(a.Session)
taskARNs := []*string{}
for proc := range a.DeployStatus.Processes {
listTaskOutput, err := ecsSvc.ListTasks(&ecs.ListTasksInput{
Family: aws.String(fmt.Sprintf("%s-%s", a.Name, proc)),
Cluster: &a.Settings.Cluster.ARN,
})
if err != nil {
return nil, err
}
taskARNs = append(taskARNs, listTaskOutput.TaskArns...)
}
describeTasksOutput, err := ecsSvc.DescribeTasks(&ecs.DescribeTasksInput{
Tasks: taskARNs,
Cluster: &a.Settings.Cluster.ARN,
Include: []*string{aws.String("TAGS")},
})
if err != nil {
return nil, err
}
return describeTasksOutput.Tasks, nil
}
type Scaling struct {
CPU int `json:"cpu"`
Memory int `json:"memory"`
MinProcesses int `json:"min_processes"`
MaxProcesses int `json:"max_processes"`
}
func (a *App) ResizeProcess(processType string, cpu int, memory int) error {
ssmSvc := ssm.New(a.Session)
parameterName := fmt.Sprintf("/apppack/apps/%s/scaling", a.Name)
parameterOutput, err := ssmSvc.GetParameter(&ssm.GetParameterInput{
Name: ¶meterName,
})
var scaling map[string]*Scaling
if err != nil {
scaling = map[string]*Scaling{}
} else {
if err = json.Unmarshal([]byte(*parameterOutput.Parameter.Value), &scaling); err != nil {
return err
}
}
_, ok := scaling[processType]
if !ok {
scaling[processType] = &Scaling{
CPU: 1024,
Memory: 2048,
MinProcesses: 1,
MaxProcesses: 1,
}
}
scaling[processType].CPU = cpu
scaling[processType].Memory = memory
scalingJSON, err := json.Marshal(scaling)
if err != nil {
return err
}
_, err = ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: ¶meterName,
Type: aws.String("String"),
Value: aws.String(fmt.Sprintf("%s", scalingJSON)),
Overwrite: aws.Bool(true),
})
if err != nil {
return err
}
return nil
}
func (a *App) ScaleProcess(processType string, minProcessCount int, maxProcessCount int) error {
ssmSvc := ssm.New(a.Session)
parameterName := fmt.Sprintf("/apppack/apps/%s/scaling", a.Name)
parameterOutput, err := ssmSvc.GetParameter(&ssm.GetParameterInput{
Name: ¶meterName,
})
var scaling map[string]*Scaling
if err != nil {
scaling = map[string]*Scaling{}
} else {
if err = json.Unmarshal([]byte(*parameterOutput.Parameter.Value), &scaling); err != nil {
return err
}
}
_, ok := scaling[processType]
if !ok {
scaling[processType] = &Scaling{
CPU: 1024,
Memory: 2048,
MinProcesses: 1,
MaxProcesses: 1,
}
}
scaling[processType].MinProcesses = minProcessCount
scaling[processType].MaxProcesses = maxProcessCount
scalingJSON, err := json.Marshal(scaling)
if err != nil {
return err
}
_, err = ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: ¶meterName,
Type: aws.String("String"),
Value: aws.String(fmt.Sprintf("%s", scalingJSON)),
Overwrite: aws.Bool(true),
})
if err != nil {
return err
}
return nil
}
type ScheduledTask struct {
Schedule string `json:"schedule"`
Command string `json:"command"`
}
// ScheduledTasks lists scheduled tasks for the app
func (a *App) ScheduledTasks() ([]*ScheduledTask, error) {
ssmSvc := ssm.New(a.Session)
parameterName := fmt.Sprintf("/apppack/apps/%s/scheduled-tasks", a.Name)
parameterOutput, err := ssmSvc.GetParameter(&ssm.GetParameterInput{
Name: ¶meterName,
})
var tasks []*ScheduledTask
if err != nil {
tasks = []*ScheduledTask{}
} else {
if err = json.Unmarshal([]byte(*parameterOutput.Parameter.Value), &tasks); err != nil {
return nil, err
}
}
return tasks, nil
}
// CreateScheduledTask adds a scheduled task for the app
func (a *App) CreateScheduledTask(schedule string, command string) ([]*ScheduledTask, error) {
if err := a.ValidateCronString(schedule); err != nil {
return nil, err
}
ssmSvc := ssm.New(a.Session)
tasks, err := a.ScheduledTasks()
if err != nil {
return nil, err
}
tasks = append(tasks, &ScheduledTask{
Schedule: schedule,
Command: command,
})
tasksBytes, err := json.Marshal(tasks)
if err != nil {
return nil, err
}
parameterName := fmt.Sprintf("/apppack/apps/%s/scheduled-tasks", a.Name)
_, err = ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: ¶meterName,
Value: aws.String(fmt.Sprintf("%s", tasksBytes)),
Overwrite: aws.Bool(true),
Type: aws.String("String"),
})
return tasks, nil
}
// DeleteScheduledTask deletes the scheduled task at the given index
func (a *App) DeleteScheduledTask(idx int) (*ScheduledTask, error) {
ssmSvc := ssm.New(a.Session)
tasks, err := a.ScheduledTasks()
if err != nil {
return nil, err
}
if idx > len(tasks) || idx < 0 {
return nil, fmt.Errorf("invalid index for task to delete")
}
taskToDelete := tasks[idx]
tasks = append(tasks[:idx], tasks[idx+1:]...)
tasksBytes, err := json.Marshal(tasks)
if err != nil {
return nil, err
}
parameterName := fmt.Sprintf("/apppack/apps/%s/scheduled-tasks", a.Name)
_, err = ssmSvc.PutParameter(&ssm.PutParameterInput{
Name: ¶meterName,
Value: aws.String(fmt.Sprintf("%s", tasksBytes)),
Overwrite: aws.Bool(true),
Type: aws.String("String"),
})
return taskToDelete, nil
}
// ValidateCronString validates a cron schedule rule
func (a *App) ValidateCronString(rule string) error {
eventSvc := eventbridge.New(a.Session)
ruleName := fmt.Sprintf("apppack-validate-%s", uuid.New().String())
_, err := eventSvc.PutRule(&eventbridge.PutRuleInput{
Name: &ruleName,
ScheduleExpression: aws.String(fmt.Sprintf("cron(%s)", rule)),
State: aws.String("DISABLED"),
})
if err == nil {
eventSvc.DeleteRule(&eventbridge.DeleteRuleInput{
Name: &ruleName,
})
}
return err
}
// Init will pull in app settings from DyanmoDB and provide helper
func Init(name string) (*App, error) {
sess, err := auth.AwsSession(name)
if err != nil {
return nil, err
}
app := App{
Name: name,
Session: sess,
}
return &app, nil
}