terragrunt-local 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #! /bin/bash
  2. findup ()
  3. {
  4. DIR='..'
  5. DIRTEST=$( cd $DIR ; pwd )
  6. while [[ "$DIRTEST" != "/" ]]; do
  7. DIR="$DIR/.."
  8. DIRTEST=$( cd $DIR ; pwd )
  9. if [[ -d "$DIR/$1" ]]; then
  10. echo "$DIR/$1"
  11. return 0
  12. fi
  13. done
  14. # Failed to find
  15. return 1
  16. }
  17. if [ ! -f terragrunt.hcl ]; then
  18. echo "ERROR: No terragrunt.hcl in this directory. Aborting."
  19. exit 1
  20. fi
  21. GITSOURCE=$( cat terragrunt.hcl | egrep -v '^ *#' | egrep 'source *= *"git@' )
  22. if [ "$GITSOURCE" == "" ]; then
  23. echo "ERROR: No source from Git detected. Aborting."
  24. exit 2
  25. fi
  26. # Strip off source = and quotes
  27. GITSOURCE=$( echo "$GITSOURCE" | sed -E 's/.*"([^"]+)".*/\1/' )
  28. # Strip off any ?ref= if it's there
  29. GITSOURCE=$( echo "$GITSOURCE" | sed -E 's/\?.*$//' )
  30. # Thanks to terragrunt needing that magic // construct
  31. GITSOURCE_REPO=$( echo "$GITSOURCE" | sed -E 's#//.*$##' )
  32. GITSOURCE_PATHINREPO=$( echo "$GITSOURCE" | sed -E "s#.*//##" )
  33. BARE_REPONAME=$( basename "$GITSOURCE_REPO" .git )
  34. LOCALPATH=$( findup "$BARE_REPONAME" )
  35. if [[ "$LOCALPATH" == "" ]]; then
  36. echo "ERROR: No $BARE_REPONAME in any parent directory"
  37. exit 1
  38. fi
  39. NEWPATH="$LOCALPATH//$GITSOURCE_PATHINREPO"
  40. echo Substituting \'$GITSOURCE\' with \'$NEWPATH\'
  41. if [ ! -f .terraform.lock.hcl ]; then
  42. echo "No providers lock (.terraform.lock.hcl) found in this directory. Creating..."
  43. terragrunt providers lock -platform=darwin_amd64 -platform=linux_amd64 -platform=windows_amd64 -platform=linux_arm64 --terragrunt-source $NEWPATH
  44. RC=$?
  45. if [[ $RC != 0 ]]; then
  46. read -p "ERROR: Failed to generate providers file. Apply anyway? [yN] " -n 1 -r
  47. echo ""
  48. if [[ ! $REPLY =~ ^[Yy]$ ]]
  49. then
  50. echo Exiting...
  51. exit 4
  52. fi
  53. fi
  54. fi
  55. # Test locally
  56. # the double // is intentional! Terragrunt uses this to determine the root of the modules repository.
  57. terragrunt $* --terragrunt-source $NEWPATH