diff --git a/bin/upgrade-s3-provider.sh b/bin/upgrade-s3-provider.sh index 015042a..2ede686 100755 --- a/bin/upgrade-s3-provider.sh +++ b/bin/upgrade-s3-provider.sh @@ -1,14 +1,19 @@ #!/bin/bash -VERSION="1.0.0" +VERSION="1.0.2" THIS=$(basename $0 .sh) STATUS=0 MODULE=$1 +RNAME=$2 if [ -z $MODULE ] then echo "* missing module, expecting 'module.s3_name'" exit 1 fi +if [ -z $RNAME ] +then + RNAME="this" +fi LOGDIR="logs" test -d $LOGDIR || mkdir -p $LOGDIR @@ -16,21 +21,25 @@ YMDSTAMP=$(date +%Y%m%d) start=$(date +%s) STAMP="$YMDSTAMP.$start" LOGFILE="$LOGDIR/$THIS.$STAMP.log" +if [ -z $TFCOMMAND ] +then + TFCOMMAND=terraform +fi FILE=$(mktemp -t tfplan.XXXXX) -echo "* getting tf-plan for $MODULE to $FILE (logfile $LOGFILE)" -terraform plan -no-color -target=$MODULE > $FILE +echo "* getting tf-plan for $MODULE resource_name $RNAME to $FILE (logfile $LOGFILE)" +$TFCOMMAND plan -no-color -target=$MODULE > $FILE echo "* checking that a bucket exists in $MODULE" -EXISTS=$(grep -c ^$MODULE.aws_s3_bucket.this: $FILE) +EXISTS=$(grep -c ^$MODULE.aws_s3_bucket.$RNAME: $FILE) if [ $EXISTS == 0 ] then - echo "* no S3 bucket at module $MODULE aws_s3_bucket.this" + echo "* no S3 bucket at module $MODULE aws_s3_bucket.$RNAME" exit 1 fi echo "* getting bucket ID from $MODULE" -BUCKETID=$(terraform state show -no-color $MODULE.aws_s3_bucket.this|grep -E 'id.* *='|awk '{print $1,$3}' |grep ^id|awk '{print $2}'|sed -e 's/"//g') +BUCKETID=$($TFCOMMAND state show -no-color $MODULE.aws_s3_bucket.$RNAME|grep -E 'id.* *='|awk '{print $1,$3}' |grep ^id|awk '{print $2}'|sed -e 's/"//g') if [ -z $BUCKETID ] then echo "* cannot determine bucket id for $MODULE" @@ -41,10 +50,10 @@ fi COUNT=0 echo "* importing resources to be created" -for resource in $(grep ' created' $FILE | awk '{print $2}') +for resource in $(grep ' created' $FILE | awk '{print $2}' | grep aws_s3) do echo ". resource: tf-import $resource $BUCKETID" - terraform import -no-color $resource $BUCKETID > $LOGFILE + $TFCOMMAND import -no-color $resource $BUCKETID > $LOGFILE if [ $? != 0 ] then echo "* error importing resource $resource"