Last active
September 16, 2022 05:49
-
-
Save magodo/a1700ad2361f35944b925cecd433ec49 to your computer and use it in GitHub Desktop.
Setup and run aztfy
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
#!/bin/bash | |
# This script setup aztfy and run it in batch mode for terrafying a resource group. | |
# Afterwards, it will download the output files (incl. HCL and state file) to your local fs. | |
set -e | |
MYDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
MYNAME="$(basename "${BASH_SOURCE[0]}")" | |
die() { | |
echo "$@" >&2 | |
exit 1 | |
} | |
usage() { | |
cat << EOF | |
Usage: ./${MYNAME} [options] | |
(Azure CLI is needed to be installed and logged in) | |
Options: | |
-h|--help show this message | |
--subscription-id the subscription id used to run aztfy | |
--resource-group the resource group name to run aztfy | |
EOF | |
} | |
while :; do | |
case $1 in | |
-h|--help) | |
usage | |
exit 1 | |
;; | |
--subscription-id) | |
shift | |
subid=$1 | |
;; | |
--resource-group) | |
shift | |
rg=$1 | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
break | |
;; | |
esac | |
shift | |
done | |
[[ -z $subid ]] && die 'Option "--subscription-id" is not specified' | |
[[ -z $rg ]] && die 'Option "--resource-group" is not specified' | |
# Ensure aztfy with the expected version is installed | |
VERSION=v0.7.0 | |
AZTFY_INSTALL_DIR=$HOME/.aztfy.d | |
[[ -d $AZTFY_INSTALL_DIR ]] || mkdir $AZTFY_INSTALL_DIR | |
if [[ ! -x $AZTFY_INSTALL_DIR/aztfy ]] || ! grep -q ${VERSION} < <($AZTFY_INSTALL_DIR/aztfy -v) ; then | |
echo "Downloading aztfy ${VERSION}" | |
tmp_dir=$(mktemp -d) | |
wget "https://github.com/Azure/aztfy/releases/download/${VERSION}/aztfy_${VERSION}_linux_amd64.zip" -O ${tmp_dir}/aztfy.zip -q | |
unzip ${tmp_dir}/aztfy.zip -d $AZTFY_INSTALL_DIR > /dev/null | |
rm -r ${tmp_dir} | |
fi | |
# Setup azure cli | |
az account set -s $subid | |
# Run aztfy | |
outdir=$(mktemp -d) | |
${AZTFY_INSTALL_DIR}/aztfy rg --batch --output-dir $outdir $rg | |
# Package the result | |
outfile=$HOME/aztfy_result_$(date +%s).zip | |
pushd $outdir > /dev/null | |
zip -q $outfile main.tf provider.tf terraform.tfstate | |
popd > /dev/null | |
# Download is a Azure cloud shell script, which downloads stuff from cloud shell to your local fs. | |
download ${outfile} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment