-
Notifications
You must be signed in to change notification settings - Fork 40
/
populate.go
121 lines (106 loc) · 3.53 KB
/
populate.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/sqs"
"os/exec"
"strings"
)
var mount_base = "/var/app/current/dufflebag"
// Goroutine thread to add one new SQS message with a snapshot ID
func populate(queue_url string, snapshot string) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
sqs_svc := sqs.New(sess, &aws.Config{
Region: aws.String(aws_region)})
_, err := sqs_svc.SendMessage(&sqs.SendMessageInput{
DelaySeconds: aws.Int64(0),
MessageBody: aws.String(snapshot),
QueueUrl: &queue_url,
})
if err != nil {
fmt.Printf("Error %s\n", err)
return
}
}
func main() {
fmt.Printf("Running populate...\n")
cmd := exec.Command("mkdir", "-p", mount_base)
_, cmderr := cmd.Output()
if cmderr != nil {
fmt.Printf("mkdir error: %s\n", cmderr.Error())
}
// Get a list of every public snapshot for the region
ec2_svc := ec2.New(session.New(), &aws.Config{
Region: aws.String(aws_region)})
input := &ec2.DescribeSnapshotsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("encrypted"),
Values: []*string{aws.String("false")},
},
},
}
snapshots_result, _ := ec2_svc.DescribeSnapshots(input)
if len(snapshots_result.Snapshots) == 0 {
fmt.Printf("ERROR: EBS list was empty. You probably didn't set up the IAM permissions correctly.\n")
}
sqssvc := sqs.New(session.New(), aws.NewConfig().WithRegion(aws_region))
queue_url := ""
params := &sqs.ListQueuesInput{}
sqs_resp, err := sqssvc.ListQueues(params)
if err != nil {
fmt.Printf("SQS ERROR: %s\n", err.Error())
return
}
for _, url := range sqs_resp.QueueUrls {
if strings.Contains(*url, "AWSEBWorkerQueue") {
queue_url = *url
}
}
snapshots := snapshots_result.Snapshots
//#####################################################################
//#### Safety Valve ####
//#### Remove this line of code below to search all of your region ####
//#####################################################################
snapshots = snapshots_result.Snapshots[0:20]
fmt.Printf("Using URL: %s\n", queue_url)
fmt.Printf("Adding %d volumes to the queue\n", len(snapshots))
// First, let's delete any existing messages in the queue. Could be left over from an old run
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
sqs_svc := sqs.New(sess, &aws.Config{
Region: aws.String(aws_region)})
params_purge := &sqs.PurgeQueueInput{
QueueUrl: aws.String(queue_url),
}
_, err_purge := sqs_svc.PurgeQueue(params_purge)
if err_purge != nil {
fmt.Printf("ERROR: Couldn't purge the queue. %s\n", err_purge.Error())
} else {
fmt.Printf("Purged the queues. Waiting a bit for it to finish...\n")
}
// Check if the queue is empty
for i := 0; i < 10; i++ {
result, _ := sqs_svc.ReceiveMessage(&sqs.ReceiveMessageInput{
WaitTimeSeconds: aws.Int64(1),
})
if len(result.Messages) == 0 {
break
}
}
fmt.Printf("Finished waiting for the purge. Pushing new items into the queue.\n")
for _, snapshot := range snapshots {
// Make doubly sure that the snapshot isn't encrypted
if *snapshot.Encrypted == false {
populate(queue_url, *snapshot.SnapshotId)
} else {
fmt.Printf("WARN: Skipping snapshot %s, it was encrypted. %t\n", *snapshot.SnapshotId, *snapshot.Encrypted == true)
}
}
fmt.Printf("Finished inserting into the queue.\n")
}