11#! /bin/bash
2- #  Purpose: The shell  CLI program to check and operate LambdaRedshiftLoader batches
2+ #  Purpose: CLI program to check and operate LambdaRedshiftLoader batches
33#  Author: Andrey Suvorov
44#  Created: 2021-09-14
5+ #  Updated: 2021-11-02
56
67SCRIPT_NAME=$0 
78
@@ -44,7 +45,7 @@ while getopts ":a:r:p:g:u:" opt; do
4445done 
4546
4647#  set default arguments here:
47- if  [ " $AWS_ACCOUNT_ID "   ==  " "   ];  then  AWS_ACCOUNT_ID=" 000000000000 " ;  fi 
48+ if  [ " $AWS_ACCOUNT_ID "   ==  " "   ];  then  AWS_ACCOUNT_ID=" 823951367757 " ;  fi 
4849if  [ " $AWS_ROLE "   ==  " "   ];  then  AWS_ROLE=" Developer" ;  fi 
4950if  [ " $AWS_PROFILE "   ==  " "   ];  then  AWS_PROFILE=" default" ;  fi 
5051if  [ " $AWS_REGION "   ==  " "   ];  then  AWS_REGION=" us-west-2" ;  fi 
@@ -111,20 +112,24 @@ batchAction(){
111112    PS3=" Please select the action from the menu above: " 
112113    select  action  in  " ${actions[@]} " ;  do 
113114      case  $action  in 
114-         " Display "  )
115+         " List "  )
115116          displayMsg " $action  batches with $l_batchStatus  status" 
116117          echo  " $l_response "   |  jq
117118          action_executed=$action 
118119          break 
119120          ;;
120-         " Display with description "  )
121+         " Describe "  )
121122          displayMsg " $action  batches with $l_batchStatus  status" 
123+           counter=1
122124
123125          for  row  in  $( echo " $l_response "   |  jq -r ' .[] | @base64' ) ;  do 
124126            _jq () {
125127                  echo  " ${row} "   |  base64 --decode |  jq -r " ${1} " 
126128                  }
127129
130+             #  exit individual items action menu
131+             if  !  [[  " $action_executed "   ==  " "   ]];  then  break ;  fi 
132+ 
128133            batchId=$( _jq ' .batchId' ) 
129134            s3Prefix=$( _jq ' .s3Prefix' ) 
130135
@@ -138,6 +143,57 @@ batchAction(){
138143            echo  " " 
139144
140145            counter=$(( counter+ 1 )) 
146+ 
147+             #  select an individual action for each batch in the query, one by one
148+             PS3=" Please select the batch action from the menu above: " 
149+             if  [[ " $l_batchStatus "   ==  " error"   ||  " $l_batchStatus "   ==  " locked"   ]];  then 
150+               batch_actions=(" Reprocess"   " Unlock"   " Delete"   " Next"   " Exit"  )
151+             elif  [[ " $l_batchStatus "   ==  " complete"   ]];  then 
152+               batch_actions=(" Next"   " Delete"   " Exit"  )
153+             elif  [[ " $l_batchStatus "   ==  " open"   ]];  then 
154+               batch_actions=(" Next"   " Exit"  )
155+             fi 
156+ 
157+             select  batch_action  in  " ${batch_actions[@]} " ;  do 
158+               case  $batch_action  in 
159+                 " Reprocess"  )
160+                   echo  " ==> Reprocessing (batchId: ${batchId}  s3Prefix: ${s3Prefix} )" 
161+                   node reprocessBatch.js \
162+                         --region " ${AWS_REGION} "   \
163+                         --batchId " ${batchId} "   \
164+                         --prefix " ${s3Prefix} " 
165+ 
166+                   break 
167+                   ;;
168+                 " Unlock"  )
169+                   echo  " ==> Unlocking (batchId: ${batchId}  s3Prefix: ${s3Prefix} )" 
170+                   node unlockBatch.js \
171+                         " ${AWS_REGION} "   \
172+                         " ${batchId} "   \
173+                         " ${s3Prefix} " 
174+                   break 
175+                   ;;
176+                 " Delete"  )
177+                   echo  " ==> Deleting (batchId: ${batchId}  s3Prefix: ${s3Prefix} )" 
178+                   node deleteBatch.js \
179+                         --region " ${AWS_REGION} "   \
180+                         --batchId " ${batchId} "   \
181+                         --s3Prefix " ${s3Prefix} " 
182+                   break 
183+                   ;;
184+                 " Next"  )
185+                   echo  " ==> Next batch" 
186+                   break 
187+                   ;;
188+                 " Exit"  )
189+                   clear
190+                   action_executed=$action 
191+                   break 
192+                   ;;
193+                 * ) displayMsg " Invalid option selected $REPLY " 
194+                   ;;
195+               esac 
196+             done 
141197          done 
142198          action_executed=$action 
143199          break 
@@ -223,115 +279,119 @@ queryBatch(){
223279  echo  " $response " 
224280}
225281
226- getCredentials
227- 
228- startDate=$( date -v-7d ' +%m/%d/%Y' ) 
229- startDateUnix=$( date -j -f %m/%d/%Y " $startDate "    +%s) 
230- endDate=$( date ' +%m/%d/%Y' ) 
231- endDateUnix=$( date -j -f %m/%d/%Y " $endDate "    +%s) 
232- dates_set=false
233- 
234- main_menu=true
235- while  [ " $main_menu "   ==  true  ];  do 
236- 
237-   menuHeader "  Main Menu " 
238- 
239-   if  !  [[ " $startDate "   ==  " "   &&  " $endDate "   ==  " "   ]];  then 
240-     if  [[ " $dates_set "   ==  false   ]];  then 
241-       read  -r -p " Do you want to use selected dates to query batches? $startDate  to $endDate  (y/n): "   defaultDates
242-       if  !  [[ $defaultDates  =~  ^[Yy]$ ]];  then 
243-         startDateUnix=0
244-         endDateUnix=0
245-         while  !  [[ " $startDateUnix "   <  " $endDateUnix "   ]];  do 
246-           startDate=$( inputDate ' Start Date' ) 
247-           startDateUnix=$( date -j -f %m/%d/%Y " $startDate "    +%s) 
248-           endDate=$( inputDate ' End Date' ) 
249-           endDateUnix=$( date -j -f %m/%d/%Y " $endDate "    +%s) 
250-           if  !  [[ " $startDateUnix "   <  " $endDateUnix "   ]];  then 
251-             displayMsg " The Start Date $startDate  can't be greater than End Date $endDate " ; 
252-           fi 
253-         done 
282+ main (){
283+   startDate=$( date -v-7d ' +%m/%d/%Y' ) 
284+   startDateUnix=$( date -j -f %m/%d/%Y " $startDate "    +%s) 
285+   endDate=$( date ' +%m/%d/%Y' ) 
286+   endDateUnix=$( date -j -f %m/%d/%Y " $endDate "    +%s) 
287+   dates_set=false
288+ 
289+   getCredentials
290+ 
291+   main_menu=true
292+   while  [ " $main_menu "   ==  true  ];  do 
293+ 
294+     menuHeader "  Main Menu " 
295+ 
296+     if  !  [[ " $startDate "   ==  " "   &&  " $endDate "   ==  " "   ]];  then 
297+       if  [[ " $dates_set "   ==  false   ]];  then 
298+         read  -r -p " Do you want to use selected dates to query batches? $startDate  to $endDate  (y/n): "   defaultDates
299+         if  !  [[ $defaultDates  =~  ^[Yy]$ ]];  then 
300+           startDateUnix=0
301+           endDateUnix=0
302+           while  !  [[ " $startDateUnix "   <  " $endDateUnix "   ]];  do 
303+             startDate=$( inputDate ' Start Date' ) 
304+             startDateUnix=$( date -j -f %m/%d/%Y " $startDate "    +%s) 
305+             endDate=$( inputDate ' End Date' ) 
306+             endDateUnix=$( date -j -f %m/%d/%Y " $endDate "    +%s) 
307+             if  !  [[ " $startDateUnix "   <  " $endDateUnix "   ]];  then 
308+               displayMsg " The Start Date $startDate  can't be greater than End Date $endDate " ; 
309+             fi 
310+           done 
311+         fi 
312+         dates_set=true
254313      fi 
255-       dates_set=true
256314    fi 
257-    fi 
258- 
259-   menuHeader  "  Main Menu  " 
260-   if   !  [[  $outputMsg   ==   " "  ]] ;   then 
261-     displayMsg  " $ outputMsg" 
262-     outputMsg= " " 
263-   else 
264-     displayMsg  " Query dates set from Start Date  $startDate  to End Date  $endDate \n Please use option 6 to change dates " 
265-    fi 
266- 
267-   PS3= " What you would like to do? Please select from the menu:  " 
268-   statuses=( " Check Error  batches"   " Check Locked  batches"   " Check Open batches " 
269-              " Check Complete batches "   " Check Other batches "   " Change query dates "   " Exit " ) 
270-   select   status   in   " ${statuses[@]} " ;   do 
271-     case   $status   in 
272-       " Check Error batches "  )
273-         queryResult= $( queryBatch  " error " ) 
274-         if   !  [[  " $queryResult "   ==   " [] "  ]] ;   then 
275-           actions=( " Display "   " Display with description "   " Reprocess "   " Delete "   " Main Menu " ) 
276-           batchAction  " error "   " $queryResult " 
277-         else 
278-           outputMsg= " No batches with status 'error' found for specified dates  $startDate  -  $endDate " 
279-         fi 
280-         break 
281-         ;; 
282-       " Check Locked batches "  )
283-         queryResult= $( queryBatch  " locked " ) 
284-         if   !  [[  " $queryResult "   ==   " [] "  ]] ;   then 
285-           actions=( " Display "   " Display with description "   " Unlock "   " Reprocess "   " Delete "   " Main Menu " ) 
286-           batchAction  " locked "   " $queryResult " 
287-         else 
288-           outputMsg= " No batches with status 'locked' found for specified dates  $startDate  -  $endDate " 
289-         fi 
290-         break 
291-         ;; 
292-       " Check Open batches "  )
293-         queryResult= $( queryBatch  " open " ) 
294-         if   !  [[  " $queryResult "   ==   " [] "  ]] ;   then 
295-           actions=( " Display "   " Display with description "   " Main Menu " ) 
296-           batchAction  " open "   " $queryResult " 
297-         else 
298-           outputMsg= " No batches with status 'open' found for specified dates  $startDate  -  $endDate " 
299-         fi 
300-         break 
301-         ;; 
302-       " Check Complete batches "  )
303-         queryResult= $( queryBatch  " complete " ) 
304-         if   !  [[  " $queryResult "   ==   " [] "  ]] ;   then 
305-           actions=( " Display "   " Display with description "   " Delete "   " Main Menu " ) 
306-           batchAction  " complete "   " $queryResult " 
307-         else 
308-           outputMsg= " No batches with status 'complete' found for specified dates  $startDate  -  $endDate " 
309-         fi 
310-         break 
311-         ;; 
312-       " Check Other batches " ) 
313-         read  -r -p  " Please type in batch status to query:  "   batchStatus
314-         queryResult= $( queryBatch  " $batchStatus " ) 
315-         if   !  [[  " $queryResult "   ==   " [] "  ]] ;   then 
316-           actions=( " Display "   " Display with description "   " Delete "   " Main Menu " ) 
317-           batchAction  " $batchStatus "   " $queryResult " 
318-         else 
319-           outputMsg= " No batches with status ' $batchStatus ' found for specified dates  $startDate  -  $endDate " 
320-         fi 
321-         break 
322-         ;; 
323-       " Change query dates " ) 
324-         dates_set=false 
325-         clear 
326-         break 
327-         ;; 
328-       " Exit " ) 
329-         displayMsg  " Exiting program " 
330-         exit 
331-         ;; 
332-       * ) 
333-         displayMsg  " Invalid option selected  $REPLY " 
334-         ;; 
335-     esac 
315+ 
316+     menuHeader  "  Main Menu  " 
317+      if   !  [[  $outputMsg   ==   " "  ]] ;   then 
318+       displayMsg  " $outputMsg " 
319+        outputMsg= " " 
320+     else 
321+       displayMsg  " Query dates set from Start Date  $startDate  to End Date  $endDate \n Please use option 6 to change dates " 
322+     fi 
323+ 
324+     PS3= " What you would like to do? Please select from the menu:  " 
325+     statuses=( " Check Error batches "   " Check Locked batches "   " Check Open batches " 
326+                " Check Complete  batches"   " Check Other  batches"   " Change query dates "   " Exit " ) 
327+     select   status   in   " ${statuses[@]} " ;   do 
328+        case   $status   in 
329+          " Check Error batches " ) 
330+           queryResult= $( queryBatch  " error " ) 
331+            if   !  [[  " $ queryResult"   ==   " [] "  ]] ;   then 
332+             actions=( " List "   " Describe "   " Reprocess "   " Delete "   " Main Menu " ) 
333+             batchAction  " error "   " $queryResult " 
334+           else 
335+             outputMsg= " No batches with status 'error' found for specified dates  $startDate  -  $endDate " 
336+           fi 
337+            break 
338+           ;; 
339+         " Check Locked batches " ) 
340+           queryResult= $( queryBatch  " locked " ) 
341+            if   !  [[  " $ queryResult"   ==   " [] "  ]] ;   then 
342+             actions=( " List "   " Describe "   " Unlock "   " Reprocess "   " Delete "   " Main Menu " ) 
343+             batchAction  " locked "   " $queryResult " 
344+           else 
345+             outputMsg= " No batches with status 'locked' found for specified dates  $startDate  -  $endDate " 
346+           fi 
347+            break 
348+           ;; 
349+         " Check Open batches " ) 
350+           queryResult= $( queryBatch  " open " ) 
351+            if   !  [[  " $ queryResult"   ==   " [] "  ]] ;   then 
352+             actions=( " List "   " Describe "   " Main Menu " ) 
353+             batchAction  " open "   " $queryResult " 
354+           else 
355+             outputMsg= " No batches with status 'open' found for specified dates  $startDate  -  $endDate " 
356+           fi 
357+            break 
358+           ;; 
359+         " Check Complete batches " ) 
360+           queryResult= $( queryBatch  " complete " ) 
361+            if   !  [[  " $ queryResult"   ==   " [] "  ]] ;   then 
362+             actions=( " List "   " Describe "   " Delete "   " Main Menu " ) 
363+             batchAction  " complete "   " $queryResult " 
364+           else 
365+             outputMsg= " No batches with status 'complete' found for specified dates  $startDate  -  $endDate " 
366+           fi 
367+            break 
368+           ;; 
369+         " Check Other batches " ) 
370+            read  -r -p  " Please type in batch status to query:  "  batchStatus 
371+           queryResult= $( queryBatch  " $ batchStatus" ) 
372+            if   !  [[  " $ queryResult"   ==   " [] "  ]] ;   then 
373+             actions=( " List "   " Describe "   " Delete "   " Main Menu " ) 
374+             batchAction  " $batchStatus "   " $queryResult " 
375+           else 
376+             outputMsg= " No batches with status ' $batchStatus ' found for specified dates  $startDate  -  $endDate " 
377+           fi 
378+            break 
379+           ;; 
380+         " Change query dates " ) 
381+           dates_set=false 
382+           clear 
383+            break 
384+           ;; 
385+         " Exit " ) 
386+           displayMsg  " Exiting program " 
387+            exit 
388+           ;; 
389+         * ) 
390+           displayMsg  " Invalid option selected  $REPLY " 
391+           ;; 
392+       esac 
393+     done 
336394  done 
337- done 
395+ }
396+ 
397+ main
0 commit comments