apply_last_updated_modules 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #! /bin/bash
  2. #
  3. # Runs an init and apply in all copies of a module
  4. #
  5. # TODO: Would be really nice if this did some input validation, and if you
  6. # could specify 'local' or not.
  7. # Grab a list of the terragrunt.hcls that were updated in the last git commit,
  8. # but ignore the root terragrunt.hcl, and strip terragrunt.hcl from the end.
  9. UPDATED_MODULES=$( git diff --name-only HEAD HEAD~1 | grep terragrunt.hcl | egrep -v "^terragrunt.hcl" | sed "s#/terragrunt.hcl##" | sort -r )
  10. PWD=`pwd`
  11. BN=`basename $PWD`
  12. if [[ $BN != "xdr-terraform-live" ]]; then
  13. echo Must be run from xdr-terraform-live root.
  14. exit 1
  15. fi
  16. echo The following modules will be applied:
  17. for m in ${UPDATED_MODULES}; do
  18. printf '\t%s\n' $m
  19. done
  20. echo ""
  21. read -p "Continue [Y/n]? " -n 1 -r
  22. echo ""
  23. if [[ $REPLY =~ ^[Nn]$ ]]; then
  24. echo Exiting....
  25. exit 1
  26. fi
  27. for m in ${UPDATED_MODULES}; do
  28. echo ""
  29. echo =============================================
  30. echo \ Applying $m
  31. echo =============================================
  32. pushd $m
  33. terragrunt init --upgrade && terragrunt apply
  34. EXITCODE=$?
  35. if [[ $EXITCODE != 0 ]]; then
  36. # Prompt to continue after each failed module.
  37. read -p "Terragrunt completed unsuccessfully. Continue to next module [Y/n]? " -n 1 -r
  38. echo ""
  39. if [[ $REPLY =~ ^[Nn]$ ]]
  40. then
  41. echo Exiting...
  42. exit 2
  43. fi
  44. fi
  45. popd
  46. done