-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
289 lines (230 loc) · 8.69 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
package main
import (
"fmt"
"strconv"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/fatih/color"
"github.com/mercury2269/sqsmover/platform"
"github.com/tj/go-progress"
"github.com/tj/go/term"
"gopkg.in/alecthomas/kingpin.v2"
)
// nolint: gochecknoglobals
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
)
var (
sourceQueue = kingpin.Flag("source", "The source queue name to move messages from.").Short('s').Required().String()
destinationQueue = kingpin.Flag("destination", "The destination queue name to move messages to.").Short('d').Required().String()
region = kingpin.Flag("region", "The AWS region for source and destination queues.").Short('r').Default("").String()
endpoint = kingpin.Flag("endpoint", "Use a specific endpoint in an AWS region.").Short('e').Default("").String()
profile = kingpin.Flag("profile", "Use a specific profile from AWS credentials file.").Short('p').String()
limit = kingpin.Flag("limit", "Limits total number of messages moved. No limit is set by default.").Short('l').Default("0").Int()
maxBatchSize = kingpin.Flag("batch", "The maximum number of messages to move at a time").Short('b').Default("10").Int64()
deleteFromSource = kingpin.Flag("delete", "Delete messages from source queue").Default("true").Bool()
)
func main() {
platform.EnableVirtualTerminal()
log.SetHandler(cli.Default)
fmt.Println()
defer fmt.Println()
kingpin.Version(buildVersion(version, commit, date, builtBy))
kingpin.UsageTemplate(kingpin.CompactUsageTemplate)
kingpin.CommandLine.VersionFlag.Short('v')
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
options := session.Options{
Profile: *profile,
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
}
if region != nil {
options.Config.Region = aws.String(*region)
}
// Our default "" value uses the AWS auto generated value
options.Config.Endpoint = aws.String(*endpoint)
if *sourceQueue == *destinationQueue {
log.Error(color.New(color.FgRed).Sprintf("Source and destination queues are the same \r\n"))
return
}
sess, err := session.NewSessionWithOptions(options)
if err != nil {
log.Error(color.New(color.FgRed).Sprintf("Unable to create AWS session for region %s \r\n", *region))
return
}
svc := sqs.New(sess)
sourceQueueUrl, err := resolveQueueUrl(svc, *sourceQueue)
if err != nil {
logAwsError("Failed to resolve source queue", err)
return
}
log.Info(color.New(color.FgCyan).Sprintf("Source queue URL: %s", sourceQueueUrl))
destinationQueueUrl, err := resolveQueueUrl(svc, *destinationQueue)
if err != nil {
logAwsError("Failed to resolve destination queue", err)
return
}
log.Info(color.New(color.FgCyan).Sprintf("Destination queue URL: %s", destinationQueueUrl))
queueAttributes, err := svc.GetQueueAttributes(&sqs.GetQueueAttributesInput{
QueueUrl: aws.String(sourceQueueUrl),
AttributeNames: []*string{aws.String("All")},
})
if err != nil {
logAwsError("Failed to resolve queue attributes", err)
return
}
numberOfMessages, _ := strconv.Atoi(*queueAttributes.Attributes["ApproximateNumberOfMessages"])
log.Info(color.New(color.FgCyan).Sprintf("Approximate number of messages in the source queue: %d", numberOfMessages))
if numberOfMessages == 0 {
log.Info("Looks like nothing to move. Done.")
return
}
if *limit > 0 && numberOfMessages > *limit {
numberOfMessages = *limit
log.Info(color.New(color.FgCyan).Sprintf("Limit is set, will only move %d messages", numberOfMessages))
}
moveMessages(sourceQueueUrl, destinationQueueUrl, svc, numberOfMessages)
}
func resolveQueueUrl(svc *sqs.SQS, queueName string) (string, error) {
params := &sqs.GetQueueUrlInput{
QueueName: aws.String(queueName),
}
resp, err := svc.GetQueueUrl(params)
if err != nil {
return "", err
}
return *resp.QueueUrl, nil
}
func logAwsError(message string, err error) {
if awsErr, ok := err.(awserr.Error); ok {
log.Error(color.New(color.FgRed).Sprintf("%s. Error: %s", message, awsErr.Message()))
} else {
log.Error(color.New(color.FgRed).Sprintf("%s. Error: %s", message, err.Error()))
}
}
func convertToEntries(messages []*sqs.Message) []*sqs.SendMessageBatchRequestEntry {
result := make([]*sqs.SendMessageBatchRequestEntry, len(messages))
for i, message := range messages {
result[i] = &sqs.SendMessageBatchRequestEntry{
MessageBody: message.Body,
Id: message.MessageId,
MessageAttributes: message.MessageAttributes,
MessageGroupId: message.Attributes[sqs.MessageSystemAttributeNameMessageGroupId],
MessageDeduplicationId: message.Attributes[sqs.MessageSystemAttributeNameMessageDeduplicationId],
}
}
return result
}
func convertSuccessfulMessageToBatchRequestEntry(messages []*sqs.Message) []*sqs.DeleteMessageBatchRequestEntry {
result := make([]*sqs.DeleteMessageBatchRequestEntry, len(messages))
for i, message := range messages {
result[i] = &sqs.DeleteMessageBatchRequestEntry{
ReceiptHandle: message.ReceiptHandle,
Id: message.MessageId,
}
}
return result
}
func moveMessages(sourceQueueUrl string, destinationQueueUrl string, svc *sqs.SQS, totalMessages int) {
var params = &sqs.ReceiveMessageInput{
QueueUrl: aws.String(sourceQueueUrl),
VisibilityTimeout: aws.Int64(2),
WaitTimeSeconds: aws.Int64(0),
MaxNumberOfMessages: aws.Int64(*maxBatchSize),
MessageAttributeNames: []*string{aws.String(sqs.QueueAttributeNameAll)},
AttributeNames: []*string{
aws.String(sqs.MessageSystemAttributeNameMessageGroupId),
aws.String(sqs.MessageSystemAttributeNameMessageDeduplicationId)},
}
log.Info(color.New(color.FgCyan).Sprintf("Starting to move messages..."))
fmt.Println()
term.HideCursor()
defer term.ShowCursor()
b := progress.NewInt(totalMessages)
b.Width = 40
b.StartDelimiter = color.New(color.FgCyan).Sprint("|")
b.EndDelimiter = color.New(color.FgCyan).Sprint("|")
b.Filled = color.New(color.FgCyan).Sprint("█")
b.Empty = color.New(color.FgCyan).Sprint("░")
b.Template(` {{.Bar}} {{.Text}}{{.Percent | printf "%3.0f"}}%`)
render := term.Renderer()
messagesProcessed := 0
for {
resp, err := svc.ReceiveMessage(params)
if err != nil {
logAwsError("Failed to receive messages", err)
return
}
if len(resp.Messages) == 0 || messagesProcessed == totalMessages {
fmt.Println()
log.Info(color.New(color.FgCyan).Sprintf("Done. Moved %s messages", strconv.Itoa(messagesProcessed)))
return
}
messagesToCopy := resp.Messages
if len(resp.Messages)+messagesProcessed > totalMessages {
messagesToCopy = resp.Messages[0 : totalMessages-messagesProcessed]
}
batch := &sqs.SendMessageBatchInput{
QueueUrl: aws.String(destinationQueueUrl),
Entries: convertToEntries(messagesToCopy),
}
sendResp, err := svc.SendMessageBatch(batch)
if err != nil {
logAwsError("Failed to un-queue messages to the destination", err)
return
}
if len(sendResp.Failed) > 0 {
log.Error(color.New(color.FgRed).Sprintf("%d messages failed to enqueue, see details below", len(sendResp.Failed)))
for index, failed := range sendResp.Failed {
log.Error(color.New(color.FgRed).Sprintf("%d - (%s) %s", index, *failed.Code, *failed.Message))
}
return
}
if len(sendResp.Successful) == len(messagesToCopy) {
if *deleteFromSource {
deleteMessageBatch := &sqs.DeleteMessageBatchInput{
Entries: convertSuccessfulMessageToBatchRequestEntry(messagesToCopy),
QueueUrl: aws.String(sourceQueueUrl),
}
deleteResp, err := svc.DeleteMessageBatch(deleteMessageBatch)
if err != nil {
logAwsError("Failed to delete messages from source queue", err)
return
}
if len(deleteResp.Failed) > 0 {
log.Error(color.New(color.FgRed).Sprintf("Error deleting messages, the following were not deleted\n %s", deleteResp.Failed))
return
}
}
messagesProcessed += len(messagesToCopy)
}
// Increase the total if the approximation was under - avoids exception
if messagesProcessed > totalMessages {
b.Total = float64(messagesProcessed)
}
b.ValueInt(messagesProcessed)
render(b.String())
}
}
func buildVersion(version, commit, date, builtBy string) string {
var result = fmt.Sprintf("version: %s", version)
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if builtBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
}
return result
}