-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #!/bin/bash | ||
|
|
||
| VERSION="1.0.0" | ||
| 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 | ||
| YMDSTAMP=$(date +%Y%m%d) | ||
| start=$(date +%s) | ||
| STAMP="$YMDSTAMP.$start" | ||
| LOGFILE="$LOGDIR/$THIS.$STAMP.log" | ||
| if [ -z $TFCOMMAND ] | ||
| then | ||
| TFCOMMAND=terraform | ||
| fi | ||
|
|
||
| ERRORS="" | ||
| FILE=$(mktemp -t tfplan.XXXXX) | ||
| 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.$RNAME: $FILE) | ||
| if [ $EXISTS == 0 ] | ||
| then | ||
| echo "* no S3 bucket at module $MODULE aws_s3_bucket.$RNAME" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "* getting bucket ID from $MODULE" | ||
| 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" | ||
| exit 1 | ||
| else | ||
| echo "* found bucket $BUCKETID" | ||
| fi | ||
|
|
||
| COUNT=0 | ||
| RMCOUNT=0 | ||
|
|
||
| echo "* checking for bucket_objects in $MODULE" | ||
| EXISTS=$(grep -c ^$MODULE.aws_s3_bucket_object.$RNAME $FILE) | ||
| if [ $EXISTS == 0 ] | ||
| then | ||
| echo "* no S3 bucket objects at module $MODULE aws_s3_bucket_object.$RNAME" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "* importing s3_object resources to be created" | ||
| for resource in $(grep ^$MODULE.aws_s3_bucket_object.$RNAME $FILE | awk '{print $1}' | sed -e's/:$//') | ||
| do | ||
| nresource=$(echo $resource | sed -e 's/aws_s3_bucket_object/aws_s3_object/') | ||
| oentry=$($TFCOMMAND state show -no-color $resource | grep -iE " key " | awk '{print $3}' | sed -e 's/"//g') | ||
| echo ". resource: tf-import $nresource s3://${BUCKETID}/${oentry}" | ||
| $TFCOMMAND import -no-color $nresource s3://${BUCKETID}/${oentry} >> $LOGFILE | ||
| if [ $? != 0 ] | ||
| then | ||
| echo "* error importing resource $nresource" | ||
| STATUS=$(( $STATUS + 1 )) | ||
| ERRORS+=" iomport:$nresource" | ||
| else | ||
| COUNT=$(( $COUNT + 1 )) | ||
| fi | ||
| echo ". resource: tf-state rm $resource" | ||
| $TFCOMMAND state rm "$resource" >> $LOGFILE | ||
| if [ $? != 0 ] | ||
| then | ||
| echo "* error removing resource $resource" | ||
| STATUS=$(( $STATUS + 1 )) | ||
| ERRORS+=" rm:$resource" | ||
| else | ||
| RMCOUNT=$(( $RMCOUNT + 1 )) | ||
| fi | ||
| done | ||
|
|
||
| if [ $COUNT == 0 ] | ||
| then | ||
| echo "* no resources to import" | ||
| else | ||
| echo "* imported $COUNT resources" | ||
| fi | ||
|
|
||
| if [ $RMCOUNT == 0 ] | ||
| then | ||
| echo "* no resources to remove" | ||
| else | ||
| echo "* removed $RMCOUNT resources" | ||
| fi | ||
|
|
||
| rm -f $FILE | ||
| if [ $STATUS == 0 ] | ||
| then | ||
| echo "* import complete" | ||
| exit 0 | ||
| else | ||
| echo "* some portion of import failed: $ERRORS" | ||
| exit 1 | ||
| fi |