Last active
November 14, 2022 12:24
-
-
Save dtelaroli/1ba3ca1409b1f62e4aa01643d4c5126e to your computer and use it in GitHub Desktop.
script which splits terraform states and adds depends_on to some resources
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
grep -E 'resource.+"codefresh_project"' ./squads/**/*.tf | sed -E 's/([a-z_\/]+):.+" "(.+)" {/\1:\2/g' | | |
while IFS=":"; read -r key value; do | |
export key=$key value=$value | |
cat $key | grep name | sed -E 's/.+name.+\"(.+)\"/\1/g' | | |
xargs -I{} grep -E 'project_name.+"{}"' ./squads/**/*.tf | sed -E 's/([a-z\/_-]+):.+/\1/g' > tmp.txt | |
while read file; do | |
if [ -z "$(grep depends_on $file)" ]; then | |
replace="\ndepends_on = [codefresh_project.${value}]\n}" | |
perl -i"" -pe "s/^}/${replace}/g" $file | |
terraform fmt $file | |
fi | |
done < tmp.txt | |
donet | |
rm tmp.txt |
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
# copia os arquivos de pipeline com o nome da squad | |
grep --exclude-dir=terraform/tf_modules "\"$value\"" terraform/**/*.tf | sed -E 's/([a-z\/_-]+):.+/\1/g' | xargs -I {} bash -c 'mv {} squads/$key/$(basename {})'; | |
# copia os arquivos de projeto com o nome da squad | |
grep -l project_name squads/$key/**.tf | xargs cat | grep project_name | sed -E 's/.*project_name.*"(.+)".*/\1/g' | \ | |
xargs -I {} bash -c 'mv terraform/codefresh_project/{}.tf squads/$key/$(basename {}).tf'; | |
# ajusta o nome da squad no state file | |
sed -i '' -e "s/#squad#/$key/" squads/$key/_state.tf; | |
sed -i '' -e "s/\.\./..\/..\/terraform/" squads/$key/*.tf; | |
done | |
for d in ./squads/* ; do | |
export d=${d} | |
# move o state dos recursos | |
grep -l resource ${d}/**.tf | xargs cat | grep resource | sed -E 's/.*resource.*"(.+)".*/\1/g' | \ | |
xargs -I {} bash -c 'terraform state mv -lock=false -state=terraform/source.tfstate -state-out=${d}/destination.tfstate module.codefresh_project.codefresh_project.{} codefresh_project.{}'; | |
# move o state dos módulos | |
grep -l module ${d}/**.tf | xargs -I {} bash -c 'cat {} | grep "module "' | sed -E 's/.*module "(.+)".*/\1/' | \ | |
xargs -I {} bash -c 'terraform state mv -lock=false -state=terraform/source.tfstate -state-out=${d}/destination.tfstate module.codefresh_pipeline.module.{} module.{}'; | |
# push do state novo | |
cd $d; terraform init; terraform state push -force destination.tfstate; cd -; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment