Skip to content

Commit

Permalink
change how to pull profile
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Oct 10, 2025
1 parent 863fec2 commit d2e8268
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions local-app/bin/manage-remote-state.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION="1.2.0"
VERSION="1.2.1"
THIS=$(basename $0 .sh)

test -r /apps/terraform/etc/aws-functions.sh && source /apps/terraform/etc/aws-functions.sh
Expand Down Expand Up @@ -38,6 +38,11 @@ fi

STAMP=$(date +%Y%m%d.%s)

profile=$(grep -E "\bprofile\b" $RSFILE | sed -e 's/^.*=//' -e 's/"//g' -e 's/ *//')
if [ -z "$profile" ]
then
profile=$(get_profile)
fi
bucket=$(grep -E "\bbucket\b" $RSFILE | sed -e 's/^.*=//' -e 's/"//g' -e 's/ *//')
bucket_key=$(grep -E "\bkey\b" $RSFILE | sed -e 's/^.*=//' -e 's/"//g' -e 's/ *//')
region=$(grep -E "\bregion\b" $RSFILE | sed -e 's/^.*=//' -e 's/"//g' -e 's/ *//')
Expand All @@ -57,12 +62,12 @@ then
TFA="tfstate-archive"
test -d $TFA || (mkdir $TFA && echo "* creating directory $TFA" )
echo "* syncing bucket s3://$bucket/ to $TFA"
aws --profile $(get_profile) --region $region s3 sync s3://$bucket/ ./$TFA/
aws --profile $profile --region $region s3 sync s3://$bucket/ ./$TFA/

if [ ! -z "$ddbtable" ]
then
echo "* archiving ddb table $ddbtable"
aws --profile $(get_profile) --region $region dynamodb scan --table $ddbtable > ./$TFA/$ddbtable.json
aws --profile $profile --region $region dynamodb scan --table $ddbtable > ./$TFA/$ddbtable.json
echo ""
else
echo "* no ddb table defined"
Expand All @@ -72,12 +77,12 @@ fi
if [[ $ACTION == "list" ]] || [[ $ACTION == "delete" ]]
then
echo "* listing bucket s3://$bucket/$bucket_key"
aws --profile $(get_profile) --region $region s3 ls s3://$bucket/$bucket_key
aws --profile $profile --region $region s3 ls s3://$bucket/$bucket_key

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\"}}"
aws --profile $profile --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}"
echo ""
else
echo "* no ddb table defined"
Expand All @@ -90,13 +95,13 @@ then
mkdir -p logs/$STAMP
OFILE=$(basename $bucket_key)
echo "* getting bucket s3://$bucket/$bucket_key to logs/$STAMP/$OFILE"
aws --profile $(get_profile) --region $region s3 cp s3://$bucket/$bucket_key logs/$STAMP/$OFILE
aws --profile $profile --region $region s3 cp s3://$bucket/$bucket_key logs/$STAMP/$OFILE

OFILE="lock-entry.json"
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
aws --profile $profile --region $region dynamodb get-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}" > logs/$STAMP/$OFILE
echo ""
else
echo "* no ddb table defined"
Expand All @@ -107,12 +112,12 @@ if [[ $ACTION == "delete" ]]
then
OFILE=$(basename $bucket_key)
echo "* deleting bucket s3://$bucket/$bucket_key"
aws --profile $(get_profile) --region $region s3 rm s3://$bucket/$bucket_key
aws --profile $profile --region $region s3 rm s3://$bucket/$bucket_key

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\"}}"
aws --profile $profile --region $region dynamodb delete-item --table $ddbtable --key "{\"LockID\":{\"S\":\"$ddbentry\"}}"
echo ""
else
echo "* no ddb table defined"
Expand Down Expand Up @@ -145,10 +150,10 @@ then
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
aws --profile $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\"}}"
aws --profile $profile --region $region dynamodb put-item --table $ddbtable --item "{\"LockID\":{\"S\":\"$ddbentry\"},\"Digest\":{\"S\":\"$VALUE\"}}"
echo ""
else
echo "* no ddb table defined"
Expand Down

0 comments on commit d2e8268

Please sign in to comment.