forked from awslabs/aws-lambda-redshift-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliBatchOperations.sh
397 lines (357 loc) · 12.6 KB
/
cliBatchOperations.sh
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
#!/bin/bash
# Purpose: The shell CLI program to check and operate LambdaRedshiftLoader batches
# Author: Andrey Suvorov
# Created: 2021-09-14
# Updated: 2021-11-02
SCRIPT_NAME=$0
usage(){
echo ""
echo "usage: $SCRIPT_NAME [-arpg] [-a account] [-r role] [-p profile] [-g region] [-u 1]"
echo "arguments: "
echo " -a aws account id"
echo " -r aws role"
echo " -p aws default profile"
echo " -g aws region"
echo " -u (1) show usage"
echo ""
exit 1
}
# get arguments
while getopts ":a:r:p:g:u:" opt; do
case $opt in
a) AWS_ACCOUNT_ID="$OPTARG"
;;
r) AWS_ROLE="$OPTARG"
;;
p) AWS_PROFILE="$OPTARG"
;;
g) AWS_REGION="$OPTARG"
;;
u)
usage
;;
:)
echo "$SCRIPT_NAME: missing value for -$OPTARG argument" >&2
usage
;;
\?)
echo "$SCRIPT_NAME: invalid argument -$OPTARG provided" >&2
usage
;;
esac
done
# set default arguments here:
if [ "$AWS_ACCOUNT_ID" == "" ]; then AWS_ACCOUNT_ID="000000000000"; fi
if [ "$AWS_ROLE" == "" ]; then AWS_ROLE="Developer"; fi
if [ "$AWS_PROFILE" == "" ]; then AWS_PROFILE="default"; fi
if [ "$AWS_REGION" == "" ]; then AWS_REGION="us-west-2"; fi
menuHeader(){
clear
echo " "
echo "*****************************************************************************************"
echo "* $1 *"
echo "*****************************************************************************************"
}
displayMsg(){
echo " "
echo "---------------------------------------- MESSAGE ----------------------------------------"
echo " $1"
echo "-----------------------------------------------------------------------------------------"
echo " "
}
getCredentials(){
local l_retryLogin
menuHeader " AWS Login "
displayMsg "AWS account: $AWS_ACCOUNT_ID\n AWS Region: $AWS_REGION\n AWS Role: $AWS_ROLE\n AWS Profile: $AWS_PROFILE"
while ! eiamcli getAWSTempCredentials -a "$AWS_ACCOUNT_ID" -r "$AWS_ROLE" -p "$AWS_PROFILE" ; do
if ! eiamcli login; then
displayMsg "Can't authenticate the user. Exiting program."
read -r -p "Would you like to try login again? (y/n): " l_retryLogin
if ! [ "$l_retryLogin" == 'y' ]; then
displayMsg "Exiting program"
exit 1
fi
else
eiamcli getAWSTempCredentials -a "$AWS_ACCOUNT_ID" -r "$AWS_ROLE" -p "$AWS_PROFILE"
fi
done
}
inputDate(){
local dt=""
while ! [[ "$dt" =~ ^[0-1][0-9]/[0-3][0-9]/[0-9]{4}$ && $(date -j -f %m/%d/%Y "$dt" +%s) ]]; do
if ! [[ "$dt" == "" ]]; then displayMsg "Invalid date format for $1 : $dt" > /dev/tty; fi
read -r -p "Please enter $1 (MM/DD/YYYY): " dt
done
echo "$dt"
}
batchAction(){
local l_batchStatus=$1
local l_response=$2
batchCount=$(echo "$2" | jq '. | length')
action_menu=true
action_executed=""
menuHeader "Action Menu"
while [[ "$action_menu" == true ]]; do
if [[ "$action_executed" == "" ]]; then
displayMsg "Found $batchCount batches with '$l_batchStatus' status from $startDate to $endDate"
else
displayMsg "$action_executed batches command completed"
action_executed=""
fi
PS3="Please select the action from the menu above: "
select action in "${actions[@]}"; do
case $action in
"List")
displayMsg "$action batches with $l_batchStatus status"
echo "$l_response" | jq
action_executed=$action
break
;;
"Describe")
displayMsg "$action batches with $l_batchStatus status"
counter=1
for row in $(echo "$l_response" | jq -r '.[] | @base64'); do
_jq() {
echo "${row}" | base64 --decode | jq -r "${1}"
}
# exit individual items action menu
if ! [[ "$action_executed" == "" ]]; then break; fi
batchId=$(_jq '.batchId')
s3Prefix=$(_jq '.s3Prefix')
echo "==> Describe batch $counter of $batchCount (batchId: ${batchId} s3Prefix: ${s3Prefix})"
node describeBatch.js \
--region "${AWS_REGION}" \
--batchId "${batchId}" \
--s3prefix "${s3Prefix}" | jq
echo ""
counter=$((counter+1))
# select an individual action for each batch in the query, one by one
PS3="Please select the batch action from the menu above: "
if [[ "$l_batchStatus" == "error" || "$l_batchStatus" == "locked" ]]; then
batch_actions=("Reprocess" "Unlock" "Delete" "Next" "Exit")
elif [[ "$l_batchStatus" == "complete" ]]; then
batch_actions=("Next" "Delete" "Exit")
elif [[ "$l_batchStatus" == "open" ]]; then
batch_actions=("Next" "Exit")
fi
select batch_action in "${batch_actions[@]}"; do
case $batch_action in
"Reprocess")
echo "==> Reprocessing (batchId: ${batchId} s3Prefix: ${s3Prefix})"
node reprocessBatch.js \
--region "${AWS_REGION}" \
--batchId "${batchId}" \
--prefix "${s3Prefix}"
break
;;
"Unlock")
echo "==> Unlocking (batchId: ${batchId} s3Prefix: ${s3Prefix})"
node unlockBatch.js \
"${AWS_REGION}" \
"${batchId}" \
"${s3Prefix}"
break
;;
"Delete")
echo "==> Deleting (batchId: ${batchId} s3Prefix: ${s3Prefix})"
node deleteBatch.js \
--region "${AWS_REGION}" \
--batchId "${batchId}" \
--s3Prefix "${s3Prefix}"
break
;;
"Next")
echo "==> Next batch"
break
;;
"Exit")
clear
action_executed=$action
break
;;
*) displayMsg "Invalid option selected $REPLY"
;;
esac
done
done
action_executed=$action
break
;;
"Reprocess")
displayMsg "$action batches with $l_batchStatus status"
counter=1
for row in $(echo "$l_response" | jq -r '.[] | @base64'); do
_jq() {
echo "${row}" | base64 --decode | jq -r "${1}"
}
batchId=$(_jq '.batchId')
s3Prefix=$(_jq '.s3Prefix')
echo "==> Reprocessing batch $counter of $batchCount (batchId: ${batchId} s3Prefix: ${s3Prefix})"
node reprocessBatch.js \
--region "${AWS_REGION}" \
--batchId "${batchId}" \
--prefix "${s3Prefix}"
counter=$((counter+1))
done
action_executed=$action
break
;;
"Unlock")
displayMsg "$action batches with $l_batchStatus status"
counter=1
for row in $(echo "$l_response" | jq -r '.[] | @base64'); do
_jq() {
echo "${row}" | base64 --decode | jq -r "${1}"
}
batchId=$(_jq '.batchId')
s3Prefix=$(_jq '.s3Prefix')
echo "==> Unlocking batch $counter of $batchCount (batchId: ${batchId} s3Prefix: ${s3Prefix})"
node unlockBatch.js \
"${AWS_REGION}" \
"${batchId}" \
"${s3Prefix}"
counter=$((counter+1))
done
action_executed=$action
break
;;
"Delete")
displayMsg "$action batches with $l_batchStatus status from $startDate to $endDate"
node deleteBatches.js \
--region "${AWS_REGION}" \
--startDate "${startDateUnix}" \
--endDate "${endDateUnix}" \
--batchStatus "$l_batchStatus" \
--dryRun false
action_executed=$action
break
;;
"Main Menu")
clear
action_menu=false
break
;;
*) displayMsg "Invalid option selected $REPLY"
;;
esac
done
done
}
queryBatch(){
response=$(node queryBatches.js \
--region "${AWS_REGION}" \
--batchStatus "$1" \
--startDate "${startDateUnix}" \
--endDate "${endDateUnix}" \
)
echo "$response"
}
main(){
startDate=$(date -v-7d '+%m/%d/%Y')
startDateUnix=$(date -j -f %m/%d/%Y "$startDate" +%s)
endDate=$(date '+%m/%d/%Y')
endDateUnix=$(date -j -f %m/%d/%Y "$endDate" +%s)
dates_set=false
getCredentials
main_menu=true
while [ "$main_menu" == true ]; do
menuHeader " Main Menu "
if ! [[ "$startDate" == "" && "$endDate" == "" ]]; then
if [[ "$dates_set" == false ]]; then
read -r -p "Do you want to use selected dates to query batches? $startDate to $endDate (y/n): " defaultDates
if ! [[ $defaultDates =~ ^[Yy]$ ]]; then
startDateUnix=0
endDateUnix=0
while ! [[ "$startDateUnix" < "$endDateUnix" ]]; do
startDate=$(inputDate 'Start Date')
startDateUnix=$(date -j -f %m/%d/%Y "$startDate" +%s)
endDate=$(inputDate 'End Date')
endDateUnix=$(date -j -f %m/%d/%Y "$endDate" +%s)
if ! [[ "$startDateUnix" < "$endDateUnix" ]]; then
displayMsg "The Start Date $startDate can't be greater than End Date $endDate";
fi
done
fi
dates_set=true
fi
fi
menuHeader " Main Menu "
if ! [[ $outputMsg == "" ]]; then
displayMsg "$outputMsg"
outputMsg=""
else
displayMsg "Query dates set from Start Date $startDate to End Date $endDate\n Please use option 6 to change dates"
fi
PS3="What you would like to do? Please select from the menu: "
statuses=("Check Error batches" "Check Locked batches" "Check Open batches"
"Check Complete batches" "Check Other batches" "Change query dates" "Exit")
select status in "${statuses[@]}"; do
case $status in
"Check Error batches")
queryResult=$(queryBatch "error")
if ! [[ "$queryResult" == "[]" ]]; then
actions=("List" "Describe" "Reprocess" "Delete" "Main Menu")
batchAction "error" "$queryResult"
else
outputMsg="No batches with status 'error' found for specified dates $startDate - $endDate"
fi
break
;;
"Check Locked batches")
queryResult=$(queryBatch "locked")
if ! [[ "$queryResult" == "[]" ]]; then
actions=("List" "Describe" "Unlock" "Reprocess" "Delete" "Main Menu")
batchAction "locked" "$queryResult"
else
outputMsg="No batches with status 'locked' found for specified dates $startDate - $endDate"
fi
break
;;
"Check Open batches")
queryResult=$(queryBatch "open")
if ! [[ "$queryResult" == "[]" ]]; then
actions=("List" "Describe" "Main Menu")
batchAction "open" "$queryResult"
else
outputMsg="No batches with status 'open' found for specified dates $startDate - $endDate"
fi
break
;;
"Check Complete batches")
queryResult=$(queryBatch "complete")
if ! [[ "$queryResult" == "[]" ]]; then
actions=("List" "Describe" "Delete" "Main Menu")
batchAction "complete" "$queryResult"
else
outputMsg="No batches with status 'complete' found for specified dates $startDate - $endDate"
fi
break
;;
"Check Other batches")
read -r -p "Please type in batch status to query: " batchStatus
queryResult=$(queryBatch "$batchStatus")
if ! [[ "$queryResult" == "[]" ]]; then
actions=("List" "Describe" "Delete" "Main Menu")
batchAction "$batchStatus" "$queryResult"
else
outputMsg="No batches with status '$batchStatus' found for specified dates $startDate - $endDate"
fi
break
;;
"Change query dates")
dates_set=false
clear
break
;;
"Exit")
displayMsg "Exiting program"
exit
;;
*)
displayMsg "Invalid option selected $REPLY"
;;
esac
done
done
}
main