Created
March 19, 2019 13:16
-
-
Save emulanob/b10ced32a207f09bf26279efc077f29d to your computer and use it in GitHub Desktop.
Terraform symlinks file that allows having a multi-environment repo
This file contains hidden or 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/sh | |
# | |
# SymLinks the environment's files to a separate file structure for each environments | |
# | |
# (initially done for terragrunt (see https://github.com/gruntwork-io/terragrunt#execute-terraform-commands-on-multiple-modules-at-once) | |
# but modified to include *all* tf files of an environment (to overcome the interpolation in provider issue with multiple accounts)) | |
# | |
# Must be run in terraform/stacks/ folder, will update/create links | |
# | |
# define colors | |
RED='\033[0;31m' | |
YELLOW='\033[0;33m' | |
GREEN='\033[0;32m' | |
LIGHTBLUE='\033[0;34m' | |
NC='\033[0m' # No Color | |
# where the symlinks live (parent folder) | |
LIVE='live' | |
#create base folder if not existing | |
echo "Creating main folder and symlinking modules" | |
echo "-------------------------------------------" | |
mkdir -p ../$LIVE | |
ln -sf ../modules ../$LIVE/modules | |
echo | |
# get all unique environment names from the tfvars. | |
echo "Creating environments subfolders under main folder:" | |
echo "---------------------------------------------------" | |
for i in `find . -name *.tfvars | rev | cut -d. -f2 | cut -f1 -d/ | rev | sort | uniq`; do | |
echo ${GREEN}$i${NC} | |
mkdir -p ../$LIVE/$i | |
#terragrunt ? not yet | |
#echo "terragrunt = {}" > ../$LIVE/$i/terraform.tfvars | |
done | |
echo | |
# traverse stack folders and symlinks envs | |
echo "Linking existing tfvars" | |
echo "-----------------------" | |
# looking for tfvars files (while avoiding folders with compiled stuff like .terraform or templates) | |
for i in `find . -name main.tf -not -path "*/.terraform/*" -not -path "*/_microservice_template/*"`; do | |
echo "${LIGHTBLUE}`dirname $i`${NC}" | |
# for each environment (should be just environments and a module symlink that we'll exclude) | |
for j in `ls ../$LIVE/ | grep -v "modules"`; do | |
# if there's an environment tfvars in that stack's folder | |
if [[ -f `dirname $i`/$j.tfvars ]] && [[ -f `dirname $i`/$j.backend ]]; then | |
# get the name of the path | |
DIRNAME=`dirname $i` # e.g. ./stacks/queue/static | |
ENVDIRNAME="../$LIVE/$j/${DIRNAME#./}" # eg ../live/gallactica/stacks/queue/static | |
# create folder structure in env if not existing | |
[ -d ${ENVDIRNAME} ] || mkdir -p ${ENVDIRNAME} | |
# change directory there | |
# get the right slashes and path for linking | |
PARENTPATH=$(echo "$LIVE/$j/${DIRNAME#./}"/ | sed -e "s|[^/]||g" -e "s|/|../|g") # e.g. ../../../../ | |
# go to stack's env's live folder and link | |
cd ${ENVDIRNAME} | |
# link the special guys (tfbars and 'interpolated' backend) | |
ln -sf "${PARENTPATH}stacks${DIRNAME#.}/$j.tfvars" "terraform.tfvars" | |
ln -sf "${PARENTPATH}stacks${DIRNAME#.}/$j.backend" "terraform.tf" | |
# link all other tf files | |
for file in ./${PARENTPATH}stacks${DIRNAME#.}/*.tf | |
do | |
if [ ! -d "$file" ]; then | |
ln -sf "$file" "$(basename "$file")" | |
fi | |
done | |
#echo "for tf in "${PARENTPATH}stacks${DIRNAME#.}/*.tf"; do ln -sf "$tf" "$(basename "$tf").tf"; done" | |
cd - > /dev/null # go back | |
echo "\t ${GREEN}linked${NC}\t `dirname $i`/${GREEN}$j${NC}.tfvars" | |
else | |
echo "\t ${RED}n/a${NC}\t `dirname $i`/${YELLOW}$j${NC}.tfvars" | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment