-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks_start.go
92 lines (75 loc) · 2.44 KB
/
tasks_start.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/databasemigrationservice"
)
// startTasks will start all tasks stored in tasksFile
func startTasks(t string) {
counter := 0
// Create AWS session
s, err := session.NewSession()
if err != nil {
log.Fatal("Couldn't create AWS Session.")
}
// Create the AWS Service
svc := databasemigrationservice.New(s, &aws.Config{Region: ®ion})
// Read the defaults file
readTasks, err := ioutil.ReadFile(tasksFile)
if err != nil {
log.Fatal("Couldn't read file "+tasksFile, err)
}
// Create tasks and unmarshal the JSON
tasks := new([]ReplicationTask)
remainingTasks := new([]ReplicationTask) // Tasks that will be saved (if they couldn't be removed for example)
err = json.Unmarshal(readTasks, tasks)
if err != nil {
log.Fatal("Couldn't JSON unmarshal file "+tasksFile, err)
}
// Start all the tasks stored in tasks
for _, task := range *tasks {
params := &databasemigrationservice.StartReplicationTaskInput{
ReplicationTaskArn: aws.String(task.ReplicationTaskArn),
StartReplicationTaskType: &t,
}
_, err := svc.StartReplicationTask(params)
if err != nil {
// If the task doesn't exists we shouldn't keep it in the tasks.json file - just continue
if strings.Contains(err.Error(), doesntExists) {
continue
}
// If the task errored and not because it doesn't exists, keep it in the tasks.json file
*remainingTasks = append(*remainingTasks, task)
// Keep in switch so we can add more states - if needed ;)
switch {
case strings.Contains(err.Error(), cantBeStarted):
fmt.Println("Task can't be started", task.ReplicationTaskIdentifier, "in it's current state (still being created?)")
continue
}
fmt.Println("Couldn't start Replication Task", err)
continue
}
// If the task errored and not because it doesn't exists, keep it in the tasks.json file
*remainingTasks = append(*remainingTasks, task)
counter++
fmt.Println("Task started: " + task.ReplicationTaskIdentifier)
}
// If we have no tasks left, delete the whole file
switch {
case len(*remainingTasks) == 0:
err := os.Remove(tasksFile)
if err != nil {
fmt.Println("Couldn't remove tasks files", err)
}
default:
// Write remaining tasks to tasks-file
writeTaskFile(remainingTasks)
}
fmt.Println("\nDONE! Started", counter, "tasks.")
}