diff --git a/local-app/bin/manage-remote-state.sh b/local-app/bin/manage-remote-state.sh index 9591d2fc..208aa7d9 100755 --- a/local-app/bin/manage-remote-state.sh +++ b/local-app/bin/manage-remote-state.sh @@ -1,6 +1,6 @@ #!/bin/bash -VERSION="1.1.0" +VERSION="1.2.0" THIS=$(basename $0 .sh) test -r /apps/terraform/etc/aws-functions.sh && source /apps/terraform/etc/aws-functions.sh @@ -59,9 +59,14 @@ then echo "* syncing bucket s3://$bucket/ to $TFA" aws --profile $(get_profile) --region $region s3 sync s3://$bucket/ ./$TFA/ - echo "* archiving ddb table $ddbtable" - aws --profile $(get_profile) --region $region dynamodb scan --table $ddbtable > ./$TFA/$ddbtable.json - echo "" + if [ ! -z "$ddbtable" ] + then + echo "* archiving ddb table $ddbtable" + aws --profile $(get_profile) --region $region dynamodb scan --table $ddbtable > ./$TFA/$ddbtable.json + echo "" + else + echo "* no ddb table defined" + fi fi if [[ $ACTION == "list" ]] || [[ $ACTION == "delete" ]] @@ -69,9 +74,14 @@ then echo "* listing bucket s3://$bucket/$bucket_key" aws --profile $(get_profile) --region $region s3 ls s3://$bucket/$bucket_key - echo "* listing ddb table entry $ddbentry" - aws --profile $(get_profile) --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" - echo "" + if [ ! -z "$ddbtable" ] + then + echo "* listing ddb table entry $ddbentry" + aws --profile $(get_profile) --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" + echo "" + else + echo "* no ddb table defined" + fi fi if [[ $ACTION == "get" ]] || [[ $ACTION == "delete" ]] @@ -83,9 +93,14 @@ then aws --profile $(get_profile) --region $region s3 cp s3://$bucket/$bucket_key logs/$STAMP/$OFILE OFILE="lock-entry.json" - echo "* getting ddb table entry $ddbentry to logs/$STAMP/$OFILE" - aws --profile $(get_profile) --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" > logs/$STAMP/$OFILE - echo "" + if [ ! -z "$ddbtable" ] + then + echo "* getting ddb table entry $ddbentry to logs/$STAMP/$OFILE" + aws --profile $(get_profile) --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" > logs/$STAMP/$OFILE + echo "" + else + echo "* no ddb table defined" + fi fi if [[ $ACTION == "delete" ]] @@ -94,9 +109,14 @@ then echo "* deleting bucket s3://$bucket/$bucket_key" aws --profile $(get_profile) --region $region s3 rm s3://$bucket/$bucket_key - echo "* deleting ddb table entry $ddbentry" - aws --profile $(get_profile) --region $region dynamodb delete-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" - echo "" + if [ ! -z "$ddbtable" ] + then + echo "* deleting ddb table entry $ddbentry" + aws --profile $(get_profile) --region $region dynamodb delete-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" + echo "" + else + echo "* no ddb table defined" + fi fi if [[ $ACTION == "put" ]] @@ -122,12 +142,17 @@ then echo "* making directory logs/$STAMP" mkdir -p logs/$STAMP OFILE="lock-entry.json" - echo "* getting previous ddb table entry $ddbentry to logs/$STAMP/$OFILE" - aws --profile $(get_profile) --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" > logs/$STAMP/$OFILE - - echo "* updating ddb table entry $ddbentry with value $VALUE" - aws --profile $(get_profile) --region $region dynamodb put-item --table $ddbtable --item "{\"LockID\":{\"S\":\"$ddbentry\"},\"Digest\":{\"S\":\"$VALUE\"}}" - echo "" + if [ ! -z "$ddbtable" ] + then + echo "* getting previous ddb table entry $ddbentry to logs/$STAMP/$OFILE" + aws --profile $(get_profile) --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" > logs/$STAMP/$OFILE + + echo "* updating ddb table entry $ddbentry with value $VALUE" + aws --profile $(get_profile) --region $region dynamodb put-item --table $ddbtable --item "{\"LockID\":{\"S\":\"$ddbentry\"},\"Digest\":{\"S\":\"$VALUE\"}}" + echo "" + else + echo "* no ddb table defined" + fi fi exit $?