apply_last_updated_modules 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. if [[ -f "../UNUSED.ACCOUNT" ]]; then
  34. echo $i is unused. Continuing.
  35. popd
  36. continue
  37. fi
  38. terragrunt init --upgrade && terragrunt apply
  39. EXITCODE=$?
  40. if [[ $EXITCODE != 0 ]]; then
  41. # Prompt to continue after each failed module.
  42. read -p "Terragrunt completed unsuccessfully. Continue to next module [Y/n]? " -n 1 -r
  43. echo ""
  44. if [[ $REPLY =~ ^[Nn]$ ]]
  45. then
  46. echo Exiting...
  47. exit 2
  48. fi
  49. fi
  50. popd
  51. done